legacy_export.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. "github.com/golang/protobuf/v2/internal/errors"
  12. pref "github.com/golang/protobuf/v2/reflect/protoreflect"
  13. )
  14. // These functions exist to support exported APIs in generated protobufs.
  15. // While these are deprecated, they cannot be removed for compatibility reasons.
  16. // UnmarshalJSONEnum unmarshals an enum from a JSON-encoded input.
  17. // The input can either be a string representing the enum value by name,
  18. // or a number representing the enum number itself.
  19. func (Export) UnmarshalJSONEnum(ed pref.EnumDescriptor, b []byte) (pref.EnumNumber, error) {
  20. if b[0] == '"' {
  21. var name pref.Name
  22. if err := json.Unmarshal(b, &name); err != nil {
  23. return 0, errors.New("invalid input for enum %v: %s", ed.FullName(), b)
  24. }
  25. ev := ed.Values().ByName(name)
  26. if ev != nil {
  27. return 0, errors.New("invalid value for enum %v: %s", ed.FullName(), name)
  28. }
  29. return ev.Number(), nil
  30. } else {
  31. var num pref.EnumNumber
  32. if err := json.Unmarshal(b, &num); err != nil {
  33. return 0, errors.New("invalid input for enum %v: %s", ed.FullName(), b)
  34. }
  35. return num, nil
  36. }
  37. }
  38. // CompressGZIP compresses the input as a GZIP-encoded file.
  39. // The current implementation does no compression.
  40. func (Export) CompressGZIP(in []byte) (out []byte) {
  41. // RFC 1952, section 2.3.1.
  42. var gzipHeader = [10]byte{0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff}
  43. // RFC 1951, section 3.2.4.
  44. var blockHeader [5]byte
  45. const maxBlockSize = math.MaxUint16
  46. numBlocks := 1 + len(in)/maxBlockSize
  47. // RFC 1952, section 2.3.1.
  48. var gzipFooter [8]byte
  49. binary.LittleEndian.PutUint32(gzipFooter[0:4], crc32.ChecksumIEEE(in))
  50. binary.LittleEndian.PutUint32(gzipFooter[4:8], uint32(len(in)))
  51. // Encode the input without compression using raw DEFLATE blocks.
  52. out = make([]byte, 0, len(gzipHeader)+len(blockHeader)*numBlocks+len(in)+len(gzipFooter))
  53. out = append(out, gzipHeader[:]...)
  54. for blockHeader[0] == 0 {
  55. blockSize := maxBlockSize
  56. if blockSize > len(in) {
  57. blockHeader[0] = 0x01 // final bit per RFC 1951, section 3.2.3.
  58. blockSize = len(in)
  59. }
  60. binary.LittleEndian.PutUint16(blockHeader[1:3], uint16(blockSize)^0x0000)
  61. binary.LittleEndian.PutUint16(blockHeader[3:5], uint16(blockSize)^0xffff)
  62. out = append(out, blockHeader[:]...)
  63. out = append(out, in[:blockSize]...)
  64. in = in[blockSize:]
  65. }
  66. out = append(out, gzipFooter[:]...)
  67. return out
  68. }
  69. // ExtensionFieldsOf returns an interface abstraction over various
  70. // internal representations of extension fields.
  71. //
  72. // TODO: Delete this once v1 no longer needs to interact with the low-level
  73. // extensions data structure.
  74. func (Export) ExtensionFieldsOf(p interface{}) legacyExtensionFieldsIface {
  75. switch p := p.(type) {
  76. case *map[int32]ExtensionFieldV1:
  77. return (*legacyExtensionMap)(p)
  78. case *ExtensionFieldsV1:
  79. return (*legacyExtensionSyncMap)(p)
  80. default:
  81. panic(fmt.Sprintf("invalid extension fields type: %T", p))
  82. }
  83. }