summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/couchbase/gomemcached/flexibleFraming.go
blob: a545885fd83ed5de93afff76eeea3fcd5d0bbdd7 (plain)
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
package gomemcached

import (
	"encoding/binary"
	"fmt"
)

type FrameObjType int

const (
	FrameBarrier     FrameObjType = iota
	FrameDurability  FrameObjType = iota
	FrameDcpStreamId FrameObjType = iota
	FrameOpenTracing FrameObjType = iota
	FrameImpersonate FrameObjType = iota
)

const MAX_USER_LEN = 15 // TODO half byte shifting to be implemented
// it's not very efficient so we currently truncate user names
const FAST_USER_LEN = 15

type FrameInfo struct {
	ObjId   FrameObjType
	ObjLen  int
	ObjData []byte
}

var ErrorInvalidOp error = fmt.Errorf("Specified method is not applicable")
var ErrorObjLenNotMatch error = fmt.Errorf("Object length does not match data")

func (f *FrameInfo) Validate() error {
	switch f.ObjId {
	case FrameBarrier:
		if f.ObjLen != 0 {
			return fmt.Errorf("Invalid FrameBarrier - length is %v\n", f.ObjLen)
		} else if f.ObjLen != len(f.ObjData) {
			return ErrorObjLenNotMatch
		}
	case FrameDurability:
		if f.ObjLen != 1 && f.ObjLen != 3 {
			return fmt.Errorf("Invalid FrameDurability - length is %v\n", f.ObjLen)
		} else if f.ObjLen != len(f.ObjData) {
			return ErrorObjLenNotMatch
		}
	case FrameDcpStreamId:
		if f.ObjLen != 2 {
			return fmt.Errorf("Invalid FrameDcpStreamId - length is %v\n", f.ObjLen)
		} else if f.ObjLen != len(f.ObjData) {
			return ErrorObjLenNotMatch
		}
	case FrameOpenTracing:
		if f.ObjLen != 1 {
			return fmt.Errorf("Invalid FrameImpersonate - length is %v\n", f.ObjLen)
		} else if f.ObjLen != len(f.ObjData) {
			return ErrorObjLenNotMatch
		}
	case FrameImpersonate:
	default:
		return fmt.Errorf("Unknown FrameInfo type")
	}
	return nil
}

func (f *FrameInfo) GetStreamId() (uint16, error) {
	if f.ObjId != FrameDcpStreamId {
		return 0, ErrorInvalidOp
	}

	var output uint16
	output = uint16(f.ObjData[0])
	output = output << 8
	output |= uint16(f.ObjData[1])
	return output, nil
}

type DurabilityLvl uint8

const (
	DuraInvalid                    DurabilityLvl = iota // Not used (0x0)
	DuraMajority                   DurabilityLvl = iota // (0x01)
	DuraMajorityAndPersistOnMaster DurabilityLvl = iota // (0x02)
	DuraPersistToMajority          DurabilityLvl = iota // (0x03)
)

func (f *FrameInfo) GetDurabilityRequirements() (lvl DurabilityLvl, timeoutProvided bool, timeoutMs uint16, err error) {
	if f.ObjId != FrameDurability {
		err = ErrorInvalidOp
		return
	}
	if f.ObjLen != 1 && f.ObjLen != 3 {
		err = ErrorObjLenNotMatch
		return
	}

	lvl = DurabilityLvl(uint8(f.ObjData[0]))

	if f.ObjLen == 3 {
		timeoutProvided = true
		timeoutMs = binary.BigEndian.Uint16(f.ObjData[1:2])
	}

	return
}

func incrementMarker(bitsToBeIncremented, byteIncrementCnt *int, framingElen, curObjIdx int) (int, error) {
	for *bitsToBeIncremented >= 8 {
		*byteIncrementCnt++
		*bitsToBeIncremented -= 8
	}
	marker := curObjIdx + *byteIncrementCnt
	if marker > framingElen {
		return -1, fmt.Errorf("Out of bounds")
	}
	return marker, nil
}

func (f *FrameInfo) Bytes() ([]byte, bool) {
	return obj2Bytes(f.ObjId, f.ObjLen, f.ObjData)
}

// TODO implement half byte shifting for impersonate user names
// halfByteRemaining will always be false, because ObjID and Len haven't gotten that large yet
// and user names are truncated
func obj2Bytes(id FrameObjType, len int, data []byte) (output []byte, halfByteRemaining bool) {
	if len < 16 {

		// ObjIdentifier - 4 bits + ObjLength - 4 bits
		var idAndLen uint8
		idAndLen |= uint8(id) << 4
		idAndLen |= uint8(len)
		output = append(output, byte(idAndLen))

		// Rest is Data
		output = append(output, data[:len]...)

	} else {
	}
	return
}

func parseFrameInfoObjects(buf []byte, framingElen int) (objs []FrameInfo, err error, halfByteRemaining bool) {
	var curObjIdx int
	var byteIncrementCnt int
	var bitsToBeIncremented int
	var marker int

	// Parse frameInfo objects
	for curObjIdx = 0; curObjIdx < framingElen; curObjIdx += byteIncrementCnt {
		byteIncrementCnt = 0
		var oneFrameObj FrameInfo

		// First get the objId
		// -------------------------
		var objId int
		var objHeader uint8 = buf[curObjIdx]
		var objIdentifierRaw uint8
		if bitsToBeIncremented == 0 {
			// ObjHeader
			// 0 1 2 3 4 5 6 7
			// ^-----^
			// ObjIdentifierRaw
			objIdentifierRaw = (objHeader & 0xf0) >> 4
		} else {
			// ObjHeader
			// 0 1 2 3 4 5 6 7
			//         ^-----^
			//           ObjIdentifierRaw
			objIdentifierRaw = (objHeader & 0x0f)
		}
		bitsToBeIncremented += 4

		marker, err = incrementMarker(&bitsToBeIncremented, &byteIncrementCnt, framingElen, curObjIdx)
		if err != nil {
			return
		}

		// Value is 0-14
		objId = int(objIdentifierRaw & 0xe)
		// If bit 15 is set, ID is 15 + value of next byte
		if objIdentifierRaw&0x1 > 0 {
			if bitsToBeIncremented > 0 {
				// ObjHeader
				// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
				// ^-----^ ^---------------^
				// ObjId1    Extension
				// ^ marker
				buffer := uint16(buf[marker])
				buffer = buffer << 8
				buffer |= uint16(buf[marker+1])
				var extension uint8 = uint8(buffer & 0xff0 >> 4)
				objId += int(extension)
			} else {
				// ObjHeader
				// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
				//         ^-----^ ^-------------------^
				//          ObjId1    extension
				//                 ^ marker
				var extension uint8 = uint8(buf[marker])
				objId += int(extension)
			}
			bitsToBeIncremented += 8
		}

		marker, err = incrementMarker(&bitsToBeIncremented, &byteIncrementCnt, framingElen, curObjIdx)
		if err != nil {
			return
		}
		oneFrameObj.ObjId = FrameObjType(objId)

		// Then get the obj length
		// -------------------------
		var objLenRaw uint8
		var objLen int
		if bitsToBeIncremented > 0 {
			// ObjHeader
			// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
			//                 ^         ^---------^
			//                 marker       objLen
			objLenRaw = uint8(buf[marker]) & 0x0f
		} else {
			// ObjHeader
			// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
			//                                        ^--------^
			//                                          objLen
			//                                        ^ marker
			objLenRaw = uint8(buf[marker]) & 0xf0 >> 4
		}
		bitsToBeIncremented += 4

		marker, err = incrementMarker(&bitsToBeIncremented, &byteIncrementCnt, framingElen, curObjIdx)
		if err != nil {
			return
		}

		// Length is 0-14
		objLen = int(objLenRaw & 0xe)
		// If bit 15 is set, lenghth is 15 + value of next byte
		if objLenRaw&0x1 > 0 {
			if bitsToBeIncremented == 0 {
				// ObjHeader
				// 12 13 14 15 16 17 18 19 20 21 22 23
				// ^---------^ ^--------------------^
				//   objLen        extension
				//             ^ marker
				var extension uint8 = uint8(buf[marker])
				objLen += int(extension)
			} else {
				// ObjHeader
				// 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
				// ^--------^  ^---------------------^
				//  objLen          extension
				// ^ marker				var buffer uint16
				buffer := uint16(buf[marker])
				buffer = buffer << 8
				buffer |= uint16(buf[marker+1])
				var extension uint8 = uint8(buffer & 0xff0 >> 4)
				objLen += int(extension)
			}
			bitsToBeIncremented += 8
		}

		marker, err = incrementMarker(&bitsToBeIncremented, &byteIncrementCnt, framingElen, curObjIdx)
		if err != nil {
			return
		}
		oneFrameObj.ObjLen = objLen

		// The rest is N-bytes of data based on the length
		if bitsToBeIncremented == 0 {
			// No weird alignment needed
			oneFrameObj.ObjData = buf[marker : marker+objLen]
		} else {
			// 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
			// ^--------^  ^---------------------^ ^--------->
			//  objLen          extension            data
			//                          ^ marker
			oneFrameObj.ObjData = ShiftByteSliceLeft4Bits(buf[marker : marker+objLen+1])
		}
		err = oneFrameObj.Validate()
		if err != nil {
			return
		}
		objs = append(objs, oneFrameObj)

		bitsToBeIncremented += 8 * objLen
		marker, err = incrementMarker(&bitsToBeIncremented, &byteIncrementCnt, framingElen, curObjIdx)
	}

	if bitsToBeIncremented > 0 {
		halfByteRemaining = true
	}
	return
}

