Debian Codegraph @

ntlm/payload.go

raw symbols SCIP
1 //Copyright 2013 Thomson Reuters Global Resources. BSD License please see License file for more information
2
3 package ntlm
4
5 import (
6 "bytes"
7 "encoding/binary"
8 "encoding/hex"
9 )
10
11 const (
12 UnicodeStringPayload = iota
13 OemStringPayload
14 BytesPayload
15 )
16
17 type PayloadStruct struct {
18 Type int
19 Len uint16
20 MaxLen uint16
21 Offset uint32
22 Payload []byte
23 }
24
25 func (p *PayloadStruct) Bytes() []byte {
26 dest := make([]byte, 0, 8)
27 buffer := bytes.NewBuffer(dest)
28
29 binary.Write(buffer, binary.LittleEndian, p.Len)
30 binary.Write(buffer, binary.LittleEndian, p.MaxLen)
31 binary.Write(buffer, binary.LittleEndian, p.Offset)
32
33 return buffer.Bytes()
34 }
35
36 func (p *PayloadStruct) String() string {
37 var returnString string
38
39 switch p.Type {
40 case UnicodeStringPayload:
41 returnString = utf16ToString(p.Payload)
42 case OemStringPayload:
43 returnString = string(p.Payload)
44 case BytesPayload:
45 returnString = hex.EncodeToString(p.Payload)
46 default:
47 returnString = "unknown type"
48 }
49 return returnString
50 }
51
52 func CreateBytePayload(bytes []byte) (*PayloadStruct, error) {
53 p := new(PayloadStruct)
54 p.Type = BytesPayload
55 p.Len = uint16(len(bytes))
56 p.MaxLen = uint16(len(bytes))
57 p.Payload = bytes // TODO: Copy these bytes instead of keeping a reference
58 return p, nil
59 }
60
61 func CreateStringPayload(value string) (*PayloadStruct, error) {
62 // Create UTF16 unicode bytes from string
63 bytes := utf16FromString(value)
64 p := new(PayloadStruct)
65 p.Type = UnicodeStringPayload
66 p.Len = uint16(len(bytes))
67 p.MaxLen = uint16(len(bytes))
68 p.Payload = bytes // TODO: Copy these bytes instead of keeping a reference
69 return p, nil
70 }
71
72 func ReadStringPayload(startByte int, bytes []byte) (*PayloadStruct, error) {
73 return ReadPayloadStruct(startByte, bytes, UnicodeStringPayload)
74 }
75
76 func ReadBytePayload(startByte int, bytes []byte) (*PayloadStruct, error) {
77 return ReadPayloadStruct(startByte, bytes, BytesPayload)
78 }
79
80 func ReadPayloadStruct(startByte int, bytes []byte, PayloadType int) (*PayloadStruct, error) {
81 p := new(PayloadStruct)
82
83 p.Type = PayloadType
84 p.Len = binary.LittleEndian.Uint16(bytes[startByte : startByte+2])
85 p.MaxLen = binary.LittleEndian.Uint16(bytes[startByte+2 : startByte+4])
86 p.Offset = binary.LittleEndian.Uint32(bytes[startByte+4 : startByte+8])
87
88 if p.Len > 0 {
89 endOffset := p.Offset + uint32(p.Len)
90 p.Payload = bytes[p.Offset:endOffset]
91 }
92
93 return p, nil
94 }