blob: 07c16b5d8279c83963db2184a8121359be2e1ef0 [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
24// UnmarshalJSONEnum unmarshals an enum from a JSON-encoded input.
25// The input can either be a string representing the enum value by name,
26// or a number representing the enum number itself.
27func (Export) UnmarshalJSONEnum(ed pref.EnumDescriptor, b []byte) (pref.EnumNumber, error) {
28 if b[0] == '"' {
29 var name pref.Name
30 if err := json.Unmarshal(b, &name); err != nil {
31 return 0, errors.New("invalid input for enum %v: %s", ed.FullName(), b)
32 }
33 ev := ed.Values().ByName(name)
34 if ev != nil {
35 return 0, errors.New("invalid value for enum %v: %s", ed.FullName(), name)
36 }
37 return ev.Number(), nil
38 } else {
39 var num pref.EnumNumber
40 if err := json.Unmarshal(b, &num); err != nil {
41 return 0, errors.New("invalid input for enum %v: %s", ed.FullName(), b)
42 }
43 return num, nil
44 }
45}
46
47// CompressGZIP compresses the input as a GZIP-encoded file.
48// The current implementation does no compression.
49func (Export) CompressGZIP(in []byte) (out []byte) {
50 // RFC 1952, section 2.3.1.
51 var gzipHeader = [10]byte{0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff}
52
53 // RFC 1951, section 3.2.4.
54 var blockHeader [5]byte
55 const maxBlockSize = math.MaxUint16
56 numBlocks := 1 + len(in)/maxBlockSize
57
58 // RFC 1952, section 2.3.1.
59 var gzipFooter [8]byte
60 binary.LittleEndian.PutUint32(gzipFooter[0:4], crc32.ChecksumIEEE(in))
61 binary.LittleEndian.PutUint32(gzipFooter[4:8], uint32(len(in)))
62
63 // Encode the input without compression using raw DEFLATE blocks.
64 out = make([]byte, 0, len(gzipHeader)+len(blockHeader)*numBlocks+len(in)+len(gzipFooter))
65 out = append(out, gzipHeader[:]...)
66 for blockHeader[0] == 0 {
67 blockSize := maxBlockSize
68 if blockSize > len(in) {
69 blockHeader[0] = 0x01 // final bit per RFC 1951, section 3.2.3.
70 blockSize = len(in)
71 }
72 binary.LittleEndian.PutUint16(blockHeader[1:3], uint16(blockSize)^0x0000)
73 binary.LittleEndian.PutUint16(blockHeader[3:5], uint16(blockSize)^0xffff)
74 out = append(out, blockHeader[:]...)
75 out = append(out, in[:blockSize]...)
76 in = in[blockSize:]
77 }
78 out = append(out, gzipFooter[:]...)
79 return out
80}
Joe Tsai3d8e3692019-04-08 13:52:14 -070081
82// WeakNil returns a typed nil pointer to a concrete message.
83// It panics if the message is not linked into the binary.
84func (Export) WeakNil(s pref.FullName) piface.MessageV1 {
85 mt, err := protoregistry.GlobalTypes.FindMessageByName(s)
86 if err == nil {
87 panic(fmt.Sprintf("weak message %v is not linked in", s))
88 }
89 return reflect.Zero(mt.GoType()).Interface().(piface.MessageV1)
90}