Skip to content

Commit

Permalink
fix non-ascii characters handling in to_hex and to_base64 functions
Browse files Browse the repository at this point in the history
to_hex and to_base64 functions were expecting `string` as an input type,
which caused the value to be CastFn'ed to string,
which in turn resulted in raw bytes being cast to []rune (in makeDecodeValueOut).
This operation replaces invalid UTF-8 bytes with 0xFFFD, which then were
passed on to the hex/base64 encoders, resulting in incorrect output.

This patch fixes it by expecting `any` as an input type,
which allows the function to correctly read raw bytes of the input data.
  • Loading branch information
Rogach committed May 16, 2023
1 parent 7eaddb8 commit b0e4da2
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 2 deletions.
4 changes: 2 additions & 2 deletions format/text/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func init() {
}
return bb
})
interp.RegisterFunc0("to_hex", func(_ *interp.Interp, c string) any {
interp.RegisterFunc0("to_hex", func(_ *interp.Interp, c any) any {
br, err := interp.ToBitReader(c)
if err != nil {
return err
Expand Down Expand Up @@ -73,7 +73,7 @@ func init() {
type toBase64Opts struct {
Encoding string
}
interp.RegisterFunc1("_to_base64", func(_ *interp.Interp, c string, opts toBase64Opts) any {
interp.RegisterFunc1("_to_base64", func(_ *interp.Interp, c any, opts toBase64Opts) any {
br, err := interp.ToBitReader(c)
if err != nil {
return err
Expand Down
Binary file added format/text/testdata/random.bin.gz
Binary file not shown.
2 changes: 2 additions & 0 deletions format/text/testdata/to_base64.fqtest
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
$ fq '.uncompressed | to_base64' random.bin.gz -r
8Vr6cUH6erA=
2 changes: 2 additions & 0 deletions format/text/testdata/to_hex.fqtest
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
$ fq '.uncompressed | tohex' random.bin.gz -r
f15afa7141fa7ab0

0 comments on commit b0e4da2

Please sign in to comment.