UINT32 AcpiUtToLower ( UINT32 c) { return (IS_UPPER(c) ? ((c)+0x20) : (c)); }
R_API void r_bin_filter_sym(RBinFile *bf, Sdb *db, ut64 vaddr, RBinSymbol *sym) { if (!db || !sym || !sym->name) { return; } char *name = sym->name; // if (!strncmp (sym->name, "imp.", 4)) { // demangle symbol name depending on the language specs if any if (bf && bf->o && bf->o->lang) { const char *lang = r_bin_lang_tostring (bf->o->lang); char *dn = r_bin_demangle (bf, lang, sym->name, sym->vaddr); if (dn && *dn) { sym->dname = dn; // XXX this is wrong but is required for this test to pass // pmb:new pancake$ bin/r2r.js db/formats/mangling/swift sym->name = dn; // extract class information from demangled symbol name char *p = strchr (dn, '.'); if (p) { if (IS_UPPER (*dn)) { sym->classname = strdup (dn); sym->classname[p - dn] = 0; } else if (IS_UPPER (p[1])) { sym->classname = strdup (p + 1); p = strchr (sym->classname, '.'); if (p) { *p = 0; } } } } } // XXX this is very slow, must be optimized const char *uname = sdb_fmt ("%" PFMT64x ".%s", vaddr, name); ut32 vhash = sdb_hash (uname); // vaddr hash - unique ut32 hash = sdb_hash (name); // name hash - if dupped and not in unique hash must insert int count = sdb_num_inc (db, sdb_fmt ("%x", hash), 1, 0); if (sdb_exists (db, sdb_fmt ("%x", vhash))) { // TODO: symbol is dupped, so symbol can be removed! return; } sdb_num_set (db, sdb_fmt ("%x", vhash), 1, 0); sym->dup_count = count - 1; }
/***************************************************************************** * * CharPtr AsnFindBaseName(atp) * returns the string name of an ASN.1 basic entity * returns NULL if not resolved * returns NULL if resolves to a primitive type (use AsnFindPrimName) * *****************************************************************************/ NLM_EXTERN CharPtr LIBCALL AsnFindBaseName (AsnTypePtr atp) { AsnTypePtr atp2; atp2 = AsnFindBaseType(atp); if (atp2 == NULL) return NULL; if (atp2->name == NULL) return NULL; else if (IS_UPPER(*atp2->name)) return atp2->name; else return NULL; }
static bool varsub(RParse *p, RAnalFunction *f, ut64 addr, int oplen, char *data, char *str, int len) { RAnalVar *var; RListIter *iter; char *oldstr; char *tstr = strdup (data); RAnal *anal = p->analb.anal; if (!p->varlist) { free (tstr); return false; } RList *bpargs = p->varlist (anal, f, 'b'); RList *spargs = p->varlist (anal, f, 's'); const bool ucase = IS_UPPER (*tstr); r_list_foreach (spargs, iter, var) { char *tmpf; //TODO: honor asm pseudo if (var->delta < 10) { tmpf = "%d(%s)"; } else if (var->delta > 0) { tmpf = "0x%x(%s)"; } else { tmpf = "-0x%x(%s)"; } oldstr = r_str_newf (tmpf, r_num_abs(var->delta), anal->reg->name[R_REG_NAME_SP]); if (ucase) { char *comma = strchr (oldstr, ','); if (comma) { *comma = 0; r_str_case (oldstr, true); *comma = ','; } } if (strstr (tstr, oldstr)) { char *newstr = (p->localvar_only) ? r_str_newf ("(%s)", var->name): r_str_newf ("%s%s(%s)", var->delta > 0 ? "" : "-", var->name, anal->reg->name[R_REG_NAME_SP]); tstr = r_str_replace (tstr, oldstr, newstr, 1); free (newstr); free (oldstr); break; } free (oldstr); }
static bool varsub(RParse *p, RAnalFunction *f, ut64 addr, int oplen, char *data, char *str, int len) { RList *spargs, *bpargs, *regargs; RAnalVar *var; RListIter *iter; char *oldstr, *newstr; char *tstr = strdup (data); if (!tstr) { return false; } if (!p->varlist) { free (tstr); return false; } if (p->relsub) { char *rip = (char *)r_str_casestr (tstr, "[pc, "); if (rip) { rip += 4; char *tstr_new, *ripend = strchr (rip, ']'); const char *neg = strchr (rip, '-'); ut64 off = (oplen == 2 || strstr (tstr, ".w")) ? 4 : 8; ut64 repl_num = (addr + off) & ~3; if (!ripend) { ripend = "]"; } if (neg) { repl_num -= r_num_get (NULL, neg + 1); } else { repl_num += r_num_get (NULL, rip); } rip -= 3; *rip = 0; tstr_new = r_str_newf ("%s0x%08"PFMT64x"%s", tstr, repl_num, ripend); free (tstr); tstr = tstr_new; } } regargs = p->varlist (p->anal, f, 'r'); bpargs = p->varlist (p->anal, f, 'b'); spargs = p->varlist (p->anal, f, 's'); bool ucase = IS_UPPER (*tstr); r_list_foreach (bpargs, iter, var) { if (var->delta > -10 && var->delta < 10) { oldstr = r_str_newf ("[%s, %d]", p->anal->reg->name[R_REG_NAME_BP], var->delta); } else if (var->delta > 0) { oldstr = r_str_newf ("[%s, 0x%x]", p->anal->reg->name[R_REG_NAME_BP], var->delta); } else { oldstr = r_str_newf ("[%s, -0x%x]", p->anal->reg->name[R_REG_NAME_BP], -var->delta); } if (ucase) { char *comma = strchr (oldstr, ','); if (comma) { *comma = 0; r_str_case (oldstr, true); *comma = ','; } } if (strstr (tstr, oldstr)) { newstr = r_str_newf ("[%s %c %s]", p->anal->reg->name[R_REG_NAME_BP], var->delta > 0 ? '+' : '-', var->name); if (ucase) { char *comma = strchr (newstr, ' '); if (comma) { *comma = 0; r_str_case (newstr, true); *comma = ' '; } } tstr = r_str_replace (tstr, oldstr, newstr, 1); free (newstr); free (oldstr); break; } free (oldstr); } r_list_foreach (spargs, iter, var) { if (var->delta > -10 && var->delta < 10) { oldstr = r_str_newf ("[sp, %d]", var->delta); } else if (var->delta > 0) { oldstr = r_str_newf ("[sp, 0x%x]", var->delta); } else { oldstr = r_str_newf ("[sp, -0x%x]", -var->delta); } if (strstr (tstr, oldstr)) { newstr = r_str_newf ("[sp %c %s]", var->delta > 0 ? '+' : '-', var->name); tstr = r_str_replace (tstr, oldstr, newstr, 1); free (newstr); free (oldstr); break; } free (oldstr); if (var->delta > -10 && var->delta < 10) { oldstr = r_str_newf ("[%s, %d]", p->anal->reg->name[R_REG_NAME_SP], var->delta); } else if (var->delta > 0) { oldstr = r_str_newf ("[%s, 0x%x]", p->anal->reg->name[R_REG_NAME_SP], var->delta); } else { oldstr = r_str_newf ("[%s, -0x%x]", p->anal->reg->name[R_REG_NAME_SP], -var->delta); } if (strstr (tstr, oldstr)) { newstr = r_str_newf ("[%s %c %s]", p->anal->reg->name[R_REG_NAME_BP], var->delta > 0 ? '+' : '-', var->name); tstr = r_str_replace (tstr, oldstr, newstr, 1); free (newstr); free (oldstr); break; } free (oldstr); } r_list_foreach (regargs, iter, var) { RRegItem *r = r_reg_index_get (p->anal->reg, var->delta); if (r && r->name && strstr (tstr, r->name)) { tstr = r_str_replace (tstr, r->name, var->name, 1); } }
/***************************************************************************** * * Int2 AsnLexTWord(aip) * reads words, punctuation, and asn keywords with 2 parts * returns tokens defined at top * *****************************************************************************/ NLM_EXTERN Int2 AsnLexTWord (AsnIoPtr aip) { register CharPtr pos; register int len; Int1 state; Int2 token, asntype, linepos; int done; Boolean first = FALSE, hitnewline = FALSE; CharPtr commentptr; if (! aip->bytes) /* no data loaded */ { hitnewline = TRUE; first = TRUE; AsnIoGets(aip); } linepos = aip->linepos; pos = aip->linebuf + linepos; state = aip->state; len = 0; token = -1; while (*pos == '\n' || *pos == '\r') /* skip empty lines */ { hitnewline = TRUE; pos = AsnIoGets(aip); if (pos == NULL) return EOF_TOKEN; } if (state == IN_STRING_STATE) { aip->word = pos; if (* pos == '\"') /* end of string */ { token = END_STRING; pos++; state = 0; /* reset state */ } else { token = IN_STRING; while ((* pos != '\"') && (* pos != '\n') && (* pos != '\r')) { pos++; len++; } if ((*pos != '\n') && (*pos != '\r') && (* (pos + 1) == '\"')) /* internal quote */ { len++; /* include in previous string */ pos += 2; /* point to rest of string */ } } } else if (state == IN_BITHEX_STATE) { aip->word = pos; if (*pos == '\'') /* end of binhex */ { state = 0; /* set to normal */ pos++; /* move past quote */ while (IS_WHITESP(*pos)) pos++; if (* pos == 'H') token = OCTETS; else if (* pos == 'B') token = ASNBITS; else { AsnIoErrorMsg(aip, 58, aip->linenumber); token = ERROR_TOKEN; } } else { token = IN_BITHEX; while ((* pos != '\'') && (* pos != '\n') && (* pos != '\r')) { pos++; len++; } } } else /* normal scanning */ { done = 0; while (! done) { while (* pos <= ' ') /* skip leading white space */ { if (*pos == '\n' || *pos == '\r') { hitnewline = TRUE; pos = AsnIoGets(aip); if (pos == NULL) return EOF_TOKEN; } else pos++; } done = 1; while (done && (*pos == '-') && (*(pos+1) == '-')) /* skip comments */ { pos += 2; if (first) /* could be revision */ { first = FALSE; if (StrMatch(asnwords[57], pos, 10)) /* $Revision: */ { token = REVISION_TOKEN; pos += 10; while (IS_WHITESP(*pos)) pos++; aip->word = pos; while (IS_DIGIT(*pos)) /* eg. 1.2 */ { len++; pos++; } if (*pos == '.') /* take after . if present */ { pos++; len++; while (IS_DIGIT(*pos)) { len++; pos++; } } } } commentptr = pos; done = 0; while (! done) /* skip to end of comment */ { if ((*pos == '-') && (*(pos +1) == '-')) { if (token != REVISION_TOKEN) { AsnLexTAddComment(commentptr, pos, aip); if ((! hitnewline) && (aip->token != COMMENT_TOKEN)) token = COMMENT_TOKEN; } pos += 2; done = 1; } else if (*pos == '\n' || *pos == '\r') { if (token != REVISION_TOKEN) { AsnLexTAddComment(commentptr, pos, aip); if ((! hitnewline) && (aip->token != COMMENT_TOKEN)) token = COMMENT_TOKEN; } done = 1; } else pos++; } if ((token == REVISION_TOKEN) || (token == COMMENT_TOKEN)) { aip->linepos = pos - aip->linebuf; aip->state = state; aip->wordlen = len; aip->token = token; return token; } if (*pos <= ' ') done = 0; else done = 1; } } aip->word = pos; if (* pos == '\"') { token = START_STRING; state = IN_STRING_STATE; } else if (* pos == '\'') { token = START_BITHEX; state = IN_BITHEX_STATE; } else if (* pos == ',') token = COMMA; else if (* pos == '{') token = START_STRUCT; else if (* pos == '}') token = END_STRUCT; else if (* pos == '[') token = START_TAG; else if (* pos == ']') token = END_TAG; else if (* pos == '(') token = OPEN_PAREN; else if (* pos == ')') token = CLOSE_PAREN; else if (* pos == ';') token = SEMI_COLON; else if (* pos == ':') { if ((*(pos + 1) == ':') && (*(pos + 2) == '=')) { token = ISDEF; pos += 2; len = 3; } else { AsnIoErrorMsg(aip, 59, *pos, aip->linenumber); token = ERROR_TOKEN; } } else if (IS_UPPER(*pos)) /* a reference or keyword */ { token = REF; while ((IS_ALPHANUM(*pos)) || (*pos == '-')) { pos++; len++; } aip->wordlen = len; asntype = AsnLexTMatchToken(aip); /* check types */ if (asntype) /* did it match ? */ { if ((asntype > 27) && (asntype < 57)) /* not a primitive type */ { token = asntype + 400; /* make a keyword type */ if (asntype == COMPS_TOKEN) /* COMPONENTS OF */ { if ((*(pos + 1) == 'O') && (*(pos + 2) == 'F') && (IS_WHITESP(*(pos+3)))) { pos += 3; /* move past OF */ len += 3; } else AsnIoErrorMsg(aip, 89, aip->linenumber); } } else if (asntype == 57) /* StringStore */ token = STRSTORE_TYPE; else if (asntype == 59) /* BitInt */ token = BIGINT_TYPE; else { switch (asntype) { case 3: /* BIT */ case 4: /* OCTET */ if (! StrMatch(asnwords[11], (pos+1), 6)) AsnIoErrorMsg(aip, 90, aip->linenumber); pos += 7; /* move past STRING */ len += 7; break; case 11: /* SEQUENCE */ case 13: /* SET */ if ((*(pos + 1) == 'O') && (*(pos + 2) == 'F')) { asntype++; /* SET or SEQ OF */ pos += 3; len += 3; if (! IS_WHITESP(*pos)) AsnIoErrorMsg(aip, 91, aip->linenumber); } break; case 6: /* OBJECT */ if ((! StrMatch(asnwords[55], (pos+1), 10))) /* IDENTIFIER */ AsnIoErrorMsg(aip, 92, aip->linenumber); pos += 11; len += 11; break; default: break; } token = asntype + 300; /* change to point at type */ } } pos--; /* move back for increment at end */ len--; } else if (IS_LOWER(*pos)) /* an identifier or valuereference */ { token = IDENT; while ((IS_ALPHANUM(*pos)) || (*pos == '-')) { pos++; len++; } pos--; /* move back for increment at end */ len--; } else if ((IS_DIGIT(*pos)) || ((*pos == '-') && (IS_DIGIT(*(pos+1))))) { token = NUMBER; if (*pos == '-') { pos++; len++; } while (IS_DIGIT(*pos)) { pos++; len++; } pos--; /* move back for increment at end */ len--; } else { AsnIoErrorMsg(aip, 59, *pos, aip->linenumber); token = ERROR_TOKEN; } len++; pos++; /* move over last symbol */ } aip->linepos = pos - aip->linebuf; aip->state = state; aip->wordlen = len; aip->token = token; return token; }
UINT32 AcpiUtStrtoul ( const NATIVE_CHAR *String, NATIVE_CHAR **Terminator, UINT32 Base) { UINT32 converted = 0; UINT32 index; UINT32 sign; const NATIVE_CHAR *StringStart; UINT32 ReturnValue = 0; ACPI_STATUS Status = AE_OK; /* * Save the value of the pointer to the buffer's first * character, save the current errno value, and then * skip over any white space in the buffer: */ StringStart = String; while (IS_SPACE (*String) || *String == '\t') { ++String; } /* * The buffer may contain an optional plus or minus sign. * If it does, then skip over it but remember what is was: */ if (*String == '-') { sign = NEGATIVE; ++String; } else if (*String == '+') { ++String; sign = POSITIVE; } else { sign = POSITIVE; } /* * If the input parameter Base is zero, then we need to * determine if it is octal, decimal, or hexadecimal: */ if (Base == 0) { if (*String == '0') { if (AcpiUtToLower (*(++String)) == 'x') { Base = 16; ++String; } else { Base = 8; } } else { Base = 10; } } else if (Base < 2 || Base > 36) { /* * The specified Base parameter is not in the domain of * this function: */ goto done; } /* * For octal and hexadecimal bases, skip over the leading * 0 or 0x, if they are present. */ if (Base == 8 && *String == '0') { String++; } if (Base == 16 && *String == '0' && AcpiUtToLower (*(++String)) == 'x') { String++; } /* * Main loop: convert the string to an unsigned long: */ while (*String) { if (IS_DIGIT (*String)) { index = *String - '0'; } else { index = AcpiUtToUpper (*String); if (IS_UPPER (index)) { index = index - 'A' + 10; } else { goto done; } } if (index >= Base) { goto done; } /* * Check to see if value is out of range: */ if (ReturnValue > ((ACPI_UINT32_MAX - (UINT32) index) / (UINT32) Base)) { Status = AE_ERROR; ReturnValue = 0L; /* reset */ } else { ReturnValue *= Base; ReturnValue += index; converted = 1; } ++String; } done: /* * If appropriate, update the caller's pointer to the next * unconverted character in the buffer. */ if (Terminator) { if (converted == 0 && ReturnValue == 0L && String != NULL) { *Terminator = (NATIVE_CHAR *) StringStart; } else { *Terminator = (NATIVE_CHAR *) String; } } if (Status == AE_ERROR) { ReturnValue = ACPI_UINT32_MAX; } /* * If a minus sign was present, then "the conversion is negated": */ if (sign == NEGATIVE) { ReturnValue = (ACPI_UINT32_MAX - ReturnValue) + 1; } return (ReturnValue); }
static bool varsub(RParse *p, RAnalFunction *f, ut64 addr, int oplen, char *data, char *str, int len) { RList *spargs = NULL; RList *bpargs = NULL; RAnalVar *var; RListIter *iter; char *oldstr, *newstr; char *tstr = strdup (data); if (!tstr) { return false; } if (!p->varlist) { free (tstr); return false; } if (p->relsub) { char *rip = (char *)r_str_casestr (tstr, "[pc, "); if (!rip) { rip = (char *)r_str_casestr (tstr, "[PC, "); } if (rip && !strchr (rip + 4, ',')) { rip += 4; char *tstr_new, *ripend = strchr (rip, ']'); const char *neg = strchr (rip, '-'); ut64 off = (oplen == 2 || strstr (tstr, ".w") || strstr(tstr, ".W")) ? 4 : 8; ut64 repl_num = (addr + off) & ~3; if (!ripend) { ripend = "]"; } if (neg) { repl_num -= r_num_get (NULL, neg + 1); } else { repl_num += r_num_get (NULL, rip); } rip -= 3; *rip = 0; tstr_new = r_str_newf ("%s0x%08"PFMT64x"%s", tstr, repl_num, ripend); free (tstr); tstr = tstr_new; } } bpargs = p->varlist (p->anal, f, 'b'); spargs = p->varlist (p->anal, f, 's'); bool ucase = IS_UPPER (*tstr); r_list_foreach (bpargs, iter, var) { char *reg = p->anal->reg->name[R_REG_NAME_BP]; char *tmplt = NULL; if (var->delta > -10 && var->delta < 10) { if (p->pseudo) { char sign = '+'; int delta = var->delta; if (var->delta < 0) { sign = '-'; delta = -delta; } oldstr = r_str_newf ("[%s %c %d]", reg, sign, delta); } else { oldstr = r_str_newf ("[%s, %d]", reg, var->delta); } } else if (var->delta > 0) { tmplt = p->pseudo ? "[%s + 0x%x]" : (ucase ? "[%s, 0x%X]" : "[%s, 0x%x]"); oldstr = r_str_newf (tmplt, reg, var->delta); } else { tmplt = p->pseudo ? "[%s - 0x%x]" : (ucase ? "[%s, -0x%X]" : "[%s, -0x%x]"); oldstr = r_str_newf (tmplt, reg, -var->delta); } if (ucase) { char *comma = strchr (oldstr, ','); if (comma) { *comma = 0; r_str_case (oldstr, true); *comma = ','; } } if (strstr (tstr, oldstr)) { if (p->localvar_only) { newstr = r_str_newf ("[%s]", var->name); } else { newstr = r_str_newf ("[%s %c %s]", reg, var->delta > 0 ? '+' : '-', var->name); } if (ucase) { char *comma = strchr (newstr, ' '); if (comma) { *comma = 0; r_str_case (newstr, true); *comma = ' '; } } tstr = r_str_replace (tstr, oldstr, newstr, 1); free (newstr); free (oldstr); break; } free (oldstr); }
static void AddXMLname(AsnTypePtr atp, AsnTypePtr PNTR typestack, Int2 stackptr) { Char buf[80]; AsnTypePtr atp2; Boolean found=FALSE, getparent=TRUE, doitem = FALSE, done = FALSE; CharPtr tmp; Int2 i, isa; if (atp->XMLname == NULL) /* only do it once */ { tmp = buf; *tmp = '\0'; if (atp->name == NULL) { doitem = TRUE; } else if (IS_UPPER(*(atp->name))) { getparent = FALSE; tmp = StringMove(tmp, atp->name); } if ((getparent) && (stackptr)) { atp2 = typestack[stackptr - 1]; isa = AsnFindBaseIsa(atp2); if ((doitem) && ((isa == SEQOF_TYPE) || (isa == SETOF_TYPE))) { atp2 = AsnFindBaseTypeDTD(atp); if (atp2 != NULL) { if (atp2->name == NULL) { atp2 = typestack[stackptr - 1]; if (atp->type != NULL) { if (ISA_BASETYPE(atp->type->isa)) { if (atp2->XMLname != NULL) { tmp = StringMove(tmp, atp2->XMLname); getparent = FALSE; found = TRUE; } } } } else { if (atp2->XMLname != NULL) tmp = StringMove(tmp, atp2->XMLname); else if (atp2->name != NULL) tmp = StringMove(tmp, atp2->name); if (*buf != '\0') { doitem = FALSE; getparent = FALSE; done = TRUE; found = TRUE; } } } } atp2 = typestack[stackptr - 1]; if ((getparent) && (atp2->XMLname != NULL)) /* already nested */ { tmp = StringMove(tmp, atp2->XMLname); tmp = StringMove(tmp, "_"); getparent = FALSE; found = TRUE; } if ((stackptr) && (getparent)) { found = FALSE; for (i = (stackptr - 1); (i >= 0) && (! found); i--) { atp2 = AsnFindBaseTypeDTD(typestack[i]); if ((atp2 != NULL) && (atp2->name != NULL) && (IS_UPPER(*(atp2->name)))) { while (i < stackptr) { atp2 = AsnFindBaseTypeDTD(typestack[i]); if (atp2->name == NULL) tmp = StringMove(tmp, "E"); else tmp = StringMove(tmp, atp2->name); tmp = StringMove(tmp, "_"); i++; } found = TRUE; } } } if ((! done) && found) { if (doitem) tmp = StringMove(tmp, "_E"); else tmp = StringMove(tmp, atp->name); } } if (*buf != '\0') { atp->XMLname = StringSave(buf); } } atp2 = AsnFindBaseTypeDTD(atp); if ((! atp->imported) && (atp2 != NULL) && (atp2->type != NULL)) { isa = atp2->type->isa; if ((isa == SEQ_TYPE) || (isa == SET_TYPE)) { typestack[stackptr] = atp; for (atp2 = atp->branch; atp2 != NULL; atp2 = atp2->next) AddXMLname(atp2, typestack, (Int2)(stackptr + 1)); } else if ((isa == SEQOF_TYPE) || (isa == SETOF_TYPE)) { typestack[stackptr] = atp; for (atp2 = atp->branch; atp2 != NULL; atp2 = atp2->next) AddXMLname(atp2, typestack, (Int2)(stackptr + 1)); } else if (isa == CHOICE_TYPE) { typestack[stackptr] = atp; for (atp2 = atp->branch; atp2 != NULL; atp2 = atp2->next) AddXMLname(atp2, typestack, (Int2)(stackptr + 1)); } } return; }
/***************************************************************************** * * AsnTypePtr AsnTypeFindType(atp, str, typeptr, count, in_it) * *****************************************************************************/ NLM_EXTERN AsnTypePtr AsnTypeFindType (AsnTypePtr atp, CharPtr str, AsnTypePtr PNTR typeptr, Int2 count, Boolean in_it) { AsnTypePtr curr, next; CharPtr beg; Int2 len, isa; Boolean is_element, /* (unamed) element of setof seqof */ is_type, /* if a type, must find the original def */ got_it; AsnTypePtr PNTR typenext; Int2 newcount; if (count < 0) /* off array ... error condition */ return NULL; beg = str; len = 0; if (typeptr == NULL) { typenext = NULL; newcount = 0; } else { newcount = count - 1; if (newcount >= 0) typenext = typeptr + 1; else typenext = NULL; } is_element = FALSE; is_type = FALSE; while ((*beg != '.') && (*beg != '\0')) { beg++; len++; } if ((*str == 'E') && (len == 1)) is_element = TRUE; else if (IS_UPPER(*str)) is_type = TRUE; curr = atp; /* look at this level */ while (curr != NULL) { got_it = FALSE; if (is_element) got_it = TRUE; else if (StrMatch(curr->name, str, len)) /* found it */ { if (! is_type) got_it = TRUE; else /* check that this is the real def */ { if (! curr->imported) got_it = TRUE; } } if (got_it) { if (typeptr != NULL) *typeptr = curr; if (*beg == '\0') /* that's it */ return curr; else /* go down the path */ { next = AsnFindBaseType(curr); isa = next->type->isa; if ((next->branch != NULL) /* go further */ && (isa != INTEGER_TYPE) && (isa != ENUM_TYPE)) { next = AsnTypeFindType((AsnTypePtr) next->branch, (beg + 1), typenext, newcount, TRUE); if (next != NULL) return next; } } } curr = curr->next; } if (in_it) /* processing a path and can't find next node here */ return NULL; curr = atp; /* start down a level */ while (curr != NULL) { if (curr->branch != NULL) { next = AsnFindBaseType(curr); if ((next != NULL) && (next->type != NULL)) { isa = next->type->isa; if ((next->branch != NULL) /* go further */ && (isa != INTEGER_TYPE) && (isa != ENUM_TYPE)) { next = AsnTypeFindType((AsnTypePtr) next->branch, str, typenext, newcount, FALSE); if (next != NULL) { if (typeptr != NULL) *typeptr = curr; return next; } } } /** else, an error has occurred, unresolved branch **/ } curr = curr->next; } return NULL; }
/***************************************************************************** * * Int2 AsnLexScan(aip, name) * scans until name ::= to start reading asn.1 past garbage * *****************************************************************************/ static Int2 AsnLexScan (AsnIoPtr aip, CharPtr name) { register CharPtr pos; Int2 token = 0; register int linepos, len; int done; Boolean started = FALSE, matched_ref = FALSE; if (aip->type_indent) /* only for start */ return ERROR_TOKEN; if (! aip->bytes) /* no data loaded */ AsnIoGets(aip); linepos = aip->linepos; pos = aip->linebuf + linepos; len = 0; while (*pos == '\n' || *pos == '\r') /* skip empty lines */ { pos = AsnIoGets(aip); /* get a line */ if (pos == NULL) return EOF_TOKEN; } while (! started) { while ((* pos <= ' ') || ((*pos == '-') && (*(pos+1) == '-'))) /* skip leading white space */ { if (*pos == '\n' || *pos == '\r') { pos = AsnIoGets(aip); if (pos == NULL) return EOF_TOKEN; } else if ((*pos == '-') && (*(pos+1) == '-')) /* skip comments */ { pos += 2; done = 0; while (! done) { if ((*pos == '-') && (*(pos +1) == '-')) { pos += 2; done = 1; } else if (*pos == '\n' || *pos == '\r') done = 1; else pos++; } } else pos++; } aip->word = pos; len = 0; if (* pos == ':') { if ((*(pos + 1) == ':') && (*(pos + 2) == '=')) { token = ISDEF; pos += 2; len = 3; if (matched_ref) started = TRUE; else { AsnIoErrorMsg(aip, 59, *pos, aip->linenumber); return ERROR_TOKEN; } } else matched_ref = FALSE; } else if (IS_UPPER(*pos)) /* a reference or keyword */ { token = REF; while ((IS_ALPHANUM(*pos)) || (*pos == '-')) { pos++; len++; } pos--; /* move back for increment at end */ aip->wordlen = len; if (StrMatch(name, aip->word, (Int2)len)) matched_ref = TRUE; else matched_ref = FALSE; } else matched_ref = FALSE; pos++; /* move over last symbol */ } /* found it , do normal return after ::= */ aip->linepos = pos - aip->linebuf; aip->wordlen = len; aip->token = token; return token; }
/***************************************************************************** * * AsnTypePtr AsnTxtReadId(aip, amp, atp) * reads identifier for next value * *****************************************************************************/ NLM_EXTERN AsnTypePtr LIBCALL AsnTxtReadId (AsnIoPtr aip, AsnModulePtr amp, AsnTypePtr atp) { Int2 token, isa; AsnTypePtr atp2, atp3, atp4; PstackPtr currpsp, prevpsp; Boolean is_ref; if (! aip->type_indent) /* just starting reading */ { if (atp == NULL) /* starting an unknown item */ { token = AsnLexWord(aip); if (token == EOF_TOKEN) return NULL; if (token != REF) { AsnIoErrorMsg(aip, 26, aip->linenumber); return NULL; } atp = AsnLexFindType(aip, amp); if (atp == NULL) { AsnIoErrorMsg(aip, 27, aip->linenumber); return NULL; } token = AsnLexWord(aip); if (token != ISDEF) { AsnIoErrorMsg(aip, 28, aip->linenumber); return NULL; } } else /* reading a known, possibly internal value */ { if ((atp->name != NULL) && /* do we need to read a type ref? */ (IS_UPPER(*atp->name))) is_ref = TRUE; else is_ref = FALSE; if ((is_ref) && (aip->scan_for_start)) { token = AsnLexScan(aip, atp->name); /* scan to start */ if (token == EOF_TOKEN) return NULL; if (token != ISDEF) { AsnIoErrorMsg(aip, 28, aip->linenumber); return NULL; } } else if ((is_ref) || (aip->bytes)) /* reading an internal value */ { token = AsnLexWord(aip); if (token == EOF_TOKEN) return NULL; if (! StrMatch(atp->name, aip->word, aip->wordlen)) { AsnIoErrorMsg(aip, 29,AsnErrGetTypeName(atp->name), aip->linenumber); return NULL; } if (token == REF) /* read of a non-internal value */ { token = AsnLexWord(aip); if (token != ISDEF) { AsnIoErrorMsg(aip, 28, aip->linenumber); return NULL; } } } } aip->typestack[0].type = atp; /* load first type */ aip->typestack[0].resolved = FALSE; return atp; } currpsp = & aip->typestack[aip->type_indent]; prevpsp = currpsp - 1; if (currpsp->type != NULL) /* reading in a struct */ { token = AsnLexWord(aip); switch (token) { case COMMA: break; case END_STRUCT: return prevpsp->type; default: AsnIoErrorMsg(aip, 30, aip->linenumber); return NULL; } } /* check for SEQOF and SETOF */ atp = prevpsp->type; atp2 = AsnFindBaseType(atp); isa = atp2->type->isa; if ((isa == SEQOF_TYPE) || (isa == SETOF_TYPE)) { token = AsnLexWord(aip); if (token == END_STRUCT) /* empty set */ return prevpsp->type; aip->tagsaved = TRUE; /* not empty, note not read */ atp = (AsnTypePtr) atp2->branch; /* set of what type? */ currpsp->type = atp; currpsp->resolved = FALSE; return atp; /* no type identifier */ } token = AsnLexWord(aip); if (token == EOF_TOKEN) { AsnIoErrorMsg(aip, 17 ); return NULL; } if ((token == END_STRUCT) && ((isa == SEQ_TYPE) || (isa == SET_TYPE))) return prevpsp->type; /* empty SET/SEQ */ if (token != IDENT) { AsnIoErrorMsg(aip, 31, aip->linenumber); return NULL; } atp = AsnLexFindElement(aip, (AsnTypePtr) atp2->branch); if (atp == NULL) { AsnIoErrorMsg(aip, 32, AsnErrGetTypeName(prevpsp->type->name), aip->linenumber); return NULL; } if (atp2->type->isa == SEQ_TYPE) /* check sequence order */ { atp3 = currpsp->type; if (atp3 != NULL) { atp4 = (AsnTypePtr) atp2->branch; atp3 = atp3->next; while (atp4 != atp3) { if (atp == atp4) { AsnIoErrorMsg(aip, 33,AsnErrGetTypeName(atp->name), AsnErrGetTypeName(atp2->name), aip->linenumber); return NULL; } atp4 = atp4->next; } } else atp3 = (AsnTypePtr) atp2->branch; while ((atp3 != NULL) && (atp3 != atp)) { if (! (atp3->optional || atp3->hasdefault)) { AsnIoErrorMsg(aip, 34,AsnErrGetTypeName(atp3->name), AsnErrGetTypeName(atp2->name), aip->linenumber); return NULL; } atp3 = atp3->next; } } currpsp->type = atp; currpsp->resolved = FALSE; /* mark first use */ return atp; }
static Int2 AsnLexWordEx (AsnIoPtr aip, Uint1 fix_non_print) { register CharPtr pos; Int1 state; Int2 token; register int linepos, len; int done; if (aip->tagsaved) /* had to read ahead */ { aip->tagsaved = FALSE; return aip->token; } if (! aip->bytes) /* no data loaded */ AsnIoGets(aip); linepos = aip->linepos; pos = aip->linebuf + linepos; state = aip->state; len = 0; while (*pos == '\n' || *pos == '\r') /* skip empty lines */ { pos = AsnIoGets(aip); /* get a line */ if (pos == NULL) return EOF_TOKEN; } if (state == IN_STRING_STATE) { aip->word = pos; if ((* pos == '\"') && (*(pos + 1) != '\"')) /* end of string */ { token = END_STRING; pos++; state = 0; /* reset state */ } else { token = IN_STRING; while ((* pos != '\"') && (* pos != '\n') && (* pos != '\r')) { if ((fix_non_print != 2) && ((*pos < ' ') || (*pos > '~'))) { done = (int)(*pos); *pos = '\0'; if ((fix_non_print == 0) || (fix_non_print == 3)) { AsnIoErrorMsg(aip, 106, done, aip->word); } done = 0; *pos = '#'; /* replace with # */ } pos++; len++; } if ((*pos != '\n') && (*pos != '\r') && (* (pos + 1) == '\"')) /* internal quote */ { len++; /* include in previous string */ pos += 2; /* point to rest of string */ } } } else if (state == IN_BITHEX_STATE) { aip->word = pos; if (*pos == '\'') /* end of binhex */ { state = 0; /* set to normal */ pos++; /* move past quote */ while (IS_WHITESP(*pos)) { if (*pos == '\n' || *pos == '\r') /* skip empty lines */ { pos = AsnIoGets(aip); /* get a line */ if (pos == NULL) return EOF_TOKEN; } else pos++; } if (* pos == 'H') token = OCTETS; else if (* pos == 'B') token = ASNBITS; else { AsnIoErrorMsg(aip, 58, aip->linenumber); token = ERROR_TOKEN; } pos++; /* move past H or B */ } else { token = IN_BITHEX; while ((* pos != '\'') && (* pos != '\n') && (* pos != '\r')) { pos++; len++; } } } else /* normal scanning */ { while ((* pos <= ' ') || ((*pos == '-') && (*(pos+1) == '-'))) /* skip leading white space */ { if (*pos == '\n' || *pos == '\r') { pos = AsnIoGets(aip); if (pos == NULL) return EOF_TOKEN; } else if ((*pos == '-') && (*(pos+1) == '-')) /* skip comments */ { pos += 2; done = 0; while (! done) { if ((*pos == '-') && (*(pos +1) == '-')) { pos += 2; done = 1; } else if (*pos == '\n' || *pos == '\r') done = 1; else pos++; } } else pos++; } aip->word = pos; if (* pos == '\"') { token = START_STRING; state = IN_STRING_STATE; } else if (* pos == '\'') { token = START_BITHEX; state = IN_BITHEX_STATE; } else if (* pos == ',') token = COMMA; else if (* pos == '{') token = START_STRUCT; else if (* pos == '}') token = END_STRUCT; else if (* pos == '[') token = START_TAG; else if (* pos == ']') token = END_TAG; else if (* pos == '(') token = OPEN_PAREN; else if (* pos == ')') token = CLOSE_PAREN; else if (* pos == ';') token = SEMI_COLON; else if (* pos == ':') { if ((*(pos + 1) == ':') && (*(pos + 2) == '=')) { token = ISDEF; pos += 2; len = 3; } else { AsnIoErrorMsg(aip, 59, *pos, aip->linenumber); token = ERROR_TOKEN; } } else if (IS_UPPER(*pos)) /* a reference or keyword */ { token = REF; while ((IS_ALPHANUM(*pos)) || (*pos == '-')) { pos++; len++; } pos--; /* move back for increment at end */ len--; } else if (IS_LOWER(*pos)) /* an identifier or valuereference */ { token = IDENT; while ((IS_ALPHANUM(*pos)) || (*pos == '-')) { pos++; len++; } pos--; /* move back for increment at end */ len--; } else if ((IS_DIGIT(*pos)) || ((*pos == '-') && (IS_DIGIT(*(pos+1))))) { token = NUMBER; if (*pos == '-') { pos++; len++; } while (IS_DIGIT(*pos)) { pos++; len++; } pos--; /* move back for increment at end */ len--; } else { AsnIoErrorMsg(aip, 59, *pos, aip->linenumber); token = ERROR_TOKEN; } len++; pos++; /* move over last symbol */ } aip->linepos = pos - aip->linebuf; /******************** check on MSWIN linepos = 0; while (pos != linebuf) { linepos++; pos++; } aip->linepos = linepos; **********************/ aip->state = state; aip->wordlen = len; aip->token = token; return token; }