Commit 689a60d6 authored by Joshua Gutow's avatar Joshua Gutow

Remove auto-detect & only use base64

parent d6781692
...@@ -8,7 +8,6 @@ import ( ...@@ -8,7 +8,6 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
"regexp"
"sync" "sync"
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
...@@ -21,19 +20,6 @@ var zlibWriterPool = sync.Pool{ ...@@ -21,19 +20,6 @@ var zlibWriterPool = sync.Pool{
}, },
} }
func detectEncoding(data []byte) (string, error) {
// Regular expressions to check for base64 and hex patterns
base64Pattern := "^[A-Za-z0-9+/]*={0,2}$"
hexPattern := "^[0-9a-fA-F]*$"
if regexp.MustCompile(hexPattern).MatchString(string(data)) {
return "hex", nil
} else if regexp.MustCompile(base64Pattern).MatchString(string(data)) {
return "base64", nil
}
return "", fmt.Errorf("Unable to determine encoding for data: %s", data)
}
type Page [PageSize]byte type Page [PageSize]byte
func (p *Page) MarshalJSON() ([]byte, error) { // nosemgrep func (p *Page) MarshalJSON() ([]byte, error) { // nosemgrep
...@@ -53,12 +39,6 @@ func (p *Page) MarshalJSON() ([]byte, error) { // nosemgrep ...@@ -53,12 +39,6 @@ func (p *Page) MarshalJSON() ([]byte, error) { // nosemgrep
func (p *Page) UnmarshalJSON(dat []byte) error { func (p *Page) UnmarshalJSON(dat []byte) error {
// Strip off the `"` characters at the start & end. // Strip off the `"` characters at the start & end.
dat = dat[1 : len(dat)-1] dat = dat[1 : len(dat)-1]
// Detect hex or bas64 encoding. legacy hex encoding is uncompressed
if t, err := detectEncoding(dat); err != nil {
return err
} else if t == "hex" {
return p.UnmarshalText(dat)
}
// Decode b64 then decompress // Decode b64 then decompress
r, err := zlib.NewReader(base64.NewDecoder(base64.StdEncoding, bytes.NewReader(dat))) r, err := zlib.NewReader(base64.NewDecoder(base64.StdEncoding, bytes.NewReader(dat)))
if err != nil { if err != nil {
......
...@@ -47,37 +47,3 @@ func TestCachedPage(t *testing.T) { ...@@ -47,37 +47,3 @@ func TestCachedPage(t *testing.T) {
post5 := p.MerkleRoot() post5 := p.MerkleRoot()
require.NotEqual(t, post4, post5, "and global invalidation works regardless of changed data") require.NotEqual(t, post4, post5, "and global invalidation works regardless of changed data")
} }
func TestDetectEncoding(t *testing.T) {
tests := []struct {
data []byte
errs bool
t string
}{
{
data: []byte("deadbeef"),
errs: false,
t: "hex",
},
{
data: []byte("c3ViamVjdAc=="),
errs: false,
t: "base64",
},
{
data: []byte{0x10, 0xff, 0xe5},
errs: true,
t: "",
},
}
for _, tc := range tests {
res, err := detectEncoding(tc.data)
if tc.errs {
require.Error(t, err)
} else {
require.Equal(t, tc.t, res)
require.NoError(t, err)
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment