<commit_before>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <liboil/liboil.h>
#include <liboil/liboil-stdint.h>
#include <cogcompat.h>

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "jpeg.h"


extern uint8_t jpeg_standard_tables[];
extern int jpeg_standard_tables_size;

void jpeg_decoder_define_huffman_tables (JpegDecoder * dec);
void jpeg_decoder_define_arithmetic_conditioning (JpegDecoder *dec);
void jpeg_decoder_define_quantization_tables (JpegDecoder *dec);
void jpeg_decoder_define_restart_interval (JpegDecoder *dec);
void jpeg_decoder_start_of_frame (JpegDecoder * dec, int marker);
void jpeg_decoder_start_of_scan (JpegDecoder * dec);


/* misc helper function declarations */

static void jpeg_load_standard_huffman_tables (JpegDecoder * dec);

static void jpeg_decoder_verify_header (JpegDecoder *dec);
static void jpeg_decoder_init_decoder (JpegDecoder *dec);


void
jpeg_decoder_error(JpegDecoder *dec, const char *fmt, ...)
{
  va_list varargs;

  if (dec->error) return;

  va_start (varargs, fmt);
#ifdef HAVE_VASPRINTF
  vasprintf(&dec->error_message, fmt, varargs);
#else
  dec->error_message = malloc(250);
  vsnprintf(dec->error_message, 250 - 1, fmt, varargs);
  dec->error_message[250 - 1] = 0;
#endif
  va_end (varargs);

  dec->error = TRUE;
}

#define jpeg_decoder_error(dec, ...) { \
  COG_ERROR("decoder error: "__VA_ARGS__); \
  jpeg_decoder_error (dec, __VA_ARGS__); \
}

static void
jpeg_decoder_verify_header (JpegDecoder *dec)
{
  int max_quant_table = 0;
  int i;

  if (dec->sof_type != JPEG_MARKER_SOF_0) {
    COG_ERROR("only handle baseline DCT");
    dec->error = TRUE;
  }

  if (dec->width < 1) {
    COG_ERROR("height can't be 0");
    dec->error = TRUE;
  }

  switch (dec->sof_type) {
    case JPEG_MARKER_SOF_0:
      /* baseline DCT */
      max_quant_table = 3;
      if (dec->depth != 8) {
        COG_ERROR("depth must be 8 (%d)", dec->depth);
        dec->error = TRUE;
      }
      break;
    case JPEG_MARKER_SOF_1:
      /* extended DCT */
      max_quant_table = 3;
      if (dec->depth != 8 && dec->depth != 12) {
        COG_ERROR("depth must be 8 or 12 (%d)", dec->depth);
        dec->error = TRUE;
      }
      break;
    case JPEG_MARKER_SOF_2:
      /* progressive DCT */
      max_quant_table = 3;
      if (dec->depth != 8 && dec->depth != 12) {
        COG_ERROR("depth must be 8 or 12 (%d)", dec->depth);
        dec->error = TRUE;
      }
      break;
    case JPEG_MARKER_SOF_3:
      /* lossless DCT */
      max_quant_table = 0;
      if (dec->depth < 2 || dec->depth > 16) {
        COG_ERROR("depth must be between 2 and 16 (%d)", dec->depth);
        dec->error = TRUE;
      }
      break;
    default:
      break;
  }

  if (dec->n_components < 0 || dec->n_components > 255) {
    COG_ERROR("n_components must be in the range 0-255 (%d)",
        dec->n_components);
    dec->error = TRUE;
  }
  if (dec->sof_type == JPEG_MARKER_SOF_2 && dec->n_components > 4) {
    COG_ERROR("n_components must be <= 4 for progressive DCT (%d)",
        dec->n_components);
    dec->error = TRUE;
  }

  for (i = 0; i < dec->n_components; i++) {
    if (dec->components[i].id < 0 || dec->components[i].id > 255) {
      COG_ERROR("component ID out of range");
      dec->error = TRUE;
      break;
    }
    if (dec->components[i].h_sample < 1 || dec->components[i].h_sample > 4 ||
        dec->components[i].v_sample < 1 || dec->components[i].v_sample > 4) {
      COG_ERROR("sample factor(s) for component %d out of range %d %d",
          i, dec->components[i].h_sample, dec->components[i].v_sample);
      dec->error = TRUE;
      break;
    }
    if (dec->components[i].quant_table < 0 ||
        dec->components[i].quant_table > max_quant_table) {
      COG_ERROR("quant table for component %d out of range (%d)",
          i, dec->components[i].quant_table);
      dec->error = TRUE;
      break;
    }
  }
}

