Mercurial > gemma
comparison auth/encode.go @ 215:f345edb409b2
Made serialisation and deserialisation of sessions more robust (fixed a small bug on the way).
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Tue, 24 Jul 2018 11:09:18 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
214:da24df80c25d | 215:f345edb409b2 |
---|---|
1 package auth | |
2 | |
3 import ( | |
4 "encoding/binary" | |
5 "io" | |
6 ) | |
7 | |
8 type binReader struct { | |
9 r io.Reader | |
10 err error | |
11 } | |
12 | |
13 func (r *binReader) read(x interface{}) { | |
14 if r.err == nil { | |
15 r.err = binary.Read(r.r, binary.BigEndian, x) | |
16 } | |
17 } | |
18 | |
19 func (r *binReader) readString(s *string) { | |
20 if r.err != nil { | |
21 return | |
22 } | |
23 var l uint32 | |
24 if r.err = binary.Read(r.r, binary.BigEndian, &l); r.err != nil { | |
25 return | |
26 } | |
27 b := make([]byte, l) | |
28 if r.err = binary.Read(r.r, binary.BigEndian, b); r.err != nil { | |
29 return | |
30 } | |
31 *s = string(b) | |
32 } | |
33 | |
34 type binWriter struct { | |
35 w io.Writer | |
36 err error | |
37 } | |
38 | |
39 func (w *binWriter) write(x interface{}) { | |
40 if w.err == nil { | |
41 w.err = binary.Write(w.w, binary.BigEndian, x) | |
42 } | |
43 } | |
44 | |
45 func (w *binWriter) writeString(s string) { | |
46 if w.err == nil { | |
47 w.err = binary.Write(w.w, binary.BigEndian, uint32(len(s))) | |
48 } | |
49 if w.err == nil { | |
50 w.err = binary.Write(w.w, binary.BigEndian, []byte(s)) | |
51 } | |
52 } |