base32.scm 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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-1)
  20. #:use-module (srfi srfi-34)
  21. #:use-module (srfi srfi-35)
  22. #:use-module (srfi srfi-60)
  23. #:use-module (rnrs bytevectors)
  24. #:use-module (ice-9 vlist)
  25. #:export (bytevector-quintet-length
  26. bytevector->base32-string
  27. bytevector->nix-base32-string
  28. base32-string->bytevector
  29. nix-base32-string->bytevector
  30. %nix-base32-charset
  31. %rfc4648-base32-charset
  32. &invalid-base32-character
  33. invalid-base32-character?
  34. invalid-base32-character-value
  35. invalid-base32-character-string))
  36. ;;; Commentary:
  37. ;;;
  38. ;;; A generic, customizable to convert bytevectors to/from a base32
  39. ;;; representation.
  40. ;;;
  41. ;;; Code:
  42. (define-syntax bit-field
  43. (lambda (s)
  44. ;; This inline version of 'bit-field' assumes that START and END are
  45. ;; literals and pre-computes the mask. In an ideal world, using 'define'
  46. ;; or 'define-inlinable' would be enough, but as of 3.0.7, peval doesn't
  47. ;; expand calls to 'expt' (and 'bit-field' is a subr.)
  48. (syntax-case s ()
  49. ((_ n start end)
  50. (let* ((s (syntax->datum #'start))
  51. (e (syntax->datum #'end))
  52. (mask (- (expt 2 (- e s)) 1)))
  53. #`(logand (ash n (- start)) #,mask))))))
  54. (define bytevector-quintet-ref
  55. (let* ((ref bytevector-u8-ref)
  56. (ref+ (lambda (bv offset)
  57. (let ((o (+ 1 offset)))
  58. (if (>= o (bytevector-length bv))
  59. 0
  60. (bytevector-u8-ref bv o)))))
  61. (ref0 (lambda (bv offset)
  62. (bit-field (ref bv offset) 3 8)))
  63. (ref1 (lambda (bv offset)
  64. (logior (ash (bit-field (ref bv offset) 0 3) 2)
  65. (bit-field (ref+ bv offset) 6 8))))
  66. (ref2 (lambda (bv offset)
  67. (bit-field (ref bv offset) 1 6)))
  68. (ref3 (lambda (bv offset)
  69. (logior (ash (bit-field (ref bv offset) 0 1) 4)
  70. (bit-field (ref+ bv offset) 4 8))))
  71. (ref4 (lambda (bv offset)
  72. (logior (ash (bit-field (ref bv offset) 0 4) 1)
  73. (bit-field (ref+ bv offset) 7 8))))
  74. (ref5 (lambda (bv offset)
  75. (bit-field (ref bv offset) 2 7)))
  76. (ref6 (lambda (bv offset)
  77. (logior (ash (bit-field (ref bv offset) 0 2) 3)
  78. (bit-field (ref+ bv offset) 5 8))))
  79. (ref7 (lambda (bv offset)
  80. (bit-field (ref bv offset) 0 5)))
  81. (refs (vector ref0 ref1 ref2 ref3 ref4 ref5 ref6 ref7)))
  82. (lambda (bv index)
  83. "Return the INDEXth quintet of BV."
  84. (let ((p (vector-ref refs (modulo index 8))))
  85. (p bv (quotient (* index 5) 8))))))
  86. (define bytevector-quintet-ref-right
  87. (let* ((ref bytevector-u8-ref)
  88. (ref+ (lambda (bv offset)
  89. (let ((o (+ 1 offset)))
  90. (if (>= o (bytevector-length bv))
  91. 0
  92. (bytevector-u8-ref bv o)))))
  93. (ref0 (lambda (bv offset)
  94. (bit-field (ref bv offset) 0 5)))
  95. (ref1 (lambda (bv offset)
  96. (logior (bit-field (ref bv offset) 5 8)
  97. (ash (bit-field (ref+ bv offset) 0 2) 3))))
  98. (ref2 (lambda (bv offset)
  99. (bit-field (ref bv offset) 2 7)))
  100. (ref3 (lambda (bv offset)
  101. (logior (bit-field (ref bv offset) 7 8)
  102. (ash (bit-field (ref+ bv offset) 0 4) 1))))
  103. (ref4 (lambda (bv offset)
  104. (logior (bit-field (ref bv offset) 4 8)
  105. (ash (bit-field (ref+ bv offset) 0 1) 4))))
  106. (ref5 (lambda (bv offset)
  107. (bit-field (ref bv offset) 1 6)))
  108. (ref6 (lambda (bv offset)
  109. (logior (bit-field (ref bv offset) 6 8)
  110. (ash (bit-field (ref+ bv offset) 0 3) 2))))
  111. (ref7 (lambda (bv offset)
  112. (bit-field (ref bv offset) 3 8)))
  113. (refs (vector ref0 ref1 ref2 ref3 ref4 ref5 ref6 ref7)))
  114. (lambda (bv index)
  115. "Return the INDEXth quintet of BV, assuming quintets start from the
  116. least-significant bits, contrary to what RFC 4648 describes."
  117. (let ((p (vector-ref refs (modulo index 8))))
  118. (p bv (quotient (* index 5) 8))))))
  119. (define (bytevector-quintet-length bv)
  120. "Return the number of quintets (including truncated ones) available in BV."
  121. (ceiling (/ (* (bytevector-length bv) 8) 5)))
  122. (define (bytevector-quintet-fold proc init bv)
  123. "Return the result of applying PROC to each quintet of BV and the result of
  124. the previous application or INIT."
  125. (define len
  126. (bytevector-quintet-length bv))
  127. (let loop ((i 0)
  128. (r init))
  129. (if (= i len)
  130. r
  131. (loop (1+ i) (proc (bytevector-quintet-ref bv i) r)))))
  132. (define (bytevector-quintet-fold-right proc init bv)
  133. "Return the result of applying PROC to each quintet of BV and the result of
  134. the previous application or INIT."
  135. (define len
  136. (bytevector-quintet-length bv))
  137. (let loop ((i len)
  138. (r init))
  139. (if (zero? i)
  140. r
  141. (let ((j (- i 1)))
  142. (loop j (proc (bytevector-quintet-ref-right bv j) r))))))
  143. (define (make-bytevector->base32-string quintet-fold base32-chars)
  144. (lambda (bv)
  145. "Return a base32 encoding of BV using BASE32-CHARS as the alphabet."
  146. (let ((chars (quintet-fold (lambda (q r)
  147. (cons (vector-ref base32-chars q)
  148. r))
  149. '()
  150. bv)))
  151. (list->string (reverse chars)))))
  152. (define %nix-base32-chars
  153. ;; See `libutil/hash.cc'.
  154. #(#\0 #\1 #\2 #\3 #\4 #\5 #\6 #\7 #\8 #\9
  155. #\a #\b #\c #\d #\f #\g #\h #\i #\j #\k #\l #\m #\n
  156. #\p #\q #\r #\s #\v #\w #\x #\y #\z))
  157. (define %nix-base32-charset
  158. (list->char-set (vector->list %nix-base32-chars)))
  159. (define %rfc4648-base32-chars
  160. #(#\a #\b #\c #\d #\e #\f #\g #\h #\i #\j #\k #\l #\m
  161. #\n #\o #\p #\q #\r #\s #\t #\u #\v #\w #\x #\y #\z
  162. #\2 #\3 #\4 #\5 #\6 #\7))
  163. (define %rfc4648-base32-charset
  164. (list->char-set (vector->list %rfc4648-base32-chars)))
  165. (define bytevector->base32-string
  166. (make-bytevector->base32-string bytevector-quintet-fold
  167. %rfc4648-base32-chars))
  168. (define bytevector->nix-base32-string
  169. (make-bytevector->base32-string bytevector-quintet-fold-right
  170. %nix-base32-chars))
  171. (define bytevector-quintet-set!
  172. (let* ((setq! (lambda (bv offset start stop value)
  173. (let ((v (bytevector-u8-ref bv offset))
  174. (w (arithmetic-shift value start))
  175. (m (bitwise-xor (1- (expt 2 stop))
  176. (1- (expt 2 start)))))
  177. (bytevector-u8-set! bv offset
  178. (bitwise-merge m w v)))))
  179. (set0! (lambda (bv offset value)
  180. (setq! bv offset 3 8 value)))
  181. (set1! (lambda (bv offset value)
  182. (setq! bv offset 0 3 (bit-field value 2 5))
  183. (or (= (+ 1 offset) (bytevector-length bv))
  184. (setq! bv (+ 1 offset) 6 8 (bit-field value 0 2)))))
  185. (set2! (lambda (bv offset value)
  186. (setq! bv offset 1 6 value)))
  187. (set3! (lambda (bv offset value)
  188. (setq! bv offset 0 1 (bit-field value 4 5))
  189. (or (= (+ 1 offset) (bytevector-length bv))
  190. (setq! bv (+ 1 offset) 4 8 (bit-field value 0 4)))))
  191. (set4! (lambda (bv offset value)
  192. (setq! bv offset 0 4 (bit-field value 1 5))
  193. (or (= (+ 1 offset) (bytevector-length bv))
  194. (setq! bv (+ 1 offset) 7 8 (bit-field value 0 1)))))
  195. (set5! (lambda (bv offset value)
  196. (setq! bv offset 2 7 value)))
  197. (set6! (lambda (bv offset value)
  198. (setq! bv offset 0 2 (bit-field value 3 5))
  199. (or (= (+ 1 offset) (bytevector-length bv))
  200. (setq! bv (+ 1 offset) 5 8 (bit-field value 0 3)))))
  201. (set7! (lambda (bv offset value)
  202. (setq! bv offset 0 5 value)))
  203. (sets (vector set0! set1! set2! set3! set4! set5! set6! set7!)))
  204. (lambda (bv index value)
  205. "Set the INDEXth quintet of BV to VALUE."
  206. (let ((p (vector-ref sets (modulo index 8))))
  207. (p bv (quotient (* index 5) 8) (logand value #x1f))))))
  208. (define bytevector-quintet-set-right!
  209. (let* ((setq! (lambda (bv offset start stop value)
  210. (let ((v (bytevector-u8-ref bv offset))
  211. (w (arithmetic-shift value start))
  212. (m (bitwise-xor (1- (expt 2 stop))
  213. (1- (expt 2 start)))))
  214. (bytevector-u8-set! bv offset
  215. (bitwise-merge m w v)))))
  216. (set0! (lambda (bv offset value)
  217. (setq! bv offset 0 5 value)))
  218. (set1! (lambda (bv offset value)
  219. (setq! bv offset 5 8 (bit-field value 0 3))
  220. (or (= (+ 1 offset) (bytevector-length bv))
  221. (setq! bv (+ 1 offset) 0 2 (bit-field value 3 5)))))
  222. (set2! (lambda (bv offset value)
  223. (setq! bv offset 2 7 value)))
  224. (set3! (lambda (bv offset value)
  225. (setq! bv offset 7 8 (bit-field value 0 1))
  226. (or (= (+ 1 offset) (bytevector-length bv))
  227. (setq! bv (+ 1 offset) 0 4 (bit-field value 1 5)))))
  228. (set4! (lambda (bv offset value)
  229. (setq! bv offset 4 8 (bit-field value 0 4))
  230. (or (= (+ 1 offset) (bytevector-length bv))
  231. (setq! bv (+ 1 offset) 0 1 (bit-field value 4 5)))))
  232. (set5! (lambda (bv offset value)
  233. (setq! bv offset 1 6 value)))
  234. (set6! (lambda (bv offset value)
  235. (setq! bv offset 6 8 (bit-field value 0 2))
  236. (or (= (+ 1 offset) (bytevector-length bv))
  237. (setq! bv (+ 1 offset) 0 3 (bit-field value 2 5)))))
  238. (set7! (lambda (bv offset value)
  239. (setq! bv offset 3 8 value)))
  240. (sets (vector set0! set1! set2! set3! set4! set5! set6! set7!)))
  241. (lambda (bv index value)
  242. "Set the INDEXth quintet of BV to VALUE, assuming quintets start from
  243. the least-significant bits."
  244. (let ((p (vector-ref sets (modulo index 8))))
  245. (p bv (quotient (* index 5) 8) (logand value #x1f))))))
  246. (define (base32-string-unfold f s)
  247. "Given procedure F which, when applied to a character, returns the
  248. corresponding quintet, return the bytevector corresponding to string S."
  249. (define len (string-length s))
  250. (let ((bv (make-bytevector (quotient (* len 5) 8))))
  251. (string-fold (lambda (chr index)
  252. (bytevector-quintet-set! bv index (f chr))
  253. (+ 1 index))
  254. 0
  255. s)
  256. bv))
  257. (define (base32-string-unfold-right f s)
  258. "Given procedure F which, when applied to a character, returns the
  259. corresponding quintet, return the bytevector corresponding to string S,
  260. starting from the right of S."
  261. (define len (string-length s))
  262. (let ((bv (make-bytevector (quotient (* len 5) 8))))
  263. (string-fold-right (lambda (chr index)
  264. (bytevector-quintet-set-right! bv index (f chr))
  265. (+ 1 index))
  266. 0
  267. s)
  268. bv))
  269. ;; Invalid base32 character error condition when decoding base32.
  270. (define-condition-type &invalid-base32-character &error
  271. invalid-base32-character?
  272. (character invalid-base32-character-value)
  273. (string invalid-base32-character-string))
  274. (define (make-base32-string->bytevector base32-string-unfold base32-chars)
  275. (let ((char->value (let loop ((i 0)
  276. (v vlist-null))
  277. (if (= i (vector-length base32-chars))
  278. v
  279. (loop (+ 1 i)
  280. (vhash-consv (vector-ref base32-chars i)
  281. i v))))))
  282. (lambda (s)
  283. "Return the binary representation of base32 string S as a bytevector."
  284. (base32-string-unfold (lambda (chr)
  285. (or (and=> (vhash-assv chr char->value) cdr)
  286. (raise (condition
  287. (&invalid-base32-character
  288. (character chr)
  289. (string s))))))
  290. s))))
  291. (define base32-string->bytevector
  292. (make-base32-string->bytevector base32-string-unfold %rfc4648-base32-chars))
  293. (define nix-base32-string->bytevector
  294. (make-base32-string->bytevector base32-string-unfold-right %nix-base32-chars))
  295. ;;; base32.scm ends here