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