<commit_before>#include "ruby.h"
#ifdef HAVE_RUBY_DIGEST_H
#include "ruby/digest.h"
#else
#include "digest.h"
#endif
#include "KeccakNISTInterface.h"

#define MAX_DIGEST_SIZE 64
#define DEFAULT_DIGEST_LEN 512

static void sha3_init_func(hashState *ctx);
static void sha3_update_func(hashState *ctx, unsigned char *str, size_t len);

static rb_digest_metadata_t sha3 = {
	RUBY_DIGEST_API_VERSION,
	DEFAULT_DIGEST_LEN,
	KeccakPermutationSize - (2 * DEFAULT_DIGEST_LEN),
	sizeof(hashState),
	(rb_digest_hash_init_func_t)sha3_init_func,
	(rb_digest_hash_update_func_t)sha3_update_func,
	NULL,
};

static void
sha3_init(hashState *ctx, size_t bitlen) {
	switch (Init(ctx, bitlen)) {
	case SUCCESS:
		return;
	case FAIL:
		rb_raise(rb_eRuntimeError, "Unknown error");
	case BAD_HASHLEN:
		rb_raise(rb_eRuntimeError, "Bad hash length (must be 0, 224, 256, 384 or 512)");
	default:
		rb_raise(rb_eRuntimeError, "Unknown error code");
	}
}

static void
sha3_init_func(hashState *ctx) {
	Init(ctx, ctx->capacity / 2);
}

static void
sha3_update_func(hashState *ctx, unsigned char *str, size_t len) {
	Update(ctx, str, len * 8);
}

static VALUE
rb_sha3_alloc(VALUE klass) {
	hashState *ctx;

	ctx = (hashState *) xmalloc(sizeof(hashState));
	sha3_init(ctx, DEFAULT_DIGEST_LEN);
	return Data_Wrap_Struct(klass, 0, xfree, ctx);
}

static VALUE
rb_sha3_initialize(int argc, VALUE *argv, VALUE self) {
	hashState *ctx;
	VALUE hashlen;
	int i_hashlen;

	if (rb_scan_args(argc, argv, "01", &hashlen) == 0) {
		i_hashlen = DEFAULT_DIGEST_LEN;
	} else {
		i_hashlen = NUM2INT(hashlen);
	}
	switch (i_hashlen) {
	case 0:
		rb_raise(rb_eRuntimeError, "Unsupported hash length");
	case DEFAULT_DIGEST_LEN:
		break;
	default:<commit_msg>Raise ArgumentError instead of RuntimeError on bad hash length.<commit_after>#include "ruby.h"
#ifdef HAVE_RUBY_DIGEST_H
#include "ruby/digest.h"
#else
#include "digest.h"
#endif
#include "KeccakNISTInterface.h"

#define MAX_DIGEST_SIZE 64
#define DEFAULT_DIGEST_LEN 512

static void sha3_init_func(hashState *ctx);
static void sha3_update_func(hashState *ctx, unsigned char *str, size_t len);

static rb_digest_metadata_t sha3 = {
	RUBY_DIGEST_API_VERSION,
	DEFAULT_DIGEST_LEN,
	KeccakPermutationSize - (2 * DEFAULT_DIGEST_LEN),
	sizeof(hashState),
	(rb_digest_hash_init_func_t)sha3_init_func,
	(rb_digest_hash_update_func_t)sha3_update_func,
	NULL,
};

static void
sha3_init(hashState *ctx, size_t bitlen) {
	switch (Init(ctx, bitlen)) {
	case SUCCESS:
		return;
	case FAIL:
		rb_raise(rb_eRuntimeError, "Unknown error");
	case BAD_HASHLEN:
		rb_raise(rb_eArgError, "Bad hash length (must be 0, 224, 256, 384 or 512)");
	default:
		rb_raise(rb_eRuntimeError, "Unknown error code");
	}
}

static void
sha3_init_func(hashState *ctx) {
	Init(ctx, ctx->capacity / 2);
}

static void
sha3_update_func(hashState *ctx, unsigned char *str, size_t len) {
	Update(ctx, str, len * 8);
}

static VALUE
rb_sha3_alloc(VALUE klass) {
	hashState *ctx;

	ctx = (hashState *) xmalloc(sizeof(hashState));
	sha3_init(ctx, DEFAULT_DIGEST_LEN);
	return Data_Wrap_Struct(klass, 0, xfree, ctx);
}

static VALUE
rb_sha3_initialize(int argc, VALUE *argv, VALUE self) {
	hashState *ctx;
	VALUE hashlen;
	int i_hashlen;

	if (rb_scan_args(argc, argv, "01", &hashlen) == 0) {
		i_hashlen = DEFAULT_DIGEST_LEN;
	} else {
		i_hashlen = NUM2INT(hashlen);
	}
	switch (i_hashlen) {
	case 0:
		rb_raise(rb_eArgError, "Unsupported hash length");
	case DEFAULT_DIGEST_LEN:
		break;
	default:
{'fraction_non_alphanumeric': 0.11210374639769452, 'fraction_numerical': 0.02190201729106628, 'mean_word_length': 6.026315789473684, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '63561', 'n_tokens_mistral': 1474, 'n_tokens_neox': 1373, 'n_words': 311}