spacepaste

  1.  
  2. (define in-biwa #f)
  3. (define idiv (if in-biwa div quotient))
  4. (define imod (if in-biwa mod remainder))
  5. (define ls (lambda (n i) (* n (expt 2 i))))
  6. (define rs (lambda (n i) (if (> i 0) (rs (idiv n 2) (- i 1)) n)))
  7. (define b64map (map (lambda (p) (map char->integer p)) (list '(#\A #\Z) '(#\a #\z) '(#\0 #\9) '(#\+ #\+) '(#\/ #\/))))
  8. ;; (char->sixtet a-char)
  9. ;; =>
  10. ;; (a-six-bit-integer)
  11. (define char->sixtet (lambda (c)
  12. (let ([n (char->integer c)])
  13. (let check-range ([rest b64map] [base 0])
  14. (let* ([trial (car rest)] [nmin (car trial)] [nmax (cadr trial)])
  15. (if (and (>= n nmin) (<= n nmax))
  16. (+ base (- n (car trial)))
  17. (check-range (cdr rest) (+ base 1 (- nmax nmin)))))))))
  18. ;; (base64->char (string->list a-base64-string))
  19. ;; =>
  20. ;; (string->list the-original)
  21. (define base64->char
  22. (lambda (source)
  23. (call-with-values
  24. (lambda ()
  25. (let parse-next ([rest source] [offset 0])
  26. (let parse-sixtet ([rest rest])
  27. (if (null? rest)
  28. (values (list) 0)
  29. (if (memv (car rest) '(#\space #\newline #\tab #\=))
  30. (parse-sixtet (cdr rest))
  31. (let ([current (char->sixtet (car rest))])
  32. (call-with-values (lambda () (parse-next (cdr rest) (imod (+ offset 2) 8)))
  33. (lambda (result next)
  34. (values
  35. (if (= offset 6)
  36. result
  37. (append (list (integer->char (+ (imod (ls current (+ offset 2)) (ls 1 8)) (rs next (- 4 offset))))) result))
  38. current)))))))))
  39. (lambda (result ignore) result))))
  40. (display (base64->char (string->list "TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=")))
  41.