~ubuntu-branches/debian/experimental/nzbget/experimental

« back to all changes in this revision

Viewing changes to .pc/fix_fsf_address.patch/Decoder.cpp

  • Committer: Package Import Robot
  • Author(s): Andreas Moog
  • Date: 2013-07-18 14:50:28 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20130718145028-qhxse81w1sj5w424
Tags: 11.0+dfsg-1
* New upstream release (Closes: #701896)
* Repackage original tarball to remove copies of jquery and twitter-
  bootstrap
* debian/watch: Update for new versioning scheme
* debian/patches: Remove all old patches, add one patch:
  - dont-embed-libraries.patch: Don't install embedded jquery and bootstrap 
    libraries
* debian/combat: Upgrade to debhelper combat 9
* debian/control:
  - Fix Vcs-Git field
  - Adjust debhelper version for combat level 9
  - Add jquery and bootstrap to depends for integrated webserver
  - Add python to recommends for post-processing scripts
  - Bump build-depends on libpar2-dev to support the cancel function
* debian/links:
  - Use the system jquery and bootstrap libraries
* debian/rules:
  - Add get-orig-source target to build modified upstream tarball
* Adjust sample nzbget.conf:
  - Only listen to 127.0.0.1 instead of 0.0.0.0
  - Use nzbget.conf as template for webui configuration
* Adjust sample nzbgetd init file:
  - Point to correct location of nzbget binary

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 *  This file is part of nzbget
3
 
 *
4
 
 *  Copyright (C) 2004  Sven Henkel <sidddy@users.sourceforge.net>
5
 
 *  Copyright (C) 2007  Andrei Prygounkov <hugbug@users.sourceforge.net>
6
 
 *
7
 
 *  This program is free software; you can redistribute it and/or modify
8
 
 *  it under the terms of the GNU General Public License as published by
9
 
 *  the Free Software Foundation; either version 2 of the License, or
10
 
 *  (at your option) any later version.
11
 
 *
12
 
 *  This program is distributed in the hope that it will be useful,
13
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 
 *  GNU General Public License for more details.
16
 
 *
17
 
 *  You should have received a copy of the GNU General Public License
18
 
 *  along with this program; if not, write to the Free Software
19
 
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20
 
 *
21
 
 * $Revision: 275 $
22
 
 * $Date: 2009-01-26 23:14:50 +0100 (Mo, 26 Jan 2009) $
23
 
 *
24
 
 */
25
 
 
26
 
 
27
 
#ifdef HAVE_CONFIG_H
28
 
#include <config.h>
29
 
#endif
30
 
 
31
 
#ifdef WIN32
32
 
#include "win32.h"
33
 
#endif
34
 
 
35
 
#include <stdlib.h>
36
 
#include <string.h>
37
 
#include <stdio.h>
38
 
#ifndef WIN32
39
 
#include <unistd.h>
40
 
#endif
41
 
 
42
 
#include "nzbget.h"
43
 
#include "Decoder.h"
44
 
#include "Log.h"
45
 
#include "Util.h"
46
 
 
47
 
const char* Decoder::FormatNames[] = { "Unknown", "yEnc", "UU" };
48
 
unsigned int YDecoder::crc_tab[256];
49
 
 
50
 
Decoder::Decoder()
51
 
{
52
 
        debug("Creating Decoder");
53
 
 
54
 
        m_szSrcFilename         = NULL;
55
 
        m_szDestFilename        = NULL;
56
 
        m_szArticleFilename     = NULL;
57
 
}
58
 
 
59
 
Decoder::~ Decoder()
60
 
{
61
 
        debug("Destroying Decoder");
62
 
 
63
 
        if (m_szArticleFilename)
64
 
        {
65
 
                free(m_szArticleFilename);
66
 
        }
67
 
}
68
 
 
69
 
void Decoder::Clear()
70
 
{
71
 
        if (m_szArticleFilename)
72
 
        {
73
 
                free(m_szArticleFilename);
74
 
        }
75
 
        m_szArticleFilename = NULL;
76
 
}
77
 
 
78
 
Decoder::EFormat Decoder::DetectFormat(const char* buffer, int len)
79
 
{
80
 
        if (!strncmp(buffer, "=ybegin ", 8))
81
 
        {
82
 
                return efYenc;
83
 
        }
84
 
        
85
 
        if ((len == 62 || len == 63) && (buffer[62] == '\n' || buffer[62] == '\r') && *buffer == 'M')
86
 
        {
87
 
                return efUx;
88
 
        }
89
 
 
90
 
        if (!strncmp(buffer, "begin ", 6))
91
 
        {
92
 
                bool bOK = true;
93
 
                buffer += 6; //strlen("begin ")
94
 
                while (*buffer && *buffer != ' ')
95
 
                {
96
 
                        char ch = *buffer++;
97
 
                        if (ch < '0' || ch > '7')
98
 
                        {
99
 
                                bOK = false;
100
 
                                break;
101
 
                        }
102
 
                }
103
 
                if (bOK)
104
 
                {
105
 
                        return efUx;
106
 
                }
107
 
        }
108
 
 
109
 
        return efUnknown;
110
 
}
111
 
 
112
 
/**
113
 
  * YDecoder: fast implementation of yEnc-Decoder
114
 
  */
115
 
 
116
 
void YDecoder::Init()
117
 
{
118
 
        debug("Initializing global decoder");
119
 
        crc32gentab();
120
 
}
121
 
 
122
 
void YDecoder::Final()
123
 
{
124
 
        debug("Finalizing global Decoder");
125
 
}
126
 
 
127
 
YDecoder::YDecoder()
128
 
{
129
 
        Clear();
130
 
}
131
 
 
132
 
void YDecoder::Clear()
133
 
{
134
 
        Decoder::Clear();
135
 
 
136
 
        m_bBody = false;
137
 
        m_bBegin = false;
138
 
        m_bPart = false;
139
 
        m_bEnd = false;
140
 
        m_bCrc = false;
141
 
        m_lExpectedCRC = 0;
142
 
        m_lCalculatedCRC = 0xFFFFFFFF;
143
 
        m_iBegin = 0;
144
 
        m_iEnd = 0xFFFFFFFF;
145
 
        m_iSize = 0;
146
 
        m_iEndSize = 0;
147
 
        m_bAutoSeek = false;
148
 
        m_bNeedSetPos = false;
149
 
        m_bCrcCheck = false;
150
 
}
151
 
 
152
 
/* from crc32.c (http://www.koders.com/c/fid699AFE0A656F0022C9D6B9D1743E697B69CE5815.aspx)
153
 
 *
154
 
 * (c) 1999,2000 Krzysztof Dabrowski
155
 
 * (c) 1999,2000 ElysiuM deeZine
156
 
 * Released under GPL (thanks)
157
 
 *
158
 
 * chksum_crc32gentab() --      to a global crc_tab[256], this one will
159
 
 *                              calculate the crcTable for crc32-checksums.
160
 
 *                              it is generated to the polynom [..]
161
 
 */
162
 
void YDecoder::crc32gentab()
163
 
{
164
 
        unsigned long crc, poly;
165
 
        int i, j;
166
 
 
167
 
        poly = 0xEDB88320L;
168
 
        for (i = 0; i < 256; i++)
169
 
        {
170
 
                crc = i;
171
 
                for (j = 8; j > 0; j--)
172
 
                {
173
 
                        if (crc & 1)
174
 
                        {
175
 
                                crc = (crc >> 1) ^ poly;
176
 
                        }
177
 
                        else
178
 
                        {
179
 
                                crc >>= 1;
180
 
                        }
181
 
                }
182
 
                crc_tab[i] = crc;
183
 
        }
184
 
}
185
 
 
186
 
/* This is modified version of chksum_crc() from
187
 
 * crc32.c (http://www.koders.com/c/fid699AFE0A656F0022C9D6B9D1743E697B69CE5815.aspx)
188
 
 * (c) 1999,2000 Krzysztof Dabrowski
189
 
 * (c) 1999,2000 ElysiuM deeZine
190
 
 *
191
 
 * chksum_crc() -- to a given block, this one calculates the
192
 
 *                              crc32-checksum until the length is
193
 
 *                              reached. the crc32-checksum will be
194
 
 *                              the result.
195
 
 */
196
 
unsigned long YDecoder::crc32m(unsigned long startCrc, unsigned char *block, unsigned int length)
197
 
{
198
 
        register unsigned long crc = startCrc;
199
 
        for (unsigned long i = 0; i < length; i++)
200
 
        {
201
 
                crc = ((crc >> 8) & 0x00FFFFFF) ^ crc_tab[(crc ^ *block++) & 0xFF];
202
 
        }
203
 
        return crc;
204
 
}
205
 
 
206
 
unsigned int YDecoder::DecodeBuffer(char* buffer)
207
 
{
208
 
        if (m_bBody && !m_bEnd)
209
 
        {
210
 
                if (!strncmp(buffer, "=yend ", 6))
211
 
                {
212
 
                        m_bEnd = true;
213
 
                        char* pb = strstr(buffer, m_bPart ? " pcrc32=" : " crc32=");
214
 
                        if (pb)
215
 
                        {
216
 
                                m_bCrc = true;
217
 
                                pb += 7 + (int)m_bPart; //=strlen(" crc32=") or strlen(" pcrc32=")
218
 
                                m_lExpectedCRC = strtoul(pb, NULL, 16);
219
 
                        }
220
 
                        pb = strstr(buffer, " size=");
221
 
                        if (pb) 
222
 
                        {
223
 
                                pb += 6; //=strlen(" size=")
224
 
                                m_iEndSize = (int)atoi(pb);
225
 
                        }
226
 
                        return 0;
227
 
                }
228
 
 
229
 
                char* iptr = buffer;
230
 
                char* optr = buffer;
231
 
                while (true)
232
 
                {
233
 
                        switch (*iptr)
234
 
                        {
235
 
                                case '=':       //escape-sequence
236
 
                                        iptr++;
237
 
                                        *optr = *iptr - 64 - 42;
238
 
                                        optr++;
239
 
                                        break;
240
 
                                case '\n':      // ignored char
241
 
                                case '\r':      // ignored char
242
 
                                        break;
243
 
                                case '\0':
244
 
                                        goto BreakLoop;
245
 
                                default:        // normal char
246
 
                                        *optr = *iptr - 42;
247
 
                                        optr++;
248
 
                                        break;
249
 
                        }
250
 
                        iptr++;
251
 
                }
252
 
BreakLoop:
253
 
 
254
 
                if (m_bCrcCheck)
255
 
                {
256
 
                        m_lCalculatedCRC = crc32m(m_lCalculatedCRC, (unsigned char *)buffer, (unsigned int)(optr - buffer));
257
 
                }
258
 
                return (unsigned int)(optr - buffer);
259
 
        }
260
 
        else 
261
 
        {
262
 
                if (!m_bPart && !strncmp(buffer, "=ybegin ", 8))
263
 
                {
264
 
                        m_bBegin = true;
265
 
                        char* pb = strstr(buffer, " name=");
266
 
                        if (pb)
267
 
                        {
268
 
                                pb += 6; //=strlen(" name=")
269
 
                                char* pe;
270
 
                                for (pe = pb; *pe != '\0' && *pe != '\n' && *pe != '\r'; pe++) ;
271
 
                                if (m_szArticleFilename)
272
 
                                {
273
 
                                        free(m_szArticleFilename);
274
 
                                }
275
 
                                m_szArticleFilename = (char*)malloc(pe - pb + 1);
276
 
                                strncpy(m_szArticleFilename, pb, pe - pb);
277
 
                                m_szArticleFilename[pe - pb] = '\0';
278
 
                        }
279
 
                        pb = strstr(buffer, " size=");
280
 
                        if (pb) 
281
 
                        {
282
 
                                pb += 6; //=strlen(" size=")
283
 
                                m_iSize = (int)atoi(pb);
284
 
                        }
285
 
                        m_bPart = strstr(buffer, " part=");
286
 
                        if (!m_bPart)
287
 
                        {
288
 
                                m_bBody = true;
289
 
                                m_iBegin = 1;
290
 
                                m_iEnd = m_iSize;
291
 
                        }
292
 
                }
293
 
                else if (m_bPart && !strncmp(buffer, "=ypart ", 7))
294
 
                {
295
 
                        m_bPart = true;
296
 
                        m_bBody = true;
297
 
                        char* pb = strstr(buffer, " begin=");
298
 
                        if (pb) 
299
 
                        {
300
 
                                pb += 7; //=strlen(" begin=")
301
 
                                m_iBegin = (int)atoi(pb);
302
 
                        }
303
 
                        pb = strstr(buffer, " end=");
304
 
                        if (pb) 
305
 
                        {
306
 
                                pb += 5; //=strlen(" end=")
307
 
                                m_iEnd = (int)atoi(pb);
308
 
                        }
309
 
                }
310
 
        }
311
 
 
312
 
        return 0;
313
 
}
314
 
 
315
 
bool YDecoder::Write(char* buffer, int len, FILE* outfile)
316
 
{
317
 
        unsigned int wcnt = DecodeBuffer(buffer);
318
 
        if (wcnt > 0)
319
 
        {
320
 
                if (m_bNeedSetPos)
321
 
                {
322
 
                        if (m_iBegin == 0 || m_iEnd == 0xFFFFFFFF || !outfile)
323
 
                        {
324
 
                                return false;
325
 
                        }
326
 
                        if (fseek(outfile, m_iBegin - 1, SEEK_SET))
327
 
                        {
328
 
                                return false;
329
 
                        }
330
 
                        m_bNeedSetPos = false;
331
 
                }
332
 
                fwrite(buffer, 1, wcnt, outfile);
333
 
        }
334
 
        return true;
335
 
}
336
 
 
337
 
Decoder::EStatus YDecoder::Check()
338
 
{
339
 
        m_lCalculatedCRC ^= 0xFFFFFFFF;
340
 
 
341
 
        debug("Expected crc32=%x", m_lExpectedCRC);
342
 
        debug("Calculated crc32=%x", m_lCalculatedCRC);
343
 
 
344
 
        if (!m_bBegin)
345
 
        {
346
 
                return eNoBinaryData;
347
 
        }
348
 
        else if (!m_bEnd)
349
 
        {
350
 
                return eArticleIncomplete;
351
 
        }
352
 
        else if (!m_bPart && m_iSize != m_iEndSize)
353
 
        {
354
 
                return eInvalidSize;
355
 
        }
356
 
        else if (m_bCrcCheck && m_bCrc && (m_lExpectedCRC != m_lCalculatedCRC))
357
 
        {
358
 
                return eCrcError;
359
 
        }
360
 
 
361
 
        return eFinished;
362
 
}
363
 
 
364
 
 
365
 
/**
366
 
  * UDecoder: supports UU encoding formats
367
 
  */
368
 
 
369
 
UDecoder::UDecoder()
370
 
{
371
 
 
372
 
}
373
 
 
374
 
void UDecoder::Clear()
375
 
{
376
 
        Decoder::Clear();
377
 
 
378
 
        m_bBody = false;
379
 
        m_bEnd = false;
380
 
}
381
 
 
382
 
/* DecodeBuffer-function uses portions of code from tool UUDECODE by Clem Dye
383
 
 * UUDECODE.c (http://www.bastet.com/uue.zip)
384
 
 * Copyright (C) 1998 Clem Dye
385
 
 *
386
 
 * Released under GPL (thanks)
387
 
 */
388
 
 
389
 
#define UU_DECODE_CHAR(c) (c == '`' ? 0 : (((c) - ' ') & 077))
390
 
 
391
 
unsigned int UDecoder::DecodeBuffer(char* buffer, int len)
392
 
{
393
 
        if (!m_bBody)
394
 
        {
395
 
                if (!strncmp(buffer, "begin ", 6))
396
 
                {
397
 
                        char* pb = buffer;
398
 
                        pb += 6; //strlen("begin ")
399
 
 
400
 
                        // skip file-permissions
401
 
                        for (; *pb != ' ' && *pb != '\0' && *pb != '\n' && *pb != '\r'; pb++) ; 
402
 
                        pb++;
403
 
 
404
 
                        // extracting filename
405
 
                        char* pe;
406
 
                        for (pe = pb; *pe != '\0' && *pe != '\n' && *pe != '\r'; pe++) ;
407
 
                        if (m_szArticleFilename)
408
 
                        {
409
 
                                free(m_szArticleFilename);
410
 
                        }
411
 
                        m_szArticleFilename = (char*)malloc(pe - pb + 1);
412
 
                        strncpy(m_szArticleFilename, pb, pe - pb);
413
 
                        m_szArticleFilename[pe - pb] = '\0';
414
 
 
415
 
                        m_bBody = true;
416
 
                        return 0;
417
 
                }
418
 
                else if ((len == 62 || len == 63) && (buffer[62] == '\n' || buffer[62] == '\r') && *buffer == 'M')
419
 
                {
420
 
                        m_bBody = true;
421
 
                }
422
 
        }
423
 
 
424
 
        if (m_bBody && (!strncmp(buffer, "end ", 4) || *buffer == '`'))
425
 
        {
426
 
                m_bEnd = true;
427
 
        }
428
 
 
429
 
        if (m_bBody && !m_bEnd)
430
 
        {
431
 
                int iEffLen = UU_DECODE_CHAR(buffer[0]);
432
 
                if (iEffLen > len)
433
 
                {
434
 
                        // error;
435
 
                        return 0;
436
 
                }
437
 
 
438
 
                char* iptr = buffer;
439
 
                char* optr = buffer;
440
 
                for (++iptr; iEffLen > 0; iptr += 4, iEffLen -= 3)
441
 
                {
442
 
                        if (iEffLen >= 3)
443
 
                        {
444
 
                                *optr++ = UU_DECODE_CHAR (iptr[0]) << 2 | UU_DECODE_CHAR (iptr[1]) >> 4; 
445
 
                                *optr++ = UU_DECODE_CHAR (iptr[1]) << 4 | UU_DECODE_CHAR (iptr[2]) >> 2; 
446
 
                                *optr++ = UU_DECODE_CHAR (iptr[2]) << 6 | UU_DECODE_CHAR (iptr[3]);
447
 
                        }
448
 
                        else
449
 
                        {
450
 
                                if (iEffLen >= 1)
451
 
                                {
452
 
                                        *optr++ = UU_DECODE_CHAR (iptr[0]) << 2 | UU_DECODE_CHAR (iptr[1]) >> 4; 
453
 
                                }
454
 
                                if (iEffLen >= 2)
455
 
                                {
456
 
                                        *optr++ = UU_DECODE_CHAR (iptr[1]) << 4 | UU_DECODE_CHAR (iptr[2]) >> 2; 
457
 
                                }
458
 
                        }
459
 
                }
460
 
 
461
 
                return (unsigned int)(optr - buffer);
462
 
        }
463
 
 
464
 
        return 0;
465
 
}
466
 
 
467
 
bool UDecoder::Write(char* buffer, int len, FILE* outfile)
468
 
{
469
 
        unsigned int wcnt = DecodeBuffer(buffer, len);
470
 
        if (wcnt > 0)
471
 
        {
472
 
                fwrite(buffer, 1, wcnt, outfile);
473
 
        }
474
 
        return true;
475
 
}
476
 
 
477
 
Decoder::EStatus UDecoder::Check()
478
 
{
479
 
        if (!m_bBody)
480
 
        {
481
 
                return eNoBinaryData;
482
 
        }
483
 
 
484
 
        return eFinished;
485
 
}