Skip to content
This repository has been archived by the owner on Mar 2, 2018. It is now read-only.

Commit

Permalink
Merge pull request #8 from jlindgren90/master
Browse files Browse the repository at this point in the history
Various bug fixes
  • Loading branch information
simsong committed May 9, 2013
2 parents 32ec6e0 + 168831b commit b0a36e3
Show file tree
Hide file tree
Showing 28 changed files with 587 additions and 937 deletions.
446 changes: 0 additions & 446 deletions config.h.in

This file was deleted.

23 changes: 14 additions & 9 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ AC_INIT([AFFLIB],[3.7.1],[[email protected]])
AM_INIT_AUTOMAKE
AM_MAINTAINER_MODE

AC_CONFIG_FILES([Makefile lib/Makefile tools/Makefile lzma443/Makefile
AC_CONFIG_FILES([Makefile lib/Makefile tools/Makefile lzma443/Makefile
tests/Makefile doc/Makefile pyaff/Makefile man/Makefile lib/version.h])
AC_CONFIG_FILES([afflib.spec])
AC_CONFIG_HEADERS([affconfig.h])
Expand Down Expand Up @@ -43,13 +43,13 @@ if test x"${cross_compiling}" = "xno" ; then
AC_MSG_NOTICE([ *** ADDING ${spfx}/lib to LDFLAGS *** ])
fi
done
AC_MSG_NOTICE([ CPPFLAGS = ${CPPFLAGS} ])
AC_MSG_NOTICE([ LDFLAGS = ${LDFLAGS} ])
AC_MSG_NOTICE([ CPPFLAGS = ${CPPFLAGS} ])
AC_MSG_NOTICE([ LDFLAGS = ${LDFLAGS} ])
else
AC_MSG_NOTICE([Cross Compiling --- will not update CPPFALGS or LDFLAGS with /usr/local, /opt/local or /sw])
LIBS="$LIBS -lws2_32 -lgdi32"
fi

if test -r /bin/uname.exe ; then
if test `uname -o` == "Msys" ; then
AC_MSG_NOTICE([Compiling with Msys. Setting flags appropriately.])
Expand All @@ -75,7 +75,7 @@ AC_C_INLINE
AC_TYPE_MODE_T
AC_TYPE_OFF_T
AC_TYPE_SIZE_T
AC_STRUCT_TM
AC_STRUCT_TM
AC_STRUCT_ST_BLOCKS
AC_STRUCT_ST_RDEV

Expand Down Expand Up @@ -135,6 +135,11 @@ AC_CHECK_HEADER([expat.h])
AC_CHECK_LIB([expat],[XML_ParserCreate],,[have_expat="no ";AC_MSG_WARN([expat not found; S3 and Digital Signatures not enabled])])


################################################################
## LZMA (always enabled)
AC_DEFINE(USE_LZMA, 1, [Enable LZMA compression])


################################################################
## Amazon S3
## S3 requires curl and expat; otherwise we don't need them
Expand Down Expand Up @@ -166,7 +171,7 @@ AC_SUBST(S3_BIN)
AM_CONDITIONAL([MAYBE_S3],[test "x${enable_s3}" = "xyes"]) # used by automake

################################################################
### QEMU
### QEMU
################################################################

AC_ARG_ENABLE([qemu])
Expand All @@ -191,9 +196,9 @@ AM_CONDITIONAL([MAYBE_QEMU],[test "x${enable_qemu}" = "xyes"]) # used by automa


################################################################
### PyAFF
### PyAFF
################################################################
# python checks
# python checks
# (requires autoconf 1.5+ and the macros in acinclude.m4)
AC_ARG_ENABLE([python],
AC_HELP_STRING([--enable-python=no], [Build python bindings (pyaff)]),
Expand Down Expand Up @@ -267,7 +272,7 @@ if test x"${AFF_NOOPT}" != "x" ; then
fi

if test "${with_noopt}" = "yes" ; then
CFLAGS=`echo "$CFLAGS" | sed s/-O[[0-9]]//` # note the double quoting!
CFLAGS=`echo "$CFLAGS" | sed s/-O[[0-9]]//` # note the double quoting!
CXXFLAGS=`echo "$CXXFLAGS" | sed s/-O[[0-9]]//`
fi

Expand Down
14 changes: 7 additions & 7 deletions lib/aff_db.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* Distributed under the 4-part Berkeley License */
/* Distributed under the 4-part Berkeley License */

/*
* afflib_db.cpp:
*
*
* Functions for the manipulation of the AFF database.
*/

Expand All @@ -18,7 +18,7 @@
****************************************************************/

/****************************************************************
*** Probe Functions
*** Probe Functions
****************************************************************/

int af_probe_next_seg(AFFILE *af,
Expand All @@ -29,15 +29,15 @@ int af_probe_next_seg(AFFILE *af,
size_t *segsize_, // optional get size of entire segment
int do_rewind) // optional rewind af->aseg, otherwise leave at start of segment data
{
if(!af->aseg)(*af->error_reporter)("af_probe_next_segment only works with aff files");
if(!af->aseg)(*af->error_reporter)("af_probe_next_segment only works with aff files");

struct af_segment_head segh;
memset(&segh,0,sizeof(segh));

uint64_t start = ftello(af->aseg);
#ifdef __BORLANDC__
fseeko(af->aseg, start, SEEK_SET) ; // Windows is dumb :-(
#endif
#endif

if(fread(&segh,sizeof(segh),1,af->aseg)!=1){
return AF_ERROR_EOF;
Expand Down Expand Up @@ -152,7 +152,7 @@ int aff_find_seg(AFFILE *af,const char *segname,
}
return -1; // couldn't find segment
}

int af_get_segq(AFFILE *af,const char *name,int64_t *aff_quad)
{
unsigned char buf[8];
Expand All @@ -164,7 +164,7 @@ int af_get_segq(AFFILE *af,const char *name,int64_t *aff_quad)
if(bufsize!=sizeof(struct aff_quad)){ // make sure size is good.
return -1;
}

*aff_quad = af_decode_q(buf);
return 0;
}
Expand Down
96 changes: 92 additions & 4 deletions lib/aff_toc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

/*
* afflib_dir.cpp:
*
*
* Functions for the manipulation of the AFF directories
*/

Expand Down Expand Up @@ -47,6 +47,10 @@ static int toc_sort(const void *a_,const void *b_)

static int aff_toc_append(AFFILE *af,const char *segname,uint64_t offset,uint64_t datalen)
{
/* don't add AF_IGNORE segments to TOC */
if(!segname[0])
return 0;

af->toc = (aff_toc_mem *)realloc(af->toc,sizeof(*af->toc)*(af->toc_count+1));
if(af->toc==0){
(*af->error_reporter)("realloc() failed in aff_toc_append. toc_count=%d\n",af->toc_count);
Expand All @@ -64,6 +68,10 @@ static int aff_toc_append(AFFILE *af,const char *segname,uint64_t offset,uint64_
*/
void aff_toc_update(AFFILE *af,const char *segname,uint64_t offset,uint64_t datalen)
{
/* don't add AF_IGNORE segments to TOC */
if(!segname[0])
return;

for(int i=0;i<af->toc_count;i++){
if(af->toc[i].name==0 || strcmp(af->toc[i].name,segname)==0){
if(af->toc[i].name==0){ // if name was empty, copy it over
Expand All @@ -77,6 +85,35 @@ void aff_toc_update(AFFILE *af,const char *segname,uint64_t offset,uint64_t data
aff_toc_append(af,segname,offset,datalen); /* Need to append it to the directory */
}

/* exact compression settings are not saved in the AFF file, so make a best guess */
static void guess_compression(AFFILE *af, const char *segname, uint32_t flags)
{
/* ignore non-data segments */
if(af_segname_page_number(segname) < 0)
return;

if(flags & AF_PAGE_COMPRESSED)
{
switch(flags & AF_PAGE_COMP_ALG_MASK)
{
case AF_PAGE_COMP_ALG_ZLIB:
af->compression_type = AF_COMPRESSION_ALG_ZLIB;
af->compression_level = (flags & AF_PAGE_COMP_MAX) ? AF_COMPRESSION_MAX : AF_COMPRESSION_DEFAULT;
break;

case AF_PAGE_COMP_ALG_LZMA:
af->compression_type = AF_COMPRESSION_ALG_LZMA;
af->compression_level = (flags & AF_PAGE_COMP_MAX) ? AF_COMPRESSION_MAX : AF_COMPRESSION_DEFAULT;
break;
}
}
else
{
af->compression_type = AF_COMPRESSION_ALG_NONE;
af->compression_level = 0;
}
}

/*
* aff_toc_build:
* Build the directory by reading the existing file.
Expand All @@ -87,25 +124,33 @@ int aff_toc_build(AFFILE *af) // build the dir if we couldn't find it
aff_toc_free(af); // clear the old one
af_rewind_seg(af); // start at the beginning

// note: was malloc(0), but that causes problems under Borland
/* default compression settings */
af->compression_type = AF_COMPRESSION_ALG_ZLIB;
af->compression_level = AF_COMPRESSION_DEFAULT;

// note: was malloc(0), but that causes problems under Borland
af->toc = (aff_toc_mem *)malloc(sizeof(aff_toc_mem));
while(1){
char segname[AF_MAX_NAME_LEN];
size_t segname_len=sizeof(segname);
uint64_t pos = ftello(af->aseg);
uint32_t flags = 0;
size_t datalen=0;

int r = af_get_next_seg(af,segname,segname_len,0,0,&datalen);
errno = 0;
int r = af_get_next_seg(af, segname, segname_len, &flags, 0, &datalen);
switch(r){
case AF_ERROR_NO_ERROR:
guess_compression(af, segname, flags);
if(aff_toc_append(af,segname,pos,datalen)){
return -1; // malloc error?
}
break;
case AF_ERROR_EOF:
return 0; // end of file; no errors
default: /* unknown error */
fseeko(af->aseg,pos,SEEK_SET); // go back
if(!errno)
errno = EIO;
return r; // send up the error code
}
}
Expand Down Expand Up @@ -137,3 +182,46 @@ int aff_toc_del(AFFILE *af,const char *segname)
return -1;
}

/* Finds the next segment starting at or beyond a given byte offset. */
/* Returns the TOC entry if found; null if not found. */
struct aff_toc_mem *aff_toc_next_seg(AFFILE *af, uint64_t offset)
{
struct aff_toc_mem *next_seg = 0;
struct aff_toc_mem *end = af->toc + af->toc_count;

for(struct aff_toc_mem *seg = af->toc; seg != end; seg++)
{
if(!seg->name)
continue;

if(seg->offset >= offset && (!next_seg || seg->offset < next_seg->offset))
next_seg = seg;
}

return next_seg;
}

/* Finds the smallest unused block of at least min_size bytes. */
/* Returns 0 if found, -1 if not found. */
int aff_toc_find_hole(AFFILE *af, uint64_t min_size, uint64_t *offset, uint64_t *size)
{
int ret = -1;
uint64_t search_offset = 0;
struct aff_toc_mem *seg;

while((seg = aff_toc_next_seg(af, search_offset)))
{
uint64_t test_size = seg->offset - search_offset;

if(test_size >= min_size && (ret < 0 || test_size < *size))
{
*offset = search_offset;
*size = test_size;
ret = 0;
}

search_offset = seg->offset + seg->segment_len;
}

return ret;
}
Loading

0 comments on commit b0a36e3

Please sign in to comment.