]> Cypherpunks.ru repositories - netstring.git/blob - r.go
9b8f51069e541d86986497e383dec57349f563b8
[netstring.git] / r.go
1 /*
2 netstring -- netstring format serialization library
3 Copyright (C) 2015-2023 Sergey Matveev <stargrave@stargrave.org>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, version 3 of the License.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 package netstring
19
20 import (
21         "bufio"
22         "errors"
23         "fmt"
24         "io"
25         "strconv"
26 )
27
28 type Reader struct {
29         r    *bufio.Reader
30         left uint64
31         eof  bool
32 }
33
34 // Create new Reader.
35 // Pay attention that bufio.Reader is used to read from it.
36 func NewReader(r io.Reader) *Reader {
37         return &Reader{r: bufio.NewReader(r), eof: true}
38 }
39
40 // Parse netstring prefix. It returns data length.
41 // After this method you have to call either Read() or Discard().
42 // io.EOF is returned when underlying reader has no data anymore.
43 func (r *Reader) Next() (uint64, error) {
44         if !r.eof {
45                 return 0, errors.New("current chunk is unread")
46         }
47         lenRaw, err := r.r.ReadSlice(':')
48         if err != nil {
49                 return 0, fmt.Errorf("netstring header: %w", err)
50         }
51         lenRaw = lenRaw[:len(lenRaw)-1]
52         if len(lenRaw) > 1 && lenRaw[0] == '0' {
53                 return 0, errors.New("netstring header: leading zero")
54         }
55         size, err := strconv.ParseUint(string(lenRaw), 10, 64)
56         if err != nil {
57                 return 0, fmt.Errorf("netstring header: %w", err)
58         }
59         r.left = size
60         r.eof = false
61         return size, nil
62 }
63
64 func (r *Reader) checkTerminator() error {
65         b, err := r.r.ReadByte()
66         if err != nil {
67                 return fmt.Errorf("netstring terminator: %w", err)
68         }
69         if b != ',' {
70                 return errors.New("netstring terminator: not found")
71         }
72         return nil
73 }
74
75 // Read current netstring chunk.
76 // This method must be called after Next().
77 // Terminator check and error raising are performed only at the end.
78 func (r *Reader) Read(buf []byte) (n int, err error) {
79         if r.eof {
80                 return 0, io.EOF
81         }
82         if uint64(len(buf)) > r.left {
83                 buf = buf[:r.left]
84         }
85         n, err = r.r.Read(buf)
86         r.left -= uint64(n)
87         if r.left == 0 {
88                 err = r.checkTerminator()
89                 if err == nil {
90                         r.eof = true
91                 }
92         }
93         return
94 }
95
96 // Discard remaining bytes in the chunk, possibly fully skipping it.
97 // This method must be called after Next().
98 func (r *Reader) Discard() (err error) {
99         _, err = r.r.Discard(int(r.left))
100         if err != nil {
101                 return
102         }
103         err = r.checkTerminator()
104         if err != nil {
105                 return
106         }
107         r.eof = true
108         return
109 }