/***************************************** tcp/ip 数据发送 入口参数:data 数据buffer地址 len 长度 最大GTM900C_TCPIP_SEND_DATALEN_MAX link_num 多连接模式时链接序号 单链接模式不起作用,可赋值0 返回参数:正确返回0 *******************************************/ s8 gtm900c_tcpip_send(u8 *data, u16 len, u8 link_num) { u8 databuf[GTM900C_TCPIP_SEND_DATALEN_MAX*2+15]; u16 databuf_len; // memset(buf, 0, GTM900C_TCPIP_SEND_DATALEN_MAX*2); if(len > GTM900C_TCPIP_SEND_DATALEN_MAX) return 1; //选择单\多链接,发送 if(GTM900C_TCPIP_IOMODE & 0x02) { strcpy(databuf, "AT%IPSENDX="); } else { strcpy(databuf, "AT%IPSEND="); } databuf_len = strlen(databuf); //数据转换 if(GTM900C_TCPIP_IOMODE & 0x01) { databuf_len += hex_to_ascii(data, databuf+databuf_len, len); } else { memcpy(databuf+databuf_len, data, len); databuf_len += len; } databuf[databuf_len++] = '\r'; databuf[databuf_len++] = '\0'; at_send_data(databuf, databuf_len); at_return_data(databuf, 4); if(check_string(databuf, "OK", 4)) return 0; else return 2; }
static void show_buf() { uint32_t i; for(i = 0; i < SIZE_BUF; i++) { write_uart(hex_to_ascii(buf[i] & 0xF0) >> 4); write_uart(hex_to_ascii(buf[i] & 0x0F)); write_uart(i%16==0 ? '\n' : ' '); } writeln_uart("######\n\r"); }
/* * \brief Returns ascii string from a number in mpz_t form. * * \param output_str A pointer to the output string. * \param input The number to convert to ascii. */ void mpz_get_ascii(char *output_str, mpz_t input) { int i,j; char *result_str; result_str = mpz_get_str(NULL, HEX_BASE, input); i = 0; j = 0; while (result_str[i] != '\0') { output_str[j] = hex_to_ascii(result_str[i], result_str[i+1]); j += 1; i += 2; } }
int main(){ const char* st = "48656C6C6F3B"; int length = strlen( st); int i; char buf = 0; for( i = 0; i < length; i++){ if ( i % 2 != 0){ printf("%c", hex_to_ascii( buf, st[ i])); } else{ buf = st[ i]; } } binary( 11); binary( 56); binary2( 11); binary2( 56); }
int main(void) { struct cks_config *config = NULL; PGconn *conn = NULL; char *method = NULL; char *content = NULL; unsigned long content_length = 0; PGresult *result = NULL; unsigned char stmt[] = "select server,sync_priority from cks_other_servers order by sync_priority"; struct name_value_pair_dllst *form = NULL; /* cgi vars */ char *hostname = NULL; char *srvr_type = NULL; char *priority = NULL; int rslt = 0; int nts = 0; config = (struct cks_config *)malloc(sizeof(struct cks_config)); if(config == NULL) { do_error_page(_("syn_manage: malloc call failed: out of memroy!\n")); return -1; } rslt = init_config(&config); if(rslt == -1) { fprintf(stderr,_("sync_manage: Non-Fatal Error: Failed to read config.\n")); fprintf(stderr,_("sync_manage: Using default configuration information.\n")); } /* Make the DB Connection. */ conn = db_connect(config); if(conn == NULL) { fprintf(stderr,"Failed to connect to the db.\n"); if(config != NULL) { free(config); } return -1; } method = getenv("REQUEST_METHOD"); if(method == NULL) { do_error_page(_("Request Method was Null.\n<P><P>Exiting...")); db_disconnect(conn); if(config != NULL) { free(config); } return 0; } else if(strcmp(method,"GET") == 0) { /* Just Fall Through and Print the Form */ } else if(strcmp(method,"POST") == 0) { content_length = atoi(getenv("CONTENT_LENGTH")); if(content_length > 800) { do_error_page(_("Content Length expectation exceeded\n")); db_disconnect(conn); if(config != NULL) { free(config); } return -1; } content = (char *)malloc(content_length+1); if(content == NULL) { do_error_page(_("Server was unable to malloc memory. Server out of memory.")); db_disconnect(conn); if(config != NULL) { free(config); } return -1; } rslt = fread(content,1,content_length,stdin); if(rslt == 0) { do_error_page(_("Server was unable to read content.")); if(config != NULL) free(config); return -1; } content[content_length] = '\0'; hex_to_ascii(content); /* Test value for SQL injection */ if( (strchr(content, '\'') != NULL) || (strchr(content, ';') != NULL) ) { do_error_page(_("The characters ' and ; are currently not allowed in queries.")); db_disconnect(conn); if(config != NULL) free(config); if(content != NULL) free(content); return 0; } form = parse_name_value_pairs(content); if(form == NULL) { /* No Request, so Echo Default Page */ do_error_page(_("Failed to Parse CGI Form. Are you using a Standard Web Browser?\n")); db_disconnect(conn); if(config != NULL) free(config); if(content != NULL) free(content); return -1; } hostname = get_value(form,"hostname"); srvr_type = get_value(form,"srvr_type"); priority = get_value(form,"priority"); rslt = insert_into_other_server(conn,hostname,srvr_type,priority); if(rslt == -1) { do_error_page("Failed To Insert new record into database."); db_disconnect(conn); if(config != NULL) free(config); if(content != NULL) free(content); return -1; } /* Now Fall Through and Re-Print the Form */ } else { do_error_page("Unknown Method."); db_disconnect(conn); if(config != NULL) free(config); if(content != NULL) free(content); return -1; } printf("content-type: text/html\n\n"); printf(_("<html><head><title>CryptNET OpenPGP Public Key Server</title></head>\n")); printf("<body bgcolor=\"#FFFFFF\">\n"); printf(_("<center><H2>CryptNET Keyserver Administration</h2></center>\n")); printf("<hr size=\"1\" width=\"100%%\">\n"); printf("<center>\n"); printf(_("[ <a href=\"sync.html\">Manage Sync Hosts</a> ]\n")); printf(_("[ <a href=\"delete.html\">Delete A Key From This Server</a> ]\n")); printf(_("[ <a href=\"stats.cgi\">Stats On This Server</a> ]\n")); printf(_("[ <a href=\"index.html\">Admin Home</a> ]\n")); printf(_("[ <a href=\"/index.html\">Home</a> ]\n")); printf("</center>\n"); printf("<hr size=\"1\" width=\"100%%\">\n"); printf(_("<h3>Synchronization Host Management</h3>\n")); result = PQexec(conn, stmt); if(PQresultStatus(result) != PGRES_TUPLES_OK) { do_error_page("Database Query failed.\n"); fprintf(stderr,_("Command didn't return tuples properly\n")); PQclear(result); db_disconnect(conn); if(config != NULL) free(config); if(content != NULL) free(content); return -1; } printf("<table width=\"90%%\" cols=\"5\" border=\"1\">\n"); printf(_("<tr><th width=\"15%%\" align=\"CENTER\">Delete</th><th align=\"CENTER\">Server</th><th align=\"CENTER\">Sync Priority</th><th align=\"center\">Server Type</th></tr>\n")); nts = PQntuples(result); if(nts == 0) { printf("</table>\n"); printf("<p>No host synchronization records found.</p>\n"); } else if(nts == 1) { printf("<form method=\"post\" action=\"sync_manage.cgi\">\n"); printf("<tr><td><input type=\"checkbox\" name=\"%s\"></td><td>%s</td><td>%s</td><td>%s</td><td><input type=\"submit\" value=\"Update\"></td></tr>\n",PQgetvalue(result,0,0),PQgetvalue(result,0,0),PQgetvalue(result,0,1),PQgetvalue(result,0,2)); printf("</form>\n"); } else if(nts > 1) { int i = 0; for(i = 0;i<nts;i++) { printf("<form method=\"post\" action=\"sync_manage.cgi\">\n"); printf("<tr><td><input type=\"checkbox\" name=\"%s\"></td><td>%s</td><td>%s</td><td>%s</td><td><input type=\"submit\" value=\"Update\"></td></tr>\n",PQgetvalue(result,i,0),PQgetvalue(result,i,0),PQgetvalue(result,i,1),PQgetvalue(result,i,2)); printf("</form>\n"); } } else { printf("</table>\n"); fprintf(stderr, _("sync_manage.c: Weird Tuples Returned.\n")); fprintf(stderr,_("Weird Tuples Returned! (negative)\n")); do_error_page("The database query returned in an error state.\n"); PQclear(result); PQfinish(conn); return -1; } /* close up and clean up database connection, we're done with it */ PQclear(result); db_disconnect(conn); /* Print the input form */ printf("</table>\n"); printf("<p></p>\n"); printf("<p></p>\n"); printf("<hr size=\"1\" width=\"100%%\">\n"); printf("<form method=\"POST\" action=\"sync_manage.cgi\">\n"); printf(_("<h3>Add New Synchronization Host</h3>\n")); printf(_("<p>Add Host: <input name=\"hostname\" type=\"text\" length=\"60\" maxlen=\"60\"></p>\n")); printf(_("<p>Priority:\n")); printf("<select name=\"priority\">\n"); printf("<option value=\"1\">1</option>\n"); printf("<option value=\"2\">2</option>\n"); printf("<option value=\"3\">3</option>\n"); printf("<option value=\"4\">4</option\n"); printf("<option value=\"5\">5</option selected>\n"); printf("<option value=\"6\">6</option>\n"); printf("<option value=\"7\">7</option\n"); printf("<option value=\"8\">8</option>\n"); printf("<option value=\"9\">9</option>\n"); printf("<option>10</option>\n"); printf("</select></p>\n"); printf(_("<p>Server Type: <select name=\"srvr_type\">\n")); printf("<option value=\"1\">CryptNET Key Server</option>\n"); printf("<option value=\"2\">PKS Key Server</option>\n"); printf("<option value=\"3\">SKS Key Server</option>\n"); printf("</select></p>\n"); printf("<br></br>\n"); printf(_("<input type=\"submit\" value=\"Update Sync List\">\n")); printf("</form>\n"); printf("<hr size=\"1\" width=\"100%%\">\n"); printf("<center>\n"); printf(_("[ <a href=\"sync.html\">Manage Sync Hosts</a> ]\n")); printf(_("[ <a href=\"delete.html\">Delete A Key From This Server</a> ]\n")); printf(_("[ <a href=\"stats.cgi\">Stats On This Server</a> ]\n")); printf(_("[ <a href=\"index.html\">Admin Home</a> ]\n")); printf(_("[ <a href=\"/index.html\">Home</a> ]\n")); printf("</center>\n"); printf("<hr size=\"1\" width=\"100%%\">\n"); printf(_("<center><a href=\"http://keyserver.cryptnet.net/\">CryptNET Key Server Network</a></center>\n")); printf("</body></html>\n"); /* Free Memory and Exit */ if(content != NULL) { free(content); } if(config != NULL) { free(config); } return 0; }
TToken* get_token() { if(token_buffer != NULL) { token = token_buffer; token_buffer = NULL; return token; } TString buffer; States state = S_START; char a[2]; // array for hex number int c, i=0; initString(&buffer, STR_DEFAULT_SIZE); token->data = buffer.data; while(1) { c = fgetc(fp); switch(state) { case S_START: switch (c) { case EOF: token->type = TOKEN_EOF; return token; case '+': token->type = TOKEN_ADD; return token; case '-': token->type = TOKEN_SUB; return token; case '*': token->type = TOKEN_MUL; return token; case '/': state = S_DIV; break; case '=': state = S_ASSIGN; break; case '!': state = S_EXCM; break; case '<': state = S_LESS; break; case '>': state = S_GREAT; break; case '(': token->type = TOKEN_LROUND_BRACKET; return token; case ')': token->type = TOKEN_RROUND_BRACKET; return token; case '{': token->type = TOKEN_LCURLY_BRACKET; return token; case '}': token->type = TOKEN_RCURLY_BRACKET; return token; case ',': token->type = TOKEN_COMMA; return token; case ';': token->type = TOKEN_SEMICOLON; return token; case '"': state = S_QUOT; break; default: if(isdigit(c)) { state = S_INT; insertIntoString(&buffer, c); } else if (isalpha(c) || c == '_') { state = S_IDENT; insertIntoString(&buffer, c); } else if (isspace(c)) break; else state = S_ERROR; } break; //********************************************** case S_INT: // INTEGER if (isdigit(c)) { insertIntoString(&buffer, c); } else if(c == '.') { insertIntoString(&buffer, c); state = S_DOT; } else if(c == 'E' || c == 'e') { insertIntoString(&buffer, c); state = S_EXPO_E; } else { insertIntoString(&buffer, 0); ungetc(c,fp); token->type = TOKEN_INT_VALUE; token->data = buffer.data; return token; } break; //************************************************* case S_DOT: // number DOT if (isdigit(c)) { insertIntoString(&buffer, c); state = S_DBL; } else { state = S_ERROR; } break; //************************************************* case S_DBL: // DOUBLE if (isdigit(c)) { insertIntoString(&buffer, c); } else if(c == 'E' || c == 'e') { insertIntoString(&buffer, c); state = S_EXPO_E; } else { insertIntoString(&buffer, 0); ungetc(c,fp); token->type = TOKEN_DOUBLE_VALUE; token->data = buffer.data; return token; } break; //****************************************************** case S_EXPO_E: // state S_EXPO_E if (isdigit(c)) { insertIntoString(&buffer, c); state = S_EXPO; } else if (c == '+' || c == '-') { insertIntoString(&buffer, c); state= S_EXPO_M; } else { state = S_ERROR; } break; //***************************************************** case S_EXPO_M: // state S_EXPO_M if (isdigit(c)) { insertIntoString(&buffer, c); state= S_EXPO; } else { state=S_ERROR; } break; //***************************************************** case S_EXPO: // state S_EXPO if (isdigit(c)) insertIntoString(&buffer, c); else { insertIntoString(&buffer, 0); ungetc(c,fp); token->type = TOKEN_DOUBLE_VALUE; token->data = buffer.data; return token; } break; //*************************************************** case S_IDENT: //STATE IDENTIFIER if (isalnum(c) || c == '_') { insertIntoString(&buffer, c); } else { insertIntoString(&buffer, 0); token->type = TOKEN_IDENTIFIER; token->data = buffer.data; keyword_check(); ungetc(c,fp); return token; } break; //************************************************************* case S_DIV: // DIVIDE if (c == '/') state = S_LCOM; else if (c == '*') state = S_LBC; else { ungetc(c,fp); token->type = TOKEN_DIV; return token; } break; //*************************************************** case S_LCOM: // LINE COMMENT if (c == '\n') state = S_START; break; //**************************************************** case S_LBC: // LEFT BLOCK COMMENT if (c == '*') state = S_RBC; if(c == EOF) state = S_ERROR; break; //**************************************************** case S_RBC: //RIGHT BLOCK COMMENT if (c == '/') state = S_START; else if (c == EOF) state = S_ERROR; else if (c == '*'); else state = S_LBC; break; //**************************************************** case S_ASSIGN: // ASSIGNMENT if (c == '=') { token->type = TOKEN_EQUAL; return token; } else { ungetc(c,fp); token->type = TOKEN_ASSIGN; return token; } break; //**************************************************** case S_EXCM: // EXCLAMATION MARK if (c == '=') { token->type = TOKEN_NOT_EQUAL; return token; } else state = S_ERROR; break; //**************************************************** case S_LESS: // LESS THAN if (c=='=') { token->type=TOKEN_LESS_EQUAL; return token; } else if (c == '<') { token->type=TOKEN_COUT_BRACKET; return token; } else { ungetc(c,fp); token->type = TOKEN_LESS; return token; } break; //**************************************************** case S_GREAT: //GREATER THAN if (c=='=') { token->type = TOKEN_GREATER_EQUAL; return token; } else if (c == '>') { token->type = TOKEN_CIN_BRACKET; return token; } else { ungetc(c,fp); token->type = TOKEN_GREATER; return token; } break; //**************************************************** case S_QUOT: //QUOTATION if (c == '"') { insertIntoString(&buffer, 0); token->type = TOKEN_STRING_VALUE; token->data = buffer.data; return token; } else if(c == EOF) { state = S_ERROR; } else if(c == '\\') { state = S_ESCAPE; } else if(c > 31) { insertIntoString(&buffer, c); } else state = S_ERROR; break; //**************************************************** case S_ESCAPE: //ESCAPE SEQUENCES if (c == 't') { insertIntoString(&buffer, '\t'); state = S_QUOT; } else if (c == '"') { insertIntoString(&buffer, '"'); state = S_QUOT; } else if (c == '\\') { insertIntoString(&buffer, '\\'); state = S_QUOT; } else if (c == 'n') { insertIntoString(&buffer, '\n'); state = S_QUOT; } else if (c == 'x') { state = S_HEX_NUMBER; } else { state=S_ERROR; } break; //**************************************************** case S_HEX_NUMBER: // HEXADECIMAL NUMBER if (isxdigit(c) && (i < 2)) // if is hexadigit and i<2 { a[i]=c; i++; if (i == 2) { if ((a[0] == '0') && (a[1] == '0')) state=S_ERROR; else { char hta = hex_to_ascii(a[0], a[1]); insertIntoString(&buffer, hta); state = S_QUOT; i = 0; } } } else { state=S_ERROR; } break; //**************************************************** case S_ERROR: // ERROR exit_error(E_LEX); } } }
int main(void) { struct cks_config *config = NULL; PGconn *conn = NULL; char *method = NULL; char *content = NULL; unsigned long content_length = 0; char *name = NULL; char *val = NULL; char *value = NULL; char *debug_val = NULL; int rslt = 0; int tmp_var = 0; config = (struct cks_config *)malloc(sizeof(struct cks_config)); if(config == NULL) { fprintf(stderr,_("cks_export: Fatal Error: Malloc Call Failed: Out of memroy.\n")); return -1; } rslt = init_config(&config); if(rslt == -1) { fprintf(stderr,_("search: Non-Fatal Error: Failed to read config.\n")); fprintf(stderr,_("search: Using default configuration information.\n")); } method = getenv("REQUEST_METHOD"); if(method == NULL) { /* Make the DB Connection. */ conn = db_connect(config); if(conn == NULL) { fprintf(stderr,"Failed to connect to the db.\n"); if(content != NULL) free(content); if(config != NULL) free(config); return -1; } debug_val = (char *)malloc(50); sprintf(debug_val,"%s","92987FBD"); search_by_keyid(conn,debug_val,config); /*search_by_uid(conn,"*****@*****.**",config); */ free(config); return 0; } if(method == NULL) { fprintf(stderr, _("search.c: Request Method is Null.\nExiting...\n")); free(config); return -1; } else if(strcmp(method,"GET") == 0) { content_length = strlen(getenv("QUERY_STRING")); if(content_length > 300) { do_error_page(_("Content Length expectation exceeded\n")); free(config); return -1; } content = (char *)malloc(content_length+1); if(content == NULL) { do_error_page(_("Server was unable to malloc memory. Server out of memory.")); free(config); return -1; } memset(content,0x00,content_length+1); strncpy(content,getenv("QUERY_STRING"),content_length); } else if(strcmp(method,"POST") == 0) { content_length = atoi(getenv("CONTENT_LENGTH")); if(content_length > 300) { do_error_page(_("Content Length expectation exceeded\n")); if(config != NULL) free(config); return -1; } content = (char *)malloc(content_length+1); if(content == NULL) { do_error_page(_("Server was unable to malloc memory. Server out of memory.")); if(content != NULL) free(content); if(config != NULL) free(config); return -1; } rslt = fread(content,1,content_length,stdin); if(rslt == 0) { do_error_page(_("Error reading content.")); if(content != NULL) free(content); if(config != NULL) free(config); return -1; } content[content_length] = '\0'; } else { do_error_page(_("Unknown Method.")); free(config); return -1; } hex_to_ascii(content); name = strtok(content,"&"); if(name == NULL) { fprintf(stderr,"name was null\n"); if(content != NULL) free(content); if(config != NULL) free(config); return -1; } val = strtok('\0',"\0"); if(!(val)) { do_error_page(_("Error: NULL Search value. Please, hit the back button on your browser and search again.")); if(content != NULL) free(content); if(config != NULL) free(config); return 0; } strtok(val,"="); value = strtok('\0',"\0"); if(!(value)) { do_error_page(_("Error: NULL Search value. Please, hit the back button on your browser and search again.")); if(content != NULL) free(content); if(config != NULL) free(config); return 0; } /* Test value for SQL injection */ if( (strchr(value, '\'') != NULL) || (strchr(value, ';') != NULL) ) { do_error_page(_("The characters ' and ; are currently not allowed in queries.")); if(content != NULL) free(content); if(config != NULL) free(config); return 0; } /* Make the DB Connection. */ conn = db_connect(config); if(conn == NULL) { fprintf(stderr,"Failed to connect to the db.\n"); if(content != NULL) free(content); if(config != NULL) free(config); return -1; } print_header(_("Search Results:")); if(strcmp("stype=uid",name) == 0) { search_by_uid(conn,value,config); } else if(strcmp("stype=fp",name) == 0) { rslt = search_by_fingerprint(conn,value,config); if(rslt != 0) { fprintf(stderr,"Function search_by_fingerprint() returned and error: %d\n", rslt); if(content != NULL) free(content); if(config != NULL) free(config); return -1; } } else if(strcmp("stype=keyid_4b",name) == 0) { if(memcmp(value,"00000000",8) == 0) { print_pgp5_x509_note(); } else { search_by_keyid(conn,value,config); } } else if(strcmp("stype=keyid_8b",name) == 0) { search_by_fkeyid(conn,value,config); } else if(strcmp("stype=keyring",name) == 0) { search_ret_keyring(conn,value,config); } else if(strcmp("stype=signers",name) == 0) { search_ret_with_signers(conn,value,config); } else { do_error_page(_("Invalid query. Search type not understood.")); db_disconnect(conn); if(content != NULL) free(content); if(config != NULL) free(config); return 0; } print_footer(); db_disconnect(conn); if(content != NULL) free(content); if(config != NULL) free(config); return 0; }
void dump(unsigned char* in_buff, char* out_buff, unsigned int len) { while(len--) { *(out_buff + len + len + 1) = hex_to_ascii(*(in_buff + len) & 0x0F); *(out_buff + len + len) = hex_to_ascii(*(in_buff + len) >> 4); } }
int main(void) { struct cks_config *config = NULL; PGconn *conn = NULL; char *method = NULL; char *content = NULL; unsigned int content_length = 0; char *name = NULL; char *val = NULL; char *value = NULL; unsigned char *fingerprint = NULL; int rslt = 0; int tmp_var = 0; config = (struct cks_config *)malloc(sizeof(struct cks_config)); if(config == NULL) { fprintf(stderr, "Failed to malloc config\n"); return -1; } rslt = init_config(&config); if(rslt == -1) { fprintf(stderr,_("cks: cks_keyimg.c: Non-Fatal Error: Failed to read config.\n")); fprintf(stderr,_("cks: cks_keyimg.c: Using default configuration information.\n")); } conn = db_connect(config); if(conn == NULL) { do_error_page(_("Failed to connect to postgres database.")); fprintf(stderr,_("cks: cks_keyimg.c: Connection to database failed.\n")); fprintf(stderr,"cks: cks_keyimg.c: %s", PQerrorMessage(conn)); db_disconnect(conn); if(config != NULL) free(config); return -1; } #ifdef DEBUG fingerprint = (char *)malloc(100); strcpy(fingerprint,"72F871AD580481D14C84CADC08B779A592987FBD"); #else method = getenv("REQUEST_METHOD"); if(method == NULL) { fprintf(stderr,"Method was null.\n"); db_disconnect(conn); if(config != NULL) free(config); return 0; } if(method == NULL) { fprintf(stderr, _("search.c: Request Method is Null.\nExiting...\n")); db_disconnect(conn); if(config != NULL) free(config); return -1; } if(strcmp(method,"GET") == 0) { content_length = strlen(getenv("QUERY_STRING")); if(content_length > 300) { do_error_page(_("Content Length expectation exceeded\n")); db_disconnect(conn); if(config != NULL) free(config); return -1; } /* TODO: Make sure content length is not too small */ content = (char *)malloc(content_length+1); if(content == NULL) { do_error_page(_("Server was unable to malloc memory. Server out of memory.")); db_disconnect(conn); if(config != NULL) free(config); return -1; } strncpy(content,getenv("QUERY_STRING"),content_length); } hex_to_ascii(content); value = (unsigned char *)malloc(content_length+1); if(value == NULL) { do_error_page("cks_keyimg.c: Malloc call failed. Out of Memory.\n"); db_disconnect(conn); if(config != NULL) free(config); return -1; } fingerprint = (unsigned char *)malloc(content_length+1); if(fingerprint == NULL) { do_error_page("cks_keyimg.c: Malloc call failed. Out of Memory.\n"); db_disconnect(conn); if(config != NULL) free(config); return -1; } strncpy(value,content,41); strncpy(fingerprint,content,41); if(!(value)) { do_error_page(_("Error: NULL Search value. Please, hit the back button on your browser and search again.")); if(value != NULL) { free(value); } if(fingerprint != NULL) { free(fingerprint); } db_disconnect(conn); if(config != NULL) free(config); return 0; } /* TODO: Move this up higher so we don't have to do the allocs if there's bad code */ /* Test value for SQL injection */ if( (strchr(value, '\'') != NULL) || (strchr(value, ';') != NULL) ) { do_error_page(_("The characters ' and ; are currently not allowed in queries.")); if(value != NULL) { free(value); } if(fingerprint != NULL) { free(fingerprint); } db_disconnect(conn); if(config != NULL) free(config); return 0; } #endif /* Search by Fingerprint */ remove_spaces(fingerprint); if(((strlen(fingerprint)) != 32) && ((strlen(fingerprint)) != 40)) { do_error_page(_("The characters ' and ; are currently not allowed in queries.")); if(value != NULL) { free(value); } if(fingerprint != NULL) { free(fingerprint); } db_disconnect(conn); if(config != NULL) free(config); return -1; } retrieve_key_and_display(conn,fingerprint,1,config); if(value != NULL) { free(value); } if(fingerprint != NULL) { free(fingerprint); } db_disconnect(conn); if(config != NULL) { free(config); } return 0; }
void FreeIMUReadCalibration(void) { char outbuff[100]; int pos=0; EEPROMGet(VAR_CALIBRATION, (uint8_t*)&calibration); // Accelerometer offsets memcpy(outbuff, "acc offset: ", 12); pos+=12; hex_to_ascii((unsigned char*)&calibration.offsets[0], &outbuff[pos], sizeof(uint16_t)); pos += sizeof(uint16_t)*2; outbuff[pos++] = ','; hex_to_ascii((unsigned char*)&calibration.offsets[1], &outbuff[pos], sizeof(uint16_t)); pos += sizeof(uint16_t)*2; outbuff[pos++] = ','; hex_to_ascii((unsigned char*)&calibration.offsets[2], &outbuff[pos], sizeof(uint16_t)); pos += sizeof(uint16_t)*2; outbuff[pos++] = '\n'; VCP_write(outbuff, pos); // Magnetomer offsets pos=0; memcpy(outbuff, "mag offset: ", 12); pos+=12; hex_to_ascii((unsigned char*)&calibration.offsets[3], &outbuff[pos], sizeof(uint16_t)); pos += sizeof(uint16_t)*2; outbuff[pos++] = ','; hex_to_ascii((unsigned char*)&calibration.offsets[4], &outbuff[pos], sizeof(uint16_t)); pos += sizeof(uint16_t)*2; outbuff[pos++] = ','; hex_to_ascii((unsigned char*)&calibration.offsets[5], &outbuff[pos], sizeof(uint16_t)); pos += sizeof(uint16_t)*2; outbuff[pos++] = '\n'; VCP_write(outbuff, pos); // Accelerometer scale pos=0; memcpy(outbuff, "acc scale: ", 11); pos+=11; hex_to_ascii((unsigned char*)&calibration.scales[0], &outbuff[pos], sizeof(float)); pos += sizeof(float)*2; outbuff[pos++] = ','; hex_to_ascii((unsigned char*)&calibration.scales[1], &outbuff[pos], sizeof(float)); pos += sizeof(float)*2; outbuff[pos++] = ','; hex_to_ascii((unsigned char*)&calibration.scales[2], &outbuff[pos], sizeof(float)); pos += sizeof(float)*2; outbuff[pos++] = '\n'; VCP_write(outbuff, pos); //Magnetometer scale pos=0; memcpy(outbuff, "mag scale: ", 11); pos+=11; hex_to_ascii((unsigned char*)&calibration.scales[3], &outbuff[pos], sizeof(float)); pos += sizeof(float)*2; outbuff[pos++] = ','; hex_to_ascii((unsigned char*)&calibration.scales[4], &outbuff[pos], sizeof(float)); pos += sizeof(float)*2; outbuff[pos++] = ','; hex_to_ascii((unsigned char*)&calibration.scales[5], &outbuff[pos], sizeof(float)); pos += sizeof(float)*2; outbuff[pos++] = '\n'; VCP_write(outbuff, pos); }
void FreeIMUSendYawPitchRoll(int count) { Point3df gyro_xyz; Point3df accel_xyz; Point3df mag_xyz; char outbuff[60]; int pos=0, i; if(filter_init == 0){ memset(&gyro_xyz_filtered, 0, sizeof(Point3df)); filter_init = 1; } ExpLedOn(ORANGE_LED); for(i=0; i<count; i++){ ExpLedToggle(ORANGE_LED); gyro_read(&gyro_xyz); accelerometer_read(&accel_xyz); MagPointRaw(&mag_xyz); // Apply calibration values accel_xyz.x = (accel_xyz.x - (float)calibration.offsets[0]) / calibration.scales[0]; accel_xyz.y = (accel_xyz.y - (float)calibration.offsets[1]) / calibration.scales[1]; accel_xyz.z = (accel_xyz.z - (float)calibration.offsets[2]) / calibration.scales[2]; mag_xyz.x = (mag_xyz.x - (float)calibration.offsets[3]) / calibration.scales[3]; mag_xyz.y = (mag_xyz.y - (float)calibration.offsets[4]) / calibration.scales[4]; mag_xyz.z = (mag_xyz.z - (float)calibration.offsets[5]) / calibration.scales[5]; // Normalize acceleration measurements so they range from 0 to 1 float accxnorm = accel_xyz.x/sqrtf(accel_xyz.x*accel_xyz.x+accel_xyz.y*accel_xyz.y+accel_xyz.z*accel_xyz.z); float accynorm = accel_xyz.y/sqrtf(accel_xyz.x*accel_xyz.x+accel_xyz.y*accel_xyz.y+accel_xyz.z*accel_xyz.z); // calculate pitch and roll float Pitch = asinf(-accxnorm); float Roll = asinf(accynorm/cosf(Pitch)); // tilt compensated magnetic sensor measurements float magxcomp = mag_xyz.x*cosf(Pitch)+mag_xyz.z*sinf(Pitch); float magycomp = mag_xyz.x*sinf(Roll)*sinf(Pitch)+mag_xyz.y*cosf(Roll)-mag_xyz.z*sinf(Roll)*cosf(Pitch); // arctangent of y/x converted to degrees float heading = (float)(180*atan2f(magycomp,magxcomp)/PI); if(heading < 0) heading +=360; previous_time = (float)HAL_GetTick(); /* float tau=0.075; float a=0.0; float Complementary(float newAngle, float newRate,int looptime) { float dtC = float(looptime)/1000.0; a=tau/(tau+dtC); x_angleC= a* (x_angleC + newRate * dtC) + (1-a) * (newAngle); return x_angleC; } */ /* // Adjust the gyro readings with a complimentary filter // angle = 0.98 *(angle+gyro*dt) + 0.02*acc if(previous_time > 0){ float dt = ((float)HAL_GetTick() - previous_time)/1000000; gyro_xyz.x = 0.98 *(gyro_xyz.x+gyro_xyz.x*dt) + 0.02*accel_xyz.x; gyro_xyz.y = 0.98 *(gyro_xyz.y+gyro_xyz.y*dt) + 0.02*accel_xyz.y; gyro_xyz.z = 0.98 *(gyro_xyz.z+gyro_xyz.z*dt) + 0.02*accel_xyz.z; } */ pos = 0; hex_to_ascii((unsigned char*)&heading, &outbuff[pos], sizeof(float)); pos += sizeof(float)*2; outbuff[pos++] = ','; hex_to_ascii((unsigned char*)&Pitch, &outbuff[pos], sizeof(float)); pos += sizeof(float)*2; outbuff[pos++] = ','; hex_to_ascii((unsigned char*)&Roll, &outbuff[pos], sizeof(float)); pos += sizeof(float)*2; outbuff[pos++] = ','; outbuff[pos++] = '\r'; outbuff[pos++] = '\n'; VCP_write(outbuff, pos); } ExpLedOff(ORANGE_LED); }