summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthew Endsley <mendsley@gmail.com>2012-05-13 15:19:25 -0700
committerMatthew Endsley <mendsley@gmail.com>2012-05-14 01:04:24 -0700
commita85c01f62ed1c27f1cb9308f4549609888c68438 (patch)
tree810ed079d8719f0b0dd0b3e12aaefffb57d8e0d9
parent6e0e2caf4b253e74776c68f6b3f0e9ad3ea343a8 (diff)
downloadbsdiff-a85c01f62ed1c27f1cb9308f4549609888c68438.tar.gz
bsdiff_stream now provides malloc/free functions
-rw-r--r--bsdiff.c29
1 files changed, 17 insertions, 12 deletions
diff --git a/bsdiff.c b/bsdiff.c
index fe4d0ae..4fc3b9c 100644
--- a/bsdiff.c
+++ b/bsdiff.c
@@ -29,6 +29,7 @@
__FBSDID("$FreeBSD: src/usr.bin/bsdiff/bsdiff/bsdiff.c,v 1.1 2005/08/06 01:59:05 cperciva Exp $");
#endif
+#include <stddef.h>
#include <stdint.h>
struct bsdiff_header
@@ -43,6 +44,8 @@ struct bsdiff_stream
{
void* opaque;
+ void* (*malloc)(size_t size);
+ void (*free)(void* ptr);
int (*write)(struct bsdiff_stream* compresor, const void* buffer, int size);
int (*finish)(struct bsdiff_stream* stream);
};
@@ -52,7 +55,6 @@ int bsdiff(const uint8_t* old, int64_t oldsize, const uint8_t* new, int64_t news
#if !defined(BSDIFF_HEADER_ONLY)
#include <limits.h>
-#include <stdlib.h>
#include <string.h>
#define MIN(x,y) (((x)<(y)) ? (x) : (y))
@@ -257,11 +259,11 @@ static int bsdiff_internal(const struct bsdiff_request req)
uint8_t *db,*eb;
uint8_t buf[8 * 3];
- if((V=malloc((req.oldsize+1)*sizeof(int64_t)))==NULL) return -1;
+ if((V=req.stream->malloc((req.oldsize+1)*sizeof(int64_t)))==NULL) return -1;
I = req.I;
qsufsort(I,V,req.old,req.oldsize);
- free(V);
+ req.stream->free(V);
db = req.db;
eb = req.eb;
@@ -397,19 +399,19 @@ int bsdiff(const uint8_t* old, int64_t oldsize, const uint8_t* new, int64_t news
int result;
struct bsdiff_request req;
- if((req.I=malloc((oldsize+1)*sizeof(int64_t)))==NULL)
+ if((req.I=stream->malloc((oldsize+1)*sizeof(int64_t)))==NULL)
return -1;
- if((req.db=malloc(newsize+1))==NULL)
+ if((req.db=stream->malloc(newsize+1))==NULL)
{
- free(req.I);
+ stream->free(req.I);
return -1;
}
- if((req.eb=malloc(newsize+1))==NULL)
+ if((req.eb=stream->malloc(newsize+1))==NULL)
{
- free(req.db);
- free(req.I);
+ stream->free(req.db);
+ stream->free(req.I);
return -1;
}
@@ -422,9 +424,9 @@ int bsdiff(const uint8_t* old, int64_t oldsize, const uint8_t* new, int64_t news
result = bsdiff_internal(req);
- free(req.eb);
- free(req.db);
- free(req.I);
+ stream->free(req.eb);
+ stream->free(req.db);
+ stream->free(req.I);
return result;
}
@@ -437,6 +439,7 @@ int bsdiff(const uint8_t* old, int64_t oldsize, const uint8_t* new, int64_t news
#include <err.h>
#include <fcntl.h>
#include <stdio.h>
+#include <stdlib.h>
#include <unistd.h>
static int bz2_write(struct bsdiff_stream* stream, const void* buffer, int size)
@@ -528,6 +531,8 @@ int main(int argc,char *argv[])
FILE * pf;
struct bsdiff_stream stream;
bz_stream bz2 = {0};
+ stream.malloc = malloc;
+ stream.free = free;
stream.opaque = &bz2;
stream.write = bz2_write;
stream.finish = bz2_finish;