MCPcopy Create free account
hub / github.com/DeusData/codebase-memory-mcp / utf8_sequence_length

Function utf8_sequence_length

src/mcp/compact_out.c:104–141  ·  view source on GitHub ↗

UTF-8 sequence length starting at p, validating continuation bytes and the * lead-byte ranges (RFC 3629: no overlongs, no surrogates, max U+10FFFF). * `remaining` makes truncated multibyte input safe to inspect. */

Source from the content-addressed store, hash-verified

102 * lead-byte ranges (RFC 3629: no overlongs, no surrogates, max U+10FFFF).
103 * `remaining` makes truncated multibyte input safe to inspect. */
104static size_t utf8_sequence_length(const unsigned char *p, size_t remaining) {
105 if (!p || remaining == 0) {
106 return 0;
107 }
108 unsigned char c = p[0];
109 if (c < 0x80) {
110 return 1;
111 }
112 if (c < 0xC2) {
113 return 0; /* bare continuation byte or overlong lead */
114 }
115 if (c < 0xE0) {
116 return remaining >= 2 && (p[1] & 0xC0) == 0x80 ? 2 : 0;
117 }
118 if (c < 0xF0) {
119 if (remaining < 3) {
120 return 0;
121 }
122 unsigned char lo = c == 0xE0 ? 0xA0 : 0x80;
123 unsigned char hi = c == 0xED ? 0x9F : 0xBF;
124 if (p[1] < lo || p[1] > hi || (p[2] & 0xC0) != 0x80) {
125 return 0;
126 }
127 return 3;
128 }
129 if (c < 0xF5) {
130 if (remaining < 4) {
131 return 0;
132 }
133 unsigned char lo = c == 0xF0 ? 0x90 : 0x80;
134 unsigned char hi = c == 0xF4 ? 0x8F : 0xBF;
135 if (p[1] < lo || p[1] > hi || (p[2] & 0xC0) != 0x80 || (p[3] & 0xC0) != 0x80) {
136 return 0;
137 }
138 return 4;
139 }
140 return 0;
141}
142
143static bool output_text_requires_encoding(const char *data, size_t len) {
144 static const char bytes_prefix[] = "@bytes:";

Callers 5

cbm_output_encode_textFunction · 0.85
needs_quotes_nFunction · 0.85
append_quoted_nFunction · 0.85
json_tree_block_stringFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected