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

Function extract_json_field

src/cli/progress_sink.c:159–218  ·  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

157 * accepted only at object boundaries so worker-controlled values cannot spoof
158 * a progress event by merely containing a key-shaped substring. */
159static const char *extract_json_field(const char *line, const char *key, char *buf, int buf_len) {
160 char needle[CBM_SZ_64];
161 int needle_len = snprintf(needle, sizeof(needle), "\"%s\":", key);
162 if (needle_len <= 0 || needle_len >= (int)sizeof(needle)) {
163 return NULL;
164 }
165 const char *candidate = line;
166 while ((candidate = strstr(candidate, needle)) != NULL) {
167 const char *before = candidate;
168 while (before > line && (before[-1] == ' ' || before[-1] == '\t')) {
169 before--;
170 }
171 if (before == line || (before[-1] != '{' && before[-1] != ',')) {
172 candidate += needle_len;
173 continue;
174 }
175 const char *value = candidate + needle_len;
176 while (*value == ' ' || *value == '\t') {
177 value++;
178 }
179 if (*value++ != '\"') {
180 return NULL;
181 }
182 int offset = 0;
183 bool escaped = false;
184 while (*value && offset < buf_len - 1) {
185 if (!escaped && *value == '\"') {
186 buf[offset] = '\0';
187 return buf;
188 }
189 if (!escaped && *value == '\\') {
190 escaped = true;
191 value++;
192 continue;
193 }
194 if (escaped) {
195 switch (*value) {
196 case 'n':
197 buf[offset++] = '\n';
198 break;
199 case 'r':
200 buf[offset++] = '\r';
201 break;
202 case 't':
203 buf[offset++] = '\t';
204 break;
205 default:
206 buf[offset++] = *value;
207 break;
208 }
209 escaped = false;
210 } else {
211 buf[offset++] = *value;
212 }
213 value++;
214 }
215 return NULL;
216 }

Callers 1

extract_kvFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected