adler.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /* $MirOS: contrib/hosted/fwcf/adler.h,v 1.10 2007/05/07 16:15:56 tg Exp $ */
  2. /*-
  3. * Copyright (c) 2006, 2007
  4. * Thorsten Glaser <tg@mirbsd.de>
  5. * The adler32 algorithm is
  6. * Copyright (C) 1995 Mark Adler
  7. *
  8. * Provided that these terms and disclaimer and all copyright notices
  9. * are retained or reproduced in an accompanying document, permission
  10. * is granted to deal in this work without restriction, including un-
  11. * limited rights to use, publicly perform, distribute, sell, modify,
  12. * merge, give away, or sublicence.
  13. *
  14. * This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to
  15. * the utmost extent permitted by applicable law, neither express nor
  16. * implied; without malicious intent or gross negligence. In no event
  17. * may a licensor, author or contributor be held liable for indirect,
  18. * direct, other damage, loss, or other issues arising in any way out
  19. * of dealing in the work, even if advised of the possibility of such
  20. * damage or existence of a defect, except proven that it results out
  21. * of said person's immediate fault when using the work as intended.
  22. *-
  23. * See also:
  24. * contrib/hosted/fwcf/adler.h
  25. * src/lib/libc/hash/adh32.c
  26. * src/kern/z/adler32s.c
  27. * src/kern/z/adler32_i386.S
  28. */
  29. #ifndef ADLER_H
  30. #define ADLER_H "$MirOS: contrib/hosted/fwcf/adler.h,v 1.10 2007/05/07 16:15:56 tg Exp $"
  31. /*
  32. * ADLER-32 implementation
  33. */
  34. #define ADLER_BASE 65521 /* largest prime smaller than 65536 */
  35. #define ADLER_NMAX 5552 /* largest n: 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
  36. /* declare everything needed by the adler32 routine */
  37. #define ADLER_DECL unsigned s1 = 1, s2 = 0, n
  38. /* calculate the adler32 crc of the data pointed to
  39. by the 'buffer' argument, size expected in 'len'
  40. which is TRASHED; stores the result in s1 and s2 */
  41. #define ADLER_CALC(buffer) do { \
  42. const uint8_t *adler_buf = (const uint8_t *)(buffer); \
  43. while (len) { \
  44. len -= (n = MIN(len, ADLER_NMAX)); \
  45. while (n--) { \
  46. s1 += *adler_buf++; \
  47. s2 += s1; \
  48. } \
  49. s1 %= ADLER_BASE; \
  50. s2 %= ADLER_BASE; \
  51. } \
  52. } while (0)
  53. #endif