<commit_before>#include "bmfont.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

struct bmfont *create_bmfont(char *data) {
	if (strncmp(data, "BMF\003", 4) != 0) return 0; // Check magic, only binary is supported
	data += 4;

	struct bmfont *font = (struct bmfont *)malloc(sizeof(struct bmfont));
	if (font == 0) return 0;

	while (*data != '\0') {
		char blockType = *data++;
		unsigned int blockSize = ((unsigned) data[3] << 24) | ((unsigned) data[2] << 16) | ((unsigned) data[1] << 8) | (((unsigned char *) data)[0]);
		data += 4; // Skip size

		switch (blockType) {
		case 1:
			data += blockSize; // Skip the information on how the font was generated
			break;
		case 2:
		{
			// Read common block
			unsigned int lineHeight = font->lineHeight = ((int) data[1] << 8) | data[0];
			data += 2;
			unsigned int base = ((int) data[1] << 8) | data[0];
			data += 6; // Skip base, scaleW and scaleH
			unsigned int pages = font->pages = ((int) data[1] << 8) | data[0];
			data += 7;

			// font->glyphs = malloc(sizeof(struct glyph *) * pages);
			// for (unsigned int p = 0; p < font->pages; p++) font->glyphs[p] = malloc(sizeof(struct glyph) * PAGE_SIZE);
			for (unsigned int p = 0; p < PAGES; p++) {
				font->glyphs[p] = malloc(sizeof(struct glyph) * PAGE_SIZE);
			}
			break;
		}
		case 3:
		{
			// Page block
			char **pageNames = font->pageNames = malloc(sizeof(char *) * font->pages);
			for (unsigned int p = 0, n = strlen(data); p < font->pages; p++) {
				pageNames[p] = _strdup(data);
				data += n + 1;
			}
			break;
		}
		case 4:
		{
			// Character block
			int numChars = blockSize / 20;
			for (int c = 0; c < numChars; c++, data += 20) {
				struct glyph glyph;
				// unsigned int id = glyph.id = ((data[3] & 0xFF) << 24) | ((data[2] & 0xFF) << 16) | ((data[1] & 0xFF) << 8) | (data[0] & 0xFF);
				unsigned int id = glyph.id = (unsigned char) data[3] << 24 | (unsigned char) data[2] << 16 | (unsigned char) data[1] << 8 | (unsigned char) data[0];
				glyph.x = ((int) data[5] << 8) | (data[4]);
				glyph.y = ((int) data[7] << 8) | (data[6]);
				glyph.width = ((int) data[9] << 8) | (data[8]);
				glyph.height = ((int) data[11] << 8) | (data[10]);
				glyph.xoffset = ((int) data[13] << 8) | (data[12]);
				glyph.yoffset = ((int) data[15] << 8) | (data[14]);
				glyph.xadvance = ((int) data[17] << 8) | (data[16]);
				glyph.page = data[18];
				glyph.kerning = 0;
				unsigned int chnl = data[19];

				font->glyphs[id / PAGE_SIZE][id & PAGE_SIZE - 1] = glyph;
			}
			break;
		}
		case 5:
		{
			// Kerning pair block
			for (int i = 0; i < blockSize / 10; i++, data += 10) {
				unsigned int first = (unsigned char) data[3] << 24 | (unsigned char) data[2] << 16 | (unsigned char) data[1] << 8 | (unsigned char) data[0],
					second = (unsigned char) data[7] << 24 | (unsigned char) data[6] << 16 | (unsigned char) data[5] << 8 | (unsigned char) data[4];
				int amount = data[9] << 8 | data[8];

				struct glyph *glyph = font->glyphs[first / PAGE_SIZE] + (first & PAGE_SIZE - 1);
				if (glyph->kerning == 0) glyph->kerning = calloc(PAGES, sizeof(char *));

				char *page = glyph->kerning[second >> LOG2_PAGE_SIZE];
				if (page == 0) page = glyph->kerning[second >> LOG2_PAGE_SIZE] = calloc(PAGE_SIZE, 1);
				page[second & PAGE_SIZE - 1] = amount;
			}
			int i = bmfont_get_glyph(font, 'A').kerning['l' >> LOG2_PAGE_SIZE]['l' & PAGE_SIZE - 1];
			break;
		}
		default:
			printf("blockType: %d.\n", blockType);
		}
	}

	return font;
}
<commit_msg>Fixed unsigned/signed comparison.<commit_after>#include "bmfont.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

struct bmfont *create_bmfont(char *data) {
	if (strncmp(data, "BMF\003", 4) != 0) return 0; // Check magic, only binary is supported
	data += 4;

