/** * @brief A wrapper routine for get_next_token(). Reads next token from query * using get_next_token() and further ensures that the token read is a * valid one. * @param query The query from where next token will be read. * @param index The index in the passed query from where next token will be * scanned. * @return The token that we have just read. */ std::string get_next_valid_token(std::string *query, unsigned int* index) { std::string current_token = get_next_token(query, index); std::string tmp = ""; //check if this token is a valid one while (!is_valid_token(current_token) and *index < query->length()) { current_token = get_next_token(query, index); } //how did we get out ? was it because we got a valid token or because end // of query reched ? if (*index >= query->length() and !is_valid_token(current_token)) { return ""; } // remove tokens enclosed in backticks if any if (current_token[0] == '`') { sanitize_token(¤t_token); } // if token is CONCAT then eat out everything until a ')' is found if (convert_to_uppercase(current_token) == "CONCAT") { current_token = get_next_token(query, index); while (current_token != ")" and *index < query->length()) { current_token = get_next_token(query, index); } if (*index < query->length()) { //this is bad as even though closing ')' have been encountered // we still need a valid token return ""; } /* * if we are here then current_token is ")" now read next_token * so far we have only eaten CONCAT block. it's not for sure that next * block is a valid one. * is this recursive block correct ? */ current_token = get_next_valid_token(query, index); } else if (convert_to_uppercase(current_token) == "MAX") { /* * MAx(coulmn_name) is a keyword that gives a column name in round * brackets. */ tmp = get_next_token(query, index); //must be a ( if (tmp != "(") { std::cerr << "No '(' after MAX at pos: " << *index << std::endl; return ""; } //now read the actual col name current_token = get_next_token(query, index); //bypass the closing ')' tmp = get_next_token(query, index); //must be a ( if (tmp != ")") { std::cerr << "No ')' after MAX at pos: " << *index << std::endl; return ""; } } return current_token; }
static int http_header_add(struct http_context *ctx, struct http_header* header) { int size; struct http_header *p; if(ctx->header_size+1 >= ctx->header_capacity) { size = ctx->header_capacity < 16 ? 16 : (ctx->header_size * 3 / 2); p = (struct http_header*)realloc(ctx->headers, sizeof(struct http_header) * size); if(!p) return ENOMEM; ctx->headers = p; ctx->header_capacity = size; } assert(header->npos > 0); assert(header->nlen > 0); assert(header->vpos > 0); assert(is_valid_token(ctx->raw+header->npos, header->nlen)); ctx->raw[header->npos+header->nlen] = '\0'; ctx->raw[header->vpos+header->vlen] = '\0'; memmove(ctx->headers + ctx->header_size, header, sizeof(struct http_header)); ++ctx->header_size; // handle http_header_handler(ctx, header->npos, header->vpos); return 0; }
void parse_command_line ( int argc, char **argv, char **env ) { char *p; register int i; query.num_nodes = 0; for (i = 1 ; i < argc ; i++ ){ p = argv[i]; query.token_data[token_number].order = 1; if ( isdigit( p[0] ) ){ if ( !token_number ){ fprintf(stderr, "\nERROR: you didn't specify any sort tokens\n\n"); exit(1); }else{ query.num_nodes = atoi ( p ); break; } } query.token_data[token_number].order = 1; if ( p[0] == '+' ) p++; else if ( p[0] == '-' ){ query.token_data[token_number].order = -1; p++; } if ( is_valid_token( p ) ) strcpy( query.token_data[token_number].token , p); else { fprintf(stderr, "ERROR: %s is not a valid token\n", p); fprintf(stderr, "\nType \"ganglia\" for command SYNTAX\n\n"); exit(1); } token_number++; if ( token_number > NUM_TOKENS ){ fprintf(stderr, "ERROR: only %d tokens can be specified\n", NUM_TOKENS); exit(1); } } }
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; }
// H4.2 Message Headers // message-header = field-name ":" [ field-value ] // field-name = token // field-value = *( field-content | LWS ) // field-content = <the OCTETs making up the field-value // and consisting of either *TEXT or combinations // of token, separators, and quoted-string> static int http_parse_header_line(struct http_context *ctx) { enum { SM_HEADER_START = SM_HEADER, SM_HEADER_NAME, SM_HEADER_NAME_SP, SM_HEADER_SEPARATOR, SM_HEADER_VALUE }; int r; for(; ctx->offset < ctx->raw_size; ctx->offset++) { switch(ctx->stateM) { case SM_HEADER_START: switch(ctx->raw[ctx->offset]) { case '\r': assert(0 == ctx->header.npos); assert(0 == ctx->header.vpos); if(ctx->offset + 2 > ctx->raw_size) return 0; // wait more date ++ctx->offset; assert('\n' == ctx->raw[ctx->offset]); case '\n': assert(0 == ctx->header.npos); assert(0 == ctx->header.vpos); ++ctx->offset; ctx->stateM = SM_BODY; return 0; case ' ': case '\t': assert(0); // multi-line header ? break; default: assert(0 == ctx->header.npos); assert(0 == ctx->header.nlen); ctx->header.npos = ctx->offset; ctx->stateM = SM_HEADER_NAME; } break; case SM_HEADER_NAME: switch(ctx->raw[ctx->offset]) { case '\r': case '\n': assert(0); return -1; // invalid case ' ': ctx->header.nlen = ctx->offset - ctx->header.npos; assert(ctx->header.nlen > 0 && is_valid_token(ctx->raw+ctx->header.npos, ctx->header.nlen)); ctx->stateM = SM_HEADER_NAME_SP; break; case ':': ctx->header.nlen = ctx->offset - ctx->header.npos; assert(ctx->header.nlen > 0 && is_valid_token(ctx->raw+ctx->header.npos, ctx->header.nlen)); ctx->stateM = SM_HEADER_SEPARATOR; break; } break; case SM_HEADER_NAME_SP: switch(ctx->raw[ctx->offset]) { case ' ': break; // skip SP case ':': ctx->stateM = SM_HEADER_SEPARATOR; break; default: assert(0); return -1; } break; case SM_HEADER_SEPARATOR: switch(ctx->raw[ctx->offset]) { case '\r': // empty value // e.g. x-wap-profile: \r\nx-forwarded-for: 10.25.110.244, 115.168.35.85\r\n ++ctx->offset; assert('\n' == ctx->raw[ctx->offset]); case '\n': ctx->header.vpos = ctx->offset; ctx->header.vlen = 0; r = http_header_add(ctx, &ctx->header); if(0 != r) return r; memset(&ctx->header, 0, sizeof(struct http_header)); // reuse header ctx->stateM = SM_HEADER; break; case ' ': break; // skip SP default: ctx->stateM = SM_HEADER_VALUE; ctx->header.vpos = ctx->offset; break; } break; case SM_HEADER_VALUE: switch(ctx->raw[ctx->offset]) { case '\n': assert('\r' == ctx->raw[ctx->offset-1]); ctx->header.vlen = ctx->offset - 1 - ctx->header.vpos; trim_right(ctx->raw, &ctx->header.vpos, &ctx->header.vlen); ctx->stateM = SM_HEADER; // add new header r = http_header_add(ctx, &ctx->header); if(0 != r) return r; memset(&ctx->header, 0, sizeof(struct http_header)); // reuse header break; default: break; } break; default: assert(0); return -1; } } return 0; }