int test_getline(void) { STR_DEFINE(s1, 10); STR_DEFINE(s2, 10); FILE *fp; int ret; int line_nr; dump_str(s1, "s1"); dump_str(s2, "s2"); T_GETLINE(""); T_GETLINE("A\nB\rC\r\nD\n\rE"); T_GETLINE("A\nB\rC\r\nD\n\rE\n"); T_GETLINE("A\nB\rC\r\nD\n\rE\r"); T_GETLINE("A\nB\rC\r\nD\n\rE\n\r"); T_GETLINE("A\nB\rC\r\nD\n\rE\r\n"); T_GETLINE("1234567890" "1234567890" "1234567890" "1234567890"); STR_DELETE(s1); mu_assert_ptr_null(s1); STR_DELETE(s2); mu_assert_ptr_null(s2); return MU_PASS; }
void packet_client_authenticate(unsigned char* buff, int len) { /* first 2 bytes are unknown */ buff += 2; printf("Account authenticate request:\n"); printf(" Account Name: %s\n", dump_str(&buff)); printf(" Password: %s\n", dump_str(&buff)); send_billinginfo_request(); }
static int __noinstrument print_trigger(char* buf, int len, struct kfi_trigger* t, int start_trigger) { char trigbuf[80]; switch (t->type) { case TRIGGER_DEV: sprintf(trigbuf, "system call\n"); break; case TRIGGER_TIME: sprintf(trigbuf, "time at %lu usec from %s\n", t->time, start_trigger ? "boot" : "start trigger"); break; case TRIGGER_FUNC_ENTRY: sprintf(trigbuf, "entry to function 0x%08lx\n", (unsigned long)t->func_addr); break; case TRIGGER_FUNC_EXIT: sprintf(trigbuf, "exit from function 0x%08lx\n", (unsigned long)t->func_addr); break; case TRIGGER_LOG_FULL: sprintf(trigbuf, "log full\n"); break; default: sprintf(trigbuf, "?\n"); break; } dump_str(buf, len, "Logging %s at %lu usec by %s", (start_trigger ? "started" : "stopped"), t->mark, trigbuf); return len; }
/** * Berkley DB's db_dump format * http://www.sleepycat.com/docs/utility/db_dump.html */ static void do_dbdump(MDBM *db, FILE *fp) { kvpair kv; fputs("format=print\n", fp); fputs("type=hash\n", fp); fprintf(fp, "mdbm_pagesize=%d\n", mdbm_get_page_size(db)); fprintf(fp, "mdbm_pagecount=%d\n", (int)(mdbm_get_size(db) / mdbm_get_page_size(db))); /* fprintf(fp, "mdbm_largeobj=%d\n", */ /* (db->m_flags & _MDBM_LARGEOBJ) ? 1 : 0); */ fputs("HEADER=END\n", fp); for (kv = mdbm_first(db); kv.key.dptr != NULL; kv = mdbm_next(db)) { dump_str(kv.key, fp); dump_str(kv.val, fp); } }
void packet_client_billinginfo(unsigned char* buff, int len) { unsigned char rsa_out[1024]; unsigned char depad_out[1024]; unsigned char outbuff[4096]; unsigned long x, y; int err; int chunk_size; int outpos = 0; //bytes_out(buff, len); /* first two bytes are unknown */ buff += 2; len -= 2; /* key is made up of blocks which are padded then crypted. They come on the wire as 2 bytes size (net order) then data */ while (len > 0) { chunk_size = (buff[0] << 8) | buff[1]; buff += 2; len -= 2; x = sizeof(rsa_out); if ((err = rsa_exptmod(buff, chunk_size, rsa_out, &x, PK_PRIVATE, &key)) != CRYPT_OK) { printf("rsa_exptmod failed: %s\n", error_to_string(err)); return; } y = sizeof(depad_out); if ((err = rsa_depad(rsa_out, x, depad_out, &y)) != CRYPT_OK) { printf("rsa_depad failed: %s\n", error_to_string(err)); return; } memcpy(&outbuff[outpos], depad_out, y); outpos += y; //printf("packet_client_billinginfo has %lu bytes\n", y); buff += chunk_size; len -= chunk_size; } buff = outbuff; printf("Billing Info:\n"); printf(" Account Name: %s\n", dump_str(&buff)); printf(" Password: %s\n", dump_str(&buff)); printf(" Cardholder's Name: %s\n", dump_str(&buff)); printf(" CreditCard Number: %s\n", dump_str(&buff)); printf(" Expiration Date: %s/", dump_str(&buff)); printf("%s\n", dump_str(&buff)); printf(" Billing cycle: %s\n", dump_str(&buff)); }
int test_decode_int() { const size_t good_n = 12; const str good_in[] = { STR_C(1, "\x00"), STR_C(1, "\x01"), STR_C(1, "\xFE"), STR_C(2, "\xFF\x00"), STR_C(2, "\xFF\x01"), STR_C(1, "\x01"), STR_C(1, "\x7E"), STR_C(2, "\x7F\x00"), STR_C(2, "\x7F\x7F"), STR_C(3, "\x7F\x80\x01"), STR_C(3, "\x7F\x81\x01"), STR_C(6, "\xFF\xF2\x81\xC0\x80\x01"), }; const size_t good_pb[] = { 8,8,8,8,8, 7,7,7,7,7,7, 4, }; const uint8_t good_pf[] = { 0,0,0,0,0, 0,0,0,0,0,0, 0xF0, }; const HPACK_INT_T good_out[] = { 0x00, 0x01, 0xFE, 0xFF, 0x100, 0x01, 0x7E, 0x7F, 0xFE, 0xFF, 0x100, 0x10100101, }; int hpack_decoder_error; char match; int retval=0; size_t i; HPACK_INT_T result; uint8_t pf; printf("\n" BOLD "**\n** DECODE INT\n**\n" NORMAL "\n"); for (i = 0; i < good_n; i++) { printf(BOLD "Test %d: " NORMAL, (int)i); /* aha! sending uninitialised buff to my function! */ hpack_decoder_error = hpack_decode_int(good_in[i].ptr, good_in[i].length, NULL, good_pb[i], &result, &pf); if (hpack_decoder_error) { printf(RED "error: %d" NORMAL "\n", hpack_decoder_error); retval ++; } else { if (result == good_out[i]) { if (pf == good_pf[i]) { match = 1; } else { match = 0; printf(RED "prefix was %02x, expected %02x" NORMAL "\n", pf, good_pf[i]); } } else { match = 0; printf(RED "returned %x bytes, expected %x" NORMAL "\n", (int)result, (int)good_out[i]); } if (!match) { dump_str(good_in[i], '<', 0); printf(" > %02X | %d\n", pf, (int)result); printf(" ~ %02X | %d\n", good_pf[i], (int)(good_out[i])); printf("\n"); } else { printf(GREEN "PASS" NORMAL "\n"); } retval += (!match); } } return retval; }
int test_encode_int() { const size_t good_n = 12; const HPACK_INT_T good_in[] = { 0x00, 0x01, 0xFE, 0xFF, 0x100, 0x01, 0x7E, 0x7F, 0xFE, 0xFF, 0x100, 0x10100101, }; const size_t good_pb[] = { 8,8,8,8,8, 7,7,7,7,7,7, 4, }; const uint8_t good_pf[] = { 0,0,0,0,0, 0,0,0,0,0,0, 0xF0, }; const str good_out[] = { STR_C(1, "\x00"), STR_C(1, "\x01"), STR_C(1, "\xFE"), STR_C(2, "\xFF\x00"), STR_C(2, "\xFF\x01"), STR_C(1, "\x01"), STR_C(1, "\x7E"), STR_C(2, "\x7F\x00"), STR_C(2, "\x7F\x7F"), STR_C(3, "\x7F\x80\x01"), STR_C(3, "\x7F\x81\x01"), STR_C(6, "\xFF\xF2\x81\xC0\x80\x01"), }; const size_t bad_n = 4; const HPACK_INT_T bad_in[] = { 0, 0, 0, 0, }; const size_t bad_pb[] = { 0, /* < 1 */ 9, /* > 8 */ 8, 7, }; const uint8_t bad_pf[] = { 0, 0, 0x01, /* prefix sets masked bits */ 0xC0, /* prefix sets masked bits */ }; const int bad_out[] = { 4, /* invalid prefix */ 4, /* invalid prefix */ 4, /* invalid prefix */ 4, /* invalid prefix */ }; int hpack_encoder_error; const size_t n = 32; uint8_t buff[33]; /*n+1*/ char match; int retval=0; size_t i,j; size_t length; printf("\n" BOLD "**\n** ENCODE INT\n**\n" NORMAL "\n"); for (i = 0; i < good_n; i++) { printf(BOLD "Test %d: " NORMAL, (int)i); /* aha! sending uninitialised buff to my function! */ hpack_encoder_error = hpack_encode_int(good_in[i], good_pb[i], good_pf[i], buff, n, &length); if (hpack_encoder_error) { printf(RED "error: %d" NORMAL "\n", hpack_encoder_error); retval ++; } else { if (length == good_out[i].length) { match = 1; for (j = 0; j < length; j++) { if (buff[j] != good_out[i].ptr[j]) { printf(RED "mismatch at byte %d: got %02x, expected %02x" NORMAL "\n", (int)j, buff[j], good_out[i].ptr[j]); match = 0; break; } } } else { match = 0; printf(RED "returned %d, expected %d" NORMAL "\n", (int)length, (int)good_out[i].length); } if (!match) { printf(" < %08x [%3d]; %d | %02x\n", (unsigned int)(good_in[i]), (int)(good_in[i]), (int)(good_pb[i]), good_pf[i]); dump(buff, length, '>', 0); dump_str(good_out[i], '~', 0); printf("\n"); } else { printf(GREEN "PASS" NORMAL "\n"); } retval += (!match); } } for (i = 0; i < bad_n; i++) { printf(BOLD "Test %d: " NORMAL, (int)(good_n+i)); /* aha! sending uninitialised buff to my function! */ hpack_encoder_error = hpack_encode_int(bad_in[i], bad_pb[i], bad_pf[i], buff, n, &length); if (hpack_encoder_error != bad_out[i]) { printf(RED "error: %d (expected %d)" NORMAL "\n", hpack_encoder_error, bad_out[i]); printf(" < %08x [%3d]; %d | %02x\n", (unsigned int)(bad_in[i]), (int)(bad_in[i]), (int)(bad_pb[i]), bad_pf[i]); /*dump(buff, length, '>', 1);*/ printf("\n"); retval ++; } else { printf(GREEN "PASS" NORMAL "\n"); } } return retval; }
/* * Get datapoint data according to datapoint properties. * Here we fake the data using random numbers. */ void * get_datapoint_data(void *props) { float temperature = 0.0; void *ret = NULL; const char *name = get_string_by_name(props, "name"); ret = malloc(sizeof(double)); if (ret == NULL) { perror("No memory available.\n"); exit(-1); } if (strcmp(name, "grove_temperature") == 0) { /* the temperature sensor's output connect to a0 pin of galileo */ int a0v = galileo_analog_read(0); /* the value of a0v should within [0,4096], that use 12bit to * moderize 0~5v voltage, which means 0 stands for 0v while * 4096 stands for 5v. */ printf("Readed a0 pin voltage: %1.2f\n", ((double)a0v * 5) / 4096); /* then next we'll need to calculate temperature based on * the design of temperature sensor. */ int val = a0v / 4; int B = 3975; float resistance; if (val != 0) { resistance = (float)(1023 - val) * 10000 / val; temperature = 1 / (log(resistance / 10000) / B + 1 / 298.15) - 273.15; } printf("The temperature is: %2.2f c\n", temperature); /* return the temperature to libsensor */ *(double *)ret = (double)temperature; } else if (strcmp(name, "grove_light") == 0) { int a1v = galileo_analog_read(1); printf("Readed a1 pin voltage: %1.2f\n", ((double)a1v * 5) / 4096); int val = a1v / 4; printf("The light number is: %2.2f\n", (double)val); *(double *)ret = (double)val; } else if (strcmp(name, "grove_sound") == 0) { int a2v = galileo_analog_read(2); printf("Readed a2 pin voltage: %1.2f\n", ((double)a2v * 5) / 4096); int val = a2v / 4; printf("The sound number is: %2.2f\n", (double)val); *(double *)ret = (double)val; } else if (strcmp(name, "lm35-temperature") == 0) { int a0v = galileo_analog_read(0); /* get voltage */ printf("Readed a0 pin voltage: %1.2f\n", ((double)a0v * 5) / 4096); double val = a0v / 4; /* the lm35 output voltage is 10mV per degree, from 0 to 100 C */ double temperature = (val * 5 / 1024) * 100.00; printf("The lm35 temperature is: %2.2f C\n", temperature); *(double *)ret = temperature; } else if (strcmp(name, "oc_image") == 0) { struct timeb t; ftime(&t); /* prepare a image file then return its name to libsensor */ char *file = NULL; char *cmd = NULL; /* note, according to asprintf, the 'file' need be freed later, which will be done by libsensor */ asprintf(&file, "image_%lld%s", 1000 * (long long)t.time + t.millitm, ".jpg"); asprintf(&cmd, "capture %s 2>/dev/null", file); system(cmd); free(cmd); cmd = NULL; ret = (void*)file; } else if (strcmp(name, "image") == 0) { struct timeb t; ftime(&t); /* prepare a image file then return its name to libsensor */ char *file = NULL; char *cmd = NULL; /* note, according to asprintf, the 'file' need be freed later, which will be done by libsensor */ asprintf(&file, "image_%lld%s", 1000 * (long long)t.time + t.millitm, ".jpg"); asprintf(&cmd, "fswebcam -r 1280x720 --save %s 2>/dev/null", file); system(cmd); free(cmd); cmd = NULL; ret = (void*)file; } else if (strcmp(name, "serial_test") == 0) { unsigned char buf[32] = {}; int fd = init_serial_port(SERISL_DEVICE); if (fd < 0) { *(double *)ret = 0; } else { int len = do_read_from_serial(buf, fd); dump_str(buf, len); *(double *)ret = 1; } clean_serial_port(fd); } return ret; }
int npr_sprintf(struct npr_printf_state *st, char *dst, int dstlen, const struct npr_printf_format *formats, int nformat, const struct npr_printf_arg *args, int *is_fini) { char *d = dst; int di=0; int fi, ai = st->arg_index; *is_fini = 0; for (fi = st->format_index; fi < nformat; fi++) { const struct npr_printf_format *f = &formats[fi]; const struct npr_printf_arg *a = &args[ai]; int wid = f->u.wid; switch(f->type) { case NPR_PRINTF_STR: { const char *s = a->p; int len = a->u.si; int outlen = MAX( len, wid ); int d = outlen-len; switch (st->state) { case 0: di = fill_with(dst, di, dstlen, ' ', d, &st->char_index, is_fini); if (*is_fini == 0) goto quit; st->state = 1; /* fa */ case 1: di = dump_str(dst, di, dstlen, s, len, &st->char_index, is_fini); if (*is_fini == 0) goto quit; st->state = 0; break; } } ai++; break; #define CASE_DIGIT(casetag,type,uniontag,casttype,base,flags) \ case casetag: \ { \ type v = a->u.uniontag; \ di = output_integer(st, d, di, \ dstlen, \ wid, f->zero_fill, \ (casttype)v, base, flags, \ is_fini); \ if (*is_fini == 0) \ goto quit; \ } \ ai++; \ break; CASE_DIGIT(NPR_PRINTF_DIGIT, int, si, signed long long, 10, 0); CASE_DIGIT(NPR_PRINTF_LDIGIT, long, sl, signed long long, 10, 0); CASE_DIGIT(NPR_PRINTF_LLDIGIT, long long, sll, signed long long, 10, 0); CASE_DIGIT(NPR_PRINTF_UDIGIT, unsigned int, ui, unsigned long long, 10, 0); CASE_DIGIT(NPR_PRINTF_LUDIGIT, unsigned long, ul, unsigned long long, 10, 0); CASE_DIGIT(NPR_PRINTF_LLUDIGIT, unsigned long long, ull, unsigned long long, 10, 0); CASE_DIGIT(NPR_PRINTF_HEX, unsigned int, ui, unsigned long long, 16, FLAG_UPCASE); CASE_DIGIT(NPR_PRINTF_LHEX, unsigned long, ul, unsigned long long, 16, FLAG_UPCASE); CASE_DIGIT(NPR_PRINTF_LLHEX, unsigned long long, ull, unsigned long long, 16, FLAG_UPCASE); CASE_DIGIT(NPR_PRINTF_hex, unsigned int, ui, unsigned long long, 16, 0); CASE_DIGIT(NPR_PRINTF_lhex, unsigned long, ul, unsigned long long, 16, 0); CASE_DIGIT(NPR_PRINTF_llhex, unsigned long long, ull, unsigned long long, 16, 0); CASE_DIGIT(NPR_PRINTF_POINTER, void *, p, uintptr_t, 16, 0); case NPR_PRINTF_CHAR: { if (di >= dstlen) goto quit; dst[di] = a->u.c; di++; ai++; } break; case NPR_PRINTF_ORDINARY: { const char *s = f->u.ordinary; int len = f->zero_fill; di = dump_str(dst, di, dstlen, s, len, &st->char_index, is_fini); if (*is_fini == 0) goto quit; } break; case NPR_PRINTF_ORDINARY_CHAR: { if (di >= dstlen) goto quit; dst[di] = f->zero_fill; di++; } break; } } *is_fini = 1; quit: st->format_index = fi; st->arg_index = ai; return di; }
int test_str(void) { STR_DEFINE( s3, 10 ); Str *s4, *s5; STR_DEFINE(s6, 10); STR_DEFINE(s7, 10); dump_str( s1, "s1" ); dump_str( s2, "s2" ); dump_str( s3, "s3" ); /* check static strings keep their value */ mu_assert_str( static_str(4), ==, "4" ); mu_assert_str( static_str(-1), ==, "4" ); mu_assert_str( static_str(-1), ==, "4" ); mu_assert_str( static_str(7), ==, "7" ); mu_assert_str( static_str(-1), ==, "7" ); mu_assert_str( static_str(-1), ==, "7" ); T_STR(s4, s4 = str_new(6)); /* alloc, keep memory leak */ T_STR(s5, s5 = str_new(6)); /* expand */ T_STR(s3, str_clear(s3)); T_STR(s3, str_reserve(s3, 9)); T_STR(s3, str_reserve(s3, 10)); T_STR(s3, str_reserve(s3, 11)); T_STR(s3, str_clear(s3)); /* char */ T_STR(s4, str_set(s4, "xxxx")); T_STR(s4, str_set_char(s4, 0)); T_STR(s4, str_append_char(s4, 1)); T_STR(s4, str_append_char(s4, 2)); T_STR(s4, str_append_char(s4, 3)); T_STR(s4, str_append_char(s4, 4)); T_STR(s4, str_append_char(s4, 5)); T_STR(s4, str_append_char(s4, 6)); T_STR(s4, str_append_char(s4, 7)); T_STR(s4, str_append_char(s4, 8)); T_STR(s4, str_append_char(s4, 9)); T_STR(s4, str_append_char(s4, 10)); T_STR(s4, str_clear(s4)); /* string */ T_STR(s5, str_set(s5, "1234")); T_STR(s5, str_append(s5, "56789")); T_STR(s5, str_append(s5, "0")); T_STR(s5, str_clear(s5)); /* substring */ T_STR(s5, str_set_n(s5, "1234xx", 4)); T_STR(s5, str_append_n(s5, "56789xx", 5)); T_STR(s5, str_append_n(s5, "01234567890xx", 11)); T_STR(s5, str_clear(s5)); /* bytes */ T_STR(s5, str_set_bytes(s5, "\0\1\2\3x", 3)); T_STR(s5, str_append_bytes(s5, "\4\5\6x", 3)); T_STR(s5, str_clear(s5)); /* sprintf - test repeated call to vsprintf when buffer grows, needs va_copy in MacOS */ T_STR(s6, str_set(s6, "xxxx")); T_STR(s6, str_sprintf(s6, "%s %d", "hello", 123)); T_STR(s6, str_sprintf(s6, "%s %d", "hello", 1234)); T_STR(s6, str_append_sprintf(s6, "%s %d", "hello", 12345)); T_STR(s6, str_clear(s6)); /* vsprintf - test repeated call to vsprintf when buffer grows, needs va_copy in MacOS */ T_STR(s7, str_set(s7, "xxxx")); T_STR(s7, call_vsprintf(s7, "%s %d", "hello", 123)); T_STR(s7, call_vsprintf(s7, "%s %d", "hello", 1234)); T_STR(s7, call_append_vsprintf(s7, "%s %d", "hello", 12345)); T_STR(s7, str_clear(s7)); str_delete(s5); mu_assert_ptr_null(s5); STR_DELETE(s3); mu_assert_ptr_null(s3); STR_DELETE(s6); mu_assert_ptr_null(s6); STR_DELETE(s7); mu_assert_ptr_null(s7); return MU_PASS; }
int __noinstrument kfi_dump_log(char* buf) { int i, len = 0; struct kfi_run* run = run_curr; struct kfi_filters* filters = &run->filters; if (!run) { dump_str(buf, len, "\nNo logging run registered\n"); return len; } if (!run->triggered) { dump_str(buf, len, "\nLogging not yet triggered\n"); return len; } if (!run->complete) { dump_str(buf, len, "\nLogging is running\n"); return len; } dump_str(buf, len, "\nKernel Instrumentation Run ID %d\n\n", run->id); dump_str(buf, len, "Filters:\n"); if (filters->func_list_size) { dump_str(buf, len, "\t%d-entry function list\n", filters->func_list_size); } if (filters->min_delta) { dump_str(buf, len, "\t%ld usecs minimum execution time\n", filters->min_delta); } if (filters->max_delta) { dump_str(buf, len, "\t%ld usecs maximum execution time\n", filters->max_delta); } if (filters->no_ints) { dump_str(buf, len, "\tno functions in interrupt context\n"); } if (filters->only_ints) { dump_str(buf, len, "\tno functions NOT in interrupt context\n"); } if (filters->func_list) { dump_str(buf, len, "\tfunction list\n"); } #ifdef KFI_DEBUG dump_str(buf, len, "\nFilter Counters:\n"); if (filters->min_delta || filters->max_delta) { dump_str(buf, len, "\nExecution time filter count = %d\n", filters->cnt.delta); } if (filters->no_ints) { dump_str(buf, len, "No Interrupt functions filter count = %d\n", filters->cnt.no_ints); } if (filters->only_ints) { dump_str(buf, len, "Only Interrupt functions filter count = %d\n", filters->cnt.only_ints); } if (filters->func_list_size) { dump_str(buf, len, "Function List filter count = %d\n", filters->cnt.func_list); } dump_str(buf, len, "Total entries filtered = %d\n", filters->cnt.delta + filters->cnt.no_ints + filters->cnt.only_ints + filters->cnt.func_list); dump_str(buf, len, "Entries not found = %d\n", run->notfound); #endif dump_str(buf, len, "\nNumber of entries after filters = %d\n\n", run->next_entry); len += print_trigger(buf, len, &run->start_trigger, 1); len += print_trigger(buf, len, &run->stop_trigger, 0); /* print out header */ dump_str(buf, len, "\n"); dump_str(buf, len, " Entry Delta PID Function Caller\n"); dump_str(buf, len, "-------- -------- -------- -------- --------\n"); for (i=0; i < run->next_entry; i++) { dump_str(buf, len, "%8lu %8lu %7d%s %08x %08x\n", run->log[i].time, run->log[i].delta, run->log[i].pid, (run->log[i].pid == INTR_CONTEXT) ? "i" : " ", (unsigned int)run->log[i].va, (unsigned int)run->log[i].call_site); } return len; }