int main(int argc, char **argv) { unsigned char buff[1024], *json = NULL; int len, lastlen=0, ret; unsigned short *res; FILE *f; int loops; if((f = fopen(argv[1],"r")) == NULL) { printf("uhoh opening %s\n",argv[1]); exit(1); } while((len = fread(buff,1,1024,f)) > 0) { json = realloc(json,lastlen+len); memcpy(json+lastlen,buff,len); lastlen+=len; } fclose(f); loops = 1000000; while (loops-- > 0) { //printf("got[%.*s]\n",lastlen,json); res = malloc(lastlen); // way more than enough memset(res,0,lastlen); ret = js0n(json,lastlen,res); if (0 && ret) exit(0); free(res); } return 0; }
void moloch_field_define_json(unsigned char *expression, int expression_len, unsigned char *data, int data_len) { MolochFieldInfo_t *info = MOLOCH_TYPE_ALLOC0(MolochFieldInfo_t); int i; uint32_t out[4*100]; // Can have up to 100 elements at any level memset(out, 0, sizeof(out)); if (js0n(data, data_len, out) != 0) { LOG("ERROR: Parse error for >%.*s<\n", data_len, data); fflush(stdout); exit(0); } info->expression = g_strndup((char*)expression, expression_len); for (i = 0; out[i]; i += 4) { if (strncmp("group", (char*)data + out[i], 5) == 0) { info->group = g_strndup((char*)data + out[i+2], out[i+3]); } else if (strncmp("dbField", (char*)data + out[i], 7) == 0) { info->dbFieldMem = info->dbField = g_strndup((char*)data + out[i+2], out[i+3]); info->dbFieldLen = out[i+3]; } } info->pos = -1; HASH_ADD(f_, fields, info->dbField, info); return; }
void leadHQHandle(void) { int rsize; unsigned short index[32]; // <10 keypairs in the incoming json // only continue if new data to read if (!Scout.wifi.client.available()) { return; } // Read a block of data and look for packets while ((rsize = hqIncoming.readClient(Scout.wifi.client, 128))) { int nl; while((nl = hqIncoming.indexOf('\n')) >= 0) { // look for a packet if (hqVerboseOutput) { Serial.print(F("looking for packet in: ")); Serial.println(hqIncoming); } // Parse JSON up to the first newline if (!js0n((const unsigned char*)hqIncoming.c_str(), nl, index, 32)) { leadIncoming(hqIncoming.c_str(), nl, index); } else { if (hqVerboseOutput) { Serial.println(F("JSON parse failed")); } } // Remove up to and including the newline hqIncoming.remove(0, nl + 1); } } }
void moloch_field_define_json(unsigned char *expression, int expression_len, unsigned char *data, int data_len) { MolochFieldInfo_t *info = MOLOCH_TYPE_ALLOC0(MolochFieldInfo_t); int i; uint32_t out[4*100]; // Can have up to 100 elements at any level memset(out, 0, sizeof(out)); if (js0n(data, data_len, out) != 0) { LOGEXIT("ERROR: Parse error for >%.*s<\n", data_len, data); } info->expression = g_strndup((char*)expression, expression_len); for (i = 0; out[i]; i += 4) { if (strncmp("group", (char*)data + out[i], 5) == 0) { info->group = g_strndup((char*)data + out[i+2], out[i+3]); } else if (strncmp("dbField", (char*)data + out[i], 7) == 0) { info->dbFieldFull = info->dbField = g_strndup((char*)data + out[i+2], out[i+3]); info->dbFieldLen = out[i+3]; } else if (strncmp("type", (char*)data + out[i], 4) == 0) { info->kind = g_strndup((char*)data + out[i+2], out[i+3]); } else if (strncmp("category", (char*)data + out[i], 8) == 0) { info->category = g_strndup((char*)data + out[i+2], out[i+3]); } else if (strncmp("disabled", (char*)data + out[i], 8) == 0) { if (strncmp((char *)data + out[i+2], "true", 4) == 0) { info->flags |= MOLOCH_FIELD_FLAG_DISABLED; } } } info->pos = -1; HASH_ADD(d_, fieldsByDb, info->dbField, info); HASH_ADD(e_, fieldsByExp, info->expression, info); return; }
// dumbed down parser, expects null terminated json, if fails the *index is 0 (and safe to pass into j0g_*) char *j0g(char *json, unsigned short *index, int ilen) { int ret, len; if(!json) return NULL; len = strlen(json); ret = js0n((unsigned char*)json, len, index, ilen); if(ret) *index = 0; return json; }
SDB_IPI Rangstr json_find (const char *s, Rangstr *rs) { #define RESFIXSZ 1024 RangstrType resfix[RESFIXSZ] = {0}; RangstrType *res = resfix; int i, j, n, len, ret; Rangstr rsn; if (!s) { return rangstr_null (); } len = strlen (s); if (len > RESFIXSZ) { res = calloc (len + 1, sizeof (RangstrType)); if (!res) { eprintf ("Cannot allocate %d byte(s)\n", len + 1); return rangstr_null (); } } ret = js0n ((const unsigned char *)s, len, res); #define PFREE(x) if (x && x != resfix) free (x) if (ret > 0) { PFREE (res); return rangstr_null (); } if (*s == '[') { n = rangstr_int (rs); if (n < 0) { goto beach; } for (i = j = 0; res[i] && j < n; i += 2, j++); if (!res[i]) { goto beach; } rsn = rangstr_news (s, res, i); PFREE (res); return rsn; } else { for (i=0; res[i]; i+=4) { Rangstr rsn = rangstr_news (s, res, i); if (!rangstr_cmp (rs, &rsn)) { rsn = rangstr_news (s, res, i+2); PFREE (res); return rsn; } } } beach: PFREE (res); return rangstr_null (); }
char *report2json(char *in) { char *keys, *vals, report[100]; unsigned short ir[16], ik[32], iv[32], i; // copy cuz we edit it memcpy(report, in, 100); // parse this and humanize js0n((unsigned char*)report, strlen(report), ir, 16); if (!*ir) { return NULL; } sprintf(reportJson, "{\"type\":\"%s\"", keyGet(atoi(j0g_safe(0, report, ir)))); keys = report + ir[2]; js0n((unsigned char*)keys, ir[3], ik, 32); if (!*ik) { return NULL; } vals = report+ir[4]; js0n((unsigned char*)vals, ir[5], iv, 32); if (!*iv) { return NULL; } for (i=0; ik[i]; i+=2) { sprintf(reportJson + strlen(reportJson), ",\"%s\":", keyGet(atoi(j0g_safe(i, keys, ik)))); if (vals[iv[i]-1] == '"') { iv[i]--; iv[i+1]+=2; } *(vals+iv[i]+iv[i+1]) = 0; sprintf(reportJson + strlen(reportJson), "%s", vals + iv[i]); } sprintf(reportJson + strlen(reportJson), "}"); return reportJson; }
Rangstr json_find (const char *s, Rangstr *rs) { #define RESFIXSZ 1024 RangstrType resfix[RESFIXSZ], *res = NULL; int i, j, n, len, ret; Rangstr rsn; if (!s) return rangstr_null (); len = strlen (s); res = (len<RESFIXSZ)? resfix: malloc (sizeof (RangstrType)* (len+1)); if (!res) { eprintf ("Cannot allocate %d bytes\n", len+1); return rangstr_null (); } for (i=0; i<len; i++) res[i] = 0; ret = js0n ((const unsigned char *)s, len, res); #define PFREE(x) if (x&&x!=resfix) free (x) if (ret>0) { PFREE (res); return rangstr_null (); } if (*s=='[') { n = rangstr_int (rs); n++; if (n<0) goto beach; for (i=j=0; res[i] && j<n; i+=2, j++); if (j<n) goto beach; rsn = rangstr_news (s, res, i-2); PFREE (res); return rsn; } else { for (i=0; res[i]; i+=4) { Rangstr rsn = rangstr_news (s, res, i); if (!rangstr_cmp (rs, &rsn)) { rsn = rangstr_news (s, res, i+2); PFREE (res); return rsn; } } } beach: PFREE (res); return rangstr_null (); }
path_t path_parse(char *json, int len) { unsigned short js[64]; path_t p; if(!json) return NULL; if(!len) len = strlen(json); js0n((unsigned char*)json, len, js, 64); if(!j0g_val("type",json,js)) return NULL; p = path_new(j0g_str("type",json,js)); // just try to set all possible attributes path_ip(p, j0g_str("ip",json,js)); if(j0g_str("port",json,js)) path_port(p, (uint16_t)strtol(j0g_str("port",json,js),NULL,10)); path_id(p, j0g_str("id",json,js)); path_http(p, j0g_str("http",json,js)); return p; }
int json_foreach(const char *s, JSONCallback cb UNUSED) { int i, len, ret; unsigned short *res = NULL; len = strlen (s); res = malloc (len); ret = js0n ((const unsigned char *)s, len, res); if (!ret) return 0; if (*s=='[') { for (i=0; res[i]; i+=2) { printf ("%d %.*s\n", i, res[i+1], s+res[i]); } } else { for (i=0; res[i]; i+=4) { printf ("%.*s = ", res[i+1], s+res[i]); printf ("%.*s\n", res[i+3], s+res[i+2]); } } return 1; }
int packet_json(packet_t p, unsigned char *json, unsigned short len) { uint16_t nlen; void *ptr; if(!p) return 1; if(len >= 2 && js0n(json,len,p->js,JSONDENSITY)) return 1; // new space and update pointers if(!(ptr = realloc(p->raw,2+len+p->body_len))) return 1; p->raw = (unsigned char *)ptr; p->json = p->raw+2; p->body = p->raw+(2+len); // move the body forward to make space memmove(p->body,p->raw+(2+p->json_len),p->body_len); // copy in new json memcpy(p->json,json,len); p->json_len = len; nlen = platform_short(len); memcpy(p->raw,&nlen,2); free(p->jsoncp); p->jsoncp = NULL; return 0; }
SDB_IPI int json_walk (const char *s) { RangstrType *res; int i, ret, len = strlen (s); res = malloc (len+1); ret = js0n ((const unsigned char *)s, len, res); if (!ret) { free (res); return 0; } if (*s=='[' || *s=='{') { for (i=0; res[i]; i+=2) { printf ("%d %.*s\n", i, res[i+1], s+res[i]); } } else { for (i=0; res[i]; i+=4) { printf ("%.*s = ", res[i+1], s+res[i]); printf ("%.*s\n", res[i+3], s+res[i+2]); } } free (res); return 1; }
unsigned char *moloch_js0n_get(unsigned char *data, uint32_t len, char *key, uint32_t *olen) { uint32_t key_len = strlen(key); int i; uint32_t out[4*100]; // Can have up to 100 elements at any level memset(out, 0, sizeof(out)); *olen = 0; if (js0n(data, len, out) != 0) { LOG("ERROR: Parse error for >%s< in >%.*s<\n", key, len, data); fflush(stdout); return 0; } for (i = 0; out[i]; i+= 4) { if (out[i+1] == key_len && memcmp(key, data + out[i], key_len) == 0) { *olen = out[i+3]; return data + out[i+2]; } } return 0; }
int parse_bitcoin_block(unsigned char* json, size_t size, block_header* block) { unsigned short* res = calloc(size, sizeof(unsigned short)); if (res == NULL) { return -1; } int ret = js0n(json, size, res, size); enum block_tokens token = UNKNOWN; uint32 value; int i =0, processed = 0; while (processed < 7 & res[i] != 0) { int s = res[i++]; int l = res[i++]; token = parse_token(json, s, l); s = res[i++]; l = res[i++]; if (is_valid_token(token)) { char* str = (char*)(json + s); str[l] ='\0'; // LAMb // printf("str=%s\n", str); switch(token) { case HASH: break; case VERSION: value = parse_int(str); REVERSE_32(block->version, value); break; case PREV_BLOCK: hex_to_bytes_rev(str, l, block->prev_block, 256); break; case MRKL_ROOT: hex_to_bytes_rev(str, l, block->mrkl_root, 256); break; case SECS: value = parse_int(str); REVERSE_32(block->time, value); break; case BITS: value = parse_int(str); REVERSE_32(block->bits, value); break; case NONCE: value = parse_int(str); REVERSE_32(block->nonce, value); break; default: break; } processed++; } } free(res); return 0; }