impl.go 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright 2018 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 protoapi
  5. import (
  6. "encoding/binary"
  7. "encoding/json"
  8. "fmt"
  9. "hash/crc32"
  10. "math"
  11. "strconv"
  12. )
  13. // TODO: Are these the final signatures that we want?
  14. // EnumName is a helper function to simplify printing protocol buffer enums
  15. // by name. Given an enum map and a value, it returns a useful string.
  16. func EnumName(m map[int32]string, v int32) string {
  17. s, ok := m[v]
  18. if ok {
  19. return s
  20. }
  21. return strconv.Itoa(int(v))
  22. }
  23. // UnmarshalJSONEnum is a helper function to simplify recovering enum int values
  24. // from their JSON-encoded representation. Given a map from the enum's symbolic
  25. // names to its int values, and a byte buffer containing the JSON-encoded
  26. // value, it returns an int32 that can be cast to the enum type by the caller.
  27. //
  28. // The function can deal with both JSON representations, numeric and symbolic.
  29. func UnmarshalJSONEnum(m map[string]int32, data []byte, enumName string) (int32, error) {
  30. if data[0] == '"' {
  31. // New style: enums are strings.
  32. var repr string
  33. if err := json.Unmarshal(data, &repr); err != nil {
  34. return -1, err
  35. }
  36. val, ok := m[repr]
  37. if !ok {
  38. return 0, fmt.Errorf("unrecognized enum %s value %q", enumName, repr)
  39. }
  40. return val, nil
  41. }
  42. // Old style: enums are ints.
  43. var val int32
  44. if err := json.Unmarshal(data, &val); err != nil {
  45. return 0, fmt.Errorf("cannot unmarshal %#q into enum %s", data, enumName)
  46. }
  47. return val, nil
  48. }
  49. // CompressGZIP compresses the input as a GZIP-encoded file.
  50. // The current implementation does no compression.
  51. func CompressGZIP(in []byte) (out []byte) {
  52. // RFC 1952, section 2.3.1.
  53. var gzipHeader = [10]byte{0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff}
  54. // RFC 1951, section 3.2.4.
  55. var blockHeader [5]byte
  56. const maxBlockSize = math.MaxUint16
  57. numBlocks := 1 + len(in)/maxBlockSize
  58. // RFC 1952, section 2.3.1.
  59. var gzipFooter [8]byte
  60. binary.LittleEndian.PutUint32(gzipFooter[0:4], crc32.ChecksumIEEE(in))
  61. binary.LittleEndian.PutUint32(gzipFooter[4:8], uint32(len(in)))
  62. // Encode the input without compression using raw DEFLATE blocks.
  63. out = make([]byte, 0, len(gzipHeader)+len(blockHeader)*numBlocks+len(in)+len(gzipFooter))
  64. out = append(out, gzipHeader[:]...)
  65. for blockHeader[0] == 0 {
  66. blockSize := maxBlockSize
  67. if blockSize > len(in) {
  68. blockHeader[0] = 0x01 // final bit per RFC 1951, section 3.2.3.
  69. blockSize = len(in)
  70. }
  71. binary.LittleEndian.PutUint16(blockHeader[1:3], uint16(blockSize)^0x0000)
  72. binary.LittleEndian.PutUint16(blockHeader[3:5], uint16(blockSize)^0xffff)
  73. out = append(out, blockHeader[:]...)
  74. out = append(out, in[:blockSize]...)
  75. in = in[blockSize:]
  76. }
  77. out = append(out, gzipFooter[:]...)
  78. return out
  79. }
  80. // TODO: Remove this when v2 textpb is available.
  81. var CompactTextString func(Message) string