blob: d40d4ffc48cc6a21cd710ef6b1921e738520ab19 [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 Tsai4fddeba2019-03-20 18:29:32 -070010 "fmt"
Joe Tsai8e506a82019-03-16 00:05:34 -070011 "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.
24func (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.
46func (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 Tsai4fddeba2019-03-20 18:29:32 -070078
79// ExtensionFieldsOf returns an interface abstraction over various
80// internal representations of extension fields.
81//
Joe Tsai50c16712019-04-16 01:18:50 -070082// TODO: Delete this once v1 no longer needs this.
83// Remember to delete the HasInit, Lock, and Unlock methods.
Joe Tsai4fddeba2019-03-20 18:29:32 -070084func (Export) ExtensionFieldsOf(p interface{}) legacyExtensionFieldsIface {
85 switch p := p.(type) {
Joe Tsai4fddeba2019-03-20 18:29:32 -070086 case *ExtensionFieldsV1:
Joe Tsai50c16712019-04-16 01:18:50 -070087 return (*legacyExtensionMap)(p)
Joe Tsai4fddeba2019-03-20 18:29:32 -070088 default:
89 panic(fmt.Sprintf("invalid extension fields type: %T", p))
90 }
91}