func ShiftByteSliceLeft4Bits(slice []byte) (replacement []byte) {
	var buffer uint16
	var i int
	sliceLen := len(slice)

	if sliceLen < 2 {
		// Let's not shift less than 16 bits
		return
	}

	replacement = make([]byte, sliceLen, cap(slice))

	for i = 0; i < sliceLen-1; i++ {
		// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
		// ^-----^ ^---------------^ ^-----------
		// garbage   data byte 0       data byte 1
		buffer = uint16(slice[i])
		buffer = buffer << 8
		buffer |= uint16(slice[i+1])
		replacement[i] = uint8(buffer & 0xff0 >> 4)
	}

	if i < sliceLen {
		lastByte := slice[sliceLen-1]
		lastByte = lastByte << 4
		replacement[i] = lastByte
	}
	return
}

// The following is used to theoretically support frameInfo ObjID extensions
// for completeness, but they are not very efficient though
func ShiftByteSliceRight4Bits(slice []byte) (replacement []byte) {
	var buffer uint16
	var i int
	var leftovers uint8 // 4 bits only
	var replacementUnit uint16
	var first bool = true
	var firstLeftovers uint8
	var lastLeftovers uint8
	sliceLen := len(slice)

	if sliceLen < 2 {
		// Let's not shift less than 16 bits
		return
	}

	if slice[sliceLen-1]&0xf == 0 {
		replacement = make([]byte, sliceLen, cap(slice))
	} else {
		replacement = make([]byte, sliceLen+1, cap(slice)+1)
	}

	for i = 0; i < sliceLen-1; i++ {
		buffer = binary.BigEndian.Uint16(slice[i : i+2])
		// (buffer)
		// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
		// ^-------------^ ^-------------------^
		//     data byte 0        data byte 1
		//
		// into
		//
		// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
		// ^-----^ ^---------------^ ^--------------------^ ^----------^
		// zeroes   data byte 0      data byte 1              zeroes

		if first {
			// The leftover OR'ing will overwrite the first 4 bits of data byte 0. Save them
			firstLeftovers = uint8(buffer & 0xf000 >> 12)
			first = false
		}
		replacementUnit = 0
		replacementUnit |= uint16(leftovers) << 12
		replacementUnit |= (buffer & 0xff00) >> 4 // data byte 0
		replacementUnit |= buffer & 0xff >> 4     // data byte 1 first 4 bits
		lastLeftovers = uint8(buffer&0xf) << 4

		replacement[i+1] = byte(replacementUnit)

		leftovers = uint8((buffer & 0x000f) << 4)
	}

	replacement[0] = byte(uint8(replacement[0]) | firstLeftovers)
	if lastLeftovers > 0 {
		replacement[sliceLen] = byte(lastLeftovers)
	}
	return
}

func Merge2HalfByteSlices(src1, src2 []byte) (output []byte) {
	src1Len := len(src1)
	src2Len := len(src2)
	output = make([]byte, src1Len+src2Len-1)

	var mergeByte uint8 = src1[src1Len-1]
	mergeByte |= uint8(src2[0])

	copy(output, src1)
	copy(output[src1Len:], src2[1:])

	output[src1Len-1] = byte(mergeByte)

	return
}