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