File: indexedzip.cc

package info (click to toggle)
goldendict 1.5.0~rc2%2Bgit20181207%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 16,496 kB
  • sloc: cpp: 58,990; ansic: 11,311; xml: 488; makefile: 77; sh: 42
file content (280 lines) | stat: -rw-r--r-- 6,831 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/* This file is (c) 2008-2012 Konstantin Isakov <ikm@goldendict.org>
 * Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */

#include "indexedzip.hh"
#include "zipfile.hh"
#include <zlib.h>
#include "gddebug.hh"
#include "utf8.hh"
#include "iconv.hh"
#include "wstring_qt.hh"

#include <QDebug>

using namespace BtreeIndexing;
using std::vector;

bool IndexedZip::openZipFile( QString const & name )
{
  zip.setFileName( name );

  zipIsOpen = zip.open( QFile::ReadOnly );

  return zipIsOpen;
}

bool IndexedZip::hasFile( gd::wstring const & name )
{
  if ( !zipIsOpen )
    return false;

  vector< WordArticleLink > links = findArticles( name );

  return !links.empty();
}

bool IndexedZip::loadFile( gd::wstring const & name, vector< char > & data )
{
  if ( !zipIsOpen )
    return false;

  vector< WordArticleLink > links = findArticles( name );

  if ( links.empty() )
    return false;

  return loadFile( links[ 0 ].articleOffset, data );
}

bool IndexedZip::loadFile( uint32_t offset, vector< char > & data )
{
  if ( !zipIsOpen )
    return false;

  // Now seek into the zip file and read its header

  if ( !zip.seek( offset ) )
    return false;

  ZipFile::LocalFileHeader header;

  if ( !ZipFile::readLocalHeader( zip, header ) )
  {
    GD_DPRINTF( "Failed to load header\n" );
    return false;
  }

  // Which algorithm was used?

  switch( header.compressionMethod )
  {
    case ZipFile::Uncompressed:
      GD_DPRINTF( "Uncompressed\n" );
      data.resize( header.uncompressedSize );
      return (size_t) zip.read( &data.front(), data.size() ) == data.size();

    case ZipFile::Deflated:
    {
      GD_DPRINTF( "Deflated\n" );

      // Now do the deflation

      QByteArray compressedData = zip.read( header.compressedSize );

      if ( compressedData.size() != (int)header.compressedSize )
        return false;

      data.resize( header.uncompressedSize );

      z_stream stream;

      memset( &stream, 0, sizeof( stream ) );

      stream.next_in = ( Bytef * ) compressedData.data();
      stream.avail_in = compressedData.size();
      stream.next_out = ( Bytef * ) &data.front();
      stream.avail_out = data.size();

      if ( inflateInit2( &stream, -MAX_WBITS ) != Z_OK )
      {
        data.clear();        
        return false;
      }

      if ( inflate( &stream, Z_FINISH ) != Z_STREAM_END )
      {
        GD_DPRINTF( "Not zstream end!" );

        data.clear();

        inflateEnd( &stream );

        return false;
      }

      inflateEnd( &stream );

      return true;
    }

    default:

      return false;
  }
}

bool IndexedZip::indexFile( BtreeIndexing::IndexedWords &zipFileNames, quint32 * filesCount )
{
    if ( !zipIsOpen )
        return false;
    if ( !ZipFile::positionAtCentralDir( zip ) )
        return false;

    // File seems to be a valid zip file


    QTextCodec * localeCodec = QTextCodec::codecForLocale();

    ZipFile::CentralDirEntry entry;

    bool alreadyCounted;
    if( filesCount )
      *filesCount = 0;

    while( ZipFile::readNextEntry( zip, entry ) )
    {
      if ( entry.compressionMethod == ZipFile::Unsupported )
      {
        qWarning( "Zip warning: compression method unsupported -- skipping file \"%s\"\n",
                  entry.fileName.data() );
        continue;
      }

      // Check if the file name has some non-ascii letters.

      unsigned char const * ptr = ( unsigned char const * )
                                    entry.fileName.constData();

      bool hasNonAscii = false;

      for( ; ; )
      {
        if ( *ptr & 0x80 )
        {
          hasNonAscii = true;
          break;
        }
        else
        if ( !*ptr++ )
          break;
      }

      alreadyCounted = false;

      if ( !hasNonAscii )
      {
        // Add entry as is

        zipFileNames.addSingleWord( Utf8::decode( entry.fileName.data() ),
                                    entry.localHeaderOffset );
        if( filesCount )
          *filesCount += 1;
      }
      else
      {
        // Try assuming different encodings. Those are UTF8, system locale and two
        // Russian ones (Windows and Windows OEM). Unfortunately, zip
        // files do not say which encoding they utilize.

        // Utf8
        try
        {
          wstring decoded = Utf8::decode( entry.fileName.constData() );

          zipFileNames.addSingleWord( decoded,
                                      entry.localHeaderOffset );
          if( filesCount != 0 && !alreadyCounted )
          {
            *filesCount += 1;
            alreadyCounted = true;
          }
        }
        catch( Utf8::exCantDecode & )
        {
          // Failed to decode
        }

        if( !entry.fileNameInUTF8 )
        {
          wstring nameInSystemLocale;

          // System locale
          if( localeCodec )
          {
            QString name = localeCodec->toUnicode( entry.fileName.constData(),
                                                   entry.fileName.size() );
            nameInSystemLocale = gd::toWString( name );
            if( !nameInSystemLocale.empty() )
            {
              zipFileNames.addSingleWord( nameInSystemLocale,
                                          entry.localHeaderOffset );

              if( filesCount != 0 && !alreadyCounted )
              {
                *filesCount += 1;
                alreadyCounted = true;
              }
            }
          }


          // CP866
          try
          {
            wstring decoded = Iconv::toWstring( "CP866", entry.fileName.constData(),
                                                entry.fileName.size() );

            if( nameInSystemLocale.compare( decoded ) != 0 )
            {
              zipFileNames.addSingleWord( decoded,
                                          entry.localHeaderOffset );

              if( filesCount != 0 && !alreadyCounted )
              {
                *filesCount += 1;
                alreadyCounted = true;
              }
            }
          }
          catch( Iconv::Ex & )
          {
              // Failed to decode
          }

          // CP1251
          try
          {
            wstring decoded = Iconv::toWstring( "CP1251", entry.fileName.constData(),
                                                entry.fileName.size() );

            if( nameInSystemLocale.compare( decoded ) != 0 )
            {
              zipFileNames.addSingleWord( decoded,
                                          entry.localHeaderOffset );

              if( filesCount != 0 && !alreadyCounted )
              {
                *filesCount += 1;
                alreadyCounted = true;
              }
            }
          }
          catch( Iconv::Ex & )
          {
            // Failed to decode
          }
        }
      }
    }
    return true;
}