File: helper.go

package info (click to toggle)
golang-github-marcinbor85-gohex 0.0~git20210308.55fb1c6-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 140 kB
  • sloc: makefile: 4
file content (114 lines) | stat: -rw-r--r-- 3,064 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package gohex

import (
	"encoding/binary"
	"encoding/hex"
	"errors"
	"fmt"
	"io"
	"strings"
)

func calcSum(bytes []byte) byte {
	sum := 0
	for _, b := range bytes {
		sum += int(b)
	}
	sum %= 256
	sum = 256 - sum
	return byte(sum)
}

func checkSum(bytes []byte) error {
	sum := calcSum(bytes[:len(bytes)-1])
	last := bytes[len(bytes)-1]
	if sum != last {
		return errors.New("incorrect checksum (sum = " + fmt.Sprintf("%02X != %02X", sum, last) + ")")
	}
	return nil
}

func checkRecordSize(bytes []byte) error {
	if (int(bytes[0]) + 5) != len(bytes) {
		return errors.New("incorrect data length")
	}
	return nil
}

func checkEOF(bytes []byte) error {
	if bytes[0] != 0 {
		return errors.New("incorrect data length field in eof line")
	}
	if binary.BigEndian.Uint16(bytes[1:3]) != 0 {
		return errors.New("incorrect address field in eof line")
	}
	return nil
}

func getExtendedAddress(bytes []byte) (adr uint32, err error) {
	if bytes[0] != 2 {
		return 0, errors.New("incorrect data length field in extended linear address line")
	}
	if binary.BigEndian.Uint16(bytes[1:3]) != 0 {
		return 0, errors.New("incorrect address field in extended linear address line")
	}
	adr = uint32(binary.BigEndian.Uint16(bytes[4:6])) << (1 << bytes[3])
	return adr, nil
}

func getDataLine(bytes []byte) (adr uint16, data []byte) {
	size := bytes[0]
	adr = binary.BigEndian.Uint16(bytes[1:3])
	data = bytes[4 : size+4]
	return adr, data
}

func getStartAddress(bytes []byte) (adr uint32, err error) {
	if bytes[0] != 4 {
		return 0, errors.New("incorrect data length field in start address line")
	}
	if binary.BigEndian.Uint16(bytes[1:3]) != 0 {
		return 0, errors.New("incorrect address field in start address line")
	}
	adr = binary.BigEndian.Uint32(bytes[4:8])
	return adr, nil
}

func makeDataLine(adr uint16, recordType byte, data []byte) []byte {
	line := make([]byte, 5+len(data))
	line[0] = byte(len(data))
	binary.BigEndian.PutUint16(line[1:3], adr)
	line[3] = recordType
	copy(line[4:], data)
	line[len(line)-1] = calcSum(line[:len(line)-1])
	return line
}

func writeDataLine(writer io.Writer, lineAdr *uint32, byteAdr uint32, lineData *[]byte) error {
	s := strings.ToUpper(hex.EncodeToString(makeDataLine(uint16(*lineAdr&0x0000FFFF), _DATA_RECORD, *lineData)))
	_, err := fmt.Fprintf(writer, ":%s\n", s)
	*lineAdr = byteAdr
	*lineData = []byte{}
	return err
}

func writeStartAddressLine(writer io.Writer, startAdr uint32) error {
	a := make([]byte, 4)
	binary.BigEndian.PutUint32(a, startAdr)
	s := strings.ToUpper(hex.EncodeToString(makeDataLine(0, _START_RECORD, a)))
	_, err := fmt.Fprintf(writer, ":%s\n", s)
	return err
}

func writeExtendedAddressLine(writer io.Writer, extAdr uint32) {
	a := make([]byte, 2)
	binary.BigEndian.PutUint16(a, uint16(extAdr>>16))
	s := strings.ToUpper(hex.EncodeToString(makeDataLine(0, _ADR_32_RECORD, a)))
	fmt.Fprintf(writer, ":%s\n", s)
}

func writeEofLine(writer io.Writer) error {
	s := strings.ToUpper(hex.EncodeToString(makeDataLine(0, _EOF_RECORD, []byte{})))
	_, err := fmt.Fprintf(writer, ":%s\n", s)
	return err
}