podman
1// Go support for Protocol Buffers - Google's data interchange format
2//
3// Copyright 2010 The Go Authors. All rights reserved.
4// https://github.com/golang/protobuf
5//
6// Redistribution and use in source and binary forms, with or without
7// modification, are permitted provided that the following conditions are
8// met:
9//
10// * Redistributions of source code must retain the above copyright
11// notice, this list of conditions and the following disclaimer.
12// * Redistributions in binary form must reproduce the above
13// copyright notice, this list of conditions and the following disclaimer
14// in the documentation and/or other materials provided with the
15// distribution.
16// * Neither the name of Google Inc. nor the names of its
17// contributors may be used to endorse or promote products derived from
18// this software without specific prior written permission.
19//
20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
32package proto33
34/*
35* Support for message sets.
36*/
37
38import (39"errors"40)
41
42// errNoMessageTypeID occurs when a protocol buffer does not have a message type ID.
43// A message type ID is required for storing a protocol buffer in a message set.
44var errNoMessageTypeID = errors.New("proto does not have a message type ID")45
46// The first two types (_MessageSet_Item and messageSet)
47// model what the protocol compiler produces for the following protocol message:
48// message MessageSet {
49// repeated group Item = 1 {
50// required int32 type_id = 2;
51// required string message = 3;
52// };
53// }
54// That is the MessageSet wire format. We can't use a proto to generate these
55// because that would introduce a circular dependency between it and this package.
56
57type _MessageSet_Item struct {58TypeId *int32 `protobuf:"varint,2,req,name=type_id"`59Message []byte `protobuf:"bytes,3,req,name=message"`60}
61
62type messageSet struct {63Item []*_MessageSet_Item `protobuf:"group,1,rep"`64XXX_unrecognized []byte65// TODO: caching?66}
67
68// Make sure messageSet is a Message.
69var _ Message = (*messageSet)(nil)70
71// messageTypeIder is an interface satisfied by a protocol buffer type
72// that may be stored in a MessageSet.
73type messageTypeIder interface {74MessageTypeId() int3275}
76
77func (ms *messageSet) find(pb Message) *_MessageSet_Item {78mti, ok := pb.(messageTypeIder)79if !ok {80return nil81}82id := mti.MessageTypeId()83for _, item := range ms.Item {84if *item.TypeId == id {85return item86}87}88return nil89}
90
91func (ms *messageSet) Has(pb Message) bool {92return ms.find(pb) != nil93}
94
95func (ms *messageSet) Unmarshal(pb Message) error {96if item := ms.find(pb); item != nil {97return Unmarshal(item.Message, pb)98}99if _, ok := pb.(messageTypeIder); !ok {100return errNoMessageTypeID101}102return nil // TODO: return error instead?103}
104
105func (ms *messageSet) Marshal(pb Message) error {106msg, err := Marshal(pb)107if err != nil {108return err109}110if item := ms.find(pb); item != nil {111// reuse existing item112item.Message = msg113return nil114}115
116mti, ok := pb.(messageTypeIder)117if !ok {118return errNoMessageTypeID119}120
121mtid := mti.MessageTypeId()122ms.Item = append(ms.Item, &_MessageSet_Item{123TypeId: &mtid,124Message: msg,125})126return nil127}
128
129func (ms *messageSet) Reset() { *ms = messageSet{} }130func (ms *messageSet) String() string { return CompactTextString(ms) }131func (*messageSet) ProtoMessage() {}132
133// Support for the message_set_wire_format message option.
134
135func skipVarint(buf []byte) []byte {136i := 0137for ; buf[i]&0x80 != 0; i++ {138}139return buf[i+1:]140}
141
142// unmarshalMessageSet decodes the extension map encoded in buf in the message set wire format.
143// It is called by Unmarshal methods on protocol buffer messages with the message_set_wire_format option.
144func unmarshalMessageSet(buf []byte, exts interface{}) error {145var m map[int32]Extension146switch exts := exts.(type) {147case *XXX_InternalExtensions:148m = exts.extensionsWrite()149case map[int32]Extension:150m = exts151default:152return errors.New("proto: not an extension map")153}154
155ms := new(messageSet)156if err := Unmarshal(buf, ms); err != nil {157return err158}159for _, item := range ms.Item {160id := *item.TypeId161msg := item.Message162
163// Restore wire type and field number varint, plus length varint.164// Be careful to preserve duplicate items.165b := EncodeVarint(uint64(id)<<3 | WireBytes)166if ext, ok := m[id]; ok {167// Existing data; rip off the tag and length varint168// so we join the new data correctly.169// We can assume that ext.enc is set because we are unmarshaling.170o := ext.enc[len(b):] // skip wire type and field number171_, n := DecodeVarint(o) // calculate length of length varint172o = o[n:] // skip length varint173msg = append(o, msg...) // join old data and new data174}175b = append(b, EncodeVarint(uint64(len(msg)))...)176b = append(b, msg...)177
178m[id] = Extension{enc: b}179}180return nil181}
182