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

Function extract_json_field

src/cli/progress_sink.c:95–154  ·  view source on GitHub ↗

Extract one string field from the logger's compact JSON format. Keys are * accepted only at object boundaries so worker-controlled values cannot spoof * a progress event by merely containing a key-shaped substring. */

Source from the content-addressed store, hash-verified

93 * accepted only at object boundaries so worker-controlled values cannot spoof
94 * a progress event by merely containing a key-shaped substring. */
95static const char *extract_json_field(const char *line, const char *key, char *buf, int buf_len) {
96 char needle[CBM_SZ_64];
97 int needle_len = snprintf(needle, sizeof(needle), "\"%s\":", key);
98 if (needle_len <= 0 || needle_len >= (int)sizeof(needle)) {
99 return NULL;
100 }
101 const char *candidate = line;
102 while ((candidate = strstr(candidate, needle)) != NULL) {
103 const char *before = candidate;
104 while (before > line && (before[-1] == ' ' || before[-1] == '\t')) {
105 before--;
106 }
107 if (before == line || (before[-1] != '{' && before[-1] != ',')) {
108 candidate += needle_len;
109 continue;
110 }
111 const char *value = candidate + needle_len;
112 while (*value == ' ' || *value == '\t') {
113 value++;
114 }
115 if (*value++ != '\"') {
116 return NULL;
117 }
118 int offset = 0;
119 bool escaped = false;
120 while (*value && offset < buf_len - 1) {
121 if (!escaped && *value == '\"') {
122 buf[offset] = '\0';
123 return buf;
124 }
125 if (!escaped && *value == '\\') {
126 escaped = true;
127 value++;
128 continue;
129 }
130 if (escaped) {
131 switch (*value) {
132 case 'n':
133 buf[offset++] = '\n';
134 break;
135 case 'r':
136 buf[offset++] = '\r';
137 break;
138 case 't':
139 buf[offset++] = '\t';
140 break;
141 default:
142 buf[offset++] = *value;
143 break;
144 }
145 escaped = false;
146 } else {
147 buf[offset++] = *value;
148 }
149 value++;
150 }
151 return NULL;
152 }

Callers 1

extract_kvFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected