Example #1
0
// MarshalJSON returns a JSON representation of a data dictionary.
// If any of the dictionary's embedded values is a cborrpc.PythonTuple
// or a uuid.UUID, returns a base64-encoded CBOR string; otherwise
// returns a normal JSON object.
func (d DataDict) MarshalJSON() (out []byte, err error) {
	var v interface{}
	if needsCBOREncoding(reflect.ValueOf(d)) {
		// Do CBOR encoding to a byte array
		var intermediate []byte
		cborHandle := &codec.CborHandle{}
		err = cborrpc.SetExts(cborHandle)
		if err != nil {
			return nil, err
		}
		encoder := codec.NewEncoderBytes(&intermediate, cborHandle)
		err = encoder.Encode(map[string]interface{}(d))
		if err != nil {
			return nil, err
		}

		// base64 encode that byte array
		s := base64.StdEncoding.EncodeToString(intermediate)

		// Then we will JSON encode that string
		v = s
	} else {
		// We will JSON encode the base object
		v = map[string]interface{}(d)
	}
	codecHandle := &codec.JsonHandle{}
	encoder := codec.NewEncoderBytes(&out, codecHandle)
	err = encoder.Encode(v)
	return
}
Example #2
0
/*
 * Not able to install custom decoder for indefite length objects with the codec.
 * So we need to decode the whole response, and then re-encode the newtmgr response
 * part.
 */
