mirror of
https://github.com/wader/fq.git
synced 2024-12-01 02:30:32 +03:00
f4480c6fe5
interp: Refactor format help and also include options interp: Add -o name=@path to load file content as value (not documented yet, might change) interp,decode: Expose decode out value as _out (might change) interp: Refactor foramts.jq into format_{decode,func,include}.jq interp: Refactor torepr into _format_func for generic format function overloading interp: Refactor -o options parsing to be more generic and collect unknowns options to be used as format options decode of decode alises func for format overloaded functions include for format specific jq functions (also _help, torepr etc) flac_frame: Add bits_per_sample option mp3: Add max_unique_header_config and max_sync_seek options mp4: Add decode_samples and allow_truncate options avc_au: Has length_size option hevc_au: Has length_size option aac_frame: Has object_typee option doc: Rewrite format doc generation, less hack more jq
97 lines
2.1 KiB
Go
97 lines
2.1 KiB
Go
package bencode
|
|
|
|
// https://wiki.theory.org/BitTorrentSpecification#Bencoding
|
|
|
|
import (
|
|
"embed"
|
|
"strconv"
|
|
|
|
"github.com/wader/fq/format"
|
|
"github.com/wader/fq/format/registry"
|
|
"github.com/wader/fq/pkg/decode"
|
|
"github.com/wader/fq/pkg/scalar"
|
|
)
|
|
|
|
//go:embed bencode.jq
|
|
var bencodeFS embed.FS
|
|
|
|
func init() {
|
|
registry.MustRegister(decode.Format{
|
|
Name: format.BENCODE,
|
|
Description: "BitTorrent bencoding",
|
|
DecodeFn: decodeBencode,
|
|
Files: bencodeFS,
|
|
Functions: []string{"torepr", "_help"},
|
|
})
|
|
}
|
|
|
|
var typeToNames = scalar.StrToSymStr{
|
|
"d": "dictionary",
|
|
"i": "integer",
|
|
"l": "list",
|
|
"0": "string",
|
|
"1": "string",
|
|
"2": "string",
|
|
"3": "string",
|
|
"4": "string",
|
|
"5": "string",
|
|
"6": "string",
|
|
"7": "string",
|
|
"8": "string",
|
|
"9": "string",
|
|
}
|
|
|
|
func decodeStrIntUntil(b byte) func(d *decode.D) int64 {
|
|
return func(d *decode.D) int64 {
|
|
// 21 is sign + longest 64 bit in base 10
|
|
i := d.PeekFindByte(b, 21)
|
|
if i == -1 {
|
|
d.Fatalf("decodeStrIntUntil: failed to find %v", b)
|
|
}
|
|
s := d.UTF8(int(i))
|
|
n, err := strconv.ParseInt(s, 10, 64)
|
|
if err != nil {
|
|
d.Fatalf("decodeStrIntUntil: %q: %s", s, err)
|
|
}
|
|
return n
|
|
}
|
|
}
|
|
|
|
func decodeBencodeValue(d *decode.D) {
|
|
typ := d.FieldUTF8("type", 1, typeToNames)
|
|
switch typ {
|
|
case "0", "1", "2", "3", "4", "5", "6", "7", "8", "9":
|
|
d.SeekRel(-8)
|
|
length := d.FieldSFn("length", decodeStrIntUntil(':'))
|
|
d.FieldUTF8("separator", 1, d.AssertStr(":"))
|
|
d.FieldUTF8("value", int(length))
|
|
case "i":
|
|
d.FieldSFn("value", decodeStrIntUntil('e'))
|
|
d.FieldUTF8("end", 1, d.AssertStr("e"))
|
|
case "l":
|
|
d.FieldArray("values", func(d *decode.D) {
|
|
for d.PeekBits(8) != 'e' {
|
|
d.FieldStruct("value", decodeBencodeValue)
|
|
}
|
|
})
|
|
d.FieldUTF8("end", 1, d.AssertStr("e"))
|
|
case "d":
|
|
d.FieldArray("pairs", func(d *decode.D) {
|
|
for d.PeekBits(8) != 'e' {
|
|
d.FieldStruct("pair", func(d *decode.D) {
|
|
d.FieldStruct("key", decodeBencodeValue)
|
|
d.FieldStruct("value", decodeBencodeValue)
|
|
})
|
|
}
|
|
})
|
|
d.FieldUTF8("end", 1, d.AssertStr("e"))
|
|
default:
|
|
d.Fatalf("unknown type %v", typ)
|
|
}
|
|
}
|
|
|
|
func decodeBencode(d *decode.D, in interface{}) interface{} {
|
|
decodeBencodeValue(d)
|
|
return nil
|
|
}
|