void test_lex_look_ahead_3() { const char *input = "int a=2; int b=17;"; unsigned int len, pos; len = strlen(input); pos = 0; check_token(input, len, &pos, "int", 0); pos += 3; check_token(input, len, &pos, "a", pos + 1); pos += 1; check_token(input, len, &pos, "=", pos); pos += 1; check_token(input, len, &pos, "2", pos); pos += 1; check_token(input, len, &pos, ";", pos); pos += 1; check_token(input, len, &pos, "int", pos + 1); pos += 3; check_token(input, len, &pos, "b", pos + 1); pos += 1; check_token(input, len, &pos, "=", pos); pos += 1; check_token(input, len, &pos, "17", pos); }
int main(int argc, char *argv[]) { int i; for (i = 1 ; i < argc ; i++) { printf ("check returns %d\n", check_token (NULL, argv[i])); } }
void test_lex_look_ahead_2() { const char *input = "231+ 1421 - 7"; unsigned int len, pos; len = strlen(input); pos = 0; check_token(input, len, &pos, "231", 0); pos += 3; check_token(input, len, &pos, "+", pos); pos += 1; check_token(input, len, &pos, "1421", pos + 1); pos += 4; check_token(input, len, &pos, "-", pos + 1); pos += 1; check_token(input, len, &pos, "7", pos + 1); }
void CDbProxyHandler::query(DBTable& _return, const std::string& sign, const int32_t seq, const int32_t query_index, const std::vector<std::string> & tokens, const int32_t limit, const int32_t limit_start) { CConfigLoader* config_loader = CConfigLoader::get_singleton(); struct QueryInfo query_info; if (check_token(seq, tokens)) { throw apache::thrift::TApplicationException("invalid parameter"); } if (!config_loader->get_query_info(query_index, &query_info)) { MYLOG_ERROR("query_index[%d] not exists\n", query_index); throw apache::thrift::TApplicationException("query_index not exists"); } if (sign != query_info.sign) { MYLOG_ERROR("sign[%s] error: %s\n", sign.c_str(), query_info.sign.c_str()); throw apache::thrift::TApplicationException("sign error"); } try { sys::DBConnection* db_connection = config_loader->get_db_connection(query_info.database_index); if (NULL == db_connection) { MYLOG_ERROR("database_index[%d] not exists\n", query_info.database_index); throw apache::thrift::TApplicationException("database_index not exists"); } else if (tokens.size() > utils::FORMAT_STRING_SIZE) { MYLOG_ERROR("[%d]too big: %d\n", seq, (int)tokens.size()); throw apache::thrift::TApplicationException("tokens too many"); } else { std::string sql = utils::format_string(query_info.sql_template.c_str(), tokens); MYLOG_DEBUG("%s LIMIT %d,%d\n", sql.c_str(), limit_start, limit); if (limit_start > 0) db_connection->query(_return, "%s LIMIT %d,%d", sql.c_str(), limit_start, limit); else db_connection->query(_return, "%s LIMIT %d", sql.c_str(), limit); } } catch (sys::CDBException& db_ex) { MYLOG_ERROR("[%d]%s\n", seq, db_ex.str().c_str()); throw apache::thrift::TApplicationException(db_ex.str()); } }
void test_lex_look_ahead_1() { const char *input = "1+ 2"; unsigned int len, pos, expected_pos; char *expected_result; len = strlen(input); pos = 0; expected_pos = 0; expected_result = "1"; check_token(input, len, &pos, expected_result, expected_pos); pos = 1; expected_pos = 1; expected_result = "+"; check_token(input, len, &pos, expected_result, expected_pos); pos = 2; expected_pos = 3; expected_result = "2"; check_token(input, len, &pos, expected_result, expected_pos); }
int check_user_token(char *user, char *token, sqlite3 *db) { char *request; sqlite3_stmt *ppStmt; entry_st entry; yubikey_token_st tok; int id, rc; request = sqlite3_mprintf("SELECT id FROM tokens WHERE user=%Q;", user); rc = sqlite3_prepare_v2(db, request, -1, &ppStmt, NULL); sqlite3_free(request); if(check_error("Error in preparing SELECT", rc, db)) return CHK_FAIL; rc = sqlite3_step(ppStmt); if (rc != SQLITE_ROW) { sqlite3_finalize(ppStmt); return CHK_UNKNOWN; } if (strlen(token) != 32) { sqlite3_finalize(ppStmt); return CHK_FAIL; } while (rc == SQLITE_ROW) { id = sqlite3_column_int(ppStmt, 0); get_entry_from_id(id, &entry, db); yubikey_parse ((uint8_t *) token, entry.aes_bin, &tok); rc = check_token(&tok, entry.uid, entry.counter, entry.session); if (rc == CHK_OK) { rc = update_db(db, id, &tok); sqlite3_finalize(ppStmt); return rc; } rc = sqlite3_step(ppStmt); } sqlite3_finalize(ppStmt); return CHK_FAIL; }
int CDbProxyHandler::update(const std::string& sign, const int32_t seq, const int32_t update_index, const std::vector<std::string>& tokens) { CConfigLoader* config_loader = CConfigLoader::get_singleton(); struct UpdateInfo update_info; if (check_token(seq, tokens)) { throw apache::thrift::TApplicationException("invalid parameter"); } if (!config_loader->get_update_info(update_index, &update_info)) { MYLOG_ERROR("[%d]update_index[%d] not exists\n", seq, update_index); throw apache::thrift::TApplicationException("update_index not exists"); } try { sys::DBConnection* db_connection = config_loader->get_db_connection(update_info.database_index); if (NULL == db_connection) { MYLOG_ERROR("[%d]database_index[%d] not exists\n", seq, update_info.database_index); throw apache::thrift::TApplicationException("database_index not exists"); } else if (tokens.size() > utils::FORMAT_STRING_SIZE) { MYLOG_ERROR("[%d]too big: %d\n", seq, (int)tokens.size()); throw apache::thrift::TApplicationException("tokens too many"); } else { std::string sql = utils::format_string(update_info.sql_template.c_str(), tokens); MYLOG_DEBUG("%s\n", sql.c_str()); int affected_rows = db_connection->update("%s", sql.c_str()); return affected_rows; } } catch (sys::CDBException& db_ex) { MYLOG_ERROR("[%d]%s\n", seq, db_ex.str().c_str()); throw apache::thrift::TApplicationException(db_ex.str()); } }
int lexer(char *cmd, t_token **token, t_42sh *shell) { char *error; t_lex *lexi; lexi = xmalloc(sizeof(*lexi)); cmd = check_carac(cmd); lexi->cmd = epur_str(strdup(cmd)); if (!(lexi->cmd[0])) return (0); lex(lexi->cmd, token); if ((error = check_token(token, lexi)) != NULL) { printf("42sh : Syntax error near unexpected token `%s`\n", error); return (0); } else return (parser(token, shell)); return (0); }
static void psync_p2p_tcphandler(void *ptr){ packet_get packet; psync_fileid_t localfileid; psync_binary_rsa_key_t binpubrsa; psync_rsa_publickey_t pubrsa; psync_symmetric_key_t aeskey; psync_encrypted_symmetric_key_t encaeskey; psync_crypto_aes256_ctr_encoder_decoder_t encoder; char *token, *localpath; uint64_t off; size_t rd; psync_socket_t sock; psync_file_t fd; uint32_t keylen, enctype; unsigned char hashhex[PSYNC_HASH_DIGEST_HEXLEN], buff[4096]; sock=*((psync_socket_t *)ptr); psync_free(ptr); debug(D_NOTICE, "got tcp connection"); if (unlikely_log(socket_read_all(sock, &packet, sizeof(packet)))) goto err0; if (unlikely_log(packet.keylen>PSYNC_P2P_RSA_SIZE) || unlikely_log(packet.tokenlen>512)) /* lets allow 8 times larger keys than we use */ goto err0; localfileid=psync_p2p_has_file(packet.hashstart, packet.genhash, packet.rand, packet.filesize, hashhex); if (!localfileid){ debug(D_WARNING, "got request for file that we do not have"); goto err0; } binpubrsa=psync_ssl_alloc_binary_rsa(packet.keylen); if (unlikely_log(socket_read_all(sock, binpubrsa->data, binpubrsa->datalen))){ psync_free(binpubrsa); goto err0; } token=psync_new_cnt(char, packet.tokenlen); if (unlikely_log(socket_read_all(sock, token, packet.tokenlen)) || unlikely_log(!check_token(token, packet.tokenlen, binpubrsa->data, packet.keylen, hashhex))){ psync_free(binpubrsa); psync_free(token); goto err0; } psync_free(token); pubrsa=psync_ssl_rsa_binary_to_public(binpubrsa); psync_free(binpubrsa); if (unlikely_log(pubrsa==PSYNC_INVALID_RSA)) goto err0; localpath=psync_local_path_for_local_file(localfileid, NULL); if (unlikely_log(!localpath)) goto err0; fd=psync_file_open(localpath, P_O_RDONLY, 0); debug(D_NOTICE, "sending file %s to peer", localpath); psync_free(localpath); if (fd==INVALID_HANDLE_VALUE){ debug(D_WARNING, "could not open local file %lu", (unsigned long)localfileid); goto err0; } aeskey=psync_crypto_aes256_ctr_gen_key(); encaeskey=psync_ssl_rsa_encrypt_symmetric_key(pubrsa, aeskey); encoder=psync_crypto_aes256_ctr_encoder_decoder_create(aeskey); psync_ssl_free_symmetric_key(aeskey); keylen=encaeskey->datalen; enctype=P2P_ENCTYPE_RSA_AES; if (unlikely_log(encaeskey==PSYNC_INVALID_ENC_SYM_KEY) || unlikely_log(encoder==PSYNC_CRYPTO_INVALID_ENCODER) || unlikely_log(socket_write_all(sock, &keylen, sizeof(keylen)) || socket_write_all(sock, &enctype, sizeof(enctype)) || socket_write_all(sock, encaeskey->data, encaeskey->datalen))){ if (encaeskey!=PSYNC_INVALID_ENC_SYM_KEY) psync_free(encaeskey); if (encoder!=PSYNC_CRYPTO_INVALID_ENCODER) psync_crypto_aes256_ctr_encoder_decoder_free(encoder); psync_file_close(fd); goto err0; } psync_free(encaeskey); off=0; while (off<packet.filesize){ if (packet.filesize-off<sizeof(buff)) rd=packet.filesize-off; else rd=sizeof(buff); if (unlikely_log(psync_file_read(fd, buff, rd)!=rd)) break; psync_crypto_aes256_ctr_encode_decode_inplace(encoder, buff, rd, off); if (unlikely_log(socket_write_all(sock, buff, rd))) break; off+=rd; } psync_crypto_aes256_ctr_encoder_decoder_free(encoder); psync_file_close(fd); debug(D_NOTICE, "file sent successfuly"); err0: psync_close_socket(sock); }
/* process for function and variable declaration. decl_variable -> type declaration_list ';' declarator_list -> declarator_list ',' declarator | declarator declarator -> identifier | identifier ASSIGN expr_assign */ static struct finsh_node* proc_variable_decl(struct finsh_parser* self) { enum finsh_token_type token; enum finsh_type type; char id[FINSH_NAME_MAX + 1]; struct finsh_node *node; struct finsh_node *end; struct finsh_node *assign; node = NULL; end = NULL; /* get type */ type = proc_type(self); /*process id.*/ if (proc_identifier(self, id) == 0) { /* if add variable failed */ if (finsh_var_insert(id, type) < 0) { finsh_error_set(FINSH_ERROR_VARIABLE_EXIST); } } next_token(token, &(self->token)); switch ( token ) { case finsh_token_type_comma:/*',', it's a variable_list declaration.*/ if (proc_identifier(self, id) == 0) { /* if add variable failed */ if (finsh_var_insert(id, type) < 0) { finsh_error_set(FINSH_ERROR_VARIABLE_EXIST); } } next_token(token, &(self->token)); if ( token == finsh_token_type_assign ) { /* get the right side of assign expression */ assign = proc_assign_expr(self); if (assign != NULL) { struct finsh_node* idnode; idnode = finsh_node_new_id(id); end = make_sys_node(FINSH_NODE_SYS_ASSIGN, idnode, assign); node = end; next_token(token, &(self->token)); } } while ( token == finsh_token_type_comma ) { if (proc_identifier(self, id) == 0) { /* if add variable failed */ if (finsh_var_insert(id, type) < 0) { finsh_error_set(FINSH_ERROR_VARIABLE_EXIST); } } next_token(token, &(self->token)); if ( token == finsh_token_type_assign ) { /* get the right side of assign expression */ assign = proc_assign_expr(self); if (assign != NULL) { struct finsh_node* idnode; idnode = finsh_node_new_id(id); /* make assign expression node */ if (node != NULL) { finsh_node_sibling(end) = make_sys_node(FINSH_NODE_SYS_ASSIGN, idnode, assign); end = finsh_node_sibling(end); } else { end = make_sys_node(FINSH_NODE_SYS_ASSIGN, idnode, assign); node = end; } next_token(token, &(self->token)); } } } check_token(token, &(self->token), finsh_token_type_semicolon); return node; case finsh_token_type_assign:/*'=', it's a variable with assign declaration.*/ { struct finsh_node *idnode; assign = proc_assign_expr(self); if (assign != NULL) { idnode = finsh_node_new_id(id); /* make assign expression node */ end = make_sys_node(FINSH_NODE_SYS_ASSIGN, idnode, assign); node = end; next_token(token, &(self->token)); } while ( token == finsh_token_type_comma ) { if (proc_identifier(self, id) == 0) { /* if add variable failed */ if (finsh_var_insert(id, type) < 0) { finsh_error_set(FINSH_ERROR_VARIABLE_EXIST); } } next_token(token, &(self->token)); if (token == finsh_token_type_assign) { /* get the right side of assign expression */ assign = proc_assign_expr(self); if (assign != NULL) { idnode = finsh_node_new_id(id); /* make assign expression node */ if (node != NULL) { finsh_node_sibling(end) = make_sys_node(FINSH_NODE_SYS_ASSIGN, idnode, assign); end = finsh_node_sibling(end); } else { end = make_sys_node(FINSH_NODE_SYS_ASSIGN, idnode, assign); node = end; } next_token(token, &(self->token)); } } } check_token(token, &(self->token), finsh_token_type_semicolon); return node; } case finsh_token_type_semicolon:/*';', it's a variable declaration.*/ return node; default: finsh_error_set(FINSH_ERROR_EXPECT_TYPE); return NULL; } }