Scott Baker | 105df15 | 2020-04-13 15:55:14 -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" |
| 10 | "fmt" |
| 11 | "hash/crc32" |
| 12 | "math" |
| 13 | "reflect" |
| 14 | |
| 15 | "google.golang.org/protobuf/internal/errors" |
| 16 | pref "google.golang.org/protobuf/reflect/protoreflect" |
| 17 | "google.golang.org/protobuf/reflect/protoregistry" |
| 18 | piface "google.golang.org/protobuf/runtime/protoiface" |
| 19 | ) |
| 20 | |
| 21 | // These functions exist to support exported APIs in generated protobufs. |
| 22 | // While these are deprecated, they cannot be removed for compatibility reasons. |
| 23 | |
| 24 | // LegacyEnumName returns the name of enums used in legacy code. |
| 25 | func (Export) LegacyEnumName(ed pref.EnumDescriptor) string { |
| 26 | return legacyEnumName(ed) |
| 27 | } |
| 28 | |
| 29 | // LegacyMessageTypeOf returns the protoreflect.MessageType for m, |
| 30 | // with name used as the message name if necessary. |
| 31 | func (Export) LegacyMessageTypeOf(m piface.MessageV1, name pref.FullName) pref.MessageType { |
| 32 | if mv := (Export{}).protoMessageV2Of(m); mv != nil { |
| 33 | return mv.ProtoReflect().Type() |
| 34 | } |
| 35 | return legacyLoadMessageInfo(reflect.TypeOf(m), name) |
| 36 | } |
| 37 | |
| 38 | // UnmarshalJSONEnum unmarshals an enum from a JSON-encoded input. |
| 39 | // The input can either be a string representing the enum value by name, |
| 40 | // or a number representing the enum number itself. |
| 41 | func (Export) UnmarshalJSONEnum(ed pref.EnumDescriptor, b []byte) (pref.EnumNumber, error) { |
| 42 | if b[0] == '"' { |
| 43 | var name pref.Name |
| 44 | if err := json.Unmarshal(b, &name); err != nil { |
| 45 | return 0, errors.New("invalid input for enum %v: %s", ed.FullName(), b) |
| 46 | } |
| 47 | ev := ed.Values().ByName(name) |
| 48 | if ev == nil { |
| 49 | return 0, errors.New("invalid value for enum %v: %s", ed.FullName(), name) |
| 50 | } |
| 51 | return ev.Number(), nil |
| 52 | } else { |
| 53 | var num pref.EnumNumber |
| 54 | if err := json.Unmarshal(b, &num); err != nil { |
| 55 | return 0, errors.New("invalid input for enum %v: %s", ed.FullName(), b) |
| 56 | } |
| 57 | return num, nil |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | // CompressGZIP compresses the input as a GZIP-encoded file. |
| 62 | // The current implementation does no compression. |
| 63 | func (Export) CompressGZIP(in []byte) (out []byte) { |
| 64 | // RFC 1952, section 2.3.1. |
| 65 | var gzipHeader = [10]byte{0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff} |
| 66 | |
| 67 | // RFC 1951, section 3.2.4. |
| 68 | var blockHeader [5]byte |
| 69 | const maxBlockSize = math.MaxUint16 |
| 70 | numBlocks := 1 + len(in)/maxBlockSize |
| 71 | |
| 72 | // RFC 1952, section 2.3.1. |
| 73 | var gzipFooter [8]byte |
| 74 | binary.LittleEndian.PutUint32(gzipFooter[0:4], crc32.ChecksumIEEE(in)) |
| 75 | binary.LittleEndian.PutUint32(gzipFooter[4:8], uint32(len(in))) |
| 76 | |
| 77 | // Encode the input without compression using raw DEFLATE blocks. |
| 78 | out = make([]byte, 0, len(gzipHeader)+len(blockHeader)*numBlocks+len(in)+len(gzipFooter)) |
| 79 | out = append(out, gzipHeader[:]...) |
| 80 | for blockHeader[0] == 0 { |
| 81 | blockSize := maxBlockSize |
| 82 | if blockSize > len(in) { |
| 83 | blockHeader[0] = 0x01 // final bit per RFC 1951, section 3.2.3. |
| 84 | blockSize = len(in) |
| 85 | } |
| 86 | binary.LittleEndian.PutUint16(blockHeader[1:3], uint16(blockSize)^0x0000) |
| 87 | binary.LittleEndian.PutUint16(blockHeader[3:5], uint16(blockSize)^0xffff) |
| 88 | out = append(out, blockHeader[:]...) |
| 89 | out = append(out, in[:blockSize]...) |
| 90 | in = in[blockSize:] |
| 91 | } |
| 92 | out = append(out, gzipFooter[:]...) |
| 93 | return out |
| 94 | } |
| 95 | |
| 96 | // WeakNil returns a typed nil pointer to a concrete message. |
| 97 | // It panics if the message is not linked into the binary. |
| 98 | func (Export) WeakNil(s pref.FullName) piface.MessageV1 { |
| 99 | mt, err := protoregistry.GlobalTypes.FindMessageByName(s) |
| 100 | if err != nil { |
| 101 | panic(fmt.Sprintf("weak message %v is not linked in", s)) |
| 102 | } |
| 103 | return mt.Zero().Interface().(piface.MessageV1) |
| 104 | } |