static void
jpeg_decoder_init_decoder (JpegDecoder *dec)
{
  int max_h_sample = 0;
  int max_v_sample = 0;
  int i;

  /* decoder limitations */
  if (dec->n_components != 3) {
    jpeg_decoder_error(dec, "wrong number of components %d", dec->n_components);
    return;
  }
  if (dec->sof_type != JPEG_MARKER_SOF_0) {
    jpeg_decoder_error(dec, "only handle baseline DCT");
    return;
  }




  for (i=0; i < dec->n_components; i++) {
    max_h_sample = MAX (max_h_sample, dec->components[i].h_sample);
    max_v_sample = MAX (max_v_sample, dec->components[i].v_sample);
  }

  dec->width_blocks =
      (dec->width + 8 * max_h_sample - 1) / (8 * max_h_sample);
  dec->height_blocks =
      (dec->height + 8 * max_v_sample - 1) / (8 * max_v_sample);
  for (i = 0; i < dec->n_components; i++) {
    int rowstride;
    int image_size;

    dec->components[i].h_subsample = max_h_sample /
        dec->components[i].h_sample;
    dec->components[i].v_subsample = max_v_sample /
        dec->components[i].v_sample;

    rowstride = dec->width_blocks * 8 * max_h_sample /
        dec->components[i].h_subsample;
    image_size = rowstride *
        (dec->height_blocks * 8 * max_v_sample /
        dec->components[i].v_subsample);
    dec->components[i].rowstride = rowstride;<commit_msg>remove excessive whitespace<commit_after>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <liboil/liboil.h>
#include <liboil/liboil-stdint.h>
#include <cogcompat.h>

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "jpeg.h"


extern uint8_t jpeg_standard_tables[];
extern int jpeg_standard_tables_size;

void jpeg_decoder_define_huffman_tables (JpegDecoder * dec);
void jpeg_decoder_define_arithmetic_conditioning (JpegDecoder *dec);
void jpeg_decoder_define_quantization_tables (JpegDecoder *dec);
void jpeg_decoder_define_restart_interval (JpegDecoder *dec);
void jpeg_decoder_start_of_frame (JpegDecoder * dec, int marker);
void jpeg_decoder_start_of_scan (JpegDecoder * dec);


/* misc helper function declarations */

static void jpeg_load_standard_huffman_tables (JpegDecoder * dec);

static void jpeg_decoder_verify_header (JpegDecoder *dec);
static void jpeg_decoder_init_decoder (JpegDecoder *dec);


void
jpeg_decoder_error(JpegDecoder *dec, const char *fmt, ...)
{
  va_list varargs;

  if (dec->error) return;

  va_start (varargs, fmt);
#ifdef HAVE_VASPRINTF
  vasprintf(&dec->error_message, fmt, varargs);
#else
  dec->error_message = malloc(250);
  vsnprintf(dec->error_message, 250 - 1, fmt, varargs);
  dec->error_message[250 - 1] = 0;
#endif
  va_end (varargs);

  dec->error = TRUE;
}

#define jpeg_decoder_error(dec, ...) { \
  COG_ERROR("decoder error: "__VA_ARGS__); \
  jpeg_decoder_error (dec, __VA_ARGS__); \
}

static void
jpeg_decoder_verify_header (JpegDecoder *dec)
{
  int max_quant_table = 0;
  int i;

  if (dec->sof_type != JPEG_MARKER_SOF_0) {
    COG_ERROR("only handle baseline DCT");
    dec->error = TRUE;
  }

  if (dec->width < 1) {
    COG_ERROR("height can't be 0");
    dec->error = TRUE;
  }

  switch (dec->sof_type) {
    case JPEG_MARKER_SOF_0:
      /* baseline DCT */
      max_quant_table = 3;
      if (dec->depth != 8) {
        COG_ERROR("depth must be 8 (%d)", dec->depth);
        dec->error = TRUE;
      }
      break;
    case JPEG_MARKER_SOF_1:
      /* extended DCT */
      max_quant_table = 3;
      if (dec->depth != 8 && dec->depth != 12) {
        COG_ERROR("depth must be 8 or 12 (%d)", dec->depth);
        dec->error = TRUE;
      }
      break;
    case JPEG_MARKER_SOF_2:
      /* progressive DCT */
      max_quant_table = 3;
      if (dec->depth != 8 && dec->depth != 12) {
        COG_ERROR("depth must be 8 or 12 (%d)", dec->depth);
        dec->error = TRUE;
      }
      break;
    case JPEG_MARKER_SOF_3:
      /* lossless DCT */
      max_quant_table = 0;
      if (dec->depth < 2 || dec->depth > 16) {
        COG_ERROR("depth must be between 2 and 16 (%d)", dec->depth);
        dec->error = TRUE;
      }
      break;
    default:
      break;
  }

  if (dec->n_components < 0 || dec->n_components > 255) {
    COG_ERROR("n_components must be in the range 0-255 (%d)",
        dec->n_components);
    dec->error = TRUE;
  }
  if (dec->sof_type == JPEG_MARKER_SOF_2 && dec->n_components > 4) {
    COG_ERROR("n_components must be <= 4 for progressive DCT (%d)",
        dec->n_components);
    dec->error = TRUE;
  }

  for (i = 0; i < dec->n_components; i++) {
    if (dec->components[i].id < 0 || dec->components[i].id > 255) {
      COG_ERROR("component ID out of range");
      dec->error = TRUE;
      break;
    }
    if (dec->components[i].h_sample < 1 || dec->components[i].h_sample > 4 ||
        dec->components[i].v_sample < 1 || dec->components[i].v_sample > 4) {
      COG_ERROR("sample factor(s) for component %d out of range %d %d",
          i, dec->components[i].h_sample, dec->components[i].v_sample);
      dec->error = TRUE;
      break;
    }
    if (dec->components[i].quant_table < 0 ||
        dec->components[i].quant_table > max_quant_table) {
      COG_ERROR("quant table for component %d out of range (%d)",
          i, dec->components[i].quant_table);
      dec->error = TRUE;
      break;
    }
  }
}

static void
jpeg_decoder_init_decoder (JpegDecoder *dec)
{
  int max_h_sample = 0;
  int max_v_sample = 0;
  int i;

  /* decoder limitations */
  if (dec->n_components != 3) {
    jpeg_decoder_error(dec, "wrong number of components %d", dec->n_components);
    return;
  }
  if (dec->sof_type != JPEG_MARKER_SOF_0) {
    jpeg_decoder_error(dec, "only handle baseline DCT");
    return;
  }

  for (i=0; i < dec->n_components; i++) {
    max_h_sample = MAX (max_h_sample, dec->components[i].h_sample);
    max_v_sample = MAX (max_v_sample, dec->components[i].v_sample);
  }

  dec->width_blocks =
      (dec->width + 8 * max_h_sample - 1) / (8 * max_h_sample);
  dec->height_blocks =
      (dec->height + 8 * max_v_sample - 1) / (8 * max_v_sample);
  for (i = 0; i < dec->n_components; i++) {
    int rowstride;
    int image_size;

    dec->components[i].h_subsample = max_h_sample /
        dec->components[i].h_sample;
    dec->components[i].v_subsample = max_v_sample /
        dec->components[i].v_sample;

    rowstride = dec->width_blocks * 8 * max_h_sample /
        dec->components[i].h_subsample;
    image_size = rowstride *
        (dec->height_blocks * 8 * max_v_sample /
        dec->components[i].v_subsample);
    dec->components[i].rowstride = rowstride;
{'fraction_non_alphanumeric': 0.1418562242057779, 'fraction_numerical': 0.015356560130530762, 'mean_word_length': 3.06396255850234, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 167, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4468483', 'n_tokens_mistral': 3981, 'n_tokens_neox': 3736, 'n_words': 835}