Skip to content
/ fq Public
forked from wader/fq

Commit 36b776e

Browse files
committed
Merge branch 'master' into midi
2 parents 2970fb1 + f2f5ca5 commit 36b776e

File tree

25 files changed

+265
-147
lines changed

25 files changed

+265
-147
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ on:
77
pull_request:
88

99
env:
10-
GOLANGCILINT_VERSION: "1.60.1"
10+
GOLANGCILINT_VERSION: "1.60.3"
1111

1212
jobs:
1313
lint:

.golangci.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ linters-settings:
3737
excludes:
3838
- G401
3939
- G501
40+
- G115
41+
- G406
42+
- G506
4043
misspell:
4144
ignore-words:
4245
# elf RELA

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ gogenerate: always
6161
lint: always
6262
# bump: make-golangci-lint /golangci-lint@v([\d.]+)/ git:https://github.com/golangci/golangci-lint.git|^1
6363
# bump: make-golangci-lint link "Release notes" https://github.com/golangci/golangci-lint/releases/tag/v$LATEST
64-
go run github.com/golangci/golangci-lint/cmd/golangci-lint@v1.60.1 run
64+
go run github.com/golangci/golangci-lint/cmd/golangci-lint@v1.60.3 run
6565

6666
depgraph.svg: always
6767
go run github.com/kisielk/godepgraph@latest github.com/wader/fq | dot -Tsvg -o godepgraph.svg

doc/formats.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1261,6 +1261,15 @@ You will often find this format embedded inside the TZX tape format.
12611261

12621262
The default file extension is `.tap`.
12631263

1264+
### Processing JSON files
1265+
1266+
When needing to process a generated JSON file it's recommended to convert the
1267+
plain data bytes to an array by setting `bits_format=byte_array`:
1268+
1269+
```bash
1270+
fq -o bits_format=byte_array -d tap -V d /path/to/file.tap
1271+
```
1272+
12641273
### Authors
12651274

12661275
- Michael R. Cook work.mrc@pm.me, original author
@@ -1451,6 +1460,15 @@ the company Ramsoft became the maintainers, and created revision `v1.20`.
14511460

14521461
The default file extension is `.tzx`.
14531462

1463+
### Processing JSON files
1464+
1465+
When needing to process a generated JSON file it's recommended to convert the
1466+
plain data bytes to an array by setting `bits_format=byte_array`:
1467+
1468+
```bash
1469+
fq -o bits_format=byte_array -d tzx -V d /path/to/file.tzx
1470+
```
1471+
14541472
### Authors
14551473

14561474
- Michael R. Cook work.mrc@pm.me, original author

doc/usage.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ fq -Vr .path.to.string file
5555
fq -o bits_format=truncate tovalue file
5656
# JSON but raw bit fields as md5 hex string
5757
fq -o bits_format=md5 tovalue file
58+
# JSON but raw bit fields as byte arrays
59+
fq -o bits_format=byte_array tovalue file
5860
# look up a path
5961
fq '.some[1].path' file
6062
# look up a path and output JSON
@@ -403,8 +405,8 @@ There is also `tobitsrange` and `tobytesrange` which does the same thing but wil
403405
- `[0x12, 0x34, 0x56] | tobits[4:12]` will be a binary with the byte `0x23`
404406
- `[0x12, 0x34, 0x56] | tobits[4:20]` will be a binary with the byte `0x23`, `0x45`
405407
- `[0x12, 0x34, 0x56] | tobits[4:20] | tobytes[1:]` will be a binary with the byte `0x45`,
406-
407-
Both `.[index]` and `.[start:end]` support negative indices to index from end.
408+
- Both `.[index]` and `.[start:end]` support negative indices to index from end.
409+
- `explode` output an array with all byte or bits as integers.
408410

409411
#### Binary array
410412

@@ -898,12 +900,13 @@ fq has some general options in addition to decode and decoders specific options.
898900

899901
How to represent raw binary as JSON.
900902

901-
- `-o bits_format=string` String with raw bytes (zero bit padded if size is not byte aligned). The string is binary safe internally in fq but bytes not representable as UTF-8 will be lost if turn to JSON.
902-
- `-o bits_format=md5` MD5 hex string (zero bit padded).
903-
- `-o bits_format=hex` Hex string.
904903
- `-o bits_format=base64` Base64 string.
905-
- `-p bits_format=truncate` Truncated string.
904+
- `-o bits_format=byte_array` Array of bytes (zero bit padded if size is not byte aligned).
905+
- `-o bits_format=hex` Hex string.
906+
- `-o bits_format=md5` MD5 hex string (zero bit padded).
906907
- `-o bits_format=snippet` Truncated Base64 string prefixed with bit length.
908+
- `-o bits_format=string` String with raw bytes (zero bit padded if size is not byte aligned). The string is binary safe internally in fq but bytes not representable as UTF-8 will be lost if turn into JSON (default).
909+
- `-p bits_format=truncate` Truncated string.
907910

