⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 asn_gen.c

📁 ASN.1语法解析代码
💻 C
📖 第 1 页 / 共 3 页
字号:
    if (*buf > ' ') return 1;      /* had ',' or '}' */
    if (!(lth = read(fd, buf, 1)) || *buf == '\n') return 1;
    if (*buf <= ' ') c = buf;
    }
do                                                      /* step 2 */
    {
    for (buf[1] = 0; c == buf && (lth = read(fd, c, 1)) && *c <= ' '; )
        {
	if(*c == '\n')
	    {
	    if (!(lth = read(fd, c, 1))) c[1] = 0;
            if (*c == '\n') return 1;
	    if (*c > ' ') break;
	    }
	}
    if (!lth) return lth;
    c = buf;
    if (*c == skip)
        {
        if (skip == '(') partner = ')';
        else if (skip == '[' || skip == '{') partner = skip + 2;
        else partner = skip;
        for (c++, parens = 1; parens; )
            {
            if (!(lth = read(fd, c, 1))) return 0;
            if (*c == skip) parens++;
            if (*c == partner) parens--;
	    if (*c == '-')    /* skip over a comment */
		{
		if (!(lth = read(fd, c, 1))) return 0;
		if (*c == '-')
		    {
		    while ((lth = read(fd, c, 1)))
			{
                        if (*c == '\n') break;
                        if (*c == '-')
			    {
			    if (!(lth = read(fd, c, 1))) return 0;
			    if (*c == '-') break;
			    }
			}
		    }
		}
            }
	c = buf;
	}
    }
while (*buf == skip);                                           /* step 3 */
if (*c == '[' || *c == ',' || *c == '(' || *c == '{' || *c == '}' ||
    *c == ';' || *c == partner) c++;
else
    {
    c++;
    do
	{                               /* for commented blank lines */
        while ((lth = read(fd, c, 1)) && (*buf != '-' || *buf != buf[1]) &&
            *c > ' ' && *c != '{' && *c != '}' && *c != ',' && *c != '(' &&
            *c != ')' && *c != ';') c++;
	if (*buf == '-' && buf[1] == '-')
	    {
	    for (c = &buf[2]; (lth = read(fd, c, 1)) && *c != '\n' &&
                c < &buf[4]; )
                {
                if (*c == '-') c++;
		else c = &buf[2];
		}
	    if (!(lth = get_token(fd, buf, skip))) return 0;
            *(c = &buf[lth]) = 0;
	    }
	}
    while (*buf == '-' && buf[1] == '-');
    if (*c > ' ' || *c == '\n')
	{
	if (c > buf) comma = *c;
	else c++;
	}
    }
*c = 0;
lth = (c - buf);
while(c > buf) if (*(--c) == '-') *c = '_';
return lth;
}

int isreserved(char *name)
{
char **p;
for(p = &reserved_words[1]; *p && **p < *name; p++);
while (*p && **p == *name && strcmp(name, *p)) p++;
if (*p && **p == *name) return p - reserved_words;
return 0;
}

struct ub_table *is_ub(char *name)
{
struct ub_table *ntbp, *entbp;
for (ntbp = (struct ub_table *)ub_area.area, entbp = &ntbp[ub_area.next];
    ntbp < entbp && strcmp(ntbp->name, name); ntbp++);
if (ntbp >= entbp) return (struct ub_table *)0;
return ntbp;
}

static int *indexlist, indexsize;

