View Single Post
Cataphract

JCF Member

Joined: Sep 2002

Posts: 24

Cataphract is doing well so far

Feb 14, 2007, 04:59 PM
Cataphract is offline
Reply With Quote
Unhappy

To Drmoo: it'll be part of version 4 of my add on.

I've followed you example, but I always find a mismatch between what you say to be the declared size of the output stream and the actual size of the output stream as reported by uncompress(). I'm using zlib 1.2.3

Code:
#include <stdio.h>
#include <stdlib.h>
#include "zlib/zlib.h"

#define err(str)	fwrite(str "\n", 1, strlen(str)+2, stderr)

int main(int argc, char **argv) {
  FILE *file;
  int compsize,uncompsize,actuncompsize; //assumed to be 32bit long
  int resuncomp; //to hold the result of uncompress
  char *inputstream,*outputstream;
  if (argc < 2) { err("Give the file as a second argument."); return 1; }
  
  if ((file = fopen(argv[1],"rb")) == NULL) { perror("Could not open the file for reading"); return 1; }
  
  if (fseek(file, (long int) 254, SEEK_SET) != 0) {
	perror("Error setting the position for reading");
	return 1;
  }
  
  if (fread(&compsize, 4, 1, file) == 0) {
	perror("Could not read the size of the input stream");
	return 1;
  };
  inputstream = malloc (compsize);
  if (inputstream == NULL) {
	perror("Could not allocate memory for the input stream");
	return 1;
  };
  
  if (fread(&uncompsize, 4, 1, file) == 0) {
	perror("Could not read the size of the output stream");
	return 1;
  }
  outputstream = malloc (uncompsize);
  
  if (inputstream == NULL) {
	perror("Could not allocate memory for the output stream");
	return 1;
  };
   
  if (fread(inputstream, 1, compsize, file) < compsize) {
	perror("Could not read the input stream");
	return 1;
  };
  if (fclose(file) != 0)  { perror("Error closing the file"); return 1; }
  
  actuncompsize = uncompsize;
  actuncompsize = 1000000; outputstream = realloc (outputstream,1000000); //testing purposes
  resuncomp = uncompress(outputstream, &actuncompsize, inputstream, compsize);
  printf("input size %i, actual output size %i, "
	"expected output size %i\n",compsize,actuncompsize,uncompsize);
  //if (resuncomp != Z_OK) { err("Error uncompressing the input stream."); return 1; }
  //if (uncompsize != actuncompsize) { perror("Output stream had an unexpected size."); return 1; }
  
  
  printf("Written: %u bytes\n",fwrite(outputstream, 1, actuncompsize, fopen("t:\\j\\out.txt","wb")));

  return 0;
}
examples:

battle 3 (the 1998 file):
input size 3264, actual output size 33517, expected output size 16814
Written: 33517 bytes

ab5btl11.j2l
input size 2039, actual output size 34029, expected output size 6810
Written: 34029 bytes

ab5btl12.j2l
input size 1389, actual output size 34029, expected output size 5452
Written: 34029 bytes

Actual output size for 1.24 files is usually 73069.

Last edited by Cataphract; Feb 14, 2007 at 05:12 PM.