| 1 |
/* |
| 2 |
* Copyright 2015 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 |
|
| 17 |
#ifndef CFFCOMPRESSOR_H_ |
| 18 |
#define CFFCOMPRESSOR_H_ |
| 19 |
|
| 20 |
#include <assert.h> |
| 21 |
#include <forward_list> |
| 22 |
#include <stdint.h> |
| 23 |
#include <string.h> |
| 24 |
#include <thread> |
| 25 |
|
| 26 |
/* If MinGW GCC is compiled with "win32" threads instead of "posix" |
| 27 |
* it lacks the C++11 standard threading classes, so we need to include |
| 28 |
* the "mingw-std-threads" header-only library from: |
| 29 |
* |
| 30 |
* https://github.com/meganz/mingw-std-threads |
| 31 |
*/ |
| 32 |
#if defined(__MINGW32__) && !defined(__WINPTHREADS_VERSION) |
| 33 |
#include "mingw-std-threads/mingw.thread.h" |
| 34 |
#endif |
| 35 |
|
| 36 |
#include <algorithm> |
| 37 |
#include <fstream> |
| 38 |
#include <iostream> |
| 39 |
#include <list> |
| 40 |
#include <map> |
| 41 |
#include <sstream> |
| 42 |
#include <stdexcept> |
| 43 |
#include <string> |
| 44 |
#include <queue> |
| 45 |
#include <utility> |
| 46 |
#include <vector> |
| 47 |
|
| 48 |
class token_t; |
| 49 |
struct charstring_t; |
| 50 |
class substring_t; |
| 51 |
class charstring_pool_t; |
| 52 |
|
| 53 |
typedef uint32_t int_type; |
| 54 |
typedef std::map<std::string, unsigned> tokmap_t; |
| 55 |
typedef std::vector<token_t>::iterator tokiter_t; |
| 56 |
typedef std::vector<token_t>::const_iterator const_tokiter_t; |
| 57 |
|
| 58 |
class token_t { |
| 59 |
public: |
| 60 |
explicit token_t(int_type value_ = 0); |
| 61 |
token_t(const token_t &other); |
| 62 |
inline int_type getValue() const; |
| 63 |
inline unsigned size() const; |
| 64 |
inline unsigned part(unsigned idx) const; |
| 65 |
std::string toString() const; |
| 66 |
bool operator<(const token_t &other) const; |
| 67 |
bool operator!=(const token_t &other) const; |
| 68 |
bool operator==(const token_t &other) const; |
| 69 |
|
| 70 |
private: |
| 71 |
int_type value; |
| 72 |
}; |
| 73 |
|
| 74 |
typedef struct charstring_t { |
| 75 |
tokiter_t begin; |
| 76 |
uint32_t len; |
| 77 |
} charstring_t; |
| 78 |
|
| 79 |
class light_substring_t { |
| 80 |
public: |
| 81 |
light_substring_t(const_tokiter_t _begin, const_tokiter_t _end) |
| 82 |
: begin(_begin), end(_end) {} |
| 83 |
light_substring_t(uint32_t start, uint32_t len, charstring_pool_t* pool); |
| 84 |
light_substring_t& operator=(const light_substring_t &other) { |
| 85 |
begin = other.begin; |
| 86 |
end = other.end; |
| 87 |
return *this; |
| 88 |
}; |
| 89 |
bool operator<(const light_substring_t &other) const; |
| 90 |
|
| 91 |
const_tokiter_t begin; |
| 92 |
const_tokiter_t end; |
| 93 |
}; |
| 94 |
|
| 95 |
typedef struct encoding_item { |
| 96 |
uint32_t pos; |
| 97 |
substring_t* substr; |
| 98 |
} encoding_item; |
| 99 |
|
| 100 |
typedef std::vector<encoding_item> encoding_list; |
| 101 |
|
| 102 |
class substring_t { |
| 103 |
public: |
| 104 |
substring_t(unsigned _len, unsigned _start, unsigned _freq); |
| 105 |
substring_t(const substring_t &other); |
| 106 |
const_tokiter_t begin(const charstring_pool_t &chPool) const; |
| 107 |
const_tokiter_t end(const charstring_pool_t &chPool) const; |
| 108 |
uint16_t cost(const charstring_pool_t &chPool); |
| 109 |
int subrSaving(const charstring_pool_t &chPool); |
| 110 |
uint16_t cost(const charstring_pool_t &chPool) const; |
| 111 |
int subrSaving(const charstring_pool_t &chPool) const; |
| 112 |
std::string toString(const charstring_pool_t &chPool); |
| 113 |
bool operator<(const substring_t &other) const; |
| 114 |
bool operator==(const substring_t &other) const; |
| 115 |
bool operator!=(const substring_t &other) const; |
| 116 |
substring_t& operator=(const substring_t &other); |
| 117 |
inline uint32_t size() const; |
| 118 |
inline uint32_t getStart() const; |
| 119 |
void updatePrice(); |
| 120 |
uint32_t getFreq() const; |
| 121 |
void resetFreq(); |
| 122 |
void incrementFreq(); |
| 123 |
void increaseFreq(unsigned amt); |
| 124 |
void decrementFreq(); |
| 125 |
float getPrice() const; |
| 126 |
void setPrice(float newPrice); |
| 127 |
void setAdjCost(float value); |
| 128 |
void syncPrice(); |
| 129 |
std::vector<unsigned char> getTranslatedValue( |
| 130 |
const charstring_pool_t& chPool) const; |
| 131 |
|
| 132 |
uint16_t pos; |
| 133 |
bool flatten; |
| 134 |
encoding_list encoding; |
| 135 |
|
| 136 |
private: |
| 137 |
uint32_t start; |
| 138 |
uint32_t len; |
| 139 |
uint32_t freq; |
| 140 |
uint16_t _cost; |
| 141 |
float adjCost; |
| 142 |
float price; |
| 143 |
|
| 144 |
int doSubrSaving(int subCost) const; |
| 145 |
uint16_t doCost(const charstring_pool_t &chPool) const; |
| 146 |
}; |
| 147 |
|
| 148 |
typedef std::pair<std::vector<encoding_list>, std::vector<substring_t> > |
| 149 |
subr_pair; |
| 150 |
|
| 151 |
void optimizeSubstrings( |
| 152 |
std::map<light_substring_t, |
| 153 |
substring_t*> &substrMap, |
| 154 |
charstring_pool_t &csPool, |
| 155 |
std::list<substring_t>::iterator begin, |
| 156 |
std::list<substring_t>::iterator end); |
| 157 |
|
| 158 |
void optimizeGlyphstrings( |
| 159 |
std::map<light_substring_t, |
| 160 |
substring_t*> &substrMap, |
| 161 |
charstring_pool_t &csPool, |
| 162 |
unsigned start, |
| 163 |
unsigned stop, |
| 164 |
std::vector<encoding_list>& result); |
| 165 |
|
| 166 |
std::pair<encoding_list, float> optimizeCharstring( |
| 167 |
const_tokiter_t begin, |
| 168 |
uint32_t len, |
| 169 |
std::map<light_substring_t, substring_t*> &substrMap, |
| 170 |
charstring_pool_t& csPool, |
| 171 |
bool isSubstring); |
| 172 |
|
| 173 |
class charstring_pool_t { |
| 174 |
public: |
| 175 |
explicit charstring_pool_t(unsigned nCharstrings); |
| 176 |
charstring_pool_t(unsigned nCharstrings, int numRounds); |
| 177 |
void writeSubrs( |
| 178 |
std::list<substring_t>& substrings, |
| 179 |
std::vector<encoding_list>& glyphEncodings, |
| 180 |
std::ostream& outFile); |
| 181 |
uint32_t* getResponse( |
| 182 |
std::list<substring_t>& substrings, |
| 183 |
std::vector<encoding_list>& glyphEncodings, |
| 184 |
unsigned& outputLength); |
| 185 |
std::vector<unsigned char> formatInt(int num); |
| 186 |
void subroutinize( |
| 187 |
std::list<substring_t>& substrings, |
| 188 |
std::vector<encoding_list>& glyphEncodings); |
| 189 |
std::list<substring_t> getSubstrings(); |
| 190 |
charstring_t getCharstring(unsigned idx); |
| 191 |
void addRawCharstring(unsigned char* data, unsigned len); |
| 192 |
void setFDSelect(uint8_t* rawFD); |
| 193 |
void finalize(); |
| 194 |
const_tokiter_t get(unsigned idx) const; |
| 195 |
std::vector<unsigned char> translateToken(const token_t& tok) const; |
| 196 |
|
| 197 |
void printSuffix(unsigned idx, bool printVal = false); |
| 198 |
bool verify_lcp(std::vector<unsigned>& lcp, std::vector<unsigned>& suffixes); |
| 199 |
|
| 200 |
private: |
| 201 |
tokmap_t quarkMap; |
| 202 |
unsigned nextQuark; |
| 203 |
std::vector<std::string> revQuark; |
| 204 |
std::vector<token_t> pool; |
| 205 |
std::vector<unsigned> offset; |
| 206 |
std::vector<unsigned> rev; |
| 207 |
unsigned count; |
| 208 |
bool finalized; |
| 209 |
int numRounds; |
| 210 |
|
| 211 |
inline uint16_t quarkFor(unsigned char* data, unsigned len); |
| 212 |
void addRawToken(unsigned char* data, unsigned len); |
| 213 |
int_type generateValue(unsigned char* data, unsigned len); |
| 214 |
std::vector<unsigned> generateSuffixes(); |
| 215 |
struct suffixSortFunctor; |
| 216 |
std::vector<unsigned> generateLCP(const std::vector<unsigned>& suffixes); |
| 217 |
std::list<substring_t> generateSubstrings( |
| 218 |
std::vector<unsigned> &suffixes, |
| 219 |
std::vector<unsigned> &lcp); |
| 220 |
encoding_list getUpdatedEncoding(substring_t* subr); |
| 221 |
void writeEncoding( |
| 222 |
const encoding_list& enc, |
| 223 |
const std::map<const substring_t*, uint32_t>& index, |
| 224 |
std::ostream& outFile); |
| 225 |
unsigned packEncoding( |
| 226 |
const encoding_list& enc, |
| 227 |
const std::map<const substring_t*, uint32_t>& index, |
| 228 |
uint32_t* buffer); |
| 229 |
}; |
| 230 |
|
| 231 |
charstring_pool_t CharstringPoolFactory( |
| 232 |
std::istream& instream, |
| 233 |
int numRounds); |
| 234 |
|
| 235 |
charstring_pool_t CharstringPoolFactoryFromString( |
| 236 |
unsigned char* buffer, |
| 237 |
int numRounds); |
| 238 |
|
| 239 |
extern "C" uint32_t* compreff(unsigned char* dataStream, int numRounds, unsigned& outputLength); |
| 240 |
extern "C" void unload(uint32_t* response); |
| 241 |
|
| 242 |
#endif |