codelength.h 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /* $Id: codelength.h,v 1.3 2011/07/30 13:10:05 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) n = 0; \
  16. do { n = (n << 7) | (*p & 0x7f); } \
  17. while((*(p++)&0x80) && (n<(1<<25)));
  18. /* n : unsigned
  19. * READ : function/macro to read one byte (unsigned char)
  20. */
  21. #define DECODELENGTH_READ(n, READ) \
  22. n = 0; \
  23. do { \
  24. unsigned char c; \
  25. READ(c); \
  26. n = (n << 7) | (c & 0x07f); \
  27. if(!(c&0x80)) break; \
  28. } while(n<(1<<25));
  29. /* n : unsigned
  30. * p : unsigned char *
  31. * p_limit : unsigned char *
  32. */
  33. #define DECODELENGTH_CHECKLIMIT(n, p, p_limit) \
  34. n = 0; \
  35. do { \
  36. if((p) >= (p_limit)) break; \
  37. n = (n << 7) | (*(p) & 0x7f); \
  38. } while((*((p)++)&0x80) && (n<(1<<25)));
  39. /* n : unsigned
  40. * p : unsigned char *
  41. */
  42. #define CODELENGTH(n, p) if(n>=268435456) *(p++) = (n >> 28) | 0x80; \
  43. if(n>=2097152) *(p++) = (n >> 21) | 0x80; \
  44. if(n>=16384) *(p++) = (n >> 14) | 0x80; \
  45. if(n>=128) *(p++) = (n >> 7) | 0x80; \
  46. *(p++) = n & 0x7f;
  47. #endif /* CODELENGTH_H_INCLUDED */