Test Failed
Push — master ( 17f9f7...fd8660 )
by Nikita
01:46
created

decode.isStorageContainer   A

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
dl 0
loc 2
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0
nop 1
1
package decode
2
3
import (
4
	"fmt"
5
	"io"
6
7
	"github.com/et-nik/binngo/binn"
8
	"github.com/et-nik/binngo/encode"
9
)
10
11
func readValue(btype binn.Type, reader io.Reader) ([]byte, error) {
12 1
	tp := btype &^ binn.StorageTypeMask
13
14 1
	var readingSize int
15 1
	var containerSize int
16
17 1
	var bytes []byte
18
19 1
	switch tp {
20
	case binn.StorageNoBytes:
21 1
		readingSize = 0
22
	case binn.StorageByte:
23 1
		readingSize = 1
24
	case binn.StorageWord:
25 1
		readingSize = 2
26
	case binn.StorageDWord:
27 1
		readingSize = 4
28
	case binn.StorageQWord:
29 1
		readingSize = 8
30
	case binn.StorageString:
31 1
		dataSize, _, err := readSize(reader)
32 1
		if err != nil {
33
			return nil, fmt.Errorf("failed to read string storage size: %w", err)
34
		}
35 1
		readingSize = dataSize + 1 // data size and null terminator
36
	case binn.StorageBlob:
37 1
		dataSize, _, err := readSize(reader)
38 1
		if err != nil {
39
			return nil, fmt.Errorf("failed to read string storage size: %w", err)
40
		}
41 1
		readingSize = dataSize
42
	case binn.StorageContainer:
43 1
		s, l, err := readSize(reader)
44 1
		if err != nil {
45
			return nil, fmt.Errorf("failed to read storage size: %w", err)
46
		}
47
48 1
		containerSize = s
49
50 1
		bytes = append(bytes, encode.Int(s)...)
51
52 1
		readingSize = containerSize - 1 - int(l) // minus container type byte and size byte
53
	default:
54
		return nil, ErrUnknownType
55
	}
56
57 1
	if readingSize == 0 {
58 1
		return []byte{byte(btype)}, nil
59
	}
60
61 1
	b := make([]byte, readingSize)
62
63 1
	_, err := reader.Read(b)
64 1
	bytes = append(bytes, b...)
65
66 1
	if err != nil {
67
		return nil, fmt.Errorf("failed to read storage: %w", err)
68
	}
69
70 1
	return bytes, nil
71
}
72
73
func isStorageContainer(btype binn.Type) bool {
74 1
	return (btype &^ binn.StorageTypeMask) == binn.StorageContainer
75
}
76 1
77 1
func readType(reader io.Reader) (binn.Type, readLen, error) {
78 1
	var bt = make([]byte, 1)
79
80
	_, err := reader.Read(bt)
81 1
	if err != nil {
82
		return binn.Null, 0, ErrFailedToReadType
83
	}
84
85 1
	return Type(bt), 1, nil
86 1
}
87 1
88
func readSize(reader io.Reader) (int, readLen, error) {
89
	var bsz = make([]byte, 1)
90
	_, err := reader.Read(bsz)
91 1
	if err != nil {
92
		return 0, 0, ErrFailedToReadSize
93 1
	}
94
95 1
	read := 1
96
97
	sz := int(Uint8(bsz))
98
99
	if sz > maxOneByteSize {
100
		var bszOtherBytes = make([]byte, 3)
101
		_, err := reader.Read(bszOtherBytes)
102
		if err != nil {
103
			return 0, 0, fmt.Errorf("failed to read long size: %w", err)
104
		}
105
		read += 3
106
		sz ^= 0x80000000
107
108
		sz = int(Uint32([]byte{
109
			byte(sz),
110
			bszOtherBytes[0],
111
			bszOtherBytes[1],
112 1
			bszOtherBytes[2],
113
		}))
114
	}
115
116
	return sz, readLen(read), nil
117
}
118