-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader.go
47 lines (38 loc) · 940 Bytes
/
reader.go
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
package asn1dynamic
import (
"fmt"
"io"
)
type asnReader struct {
reader io.Reader
buff1 []byte
buff2 []byte
}
func NewDataReader(r io.Reader, size int) asnReader {
return asnReader{reader: r, buff1: make([]byte, 512), buff2: make([]byte, 0, size)}
}
func (rd *asnReader) Read() (AsnElm, error) {
dec := NewDecoder()
ln, err := rd.reader.Read(rd.buff1)
debugPrint("ASNReader Read: %d tail: %d", ln, len(rd.buff2))
if err != nil || ln == 0 {
err = fmt.Errorf("ASNReader Read: %s", err.Error())
debugPrintln(err.Error())
return nil, err
}
rd.buff2 = append(rd.buff2, rd.buff1[:ln]...)
tail, ok, err := dec.Parse(rd.buff2)
//debugPrintln(len(tail), ok, err)
if err != nil {
rd.buff2 = rd.buff2[0:0]
err = fmt.Errorf("ASNReader Decode: %s", err.Error())
debugPrintln(err.Error())
return nil, err
}
rd.buff2 = tail
if ok {
debugPrintln("ASNReader Decode: OK")
return dec, nil
}
return nil, nil
}