blob: cf17794a40b2b7714c3733a43914c856da76e7e3 [file] [log] [blame]
Joe Tsai8e506a82019-03-16 00:05:34 -07001// 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
5package impl
6
7import (
8 "encoding/binary"
9 "encoding/json"
Joe Tsai3d8e3692019-04-08 13:52:14 -070010 "fmt"
Joe Tsai8e506a82019-03-16 00:05:34 -070011 "hash/crc32"
12 "math"
Joe Tsai3d8e3692019-04-08 13:52:14 -070013 "reflect"
Joe Tsai8e506a82019-03-16 00:05:34 -070014
Damien Neile89e6242019-05-13 23:55:40 -070015 "google.golang.org/protobuf/internal/errors"
16 pref "google.golang.org/protobuf/reflect/protoreflect"
Joe Tsai3d8e3692019-04-08 13:52:14 -070017 "google.golang.org/protobuf/reflect/protoregistry"
18 piface "google.golang.org/protobuf/runtime/protoiface"
Joe Tsai8e506a82019-03-16 00:05:34 -070019)
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
Joe Tsai2e7817f2019-08-23 12:18:57 -070024// LegacyEnumName returns the name of enums used in legacy code.
25func (Export) LegacyEnumName(ed pref.EnumDescriptor) string {
26 return legacyEnumName(ed)
27}
28
Joe Tsaiea5ada12019-09-04 22:41:40 -070029// LegacyMessageTypeOf returns the protoreflect.MessageType for m,
30// with name used as the message name if necessary.
31func (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
Joe Tsai8e506a82019-03-16 00:05:34 -070038// 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.
41func (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.
63func (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}
Joe Tsai3d8e3692019-04-08 13:52:14 -070095
96// WeakNil returns a typed nil pointer to a concrete message.
97// It panics if the message is not linked into the binary.
98func (Export) WeakNil(s pref.FullName) piface.MessageV1 {
99 mt, err := protoregistry.GlobalTypes.FindMessageByName(s)
Damien Neil6e40b322019-10-01 13:05:16 -0700100 if err != nil {
Joe Tsai3d8e3692019-04-08 13:52:14 -0700101 panic(fmt.Sprintf("weak message %v is not linked in", s))
102 }
Damien Neil6e40b322019-10-01 13:05:16 -0700103 return mt.Zero().Interface().(piface.MessageV1)
Joe Tsai3d8e3692019-04-08 13:52:14 -0700104}