base64.scm 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. ;; -*- mode: scheme; coding: utf-8 -*-
  2. ;;
  3. ;; This module was renamed from (weinholt text base64 (1 0 20100612)) to
  4. ;; (guix base64) by Nikita Karetnikov <nikita@karetnikov.org> on
  5. ;; February 12, 2014. It was later renamed to (gcrypt base64) by
  6. ;; Christopher Allan Webber <cwebber@dustycloud.org> on May 20, 2017.
  7. ;;
  8. ;; Some optimizations made by Ludovic Courtès <ludo@gnu.org>, 2015.
  9. ;; Turned into a Guile module (instead of R6RS).
  10. ;;
  11. ;; This program is free software: you can redistribute it and/or modify
  12. ;; it under the terms of the GNU General Public License as published by
  13. ;; the Free Software Foundation, either version 3 of the License, or
  14. ;; (at your option) any later version.
  15. ;;
  16. ;; This program is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;;
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. ;;
  24. ;; This file incorporates work covered by the following copyright and
  25. ;; permission notice:
  26. ;;
  27. ;; Copyright © 2009, 2010, 2012, 2013, 2018 Göran Weinholt <goran@weinholt.se>
  28. ;;
  29. ;; Permission is hereby granted, free of charge, to any person obtaining a
  30. ;; copy of this software and associated documentation files (the "Software"),
  31. ;; to deal in the Software without restriction, including without limitation
  32. ;; the rights to use, copy, modify, merge, publish, distribute, sublicense,
  33. ;; and/or sell copies of the Software, and to permit persons to whom the
  34. ;; Software is furnished to do so, subject to the following conditions:
  35. ;;
  36. ;; The above copyright notice and this permission notice shall be included in
  37. ;; all copies or substantial portions of the Software.
  38. ;;
  39. ;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  40. ;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  41. ;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  42. ;; THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  43. ;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  44. ;; FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  45. ;; DEALINGS IN THE SOFTWARE.
  46. ;; RFC 4648 Base-N Encodings
  47. (define-module (gcrypt base64)
  48. #:export (base64-encode
  49. base64-decode
  50. base64-alphabet
  51. base64url-alphabet
  52. get-delimited-base64
  53. put-delimited-base64)
  54. #:use-module (srfi srfi-11)
  55. #:use-module (srfi srfi-60)
  56. #:use-module (rnrs bytevectors)
  57. #:use-module (rnrs io ports))
  58. (define-syntax define-alias
  59. (syntax-rules ()
  60. ((_ new old)
  61. (define-syntax new (identifier-syntax old)))))
  62. ;; Force the use of Guile's own primitives to avoid the overhead of its 'fx'
  63. ;; procedures.
  64. (define-alias fxbit-field bit-field)
  65. (define-alias fxarithmetic-shift ash)
  66. (define-alias fxarithmetic-shift-left ash)
  67. (define-alias fxand logand)
  68. (define-alias fxior logior)
  69. (define-alias fxxor logxor)
  70. (define-alias fx=? =)
  71. (define-alias fx<=? <=)
  72. (define-alias fxzero? zero?)
  73. (define-alias fx+ +)
  74. (define-alias fx- -)
  75. (define-alias fxmod modulo)
  76. (define-alias mod modulo)
  77. (define-syntax-rule (assert exp)
  78. (unless exp
  79. (throw 'assertion-failure 'exp)))
  80. (define base64-alphabet
  81. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/")
  82. (define base64url-alphabet
  83. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_")
  84. (define base64-encode
  85. (case-lambda
  86. ;; Simple interface. Returns a string containing the canonical
  87. ;; base64 representation of the given bytevector.
  88. ((bv)
  89. (base64-encode bv 0 (bytevector-length bv) #f #f base64-alphabet #f))
  90. ((bv start)
  91. (base64-encode bv start (bytevector-length bv) #f #f base64-alphabet #f))
  92. ((bv start end)
  93. (base64-encode bv start end #f #f base64-alphabet #f))
  94. ((bv start end line-length)
  95. (base64-encode bv start end line-length #f base64-alphabet #f))
  96. ((bv start end line-length no-padding)
  97. (base64-encode bv start end line-length no-padding base64-alphabet #f))
  98. ((bv start end line-length no-padding alphabet)
  99. (base64-encode bv start end line-length no-padding alphabet #f))
  100. ;; Base64 encodes the bytes [start,end[ in the given bytevector.
  101. ;; Lines are limited to line-length characters (unless #f),
  102. ;; which must be a multiple of four. To omit the padding
  103. ;; characters (#\=) set no-padding to a true value. If port is
  104. ;; #f, returns a string.
  105. ((bv start end line-length no-padding alphabet port)
  106. (assert (or (not line-length) (zero? (mod line-length 4))))
  107. (let-values (((p extract) (if port
  108. (values port (lambda () (values)))
  109. (open-string-output-port))))
  110. (letrec ((put (if line-length
  111. (let ((chars 0))
  112. (lambda (p c)
  113. (when (fx=? chars line-length)
  114. (set! chars 0)
  115. (put-char p #\linefeed))
  116. (set! chars (fx+ chars 1))
  117. (put-char p c)))
  118. put-char)))
  119. (let lp ((i start))
  120. (cond ((= i end))
  121. ((<= (+ i 3) end)
  122. (let ((x (bytevector-uint-ref bv i (endianness big) 3)))
  123. (put p (string-ref alphabet (fxbit-field x 18 24)))
  124. (put p (string-ref alphabet (fxbit-field x 12 18)))
  125. (put p (string-ref alphabet (fxbit-field x 6 12)))
  126. (put p (string-ref alphabet (fxbit-field x 0 6)))
  127. (lp (+ i 3))))
  128. ((<= (+ i 2) end)
  129. (let ((x (fxarithmetic-shift-left (bytevector-u16-ref bv i (endianness big)) 8)))
  130. (put p (string-ref alphabet (fxbit-field x 18 24)))
  131. (put p (string-ref alphabet (fxbit-field x 12 18)))
  132. (put p (string-ref alphabet (fxbit-field x 6 12)))
  133. (unless no-padding
  134. (put p #\=))))
  135. (else
  136. (let ((x (fxarithmetic-shift-left (bytevector-u8-ref bv i) 16)))
  137. (put p (string-ref alphabet (fxbit-field x 18 24)))
  138. (put p (string-ref alphabet (fxbit-field x 12 18)))
  139. (unless no-padding
  140. (put p #\=)
  141. (put p #\=)))))))
  142. (extract)))))
  143. ;; Create a lookup table for the alphabet and remember the latest table.
  144. (define get-decode-table
  145. (let ((ascii-table #f)
  146. (extra-table '()) ;in the unlikely case of unicode chars
  147. (table-alphabet #f))
  148. (lambda (alphabet)
  149. (unless (eq? alphabet table-alphabet)
  150. ;; Rebuild the table.
  151. (do ((ascii (make-vector 128 #f))
  152. (extra '())
  153. (i 0 (+ i 1)))
  154. ((= i (string-length alphabet))
  155. (set! ascii-table ascii)
  156. (set! extra-table extra))
  157. (let ((c (char->integer (string-ref alphabet i))))
  158. (if (fx<=? c 127)
  159. (vector-set! ascii c i)
  160. (set! extra (cons (cons c i) extra)))))
  161. (set! table-alphabet alphabet))
  162. (values ascii-table extra-table))))
  163. ;; Decodes a base64 string, optionally ignoring non-alphabet
  164. ;; characters and lack of padding.
  165. (define base64-decode
  166. (case-lambda
  167. ((str)
  168. (base64-decode str base64-alphabet #f))
  169. ((str alphabet)
  170. (base64-decode str alphabet #f))
  171. ((str alphabet port)
  172. (base64-decode str alphabet port #t))
  173. ((str alphabet port strict?)
  174. (base64-decode str alphabet port strict? #t))
  175. ((str alphabet port strict? strict-padding?)
  176. (define (pad? c) (eqv? c (char->integer #\=)))
  177. (let-values (((p extract) (if port
  178. (values port (lambda () (values)))
  179. (open-bytevector-output-port)))
  180. ((ascii extra) (get-decode-table alphabet)))
  181. (define-syntax lookup
  182. (syntax-rules ()
  183. ((_ c) (or (and (fx<=? c 127) (vector-ref ascii c))
  184. (cond ((assv c extra) => cdr)
  185. (else #f))))))
  186. (let lp-restart ((str str))
  187. (let* ((len (if strict?
  188. (string-length str)
  189. (let lp ((i (fx- (string-length str) 1)))
  190. ;; Skip trailing invalid chars.
  191. (cond ((fxzero? i) 0)
  192. ((let ((c (char->integer (string-ref str i))))
  193. (or (lookup c) (pad? c)))
  194. (fx+ i 1))
  195. (else (lp (fx- i 1))))))))
  196. (let lp ((i 0))
  197. (cond
  198. ((fx=? i len)
  199. (extract))
  200. ((fx<=? i (fx- len 4))
  201. (let lp* ((c1 (char->integer (string-ref str i)))
  202. (c2 (char->integer (string-ref str (fx+ i 1))))
  203. (c3 (char->integer (string-ref str (fx+ i 2))))
  204. (c4 (char->integer (string-ref str (fx+ i 3))))
  205. (i i))
  206. (let ((i1 (lookup c1)) (i2 (lookup c2))
  207. (i3 (lookup c3)) (i4 (lookup c4)))
  208. (cond
  209. ((and i1 i2 i3 i4)
  210. ;; All characters present and accounted for.
  211. ;; The most common case.
  212. (let ((x (fxior (fxarithmetic-shift-left i1 18)
  213. (fxarithmetic-shift-left i2 12)
  214. (fxarithmetic-shift-left i3 6)
  215. i4)))
  216. (put-u8 p (fxbit-field x 16 24))
  217. (put-u8 p (fxbit-field x 8 16))
  218. (put-u8 p (fxbit-field x 0 8))
  219. (lp (fx+ i 4))))
  220. ((and i1 i2 i3 (pad? c4) (= i (- len 4)))
  221. ;; One padding character at the end of the input.
  222. (let ((x (fxior (fxarithmetic-shift-left i1 18)
  223. (fxarithmetic-shift-left i2 12)
  224. (fxarithmetic-shift-left i3 6))))
  225. (put-u8 p (fxbit-field x 16 24))
  226. (put-u8 p (fxbit-field x 8 16))
  227. (lp (fx+ i 4))))
  228. ((and i1 i2 (pad? c3) (pad? c4) (= i (- len 4)))
  229. ;; Two padding characters.
  230. (let ((x (fxior (fxarithmetic-shift-left i1 18)
  231. (fxarithmetic-shift-left i2 12))))
  232. (put-u8 p (fxbit-field x 16 24))
  233. (lp (fx+ i 4))))
  234. ((not strict?)
  235. ;; Non-alphabet characters.
  236. (let lp ((i i) (c* '()) (n 4))
  237. (cond ((fxzero? n)
  238. ;; Found four valid characters.
  239. (lp* (cadddr c*) (caddr c*) (cadr c*) (car c*)
  240. (fx- i 4)))
  241. ((fx=? i len)
  242. (error 'base64-decode
  243. "Invalid input in non-strict mode."
  244. i c*))
  245. (else
  246. ;; Gather alphabetic (or valid
  247. ;; padding) characters.
  248. (let ((c (char->integer (string-ref str i))))
  249. (cond ((or (lookup c)
  250. (and (pad? c)
  251. (fx<=? n 2)
  252. (fx=? i (fx- len n))))
  253. (lp (fx+ i 1) (cons c c*) (fx- n 1)))
  254. (else
  255. (lp (fx+ i 1) c* n))))))))
  256. (else
  257. (error 'base64-decode
  258. "Invalid input in strict mode."
  259. c1 c2 c3 c4))))))
  260. ((not strict-padding?)
  261. ;; Append an appropriate amount of padding after the
  262. ;; remaining characters.
  263. (if (<= 2 (- len i) 3)
  264. (lp-restart (string-append (substring str i (string-length str))
  265. (if (= (- len i) 2) "==" "=")))
  266. (error 'base64-decode "The input is too short." i)))
  267. (else
  268. (error 'base64-decode
  269. "The input is too short, it may be missing padding."
  270. i))))))))))
  271. (define (get-line-comp f port)
  272. (if (port-eof? port)
  273. (eof-object)
  274. (f (get-line port))))
  275. ;; Reads the common -----BEGIN/END type----- delimited format from
  276. ;; the given port. Returns two values: a string with the type and a
  277. ;; bytevector containing the base64 decoded data. The second value
  278. ;; is the eof object if there is an eof before the BEGIN delimiter.
  279. (define get-delimited-base64
  280. (case-lambda
  281. ((port)
  282. (get-delimited-base64 port #t))
  283. ((port strict)
  284. (define (get-first-data-line port)
  285. ;; Some MIME data has header fields in the same format as mail
  286. ;; or http. These are ignored.
  287. (let ((line (get-line-comp string-trim-both port)))
  288. (cond ((eof-object? line) line)
  289. ((string-index line #\:)
  290. (let lp () ;read until empty line
  291. (let ((line (get-line-comp string-trim-both port)))
  292. (if (string=? line "")
  293. (get-line-comp string-trim-both port)
  294. (lp)))))
  295. (else line))))
  296. (let ((line (get-line-comp string-trim-both port)))
  297. (cond ((eof-object? line)
  298. (values "" (eof-object)))
  299. ((string=? line "")
  300. (get-delimited-base64 port))
  301. ((and (string-prefix? "-----BEGIN " line)
  302. (string-suffix? "-----" line))
  303. (let* ((type (substring line 11 (- (string-length line) 5)))
  304. (endline (string-append "-----END " type "-----")))
  305. (let-values ([(outp extract) (open-bytevector-output-port)])
  306. (let lp ((previous "") (line (get-first-data-line port)))
  307. (cond ((eof-object? line)
  308. (error 'get-delimited-base64
  309. "unexpected end of file"))
  310. ((string-prefix? "-" line)
  311. (unless (string=? line endline)
  312. (error 'get-delimited-base64
  313. "bad end delimiter" type line))
  314. (values type (extract)))
  315. ((and (= (string-length line) 5)
  316. (string-prefix? "=" line))
  317. ;; Skip Radix-64 checksum
  318. (lp previous (get-line-comp string-trim-both port)))
  319. ((not (fxzero? (fxmod (fx+ (string-length previous)
  320. (string-length line))
  321. 4)))
  322. ;; OpenSSH outputs lines with a bad length
  323. (lp (string-append previous line)
  324. (get-line-comp string-trim-both port)))
  325. (else
  326. (base64-decode (string-append previous line) base64-alphabet outp)
  327. (lp "" (get-line-comp string-trim-both port))))))))
  328. (else ;skip garbage (like in openssl x509 -in foo -text output).
  329. (get-delimited-base64 port)))))))
  330. (define put-delimited-base64
  331. (case-lambda
  332. ((port type bv line-length)
  333. (display (string-append "-----BEGIN " type "-----\n") port)
  334. (base64-encode bv 0 (bytevector-length bv)
  335. line-length #f base64-alphabet port)
  336. (display (string-append "\n-----END " type "-----\n") port))
  337. ((port type bv)
  338. (put-delimited-base64 port type bv 76))))