base64.nim 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. #
  2. #
  3. # Nim's Runtime Library
  4. # (c) Copyright 2010 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## This module implements a base64 encoder and decoder.
  10. ##
  11. ## Unstable API.
  12. ##
  13. ## Base64 is an encoding and decoding technique used to convert binary
  14. ## data to an ASCII string format.
  15. ## Each Base64 digit represents exactly 6 bits of data. Three 8-bit
  16. ## bytes (i.e., a total of 24 bits) can therefore be represented by
  17. ## four 6-bit Base64 digits.
  18. ##
  19. ## Basic usage
  20. ## ===========
  21. ##
  22. ## Encoding data
  23. ## -------------
  24. ##
  25. ## .. code-block::nim
  26. ## import base64
  27. ## let encoded = encode("Hello World")
  28. ## assert encoded == "SGVsbG8gV29ybGQ="
  29. ##
  30. ## Apart from strings you can also encode lists of integers or characters:
  31. ##
  32. ## .. code-block::nim
  33. ## import base64
  34. ## let encodedInts = encode([1,2,3])
  35. ## assert encodedInts == "AQID"
  36. ## let encodedChars = encode(['h','e','y'])
  37. ## assert encodedChars == "aGV5"
  38. ##
  39. ##
  40. ## Decoding data
  41. ## -------------
  42. ##
  43. ## .. code-block::nim
  44. ## import base64
  45. ## let decoded = decode("SGVsbG8gV29ybGQ=")
  46. ## assert decoded == "Hello World"
  47. ##
  48. ##
  49. ## See also
  50. ## ========
  51. ##
  52. ## * `hashes module<hashes.html>`_ for efficient computations of hash values for diverse Nim types
  53. ## * `md5 module<md5.html>`_ implements the MD5 checksum algorithm
  54. ## * `sha1 module<sha1.html>`_ implements a sha1 encoder and decoder
  55. const
  56. cb64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
  57. invalidChar = 255
  58. template encodeInternal(s: typed): untyped =
  59. ## encodes `s` into base64 representation.
  60. proc encodeSize(size: int): int =
  61. return (size * 4 div 3) + 6
  62. result.setLen(encodeSize(s.len))
  63. var
  64. inputIndex = 0
  65. outputIndex = 0
  66. inputEnds = s.len - s.len mod 3
  67. n: uint32
  68. b: uint32
  69. template inputByte(exp: untyped) =
  70. b = uint32(s[inputIndex])
  71. n = exp
  72. inc inputIndex
  73. template outputChar(x: untyped) =
  74. result[outputIndex] = cb64[x and 63]
  75. inc outputIndex
  76. template outputChar(c: char) =
  77. result[outputIndex] = c
  78. inc outputIndex
  79. while inputIndex != inputEnds:
  80. inputByte(b shl 16)
  81. inputByte(n or b shl 8)
  82. inputByte(n or b shl 0)
  83. outputChar(n shr 18)
  84. outputChar(n shr 12)
  85. outputChar(n shr 6)
  86. outputChar(n shr 0)
  87. var padding = s.len mod 3
  88. if padding == 1:
  89. inputByte(b shl 16)
  90. outputChar(n shr 18)
  91. outputChar(n shr 12)
  92. outputChar('=')
  93. outputChar('=')
  94. elif padding == 2:
  95. inputByte(b shl 16)
  96. inputByte(n or b shl 8)
  97. outputChar(n shr 18)
  98. outputChar(n shr 12)
  99. outputChar(n shr 6)
  100. outputChar('=')
  101. result.setLen(outputIndex)
  102. proc encode*[T: SomeInteger|char](s: openArray[T]): string =
  103. ## Encodes `s` into base64 representation.
  104. ##
  105. ## This procedure encodes an openarray (array or sequence) of either integers
  106. ## or characters.
  107. ##
  108. ## **See also:**
  109. ## * `encode proc<#encode,string>`_ for encoding a string
  110. ## * `decode proc<#decode,string>`_ for decoding a string
  111. runnableExamples:
  112. assert encode(['n', 'i', 'm']) == "bmlt"
  113. assert encode(@['n', 'i', 'm']) == "bmlt"
  114. assert encode([1, 2, 3, 4, 5]) == "AQIDBAU="
  115. encodeInternal(s)
  116. proc encode*(s: string): string =
  117. ## Encodes ``s`` into base64 representation.
  118. ##
  119. ## This procedure encodes a string.
  120. ##
  121. ## **See also:**
  122. ## * `encode proc<#encode,openArray[T]>`_ for encoding an openarray
  123. ## * `decode proc<#decode,string>`_ for decoding a string
  124. runnableExamples:
  125. assert encode("Hello World") == "SGVsbG8gV29ybGQ="
  126. encodeInternal(s)
  127. proc encodeMIME*(s: string, lineLen = 75, newLine = "\r\n"): string =
  128. ## Encodes ``s`` into base64 representation as lines.
  129. ## Used in email MIME forma, use ``lineLen`` and ``newline``.
  130. ##
  131. ## This procedure encodes a string according to MIME spec.
  132. ##
  133. ## **See also:**
  134. ## * `encode proc<#encode,string>`_ for encoding a string
  135. ## * `decode proc<#decode,string>`_ for decoding a string
  136. runnableExamples:
  137. assert encodeMIME("Hello World", 4, "\n") == "SGVs\nbG8g\nV29y\nbGQ="
  138. for i, c in encode(s):
  139. if i != 0 and (i mod lineLen == 0):
  140. result.add(newLine)
  141. result.add(c)
  142. proc initDecodeTable*(): array[256, char] =
  143. # computes a decode table at compile time
  144. for i in 0 ..< 256:
  145. let ch = char(i)
  146. var code = invalidChar
  147. if ch >= 'A' and ch <= 'Z': code = i - 0x00000041
  148. if ch >= 'a' and ch <= 'z': code = i - 0x00000047
  149. if ch >= '0' and ch <= '9': code = i + 0x00000004
  150. if ch == '+' or ch == '-': code = 0x0000003E
  151. if ch == '/' or ch == '_': code = 0x0000003F
  152. result[i] = char(code)
  153. const
  154. decodeTable = initDecodeTable()
  155. proc decode*(s: string): string =
  156. ## Decodes string ``s`` in base64 representation back into its original form.
  157. ## The initial whitespace is skipped.
  158. ##
  159. ## **See also:**
  160. ## * `encode proc<#encode,openArray[T]>`_ for encoding an openarray
  161. ## * `encode proc<#encode,string>`_ for encoding a string
  162. runnableExamples:
  163. assert decode("SGVsbG8gV29ybGQ=") == "Hello World"
  164. assert decode(" SGVsbG8gV29ybGQ=") == "Hello World"
  165. if s.len == 0: return
  166. proc decodeSize(size: int): int =
  167. return (size * 3 div 4) + 6
  168. template inputChar(x: untyped) =
  169. let x = int decodeTable[ord(s[inputIndex])]
  170. inc inputIndex
  171. if x == invalidChar:
  172. raise newException(ValueError,
  173. "Invalid base64 format character `" & s[inputIndex] &
  174. "` (ord " & $s[inputIndex].ord & ") at location " & $inputIndex & ".")
  175. template outputChar(x: untyped) =
  176. result[outputIndex] = char(x and 255)
  177. inc outputIndex
  178. # pre allocate output string once
  179. result.setLen(decodeSize(s.len))
  180. var
  181. inputIndex = 0
  182. outputIndex = 0
  183. inputLen = s.len
  184. inputEnds = 0
  185. # strip trailing characters
  186. while s[inputLen - 1] in {'\n', '\r', ' ', '='}:
  187. dec inputLen
  188. # hot loop: read 4 characters at at time
  189. inputEnds = inputLen - 4
  190. while inputIndex <= inputEnds:
  191. while s[inputIndex] in {'\n', '\r', ' '}:
  192. inc inputIndex
  193. inputChar(a)
  194. inputChar(b)
  195. inputChar(c)
  196. inputChar(d)
  197. outputChar(a shl 2 or b shr 4)
  198. outputChar(b shl 4 or c shr 2)
  199. outputChar(c shl 6 or d shr 0)
  200. # do the last 2 or 3 characters
  201. var leftLen = abs((inputIndex - inputLen) mod 4)
  202. if leftLen == 2:
  203. inputChar(a)
  204. inputChar(b)
  205. outputChar(a shl 2 or b shr 4)
  206. elif leftLen == 3:
  207. inputChar(a)
  208. inputChar(b)
  209. inputChar(c)
  210. outputChar(a shl 2 or b shr 4)
  211. outputChar(b shl 4 or c shr 2)
  212. result.setLen(outputIndex)