Beispiel #1
0
unsigned char * unpack(unsigned char *packeddata,unsigned int size)
{
	
	unsigned char * buffer,* finalbuffer;
	int i,j;

	init_decompress();

	buffer_size=size;
	buffer_offset=0;
	buffer_ptr=packeddata+sizeof(TELEDISK_HEADER);


	j=0;
	buffer=0;
	buffer=(unsigned char*)realloc(	buffer,512);
	do
	{

		//getblock(buffer, 128, 0);
		i=0;
		do
		{

			buffer[j]=getbyte();
			j++;
			i++;
		}while(i<512 && (Eof!=255));
		buffer=(unsigned char*)realloc(	buffer,j+512);

	}while(Eof!=255);

	finalbuffer=0;
	finalbuffer=malloc(j+sizeof(TELEDISK_HEADER));
	if(finalbuffer)
	{
		memcpy(finalbuffer,packeddata,sizeof(TELEDISK_HEADER));
		memcpy(&finalbuffer[sizeof(TELEDISK_HEADER)],buffer,j);

	}

	free(packeddata);
	free(buffer);

	return finalbuffer;
}
int main(int argc, char **argv) {
  int fin, res, id=0;
  off_t block_end, block_start, upto;
  id_info_t id_info;
  char *filename = NULL;
  char *type = NULL;
  int optindex=0;
  bz_info_t bfile;
  int verbose = 0;
  int optc;
  int result;

  struct option optvalues[] = {
    {"filename", 1, 0, 'f'},
    {"type", 1, 0, 't'},
    {"verbose", 0, 0, 'v'},
    {"version", 0, 0, 'V'},
    {NULL, 0, NULL, 0}
  };

  while (1) {
    optc = getopt_long_only(argc,argv,"f:hvV", optvalues, &optindex);
    if (optc=='f') {
     filename=optarg;
    }
    else if (optc == 't') {
     type = optarg;
    }
    else if (optc == 'h')
      usage(NULL);
    else if (optc == 'v')
      verbose++;
    else if (optc == 'V')
      show_version(VERSION);
    else if (optc == -1) break;
    else usage("Unknown option or other error\n");
  }

  if (! filename) {
    usage(NULL);
  }

  fin = open (filename, O_RDONLY);
  if (fin < 0) {
    fprintf(stderr,"Failed to open file %s for read\n", filename);
    exit(1);
  }

  bfile.file_size = get_file_size(fin);
  bfile.footer = init_footer();
  bfile.marker = init_marker();
  result = check_file_for_footer(fin, &bfile);
  if (result == -1) {
    bfile.position = bfile.file_size;
  }
  else {
    bfile.position = bfile.file_size - (off_t)11; /* size of footer, perhaps with 1 byte extra */
  }
  bfile.position -=(off_t)6; /* size of marker */
  bfile.initialized = 0;
  bfile.bytes_read = 0;

  /* start at end of file */
  block_end = bfile.position;
  upto = block_end;

  block_start = (off_t)-1;
  id = 0;

  while (!id) {
    bfile.initialized = 0;
    init_decompress(&bfile);

    block_start = find_first_bz2_block_from_offset(&bfile, fin, block_end, BACKWARD);

    if (block_start <= (off_t) 0) giveup(fin);
    BZ2_bzDecompressEnd (&(bfile.strm));

    res = get_last_id_after_offset(fin, &id_info, &bfile, upto, type, verbose);
    if (res > 0) {
      id = id_info.id;
    }
    else {
      upto = block_end;
      block_end = block_start - (off_t) 1;
      if (block_end <= (off_t) 0) giveup(fin);
    }
    BZ2_bzDecompressEnd (&(bfile.strm));
  }
  if (!id) giveup(fin);

  fprintf(stdout, "%s_id:%d\n", type, id);
  close(fin);
  exit(0);
}