func DeserializeOmgrReq(data []byte) (*NmgrReq, error) {
	req := coap.Message{}
	err := req.UnmarshalBinary(data)
	if err != nil {
		return nil, util.NewNewtError(fmt.Sprintf(
			"Oicmgr request invalid %s", err.Error()))
	}
	if req.Code == coap.GET || req.Code == coap.PUT {
		return nil, nil
	}
	if req.Code != coap.Created && req.Code != coap.Deleted &&
		req.Code != coap.Valid && req.Code != coap.Changed &&
		req.Code != coap.Content {
		return nil, util.NewNewtError(fmt.Sprintf(
			"OIC error rsp: %s", req.Code.String()))
	}

	var rsp OicRsp
	err = codec.NewDecoderBytes(req.Payload, new(codec.CborHandle)).Decode(&rsp)
	if err != nil {
		return nil, util.NewNewtError(fmt.Sprintf("Invalid incoming cbor: %s",
			err.Error()))
	}
	log.Debugf("Deserialized response %+v", rsp)

	nmr := &NmgrReq{}

	var ndata []byte = make([]byte, 0)

	if rsp.Read != nil {
		err = codec.NewEncoderBytes(&ndata,
			new(codec.CborHandle)).Encode(rsp.Read)
		nmr.Op = NMGR_OP_READ_RSP
	} else {
		err = codec.NewEncoderBytes(&ndata,
			new(codec.CborHandle)).Encode(rsp.Write)
		nmr.Op = NMGR_OP_WRITE_RSP
	}
	if err != nil {
		return nil, util.NewNewtError(fmt.Sprintf("Internal error: %s",
			err.Error()))
	}

	nmr.Len = uint16(len(ndata))
	nmr.Flags = 0
	nmr.Group = 0
	nmr.Seq = 0
	nmr.Id = 0

	nmr.Data = ndata

	log.Debugf("Deserialized response %+v", nmr)

	return nmr, nil
}
Example #3
0
func (c *Config) EncodeRequest() (*NmgrReq, error) {
	nmr, err := NewNmgrReq()
	if err != nil {
		return nil, err
	}

	nmr.Op = NMGR_OP_WRITE
	nmr.Flags = 0
	nmr.Group = NMGR_GROUP_ID_CONFIG
	nmr.Id = 0
	nmr.Len = 0

	data := make([]byte, 0)
	enc := codec.NewEncoderBytes(&data, new(codec.CborHandle))

	if c.Value == "" {
		type ConfigReadReq struct {
			Name string `codec:"name"`
		}

		readReq := &ConfigReadReq{
			Name: c.Name,
		}

		enc.Encode(readReq)

		nmr.Data = data
		nmr.Op = NMGR_OP_READ
	} else {
		enc.Encode(c)
		nmr.Data = data
	}
	nmr.Len = uint16(len(nmr.Data))
	return nmr, nil
}
Example #4
0
func (s *Suite) encoderTest(c *check.C, obj interface{}, expecteds ...[]byte) {
	var actual []byte
	encoder := codec.NewEncoderBytes(&actual, s.cbor)
	err := encoder.Encode(obj)
	c.Assert(err, check.IsNil)
	c.Check(actual, DeepEqualAny, expecteds)
}
Example #5
0
// IfcToJSON encodes interface{} to a JSON byte slice
func IfcToJSON(ifcVal interface{}) (jsonOut []byte, err error) {
	jsonOut = make([]byte, 0, unsafe.Sizeof(ifcVal))
	handle := new(codec.JsonHandle)
	encoder := codec.NewEncoderBytes(&(jsonOut), handle)
	err = encoder.Encode(ifcVal)
	return
}
Example #6
0
func (e *Echo) EncodeEchoCtrl() (*NmgrReq, error) {
	type SerialEchoCtl struct {
		Echo int `codec:"echo"`
	}

	integer, err := strconv.Atoi(e.Message)
	if err != nil {
		return nil, util.NewNewtError(fmt.Sprintf("Invalid echo ctrl setting %s",
			err.Error()))
	}
	echoCtl := &SerialEchoCtl{
		Echo: integer,
	}

	nmr, err := NewNmgrReq()
	if err != nil {
		return nil, err
	}

	nmr.Op = NMGR_OP_WRITE
	nmr.Flags = 0
	nmr.Group = NMGR_GROUP_ID_DEFAULT
	nmr.Id = NMGR_ID_CONS_ECHO_CTRL

	data := make([]byte, 0)
	enc := codec.NewEncoderBytes(&data, new(codec.CborHandle))
	if err := enc.Encode(echoCtl); err != nil {
		return nil, util.NewNewtError(fmt.Sprintf("Failed to encode message %s",
			err.Error()))
	}
	nmr.Len = uint16(len(data))
	nmr.Data = data

	return nmr, nil
}
func (f *FileDownload) EncodeWriteRequest() (*NmgrReq, error) {
	type DownloadReq struct {
		Off  uint32 `codec:"off"`
		Name string `codec:"name"`
	}
	nmr, err := NewNmgrReq()
	if err != nil {
		return nil, err
	}

	nmr.Op = NMGR_OP_READ
	nmr.Flags = 0
	nmr.Group = NMGR_GROUP_ID_IMAGE
	nmr.Id = IMGMGR_NMGR_OP_FILE

	downloadReq := &DownloadReq{
		Off:  f.Offset,
		Name: f.Name,
	}

	data := make([]byte, 0)
	enc := codec.NewEncoderBytes(&data, new(codec.CborHandle))
	enc.Encode(downloadReq)
	nmr.Len = uint16(len(data))
	nmr.Data = data

	return nmr, nil
}
func (i *ImageStateWriteReq) Encode() (*NmgrReq, error) {
	nmr, err := NewNmgrReq()
	if err != nil {
		return nil, err
	}

	clone, err := NewImageStateWriteReq()
	if err != nil {
		return nil, err
	}

	clone.Confirm = i.Confirm

	if len(i.Hash) != 0 {
		clone.Hash = i.Hash
		if err != nil {
			return nil, err
		}
	}

	nmr.Op = NMGR_OP_WRITE
	nmr.Flags = 0
	nmr.Group = NMGR_GROUP_ID_IMAGE
	nmr.Id = IMGMGR_NMGR_OP_STATE

	data := make([]byte, 0)
	enc := codec.NewEncoderBytes(&data, new(codec.CborHandle))
	enc.Encode(clone)
	nmr.Data = data
	nmr.Len = uint16(len(data))

	return nmr, nil
}
Example #9
0
func (client *Client) writeMessage(typ MsgType, session [16]byte, seq uint64, meta MetaMap, args []interface{}) (err error) {
	// turn seq into 16 bytes
	var seqBytes [16]byte
	binary.BigEndian.PutUint64(seqBytes[8:], seq)

	// message body as msgpack list
	msg := []interface{}{&session, &seqBytes, meta}
	msg = append(msg, args...)
	var msgBytes []byte
	enc := codec.NewEncoderBytes(&msgBytes, &mh)
	if err = enc.Encode(msg); err != nil {
		return
	}

	// fill in the rest of the header
	h := &MsgHeader{0, typ, uint32(len(msgBytes))}
	if err = binary.Write(client.wbuf, binary.BigEndian, h); err != nil {
		return
	}

	if _, err = client.wbuf.Write(msgBytes); err != nil {
		return
	}

	return client.wbuf.Flush()
}
Example #10
0
func scriptBody(sessionId []byte, graphName, script string, bindings map[string]interface{}) (out []byte, err error) {
	var (
		mh            = new(codec.MsgpackHandle)
		enc           = codec.NewEncoderBytes(&out, mh)
		reqId         = uuid.NewV4()
		isSessionless = bytes.Equal(sessionId, sessionlessUuid[:])
	)
	mh.MapType = reflect.TypeOf(map[string]interface{}(nil))
	meta := map[string]interface{}{
		"inSession":    !isSessionless,
		"isolate":      isSessionless,
		"graphObjName": "g",
	}
	if isSessionless {
		meta["graphName"] = graphName
	}
	err = enc.Encode([]interface{}{
		sessionId,
		reqId[:],
		meta,
		"groovy",
		script,
		bindings,
	})
	return
}
Example #11
0
func (p *KeybasePacket) unmarshalBinary(data []byte) error {
	ch := codecHandle()
	if err := MsgpackDecodeAll(data, ch, p); err != nil {
		return err
	}

	if err := p.unpackBody(ch); err != nil {
		return err
	}

	// Test for nonstandard msgpack data (which could be maliciously crafted)
	// by re-encoding and making sure we get the same thing.
	// https://github.com/keybase/client/issues/423
	//
	// Ideally this should be done at a lower level, like MsgpackDecodeAll, but
	// our msgpack library doesn't sort maps the way we expect. See
	// https://github.com/ugorji/go/issues/103
	var reencoded []byte
	if err := codec.NewEncoderBytes(&reencoded, ch).Encode(p); err != nil {
		return err
	}

	if reencoded, err := p.Encode(); err != nil {
		return err
	} else if !bytes.Equal(reencoded, data) {
		return FishyMsgpackError{data, reencoded}
	}

	return p.checkHash()
}
Example #12
0
// Creates a new token of an user "id" which expires on "expires"
func New(id string, expires time.Time) (string, error) {
	// Expiration time in unix
	unix := expires.Unix()

	// Create a new instance of a Token
	token := Token{
		id,
		unix,
		signToken(id, unix),
	}

	// Initialize an empty []byte that will receive msgpack data
	var message []byte
	// Create a new msgpack encoder
	encoder := codec.NewEncoderBytes(&message, msgpack)

	// Encode it
	err := encoder.Encode(token)
	if err != nil {
		return "", err
	}

	// Base64 it in a form that is friendly to HTTP headers and return it
	return base64.URLEncoding.EncodeToString(message), nil
}
Example #13
0
func (p *KeybasePacket) unpackBody(ch *codec.MsgpackHandle) error {
	var body interface{}

	switch p.Tag {
	case TagP3skb:
		// XXX this function should get a G passed into it, but to do that requires
		// a lot of changes upstream.
		body = NewSKB(G)
	case TagSignature:
		body = &NaclSigInfo{}
	case TagEncryption:
		body = &NaclEncryptionInfo{}
	default:
		return fmt.Errorf("Unknown packet tag: %d", p.Tag)
	}
	var encoded []byte
	if err := codec.NewEncoderBytes(&encoded, ch).Encode(p.Body); err != nil {
		return err
	}
	if err := MsgpackDecodeAll(encoded, ch, body); err != nil {
		return err
	}
	p.Body = body

	return nil
}
func TestHTTPDecoder(t *testing.T) {

	var out []byte
	if err := codec.NewEncoderBytes(&out, h).Encode(req); err != nil {
		t.Fatalf("unable to pack test data: %v", err)
	}

	r, err := UnpackProxyRequest(out)
	if err != nil {
		t.Fatalf("unable to unpack request %v", err)
	}
	defer r.Body.Close()

	if b, _ := ioutil.ReadAll(r.Body); !bytes.Equal(b, body) {
		t.Fatalf("bad bytes: %s %s", b, body)
	}

	if r.Method != method {
		t.Fatalf("bad method: %s %s", r.Method, method)
	}

	if r.Header.Get("X-Cocaine-Service") != "Test" {
		t.Fatalf("bad header", r.Header.Get("X-Cocaine-Service"))
	}
}
Example #15
0
func packRequest(req *http.Request) ([]byte, error) {
	body, err := ioutil.ReadAll(req.Body)
	if err != nil {
		return nil, err

	}
	// method uri 1.1 headers body
	headers := make([][2]string, 0, len(req.Header))
	for header, values := range req.Header {
		for _, val := range values {
			headers = append(headers, [2]string{header, val})
		}
	}

	var task []byte
	codec.NewEncoderBytes(&task, hAsocket).Encode([]interface{}{
		req.Method,
		req.URL.RequestURI(),
		fmt.Sprintf("%d.%d", req.ProtoMajor, req.ProtoMinor),
		headers,
		body,
	})

	return task, nil
}
Example #16
0
func WriteMessage(conn net.Conn, timeout time.Duration, msg *Message) error {
	var handle = &codec.MsgpackHandle{}
	var buf []byte
	var encoder = codec.NewEncoderBytes(&buf, handle)
	err := encoder.Encode(msg)
	if err != nil {
		return err
	}

	length := len(buf)
	if length > DefaultMessageLengthCap {
		return errors.New("message is too long")
	}

	var lengthBuf [2]byte
	binary.BigEndian.PutUint16(lengthBuf[:], uint16(length))

	conn.SetWriteDeadline(time.Now().Add(timeout))
	_, err = conn.Write(lengthBuf[:])
	if err != nil {
		return err
	}

	_, err = io.Copy(conn, bytes.NewBuffer(buf))
	return err
}
Example #17
0
func main() {
	jh := new(codec.JsonHandle)
	u := &user.User{
		Name:  "Inigo Montoya",
		Email: "*****@*****.**",
	}

	var out []byte
	err := codec.NewEncoderBytes(&out, jh).Encode(&u)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	fmt.Println(string(out))

	var u2 user.User
	err = codec.NewDecoderBytes(out, jh).Decode(&u2)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	fmt.Println(u2)
}
Example #18
0
func encoderTest(t *testing.T, obj interface{}, expecteds ...[]byte) {
	var actual []byte
	encoder := codec.NewEncoderBytes(&actual, cbor)
	err := encoder.Encode(obj)
	if assert.NoError(t, err) {
		assert.Contains(t, expecteds, actual)
	}
}
func BenchmarkHTTPDecoder(b *testing.B) {
	var out []byte
	codec.NewEncoderBytes(&out, h).Encode(req)

	for n := 0; n < b.N; n++ {
		UnpackProxyRequest(out)
	}
}
Example #20
0
File: user.go Project: zvin/vandal
func encodeEvent(event *[]interface{}) (*[]byte, error) {
	var result []byte
	var err = codec.NewEncoderBytes(&result, &msgpackHandle).Encode(event)
	if err != nil {
		Log.Printf("Failed to encode event %v\n", event)
	}
	return &result, err
}
Example #21
0
func MsgpackMarshal(v interface{}) (data []byte, payloadType byte, err error) {
	mh := codec.MsgpackHandle{}
	enc := codec.NewEncoderBytes(&data, &mh)
	err = enc.Encode(v)
	//fmt.Println("Marshal: " + B64Enc(string(data)))
	payloadType = websocket.BinaryFrame
	return
}
Example #22
0
func packMsg(msg messageInterface) rawMessage {
	var buf []byte
	err := codec.NewEncoderBytes(&buf, h).Encode([]interface{}{msg.getTypeID(), msg.getSessionID(), msg.getPayload()})
	if err != nil {
		fmt.Println(err)
	}
	return buf
}
func (f *FileUpload) EncodeWriteRequest() (*NmgrReq, error) {
	type UploadReq struct {
		Off  uint32 `codec:"off"`
		Data []byte `codec:"data"`
	}
	type UploadFirstReq struct {
		Off  uint32 `codec:"off"`
		Size uint32 `codec:"len"`
		Name string `codec:"name"`
		Data []byte `codec:"data"`
	}
	nmr, err := NewNmgrReq()
	if err != nil {
		return nil, err
	}

	nmr.Op = NMGR_OP_WRITE
	nmr.Flags = 0
	nmr.Group = NMGR_GROUP_ID_IMAGE
	nmr.Id = IMGMGR_NMGR_OP_FILE

	data := []byte{}

	if f.Offset == 0 {
		uploadReq := &UploadFirstReq{
			Off:  f.Offset,
			Size: f.Size,
			Name: f.Name,
			Data: f.Data,
		}
		enc := codec.NewEncoderBytes(&data, new(codec.CborHandle))
		enc.Encode(uploadReq)
	} else {
		uploadReq := &UploadReq{
			Off:  f.Offset,
			Data: f.Data,
		}
		enc := codec.NewEncoderBytes(&data, new(codec.CborHandle))
		enc.Encode(uploadReq)
	}
	nmr.Len = uint16(len(data))
	nmr.Data = data

	return nmr, nil
}
Example #24
0
// MarshalBinary encodes the RSDic into a binary form and returns the result.
func (rsd RSDic) MarshalBinary() (out []byte, err error) {
	var bh codec.MsgpackHandle
	enc := codec.NewEncoderBytes(&out, &bh)
	err = enc.Encode(rsd.bits)
	if err != nil {
		return
	}
	err = enc.Encode(rsd.pointerBlocks)
	if err != nil {
		return
	}
	err = enc.Encode(rsd.rankBlocks)
	if err != nil {
		return
	}
	err = enc.Encode(rsd.selectOneInds)
	if err != nil {
		return
	}
	err = enc.Encode(rsd.selectZeroInds)
	if err != nil {
		return
	}
	err = enc.Encode(rsd.rankSmallBlocks)
	if err != nil {
		return
	}
	err = enc.Encode(rsd.num)
	if err != nil {
		return
	}
	err = enc.Encode(rsd.oneNum)
	if err != nil {
		return
	}
	err = enc.Encode(rsd.zeroNum)
	if err != nil {
		return
	}
	err = enc.Encode(rsd.lastBlock)
	if err != nil {
		return
	}
	err = enc.Encode(rsd.lastOneNum)
	if err != nil {
		return
	}
	err = enc.Encode(rsd.lastZeroNum)
	if err != nil {
		return
	}
	err = enc.Encode(rsd.codeLen)
	if err != nil {
		return
	}
	return
}
Example #25
0
func mapToBytes(in map[string]interface{}) (out []byte, err error) {
	cbor := new(codec.CborHandle)
	err = cborrpc.SetExts(cbor)
	if err != nil {
		return
	}
	encoder := codec.NewEncoderBytes(&out, cbor)
	err = encoder.Encode(in)
	return
}
Example #26
0
func (q QueryResponse) Encode() ([]byte, error) {
	b := []byte{}
	msgpack.MapType = reflect.TypeOf(map[string]interface{}(nil))
	encoder := codec.NewEncoderBytes(&b, &msgpack)
	err := encoder.Encode(q)
	if err != nil {
		return b, err
	}
	return b, nil
}
Example #27
0
File: message.go Project: 40a/vega
func (m *Message) AsBytes() (ret []byte) {
	enc := codec.NewEncoderBytes(&ret, &msgpack)

	err := enc.Encode(m)
	if err != nil {
		panic(err)
	}

	return
}
Example #28
0
func (aW *wrappedApp) StreamCall(method string, args interface{}) (<-chan cocaine.ServiceResult, error) {
	var buf []byte
	err := codec.NewEncoderBytes(&buf, h).Encode(args)
	if err != nil {
		return nil, err
	}

	ch := aW.app.Call("enqueue", method, buf)
	return ch, nil
}
Example #29
0
func convertPayload(in interface{}, out interface{}) error {
	var buf []byte
	if err := codec.NewEncoderBytes(&buf, payloadHandler).Encode(in); err != nil {
		return err
	}
	if err := codec.NewDecoderBytes(buf, payloadHandler).Decode(out); err != nil {
		return err
	}
	return nil
}
Example #30
0
func (x pythonTupleExt) WriteExt(v interface{}) (resp []byte) {
	tuple := v.(PythonTuple)
	encoder := codec.NewEncoderBytes(&resp, x.cbor)
	items := tuple.Items
	if items == nil {
		items = []interface{}{}
	}
	encoder.MustEncode(items)
	return
}