<commit_before>// SPDX-License-Identifier: GPL-2.1-or-later
/*
 * Copyright (C) 2020 <NAME> <<EMAIL>>
 */

/*

  webp image support using libwepb library.

 */

#include <errno.h>
#include <string.h>

#include "../../config.h"
#include <core/gp_debug.h>
#include <core/gp_get_put_pixel.h>

#include <loaders/gp_io.h>
#include <loaders/gp_loaders.gen.h>

int gp_match_webp(const void *buf)
{
	if (memcmp(buf, "RIFF", 4))
		return 0;

	if (memcmp(buf + 8, "WEBP", 4))
		return 0;

	return 1;
}

#ifdef HAVE_WEBP

#include <webp/decode.h>

static void fill_metadata(gp_storage *storage, int w, int h)
{
	gp_storage_add_int(storage, NULL, "Width", w);
	gp_storage_add_int(storage, NULL, "Height", h);
}

int gp_read_webp_ex(gp_io *io, gp_pixmap **img, gp_storage *storage,
                    gp_progress_cb *callback)
{
	unsigned char buf[1024];
	WebPBitstreamFeatures features;
	gp_pixel_type ptype;
	int err;

	ssize_t ret = gp_io_read(io, buf, sizeof(buf));

	if (ret <= 0) {
		GP_DEBUG(1, "initial read failed");
		errno = EINVAL;
		return 1;
	}

	if (WebPGetFeatures(buf, ret, &features) != VP8_STATUS_OK) {
		GP_DEBUG(1, "Failed to get webp features");
		errno = EINVAL;
		return 1;
	}

	GP_DEBUG(1, "Have webp image %ix%i has_alpha=%i",
		 features.width, features.height, features.has_alpha);

	WebPDecoderConfig config = {};
	if (!WebPInitDecoderConfig(&config)) {
		GP_DEBUG(1, "Failed to initialize decoder");
		errno = EINVAL;
		return 1;
	}

	fill_metadata(storage, features.width, features.height);

	if (!img)
		return 0;<commit_msg>loaders: webp: Fix segfault with NULL metadata storage<commit_after>// SPDX-License-Identifier: GPL-2.1-or-later
/*
 * Copyright (C) 2020 <NAME> <<EMAIL>>
 */

/*

  webp image support using libwepb library.

 */

#include <errno.h>
#include <string.h>

#include "../../config.h"
#include <core/gp_debug.h>
#include <core/gp_get_put_pixel.h>

#include <loaders/gp_io.h>
#include <loaders/gp_loaders.gen.h>

int gp_match_webp(const void *buf)
{
	if (memcmp(buf, "RIFF", 4))
		return 0;

	if (memcmp(buf + 8, "WEBP", 4))
		return 0;

	return 1;
}

#ifdef HAVE_WEBP

#include <webp/decode.h>

static void fill_metadata(gp_storage *storage, int w, int h)
{
	gp_storage_add_int(storage, NULL, "Width", w);
	gp_storage_add_int(storage, NULL, "Height", h);
}

int gp_read_webp_ex(gp_io *io, gp_pixmap **img, gp_storage *storage,
                    gp_progress_cb *callback)
{
	unsigned char buf[1024];
	WebPBitstreamFeatures features;
	gp_pixel_type ptype;
	int err;

	ssize_t ret = gp_io_read(io, buf, sizeof(buf));

	if (ret <= 0) {
		GP_DEBUG(1, "initial read failed");
		errno = EINVAL;
		return 1;
	}

	if (WebPGetFeatures(buf, ret, &features) != VP8_STATUS_OK) {
		GP_DEBUG(1, "Failed to get webp features");
		errno = EINVAL;
		return 1;
	}

	GP_DEBUG(1, "Have webp image %ix%i has_alpha=%i",
		 features.width, features.height, features.has_alpha);

	WebPDecoderConfig config = {};
	if (!WebPInitDecoderConfig(&config)) {
		GP_DEBUG(1, "Failed to initialize decoder");
		errno = EINVAL;
		return 1;
	}

	if (storage)
		fill_metadata(storage, features.width, features.height);

	if (!img)
		return 0;
{'fraction_non_alphanumeric': 0.14812460267005723, 'fraction_numerical': 0.01652892561983471, 'mean_word_length': 4.159016393442623, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 23, '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': '2845410', 'n_tokens_mistral': 1327, 'n_tokens_neox': 1219, 'n_words': 298}