1 /*
2 Copyright 2011 Google Inc. All Rights Reserved.
3 
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7 
8     http://www.apache.org/licenses/LICENSE-2.0
9 
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 
16 Author: lode.vandevenne@gmail.com (Lode Vandevenne)
17 Author: jyrki.alakuijala@gmail.com (Jyrki Alakuijala)
18 */
19 
20 #include "cache.h"
21 
22 #include <assert.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 
26 #ifdef ZOPFLI_LONGEST_MATCH_CACHE
27 
ZopfliInitCache(size_t blocksize,ZopfliLongestMatchCache * lmc)28 void ZopfliInitCache(size_t blocksize, ZopfliLongestMatchCache* lmc) {
29   size_t i;
30   lmc->length = (unsigned short*)malloc(sizeof(unsigned short) * blocksize);
31   lmc->dist = (unsigned short*)malloc(sizeof(unsigned short) * blocksize);
32   /* Rather large amount of memory. */
33   lmc->sublen = (unsigned char*)malloc(ZOPFLI_CACHE_LENGTH * 3 * blocksize);
34 
35   /* length > 0 and dist 0 is invalid combination, which indicates on purpose
36   that this cache value is not filled in yet. */
37   for (i = 0; i < blocksize; i++) lmc->length[i] = 1;
38   for (i = 0; i < blocksize; i++) lmc->dist[i] = 0;
39   for (i = 0; i < ZOPFLI_CACHE_LENGTH * blocksize * 3; i++) lmc->sublen[i] = 0;
40 }
41 
ZopfliCleanCache(ZopfliLongestMatchCache * lmc)42 void ZopfliCleanCache(ZopfliLongestMatchCache* lmc) {
43   free(lmc->length);
44   free(lmc->dist);
45   free(lmc->sublen);
46 }
47 
ZopfliSublenToCache(const unsigned short * sublen,size_t pos,size_t length,ZopfliLongestMatchCache * lmc)48 void ZopfliSublenToCache(const unsigned short* sublen,
49                          size_t pos, size_t length,
50                          ZopfliLongestMatchCache* lmc) {
51   size_t i;
52   size_t j = 0;
53   unsigned bestlength = 0;
54   unsigned char* cache;
55 
56 #if ZOPFLI_CACHE_LENGTH == 0
57   return;
58 #endif
59 
60   cache = &lmc->sublen[ZOPFLI_CACHE_LENGTH * pos * 3];
61   if (length < 3) return;
62   for (i = 3; i <= length; i++) {
63     if (i == length || sublen[i] != sublen[i + 1]) {
64       cache[j * 3] = i - 3;
65       cache[j * 3 + 1] = sublen[i] % 256;
66       cache[j * 3 + 2] = (sublen[i] >> 8) % 256;
67       bestlength = i;
68       j++;
69       if (j >= ZOPFLI_CACHE_LENGTH) break;
70     }
71   }
72   if (j < ZOPFLI_CACHE_LENGTH) {
73     assert(bestlength == length);
74     cache[(ZOPFLI_CACHE_LENGTH - 1) * 3] = bestlength - 3;
75   } else {
76     assert(bestlength <= length);
77   }
78   assert(bestlength == ZopfliMaxCachedSublen(lmc, pos, length));
79 }
80 
ZopfliCacheToSublen(const ZopfliLongestMatchCache * lmc,size_t pos,size_t length,unsigned short * sublen)81 void ZopfliCacheToSublen(const ZopfliLongestMatchCache* lmc,
82                          size_t pos, size_t length,
83                          unsigned short* sublen) {
84   size_t i, j;
85   unsigned maxlength = ZopfliMaxCachedSublen(lmc, pos, length);
86   unsigned prevlength = 0;
87   unsigned char* cache;
88 #if ZOPFLI_CACHE_LENGTH == 0
89   return;
90 #endif
91   if (length < 3) return;
92   cache = &lmc->sublen[ZOPFLI_CACHE_LENGTH * pos * 3];
93   for (j = 0; j < ZOPFLI_CACHE_LENGTH; j++) {
94     unsigned length = cache[j * 3] + 3;
95     unsigned dist = cache[j * 3 + 1] + 256 * cache[j * 3 + 2];
96     for (i = prevlength; i <= length; i++) {
97       sublen[i] = dist;
98     }
99     if (length == maxlength) break;
100     prevlength = length + 1;
101   }
102 }
103 
104 /*
105 Returns the length up to which could be stored in the cache.
106 */
ZopfliMaxCachedSublen(const ZopfliLongestMatchCache * lmc,size_t pos,size_t length)107 unsigned ZopfliMaxCachedSublen(const ZopfliLongestMatchCache* lmc,
108                                size_t pos, size_t length) {
109   unsigned char* cache;
110 #if ZOPFLI_CACHE_LENGTH == 0
111   return 0;
112 #endif
113   cache = &lmc->sublen[ZOPFLI_CACHE_LENGTH * pos * 3];
114   (void)length;
115   if (cache[1] == 0 && cache[2] == 0) return 0;  /* No sublen cached. */
116   return cache[(ZOPFLI_CACHE_LENGTH - 1) * 3] + 3;
117 }
118 
119 #endif  /* ZOPFLI_LONGEST_MATCH_CACHE */
120