Debian Codegraph @

ndr/ndr.go

raw symbols SCIP
1 // Package ndr is DEPRECATED and will be removed from next major revision of gokrb5. Please use gopkg.in/jcmturner/rpc.vX instead. This package is a partial implementation of NDR encoding: http://pubs.opengroup.org/onlinepubs/9629399/chap14.htm
2 package ndr
3
4 import (
5 "bytes"
6 "encoding/binary"
7 "fmt"
8 "math"
9 )
10
11 const (
12 protocolVersion = 1
13 commonHeaderBytes = 8
14 privateHeaderBytes = 8
15 bigEndian = 0
16 littleEndian = 1
17 ascii = 0
18 ebcdic = 1
19 ieee = 0
20 vax = 1
21 cray = 2
22 ibm = 3
23 )
24
25 // CommonHeader implements the NDR common header: https://msdn.microsoft.com/en-us/library/cc243889.aspx
26 type CommonHeader struct {
27 Version uint8
28 Endianness binary.ByteOrder
29 CharacterEncoding uint8
30 //FloatRepresentation uint8
31 HeaderLength uint16
32 Filler []byte
33 }
34
35 // PrivateHeader implements the NDR private header: https://msdn.microsoft.com/en-us/library/cc243919.aspx
36 type PrivateHeader struct {
37 ObjectBufferLength uint32
38 Filler []byte
39 }
40
41 // ReadHeaders processes the bytes to return the NDR Common and Private headers.
42 func ReadHeaders(b *[]byte) (CommonHeader, PrivateHeader, int, error) {
43 ch, p, err := GetCommonHeader(b)
44 if err != nil {
45 return CommonHeader{}, PrivateHeader{}, 0, err
46 }
47 ph, err := GetPrivateHeader(b, &p, &ch.Endianness)
48 if err != nil {
49 return CommonHeader{}, PrivateHeader{}, 0, err
50 }
51 return ch, ph, p, err
52 }
53
54 // GetCommonHeader processes the bytes to return the NDR Common header.
55 func GetCommonHeader(b *[]byte) (CommonHeader, int, error) {
56 //The first 8 bytes comprise the Common RPC Header for type marshalling.
57 if len(*b) < commonHeaderBytes {
58 return CommonHeader{}, 0, Malformed{EText: "Not enough bytes."}
59 }
60 if (*b)[0] != protocolVersion {
61 return CommonHeader{}, 0, Malformed{EText: fmt.Sprintf("Stream does not indicate a RPC Type serialization of version %v", protocolVersion)}
62 }
63 endian := int((*b)[1] >> 4 & 0xF)
64 if endian != 0 && endian != 1 {
65 return CommonHeader{}, 1, Malformed{EText: "Common header does not indicate a valid endianness"}
66 }
67 charEncoding := uint8((*b)[1] & 0xF)
68 if charEncoding != 0 && charEncoding != 1 {
69 return CommonHeader{}, 1, Malformed{EText: "Common header does not indicate a valid charater encoding"}
70 }
71 var bo binary.ByteOrder
72 switch endian {
73 case littleEndian:
74 bo = binary.LittleEndian
75 case bigEndian:
76 bo = binary.BigEndian
77 }
78 l := bo.Uint16((*b)[2:4])
79 if l != commonHeaderBytes {
80 return CommonHeader{}, 4, Malformed{EText: fmt.Sprintf("Common header does not indicate a valid length: %v instead of %v", uint8((*b)[3]), commonHeaderBytes)}
81 }
82
83 return CommonHeader{
84 Version: uint8((*b)[0]),
85 Endianness: bo,
86 CharacterEncoding: charEncoding,
87 //FloatRepresentation: uint8(b[2]),
88 HeaderLength: l,
89 Filler: (*b)[4:8],
90 }, 8, nil
91 }
92
93 // GetPrivateHeader processes the bytes to return the NDR Private header.
94 func GetPrivateHeader(b *[]byte, p *int, bo *binary.ByteOrder) (PrivateHeader, error) {
95 //The next 8 bytes comprise the RPC type marshalling private header for constructed types.
96 if len(*b) < (privateHeaderBytes) {
97 return PrivateHeader{}, Malformed{EText: "Not enough bytes."}
98 }
99 var l uint32
100 buf := bytes.NewBuffer((*b)[*p : *p+4])
101 binary.Read(buf, *bo, &l)
102 if l%8 != 0 {
103 return PrivateHeader{}, Malformed{EText: "Object buffer length not a multiple of 8"}
104 }
105 *p += 8
106 return PrivateHeader{
107 ObjectBufferLength: l,
108 Filler: (*b)[4:8],
109 }, nil
110 }
111
112 // ReadUint8 reads bytes representing a thirty two bit integer.
113 func ReadUint8(b *[]byte, p *int) (i uint8) {
114 if len((*b)[*p:]) < 1 {
115 return
116 }
117 ensureAlignment(p, 1)
118 i = uint8((*b)[*p])
119 *p++
120 return
121 }
122
123 // ReadUint16 reads bytes representing a thirty two bit integer.
124 func ReadUint16(b *[]byte, p *int, e *binary.ByteOrder) (i uint16) {
125 if len((*b)[*p:]) < 2 {
126 return
127 }
128 ensureAlignment(p, 2)
129 i = (*e).Uint16((*b)[*p : *p+2])
130 *p += 2
131 return
132 }
133
134 // ReadUint32 reads bytes representing a thirty two bit integer.
135 func ReadUint32(b *[]byte, p *int, e *binary.ByteOrder) (i uint32) {
136 if len((*b)[*p:]) < 4 {
137 return
138 }
139 ensureAlignment(p, 4)
140 i = (*e).Uint32((*b)[*p : *p+4])
141 *p += 4
142 return
143 }
144
145 // ReadUint64 reads bytes representing a thirty two bit integer.
146 func ReadUint64(b *[]byte, p *int, e *binary.ByteOrder) (i uint64) {
147 if len((*b)[*p:]) < 8 {
148 return
149 }
150 ensureAlignment(p, 8)
151 i = (*e).Uint64((*b)[*p : *p+8])
152 *p += 8
153 return
154 }
155
156 // ReadBytes reads the number of bytes specified.
157 func ReadBytes(b *[]byte, p *int, s int, e *binary.ByteOrder) (r []byte) {
158 if len((*b)[*p:]) < s {
159 return
160 }
161 buf := bytes.NewBuffer((*b)[*p : *p+s])
162 r = make([]byte, s)
163 binary.Read(buf, *e, &r)
164 *p += s
165 return r
166 }
167
168 // ReadBool reads bytes representing a boolean.
169 func ReadBool(b *[]byte, p *int) bool {
170 if len((*b)[*p:]) < 1 {
171 return false
172 }
173 if ReadUint8(b, p) != 0 {
174 return true
175 }
176 return false
177 }
178
179 // ReadIEEEfloat32 reads bytes representing a IEEE formatted 32 bit float.
180 func ReadIEEEfloat32(b *[]byte, p *int, e *binary.ByteOrder) float32 {
181 ensureAlignment(p, 4)
182 return math.Float32frombits(ReadUint32(b, p, e))
183 }
184
185 // ReadIEEEfloat64 reads bytes representing a IEEE formatted 64 bit float.
186 func ReadIEEEfloat64(b *[]byte, p *int, e *binary.ByteOrder) float64 {
187 ensureAlignment(p, 8)
188 return math.Float64frombits(ReadUint64(b, p, e))
189 }
190
191 // ReadConformantVaryingString reads a Conformant and Varying String from the bytes slice.
192 func ReadConformantVaryingString(b *[]byte, p *int, e *binary.ByteOrder) (string, error) {
193 m := ReadUint32(b, p, e) // Max element count
194 o := ReadUint32(b, p, e) // Offset
195 a := ReadUint32(b, p, e) // Actual count
196 if a > (m-o) || o > m {
197 return "", Malformed{EText: fmt.Sprintf("Not enough bytes. Max: %d, Offset: %d, Actual: %d", m, o, a)}
198 }
199 //Unicode string so each element is 2 bytes
200 //move position based on the offset
201 if o > 0 {
202 *p += int(o * 2)
203 }
204 s := make([]rune, a, a)
205 for i := 0; i < len(s); i++ {
206 s[i] = rune(ReadUint16(b, p, e))
207 }
208 ensureAlignment(p, 4)
209 return string(s), nil
210 }
211
212 // ReadUniDimensionalConformantArrayHeader reads a UniDimensionalConformantArrayHeader from the bytes slice.
213 func ReadUniDimensionalConformantArrayHeader(b *[]byte, p *int, e *binary.ByteOrder) int {
214 return int(ReadUint32(b, p, e))
215 }
216
217 func ensureAlignment(p *int, byteSize int) {
218 if byteSize > 0 {
219 if s := *p % byteSize; s != 0 {
220 *p += byteSize - s
221 }
222 }
223 }