int loop_test(struct name_table *table, struct name_table *ctbp,
    int loops)
{
/**
Function: Looks recursively to see if any item is its own ancestor
Returns: 1 if loop found, ELSE zero
Procedure:
1. IF item is a pointer item, return 0
   FOR each parent of the item
	IF it's in the indexlist, return 1
	IF the indexlist isn't big enough, enlarge it
	Put the parent's index in the list
	IF loop_test for that parent returns 1
	    Print the child name
	    Return 1
2. Return 0
**/
struct parent *cparentp;
int *inxp, *einxp = &indexlist[loops];
if ((ctbp->flags & ASN_POINTER_FLAG)) return 0;
for(cparentp = &ctbp->parent; cparentp; cparentp = cparentp->next)
    {
    if (cparentp->index < 0) continue;
    for (inxp = indexlist; inxp && inxp < einxp; inxp++)
	{
        if (*inxp == cparentp->index)
	    {
            printf("Nesting detected: %s contains", ctbp->name);
	    for (einxp = &indexlist[loops - 2]; einxp >= inxp;
		printf(" %s\n    which contains", table[*einxp--].name));
	    printf (" %s\n", ctbp->name);
	    return 1;
    	    }
	}
    if (loops >= indexsize)
	{
	if ((!indexsize && !(indexlist = (int *)calloc(4, sizeof(int)))) ||
	    (indexlist && !(indexlist = (int *)recalloc((char *)indexlist,
	    (indexsize * sizeof(int)), ((indexsize + 4) * sizeof(int))))))
	    fatal(7, (char *)0);
	indexsize += 4;
	}
    indexlist[loops] = cparentp->index;
    if (loop_test(table, &table[cparentp->index], loops + 1)) return 1;
    }
return 0;
}

void mk_in_name(char *to, char *part1, char *part2)
{
cat(cat(cat(to, part1), "In"), part2);
}

FILE *mk_tempfile(char *buf, char *msg)
{
char *c;
FILE *nstr;
strcpy(buf, "asnXXXXXX");
if (!(c = mktemp(buf)) || !*c ||
    !(nstr = fopen(c, "w+"))) fatal(2, msg);
return nstr;
}

static void print_if_include(FILE *outstr, char *name)
{
char *b, locbuf[80];
for(b = strcpy(locbuf, name); *b; b++)
    {
    if (*b == '.') *b = '_';
    }
fprintf(outstr, if_include, locbuf, name);
}

void print_tables()
{
struct ub_table *ubp, *eubp;
struct id_table *idp, *eidp;
int did;
struct name_table *ntbp;
struct parent *parentp;
for(did = 0, ntbp = (struct name_table *)name_area.area; vflag && ntbp <
    (struct name_table *)&name_area.area[name_area.next * name_area.item];
    ntbp++, did++)
    {
    if (!did) printf("Start position for non-imports is 0x%lX\n", real_start);
    printf("#%d Name %s generation %d, at ", did, ntbp->name, ntbp->generation);
    if (ntbp->pos < 0) printf("-1");
    else printf("0x%lX", ntbp->pos);
    printf(", flags 0x%X, type 0x%lX, ", ntbp->flags, ntbp->type);
    if (ntbp->tag != -1) printf("tag 0x%lX, ", ntbp->tag);
    if (ntbp->subtype != -1) printf("subtype %s, ", find_define(ntbp->subtype));
    if (ntbp->max) printf("min %d, max %d ", ntbp->min, ntbp->max);
    printf("has:\n");
    for(parentp = &ntbp->parent; parentp; parentp = parentp->next)
	{
	printf("    Parent %d, mymap is '%s', length %d\n", parentp->index,
	    parentp->mymap, parentp->map_lth);
        }
    }
if (bflag && ub_area.area) printf("Defined values:\n");
for(ubp = (struct ub_table *)ub_area.area, eubp = &ubp[ub_area.next];
    bflag && ubp < eubp; ubp++)
    printf("Name: %s, value %d\n", ubp->name, ubp->val);
bflag = 0;
if (dflag && id_area.area) printf("Defined object identifiers:\n");
for(idp = (struct id_table *)id_area.area, eidp = &idp[id_area.next];
    dflag && idp < eidp; idp++)
    printf("Name: %s, value %s\n", idp->name, idp->val);
dflag = 0;
}

int putobjid(char *to, int val, int lev)
{
char *c = to;
uchar tmp = (val & 0x7F);
if (lev) tmp += 0x80;
if ((val >>= 7)) c += putobjid(to, val, lev + 1);
sprintf(c, "\\%03o", tmp);
return (c - to) + 4;
}

int putoct(char *to, int val)
{
char *c = to;
int tmp = val & 0xFF;
if ((val >>= 8)) c += putoct(c, val);
sprintf(c, "\\%03o", tmp);
return (c - to) + 4;
}

