blob: b244b105718f1bf0669b9dbb3f3f17f34659aa70 [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{binascii} ---
Fred Drakec37b65e2001-11-28 07:26:15 +00002 Convert between binary and \ASCII}
Fred Drakeb91e9341998-07-23 17:59:49 +00003
Fred Drakeffbe6871999-04-22 21:23:22 +00004\declaremodule{builtin}{binascii}
Fred Drake2d623951998-08-07 16:00:30 +00005\modulesynopsis{Tools for converting between binary and various
Fred Drakec116b822001-05-09 15:50:17 +00006 \ASCII-encoded binary representations.}
Fred Drakeb91e9341998-07-23 17:59:49 +00007
Jack Jansen4549b131995-08-29 11:30:24 +00008
Fred Drake930f1341998-04-03 03:44:56 +00009The \module{binascii} module contains a number of methods to convert
Fred Drakec37b65e2001-11-28 07:26:15 +000010between binary and various \ASCII-encoded binary
Fred Drakeffbe6871999-04-22 21:23:22 +000011representations. Normally, you will not use these functions directly
12but use wrapper modules like \refmodule{uu}\refstmodindex{uu} or
Fred Drakeee4d54e1999-04-23 15:42:36 +000013\refmodule{binhex}\refstmodindex{binhex} instead, this module solely
Thomas Woutersf8316632000-07-16 19:01:10 +000014exists because bit-manipulation of large amounts of data is slow in
Fred Drake930f1341998-04-03 03:44:56 +000015Python.
Jack Jansen4549b131995-08-29 11:30:24 +000016
Fred Drake930f1341998-04-03 03:44:56 +000017The \module{binascii} module defines the following functions:
Jack Jansen4549b131995-08-29 11:30:24 +000018
19\begin{funcdesc}{a2b_uu}{string}
20Convert a single line of uuencoded data back to binary and return the
21binary data. Lines normally contain 45 (binary) bytes, except for the
22last line. Line data may be followed by whitespace.
23\end{funcdesc}
24
25\begin{funcdesc}{b2a_uu}{data}
Fred Drake930f1341998-04-03 03:44:56 +000026Convert binary data to a line of \ASCII{} characters, the return value
27is the converted line, including a newline char. The length of
28\var{data} should be at most 45.
Jack Jansen4549b131995-08-29 11:30:24 +000029\end{funcdesc}
30
Jack Jansen06cf5d01995-10-10 14:41:03 +000031\begin{funcdesc}{a2b_base64}{string}
32Convert a block of base64 data back to binary and return the
33binary data. More than one line may be passed at a time.
34\end{funcdesc}
35
36\begin{funcdesc}{b2a_base64}{data}
Fred Drake930f1341998-04-03 03:44:56 +000037Convert binary data to a line of \ASCII{} characters in base64 coding.
Jack Jansen06cf5d01995-10-10 14:41:03 +000038The return value is the converted line, including a newline char.
39The length of \var{data} should be at most 57 to adhere to the base64
40standard.
41\end{funcdesc}
42
Martin v. Löwis16dc7f42001-09-30 20:32:11 +000043\begin{funcdesc}{a2b_qp}{string\optional{, header}}
44Convert a block of quoted-printable data back to binary and return the
45binary data. More than one line may be passed at a time.
46If the optional argument \var{header} is present and true, underscores
47will be decoded as spaces.
48\end{funcdesc}
49
50\begin{funcdesc}{b2a_qp}{data\optional{, quotetabs, istext, header}}
51Convert binary data to a line(s) of \ASCII{} characters in
52quoted-printable encoding. The return value is the converted line(s).
53If the optional argument \var{quotetabs} is present and true, all tabs
Andrew M. Kuchlingba7d95e2005-08-12 14:02:32 +000054and spaces will be encoded.
55If the optional argument \var{istext} is present and true,
56newlines are not encoded but trailing whitespace will be encoded.
57If the optional argument \var{header} is
Martin v. Löwis16dc7f42001-09-30 20:32:11 +000058present and true, spaces will be encoded as underscores per RFC1522.
59If the optional argument \var{header} is present and false, newline
Andrew M. Kuchlingba7d95e2005-08-12 14:02:32 +000060characters will be encoded as well; otherwise linefeed conversion might
Martin v. Löwis16dc7f42001-09-30 20:32:11 +000061corrupt the binary data stream.
62\end{funcdesc}
63
Jack Jansen4549b131995-08-29 11:30:24 +000064\begin{funcdesc}{a2b_hqx}{string}
Fred Drake930f1341998-04-03 03:44:56 +000065Convert binhex4 formatted \ASCII{} data to binary, without doing
66RLE-decompression. The string should contain a complete number of
Jack Jansen4549b131995-08-29 11:30:24 +000067binary bytes, or (in case of the last portion of the binhex4 data)
68have the remaining bits zero.
69\end{funcdesc}
70
71\begin{funcdesc}{rledecode_hqx}{data}
72Perform RLE-decompression on the data, as per the binhex4
73standard. The algorithm uses \code{0x90} after a byte as a repeat
74indicator, followed by a count. A count of \code{0} specifies a byte
75value of \code{0x90}. The routine returns the decompressed data,
76unless data input data ends in an orphaned repeat indicator, in which
Fred Drake930f1341998-04-03 03:44:56 +000077case the \exception{Incomplete} exception is raised.
Jack Jansen4549b131995-08-29 11:30:24 +000078\end{funcdesc}
79
80\begin{funcdesc}{rlecode_hqx}{data}
81Perform binhex4 style RLE-compression on \var{data} and return the
82result.
83\end{funcdesc}
84
85\begin{funcdesc}{b2a_hqx}{data}
Fred Drake930f1341998-04-03 03:44:56 +000086Perform hexbin4 binary-to-\ASCII{} translation and return the
87resulting string. The argument should already be RLE-coded, and have a
88length divisible by 3 (except possibly the last fragment).
Jack Jansen4549b131995-08-29 11:30:24 +000089\end{funcdesc}
90
91\begin{funcdesc}{crc_hqx}{data, crc}
92Compute the binhex4 crc value of \var{data}, starting with an initial
93\var{crc} and returning the result.
94\end{funcdesc}
Guido van Rossum8006d312000-02-16 21:13:37 +000095
96\begin{funcdesc}{crc32}{data\optional{, crc}}
97Compute CRC-32, the 32-bit checksum of data, starting with an initial
Fred Drake327798c2001-10-15 13:45:49 +000098crc. This is consistent with the ZIP file checksum. Since the
99algorithm is designed for use as a checksum algorithm, it is not
100suitable for use as a general hash algorithm. Use as follows:
Guido van Rossum8006d312000-02-16 21:13:37 +0000101\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 Jansen4549b131995-08-29 11:30:24 +0000109
Barry Warsaw0be43462000-08-15 06:08:00 +0000110\begin{funcdesc}{b2a_hex}{data}
Fred Draked066f6d2000-08-15 17:47:09 +0000111\funcline{hexlify}{data}
Barry Warsaw0be43462000-08-15 06:08:00 +0000112Return the hexadecimal representation of the binary \var{data}. Every
113byte of \var{data} is converted into the corresponding 2-digit hex
Fred Draked066f6d2000-08-15 17:47:09 +0000114representation. The resulting string is therefore twice as long as
115the length of \var{data}.
Barry Warsaw0be43462000-08-15 06:08:00 +0000116\end{funcdesc}
117
118\begin{funcdesc}{a2b_hex}{hexstr}
Fred Draked066f6d2000-08-15 17:47:09 +0000119\funcline{unhexlify}{hexstr}
Barry Warsaw0be43462000-08-15 06:08:00 +0000120Return 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
123can be upper or lower case), otherwise a \exception{TypeError} is
Fred Draked066f6d2000-08-15 17:47:09 +0000124raised.
125\end{funcdesc}
Barry Warsaw0be43462000-08-15 06:08:00 +0000126
Jack Jansen4549b131995-08-29 11:30:24 +0000127\begin{excdesc}{Error}
128Exception raised on errors. These are usually programming errors.
129\end{excdesc}
130
131\begin{excdesc}{Incomplete}
132Exception raised on incomplete data. These are usually not programming
Fred Drake930f1341998-04-03 03:44:56 +0000133errors, but may be handled by reading a little more data and trying
134again.
Jack Jansen4549b131995-08-29 11:30:24 +0000135\end{excdesc}
Fred Drakeee4d54e1999-04-23 15:42:36 +0000136
137
138\begin{seealso}
Fred Draked066f6d2000-08-15 17:47:09 +0000139 \seemodule{base64}{Support for base64 encoding used in MIME email messages.}
Fred Drakeee4d54e1999-04-23 15:42:36 +0000140
Fred Draked066f6d2000-08-15 17:47:09 +0000141 \seemodule{binhex}{Support for the binhex format used on the Macintosh.}
Fred Drakeee4d54e1999-04-23 15:42:36 +0000142
Fred Draked066f6d2000-08-15 17:47:09 +0000143 \seemodule{uu}{Support for UU encoding used on \UNIX.}
Martin v. Löwis16dc7f42001-09-30 20:32:11 +0000144
145 \seemodule{quopri}{Support for quoted-printable encoding used in MIME email messages. }
Fred Drakeee4d54e1999-04-23 15:42:36 +0000146\end{seealso}