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

#include "scard.h"
#include "sc_helpers.h"
#include "tlv.h"
#include "tlvs.h"

#ifdef WIN32
static char *pcsc_stringify_error(LONG rv)
{

	static char out[20];
	sprintf_s(out, sizeof(out), "0x%08X", rv);

	return out;
}
#endif

#define CHECK(f, rv) \
	if (SCARD_S_SUCCESS != rv) \
	{ \
		printf(f ": %s\n", pcsc_stringify_error(rv)); \
		return -1; \
	}

static bool print_cb(void *data, const struct tlv_elem_info *tei)
{
	int i, j;

	if (!tei) {
		printf("NULL\n");
		return false;
	}

	if (tei->tag < 0x100)
		printf("Got tag %02hx len %02zx:\n", tei->tag, tei->len);
	else
		printf("Got tag %04hx len %02zx:\n", tei->tag, tei->len);
	for (i = 0; i < tei->len; i += 16) {
		printf("\t%02x:", i);
		for (j = 0; j < 16; j++) {
			if (i + j < tei->len)
				printf(" %02hhx", tei->ptr[i + j]);
			else
				printf("   ");
		}
		printf(" |");
		for (j = 0; j < 16 && i + j < tei->len; j++) {
			printf("%c", (tei->ptr[i+j] >= 0x20 && tei->ptr[i+j] < 0x7f) ? tei->ptr[i+j] : '.' );
		}
		printf("\n");
	}

	return true;
}

static struct tlv *docmd(struct sc *sc,
		unsigned char cla,
		unsigned char ins,
		unsigned char p1,
		unsigned char p2,
		size_t dlen,
		const unsigned char *data)
{
	unsigned short sw;
	size_t outlen;
	unsigned char *outbuf;
	struct tlv *tlv = NULL;

	printf("CMD: %02hhx %02hhx %02hhx %02hhx (%02zx)\n", cla, ins, p1, p2, dlen);
	outbuf = sc_command(sc, cla, ins, p1, p2,
			dlen, data, &sw, &outlen);
	if (scard_is_error(sc)) {
		printf(scard_error(sc));
		return NULL;
	}<commit_msg>split out dump command<commit_after>#include <stdio.h>
#include <stdlib.h>

#include "scard.h"
#include "sc_helpers.h"
#include "tlv.h"
#include "tlvs.h"

#ifdef WIN32
static char *pcsc_stringify_error(LONG rv)
{

	static char out[20];
	sprintf_s(out, sizeof(out), "0x%08X", rv);

	return out;
}
#endif

#define CHECK(f, rv) \
	if (SCARD_S_SUCCESS != rv) \
	{ \
		printf(f ": %s\n", pcsc_stringify_error(rv)); \
		return -1; \
	}

static void dump(const unsigned char *ptr, size_t len)
{
	int i, j;

	for (i = 0; i < len; i += 16) {
		printf("\t%02x:", i);
		for (j = 0; j < 16; j++) {
			if (i + j < len)
				printf(" %02hhx", ptr[i + j]);
			else
				printf("   ");
		}
		printf(" |");
		for (j = 0; j < 16 && i + j < len; j++) {
			printf("%c", (ptr[i+j] >= 0x20 && ptr[i+j] < 0x7f) ? ptr[i+j] : '.' );
		}
		printf("\n");
	}
}

static bool print_cb(void *data, const struct tlv_elem_info *tei)
{
	if (!tei) {
		printf("NULL\n");
		return false;
	}

	if (tei->tag < 0x100)
		printf("Got tag %02hx len %02x:\n", tei->tag, tei->len);
	else
		printf("Got tag %04hx len %02x:\n", tei->tag, tei->len);

	dump(tei->ptr, tei->len);

	return true;
}

static struct tlv *docmd(struct sc *sc,
		unsigned char cla,
		unsigned char ins,
		unsigned char p1,
		unsigned char p2,
		size_t dlen,
		const unsigned char *data)
{
	unsigned short sw;
	size_t outlen;
	unsigned char *outbuf;
	struct tlv *tlv = NULL;

	printf("CMD: %02hhx %02hhx %02hhx %02hhx (%02zx)\n", cla, ins, p1, p2, dlen);
	outbuf = sc_command(sc, cla, ins, p1, p2,
			dlen, data, &sw, &outlen);
	if (scard_is_error(sc)) {
		printf(scard_error(sc));
		return NULL;
	}
{'fraction_non_alphanumeric': 0.19711236660389203, 'fraction_numerical': 0.03389830508474576, 'mean_word_length': 3.3898071625344355, 'pattern_counts': {'":': 2, '<': 21, '<?xml version=': 0, '>': 28, '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': '3159796', 'n_tokens_mistral': 1573, 'n_tokens_neox': 1426, 'n_words': 371}