Debian Codegraph @

Sound/Osc/Coding/Byte.hs

raw symbols SCIP
1 {- | Byte-level coding utility functions.
2 Plain forms are big-endian, little-endian forms have @_le@ suffix.
3 -}
4 module Sound.Osc.Coding.Byte where
5
6 import Data.Bits {- base -}
7 import Data.Int {- base -}
8 import Data.Word {- base -}
9 import System.IO {- base -}
10
11 import qualified GHC.ByteOrder {- base -}
12
13 import qualified Data.Binary as Binary {- binary -}
14 import qualified Data.Binary.Get as Binary.Get {- binary -}
15 import qualified Data.Binary.Put as Binary.Put {- binary -}
16
17 import qualified Data.ByteString as ByteString {- bytestring -}
18 import qualified Data.ByteString.Char8 as ByteString.Char8 {- bytestring -}
19 import qualified Data.ByteString.Internal as ByteString.Internal {- bytestring -}
20 import qualified Data.ByteString.Lazy as ByteString.Lazy {- bytestring -}
21 import qualified Data.ByteString.Lazy.Char8 as ByteString.Lazy.Char8 {- bytestring -}
22 import qualified Data.ByteString.Unsafe as ByteString.Unsafe {- bytestring -}
23
24 import qualified Sound.Osc.Coding.Convert as Convert {- hosc -}
25
26 -- * Encode
27
28 -- | Type specialised 'Binary.encode' (big-endian).
29 encode_int8 :: Int8 -> ByteString.Lazy.ByteString
30 encode_int8 = Binary.encode
31
32 {- | Type specialised 'Binary.encode' (big-endian).
33
34 >>> encode_int16 0x0102 == ByteString.Lazy.pack [0x01,0x02]
35 True
36 -}
37 encode_int16 :: Int16 -> ByteString.Lazy.ByteString
38 encode_int16 = Binary.encode
39
40 {- | Little-endian.
41
42 >>> encode_int16_le 0x0102 == ByteString.Lazy.pack [0x02,0x01]
43 True
44 -}
45 encode_int16_le :: Int16 -> ByteString.Lazy.ByteString
46 encode_int16_le = Binary.Put.runPut . Binary.Put.putInt16le
47
48 -- | Encode a signed 64-bit integer (big-endian).
49 encode_int64 :: Int64 -> ByteString.Lazy.ByteString
50 encode_int64 = Binary.encode
51
52 -- | Type specialised 'Binary.encode' (big-endian).
53 encode_word8 :: Word8 -> ByteString.Lazy.ByteString
54 encode_word8 = Binary.encode
55
56 {- | Type specialised 'Binary.encode' (big-endian).
57
58 >>> encode_word16 0x0102 == ByteString.Lazy.pack [0x01,0x02]
59 True
60 -}
61 encode_word16 :: Word16 -> ByteString.Lazy.ByteString
62 encode_word16 = Binary.encode
63
64 {- | Little-endian.
65
66 >>> encode_word16_le 0x0102 == ByteString.Lazy.pack [0x02,0x01]
67 True
68 -}
69 encode_word16_le :: Word16 -> ByteString.Lazy.ByteString
70 encode_word16_le = Binary.Put.runPut . Binary.Put.putWord16le
71
72 -- | Type specialised 'Binary.encode'.
73 encode_word32 :: Word32 -> ByteString.Lazy.ByteString
74 encode_word32 = Binary.encode
75
76 -- | Little-endian variant of 'encode_word32'.
77 encode_word32_le :: Word32 -> ByteString.Lazy.ByteString
78 encode_word32_le = Binary.Put.runPut . Binary.Put.putWord32le
79
80 -- | Encode an unsigned 64-bit integer.
81 encode_word64 :: Word64 -> ByteString.Lazy.ByteString
82 encode_word64 = Binary.encode
83
84 -- * Encode/Int
85
86 -- | Encode a signed 8-bit integer.
87 encode_i8 :: Int -> ByteString.Lazy.ByteString
88 encode_i8 = encode_int8 . Convert.int_to_int8
89
90 -- | Encode an un-signed 8-bit integer.
91 encode_u8 :: Int -> ByteString.Lazy.ByteString
92 encode_u8 = encode_word8 . Convert.int_to_word8
93
94 {- | Encode an un-signed 16-bit integer.
95
96 >>> encode_u16 0x0102 == ByteString.Lazy.pack [1,2]
97 True
98 -}
99 encode_u16 :: Int -> ByteString.Lazy.ByteString
100 encode_u16 = encode_word16 . Convert.int_to_word16
101
102 {- | Little-endian.
103
104 >>> encode_u16_le 0x0102 == ByteString.Lazy.pack [2,1]
105 True
106 -}
107 encode_u16_le :: Int -> ByteString.Lazy.ByteString
108 encode_u16_le = encode_word16_le . Convert.int_to_word16
109
110 -- | Encode a signed 16-bit integer.
111 encode_i16 :: Int -> ByteString.Lazy.ByteString
112 encode_i16 = Binary.encode . Convert.int_to_int16
113
114 -- | Encode a signed 32-bit integer.
115 encode_i32 :: Int -> ByteString.Lazy.ByteString
116 encode_i32 = Binary.encode . Convert.int_to_int32
117
118 {- | Encode an unsigned 32-bit integer.
119
120 >>> ByteString.Lazy.unpack (encode_u32 0x01020304)
121 [1,2,3,4]
122 -}
123 encode_u32 :: Int -> ByteString.Lazy.ByteString
124 encode_u32 = encode_word32 . Convert.int_to_word32
125
126 {- | Little-endian.
127
128 >>> ByteString.Lazy.unpack (encode_u32_le 0x01020304)
129 [4,3,2,1]
130 -}
131 encode_u32_le :: Int -> ByteString.Lazy.ByteString
132 encode_u32_le = encode_word32_le . Convert.int_to_word32
133
134 -- * Encode/Float
135
136 {- | Encode a 32-bit IEEE floating point number.
137
138 >>> ByteString.Lazy.unpack (encode_f32 3.141)
139 [64,73,6,37]
140 -}
141 encode_f32 :: Float -> ByteString.Lazy.ByteString
142 encode_f32 = Binary.Put.runPut . Binary.Put.putFloatbe
143
144 {- | Little-endian variant of 'encode_f32'.
145
146 >>> ByteString.Lazy.unpack (encode_f32_le 3.141)
147 [37,6,73,64]
148 -}
149 encode_f32_le :: Float -> ByteString.Lazy.ByteString
150 encode_f32_le = Binary.Put.runPut . Binary.Put.putFloatle
151
152 {- | Encode a 64-bit IEEE floating point number.
153
154 >>> ByteString.Lazy.unpack (encode_f64 3.141)
155 [64,9,32,196,155,165,227,84]
156 -}
157 encode_f64 :: Double -> ByteString.Lazy.ByteString
158 encode_f64 = Binary.Put.runPut . Binary.Put.putDoublebe
159
160 {- | Little-endian variant of 'encode_f64'.
161
162 >>> ByteString.Lazy.unpack (encode_f64_le 3.141)
163 [84,227,165,155,196,32,9,64]
164 -}
165 encode_f64_le :: Double -> ByteString.Lazy.ByteString
166 encode_f64_le = Binary.Put.runPut . Binary.Put.putDoublele
167
168 -- * Encode/Ascii
169
170 -- | Encode an Ascii string (Ascii at Datum is an alias for a Char8 Bytetring).
171 encode_ascii :: ByteString.Char8.ByteString -> ByteString.Lazy.ByteString
172 encode_ascii = ByteString.Lazy.pack . ByteString.unpack
173
174 -- * Decode
175
176 -- | Type specialised 'Binary.decode'.
177 decode_word16 :: ByteString.Lazy.ByteString -> Word16
178 decode_word16 = Binary.decode
179
180 -- | Little-endian variant of 'decode_word16'.
181 decode_word16_le :: ByteString.Lazy.ByteString -> Word16
182 decode_word16_le = Binary.Get.runGet Binary.Get.getWord16le
183
184 -- | Type specialised 'Binary.decode'.
185 decode_int16 :: ByteString.Lazy.ByteString -> Int16
186 decode_int16 = Binary.decode
187
188 -- | Type specialised 'Binary.decode'.
189 decode_word32 :: ByteString.Lazy.ByteString -> Word32
190 decode_word32 = Binary.decode
191
192 -- | Little-endian variant of 'decode_word32'.
193 decode_word32_le :: ByteString.Lazy.ByteString -> Word32
194 decode_word32_le = Binary.Get.runGet Binary.Get.getWord32le
195
196 -- | Type specialised 'Binary.decode'.
197 decode_int64 :: ByteString.Lazy.ByteString -> Int64
198 decode_int64 = Binary.decode
199
200 -- | Type specialised 'Binary.decode'.
201 decode_word64 :: ByteString.Lazy.ByteString -> Word64
202 decode_word64 = Binary.decode
203
204 -- * Decode/Int
205
206 -- | Decode an un-signed 8-bit integer.
207 decode_u8 :: ByteString.Lazy.ByteString -> Int
208 decode_u8 = Convert.word8_to_int . ByteString.Lazy.head
209
210 -- | Decode a signed 8-bit integer.
211 decode_i8 :: ByteString.Lazy.ByteString -> Int
212 decode_i8 = Convert.int8_to_int . Binary.decode
213
214 -- | Decode an unsigned 8-bit integer.
215 decode_u16 :: ByteString.Lazy.ByteString -> Int
216 decode_u16 = Convert.word16_to_int . decode_word16
217
218 -- | Little-endian variant of 'decode_u16'.
219 decode_u16_le :: ByteString.Lazy.ByteString -> Int
220 decode_u16_le = Convert.word16_to_int . decode_word16_le
221
222 -- | Decode a signed 16-bit integer.
223 decode_i16 :: ByteString.Lazy.ByteString -> Int
224 decode_i16 = Convert.int16_to_int . decode_int16
225
226 -- | Little-endian variant of 'decode_i16'.
227 decode_i16_le :: ByteString.Lazy.ByteString -> Int
228 decode_i16_le = decode_i16 . ByteString.Lazy.reverse
229
230 {- | Decode a signed 32-bit integer.
231
232 >>> decode_i32 (ByteString.Lazy.pack [0x00,0x00,0x03,0xe7]) == 0x03e7
233 True
234 -}
235 decode_i32 :: ByteString.Lazy.ByteString -> Int
236 decode_i32 = Convert.int32_to_int . Binary.decode
237
238 {- | Little-endian variant of 'decode_i32'.
239
240 >>> decode_i32_le (ByteString.Lazy.pack [0xe7,0x03,0x00,0x00]) == 0x03e7
241 True
242 -}
243 decode_i32_le :: ByteString.Lazy.ByteString -> Int
244 decode_i32_le = decode_i32 . ByteString.Lazy.reverse
245
246 {- | Decode an unsigned 32-bit integer.
247
248 >>> decode_u32 (ByteString.Lazy.pack [1,2,3,4]) == 0x01020304
249 True
250 -}
251 decode_u32 :: ByteString.Lazy.ByteString -> Int
252 decode_u32 = Convert.word32_to_int . decode_word32
253
254 {- | Little-endian variant of decode_u32.
255
256 >>> decode_u32_le (ByteString.Lazy.pack [1,2,3,4]) == 0x04030201
257 True
258 -}
259 decode_u32_le :: ByteString.Lazy.ByteString -> Int
260 decode_u32_le = Convert.word32_to_int . decode_word32_le
261
262 -- * Decode/Float
263
264 {- | Decode a 32-bit IEEE floating point number.
265
266 >>> decode_f32 (ByteString.Lazy.pack [64,73,6,37])
267 3.141
268 -}
269 decode_f32 :: ByteString.Lazy.ByteString -> Float
270 decode_f32 = Binary.Get.runGet Binary.Get.getFloatbe
271
272 -- | Little-endian variant of 'decode_f32'.
273 decode_f32_le :: ByteString.Lazy.ByteString -> Float
274 decode_f32_le = Binary.Get.runGet Binary.Get.getFloatle
275
276 -- | Decode a 64-bit IEEE floating point number.
277 decode_f64 :: ByteString.Lazy.ByteString -> Double
278 decode_f64 = Binary.Get.runGet Binary.Get.getDoublebe
279
280 -- * Decode/Ascii
281
282 -- | Decode an Ascii string, inverse of 'encode_ascii'.
283 decode_ascii :: ByteString.Lazy.ByteString -> ByteString.Char8.ByteString
284 {-# INLINE decode_ascii #-}
285 decode_ascii = ByteString.Char8.pack . ByteString.Lazy.Char8.unpack
286
287 -- * IO
288
289 -- | Read /n/ bytes from /h/ and run /f/.
290 read_decode :: (ByteString.Lazy.ByteString -> t) -> Int -> Handle -> IO t
291 read_decode f n = fmap f . flip ByteString.Lazy.hGet n
292
293 -- | Type-specialised reader for 'Binary.decode'.
294 read_word32 :: Handle -> IO Word32
295 read_word32 = read_decode Binary.decode 4
296
297 -- | 'read_decode' of 'decode_word32_le'.
298 read_word32_le :: Handle -> IO Word32
299 read_word32_le = read_decode decode_word32_le 4
300
301 -- | 'ByteString.Lazy.hPut' of 'encode_word32'.
302 write_word32 :: Handle -> Word32 -> IO ()
303 write_word32 h = ByteString.Lazy.hPut h . encode_word32
304
305 -- | 'ByteString.Lazy.hPut' of 'encode_word32_le'.
306 write_word32_le :: Handle -> Word32 -> IO ()
307 write_word32_le h = ByteString.Lazy.hPut h . encode_word32_le
308
309 -- * Io/Int
310
311 -- | 'decode_i8' of 'ByteString.Lazy.hGet'.
312 read_i8 :: Handle -> IO Int
313 read_i8 = read_decode decode_i8 1
314
315 -- | 'decode_i16' of 'ByteString.Lazy.hGet'.
316 read_i16 :: Handle -> IO Int
317 read_i16 = read_decode decode_i16 2
318
319 -- | 'decode_i32' of 'ByteString.Lazy.hGet'.
320 read_i32 :: Handle -> IO Int
321 read_i32 = read_decode decode_i32 4
322
323 -- | 'decode_i32_le' of 'ByteString.Lazy.hGet'.
324 read_i32_le :: Handle -> IO Int
325 read_i32_le = read_decode decode_i32_le 4
326
327 -- | 'decode_u32' of 'ByteString.Lazy.hGet'.
328 read_u32 :: Handle -> IO Int
329 read_u32 = read_decode decode_u32 4
330
331 -- | 'decode_u32_le' of 'ByteString.Lazy.hGet'.
332 read_u32_le :: Handle -> IO Int
333 read_u32_le = read_decode decode_u32_le 4
334
335 -- | 'ByteString.Lazy.hPut' of 'encode_u32'.
336 write_u32 :: Handle -> Int -> IO ()
337 write_u32 h = ByteString.Lazy.hPut h . encode_u32
338
339 -- | 'ByteString.Lazy.hPut' of 'encode_u32_le'.
340 write_u32_le :: Handle -> Int -> IO ()
341 write_u32_le h = ByteString.Lazy.hPut h . encode_u32_le
342
343 -- * Io/Float
344
345 -- | 'decode_f32' of 'ByteString.Lazy.hGet'.
346 read_f32 :: Handle -> IO Float
347 read_f32 = read_decode decode_f32 4
348
349 -- | 'decode_f32_le' of 'ByteString.Lazy.hGet'.
350 read_f32_le :: Handle -> IO Float
351 read_f32_le = read_decode decode_f32_le 4
352
353 -- * Io/Ascii
354
355 -- | Read u8 length prefixed Ascii string (pascal string).
356 read_pstr :: Handle -> IO ByteString.Char8.ByteString
357 read_pstr h = do
358 n <- fmap decode_u8 (ByteString.Lazy.hGet h 1)
359 fmap decode_ascii (ByteString.Lazy.hGet h n)
360
361 -- * Util
362
363 {- | Bundle header as a (strict) 'ByteString.Char8.ByteString'.
364
365 >>> ByteString.Char8.length bundleHeader_strict
366 8
367 -}
368 bundleHeader_strict :: ByteString.Char8.ByteString
369 bundleHeader_strict = ByteString.Char8.pack "#bundle\0"
370
371 {- | Bundle header as a lazy ByteString.
372
373 >>> ByteString.Lazy.length bundleHeader
374 8
375 -}
376 bundleHeader :: ByteString.Lazy.ByteString
377 {-# INLINE bundleHeader #-}
378 bundleHeader = ByteString.Lazy.Char8.fromChunks [bundleHeader_strict]
379
380 {- | The number of bytes required to align an Osc value to the next 4-byte boundary.
381
382 >>> map align [0::Int .. 7]
383 [0,3,2,1,0,3,2,1]
384
385 >>> map align [512::Int .. 519]
386 [0,3,2,1,0,3,2,1]
387 -}
388 align :: (Num i, Bits i) => i -> i
389 {-# INLINE align #-}
390 align n = ((n + 3) .&. complement 3) - n
391
392 -- * ByteString
393
394 -- | Is machine little endian?
395 isLittleEndian :: Bool
396 isLittleEndian = GHC.ByteOrder.targetByteOrder == GHC.ByteOrder.LittleEndian
397
398 -- | Byte-swap byte string in four-byte segments.
399 byteStringSwap32BitWords :: ByteString.ByteString -> ByteString.ByteString
400 byteStringSwap32BitWords xs =
401 ByteString.Internal.unsafePackLenBytes
402 (ByteString.length xs `quot` 4 * 4)
403 [ ByteString.Unsafe.unsafeIndex xs $ i * 4 + j
404 | i <- [0 .. ByteString.length xs `quot` 4 - 1]
405 , j <- [3, 2, 1, 0]
406 ]
407
408 -- | If target is little-endian, swap bytes to be in network order, else identity.
409 byteString32BitNetworkOrder :: ByteString.ByteString -> ByteString.ByteString
410 byteString32BitNetworkOrder x = if isLittleEndian then byteStringSwap32BitWords x else x