is_json.c (48c779cdecb5f803e5fe5d761987e976ca9609db) | is_json.c (2726a7014867ad7224d09b66836c5d385f0350f4) |
---|---|
1/*- 2 * Copyright (c) 2018 Christos Zoulas 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 18 unchanged lines hidden (view full) --- 27/* 28 * Parse JSON object serialization format (RFC-7159) 29 */ 30 31#ifndef TEST 32#include "file.h" 33 34#ifndef lint | 1/*- 2 * Copyright (c) 2018 Christos Zoulas 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 18 unchanged lines hidden (view full) --- 27/* 28 * Parse JSON object serialization format (RFC-7159) 29 */ 30 31#ifndef TEST 32#include "file.h" 33 34#ifndef lint |
35FILE_RCSID("@(#)$File: is_json.c,v 1.13 2019/03/02 01:08:10 christos Exp $") | 35FILE_RCSID("@(#)$File: is_json.c,v 1.15 2020/06/07 19:05:47 christos Exp $") |
36#endif 37 38#include <string.h> 39#include "magic.h" 40#endif 41 42#ifdef DEBUG 43#include <stdio.h> --- 107 unchanged lines hidden (view full) --- 151 if (!json_isxdigit(*uc++)) 152 goto out; 153 continue; 154 default: 155 goto out; 156 } 157 case '"': 158 *ucp = uc; | 36#endif 37 38#include <string.h> 39#include "magic.h" 40#endif 41 42#ifdef DEBUG 43#include <stdio.h> --- 107 unchanged lines hidden (view full) --- 151 if (!json_isxdigit(*uc++)) 152 goto out; 153 continue; 154 default: 155 goto out; 156 } 157 case '"': 158 *ucp = uc; |
159 DPRINTF("Good string: ", uc, *ucp); |
|
159 return 1; 160 default: 161 continue; 162 } 163 } 164out: 165 DPRINTF("Bad string: ", uc, *ucp); 166 *ucp = uc; 167 return 0; 168} 169 170static int 171json_parse_array(const unsigned char **ucp, const unsigned char *ue, 172 size_t *st, size_t lvl) 173{ 174 const unsigned char *uc = *ucp; | 160 return 1; 161 default: 162 continue; 163 } 164 } 165out: 166 DPRINTF("Bad string: ", uc, *ucp); 167 *ucp = uc; 168 return 0; 169} 170 171static int 172json_parse_array(const unsigned char **ucp, const unsigned char *ue, 173 size_t *st, size_t lvl) 174{ 175 const unsigned char *uc = *ucp; |
175 int more = 0; /* Array has more than 1 element */ | |
176 177 DPRINTF("Parse array: ", uc, *ucp); 178 while (uc < ue) { | 176 177 DPRINTF("Parse array: ", uc, *ucp); 178 while (uc < ue) { |
179 if (*uc == ']') 180 goto done; |
|
179 if (!json_parse(&uc, ue, st, lvl + 1)) 180 goto out; 181 if (uc == ue) 182 goto out; 183 switch (*uc) { 184 case ',': | 181 if (!json_parse(&uc, ue, st, lvl + 1)) 182 goto out; 183 if (uc == ue) 184 goto out; 185 switch (*uc) { 186 case ',': |
185 more++; | |
186 uc++; 187 continue; 188 case ']': | 187 uc++; 188 continue; 189 case ']': |
189 if (more) 190 st[JSON_ARRAYN]++; | 190 done: 191 st[JSON_ARRAYN]++; |
191 *ucp = uc + 1; | 192 *ucp = uc + 1; |
193 DPRINTF("Good array: ", uc, *ucp); |
|
192 return 1; 193 default: 194 goto out; 195 } 196 } 197out: 198 DPRINTF("Bad array: ", uc, *ucp); 199 *ucp = uc; --- 5 unchanged lines hidden (view full) --- 205 size_t *st, size_t lvl) 206{ 207 const unsigned char *uc = *ucp; 208 DPRINTF("Parse object: ", uc, *ucp); 209 while (uc < ue) { 210 uc = json_skip_space(uc, ue); 211 if (uc == ue) 212 goto out; | 194 return 1; 195 default: 196 goto out; 197 } 198 } 199out: 200 DPRINTF("Bad array: ", uc, *ucp); 201 *ucp = uc; --- 5 unchanged lines hidden (view full) --- 207 size_t *st, size_t lvl) 208{ 209 const unsigned char *uc = *ucp; 210 DPRINTF("Parse object: ", uc, *ucp); 211 while (uc < ue) { 212 uc = json_skip_space(uc, ue); 213 if (uc == ue) 214 goto out; |
215 if (*uc == '}') { 216 uc++; 217 goto done; 218 } |
|
213 if (*uc++ != '"') { 214 DPRINTF("not string", uc, *ucp); 215 goto out; 216 } 217 DPRINTF("next field", uc, *ucp); 218 if (!json_parse_string(&uc, ue)) { 219 DPRINTF("not string", uc, *ucp); 220 goto out; --- 10 unchanged lines hidden (view full) --- 231 goto out; 232 } 233 if (uc == ue) 234 goto out; 235 switch (*uc++) { 236 case ',': 237 continue; 238 case '}': /* { */ | 219 if (*uc++ != '"') { 220 DPRINTF("not string", uc, *ucp); 221 goto out; 222 } 223 DPRINTF("next field", uc, *ucp); 224 if (!json_parse_string(&uc, ue)) { 225 DPRINTF("not string", uc, *ucp); 226 goto out; --- 10 unchanged lines hidden (view full) --- 237 goto out; 238 } 239 if (uc == ue) 240 goto out; 241 switch (*uc++) { 242 case ',': 243 continue; 244 case '}': /* { */ |
245 done: |
|
239 *ucp = uc; 240 DPRINTF("Good object: ", uc, *ucp); 241 return 1; 242 default: 243 *ucp = uc - 1; 244 DPRINTF("not more", uc, *ucp); 245 goto out; 246 } --- 216 unchanged lines hidden --- | 246 *ucp = uc; 247 DPRINTF("Good object: ", uc, *ucp); 248 return 1; 249 default: 250 *ucp = uc - 1; 251 DPRINTF("not more", uc, *ucp); 252 goto out; 253 } --- 216 unchanged lines hidden --- |