srfi-74.scm 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. ; Copyright (c) 1993-2007 by Richard Kelsey and Jonathan Rees. See file COPYING.
  2. ; Octet-addressed binary objects
  3. ; The efficiency of this is probably less than optimal.
  4. ; This uses SRFIs 23, 26, 60, and 66
  5. (define *endianness/little* (list 'little))
  6. (define *endianness/big* (list 'big))
  7. (define-syntax endianness
  8. (syntax-rules (little big native)
  9. ((endianness little) *endianness/little*)
  10. ((endianness big) *endianness/big*)
  11. ;; change this to the endianness of your architecture
  12. ((endianness native) *endianness/big*)))
  13. (define blob? u8vector?)
  14. (define (make-blob k)
  15. (make-u8vector k 0))
  16. (define (blob-length b)
  17. (u8vector-length b))
  18. (define (blob-u8-ref b k)
  19. (u8vector-ref b k))
  20. (define (blob-u8-set! b k octet)
  21. (u8vector-set! b k octet))
  22. (define (blob-s8-ref b k)
  23. (u8->s8 (u8vector-ref b k)))
  24. (define (u8->s8 octet)
  25. (if (> octet 127)
  26. (- octet 256)
  27. octet))
  28. (define (blob-s8-set! b k val)
  29. (u8vector-set! b k (s8->u8 val)))
  30. (define (s8->u8 val)
  31. (if (negative? val)
  32. (+ val 256)
  33. val))
  34. (define (index-iterate start count low-first?
  35. unit proc)
  36. (if low-first?
  37. (let loop ((index 0)
  38. (acc unit))
  39. (if (>= index count)
  40. acc
  41. (loop (+ index 1)
  42. (proc (+ start index) acc))))
  43. (let loop ((index (- (+ start count) 1))
  44. (acc unit))
  45. (if (< index start)
  46. acc
  47. (loop (- index 1)
  48. (proc index acc))))))
  49. (define (blob-uint-ref size endness blob index)
  50. (index-iterate index size
  51. (eq? (endianness big) endness)
  52. 0
  53. (lambda (index acc)
  54. (+ (u8vector-ref blob index) (arithmetic-shift acc 8)))))
  55. (define (blob-sint-ref size endness blob index)
  56. (let ((high-byte (u8vector-ref blob
  57. (if (eq? endness (endianness big))
  58. index
  59. (- (+ index size) 1)))))
  60. (if (> high-byte 127)
  61. (- (+ 1
  62. (index-iterate index size
  63. (eq? (endianness big) endness)
  64. 0
  65. (lambda (index acc)
  66. (+ (- 255 (u8vector-ref blob index))
  67. (arithmetic-shift acc 8))))))
  68. (index-iterate index size
  69. (eq? (endianness big) endness)
  70. 0
  71. (lambda (index acc)
  72. (+ (u8vector-ref blob index) (arithmetic-shift acc 8)))))))
  73. (define (make-uint-ref size)
  74. (cut blob-uint-ref size <> <> <>))
  75. (define (make-sint-ref size)
  76. (cut blob-sint-ref size <> <> <>))
  77. (define (blob-uint-set! size endness blob index val)
  78. (index-iterate index size (eq? (endianness little) endness)
  79. val
  80. (lambda (index acc)
  81. (u8vector-set! blob index (remainder acc 256))
  82. (quotient acc 256)))
  83. (values))
  84. (define (blob-sint-set! size endness blob index val)
  85. (if (negative? val)
  86. (index-iterate index size (eq? (endianness little) endness)
  87. (- -1 val)
  88. (lambda (index acc)
  89. (u8vector-set! blob index (- 255 (remainder acc 256)))
  90. (quotient acc 256)))
  91. (index-iterate index size (eq? (endianness little) endness)
  92. val
  93. (lambda (index acc)
  94. (u8vector-set! blob index (remainder acc 256))
  95. (quotient acc 256))))
  96. (values))
  97. (define (make-uint-set! size)
  98. (cut blob-uint-set! size <> <> <> <>))
  99. (define (make-sint-set! size)
  100. (cut blob-sint-set! size <> <> <> <>))
  101. (define (make-ref/native base base-ref)
  102. (lambda (blob index)
  103. (ensure-aligned index base)
  104. (base-ref (endianness native) blob index)))
  105. (define (make-set!/native base base-set!)
  106. (lambda (blob index val)
  107. (ensure-aligned index base)
  108. (base-set! (endianness native) blob index val)))
  109. (define (ensure-aligned index base)
  110. (if (not (zero? (remainder index base)))
  111. (error "non-aligned blob access" index base)))
  112. (define blob-u16-ref (make-uint-ref 2))
  113. (define blob-u16-set! (make-uint-set! 2))
  114. (define blob-s16-ref (make-sint-ref 2))
  115. (define blob-s16-set! (make-sint-set! 2))
  116. (define blob-u16-native-ref (make-ref/native 2 blob-u16-ref))
  117. (define blob-u16-native-set! (make-set!/native 2 blob-u16-set!))
  118. (define blob-s16-native-ref (make-ref/native 2 blob-s16-ref))
  119. (define blob-s16-native-set! (make-set!/native 2 blob-s16-set!))
  120. (define blob-u32-ref (make-uint-ref 4))
  121. (define blob-u32-set! (make-uint-set! 4))
  122. (define blob-s32-ref (make-sint-ref 4))
  123. (define blob-s32-set! (make-sint-set! 4))
  124. (define blob-u32-native-ref (make-ref/native 4 blob-u32-ref))
  125. (define blob-u32-native-set! (make-set!/native 4 blob-u32-set!))
  126. (define blob-s32-native-ref (make-ref/native 4 blob-s32-ref))
  127. (define blob-s32-native-set! (make-set!/native 4 blob-s32-set!))
  128. (define blob-u64-ref (make-uint-ref 8))
  129. (define blob-u64-set! (make-uint-set! 8))
  130. (define blob-s64-ref (make-sint-ref 8))
  131. (define blob-s64-set! (make-sint-set! 8))
  132. (define blob-u64-native-ref (make-ref/native 8 blob-u64-ref))
  133. (define blob-u64-native-set! (make-set!/native 8 blob-u64-set!))
  134. (define blob-s64-native-ref (make-ref/native 8 blob-s64-ref))
  135. (define blob-s64-native-set! (make-set!/native 8 blob-s64-set!))
  136. ; Auxiliary stuff
  137. (define (blob-copy! source source-start target target-start count)
  138. (u8vector-copy! source source-start target target-start count))
  139. (define (blob-copy b)
  140. (u8vector-copy b))
  141. (define (blob=? b1 b2)
  142. (u8vector=? b1 b2))
  143. (define (blob->u8-list b)
  144. (u8vector->list b))
  145. (define (blob->s8-list b)
  146. (map u8->s8 (u8vector->list b)))
  147. (define (u8-list->blob l)
  148. (list->u8vector l))
  149. (define (s8-list->blob l)
  150. (list->u8vector (map s8->u8 l)))
  151. (define (make-blob->int-list blob-ref)
  152. (lambda (size endness b)
  153. (let ((ref (cut blob-ref size endness b <>))
  154. (length (blob-length b)))
  155. (let loop ((i 0) (r '()))
  156. (if (>= i length)
  157. (reverse r)
  158. (loop (+ i size)
  159. (cons (ref i) r)))))))
  160. (define blob->uint-list (make-blob->int-list blob-uint-ref))
  161. (define blob->sint-list (make-blob->int-list blob-sint-ref))
  162. (define (make-int-list->blob blob-set!)
  163. (lambda (size endness l)
  164. (let* ((blob (make-blob (* size (length l))))
  165. (set! (cut blob-set! size endness blob <> <>)))
  166. (let loop ((i 0) (l l))
  167. (if (null? l)
  168. blob
  169. (begin
  170. (set! i (car l))
  171. (loop (+ i size) (cdr l))))))))
  172. (define uint-list->blob (make-int-list->blob blob-uint-set!))
  173. (define sint-list->blob (make-int-list->blob blob-sint-set!))