提交 c05cae9a 编写于 作者: D David Howells

ASN.1: Copy string names to tokens in ASN.1 compiler

Copy string names to tokens in ASN.1 compiler rather than storing a pointer
into the source text.  This means we don't have to use "%*.*s" all over the
place.
Signed-off-by: NDavid Howells <dhowells@redhat.com>
Reviewed-by: NDavid Woodhouse <David.Woodhouse@intel.com>
上级 ae44a2f6
...@@ -294,8 +294,8 @@ static const char *const directives[NR__DIRECTIVES] = { ...@@ -294,8 +294,8 @@ static const char *const directives[NR__DIRECTIVES] = {
struct action { struct action {
struct action *next; struct action *next;
char *name;
unsigned char index; unsigned char index;
char name[];
}; };
static struct action *action_list; static struct action *action_list;
...@@ -306,7 +306,7 @@ struct token { ...@@ -306,7 +306,7 @@ struct token {
enum token_type token_type : 8; enum token_type token_type : 8;
unsigned char size; unsigned char size;
struct action *action; struct action *action;
const char *value; char *content;
struct type *type; struct type *type;
}; };
...@@ -328,11 +328,9 @@ static int directive_compare(const void *_key, const void *_pdir) ...@@ -328,11 +328,9 @@ static int directive_compare(const void *_key, const void *_pdir)
dlen = strlen(dir); dlen = strlen(dir);
clen = (dlen < token->size) ? dlen : token->size; clen = (dlen < token->size) ? dlen : token->size;
//debug("cmp(%*.*s,%s) = ", //debug("cmp(%s,%s) = ", token->content, dir);
// (int)token->size, (int)token->size, token->value,
// dir);
val = memcmp(token->value, dir, clen); val = memcmp(token->content, dir, clen);
if (val != 0) { if (val != 0) {
//debug("%d [cmp]\n", val); //debug("%d [cmp]\n", val);
return val; return val;
...@@ -352,7 +350,7 @@ static int directive_compare(const void *_key, const void *_pdir) ...@@ -352,7 +350,7 @@ static int directive_compare(const void *_key, const void *_pdir)
static void tokenise(char *buffer, char *end) static void tokenise(char *buffer, char *end)
{ {
struct token *tokens; struct token *tokens;
char *line, *nl, *p, *q; char *line, *nl, *start, *p, *q;
unsigned tix, lineno; unsigned tix, lineno;
/* Assume we're going to have half as many tokens as we have /* Assume we're going to have half as many tokens as we have
...@@ -411,11 +409,11 @@ static void tokenise(char *buffer, char *end) ...@@ -411,11 +409,11 @@ static void tokenise(char *buffer, char *end)
break; break;
tokens[tix].line = lineno; tokens[tix].line = lineno;
tokens[tix].value = p; start = p;
/* Handle string tokens */ /* Handle string tokens */
if (isalpha(*p)) { if (isalpha(*p)) {
const char **dir; const char **dir, *start = p;
/* Can be a directive, type name or element /* Can be a directive, type name or element
* name. Find the end of the name. * name. Find the end of the name.
...@@ -426,10 +424,18 @@ static void tokenise(char *buffer, char *end) ...@@ -426,10 +424,18 @@ static void tokenise(char *buffer, char *end)
tokens[tix].size = q - p; tokens[tix].size = q - p;
p = q; p = q;
tokens[tix].content = malloc(tokens[tix].size + 1);
if (!tokens[tix].content) {
perror(NULL);
exit(1);
}
memcpy(tokens[tix].content, start, tokens[tix].size);
tokens[tix].content[tokens[tix].size] = 0;
/* If it begins with a lowercase letter then /* If it begins with a lowercase letter then
* it's an element name * it's an element name
*/ */
if (islower(tokens[tix].value[0])) { if (islower(tokens[tix].content[0])) {
tokens[tix++].token_type = TOKEN_ELEMENT_NAME; tokens[tix++].token_type = TOKEN_ELEMENT_NAME;
continue; continue;
} }
...@@ -458,6 +464,13 @@ static void tokenise(char *buffer, char *end) ...@@ -458,6 +464,13 @@ static void tokenise(char *buffer, char *end)
q++; q++;
tokens[tix].size = q - p; tokens[tix].size = q - p;
p = q; p = q;
tokens[tix].content = malloc(tokens[tix].size + 1);
if (!tokens[tix].content) {
perror(NULL);
exit(1);
}
memcpy(tokens[tix].content, start, tokens[tix].size);
tokens[tix].content[tokens[tix].size] = 0;
tokens[tix++].token_type = TOKEN_NUMBER; tokens[tix++].token_type = TOKEN_NUMBER;
continue; continue;
} }
...@@ -466,6 +479,7 @@ static void tokenise(char *buffer, char *end) ...@@ -466,6 +479,7 @@ static void tokenise(char *buffer, char *end)
if (memcmp(p, "::=", 3) == 0) { if (memcmp(p, "::=", 3) == 0) {
p += 3; p += 3;
tokens[tix].size = 3; tokens[tix].size = 3;
tokens[tix].content = "::=";
tokens[tix++].token_type = TOKEN_ASSIGNMENT; tokens[tix++].token_type = TOKEN_ASSIGNMENT;
continue; continue;
} }
...@@ -475,12 +489,14 @@ static void tokenise(char *buffer, char *end) ...@@ -475,12 +489,14 @@ static void tokenise(char *buffer, char *end)
if (memcmp(p, "({", 2) == 0) { if (memcmp(p, "({", 2) == 0) {
p += 2; p += 2;
tokens[tix].size = 2; tokens[tix].size = 2;
tokens[tix].content = "({";
tokens[tix++].token_type = TOKEN_OPEN_ACTION; tokens[tix++].token_type = TOKEN_OPEN_ACTION;
continue; continue;
} }
if (memcmp(p, "})", 2) == 0) { if (memcmp(p, "})", 2) == 0) {
p += 2; p += 2;
tokens[tix].size = 2; tokens[tix].size = 2;
tokens[tix].content = "})";
tokens[tix++].token_type = TOKEN_CLOSE_ACTION; tokens[tix++].token_type = TOKEN_CLOSE_ACTION;
continue; continue;
} }
...@@ -491,22 +507,27 @@ static void tokenise(char *buffer, char *end) ...@@ -491,22 +507,27 @@ static void tokenise(char *buffer, char *end)
switch (*p) { switch (*p) {
case '{': case '{':
p += 1; p += 1;
tokens[tix].content = "{";
tokens[tix++].token_type = TOKEN_OPEN_CURLY; tokens[tix++].token_type = TOKEN_OPEN_CURLY;
continue; continue;
case '}': case '}':
p += 1; p += 1;
tokens[tix].content = "}";
tokens[tix++].token_type = TOKEN_CLOSE_CURLY; tokens[tix++].token_type = TOKEN_CLOSE_CURLY;
continue; continue;
case '[': case '[':
p += 1; p += 1;
tokens[tix].content = "[";
tokens[tix++].token_type = TOKEN_OPEN_SQUARE; tokens[tix++].token_type = TOKEN_OPEN_SQUARE;
continue; continue;
case ']': case ']':
p += 1; p += 1;
tokens[tix].content = "]";
tokens[tix++].token_type = TOKEN_CLOSE_SQUARE; tokens[tix++].token_type = TOKEN_CLOSE_SQUARE;
continue; continue;
case ',': case ',':
p += 1; p += 1;
tokens[tix].content = ",";
tokens[tix++].token_type = TOKEN_COMMA; tokens[tix++].token_type = TOKEN_COMMA;
continue; continue;
default: default:
...@@ -527,10 +548,7 @@ static void tokenise(char *buffer, char *end) ...@@ -527,10 +548,7 @@ static void tokenise(char *buffer, char *end)
{ {
int n; int n;
for (n = 0; n < nr_tokens; n++) for (n = 0; n < nr_tokens; n++)
debug("Token %3u: '%*.*s'\n", debug("Token %3u: '%s'\n", n, token_list[n].content);
n,
(int)token_list[n].size, (int)token_list[n].size,
token_list[n].value);
} }
#endif #endif
} }
...@@ -709,7 +727,7 @@ static int type_index_compare(const void *_a, const void *_b) ...@@ -709,7 +727,7 @@ static int type_index_compare(const void *_a, const void *_b)
if ((*a)->name->size != (*b)->name->size) if ((*a)->name->size != (*b)->name->size)
return (*a)->name->size - (*b)->name->size; return (*a)->name->size - (*b)->name->size;
else else
return memcmp((*a)->name->value, (*b)->name->value, return memcmp((*a)->name->content, (*b)->name->content,
(*a)->name->size); (*a)->name->size);
} }
...@@ -722,7 +740,7 @@ static int type_finder(const void *_key, const void *_ti) ...@@ -722,7 +740,7 @@ static int type_finder(const void *_key, const void *_ti)
if (token->size != type->name->size) if (token->size != type->name->size)
return token->size - type->name->size; return token->size - type->name->size;
else else
return memcmp(token->value, type->name->value, return memcmp(token->content, type->name->content,
token->size); token->size);
} }
...@@ -776,10 +794,7 @@ static void build_type_list(void) ...@@ -776,10 +794,7 @@ static void build_type_list(void)
#if 0 #if 0
for (n = 0; n < nr_types; n++) { for (n = 0; n < nr_types; n++) {
struct type *type = type_index[n]; struct type *type = type_index[n];
debug("- %*.*s\n", debug("- %*.*s\n", type->name->content);
(int)type->name->size,
(int)type->name->size,
type->name->value);
} }
#endif #endif
} }
...@@ -809,9 +824,8 @@ static void parse(void) ...@@ -809,9 +824,8 @@ static void parse(void)
type->element->type_def = type; type->element->type_def = type;
if (cursor != type[1].name) { if (cursor != type[1].name) {
fprintf(stderr, "%s:%d: Parse error at token '%*.*s'\n", fprintf(stderr, "%s:%d: Parse error at token '%s'\n",
filename, cursor->line, filename, cursor->line, cursor->content);
(int)cursor->size, (int)cursor->size, cursor->value);
exit(1); exit(1);
} }
...@@ -878,34 +892,31 @@ static struct element *parse_type(struct token **_cursor, struct token *end, ...@@ -878,34 +892,31 @@ static struct element *parse_type(struct token **_cursor, struct token *end,
cursor++; cursor++;
break; break;
default: default:
fprintf(stderr, "%s:%d: Unrecognised tag class token '%*.*s'\n", fprintf(stderr, "%s:%d: Unrecognised tag class token '%s'\n",
filename, cursor->line, filename, cursor->line, cursor->content);
(int)cursor->size, (int)cursor->size, cursor->value);
exit(1); exit(1);
} }
if (cursor >= end) if (cursor >= end)
goto overrun_error; goto overrun_error;
if (cursor->token_type != TOKEN_NUMBER) { if (cursor->token_type != TOKEN_NUMBER) {
fprintf(stderr, "%s:%d: Missing tag number '%*.*s'\n", fprintf(stderr, "%s:%d: Missing tag number '%s'\n",
filename, cursor->line, filename, cursor->line, cursor->content);
(int)cursor->size, (int)cursor->size, cursor->value);
exit(1); exit(1);
} }
element->tag &= ~0x1f; element->tag &= ~0x1f;
element->tag |= strtoul(cursor->value, &p, 10); element->tag |= strtoul(cursor->content, &p, 10);
element->flags |= ELEMENT_TAG_SPECIFIED; element->flags |= ELEMENT_TAG_SPECIFIED;
if (p - cursor->value != cursor->size) if (p - cursor->content != cursor->size)
abort(); abort();
cursor++; cursor++;
if (cursor >= end) if (cursor >= end)
goto overrun_error; goto overrun_error;
if (cursor->token_type != TOKEN_CLOSE_SQUARE) { if (cursor->token_type != TOKEN_CLOSE_SQUARE) {
fprintf(stderr, "%s:%d: Missing closing square bracket '%*.*s'\n", fprintf(stderr, "%s:%d: Missing closing square bracket '%s'\n",
filename, cursor->line, filename, cursor->line, cursor->content);
(int)cursor->size, (int)cursor->size, cursor->value);
exit(1); exit(1);
} }
cursor++; cursor++;
...@@ -1005,9 +1016,8 @@ static struct element *parse_type(struct token **_cursor, struct token *end, ...@@ -1005,9 +1016,8 @@ static struct element *parse_type(struct token **_cursor, struct token *end,
ref = bsearch(cursor, type_index, nr_types, sizeof(type_index[0]), ref = bsearch(cursor, type_index, nr_types, sizeof(type_index[0]),
type_finder); type_finder);
if (!ref) { if (!ref) {
fprintf(stderr, "%s:%d: Type '%*.*s' undefined\n", fprintf(stderr, "%s:%d: Type '%s' undefined\n",
filename, cursor->line, filename, cursor->line, cursor->content);
(int)cursor->size, (int)cursor->size, cursor->value);
exit(1); exit(1);
} }
cursor->type = *ref; cursor->type = *ref;
...@@ -1056,9 +1066,8 @@ static struct element *parse_type(struct token **_cursor, struct token *end, ...@@ -1056,9 +1066,8 @@ static struct element *parse_type(struct token **_cursor, struct token *end,
break; break;
default: default:
fprintf(stderr, "%s:%d: Token '%*.*s' does not introduce a type\n", fprintf(stderr, "%s:%d: Token '%s' does not introduce a type\n",
filename, cursor->line, filename, cursor->line, cursor->content);
(int)cursor->size, (int)cursor->size, cursor->value);
exit(1); exit(1);
} }
...@@ -1075,20 +1084,18 @@ static struct element *parse_type(struct token **_cursor, struct token *end, ...@@ -1075,20 +1084,18 @@ static struct element *parse_type(struct token **_cursor, struct token *end,
if (cursor >= end) if (cursor >= end)
goto overrun_error; goto overrun_error;
if (cursor->token_type != TOKEN_ELEMENT_NAME) { if (cursor->token_type != TOKEN_ELEMENT_NAME) {
fprintf(stderr, "%s:%d: Token '%*.*s' is not an action function name\n", fprintf(stderr, "%s:%d: Token '%s' is not an action function name\n",
filename, cursor->line, filename, cursor->line, cursor->content);
(int)cursor->size, (int)cursor->size, cursor->value);
exit(1); exit(1);
} }
action = malloc(sizeof(struct action) + cursor->size + 1); action = malloc(sizeof(struct action));
if (!action) { if (!action) {
perror(NULL); perror(NULL);
exit(1); exit(1);
} }
action->index = 0; action->index = 0;
memcpy(action->name, cursor->value, cursor->size); action->name = cursor->content;
action->name[cursor->size] = 0;
for (ppaction = &action_list; for (ppaction = &action_list;
*ppaction; *ppaction;
...@@ -1118,9 +1125,8 @@ static struct element *parse_type(struct token **_cursor, struct token *end, ...@@ -1118,9 +1125,8 @@ static struct element *parse_type(struct token **_cursor, struct token *end,
if (cursor >= end) if (cursor >= end)
goto overrun_error; goto overrun_error;
if (cursor->token_type != TOKEN_CLOSE_ACTION) { if (cursor->token_type != TOKEN_CLOSE_ACTION) {
fprintf(stderr, "%s:%d: Missing close action, got '%*.*s'\n", fprintf(stderr, "%s:%d: Missing close action, got '%s'\n",
filename, cursor->line, filename, cursor->line, cursor->content);
(int)cursor->size, (int)cursor->size, cursor->value);
exit(1); exit(1);
} }
cursor++; cursor++;
...@@ -1130,9 +1136,8 @@ static struct element *parse_type(struct token **_cursor, struct token *end, ...@@ -1130,9 +1136,8 @@ static struct element *parse_type(struct token **_cursor, struct token *end,
return top; return top;
parse_error: parse_error:
fprintf(stderr, "%s:%d: Unexpected token '%*.*s'\n", fprintf(stderr, "%s:%d: Unexpected token '%s'\n",
filename, cursor->line, filename, cursor->line, cursor->content);
(int)cursor->size, (int)cursor->size, cursor->value);
exit(1); exit(1);
overrun_error: overrun_error:
...@@ -1150,9 +1155,8 @@ static struct element *parse_compound(struct token **_cursor, struct token *end, ...@@ -1150,9 +1155,8 @@ static struct element *parse_compound(struct token **_cursor, struct token *end,
struct token *cursor = *_cursor, *name; struct token *cursor = *_cursor, *name;
if (cursor->token_type != TOKEN_OPEN_CURLY) { if (cursor->token_type != TOKEN_OPEN_CURLY) {
fprintf(stderr, "%s:%d: Expected compound to start with brace not '%*.*s'\n", fprintf(stderr, "%s:%d: Expected compound to start with brace not '%s'\n",
filename, cursor->line, filename, cursor->line, cursor->content);
(int)cursor->size, (int)cursor->size, cursor->value);
exit(1); exit(1);
} }
cursor++; cursor++;
...@@ -1193,9 +1197,8 @@ static struct element *parse_compound(struct token **_cursor, struct token *end, ...@@ -1193,9 +1197,8 @@ static struct element *parse_compound(struct token **_cursor, struct token *end,
children->flags &= ~ELEMENT_CONDITIONAL; children->flags &= ~ELEMENT_CONDITIONAL;
if (cursor->token_type != TOKEN_CLOSE_CURLY) { if (cursor->token_type != TOKEN_CLOSE_CURLY) {
fprintf(stderr, "%s:%d: Expected compound closure, got '%*.*s'\n", fprintf(stderr, "%s:%d: Expected compound closure, got '%s'\n",
filename, cursor->line, filename, cursor->line, cursor->content);
(int)cursor->size, (int)cursor->size, cursor->value);
exit(1); exit(1);
} }
cursor++; cursor++;
...@@ -1212,10 +1215,8 @@ static void dump_element(const struct element *e, int level) ...@@ -1212,10 +1215,8 @@ static void dump_element(const struct element *e, int level)
{ {
const struct element *c; const struct element *c;
const struct type *t = e->type_def; const struct type *t = e->type_def;
const char *name = e->name ? e->name->value : "."; const char *name = e->name ? e->name->content : ".";
int nsize = e->name ? e->name->size : 1; const char *tname = t && t->name ? t->name->content : ".";
const char *tname = t && t->name ? t->name->value : ".";
int tnsize = t && t->name ? t->name->size : 1;
char tag[32]; char tag[32];
if (e->class == 0 && e->method == 0 && e->tag == 0) if (e->class == 0 && e->method == 0 && e->tag == 0)
...@@ -1231,7 +1232,7 @@ static void dump_element(const struct element *e, int level) ...@@ -1231,7 +1232,7 @@ static void dump_element(const struct element *e, int level)
asn1_methods[e->method], asn1_methods[e->method],
e->tag); e->tag);
printf("%c%c%c%c%c %c %*s[*] \e[33m%s\e[m %*.*s %*.*s \e[35m%s\e[m\n", printf("%c%c%c%c%c %c %*s[*] \e[33m%s\e[m %s %s \e[35m%s\e[m\n",
e->flags & ELEMENT_IMPLICIT ? 'I' : '-', e->flags & ELEMENT_IMPLICIT ? 'I' : '-',
e->flags & ELEMENT_EXPLICIT ? 'E' : '-', e->flags & ELEMENT_EXPLICIT ? 'E' : '-',
e->flags & ELEMENT_TAG_SPECIFIED ? 'T' : '-', e->flags & ELEMENT_TAG_SPECIFIED ? 'T' : '-',
...@@ -1240,8 +1241,8 @@ static void dump_element(const struct element *e, int level) ...@@ -1240,8 +1241,8 @@ static void dump_element(const struct element *e, int level)
"-tTqQcaro"[e->compound], "-tTqQcaro"[e->compound],
level, "", level, "",
tag, tag,
tnsize, tnsize, tname, tname,
nsize, nsize, name, name,
e->action ? e->action->name : ""); e->action ? e->action->name : "");
if (e->compound == TYPE_REF) if (e->compound == TYPE_REF)
dump_element(e->type->type->element, level + 3); dump_element(e->type->type->element, level + 3);
...@@ -1454,9 +1455,7 @@ static void render_element(FILE *out, struct element *e, struct element *tag) ...@@ -1454,9 +1455,7 @@ static void render_element(FILE *out, struct element *e, struct element *tag)
outofline = 1; outofline = 1;
if (e->type_def && out) { if (e->type_def && out) {
render_more(out, "\t// %*.*s\n", render_more(out, "\t// %s\n", e->type_def->name->content);
(int)e->type_def->name->size, (int)e->type_def->name->size,
e->type_def->name->value);
} }
/* Render the operation */ /* Render the operation */
...@@ -1468,9 +1467,7 @@ static void render_element(FILE *out, struct element *e, struct element *tag) ...@@ -1468,9 +1467,7 @@ static void render_element(FILE *out, struct element *e, struct element *tag)
render_opcode(out, "ASN1_OP_%sMATCH_ANY%s%s,", render_opcode(out, "ASN1_OP_%sMATCH_ANY%s%s,",
cond, act, skippable ? "_OR_SKIP" : ""); cond, act, skippable ? "_OR_SKIP" : "");
if (e->name) if (e->name)
render_more(out, "\t\t// %*.*s", render_more(out, "\t\t// %s", e->name->content);
(int)e->name->size, (int)e->name->size,
e->name->value);
render_more(out, "\n"); render_more(out, "\n");
goto dont_render_tag; goto dont_render_tag;
...@@ -1503,9 +1500,7 @@ static void render_element(FILE *out, struct element *e, struct element *tag) ...@@ -1503,9 +1500,7 @@ static void render_element(FILE *out, struct element *e, struct element *tag)
x = tag ?: e; x = tag ?: e;
if (x->name) if (x->name)
render_more(out, "\t\t// %*.*s", render_more(out, "\t\t// %s", x->name->content);
(int)x->name->size, (int)x->name->size,
x->name->value);
render_more(out, "\n"); render_more(out, "\n");
/* Render the tag */ /* Render the tag */
...@@ -1543,10 +1538,8 @@ static void render_element(FILE *out, struct element *e, struct element *tag) ...@@ -1543,10 +1538,8 @@ static void render_element(FILE *out, struct element *e, struct element *tag)
* skipability */ * skipability */
render_opcode(out, "_jump_target(%u),", e->entry_index); render_opcode(out, "_jump_target(%u),", e->entry_index);
if (e->type_def && e->type_def->name) if (e->type_def && e->type_def->name)
render_more(out, "\t\t// --> %*.*s", render_more(out, "\t\t// --> %s",
(int)e->type_def->name->size, e->type_def->name->content);
(int)e->type_def->name->size,
e->type_def->name->value);
render_more(out, "\n"); render_more(out, "\n");
if (!(e->flags & ELEMENT_RENDERED)) { if (!(e->flags & ELEMENT_RENDERED)) {
e->flags |= ELEMENT_RENDERED; e->flags |= ELEMENT_RENDERED;
...@@ -1571,10 +1564,8 @@ static void render_element(FILE *out, struct element *e, struct element *tag) ...@@ -1571,10 +1564,8 @@ static void render_element(FILE *out, struct element *e, struct element *tag)
* skipability */ * skipability */
render_opcode(out, "_jump_target(%u),", e->entry_index); render_opcode(out, "_jump_target(%u),", e->entry_index);
if (e->type_def && e->type_def->name) if (e->type_def && e->type_def->name)
render_more(out, "\t\t// --> %*.*s", render_more(out, "\t\t// --> %s",
(int)e->type_def->name->size, e->type_def->name->content);
(int)e->type_def->name->size,
e->type_def->name->value);
render_more(out, "\n"); render_more(out, "\n");
if (!(e->flags & ELEMENT_RENDERED)) { if (!(e->flags & ELEMENT_RENDERED)) {
e->flags |= ELEMENT_RENDERED; e->flags |= ELEMENT_RENDERED;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册