legacy_export.go 3.5 KB

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