libclamav/unrar/unrarfilter.c
9e46bc4d
 /*
  *  Extract RAR archives
  *
  *  Copyright (C) 2005 trog@uncon.org
  *
  *  This code is based on the work of Alexander L. Roshal
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
  *  the Free Software Foundation; either version 2 of the License, or
  *  (at your option) any later version.
  *
  *  This program is distributed in the hope that it will be useful,
  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  *  GNU General Public License for more details.
  *
  *  You should have received a copy of the GNU General Public License
  *  along with this program; if not, write to the Free Software
30738099
  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  *  MA 02110-1301, USA.
9e46bc4d
  */
59afa53d
 #if HAVE_CONFIG_H
 #include "clamav-config.h"
 #endif
9e46bc4d
 
59afa53d
 
 #ifdef	HAVE_UNISTD_H
a3af3f37
 #include <unistd.h>
59afa53d
 #endif
a3af3f37
 
 #include "unrar.h"
 #include "unrarfilter.h"
de2c0474
 #include "others.h"
a3af3f37
 
 void rar_filter_array_init(rar_filter_array_t *filter_a)
 {
 	filter_a->array = NULL;
 	filter_a->num_items = 0;
 }
 
 void rar_filter_array_reset(rar_filter_array_t *filter_a)
 {
 	int i;
 	
 	if (!filter_a) {
 		return;
 	}
 	for (i=0 ; i < filter_a->num_items ; i++) {
 		rar_filter_delete(filter_a->array[i]);
 	}
 	if (filter_a->array) {
 		free(filter_a->array);
 	}
 	filter_a->array = NULL;
 	filter_a->num_items = 0;
 }
 
 int rar_filter_array_add(rar_filter_array_t *filter_a, int num)
 {
 	filter_a->num_items += num;
6f1c5a53
 	filter_a->array = (struct UnpackFilter **) cli_realloc(filter_a->array,
a3af3f37
 			filter_a->num_items * sizeof(struct UnpackFilter **));
 	if (filter_a->array == NULL) {
 		filter_a->num_items=0;
 		return FALSE;
 	}
 	filter_a->array[filter_a->num_items-1] = NULL;
 	return TRUE;
 }
 
b0674c6e
 struct UnpackFilter *rar_filter_new(void)
a3af3f37
 {
 	struct UnpackFilter *filter;
 	
e2b5770b
 	filter = (struct UnpackFilter *) cli_malloc(sizeof(struct UnpackFilter));
a3af3f37
 	if (!filter) {
 		return NULL;
 	}
 	filter->block_start = 0;
   	filter->block_length = 0;
   	filter->exec_count = 0;
   	filter->next_window = 0;
   	
    	rar_cmd_array_init(&filter->prg.cmd);
 	filter->prg.global_data = NULL;
 	filter->prg.static_data = NULL;
 	filter->prg.global_size = filter->prg.static_size = 0;
 	filter->prg.filtered_data = NULL;
 	filter->prg.filtered_data_size = 0;
   	return filter;
 }
 
 void rar_filter_delete(struct UnpackFilter *filter)
 {
 	if (!filter) {
 		return;
 	}
 	if (filter->prg.global_data) {
 		free(filter->prg.global_data);
 	}
 	if (filter->prg.static_data) {
 		free(filter->prg.static_data);
 	}
 	rar_cmd_array_reset(&filter->prg.cmd);
 	free(filter);
 }