Beispiel #1
0
static int JBIGDecode(TIFF* tif, uint8* buffer, tmsize_t size, uint16 s)
{
	struct jbg_dec_state decoder;
	int decodeStatus = 0;
	unsigned char* pImage = NULL;
	(void) size, (void) s;

	if (isFillOrder(tif, tif->tif_dir.td_fillorder))
	{
		TIFFReverseBits(tif->tif_rawdata, tif->tif_rawdatasize);
	}

	jbg_dec_init(&decoder);

#if defined(HAVE_JBG_NEWLEN)
	jbg_newlen(tif->tif_rawdata, (size_t)tif->tif_rawdatasize);
	/*
	 * I do not check the return status of jbg_newlen because even if this
	 * function fails it does not necessarily mean that decoding the image
	 * will fail.  It is generally only needed for received fax images
	 * that do not contain the actual length of the image in the BIE
	 * header.  I do not log when an error occurs because that will cause
	 * problems when converting JBIG encoded TIFF's to
	 * PostScript.  As long as the actual image length is contained in the
	 * BIE header jbg_dec_in should succeed.
	 */
#endif /* HAVE_JBG_NEWLEN */

	decodeStatus = jbg_dec_in(&decoder, (unsigned char*)tif->tif_rawdata,
				  (size_t)tif->tif_rawdatasize, NULL);
	if (JBG_EOK != decodeStatus)
	{
		/*
		 * XXX: JBG_EN constant was defined in pre-2.0 releases of the
		 * JBIG-KIT. Since the 2.0 the error reporting functions were
		 * changed. We will handle both cases here.
		 */
		TIFFErrorExt(tif->tif_clientdata,
			     "JBIG", "Error (%d) decoding: %s",
			     decodeStatus,
#if defined(JBG_EN)
			     jbg_strerror(decodeStatus, JBG_EN)
#else
			     jbg_strerror(decodeStatus)
#endif
			     );
		return 0;
	}

	pImage = jbg_dec_getimage(&decoder, 0);
	_TIFFmemcpy(buffer, pImage, jbg_dec_getsize(&decoder));
	jbg_dec_free(&decoder);
	return 1;
}
Beispiel #2
0
static int JBIGDecode(TIFF* tif, tidata_t buffer, tsize_t size, tsample_t s)
{
        struct jbg_dec_state decoder;
        int decodeStatus = 0;
        unsigned char* pImage = NULL;
	(void) size, (void) s;

        if (isFillOrder(tif, tif->tif_dir.td_fillorder))
        {
                TIFFReverseBits(tif->tif_rawdata, tif->tif_rawdatasize);
        }

        jbg_dec_init(&decoder);

#if defined(HAVE_JBG_NEWLEN)
        jbg_newlen(tif->tif_rawdata, tif->tif_rawdatasize);
        /*
         * I do not check the return status of jbg_newlen because even if this
         * function fails it does not necessarily mean that decoding the image
         * will fail.  It is generally only needed for received fax images
         * that do not contain the actual length of the image in the BIE
         * header.  I do not log when an error occurs because that will cause
         * problems when converting JBIG encoded TIFF's to 
         * PostScript.  As long as the actual image length is contained in the
         * BIE header jbg_dec_in should succeed.
         */
#endif /* HAVE_JBG_NEWLEN */

        decodeStatus = jbg_dec_in(&decoder, tif->tif_rawdata,
                                  tif->tif_rawdatasize, NULL);
        if (JBG_EOK != decodeStatus)
        {
                TIFFError("JBIG", "Error (%d) decoding: %s",
                          decodeStatus, jbg_strerror(decodeStatus, JBG_EN));
                return 0;
        }
        
        pImage = jbg_dec_getimage(&decoder, 0);
        _TIFFmemcpy(buffer, pImage, jbg_dec_getsize(&decoder));
        jbg_dec_free(&decoder);
        return 1;
}
/**
 * Analyse contract and respond
 *
 * Attempts to parse the supplied file as JBIG
 *
 * \param to_analyse The contract to analyse
 * \param ccr The contract completion report to populate
 * \returns 0 on sucess, -1 on error
 */
int analyse_contract(contract_t to_analyse, contract_completion_report_t ccr)
{
  struct jbg_dec_state sd;
  unsigned char data[JHEAD_SIZE];
  int status;
  size_t cnt;

  const char *path = contract_get_path(to_analyse);

  jbg_dec_init(&sd);
  FILE *file = fopen(path, "r");

  if (file == NULL)
  {
    warning_log("analyse_contract: Could not open file");
    return 0;
  }

  /* Read header and check for valid JBIG */
  int size = 0;

  size = fread(&data, BYTE, JHEAD_SIZE, file);
  status = jbg_dec_in(&sd, data, JHEAD_SIZE, &cnt);

  if (status >= JBG_EINVAL && status <= (JBG_EINVAL | 14))
  {
    debug_log("Not a valid JBIG file");
    jbg_dec_free(&sd);
    return 0;
  }

  /* Read in the whole JBIG */
  int bytes_read;

  while ((bytes_read = fread(&data, BYTE, BYTE, file)))
  {
    size += bytes_read;
    status = jbg_dec_in(&sd, data, BYTE, &cnt);

    if (status == JBG_EOK)
    {                           /* Found end of file */
      break;
    } else if (status != JBG_EAGAIN)
    {                           /* Error value returned */
      debug_log("Error reading JBIG: %s", jbg_strerror(status));
      jbg_dec_free(&sd);
      return 0;
    } else if (size == 104857600)
    {                           /* Only read upto 100MB */
      debug_log("Read 100MB, couldnt find end of JBIG");
      jbg_dec_free(&sd);
      return 0;
    }
  }
  fclose(file);

  /* Process loaded JBIG */
  gchar *hxw = g_strdup_printf("%ldx%ld", jbg_dec_getwidth(&sd), jbg_dec_getheight(&sd));
  gchar *planes = g_strdup_printf("%d plane(s)", jbg_dec_getplanes(&sd));
  gchar *bytes = g_strdup_printf("%dB", size);

  gchar *data_str = g_strdup_printf("%s, %s, %s", bytes, hxw, planes);

  g_free(hxw);
  g_free(planes);
  g_free(bytes);

  /* Fill in report and result data, identifying blocks if possible */
  long long int offset = contract_get_absolute_offset(to_analyse);
  int contig = contract_is_contiguous(to_analyse);

  result_t result = result_init(NULL, 0);

  populate_result_with_length(result, "JBIG", data_str, 100, offset, size, contig);
  contract_completion_report_add_result(ccr, result);
  result_close(result);

  g_free(data_str);

  return 0;
}