Joe Tsai | 8e506a8 | 2019-03-16 00:05:34 -0700 | [diff] [blame] | 1 | // Copyright 2019 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package impl |
| 6 | |
| 7 | import ( |
| 8 | "encoding/binary" |
| 9 | "encoding/json" |
Joe Tsai | 4fddeba | 2019-03-20 18:29:32 -0700 | [diff] [blame^] | 10 | "fmt" |
Joe Tsai | 8e506a8 | 2019-03-16 00:05:34 -0700 | [diff] [blame] | 11 | "hash/crc32" |
| 12 | "math" |
| 13 | |
| 14 | "github.com/golang/protobuf/v2/internal/errors" |
| 15 | pref "github.com/golang/protobuf/v2/reflect/protoreflect" |
| 16 | ) |
| 17 | |
| 18 | // These functions exist to support exported APIs in generated protobufs. |
| 19 | // While these are deprecated, they cannot be removed for compatibility reasons. |
| 20 | |
| 21 | // UnmarshalJSONEnum unmarshals an enum from a JSON-encoded input. |
| 22 | // The input can either be a string representing the enum value by name, |
| 23 | // or a number representing the enum number itself. |
| 24 | func (Export) UnmarshalJSONEnum(ed pref.EnumDescriptor, b []byte) (pref.EnumNumber, error) { |
| 25 | if b[0] == '"' { |
| 26 | var name pref.Name |
| 27 | if err := json.Unmarshal(b, &name); err != nil { |
| 28 | return 0, errors.New("invalid input for enum %v: %s", ed.FullName(), b) |
| 29 | } |
| 30 | ev := ed.Values().ByName(name) |
| 31 | if ev != nil { |
| 32 | return 0, errors.New("invalid value for enum %v: %s", ed.FullName(), name) |
| 33 | } |
| 34 | return ev.Number(), nil |
| 35 | } else { |
| 36 | var num pref.EnumNumber |
| 37 | if err := json.Unmarshal(b, &num); err != nil { |
| 38 | return 0, errors.New("invalid input for enum %v: %s", ed.FullName(), b) |
| 39 | } |
| 40 | return num, nil |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | // CompressGZIP compresses the input as a GZIP-encoded file. |
| 45 | // The current implementation does no compression. |
| 46 | func (Export) CompressGZIP(in []byte) (out []byte) { |
| 47 | // RFC 1952, section 2.3.1. |
| 48 | var gzipHeader = [10]byte{0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff} |
| 49 | |
| 50 | // RFC 1951, section 3.2.4. |
| 51 | var blockHeader [5]byte |
| 52 | const maxBlockSize = math.MaxUint16 |
| 53 | numBlocks := 1 + len(in)/maxBlockSize |
| 54 | |
| 55 | // RFC 1952, section 2.3.1. |
| 56 | var gzipFooter [8]byte |
| 57 | binary.LittleEndian.PutUint32(gzipFooter[0:4], crc32.ChecksumIEEE(in)) |
| 58 | binary.LittleEndian.PutUint32(gzipFooter[4:8], uint32(len(in))) |
| 59 | |
| 60 | // Encode the input without compression using raw DEFLATE blocks. |
| 61 | out = make([]byte, 0, len(gzipHeader)+len(blockHeader)*numBlocks+len(in)+len(gzipFooter)) |
| 62 | out = append(out, gzipHeader[:]...) |
| 63 | for blockHeader[0] == 0 { |
| 64 | blockSize := maxBlockSize |
| 65 | if blockSize > len(in) { |
| 66 | blockHeader[0] = 0x01 // final bit per RFC 1951, section 3.2.3. |
| 67 | blockSize = len(in) |
| 68 | } |
| 69 | binary.LittleEndian.PutUint16(blockHeader[1:3], uint16(blockSize)^0x0000) |
| 70 | binary.LittleEndian.PutUint16(blockHeader[3:5], uint16(blockSize)^0xffff) |
| 71 | out = append(out, blockHeader[:]...) |
| 72 | out = append(out, in[:blockSize]...) |
| 73 | in = in[blockSize:] |
| 74 | } |
| 75 | out = append(out, gzipFooter[:]...) |
| 76 | return out |
| 77 | } |
Joe Tsai | 4fddeba | 2019-03-20 18:29:32 -0700 | [diff] [blame^] | 78 | |
| 79 | // ExtensionFieldsOf returns an interface abstraction over various |
| 80 | // internal representations of extension fields. |
| 81 | // |
| 82 | // TODO: Delete this once v1 no longer needs to interact with the low-level |
| 83 | // extensions data structure. |
| 84 | func (Export) ExtensionFieldsOf(p interface{}) legacyExtensionFieldsIface { |
| 85 | switch p := p.(type) { |
| 86 | case *map[int32]ExtensionFieldV1: |
| 87 | return (*legacyExtensionMap)(p) |
| 88 | case *ExtensionFieldsV1: |
| 89 | return (*legacyExtensionSyncMap)(p) |
| 90 | default: |
| 91 | panic(fmt.Sprintf("invalid extension fields type: %T", p)) |
| 92 | } |
| 93 | } |