| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1 | // Go support for Protocol Buffers - Google's data interchange format |
| 2 | // |
| David Symonds | ee6e9c5 | 2012-11-29 08:51:07 +1100 | [diff] [blame] | 3 | // Copyright 2010 The Go Authors. All rights reserved. |
| David Symonds | 558f13f | 2014-11-24 10:28:53 +1100 | [diff] [blame] | 4 | // https://github.com/golang/protobuf |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 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 | |
| 32 | package proto |
| 33 | |
| 34 | /* |
| 35 | * Routines for encoding data into the wire format for protocol buffers. |
| 36 | */ |
| 37 | |
| 38 | import ( |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 39 | "errors" |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 40 | "fmt" |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 41 | "reflect" |
| David Symonds | df583ae | 2012-12-06 14:06:53 +1100 | [diff] [blame] | 42 | "sort" |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 43 | ) |
| 44 | |
| David Symonds | e583a5f | 2013-09-27 10:02:37 +1000 | [diff] [blame] | 45 | // RequiredNotSetError is the error returned if Marshal is called with |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 46 | // a protocol buffer struct whose required fields have not |
| Rob Pike | c6d8e4a | 2010-07-28 15:34:32 -0700 | [diff] [blame] | 47 | // all been initialized. It is also the error returned if Unmarshal is |
| 48 | // called with an encoded protocol buffer that does not include all the |
| 49 | // required fields. |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 50 | // |
| David Symonds | e583a5f | 2013-09-27 10:02:37 +1000 | [diff] [blame] | 51 | // When printed, RequiredNotSetError reports the first unset required field in a |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 52 | // message. If the field cannot be precisely determined, it is reported as |
| 53 | // "{Unknown}". |
| David Symonds | e583a5f | 2013-09-27 10:02:37 +1000 | [diff] [blame] | 54 | type RequiredNotSetError struct { |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 55 | field string |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 56 | } |
| 57 | |
| David Symonds | e583a5f | 2013-09-27 10:02:37 +1000 | [diff] [blame] | 58 | func (e *RequiredNotSetError) Error() string { |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 59 | return fmt.Sprintf("proto: required field %q not set", e.field) |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 60 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 61 | |
| David Symonds | 7656e74 | 2011-07-22 14:54:17 +1000 | [diff] [blame] | 62 | var ( |
| David Symonds | f62db48 | 2015-02-23 12:37:00 +1100 | [diff] [blame] | 63 | // errRepeatedHasNil is the error returned if Marshal is called with |
| David Symonds | 7656e74 | 2011-07-22 14:54:17 +1000 | [diff] [blame] | 64 | // a struct with a repeated field containing a nil element. |
| David Symonds | f62db48 | 2015-02-23 12:37:00 +1100 | [diff] [blame] | 65 | errRepeatedHasNil = errors.New("proto: repeated field has nil element") |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 66 | |
| Bryan Mills | 8d92cf5 | 2016-03-19 05:57:00 +1100 | [diff] [blame] | 67 | // errOneofHasNil is the error returned if Marshal is called with |
| 68 | // a struct with a oneof field containing a nil element. |
| 69 | errOneofHasNil = errors.New("proto: oneof field has nil value") |
| 70 | |
| David Symonds | 7656e74 | 2011-07-22 14:54:17 +1000 | [diff] [blame] | 71 | // ErrNil is the error returned if Marshal is called with nil. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 72 | ErrNil = errors.New("proto: Marshal called with nil") |
| Ross Light | cd85f19 | 2016-05-19 15:50:37 -0400 | [diff] [blame] | 73 | |
| 74 | // ErrTooLarge is the error returned if Marshal is called with a |
| 75 | // message that encodes to >2GB. |
| 76 | ErrTooLarge = errors.New("proto: message encodes to over 2 GB") |
| David Symonds | 7656e74 | 2011-07-22 14:54:17 +1000 | [diff] [blame] | 77 | ) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 78 | |
| 79 | // The fundamental encoders that put bytes on the wire. |
| 80 | // Those that take integer types all accept uint64 and are |
| 81 | // therefore of type valueEncoder. |
| 82 | |
| David Symonds | 4fee3b1 | 2010-11-11 10:00:13 +1100 | [diff] [blame] | 83 | const maxVarintBytes = 10 // maximum length of a varint |
| 84 | |
| Ross Light | cd85f19 | 2016-05-19 15:50:37 -0400 | [diff] [blame] | 85 | // maxMarshalSize is the largest allowed size of an encoded protobuf, |
| 86 | // since C++ and Java use signed int32s for the size. |
| 87 | const maxMarshalSize = 1<<31 - 1 |
| 88 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 89 | // EncodeVarint returns the varint encoding of x. |
| 90 | // This is the format for the |
| 91 | // int32, int64, uint32, uint64, bool, and enum |
| 92 | // protocol buffer types. |
| 93 | // Not used by the package itself, but helpful to clients |
| 94 | // wishing to use the same encoding. |
| 95 | func EncodeVarint(x uint64) []byte { |
| David Symonds | 4fee3b1 | 2010-11-11 10:00:13 +1100 | [diff] [blame] | 96 | var buf [maxVarintBytes]byte |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 97 | var n int |
| 98 | for n = 0; x > 127; n++ { |
| 99 | buf[n] = 0x80 | uint8(x&0x7F) |
| 100 | x >>= 7 |
| 101 | } |
| 102 | buf[n] = uint8(x) |
| 103 | n++ |
| 104 | return buf[0:n] |
| 105 | } |
| 106 | |
| 107 | // EncodeVarint writes a varint-encoded integer to the Buffer. |
| 108 | // This is the format for the |
| 109 | // int32, int64, uint32, uint64, bool, and enum |
| 110 | // protocol buffer types. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 111 | func (p *Buffer) EncodeVarint(x uint64) error { |
| David Symonds | 4fee3b1 | 2010-11-11 10:00:13 +1100 | [diff] [blame] | 112 | for x >= 1<<7 { |
| David Symonds | d9da6ba | 2011-08-30 14:41:30 +1000 | [diff] [blame] | 113 | p.buf = append(p.buf, uint8(x&0x7f|0x80)) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 114 | x >>= 7 |
| 115 | } |
| David Symonds | d9da6ba | 2011-08-30 14:41:30 +1000 | [diff] [blame] | 116 | p.buf = append(p.buf, uint8(x)) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 117 | return nil |
| 118 | } |
| 119 | |
| Damien Neil | 0879490 | 2015-12-04 07:39:00 +1100 | [diff] [blame] | 120 | // SizeVarint returns the varint encoding size of an integer. |
| 121 | func SizeVarint(x uint64) int { |
| 122 | return sizeVarint(x) |
| 123 | } |
| 124 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 125 | func sizeVarint(x uint64) (n int) { |
| 126 | for { |
| 127 | n++ |
| 128 | x >>= 7 |
| 129 | if x == 0 { |
| 130 | break |
| 131 | } |
| 132 | } |
| 133 | return n |
| 134 | } |
| 135 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 136 | // EncodeFixed64 writes a 64-bit integer to the Buffer. |
| 137 | // This is the format for the |
| 138 | // fixed64, sfixed64, and double protocol buffer types. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 139 | func (p *Buffer) EncodeFixed64(x uint64) error { |
| David Symonds | d9da6ba | 2011-08-30 14:41:30 +1000 | [diff] [blame] | 140 | p.buf = append(p.buf, |
| 141 | uint8(x), |
| 142 | uint8(x>>8), |
| 143 | uint8(x>>16), |
| 144 | uint8(x>>24), |
| 145 | uint8(x>>32), |
| 146 | uint8(x>>40), |
| 147 | uint8(x>>48), |
| 148 | uint8(x>>56)) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 149 | return nil |
| 150 | } |
| 151 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 152 | func sizeFixed64(x uint64) int { |
| 153 | return 8 |
| 154 | } |
| 155 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 156 | // EncodeFixed32 writes a 32-bit integer to the Buffer. |
| 157 | // This is the format for the |
| 158 | // fixed32, sfixed32, and float protocol buffer types. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 159 | func (p *Buffer) EncodeFixed32(x uint64) error { |
| David Symonds | d9da6ba | 2011-08-30 14:41:30 +1000 | [diff] [blame] | 160 | p.buf = append(p.buf, |
| 161 | uint8(x), |
| 162 | uint8(x>>8), |
| 163 | uint8(x>>16), |
| 164 | uint8(x>>24)) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 165 | return nil |
| 166 | } |
| 167 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 168 | func sizeFixed32(x uint64) int { |
| 169 | return 4 |
| 170 | } |
| 171 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 172 | // EncodeZigzag64 writes a zigzag-encoded 64-bit integer |
| 173 | // to the Buffer. |
| 174 | // This is the format used for the sint64 protocol buffer type. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 175 | func (p *Buffer) EncodeZigzag64(x uint64) error { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 176 | // use signed number to get arithmetic right shift. |
| 177 | return p.EncodeVarint(uint64((x << 1) ^ uint64((int64(x) >> 63)))) |
| 178 | } |
| 179 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 180 | func sizeZigzag64(x uint64) int { |
| 181 | return sizeVarint(uint64((x << 1) ^ uint64((int64(x) >> 63)))) |
| 182 | } |
| 183 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 184 | // EncodeZigzag32 writes a zigzag-encoded 32-bit integer |
| 185 | // to the Buffer. |
| 186 | // This is the format used for the sint32 protocol buffer type. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 187 | func (p *Buffer) EncodeZigzag32(x uint64) error { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 188 | // use signed number to get arithmetic right shift. |
| 189 | return p.EncodeVarint(uint64((uint32(x) << 1) ^ uint32((int32(x) >> 31)))) |
| 190 | } |
| 191 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 192 | func sizeZigzag32(x uint64) int { |
| 193 | return sizeVarint(uint64((uint32(x) << 1) ^ uint32((int32(x) >> 31)))) |
| 194 | } |
| 195 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 196 | // EncodeRawBytes writes a count-delimited byte buffer to the Buffer. |
| 197 | // This is the format used for the bytes protocol buffer |
| 198 | // type and for embedded messages. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 199 | func (p *Buffer) EncodeRawBytes(b []byte) error { |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 200 | p.EncodeVarint(uint64(len(b))) |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 201 | p.buf = append(p.buf, b...) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 202 | return nil |
| 203 | } |
| 204 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 205 | func sizeRawBytes(b []byte) int { |
| 206 | return sizeVarint(uint64(len(b))) + |
| 207 | len(b) |
| 208 | } |
| 209 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 210 | // EncodeStringBytes writes an encoded string to the Buffer. |
| 211 | // This is the format used for the proto2 string type. |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 212 | func (p *Buffer) EncodeStringBytes(s string) error { |
| Rob Pike | 76f6ee5 | 2011-10-20 12:58:28 -0700 | [diff] [blame] | 213 | p.EncodeVarint(uint64(len(s))) |
| 214 | p.buf = append(p.buf, s...) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 215 | return nil |
| 216 | } |
| 217 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 218 | func sizeStringBytes(s string) int { |
| 219 | return sizeVarint(uint64(len(s))) + |
| 220 | len(s) |
| 221 | } |
| 222 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 223 | // Marshaler is the interface representing objects that can marshal themselves. |
| 224 | type Marshaler interface { |
| Rob Pike | a17fdd9 | 2011-11-02 12:43:05 -0700 | [diff] [blame] | 225 | Marshal() ([]byte, error) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 226 | } |
| 227 | |
| David Symonds | 9f60f43 | 2012-06-14 09:45:25 +1000 | [diff] [blame] | 228 | // Marshal takes the protocol buffer |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 229 | // and encodes it into the wire format, returning the data. |
| David Symonds | 9f60f43 | 2012-06-14 09:45:25 +1000 | [diff] [blame] | 230 | func Marshal(pb Message) ([]byte, error) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 231 | // Can the object marshal itself? |
| 232 | if m, ok := pb.(Marshaler); ok { |
| 233 | return m.Marshal() |
| 234 | } |
| 235 | p := NewBuffer(nil) |
| 236 | err := p.Marshal(pb) |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 237 | var state errorState |
| 238 | if err != nil && !state.shouldContinue(err, nil) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 239 | return nil, err |
| 240 | } |
| David Symonds | 8b25330 | 2014-01-14 16:24:19 +1100 | [diff] [blame] | 241 | if p.buf == nil && err == nil { |
| 242 | // Return a non-nil slice on success. |
| 243 | return []byte{}, nil |
| 244 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 245 | return p.buf, err |
| 246 | } |
| 247 | |
| David Symonds | 59b73b3 | 2015-08-24 13:22:02 +1000 | [diff] [blame] | 248 | // EncodeMessage writes the protocol buffer to the Buffer, |
| 249 | // prefixed by a varint-encoded length. |
| 250 | func (p *Buffer) EncodeMessage(pb Message) error { |
| 251 | t, base, err := getbase(pb) |
| 252 | if structPointer_IsNil(base) { |
| 253 | return ErrNil |
| 254 | } |
| 255 | if err == nil { |
| 256 | var state errorState |
| 257 | err = p.enc_len_struct(GetProperties(t.Elem()), base, &state) |
| 258 | } |
| 259 | return err |
| 260 | } |
| 261 | |
| David Symonds | 9f60f43 | 2012-06-14 09:45:25 +1000 | [diff] [blame] | 262 | // Marshal takes the protocol buffer |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 263 | // and encodes it into the wire format, writing the result to the |
| 264 | // Buffer. |
| David Symonds | 9f60f43 | 2012-06-14 09:45:25 +1000 | [diff] [blame] | 265 | func (p *Buffer) Marshal(pb Message) error { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 266 | // Can the object marshal itself? |
| 267 | if m, ok := pb.(Marshaler); ok { |
| 268 | data, err := m.Marshal() |
| 269 | if err != nil { |
| 270 | return err |
| 271 | } |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 272 | p.buf = append(p.buf, data...) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 273 | return nil |
| 274 | } |
| 275 | |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 276 | t, base, err := getbase(pb) |
| 277 | if structPointer_IsNil(base) { |
| David Symonds | d4661c5 | 2012-08-30 15:17:53 +1000 | [diff] [blame] | 278 | return ErrNil |
| 279 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 280 | if err == nil { |
| David Symonds | ba7896c | 2014-11-20 15:29:05 +1100 | [diff] [blame] | 281 | err = p.enc_struct(GetProperties(t.Elem()), base) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 282 | } |
| 283 | |
| David Symonds | 9f60f43 | 2012-06-14 09:45:25 +1000 | [diff] [blame] | 284 | if collectStats { |
| 285 | stats.Encode++ |
| 286 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 287 | |
| Ross Light | cd85f19 | 2016-05-19 15:50:37 -0400 | [diff] [blame] | 288 | if len(p.buf) > maxMarshalSize { |
| 289 | return ErrTooLarge |
| 290 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 291 | return err |
| 292 | } |
| 293 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 294 | // Size returns the encoded size of a protocol buffer. |
| 295 | func Size(pb Message) (n int) { |
| 296 | // Can the object marshal itself? If so, Size is slow. |
| 297 | // TODO: add Size to Marshaler, or add a Sizer interface. |
| 298 | if m, ok := pb.(Marshaler); ok { |
| 299 | b, _ := m.Marshal() |
| 300 | return len(b) |
| 301 | } |
| 302 | |
| 303 | t, base, err := getbase(pb) |
| 304 | if structPointer_IsNil(base) { |
| 305 | return 0 |
| 306 | } |
| 307 | if err == nil { |
| David Symonds | ba7896c | 2014-11-20 15:29:05 +1100 | [diff] [blame] | 308 | n = size_struct(GetProperties(t.Elem()), base) |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | if collectStats { |
| 312 | stats.Size++ |
| 313 | } |
| 314 | |
| 315 | return |
| 316 | } |
| 317 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 318 | // Individual type encoders. |
| 319 | |
| 320 | // Encode a bool. |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 321 | func (o *Buffer) enc_bool(p *Properties, base structPointer) error { |
| 322 | v := *structPointer_Bool(base, p.field) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 323 | if v == nil { |
| 324 | return ErrNil |
| 325 | } |
| Rob Pike | 0f42a27 | 2011-10-20 16:03:11 -0700 | [diff] [blame] | 326 | x := 0 |
| 327 | if *v { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 328 | x = 1 |
| 329 | } |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 330 | o.buf = append(o.buf, p.tagcode...) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 331 | p.valEnc(o, uint64(x)) |
| 332 | return nil |
| 333 | } |
| 334 | |
| David Symonds | abd3b41 | 2014-11-28 11:43:44 +1100 | [diff] [blame] | 335 | func (o *Buffer) enc_proto3_bool(p *Properties, base structPointer) error { |
| 336 | v := *structPointer_BoolVal(base, p.field) |
| 337 | if !v { |
| 338 | return ErrNil |
| 339 | } |
| 340 | o.buf = append(o.buf, p.tagcode...) |
| 341 | p.valEnc(o, 1) |
| 342 | return nil |
| 343 | } |
| 344 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 345 | func size_bool(p *Properties, base structPointer) int { |
| 346 | v := *structPointer_Bool(base, p.field) |
| 347 | if v == nil { |
| 348 | return 0 |
| 349 | } |
| 350 | return len(p.tagcode) + 1 // each bool takes exactly one byte |
| 351 | } |
| 352 | |
| David Symonds | abd3b41 | 2014-11-28 11:43:44 +1100 | [diff] [blame] | 353 | func size_proto3_bool(p *Properties, base structPointer) int { |
| 354 | v := *structPointer_BoolVal(base, p.field) |
| David Symonds | 535a104 | 2015-09-11 08:27:00 +1000 | [diff] [blame] | 355 | if !v && !p.oneof { |
| David Symonds | abd3b41 | 2014-11-28 11:43:44 +1100 | [diff] [blame] | 356 | return 0 |
| 357 | } |
| 358 | return len(p.tagcode) + 1 // each bool takes exactly one byte |
| 359 | } |
| 360 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 361 | // Encode an int32. |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 362 | func (o *Buffer) enc_int32(p *Properties, base structPointer) error { |
| 363 | v := structPointer_Word32(base, p.field) |
| 364 | if word32_IsNil(v) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 365 | return ErrNil |
| 366 | } |
| David Symonds | f054e84 | 2014-07-22 14:06:27 +1000 | [diff] [blame] | 367 | x := int32(word32_Get(v)) // permit sign extension to use full 64-bit range |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 368 | o.buf = append(o.buf, p.tagcode...) |
| David Symonds | c31645c | 2013-06-22 17:57:36 +1000 | [diff] [blame] | 369 | p.valEnc(o, uint64(x)) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 370 | return nil |
| 371 | } |
| 372 | |
| David Symonds | abd3b41 | 2014-11-28 11:43:44 +1100 | [diff] [blame] | 373 | func (o *Buffer) enc_proto3_int32(p *Properties, base structPointer) error { |
| 374 | v := structPointer_Word32Val(base, p.field) |
| 375 | x := int32(word32Val_Get(v)) // permit sign extension to use full 64-bit range |
| 376 | if x == 0 { |
| 377 | return ErrNil |
| 378 | } |
| 379 | o.buf = append(o.buf, p.tagcode...) |
| 380 | p.valEnc(o, uint64(x)) |
| 381 | return nil |
| 382 | } |
| 383 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 384 | func size_int32(p *Properties, base structPointer) (n int) { |
| 385 | v := structPointer_Word32(base, p.field) |
| 386 | if word32_IsNil(v) { |
| 387 | return 0 |
| 388 | } |
| David Symonds | f054e84 | 2014-07-22 14:06:27 +1000 | [diff] [blame] | 389 | x := int32(word32_Get(v)) // permit sign extension to use full 64-bit range |
| 390 | n += len(p.tagcode) |
| 391 | n += p.valSize(uint64(x)) |
| 392 | return |
| 393 | } |
| 394 | |
| David Symonds | abd3b41 | 2014-11-28 11:43:44 +1100 | [diff] [blame] | 395 | func size_proto3_int32(p *Properties, base structPointer) (n int) { |
| 396 | v := structPointer_Word32Val(base, p.field) |
| 397 | x := int32(word32Val_Get(v)) // permit sign extension to use full 64-bit range |
| David Symonds | 535a104 | 2015-09-11 08:27:00 +1000 | [diff] [blame] | 398 | if x == 0 && !p.oneof { |
| David Symonds | abd3b41 | 2014-11-28 11:43:44 +1100 | [diff] [blame] | 399 | return 0 |
| 400 | } |
| 401 | n += len(p.tagcode) |
| 402 | n += p.valSize(uint64(x)) |
| 403 | return |
| 404 | } |
| 405 | |
| David Symonds | f054e84 | 2014-07-22 14:06:27 +1000 | [diff] [blame] | 406 | // Encode a uint32. |
| 407 | // Exactly the same as int32, except for no sign extension. |
| 408 | func (o *Buffer) enc_uint32(p *Properties, base structPointer) error { |
| 409 | v := structPointer_Word32(base, p.field) |
| 410 | if word32_IsNil(v) { |
| 411 | return ErrNil |
| 412 | } |
| 413 | x := word32_Get(v) |
| 414 | o.buf = append(o.buf, p.tagcode...) |
| 415 | p.valEnc(o, uint64(x)) |
| 416 | return nil |
| 417 | } |
| 418 | |
| David Symonds | abd3b41 | 2014-11-28 11:43:44 +1100 | [diff] [blame] | 419 | func (o *Buffer) enc_proto3_uint32(p *Properties, base structPointer) error { |
| 420 | v := structPointer_Word32Val(base, p.field) |
| 421 | x := word32Val_Get(v) |
| 422 | if x == 0 { |
| 423 | return ErrNil |
| 424 | } |
| 425 | o.buf = append(o.buf, p.tagcode...) |
| 426 | p.valEnc(o, uint64(x)) |
| 427 | return nil |
| 428 | } |
| 429 | |
| David Symonds | f054e84 | 2014-07-22 14:06:27 +1000 | [diff] [blame] | 430 | func size_uint32(p *Properties, base structPointer) (n int) { |
| 431 | v := structPointer_Word32(base, p.field) |
| 432 | if word32_IsNil(v) { |
| 433 | return 0 |
| 434 | } |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 435 | x := word32_Get(v) |
| 436 | n += len(p.tagcode) |
| 437 | n += p.valSize(uint64(x)) |
| 438 | return |
| 439 | } |
| 440 | |
| David Symonds | abd3b41 | 2014-11-28 11:43:44 +1100 | [diff] [blame] | 441 | func size_proto3_uint32(p *Properties, base structPointer) (n int) { |
| 442 | v := structPointer_Word32Val(base, p.field) |
| 443 | x := word32Val_Get(v) |
| David Symonds | 535a104 | 2015-09-11 08:27:00 +1000 | [diff] [blame] | 444 | if x == 0 && !p.oneof { |
| David Symonds | abd3b41 | 2014-11-28 11:43:44 +1100 | [diff] [blame] | 445 | return 0 |
| 446 | } |
| 447 | n += len(p.tagcode) |
| 448 | n += p.valSize(uint64(x)) |
| 449 | return |
| 450 | } |
| 451 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 452 | // Encode an int64. |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 453 | func (o *Buffer) enc_int64(p *Properties, base structPointer) error { |
| 454 | v := structPointer_Word64(base, p.field) |
| 455 | if word64_IsNil(v) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 456 | return ErrNil |
| 457 | } |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 458 | x := word64_Get(v) |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 459 | o.buf = append(o.buf, p.tagcode...) |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 460 | p.valEnc(o, x) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 461 | return nil |
| 462 | } |
| 463 | |
| David Symonds | abd3b41 | 2014-11-28 11:43:44 +1100 | [diff] [blame] | 464 | func (o *Buffer) enc_proto3_int64(p *Properties, base structPointer) error { |
| 465 | v := structPointer_Word64Val(base, p.field) |
| 466 | x := word64Val_Get(v) |
| 467 | if x == 0 { |
| 468 | return ErrNil |
| 469 | } |
| 470 | o.buf = append(o.buf, p.tagcode...) |
| 471 | p.valEnc(o, x) |
| 472 | return nil |
| 473 | } |
| 474 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 475 | func size_int64(p *Properties, base structPointer) (n int) { |
| 476 | v := structPointer_Word64(base, p.field) |
| 477 | if word64_IsNil(v) { |
| 478 | return 0 |
| 479 | } |
| 480 | x := word64_Get(v) |
| 481 | n += len(p.tagcode) |
| 482 | n += p.valSize(x) |
| 483 | return |
| 484 | } |
| 485 | |
| David Symonds | abd3b41 | 2014-11-28 11:43:44 +1100 | [diff] [blame] | 486 | func size_proto3_int64(p *Properties, base structPointer) (n int) { |
| 487 | v := structPointer_Word64Val(base, p.field) |
| 488 | x := word64Val_Get(v) |
| David Symonds | 535a104 | 2015-09-11 08:27:00 +1000 | [diff] [blame] | 489 | if x == 0 && !p.oneof { |
| David Symonds | abd3b41 | 2014-11-28 11:43:44 +1100 | [diff] [blame] | 490 | return 0 |
| 491 | } |
| 492 | n += len(p.tagcode) |
| 493 | n += p.valSize(x) |
| 494 | return |
| 495 | } |
| 496 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 497 | // Encode a string. |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 498 | func (o *Buffer) enc_string(p *Properties, base structPointer) error { |
| 499 | v := *structPointer_String(base, p.field) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 500 | if v == nil { |
| 501 | return ErrNil |
| 502 | } |
| 503 | x := *v |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 504 | o.buf = append(o.buf, p.tagcode...) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 505 | o.EncodeStringBytes(x) |
| 506 | return nil |
| 507 | } |
| 508 | |
| David Symonds | abd3b41 | 2014-11-28 11:43:44 +1100 | [diff] [blame] | 509 | func (o *Buffer) enc_proto3_string(p *Properties, base structPointer) error { |
| 510 | v := *structPointer_StringVal(base, p.field) |
| 511 | if v == "" { |
| 512 | return ErrNil |
| 513 | } |
| 514 | o.buf = append(o.buf, p.tagcode...) |
| 515 | o.EncodeStringBytes(v) |
| 516 | return nil |
| 517 | } |
| 518 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 519 | func size_string(p *Properties, base structPointer) (n int) { |
| 520 | v := *structPointer_String(base, p.field) |
| 521 | if v == nil { |
| 522 | return 0 |
| 523 | } |
| 524 | x := *v |
| 525 | n += len(p.tagcode) |
| 526 | n += sizeStringBytes(x) |
| 527 | return |
| 528 | } |
| 529 | |
| David Symonds | abd3b41 | 2014-11-28 11:43:44 +1100 | [diff] [blame] | 530 | func size_proto3_string(p *Properties, base structPointer) (n int) { |
| 531 | v := *structPointer_StringVal(base, p.field) |
| David Symonds | 535a104 | 2015-09-11 08:27:00 +1000 | [diff] [blame] | 532 | if v == "" && !p.oneof { |
| David Symonds | abd3b41 | 2014-11-28 11:43:44 +1100 | [diff] [blame] | 533 | return 0 |
| 534 | } |
| 535 | n += len(p.tagcode) |
| 536 | n += sizeStringBytes(v) |
| 537 | return |
| 538 | } |
| 539 | |
| Rob Pike | 97e934d | 2011-04-11 12:52:49 -0700 | [diff] [blame] | 540 | // All protocol buffer fields are nillable, but be careful. |
| 541 | func isNil(v reflect.Value) bool { |
| 542 | switch v.Kind() { |
| David Symonds | 007ed9d | 2012-07-24 10:59:36 +1000 | [diff] [blame] | 543 | case reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice: |
| Rob Pike | 97e934d | 2011-04-11 12:52:49 -0700 | [diff] [blame] | 544 | return v.IsNil() |
| 545 | } |
| 546 | return false |
| 547 | } |
| 548 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 549 | // Encode a message struct. |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 550 | func (o *Buffer) enc_struct_message(p *Properties, base structPointer) error { |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 551 | var state errorState |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 552 | structp := structPointer_GetStructPointer(base, p.field) |
| 553 | if structPointer_IsNil(structp) { |
| David Symonds | a80b282 | 2012-03-14 14:31:25 +1100 | [diff] [blame] | 554 | return ErrNil |
| 555 | } |
| 556 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 557 | // Can the object marshal itself? |
| David Symonds | a80b282 | 2012-03-14 14:31:25 +1100 | [diff] [blame] | 558 | if p.isMarshaler { |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 559 | m := structPointer_Interface(structp, p.stype).(Marshaler) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 560 | data, err := m.Marshal() |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 561 | if err != nil && !state.shouldContinue(err, nil) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 562 | return err |
| 563 | } |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 564 | o.buf = append(o.buf, p.tagcode...) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 565 | o.EncodeRawBytes(data) |
| David Symonds | 68c687d | 2015-07-27 19:06:00 +1000 | [diff] [blame] | 566 | return state.err |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 567 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 568 | |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 569 | o.buf = append(o.buf, p.tagcode...) |
| David Symonds | ba7896c | 2014-11-20 15:29:05 +1100 | [diff] [blame] | 570 | return o.enc_len_struct(p.sprop, structp, &state) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 571 | } |
| 572 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 573 | func size_struct_message(p *Properties, base structPointer) int { |
| 574 | structp := structPointer_GetStructPointer(base, p.field) |
| 575 | if structPointer_IsNil(structp) { |
| 576 | return 0 |
| 577 | } |
| 578 | |
| 579 | // Can the object marshal itself? |
| 580 | if p.isMarshaler { |
| 581 | m := structPointer_Interface(structp, p.stype).(Marshaler) |
| 582 | data, _ := m.Marshal() |
| 583 | n0 := len(p.tagcode) |
| 584 | n1 := sizeRawBytes(data) |
| 585 | return n0 + n1 |
| 586 | } |
| 587 | |
| 588 | n0 := len(p.tagcode) |
| David Symonds | ba7896c | 2014-11-20 15:29:05 +1100 | [diff] [blame] | 589 | n1 := size_struct(p.sprop, structp) |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 590 | n2 := sizeVarint(uint64(n1)) // size of encoded length |
| 591 | return n0 + n1 + n2 |
| 592 | } |
| 593 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 594 | // Encode a group struct. |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 595 | func (o *Buffer) enc_struct_group(p *Properties, base structPointer) error { |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 596 | var state errorState |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 597 | b := structPointer_GetStructPointer(base, p.field) |
| 598 | if structPointer_IsNil(b) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 599 | return ErrNil |
| 600 | } |
| 601 | |
| 602 | o.EncodeVarint(uint64((p.Tag << 3) | WireStartGroup)) |
| David Symonds | ba7896c | 2014-11-20 15:29:05 +1100 | [diff] [blame] | 603 | err := o.enc_struct(p.sprop, b) |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 604 | if err != nil && !state.shouldContinue(err, nil) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 605 | return err |
| 606 | } |
| 607 | o.EncodeVarint(uint64((p.Tag << 3) | WireEndGroup)) |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 608 | return state.err |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 609 | } |
| 610 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 611 | func size_struct_group(p *Properties, base structPointer) (n int) { |
| 612 | b := structPointer_GetStructPointer(base, p.field) |
| 613 | if structPointer_IsNil(b) { |
| 614 | return 0 |
| 615 | } |
| 616 | |
| 617 | n += sizeVarint(uint64((p.Tag << 3) | WireStartGroup)) |
| David Symonds | ba7896c | 2014-11-20 15:29:05 +1100 | [diff] [blame] | 618 | n += size_struct(p.sprop, b) |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 619 | n += sizeVarint(uint64((p.Tag << 3) | WireEndGroup)) |
| 620 | return |
| 621 | } |
| 622 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 623 | // Encode a slice of bools ([]bool). |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 624 | func (o *Buffer) enc_slice_bool(p *Properties, base structPointer) error { |
| 625 | s := *structPointer_BoolSlice(base, p.field) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 626 | l := len(s) |
| 627 | if l == 0 { |
| 628 | return ErrNil |
| 629 | } |
| 630 | for _, x := range s { |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 631 | o.buf = append(o.buf, p.tagcode...) |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 632 | v := uint64(0) |
| 633 | if x { |
| 634 | v = 1 |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 635 | } |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 636 | p.valEnc(o, v) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 637 | } |
| 638 | return nil |
| 639 | } |
| 640 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 641 | func size_slice_bool(p *Properties, base structPointer) int { |
| 642 | s := *structPointer_BoolSlice(base, p.field) |
| 643 | l := len(s) |
| 644 | if l == 0 { |
| 645 | return 0 |
| 646 | } |
| 647 | return l * (len(p.tagcode) + 1) // each bool takes exactly one byte |
| 648 | } |
| 649 | |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 650 | // Encode a slice of bools ([]bool) in packed format. |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 651 | func (o *Buffer) enc_slice_packed_bool(p *Properties, base structPointer) error { |
| 652 | s := *structPointer_BoolSlice(base, p.field) |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 653 | l := len(s) |
| 654 | if l == 0 { |
| 655 | return ErrNil |
| 656 | } |
| 657 | o.buf = append(o.buf, p.tagcode...) |
| 658 | o.EncodeVarint(uint64(l)) // each bool takes exactly one byte |
| 659 | for _, x := range s { |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 660 | v := uint64(0) |
| 661 | if x { |
| 662 | v = 1 |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 663 | } |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 664 | p.valEnc(o, v) |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 665 | } |
| 666 | return nil |
| 667 | } |
| 668 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 669 | func size_slice_packed_bool(p *Properties, base structPointer) (n int) { |
| 670 | s := *structPointer_BoolSlice(base, p.field) |
| 671 | l := len(s) |
| 672 | if l == 0 { |
| 673 | return 0 |
| 674 | } |
| 675 | n += len(p.tagcode) |
| 676 | n += sizeVarint(uint64(l)) |
| 677 | n += l // each bool takes exactly one byte |
| 678 | return |
| 679 | } |
| 680 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 681 | // Encode a slice of bytes ([]byte). |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 682 | func (o *Buffer) enc_slice_byte(p *Properties, base structPointer) error { |
| 683 | s := *structPointer_Bytes(base, p.field) |
| Mikkel Krautz | db488aa | 2010-11-29 14:15:51 -0800 | [diff] [blame] | 684 | if s == nil { |
| 685 | return ErrNil |
| 686 | } |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 687 | o.buf = append(o.buf, p.tagcode...) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 688 | o.EncodeRawBytes(s) |
| 689 | return nil |
| 690 | } |
| 691 | |
| David Symonds | abd3b41 | 2014-11-28 11:43:44 +1100 | [diff] [blame] | 692 | func (o *Buffer) enc_proto3_slice_byte(p *Properties, base structPointer) error { |
| 693 | s := *structPointer_Bytes(base, p.field) |
| 694 | if len(s) == 0 { |
| 695 | return ErrNil |
| 696 | } |
| 697 | o.buf = append(o.buf, p.tagcode...) |
| 698 | o.EncodeRawBytes(s) |
| 699 | return nil |
| 700 | } |
| 701 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 702 | func size_slice_byte(p *Properties, base structPointer) (n int) { |
| 703 | s := *structPointer_Bytes(base, p.field) |
| David Symonds | 535a104 | 2015-09-11 08:27:00 +1000 | [diff] [blame] | 704 | if s == nil && !p.oneof { |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 705 | return 0 |
| 706 | } |
| 707 | n += len(p.tagcode) |
| 708 | n += sizeRawBytes(s) |
| 709 | return |
| 710 | } |
| 711 | |
| David Symonds | abd3b41 | 2014-11-28 11:43:44 +1100 | [diff] [blame] | 712 | func size_proto3_slice_byte(p *Properties, base structPointer) (n int) { |
| 713 | s := *structPointer_Bytes(base, p.field) |
| David Symonds | 535a104 | 2015-09-11 08:27:00 +1000 | [diff] [blame] | 714 | if len(s) == 0 && !p.oneof { |
| David Symonds | abd3b41 | 2014-11-28 11:43:44 +1100 | [diff] [blame] | 715 | return 0 |
| 716 | } |
| 717 | n += len(p.tagcode) |
| 718 | n += sizeRawBytes(s) |
| 719 | return |
| 720 | } |
| 721 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 722 | // Encode a slice of int32s ([]int32). |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 723 | func (o *Buffer) enc_slice_int32(p *Properties, base structPointer) error { |
| 724 | s := structPointer_Word32Slice(base, p.field) |
| 725 | l := s.Len() |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 726 | if l == 0 { |
| 727 | return ErrNil |
| 728 | } |
| 729 | for i := 0; i < l; i++ { |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 730 | o.buf = append(o.buf, p.tagcode...) |
| David Symonds | 0ec36a2 | 2014-08-12 13:21:46 +1000 | [diff] [blame] | 731 | x := int32(s.Index(i)) // permit sign extension to use full 64-bit range |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 732 | p.valEnc(o, uint64(x)) |
| 733 | } |
| 734 | return nil |
| 735 | } |
| 736 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 737 | func size_slice_int32(p *Properties, base structPointer) (n int) { |
| 738 | s := structPointer_Word32Slice(base, p.field) |
| 739 | l := s.Len() |
| 740 | if l == 0 { |
| 741 | return 0 |
| 742 | } |
| 743 | for i := 0; i < l; i++ { |
| 744 | n += len(p.tagcode) |
| David Symonds | 0ec36a2 | 2014-08-12 13:21:46 +1000 | [diff] [blame] | 745 | x := int32(s.Index(i)) // permit sign extension to use full 64-bit range |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 746 | n += p.valSize(uint64(x)) |
| 747 | } |
| 748 | return |
| 749 | } |
| 750 | |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 751 | // Encode a slice of int32s ([]int32) in packed format. |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 752 | func (o *Buffer) enc_slice_packed_int32(p *Properties, base structPointer) error { |
| 753 | s := structPointer_Word32Slice(base, p.field) |
| 754 | l := s.Len() |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 755 | if l == 0 { |
| 756 | return ErrNil |
| 757 | } |
| 758 | // TODO: Reuse a Buffer. |
| 759 | buf := NewBuffer(nil) |
| 760 | for i := 0; i < l; i++ { |
| David Symonds | 0ec36a2 | 2014-08-12 13:21:46 +1000 | [diff] [blame] | 761 | x := int32(s.Index(i)) // permit sign extension to use full 64-bit range |
| 762 | p.valEnc(buf, uint64(x)) |
| 763 | } |
| 764 | |
| 765 | o.buf = append(o.buf, p.tagcode...) |
| 766 | o.EncodeVarint(uint64(len(buf.buf))) |
| 767 | o.buf = append(o.buf, buf.buf...) |
| 768 | return nil |
| 769 | } |
| 770 | |
| 771 | func size_slice_packed_int32(p *Properties, base structPointer) (n int) { |
| 772 | s := structPointer_Word32Slice(base, p.field) |
| 773 | l := s.Len() |
| 774 | if l == 0 { |
| 775 | return 0 |
| 776 | } |
| 777 | var bufSize int |
| 778 | for i := 0; i < l; i++ { |
| 779 | x := int32(s.Index(i)) // permit sign extension to use full 64-bit range |
| 780 | bufSize += p.valSize(uint64(x)) |
| 781 | } |
| 782 | |
| 783 | n += len(p.tagcode) |
| 784 | n += sizeVarint(uint64(bufSize)) |
| 785 | n += bufSize |
| 786 | return |
| 787 | } |
| 788 | |
| 789 | // Encode a slice of uint32s ([]uint32). |
| 790 | // Exactly the same as int32, except for no sign extension. |
| 791 | func (o *Buffer) enc_slice_uint32(p *Properties, base structPointer) error { |
| 792 | s := structPointer_Word32Slice(base, p.field) |
| 793 | l := s.Len() |
| 794 | if l == 0 { |
| 795 | return ErrNil |
| 796 | } |
| 797 | for i := 0; i < l; i++ { |
| 798 | o.buf = append(o.buf, p.tagcode...) |
| 799 | x := s.Index(i) |
| 800 | p.valEnc(o, uint64(x)) |
| 801 | } |
| 802 | return nil |
| 803 | } |
| 804 | |
| 805 | func size_slice_uint32(p *Properties, base structPointer) (n int) { |
| 806 | s := structPointer_Word32Slice(base, p.field) |
| 807 | l := s.Len() |
| 808 | if l == 0 { |
| 809 | return 0 |
| 810 | } |
| 811 | for i := 0; i < l; i++ { |
| 812 | n += len(p.tagcode) |
| 813 | x := s.Index(i) |
| 814 | n += p.valSize(uint64(x)) |
| 815 | } |
| 816 | return |
| 817 | } |
| 818 | |
| 819 | // Encode a slice of uint32s ([]uint32) in packed format. |
| 820 | // Exactly the same as int32, except for no sign extension. |
| 821 | func (o *Buffer) enc_slice_packed_uint32(p *Properties, base structPointer) error { |
| 822 | s := structPointer_Word32Slice(base, p.field) |
| 823 | l := s.Len() |
| 824 | if l == 0 { |
| 825 | return ErrNil |
| 826 | } |
| 827 | // TODO: Reuse a Buffer. |
| 828 | buf := NewBuffer(nil) |
| 829 | for i := 0; i < l; i++ { |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 830 | p.valEnc(buf, uint64(s.Index(i))) |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 831 | } |
| 832 | |
| 833 | o.buf = append(o.buf, p.tagcode...) |
| 834 | o.EncodeVarint(uint64(len(buf.buf))) |
| 835 | o.buf = append(o.buf, buf.buf...) |
| 836 | return nil |
| 837 | } |
| 838 | |
| David Symonds | 0ec36a2 | 2014-08-12 13:21:46 +1000 | [diff] [blame] | 839 | func size_slice_packed_uint32(p *Properties, base structPointer) (n int) { |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 840 | s := structPointer_Word32Slice(base, p.field) |
| 841 | l := s.Len() |
| 842 | if l == 0 { |
| 843 | return 0 |
| 844 | } |
| 845 | var bufSize int |
| 846 | for i := 0; i < l; i++ { |
| 847 | bufSize += p.valSize(uint64(s.Index(i))) |
| 848 | } |
| 849 | |
| 850 | n += len(p.tagcode) |
| 851 | n += sizeVarint(uint64(bufSize)) |
| 852 | n += bufSize |
| 853 | return |
| 854 | } |
| 855 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 856 | // Encode a slice of int64s ([]int64). |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 857 | func (o *Buffer) enc_slice_int64(p *Properties, base structPointer) error { |
| 858 | s := structPointer_Word64Slice(base, p.field) |
| 859 | l := s.Len() |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 860 | if l == 0 { |
| 861 | return ErrNil |
| 862 | } |
| 863 | for i := 0; i < l; i++ { |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 864 | o.buf = append(o.buf, p.tagcode...) |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 865 | p.valEnc(o, s.Index(i)) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 866 | } |
| 867 | return nil |
| 868 | } |
| 869 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 870 | func size_slice_int64(p *Properties, base structPointer) (n int) { |
| 871 | s := structPointer_Word64Slice(base, p.field) |
| 872 | l := s.Len() |
| 873 | if l == 0 { |
| 874 | return 0 |
| 875 | } |
| 876 | for i := 0; i < l; i++ { |
| 877 | n += len(p.tagcode) |
| 878 | n += p.valSize(s.Index(i)) |
| 879 | } |
| 880 | return |
| 881 | } |
| 882 | |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 883 | // Encode a slice of int64s ([]int64) in packed format. |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 884 | func (o *Buffer) enc_slice_packed_int64(p *Properties, base structPointer) error { |
| 885 | s := structPointer_Word64Slice(base, p.field) |
| 886 | l := s.Len() |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 887 | if l == 0 { |
| 888 | return ErrNil |
| 889 | } |
| 890 | // TODO: Reuse a Buffer. |
| 891 | buf := NewBuffer(nil) |
| 892 | for i := 0; i < l; i++ { |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 893 | p.valEnc(buf, s.Index(i)) |
| David Symonds | 5b7775e | 2010-12-01 10:09:04 +1100 | [diff] [blame] | 894 | } |
| 895 | |
| 896 | o.buf = append(o.buf, p.tagcode...) |
| 897 | o.EncodeVarint(uint64(len(buf.buf))) |
| 898 | o.buf = append(o.buf, buf.buf...) |
| 899 | return nil |
| 900 | } |
| 901 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 902 | func size_slice_packed_int64(p *Properties, base structPointer) (n int) { |
| 903 | s := structPointer_Word64Slice(base, p.field) |
| 904 | l := s.Len() |
| 905 | if l == 0 { |
| 906 | return 0 |
| 907 | } |
| 908 | var bufSize int |
| 909 | for i := 0; i < l; i++ { |
| 910 | bufSize += p.valSize(s.Index(i)) |
| 911 | } |
| 912 | |
| 913 | n += len(p.tagcode) |
| 914 | n += sizeVarint(uint64(bufSize)) |
| 915 | n += bufSize |
| 916 | return |
| 917 | } |
| 918 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 919 | // Encode a slice of slice of bytes ([][]byte). |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 920 | func (o *Buffer) enc_slice_slice_byte(p *Properties, base structPointer) error { |
| 921 | ss := *structPointer_BytesSlice(base, p.field) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 922 | l := len(ss) |
| 923 | if l == 0 { |
| 924 | return ErrNil |
| 925 | } |
| 926 | for i := 0; i < l; i++ { |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 927 | o.buf = append(o.buf, p.tagcode...) |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 928 | o.EncodeRawBytes(ss[i]) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 929 | } |
| 930 | return nil |
| 931 | } |
| 932 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 933 | func size_slice_slice_byte(p *Properties, base structPointer) (n int) { |
| 934 | ss := *structPointer_BytesSlice(base, p.field) |
| 935 | l := len(ss) |
| 936 | if l == 0 { |
| 937 | return 0 |
| 938 | } |
| 939 | n += l * len(p.tagcode) |
| 940 | for i := 0; i < l; i++ { |
| 941 | n += sizeRawBytes(ss[i]) |
| 942 | } |
| 943 | return |
| 944 | } |
| 945 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 946 | // Encode a slice of strings ([]string). |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 947 | func (o *Buffer) enc_slice_string(p *Properties, base structPointer) error { |
| 948 | ss := *structPointer_StringSlice(base, p.field) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 949 | l := len(ss) |
| 950 | for i := 0; i < l; i++ { |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 951 | o.buf = append(o.buf, p.tagcode...) |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 952 | o.EncodeStringBytes(ss[i]) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 953 | } |
| 954 | return nil |
| 955 | } |
| 956 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 957 | func size_slice_string(p *Properties, base structPointer) (n int) { |
| 958 | ss := *structPointer_StringSlice(base, p.field) |
| 959 | l := len(ss) |
| 960 | n += l * len(p.tagcode) |
| 961 | for i := 0; i < l; i++ { |
| 962 | n += sizeStringBytes(ss[i]) |
| 963 | } |
| 964 | return |
| 965 | } |
| 966 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 967 | // Encode a slice of message structs ([]*struct). |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 968 | func (o *Buffer) enc_slice_struct_message(p *Properties, base structPointer) error { |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 969 | var state errorState |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 970 | s := structPointer_StructPointerSlice(base, p.field) |
| 971 | l := s.Len() |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 972 | |
| 973 | for i := 0; i < l; i++ { |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 974 | structp := s.Index(i) |
| 975 | if structPointer_IsNil(structp) { |
| David Symonds | f62db48 | 2015-02-23 12:37:00 +1100 | [diff] [blame] | 976 | return errRepeatedHasNil |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 977 | } |
| 978 | |
| 979 | // Can the object marshal itself? |
| David Symonds | a80b282 | 2012-03-14 14:31:25 +1100 | [diff] [blame] | 980 | if p.isMarshaler { |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 981 | m := structPointer_Interface(structp, p.stype).(Marshaler) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 982 | data, err := m.Marshal() |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 983 | if err != nil && !state.shouldContinue(err, nil) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 984 | return err |
| 985 | } |
| Rob Pike | 99fa2b6 | 2010-12-02 10:39:42 -0800 | [diff] [blame] | 986 | o.buf = append(o.buf, p.tagcode...) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 987 | o.EncodeRawBytes(data) |
| 988 | continue |
| 989 | } |
| 990 | |
| David Symonds | 1d8ba13 | 2014-01-13 16:01:15 +1100 | [diff] [blame] | 991 | o.buf = append(o.buf, p.tagcode...) |
| David Symonds | ba7896c | 2014-11-20 15:29:05 +1100 | [diff] [blame] | 992 | err := o.enc_len_struct(p.sprop, structp, &state) |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 993 | if err != nil && !state.shouldContinue(err, nil) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 994 | if err == ErrNil { |
| David Symonds | f62db48 | 2015-02-23 12:37:00 +1100 | [diff] [blame] | 995 | return errRepeatedHasNil |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 996 | } |
| 997 | return err |
| 998 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 999 | } |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 1000 | return state.err |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1001 | } |
| 1002 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 1003 | func size_slice_struct_message(p *Properties, base structPointer) (n int) { |
| 1004 | s := structPointer_StructPointerSlice(base, p.field) |
| 1005 | l := s.Len() |
| 1006 | n += l * len(p.tagcode) |
| 1007 | for i := 0; i < l; i++ { |
| 1008 | structp := s.Index(i) |
| 1009 | if structPointer_IsNil(structp) { |
| 1010 | return // return the size up to this point |
| 1011 | } |
| 1012 | |
| 1013 | // Can the object marshal itself? |
| 1014 | if p.isMarshaler { |
| 1015 | m := structPointer_Interface(structp, p.stype).(Marshaler) |
| 1016 | data, _ := m.Marshal() |
| 1017 | n += len(p.tagcode) |
| 1018 | n += sizeRawBytes(data) |
| 1019 | continue |
| 1020 | } |
| 1021 | |
| David Symonds | ba7896c | 2014-11-20 15:29:05 +1100 | [diff] [blame] | 1022 | n0 := size_struct(p.sprop, structp) |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 1023 | n1 := sizeVarint(uint64(n0)) // size of encoded length |
| 1024 | n += n0 + n1 |
| 1025 | } |
| 1026 | return |
| 1027 | } |
| 1028 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1029 | // Encode a slice of group structs ([]*struct). |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 1030 | func (o *Buffer) enc_slice_struct_group(p *Properties, base structPointer) error { |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 1031 | var state errorState |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 1032 | s := structPointer_StructPointerSlice(base, p.field) |
| 1033 | l := s.Len() |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1034 | |
| 1035 | for i := 0; i < l; i++ { |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 1036 | b := s.Index(i) |
| 1037 | if structPointer_IsNil(b) { |
| David Symonds | f62db48 | 2015-02-23 12:37:00 +1100 | [diff] [blame] | 1038 | return errRepeatedHasNil |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1039 | } |
| 1040 | |
| 1041 | o.EncodeVarint(uint64((p.Tag << 3) | WireStartGroup)) |
| 1042 | |
| David Symonds | ba7896c | 2014-11-20 15:29:05 +1100 | [diff] [blame] | 1043 | err := o.enc_struct(p.sprop, b) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1044 | |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 1045 | if err != nil && !state.shouldContinue(err, nil) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1046 | if err == ErrNil { |
| David Symonds | f62db48 | 2015-02-23 12:37:00 +1100 | [diff] [blame] | 1047 | return errRepeatedHasNil |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1048 | } |
| 1049 | return err |
| 1050 | } |
| 1051 | |
| 1052 | o.EncodeVarint(uint64((p.Tag << 3) | WireEndGroup)) |
| 1053 | } |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 1054 | return state.err |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1055 | } |
| 1056 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 1057 | func size_slice_struct_group(p *Properties, base structPointer) (n int) { |
| 1058 | s := structPointer_StructPointerSlice(base, p.field) |
| 1059 | l := s.Len() |
| 1060 | |
| 1061 | n += l * sizeVarint(uint64((p.Tag<<3)|WireStartGroup)) |
| 1062 | n += l * sizeVarint(uint64((p.Tag<<3)|WireEndGroup)) |
| 1063 | for i := 0; i < l; i++ { |
| 1064 | b := s.Index(i) |
| 1065 | if structPointer_IsNil(b) { |
| 1066 | return // return size up to this point |
| 1067 | } |
| 1068 | |
| David Symonds | ba7896c | 2014-11-20 15:29:05 +1100 | [diff] [blame] | 1069 | n += size_struct(p.sprop, b) |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 1070 | } |
| 1071 | return |
| 1072 | } |
| 1073 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1074 | // Encode an extension map. |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 1075 | func (o *Buffer) enc_map(p *Properties, base structPointer) error { |
| matloob@google.com | e51d002 | 2016-05-23 09:09:04 -0400 | [diff] [blame^] | 1076 | exts := structPointer_ExtMap(base, p.field) |
| 1077 | if err := encodeExtensionsMap(*exts); err != nil { |
| David Symonds | 1d72f7a | 2011-08-19 18:28:52 +1000 | [diff] [blame] | 1078 | return err |
| 1079 | } |
| matloob@google.com | e51d002 | 2016-05-23 09:09:04 -0400 | [diff] [blame^] | 1080 | |
| 1081 | return o.enc_map_body(*exts) |
| 1082 | } |
| 1083 | |
| 1084 | func (o *Buffer) enc_exts(p *Properties, base structPointer) error { |
| 1085 | exts := structPointer_Extensions(base, p.field) |
| 1086 | if err := encodeExtensions(exts); err != nil { |
| 1087 | return err |
| 1088 | } |
| 1089 | v, _ := exts.extensionsRead() |
| 1090 | |
| 1091 | return o.enc_map_body(v) |
| 1092 | } |
| 1093 | |
| 1094 | func (o *Buffer) enc_map_body(v map[int32]Extension) error { |
| David Symonds | df583ae | 2012-12-06 14:06:53 +1100 | [diff] [blame] | 1095 | // Fast-path for common cases: zero or one extensions. |
| 1096 | if len(v) <= 1 { |
| 1097 | for _, e := range v { |
| 1098 | o.buf = append(o.buf, e.enc...) |
| 1099 | } |
| 1100 | return nil |
| 1101 | } |
| 1102 | |
| 1103 | // Sort keys to provide a deterministic encoding. |
| 1104 | keys := make([]int, 0, len(v)) |
| 1105 | for k := range v { |
| 1106 | keys = append(keys, int(k)) |
| 1107 | } |
| 1108 | sort.Ints(keys) |
| 1109 | |
| 1110 | for _, k := range keys { |
| 1111 | o.buf = append(o.buf, v[int32(k)].enc...) |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1112 | } |
| 1113 | return nil |
| 1114 | } |
| 1115 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 1116 | func size_map(p *Properties, base structPointer) int { |
| matloob@google.com | e51d002 | 2016-05-23 09:09:04 -0400 | [diff] [blame^] | 1117 | v := structPointer_ExtMap(base, p.field) |
| 1118 | return extensionsMapSize(*v) |
| 1119 | } |
| 1120 | |
| 1121 | func size_exts(p *Properties, base structPointer) int { |
| 1122 | v := structPointer_Extensions(base, p.field) |
| 1123 | return extensionsSize(v) |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 1124 | } |
| 1125 | |
| David Symonds | 3ea3e05 | 2014-12-22 16:15:28 +1100 | [diff] [blame] | 1126 | // Encode a map field. |
| 1127 | func (o *Buffer) enc_new_map(p *Properties, base structPointer) error { |
| 1128 | var state errorState // XXX: or do we need to plumb this through? |
| 1129 | |
| 1130 | /* |
| 1131 | A map defined as |
| 1132 | map<key_type, value_type> map_field = N; |
| 1133 | is encoded in the same way as |
| 1134 | message MapFieldEntry { |
| 1135 | key_type key = 1; |
| 1136 | value_type value = 2; |
| 1137 | } |
| 1138 | repeated MapFieldEntry map_field = N; |
| 1139 | */ |
| 1140 | |
| David Symonds | 0f7a9ca | 2015-07-20 16:00:00 +1000 | [diff] [blame] | 1141 | v := structPointer_NewAt(base, p.field, p.mtype).Elem() // map[K]V |
| David Symonds | 3ea3e05 | 2014-12-22 16:15:28 +1100 | [diff] [blame] | 1142 | if v.Len() == 0 { |
| 1143 | return nil |
| 1144 | } |
| 1145 | |
| 1146 | keycopy, valcopy, keybase, valbase := mapEncodeScratch(p.mtype) |
| 1147 | |
| 1148 | enc := func() error { |
| 1149 | if err := p.mkeyprop.enc(o, p.mkeyprop, keybase); err != nil { |
| 1150 | return err |
| 1151 | } |
| 1152 | if err := p.mvalprop.enc(o, p.mvalprop, valbase); err != nil { |
| 1153 | return err |
| 1154 | } |
| 1155 | return nil |
| 1156 | } |
| 1157 | |
| David Symonds | efcaa34 | 2015-10-28 16:14:47 +1100 | [diff] [blame] | 1158 | // Don't sort map keys. It is not required by the spec, and C++ doesn't do it. |
| 1159 | for _, key := range v.MapKeys() { |
| David Symonds | 3ea3e05 | 2014-12-22 16:15:28 +1100 | [diff] [blame] | 1160 | val := v.MapIndex(key) |
| 1161 | |
| David Symonds | cab84a3 | 2015-05-19 09:29:00 +1000 | [diff] [blame] | 1162 | // The only illegal map entry values are nil message pointers. |
| 1163 | if val.Kind() == reflect.Ptr && val.IsNil() { |
| 1164 | return errors.New("proto: map has nil element") |
| 1165 | } |
| 1166 | |
| David Symonds | 3ea3e05 | 2014-12-22 16:15:28 +1100 | [diff] [blame] | 1167 | keycopy.Set(key) |
| 1168 | valcopy.Set(val) |
| 1169 | |
| 1170 | o.buf = append(o.buf, p.tagcode...) |
| 1171 | if err := o.enc_len_thing(enc, &state); err != nil { |
| 1172 | return err |
| 1173 | } |
| 1174 | } |
| 1175 | return nil |
| 1176 | } |
| 1177 | |
| 1178 | func size_new_map(p *Properties, base structPointer) int { |
| David Symonds | 0f7a9ca | 2015-07-20 16:00:00 +1000 | [diff] [blame] | 1179 | v := structPointer_NewAt(base, p.field, p.mtype).Elem() // map[K]V |
| David Symonds | 3ea3e05 | 2014-12-22 16:15:28 +1100 | [diff] [blame] | 1180 | |
| 1181 | keycopy, valcopy, keybase, valbase := mapEncodeScratch(p.mtype) |
| 1182 | |
| 1183 | n := 0 |
| 1184 | for _, key := range v.MapKeys() { |
| 1185 | val := v.MapIndex(key) |
| 1186 | keycopy.Set(key) |
| 1187 | valcopy.Set(val) |
| 1188 | |
| David Symonds | a8323e2 | 2015-03-24 10:18:00 +1100 | [diff] [blame] | 1189 | // Tag codes for key and val are the responsibility of the sub-sizer. |
| 1190 | keysize := p.mkeyprop.size(p.mkeyprop, keybase) |
| 1191 | valsize := p.mvalprop.size(p.mvalprop, valbase) |
| 1192 | entry := keysize + valsize |
| 1193 | // Add on tag code and length of map entry itself. |
| 1194 | n += len(p.tagcode) + sizeVarint(uint64(entry)) + entry |
| David Symonds | 3ea3e05 | 2014-12-22 16:15:28 +1100 | [diff] [blame] | 1195 | } |
| 1196 | return n |
| 1197 | } |
| 1198 | |
| 1199 | // mapEncodeScratch returns a new reflect.Value matching the map's value type, |
| 1200 | // and a structPointer suitable for passing to an encoder or sizer. |
| 1201 | func mapEncodeScratch(mapType reflect.Type) (keycopy, valcopy reflect.Value, keybase, valbase structPointer) { |
| 1202 | // Prepare addressable doubly-indirect placeholders for the key and value types. |
| 1203 | // This is needed because the element-type encoders expect **T, but the map iteration produces T. |
| 1204 | |
| 1205 | keycopy = reflect.New(mapType.Key()).Elem() // addressable K |
| 1206 | keyptr := reflect.New(reflect.PtrTo(keycopy.Type())).Elem() // addressable *K |
| 1207 | keyptr.Set(keycopy.Addr()) // |
| 1208 | keybase = toStructPointer(keyptr.Addr()) // **K |
| 1209 | |
| 1210 | // Value types are more varied and require special handling. |
| 1211 | switch mapType.Elem().Kind() { |
| 1212 | case reflect.Slice: |
| 1213 | // []byte |
| 1214 | var dummy []byte |
| 1215 | valcopy = reflect.ValueOf(&dummy).Elem() // addressable []byte |
| 1216 | valbase = toStructPointer(valcopy.Addr()) |
| 1217 | case reflect.Ptr: |
| 1218 | // message; the generated field type is map[K]*Msg (so V is *Msg), |
| 1219 | // so we only need one level of indirection. |
| 1220 | valcopy = reflect.New(mapType.Elem()).Elem() // addressable V |
| 1221 | valbase = toStructPointer(valcopy.Addr()) |
| 1222 | default: |
| 1223 | // everything else |
| 1224 | valcopy = reflect.New(mapType.Elem()).Elem() // addressable V |
| 1225 | valptr := reflect.New(reflect.PtrTo(valcopy.Type())).Elem() // addressable *V |
| 1226 | valptr.Set(valcopy.Addr()) // |
| 1227 | valbase = toStructPointer(valptr.Addr()) // **V |
| 1228 | } |
| 1229 | return |
| 1230 | } |
| 1231 | |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1232 | // Encode a struct. |
| David Symonds | ba7896c | 2014-11-20 15:29:05 +1100 | [diff] [blame] | 1233 | func (o *Buffer) enc_struct(prop *StructProperties, base structPointer) error { |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 1234 | var state errorState |
| David Symonds | d15e81b | 2011-10-03 14:31:12 -0700 | [diff] [blame] | 1235 | // Encode fields in tag order so that decoders may use optimizations |
| 1236 | // that depend on the ordering. |
| David Symonds | 380d2d0 | 2014-11-24 10:50:43 +1100 | [diff] [blame] | 1237 | // https://developers.google.com/protocol-buffers/docs/encoding#order |
| David Symonds | d15e81b | 2011-10-03 14:31:12 -0700 | [diff] [blame] | 1238 | for _, i := range prop.order { |
| 1239 | p := prop.Prop[i] |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1240 | if p.enc != nil { |
| 1241 | err := p.enc(o, p, base) |
| David Symonds | 4a2eeb5 | 2013-09-25 11:54:08 +1000 | [diff] [blame] | 1242 | if err != nil { |
| 1243 | if err == ErrNil { |
| 1244 | if p.Required && state.err == nil { |
| David Symonds | e583a5f | 2013-09-27 10:02:37 +1000 | [diff] [blame] | 1245 | state.err = &RequiredNotSetError{p.Name} |
| David Symonds | 4a2eeb5 | 2013-09-25 11:54:08 +1000 | [diff] [blame] | 1246 | } |
| David Symonds | f62db48 | 2015-02-23 12:37:00 +1100 | [diff] [blame] | 1247 | } else if err == errRepeatedHasNil { |
| 1248 | // Give more context to nil values in repeated fields. |
| 1249 | return errors.New("repeated field " + p.OrigName + " has nil element") |
| David Symonds | 4a2eeb5 | 2013-09-25 11:54:08 +1000 | [diff] [blame] | 1250 | } else if !state.shouldContinue(err, p) { |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1251 | return err |
| 1252 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1253 | } |
| Ross Light | cd85f19 | 2016-05-19 15:50:37 -0400 | [diff] [blame] | 1254 | if len(o.buf) > maxMarshalSize { |
| 1255 | return ErrTooLarge |
| 1256 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1257 | } |
| 1258 | } |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1259 | |
| David Symonds | 59b73b3 | 2015-08-24 13:22:02 +1000 | [diff] [blame] | 1260 | // Do oneof fields. |
| 1261 | if prop.oneofMarshaler != nil { |
| 1262 | m := structPointer_Interface(base, prop.stype).(Message) |
| Bryan Mills | 8d92cf5 | 2016-03-19 05:57:00 +1100 | [diff] [blame] | 1263 | if err := prop.oneofMarshaler(m, o); err == ErrNil { |
| 1264 | return errOneofHasNil |
| 1265 | } else if err != nil { |
| David Symonds | 59b73b3 | 2015-08-24 13:22:02 +1000 | [diff] [blame] | 1266 | return err |
| 1267 | } |
| 1268 | } |
| 1269 | |
| David Symonds | 10c93ba | 2012-08-04 16:38:08 +1000 | [diff] [blame] | 1270 | // Add unrecognized fields at the end. |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 1271 | if prop.unrecField.IsValid() { |
| 1272 | v := *structPointer_Bytes(base, prop.unrecField) |
| Ross Light | cd85f19 | 2016-05-19 15:50:37 -0400 | [diff] [blame] | 1273 | if len(o.buf)+len(v) > maxMarshalSize { |
| 1274 | return ErrTooLarge |
| 1275 | } |
| Russ Cox | d4ce3f1 | 2012-09-12 10:36:26 +1000 | [diff] [blame] | 1276 | if len(v) > 0 { |
| 1277 | o.buf = append(o.buf, v...) |
| 1278 | } |
| David Symonds | 10c93ba | 2012-08-04 16:38:08 +1000 | [diff] [blame] | 1279 | } |
| 1280 | |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 1281 | return state.err |
| 1282 | } |
| 1283 | |
| David Symonds | ba7896c | 2014-11-20 15:29:05 +1100 | [diff] [blame] | 1284 | func size_struct(prop *StructProperties, base structPointer) (n int) { |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 1285 | for _, i := range prop.order { |
| 1286 | p := prop.Prop[i] |
| 1287 | if p.size != nil { |
| 1288 | n += p.size(p, base) |
| 1289 | } |
| 1290 | } |
| 1291 | |
| 1292 | // Add unrecognized fields at the end. |
| 1293 | if prop.unrecField.IsValid() { |
| 1294 | v := *structPointer_Bytes(base, prop.unrecField) |
| 1295 | n += len(v) |
| 1296 | } |
| 1297 | |
| David Symonds | 59b73b3 | 2015-08-24 13:22:02 +1000 | [diff] [blame] | 1298 | // Factor in any oneof fields. |
| Damien Neil | 0879490 | 2015-12-04 07:39:00 +1100 | [diff] [blame] | 1299 | if prop.oneofSizer != nil { |
| 1300 | m := structPointer_Interface(base, prop.stype).(Message) |
| 1301 | n += prop.oneofSizer(m) |
| David Symonds | 59b73b3 | 2015-08-24 13:22:02 +1000 | [diff] [blame] | 1302 | } |
| 1303 | |
| David Symonds | 0bf1ad5 | 2013-10-11 09:07:50 +1100 | [diff] [blame] | 1304 | return |
| 1305 | } |
| 1306 | |
| David Symonds | 1d8ba13 | 2014-01-13 16:01:15 +1100 | [diff] [blame] | 1307 | var zeroes [20]byte // longer than any conceivable sizeVarint |
| 1308 | |
| 1309 | // Encode a struct, preceded by its encoded length (as a varint). |
| David Symonds | ba7896c | 2014-11-20 15:29:05 +1100 | [diff] [blame] | 1310 | func (o *Buffer) enc_len_struct(prop *StructProperties, base structPointer, state *errorState) error { |
| David Symonds | 3ea3e05 | 2014-12-22 16:15:28 +1100 | [diff] [blame] | 1311 | return o.enc_len_thing(func() error { return o.enc_struct(prop, base) }, state) |
| 1312 | } |
| 1313 | |
| 1314 | // Encode something, preceded by its encoded length (as a varint). |
| 1315 | func (o *Buffer) enc_len_thing(enc func() error, state *errorState) error { |
| David Symonds | 1d8ba13 | 2014-01-13 16:01:15 +1100 | [diff] [blame] | 1316 | iLen := len(o.buf) |
| 1317 | o.buf = append(o.buf, 0, 0, 0, 0) // reserve four bytes for length |
| 1318 | iMsg := len(o.buf) |
| David Symonds | 3ea3e05 | 2014-12-22 16:15:28 +1100 | [diff] [blame] | 1319 | err := enc() |
| David Symonds | 1d8ba13 | 2014-01-13 16:01:15 +1100 | [diff] [blame] | 1320 | if err != nil && !state.shouldContinue(err, nil) { |
| 1321 | return err |
| 1322 | } |
| 1323 | lMsg := len(o.buf) - iMsg |
| 1324 | lLen := sizeVarint(uint64(lMsg)) |
| 1325 | switch x := lLen - (iMsg - iLen); { |
| 1326 | case x > 0: // actual length is x bytes larger than the space we reserved |
| 1327 | // Move msg x bytes right. |
| 1328 | o.buf = append(o.buf, zeroes[:x]...) |
| 1329 | copy(o.buf[iMsg+x:], o.buf[iMsg:iMsg+lMsg]) |
| 1330 | case x < 0: // actual length is x bytes smaller than the space we reserved |
| 1331 | // Move msg x bytes left. |
| 1332 | copy(o.buf[iMsg+x:], o.buf[iMsg:iMsg+lMsg]) |
| 1333 | o.buf = o.buf[:len(o.buf)+x] // x is negative |
| 1334 | } |
| 1335 | // Encode the length in the reserved space. |
| 1336 | o.buf = o.buf[:iLen] |
| 1337 | o.EncodeVarint(uint64(lMsg)) |
| 1338 | o.buf = o.buf[:len(o.buf)+lMsg] |
| 1339 | return state.err |
| 1340 | } |
| 1341 | |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 1342 | // errorState maintains the first error that occurs and updates that error |
| 1343 | // with additional context. |
| 1344 | type errorState struct { |
| 1345 | err error |
| 1346 | } |
| 1347 | |
| 1348 | // shouldContinue reports whether encoding should continue upon encountering the |
| David Symonds | e583a5f | 2013-09-27 10:02:37 +1000 | [diff] [blame] | 1349 | // given error. If the error is RequiredNotSetError, shouldContinue returns true |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 1350 | // and, if this is the first appearance of that error, remembers it for future |
| 1351 | // reporting. |
| 1352 | // |
| 1353 | // If prop is not nil, it may update any error with additional context about the |
| 1354 | // field with the error. |
| 1355 | func (s *errorState) shouldContinue(err error, prop *Properties) bool { |
| 1356 | // Ignore unset required fields. |
| David Symonds | e583a5f | 2013-09-27 10:02:37 +1000 | [diff] [blame] | 1357 | reqNotSet, ok := err.(*RequiredNotSetError) |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 1358 | if !ok { |
| 1359 | return false |
| 1360 | } |
| 1361 | if s.err == nil { |
| 1362 | if prop != nil { |
| David Symonds | e583a5f | 2013-09-27 10:02:37 +1000 | [diff] [blame] | 1363 | err = &RequiredNotSetError{prop.Name + "." + reqNotSet.field} |
| David Symonds | 4646c37 | 2013-09-09 13:18:58 +1000 | [diff] [blame] | 1364 | } |
| 1365 | s.err = err |
| 1366 | } |
| 1367 | return true |
| Rob Pike | aaa3a62 | 2010-03-20 22:32:34 -0700 | [diff] [blame] | 1368 | } |