summaryrefslogtreecommitdiff
path: root/Source/cbzip2.h
blob: b667c0792523dc06a96017686f32e4d3bdbb9285 (plain)
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
/*
 * cbzip2.h
 * 
 * This file is a part of NSIS.
 * 
 * Copyright (C) 1999-2018 Nullsoft and Contributors
 * 
 * Licensed under the zlib/libpng license (the "License");
 * you may not use this file except in compliance with the License.
 * 
 * Licence details can be found in the file COPYING.
 * 
 * This software is provided 'as-is', without any express or implied
 * warranty.
 * 
 * Unicode support by Jim Park -- 08/24/2007
 */

#ifndef __CBZIP2_H__
#define __CBZIP2_H__

#include "compressor.h"
#include "bzip2/bzlib.h"

class CBzip2 : public ICompressor {
  public:
    virtual ~CBzip2() {}

    virtual int Init(int level, unsigned int dict_size) {
      last_ret = !BZ_STREAM_END;
      stream = new bz_stream;
      if (!stream) return BZ_MEM_ERROR;
      return BZ2_bzCompressInit(stream, level, 0, 30);
    }

    virtual int End() {
      int ret = BZ2_bzCompressEnd(stream);
      delete stream;
      return ret;
    }

    virtual int Compress(bool finish) {
      // act like zlib when it comes to stream ending
      if (last_ret == BZ_STREAM_END && finish)
        return C_FINISHED;
      last_ret = BZ2_bzCompress(stream, finish?BZ_FINISH:0);
      
      if (last_ret < 0)
        return last_ret;

      return C_OK;
    }

    virtual void SetNextIn(char *in, unsigned int size) {
      stream->next_in = (unsigned char*) in;
      stream->avail_in = size;
    }

    virtual void SetNextOut(char *out, unsigned int size) {
      stream->next_out = (unsigned char*) out;
      stream->avail_out = size;
    }

    virtual char* GetNextOut() {
      return (char*) stream->next_out;
    }

    virtual unsigned int GetAvailIn() {
      return stream->avail_in;
    }

    virtual unsigned int GetAvailOut() {
      return stream->avail_out;
    }

    virtual const TCHAR* GetName() {
      return _T("bzip2");
    }

    virtual const TCHAR* GetErrStr(int err) {
      switch (err)
      {
      case BZ_SEQUENCE_ERROR:
        return _T("sequence error - bad call");
      case BZ_PARAM_ERROR:
        return _T("parameter error - bad call");
      case BZ_MEM_ERROR:
        return _T("not enough memory");
      case BZ_CONFIG_ERROR:
        return _T("config error");
      default:
        return _T("unknown error");
      }
    }

  private:
    bz_stream *stream;
    int last_ret;
};

#endif