extensions/spellcheck/hunspell/src/hunzip.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /******* BEGIN LICENSE BLOCK *******
michael@0 2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
michael@0 3 *
michael@0 4 * The contents of this file are subject to the Mozilla Public License Version
michael@0 5 * 1.1 (the "License"); you may not use this file except in compliance with
michael@0 6 * the License. You may obtain a copy of the License at
michael@0 7 * http://www.mozilla.org/MPL/
michael@0 8 *
michael@0 9 * Software distributed under the License is distributed on an "AS IS" basis,
michael@0 10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
michael@0 11 * for the specific language governing rights and limitations under the
michael@0 12 * License.
michael@0 13 *
michael@0 14 * The Initial Developers of the Original Code are Kevin Hendricks (MySpell)
michael@0 15 * and László Németh (Hunspell). Portions created by the Initial Developers
michael@0 16 * are Copyright (C) 2002-2005 the Initial Developers. All Rights Reserved.
michael@0 17 *
michael@0 18 * Contributor(s): László Németh (nemethl@gyorsposta.hu)
michael@0 19 * Caolan McNamara (caolanm@redhat.com)
michael@0 20 *
michael@0 21 * Alternatively, the contents of this file may be used under the terms of
michael@0 22 * either the GNU General Public License Version 2 or later (the "GPL"), or
michael@0 23 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
michael@0 24 * in which case the provisions of the GPL or the LGPL are applicable instead
michael@0 25 * of those above. If you wish to allow use of your version of this file only
michael@0 26 * under the terms of either the GPL or the LGPL, and not to allow others to
michael@0 27 * use your version of this file under the terms of the MPL, indicate your
michael@0 28 * decision by deleting the provisions above and replace them with the notice
michael@0 29 * and other provisions required by the GPL or the LGPL. If you do not delete
michael@0 30 * the provisions above, a recipient may use your version of this file under
michael@0 31 * the terms of any one of the MPL, the GPL or the LGPL.
michael@0 32 *
michael@0 33 ******* END LICENSE BLOCK *******/
michael@0 34
michael@0 35 #include <stdlib.h>
michael@0 36 #include <string.h>
michael@0 37 #include <stdio.h>
michael@0 38
michael@0 39 #include "hunzip.hxx"
michael@0 40
michael@0 41 #define CODELEN 65536
michael@0 42 #define BASEBITREC 5000
michael@0 43
michael@0 44 #define UNCOMPRESSED '\002'
michael@0 45 #define MAGIC "hz0"
michael@0 46 #define MAGIC_ENCRYPT "hz1"
michael@0 47 #define MAGICLEN (sizeof(MAGIC) - 1)
michael@0 48
michael@0 49 int Hunzip::fail(const char * err, const char * par) {
michael@0 50 fprintf(stderr, err, par);
michael@0 51 return -1;
michael@0 52 }
michael@0 53
michael@0 54 Hunzip::Hunzip(const char * file, const char * key) {
michael@0 55 bufsiz = 0;
michael@0 56 lastbit = 0;
michael@0 57 inc = 0;
michael@0 58 outc = 0;
michael@0 59 dec = NULL;
michael@0 60 fin = NULL;
michael@0 61 filename = (char *) malloc(strlen(file) + 1);
michael@0 62 if (filename) strcpy(filename, file);
michael@0 63 if (getcode(key) == -1) bufsiz = -1;
michael@0 64 else bufsiz = getbuf();
michael@0 65 }
michael@0 66
michael@0 67 int Hunzip::getcode(const char * key) {
michael@0 68 unsigned char c[2];
michael@0 69 int i, j, n, p;
michael@0 70 int allocatedbit = BASEBITREC;
michael@0 71 const char * enc = key;
michael@0 72
michael@0 73 if (!filename) return -1;
michael@0 74
michael@0 75 fin = fopen(filename, "rb");
michael@0 76 if (!fin) return -1;
michael@0 77
michael@0 78 // read magic number
michael@0 79 if ((fread(in, 1, 3, fin) < MAGICLEN)
michael@0 80 || !(strncmp(MAGIC, in, MAGICLEN) == 0 ||
michael@0 81 strncmp(MAGIC_ENCRYPT, in, MAGICLEN) == 0)) {
michael@0 82 return fail(MSG_FORMAT, filename);
michael@0 83 }
michael@0 84
michael@0 85 // check encryption
michael@0 86 if (strncmp(MAGIC_ENCRYPT, in, MAGICLEN) == 0) {
michael@0 87 unsigned char cs;
michael@0 88 if (!key) return fail(MSG_KEY, filename);
michael@0 89 if (fread(&c, 1, 1, fin) < 1) return fail(MSG_FORMAT, filename);
michael@0 90 for (cs = 0; *enc; enc++) cs ^= *enc;
michael@0 91 if (cs != c[0]) return fail(MSG_KEY, filename);
michael@0 92 enc = key;
michael@0 93 } else key = NULL;
michael@0 94
michael@0 95 // read record count
michael@0 96 if (fread(&c, 1, 2, fin) < 2) return fail(MSG_FORMAT, filename);
michael@0 97
michael@0 98 if (key) {
michael@0 99 c[0] ^= *enc;
michael@0 100 if (*(++enc) == '\0') enc = key;
michael@0 101 c[1] ^= *enc;
michael@0 102 }
michael@0 103
michael@0 104 n = ((int) c[0] << 8) + c[1];
michael@0 105 dec = (struct bit *) malloc(BASEBITREC * sizeof(struct bit));
michael@0 106 if (!dec) return fail(MSG_MEMORY, filename);
michael@0 107 dec[0].v[0] = 0;
michael@0 108 dec[0].v[1] = 0;
michael@0 109
michael@0 110 // read codes
michael@0 111 for (i = 0; i < n; i++) {
michael@0 112 unsigned char l;
michael@0 113 if (fread(c, 1, 2, fin) < 2) return fail(MSG_FORMAT, filename);
michael@0 114 if (key) {
michael@0 115 if (*(++enc) == '\0') enc = key;
michael@0 116 c[0] ^= *enc;
michael@0 117 if (*(++enc) == '\0') enc = key;
michael@0 118 c[1] ^= *enc;
michael@0 119 }
michael@0 120 if (fread(&l, 1, 1, fin) < 1) return fail(MSG_FORMAT, filename);
michael@0 121 if (key) {
michael@0 122 if (*(++enc) == '\0') enc = key;
michael@0 123 l ^= *enc;
michael@0 124 }
michael@0 125 if (fread(in, 1, l/8+1, fin) < (size_t) l/8+1) return fail(MSG_FORMAT, filename);
michael@0 126 if (key) for (j = 0; j <= l/8; j++) {
michael@0 127 if (*(++enc) == '\0') enc = key;
michael@0 128 in[j] ^= *enc;
michael@0 129 }
michael@0 130 p = 0;
michael@0 131 for (j = 0; j < l; j++) {
michael@0 132 int b = (in[j/8] & (1 << (7 - (j % 8)))) ? 1 : 0;
michael@0 133 int oldp = p;
michael@0 134 p = dec[p].v[b];
michael@0 135 if (p == 0) {
michael@0 136 lastbit++;
michael@0 137 if (lastbit == allocatedbit) {
michael@0 138 allocatedbit += BASEBITREC;
michael@0 139 dec = (struct bit *) realloc(dec, allocatedbit * sizeof(struct bit));
michael@0 140 }
michael@0 141 dec[lastbit].v[0] = 0;
michael@0 142 dec[lastbit].v[1] = 0;
michael@0 143 dec[oldp].v[b] = lastbit;
michael@0 144 p = lastbit;
michael@0 145 }
michael@0 146 }
michael@0 147 dec[p].c[0] = c[0];
michael@0 148 dec[p].c[1] = c[1];
michael@0 149 }
michael@0 150 return 0;
michael@0 151 }
michael@0 152
michael@0 153 Hunzip::~Hunzip()
michael@0 154 {
michael@0 155 if (dec) free(dec);
michael@0 156 if (fin) fclose(fin);
michael@0 157 if (filename) free(filename);
michael@0 158 }
michael@0 159
michael@0 160 int Hunzip::getbuf() {
michael@0 161 int p = 0;
michael@0 162 int o = 0;
michael@0 163 do {
michael@0 164 if (inc == 0) inbits = fread(in, 1, BUFSIZE, fin) * 8;
michael@0 165 for (; inc < inbits; inc++) {
michael@0 166 int b = (in[inc / 8] & (1 << (7 - (inc % 8)))) ? 1 : 0;
michael@0 167 int oldp = p;
michael@0 168 p = dec[p].v[b];
michael@0 169 if (p == 0) {
michael@0 170 if (oldp == lastbit) {
michael@0 171 fclose(fin);
michael@0 172 fin = NULL;
michael@0 173 // add last odd byte
michael@0 174 if (dec[lastbit].c[0]) out[o++] = dec[lastbit].c[1];
michael@0 175 return o;
michael@0 176 }
michael@0 177 out[o++] = dec[oldp].c[0];
michael@0 178 out[o++] = dec[oldp].c[1];
michael@0 179 if (o == BUFSIZE) return o;
michael@0 180 p = dec[p].v[b];
michael@0 181 }
michael@0 182 }
michael@0 183 inc = 0;
michael@0 184 } while (inbits == BUFSIZE * 8);
michael@0 185 return fail(MSG_FORMAT, filename);
michael@0 186 }
michael@0 187
michael@0 188 const char * Hunzip::getline() {
michael@0 189 char linebuf[BUFSIZE];
michael@0 190 int l = 0, eol = 0, left = 0, right = 0;
michael@0 191 if (bufsiz == -1) return NULL;
michael@0 192 while (l < bufsiz && !eol) {
michael@0 193 linebuf[l++] = out[outc];
michael@0 194 switch (out[outc]) {
michael@0 195 case '\t': break;
michael@0 196 case 31: { // escape
michael@0 197 if (++outc == bufsiz) {
michael@0 198 bufsiz = getbuf();
michael@0 199 outc = 0;
michael@0 200 }
michael@0 201 linebuf[l - 1] = out[outc];
michael@0 202 break;
michael@0 203 }
michael@0 204 case ' ': break;
michael@0 205 default: if (((unsigned char) out[outc]) < 47) {
michael@0 206 if (out[outc] > 32) {
michael@0 207 right = out[outc] - 31;
michael@0 208 if (++outc == bufsiz) {
michael@0 209 bufsiz = getbuf();
michael@0 210 outc = 0;
michael@0 211 }
michael@0 212 }
michael@0 213 if (out[outc] == 30) left = 9; else left = out[outc];
michael@0 214 linebuf[l-1] = '\n';
michael@0 215 eol = 1;
michael@0 216 }
michael@0 217 }
michael@0 218 if (++outc == bufsiz) {
michael@0 219 outc = 0;
michael@0 220 bufsiz = fin ? getbuf(): -1;
michael@0 221 }
michael@0 222 }
michael@0 223 if (right) strcpy(linebuf + l - 1, line + strlen(line) - right - 1);
michael@0 224 else linebuf[l] = '\0';
michael@0 225 strcpy(line + left, linebuf);
michael@0 226 return line;
michael@0 227 }

mercurial