908911
```sh
909912
$ fq -V -o bits_format=base64 . file

format/json/jq.jq

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def from_jq:
8181
( $v.term.object.key_vals // []
8282
| map(
8383
{ key: (.key // .key_string.str)
84-
, value: (.val.queries[0] | _f)
84+
, value: (.val | _f)
8585
}
8686
)
8787
| from_entries

format/tap/tap.go

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"bufio"
77
"bytes"
88
"embed"
9+
"fmt"
910

1011
"golang.org/x/text/encoding/charmap"
1112

@@ -54,9 +55,11 @@ func decodeTapBlock(d *decode.D) {
5455
// read header, fragment, or data block
5556
switch length {
5657
case 0:
57-
// fragment with no data
58+
d.Fatalf("TAP fragments with 0 bytes are not supported")
5859
case 1:
59-
d.FieldRawLen("data", 8)
60+
d.FieldStruct("data", func(d *decode.D) {
61+
d.FieldRawLen("bytes", 8)
62+
})
6063
case 19:
6164
d.FieldStruct("header", func(d *decode.D) {
6265
decodeHeader(d)
@@ -72,15 +75,34 @@ func decodeTapBlock(d *decode.D) {
7275
func decodeHeader(d *decode.D) {
7376
blockStartPosition := d.Pos()
7477

75-
// Always 0: byte indicating a standard ROM loading header
76-
d.FieldU8("flag", scalar.UintMapSymStr{0: "standard_speed_data"})
78+
// flag indicating the type of header block, usually 0 (standard speed data)
79+
d.FieldU8("flag", scalar.UintFn(func(s scalar.Uint) (scalar.Uint, error) {
80+
if s.Actual == 0x00 {
81+
s.Sym = "standard_speed_data"
82+
} else {
83+
s.Sym = "custom_data_block"
84+
}
85+
return s, nil
86+
}))
87+
7788
// Header type
78-
dataType := d.FieldU8("data_type", scalar.UintMapSymStr{
79-
0x00: "program",
80-
0x01: "numeric",
81-
0x02: "alphanumeric",
82-
0x03: "data",
83-
})
89+
dataType := d.FieldU8("data_type", scalar.UintFn(func(s scalar.Uint) (scalar.Uint, error) {
90+
switch s.Actual {
91+
case 0x00:
92+
s.Sym = "program"
93+
case 0x01:
94+
s.Sym = "numeric"
95+
case 0x02:
96+
s.Sym = "alphanumeric"
97+
case 0x03:
98+
s.Sym = "data"
99+
default:
100+
// unofficial header types
101+
s.Sym = fmt.Sprintf("unknown%02X", s.Actual)
102+
}
103+
return s, nil
104+
}))
105+
84106
// Loading name of the program. Filled with spaces (0x20) to 10 characters.
85107
d.FieldStr("program_name", 10, charmap.ISO8859_1)
86108

@@ -120,7 +142,10 @@ func decodeHeader(d *decode.D) {
120142
// UnusedWord: 32768.
121143
d.FieldU16("unused")
122144
default:
123-
d.Fatalf("invalid TAP header type, got: %d", dataType)
145+
// Unofficial header types
146+
d.FieldU16("data_length")
147+
d.FieldU16("unknown1", scalar.UintHex)
148+
d.FieldU16("unknown2", scalar.UintHex)
124149
}
125150

126151
// Simply all bytes XORed (including flag byte).
@@ -140,7 +165,8 @@ func decodeDataBlock(d *decode.D, length uint64) {
140165
return s, nil
141166
}))
142167
// The essential data: length minus the flag/checksum bytes (may be empty)
143-
d.FieldRawLen("data", int64(length-2)*8)
168+
d.FieldRawLen("bytes", int64(length-2)*8)
169+
144170
// Simply all bytes (including flag byte) XORed
145171
d.FieldU8("checksum", d.UintValidate(calculateChecksum(d, blockStartPosition, d.Pos()-blockStartPosition)), scalar.UintHex)
146172
}

format/tap/tap.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ You will often find this format embedded inside the TZX tape format.
77

88
The default file extension is `.tap`.
99

10+
### Processing JSON files
11+
12+
When needing to process a generated JSON file it's recommended to convert the
13+
plain data bytes to an array by setting `bits_format=byte_array`:
14+
15+
```bash
16+
fq -o bits_format=byte_array -d tap -V d /path/to/file.tap
17+
```
18+
1019
### Authors
1120

1221
- Michael R. Cook work.mrc@pm.me, original author

format/tap/testdata/basic_prog1.fqtest

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ $ fq -d tap dv basic_prog1.tap
1515
0x10| 28 00 | (. | length: 40 0x15-0x17 (2)
1616
| | | data{}: 0x17-0x3f (40)
1717
0x10| ff | . | flag: "standard_speed_data" (255) 0x17-0x18 (1)
18-
0x10| 00 0a 14 00 20 f5 22 66| .... ."f| data: raw bits 0x18-0x3e (38)
18+
0x10| 00 0a 14 00 20 f5 22 66| .... ."f| bytes: raw bits 0x18-0x3e (38)
1919
0x20|71 20 69 73 20 74 68 65 20 62 65 73 74 21 22 0d|q is the best!".|
2020
0x30|00 14 0a 00 ec 31 30 0e 00 00 0a 00 00 0d |.....10....... |
2121
0x30| b6| | .|| checksum: 0xb6 (valid) 0x3e-0x3f (1)

0 commit comments

Comments
 (0)