legacy_export.go 2.5 KB

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