Convert a python number to a number field.
(n, digits=8, format=DEFAULT_FORMAT)
| 190 | return n |
| 191 | |
| 192 | def itn(n, digits=8, format=DEFAULT_FORMAT): |
| 193 | """Convert a python number to a number field. |
| 194 | """ |
| 195 | # POSIX 1003.1-1988 requires numbers to be encoded as a string of |
| 196 | # octal digits followed by a null-byte, this allows values up to |
| 197 | # (8**(digits-1))-1. GNU tar allows storing numbers greater than |
| 198 | # that if necessary. A leading 0o200 or 0o377 byte indicate this |
| 199 | # particular encoding, the following digits-1 bytes are a big-endian |
| 200 | # base-256 representation. This allows values up to (256**(digits-1))-1. |
| 201 | # A 0o200 byte indicates a positive number, a 0o377 byte a negative |
| 202 | # number. |
| 203 | n = int(n) |
| 204 | if 0 <= n < 8 ** (digits - 1): |
| 205 | s = bytes("%0*o" % (digits - 1, n), "ascii") + NUL |
| 206 | elif format == GNU_FORMAT and -256 ** (digits - 1) <= n < 256 ** (digits - 1): |
| 207 | if n >= 0: |
| 208 | s = bytearray([0o200]) |
| 209 | else: |
| 210 | s = bytearray([0o377]) |
| 211 | n = 256 ** digits + n |
| 212 | |
| 213 | for i in range(digits - 1): |
| 214 | s.insert(1, n & 0o377) |
| 215 | n >>= 8 |
| 216 | else: |
| 217 | raise ValueError("overflow in number field") |
| 218 | |
| 219 | return s |
| 220 | |
| 221 | def calc_chksums(buf): |
| 222 | """Calculate the checksum for a member's header by summing up all |