base32.scm 13 KB

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