From c7d545c7bd522853b2e56b383a20ffb7cd2cf213 Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Sat, 05 Apr 2025 19:50:05 +0800 Subject: [PATCH] git2d: Remove UTF-8 checks --- git2d/bare.c | 48 +----------------------------------------------- git2d/bare.h | 2 ++ git2d/utf8.c | 8 -------- git2d/utf8.h | 74 ----------------------------------------------------- diff --git a/git2d/bare.c b/git2d/bare.c index c54ed33b462dc618338f6f58a2f04c7d98776884..a349e91fddb9081da70c6b4f026db0f71273029c 100644 --- a/git2d/bare.c +++ b/git2d/bare.c @@ -7,7 +7,6 @@ #include #include #include "bare.h" -#include "utf8.h" #define UNUSED(x) (void)(x) @@ -18,41 +17,6 @@ U32SZ = 4, U64SZ = 8, MAXVARINTSZ = 10, }; - -static bool -checkstr(const char *x, uint64_t sz) -{ - if (x == NULL || sz == 0) { - return true; - } - - int err = 0; - uint32_t cp = 0; - char *buf = (void *)x; - uint64_t chunk = 4; - char *pad = (char *)(char[4]){0, 0, 0, 0}; - -#define _utf8_decode(buf) \ - do { \ - buf = utf8_decode(buf, &cp, &err); \ - if (err > 0) { \ - return false; \ - } \ - } while (0) - - for (; sz >= chunk; sz -= chunk) { - _utf8_decode(buf); - } - - if (sz > 0) { - memcpy(pad, buf, sz); - _utf8_decode(pad); - } - -#undef _utf8_decode - - return true; -} bare_error bare_put_uint(struct bare_writer *ctx, uint64_t x) @@ -363,21 +327,11 @@ bare_error bare_put_str(struct bare_writer *ctx, const char *src, uint64_t sz) { - if (!checkstr(src, sz)) { - return BARE_ERROR_INVALID_UTF8; - } - return bare_put_data(ctx, (uint8_t *)src, sz); } bare_error bare_get_str(struct bare_reader *ctx, char *dst, uint64_t sz) { - bare_error err = bare_get_data(ctx, (uint8_t *)dst, sz);\ - - if (err == BARE_ERROR_NONE) { - err = !checkstr(dst, sz) ? BARE_ERROR_INVALID_UTF8 : err; - } - - return err; + return bare_get_data(ctx, (uint8_t *)dst, sz); } diff --git a/git2d/bare.h b/git2d/bare.h index d494b186a04aeb130bc93c0babe79d388e5ae8ed..e813464f90c8e69546962dd97d56c3376b3c02c1 100644 --- a/git2d/bare.h +++ b/git2d/bare.h @@ -67,4 +67,6 @@ bare_error bare_get_data(struct bare_reader *ctx, uint8_t *dst, uint64_t sz); bare_error bare_put_str(struct bare_writer *ctx, const char *src, uint64_t sz); bare_error bare_get_str(struct bare_reader *ctx, char *dst, uint64_t sz); +/* Note that the _str implementation here does not check for UTF-8 validity. */ + #endif /* BARE_H */ diff --git a/git2d/utf8.c b/git2d/utf8.c deleted file mode 100644 index 27c7a22f5379ec325d4ad3c012c1e14dcfd27a69..0000000000000000000000000000000000000000 --- a/git2d/utf8.c +++ /dev/null @@ -1,8 +0,0 @@ -/*- - * SPDX-License-Identifier: AGPL-3.0-only - * SPDX-FileCopyrightText: Copyright (c) 2025 Runxi Yu - */ - -#include "utf8.h" - -extern inline void *utf8_decode(void *buf, uint32_t *c, int *e); diff --git a/git2d/utf8.h b/git2d/utf8.h deleted file mode 100644 index 56c6285040a9857fcc749228de386673009f1ee1..0000000000000000000000000000000000000000 --- a/git2d/utf8.h +++ /dev/null @@ -1,74 +0,0 @@ -/*- - * SPDX-License-Identifier: Unlicense - * SPDX-FileContributor: Chris Wellons - * - * From: https://nullprogram.com/blog/2017/10/06/ - */ - -#ifndef UTF8_H -#define UTF8_H - -#include - -/* - * Decode the next character, C, from BUF, reporting errors in E. - * - * Since this is a branchless decoder, four bytes will be read from the - * buffer regardless of the actual length of the next character. This - * means the buffer _must_ have at least three bytes of zero padding - * following the end of the data stream. - * - * Errors are reported in E, which will be non-zero if the parsed - * character was somehow invalid: invalid byte sequence, non-canonical - * encoding, or a surrogate half. - * - * The function returns a pointer to the next character. When an error - * occurs, this pointer will be a guess that depends on the particular - * error, but it will always advance at least one byte. - */ -inline void * -utf8_decode(void *buf, uint32_t *c, int *e) -{ - static const char lengths[] = { - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 3, 3, 4, 0 - }; - static const int masks[] = {0x00, 0x7f, 0x1f, 0x0f, 0x07}; - static const uint32_t mins[] = {4194304, 0, 128, 2048, 65536}; - static const int shiftc[] = {0, 18, 12, 6, 0}; - static const int shifte[] = {0, 6, 4, 2, 0}; - - uint8_t *s = buf; - int len = lengths[s[0] >> 3]; - - /* - * Compute the pointer to the next character early so that the next - * iteration can start working on the next character. Neither Clang - * nor GCC figure out this reordering on their own. - */ - uint8_t *next = s + len + !len; - - /* - * Assume a four-byte character and load four bytes. Unused bits are - * shifted out. - */ - *c = (uint32_t)(s[0] & masks[len]) << 18; - *c |= (uint32_t)(s[1] & 0x3f) << 12; - *c |= (uint32_t)(s[2] & 0x3f) << 6; - *c |= (uint32_t)(s[3] & 0x3f) << 0; - *c >>= shiftc[len]; - - /* Accumulate the various error conditions. */ - *e = (*c < mins[len]) << 6; /* non-canonical encoding */ - *e |= ((*c >> 11) == 0x1b) << 7; /* surrogate half? */ - *e |= (*c > 0x10FFFF) << 8; /* out of range? */ - *e |= (s[1] & 0xc0) >> 2; - *e |= (s[2] & 0xc0) >> 4; - *e |= (s[3] ) >> 6; - *e ^= 0x2a; /* top two bits of each tail byte correct? */ - *e >>= shifte[len]; - - return next; -} - -#endif -- 2.48.1