char *recalloc(char *from, int oldsize, int newsize)
{
char *to;
if ((to = calloc(newsize, 1)))
    {
    memcpy(to, from, oldsize);
    free(from);
    }
return to;
}

struct name_table *replace_name(char *itemname)
{
/**
Function: Replaces itemname with a name to which it is equated, e.g. replaces
A with B if A ::= B
Returns: Pointer to name table entry of replacement name
Procedure:
1. IF itemname is not in object table, return 0
2. WHILE FALSE flag is set
	Find the (only) child of this parent
	Make that the parent
3. Copy child's name to itemname
   Return pointer to table item
**/
struct name_table *ctbp, *ptbp, *table = (struct name_table *)name_area.area;
struct parent *cparentp;
int parent;
if (!(ptbp = find_name(itemname))->name) return (struct name_table *)0;
for ( ; (ptbp->flags & ASN_FALSE_FLAG); ptbp = ctbp)
    {
    parent = ptbp - table;
    for (ctbp = table; ctbp->name; ctbp++)
	{
	for(cparentp = &ctbp->parent; cparentp; cparentp = cparentp->next)
	    {
	    if (cparentp->index == parent) break;
	    }
	if (cparentp) break;
	}
    if (!ctbp->name) syntax(itemname);
    }
cat(itemname, ptbp->name);
return ptbp;
}

int set_name_option(char *to, char *from)
{
int ansr =  (*from == '*')? ASN_POINTER_FLAG: 0;
if (to != from) cat(to, from);
if (*to == '*') *to = '_';
return ansr;
}

void syntax(char *name)
{
if (!*name) name = "no item name";
fatal(12, name);
}

int test_dup(char *objname, int *type)
{
/**
Function: Tests if objname needs a dup function
Returns: Logical OR of
         ASN_OF_FLAG     IF objname is a SET/SEQUENCE OF (class AsnOf)
	 ASN_DUPED_FLAG  IF parent  is a SET/SEQUENCE OF OR
                            objname is exported          (needs _dup() & index)
	 ASN_POINTER_FLAG if _objname exists & is a pointer (needs _point())
Procedure:
1. Find objname in table
   IF it's false OR its type is primitive universal, return 0
   IF table has a '_objname' OR objname starts with '_', set POINTER flag
2. Search objname's parents
        IF a parent has the OF flag or the POINTER flag set
	    Break out of the search
3. IF there is such a parent
        IF the parent had the POINTER flag, set the POINTER bit in answer
	IF the parent had the OF flag, set the DUPED bit in answer
   IF objname has the OF flag set, set the OF bit in answer
   IF objname is exported, set the DUPED flag in the answer -- in case it
        becomes a member of an OF
   Return the answer
**/
struct name_table *ntbp, *ptbp;
struct parent *parentp;
int ansr = 0;
char name[128];
*name = '_';
cat (&name[1], objname);
ntbp = find_name(objname);                    /* step 1 */
if (!ntbp->name || (ntbp->flags & ASN_FALSE_FLAG)) return 0;
if (*objname == '_') ansr |= ASN_POINTER_FLAG;
								/* step 2 */
for (parentp = &ntbp->parent; parentp; parentp = parentp->next)
    {
    if (parentp->index < 0) continue;
    ptbp = (struct name_table*)&name_area.area[parentp->index *
	sizeof(*ptbp)];
    if ((ptbp->flags & ASN_OF_FLAG)) break;
    }
								/* step 3 */
if (parentp)
    {
    if ((ptbp->flags & ASN_OF_FLAG)) ansr |= ASN_DUPED_FLAG;
    }
if ((ntbp->flags & ASN_OF_FLAG)) ansr |= ASN_OF_FLAG;
if ((ntbp->flags & ASN_EXPORT_FLAG)) ansr |= ASN_DUPED_FLAG;
*type = ntbp->type;
return ansr;
}

void warn(int err, char *param)
{
if (err) fprintf(stderr, "****** In class %s, \007", classname);
fprintf(stderr, msgs[err], param == (char *)0 ? "(NULL)" : param);
}

int wdcmp(char *s1, char *s2)
{
for( ; *s1 > ' ' && *s1 == *s2; s1++, s2++);
if (*s1 <= ' ' && *s2 <= ' ') return 0;
return 1;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -