SDB_API int sdb_json_set (Sdb *s, const char *k, const char *p, const char *v, ut32 cas) { const char *beg[3]; const char *end[3]; int l, idx, len[3]; char *str = NULL; Rangstr rs; ut32 c; char *js = sdb_get (s, k, &c); if (!js) return 0; if (cas && c != cas) { free (js); return 0; } rs = json_get (js, p); if (!rs.p) { free (js); return 0; } #define WLEN(x) (int)(size_t)(end[x]-beg[x]) beg[0] = js; end[0] = rs.p + rs.f; len[0] = WLEN (0); beg[1] = v; end[1] = v + strlen (v); len[1] = WLEN (1); beg[2] = rs.p + rs.t; end[2] = js + strlen (js); len[2] = WLEN (2); // TODO: accelerate with small buffer in stack for small jsons str = malloc (len[0]+len[1]+len[2]+1); idx = len[0]; memcpy (str, beg[0], idx); l = len[1]; memcpy (str+idx, beg[1], l); idx += len[1]; l = len[2]; memcpy (str+idx, beg[2], l); str[idx+l] = 0; sdb_set (s, k, str, cas); free (str); free (js); return 1; }
static void scan_list_dvbv5_entry_fill( scan_list_entry_t *p_entry, const char *psz_line, size_t i_len ) { const char *psz_key; const char *psz_value; size_t i_keylen; size_t i_valuelen; if( !scan_list_token_split( psz_line, i_len, &psz_key, &i_keylen, &psz_value, &i_valuelen ) ) return; char *psz_end = (char *) &psz_value[i_valuelen]; if( KEY_EQUALS("FREQUENCY") ) { p_entry->i_freq = strtoll( psz_value, &psz_end, 10 ); } else if( KEY_EQUALS("BANDWIDTH_HZ") ) { p_entry->i_bw = strtoll( psz_value, &psz_end, 10 ); } else if( KEY_EQUALS("DELIVERY_SYSTEM") ) { if( VALUE_EQUALS("DVBT") ) p_entry->delivery = SCAN_DELIVERY_DVB_T; else if( VALUE_EQUALS("DVBT2") ) p_entry->delivery = SCAN_DELIVERY_DVB_T2; else if( VALUE_EQUALS("DVBS") ) p_entry->delivery = SCAN_DELIVERY_DVB_S; else if( VALUE_EQUALS("DVBS2") ) p_entry->delivery = SCAN_DELIVERY_DVB_S2; else if( VALUE_EQUALS("DVBC/ANNEX_A") ) p_entry->delivery = SCAN_DELIVERY_DVB_C; else if( VALUE_EQUALS("ISDBT") ) p_entry->delivery = SCAN_DELIVERY_ISDB_T; } else if( KEY_EQUALS("MODULATION") ) { static const struct { size_t i_len; const char *psz; int val; } map[] = { { WLEN( "APSK/16" ), SCAN_MODULATION_APSK_16 }, { WLEN( "APSK/32" ), SCAN_MODULATION_APSK_32 }, { WLEN( "DQPSK" ), SCAN_MODULATION_DQPSK }, { WLEN( "PSK/8" ), SCAN_MODULATION_PSK_8 }, { WLEN( "QAM/4_NR" ),SCAN_MODULATION_QAM_4NR }, { WLEN( "QAM/16" ), SCAN_MODULATION_QAM_16 }, { WLEN( "QAM/32" ), SCAN_MODULATION_QAM_32 }, { WLEN( "QAM/64" ), SCAN_MODULATION_QAM_64 }, { WLEN( "QAM/128" ), SCAN_MODULATION_QAM_128 }, { WLEN( "QAM/256" ), SCAN_MODULATION_QAM_256 }, { WLEN( "QAM/AUTO" ),SCAN_MODULATION_QAM_AUTO }, { WLEN( "QPSK" ), SCAN_MODULATION_QPSK }, { WLEN( "VSB/8" ), SCAN_MODULATION_VSB_8 }, { WLEN( "VSB/16" ), SCAN_MODULATION_VSB_16 }, }; p_entry->modulation = SCAN_MODULATION_AUTO; for(size_t i=0; i<ARRAY_SIZE(map); i++) { if( map[i].i_len == i_valuelen && !strncasecmp( psz_value, map[i].psz, i_valuelen ) ) { p_entry->modulation = map[i].val; break; } } } else if( KEY_EQUALS("POLARIZATION") ) { if( VALUE_EQUALS("VERTICAL") ) p_entry->polarization = SCAN_POLARIZATION_VERTICAL; else p_entry->polarization = SCAN_POLARIZATION_HORIZONTAL; } else if( KEY_EQUALS("SYMBOL_RATE") ) { p_entry->i_rate = strtoll( psz_value, &psz_end, 10 ); } else if( KEY_EQUALS("INNER_FEC") ) { READ_STRINGVAL( p_entry->inner_fec, scan_list_parse_fec ); } else if( KEY_EQUALS("CODE_RATE_HP") ) { READ_STRINGVAL( p_entry->coderate_hp, scan_list_parse_fec ); } else if( KEY_EQUALS("CODE_RATE_LP") ) { READ_STRINGVAL( p_entry->coderate_lp, scan_list_parse_fec ); } else if( KEY_EQUALS("GUARD_INTERVAL") ) { READ_STRINGVAL( p_entry->guard_interval, scan_list_parse_guard ); } }
SDB_API int sdb_json_set (Sdb *s, const char *k, const char *p, const char *v, ut32 cas) { const char *beg[3]; const char *end[3]; int jslen, l, idx, len[3]; char *b, *str = NULL; const char *js; Rangstr rs; ut32 c; if (!s || !k) return 0; js = sdb_const_get_len (s, k, &jslen, &c); if (!js) { b = malloc (strlen (p)+strlen (v)+8); if (b) { int is_str = isstring (v); const char *q = is_str?"\"":""; sprintf (b, "{\"%s\":%s%s%s}", p, q, v, q); #if 0 /* disabled because it memleaks */ sdb_set_owned (s, k, b, cas); #else sdb_set (s, k, b, cas); free (b); #endif return 1; } return 0; } if (cas && c != cas) { return 0; } rs = json_get (js, p); if (!rs.p) { char *b = malloc (jslen+strlen(k)+strlen (v)+32); if (b) { int curlen, is_str = isstring (v); const char *q = is_str?"\"":""; const char *e = ""; // XX: or comma if (js[0] && js[1] != '}') e = ","; curlen = sprintf (b, "{\"%s\":%s%s%s%s", p, q, v, q, e); strcpy (b+curlen, js+1); // transfer ownership sdb_set_owned (s, k, b, cas); return 1; } // invalid json? return 0; } #define WLEN(x) (int)(size_t)(end[x]-beg[x]) beg[0] = js; end[0] = rs.p + rs.f; len[0] = WLEN (0); if (*v) { beg[1] = v; end[1] = v + strlen (v); len[1] = WLEN (1); } beg[2] = rs.p + rs.t; end[2] = js + jslen; len[2] = WLEN (2); // TODO: accelerate with small buffer in stack for small jsons if (*v) { int is_str = isstring (v); int msz = len[0]+len[1]+len[2]+strlen (v); if (msz<1) return 0; str = malloc (msz); if (!str) return 0; idx = len[0]; memcpy (str, beg[0], idx); if (is_str) { if (beg[2][0]!='"') { str[idx]='"'; idx++; } } else { if (beg[2][0]=='"') { idx--; } } l = len[1]; memcpy (str+idx, beg[1], l); idx += len[1]; if (is_str) { // TODO: add quotes if (beg[2][0]!='"') { str[idx]='"'; idx++; } } else { if (beg[2][0]=='"') { beg[2]++; } } l = len[2]; memcpy (str+idx, beg[2], l); str[idx+l] = 0; } else { int kidx; // DELETE KEY rs.f -= 2; kidx = findkey (&rs); len[0] = R_MAX (1, kidx-1); if (kidx==1) { if (beg[2][0]=='"') beg[2]++; beg[2]++; } str = malloc (len[0]+len[2]+1); if (!str) return 0; memcpy (str, beg[0], len[0]); if (!*beg[2]) beg[2]--; memcpy (str+len[0], beg[2], len[2]); str[len[0]+len[2]] = 0; } sdb_set_owned (s, k, str, cas); return 1; }