Skip to content

Commit

Permalink
avoid memcmp at parsing json: inc/json_parser.h
Browse files Browse the repository at this point in the history
	modified:   src/ipapi_json.c
	modified:   src/json_parser.c
  • Loading branch information
ckevar committed Jun 20, 2024
1 parent 81258b2 commit c46dc79
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 47 deletions.
Binary file modified c_mX.bin
Binary file not shown.
8 changes: 5 additions & 3 deletions inc/json_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
#ifndef JSON_PARSER_H
#define JSON_PARSER_H

#include <stdint.h>

char *json_get_value_ptr(char *json, char *key, unsigned short key_len);
unsigned char json_query_mulKey_ValPtrLen(char *json, char n, char **key,\
unsigned short *key_len,\
char **val, unsigned short *val_len);
unsigned char json_query_mulKey_ValPtrLen(char *json, int16_t n,
char **key, uint16_t *key_len,
char **val, uint16_t *val_len);
#endif
22 changes: 11 additions & 11 deletions src/ipapi_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@
#include <string.h>

char IPAPI_get_location_PtrLen(char *json, char **vals, unsigned short *vals_len) {
char *keys[IPAPI_JSON_MEMBERS] = {IPAPI_JSON_LON, IPAPI_JSON_LAT, IPAPI_JSON_CITY};
unsigned short len[IPAPI_JSON_MEMBERS] = {IPAPI_JSON_LON_LEN,\
IPAPI_JSON_LAT_LEN,\
IPAPI_JSON_CITY_LEN};
char *keys[IPAPI_JSON_MEMBERS] = {IPAPI_JSON_LON,
IPAPI_JSON_LAT,
IPAPI_JSON_CITY};
unsigned short len[IPAPI_JSON_MEMBERS] = {IPAPI_JSON_LON_LEN,
IPAPI_JSON_LAT_LEN,
IPAPI_JSON_CITY_LEN};

vals[0] = json_get_value_ptr(json, IPAPI_JSON_STATUS, IPAPI_JSON_STATUS_LEN);

if (memcmp(IPAPI_JSON_SUCCESS, vals[0], IPAPI_JSON_SUCCESS_LEN) == 0)
{
json_query_mulKey_ValPtrLen(json, IPAPI_JSON_MEMBERS, keys,\
len, vals, vals_len);
return 0;
}
if (memcmp(IPAPI_JSON_SUCCESS, vals[0], IPAPI_JSON_SUCCESS_LEN))
return 1;

return 1;
json_query_mulKey_ValPtrLen(json, IPAPI_JSON_MEMBERS, keys,\
len, vals, vals_len);
return 0;
}
80 changes: 47 additions & 33 deletions src/json_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,29 @@
/*********************************************************************
* This replies back the pointer in the json buffer where the value of
* the key is stored. This is rudimentary, we're not checking parenthesis
* or square brackets.
* or square brackets. Inspired by strtok
*********************************************************************/
char *json_get_value_ptr(char *json, char *key, unsigned short key_len) {
while (*json) {
// Getting the Fragment of interest
register char *c;
register char *sholder;

goto_FUNC_json_get_value_ptr_FILE_json_parser:
for (c = key; *json; json++) {
if (*json == '"' && *(json + key_len + 1) == '"') {
// There's a risk that the json + key_len + 1 equals is
// different than '"'
json++;

if (memcmp(json, key, key_len) == 0)
return json + key_len + 2;

json += key_len - 1;
sholder = ++json;

while((*json++ == *c++) && (*c != 0));

if (*c != 0) {
json = sholder + key_len + 1;
goto goto_FUNC_json_get_value_ptr_FILE_json_parser;
}

return json + 2;

}
json++;
}

return NULL;
}

Expand All @@ -37,34 +43,42 @@ char *json_get_value_ptr(char *json, char *key, unsigned short key_len) {
* It replies back where the values of the keys in vals
* it returns the number of missing fields
******************************************************************/
unsigned char json_query_mulKey_ValPtrLen(char *json, char n,\
char **key, unsigned short *key_len,\
char **val, unsigned short *val_len)
unsigned char json_query_mulKey_ValPtrLen(char *json, int16_t n,\
char **key, uint16_t *key_len,\
char **val, uint16_t *val_len)
{
char *_json = json;
register char *_json;
register char *c;
char *sholder;
_json = json;
n--;

goto_FUNC_json_query_mulKey_ValPtrLen_FILE_json_parser:
for (c = key[n]; *json; json++) {
if ((*json == '"') && (*(json + key_len[n] + 1) == '"')) {
sholder = ++json; // skip '"'

while (*json) {
if ((*json == '"') && (*(json + key_len[n - 1] + 1) == '"')) {
json++; // skip '"'
if (memcmp(json, key[n - 1], key_len[n - 1]) == 0) {
json += key_len[n - 1] + 2; // skips "":"
while ((*json++ == *c++) && (*c != 0));
if (*c != 0) {
json = sholder + key_len[n] + 1;
goto goto_FUNC_json_query_mulKey_ValPtrLen_FILE_json_parser;
}

json += 2; // skips "":"

// Grabs Value
val[n - 1] = json;
while ((*json != ',') && (*json != '}'))
json++;
// Grabs Value
val[n] = json;
for (;(*json != ',') && (*json != '}'); json++);

val_len[n] = json - val[n];

val_len[n - 1] = json - val[n - 1];
// Checks if all values were found
if ((--n) > -1)
goto goto_FUNC_json_query_mulKey_ValPtrLen_FILE_json_parser;

// Checks if all values were found
n--;
if (n == 0)
return 0;
} else
json += key_len[n - 1] + 1;
return 0;

}
json++;
// This is a safe in case the buffer is full and the iteration
// keeps going.
if(json - _json > 6000) {
Expand Down

0 comments on commit c46dc79

Please sign in to comment.