2021-09-24 16:41:23 +03:00
## Basic usage
2021-09-18 20:27:46 +03:00
2021-09-19 01:51:15 +03:00
fq tries to behave the same way as jq as much as possible, so you can do:
2021-12-22 21:33:21 +03:00
```sh
fq . file
fq < file
2021-12-29 01:13:23 +03:00
file | fq
2021-12-22 21:33:21 +03:00
fq . < file
2021-12-29 01:13:23 +03:00
fq . *.png * .mp3
fq '.frames[0]' file.mp3
2021-09-19 01:51:15 +03:00
```
2021-12-22 21:33:21 +03:00
Common usages:
```sh
# recursively display decode tree but truncate long arrays
fq d file
fq display file
2021-12-29 01:13:23 +03:00
# display all or more bytes for each value
2021-12-22 21:33:21 +03:00
fq 'd({display_bytes: 0})' file
2021-12-29 01:13:23 +03:00
fq 'd({display_bytes: 200})' file
2021-12-22 21:33:21 +03:00
# recursively display decode tree
fq f file
fq full file
# recursively verbosely display decode tree
fq v file
fq verbose file
# JSON for whole file
fq tovalue file
# recursively look for decode value roots for a format
fq '.. | select(format=="jpeg")' file
# recursively look for first decode value root for a format
fq 'first(.. | select(format=="jpeg"))' file
# recursively look for objects fullfilling condition
fq '.. | select(.type=="trak")?' file
2021-09-18 20:27:46 +03:00
```
2021-09-24 16:41:23 +03:00
## Interactive REPL
2021-09-18 20:27:46 +03:00
2021-12-29 01:13:23 +03:00
The interactive [REPL ](https://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop )
has auto completion and nested REPL support:
2021-09-18 20:27:46 +03:00
2021-09-19 01:51:15 +03:00
```
2021-09-18 20:27:46 +03:00
# start REPL with null input
2021-10-29 19:46:19 +03:00
$ fq -i
null>
2021-09-18 20:27:46 +03:00
# same as
2021-10-29 19:46:19 +03:00
$ fq -ni
null>
2021-09-18 20:27:46 +03:00
# start REPL with one file as input
2021-11-05 17:04:26 +03:00
$ fq -i . doc/file.mp3
2021-10-29 19:46:19 +03:00
mp3>
2021-09-18 20:27:46 +03:00
```
In the REPL you will see a prompt indicating current input and you can type jq expression to evaluate.
2021-09-19 01:51:15 +03:00
```
2021-11-05 17:04:26 +03:00
$ fq -i . doc/file.mp3
# basic arithmetics and jq expressions
2021-09-18 20:27:46 +03:00
mp3> 1+1
2
2021-11-05 17:04:26 +03:00
mp3> 1, 2, 3 | . * 2
2
4
6
mp3> [1, 2, 3] | add
6
2021-10-29 19:46:19 +03:00
# "." is the identity function which just returns current input, the mp3 file.
2021-09-18 20:27:46 +03:00
mp3> .
# access the first frame in the mp3 file
mp3> .frames[0]
2021-10-29 19:46:19 +03:00
# start a new nested REPL with first frame as input
2021-09-18 20:27:46 +03:00
mp3> .frames[0] | repl
2021-09-24 16:41:23 +03:00
# prompt shows "path" to current input and that it's an mp3_frame.
2021-12-29 01:13:23 +03:00
# Ctrl-D to exit REPL or to shell if last REPL
2021-09-24 16:41:23 +03:00
> .frames[0] mp3_frame> ^D
2021-11-05 17:04:26 +03:00
# "jq" value of layer in first frame
mp3> .frames[0].header.layer | tovalue
3
mp3> .frames[0].header.layer * 2
6
# symbolic value, same as "jq" value
mp3> .frames[0].header.layer | tosym
3
# actual underlaying decoded value
mp3> .frames[0].header.layer | toactual
1
# description of value
mp3> .frames[0].header.layer | todescription
"MPEG Layer 3"
2021-09-24 16:41:23 +03:00
mp3> ^D
$
2021-09-18 20:27:46 +03:00
```
2021-12-22 21:33:21 +03:00
Use Ctrl-D to exit and Ctrl-C to interrupt current evaluation.
2021-09-24 16:41:23 +03:00
2021-12-20 17:15:43 +03:00
## Example usages
2021-12-29 01:13:23 +03:00
#### Second mp3 frame header as JSON
2021-12-20 17:15:43 +03:00
```sh
2021-12-20 19:21:07 +03:00
fq '.frames[1].header | tovalue' file.mp3
```
2021-12-29 01:13:23 +03:00
#### Byte start position for the first 10 mp3 frames in an array
2021-12-20 19:21:07 +03:00
```sh
fq '.frames[0:10] | map(tobytesrange.start)' file.mp3
2021-12-20 17:15:43 +03:00
```
2021-12-20 19:21:07 +03:00
2021-12-29 01:13:23 +03:00
#### Decode at range
```sh
# decode byte range 100 to end
fq -d raw 'tobytes[100:] | mp3_frame | d' file.mp3
# decode byte range 10 bytes into .somefield and preseve relative position in file
fq '.somefield | tobytesrange[10:] | mp3_frame | d' file.mp3
```
#### Show AVC SPS difference between two mp4 files
2021-12-20 19:21:07 +03:00
2021-12-20 17:15:43 +03:00
`-n` tells fq to not have an implicit `input` , `f` is function to select out some interesting value, call `diff` with two arguments,
decoded value for `a.mp4` and `b.mp4` filtered thru `f` .
2021-12-20 19:21:07 +03:00
```sh
fq -n 'def f: .. | select(format=="avc_sps"); diff(input|f; input|f)' a.mp4 b.mp4
```
2021-12-29 01:13:23 +03:00
#### Extract first JPEG found in file
2021-12-20 19:21:07 +03:00
Recursively look for first value that is a `jpeg` decode value root. Use `tobytes` to get bytes buffer for value. Redirect bytes to a file.
2021-12-20 17:15:43 +03:00
```sh
fq 'first(.. | select(format=="jpeg")) | tobytes' file > file.jpeg
```
2021-12-29 01:13:23 +03:00
#### Sample size histogram
2021-12-20 19:21:07 +03:00
Recursively look for a all sample size boxes "stsz" and use `?` to ignore errors when doing `.type` on arrays etc. Save reference to box, count unique values, save the max, output the path to the box and output a historgram scaled to 0-100.
2021-12-20 17:15:43 +03:00
```sh
fq '.. | select(.type=="stsz")? as $stsz | .entries | count | max_by(.[1])[1] as $m | ($stsz | topath | path_to_expr), (.[] | "\(.[0]): \((100*.[1]/$m)*"=") \(.[1])") | println' file.mp4
```
2021-12-29 01:13:23 +03:00
#### Find TCP streams that looks like HTTP GET requests in a PCAP file
2021-12-20 19:21:07 +03:00
Use `grep` to recursively find strings matching a regexp.
2021-12-20 17:15:43 +03:00
```sh
fq '.tcp_connections | grep("GET /.* HTTP/1.?")' file.pcap
```
2021-12-29 01:13:23 +03:00
###
#### Widest PNG in a directory
2021-12-20 17:15:43 +03:00
```sh
$ fq -rn '[inputs | [input_filename, first(.chunks[] | select(.type=="IHDR") | .width)]] | max_by(.[1]) | .[0]' *.png
```
2021-12-29 01:13:23 +03:00
#### What values include the byte at position 0x123
2021-12-20 17:15:43 +03:00
```sh
$ fq '.. | select(scalars and in_bytes_range(0x123))' file
```
2021-12-30 16:56:50 +03:00
## The jq language
2021-09-24 16:41:23 +03:00
fq is based on the [jq language ](https://stedolan.github.io/jq/ ) and for basic usage its syntax
is similar to how object and array access looks in JavaScript or JSON path, `.food[10]` etc.
To get the most out of fq it's recommended to learn more about jq, here are some good starting points:
- [jq manual ](https://stedolan.github.io/jq/manual/ )
- jq wiki pages
[jq Language Description ](https://github.com/stedolan/jq/wiki/jq-Language-Description ),
[jq wiki page Cookbook ](https://github.com/stedolan/jq/wiki/Cookbook ),
[FAQ ](https://github.com/stedolan/jq/wiki/FAQ ) and
[Pitfalls ](https://github.com/stedolan/jq/wiki/How-to:-Avoid-Pitfalls )
The most common beginner gotcha is probably jq's use of `;` and `,` . jq uses `;` as argument separator
and `,` as output separator.
To call a function `f` with two arguments use `f(1; 2)` . If you do `f(1, 2)` you pass a single
argument `1, 2` (a lambda expression that output `1` and then output `2` ) to `f` .
2021-09-18 20:27:46 +03:00
2021-09-19 11:27:56 +03:00
## Support formats
See [formats ](formats.md )
2021-09-24 16:41:23 +03:00
## Arguments
TODO: examples, stdin/stdout
2020-06-08 03:29:51 +03:00
< pre sh >
$ fq -h
2021-11-01 19:47:36 +03:00
fq - jq for binary formats
2021-12-04 19:20:12 +03:00
Tool, language and decoders for inspecting binary data.
2021-08-12 21:07:34 +03:00
For more information see https://github.com/wader/fq
Usage: fq [OPTIONS] [--] [EXPR] [FILE...]
2021-10-30 19:35:21 +03:00
2021-09-07 01:26:24 +03:00
--arg NAME VALUE Set variable $NAME to string VALUE
--argjson NAME JSON Set variable $NAME to JSON
2021-09-19 02:18:22 +03:00
--color-output,-C Force color output
2021-09-07 01:26:24 +03:00
--compact-output,-c Compact output
2021-11-16 15:03:45 +03:00
--decode,-d NAME Decode format (probe)
2021-09-07 01:26:24 +03:00
--decode-file NAME PATH Set variable $NAME to decode of file
--formats Show supported formats
--from-file,-f PATH Read EXPR from file
--help,-h Show help
--include-path,-L PATH Include search path
--join-output,-j No newline between outputs
2021-09-19 02:18:22 +03:00
--monochrome-output,-M Force monochrome output
2021-09-07 01:26:24 +03:00
--null-input,-n Null input (use input/0 and inputs/0 to read input)
--null-output,-0 Null byte between outputs
2021-10-18 01:59:35 +03:00
--option,-o KEY=VALUE Set option, eg: color=true (use options/0 to see all options)
2021-11-01 14:02:06 +03:00
--raw-file NAME PATH Set variable $NAME to string content of file
2021-09-07 01:26:24 +03:00
--raw-input,-R Read raw input strings (don't decode)
--raw-output,-r Raw string output (without quotes)
--repl,-i Interactive REPL
--slurp,-s Read (slurp) all inputs into an array
2021-09-20 18:05:24 +03:00
--version,-v Show version
2020-06-08 03:29:51 +03:00
< / pre >
2021-12-19 13:15:19 +03:00
## Color and unicode output
fq by default tries to use colors if possible, this can be disabled with `-M` . You can also
enable useage of unicode characters for improved output by setting the environment
variable `CLIUNICODE` .
## Configuration
To add own functions you can use `init.fq` that will be read from
- `$HOME/Library/Application Support/fq/init.jq` on macOS
- `$HOME/.config/fq/init.jq` on Linux, BSD etc
2022-01-04 23:46:19 +03:00
- `%AppData%\fq\init.jq` on Windows
2021-12-19 13:15:19 +03:00
2021-09-24 16:41:23 +03:00
## Use as script interpreter
2020-06-08 03:29:51 +03:00
2021-09-24 16:41:23 +03:00
fq can be used as a scrip interpreter:
2020-06-08 03:29:51 +03:00
2021-09-24 16:41:23 +03:00
`mp3_duration.jq` :
```jq
#!/usr/bin/env fq -d mp3 -rf
[.frames[].header | .sample_count / .sample_rate] | add
```
2021-09-14 13:56:09 +03:00
2021-09-24 16:41:23 +03:00
## Differences to jq
2020-06-08 03:29:51 +03:00
- [gojq's differences to jq ](https://github.com/itchyny/gojq#difference-to-jq ),
notable is support for arbitrary-precision integers.
2021-09-24 16:41:23 +03:00
- Supports hexdecimal `0xab` , octal `0o77` and binary `0b101` integer literals.
- Try include `include "file?";` that don't fail if file is missing.
- Some values can act as a object with keys even when it's an array, number etc.
2021-12-31 19:13:16 +03:00
- There can be keys hidden from `keys` and `[]` .
2021-09-24 16:41:23 +03:00
- Some values are readonly and can't be updated.
2020-06-08 03:29:51 +03:00
2021-09-24 16:41:23 +03:00
## Functions
2020-06-08 03:29:51 +03:00
- All standard library functions from jq
2021-09-28 00:11:18 +03:00
- Adds a few new general functions:
2022-01-05 23:36:22 +03:00
- `print/0` , `println/0` , `printerr/0` , `printerrln` prints to stdout and stderr.
2021-09-04 02:43:56 +03:00
- `streaks/0` , `streaks_by/1` like `group` but groups streaks based on condition.
2021-11-16 15:03:45 +03:00
- `count/0` , `count_by/1` like `group` but counts groups lengths.
2021-08-18 17:49:56 +03:00
- `debug/1` like `debug/0` but uses arg to produce debug message. `{a: 123} | debug({a}) | ...` .
2021-11-16 15:03:45 +03:00
- `path_to_expr/0` from `["key", 1]` to `".key[1]"` .
- `expr_to_path/0` from `".key[1]"` to `["key", 1]` .
2021-08-18 17:49:56 +03:00
- `diff/2` produce diff object between two values.
2021-11-16 15:03:45 +03:00
- `delta/0` , `delta_by/1` , array with difference between all consecutive pairs.
2021-09-04 12:38:49 +03:00
- `chunk/1` , split array or string into even chunks
2022-01-09 12:43:07 +03:00
- Bitwise functions `band` , `bor` , `bxor` , `bsl` , `bsr` and `bnot` . Works the same as jq math functions,
unary uses input and if more than one argument all as arguments ignoring the input. Ex: `1 | bnot` `bsl(1; 3)`
2021-09-28 00:11:18 +03:00
- Adds some decode value specific functions:
2021-11-19 13:44:23 +03:00
- `root/0` tree root for value
- `buffer_root/0` root value of buffer for value
- `format_root/0` root value of format for value
- `parent/0` parent value
2021-09-28 00:11:18 +03:00
- `parents/0` output parents of value
2021-11-19 13:44:23 +03:00
- `topath/0` path of value. Use `path_to_expr` to get a string representation.
2021-11-03 19:19:33 +03:00
- `tovalue/0` , `tovalue/1` symbolic value if available otherwise actual value
2021-11-05 17:04:26 +03:00
- `toactual/0` actual value (decoded etc)
2021-11-03 19:19:33 +03:00
- `tosym/0` symbolic value (mapped etc)
2021-11-05 17:04:26 +03:00
- `todescription/0` description of value
2022-01-10 20:25:36 +03:00
- `torepr/0` convert decode value into what it reptresents. For example convert msgpack decode value
into a value representing its JSON representation.
2021-12-14 03:45:01 +03:00
- All regexp functions work with buffers as input and pattern argument with these differences
from the string versions:
- All offset and length will be in bytes.
- For `capture` the `.string` value is a buffer.
- If pattern is a buffer it will be matched literally and not as a regexp.
- If pattern is a buffer or flags include "b" each input byte will be read as separate code points
- `scan_toend/1` , `scan_toend/2` works the same as `scan` but output buffer are from start of match to
end of buffer.
instead of possibly multi-byte UTF-8 codepoints. This allows to match raw bytes. Ex: `match("\u00ff"; "b")`
will match the byte `0xff` and not the UTF-8 encoded codepoint for 255, `match("[^\u00ff]"; "b")` will match
all non-`0xff` bytes.
- `grep` functions take 1 or 2 arguments. First is a scalar to match, where a string is
2021-10-15 19:19:59 +03:00
treated as a regexp. A buffer scalar will be matches exact bytes. Second argument are regexp
flags with addition that "b" will treat each byte in the input buffer as a code point, this
2021-12-14 03:45:01 +03:00
makes it possible to match exact bytes.
2021-10-17 02:26:30 +03:00
- `grep/1` , `grep/2` recursively match value and buffer
- `vgrep/1` , `vgrep/2` recursively match value
- `bgrep/1` , `bgrep/2` recursively match buffer
- `fgrep/1` , `fgrep/2` recursively match field name
2021-10-15 19:19:59 +03:00
- Buffers:
2021-11-16 15:03:45 +03:00
- `tobits/0` - Transform input into a bits buffer not preserving source range, will start at zero.
- `tobitsrange/0` - Transform input into a bits buffer preserving source range if possible.
- `tobytes/0` - Transform input into a bytes buffer not preserving source range, will start at zero.
- `tobytesrange/0` - Transform input into a byte buffer preserving source range if possible.
2021-10-15 19:19:59 +03:00
- `buffer[start:end]` , `buffer[:end]` , `buffer[start:]` - Create a sub buffer from start to end in buffer units preserving source range.
2020-06-08 03:29:51 +03:00
- `open` open file for reading
2021-11-16 15:03:45 +03:00
- All decode function takes a optional option argument. The only option currently is `force` to ignore decoder asserts.
For example to decode as mp3 and ignore assets do `mp3({force: true})` or `decode("mp3"; {force: true})` , from command line
you currently have to do `fq -d raw 'mp3({force: true})' file` .
- `decode/0` , `decode/1` , `decode/2` decode format
- `probe/0` , `probe/1` probe and decode format
- `mp3/0` , `mp3/1` , ..., `<name>/0` , `<name>/1` same as `decode(<name>)/1` , `decode(<name>; <opts>)/2` decode as format
2021-12-29 01:13:23 +03:00
- Display shows hexdump/ASCII/tree for decode values and JSON for other values.
- `d/0` /`display/0` display value and truncate long arrays
- `f/0` /`full/0` display value and don't truncate arrays
- `v/0` /`verbose/0` display value verbosely and don't truncate array
2021-11-16 15:03:45 +03:00
- `p/0` /`preview/0` show preview of field tree
- `hd/0` /`hexdump/0` hexdump value
- `repl/0` nested REPL, must be last in a pipeline. `1 | repl` , can "slurp" multiple outputs `1, 2, 3 | repl` .
2020-06-08 03:29:51 +03:00
2021-12-29 01:13:23 +03:00
## Decoded values
2020-06-08 03:29:51 +03:00
2021-09-24 16:41:23 +03:00
When you decode something you will get a decode value. A decode values work like
normal jq values but has special abilities and is used to represent a tree structure of the decoded
2020-06-08 03:29:51 +03:00
binary data. Each value always has a name, type and a bit range.
2021-12-29 01:13:23 +03:00
A value has these special keys (TODO: remove, are internal)
2020-06-08 03:29:51 +03:00
- `_name` name of value
- `_value` jq value of value
- `_start` bit range start
- `_stop` bit range stop
- `_len` bit range length (TODO: rename)
- `_bits` bits in range as a binary
- `_bytes` bits in range as binary using byte units
- `_path` jq path to value
- `_unknown` value is un-decoded gap
- `_symbol` symbolic string representation of value (optional)
- `_description` longer description of value (optional)
- `_format` name of decoded format (optional)
- `_error` error message (optional)
- TODO: unknown gaps
2021-09-24 16:41:23 +03:00
## Binary and IO lists
2020-06-08 03:29:51 +03:00
- TODO: similar to erlang io lists, [], binary, string (utf8) and numbers
## Own decoders and use as library
TODO
2021-09-07 02:38:52 +03:00
## Known issues and useful tricks
2021-09-06 02:24:51 +03:00
2021-09-07 02:38:52 +03:00
### Run interactive mode with no input
2021-09-06 02:24:51 +03:00
```sh
fq -i
null>
```
2020-06-08 03:29:51 +03:00
2021-09-24 16:41:23 +03:00
### `select` fails with `expected an ... but got: ...`
2020-06-08 03:29:51 +03:00
2021-09-24 16:41:23 +03:00
Try add `select(...)?` to catch and ignore type errors in the select expression.
2020-06-08 03:29:51 +03:00
2021-09-07 02:38:52 +03:00
### Manual decode
2020-06-08 03:29:51 +03:00
Sometimes fq fails to decode or you know there is valid data buried inside some binary or maybe
you know the format of some unknown value. Then you can decode manually.
< pre >
# try decode a `mp3_frame` that failed to decode
2021-10-29 19:46:19 +03:00
$ fq -d mp3 '.unknown0 | mp3_frame' file.mp3
2020-06-08 03:29:51 +03:00
# skip first 10 bytes then decode as `mp3_frame`
2021-10-29 19:46:19 +03:00
$ fq -d raw 'tobytes[10:] | mp3_frame' file.mp3
2020-06-08 03:29:51 +03:00
< / pre >
2021-09-24 16:41:23 +03:00
### Use `.` as input and in a positional argument
2021-09-04 12:52:40 +03:00
2021-09-24 16:41:23 +03:00
The expression `.a | f(.b)` might not work as expected. `.` is `.a` when evaluating the arguments so
the positional argument will end up being `.a.b` . Instead do `. as $c | .a | f($c.b)` .
2021-09-04 12:52:40 +03:00
2021-09-07 02:38:52 +03:00
### Building array is slow
2020-06-08 03:29:51 +03:00
2021-09-06 02:24:51 +03:00
Try to use `map` or `foreach` to avoid rebuilding the whole array for each append.
2020-06-08 03:29:51 +03:00
2021-09-07 02:38:52 +03:00
### Use `print` and `println` to produce more friendly compact output
2020-06-08 03:29:51 +03:00
```
> [[0,"a"],[1,"b"]]
[
[
0,
"a"
],
[
1,
"b"
]
]
> [[0,"a"],[1,"b"]] | .[] | "\(.[0]): \(.[1])" | println
0: a
1: b
```
2021-09-06 02:24:51 +03:00
### `repl` argument using function or variable causes `variable not defined`
`true as $verbose | repl({verbose: $verbose})` will currently fail as `repl` is
implemented by rewriting the query to `map(true as $verbose | .) | repl({verbose: $verbose})` .
2021-09-07 02:38:52 +03:00
### `error` produces no output
`null | error` behaves as `empty` .