int cb_check_json_string_content( unsigned char **ucsname, int namelength , int *from, char bypassisremoved){ int err=CBSUCCESS, indx=0; unsigned long int chr=0x20, chprev=0x20; char jsonfourhexadecimaldigits=-1; if( ucsname==NULL || *ucsname==NULL ){ cb_clog( CBLOGDEBUG, CBERRALLOC, "\ncb_check_json_string: string was null."); return CBERRALLOC; } //cb_clog( CBLOGDEBUG, CBNEGATION, "\ncb_check_json_string_content: BYPASSISREMOVED %i.", bypassisremoved); /* * Bypass and control characters. */ indx = *from; err = cb_get_ucs_chr( &chprev, &(*ucsname), &indx, namelength ); while( indx<namelength && indx<CBNAMEBUFLEN && err<CBNEGATION ){ err = cb_get_ucs_chr( &chr, &(*ucsname), &indx, namelength); //cb_clog( CBLOGDEBUG, CBNEGATION, "[%c]", (char) chr ); if( indx>(namelength-4) && ( chr=='"' && chprev=='\\' ) ){ cb_clog( CBLOGDEBUG, CBNEGATION, "\ncb_check_json_string: trying to bypass last quotation."); *from = indx; return CBNOTJSONSTRING; }else if( jsonfourhexadecimaldigits>=0 && jsonfourhexadecimaldigits<4 ){ if( ! JSON_HEX( chr) ){ cb_clog( CBLOGINFO, CBNEGATION, "\ncb_check_json_string: u+4 hex digits: digit was not in hexadecimal format (at place %i after \\u, char %c).", jsonfourhexadecimaldigits, (char) chr ); *from = indx; return CBNOTJSONSTRING; } ++jsonfourhexadecimaldigits; }else if( chprev=='\\' && JSON_CTRL( chr ) ){ jsonfourhexadecimaldigits=-1; if( chr=='u' ){ /* Four hexadecimal digits. */ jsonfourhexadecimaldigits=0; } }else if( chprev=='\\' && bypassisremoved==0 ){ cb_clog( CBLOGINFO, CBNEGATION, "\ncb_check_json_string: control characters '\\' and '\"' must be bypassed inside the string, (at character '%c', number %i)", (char) chr, indx/4); cb_clog( CBLOGDEBUG, CBNEGATION, ", string ' "); cb_print_ucs_chrbuf( CBLOGDEBUG, &(*ucsname), namelength, namelength); cb_clog( CBLOGDEBUG, CBNEGATION, " '"); cb_clog( CBLOGINFO, CBNEGATION, "."); *from = indx; return CBNOTJSONSTRING; } if( chr=='\\' && chprev=='\\' ) chprev = 0x20; // any not bypass else chprev = chr; } *from = indx; //return CBSUCCESS; return CBSUCCESSJSONSTRING; // 22.8.2016 }
/* * Function reading the white spaces away from the end. * * *from is the current character index. * If *from is not used, cb_compare compares witn -2 the first names length * and *from is updated with the name length. */ int cb_check_json_value( CBFILE **cfg, unsigned char **ucsvalue, int ucsvaluelen, int *from ){ int err = CBSUCCESS, wsperr = CBSUCCESS; // 22.8.2016 unsigned long int chr=0x20; if( from==NULL || cfg==NULL || *cfg==NULL || ucsvalue==NULL || *ucsvalue==NULL ){ cb_clog( CBLOGDEBUG, CBERRALLOC, "\ncb_check_json_value: ucsvalue was null."); return CBERRALLOC; } err = cb_check_json_value_subfunction( &(*cfg), &(*ucsvalue), ucsvaluelen, &(*from) ); /* * Length. */ if(*from>ucsvaluelen){ return CBINDEXOUTOFBOUNDS; }else if(*from<ucsvaluelen){ /* * Last ',' and ']' check missing 10.11.2015 */ /* * Read away white spaces. */ while( *from<ucsvaluelen && ucsvaluelen>0 && *from>0 && *from<CBNAMEBUFLEN && err<CBNEGATION && wsperr<CBNEGATION && \ ( WSP( chr ) || CR( chr ) || LF( chr ) ) ){ wsperr = cb_get_ucs_chr( &chr, &(*ucsvalue), &(*from), ucsvaluelen); //cb_clog( CBLOGDEBUG, CBNEGATION, "\ncb_check_json_value: read away white spaces chr [%c]", (char) chr ); } //cb_clog( CBLOGDEBUG, CBNEGATION, "\ncb_check_json_value: FROM: %i, UCSVALUELEN: %i, ERR: %i.", *from, ucsvaluelen, err ); if( *from<=ucsvaluelen && err<CBNEGATION ){ // 23.4.2016 //return CBSUCCESS; return err; // 22.8.2016 } }else if( *from==ucsvaluelen ) return err; // 22.8.2016 //return CBSUCCESS; return CBNOTJSONVALUE; }
/* To be used with arrays and objects. */ int cb_check_json_substring( CBFILE **cfg, unsigned char **ucsvalue, int ucsvaluelen, int *from ){ if( from==NULL || cfg==NULL || *cfg==NULL || ucsvalue==NULL || *ucsvalue==NULL ){ cb_clog( CBLOGDEBUG, CBERRALLOC, "\ncb_check_json_null: ucsvalue was null."); return CBERRALLOC; } int indx=0, err=CBSUCCESS; unsigned long int chr=0x20, chprev=0x20; // chprev is WSP character char quotecount=0; /* * Read until the second quote and pass the value to cb_check_json_string. */ indx = *from; while( indx<ucsvaluelen && indx<CBNAMEBUFLEN && err<CBNEGATION ){ if( chr=='\\' && chprev=='\\' ) chprev = 0x20; // any not bypass else chprev = chr; err = cb_get_ucs_chr( &chr, &(*ucsvalue), &indx, ucsvaluelen); // comment out the next: if(err>=CBERROR){ cb_clog( CBLOGERR, err, "\ncb_check_json_substring: cb_get_ucs_chr, error %i.", err ); } //cb_clog( CBLOGDEBUG, CBNEGATION, "\n\tcb_check_json_substring: [%c]", (char) chr ); if( (unsigned char) chr=='"' && (unsigned char) chprev!='\\' && quotecount>=1 ) ++quotecount; if( quotecount>=2 ){ return cb_check_json_string_content( &(*ucsvalue), indx, &(*from), (**cfg).cf.jsonremovebypassfromcontent ); // updates *from } if( quotecount==0 ){ if( (unsigned char) chr=='"' && ( ! WSP( chprev ) && ! CR( chprev ) && ! LF( chprev ) && chprev!=',' ) ){ *from = indx; return CBNOTJSONSTRING; }else if( (unsigned char) chr=='"' && (unsigned char) chprev!='\\' ){ ++quotecount; } } } *from = indx; return CBNOTJSONSTRING; }
/* * The only function reading white spaces away from the beginning. */ int cb_check_json_value_subfunction( CBFILE **cfg, unsigned char **ucsvalue, int ucsvaluelen, int *from ){ int err=CBSUCCESS; unsigned long int chr=0x20; char atstart=1; unsigned char *chrposition = NULL; if( from==NULL || cfg==NULL || *cfg==NULL || ucsvalue==NULL || *ucsvalue==NULL ){ cb_clog( CBLOGDEBUG, CBERRALLOC, "\ncb_check_json_value_subfunction: ucsvalue was null."); return CBERRALLOC; } //cb_clog( CBLOGDEBUG, CBNEGATION, "\n\ncb_check_json_value_subfunction: attribute ["); //cb_print_ucs_chrbuf( CBLOGEMERG, &(*ucsvalue), ucsvaluelen, ucsvaluelen ); //cb_clog( CBLOGDEBUG, CBNEGATION, "], length %i, from %i.", ucsvaluelen, *from); while( *from<ucsvaluelen && *from<CBNAMEBUFLEN && err<CBNEGATION ){ chrposition = &(*ucsvalue)[*from]; err = cb_get_ucs_chr( &chr, &(*ucsvalue), &(*from), ucsvaluelen); if(err>=CBERROR){ cb_clog(CBLOGDEBUG, err, "\ncb_check_json_value_subfunction: cb_get_ucs_chr, error %i.", err ); return err; } //cb_clog(CBLOGDEBUG, CBNEGATION, "\ncb_check_json_value_subfunction: chr [%c].", (char) chr ); if( atstart==1 && ( WSP( chr ) || CR( chr ) || LF( chr ) ) ) continue; atstart=0; //cb_clog( CBLOGDEBUG, CBNEGATION, "\ncb_check_json_value_subfunction: [%c]", (char) chr ); switch( (unsigned char) chr ){ case '"': *from -= 4; //cb_clog( CBLOGDEBUG, CBNEGATION, "\ncb_check_json_value_subfunction: return cb_check_json_substring()." ); return cb_check_json_substring( &(*cfg), &(*ucsvalue), ucsvaluelen, &(*from) ); // ok case '[': *from -= 4; //cb_clog( CBLOGDEBUG, CBNEGATION, "\ncb_check_json_value_subfunction: return cb_check_json_array()." ); return cb_check_json_array( &(*cfg), &(*ucsvalue), ucsvaluelen, &(*from) ); // ok case '{': *from -= 4; //cb_clog( CBLOGDEBUG, CBNEGATION, "\ncb_check_json_value_subfunction: return cb_check_json_object()." ); return cb_check_json_object( &(*cfg), &(*ucsvalue), ucsvaluelen, &(*from) ); // ok, 12.11.2015 case 't': if(*from>0) *from-=4; //cb_clog( CBLOGDEBUG, CBNEGATION, "\ncb_check_json_value_subfunction: return cb_check_json_true()." ); return cb_check_json_true( &(*cfg), &(*ucsvalue), ucsvaluelen, &(*from) ); case 'n': if(*from>0) *from-=4; //cb_clog( CBLOGDEBUG, CBNEGATION, "\ncb_check_json_value_subfunction: return cb_check_json_null()." ); return cb_check_json_null( &(*cfg), &(*ucsvalue), ucsvaluelen, &(*from) ); case 'f': if(*from>0) *from-=4; //cb_clog( CBLOGDEBUG, CBNEGATION, "\ncb_check_json_value_subfunction: return cb_check_json_false()." ); return cb_check_json_false( &(*cfg), &(*ucsvalue), ucsvaluelen, &(*from) ); default: if(*from>0) *from-=4; //cb_clog( CBLOGDEBUG, CBNEGATION, "\ncb_check_json_value_subfunction: return cb_check_json_number()." ); return cb_check_json_number( &(*cfg), &(*ucsvalue), ucsvaluelen, &(*from) ); } } //cb_clog( CBLOGDEBUG, CBNEGATION, "\ncb_check_json_value_subfunction: return cbnotjsonvalue." ); return CBNOTJSONVALUE; }
/* * http://www.json.org * * This function checks that name has '"' at start and end. This way it was * not cut in between name. This check is put before cb_put_name in * cb_set_cursor. * * '\' may not appear alone, only before '\' or before control characters: * '"' quotation * '\' reverse solidus * '/' solidus * 'b' backspace * 'f' formfeed * 'n' newline * 'r' carriage return * 't' horizontal tab * 'u'+4 hexadecimal digits */ int cb_check_json_string( unsigned char **ucsname, int namelength, int *from, char bypassisremoved ){ int err=CBSUCCESS, indx=0; unsigned long int chr=0x20; if( from==NULL || ucsname==NULL || *ucsname==NULL ){ cb_clog( CBLOGDEBUG, CBERRALLOC, "\ncb_check_json_string: string was null."); return CBERRALLOC; } //cb_clog( CBLOGDEBUG, CBNEGATION, "\ncb_check_json_string: checking attribute ["); //cb_print_ucs_chrbuf( CBLOGDEBUG, &(*ucsname), namelength, namelength); //cb_clog( CBLOGDEBUG, CBNEGATION, "] length %i.", namelength ); /* * Too short. */ if(namelength<2){ cb_clog( CBLOGDEBUG, CBNEGATION, "\ncb_check_json_string: returning CBNOTJSONSTRING: ["); cb_print_ucs_chrbuf( CBLOGDEBUG, &(*ucsname), namelength, namelength); cb_clog( CBLOGDEBUG, CBNEGATION, "] length %i, name length was less than two (double quotes do not fit in), returning CBNOTJSONSTRING (%i).", namelength, CBNOTJSONSTRING ); return CBNOTJSONSTRING; } /* * First '"'. */ indx = *from; err = cb_get_ucs_chr( &chr, &(*ucsname), &indx, namelength); if( chr != (unsigned long int) '"' ){ // first quotation cb_clog( CBLOGDEBUG, CBNEGATION, "\ncb_check_json_string: returning CBNOTJSONSTRING: ["); cb_print_ucs_chrbuf( CBLOGDEBUG, &(*ucsname), namelength, namelength); cb_clog( CBLOGDEBUG, CBNEGATION, "] length %i, first character was not quotation, returning CBNOTJSONSTRING (%i).", namelength, CBNOTJSONSTRING ); return CBNOTJSONSTRING; } /* * Last '"'. */ indx = namelength-4; err = cb_get_ucs_chr( &chr, &(*ucsname), &indx, namelength ); if( chr != (unsigned long int) '"' ){ // json bypass character cb_clog( CBLOGDEBUG, CBNEGATION, "\ncb_check_json_string: ["); cb_print_ucs_chrbuf( CBLOGDEBUG, &(*ucsname), namelength, namelength); cb_clog( CBLOGDEBUG, CBNEGATION, "] length %i, last was not quotation, returning CBNOTJSONSTRING (%i).", namelength, CBNOTJSONSTRING ); *from = indx; return CBNOTJSONSTRING; // second quotation } *from = 4; return cb_check_json_string_content( &(*ucsname), (namelength-4), &(*from), bypassisremoved ); }
int cb_check_json_array( CBFILE **cfg, unsigned char **ucsvalue, int ucsvaluelen, int *from ){ int err=CBSUCCESS; unsigned long int chr = 0x20; if( cfg==NULL || *cfg==NULL || ucsvalue==NULL || *ucsvalue==NULL ){ cb_clog( CBLOGDEBUG, CBERRALLOC, "\ncb_check_json_array: ucsvalue was null."); return CBERRALLOC; } err = cb_get_ucs_chr( &chr, &(*ucsvalue), &(*from), ucsvaluelen); //cb_clog( CBLOGDEBUG, CBNEGATION, "\ncb_check_json_array: first chr from *from %i is [%c], err %i", (*from-4), (char) chr, err ); if(err>=CBERROR){ cb_clog( CBLOGDEBUG, err, "\ncb_check_json_array: cb_get_ucs_chr, error %i.", err ); return err; } if( (unsigned char) chr == '[' ){ cb_check_json_array_value: /* * First value. */ //cb_clog( CBLOGDEBUG, CBNEGATION, "\ncb_check_json_array: to cb_check_json_value_subfunction, *from %i.", *from ); err = cb_check_json_value_subfunction( &(*cfg), &(*ucsvalue), ucsvaluelen, &(*from) ); if(err>=CBERROR){ cb_clog( CBLOGDEBUG, err, "\ncb_check_json_array: cb_check_json_value_subfunction, error %i.", err ); return err; } //cb_clog( CBLOGDEBUG, CBNEGATION, "\ncb_check_json_array: after cb_check_json_value_subfunction, %i.", err ); if(err>=CBNEGATION) return err; /* * Read comma or closing bracket. */ while( *from<ucsvaluelen && *from<CBNAMEBUFLEN && err<CBNEGATION ){ err = cb_get_ucs_chr( &chr, &(*ucsvalue), &(*from), ucsvaluelen); if(err>=CBERROR){ cb_clog( CBLOGDEBUG, err, "\ncb_check_json_array: cb_get_ucs_chr, error %i.", err ); return err; } //cb_clog( CBLOGDEBUG, CBNEGATION, "[ chr: %c indx: %i len: %i]", (char) chr, *from, ucsvaluelen ); if( WSP( chr ) || CR( chr ) || LF( chr ) ){ continue; }else if( (unsigned char) chr==']' ){ //cb_clog( CBLOGDEBUG, CBNEGATION, "\ncb_check_json_array: bracket, CBSUCCESS." ); //return CBSUCCESS; // 10.11.2015 return CBSUCCESSJSONARRAY; // 10.11.2015, 22.8.2016 }else if( (unsigned char) chr==',' ){ //cb_clog( CBLOGDEBUG, CBNEGATION, "\ncb_check_json_array: comma, next value." ); goto cb_check_json_array_value; }else return CBNOTJSONARRAY; } }else{ //cb_clog( CBLOGDEBUG, CBNEGATION, "\ncb_check_json_array: first char was [%c], NOTJSON.", (char) chr ); return CBNOTJSONARRAY; } return err; }
int cb_check_json_number( CBFILE **cfg, unsigned char **ucsvalue, int ucsvaluelen, int *from ){ int err=CBSUCCESS, indx=0; unsigned long int chr=0x20; char state=0, dotcount=0, ecount=0, minuscount=0, firstzero=0, expsigncount=0, atend=0; if( from==NULL || ucsvalue==NULL || *ucsvalue==NULL || cfg==NULL || *cfg==NULL ){ cb_clog( CBLOGDEBUG, CBERRALLOC, "\ncb_check_json_number: ucsvalue was null."); return CBERRALLOC; } indx = *from; if(ucsvaluelen<=0) return CBNOTJSONNUMBER; while( indx<ucsvaluelen && indx<CBNAMEBUFLEN && err<CBNEGATION ){ *from = indx; err = cb_get_ucs_chr( &chr, &(*ucsvalue), &indx, ucsvaluelen); //fprintf( stderr, "[%c]", (unsigned char) chr ); if(atend==0){ if( chr=='-' && minuscount==0 && state==0 ){ minuscount=1; state=EXPECTDIGIT; firstzero=0; continue; } if( chr=='0' && ( state==0 || ( firstzero==0 && minuscount==1 && state==EXPECTDIGIT ) ) ){ state=EXPECTDOTOREXP; firstzero=1; continue; } if( chr>='1' && chr<='9' && state==0 ){ state=EXPECTDIGITORDOT; firstzero=2; continue; } if( chr>='0' && chr<='9' && state==EXPECTEND ){ continue; } if( chr>='0' && chr<='9' && ( state==EXPECTDIGIT || state==EXPECTDIGITORDOT || state==EXPECTDIGITBEFOREEXP || state==EXPECTEXP ) ){ if(firstzero==1 && state==EXPECTDIGIT) return CBNOTJSONNUMBER; if( state==EXPECTDIGITBEFOREEXP ) state=EXPECTEXP; if( state==EXPECTDIGIT ) state=EXPECTDIGITORDOT; continue; } if( chr=='.' && dotcount==0 ){ state=EXPECTDIGITBEFOREEXP; dotcount=1; continue; } if( ( chr=='e' || chr=='E' ) && ecount==0 && ( ( dotcount==1 && state!=EXPECTDIGITBEFOREEXP ) || state==EXPECTDOTOREXP || state==EXPECTDIGITORDOT ) ){ state=EXPECTEXP; ecount=1; continue; } if( ( chr=='+' || chr=='-' ) && state==EXPECTEXP ){ expsigncount=1; state=EXPECTEND; continue; } if( WSP( chr ) || CR( chr ) || LF( chr ) ){ atend=1; continue; } if( chr==',' && state!=EXPECTDIGITBEFOREEXP && state!=EXPECTEXP && state!=EXPECTDIGIT ){ //return CBSUCCESS; return CBSUCCESSJSONNUMBER; // 22.8.2016 } if( chr==']' && state!=EXPECTDIGITBEFOREEXP && state!=EXPECTEXP && state!=EXPECTDIGIT ){ //return CBSUCCESS; return CBSUCCESSJSONNUMBER; // 22.8.2016 } return CBNOTJSONNUMBER; }else{ if( ! WSP( chr ) && ! CR( chr ) && ! LF( chr ) && chr!=']' && chr!=',' ) return CBNOTJSONNUMBER; } } //return CBSUCCESS; return CBSUCCESSJSONNUMBER; // 22.8.2016 }
/* * Copy UCS string from URL encoded data. Does not allocate. * * Can be used with only one buffer, the same buffer can be the resultbuffer. */ int cb_decode_url_encoded_bytes(unsigned char **ucshexdata, int ucshexdatalen, unsigned char **ucsdata, int *ucsdatalen, int ucsbuflen ){ int err = CBSUCCESS, indx=0, num=0, ucsindx=0; // ucsindx is important to be able to use only one parameter buffer, 23.5.2016 char cr_on=0; unsigned long int chr=0x20, chprev=0x20; char hex[3] = { '0', '0', '\0' }; if( ucshexdata==NULL || *ucshexdata==NULL || ucsdata==NULL || *ucsdata==NULL || ucsdatalen==NULL ){ cb_clog( CBLOGDEBUG, CBERRALLOC, "\ncb_decode_url_encoded_bytes: parameter was null, error %i.", CBERRALLOC ); return CBERRALLOC; } cb_clog( CBLOGDEBUG, CBNEGATION, "\ncb_decode_url_encoded_bytes: encoding data ["); cb_print_ucs_chrbuf( CBLOGDEBUG, &(*ucshexdata), ucshexdatalen, ucshexdatalen ); cb_clog( CBLOGDEBUG, CBNEGATION, "]"); for(indx=0; indx<ucshexdatalen && ucsindx<ucsbuflen && err<CBNEGATION;){ chprev = chr; err = cb_get_ucs_chr( &chr, &(*ucshexdata), &indx, ucshexdatalen); if(err>CBERROR){ cb_clog( CBLOGDEBUG, err, "\ncb_decode_url_encoded_bytes: cb_get_ucs_chr (1), error %i.", err ); continue; } // stop if( chr == '%' && ucsindx<ucsbuflen && indx<ucshexdatalen && err<CBNEGATION ){ chprev = chr; err = cb_get_ucs_chr( &chr, &(*ucshexdata), &indx, ucshexdatalen); if(err>CBERROR){ cb_clog( CBLOGDEBUG, err, "\ncb_decode_url_encoded_bytes: cb_get_ucs_chr (2), error %i.", err ); continue; } // stop if( chr == '0' && ucsindx<ucsbuflen && indx<ucshexdatalen && err<CBNEGATION ){ chprev = chr; err = cb_get_ucs_chr( &chr, &(*ucshexdata), &indx, ucshexdatalen); if(err>CBERROR){ cb_clog( CBLOGDEBUG, err, "\ncb_decode_url_encoded_bytes: cb_get_ucs_chr (3), error %i.", err ); continue; } // stop if( chr == 'D' || chr == 'd' ){ // ignore cr_on = 1; continue; } if( ( chr == 'A' || chr == 'a' ) && ucsindx<ucsbuflen && err<CBNEGATION ){ // LF -> CRLF err = cb_put_ucs_chr( (unsigned long int) 0x0D, &(*ucsdata), &ucsindx, ucsbuflen); // CR #1 cr_on = 0; if( ucsindx<ucsbuflen && err<CBNEGATION ) err = cb_put_ucs_chr( (unsigned long int) 0x0A, &(*ucsdata), &ucsindx, ucsbuflen); continue; }else if( cr_on==1 ){ // CR #2 err = cb_put_ucs_chr( (unsigned long int) 0x0D, &(*ucsdata), &ucsindx, ucsbuflen); cr_on = 0; } if( ucsindx<ucsbuflen && err<CBNEGATION ){ // %<hexnum><hexnum> #1 hex[0] = (char) chprev; hex[1] = (char) chr; num = (int) strtol( &hex[0], NULL, 16); err = cb_put_ucs_chr( (unsigned long int) num, &(*ucsdata), &ucsindx, ucsbuflen); } }else{ if(cr_on==1){ // CR #3 err = cb_put_ucs_chr( (unsigned long int) 0x0D, &(*ucsdata), &ucsindx, ucsbuflen); cr_on = 0; } if( ucsindx<ucsbuflen && err<CBNEGATION ){ // %<hexnum><hexnum> #2 hex[0] = (char) chr; chprev = chr; err = cb_get_ucs_chr( &chr, &(*ucshexdata), &indx, ucshexdatalen); if(err>CBERROR){ cb_clog( CBLOGDEBUG, err, "\ncb_decode_url_encoded_bytes: cb_get_ucs_chr (5), error %i.", err ); continue; } // stop hex[1] = (char) chr; num = (int) strtol( &hex[0], NULL, 16); err = cb_put_ucs_chr( (unsigned long int) num, &(*ucsdata), &ucsindx, ucsbuflen); } } continue; } if(cr_on==1){ // CR #4 err = cb_put_ucs_chr( (unsigned long int) 0x0D, &(*ucsdata), &ucsindx, ucsbuflen); cr_on = 0; } if( chr == '+' ){ err = cb_put_ucs_chr( (unsigned long int) 0x20, &(*ucsdata), &ucsindx, ucsbuflen); continue; // SP }else if( chr != '%' ){ // alpha, digit, special characters err = cb_put_ucs_chr( chr, &(*ucsdata), &ucsindx, ucsbuflen); } } *ucsdatalen = ucsindx; cb_clog( CBLOGDEBUG, CBNEGATION, "\ncb_decode_url_encoded_bytes: encoded data ["); cb_print_ucs_chrbuf( CBLOGDEBUG, &(*ucsdata), *ucsdatalen, ucsbuflen ); cb_clog( CBLOGDEBUG, CBNEGATION, "]"); return err; }
/* * Search name1.name2.name3 */ int search_and_print_tree(CBFILE **cbs, unsigned char **dotname, int namelen, int matchctl){ int err=CBSUCCESS, err2=CBSUCCESS, indx=0, undx=0; int ret = CBNEGATION; char namecount = 0; unsigned char origsearchstate = 1; unsigned char *ucsname = NULL; cb_name *firstname = NULL; unsigned long int chr = 0x20; cb_name *leaf = NULL; if( cbs==NULL || *cbs==NULL || (**cbs).cb==NULL ) return CBERRALLOC; if( dotname==NULL || *dotname==NULL ) return CBERRALLOC; /* Allocate new name to hold the name */ ucsname = (unsigned char *) malloc( sizeof(char)*( (unsigned int) namelen+1 ) ); if( ucsname == NULL ) { return CBERRALLOC; } memset( &ucsname[0], 0x20, (size_t) namelen ); ucsname[namelen] = '\0'; origsearchstate = (**cbs).cf.searchstate; (**cbs).cf.searchstate = CBSTATETREE; /* Every name */ undx = 0; indx=0; while( indx<namelen && undx<namelen && err<=CBNEGATION && err2<=CBNEGATION && namelen>0 ){ err2 = cb_get_ucs_chr( &chr, &(*dotname), &indx, namelen ); if( chr != (unsigned long int) '.' ) err = cb_put_ucs_chr( chr, &ucsname, &undx, namelen ); if( chr == (unsigned long int) '.' || indx>=namelen ){ // Name // HERE err = cb_set_cursor_match_length_ucs( &(*cbs), &ucsname, &undx, namecount, matchctl ); if( ( err==CBSUCCESS || err==CBSTREAM || err==CBFILESTREAM ) && (*(**cbs).cb).list.current!=NULL ){ if( (*(**cbs).cb).list.current!=NULL && namecount==0 ){ firstname = &(*(*(**cbs).cb).list.current); fprintf(stderr,"\nFound \""); if( firstname!=NULL && (*firstname).namebuf!=NULL && (*firstname).namelen>0 ) cb_print_ucs_chrbuf( CBLOGDEBUG, &(*firstname).namebuf, (*firstname).namelen, (*firstname).buflen ); fprintf(stderr,"\", (from list)"); fprintf(stderr," leaves from currentleaf: "); if( (*(*(**cbs).cb).list.currentleaf).leaf!=NULL ){ // 15.11.2015 leaf = &(* (cb_name*) (*(*(**cbs).cb).list.currentleaf).leaf); cb_print_leaves( &leaf, CBLOGDEBUG ); }else{ fprintf(stderr,"(empty)"); } }else{ if( (*(**cbs).cb).list.currentleaf!=NULL ){ // 15.11.2015 leaf = &(*(*(**cbs).cb).list.currentleaf); fprintf(stderr,"\nFound leaf (currentleaf) \""); cb_print_ucs_chrbuf( CBLOGDEBUG, &(*leaf).namebuf, (*leaf).namelen, (*leaf).buflen ); fprintf(stderr,"\" (from tree)."); fprintf(stderr," leaves from currentleaf: "); cb_print_leaves( &leaf, CBLOGDEBUG ); }else{ fprintf(stderr,"(empty)"); } } }else{ fprintf(stderr,"\n\""); cb_print_ucs_chrbuf( CBLOGDEBUG, &ucsname, undx, undx ); fprintf(stderr,"\" not found."); } if(err!=CBSUCCESS && err!=CBSUCCESSLEAVESEXIST && err!=CBSTREAM && err!=CBFILESTREAM) ret = CBNOTFOUND; else ret = err; undx = 0; ++namecount; } } (**cbs).cf.searchstate = origsearchstate; free(ucsname); ucsname=NULL; if(indx==namelen && (ret==CBSUCCESS || ret==CBSTREAM || ret==CBFILESTREAM) ) // Match and dotted name was searched through (cursor is at name), 13.12.2013 return ret; else // Name was not searched through or no match (cursor is somewhere in value) return CBNOTFOUND; }
/* * Search name and print it. */ int search_and_print_name(CBFILE **in, unsigned char **name, int namelength, char tree){ int err = 0, indx = 0; unsigned long int stchr = 0x20, endchr = 0x20; unsigned char tmp[CBNAMEBUFLEN+1]; unsigned char *ptr = NULL; cb_name *nameptr = NULL; tmp[CBNAMEBUFLEN] = '\0'; ptr = &(tmp[0]); if( in==NULL || *in==NULL || name==NULL || *name==NULL ) return CBERRALLOC; if( name!=NULL && *name!=NULL && namelength>0 ){ // %ame nam% %am% if(tree<=-8){ // regular expression search err = cb_set_cursor_match_length_ucs( &(*in), &(*name), &namelength, 0, tree ); }else{ // Test LIKECHR cb_get_ucs_chr(&stchr, &(*name), &indx, namelength); indx = (namelength - 4); cb_get_ucs_chr(&endchr, &(*name), &indx, namelength); if( stchr == (unsigned long int) LIKECHR ){ for(indx=0; indx<namelength-4 && indx<CBNAMEBUFLEN-4; ++indx) tmp[indx] = (*name)[indx+4] ; if( endchr == (unsigned long int) LIKECHR ){ indx -= 4; // %am% if(tree==0){ err = cb_set_cursor_match_length_ucs( &(*in), &ptr, &indx, 0, -6 ); }else err = search_and_print_tree( &(*in), &ptr, indx, -6 ); }else{ if(tree==0){ err = cb_set_cursor_match_length_ucs( &(*in), &ptr, &indx, 0, -5 ); // %ame }else err = search_and_print_tree( &(*in), &ptr, indx, -5 ); } }else if( endchr == (unsigned long int) LIKECHR ){ namelength -= 4; // % if(tree==0){ err = cb_set_cursor_match_length_ucs( &(*in), &(*name), &namelength, 0, -2 ); // nam% }else{ err = search_and_print_tree( &(*in), &(*name), namelength, -2 ); } }else{ if(tree==0){ err = cb_set_cursor_ucs( &(*in), &(*name), &namelength ); // matchctl=1 }else err = search_and_print_tree( &(*in), &(*name), namelength, 1 ); } } }else{ fprintf(stderr, "\n Name was null.\n"); } if(err>=CBERROR){ fprintf(stderr, "error at cb_set_cursor: %i.", err); } if(tree==0) nameptr = &(*(*(**in).cb).list.current); else nameptr = &(*(*(**in).cb).list.currentleaf); if( err==CBSUCCESS || err==CBSUCCESSLEAVESEXIST || err==CBSTREAM || err==CBFILESTREAM ){ //nameptr = &(*(*(**in).cb).list.current); //fprintf(stderr, "\n cbsearch, printing name:"); print_name(&(*in), &nameptr ); fprintf(stderr, " cbsearch, printing leaves:"); cb_print_leaves( &nameptr, CBLOGDEBUG ); } if(err==CBMESSAGEHEADEREND ){ fprintf(stderr, "\n Header end. \n"); } if( tree==0 && ( err==CBNOTFOUND || err==CBMESSAGEHEADEREND ) ){ fprintf(stderr, "\n Name \""); if( name!=NULL && *name!=NULL && namelength!=0 ) cb_print_ucs_chrbuf( CBLOGDEBUG, &(*name), namelength, namelength ); fprintf(stderr, "\" not found.\n"); return err; } if(err==CBSTREAM){ fprintf(stderr, "\n Stream.\n"); }else if(err==CBFILESTREAM){ fprintf(stderr, "\n Filestream.\n"); }else if(err==CBSTREAMEND){ fprintf(stderr, "\n Stream end.\n"); } return err; }
int cb_check_json_object( CBFILE **cfg, unsigned char **ucsvalue, int ucsvaluelen, int *from ){ int openpairs=0, err=CBSUCCESS; unsigned long int chr=0x20; if( from==NULL || cfg==NULL || *cfg==NULL || ucsvalue==NULL || *ucsvalue==NULL ){ cb_clog( CBLOGDEBUG, CBERRALLOC, "\ncb_check_json_null: ucsvalue was null."); return CBERRALLOC; } char state=0; //cb_clog( CBLOGDEBUG, CBNEGATION, "\ncb_check_json_object:" ); while( *from<ucsvaluelen && *from<CBNAMEBUFLEN && err<CBNEGATION ){ err = cb_get_ucs_chr( &chr, &(*ucsvalue), &(*from), ucsvaluelen); //cb_clog( CBLOGDEBUG, CBNEGATION, "[%c]", (char) chr ); if( ( WSP( chr ) || CR( chr ) || LF( chr ) ) ){ continue; }else if( (unsigned char) chr == '{' && state!=EXPECTCOMMA && state!=EXPECTCOLON ){ /* * Read until '{'. */ ++openpairs; cb_check_json_object_read_name: /* * Reads name within the quotes and update *from. Quotes are bypassed. */ err = cb_check_json_substring( &(*cfg), &(*ucsvalue), ucsvaluelen, &(*from) ); // comment out the next: if(err>=CBERROR){ cb_clog( CBLOGERR, err, "\ncb_check_json_object: cb_check_json_substring, error %i.", err ); } state = EXPECTCOLON; continue; }else if( state==EXPECTCOLON && (unsigned char) chr == ':' ){ /* * Read colon ':'. */ /* * Read Value. */ err = cb_check_json_value_subfunction( &(*cfg), &(*ucsvalue), ucsvaluelen, &(*from) ); // comment out the next: if(err>=CBERROR){ cb_clog( CBLOGERR, err, "\ncb_check_json_object: cb_check_json_value_subfunction, error %i.", err ); } state = EXPECTCOMMA; continue; }else if( ( state==EXPECTCOMMA || state==EXPECTCOMMAORNAME ) && ( (unsigned char) chr == ',' || (unsigned char) chr == '}' ) ){ /* * Read comma ',' or '}' . */ if( (unsigned char) chr == '}' ){ state = EXPECTCOMMAORNAME; --openpairs; }else{ /* * Read next name. */ goto cb_check_json_object_read_name; } continue; }else if( state==EXPECTCOMMAORNAME && (unsigned char) chr == '"' ){ *from-=4; goto cb_check_json_object_read_name; }else if( (unsigned char) chr == '}' ){ --openpairs; if( state!=EXPECTCOMMA ) err = CBNOTJSONOBJECT; state = 0; continue; }else err = CBNOTJSONOBJECT; } if(openpairs==0) return err; return CBNOTJSONOBJECT; }