base64.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. #! /usr/bin/env python3
  2. """Base16, Base32, Base64 (RFC 3548), Base85 and Ascii85 data encodings"""
  3. # Modified 04-Oct-1995 by Jack Jansen to use binascii module
  4. # Modified 30-Dec-2003 by Barry Warsaw to add full RFC 3548 support
  5. # Modified 22-May-2007 by Guido van Rossum to use bytes everywhere
  6. import re
  7. import struct
  8. import binascii
  9. __all__ = [
  10. # Legacy interface exports traditional RFC 2045 Base64 encodings
  11. 'encode', 'decode', 'encodebytes', 'decodebytes',
  12. # Generalized interface for other encodings
  13. 'b64encode', 'b64decode', 'b32encode', 'b32decode',
  14. 'b32hexencode', 'b32hexdecode', 'b16encode', 'b16decode',
  15. # Base85 and Ascii85 encodings
  16. 'b85encode', 'b85decode', 'a85encode', 'a85decode',
  17. # Standard Base64 encoding
  18. 'standard_b64encode', 'standard_b64decode',
  19. # Some common Base64 alternatives. As referenced by RFC 3458, see thread
  20. # starting at:
  21. #
  22. # http://zgp.org/pipermail/p2p-hackers/2001-September/000316.html
  23. 'urlsafe_b64encode', 'urlsafe_b64decode',
  24. ]
  25. bytes_types = (bytes, bytearray) # Types acceptable as binary data
  26. def _bytes_from_decode_data(s):
  27. if isinstance(s, str):
  28. try:
  29. return s.encode('ascii')
  30. except UnicodeEncodeError:
  31. raise ValueError('string argument should contain only ASCII characters')
  32. if isinstance(s, bytes_types):
  33. return s
  34. try:
  35. return memoryview(s).tobytes()
  36. except TypeError:
  37. raise TypeError("argument should be a bytes-like object or ASCII "
  38. "string, not %r" % s.__class__.__name__) from None
  39. # Base64 encoding/decoding uses binascii
  40. def b64encode(s, altchars=None):
  41. """Encode the bytes-like object s using Base64 and return a bytes object.
  42. Optional altchars should be a byte string of length 2 which specifies an
  43. alternative alphabet for the '+' and '/' characters. This allows an
  44. application to e.g. generate url or filesystem safe Base64 strings.
  45. """
  46. encoded = binascii.b2a_base64(s, newline=False)
  47. if altchars is not None:
  48. assert len(altchars) == 2, repr(altchars)
  49. return encoded.translate(bytes.maketrans(b'+/', altchars))
  50. return encoded
  51. def b64decode(s, altchars=None, validate=False):
  52. """Decode the Base64 encoded bytes-like object or ASCII string s.
  53. Optional altchars must be a bytes-like object or ASCII string of length 2
  54. which specifies the alternative alphabet used instead of the '+' and '/'
  55. characters.
  56. The result is returned as a bytes object. A binascii.Error is raised if
  57. s is incorrectly padded.
  58. If validate is False (the default), characters that are neither in the
  59. normal base-64 alphabet nor the alternative alphabet are discarded prior
  60. to the padding check. If validate is True, these non-alphabet characters
  61. in the input result in a binascii.Error.
  62. For more information about the strict base64 check, see:
  63. https://docs.python.org/3.11/library/binascii.html#binascii.a2b_base64
  64. """
  65. s = _bytes_from_decode_data(s)
  66. if altchars is not None:
  67. altchars = _bytes_from_decode_data(altchars)
  68. assert len(altchars) == 2, repr(altchars)
  69. s = s.translate(bytes.maketrans(altchars, b'+/'))
  70. return binascii.a2b_base64(s, strict_mode=validate)
  71. def standard_b64encode(s):
  72. """Encode bytes-like object s using the standard Base64 alphabet.
  73. The result is returned as a bytes object.
  74. """
  75. return b64encode(s)
  76. def standard_b64decode(s):
  77. """Decode bytes encoded with the standard Base64 alphabet.
  78. Argument s is a bytes-like object or ASCII string to decode. The result
  79. is returned as a bytes object. A binascii.Error is raised if the input
  80. is incorrectly padded. Characters that are not in the standard alphabet
  81. are discarded prior to the padding check.
  82. """
  83. return b64decode(s)
  84. _urlsafe_encode_translation = bytes.maketrans(b'+/', b'-_')
  85. _urlsafe_decode_translation = bytes.maketrans(b'-_', b'+/')
  86. def urlsafe_b64encode(s):
  87. """Encode bytes using the URL- and filesystem-safe Base64 alphabet.
  88. Argument s is a bytes-like object to encode. The result is returned as a
  89. bytes object. The alphabet uses '-' instead of '+' and '_' instead of
  90. '/'.
  91. """
  92. return b64encode(s).translate(_urlsafe_encode_translation)
  93. def urlsafe_b64decode(s):
  94. """Decode bytes using the URL- and filesystem-safe Base64 alphabet.
  95. Argument s is a bytes-like object or ASCII string to decode. The result
  96. is returned as a bytes object. A binascii.Error is raised if the input
  97. is incorrectly padded. Characters that are not in the URL-safe base-64
  98. alphabet, and are not a plus '+' or slash '/', are discarded prior to the
  99. padding check.
  100. The alphabet uses '-' instead of '+' and '_' instead of '/'.
  101. """
  102. s = _bytes_from_decode_data(s)
  103. s = s.translate(_urlsafe_decode_translation)
  104. return b64decode(s)
  105. # Base32 encoding/decoding must be done in Python
  106. _B32_ENCODE_DOCSTRING = '''
  107. Encode the bytes-like objects using {encoding} and return a bytes object.
  108. '''
  109. _B32_DECODE_DOCSTRING = '''
  110. Decode the {encoding} encoded bytes-like object or ASCII string s.
  111. Optional casefold is a flag specifying whether a lowercase alphabet is
  112. acceptable as input. For security purposes, the default is False.
  113. {extra_args}
  114. The result is returned as a bytes object. A binascii.Error is raised if
  115. the input is incorrectly padded or if there are non-alphabet
  116. characters present in the input.
  117. '''
  118. _B32_DECODE_MAP01_DOCSTRING = '''
  119. RFC 3548 allows for optional mapping of the digit 0 (zero) to the
  120. letter O (oh), and for optional mapping of the digit 1 (one) to
  121. either the letter I (eye) or letter L (el). The optional argument
  122. map01 when not None, specifies which letter the digit 1 should be
  123. mapped to (when map01 is not None, the digit 0 is always mapped to
  124. the letter O). For security purposes the default is None, so that
  125. 0 and 1 are not allowed in the input.
  126. '''
  127. _b32alphabet = b'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567'
  128. _b32hexalphabet = b'0123456789ABCDEFGHIJKLMNOPQRSTUV'
  129. _b32tab2 = {}
  130. _b32rev = {}
  131. def _b32encode(alphabet, s):
  132. global _b32tab2
  133. # Delay the initialization of the table to not waste memory
  134. # if the function is never called
  135. if alphabet not in _b32tab2:
  136. b32tab = [bytes((i,)) for i in alphabet]
  137. _b32tab2[alphabet] = [a + b for a in b32tab for b in b32tab]
  138. b32tab = None
  139. if not isinstance(s, bytes_types):
  140. s = memoryview(s).tobytes()
  141. leftover = len(s) % 5
  142. # Pad the last quantum with zero bits if necessary
  143. if leftover:
  144. s = s + b'\0' * (5 - leftover) # Don't use += !
  145. encoded = bytearray()
  146. from_bytes = int.from_bytes
  147. b32tab2 = _b32tab2[alphabet]
  148. for i in range(0, len(s), 5):
  149. c = from_bytes(s[i: i + 5]) # big endian
  150. encoded += (b32tab2[c >> 30] + # bits 1 - 10
  151. b32tab2[(c >> 20) & 0x3ff] + # bits 11 - 20
  152. b32tab2[(c >> 10) & 0x3ff] + # bits 21 - 30
  153. b32tab2[c & 0x3ff] # bits 31 - 40
  154. )
  155. # Adjust for any leftover partial quanta
  156. if leftover == 1:
  157. encoded[-6:] = b'======'
  158. elif leftover == 2:
  159. encoded[-4:] = b'===='
  160. elif leftover == 3:
  161. encoded[-3:] = b'==='
  162. elif leftover == 4:
  163. encoded[-1:] = b'='
  164. return bytes(encoded)
  165. def _b32decode(alphabet, s, casefold=False, map01=None):
  166. global _b32rev
  167. # Delay the initialization of the table to not waste memory
  168. # if the function is never called
  169. if alphabet not in _b32rev:
  170. _b32rev[alphabet] = {v: k for k, v in enumerate(alphabet)}
  171. s = _bytes_from_decode_data(s)
  172. if len(s) % 8:
  173. raise binascii.Error('Incorrect padding')
  174. # Handle section 2.4 zero and one mapping. The flag map01 will be either
  175. # False, or the character to map the digit 1 (one) to. It should be
  176. # either L (el) or I (eye).
  177. if map01 is not None:
  178. map01 = _bytes_from_decode_data(map01)
  179. assert len(map01) == 1, repr(map01)
  180. s = s.translate(bytes.maketrans(b'01', b'O' + map01))
  181. if casefold:
  182. s = s.upper()
  183. # Strip off pad characters from the right. We need to count the pad
  184. # characters because this will tell us how many null bytes to remove from
  185. # the end of the decoded string.
  186. l = len(s)
  187. s = s.rstrip(b'=')
  188. padchars = l - len(s)
  189. # Now decode the full quanta
  190. decoded = bytearray()
  191. b32rev = _b32rev[alphabet]
  192. for i in range(0, len(s), 8):
  193. quanta = s[i: i + 8]
  194. acc = 0
  195. try:
  196. for c in quanta:
  197. acc = (acc << 5) + b32rev[c]
  198. except KeyError:
  199. raise binascii.Error('Non-base32 digit found') from None
  200. decoded += acc.to_bytes(5) # big endian
  201. # Process the last, partial quanta
  202. if l % 8 or padchars not in {0, 1, 3, 4, 6}:
  203. raise binascii.Error('Incorrect padding')
  204. if padchars and decoded:
  205. acc <<= 5 * padchars
  206. last = acc.to_bytes(5) # big endian
  207. leftover = (43 - 5 * padchars) // 8 # 1: 4, 3: 3, 4: 2, 6: 1
  208. decoded[-5:] = last[:leftover]
  209. return bytes(decoded)
  210. def b32encode(s):
  211. return _b32encode(_b32alphabet, s)
  212. b32encode.__doc__ = _B32_ENCODE_DOCSTRING.format(encoding='base32')
  213. def b32decode(s, casefold=False, map01=None):
  214. return _b32decode(_b32alphabet, s, casefold, map01)
  215. b32decode.__doc__ = _B32_DECODE_DOCSTRING.format(encoding='base32',
  216. extra_args=_B32_DECODE_MAP01_DOCSTRING)
  217. def b32hexencode(s):
  218. return _b32encode(_b32hexalphabet, s)
  219. b32hexencode.__doc__ = _B32_ENCODE_DOCSTRING.format(encoding='base32hex')
  220. def b32hexdecode(s, casefold=False):
  221. # base32hex does not have the 01 mapping
  222. return _b32decode(_b32hexalphabet, s, casefold)
  223. b32hexdecode.__doc__ = _B32_DECODE_DOCSTRING.format(encoding='base32hex',
  224. extra_args='')
  225. # RFC 3548, Base 16 Alphabet specifies uppercase, but hexlify() returns
  226. # lowercase. The RFC also recommends against accepting input case
  227. # insensitively.
  228. def b16encode(s):
  229. """Encode the bytes-like object s using Base16 and return a bytes object.
  230. """
  231. return binascii.hexlify(s).upper()
  232. def b16decode(s, casefold=False):
  233. """Decode the Base16 encoded bytes-like object or ASCII string s.
  234. Optional casefold is a flag specifying whether a lowercase alphabet is
  235. acceptable as input. For security purposes, the default is False.
  236. The result is returned as a bytes object. A binascii.Error is raised if
  237. s is incorrectly padded or if there are non-alphabet characters present
  238. in the input.
  239. """
  240. s = _bytes_from_decode_data(s)
  241. if casefold:
  242. s = s.upper()
  243. if re.search(b'[^0-9A-F]', s):
  244. raise binascii.Error('Non-base16 digit found')
  245. return binascii.unhexlify(s)
  246. #
  247. # Ascii85 encoding/decoding
  248. #
  249. _a85chars = None
  250. _a85chars2 = None
  251. _A85START = b"<~"
  252. _A85END = b"~>"
  253. def _85encode(b, chars, chars2, pad=False, foldnuls=False, foldspaces=False):
  254. # Helper function for a85encode and b85encode
  255. if not isinstance(b, bytes_types):
  256. b = memoryview(b).tobytes()
  257. padding = (-len(b)) % 4
  258. if padding:
  259. b = b + b'\0' * padding
  260. words = struct.Struct('!%dI' % (len(b) // 4)).unpack(b)
  261. chunks = [b'z' if foldnuls and not word else
  262. b'y' if foldspaces and word == 0x20202020 else
  263. (chars2[word // 614125] +
  264. chars2[word // 85 % 7225] +
  265. chars[word % 85])
  266. for word in words]
  267. if padding and not pad:
  268. if chunks[-1] == b'z':
  269. chunks[-1] = chars[0] * 5
  270. chunks[-1] = chunks[-1][:-padding]
  271. return b''.join(chunks)
  272. def a85encode(b, *, foldspaces=False, wrapcol=0, pad=False, adobe=False):
  273. """Encode bytes-like object b using Ascii85 and return a bytes object.
  274. foldspaces is an optional flag that uses the special short sequence 'y'
  275. instead of 4 consecutive spaces (ASCII 0x20) as supported by 'btoa'. This
  276. feature is not supported by the "standard" Adobe encoding.
  277. wrapcol controls whether the output should have newline (b'\\n') characters
  278. added to it. If this is non-zero, each output line will be at most this
  279. many characters long, excluding the trailing newline.
  280. pad controls whether the input is padded to a multiple of 4 before
  281. encoding. Note that the btoa implementation always pads.
  282. adobe controls whether the encoded byte sequence is framed with <~ and ~>,
  283. which is used by the Adobe implementation.
  284. """
  285. global _a85chars, _a85chars2
  286. # Delay the initialization of tables to not waste memory
  287. # if the function is never called
  288. if _a85chars2 is None:
  289. _a85chars = [bytes((i,)) for i in range(33, 118)]
  290. _a85chars2 = [(a + b) for a in _a85chars for b in _a85chars]
  291. result = _85encode(b, _a85chars, _a85chars2, pad, True, foldspaces)
  292. if adobe:
  293. result = _A85START + result
  294. if wrapcol:
  295. wrapcol = max(2 if adobe else 1, wrapcol)
  296. chunks = [result[i: i + wrapcol]
  297. for i in range(0, len(result), wrapcol)]
  298. if adobe:
  299. if len(chunks[-1]) + 2 > wrapcol:
  300. chunks.append(b'')
  301. result = b'\n'.join(chunks)
  302. if adobe:
  303. result += _A85END
  304. return result
  305. def a85decode(b, *, foldspaces=False, adobe=False, ignorechars=b' \t\n\r\v'):
  306. """Decode the Ascii85 encoded bytes-like object or ASCII string b.
  307. foldspaces is a flag that specifies whether the 'y' short sequence should be
  308. accepted as shorthand for 4 consecutive spaces (ASCII 0x20). This feature is
  309. not supported by the "standard" Adobe encoding.
  310. adobe controls whether the input sequence is in Adobe Ascii85 format (i.e.
  311. is framed with <~ and ~>).
  312. ignorechars should be a byte string containing characters to ignore from the
  313. input. This should only contain whitespace characters, and by default
  314. contains all whitespace characters in ASCII.
  315. The result is returned as a bytes object.
  316. """
  317. b = _bytes_from_decode_data(b)
  318. if adobe:
  319. if not b.endswith(_A85END):
  320. raise ValueError(
  321. "Ascii85 encoded byte sequences must end "
  322. "with {!r}".format(_A85END)
  323. )
  324. if b.startswith(_A85START):
  325. b = b[2:-2] # Strip off start/end markers
  326. else:
  327. b = b[:-2]
  328. #
  329. # We have to go through this stepwise, so as to ignore spaces and handle
  330. # special short sequences
  331. #
  332. packI = struct.Struct('!I').pack
  333. decoded = []
  334. decoded_append = decoded.append
  335. curr = []
  336. curr_append = curr.append
  337. curr_clear = curr.clear
  338. for x in b + b'u' * 4:
  339. if b'!'[0] <= x <= b'u'[0]:
  340. curr_append(x)
  341. if len(curr) == 5:
  342. acc = 0
  343. for x in curr:
  344. acc = 85 * acc + (x - 33)
  345. try:
  346. decoded_append(packI(acc))
  347. except struct.error:
  348. raise ValueError('Ascii85 overflow') from None
  349. curr_clear()
  350. elif x == b'z'[0]:
  351. if curr:
  352. raise ValueError('z inside Ascii85 5-tuple')
  353. decoded_append(b'\0\0\0\0')
  354. elif foldspaces and x == b'y'[0]:
  355. if curr:
  356. raise ValueError('y inside Ascii85 5-tuple')
  357. decoded_append(b'\x20\x20\x20\x20')
  358. elif x in ignorechars:
  359. # Skip whitespace
  360. continue
  361. else:
  362. raise ValueError('Non-Ascii85 digit found: %c' % x)
  363. result = b''.join(decoded)
  364. padding = 4 - len(curr)
  365. if padding:
  366. # Throw away the extra padding
  367. result = result[:-padding]
  368. return result
  369. # The following code is originally taken (with permission) from Mercurial
  370. _b85alphabet = (b"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  371. b"abcdefghijklmnopqrstuvwxyz!#$%&()*+-;<=>?@^_`{|}~")
  372. _b85chars = None
  373. _b85chars2 = None
  374. _b85dec = None
  375. def b85encode(b, pad=False):
  376. """Encode bytes-like object b in base85 format and return a bytes object.
  377. If pad is true, the input is padded with b'\\0' so its length is a multiple of
  378. 4 bytes before encoding.
  379. """
  380. global _b85chars, _b85chars2
  381. # Delay the initialization of tables to not waste memory
  382. # if the function is never called
  383. if _b85chars2 is None:
  384. _b85chars = [bytes((i,)) for i in _b85alphabet]
  385. _b85chars2 = [(a + b) for a in _b85chars for b in _b85chars]
  386. return _85encode(b, _b85chars, _b85chars2, pad)
  387. def b85decode(b):
  388. """Decode the base85-encoded bytes-like object or ASCII string b
  389. The result is returned as a bytes object.
  390. """
  391. global _b85dec
  392. # Delay the initialization of tables to not waste memory
  393. # if the function is never called
  394. if _b85dec is None:
  395. _b85dec = [None] * 256
  396. for i, c in enumerate(_b85alphabet):
  397. _b85dec[c] = i
  398. b = _bytes_from_decode_data(b)
  399. padding = (-len(b)) % 5
  400. b = b + b'~' * padding
  401. out = []
  402. packI = struct.Struct('!I').pack
  403. for i in range(0, len(b), 5):
  404. chunk = b[i:i + 5]
  405. acc = 0
  406. try:
  407. for c in chunk:
  408. acc = acc * 85 + _b85dec[c]
  409. except TypeError:
  410. for j, c in enumerate(chunk):
  411. if _b85dec[c] is None:
  412. raise ValueError('bad base85 character at position %d'
  413. % (i + j)) from None
  414. raise
  415. try:
  416. out.append(packI(acc))
  417. except struct.error:
  418. raise ValueError('base85 overflow in hunk starting at byte %d'
  419. % i) from None
  420. result = b''.join(out)
  421. if padding:
  422. result = result[:-padding]
  423. return result
  424. # Legacy interface. This code could be cleaned up since I don't believe
  425. # binascii has any line length limitations. It just doesn't seem worth it
  426. # though. The files should be opened in binary mode.
  427. MAXLINESIZE = 76 # Excluding the CRLF
  428. MAXBINSIZE = (MAXLINESIZE//4)*3
  429. def encode(input, output):
  430. """Encode a file; input and output are binary files."""
  431. while s := input.read(MAXBINSIZE):
  432. while len(s) < MAXBINSIZE and (ns := input.read(MAXBINSIZE-len(s))):
  433. s += ns
  434. line = binascii.b2a_base64(s)
  435. output.write(line)
  436. def decode(input, output):
  437. """Decode a file; input and output are binary files."""
  438. while line := input.readline():
  439. s = binascii.a2b_base64(line)
  440. output.write(s)
  441. def _input_type_check(s):
  442. try:
  443. m = memoryview(s)
  444. except TypeError as err:
  445. msg = "expected bytes-like object, not %s" % s.__class__.__name__
  446. raise TypeError(msg) from err
  447. if m.format not in ('c', 'b', 'B'):
  448. msg = ("expected single byte elements, not %r from %s" %
  449. (m.format, s.__class__.__name__))
  450. raise TypeError(msg)
  451. if m.ndim != 1:
  452. msg = ("expected 1-D data, not %d-D data from %s" %
  453. (m.ndim, s.__class__.__name__))
  454. raise TypeError(msg)
  455. def encodebytes(s):
  456. """Encode a bytestring into a bytes object containing multiple lines
  457. of base-64 data."""
  458. _input_type_check(s)
  459. pieces = []
  460. for i in range(0, len(s), MAXBINSIZE):
  461. chunk = s[i : i + MAXBINSIZE]
  462. pieces.append(binascii.b2a_base64(chunk))
  463. return b"".join(pieces)
  464. def decodebytes(s):
  465. """Decode a bytestring of base-64 data into a bytes object."""
  466. _input_type_check(s)
  467. return binascii.a2b_base64(s)
  468. # Usable as a script...
  469. def main():
  470. """Small main program"""
  471. import sys, getopt
  472. usage = f"""usage: {sys.argv[0]} [-h|-d|-e|-u] [file|-]
  473. -h: print this help message and exit
  474. -d, -u: decode
  475. -e: encode (default)"""
  476. try:
  477. opts, args = getopt.getopt(sys.argv[1:], 'hdeu')
  478. except getopt.error as msg:
  479. sys.stdout = sys.stderr
  480. print(msg)
  481. print(usage)
  482. sys.exit(2)
  483. func = encode
  484. for o, a in opts:
  485. if o == '-e': func = encode
  486. if o == '-d': func = decode
  487. if o == '-u': func = decode
  488. if o == '-h': print(usage); return
  489. if args and args[0] != '-':
  490. with open(args[0], 'rb') as f:
  491. func(f, sys.stdout.buffer)
  492. else:
  493. func(sys.stdin.buffer, sys.stdout.buffer)
  494. if __name__ == '__main__':
  495. main()