Commit b39193c8 by devttys0

Removed miniz dependency

parent d19aae0b
......@@ -3,20 +3,15 @@ LIB_DIR="../$(PYLIBDIR)"
.PHONY: all clean_libs clean distclean
all:
make -C miniz
cp miniz/*.$(SOEXT) $(LIB_DIR)
make -C compress
cp compress/*.$(SOEXT) $(LIB_DIR)
clean_libs:
rm -f $(LIB_DIR)/libtinfl.$(SOEXT)
rm -f $(LIB_DIR)/libcompress42.$(SOEXT)
clean: clean_libs
make -C miniz clean
make -C compress clean
distclean: clean_libs
make -C miniz distclean
make -C compress distclean
......@@ -3,10 +3,7 @@ About
The libraries in this directory have been patched, extended, or otherwise modified from their original versions for use with binwalk.
Specifically:
o `libtinfl` includes several bug patches and wrapper functions not available in the upstream source.
o `libcompress42` contains code taken from the ncompress Unix utility and turned into a library. To the author's knowledge, this functionality is not available elsewhere as a standard library.
Specifically, libcompress42` contains code taken from the ncompress Unix utility and turned into a library. It is similar to the liblzw library (also ripped from ncompress source), but supports decompression of arbitrary data buffers and includes several useful wrapper functions. To the author's knowledge, this functionality is not available elsewhere as a standard library.
Package mantainers should consult their particular distribution's rules on bundled code with regards to the above libraries.
......
......@@ -173,6 +173,7 @@
/* CJH */
#define NOFUNCDEF
#ifndef NOFUNCDEF
extern void *malloc LARGS((int));
extern void free LARGS((void *));
......
LIBNAME=libtinfl.$(SOEXT)
all: $(LIBNAME)
$(LIBNAME): tinfl_wrapper.o
$(CC) $(CFLAGS) $(CPPFLAGS) -shared -Wl,$(SONAME),$(LIBNAME) tinfl_wrapper.o -o $(LIBNAME) $(LDFLAGS)
chmod +x $(LIBNAME)
tinfl_wrapper.o:
$(CC) $(CFLAGS) $(CPPFLAGS) -c tinfl_wrapper.c
install:
mkdir -p $(DESTDIR)$(LIBDIR)
install $(INSTALL_OPTIONS) $(LIBNAME) $(DESTDIR)$(LIBDIR)/$(LIBNAME)
uninstall:
rm -rf $(DESTDIR)$(LIBDIR)/$(LIBNAME)
.PHONY: clean distclean
clean:
rm -f *.o $(LIBNAME)
distclean: clean
deflate/inflate implementation library from http://code.google.com/p/miniz.
Used by the zlib plugin to validate potential zlib candidates.
Minor bug fixes to prevent crashes and infinite loops when dealing with random/malformed data.
#include <stdio.h>
#include <string.h>
#include "tinfl.c"
// Checks to see if the first block of data in in_buf is valid zlib compressed data.
// Returns 1 if valid, 0 if invalid.
int is_valid_zlib_data(char *in_buf, size_t in_buf_size);
#define BLOCK_SIZE (32*1024)
char *inflate_block(char *buf, size_t buf_size)
{
size_t out_size = BLOCK_SIZE;
return (char *) tinfl_decompress_mem_to_heap((const void *) buf, buf_size, (size_t *) &out_size, 0);
}
/* CJH */
int is_deflated_callback(const void *pBuf, int len, void *pUser)
{
int *decomp_size = pUser;
*decomp_size += len;
if(len > 0)
{
return 1;
}
return 0;
}
/*
* Tries to determine if a given buffer contains valid deflated data.
*
* @buf - The buffer of data to check for deflated data.
* @buf_size - The size of @buf.
* @includes_zlib_header - Set to 1 if the buffer should start with a valid zlib header.
*
* Returns the size of the inflated data if @buf inflated to a value larger than 32KB,
* or if it contained a valid zlib header/footer; else, returns 0.
*
* Thus, it is recommended to provide more than 32KB of data in @buf for the most accurate results.
*/
int is_deflated(char *buf, size_t buf_size, int includes_zlib_header)
{
int flags = TINFL_FLAG_HAS_MORE_INPUT;
int retval = 0, decomp_size = 0;
if(includes_zlib_header)
{
flags |= TINFL_FLAG_PARSE_ZLIB_HEADER | TINFL_FLAG_COMPUTE_ADLER32;
}
retval = tinfl_decompress_mem_to_callback(buf, &buf_size, is_deflated_callback, (void *) &decomp_size, flags);
if(retval == 1 || decomp_size > BLOCK_SIZE)
{
return decomp_size;
}
return 0;
}
int inflate_raw_file_callback(const void *pBuf, int len, void *pUser)
{
if(fwrite(pBuf, 1, len, (FILE *) pUser) == len)
{
return 1;
}
return 0;
}
/* Inflates a file containing raw deflated data.
*
* @in_file - Input file containing raw deflated data.
* @out_file - Output file where inflated data will be saved.
*
* Returns void.
*/
void inflate_raw_file(char *in_file, char *out_file)
{
char *compressed_data = NULL;
size_t in_size = 0, nbytes = 0;
FILE *fp_in = NULL, *fp_out = NULL;
fp_in = fopen(in_file, "rb");
if(fp_in)
{
fp_out = fopen(out_file, "wb");
if(fp_out)
{
fseek(fp_in, 0L, SEEK_END);
in_size = ftell(fp_in);
fseek(fp_in, 0L, SEEK_SET);
compressed_data = malloc(in_size);
if(compressed_data)
{
memset(compressed_data, 0, in_size);
nbytes = fread(compressed_data, 1, in_size, fp_in);
if(nbytes > 0)
{
tinfl_decompress_mem_to_callback(compressed_data, &nbytes, inflate_raw_file_callback, (void *) fp_out, 0);
}
free(compressed_data);
}
}
}
if(fp_in) fclose(fp_in);
if(fp_out) fclose(fp_out);
return;
}
#ifdef MAIN
int main(int argc, char *argv[])
{
if(argc != 3)
{
fprintf(stderr, "Usage: %s <input file> <output file>\n", argv[0]);
return EXIT_FAILURE;
}
inflate_raw_file(argv[1], argv[2]);
return EXIT_SUCCESS;
}
#endif
# Performs raw decompression of various compression algorithms (currently, only deflate).
import os
import zlib
import lzma
import struct
import binwalk.core.C
import binwalk.core.compat
import binwalk.core.common
from binwalk.core.module import Option, Kwarg, Module
......@@ -149,39 +149,34 @@ class Deflate(object):
ENABLED = False
BLOCK_SIZE = 33*1024
# To prevent many false positives, only show data that decompressed to a reasonable size and didn't just result in a bunch of NULL bytes
MIN_DECOMP_SIZE = 32*1024
DESCRIPTION = "Raw deflate compression stream"
TINFL_NAME = "tinfl"
TINFL_FUNCTIONS = [
binwalk.core.C.Function(name="is_deflated", type=int),
binwalk.core.C.Function(name="inflate_raw_file", type=None),
]
def __init__(self, module):
self.module = module
# The tinfl library is built and installed with binwalk
self.tinfl = binwalk.core.C.Library(self.TINFL_NAME, self.TINFL_FUNCTIONS)
# Add an extraction rule
if self.module.extractor.enabled:
self.module.extractor.add_rule(regex='^%s' % self.DESCRIPTION.lower(), extension="deflate", cmd=self.extractor)
def extractor(self, file_name):
out_file = os.path.splitext(file_name)[0]
self.tinfl.inflate_raw_file(file_name, out_file)
def decompress(self, data):
valid = True
description = None
decomp_size = self.tinfl.is_deflated(data, len(data), 0)
if decomp_size >= self.MIN_DECOMP_SIZE:
description = self.DESCRIPTION + ', uncompressed size >= %d' % decomp_size
# Prepend data with a standard zlib header
data = "\x78\x9C" + data
return description
# Looking for either a valid decompression, or an error indicating truncated input data
try:
zlib.decompress(binwalk.core.compat.str2bytes(data))
except zlib.error as e:
if not str(e).startswith("Error -5"):
# Bad data.
return None
return self.DESCRIPTION
class RawCompression(Module):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment