예제 #1
0
/* Jump to next block and read its header.  */
static int
jump_block (struct grub_lzopio *lzopio)
{
  /* only jump if block was not decompressed (and read from disk) */
  if (!lzopio->block.udata)
    {
      grub_off_t off = grub_file_tell (lzopio->file) + lzopio->block.csize;

      if (grub_file_seek (lzopio->file, off) == ((grub_off_t) - 1))
	return -1;
    }

  return read_block_header (lzopio);
}
예제 #2
0
static int
calculate_uncompressed_size (grub_file_t file)
{
  grub_lzopio_t lzopio = file->data;
  grub_off_t usize_total = 0;

  if (read_block_header (lzopio) < 0)
    return -1;

  /* FIXME: Don't do this for not easily seekable files.  */
  while (lzopio->block.usize != 0)
    {
      usize_total += lzopio->block.usize;

      if (jump_block (lzopio) < 0)
	return -1;
    }

  file->size = usize_total;

  return 0;
}
예제 #3
0
void tcp_hasher_stream::_receive_construct_data (reconstructor & reconst)
{
	reconst.start_hash_stream (filename_, blk_len_);
	try {
		while (true) {
			stream_buff_.reset ();
			block_header header = read_block_header (client_, observer_);
			if (header.blk_type == BT_EQUAL_BLOCK) {
				assert (header.blk_len != 0);
				read_block (stream_buff_, client_, header.blk_len, observer_);
				target_pos tpos;
				int32_t blk_len;
				uint64_t s_offset;
				stream_buff_ >> tpos.index >> tpos.t_offset >> blk_len >> s_offset;
				reconst.add_block (tpos, blk_len, s_offset);
			}
			else if (header.blk_type == BT_DIFF_BLOCK) {
				assert (header.blk_len != 0);
				read_block (stream_buff_, client_, header.blk_len, observer_);
				uint64_t s_offset;
				stream_buff_ >> s_offset;
				reconst.add_block (stream_buff_.rd_ptr (), header.blk_len - sizeof (s_offset), s_offset);
			}
예제 #4
0
gboolean
ews_oab_decompress_full (const gchar *filename, const gchar *output_filename,
			 GError **error)
{
	LzxHeader *lzx_h = NULL;
	guint total_decomp_size = 0;
	FILE *input, *output = NULL;
	gboolean ret = TRUE;
	GError *err = NULL;

	input = fopen (filename, "rb");
	if (!input) {
		g_set_error_literal (&err, g_quark_from_string ("lzx"), 1, "unable to open the input file");
		ret = FALSE;
		goto exit;
	}

	output = fopen (output_filename, "wb");
	if (!output) {
		g_set_error_literal (&err, g_quark_from_string ("lzx"), 1, "unable to open the output file");
		ret = FALSE;
		goto exit;
	}

	lzx_h = read_headers (input, &err);
	if (!lzx_h) {
		ret = FALSE;
		goto exit;
	}

	/* TODO decompressing multiple lzx_blocks has not been tested yet. Will need to get a setup and test it. */
	do {
		LzxBlockHeader *lzx_b;
		struct lzxd_stream *lzs;
		goffset offset;

		lzx_b = read_block_header (input, &err);
		if (err) {
			ret = FALSE;
			goto exit;
		}

		/* note the file offset */
		offset = ftell (input);

		/* lzx_b points to 1, write it directly to file */
		if (lzx_b->flags == 0) {
			gchar *buffer = g_malloc0 (lzx_b->ucomp_size);

			if (!(fread (buffer, 1, lzx_b->ucomp_size, input) == lzx_b->ucomp_size &&
				fwrite (buffer, 1, lzx_b->ucomp_size, output) == lzx_b->ucomp_size)) {
				g_set_error_literal (&err, g_quark_from_string ("lzx"), 1, "failed to write data in output file");
				g_free (buffer);
				ret = FALSE;
				goto exit;
			}
			g_free (buffer);
		} else {
			/* The window size should be the smallest power of two between 2^17 and 2^25 that is
			   greater than or equal to the sum of the size of the reference data rounded up to
			   a multiple of 32768 and the size of the subject data. Since we have no reference
			   data, forget that and the rounding. Just the smallest power of two which is large
			   enough to cover the subject data (lzx_b->ucomp_size). */

			guint window_bits = g_bit_nth_msf(lzx_b->ucomp_size - 1, -1) + 1;

			if (window_bits < 17)
				window_bits = 17;
			else if (window_bits > 25)
				window_bits = 25;

			lzs = ews_lzxd_init (input, output, window_bits,
					 0, 4096, lzx_b->ucomp_size, 1);
			if (!lzs) {
				g_set_error_literal (&err, g_quark_from_string ("lzx"), 1, "decompression failed (lzxd_init)");
				ret = FALSE;
				goto exit;
			}
			if (ews_lzxd_decompress (lzs, lzx_b->ucomp_size) != LZX_ERR_OK) {
				g_set_error_literal (&err, g_quark_from_string ("lzx"), 1, "decompression failed (lzxd_decompress)");
				ret = FALSE;
				goto exit;
			}
		}

		/* Set the fp to beggining of next block. This is a HACK, looks like decompress reads beyond the block.
		 * Since we can identify the next block start from block header, we just reset the offset */
		offset += lzx_b->comp_size;
		fseek (input, offset, SEEK_SET);

		total_decomp_size += lzx_b->ucomp_size;
		g_free (lzx_b);
	} while (total_decomp_size < lzx_h->target_size);

exit:
	if (input)
		fclose (input);

	if (output)
		fclose (output);

	if (err) {
		ret = FALSE;
		g_propagate_error (error, err);
		g_unlink (output_filename);
	}

	g_free (lzx_h);

	return ret;
}
예제 #5
0
파일: decoder.c 프로젝트: amdonov/OpenEBTS
int huffman_decode_data_file(
   short *ip,             /* image pointer */
   DTT_TABLE *dtt_table,  /*transform table pointer */
   DQT_TABLE *dqt_table,  /* quantization table */
   DHT_TABLE *dht_table,  /* huffman table */
   FILE *infp)            /* input file */
{
   int ret;
   int blk = 0;           /* block number */
   unsigned short marker; /* WSQ markers */
   int bit_count;         /* bit count for nextbits_wsq routine */
   int n;                 /* zero run count */
   int nodeptr;           /* pointers for decoding */
   int last_size;         /* last huffvalue */
   unsigned char hufftable_id;    /* huffman table number */
   HUFFCODE *hufftable;   /* huffman code structure */
   int maxcode[MAX_HUFFBITS+1]; /* used in decoding data */
   int mincode[MAX_HUFFBITS+1]; /* used in decoding data */
   int valptr[MAX_HUFFBITS+1];  /* used in decoding data */
   unsigned short tbits;


   if((ret = read_marker_wsq(&marker, TBLS_N_SOB, infp)))
      return(ret);

   bit_count = 0;

   while(marker != EOI_WSQ) {

      if(marker != 0) {
         blk++;
         while(marker != SOB_WSQ) {
            if((ret = read_table_wsq(marker, dtt_table, dqt_table,
                                dht_table, infp)))
               return(ret);
            if((ret = read_marker_wsq(&marker, TBLS_N_SOB, infp)))
               return(ret);
         }
         if((ret = read_block_header(&hufftable_id, infp)))
            return(ret);

         if((dht_table+hufftable_id)->tabdef != 1) {
            fprintf(stderr, "ERROR : huffman_decode_data_file : ");
            fprintf(stderr, "huffman table {%d} undefined.\n", hufftable_id);
            return(-53);
         }

         /* the next two routines reconstruct the huffman tables */
         if((ret = build_huffsizes(&hufftable, &last_size,
                        (dht_table+hufftable_id)->huffbits, MAX_HUFFCOUNTS_WSQ)))
            return(ret);
         build_huffcodes(hufftable);
         if((ret = check_huffcodes_wsq(hufftable, last_size)))
            fprintf(stderr, "         hufftable_id = %d\n", hufftable_id);

         /* this routine builds a set of three tables used in decoding */
         /* the compressed data*/
         gen_decode_table(hufftable, maxcode, mincode, valptr,
                          (dht_table+hufftable_id)->huffbits);
         free(hufftable);
         bit_count = 0;
         marker = 0;
      }

      /* get next huffman category code from compressed input data stream */
      if((ret = decode_data_file(&nodeptr, mincode, maxcode, valptr,
                            (dht_table+hufftable_id)->huffvalues,
                            infp, &bit_count, &marker)))
         return(ret);

      if(nodeptr == -1) {
         while(marker == COM_WSQ && blk == 3) {
            if((ret = read_table_wsq(marker, dtt_table, dqt_table,
                                dht_table, infp)))
               return(ret);
            if((ret = read_marker_wsq(&marker, ANY_WSQ, infp)))
               return(ret);
         }
         continue;
      }

      if(nodeptr > 0 && nodeptr <= 100)
         for(n = 0; n < nodeptr; n++) {
            *ip++ = 0; /* z run */
         }
      else if(nodeptr == 101){
         if((ret = nextbits_wsq(&tbits, &marker, infp, &bit_count, 8)))
            return(ret);
         *ip++ = tbits;
      }
      else if(nodeptr == 102){
         if((ret = nextbits_wsq(&tbits, &marker, infp, &bit_count, 8)))
            return(ret);
         *ip++ = -tbits;
      }
      else if(nodeptr == 103){
         if((ret = nextbits_wsq(&tbits, &marker, infp, &bit_count, 16)))
            return(ret);
         *ip++ = tbits;
      }
      else if(nodeptr == 104){
         if((ret = nextbits_wsq(&tbits, &marker, infp, &bit_count, 16)))
            return(ret);
         *ip++ = -tbits;
      }
      else if(nodeptr == 105) {
         if((ret = nextbits_wsq(&tbits, &marker, infp, &bit_count, 8)))
            return(ret);
         n = tbits;
         while(n--)
            *ip++ = 0;
      }
      else if(nodeptr == 106) {
         if((ret = nextbits_wsq(&tbits, &marker, infp, &bit_count, 16)))
            return(ret);
         n = tbits;
         while(n--)
            *ip++ = 0;
      }
      else if(nodeptr < 0xff)
         *ip++ = nodeptr - 180;
      else {
         fprintf(stderr, 
                "ERROR: huffman_decode_data_file : Invalid code %d (%x).\n",
                nodeptr, nodeptr);
         return(-54);
      }
   }

   return(0);
}