xref: /aosp_15_r20/external/brotli/research/read_dist.h (revision f4ee7fba7774faf2a30f13154332c0a06550dbc4)
1*f4ee7fbaSAndroid Build Coastguard Worker /* Copyright 2016 Google Inc. All Rights Reserved.
2*f4ee7fbaSAndroid Build Coastguard Worker    Author: [email protected] (Ivan Nikulin)
3*f4ee7fbaSAndroid Build Coastguard Worker 
4*f4ee7fbaSAndroid Build Coastguard Worker    Distributed under MIT license.
5*f4ee7fbaSAndroid Build Coastguard Worker    See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
6*f4ee7fbaSAndroid Build Coastguard Worker */
7*f4ee7fbaSAndroid Build Coastguard Worker 
8*f4ee7fbaSAndroid Build Coastguard Worker /* API for reading distances from *.dist file.
9*f4ee7fbaSAndroid Build Coastguard Worker    The format of *.dist file is as follows: for each backward reference there is
10*f4ee7fbaSAndroid Build Coastguard Worker    a position-distance pair, also a copy length may be specified. Copy length is
11*f4ee7fbaSAndroid Build Coastguard Worker    prefixed with flag byte 0, position-distance pair is prefixed with flag
12*f4ee7fbaSAndroid Build Coastguard Worker    byte 1. Each number is a 32-bit integer. Copy length always comes before
13*f4ee7fbaSAndroid Build Coastguard Worker    position-distance pair. Standalone copy length is allowed, in this case it is
14*f4ee7fbaSAndroid Build Coastguard Worker    ignored. */
15*f4ee7fbaSAndroid Build Coastguard Worker 
16*f4ee7fbaSAndroid Build Coastguard Worker #ifndef BROTLI_RESEARCH_READ_DIST_H_
17*f4ee7fbaSAndroid Build Coastguard Worker #define BROTLI_RESEARCH_READ_DIST_H_
18*f4ee7fbaSAndroid Build Coastguard Worker 
19*f4ee7fbaSAndroid Build Coastguard Worker #include <cstdio>
20*f4ee7fbaSAndroid Build Coastguard Worker #include <cstdlib>  /* exit, EXIT_FAILURE */
21*f4ee7fbaSAndroid Build Coastguard Worker 
22*f4ee7fbaSAndroid Build Coastguard Worker #if !defined(CHECK)
23*f4ee7fbaSAndroid Build Coastguard Worker #define CHECK(X) if (!(X)) exit(EXIT_FAILURE);
24*f4ee7fbaSAndroid Build Coastguard Worker #endif
25*f4ee7fbaSAndroid Build Coastguard Worker 
26*f4ee7fbaSAndroid Build Coastguard Worker /* Reads backwards reference from .dist file. Sets all missing fields to -1.
27*f4ee7fbaSAndroid Build Coastguard Worker    Returns false when EOF is met or input is corrupt. */
ReadBackwardReference(FILE * fin,int * copy,int * pos,int * dist)28*f4ee7fbaSAndroid Build Coastguard Worker bool ReadBackwardReference(FILE* fin, int* copy, int* pos, int* dist) {
29*f4ee7fbaSAndroid Build Coastguard Worker   int c = getc(fin);
30*f4ee7fbaSAndroid Build Coastguard Worker   if (c == EOF) return false;
31*f4ee7fbaSAndroid Build Coastguard Worker   if (c == 0) {
32*f4ee7fbaSAndroid Build Coastguard Worker     CHECK(fread(copy, sizeof(int), 1, fin) == 1);
33*f4ee7fbaSAndroid Build Coastguard Worker     if ((c = getc(fin)) != 1) {
34*f4ee7fbaSAndroid Build Coastguard Worker       ungetc(c, fin);
35*f4ee7fbaSAndroid Build Coastguard Worker       *pos = *dist = -1;
36*f4ee7fbaSAndroid Build Coastguard Worker     } else {
37*f4ee7fbaSAndroid Build Coastguard Worker       CHECK(fread(pos, sizeof(int), 1, fin) == 1);
38*f4ee7fbaSAndroid Build Coastguard Worker       CHECK(fread(dist, sizeof(int), 1, fin) == 1);
39*f4ee7fbaSAndroid Build Coastguard Worker     }
40*f4ee7fbaSAndroid Build Coastguard Worker   } else if (c != 1) {
41*f4ee7fbaSAndroid Build Coastguard Worker     return false;
42*f4ee7fbaSAndroid Build Coastguard Worker   } else {
43*f4ee7fbaSAndroid Build Coastguard Worker     CHECK(fread(pos, sizeof(int), 1, fin) == 1);
44*f4ee7fbaSAndroid Build Coastguard Worker     CHECK(fread(dist, sizeof(int), 1, fin) == 1);
45*f4ee7fbaSAndroid Build Coastguard Worker     *copy = -1;
46*f4ee7fbaSAndroid Build Coastguard Worker   }
47*f4ee7fbaSAndroid Build Coastguard Worker   return true;
48*f4ee7fbaSAndroid Build Coastguard Worker }
49*f4ee7fbaSAndroid Build Coastguard Worker 
50*f4ee7fbaSAndroid Build Coastguard Worker #endif  /* BROTLI_RESEARCH_READ_DIST_H_ */
51