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.

68 lines
2.0 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
7 years ago
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
  1. /* $Id: codelength.h,v 1.5 2015/07/09 12:40:18 nanard Exp $ */
  2. /* Project : miniupnp
  3. * Author : Thomas BERNARD
  4. * copyright (c) 2005-2015 Thomas Bernard
  5. * This software is subjet to the conditions detailed in the
  6. * provided LICENCE file. */
  7. #ifndef CODELENGTH_H_INCLUDED
  8. #define CODELENGTH_H_INCLUDED
  9. /* Encode length by using 7bit per Byte :
  10. * Most significant bit of each byte specifies that the
  11. * following byte is part of the code */
  12. /* n : unsigned
  13. * p : unsigned char *
  14. */
  15. #define DECODELENGTH(n, p) \
  16. n = 0; \
  17. do \
  18. { \
  19. n = (n << 7) | (*p & 0x7f); \
  20. } \
  21. while ((*(p++) & 0x80) && (n < (1 << 25)));
  22. /* n : unsigned
  23. * READ : function/macro to read one byte (unsigned char)
  24. */
  25. #define DECODELENGTH_READ(n, READ) \
  26. n = 0; \
  27. do \
  28. { \
  29. unsigned char c; \
  30. READ(c); \
  31. n = (n << 7) | (c & 0x07f); \
  32. if (! (c & 0x80)) \
  33. break; \
  34. } \
  35. while (n < (1 << 25));
  36. /* n : unsigned
  37. * p : unsigned char *
  38. * p_limit : unsigned char *
  39. */
  40. #define DECODELENGTH_CHECKLIMIT(n, p, p_limit) \
  41. n = 0; \
  42. do \
  43. { \
  44. if ((p) >= (p_limit)) \
  45. break; \
  46. n = (n << 7) | (*(p) & 0x7f); \
  47. } \
  48. while ((*((p)++) & 0x80) && (n < (1 << 25)));
  49. /* n : unsigned
  50. * p : unsigned char *
  51. */
  52. #define CODELENGTH(n, p) \
  53. if (n >= 268435456) \
  54. *(p++) = (n >> 28) | 0x80; \
  55. if (n >= 2097152) \
  56. *(p++) = (n >> 21) | 0x80; \
  57. if (n >= 16384) \
  58. *(p++) = (n >> 14) | 0x80; \
  59. if (n >= 128) \
  60. *(p++) = (n >> 7) | 0x80; \
  61. *(p++) = n & 0x7f;
  62. #endif /* CODELENGTH_H_INCLUDED */