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

Function cbm_output_encode_text

src/mcp/compact_out.c:166–220  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

164}
165
166bool cbm_output_encode_text(const char *data, size_t len, char **encoded, size_t *encoded_len) {
167 static const char bytes_prefix[] = "@bytes:";
168 static const char utf8_prefix[] = "@utf8:";
169 static const char hex[] = "0123456789abcdef";
170 if (!encoded || !encoded_len || (!data && len != 0)) {
171 return false;
172 }
173 *encoded = NULL;
174 *encoded_len = len;
175 if (!output_text_requires_encoding(data, len)) {
176 return true;
177 }
178
179 bool valid_utf8 = true;
180 size_t offset = 0;
181 while (valid_utf8 && offset < len) {
182 size_t sequence = utf8_sequence_length((const unsigned char *)data + offset, len - offset);
183 if (!sequence) {
184 valid_utf8 = false;
185 break;
186 }
187 offset += sequence;
188 }
189
190 size_t prefix_len = valid_utf8 ? sizeof(utf8_prefix) - 1 : sizeof(bytes_prefix) - 1;
191 size_t payload_len = len;
192 if (!valid_utf8) {
193 if (len > ((size_t)-1 - prefix_len - 1) / 2) {
194 return false;
195 }
196 payload_len = len * 2;
197 } else if (len > (size_t)-1 - prefix_len - 1) {
198 return false;
199 }
200 char *out = (char *)malloc(prefix_len + payload_len + 1);
201 if (!out) {
202 return false;
203 }
204 memcpy(out, valid_utf8 ? utf8_prefix : bytes_prefix, prefix_len);
205 if (valid_utf8) {
206 if (len > 0) {
207 memcpy(out + prefix_len, data, len);
208 }
209 } else {
210 for (size_t i = 0; i < len; i++) {
211 unsigned char byte = (unsigned char)data[i];
212 out[prefix_len + i * 2] = hex[byte >> 4];
213 out[prefix_len + i * 2 + 1] = hex[byte & 0x0f];
214 }
215 }
216 *encoded_len = prefix_len + payload_len;
217 out[*encoded_len] = '\0';
218 *encoded = out;
219 return true;
220}
221
222static bool needs_quotes_n(const char *s, size_t len) {
223 if (!s || len == 0) {

Callers 6

cbm_jsonrpc_format_errorFunction · 0.85
cbm_mcp_text_resultFunction · 0.85
append_value_nFunction · 0.85
append_key_nFunction · 0.85
test_mcp.cFile · 0.85

Calls 2

utf8_sequence_lengthFunction · 0.85

Tested by

no test coverage detected