LCOV - code coverage report
Current view: directory - extensions/spellcheck/hunspell/src - hunzip.cpp (source / functions) Found Hit Coverage
Test: app.info Lines: 129 0 0.0 %
Date: 2012-06-02 Functions: 6 0 0.0 %

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

Generated by: LCOV version 1.7