Fred Drake | 295da24 | 1998-08-10 19:42:37 +0000 | [diff] [blame] | 1 | \section{\module{binascii} --- |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 2 | Convert between binary and \ASCII} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 3 | |
Fred Drake | ffbe687 | 1999-04-22 21:23:22 +0000 | [diff] [blame] | 4 | \declaremodule{builtin}{binascii} |
Fred Drake | 2d62395 | 1998-08-07 16:00:30 +0000 | [diff] [blame] | 5 | \modulesynopsis{Tools for converting between binary and various |
Fred Drake | c116b82 | 2001-05-09 15:50:17 +0000 | [diff] [blame] | 6 | \ASCII-encoded binary representations.} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 7 | |
Jack Jansen | 4549b13 | 1995-08-29 11:30:24 +0000 | [diff] [blame] | 8 | |
Fred Drake | 930f134 | 1998-04-03 03:44:56 +0000 | [diff] [blame] | 9 | The \module{binascii} module contains a number of methods to convert |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 10 | between binary and various \ASCII-encoded binary |
Fred Drake | ffbe687 | 1999-04-22 21:23:22 +0000 | [diff] [blame] | 11 | representations. Normally, you will not use these functions directly |
| 12 | but use wrapper modules like \refmodule{uu}\refstmodindex{uu} or |
Fred Drake | ee4d54e | 1999-04-23 15:42:36 +0000 | [diff] [blame] | 13 | \refmodule{binhex}\refstmodindex{binhex} instead, this module solely |
Thomas Wouters | f831663 | 2000-07-16 19:01:10 +0000 | [diff] [blame] | 14 | exists because bit-manipulation of large amounts of data is slow in |
Fred Drake | 930f134 | 1998-04-03 03:44:56 +0000 | [diff] [blame] | 15 | Python. |
Jack Jansen | 4549b13 | 1995-08-29 11:30:24 +0000 | [diff] [blame] | 16 | |
Fred Drake | 930f134 | 1998-04-03 03:44:56 +0000 | [diff] [blame] | 17 | The \module{binascii} module defines the following functions: |
Jack Jansen | 4549b13 | 1995-08-29 11:30:24 +0000 | [diff] [blame] | 18 | |
| 19 | \begin{funcdesc}{a2b_uu}{string} |
| 20 | Convert a single line of uuencoded data back to binary and return the |
| 21 | binary data. Lines normally contain 45 (binary) bytes, except for the |
| 22 | last line. Line data may be followed by whitespace. |
| 23 | \end{funcdesc} |
| 24 | |
| 25 | \begin{funcdesc}{b2a_uu}{data} |
Fred Drake | 930f134 | 1998-04-03 03:44:56 +0000 | [diff] [blame] | 26 | Convert binary data to a line of \ASCII{} characters, the return value |
| 27 | is the converted line, including a newline char. The length of |
| 28 | \var{data} should be at most 45. |
Jack Jansen | 4549b13 | 1995-08-29 11:30:24 +0000 | [diff] [blame] | 29 | \end{funcdesc} |
| 30 | |
Jack Jansen | 06cf5d0 | 1995-10-10 14:41:03 +0000 | [diff] [blame] | 31 | \begin{funcdesc}{a2b_base64}{string} |
| 32 | Convert a block of base64 data back to binary and return the |
| 33 | binary data. More than one line may be passed at a time. |
| 34 | \end{funcdesc} |
| 35 | |
| 36 | \begin{funcdesc}{b2a_base64}{data} |
Fred Drake | 930f134 | 1998-04-03 03:44:56 +0000 | [diff] [blame] | 37 | Convert binary data to a line of \ASCII{} characters in base64 coding. |
Jack Jansen | 06cf5d0 | 1995-10-10 14:41:03 +0000 | [diff] [blame] | 38 | The return value is the converted line, including a newline char. |
| 39 | The length of \var{data} should be at most 57 to adhere to the base64 |
| 40 | standard. |
| 41 | \end{funcdesc} |
| 42 | |
Martin v. Löwis | 16dc7f4 | 2001-09-30 20:32:11 +0000 | [diff] [blame] | 43 | \begin{funcdesc}{a2b_qp}{string\optional{, header}} |
| 44 | Convert a block of quoted-printable data back to binary and return the |
| 45 | binary data. More than one line may be passed at a time. |
| 46 | If the optional argument \var{header} is present and true, underscores |
| 47 | will be decoded as spaces. |
| 48 | \end{funcdesc} |
| 49 | |
| 50 | \begin{funcdesc}{b2a_qp}{data\optional{, quotetabs, istext, header}} |
| 51 | Convert binary data to a line(s) of \ASCII{} characters in |
| 52 | quoted-printable encoding. The return value is the converted line(s). |
| 53 | If the optional argument \var{quotetabs} is present and true, all tabs |
Andrew M. Kuchling | ba7d95e | 2005-08-12 14:02:32 +0000 | [diff] [blame] | 54 | and spaces will be encoded. |
| 55 | If the optional argument \var{istext} is present and true, |
| 56 | newlines are not encoded but trailing whitespace will be encoded. |
| 57 | If the optional argument \var{header} is |
Martin v. Löwis | 16dc7f4 | 2001-09-30 20:32:11 +0000 | [diff] [blame] | 58 | present and true, spaces will be encoded as underscores per RFC1522. |
| 59 | If the optional argument \var{header} is present and false, newline |
Andrew M. Kuchling | ba7d95e | 2005-08-12 14:02:32 +0000 | [diff] [blame] | 60 | characters will be encoded as well; otherwise linefeed conversion might |
Martin v. Löwis | 16dc7f4 | 2001-09-30 20:32:11 +0000 | [diff] [blame] | 61 | corrupt the binary data stream. |
| 62 | \end{funcdesc} |
| 63 | |
Jack Jansen | 4549b13 | 1995-08-29 11:30:24 +0000 | [diff] [blame] | 64 | \begin{funcdesc}{a2b_hqx}{string} |
Fred Drake | 930f134 | 1998-04-03 03:44:56 +0000 | [diff] [blame] | 65 | Convert binhex4 formatted \ASCII{} data to binary, without doing |
| 66 | RLE-decompression. The string should contain a complete number of |
Jack Jansen | 4549b13 | 1995-08-29 11:30:24 +0000 | [diff] [blame] | 67 | binary bytes, or (in case of the last portion of the binhex4 data) |
| 68 | have the remaining bits zero. |
| 69 | \end{funcdesc} |
| 70 | |
| 71 | \begin{funcdesc}{rledecode_hqx}{data} |
| 72 | Perform RLE-decompression on the data, as per the binhex4 |
| 73 | standard. The algorithm uses \code{0x90} after a byte as a repeat |
| 74 | indicator, followed by a count. A count of \code{0} specifies a byte |
| 75 | value of \code{0x90}. The routine returns the decompressed data, |
| 76 | unless data input data ends in an orphaned repeat indicator, in which |
Fred Drake | 930f134 | 1998-04-03 03:44:56 +0000 | [diff] [blame] | 77 | case the \exception{Incomplete} exception is raised. |
Jack Jansen | 4549b13 | 1995-08-29 11:30:24 +0000 | [diff] [blame] | 78 | \end{funcdesc} |
| 79 | |
| 80 | \begin{funcdesc}{rlecode_hqx}{data} |
| 81 | Perform binhex4 style RLE-compression on \var{data} and return the |
| 82 | result. |
| 83 | \end{funcdesc} |
| 84 | |
| 85 | \begin{funcdesc}{b2a_hqx}{data} |
Fred Drake | 930f134 | 1998-04-03 03:44:56 +0000 | [diff] [blame] | 86 | Perform hexbin4 binary-to-\ASCII{} translation and return the |
| 87 | resulting string. The argument should already be RLE-coded, and have a |
| 88 | length divisible by 3 (except possibly the last fragment). |
Jack Jansen | 4549b13 | 1995-08-29 11:30:24 +0000 | [diff] [blame] | 89 | \end{funcdesc} |
| 90 | |
| 91 | \begin{funcdesc}{crc_hqx}{data, crc} |
| 92 | Compute the binhex4 crc value of \var{data}, starting with an initial |
| 93 | \var{crc} and returning the result. |
| 94 | \end{funcdesc} |
Guido van Rossum | 8006d31 | 2000-02-16 21:13:37 +0000 | [diff] [blame] | 95 | |
| 96 | \begin{funcdesc}{crc32}{data\optional{, crc}} |
| 97 | Compute CRC-32, the 32-bit checksum of data, starting with an initial |
Fred Drake | 327798c | 2001-10-15 13:45:49 +0000 | [diff] [blame] | 98 | crc. This is consistent with the ZIP file checksum. Since the |
| 99 | algorithm is designed for use as a checksum algorithm, it is not |
| 100 | suitable for use as a general hash algorithm. Use as follows: |
Guido van Rossum | 8006d31 | 2000-02-16 21:13:37 +0000 | [diff] [blame] | 101 | \begin{verbatim} |
| 102 | print binascii.crc32("hello world") |
| 103 | # Or, in two pieces: |
| 104 | crc = binascii.crc32("hello") |
| 105 | crc = binascii.crc32(" world", crc) |
| 106 | print crc |
| 107 | \end{verbatim} |
| 108 | \end{funcdesc} |
Jack Jansen | 4549b13 | 1995-08-29 11:30:24 +0000 | [diff] [blame] | 109 | |
Barry Warsaw | 0be4346 | 2000-08-15 06:08:00 +0000 | [diff] [blame] | 110 | \begin{funcdesc}{b2a_hex}{data} |
Fred Drake | d066f6d | 2000-08-15 17:47:09 +0000 | [diff] [blame] | 111 | \funcline{hexlify}{data} |
Barry Warsaw | 0be4346 | 2000-08-15 06:08:00 +0000 | [diff] [blame] | 112 | Return the hexadecimal representation of the binary \var{data}. Every |
| 113 | byte of \var{data} is converted into the corresponding 2-digit hex |
Fred Drake | d066f6d | 2000-08-15 17:47:09 +0000 | [diff] [blame] | 114 | representation. The resulting string is therefore twice as long as |
| 115 | the length of \var{data}. |
Barry Warsaw | 0be4346 | 2000-08-15 06:08:00 +0000 | [diff] [blame] | 116 | \end{funcdesc} |
| 117 | |
| 118 | \begin{funcdesc}{a2b_hex}{hexstr} |
Fred Drake | d066f6d | 2000-08-15 17:47:09 +0000 | [diff] [blame] | 119 | \funcline{unhexlify}{hexstr} |
Barry Warsaw | 0be4346 | 2000-08-15 06:08:00 +0000 | [diff] [blame] | 120 | Return the binary data represented by the hexadecimal string |
| 121 | \var{hexstr}. This function is the inverse of \function{b2a_hex()}. |
| 122 | \var{hexstr} must contain an even number of hexadecimal digits (which |
| 123 | can be upper or lower case), otherwise a \exception{TypeError} is |
Fred Drake | d066f6d | 2000-08-15 17:47:09 +0000 | [diff] [blame] | 124 | raised. |
| 125 | \end{funcdesc} |
Barry Warsaw | 0be4346 | 2000-08-15 06:08:00 +0000 | [diff] [blame] | 126 | |
Jack Jansen | 4549b13 | 1995-08-29 11:30:24 +0000 | [diff] [blame] | 127 | \begin{excdesc}{Error} |
| 128 | Exception raised on errors. These are usually programming errors. |
| 129 | \end{excdesc} |
| 130 | |
| 131 | \begin{excdesc}{Incomplete} |
| 132 | Exception raised on incomplete data. These are usually not programming |
Fred Drake | 930f134 | 1998-04-03 03:44:56 +0000 | [diff] [blame] | 133 | errors, but may be handled by reading a little more data and trying |
| 134 | again. |
Jack Jansen | 4549b13 | 1995-08-29 11:30:24 +0000 | [diff] [blame] | 135 | \end{excdesc} |
Fred Drake | ee4d54e | 1999-04-23 15:42:36 +0000 | [diff] [blame] | 136 | |
| 137 | |
| 138 | \begin{seealso} |
Fred Drake | d066f6d | 2000-08-15 17:47:09 +0000 | [diff] [blame] | 139 | \seemodule{base64}{Support for base64 encoding used in MIME email messages.} |
Fred Drake | ee4d54e | 1999-04-23 15:42:36 +0000 | [diff] [blame] | 140 | |
Fred Drake | d066f6d | 2000-08-15 17:47:09 +0000 | [diff] [blame] | 141 | \seemodule{binhex}{Support for the binhex format used on the Macintosh.} |
Fred Drake | ee4d54e | 1999-04-23 15:42:36 +0000 | [diff] [blame] | 142 | |
Fred Drake | d066f6d | 2000-08-15 17:47:09 +0000 | [diff] [blame] | 143 | \seemodule{uu}{Support for UU encoding used on \UNIX.} |
Martin v. Löwis | 16dc7f4 | 2001-09-30 20:32:11 +0000 | [diff] [blame] | 144 | |
| 145 | \seemodule{quopri}{Support for quoted-printable encoding used in MIME email messages. } |
Fred Drake | ee4d54e | 1999-04-23 15:42:36 +0000 | [diff] [blame] | 146 | \end{seealso} |