| 1 |
/* |
| 2 |
* cbzip2.h |
| 3 |
* |
| 4 |
* This file is a part of NSIS. |
| 5 |
* |
| 6 |
* Copyright (C) 1999-2026 Nullsoft and Contributors |
| 7 |
* |
| 8 |
* Licensed under the zlib/libpng license (the "License"); |
| 9 |
* you may not use this file except in compliance with the License. |
| 10 |
* |
| 11 |
* Licence details can be found in the file COPYING. |
| 12 |
* |
| 13 |
* This software is provided 'as-is', without any express or implied |
| 14 |
* warranty. |
| 15 |
* |
| 16 |
* Unicode support by Jim Park -- 08/24/2007 |
| 17 |
*/ |
| 18 |
|
| 19 |
#ifndef __CBZIP2_H__ |
| 20 |
#define __CBZIP2_H__ |
| 21 |
|
| 22 |
#include "compressor.h" |
| 23 |
#include "bzip2/bzlib.h" |
| 24 |
|
| 25 |
class CBzip2 : public ICompressor { |
| 26 |
public: |
| 27 |
virtual ~CBzip2() {} |
| 28 |
|
| 29 |
virtual int Init(int level, unsigned int dict_size) { |
| 30 |
last_ret = !BZ_STREAM_END; |
| 31 |
stream = new bz_stream; |
| 32 |
if (!stream) return BZ_MEM_ERROR; |
| 33 |
return BZ2_bzCompressInit(stream, level, 0, 30); |
| 34 |
} |
| 35 |
|
| 36 |
virtual int End() { |
| 37 |
int ret = BZ2_bzCompressEnd(stream); |
| 38 |
delete stream; |
| 39 |
return ret; |
| 40 |
} |
| 41 |
|
| 42 |
virtual int Compress(bool finish) { |
| 43 |
// act like zlib when it comes to stream ending |
| 44 |
if (last_ret == BZ_STREAM_END && finish) |
| 45 |
return C_FINISHED; |
| 46 |
last_ret = BZ2_bzCompress(stream, finish?BZ_FINISH:0); |
| 47 |
|
| 48 |
if (last_ret < 0) |
| 49 |
return last_ret; |
| 50 |
|
| 51 |
return C_OK; |
| 52 |
} |
| 53 |
|
| 54 |
virtual void SetNextIn(char *in, unsigned int size) { |
| 55 |
stream->next_in = (unsigned char*) in; |
| 56 |
stream->avail_in = size; |
| 57 |
} |
| 58 |
|
| 59 |
virtual void SetNextOut(char *out, unsigned int size) { |
| 60 |
stream->next_out = (unsigned char*) out; |
| 61 |
stream->avail_out = size; |
| 62 |
} |
| 63 |
|
| 64 |
virtual char* GetNextOut() { |
| 65 |
return (char*) stream->next_out; |
| 66 |
} |
| 67 |
|
| 68 |
virtual unsigned int GetAvailIn() { |
| 69 |
return stream->avail_in; |
| 70 |
} |
| 71 |
|
| 72 |
virtual unsigned int GetAvailOut() { |
| 73 |
return stream->avail_out; |
| 74 |
} |
| 75 |
|
| 76 |
virtual const TCHAR* GetName() { |
| 77 |
return _T("bzip2"); |
| 78 |
} |
| 79 |
|
| 80 |
virtual const TCHAR* GetErrStr(int err) { |
| 81 |
switch (err) |
| 82 |
{ |
| 83 |
case BZ_SEQUENCE_ERROR: |
| 84 |
return _T("sequence error - bad call"); |
| 85 |
case BZ_PARAM_ERROR: |
| 86 |
return _T("parameter error - bad call"); |
| 87 |
case BZ_MEM_ERROR: |
| 88 |
return _T("not enough memory"); |
| 89 |
case BZ_CONFIG_ERROR: |
| 90 |
return _T("config error"); |
| 91 |
default: |
| 92 |
return _T("unknown error"); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
private: |
| 97 |
bz_stream *stream; |
| 98 |
int last_ret; |
| 99 |
}; |
| 100 |
|
| 101 |
#endif |