THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
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.

31 lines
660B

  1. #include "git-compat-util.h"
  2. #include "varint.h"
  3. uintmax_t decode_varint(const unsigned char **bufp)
  4. {
  5. const unsigned char *buf = *bufp;
  6. unsigned char c = *buf++;
  7. uintmax_t val = c & 127;
  8. while (c & 128) {
  9. val += 1;
  10. if (!val || MSB(val, 7))
  11. return 0; /* overflow */
  12. c = *buf++;
  13. val = (val << 7) + (c & 127);
  14. }
  15. *bufp = buf;
  16. return val;
  17. }
  18. int encode_varint(uintmax_t value, unsigned char *buf)
  19. {
  20. unsigned char varint[16];
  21. unsigned pos = sizeof(varint) - 1;
  22. varint[pos] = value & 127;
  23. while (value >>= 7)
  24. varint[--pos] = 128 | (--value & 127);
  25. if (buf)
  26. memcpy(buf, varint + pos, sizeof(varint) - pos);
  27. return sizeof(varint) - pos;
  28. }