	struct bmfont *font = (struct bmfont *)malloc(sizeof(struct bmfont));
	if (font == 0) return 0;

	while (*data != '\0') {
		char blockType = *data++;
		unsigned int blockSize = ((unsigned) data[3] << 24) | ((unsigned) data[2] << 16) | ((unsigned) data[1] << 8) | (((unsigned char *) data)[0]);
		data += 4; // Skip size

		switch (blockType) {
		case 1:
			data += blockSize; // Skip the information on how the font was generated
			break;
		case 2:
		{
			// Read common block
			unsigned int lineHeight = font->lineHeight = ((int) data[1] << 8) | data[0];
			data += 2;
			unsigned int base = ((int) data[1] << 8) | data[0];
			data += 6; // Skip base, scaleW and scaleH
			unsigned int pages = font->pages = ((int) data[1] << 8) | data[0];
			data += 7;

			// font->glyphs = malloc(sizeof(struct glyph *) * pages);
			// for (unsigned int p = 0; p < font->pages; p++) font->glyphs[p] = malloc(sizeof(struct glyph) * PAGE_SIZE);
			for (unsigned int p = 0; p < PAGES; p++) {
				font->glyphs[p] = malloc(sizeof(struct glyph) * PAGE_SIZE);
			}
			break;
		}
		case 3:
		{
			// Page block
			char **pageNames = font->pageNames = malloc(sizeof(char *) * font->pages);
			for (unsigned int p = 0, n = strlen(data); p < font->pages; p++) {
				pageNames[p] = _strdup(data);
				data += n + 1;
			}
			break;
		}
		case 4:
		{
			// Character block
			int numChars = blockSize / 20;
			for (int c = 0; c < numChars; c++, data += 20) {
				struct glyph glyph;
				// unsigned int id = glyph.id = ((data[3] & 0xFF) << 24) | ((data[2] & 0xFF) << 16) | ((data[1] & 0xFF) << 8) | (data[0] & 0xFF);
				unsigned int id = glyph.id = (unsigned char) data[3] << 24 | (unsigned char) data[2] << 16 | (unsigned char) data[1] << 8 | (unsigned char) data[0];
				glyph.x = ((int) data[5] << 8) | (data[4]);
				glyph.y = ((int) data[7] << 8) | (data[6]);
				glyph.width = ((int) data[9] << 8) | (data[8]);
				glyph.height = ((int) data[11] << 8) | (data[10]);
				glyph.xoffset = ((int) data[13] << 8) | (data[12]);
				glyph.yoffset = ((int) data[15] << 8) | (data[14]);
				glyph.xadvance = ((int) data[17] << 8) | (data[16]);
				glyph.page = data[18];
				glyph.kerning = 0;
				unsigned int chnl = data[19];

				font->glyphs[id / PAGE_SIZE][id & PAGE_SIZE - 1] = glyph;
			}
			break;
		}
		case 5:
		{
			// Kerning pair block
			for (unsigned int i = 0; i < blockSize / 10; i++, data += 10) {
				unsigned int first = (unsigned char) data[3] << 24 | (unsigned char) data[2] << 16 | (unsigned char) data[1] << 8 | (unsigned char) data[0],
					second = (unsigned char) data[7] << 24 | (unsigned char) data[6] << 16 | (unsigned char) data[5] << 8 | (unsigned char) data[4];
				int amount = data[9] << 8 | data[8];

				struct glyph *glyph = font->glyphs[first / PAGE_SIZE] + (first & PAGE_SIZE - 1);
				if (glyph->kerning == 0) glyph->kerning = calloc(PAGES, sizeof(char *));

				char *page = glyph->kerning[second >> LOG2_PAGE_SIZE];
				if (page == 0) page = glyph->kerning[second >> LOG2_PAGE_SIZE] = calloc(PAGE_SIZE, 1);
				page[second & PAGE_SIZE - 1] = amount;
			}
			int i = bmfont_get_glyph(font, 'A').kerning['l' >> LOG2_PAGE_SIZE]['l' & PAGE_SIZE - 1];
			break;
		}
		default:
			printf("blockType: %d.\n", blockType);
		}
	}

	return font;
}

{'fraction_non_alphanumeric': 0.19837768606802333, 'fraction_numerical': 0.039277074142592856, 'mean_word_length': 3.0577367205542725, 'pattern_counts': {'":': 0, '<': 123, '<?xml version=': 0, '>': 51, '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': '4572701', 'n_tokens_mistral': 3172, 'n_tokens_neox': 2585, 'n_words': 751}