Skip to content

Commit be141e5

Browse files
committed
Code updates to include more header info
1 parent d8c67d8 commit be141e5

17 files changed

+12427
-77
lines changed

cli/ota/decode.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ type decodeHeaderFlags struct {
3333
func initDecodeHeaderCommand() *cobra.Command {
3434
flags := &decodeHeaderFlags{}
3535
uploadCommand := &cobra.Command{
36-
Use: "decode",
36+
Use: "header-decode",
3737
Short: "OTA firmware header decoder",
3838
Long: "decode OTA firmware header of the given binary file",
3939
Run: func(cmd *cobra.Command, args []string) {

cli/ota/encode.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ type encodeBinaryFlags struct {
3535
func initEncodeBinaryCommand() *cobra.Command {
3636
flags := &encodeBinaryFlags{}
3737
uploadCommand := &cobra.Command{
38-
Use: "encode",
38+
Use: "header-encode",
3939
Short: "OTA firmware encoder",
4040
Long: "encode header firmware to make it compatible with Arduino OTA",
4141
Run: func(cmd *cobra.Command, args []string) {

command/ota/encode.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func Encode(params *EncodeParams) (*string, error) {
3838
}
3939

4040
// Verify if file has already an OTA header
41-
header, _ := ota.DecodeOtaFirmwareHeader(params.File)
41+
header, _ := ota.DecodeOtaFirmwareHeaderFromFile(params.File)
4242
if header != nil {
4343
return nil, fmt.Errorf("file %s contains a valid OTA header. Skip header encoding", params.File)
4444
}

command/ota/generate.go

-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626
inota "github.com/arduino/arduino-cloud-cli/internal/ota"
2727
)
2828

29-
3029
// Generate takes a .bin file and generates a .ota file.
3130
func Generate(binFile string, outFile string, fqbn string) error {
3231

command/ota/massupload.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func MassUpload(ctx context.Context, params *MassUploadParams, cred *config.Cred
6767

6868
if !params.DoNotApplyHeader {
6969
//Verify if file has already an OTA header
70-
header, _ := ota.DecodeOtaFirmwareHeader(params.File)
70+
header, _ := ota.DecodeOtaFirmwareHeaderFromFile(params.File)
7171
if header != nil {
7272
params.DoNotApplyHeader = true
7373
}

command/ota/readheader.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func ReadHeader(params *ReadHeaderParams) error {
4747
}
4848

4949
// Verify if file has already an OTA header
50-
header, err := ota.DecodeOtaFirmwareHeader(params.File)
50+
header, err := ota.DecodeOtaFirmwareHeaderFromFile(params.File)
5151
if err != nil {
5252
return fmt.Errorf("file %s does not contains a valid OTA header: %v", params.File, err)
5353
}

command/ota/upload.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func Upload(ctx context.Context, params *UploadParams, cred *config.Credentials)
6464

6565
if !params.DoNotApplyHeader {
6666
//Verify if file has already an OTA header
67-
header, _ := ota.DecodeOtaFirmwareHeader(params.File)
67+
header, _ := ota.DecodeOtaFirmwareHeaderFromFile(params.File)
6868
if header != nil {
6969
params.DoNotApplyHeader = true
7070
}

coverage_unit.txt

+12,138
Large diffs are not rendered by default.

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ require (
4343
github.com/golang/protobuf v1.5.3 // indirect
4444
github.com/h2non/filetype v1.1.3 // indirect
4545
github.com/hashicorp/hcl v1.0.0 // indirect
46+
github.com/icza/bitio v1.1.0 // indirect
4647
github.com/inconshreveable/mousetrap v1.0.0 // indirect
4748
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
4849
github.com/josharian/intern v1.0.0 // indirect

go.sum

+3
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,9 @@ github.com/howeyc/crc16 v0.0.0-20171223171357-2b2a61e366a6/go.mod h1:JslaLRrzGsO
285285
github.com/iancoleman/strcase v0.2.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho=
286286
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
287287
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
288+
github.com/icza/bitio v1.1.0 h1:ysX4vtldjdi3Ygai5m1cWy4oLkhWTAi+SyO6HC8L9T0=
289+
github.com/icza/bitio v1.1.0/go.mod h1:0jGnlLAx8MKMr9VGnn/4YrvZiprkvBelsVIbA9Jjr9A=
290+
github.com/icza/mighty v0.0.0-20180919140131-cfd07d671de6/go.mod h1:xQig96I1VNBDIWGCdTt54nHt6EeI639SmHycLYL7FkA=
288291
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
289292
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
290293
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A=

internal/lzss/decoder.go

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// This code is a go port of LZSS encoder-decoder (Haruhiko Okumura; public domain)
2+
3+
package lzss
4+
5+
import (
6+
"bytes"
7+
8+
"github.com/icza/bitio"
9+
)
10+
11+
func Decompress(data []byte) []byte {
12+
input := bitio.NewReader(bytes.NewBuffer(data))
13+
output := make([]byte, 0)
14+
15+
buffer := make([]byte, bufsz*2)
16+
for i := 0; i < bufsz-looksz; i++ {
17+
buffer[i] = ' '
18+
}
19+
20+
r := bufsz - looksz
21+
var char byte
22+
var isChar bool
23+
var err error
24+
for {
25+
isChar, err = input.ReadBool()
26+
if err != nil {
27+
break
28+
}
29+
30+
if isChar {
31+
char, err = input.ReadByte()
32+
if err != nil {
33+
break
34+
}
35+
output = append(output, char)
36+
buffer[r] = char
37+
r++
38+
r &= bufsz - 1
39+
} else {
40+
var i, j uint64
41+
i, err = input.ReadBits(idxsz)
42+
if err != nil {
43+
break
44+
}
45+
j, err = input.ReadBits(lensz)
46+
if err != nil {
47+
break
48+
}
49+
50+
for k := 0; k <= int(j)+1; k++ {
51+
char = buffer[(int(i)+k)&(bufsz-1)]
52+
output = append(output, char)
53+
buffer[r] = char
54+
r++
55+
r &= bufsz - 1
56+
}
57+
}
58+
}
59+
60+
return output
61+
}

internal/lzss/decoder_test.go

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package lzss
2+
3+
import (
4+
"bytes"
5+
"os"
6+
"testing"
7+
)
8+
9+
func TestDecode(t *testing.T) {
10+
tests := []struct {
11+
name string
12+
infile string
13+
outfile string
14+
}{
15+
{
16+
name: "blink",
17+
infile: "testdata/blink.lzss",
18+
outfile: "testdata/blink.bin",
19+
},
20+
{
21+
name: "cloud sketch",
22+
infile: "testdata/cloud.lzss",
23+
outfile: "testdata/cloud.bin",
24+
},
25+
{
26+
name: "empty binary",
27+
infile: "testdata/empty.lzss",
28+
outfile: "testdata/empty.bin",
29+
},
30+
}
31+
32+
for _, tt := range tests {
33+
t.Run(tt.name, func(t *testing.T) {
34+
input, err := os.ReadFile(tt.infile)
35+
if err != nil {
36+
t.Fatal("couldn't open test file")
37+
}
38+
39+
want, err := os.ReadFile(tt.outfile)
40+
if err != nil {
41+
t.Fatal("couldn't open test file")
42+
}
43+
44+
got := Decompress(input)
45+
if !bytes.Equal(want, got) {
46+
t.Error("decoding failed", want, got)
47+
}
48+
})
49+
}
50+
}

internal/lzss/testdata/empty.bin

Whitespace-only changes.

internal/lzss/testdata/empty.lzss

Whitespace-only changes.

0 commit comments

Comments
 (0)