<commit_before>#include "mupdf/fitz.h"

#ifdef HAVE_LURATECH

#include <string.h>
#include <ldf_jb2.h>

struct info
{
	fz_context *ctx;
	unsigned long width, height;
	unsigned long xres, yres;
	unsigned long  stride;
	unsigned long  pages;
	fz_colorspace *cspace;
	JB2_Handle_Document doc;

	const unsigned char *data;
	size_t len;

	unsigned char *output;
};

static void * JB2_Callback
jbig2_alloc(unsigned long size, void *userdata)
{
	struct info *state = userdata;
	return fz_malloc(state->ctx, size);
}

static JB2_Error JB2_Callback
jbig2_free(void *ptr, void *userdata)
{
	struct info *state = userdata;
	fz_free(state->ctx, ptr);
	return cJB2_Error_OK;
}

static void JB2_Callback
jbig2_message(const char *msg, JB2_Message_Level level, void *userdata)
{
	struct info *state = userdata;

	if (msg != NULL && msg[0] != '\0')
		switch (level)
		{
		case cJB2_Message_Information:
#ifndef NDEBUG
			fz_warn(state->ctx, "luratech jbig2 info: %s", msg);
#endif
			break;
		case cJB2_Message_Warning:
			fz_warn(state->ctx, "luratech jbig2 warning: %s", msg);
			break;
		case cJB2_Message_Error:
			fz_warn(state->ctx, "luratech jbig2 error: %s", msg);
			break;
		default:
			fz_warn(state->ctx, "luratech jbig2 message: %s", msg);
			break;
		}
}

static JB2_Size_T JB2_Callback
jbig2_read(unsigned char *buf, JB2_Size_T offset, JB2_Size_T size, void *userdata)
{
	struct info *state = userdata;
	size_t available;

	if (state->len <= offset)
		return 0;
	available = fz_minz(state->len - offset, size);
	memcpy(buf, state->data + offset, available);
	return (JB2_Size_T)available;
}

static JB2_Error JB2_Callback
jbig2_write(unsigned char *buf, unsigned long row, unsigned long width, unsigned long bpp, void *userdata)
{
	struct info *state = userdata;
	int stride = (width + 7) >> 3;
	unsigned char *dp = state->output + row * stride;

	if (row >= state->height)
	{
		fz_warn(state->ctx, "row %lu outside of image", row);
		return cJB2_Error_OK;
	}

	while (stride--)
		*(dp++) = *(buf++) ^ 0xff;

	return cJB2_Error_OK;
}


static fz_pixmap *
jbig2_read_image(fz_context *ctx, struct info *jbig2, const unsigned char *buf, size_t len, int only_metadata, int subimage)
{
	struct info *state = jbig2;
	JB2_Error err;
	JB2_Scaling_Factor scale = {1, 1};
	JB2_Rect rect = {0, 0, 0, 0};
	fz_pixmap *pix = NULL;

	fz_var(pix);

	fz_try(ctx)
	{
		state->ctx = ctx;
		state->data = buf;
		state->len = len;

		err = JB2_Document_Start(&state->doc,
				jbig2_alloc, state,
				jbig2_free, state,
				jbig2_read, state,
				jbig2_message, state);
		if (err != cJB2_Error_OK)
			fz_throw(ctx, FZ_ERROR_GENERIC, "cannot open image: %d", (int) err);

#if defined(JB2_LICENSE_NUM_1) && defined(JB2_LICENSE_NUM_2)
		err = JB2_Document_Set_License(doc, JB2_LICENSE_NUM_1, JB2_LICENSE_NUM_2);
		if (err != cJB2_Error_OK)
			fz_throw(ctx, FZ_ERROR_GENERIC, "cannot set license: %d", (int) err);
#endif

		err = JB2_Document_Get_Property(state->doc, cJB2_Prop_Number_Of_Pages, &state->pages);
		if (err != cJB2_Error_OK)
			fz_throw(ctx, FZ_ERROR_GENERIC, "cannot get number of pages: %d", (int) err);
		if (subimage != -1)
		{
			if (subimage < 0 || subimage >= state->pages)
				fz_throw(ctx, FZ_ERROR_GENERIC, "page number out of bounds %d vs %ld", subimage, state->pages);
			err = JB2_Document_Set_Page(state->doc, subimage);
			if (err != cJB2_Error_OK)
				fz_throw(ctx, FZ_ERROR_GENERIC, "cannot select page: %d", (int) err);
		}

		err = JB2_Document_Get_Property(state->doc, cJB2_Prop_Page_Resolution_X, &state->xres);
		if (err != cJB2_Error_OK)
			fz_throw(ctx, FZ_ERROR_GENERIC, "cannot get page x resolution: %d", (int) err);
		err = JB2_Document_Get_Property(state->doc, cJB2_Prop_Page_Resolution_Y, &state->yres);
		if (err != cJB2_Error_OK)
			fz_throw(ctx, FZ_ERROR_GENERIC, "cannot get page y resolution: %d", (int) err);

		err = JB2_Document_Get_Property(state->doc, cJB2_Prop_Page_Width, &state->width);
		if (err != cJB2_Error_OK)
			fz_throw(ctx, FZ_ERROR_GENERIC, "cannot get page width: %d", (int) err);
		err = JB2_Document_Get_Property(state->doc, cJB2_Prop_Page_Height, &state->height);
		if (err != cJB2_Error_OK)
			fz_throw(ctx, FZ_ERROR_GENERIC, "cannot get page height: %d", (int) err);

		if (!only_metadata)<commit_msg>jbig2: Avoid signedness issues when referring to subimages.<commit_after>#include "mupdf/fitz.h"

#ifdef HAVE_LURATECH

#include <string.h>
#include <ldf_jb2.h>

struct info
{
	fz_context *ctx;
	unsigned long width, height;
	unsigned long xres, yres;
	unsigned long stride;
	unsigned long pages;
	fz_colorspace *cspace;
	JB2_Handle_Document doc;

	const unsigned char *data;
	size_t len;

	unsigned char *output;
};

static void * JB2_Callback
jbig2_alloc(unsigned long size, void *userdata)
{
	struct info *state = userdata;
	return fz_malloc(state->ctx, size);
}

static JB2_Error JB2_Callback
jbig2_free(void *ptr, void *userdata)
{
	struct info *state = userdata;
	fz_free(state->ctx, ptr);
	return cJB2_Error_OK;
}

static void JB2_Callback
jbig2_message(const char *msg, JB2_Message_Level level, void *userdata)
{
	struct info *state = userdata;

	if (msg != NULL && msg[0] != '\0')
		switch (level)
		{
		case cJB2_Message_Information:
#ifndef NDEBUG
			fz_warn(state->ctx, "luratech jbig2 info: %s", msg);
#endif
			break;
		case cJB2_Message_Warning:
			fz_warn(state->ctx, "luratech jbig2 warning: %s", msg);
			break;
		case cJB2_Message_Error:
			fz_warn(state->ctx, "luratech jbig2 error: %s", msg);
			break;
		default:
			fz_warn(state->ctx, "luratech jbig2 message: %s", msg);
			break;
		}
}

static JB2_Size_T JB2_Callback
jbig2_read(unsigned char *buf, JB2_Size_T offset, JB2_Size_T size, void *userdata)
{
	struct info *state = userdata;
	size_t available;

	if (state->len <= offset)
		return 0;
	available = fz_minz(state->len - offset, size);
	memcpy(buf, state->data + offset, available);
	return (JB2_Size_T)available;
}

static JB2_Error JB2_Callback
jbig2_write(unsigned char *buf, unsigned long row, unsigned long width, unsigned long bpp, void *userdata)
{
	struct info *state = userdata;
	int stride = (width + 7) >> 3;
	unsigned char *dp = state->output + row * stride;

	if (row >= state->height)
	{
		fz_warn(state->ctx, "row %lu outside of image", row);
		return cJB2_Error_OK;
	}

	while (stride--)
		*(dp++) = *(buf++) ^ 0xff;

	return cJB2_Error_OK;
}


static fz_pixmap *
jbig2_read_image(fz_context *ctx, struct info *jbig2, const unsigned char *buf, size_t len, int only_metadata, int subimage)
{
	struct info *state = jbig2;
	JB2_Error err;
	JB2_Scaling_Factor scale = {1, 1};
	JB2_Rect rect = {0, 0, 0, 0};
	fz_pixmap *pix = NULL;

	fz_var(pix);

	fz_try(ctx)
	{
		state->ctx = ctx;
		state->data = buf;
		state->len = len;

		err = JB2_Document_Start(&state->doc,
				jbig2_alloc, state,
				jbig2_free, state,
				jbig2_read, state,
				jbig2_message, state);
		if (err != cJB2_Error_OK)
			fz_throw(ctx, FZ_ERROR_GENERIC, "cannot open image: %d", (int) err);

#if defined(JB2_LICENSE_NUM_1) && defined(JB2_LICENSE_NUM_2)
		err = JB2_Document_Set_License(doc, JB2_LICENSE_NUM_1, JB2_LICENSE_NUM_2);
		if (err != cJB2_Error_OK)
			fz_throw(ctx, FZ_ERROR_GENERIC, "cannot set license: %d", (int) err);
#endif

		err = JB2_Document_Get_Property(state->doc, cJB2_Prop_Number_Of_Pages, &state->pages);
		if (err != cJB2_Error_OK)
			fz_throw(ctx, FZ_ERROR_GENERIC, "cannot get number of pages: %d", (int) err);
		if (subimage != -1)
		{
			if (subimage < 0 || (unsigned long) subimage >= state->pages)
				fz_throw(ctx, FZ_ERROR_GENERIC, "page number out of bounds %d vs %ld", subimage, state->pages);
			err = JB2_Document_Set_Page(state->doc, subimage);
			if (err != cJB2_Error_OK)
				fz_throw(ctx, FZ_ERROR_GENERIC, "cannot select page: %d", (int) err);
		}

		err = JB2_Document_Get_Property(state->doc, cJB2_Prop_Page_Resolution_X, &state->xres);
		if (err != cJB2_Error_OK)
			fz_throw(ctx, FZ_ERROR_GENERIC, "cannot get page x resolution: %d", (int) err);
		err = JB2_Document_Get_Property(state->doc, cJB2_Prop_Page_Resolution_Y, &state->yres);
		if (err != cJB2_Error_OK)
			fz_throw(ctx, FZ_ERROR_GENERIC, "cannot get page y resolution: %d", (int) err);

		err = JB2_Document_Get_Property(state->doc, cJB2_Prop_Page_Width, &state->width);
		if (err != cJB2_Error_OK)
			fz_throw(ctx, FZ_ERROR_GENERIC, "cannot get page width: %d", (int) err);
		err = JB2_Document_Get_Property(state->doc, cJB2_Prop_Page_Height, &state->height);
		if (err != cJB2_Error_OK)
			fz_throw(ctx, FZ_ERROR_GENERIC, "cannot get page height: %d", (int) err);

		if (!only_metadata)
{'fraction_non_alphanumeric': 0.12999766191255552, 'fraction_numerical': 0.01928922141688099, 'mean_word_length': 4.815771583956492, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 73, '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': '2099071', 'n_tokens_mistral': 3739, 'n_tokens_neox': 3441, 'n_words': 854}