forked from hyperledger-labs/go-perun
-
Notifications
You must be signed in to change notification settings - Fork 2
/
string.go
50 lines (41 loc) · 1.33 KB
/
string.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
48
49
50
// Copyright (c) 2019 Chair of Applied Cryptography, Technische Universität
// Darmstadt, Germany. All rights reserved. This file is part of go-perun. Use
// of this source code is governed by a MIT-style license that can be found in
// the LICENSE file.
package wire
import (
"encoding/binary"
"io"
"github.com/pkg/errors"
)
// encodeString writes the length as an uint16 and then the string itself to the io.Writer.
func encodeString(w io.Writer, s string) error {
l := uint16(len(s))
if int(l) != len(s) {
return errors.Errorf("string length exceeded: %d", len(s))
}
if err := binary.Write(w, byteOrder, l); err != nil {
return errors.Wrap(err, "failed to write string length")
}
// Early exit. Plus, io.WriteString will complain about a closed io.Writer
// even if there is nothing left to write
if l == 0 {
return nil
}
_, err := io.WriteString(w, s)
return errors.Wrap(err, "failed to write string")
}
// decodeString reads the length as uint16 and the the string itself from the io.Reader.
func decodeString(r io.Reader, s *string) error {
var l uint16
if err := binary.Read(r, byteOrder, &l); err != nil {
return errors.Wrap(err, "failed to read string length")
}
buf := make([]byte, l)
_, err := io.ReadFull(r, buf)
if err != nil {
return errors.Wrap(err, "failed to read string")
}
*s = string(buf)
return nil
}