]> Cypherpunks.ru repositories - netstring.git/blob - netstring.go
netstring format serialization library
[netstring.git] / netstring.go
1 /*
2 netstring -- netstring format serialization library
3 Copyright (C) 2015 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, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 // netstring format serialization library.
20 //
21 // This is implementation of netstring
22 // (http://cr.yp.to/proto/netstrings.txt) format for binary string
23 // serialization.
24 //
25 //   buf := bytes.NewBuffer(nil)
26 //   w := netstring.NewWriter(buf)
27 //   w.Write([]byte("hello")) // buf contains "5:hello,"
28 //   w.Write([]byte("world!")) // buf contains "5:hello,6:world!,"
29 //   r := netstring.NewReader(buf)
30 //   size, err := r.Iter() // size is 5
31 //   r.Discard() // discard (skip) current netstring ("hello")
32 //   size, err = r.Iter() // size is 6
33 //   out := make([]byte, size)
34 //   r.Read(out) // out contains "world!" bytes
35 //
36 // Pay attention that netstring uses bufio for Reader and Writer.
37 package netstring
38
39 import (
40         "errors"
41 )
42
43 const (
44         MaxPrefixSize = 21
45 )
46
47 var (
48         ErrBufSize    error = errors.New("Invalid destination buffer size")
49         ErrState      error = errors.New("Invalid state")
50         ErrTerminator error = errors.New("Invalid terminator")
51 )