Class: Mspack::ChmDecompressor

Inherits:
Object
  • Object
show all
Defined in:
lib/mspack/chm_decompressor.rb,
ext/mspack_native/chm_decompressor.c

Defined Under Namespace

Classes: File, Header

Constant Summary collapse

DEFAULT_BUFFER_SIZE =
4096.freeze

Instance Method Summary collapse

Instance Method Details

#close(header) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'ext/mspack_native/chm_decompressor.c', line 115

VALUE chmd_close(VALUE self, VALUE header) {
  if (!CLASS_OF(header) == ChmDHeader) {
    rb_raise(rb_eTypeError, "Parameter must be a CHM decompression header");
  }

  struct decom_wrapper *wrapper;
  Data_Get_Struct(self, struct decom_wrapper, wrapper);

  struct mschmd_header *headerPtr;
  Data_Get_Struct(header, struct mschmd_header, headerPtr);

  wrapper->decom->close(wrapper->decom, headerPtr);
  return Qnil;
}

#extract(file, dir_or_buffer_size = DEFAULT_BUFFER_SIZE) ⇒ Object

Expects a ChmDecompressor::File and either a string, or a fixnum if a block is given.

If no block is given, it calls Mspack.ensure_path and extracts file, returning the absolute file path.

If a block is given, chunks of data are yielded with a maximum size given by the dir_or_buffer_size param. Buffer_size must be a multiple of DEFAULT_BUFFER_SIZE.



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
# File 'lib/mspack/chm_decompressor.rb', line 16

def extract(file, dir_or_buffer_size = DEFAULT_BUFFER_SIZE)

  if block_given?
    buffer_size = dir_or_buffer_size

    if (buffer_size.fdiv(DEFAULT_BUFFER_SIZE).modulo(1) != 0)
      raise ArgumentError, 
        'Buffer_size must be a multiple of DEFAULT_BUFFER_SIZE'
    end

    a = 0
    b = 0
    did_yield = false
    buffer = "\0" * buffer_size

    extract_to_path(file) do |data|
      did_yield = false
      b = a + data.length - 1
      buffer[a..b] = data

      # full buffer - yield and reset
      if b + 1 == buffer_size 
        yield buffer[0..b]
        did_yield = true
        a = 0

      # last read if it's less than default size
      elsif data.length > 0 && data.length < DEFAULT_BUFFER_SIZE
        yield buffer[0..b]
        did_yield = true
      
      else
        a = b + 1
      end
    end

    # last read happened to be default size
    yield buffer[0..b] if !did_yield && b > 0

  else
    path = Mspack.ensure_path(file.filename, dir_or_buffer_size)
    extract_to_path(file, path)
    return path
  end
end

#extract_to_path(*args) ⇒ Object

Form 1: extract_to_path(file, outputPath)

Form 2: extract_to_path(file) { |data_chunk| do_something(data_chunk) }



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
# File 'ext/mspack_native/chm_decompressor.c', line 137

VALUE chmd_extract_to_path(int argc, VALUE* argv, VALUE self) {
  VALUE file;
  VALUE outputPath;
  rb_scan_args(argc, argv, "11", &file, &outputPath);
  const char *pathStr;

  if (!CLASS_OF(file) == ChmDFile) {
    rb_raise(rb_eTypeError, "First parameter must be a CHM decompression file");
  }

  if (argc == 1) {
    rb_need_block();
    VALUE block = rb_block_proc();
    VALUE block_name = rb_funcall(block, rb_intern("object_id"), 0);
    VALUE block_name_str = rb_funcall(block_name, rb_intern("to_s"), 0);
    pathStr = StringValueCStr(block_name_str);
  }
  else {
    Check_Type(outputPath, T_STRING);
    pathStr = StringValueCStr(outputPath);
  }

  struct decom_wrapper *wrapper;
  Data_Get_Struct(self, struct decom_wrapper, wrapper);

  struct chmd_file_wrapper *headerWrapper;
  Data_Get_Struct(file, struct chmd_file_wrapper, headerWrapper);

  int result = 
    wrapper->decom->extract(wrapper->decom, headerWrapper->file, pathStr);

  return result == MSPACK_ERR_OK ? Qtrue : Qfalse;
}

#fast_find(header, filename) ⇒ Object



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
# File 'ext/mspack_native/chm_decompressor.c', line 183

VALUE chmd_fast_find(VALUE self, VALUE header, VALUE filename) {
  struct decom_wrapper *wrapper;
  Data_Get_Struct(self, struct decom_wrapper, wrapper);

  struct mschmd_header *headerPtr;
  Data_Get_Struct(header, struct mschmd_header, headerPtr);

  const char *filenameStr = StringValueCStr(filename);

  int structSize = sizeof(struct mschmd_file);
  struct mschmd_file *file = malloc(structSize);

  int result = wrapper->decom->fast_find(
    wrapper->decom, headerPtr, filenameStr, file, structSize);

  if (result != MSPACK_ERR_OK || file->length == 0) {
    free(file);
    return Qnil;
  }  

  file->filename = malloc(sizeof(char) * strlen(filenameStr) + 1);
  strcpy(file->filename, filenameStr);

  struct chmd_file_wrapper *fileWrapper = 
    malloc(sizeof(struct chmd_file_wrapper));
  fileWrapper->is_fast_find = 1;
  fileWrapper->file = file;

  return Data_Wrap_Struct(ChmDFile, NULL, chmd_file_free, fileWrapper);
}

#fast_open(path) ⇒ Object



179
180
181
# File 'ext/mspack_native/chm_decompressor.c', line 179

VALUE chmd_fast_open(VALUE self, VALUE path) {
  return _open(self, path, 1);
}

#last_errorObject



171
172
173
174
175
176
177
# File 'ext/mspack_native/chm_decompressor.c', line 171

VALUE chmd_last_error(VALUE self) {
  struct decom_wrapper *wrapper;
  Data_Get_Struct(self, struct decom_wrapper, wrapper);

  int error = wrapper->decom->last_error(wrapper->decom);
  return _error_code_sym(error);
}

#open(path) ⇒ Object



111
112
113
# File 'ext/mspack_native/chm_decompressor.c', line 111

VALUE chmd_open(VALUE self, VALUE path) {
  return _open(self, path, 0);
}