aboutsummaryrefslogtreecommitdiffstats
path: root/formats/pktline/decoder.go
blob: 34e6f51a4316abb1da8c21874ab846df30df53b7 (plain) (blame)
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
package pktline

import (
	"errors"
	"io"
	"strconv"
)

var (
	// ErrUnderflow is triggered when a line is shorter than the described length
	ErrUnderflow = errors.New("unexpected string length (underflow)")
	// ErrInvalidHeader invalid pktline header
	ErrInvalidHeader = errors.New("invalid header")
	// ErrInvalidLen ivanlid line length found, < 0
	ErrInvalidLen = errors.New("invalid length")
)

// Decoder implements a pkt-line format decoder
type Decoder struct {
	r io.Reader
}

// NewDecoder returns a new Decoder
func NewDecoder(r io.Reader) *Decoder {
	return &Decoder{r}
}

// ReadLine reads and return one pkt-line line from the reader
func (d *Decoder) ReadLine() (string, error) {
	return d.readLine()
}

func (d *Decoder) readLine() (string, error) {
	raw := make([]byte, HeaderLength)
	if _, err := d.r.Read(raw); err != nil {
		return "", err
	}

	header, err := strconv.ParseInt(string(raw), 16, 16)
	if err != nil {
		return "", ErrInvalidHeader
	}

	if header == 0 {
		return "", nil
	}

	exp := int(header - HeaderLength)
	if exp < 0 {
		return "", ErrInvalidLen
	}

	line := make([]byte, exp)
	if read, err := io.ReadFull(d.r, line); err != nil {
		if err == io.ErrUnexpectedEOF && read < exp {
			return "", ErrUnderflow
		}

		return "", err
	}

	return string(line), nil
}

// ReadBlock reads and return multiple pkt-line lines, it stops at the end
// of the reader or if a flush-pkt is reached
func (d *Decoder) ReadBlock() ([]string, error) {
	var o []string

	for {
		line, err := d.readLine()
		if err == io.EOF {
			return o, nil
		}

		if err != nil {
			return o, err
		}

		if err == nil && line == "" {
			return o, nil
		}

		o = append(o, line)
	}
}

// ReadAll read and returns all the lines
func (d *Decoder) ReadAll() ([]string, error) {
	result, err := d.ReadBlock()
	if err != nil {
		return result, err
	}

	for {
		lines, err := d.ReadBlock()
		if err == io.EOF {
			return result, nil
		}

		if err != nil {
			return result, err
		}

		if err == nil && len(lines) == 0 {
			return result, nil
		}

		result = append(result, lines...)
	}
}