legacy_export.go 3.2 KB

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