base32.scm 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2012, 2015, 2017, 2021 Ludovic Courtès <ludo@gnu.org>
  3. ;;;
  4. ;;; This file is part of GNU Guix.
  5. ;;;
  6. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  7. ;;; under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation; either version 3 of the License, or (at
  9. ;;; your option) any later version.
  10. ;;;
  11. ;;; GNU Guix is distributed in the hope that it will be useful, but
  12. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;;; GNU General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  18. (define-module (guix base32)
  19. #:use-module (srfi srfi-34)
  20. #:use-module (srfi srfi-35)
  21. #:use-module (srfi srfi-60)
  22. #:use-module (rnrs bytevectors)
  23. #:use-module (ice-9 vlist)
  24. #:export (bytevector-quintet-length
  25. bytevector->base32-string
  26. bytevector->nix-base32-string
  27. base32-string->bytevector
  28. nix-base32-string->bytevector
  29. %nix-base32-charset
  30. %rfc4648-base32-charset
  31. &invalid-base32-character
  32. invalid-base32-character?
  33. invalid-base32-character-value
  34. invalid-base32-character-string))
  35. ;;; Commentary:
  36. ;;;
  37. ;;; A generic, customizable to convert bytevectors to/from a base32
  38. ;;; representation.
  39. ;;;
  40. ;;; Code:
  41. (define-syntax bit-field
  42. (lambda (s)
  43. ;; This inline version of 'bit-field' assumes that START and END are
  44. ;; literals and pre-computes the mask. In an ideal world, using 'define'
  45. ;; or 'define-inlinable' would be enough, but as of 3.0.7, peval doesn't
  46. ;; expand calls to 'expt' (and 'bit-field' is a subr.)
  47. (syntax-case s ()
  48. ((_ n start end)
  49. (let* ((s (syntax->datum #'start))
  50. (e (syntax->datum #'end))
  51. (mask (- (expt 2 (- e s)) 1)))
  52. ;; The baseline compiler in Guile <= 3.0.7 miscompiles (ash x N) as
  53. ;; (ash x (- N)) when N is a literal: <https://bugs.gnu.org/50696>.
  54. ;; Here we take advantage of another bug in the baseline compiler,
  55. ;; fixed in Guile commit 330c6ea83f492672578b62d0683acbb532d1a5d9: we
  56. ;; introduce 'minus-start' such that it has a different source
  57. ;; location, which in turn means that the baseline compiler pattern
  58. ;; for (ash x N) doesn't match, thus avoiding the bug (!).
  59. (with-syntax ((minus-start (datum->syntax #'start (- s))))
  60. #`(logand (ash n minus-start) #,mask)))))))
  61. (define bytevector-quintet-ref
  62. (let* ((ref bytevector-u8-ref)
  63. (ref+ (lambda (bv offset)
  64. (let ((o (+ 1 offset)))
  65. (if (>= o (bytevector-length bv))
  66. 0
  67. (bytevector-u8-ref bv o)))))
  68. (ref0 (lambda (bv offset)
  69. (bit-field (ref bv offset) 3 8)))
  70. (ref1 (lambda (bv offset)
  71. (logior (ash (bit-field (ref bv offset) 0 3) 2)
  72. (bit-field (ref+ bv offset) 6 8))))
  73. (ref2 (lambda (bv offset)
  74. (bit-field (ref bv offset) 1 6)))
  75. (ref3 (lambda (bv offset)
  76. (logior (ash (bit-field (ref bv offset) 0 1) 4)
  77. (bit-field (ref+ bv offset) 4 8))))
  78. (ref4 (lambda (bv offset)
  79. (logior (ash (bit-field (ref bv offset) 0 4) 1)
  80. (bit-field (ref+ bv offset) 7 8))))
  81. (ref5 (lambda (bv offset)
  82. (bit-field (ref bv offset) 2 7)))
  83. (ref6 (lambda (bv offset)
  84. (logior (ash (bit-field (ref bv offset) 0 2) 3)
  85. (bit-field (ref+ bv offset) 5 8))))
  86. (ref7 (lambda (bv offset)
  87. (bit-field (ref bv offset) 0 5)))
  88. (refs (vector ref0 ref1 ref2 ref3 ref4 ref5 ref6 ref7)))
  89. (lambda (bv index)
  90. "Return the INDEXth quintet of BV."
  91. (let ((p (vector-ref refs (modulo index 8))))
  92. (p bv (quotient (* index 5) 8))))))
  93. (define bytevector-quintet-ref-right
  94. (let* ((ref bytevector-u8-ref)
  95. (ref+ (lambda (bv offset)
  96. (let ((o (+ 1 offset)))
  97. (if (>= o (bytevector-length bv))
  98. 0
  99. (bytevector-u8-ref bv o)))))
  100. (ref0 (lambda (bv offset)
  101. (bit-field (ref bv offset) 0 5)))
  102. (ref1 (lambda (bv offset)
  103. (logior (bit-field (ref bv offset) 5 8)
  104. (ash (bit-field (ref+ bv offset) 0 2) 3))))
  105. (ref2 (lambda (bv offset)
  106. (bit-field (ref bv offset) 2 7)))
  107. (ref3 (lambda (bv offset)
  108. (logior (bit-field (ref bv offset) 7 8)
  109. (ash (bit-field (ref+ bv offset) 0 4) 1))))
  110. (ref4 (lambda (bv offset)
  111. (logior (bit-field (ref bv offset) 4 8)
  112. (ash (bit-field (ref+ bv offset) 0 1) 4))))
  113. (ref5 (lambda (bv offset)
  114. (bit-field (ref bv offset) 1 6)))
  115. (ref6 (lambda (bv offset)
  116. (logior (bit-field (ref bv offset) 6 8)
  117. (ash (bit-field (ref+ bv offset) 0 3) 2))))
  118. (ref7 (lambda (bv offset)
  119. (bit-field (ref bv offset) 3 8)))
  120. (refs (vector ref0 ref1 ref2 ref3 ref4 ref5 ref6 ref7)))
  121. (lambda (bv index)
  122. "Return the INDEXth quintet of BV, assuming quintets start from the
  123. least-significant bits, contrary to what RFC 4648 describes."
  124. (let ((p (vector-ref refs (modulo index 8))))
  125. (p bv (quotient (* index 5) 8))))))
  126. (define (bytevector-quintet-length bv)
  127. "Return the number of quintets (including truncated ones) available in BV."
  128. (ceiling (/ (* (bytevector-length bv) 8) 5)))
  129. (define (bytevector-quintet-fold proc init bv)
  130. "Return the result of applying PROC to each quintet of BV and the result of
  131. the previous application or INIT."
  132. (define len
  133. (bytevector-quintet-length bv))
  134. (let loop ((i 0)
  135. (r init))
  136. (if (= i len)
  137. r
  138. (loop (1+ i) (proc (bytevector-quintet-ref bv i) r)))))
  139. (define (bytevector-quintet-fold-right proc init bv)
  140. "Return the result of applying PROC to each quintet of BV and the result of
  141. the previous application or INIT."
  142. (define len
  143. (bytevector-quintet-length bv))
  144. (let loop ((i len)
  145. (r init))
  146. (if (zero? i)
  147. r
  148. (let ((j (- i 1)))
  149. (loop j (proc (bytevector-quintet-ref-right bv j) r))))))
  150. (define (make-bytevector->base32-string quintet-fold base32-chars)
  151. (lambda (bv)
  152. "Return a base32 encoding of BV using BASE32-CHARS as the alphabet."
  153. (let ((chars (quintet-fold (lambda (q r)
  154. (cons (vector-ref base32-chars q)
  155. r))
  156. '()
  157. bv)))
  158. (list->string (reverse chars)))))
  159. (define %nix-base32-chars
  160. ;; See `libutil/hash.cc'.
  161. #(#\0 #\1 #\2 #\3 #\4 #\5 #\6 #\7 #\8 #\9
  162. #\a #\b #\c #\d #\f #\g #\h #\i #\j #\k #\l #\m #\n
  163. #\p #\q #\r #\s #\v #\w #\x #\y #\z))
  164. (define %nix-base32-charset
  165. (list->char-set (vector->list %nix-base32-chars)))
  166. (define %rfc4648-base32-chars
  167. #(#\a #\b #\c #\d #\e #\f #\g #\h #\i #\j #\k #\l #\m
  168. #\n #\o #\p #\q #\r #\s #\t #\u #\v #\w #\x #\y #\z
  169. #\2 #\3 #\4 #\5 #\6 #\7))
  170. (define %rfc4648-base32-charset
  171. (list->char-set (vector->list %rfc4648-base32-chars)))
  172. (define bytevector->base32-string
  173. (make-bytevector->base32-string bytevector-quintet-fold
  174. %rfc4648-base32-chars))
  175. (define bytevector->nix-base32-string
  176. (make-bytevector->base32-string bytevector-quintet-fold-right
  177. %nix-base32-chars))
  178. (define bytevector-quintet-set!
  179. (let* ((setq! (lambda (bv offset start stop value)
  180. (let ((v (bytevector-u8-ref bv offset))
  181. (w (arithmetic-shift value start))
  182. (m (bitwise-xor (1- (expt 2 stop))
  183. (1- (expt 2 start)))))
  184. (bytevector-u8-set! bv offset
  185. (bitwise-merge m w v)))))
  186. (set0! (lambda (bv offset value)
  187. (setq! bv offset 3 8 value)))
  188. (set1! (lambda (bv offset value)
  189. (setq! bv offset 0 3 (bit-field value 2 5))
  190. (or (= (+ 1 offset) (bytevector-length bv))
  191. (setq! bv (+ 1 offset) 6 8 (bit-field value 0 2)))))
  192. (set2! (lambda (bv offset value)
  193. (setq! bv offset 1 6 value)))
  194. (set3! (lambda (bv offset value)
  195. (setq! bv offset 0 1 (bit-field value 4 5))
  196. (or (= (+ 1 offset) (bytevector-length bv))
  197. (setq! bv (+ 1 offset) 4 8 (bit-field value 0 4)))))
  198. (set4! (lambda (bv offset value)
  199. (setq! bv offset 0 4 (bit-field value 1 5))
  200. (or (= (+ 1 offset) (bytevector-length bv))
  201. (setq! bv (+ 1 offset) 7 8 (bit-field value 0 1)))))
  202. (set5! (lambda (bv offset value)
  203. (setq! bv offset 2 7 value)))
  204. (set6! (lambda (bv offset value)
  205. (setq! bv offset 0 2 (bit-field value 3 5))
  206. (or (= (+ 1 offset) (bytevector-length bv))
  207. (setq! bv (+ 1 offset) 5 8 (bit-field value 0 3)))))
  208. (set7! (lambda (bv offset value)
  209. (setq! bv offset 0 5 value)))
  210. (sets (vector set0! set1! set2! set3! set4! set5! set6! set7!)))
  211. (lambda (bv index value)
  212. "Set the INDEXth quintet of BV to VALUE."
  213. (let ((p (vector-ref sets (modulo index 8))))
  214. (p bv (quotient (* index 5) 8) (logand value #x1f))))))
  215. (define bytevector-quintet-set-right!
  216. (let* ((setq! (lambda (bv offset start stop value)
  217. (let ((v (bytevector-u8-ref bv offset))
  218. (w (arithmetic-shift value start))
  219. (m (bitwise-xor (1- (expt 2 stop))
  220. (1- (expt 2 start)))))
  221. (bytevector-u8-set! bv offset
  222. (bitwise-merge m w v)))))
  223. (set0! (lambda (bv offset value)
  224. (setq! bv offset 0 5 value)))
  225. (set1! (lambda (bv offset value)
  226. (setq! bv offset 5 8 (bit-field value 0 3))
  227. (or (= (+ 1 offset) (bytevector-length bv))
  228. (setq! bv (+ 1 offset) 0 2 (bit-field value 3 5)))))
  229. (set2! (lambda (bv offset value)
  230. (setq! bv offset 2 7 value)))
  231. (set3! (lambda (bv offset value)
  232. (setq! bv offset 7 8 (bit-field value 0 1))
  233. (or (= (+ 1 offset) (bytevector-length bv))
  234. (setq! bv (+ 1 offset) 0 4 (bit-field value 1 5)))))
  235. (set4! (lambda (bv offset value)
  236. (setq! bv offset 4 8 (bit-field value 0 4))
  237. (or (= (+ 1 offset) (bytevector-length bv))
  238. (setq! bv (+ 1 offset) 0 1 (bit-field value 4 5)))))
  239. (set5! (lambda (bv offset value)
  240. (setq! bv offset 1 6 value)))
  241. (set6! (lambda (bv offset value)
  242. (setq! bv offset 6 8 (bit-field value 0 2))
  243. (or (= (+ 1 offset) (bytevector-length bv))
  244. (setq! bv (+ 1 offset) 0 3 (bit-field value 2 5)))))
  245. (set7! (lambda (bv offset value)
  246. (setq! bv offset 3 8 value)))
  247. (sets (vector set0! set1! set2! set3! set4! set5! set6! set7!)))
  248. (lambda (bv index value)
  249. "Set the INDEXth quintet of BV to VALUE, assuming quintets start from
  250. the least-significant bits."
  251. (let ((p (vector-ref sets (modulo index 8))))
  252. (p bv (quotient (* index 5) 8) (logand value #x1f))))))
  253. (define (base32-string-unfold f s)
  254. "Given procedure F which, when applied to a character, returns the
  255. corresponding quintet, return the bytevector corresponding to string S."
  256. (define len (string-length s))
  257. (let ((bv (make-bytevector (quotient (* len 5) 8))))
  258. (string-fold (lambda (chr index)
  259. (bytevector-quintet-set! bv index (f chr))
  260. (+ 1 index))
  261. 0
  262. s)
  263. bv))
  264. (define (base32-string-unfold-right f s)
  265. "Given procedure F which, when applied to a character, returns the
  266. corresponding quintet, return the bytevector corresponding to string S,
  267. starting from the right of S."
  268. (define len (string-length s))
  269. (let ((bv (make-bytevector (quotient (* len 5) 8))))
  270. (string-fold-right (lambda (chr index)
  271. (bytevector-quintet-set-right! bv index (f chr))
  272. (+ 1 index))
  273. 0
  274. s)
  275. bv))
  276. ;; Invalid base32 character error condition when decoding base32.
  277. (define-condition-type &invalid-base32-character &error
  278. invalid-base32-character?
  279. (character invalid-base32-character-value)
  280. (string invalid-base32-character-string))
  281. (define (make-base32-string->bytevector base32-string-unfold base32-chars)
  282. (let ((char->value (let loop ((i 0)
  283. (v vlist-null))
  284. (if (= i (vector-length base32-chars))
  285. v
  286. (loop (+ 1 i)
  287. (vhash-consv (vector-ref base32-chars i)
  288. i v))))))
  289. (lambda (s)
  290. "Return the binary representation of base32 string S as a bytevector."
  291. (base32-string-unfold (lambda (chr)
  292. (or (and=> (vhash-assv chr char->value) cdr)
  293. (raise (condition
  294. (&invalid-base32-character
  295. (character chr)
  296. (string s))))))
  297. s))))
  298. (define base32-string->bytevector
  299. (make-base32-string->bytevector base32-string-unfold %rfc4648-base32-chars))
  300. (define nix-base32-string->bytevector
  301. (make-base32-string->bytevector base32-string-unfold-right %nix-base32-chars))
  302. ;;; base32.scm ends here