SofaBuffers C/C++ 0.12.0
API Documentation
Loading...
Searching...
No Matches
utf8.h
Go to the documentation of this file.
1/*!
2 * @file utf8.h
3 * @brief SofaBuffers C - Hand-written UTF-8 validator (SOFAB_STRICT_UTF8).
4 *
5 * A `string` payload is UTF-8 (MESSAGE_SPEC §8); this module provides the
6 * validator the corelib uses to enforce that on the encode and decode paths when
7 * @ref SOFAB_STRICT_UTF8 is enabled. This is a footprint corelib, so the check
8 * @b defaults @b OFF (CORELIB_PLAN §6.4's constrained-profile allowance); opt in
9 * with @c SOFAB_ENABLE_STRICT_UTF8 (or @c -DSOFAB_STRICT_UTF8=1). It is a
10 * security surface — a real validator, not a byte-range shortcut — and is
11 * declared (and compiled) only when the check is on, so the default build pays
12 * zero @c .text / @c .rodata cost (CORELIB_PLAN §6.4).
13 *
14 * SPDX-License-Identifier: MIT
15 */
16
17#ifndef SOFAB_UTF8_H
18#define SOFAB_UTF8_H
19
20/**
21 * @defgroup c_api C API
22 * @{
23 */
24
25#ifdef __cplusplus
26extern "C" {
27#endif
28
29/* includes *******************************************************************/
30#include <stddef.h>
31#include <stdint.h>
32#include <stdbool.h>
33
34#include "sofab/sofab.h"
35
36/* prototypes *****************************************************************/
37
38#if SOFAB_STRICT_UTF8
39/*!
40 * @brief Test whether @p len bytes at @p data are a well-formed UTF-8 string.
41 *
42 * A complete, self-contained validator (not incremental): it accepts exactly the
43 * byte sequences that are valid UTF-8 for the whole @p data range. It is a real
44 * validator, not a lead-byte-range shortcut, and @b rejects (returns @c false):
45 * - @b overlong encodings — any sequence whose code point could have been
46 * encoded in fewer bytes, including the 2-byte @c C0 @c 80 (Java "Modified
47 * UTF-8" NUL) and the overlong 3-/4-byte forms;
48 * - @b surrogate code points @c U+D800 – @c U+DFFF (never valid in UTF-8);
49 * - code points @b above @c U+10FFFF (out of Unicode range);
50 * - bare continuation bytes (@c 0x80 – @c 0xBF with no lead), invalid lead
51 * bytes (@c 0xC0, @c 0xC1, @c 0xF5 – @c 0xFF), and any multi-byte sequence
52 * @b truncated within @p data (a lead byte whose continuation bytes run past
53 * @p len).
54 *
55 * Embedded @c U+0000 — a single @c 0x00 byte — is @b valid UTF-8 and accepted;
56 * only its overlong form @c C0 @c 80 is rejected.
57 *
58 * The caller owns cross-chunk framing: a sequence merely split at a chunk
59 * boundary must be reassembled (or validated once the declared payload is
60 * complete) before calling this, so a well-formed prefix is not mistaken for an
61 * error (CORELIB_PLAN §5.2 / §6.4).
62 *
63 * @param data Pointer to the bytes to validate (may be @c NULL only if @p len
64 * is 0).
65 * @param len Number of bytes at @p data.
66 * @return @c true if @p data is valid UTF-8 (an empty range is valid), @c false
67 * otherwise.
68 */
69extern bool sofab_utf8_valid(const uint8_t *data, size_t len);
70#endif /* SOFAB_STRICT_UTF8 */
71
72#ifdef __cplusplus
73}
74#endif
75
76/** @} */ // end of defgroup
77
78#endif /* SOFAB_UTF8_H */
SofaBuffers C - Type definitions and constants.