mirror of
https://github.com/wader/fq.git
synced 2024-11-24 11:16:09 +03:00
06245d1295
Rename buffer to binary. Still some work left what to call buffer/binary in decode code. Document decode value and binary type Fix proper unit padding for tobytes and add still undocumenated extra padding argument. Add some additional binary tests
324 lines
15 KiB
Plaintext
324 lines
15 KiB
Plaintext
$ fq -d mp3 '.headers[0].magic._bits[8:16] | hd' /test.mp3
|
|
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
|
0x0| 44 | D |.: raw bits 0x1-0x1.7 (1)
|
|
$ fq -d mp3 '.headers[0].magic._bits | [.[8:16], .[0:8]] | hd' /test.mp3
|
|
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
|
0x0|44 49| |DI| |.: raw bits 0x0-0x1.7 (2)
|
|
$ fq -d mp3 '.headers[0].magic._bits | [.[8:16], .[0:8]] | tobits | hd' /test.mp3
|
|
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
|
0x0|44 49| |DI| |.: raw bits 0x0-0x1.7 (2)
|
|
$ fq -d mp3 '.headers[0].magic._bits | [.[8:16], .[0:8]] | tobytes | hd' /test.mp3
|
|
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
|
0x0|44 49| |DI| |.: raw bits 0x0-0x1.7 (2)
|
|
$ fq -n '"12" | tobytes | hd'
|
|
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
|
0x0|31 32| |12| |.: raw bits 0x0-0x1.7 (2)
|
|
$ fq -n '"12" | tobits | hd'
|
|
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
|
0x0|31 32| |12| |.: raw bits 0x0-0x1.7 (2)
|
|
$ fq -n '["12", "3"] | tobytes | hd'
|
|
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
|
0x0|31 32 33| |123| |.: raw bits 0x0-0x2.7 (3)
|
|
$ fq -n '["12", "3"] | tobits | hd'
|
|
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
|
0x0|31 32 33| |123| |.: raw bits 0x0-0x2.7 (3)
|
|
$ fq -n '[("11" | hex), ("22" | hex)] | tobits | hd'
|
|
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
|
0x0|11 22| |."| |.: raw bits 0x0-0x1.7 (2)
|
|
# TODO: bug, hexdump uses io.Copy which is byte oritneted
|
|
$ fq -n '[("12" | hex | .bits[4:]), ("34" | hex | .bits[0:4])] | tobits | hd'
|
|
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
|
0x0|23| |#| |.: raw bits 0x0-0x0.7 (1)
|
|
$ fq -d mp3 '.frames[]._bits[0:12] | tonumber' /test.mp3
|
|
4095
|
|
4095
|
|
4095
|
|
$ fq -d mp3 '.headers[0].magic._bits[0:24] | tostring' /test.mp3
|
|
"ID3"
|
|
$ fq -d mp3 '.frames[0].padding | ("", "md5", "base64", "snippet") as $f | tovalue({bits_format: $f})' /test.mp3
|
|
"<5>AAAAAAA="
|
|
"ca9c491ac66b2c62500882e93f3719a8"
|
|
"AAAAAAA="
|
|
"<5>AAAAAAA="
|
|
$ fq -d mp3 -i . /test.mp3
|
|
mp3> [1, 2, 3] | tobytes
|
|
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
|
0x0|01 02 03| |...| |.: raw bits 0x0-0x2.7 (3)
|
|
mp3> [1, 2, 3, [1, 2, 3]] | tobytes
|
|
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
|
0x0|01 02 03 01 02 03| |......| |.: raw bits 0x0-0x5.7 (6)
|
|
mp3> [1, 2, 3, [1, 2, 3], .headers[0].magic] | tobytes
|
|
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
|
0x0|01 02 03 01 02 03 49 44 33| |......ID3| |.: raw bits 0x0-0x8.7 (9)
|
|
mp3> [-1] | tobytes
|
|
error: byte in binary list must be bytes (0-255) got -1
|
|
mp3> [256] | tobytes
|
|
error: byte in binary list must be bytes (0-255) got 256
|
|
mp3> ^D
|
|
$ fq -d mp3 -i . /test.mp3
|
|
mp3> .frames[1] | tobits | ., .start, .stop, .size, .[4:17], (tobits, tobytes, tobitsrange, tobytesrange | ., .start, .stop, .size, .[4:17])
|
|
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
|
0x00|ff fb 50 c4 00 00 0a 2c 43 2e 55 94 80 01 80 93|..P....,C.U.....|.: raw bits 0x0-0xcf.7 (208)
|
|
* |until 0xcf.7 (end) (208) | |
|
|
0
|
|
1664
|
|
1664
|
|
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
|
0x0|ff fb 50 |..P |.: raw bits 0x0.4-0x2 (1.5)
|
|
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
|
0x00|ff fb 50 c4 00 00 0a 2c 43 2e 55 94 80 01 80 93|..P....,C.U.....|.: raw bits 0x0-0xcf.7 (208)
|
|
* |until 0xcf.7 (end) (208) | |
|
|
0
|
|
1664
|
|
1664
|
|
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
|
0x0|ff fb 50 |..P |.: raw bits 0x0.4-0x2 (1.5)
|
|
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
|
0x00|ff fb 50 c4 00 00 0a 2c 43 2e 55 94 80 01 80 93|..P....,C.U.....|.: raw bits 0x0-0xcf.7 (208)
|
|
* |until 0xcf.7 (end) (208) | |
|
|
0
|
|
208
|
|
208
|
|
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
|
0x00| 00 00 0a 2c 43 2e 55 94 80 01 80 93| ...,C.U.....|.: raw bits 0x4-0x10.7 (13)
|
|
0x10|6b |k |
|
|
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
|
0x00|ff fb 50 c4 00 00 0a 2c 43 2e 55 94 80 01 80 93|..P....,C.U.....|.: raw bits 0x0-0xcf.7 (208)
|
|
* |until 0xcf.7 (end) (208) | |
|
|
0
|
|
1664
|
|
1664
|
|
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
|
0x0|ff fb 50 |..P |.: raw bits 0x0.4-0x2 (1.5)
|
|
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
|
0x00|ff fb 50 c4 00 00 0a 2c 43 2e 55 94 80 01 80 93|..P....,C.U.....|.: raw bits 0x0-0xcf.7 (208)
|
|
* |until 0xcf.7 (end) (208) | |
|
|
0
|
|
208
|
|
208
|
|
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
|
0x00| 00 00 0a 2c 43 2e 55 94 80 01 80 93| ...,C.U.....|.: raw bits 0x4-0x10.7 (13)
|
|
0x10|6b |k |
|
|
mp3> .frames[1] | tobytes | ., .start, .stop, .size, .[4:17], (tobits, tobytes, tobitsrange, tobytesrange | ., .start, .stop, .size, .[4:17])
|
|
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
|
0x00|ff fb 50 c4 00 00 0a 2c 43 2e 55 94 80 01 80 93|..P....,C.U.....|.: raw bits 0x0-0xcf.7 (208)
|
|
* |until 0xcf.7 (end) (208) | |
|
|
0
|
|
208
|
|
208
|
|
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
|
0x00| 00 00 0a 2c 43 2e 55 94 80 01 80 93| ...,C.U.....|.: raw bits 0x4-0x10.7 (13)
|
|
0x10|6b |k |
|
|
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
|
0x00|ff fb 50 c4 00 00 0a 2c 43 2e 55 94 80 01 80 93|..P....,C.U.....|.: raw bits 0x0-0xcf.7 (208)
|
|
* |until 0xcf.7 (end) (208) | |
|
|
0
|
|
1664
|
|
1664
|
|
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
|
0x0|ff fb 50 |..P |.: raw bits 0x0.4-0x2 (1.5)
|
|
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
|
0x00|ff fb 50 c4 00 00 0a 2c 43 2e 55 94 80 01 80 93|..P....,C.U.....|.: raw bits 0x0-0xcf.7 (208)
|
|
* |until 0xcf.7 (end) (208) | |
|
|
0
|
|
208
|
|
208
|
|
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
|
0x00| 00 00 0a 2c 43 2e 55 94 80 01 80 93| ...,C.U.....|.: raw bits 0x4-0x10.7 (13)
|
|
0x10|6b |k |
|
|
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
|
0x00|ff fb 50 c4 00 00 0a 2c 43 2e 55 94 80 01 80 93|..P....,C.U.....|.: raw bits 0x0-0xcf.7 (208)
|
|
* |until 0xcf.7 (end) (208) | |
|
|
0
|
|
1664
|
|
1664
|
|
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
|
0x0|ff fb 50 |..P |.: raw bits 0x0.4-0x2 (1.5)
|
|
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
|
0x00|ff fb 50 c4 00 00 0a 2c 43 2e 55 94 80 01 80 93|..P....,C.U.....|.: raw bits 0x0-0xcf.7 (208)
|
|
* |until 0xcf.7 (end) (208) | |
|
|
0
|
|
208
|
|
208
|
|
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
|
0x00| 00 00 0a 2c 43 2e 55 94 80 01 80 93| ...,C.U.....|.: raw bits 0x4-0x10.7 (13)
|
|
0x10|6b |k |
|
|
mp3> .frames[1] | tobitsrange | ., .start, .stop, .size, .[4:17], (tobits, tobytes, tobitsrange, tobytesrange | ., .start, .stop, .size, .[4:17])
|
|
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
|
0x0e0| ff fb 50 c4 00 00 0a 2c 43 2e 55 94 80| ..P....,C.U..|.: raw bits 0xe3-0x1b2.7 (208)
|
|
0x0f0|01 80 93 6b 27 30 80 00 07 aa c3 8e 33 85 d3 64|...k'0......3..d|
|
|
* |until 0x1b2.7 (208) | |
|
|
1816
|
|
3480
|
|
1664
|
|
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
|
0xe0| ff fb 50 | ..P |.: raw bits 0xe3.4-0xe5 (1.5)
|
|
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
|
0x00|ff fb 50 c4 00 00 0a 2c 43 2e 55 94 80 01 80 93|..P....,C.U.....|.: raw bits 0x0-0xcf.7 (208)
|
|
* |until 0xcf.7 (end) (208) | |
|
|
0
|
|
1664
|
|
1664
|
|
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
|
0x0|ff fb 50 |..P |.: raw bits 0x0.4-0x2 (1.5)
|
|
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
|
0x00|ff fb 50 c4 00 00 0a 2c 43 2e 55 94 80 01 80 93|..P....,C.U.....|.: raw bits 0x0-0xcf.7 (208)
|
|
* |until 0xcf.7 (end) (208) | |
|
|
0
|
|
208
|
|
208
|
|
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
|
0x00| 00 00 0a 2c 43 2e 55 94 80 01 80 93| ...,C.U.....|.: raw bits 0x4-0x10.7 (13)
|
|
0x10|6b |k |
|
|
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
|
0x0e0| ff fb 50 c4 00 00 0a 2c 43 2e 55 94 80| ..P....,C.U..|.: raw bits 0xe3-0x1b2.7 (208)
|
|
0x0f0|01 80 93 6b 27 30 80 00 07 aa c3 8e 33 85 d3 64|...k'0......3..d|
|
|
* |until 0x1b2.7 (208) | |
|
|
1816
|
|
3480
|
|
1664
|
|
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
|
0xe0| ff fb 50 | ..P |.: raw bits 0xe3.4-0xe5 (1.5)
|
|
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
|
0x0e0| ff fb 50 c4 00 00 0a 2c 43 2e 55 94 80| ..P....,C.U..|.: raw bits 0xe3-0x1b2.7 (208)
|
|
0x0f0|01 80 93 6b 27 30 80 00 07 aa c3 8e 33 85 d3 64|...k'0......3..d|
|
|
* |until 0x1b2.7 (208) | |
|
|
227
|
|
435
|
|
208
|
|
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
|
0xe0| 00 00 0a 2c 43 2e 55 94 80| ...,C.U..|.: raw bits 0xe7-0xf3.7 (13)
|
|
0xf0|01 80 93 6b |...k |
|
|
mp3> .frames[1] | tobytesrange | ., .start, .stop, .size, .[4:17], (tobits, tobytes, tobitsrange, tobytesrange | ., .start, .stop, .size, .[4:17])
|
|
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
|
0x0e0| ff fb 50 c4 00 00 0a 2c 43 2e 55 94 80| ..P....,C.U..|.: raw bits 0xe3-0x1b2.7 (208)
|
|
0x0f0|01 80 93 6b 27 30 80 00 07 aa c3 8e 33 85 d3 64|...k'0......3..d|
|
|
* |until 0x1b2.7 (208) | |
|
|
227
|
|
435
|
|
208
|
|
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
|
0xe0| 00 00 0a 2c 43 2e 55 94 80| ...,C.U..|.: raw bits 0xe7-0xf3.7 (13)
|
|
0xf0|01 80 93 6b |...k |
|
|
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
|
0x00|ff fb 50 c4 00 00 0a 2c 43 2e 55 94 80 01 80 93|..P....,C.U.....|.: raw bits 0x0-0xcf.7 (208)
|
|
* |until 0xcf.7 (end) (208) | |
|
|
0
|
|
1664
|
|
1664
|
|
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
|
0x0|ff fb 50 |..P |.: raw bits 0x0.4-0x2 (1.5)
|
|
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
|
0x00|ff fb 50 c4 00 00 0a 2c 43 2e 55 94 80 01 80 93|..P....,C.U.....|.: raw bits 0x0-0xcf.7 (208)
|
|
* |until 0xcf.7 (end) (208) | |
|
|
0
|
|
208
|
|
208
|
|
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
|
0x00| 00 00 0a 2c 43 2e 55 94 80 01 80 93| ...,C.U.....|.: raw bits 0x4-0x10.7 (13)
|
|
0x10|6b |k |
|
|
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
|
0x0e0| ff fb 50 c4 00 00 0a 2c 43 2e 55 94 80| ..P....,C.U..|.: raw bits 0xe3-0x1b2.7 (208)
|
|
0x0f0|01 80 93 6b 27 30 80 00 07 aa c3 8e 33 85 d3 64|...k'0......3..d|
|
|
* |until 0x1b2.7 (208) | |
|
|
1816
|
|
3480
|
|
1664
|
|
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
|
0xe0| ff fb 50 | ..P |.: raw bits 0xe3.4-0xe5 (1.5)
|
|
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
|
0x0e0| ff fb 50 c4 00 00 0a 2c 43 2e 55 94 80| ..P....,C.U..|.: raw bits 0xe3-0x1b2.7 (208)
|
|
0x0f0|01 80 93 6b 27 30 80 00 07 aa c3 8e 33 85 d3 64|...k'0......3..d|
|
|
* |until 0x1b2.7 (208) | |
|
|
227
|
|
435
|
|
208
|
|
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
|
0xe0| 00 00 0a 2c 43 2e 55 94 80| ...,C.U..|.: raw bits 0xe7-0xf3.7 (13)
|
|
0xf0|01 80 93 6b |...k |
|
|
mp3> "fq" | tobits | [.[range(.size)]] | map(tobits) | tobytes | tostring
|
|
"fq"
|
|
mp3> "fq" | tobits | chunk(range(17)+1) | tobytes | tostring
|
|
"fq"
|
|
"fq"
|
|
"fq"
|
|
"fq"
|
|
"fq"
|
|
"fq"
|
|
"fq"
|
|
"fq"
|
|
"fq"
|
|
"fq"
|
|
"fq"
|
|
"fq"
|
|
"fq"
|
|
"fq"
|
|
"fq"
|
|
"fq"
|
|
"fq"
|
|
mp3> 1 | tobits(range(10)) | hex
|
|
"80"
|
|
"80"
|
|
"40"
|
|
"20"
|
|
"10"
|
|
"08"
|
|
"04"
|
|
"02"
|
|
"01"
|
|
"0080"
|
|
mp3> 1 | tobytes(range(5)) | hex
|
|
"01"
|
|
"01"
|
|
"0001"
|
|
"000001"
|
|
"00000001"
|
|
mp3> range(17) | [range(.) | 1 | tobits] | tobits | hex
|
|
""
|
|
"80"
|
|
"c0"
|
|
"e0"
|
|
"f0"
|
|
"f8"
|
|
"fc"
|
|
"fe"
|
|
"ff"
|
|
"ff80"
|
|
"ffc0"
|
|
"ffe0"
|
|
"fff0"
|
|
"fff8"
|
|
"fffc"
|
|
"fffe"
|
|
"ffff"
|
|
mp3> range(17) | [range(.) | 1 | tobits] | tobytes | hex
|
|
""
|
|
"01"
|
|
"03"
|
|
"07"
|
|
"0f"
|
|
"1f"
|
|
"3f"
|
|
"7f"
|
|
"ff"
|
|
"01ff"
|
|
"03ff"
|
|
"07ff"
|
|
"0fff"
|
|
"1fff"
|
|
"3fff"
|
|
"7fff"
|
|
"ffff"
|
|
mp3> "c9dfdac2f6ef68e5db666b6fbeee66d9c7deda66bebfbfe860bfbfbfe9d1636bbfbebf" | hex | tobits | reduce chunk(8)[] as $c ({h:[],g:[]}; .h += [(0|tobits), $c[0:7]] | .g |= . + [if length % 8 == 0 then (0|tobits) else empty end, $c[7:8]]) | .h, .g | tobytes
|
|
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
|
0x00|64 6f 6d 61 7b 77 34 72 6d 33 35 37 5f 77 33 6c|doma{w4rm357_w3l|.: raw bits 0x0-0x22.7 (35)
|
|
* |until 0x22.7 (end) (35) | |
|
|
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
|
0x0|62 6c 30 67 7d| |bl0g}| |.: raw bits 0x0-0x4.7 (5)
|
|
mp3> .frames[1] | tobytes | mp3_frame | ., ((.header.bitrate | tobitsrange) as $v | tobitsrange | [.[:$v.start], (0xf | tobits), .[$v.start+$v.size:]] | mp3_frame) | .header.bitrate
|
|
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
|
0x0| 50 | P |.header.bitrate: 64000 (5)
|
|
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|
|
|
0x0| f0 | . |.header.bitrate: 15 (bad)
|
|
mp3> ^D
|