]> Cypherpunks.ru repositories - netstring.git/blob - r.go
Wrapped errors
[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         size, err := strconv.ParseUint(string(lenRaw[:len(lenRaw)-1]), 10, 64)
52         if err != nil {
53                 return 0, fmt.Errorf("netstring header: %w", err)
54         }
55         r.left = size
56         r.eof = false
57         return size, nil
58 }
59
60 func (r *Reader) checkTerminator() error {
61         b, err := r.r.ReadByte()
62         if err != nil {
63                 return fmt.Errorf("netstring terminator: %w", err)
64         }
65         if b != ',' {
66                 return errors.New("netstring terminator: not found")
67         }
68         return nil
69 }
70
71 // Read current netstring chunk.
72 // This method must be called after Next().
73 // Terminator check and error raising are performed only at the end.
74 func (r *Reader) Read(buf []byte) (n int, err error) {
75         if r.eof {
76                 return 0, io.EOF
77         }
78         if uint64(len(buf)) > r.left {
79                 buf = buf[:r.left]
80         }
81         n, err = r.r.Read(buf)
82         r.left -= uint64(n)
83         if r.left == 0 {
84                 err = r.checkTerminator()
85                 if err == nil {
86                         r.eof = true
87                 }
88         }
89         return
90 }
91
92 // Discard remaining bytes in the chunk, possibly fully skipping it.
93 // This method must be called after Next().
94 func (r *Reader) Discard() (err error) {
95         _, err = r.r.Discard(int(r.left))
96         if err != nil {
97                 return
98         }
99         err = r.checkTerminator()
100         if err != nil {
101                 return
102         }
103         r.eof = true
104         return
105 }