insert_string_tpl.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #ifndef INSERT_STRING_H_
  2. #define INSERT_STRING_H_
  3. /* insert_string_tpl.h -- Private insert_string functions shared with more than
  4. * one insert string implementation
  5. *
  6. * Copyright (C) 1995-2024 Jean-loup Gailly and Mark Adler
  7. *
  8. * Copyright (C) 2013 Intel Corporation. All rights reserved.
  9. * Authors:
  10. * Wajdi Feghali <wajdi.k.feghali@intel.com>
  11. * Jim Guilford <james.guilford@intel.com>
  12. * Vinodh Gopal <vinodh.gopal@intel.com>
  13. * Erdinc Ozturk <erdinc.ozturk@intel.com>
  14. * Jim Kukunas <james.t.kukunas@linux.intel.com>
  15. *
  16. * Portions are Copyright (C) 2016 12Sided Technology, LLC.
  17. * Author:
  18. * Phil Vachon <pvachon@12sidedtech.com>
  19. *
  20. * For conditions of distribution and use, see copyright notice in zlib.h
  21. *
  22. */
  23. #ifndef HASH_CALC_OFFSET
  24. # define HASH_CALC_OFFSET 0
  25. #endif
  26. #ifndef HASH_CALC_MASK
  27. # define HASH_CALC_MASK HASH_MASK
  28. #endif
  29. #ifndef HASH_CALC_READ
  30. # if BYTE_ORDER == LITTLE_ENDIAN
  31. # define HASH_CALC_READ \
  32. memcpy(&val, strstart, sizeof(val));
  33. # else
  34. # define HASH_CALC_READ \
  35. val = ((uint32_t)(strstart[0])); \
  36. val |= ((uint32_t)(strstart[1]) << 8); \
  37. val |= ((uint32_t)(strstart[2]) << 16); \
  38. val |= ((uint32_t)(strstart[3]) << 24);
  39. # endif
  40. #endif
  41. /* ===========================================================================
  42. * Update a hash value with the given input byte
  43. * IN assertion: all calls to UPDATE_HASH are made with consecutive
  44. * input characters, so that a running hash key can be computed from the
  45. * previous key instead of complete recalculation each time.
  46. */
  47. Z_INTERNAL uint32_t UPDATE_HASH(uint32_t h, uint32_t val) {
  48. HASH_CALC(h, val);
  49. return h & HASH_CALC_MASK;
  50. }
  51. /* ===========================================================================
  52. * Quick insert string str in the dictionary and set match_head to the previous head
  53. * of the hash chain (the most recent string with same hash key). Return
  54. * the previous length of the hash chain.
  55. */
  56. Z_INTERNAL Pos QUICK_INSERT_STRING(deflate_state *const s, uint32_t str) {
  57. Pos head;
  58. uint8_t *strstart = s->window + str + HASH_CALC_OFFSET;
  59. uint32_t val, hm;
  60. HASH_CALC_VAR_INIT;
  61. HASH_CALC_READ;
  62. HASH_CALC(HASH_CALC_VAR, val);
  63. HASH_CALC_VAR &= HASH_CALC_MASK;
  64. hm = HASH_CALC_VAR;
  65. head = s->head[hm];
  66. if (LIKELY(head != str)) {
  67. s->prev[str & s->w_mask] = head;
  68. s->head[hm] = (Pos)str;
  69. }
  70. return head;
  71. }
  72. /* ===========================================================================
  73. * Insert string str in the dictionary and set match_head to the previous head
  74. * of the hash chain (the most recent string with same hash key). Return
  75. * the previous length of the hash chain.
  76. * IN assertion: all calls to INSERT_STRING are made with consecutive
  77. * input characters and the first STD_MIN_MATCH bytes of str are valid
  78. * (except for the last STD_MIN_MATCH-1 bytes of the input file).
  79. */
  80. Z_INTERNAL void INSERT_STRING(deflate_state *const s, uint32_t str, uint32_t count) {
  81. uint8_t *strstart = s->window + str + HASH_CALC_OFFSET;
  82. uint8_t *strend = strstart + count;
  83. for (Pos idx = (Pos)str; strstart < strend; idx++, strstart++) {
  84. uint32_t val, hm;
  85. HASH_CALC_VAR_INIT;
  86. HASH_CALC_READ;
  87. HASH_CALC(HASH_CALC_VAR, val);
  88. HASH_CALC_VAR &= HASH_CALC_MASK;
  89. hm = HASH_CALC_VAR;
  90. Pos head = s->head[hm];
  91. if (LIKELY(head != idx)) {
  92. s->prev[idx & s->w_mask] = head;
  93. s->head[hm] = idx;
  94. }
  95. }
  96. }
  97. #endif