Commit 46049a9c authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent bf033709
// Copyright (C) 2020 Nexedi SA and Contributors.
// Kirill Smelkov <kirr@nexedi.com>
//
// This program is free software: you can Use, Study, Modify and Redistribute
// it under the terms of the GNU General Public License version 3, or (at your
// option) any later version, as published by the Free Software Foundation.
//
// You can also Link and Combine this program with other software covered by
// the terms of any of the Free Software licenses or any of the Open Source
// Initiative approved licenses and Convey the resulting work. Corresponding
// source of such a combination shall include the source code for all other
// software used.
//
// This program is distributed WITHOUT ANY WARRANTY; without even the implied
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//
// See COPYING file for full licensing terms.
// See https://www.nexedi.com/licensing for rationale and options.
// XXX -> use msgp.Raw.DecodeMsg
// +build ignore
// Package msgframe helps extract MsgPack frames from stream of bytes.
//
// The sole reason this package exists is because unlike ZEO/py NEO/py switched
// to MsgPack without wrapping payload into frames with size header.
package msgframe
// Framer helps extract full MsgPack frames from stream of raw bytes.
type Framer struct {
todoObj int // number of pending msgpack objects to complete
todoRaw int // number of pending bytes to complete current object
}
// Feed feeds next data to framer.
//
// returns:
// - n: how many bytes from chunk was analyzed // XXX kill?
// - ok: whether chunk[:n] completes a frame.
//
// XXX chunk must be non-empty?
func (f *Framer) Feed(chunk []byte) (ok bool) { // XXX +n ?
if f.todoRaw > 0 {
l := len(chunk)
if l > f.todoRaw {
l = f.todoRaw
}
f.todoRaw -= l
chunk = chunk[l:]
if f.todoRaw == 0 && f.todoObj == 0 {
return true // XXX remaining part of chunk?
}
}
if len(chunk) == 0 {
return false
}
op := chunk[0]
f.todoObj--
chunk = chunk[1:]
switch {
// positive fixint
// 0xxxxxxx 0x00 - 0x7f
case op & 0b1_0000000 == 0b0_0000000:
// fixmap
// 1000xxxx 0x80 - 0x8f 2N·objects
case op & 0b1111_0000 == 0b1000_0000
f.todoObj += 2*(op & 0x0f)
// fixarray
// 1001xxxx 0x90 - 0x9f N·objects
case op & 0b1111_0000 == 0b1001_0000:
f.todoObj += op & 0x0f
// fixstr
// 101xxxxx 0xa0 - 0xbf data
case op & 0b111_00000 == 0b101_00000:
f.todoRaw += op & 0b000_11111
// nil 11000000 0xc0
case op == 0xc0:
// (never used) 11000001 0xc1
// false 11000010 0xc2
case op == 0xc2:
// true 11000011 0xc3
case op == 0xc3:
// bin8 11000100 0xc4 XXXXXXXX data
case op == 0xc4:
l := chunk[1]; chunk = chunk[1:] // XXX overflow
f.todoRaw += l
// bin16 11000101 0xc5 YYYYYYYY YYYYYYYY data
case op == 0xc5:
l := be.Uint16(chunk); chunk = chunk[2:] // XXX overflow
f.todoRaw += l
// bin32 11000110 0xc6 ZZZZZZZZ ZZZZZZZZ ZZZZZZZZ ZZZZZZZZ data
case op == 0xc6:
l := be.Uint32(chunk); chunk = chunk[4:] // XXX overflow
f.todoRaw += l
// ext8 11000111 0xc7 XXXXXXXX type data
case op == 0xc7:
l := chunk[0]; chunk = chunk[1+1/*type*/:] // XXX overflow
f.todoRaw += l
// ext16 11001000 0xc8 YYYYYYYY YYYYYYYY type data
case op == 0xc8:
l := be.Uint16(chunk); chunk = chunk[2+1/*type*/:] // XXX overflow
f.todoRaw += l
// ext32 11001001 0xc9 ZZZZZZZZ ZZZZZZZZ ZZZZZZZZ ZZZZZZZZ type data
case op == 0xc9:
l := be.Uint32(chunk); chunk = chunk[4+1/*type*/:] // XXX overflow
f.todoRaw += l
// float32 11001010 0xca XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX
case op == 0xca:
f.todoRaw += 4
// float64 11001011 0xcb Y Y Y Y Y Y Y Y
case op == 0xcb:
f.todoRaw += 8
case op == 0xcc: f.todoRaw += 1 // uint8 11001100 0xcc
case op == 0xcd: f.todoRaw += 2 // uint16 11001101 0xcd
case op == 0xce: f.todoRaw += 4 // uint32 11001110 0xce
case op == 0xcf: f.todoRaw += 8 // uint64 11001111 0xcf
case op == 0xd0: f.todoRaw += 1 // int8 11010000 0xd0
case op == 0xd1: f.todoRaw += 2 // int16 11010001 0xd1
case op == 0xd2: f.todoRaw += 4 // int32 11010010 0xd2
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment