Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
smaz.cpp
Go to the documentation of this file.
1#include <string>
2#include <sstream>
3#include <string.h>
5namespace fc {
6
7typedef const char* const_char_ptr;
8/* Our compression codebook, used for compression */
9static const_char_ptr Smaz_cb[241] = {
10"\002s,\266", "\003had\232\002leW", "\003on \216", "", "\001yS",
11"\002ma\255\002li\227", "\003or \260", "", "\002ll\230\003s t\277",
12"\004fromg\002mel", "", "\003its\332", "\001z\333", "\003ingF", "\001>\336",
13"\001 \000\003 (\002nc\344", "\002nd=\003 on\312",
14"\002ne\213\003hat\276\003re q", "", "\002ngT\003herz\004have\306\003s o\225",
15"", "\003ionk\003s a\254\002ly\352", "\003hisL\003 inN\003 be\252", "",
16"\003 fo\325\003 of \003 ha\311", "", "\002of\005",
17"\003 co\241\002no\267\003 ma\370", "", "", "\003 cl\356\003enta\003 an7",
18"\002ns\300\001\"e", "\003n t\217\002ntP\003s, \205",
19"\002pe\320\003 we\351\002om\223", "\002on\037", "", "\002y G", "\003 wa\271",
20"\003 re\321\002or*", "", "\002=\"\251\002ot\337", "\003forD\002ou[",
21"\003 toR", "\003 th\r", "\003 it\366",
22"\003but\261\002ra\202\003 wi\363\002</\361", "\003 wh\237", "\002 4",
23"\003nd ?", "\002re!", "", "\003ng c", "",
24"\003ly \307\003ass\323\001a\004\002rir", "", "", "", "\002se_", "\003of \"",
25"\003div\364\002ros\003ere\240", "", "\002ta\310\001bZ\002si\324", "",
26"\003and\a\002rs\335", "\002rt\362", "\002teE", "\003ati\316", "\002so\263",
27"\002th\021", "\002tiJ\001c\034\003allp", "\003ate\345", "\002ss\246",
28"\002stM", "", "\002><\346", "\002to\024", "\003arew", "\001d\030",
29"\002tr\303", "", "\001\n1\003 a \222", "\003f tv\002veo", "\002un\340", "",
30"\003e o\242", "\002a \243\002wa\326\001e\002", "\002ur\226\003e a\274",
31"\002us\244\003\n\r\n\247", "\002ut\304\003e c\373", "\002we\221", "", "",
32"\002wh\302", "\001f,", "", "", "", "\003d t\206", "", "", "\003th \343",
33"\001g;", "", "", "\001\r9\003e s\265", "\003e t\234", "", "\003to Y",
34"\003e\r\n\236", "\002d \036\001h\022", "", "\001,Q", "\002 a\031", "\002 b^",
35"\002\r\n\025\002 cI", "\002 d\245", "\002 e\253", "\002 fh\001i\b\002e \v",
36"", "\002 hU\001-\314", "\002 i8", "", "", "\002 l\315", "\002 m{",
37"\002f :\002 n\354", "\002 o\035", "\002 p}\001.n\003\r\n\r\250", "",
38"\002 r\275", "\002 s>", "\002 t\016", "", "\002g \235\005which+\003whi\367",
39"\002 w5", "\001/\305", "\003as \214", "\003at \207", "", "\003who\331", "",
40"\001l\026\002h \212", "", "\002, $", "", "\004withV", "", "", "", "\001m-", "",
41"", "\002ac\357", "\002ad\350", "\003TheH", "", "", "\004this\233\001n\t",
42"", "\002. y", "", "\002alX\003e, \365", "\003tio\215\002be\\",
43"\002an\032\003ver\347", "", "\004that0\003tha\313\001o\006", "\003was2",
44"\002arO", "\002as.", "\002at'\003the\001\004they\200\005there\322\005theird",
45"\002ce\210", "\004were]", "", "\002ch\231\002l \264\001p<", "", "",
46"\003one\256", "", "\003he \023\002dej", "\003ter\270", "\002cou", "",
47"\002by\177\002di\201\002eax", "", "\002ec\327", "\002edB", "\002ee\353", "",
48"", "\001r\f\002n )", "", "", "", "\002el\262", "", "\003in i\002en3", "",
49"\002o `\001s\n", "", "\002er\033", "\003is t\002es6", "", "\002ge\371",
50"\004.com\375", "\002fo\334\003our\330", "\003ch \301\001t\003", "\002hab", "",
51"\003men\374", "", "\002he\020", "", "", "\001u&", "\002hif", "",
52"\003not\204\002ic\203", "\003ed @\002id\355", "", "", "\002ho\273",
53"\002r K\001vm", "", "", "", "\003t t\257\002il\360", "\002im\342",
54"\003en \317\002in\017", "\002io\220", "\002s \027\001wA", "", "\003er |",
55"\003es ~\002is%", "\002it/", "", "\002iv\272", "",
56"\002t #\ahttp://C\001x\372", "\002la\211", "\001<\341", "\003, a\224"
57};
58
59
60/* Reverse compression codebook, used for decompression */
61static const_char_ptr Smaz_rcb[254] = {
62" ", "the", "e", "t", "a", "of", "o", "and", "i", "n", "s", "e ", "r", " th",
63" t", "in", "he", "th", "h", "he ", "to", "\r\n", "l", "s ", "d", " a", "an",
64"er", "c", " o", "d ", "on", " of", "re", "of ", "t ", ", ", "is", "u", "at",
65" ", "n ", "or", "which", "f", "m", "as", "it", "that", "\n", "was", "en",
66" ", " w", "es", " an", " i", "\r", "f ", "g", "p", "nd", " s", "nd ", "ed ",
67"w", "ed", "http://", "for", "te", "ing", "y ", "The", " c", "ti", "r ", "his",
68"st", " in", "ar", "nt", ",", " to", "y", "ng", " h", "with", "le", "al", "to ",
69"b", "ou", "be", "were", " b", "se", "o ", "ent", "ha", "ng ", "their", "\"",
70"hi", "from", " f", "in ", "de", "ion", "me", "v", ".", "ve", "all", "re ",
71"ri", "ro", "is ", "co", "f t", "are", "ea", ". ", "her", " m", "er ", " p",
72"es ", "by", "they", "di", "ra", "ic", "not", "s, ", "d t", "at ", "ce", "la",
73"h ", "ne", "as ", "tio", "on ", "n t", "io", "we", " a ", "om", ", a", "s o",
74"ur", "li", "ll", "ch", "had", "this", "e t", "g ", "e\r\n", " wh", "ere",
75" co", "e o", "a ", "us", " d", "ss", "\n\r\n", "\r\n\r", "=\"", " be", " e",
76"s a", "ma", "one", "t t", "or ", "but", "el", "so", "l ", "e s", "s,", "no",
77"ter", " wa", "iv", "ho", "e a", " r", "hat", "s t", "ns", "ch ", "wh", "tr",
78"ut", "/", "have", "ly ", "ta", " ha", " on", "tha", "-", " l", "ati", "en ",
79"pe", " re", "there", "ass", "si", " fo", "wa", "ec", "our", "who", "its", "z",
80"fo", "rs", ">", "ot", "un", "<", "im", "th ", "nc", "ate", "><", "ver", "ad",
81" we", "ly", "ee", " n", "id", " cl", "ac", "il", "</", "rt", " wi", "div",
82"e, ", " it", "whi", " ma", "ge", "x", "e c", "men", ".com"
83};
84
85uint32_t smaz_compress(const char *in, int inlen, char *out, int outlen) {
86 unsigned int h1,h2,h3=0;
87 int verblen = 0, _outlen = outlen;
88 char verb[256], *_out = out;
89
90 while(inlen) {
91 int j = 7, needed;
92 char *flush = NULL;
93 const char *slot;
94
95 h1 = h2 = in[0]<<3;
96 if (inlen > 1) h2 += in[1];
97 if (inlen > 2) h3 = h2^in[2];
98 if (j > inlen) j = inlen;
99
100 /* Try to lookup substrings into the hash table, starting from the
101 * longer to the shorter substrings */
102 for (; j > 0; j--) {
103 switch(j) {
104 case 1: slot = Smaz_cb[h1%241]; break;
105 case 2: slot = Smaz_cb[h2%241]; break;
106 default: slot = Smaz_cb[h3%241]; break;
107 }
108 while(slot[0]) {
109 if (slot[0] == j && memcmp(slot+1,in,j) == 0) {
110 /* Match found in the hash table,
111 * prepare a verbatim bytes flush if needed */
112 if (verblen) {
113 needed = (verblen == 1) ? 2 : 2+verblen;
114 flush = out;
115 out += needed;
116 outlen -= needed;
117 }
118 /* Emit the byte */
119 if (outlen <= 0) return _outlen+1;
120 out[0] = slot[slot[0]+1];
121 out++;
122 outlen--;
123 inlen -= j;
124 in += j;
125 goto out;
126 } else {
127 slot += slot[0]+2;
128 }
129 }
130 }
131 /* Match not found - add the byte to the verbatim buffer */
132 verb[verblen] = in[0];
133 verblen++;
134 inlen--;
135 in++;
136out:
137 /* Prepare a flush if we reached the flush length limit, and there
138 * is not already a pending flush operation. */
139 if (!flush && (verblen == 256 || (verblen > 0 && inlen == 0))) {
140 needed = (verblen == 1) ? 2 : 2+verblen;
141 flush = out;
142 out += needed;
143 outlen -= needed;
144 if (outlen < 0) return _outlen+1;
145 }
146 /* Perform a verbatim flush if needed */
147 if (flush) {
148 if (verblen == 1) {
149 flush[0] = (signed char)254;
150 flush[1] = verb[0];
151 } else {
152 flush[0] = (signed char)255;
153 flush[1] = (signed char)(verblen-1);
154 memcpy(flush+2,verb,verblen);
155 }
156 flush = NULL;
157 verblen = 0;
158 }
159 }
160 return out-_out;
161}
162
163void smaz_decompress(const char *in, uint32_t inlen, std::stringstream& ss ) {
164 const unsigned char *c = (const unsigned char*) in;
165// char *_out = out;
166// int _outlen = outlen;
167
168 while(inlen) {
169 if (*c == 254) {
170 /* Verbatim byte */
171 //if (outlen < 1) return _outlen+1;
172 //*out = *(c+1);
173 ss.put( *(c+1) );
174 //out++;
175 //outlen--;
176 c += 2;
177 inlen -= 2;
178 } else if (*c == 255) {
179 /* Verbatim string */
180 int len = (*(c+1))+1;
181 //if (outlen < len) return _outlen+1;
182 ss.write( (const char*)(c+2),len );
183
184 //memcpy(out,c+2,len);
185 //out += len;
186 //outlen -= len;
187 c += 2+len;
188 inlen -= 2+len;
189 } else {
190 /* Codebook entry */
191 const char *s = Smaz_rcb[*c];
192 int len = strlen(s);
193
194 //if (outlen < len) return _outlen+1;
195 //memcpy(out,s,len);
196 ss.write( s, len );
197 //out += len;
198 //outlen -= len;
199 c++;
200 inlen--;
201 }
202 }
203}
204
205
206
207 std::string smaz_compress( const std::string& in )
208 {
209 std::string out;
210 out.resize(in.size());
211 auto out_len = smaz_compress( in.c_str(), in.size(), &out[0], out.size() );
212 FC_ASSERT( out_len <= out.size() );
213 out.resize(out_len);
214 return out;
215 }
216 std::string smaz_decompress( const std::string& compressed )
217 {
218 std::stringstream ss;
219 smaz_decompress( compressed.c_str(), compressed.length(), ss );
220 return ss.str();
221 }
222
223} // namespace fc
Defines exception's used by fc.
#define FC_ASSERT(TEST,...)
Checks a condition and throws an assert_exception if the test is FALSE.
namespace sysio::chain
Definition authority.cpp:3
const char * const_char_ptr
Definition smaz.cpp:7
std::string smaz_compress(const std::string &in)
Definition smaz.cpp:207
std::string smaz_decompress(const std::string &compressed)
Definition smaz.cpp:216
unsigned int uint32_t
Definition stdint.h:126
yubihsm_pkcs11_slot * slot
char * s
uint16_t j
size_t out_len
size_t len
memcpy((char *) pInfo->slotDescription, s, l)