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