static void print_atag_serial(struct atag_serial *data) { console_write(" Serial number: 0x"); console_write(tohex(data->high, 4)); console_write(tohex(data->low, 4)); console_write("\n"); }
int NonBlockInput::encode(const char* dev, int sub, const byte* data, int len, char* dest) { int i = 0; int n; byte cs = 0; char* start = dest; *dest++ = *dev++; *dest++ = *dev++; *dest++ = ' '; *dest++ = sub + '0'; *dest++ = ' '; if (m_sendHex) { dest += sprintf(dest, "%d ", -len); for (i=0; i<len; i++) { dest = tohex(data[i], dest); } } else { len--; // do not include terminating NUL character dest += sprintf(dest, "%d ", len); for (i=0; i<len; i++) { *dest++ = data[i]; } *dest++ = ' '; } n = dest - start; for (i=0; i<n; i++) { cs += start[i]; } dest = tohex(0-cs, dest); *dest++ = '\0'; return dest-start; }
static void cpu_read_registers(struct GDBState *s, char *buf) { size_t i; uint32_t fpa_len; uint32_t regs[CPU_GP_REGS]; uint32_t cpsr; uint8_t *regptr = (uint8_t*)®s[0]; uint8_t tmp; for (i = 0; i < CPU_GP_REGS; ++i) { /* general purpose r0-r15 */ regs[i] = get_reg(s->env, i); } for (i = 0; i < sizeof(regs); ++i) { tmp = *regptr++; *buf++ = tohex(tmp >> 4); *buf++ = tohex(tmp & 0x0f); } fpa_len = (((CPU_FPA_REGS * 12) + 4) * 2); memset(buf, '0', fpa_len); buf += fpa_len; cpsr = get_reg(s->env, 16); regptr = (uint8_t*)&cpsr; for (i = 0; i < sizeof(cpsr); ++i) { /* CPSR */ tmp = *regptr++; *buf++ = tohex(tmp >> 4); *buf++ = tohex(tmp & 0x0f); } *buf = '\0'; }
char* BaiduParsePinyin(FcitxCloudPinyin* cloudpinyin, CurlQueue* queue) { char *start = NULL, *end = NULL; static iconv_t conv = 0; if (conv == 0) conv = iconv_open("utf-8", "utf-16be"); if (conv == (iconv_t)(-1)) return NULL; if ((start = strstr(queue->str, "[[[\"")) != NULL) { start += strlen( "[[[\""); if ((end = strstr(start, "\",")) != NULL) { size_t length = end - start; if (length % 6 != 0 || length == 0) return NULL; size_t i = 0, j = 0; char* buf = fcitx_utils_malloc0((length / 6 + 1) * 2); while (i < length) { if (start[i] == '\\' && start[i+1] == 'u') { if (ishex(start[i+2]) && ishex(start[i+3]) && ishex(start[i+4]) && ishex(start[i+5])) { buf[j++] = (tohex(start[i+2]) << 4) | tohex(start[i+3]); buf[j++] = (tohex(start[i+4]) << 4) | tohex(start[i+5]); } else break; } i += 6; } if (i != length) { free(buf); return NULL; } buf[j++] = 0; buf[j++] = 0; size_t len = UTF8_MAX_LENGTH * (length / 6) * sizeof(char); char* realstring = fcitx_utils_malloc0(UTF8_MAX_LENGTH * (length / 6) * sizeof(char)); IconvStr p = buf; char *pp = realstring; iconv(conv, &p, &j, &pp, &len); free(buf); if (fcitx_utf8_check_string(realstring)) return realstring; else { free(realstring); return NULL; } } } return NULL; }
static int logchar (FILE *fp) { int ch; int ch2; ch = fgetc (fp); fputc (ch, stdout); fflush (stdout); switch (ch) { case '\n': ch = EOL; break; case '\\': ch = fgetc (fp); fputc (ch, stdout); fflush (stdout); switch (ch) { case '\\': break; case 'b': ch = '\b'; break; case 'f': ch = '\f'; break; case 'n': ch = '\n'; break; case 'r': ch = '\r'; break; case 't': ch = '\t'; break; case 'v': ch = '\v'; break; case 'x': ch2 = fgetc (fp); fputc (ch2, stdout); fflush (stdout); ch = tohex (ch2) << 4; ch2 = fgetc (fp); fputc (ch2, stdout); fflush (stdout); ch |= tohex (ch2); break; default: /* Treat any other char as just itself */ break; } default: break; } return (ch); }
static const char* get_string(const char* val, const char* sep, u_int8_t* buf, int* lenp) { int len; int hexstr; u_int8_t* p; len = *lenp; p = buf; hexstr = (val[0] == '0' && tolower((u_char) val[1]) == 'x'); if (hexstr) val += 2; for (;;) { if (*val == '\0') break; if (sep != NULL && strchr(sep, *val) != NULL) { val++; break; } if (hexstr) { if (!isxdigit((u_char) val[0])) { printf("%s: bad hexadecimal digits", __func__); return NULL; } if (!isxdigit((u_char) val[1])) { printf("%s: odd count hexadecimal digits", __func__); return NULL; } } if (p >= buf + len) { if (hexstr) printf("%s: hexadecimal digits too long", __func__); else printf("%s: string too long", __func__); return NULL; } if (hexstr) { #define tohex(x) (isdigit(x) ? (x) - '0' : tolower(x) - 'a' + 10) *p++ = (tohex((u_char) val[0]) << 4) | tohex((u_char) val[1]); #undef tohex val += 2; } else *p++ = *val++; } len = p - buf; /* The string "-" is treated as the empty string. */ if (!hexstr && len == 1 && buf[0] == '-') { len = 0; memset(buf, 0, *lenp); } else if (len < *lenp) memset(p, 0, *lenp - len); *lenp = len; return val; }
static void print_atag_initrd2(struct atag_initrd2 *data) { console_write(" Address: 0x"); console_write(tohex(data->address, 4)); console_write(" - 0x"); console_write(tohex(data->address+data->size-1, 4)); console_write(" ("); console_write(todec(data->size, 0)); console_write(" bytes)\n"); }
static void print_atag_ramdisk(struct atag_ramdisk *data) { console_write(" Flags: 0x"); console_write(tohex(data->flags, 4)); console_write(", size: 0x"); console_write(tohex(data->size, 4)); console_write(", start block: 0x"); console_write(tohex(data->start, 4)); console_write("\n"); }
int backslash(int c) { if(c!='\\'){ if(c=='\n') error("newline in character or string constant"); return c; } c=getc(); if('0'<=c && c<='7'){ int n; n=c-'0'; c=getc(); if(n==0 && (c=='x' || c=='X')){ c=getc(); if(!ishex(c)){ ungetc(); return 0; } n=tohex(c); c=getc(); if(!ishex(c)){ ungetc(); return n; } n=16*n+tohex(c); return n; } if(c<'0' || '7'<c){ ungetc(); return n; } n=8*n+c-'0'; c=getc(); if(c<'0' || '7'<c){ ungetc(); return n; } n=8*n+c-'0'; return n; } if(c=='b') return '\b'; if(c=='f') return '\f'; if(c=='n') return '\n'; if(c=='r') return '\r'; if(c=='t') return '\t'; if(c=='\n') return backslash(getc()); return c; }
void assert_mem(const char *file, int line, const char *a1, const char *a2, const void *aa1, const void *aa2, size_t l, enum test_predicate pred) { int r = memcmp(aa1, aa2, l); TEST_CHECK_INT(r, pred); test_header(file, line, a1, a2, "STRING", pred); fprintf(stderr, "%12s = %s (len %zu)\n", a1, tohex(aa1, MIN(l, 256)), l); fprintf(stderr, "%12s = %s (len %zu)\n", a2, tohex(aa2, MIN(l, 256)), l); test_die(); }
const char * get_string(const char *val, const char *sep, u_int8_t *buf, int *lenp) { int len; int hexstr; u_int8_t *p; len = *lenp; p = buf; hexstr = (val[0] == '0' && tolower((u_char) val[1]) == 'x'); if (hexstr) val += 2; for (;;) { if (*val == '\0') break; if (sep != NULL && strchr(sep, *val) != NULL) { val++; break; } if (hexstr) { if (!isxdigit((u_char) val[0]) || !isxdigit((u_char) val[1])) { printf("%% get_string: bad hexadecimal digits\n"); return NULL; } } if (p > buf + len) { if (hexstr) printf("%% get_string: hexadecimal digits too long\n"); else printf("%% get_string: strings too long\n"); return NULL; } if (hexstr) { #define tohex(x) (isdigit(x) ? (x) - '0' : tolower(x) - 'a' + 10) *p++ = (tohex((u_char) val[0]) << 4) | tohex((u_char) val[1]); #undef tohex val += 2; } else { if (*val == '\\' && sep != NULL && strchr(sep, *(val + 1)) != NULL) val++; *p++ = *val++; } } len = p - buf; if (len < *lenp) memset(p, 0, *lenp - len); *lenp = len; return val; }
static void rf_router_send(uint8_t addAddr) { #ifdef RFR_DEBUG if(RFR_Buffer.buf[5] == 'T') nr_t++; else if(RFR_Buffer.buf[5] == 'F') nr_f++; else if(RFR_Buffer.buf[5] == 'E') nr_e++; else if(RFR_Buffer.buf[5] == 'K') nr_k++; else if(RFR_Buffer.buf[5] == 'H') nr_h++; else nr_r++; #endif uint8_t buf[7], l = 1; buf[0] = RF_ROUTER_PROTO_ID; if(addAddr) { tohex(rf_router_target, buf+1); tohex(rf_router_myid, buf+3), buf[5] = 'U'; l = 6; } rf_router_ping(); // 15ms ccInitChip(EE_FASTRF_CFG); // 1.6ms my_delay_ms(3); // 3ms: Found by trial and error CC1100_ASSERT; cc1100_sendbyte(CC1100_WRITE_BURST | CC1100_TXFIFO); #ifdef RFR_USBECHO uint8_t nbuf = RFR_Buffer.nbytes; #endif cc1100_sendbyte(RFR_Buffer.nbytes+l); for(uint8_t i = 0; i < l; i++) cc1100_sendbyte(buf[i]); while(RFR_Buffer.nbytes) cc1100_sendbyte(rb_get(&RFR_Buffer)); CC1100_DEASSERT; ccTX(); rb_reset(&RFR_Buffer); // needed by FHT_compress // Wait for the data to be sent uint8_t maxwait = 20; // max 20ms while((cc1100_readReg(CC1100_TXBYTES) & 0x7f) && maxwait--) my_delay_ms(1); set_txrestore(); #ifdef RFR_USBECHO #warning RFR USB DEBUGGING IS ACTIVE uint8_t odc = display_channel; display_channel = DISPLAY_USB; DC('.'); DU(nbuf, 2); DNL(); display_channel = odc; #endif }
void ethernet_reset(void) { char buf[21]; uint16_t serial = 0; buf[1] = 'i'; buf[2] = 'd'; strcpy_P(buf+3, PSTR("1")); write_eeprom(buf);//DHCP buf[2] = 'a'; strcpy_P(buf+3, PSTR("192.168.0.244")); write_eeprom(buf);//IP buf[2] = 'n'; strcpy_P(buf+3, PSTR("255.255.255.0")); write_eeprom(buf); buf[2] = 'g'; strcpy_P(buf+3, PSTR("192.168.0.1")); write_eeprom(buf);//GW buf[2] = 'p'; strcpy_P(buf+3, PSTR("2323")); write_eeprom(buf); buf[2] = 'N'; strcpy_P(buf+3, PSTR("0.0.0.0")); write_eeprom(buf);//==GW buf[2] = 'o'; strcpy_P(buf+3, PSTR("00")); write_eeprom(buf);//GMT #ifdef EE_DUDETTE_MAC // check for mac stored during manufacture uint8_t *ee = EE_DUDETTE_MAC; if (erb( ee++ ) == 0xa4) if (erb( ee++ ) == 0x50) if (erb( ee++ ) == 0x55) { buf[2] = 'm'; strcpy_P(buf+3, PSTR("A45055")); // busware.de OUI range tohex(erb( ee++ ), (uint8_t*)buf+9); tohex(erb( ee++ ), (uint8_t*)buf+11); tohex(erb( ee++ ), (uint8_t*)buf+13); buf[15] = 0; write_eeprom(buf); return; } #endif // Generate a "unique" MAC address from the unique serial number buf[2] = 'm'; strcpy_P(buf+3, PSTR("A45055")); // busware.de OUI range #define bsbg boot_signature_byte_get // tohex(bsbg(0x0e)+bsbg(0x0f), (uint8_t*)buf+9); // tohex(bsbg(0x10)+bsbg(0x11), (uint8_t*)buf+11); // tohex(bsbg(0x12)+bsbg(0x13), (uint8_t*)buf+13); for (uint8_t i = 0x00; i < 0x20; i++) serial += bsbg(i); tohex(0, (uint8_t*)buf+9); tohex((serial>>8) & 0xff, (uint8_t*)buf+11); tohex(serial & 0xff, (uint8_t*)buf+13); buf[15] = 0; write_eeprom(buf); }
const char * get_string(const char *val, const char *sep, u_int8_t *buf, int *lenp, bool hexok) { int len; bool hexstr; u_int8_t *p; len = *lenp; p = buf; hexstr = hexok && val[0] == '0' && tolower((u_char)val[1]) == 'x'; if (hexstr) val += 2; for (;;) { if (*val == '\0') break; if (sep != NULL && strchr(sep, *val) != NULL) { val++; break; } if (hexstr) { if (!isxdigit((u_char)val[0]) || !isxdigit((u_char)val[1])) { warnx("bad hexadecimal digits"); return NULL; } } if (p >= buf + len) { if (hexstr) warnx("hexadecimal digits too long"); else warnx("strings too long"); return NULL; } if (hexstr) { #define tohex(x) (isdigit(x) ? (x) - '0' : tolower(x) - 'a' + 10) *p++ = (tohex((u_char)val[0]) << 4) | tohex((u_char)val[1]); #undef tohex val += 2; } else *p++ = *val++; } len = p - buf; if (len < *lenp) memset(p, 0, *lenp - len); *lenp = len; return val; }
string Color::toString() const { char str[8]; str[0] = '#'; str[1] = tohex(m_red >> 4); str[2] = tohex(m_red); str[3] = tohex(m_green >> 4); str[4] = tohex(m_green); str[5] = tohex(m_blue >> 4); str[6] = tohex(m_blue); str[7] = 0; return string(str); }
static void print_atag_core(struct atag_core *data) { if(data->header.size == 5) { console_write(" Flags: 0x"); console_write(tohex(data->flags, 4)); console_write(", pagesize: 0x"); console_write(tohex(data->pagesize, 4)); console_write(", root device: 0x"); console_write(tohex(data->rootdevice, 4)); console_write("\n"); } else console_write(" No additional data\n"); }
int cipher3(const char* key,char* plain_text,char* crypted_text,int length) { char t_plain[MAX_CI_LEN]; char t_crypt[MAX_CI_LEN]; int en_cnt; int pad_cnt=0; int i; char usekey[32]; for(i=0;i<32;i++) usekey[i]='\0'; strncpy(usekey,key,16); if(length>MAX_CI_LEN) return -1; for(i=0;i<length;i++) t_plain[i]=*(plain_text+i); if(length%8==0) en_cnt=length/8; else { en_cnt=length/8+1; pad_cnt=8-length%8; for(i=0;i<pad_cnt;i++) t_plain[length+i]=0; } for(i=0;i<en_cnt;i++) TDes(0,&t_plain[8*i],usekey,&t_crypt[8*i]); tohex(t_crypt, crypted_text, 8 * en_cnt); return 0; }
/* Convert LENGTH bytes of data in CHAR_STRING into hex encoding and store that in HEX_STRING. HEX_STRING must be allocated of at least (2*LENGTH+1) bytes or be NULL so that the function mallocs an appropriate buffer. Returns HEX_STRING or NULL on error (which may only occur if HEX_STRING has been NULL and the internal malloc failed). */ char *convert_char_to_hexstring (const void *char_string, size_t length, char *hex_string){ const unsigned char *s; char *p; if (!hex_string) { size_t nbytes = 2 * length + 1; if (length && (nbytes-1) / 2 != length) { errno = ENOMEM; return 0; } hex_string = gcry_malloc_secure(nbytes); if (!hex_string) return 0; } for (s = char_string, p = hex_string; length; length--, s++) { *p++ = tohex ((*s>>4)&15); *p++ = tohex (*s&15); } *p = 0; return hex_string; }
int main(int argc, char **argv) { int c; int current = 0; int nibble = 0; int skip = 0; if (argv[1]) { skip = atoi(argv[1]); } #define NIBBLE_COUNT 2 while ((c = getchar()) != EOF) { if (isspace(c)) { verify_nibble(nibble, current); continue; } if (skip) { skip--; continue; } current = current << 4 | tohex(c); nibble++; if (nibble == NIBBLE_COUNT) { putchar(current); nibble = 0; current = 0; } } return 0; }
int Get_M_Line(int Times,unsigned char Turn) { char current_INIO[9]; unsigned char Flag; int now_turn; while(Time--) { read(INIO_fd,current_INIO,sizeof current_INIO); Flag = tohex(current_INIO,5); now_turn = check_which_turn(Flag); if((last_turn>=RIGHT) && (now_turn<RIGHT)) return 1; if((last_turn<=LEFT) && (now_turn>LEFT)) return 1; if(now_turn == NOTURN) { usleep(2000); return 1; } if(Turn>=2) if(now_turn > Turn) return 1; if(Turn<=2) if(now_turn < Turn) return 1; usleep(1000); } return 0; }
void dumpmem(/*int dbg_level, */char *ptr, int len) { char line[80], chars[80], *p, b, *c, *end; int /*i, */j, offset; end = ptr + len; offset = 0; while (ptr < end) { p = line; c = chars; //debugf(dbg_level, "%x ", ptr); printf("%04x ", offset); *p++ = ' '; for (j = 0; j < 16; j++) { if (ptr < end) { b = *ptr++; *p++ = tohex(b >> 4); *p++ = tohex(b); *p++ = ' '; *c++ = ' ' <= b && b <= '~' ? b : '.'; } else { *p++ = 'x'; *p++ = 'x'; *p++ = ' '; *c++ = 'x'; } }
String xxtea_c::encrypt(String data) { // Works only if the Key in setup if(this->keyset && data.length() != 0) { // If the Data within the limits of the Engine if(data.length() < MAX_XXTEA_DATA8) { size_t len; // Assign the Maximum buffer we have len = MAX_XXTEA_DATA8; // Perform Encryption if(xxtea_encrypt((uint8_t *)data.c_str(),data.length(), this->data,&len) == XXTEA_STATUS_SUCCESS) { String result; size_t i; result.reserve(len*2 + 1); result = ""; for(i=0;i<len;i++) { result+=tohex((this->data[i] >> 4) & 0x0F); result+=tohex(this->data[i] & 0x0F); } return result; } }
static void cpu_read_memory(struct GDBState *s, uint32_t addr, uint32_t len, char *buf) { uint32_t i; uint32_t mem[MAX_PACKET_LEN / 8]; uint8_t *memptr = (uint8_t*)&mem[0]; uint8_t tmp; if ((len * 2) > (MAX_PACKET_LEN / 8)) { /* one byte must use two byte ascii */ printf("read memory greater than MAX_PACKET_LEN: %d\n", len); return; } for (i = 0; i < len / 4; ++i) { mem[i] = get_mem(s->env, addr); addr += 4; } for (i = 0; i < len; ++i) { tmp = *memptr++; *buf++ = tohex(tmp >> 4); *buf++ = tohex(tmp & 0x0f); } *buf = '\0'; }
static void gdb_reply(struct GDBState *s, char *ptr) { char outbuf[MAX_PACKET_LEN] = ""; char *outptr = &outbuf[0]; size_t len = strlen(ptr); uint8_t checksum; ssize_t ret; if (len > sizeof(outbuf)) { printf("%s: input size is too long\n", __FUNCTION__); exit(1); } *outptr++ = '+'; *outptr++ = '$'; strncpy(outptr, ptr, len); outptr += len; *outptr++ = '#'; checksum = do_checksum(&outbuf[0]); *outptr++ = tohex(checksum >> 4); *outptr++ = tohex(checksum & 0x0f); *outptr = '\0'; printf("reply (%lu): %s\n", strlen(outbuf), outbuf); ret = write(s->fd, outbuf+1, strlen(outbuf)-1); if (ret <= 0) /* FIXME: strlen(outbuf) != 0 always */ perror("write error:"); }
static int hexpairtochar(const char *cp, char*myochar) { char ochar = 0; int olen = 0; char c = cp[0]; if (c) { char c2 = cp[1]; if (c2) { ochar = tohex(c,c2); olen = 2; } else { fprintf(stderr,"Translating from uri: " "A supposed hexadecimal input character pair " "runs off the end of the input after 1 hex digit.\n"); /* botched input. */ ochar = c; olen = 1; } } else { /* botched input. */ fprintf(stderr,"Translating from uri: " "A supposed hexadecimal input character pair " "runs off the end of the input.\n"); ochar = '%'; olen = 0; } *myochar = ochar; return olen; }
int cipher2(const char* key,char* plain_text,char* crypted_text,int length) { char t_plain[MAX_CI_LEN]; char t_crypt[MAX_CI_LEN]; int en_cnt; int pad_cnt=0; int i; if(length>MAX_CI_LEN) return -1; for(i=0;i<length;i++) t_plain[i]=*(plain_text+i); if(length%8==0) en_cnt=length/8; else { en_cnt=length/8+1; pad_cnt=8-length%8; for(i=0;i<pad_cnt;i++) t_plain[length+i]=0; } for(i=0;i<en_cnt;i++) TDes(0,&t_plain[8*i],triple_des_key,&t_crypt[8*i]); tohex(t_crypt, crypted_text, 8 * en_cnt); return 0; }
char *geturl(char *header) { char *end, *inpt, *oupt, *ret; char low, high; inpt = header = index(header, ' ') + 1; while (*inpt && *inpt != ' ' && *inpt != '?') { inpt++; }; end = inpt; oupt = ret = calloc(end - header + 1, sizeof(char)); inpt = header; while (inpt != end) { if (*inpt == '%') { inpt++; high = *inpt++; low = *inpt; *oupt = tohex(high, low); } else { *oupt = *inpt; } inpt++; oupt++; } *oupt = '\0'; return ret; }
void op_display(enum op_severity sev, enum op_module mod, uint16_t code) { uint32_t w0 = sev << 16 | mod; uint32_t w1; bool clean_lock; if (!fsp_present()) return; w1 = tohex((code >> 12) & 0xf) << 24; w1 |= tohex((code >> 8) & 0xf) << 16; w1 |= tohex((code >> 4) & 0xf) << 8; w1 |= tohex((code ) & 0xf); /* * We use lock_recursive to detect recursion. We avoid sending * the message if that happens as this could be a case of a * locking error in the FSP driver for example */ clean_lock = lock_recursive(&op_lock); if (!clean_lock) return; /* We don't use mkmsg, we use a preallocated msg to avoid * going down the malloc path etc... since this can be called * in case of fatal errors */ fsp_fillmsg(&op_msg, FSP_CMD_DISP_SRC_DIRECT, 3, 1, w0, w1); fsp_sync_msg(&op_msg, false); unlock(&op_lock); }
/* return -1 if error, 0 if OK */ static int put_packet_binary(GDBState *s, const char *buf, int len) { int csum, i; uint8_t *p; for(;;) { p = s->last_packet; *(p++) = '$'; memcpy(p, buf, len); p += len; csum = 0; for(i = 0; i < len; i++) { csum += buf[i]; } *(p++) = '#'; *(p++) = tohex((csum >> 4) & 0xf); *(p++) = tohex((csum) & 0xf); s->last_packet_len = p - s->last_packet; put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len); #ifdef CONFIG_USER_ONLY i = get_char(s); if (i < 0) return -1; if (i == '+') break; #else break; #endif } return 0; }
static char * do_bin2hex (const void *buffer, size_t length, char *stringbuf, int with_colon) { const unsigned char *s; char *p; if (!stringbuf) { /* Not really correct for with_colon but we don't care about the one wasted byte. */ size_t n = with_colon? 3:2; size_t nbytes = n * length + 1; if (length && (nbytes-1) / n != length) { gpg_err_set_errno (ENOMEM); return NULL; } stringbuf = xtrymalloc (nbytes); if (!stringbuf) return NULL; } for (s = buffer, p = stringbuf; length; length--, s++) { if (with_colon && s != buffer) *p++ = ':'; *p++ = tohex ((*s>>4)&15); *p++ = tohex (*s&15); } *p = 0; return stringbuf; }