comparison pkg/auth/encode.go @ 4169:91f4b3f56ce2

Moved binary session encoding/decoding into auth package as it is only used there.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Mon, 05 Aug 2019 16:24:40 +0200
parents pkg/misc/encode.go@5b9b8eabcd01
children 6270951dda28
comparison
equal deleted inserted replaced
4168:3ef3f8a99d5e 4169:91f4b3f56ce2
1 // This is Free Software under GNU Affero General Public License v >= 3.0
2 // without warranty, see README.md and license for details.
3 //
4 // SPDX-License-Identifier: AGPL-3.0-or-later
5 // License-Filename: LICENSES/AGPL-3.0.txt
6 //
7 // Copyright (C) 2018 by via donau
8 // – Österreichische Wasserstraßen-Gesellschaft mbH
9 // Software engineering by Intevation GmbH
10 //
11 // Author(s):
12 // * Sascha L. Teichmann <sascha.teichmann@intevation.de>
13
14 package auth
15
16 import (
17 "encoding/binary"
18 "io"
19 )
20
21 // BinReader is a io.Reader to support an error tolerant way
22 // to read big endian encoded binary data.
23 type BinReader struct {
24 io.Reader
25 // Err is the first encountered error.
26 Err error
27 }
28
29 // Read implements io.Reader without any endian awareness.
30 func (r *BinReader) Read(buf []byte) (int, error) {
31 if r.Err != nil {
32 return 0, r.Err
33 }
34 var n int
35 n, r.Err = r.Read(buf)
36 return n, r.Err
37 }
38
39 // ReadBin reads big endian encodes binary data into x.
40 // If an error was encountered before no data is read.
41 func (r *BinReader) ReadBin(x interface{}) {
42 if r.Err == nil {
43 r.Err = binary.Read(r.Reader, binary.BigEndian, x)
44 }
45 }
46
47 // ReadString reads a big endian encoded string from
48 // the underlying io.Reader into *s.
49 // If an error was encountered before no data is read
50 // and *s is unmodified.
51 func (r *BinReader) ReadString(s *string) {
52 if r.Err != nil {
53 return
54 }
55 var l uint32
56 if r.Err = binary.Read(r.Reader, binary.BigEndian, &l); r.Err != nil {
57 return
58 }
59 b := make([]byte, l)
60 if r.Err = binary.Read(r.Reader, binary.BigEndian, b); r.Err != nil {
61 return
62 }
63 *s = string(b)
64 }
65
66 // BinWriter is a io.Writer to support an error tolerant way
67 // to write big endian encoded binary data.
68 type BinWriter struct {
69 io.Writer
70 // Err is the first encountered error.
71 Err error
72 }
73
74 // Write implements io.Writer without any endian awareness.
75 func (w *BinWriter) Write(buf []byte) (int, error) {
76 if w.Err != nil {
77 return 0, w.Err
78 }
79 var n int
80 n, w.Err = w.Writer.Write(buf)
81 return n, w.Err
82 }
83
84 // WriteBin writes x big endian encoded to the underlying io.Writer.
85 // If an error was encountered before no data is written.
86 func (w *BinWriter) WriteBin(x interface{}) {
87 if w.Err == nil {
88 w.Err = binary.Write(w.Writer, binary.BigEndian, x)
89 }
90 }
91
92 // WriteString writes a big endian encoded string to
93 // the underlying io.Writer.
94 // If an error was encountered before no data is written.
95 func (w *BinWriter) WriteString(s string) {
96 if w.Err == nil {
97 w.Err = binary.Write(w.Writer, binary.BigEndian, uint32(len(s)))
98 }
99 if w.Err == nil {
100 w.Err = binary.Write(w.Writer, binary.BigEndian, []byte(s))
101 }
102 }