You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

legacy_export.go 3.1KB

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