legacy_export.go 3.0 KB

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