static void hex(const char *s) { unsigned long long i = 0; switch (s[0]) { case 'b': case 'B': i = read_bin(s + 1); break; case 'x': case 'X': i = read_hex(s + 1); break; case '0': if (s[1] == '\0') i = read_dec(s); else if (s[1] == 'x' || s[1] == 'X') i = read_hex(s + 2); else i = read_oct(s + 1); break; default: if (isdigit(*s)) i = read_dec(s); else usage(); break; } print_bin(i); print_chr(i); print_dec(i); print_hex(i); print_oct(i); }
int id2symbol(const char * id, Symbol ** res) { Symbol * sym = NULL; Context * ctx = NULL; ULONG64 module = 0; ULONG index = 0; unsigned frame = 0; const Symbol * base = NULL; const TypeInfo * info = NULL; size_t length = 0; const char * p; if (id != NULL && id[0] == '@' && id[1] == 'P') { p = id + 2; length = (size_t)read_hex(&p); if (*p == '.') p++; if (id2symbol(p, (Symbol **)&base)) return -1; ctx = base->ctx; } else if (id != NULL && id[0] == '@' && id[1] == 'S') { unsigned idx = 0; p = id + 2; module = (ULONG64)read_hex(&p); if (*p == '.') p++; index = (ULONG)read_hex(&p); if (*p == '.') p++; frame = (unsigned)read_hex(&p); if (*p == '.') p++; idx = (unsigned)read_hex(&p); if (idx) info = basic_type_info + (idx - 1); if (*p == '.') p++; ctx = id2ctx(p); } else { errno = ERR_INV_CONTEXT; return -1; } if (ctx == NULL) { errno = ERR_INV_CONTEXT; return -1; } sym = alloc_symbol(); sym->ctx = ctx; sym->module = module; sym->index = index; sym->frame = frame; sym->base = base; sym->info = info; sym->length = length; if (sym->base || sym->info) { sym->sym_class = SYM_CLASS_TYPE; } else { DWORD dword = 0; if (get_type_info(sym, TI_GET_SYMTAG, &dword) < 0) return -1; tag2symclass(sym, dword); } *res = sym; return 0; }
static int usbdevfs_reapurb_init_from_text(ioctl_tree * node, const char *data) { struct usbdevfs_urb *info = calloc(sizeof(struct usbdevfs_urb), 1); int offset, result; unsigned type, endpoint; result = sscanf(data, "%u %u %i %u %i %i %i %n", &type, &endpoint, &info->status, &info->flags, &info->buffer_length, &info->actual_length, &info->error_count, &offset); /* ambiguity of counting or not %n */ if (result < 7) { DBG(DBG_IOCTL_TREE, "usbdevfs_reapurb_init_from_text: failed to parse record '%s'\n", data); free(info); return FALSE; } info->type = (unsigned char)type; info->endpoint = (unsigned char)endpoint; /* read buffer */ info->buffer = calloc(info->buffer_length, 1); if (!read_hex(data + offset, info->buffer, info->buffer_length)) { DBG(DBG_IOCTL_TREE, "usbdevfs_reapurb_init_from_text: failed to parse buffer '%s'\n", data + offset); free(info->buffer); free(info); return FALSE; }; node->data = info; return TRUE; }
static int ioctl_varlenstruct_init_from_text(ioctl_tree * node, const char *data) { size_t data_len = strlen(data) / 2; node->data = malloc(data_len); if (!read_hex(data, node->data, data_len)) { fprintf(stderr, "ioctl_varlenstruct_init_from_text: failed to parse '%s'\n", data); free(node->data); return FALSE; } /* verify that the text data size actually matches get_data_size() */ size_t size = node->type->get_data_size(node->id, node->data); if (size != data_len) { fprintf(stderr, "ioctl_varlenstruct_init_from_text: ioctl %lX: expected data length %zu, but got %zu bytes from text data\n", node->id, size, data_len); free(node->data); return FALSE; } return TRUE; }
static void xbsd_change_fstype(void) { int i; i = xbsd_get_part_index(xbsd_dlabel.d_npartitions); xbsd_dlabel.d_partitions[i].p_fstype = read_hex(xbsd_fstypes); }
void vcs_from_str(struct vcs_rel *rel, const char *text) { char token[80]; int plus=0; enum { begin, f_ver, f_svn, f_rev, f_git, f_srcv } ex = begin; while (sget_token(token, sizeof(token), &text) != EOF) { switch(ex) { case begin: if(!strcmp(token,"version:")) ex = f_ver; if(!strcmp(token,"SVN")) ex = f_svn; if(!strcmp(token,"GIT-hash:")) ex = f_git; if(!strcmp(token,"srcversion:")) ex = f_srcv; break; case f_ver: if(!strcmp(token,"plus")) plus = 1; /* still waiting for version */ else { vcs_ver_from_str(rel, token); ex = begin; } break; case f_svn: if(!strcmp(token,"Revision:")) ex = f_rev; break; case f_rev: rel->svn_revision = atol(token) * 10; if( plus ) rel->svn_revision += 1; memset(rel->git_hash, 0, GIT_HASH_BYTE); return; case f_git: read_hex(rel->git_hash, token, GIT_HASH_BYTE, strlen(token)); rel->svn_revision = 0; return; case f_srcv: memset(rel->git_hash, 0, SRCVERSION_PAD); read_hex(rel->git_hash + SRCVERSION_PAD, token, SRCVERSION_BYTE, strlen(token)); rel->svn_revision = 0; return; } } }
static int handle_vendor(struct nl80211_state *state, struct nl_msg *msg, int argc, char **argv, enum id_input id) { unsigned int oui; unsigned int subcmd; char buf[2048] = {}; int res, count = 0; FILE *file = NULL; if (argc < 3) return 1; res = sscanf(argv[0], "0x%x", &oui); if (res != 1) { printf("Vendor command must start with 0x\n"); return 2; } res = sscanf(argv[1], "0x%x", &subcmd); if (res != 1) { printf("Sub command must start with 0x\n"); return 2; } if (!strcmp(argv[2], "-")) file = stdin; else file = fopen(argv[2], "r"); NLA_PUT_U32(msg, NL80211_ATTR_VENDOR_ID, oui); NLA_PUT_U32(msg, NL80211_ATTR_VENDOR_SUBCMD, subcmd); if (file) { count = read_file(file, buf, sizeof(buf)); fclose(file); } else count = read_hex(argc - 2, &argv[2], buf, sizeof(buf)); if (count < 0) return -EINVAL; if (count > 0) NLA_PUT(msg, NL80211_ATTR_VENDOR_DATA, count, buf); return 0; nla_put_failure: return -ENOBUFS; }
static int read_data(FILE *f, TDB_DATA *d, size_t size) { int c, low, high; int i; d->dptr = (unsigned char *)malloc(size); if (d->dptr == NULL) { return -1; } d->dsize = size; for (i=0; i<size; i++) { c = getc(f); if (c == EOF) { fprintf(stderr, "Unexpected EOF in data\n"); return 1; } else if (c == '"') { return 0; } else if (c == '\\') { high = read_hex(); if (high < 0) { return -1; } high = high << 4; assert(high == (high & 0xf0)); low = read_hex(); if (low < 0) { return -1; } assert(low == (low & 0x0f)); d->dptr[i] = (low|high); } else { d->dptr[i] = c; } } return 0; }
static int ioctl_simplestruct_init_from_text(ioctl_tree * node, const char *data) { /* node->id is initialized at this point, but does not necessarily have the * correct length for data; this happens for variable length ioctls such as * EVIOCGBIT */ size_t data_len = strlen(data) / 2; node->data = malloc(data_len); if (NSIZE(node) != data_len) { DBG(DBG_IOCTL_TREE, "ioctl_simplestruct_init_from_text: adjusting ioctl ID %lX (size %lu) to actual data length %zu\n", node->id, NSIZE(node), data_len); node->id = _IOC(_IOC_DIR(node->id), _IOC_TYPE(node->id), _IOC_NR(node->id), data_len); } if (!read_hex(data, node->data, NSIZE(node))) { DBG(DBG_IOCTL_TREE, "ioctl_simplestruct_init_from_text: failed to parse '%s'\n", data); free(node->data); return FALSE; } return TRUE; }
bfd_boolean generate_build_id (bfd *abfd, const char *style, checksum_fn checksum_contents, unsigned char *id_bits, int size ATTRIBUTE_UNUSED) { if (streq (style, "md5")) { struct md5_ctx ctx; md5_init_ctx (&ctx); if (!(*checksum_contents) (abfd, (sum_fn) &md5_process_bytes, &ctx)) return FALSE; md5_finish_ctx (&ctx, id_bits); } else if (streq (style, "sha1")) { struct sha1_ctx ctx; sha1_init_ctx (&ctx); if (!(*checksum_contents) (abfd, (sum_fn) &sha1_process_bytes, &ctx)) return FALSE; sha1_finish_ctx (&ctx, id_bits); } else if (streq (style, "uuid")) { #ifndef __MINGW32__ int n; int fd = open ("/dev/urandom", O_RDONLY); if (fd < 0) return FALSE; n = read (fd, id_bits, size); close (fd); if (n < size) return FALSE; #else /* __MINGW32__ */ typedef RPC_STATUS (RPC_ENTRY * UuidCreateFn) (UUID *); UUID uuid; UuidCreateFn uuid_create = 0; HMODULE rpc_library = LoadLibrary ("rpcrt4.dll"); if (!rpc_library) return FALSE; uuid_create = (UuidCreateFn) (void (WINAPI *)(void)) GetProcAddress (rpc_library, "UuidCreate"); if (!uuid_create) { FreeLibrary (rpc_library); return FALSE; } if (uuid_create (&uuid) != RPC_S_OK) { FreeLibrary (rpc_library); return FALSE; } FreeLibrary (rpc_library); memcpy (id_bits, &uuid, (size_t) size < sizeof (UUID) ? (size_t) size : sizeof (UUID)); #endif /* __MINGW32__ */ } else if (strneq (style, "0x", 2)) { /* ID is in string form (hex). Convert to bits. */ const char *id = style + 2; size_t n = 0; do { if (ISXDIGIT (id[0]) && ISXDIGIT (id[1])) { id_bits[n] = read_hex (*id++) << 4; id_bits[n++] |= read_hex (*id++); } else if (*id == '-' || *id == ':') ++id; else abort (); /* Should have been validated earlier. */ } while (*id != '\0'); } else abort (); /* Should have been validated earlier. */ return TRUE; }
/* * We switch the user's privilege to read ~/.gfarm_shared_key. * * NOTE: reading this file with root privilege may not work, * if home directory is NFS mounted and root access for * the home directory partition is not permitted. * * Do not leave the user privilege switched here, even in the switch_to case, * because it is necessary to switch back to the original user privilege when * gfarm_auth_sharedsecret fails. */ gfarm_error_t gfarm_auth_shared_key_get(unsigned int *expirep, char *shared_key, char *home, struct passwd *pwd, int create, int period) { gfarm_error_t e; FILE *fp; static char keyfile_basename[] = "/" GFARM_AUTH_SHARED_KEY_BASENAME; char *keyfilename; unsigned int expire; static pthread_mutex_t privilege_mutex = PTHREAD_MUTEX_INITIALIZER; uid_t o_uid; gid_t o_gid; #ifdef __GNUC__ /* workaround gcc warning: might be used uninitialized */ o_uid = o_gid = 0; #endif GFARM_MALLOC_ARRAY(keyfilename, strlen(home) + sizeof(keyfile_basename)); if (keyfilename == NULL) return (GFARM_ERR_NO_MEMORY); strcpy(keyfilename, home); strcat(keyfilename, keyfile_basename); if (pwd != NULL) { pthread_mutex_lock(&privilege_mutex); o_gid = getegid(); o_uid = geteuid(); seteuid(0); /* recover root privilege */ initgroups(pwd->pw_name, pwd->pw_gid); setegid(pwd->pw_gid); seteuid(pwd->pw_uid); } if ((fp = fopen(keyfilename, "r+")) != NULL) { if (skip_space(fp) || read_hex(fp, &expire, sizeof(expire))) { fclose(fp); free(keyfilename); e = GFARM_ERRMSG_SHAREDSECRET_INVALID_EXPIRE_FIELD; goto finish; } expire = ntohl(expire); if (skip_space(fp) || read_hex(fp, shared_key, GFARM_AUTH_SHARED_KEY_LEN)) { fclose(fp); free(keyfilename); e = GFARM_ERRMSG_SHAREDSECRET_INVALID_KEY_FIELD; goto finish; } } if (fp == NULL) { if (create == GFARM_AUTH_SHARED_KEY_GET) { free(keyfilename); e = GFARM_ERRMSG_SHAREDSECRET_KEY_FILE_NOT_EXIST; goto finish; } fp = fopen(keyfilename, "w+"); if (fp == NULL) { e = gfarm_errno_to_error(errno); free(keyfilename); goto finish; } if (chmod(keyfilename, 0600) == -1) { e = gfarm_errno_to_error(errno); fclose(fp); free(keyfilename); goto finish; } expire = 0; /* force to regenerate key */ } if (create == GFARM_AUTH_SHARED_KEY_CREATE_FORCE || time(NULL) >= expire) { if (create == GFARM_AUTH_SHARED_KEY_GET) { fclose(fp); free(keyfilename); e = GFARM_ERR_EXPIRED; goto finish; } if (fseek(fp, 0L, SEEK_SET) == -1) { e = gfarm_errno_to_error(errno); fclose(fp); free(keyfilename); goto finish; } gfarm_auth_random(shared_key, GFARM_AUTH_SHARED_KEY_LEN); if (period <= 0) period = GFARM_AUTH_EXPIRE_DEFAULT; expire = time(NULL) + period; expire = htonl(expire); write_hex(fp, &expire, sizeof(expire)); expire = ntohl(expire); fputc(' ', fp); write_hex(fp, shared_key, GFARM_AUTH_SHARED_KEY_LEN); fputc('\n', fp); } fclose(fp); free(keyfilename); *expirep = expire; e = GFARM_ERR_NO_ERROR; finish: if (pwd != NULL) { seteuid(0); /* recover root privilege */ setgroups(1, &o_gid); /* abandon group privileges */ setegid(o_gid); seteuid(o_uid); /* suppress root privilege, if possible */ pthread_mutex_unlock(&privilege_mutex); } return (e); }
int main(int argc, char *argv[]) { struct _memory memory; char *filename=NULL; char *uart="/dev/ttyUSB0"; int prog_type=PROG_UNDEF; int command=COMMAND_UNDEF; int param1=0,param2=0; int i; printf("\nnaken_prog - by Michael Kohn\n"); printf(" Web: http://www.mikekohn.net/\n"); printf(" Email: [email protected]\n\n"); printf("Version: "VERSION"\n\n"); if (argc<2) { printf("Usage: naken_util [options]\n"); printf(" CPU options:\n"); printf(" -lpc\n"); printf(" Command:\n"); printf(" -info\n"); printf(" -write <outfile>\n"); printf(" -read <address> <count> <infile>\n"); printf(" -execute <address>\n"); printf("\n"); exit(0); } memset(&memory, 0, sizeof(memory)); for (i=1; i<argc; i++) { if (strcmp(argv[i], "-lpc")==0) { prog_type=PROG_LPC; } else if (strcmp(argv[i], "-info")==0) { command=COMMAND_INFO; } else if (strcmp(argv[i], "-read")==0) { command=COMMAND_READ; if (i+2>=argc) { printf("Error: -read command takes 2 arguments\n"); exit(1); } param1=atoi(argv[++i]); param2=atoi(argv[++i]); } else if (strcmp(argv[i], "-write")==0) { command=COMMAND_WRITE; } else if (strcmp(argv[i], "-run")==0) { command=COMMAND_RUN; param1=atoi(argv[++i]); } else { filename=argv[i]; } } if (prog_type==PROG_UNDEF) { printf("Error: No chip type selected.\n"); exit(0); } if (command==COMMAND_INFO) { if (prog_type==PROG_LPC) { lpc_info(uart); } } else if (command==COMMAND_READ) { if (filename==NULL) { printf("No output filename. Exiting...\n"); exit(1); } memory_init(&memory, 0xffffffff, 1); lpc_memory_read(uart, &memory, param1, param2); // FIXME - make filename selectable FILE *out=fopen(filename, "wb"); write_hex(&memory, out); fclose(out); memory_free(&memory); } else if (command==COMMAND_WRITE) { unsigned char cpu_type; if (filename==NULL) { printf("No hexfile loaded. Exiting...\n"); exit(1); } memory_init(&memory, 0xffffffff, 1); if (read_elf(filename, &memory, &cpu_type, NULL)>=0) { printf("Loaded ELF %s from 0x%04x to 0x%04x\n", filename, memory.low_address, memory.high_address); } else if (read_hex(filename, &memory)>=0) { printf("Loaded hexfile %s from 0x%04x to 0x%04x\n", filename, memory.low_address, memory.high_address); } else { printf("Could not load hexfile\n"); memory_free(&memory); exit(1); } lpc_memory_write(uart, &memory); memory_free(&memory); } else if (command==COMMAND_RUN) { if (prog_type==PROG_LPC) { lpc_run(uart, param1); } } return 0; }
char * gfarm_auth_shared_key_get(unsigned int *expirep, char *shared_key, char *home, int create, int period) { FILE *fp; static char keyfile_basename[] = "/" GFARM_AUTH_SHARED_KEY_BASENAME; char *keyfilename; unsigned int expire; keyfilename = malloc(strlen(home) + sizeof(keyfile_basename)); if (keyfilename == NULL) return (GFARM_ERR_NO_MEMORY); strcpy(keyfilename, home); strcat(keyfilename, keyfile_basename); if ((fp = fopen(keyfilename, "r+")) != NULL) { if (skip_space(fp) || read_hex(fp, &expire, sizeof(expire))) { fclose(fp); free(keyfilename); return ("~/" GFARM_AUTH_SHARED_KEY_BASENAME ": invalid expire field"); } expire = ntohl(expire); if (skip_space(fp) || read_hex(fp, shared_key, GFARM_AUTH_SHARED_KEY_LEN)) { fclose(fp); free(keyfilename); return ("~/" GFARM_AUTH_SHARED_KEY_BASENAME ": invalid key field"); } } if (fp == NULL) { if (create == GFARM_AUTH_SHARED_KEY_GET) { free(keyfilename); return ("~/" GFARM_AUTH_SHARED_KEY_BASENAME ": not exist"); } fp = fopen(keyfilename, "w+"); if (fp == NULL) { free(keyfilename); return (gfarm_errno_to_error(errno)); } if (chmod(keyfilename, 0600) == -1) { fclose(fp); free(keyfilename); return (gfarm_errno_to_error(errno)); } expire = 0; /* force to regenerate key */ } if (create == GFARM_AUTH_SHARED_KEY_CREATE_FORCE || time(NULL) >= expire) { if (create == GFARM_AUTH_SHARED_KEY_GET) { fclose(fp); free(keyfilename); return (GFARM_ERR_EXPIRED); } if (fseek(fp, 0L, SEEK_SET) == -1) { fclose(fp); free(keyfilename); return (gfarm_errno_to_error(errno)); } gfarm_auth_random(shared_key, GFARM_AUTH_SHARED_KEY_LEN); if (period <= 0) period = GFARM_AUTH_EXPIRE_DEFAULT; expire = time(NULL) + period; expire = htonl(expire); write_hex(fp, &expire, sizeof(expire)); expire = ntohl(expire); fputc(' ', fp); write_hex(fp, shared_key, GFARM_AUTH_SHARED_KEY_LEN); fputc('\n', fp); } fclose(fp); free(keyfilename); *expirep = expire; return (NULL); }
/* * Given the global variables buffer and buffer_len, append at their end * the commands that with type action and action param action_param. * @param action the id of the action. * @param action_param the parameter of the action * @todo code is very dirty. Needs to be refactored. */ int append_action(int action, const char *action_param) { struct ofp_action_output *act_out; struct ofp_action_vlan_vid *act_vid; struct ofp_action_vlan_pcp *act_pcp; struct ofp_action_header *act; struct ofp_action_dl_addr *act_dl; struct ofp_action_nw_addr *act_nw; struct ofp_action_tp_port *act_port; switch(action) { case OFPAT_OUTPUT: fprintf(stderr, "output packet to port %s\n", action_param); command_len += sizeof(struct ofp_action_output); command = realloc(command, command_len); act_out = (struct ofp_action_output *) (command + (command_len - sizeof(struct ofp_action_output))); act_out->type = htons(action); act_out->len = htons(8); act_out->max_len = htons(0); act_out->port = htons((uint16_t)strtol(action_param, NULL, 16)); break; case OFPAT_SET_VLAN_VID: if( (strtol(action_param, NULL, 16) < 0) || (strtol(action_param, NULL, 16) >= 0xFFF)) { printf("invalid vlan id\n"); return -1; } fprintf(stderr, "change vlan to %ld\n", strtol(action_param, NULL, 16)); command_len += sizeof(struct ofp_action_vlan_vid); command = realloc(command, command_len); act_vid = (struct ofp_action_vlan_vid *) (command+(command_len-sizeof(struct ofp_action_vlan_vid))); act_vid->type = htons(action); act_vid->len = htons(8); act_vid->vlan_vid = htons((uint16_t)strtol(action_param, NULL, 16)); break; case OFPAT_SET_VLAN_PCP: if( (strtol(action_param, NULL, 16) < 0) || (strtol(action_param, NULL, 16) > 7)) { printf("invalid vlan pcp\n"); return -1; } printf("change vlan pcp %ld\n", strtol(action_param, NULL, 16)); command_len += sizeof(struct ofp_action_vlan_pcp); command = realloc(command, command_len); act_pcp = (struct ofp_action_vlan_pcp *) (command + (command_len - sizeof(struct ofp_action_vlan_pcp))); act_pcp->type = htons(action); act_pcp->len = htons(8); act_pcp->vlan_pcp = (uint8_t)strtol(action_param, NULL, 16); break; case OFPAT_STRIP_VLAN: printf("strip vlan header\n"); command_len += sizeof(struct ofp_action_header); command = realloc(command, command_len); act = (struct ofp_action_header *) (command + (command_len - sizeof(struct ofp_action_header))); act->type = htons(action); act->len = htons(8); break; case OFPAT_SET_DL_SRC: case OFPAT_SET_DL_DST: printf("Change ethernet address to %s\n", action_param); if((strlen(action_param) != 12) || (is_hex(action_param, 12) == 0)) { printf("invalid mac address\n"); return -1; } command_len += sizeof(struct ofp_action_dl_addr); command = realloc(command, command_len); act_dl = (struct ofp_action_dl_addr *) (command + (command_len - sizeof(struct ofp_action_dl_addr))); act_dl->type = htons(action); act_dl->len = htons(16); int i; for(i = 0 ; i < 6; i++) { act_dl->dl_addr[i] = read_hex(action_param); action_param += 2; } break; case OFPAT_SET_NW_SRC: case OFPAT_SET_NW_DST: printf("Change ip address to %s\n", action_param); if((strlen(action_param) != 8) || (is_hex(action_param, 8) == 0)) { printf("invalid ip address\n"); return -1; } command_len += sizeof(struct ofp_action_nw_addr); command = realloc(command, command_len); act_nw = (struct ofp_action_nw_addr *) (command + (command_len - sizeof(struct ofp_action_nw_addr))); act_nw->type = htons(action); act_nw->len = htons(8); act_nw->nw_addr = htonl(strtol(action_param, NULL, 16)); break; case OFPAT_SET_TP_SRC: case OFPAT_SET_TP_DST: printf("change port to %ld\n", strtol(action_param, NULL, 16)); command_len += sizeof(struct ofp_action_tp_port); command = realloc(command, command_len); act_port = (struct ofp_action_tp_port *) (command + (command_len - sizeof(struct ofp_action_tp_port))); act_port->type = htons(action); act_port->len = htons(8); act_port->tp_port = htons((uint16_t)strtol(action_param, NULL, 16)); break; } return 0; }
_INLINE static int token_char_accept(tre_Lexer *lex, uint32_t code, bool use_back_ref) { if (code == '\\') { // 对转义字符做特殊处理 if (lex->scur == lex->slen) { // 如果已经是最后一个字符,那么当作普通字符即可 lex->token.extra.code = code; lex->token.value = TK_CHAR; } else { // 如果不是,读下一个字符 code = char_lookahead(lex); if (is_spe_char(code)) { // 能确定为特殊匹配字符的话,读取结束 lex->token.extra.code = code; lex->token.value = TK_CHAR_SPE; code = char_next(lex); } else { // 否则当做 hex/unicode 转义处理 int num, len; bool is_ok = false; if (code == 'x') { code = char_next(lex); num = read_hex(lex, 2, &is_ok); if (!is_ok) return ERR_LEXER_HEX_ESCAPE; char_nextn(lex, 2); } else if (code == 'u') { code = char_next(lex); num = read_hex(lex, 4, &is_ok); if (!is_ok) return ERR_LEXER_UNICODE_ESCAPE; char_nextn(lex, 4); } else if (code == 'U') { code = char_next(lex); num = read_hex(lex, 8, &is_ok); // unicode 6.0 \U0000000A if (!is_ok) return ERR_LEXER_UNICODE6_ESCAPE; char_nextn(lex, 8); } if (is_ok) { lex->token.value = TK_CHAR; lex->token.extra.code = num; } else { num = read_int(lex, 0, &len); if (num != -1) { // back reference or normal char if (use_back_ref) { if (num == 0) { lex->token.value = TK_CHAR; lex->token.extra.code = 0; } else { lex->token.value = TK_BACK_REF; lex->token.extra.index = num; } } else { lex->token.value = TK_CHAR; lex->token.extra.code = num; } char_nextn(lex, len); } else { // 既不是转义,也不是前向引用,只是一个字符罢了 lex->token.value = TK_CHAR; lex->token.extra.code = code; char_next(lex); } } } } } else { // 若非转义字符,那么一切都很简单 lex->token.extra.code = code; lex->token.value = (code == '.') ? TK_CHAR_SPE : TK_CHAR; } return 0; }
int main(int argc, char *argv[]) { struct hexfile hf; FILE *tf; int pos, i; printf("hex2txt - convert an Intel hex/a43 file to TI txt format (max. %d bytes)\n", MAX_SIZE); if (argc != 3) { printf("usage: hex2txt <inputfile> <outputfile>\n"); return 1; } if (init_hexfile(&hf, MAX_SIZE, NULL, argv[1])!=0) { printf("error opening hex file %s\n", argv[1]); return 1; } printf("reading hex file %s...\n", argv[1]); if (read_hex(&hf)!=0) { printf("error in hex file %s\n", argv[1]); destroy_hexfile(&hf); return 1; } tf = fopen(argv[2], "w"); if (tf==NULL) { printf("error creating txt file %s\n", argv[2]); return 1; } printf("writing txt file %s...\n", argv[2]); pos = 0; do { while (pos<hf.memsize && hf.flag[pos]!=0) pos++; if (pos>=hf.memsize) break; fprintf(tf, "@%X", pos); i = 0; do { if (i%16==0) fprintf(tf, "\n"); else fprintf(tf, " "); i++; fprintf(tf, "%02X", hf.mem[pos]); pos++; } while (pos<hf.memsize && hf.flag[pos]==0); fprintf(tf, "\n"); } while (pos<hf.memsize); fprintf(tf, "q\n"); fclose(tf); destroy_hexfile(&hf); printf("done.\n"); return 0; }