// Only called at load time, so does not have to be overly optimal char *dynamic_Demangle(char *Line, int *Len) { char *tmp, *cp, *cp2, digits[3]; if (!Line || !strlen(Line)) { if (Len) *Len = 0; return str_alloc_copy(""); } tmp = str_alloc_copy(Line); cp = tmp; cp2 = Line; while (*cp2) { if (*cp2 != '\\') *cp++ = *cp2++; else { ++cp2; if (*cp2 == '\\') *cp++ = *cp2++; else { unsigned val; if (*cp2 != 'x') { *cp++ = '\\'; continue; } ++cp2; if (!cp2[0]) { *cp++ = '\\'; *cp++ = 'x'; continue; } digits[0] = *cp2++; if (!cp2[0] || !ishexdigit(digits[0])) { *cp++ = '\\'; *cp++ = 'x'; *cp++ = digits[0]; continue; } digits[1] = *cp2++; if (!ishexdigit(digits[1])) { *cp++ = '\\'; *cp++ = 'x'; *cp++ = digits[0]; *cp++ = digits[1]; continue; } digits[2] = 0; val = (unsigned)strtol(digits, NULL, 16); sprintf(cp, "%c", val); ++cp; } } } *cp = 0; if (Len) *Len = cp-tmp; return tmp; }
unsigned int atoh(char * buf) { unsigned int retval = 0; char * cp; char digit; cp = buf; /* skip past spaces and tabs */ while (*cp == ' ' || *cp == 9) cp++; /* while we see digits and the optional 'x' */ while (ishexdigit(digit = *cp++) || (digit == 'x')) { /* its questionable what we should do with input like '1x234', * or for that matter '1x2x3', what this does is ignore all */ if (digit == 'x') retval = 0; else retval = (retval << 4) + hexnibble(digit); } return retval; }
static Bit16 getAddress(Bool *hasSecond ){ /* Read the hex address from stdin */ int c; int max = 5; char buf[] = {'\0','\0','\0','\0','\0'}; unsigned int n = 0; skipspace(); while((c = getchar()) != EOF && n < max){ if (c == '\n' || c == ',' || c == ' ') { if (c == ' '){ skipspace(); c = getchar(); } *hasSecond = (c == ','); break; } if (ishexdigit(c)){ buf[n++] = c; } else { fprintf(stderr,"Invalid character only hex address are allowed.\n"); return 0; } } if (n == max){ fprintf(stderr,"The memory address is to large. The maximum memory address is $FFFF\n"); return 0; } return strtol(buf,NULL,16); }
static mutils_boolean check_hex(mutils_word8 *given_chain, mutils_word32 len) { mutils_word8 i; for (i = 0; i < len; i++) if (ishexdigit(given_chain[i]) == MUTILS_FALSE) return(MUTILS_FALSE); return(MUTILS_TRUE); }
bitvalue_c::bitvalue_c(std::string s, unsigned int allowed_bitlength) { if ((allowed_bitlength != 0) && ((allowed_bitlength % 8) != 0)) throw mtx::invalid_parameter_x(); unsigned int len = s.size(); ba::to_lower(s); std::string s2; unsigned int i; for (i = 0; i < len; i++) { // Space or tab? if (isblanktab(s[i])) continue; // Skip hyphens and curly braces. Makes copy & paste a bit easier. if ((s[i] == '-') || (s[i] == '{') || (s[i] == '}')) continue; // Space or tab followed by "0x"? Then skip it. if (s.substr(i, 2) == "0x") { i++; continue; } // Invalid character? if (!ishexdigit(s[i])) throw mtx::bitvalue_parser_x(boost::format(Y("Not a hex digit at position %1%")) % i); // Input too long? if ((allowed_bitlength > 0) && ((s2.length() * 4) >= allowed_bitlength)) throw mtx::bitvalue_parser_x(boost::format(Y("Input too long: %1% > %2%")) % (s2.length() * 4) % allowed_bitlength); // Store the value. s2 += s[i]; } // Is half a byte or more missing? len = s2.length(); if (((len % 2) != 0) || ((allowed_bitlength != 0) && ((len * 4) < allowed_bitlength))) throw mtx::bitvalue_parser_x(Y("Missing one hex digit")); m_value = memory_c::alloc(len / 2); unsigned char *buffer = m_value->get_buffer(); for (i = 0; i < len; i += 2) buffer[i / 2] = hextodec(s2[i]) << 4 | hextodec(s2[i + 1]); }
unsigned char CheckHexMsg(unsigned char * buf, unsigned char len, unsigned char *outputMsg, unsigned char *outputLen, unsigned char *eoi) { unsigned char i; *eoi = 1; //default if (('D'==toupper(buf[1])) && (';'==buf[len-1])) { --len; *eoi = 0; } if ((len & 0x01) || (len < 4)) return 0; //msg length is not even if (('0'!=buf[0]) || (('C'!=toupper(buf[1])) && ('D'!=toupper(buf[1])))) return 0; for (i=2; i<len; i++) { if (!ishexdigit(toupper(buf[i]))) return 0; } *outputLen = 0; for (i=2; i<len; i=i+2) { *outputMsg = hex2dec(toupper(buf[i])) << 4; *outputMsg += hex2dec(toupper(buf[i+1])); //printf("%d ", *outputMsg); outputMsg++; *outputLen += 1; } //printf("\r\n"); return 1; }
/* * demux() * * analyse prefix and forward accordingly * (to channel output queues or cmd input queue) */ void demux (msg_t *m, const chn_t *ch /* unused */) { int id; (void)ch;/* avoid warnings */ #ifdef DEBUG if (PRFXLEN != 5) { /* huh! prefix length does not match the following part */ tesc_emerg (CHN_MSG, MF_ERR, "demux(): internal error\n"); tesc_emerg (CHN_MSG, MF_EOF, "\n"); exit (1); } if (m->flags & MF_PLAIN) { tesc_emerg (CHN_MSG, MF_ERR, "demux(): plain message\n"); tesc_emerg (CHN_MSG, MF_EOF, "\n"); exit (1); } #endif if (m->len < 6) { /* illegal input, expected at least prefix + '\n', i.e. len == 6 */ mlpx_printf (CHN_MSG, MF_ERR, "demux(): illegal input (too short to have valid prefix)\n"); free (m); return; } /* check prefix */ if (m->prefix[0] != '<' || m->prefix[3] != '<' || m->prefix[4] != ' ') { /* illegal prefix, wrong framing chars for id */ mlpx_printf (CHN_MSG, MF_ERR, "demux(): illegal prefix (wrong framing chars)\n"); free (m); return; } /* check id part */ if (! (ishexdigit (m->prefix[1]) && ishexdigit (m->prefix[2]))) { /* illegal char for prefix */ mlpx_printf (CHN_MSG, MF_ERR, "demux(): illegal prefix (garbled channel id)\n"); free (m); return; } /* convert id */ id = hexd2int (m->prefix[1]) * 16 + hexd2int (m->prefix[2]); /* check if channel is valid and open for writing */ if (! chmap[id]) { /* channel does not exist */ mlpx_printf (CHN_MSG, MF_ERR, "demux(): channel %02X does not exist\n", id); free (m); return; } if (chmap[id]->flags & CHN_F_IP) { /* open/connect still in progress */ mlpx_printf (CHN_MSG, MF_ERR, "demux(): channel %02X not yet ready\n", id); free (m); return; } if (! (chmap[id]->flags & CHN_F_WR)) { /* channel is not writeable */ mlpx_printf (CHN_MSG, MF_ERR, "demux(): channel %02X not open for writing\n", id); free (m); return; } /* ignore prefix from now on */ m->flags |= MF_PLAIN; m->len -= PRFXLEN; /* forward to output or cmd */ if (id == CHN_CMD) { cmdi_enque (m); } else if (id == CHN_MSG) { /* simply echo back */ mux (m, chmap[CHN_MSG]); } else tesc_enq_wq (chmap[id], m); return; }
static void odoffset(int argc, char ***argvp) { register char *num, *p; int base; char *end; /* * The offset syntax of od(1) was genuinely bizarre. First, if * it started with a plus it had to be an offset. Otherwise, if * there were at least two arguments, a number or lower-case 'x' * followed by a number makes it an offset. By default it was * octal; if it started with 'x' or '0x' it was hex. If it ended * in a '.', it was decimal. If a 'b' or 'B' was appended, it * multiplied the number by 512 or 1024 byte units. There was * no way to assign a block count to a hex offset. * * We assumes it's a file if the offset is bad. */ p = **argvp; if (!p) { /* hey someone is probably piping to us ... */ return; } if ((*p != '+') && (argc < 2 || (!isdecdigit(p[0]) && ((p[0] != 'x') || !ishexdigit(p[1]))))) return; base = 0; /* * bb_dump_skip over leading '+', 'x[0-9a-fA-f]' or '0x', and * set base. */ if (p[0] == '+') ++p; if (p[0] == 'x' && ishexdigit(p[1])) { ++p; base = 16; } else if (p[0] == '0' && p[1] == 'x') { p += 2; base = 16; } /* bb_dump_skip over the number */ if (base == 16) for (num = p; ishexdigit(*p); ++p); else for (num = p; isdecdigit(*p); ++p); /* check for no number */ if (num == p) return; /* if terminates with a '.', base is decimal */ if (*p == '.') { if (base) return; base = 10; } bb_dump_skip = strtol(num, &end, base ? base : 8); /* if end isn't the same as p, we got a non-octal digit */ if (end != p) bb_dump_skip = 0; else { if (*p) { if (*p == 'b') { bb_dump_skip *= 512; ++p; } else if (*p == 'B') { bb_dump_skip *= 1024; ++p; } } if (*p) bb_dump_skip = 0; else { ++*argvp; /* * If the offset uses a non-octal base, the base of * the offset is changed as well. This isn't pretty, * but it's easy. */ #define TYPE_OFFSET 7 { char x_or_d; if (base == 16) { x_or_d = 'x'; goto DO_X_OR_D; } if (base == 10) { x_or_d = 'd'; DO_X_OR_D: bb_dump_fshead->nextfu->fmt[TYPE_OFFSET] = bb_dump_fshead->nextfs->nextfu->fmt[TYPE_OFFSET] = x_or_d; } } } } }
static unichar_t *ugetstr(FILE *file,int format,unichar_t *buffer,int len) { int ch, ch2; unichar_t *upt = buffer; if ( format==0 ) { while ( (ch=getc(file))!='\n' && ch!='\r' && ch!=EOF ) { if ( upt<buffer+len-1 ) *upt++ = ch; } if ( ch=='\r' ) { ch = getc(file); if ( ch!='\n' ) ungetc(ch,file); } } else { for (;;) { ch = getc(file); ch2 = getc(file); if ( format==1 ) ch = (ch<<8)|ch2; else ch = (ch2<<8)|ch; if ( ch2==EOF ) { ch = EOF; break; } else if ( ch=='\n' || ch=='\r' ) break; if ( upt<buffer+len-1 ) *upt++ = ch; } if ( ch=='\r' ) { ch = getc(file); ch2 = getc(file); if ( ch2!=EOF ) { if ( format==1 ) ch = (ch<<8)|ch2; else ch = (ch2<<8)|ch; if ( ch!='\n' ) fseek(file,-2,SEEK_CUR); } } } if ( ch==EOF && upt==buffer ) return( NULL ); *upt = '\0'; for ( upt=buffer; *upt; ++upt ) { if ( (*upt=='U' || *upt=='u') && upt[1]=='+' && ishexdigit(upt[2]) && ishexdigit(upt[3]) && ishexdigit(upt[4]) && ishexdigit(upt[5]) ) { ch = isdigit(upt[2]) ? upt[2]-'0' : islower(upt[2]) ? upt[2]-'a'+10 : upt[2]-'A'+10; ch = (ch<<4) + (isdigit(upt[3]) ? upt[3]-'0' : islower(upt[3]) ? upt[3]-'a'+10 : upt[3]-'A'+10); ch = (ch<<4) + (isdigit(upt[4]) ? upt[4]-'0' : islower(upt[4]) ? upt[4]-'a'+10 : upt[4]-'A'+10); ch = (ch<<4) + (isdigit(upt[5]) ? upt[5]-'0' : islower(upt[5]) ? upt[5]-'a'+10 : upt[5]-'A'+10); *upt = ch; u_strcpy(upt+1,upt+6); } } return( buffer ); }
static int fpattern_submatch(const char *pat, const char *fname, int flength) { int fch; int pch, pch2; int i; int yes, match; int lo, hi; /* Attempt to match subpattern against subfilename */ while (*pat != '\0') { fch = (unsigned char)*fname; pch = (unsigned char)*pat; pat++; switch (pch) { case FPAT_ANY: /* Match a single char */ #if defined FPAT_DELIM if (fch == FPAT_DEL || fch == FPAT_DEL2 || fch == '\0') return (FALSE); #else if (flength == 0) return (FALSE); #endif fname++; flength--; break; case FPAT_CLOS: /* Match zero or more chars */ #if defined FPAT_DELIM i = 0; while (fname[i] != '\0' && fname[i] != FPAT_DEL && fname[i] != FPAT_DEL2) i++; #else i = flength; #endif while (i >= 0) { if (fpattern_submatch(pat, fname+i, flength-i)) return (TRUE); i--; } return (FALSE); #ifdef FPAT_SUBCLOS case FPAT_CLOSP: /* Match zero or more chars */ i = 0; while (i < flength && #if defined FPAT_DELIM fname[i] != FPAT_DEL && fname[i] != FPAT_DEL2 && #endif fname[i] != '.') i++; while (i >= 0) { if (fpattern_submatch(pat, fname+i, flength-i)) return (TRUE); i--; } return (FALSE); #endif case FPAT_QUOTE: /* Match a quoted char */ assert(*pat != '\0'); pch = (unsigned char)*pat; if (ishexdigit(pch)) { pch2 = *++pat; pch = (hexdigit(pch) << 4) | hexdigit(pch2); } /* if */ if ((fch) != (pch)) return (FALSE); fname++; flength--; pat++; break; case FPAT_SET_L: /* Match char set/range */ yes = TRUE; if (*pat == FPAT_NOT) { pat++; yes = FALSE; /* Set negation */ } /* if */ /* Look for [s], [-], [abc], [a-c] */ match = !yes; while (*pat != FPAT_SET_R) { assert(*pat != '\0'); pch = (unsigned char)*pat++; if (pch == FPAT_QUOTE) { assert(*pat != '\0'); pch = (unsigned char)*pat++; if (ishexdigit(pch)) { pch2 = *pat++; lo = (hexdigit(pch) << 4) | hexdigit(pch2); } else { lo = pch; } /* if */ } else { lo = pch; } /* if */ if (*pat == FPAT_SET_THRU) { /* Range */ pat++; pch = (unsigned char)*pat++; if (pch == FPAT_QUOTE) { assert(*pat != '\0'); pch = (unsigned char)*pat++; if (ishexdigit(pch)) { pch2 = *pat++; hi = (hexdigit(pch) << 4) | hexdigit(pch2); } else { hi = pch; } /* if */ } else { hi = pch; } /* if */ /* Compare character to set range */ if ((fch) >= (lo) && (fch) <= (hi)) { match = yes; /* skip to the end of the set in the pattern (no need to * search further once a match is found) */ while (*pat != FPAT_SET_R) { assert(*pat != '\0'); pat++; } /* while */ break; } /* if */ } else { /* Compare character to single char from set */ if ((fch) == (lo)) { match = yes; /* skip to the end of the set in the pattern (no need to * search further once a match is found) */ while (*pat != FPAT_SET_R) { assert(*pat != '\0'); pat++; } /* while */ break; } /* if */ } /* if */ assert(*pat != '\0'); } /* while */ if (!match) return (FALSE); fname++; flength--; assert(*pat == FPAT_SET_R); pat++; break; #if defined FPAT_MSET_ENABLED case FPAT_MSET_L: /* Match zero or more characters in a char set/range */ yes = TRUE; if (*pat == FPAT_NOT) { pat++; yes = FALSE; /* Set negation */ } /* if */ do { const char *org_pat = pat; /* Look for [s], [-], [abc], [a-c] */ match = !yes; while (*pat != FPAT_MSET_R) { assert(*pat != '\0'); pch = (unsigned char)*pat++; if (pch == FPAT_QUOTE) { assert(*pat != '\0'); pch = (unsigned char)*pat++; if (ishexdigit(pch)) { pch2 = *pat++; lo = (hexdigit(pch) << 4) | hexdigit(pch2); } else { lo = pch; } /* if */ } else { lo = pch; } /* if */ if (*pat == FPAT_SET_THRU) { /* Range */ pat++; pch = (unsigned char)*pat++; if (pch == FPAT_QUOTE) { assert(*pat != '\0'); pch = (unsigned char)*pat++; if (ishexdigit(pch)) { pch2 = *pat++; hi = (hexdigit(pch) << 4) | hexdigit(pch2); } else { hi = pch; } /* if */ } else { hi = pch; } /* if */ /* Compare character to set range */ if ((fch) >= (lo) && (fch) <= (hi)) { match = yes; /* skip to the end of the set in the pattern (no * need to search further once a match is found) */ while (*pat != FPAT_MSET_R) { assert(*pat != '\0'); pat++; } /* while */ break; } /* if */ } else { /* Compare character to single char from the set */ if ((fch) == (lo)) { match = yes; /* skip to the end of the set in the pattern (no * need to search further once a match is found) */ while (*pat != FPAT_MSET_R) { assert(*pat != '\0'); pat++; } /* while */ break; } /* if */ } /* if */ assert(*pat != '\0'); } /* while */ assert(*pat == FPAT_MSET_R); if (match) { fname++; flength--; fch = *fname; if (flength > 0) pat = org_pat; } /* if */ } while (match && flength > 0); pat++; break; #endif /* FPAT_MSET_ENABLED */ #if defined FPAT_NOT_ENABLED case FPAT_NOT: /* Match only if rest of pattern does not match */ assert(*pat != '\0'); i = fpattern_submatch(pat, fname, flength); return !i; #endif #if defined FPAT_DELIM case FPAT_DEL: #if FPAT_DEL2 != FPAT_DEL case FPAT_DEL2: #endif /* Match path delimiter char */ if (fch != FPAT_DEL && fch != FPAT_DEL2) return (FALSE); fname++; flength--; break; #endif default: /* Match a (non-null) char exactly */ if ((fch) != (pch)) return (FALSE); fname++; flength--; break; } } /* Check for complete match */ if (flength != 0) return (FALSE); /* Successful match */ return (TRUE); }
int fpattern_isvalid(const char *pat) { static const char specialchars[] = { FPAT_QUOTE, FPAT_SET_THRU, FPAT_SET_L, FPAT_SET_R, FPAT_MSET_L, FPAT_MSET_R, FPAT_ANY, FPAT_NOT, FPAT_CLOS, FPAT_CLOSP, '\0' }; int len, open; char close, pch, pch2; unsigned char mask[256/sizeof(unsigned char)]; /* Check args */ if (pat == NULL) return (FPAT_INVALID); open = FALSE; /* pattern is not open-ended */ /* Verify that the pattern is valid */ for (len = 0; pat[len] != '\0'; len++) { switch (pat[len]) { case FPAT_SET_L: #if defined FPAT_MSET_ENABLED case FPAT_MSET_L: #endif /* Char set */ if (pat[len] == FPAT_SET_L) { open = FALSE; close = FPAT_SET_R; } else { open = TRUE; close = FPAT_MSET_R; } len++; if (pat[len] == FPAT_NOT) len++; /* Set negation */ memset(mask,0,sizeof mask); while (pat[len] != close) { pch = pat[len]; if (pat[len] == FPAT_QUOTE) { len++; /* Quoted char */ pch = pat[len]; if (ishexdigit(pch)) { len++; /* Hex digits should come in pairs */ if (!ishexdigit(pat[len])) return (FPAT_INVALID); pch2 = pat[len]; pch = (hexdigit(pch) << 4) | hexdigit(pch2); } else if (strchr(specialchars,pat[len])==NULL) { return (FPAT_INVALID); /* escaped char should be special char */ } /* if */ } /* if */ if (pat[len] == '\0') return (FPAT_INVALID); /* Missing closing bracket */ if (BITGET(mask,pch)) return (FPAT_INVALID); BITSET(mask,pch); len++; if (pat[len] == FPAT_SET_THRU && (pat[len+1] != FPAT_SET_THRU || pch == FPAT_SET_THRU)) { /* Char range */ len++; if (pat[len] == FPAT_QUOTE) { len++; /* Quoted char */ if (ishexdigit(pat[len])) { len++; /* Hex digits should come in pairs */ if (!ishexdigit(pat[len])) return (FPAT_INVALID); } else if (strchr(specialchars,pat[len])==NULL) { return (FPAT_INVALID); /* escaped char should be special char */ } /* if */ } /* if */ if (pat[len] == '\0') return (FPAT_INVALID); /* Missing closing bracket */ if (len<2 || (unsigned char)pat[len-2]>(unsigned char)pat[len] || (pat[len-2]==pat[len] && pat[len]!=FPAT_SET_THRU)) return (FPAT_INVALID); /* invalid range (decrementing) */ //??? also set bits of characters in range? len++; } if (pat[len] == '\0') return (FPAT_INVALID); /* Missing closing bracket */ } break; case FPAT_QUOTE: /* Quoted char */ len++; open = FALSE; if (ishexdigit(pat[len])) { len++; /* Hex digits should come in pairs */ if (!ishexdigit(pat[len])) return (FPAT_INVALID); } else if (strchr(specialchars,pat[len])==NULL) { return (FPAT_INVALID); /* escaped char should be special char */ } /* if */ if (pat[len] == '\0') return (FPAT_INVALID); /* Missing quoted char */ break; #if defined FPAT_NOT_ENABLED case FPAT_NOT: /* Negated pattern */ len++; open = FALSE; if (pat[len] == '\0') return (FPAT_INVALID); /* Missing subpattern */ break; #endif case FPAT_CLOS: open = TRUE; break; default: /* Valid character */ open = FALSE; break; } } return (open ? FPAT_OPEN : FPAT_CLOSED); }
/* * primop = ( conexpr ) * | integer * | label */ int parse_primop(char **p) { char lbl[MAX_SYMLEN]; char *pp; long val; int idx; skip_ws(p); pp = *p; /* handle parenthesized expressions */ if (*pp == '(') { pp++; /* skip open paren */ val = parse_logop(&pp); skip_ws(&pp); if (*pp != ')') { error("missing closing paren"); } *p = pp+1; /* skip closing paren. */ return val; } /* handle integers and labels */ if (*pp == '0') { pp++; if (*pp == 'x') { /* hex number: 0x[0-9a-f]+ */ pp++; if (!ishexdigit(*pp)) { /* must have at least one digit after the "0x" */ error("badly formed hex number"); *p = pp; return 0; } val = hexval(*pp); pp++; while (ishexdigit(*pp)) { val = 16*val + hexval(*pp); pp++; } } else { /* octal number: 0[0-7]* */ val = 0; while (isoctdigit(*pp)) { val = 8*val + (*pp) - '0'; pp++; } } } else if (isdigit((unsigned)*pp)) { /* presumably decimal value */ val = 0; while (isdigit((unsigned)*pp)) { val = 10*val + (*pp) - '0'; pp++; } } else { /* assume it is a label */ parse_label(p, lbl); if (strlen(lbl) == 0) { /* oops, what is it? */ error("error: expected constant expression"); return 0; } idx = find_sym_idx(lbl); if (idx) { val = symtab[idx].value; } else { val = 0x00; /* dummy value */ if (g_pass == 2) error("couldn't find symbol in second pass"); } pp = *p; /* nullifies later assignment */ } *p = pp; return val; }