varint.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #ifndef HADOOP_CORE_VARINT_H
  19. #define HADOOP_CORE_VARINT_H
  20. #include <stdint.h>
  21. /**
  22. * Compute the size of a varint.
  23. *
  24. * @param val The value to encode.
  25. *
  26. * @return The number of bytes it will take to encode.
  27. */
  28. int varint32_size(int32_t val);
  29. /**
  30. * Encode a 32-bit varint.
  31. *
  32. * @param val The value to encode.
  33. * @param buf The buffer to encode into.
  34. * @param len The length of the buffer.
  35. * @param off (inout) The offset to start writing at. This will be
  36. * updated with the new offset after we finish the
  37. * encoding.
  38. *
  39. * @return 0 on success; -EINVAL if we ran out of space.
  40. */
  41. int varint32_encode(int32_t val, uint8_t *buf, int32_t max, int32_t *off);
  42. /**
  43. * Decode a 32-bit varint.
  44. *
  45. * @param val (out param) The decoded value.
  46. * @param buf The buffer to decode from.
  47. * @param len The length of the buffer.
  48. * @param off (inout) The offset to start reading at. This will be
  49. * updated with the new offset after we finish the
  50. * decoding.
  51. *
  52. * @return 0 on success; -EINVAL if we ran out of space.
  53. */
  54. int varint32_decode(int32_t *val, const uint8_t *buf, int32_t max,
  55. int32_t *off);
  56. /**
  57. * Encode a fixed-len, big-endian int32_t.
  58. *
  59. * @param val The value to encode.
  60. * @param buf The buffer to encode into.
  61. */
  62. void be32_encode(int32_t val, uint8_t *buf);
  63. /**
  64. * Decode a fixed-len, big-endian int32_t.
  65. *
  66. * @param buf The buffer to decode from. The buffer must be at
  67. * least 4 bytes long.
  68. *
  69. * @return The decoded integer.
  70. */
  71. int32_t be32_decode(const uint8_t *buf);
  72. #endif
  73. // vim: ts=4:sw=4:tw=79:et