Skip to content

Commit

Permalink
Merge pull request #138 from v0-e/with-successors-support
Browse files Browse the repository at this point in the history
WITH SUCCESSORS/DESCENDANTS support
  • Loading branch information
mouse07410 authored Nov 12, 2023
2 parents 84d3a59 + 6d97ce9 commit 29ef59b
Show file tree
Hide file tree
Showing 12 changed files with 203 additions and 32 deletions.
2 changes: 1 addition & 1 deletion libasn1fix/asn1fix_export.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ asn1f_lookup_module_ex(asn1p_t *asn, const char *module_name,
arg.asn = asn;
arg.eh = a1f_replace_me_with_proper_interface_arg.eh;
arg.debug = a1f_replace_me_with_proper_interface_arg.debug;
return asn1f_lookup_module(&arg, module_name, oid);
return asn1f_lookup_module(&arg, module_name, oid, 0);
}

asn1p_expr_t *
Expand Down
41 changes: 23 additions & 18 deletions libasn1fix/asn1fix_retrieve.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ asn1f_lookup_in_imports(arg_t *arg, asn1p_module_t *mod, const char *name) {
*/
TQ_FOR(xp, &(mod->imports), xp_next) {
asn1p_module_t *fromModule =
asn1f_lookup_module(arg, xp->fromModuleName, NULL);
asn1f_lookup_module(arg, xp->fromModuleName, NULL, 0);
asn1p_expr_t *tc = (asn1p_expr_t *)0;

TQ_FOR(tc, &(xp->xp_members), next) {
Expand All @@ -78,7 +78,7 @@ asn1f_lookup_in_imports(arg_t *arg, asn1p_module_t *mod, const char *name) {
* Okay, right now we have a module name and, hopefully, an OID.
* Search the arg->asn for the specified module.
*/
mod = asn1f_lookup_module(arg, xp->fromModuleName, xp->identifier.oid);
mod = asn1f_lookup_module(arg, xp->fromModuleName, xp->identifier.oid, xp->option);
if(mod == NULL) {
/* Conditional debug */
if(!(arg->expr->_mark & TM_BROKEN)) {
Expand Down Expand Up @@ -106,8 +106,8 @@ asn1f_lookup_in_imports(arg_t *arg, asn1p_module_t *mod, const char *name) {
}

asn1p_module_t *
asn1f_lookup_module(arg_t *arg, const char *module_name, const asn1p_oid_t *oid) {
asn1p_module_t *mod;
asn1f_lookup_module(arg_t *arg, const char *module_name, const asn1p_oid_t *oid, int oid_option) {
asn1p_module_t *mod, *ret = NULL;

assert(module_name);

Expand Down Expand Up @@ -149,27 +149,32 @@ asn1f_lookup_module(arg_t *arg, const char *module_name, const asn1p_oid_t *oid)
TQ_FOR(mod, &(arg->asn->modules), mod_next) {
if(oid) {
if(mod->module_oid) {
if(asn1p_oid_compare(oid,
mod->module_oid)) {
continue;
} else {
int r = asn1p_oid_compare(oid, mod->module_oid);
if(oid_option == XPT_WITH_SUCCESSORS) {
if(r == oid->arcs_count && r == mod->module_oid->arcs_count) /* positive and last arc */
r = 0;
} else if(oid_option == XPT_WITH_DESCENDANTS) {
if(oid->arcs_count == (-1 - r))
r = 0;
}
if(0 == r) {
/* Match! Even if name doesn't. */
return mod;
oid = mod->module_oid;
ret = mod;
}
} else {
/* Not match, even if name is the same. */
continue;
}
/* Not match, even if name is the same. */
continue;
}

if(strcmp(module_name, mod->ModuleName) == 0)
return mod;
}

DEBUG("\tModule \"%s\" not found", module_name);

errno = ENOENT;
return NULL;
if(ret == NULL) {
DEBUG("\tModule \"%s\" not found", module_name);
errno = ENOENT;
}
return ret;
}

static asn1p_expr_t *
Expand Down Expand Up @@ -268,7 +273,7 @@ asn1f_lookup_symbol_impl(arg_t *arg, asn1p_expr_t *rhs_pspecs, const asn1p_ref_t
* switch namespace to that module.
*/
if(modulename) {
imports_from = asn1f_lookup_module(arg, modulename, 0);
imports_from = asn1f_lookup_module(arg, modulename, 0, 0);
if(imports_from == NULL) {
FATAL(
"Module \"%s\" "
Expand Down
2 changes: 1 addition & 1 deletion libasn1fix/asn1fix_retrieve.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ asn1p_module_t *asn1f_lookup_in_imports(arg_t *arg, asn1p_module_t *mod, const c
*/
asn1p_module_t *asn1f_lookup_module(arg_t *arg,
const char *module_name,
const asn1p_oid_t *module_oid);
const asn1p_oid_t *module_oid, int oid_option);

/*
* Return the reference to a destination of the given reference,
Expand Down
3 changes: 2 additions & 1 deletion libasn1parser/asn1p_l.l
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,8 @@ UTF8String {
VideotexString return TOK_VideotexString;
VisibleString return TOK_VisibleString;
WITH return TOK_WITH;

SUCCESSORS return TOK_SUCCESSORS;
DESCENDANTS return TOK_DESCENDANTS;

<INITIAL,with_syntax>&[A-Z][A-Za-z0-9]*([-][A-Za-z0-9]+)* {
asn1p_lval.tv_str = strdup(yytext);
Expand Down
15 changes: 6 additions & 9 deletions libasn1parser/asn1p_oid.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,26 +89,23 @@ asn1p_oid_compare(const asn1p_oid_t *a, const asn1p_oid_t *b) {

if(b->arcs_count > i) {
if(a->arcs_count <= i)
return -1;
return -1-i;
} else if(a->arcs_count > i) {
if(b->arcs_count <= i)
return 1;
return 1+i;
} else if(b->arcs_count <= i && a->arcs_count <= i) {
cmp = b->arcs_count - a->arcs_count;
if(cmp < 0)
return -1;
return -1-i;
else if(cmp > 0)
return 1;
return 1+i;
return 0;
}

cmp = b->arcs[i].number - a->arcs[i].number;
if(cmp < 0)
return -1;
return -1-i;
else if(cmp > 0)
return 1;
return 1+i;
}

}


1 change: 0 additions & 1 deletion libasn1parser/asn1p_oid.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,4 @@ void asn1p_oid_free(asn1p_oid_t *);
*/
int asn1p_oid_compare(const asn1p_oid_t *a, const asn1p_oid_t *b);


#endif /* ASN1_PARSER_OID_H */
5 changes: 5 additions & 0 deletions libasn1parser/asn1p_xports.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ typedef struct asn1p_xports_s {
asn1p_value_t *value; /* DefinedValue */
} identifier;

enum asn1p_import_option {
XPT_WITH_SUCCESSORS = 1,
XPT_WITH_DESCENDANTS
} option;

/*
* Number of entities to import.
*/
Expand Down
22 changes: 21 additions & 1 deletion libasn1parser/asn1p_y.y
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,8 @@ static asn1p_module_t *currentModule;
%token TOK_TwoDots ".."
%token TOK_ThreeDots "..."

%token TOK_SUCCESSORS
%token TOK_DESCENDANTS

/*
* Types defined herein.
Expand All @@ -286,6 +288,8 @@ static asn1p_module_t *currentModule;
%type <a_module> optImportsBundleSet
%type <a_module> ImportsBundleSet
%type <a_xports> ImportsBundle
%type <a_xports> ImportsBundleInt
%type <a_int> ImportSelectionOption
%type <a_xports> ImportsList
%type <a_xports> ExportsDefinition
%type <a_xports> ExportsBody
Expand Down Expand Up @@ -711,11 +715,19 @@ AssignedIdentifier:
| ObjectIdentifier { $$.oid = $1; };
/* | DefinedValue { $$.value = $1; }; // Handled through saved_aid */

ImportsBundle:
ImportsBundle:
ImportsBundleInt ImportSelectionOption {
$$ = $1;
$$->option = $2;
}
| ImportsBundleInt ;

ImportsBundleInt:
ImportsList TOK_FROM TypeRefName AssignedIdentifier {
$$ = $1;
$$->fromModuleName = $3;
$$->identifier = $4;
$$->option = 0;
/* This stupid thing is used for look-back hack. */
saved_aid = $$->identifier.oid ? 0 : &($$->identifier);
checkmem($$);
Expand Down Expand Up @@ -755,6 +767,14 @@ ImportsElement:
}
;

ImportSelectionOption:
TOK_WITH TOK_SUCCESSORS {
$$ = XPT_WITH_SUCCESSORS;
}
| TOK_WITH TOK_DESCENDANTS {
$$ = XPT_WITH_DESCENDANTS;
}
;

optExports:
{ $$ = 0; }
Expand Down
36 changes: 36 additions & 0 deletions tests/tests-asn1c-compiler/161-imports-with-successors-OK.asn1
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

-- OK: Everything is fine

-- iso.org.dod.internet.private.enterprise (1.3.6.1.4.1)
-- .spelio.software.asn1c.test (9363.1.5.1)
-- .121

ModuleIMPORTS
{ iso org(3) dod(6) internet (1) private(4) enterprise(1)
spelio(9363) software(1) asn1c(5) test(1) 161 }
DEFINITIONS AUTOMATIC TAGS ::=
BEGIN

IMPORTS ImportedType
FROM ImportedModule1
{ iso org(3) dod(6) internet (1) private(4) enterprise(1)
spelio(9363) software(1) asn1c(5) test(1) 161 minor-version-1(1) }
WITH SUCCESSORS
;

Type ::= ImportedType

END


ImportedModule1
{ iso org(3) dod(6) internet (1) private(4) enterprise(1)
spelio(9363) software(1) asn1c(5) test(1) 161 minor-version-5(5)}
DEFINITIONS AUTOMATIC TAGS ::=
BEGIN

IMPORTS -- nothing --;

ImportedType ::= INTEGER

END
36 changes: 36 additions & 0 deletions tests/tests-asn1c-compiler/161-imports-with-successors-SE.asn1
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

-- OK: Everything is fine

-- iso.org.dod.internet.private.enterprise (1.3.6.1.4.1)
-- .spelio.software.asn1c.test (9363.1.5.1)
-- .121

ModuleIMPORTS
{ iso org(3) dod(6) internet (1) private(4) enterprise(1)
spelio(9363) software(1) asn1c(5) test(1) 161 }
DEFINITIONS AUTOMATIC TAGS ::=
BEGIN

IMPORTS ImportedType
FROM ImportedModule1
{ iso org(3) dod(6) internet (1) private(4) enterprise(1)
spelio(9363) software(1) asn1c(5) test(1) 161 minor-version-3(3) }
WITH SUCCESSORS
;

Type ::= ImportedType

END


ImportedModule1
{ iso org(3) dod(6) internet (1) private(4) enterprise(1)
spelio(9363) software(1) asn1c(5) test(1) 161 minor-version-2(2)}
DEFINITIONS AUTOMATIC TAGS ::=
BEGIN

IMPORTS -- nothing --;

ImportedType ::= INTEGER

END
36 changes: 36 additions & 0 deletions tests/tests-asn1c-compiler/162-imports-with-successors-SE.asn1
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

-- OK: Everything is fine

-- iso.org.dod.internet.private.enterprise (1.3.6.1.4.1)
-- .spelio.software.asn1c.test (9363.1.5.1)
-- .121

ModuleIMPORTS
{ iso org(3) dod(6) internet (1) private(4) enterprise(1)
spelio(9363) software(1) asn1c(5) test(1) 161 }
DEFINITIONS AUTOMATIC TAGS ::=
BEGIN

IMPORTS ImportedType
FROM ImportedModule1
{ iso org(3) dod(6) internet (1) private(4) enterprise(1)
spelio(9363) software(1) asn1c(5) test(1) 161 minor-version-3(3) }
WITH SUCCESSORS
;

Type ::= ImportedType

END


ImportedModule1
{ iso org(3) dod(6) internet (1) private(4) enterprise(1)
spelio(9363) software(1) asn1c(5) test(1) 161 minor-version-3(3) super-minor-version(1)}
DEFINITIONS AUTOMATIC TAGS ::=
BEGIN

IMPORTS -- nothing --;

ImportedType ::= INTEGER

END
36 changes: 36 additions & 0 deletions tests/tests-asn1c-compiler/163-imports-with-descendants-OK.asn1
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

-- OK: Everything is fine

-- iso.org.dod.internet.private.enterprise (1.3.6.1.4.1)
-- .spelio.software.asn1c.test (9363.1.5.1)
-- .121

ModuleIMPORTS
{ iso org(3) dod(6) internet (1) private(4) enterprise(1)
spelio(9363) software(1) asn1c(5) test(1) 161 }
DEFINITIONS AUTOMATIC TAGS ::=
BEGIN

IMPORTS ImportedType
FROM ImportedModule1
{ iso org(3) dod(6) internet (1) private(4) enterprise(1)
spelio(9363) software(1) asn1c(5) test(1) 163 major-version-1(1) }
WITH DESCENDANTS
;

Type ::= ImportedType

END


ImportedModule1
{ iso org(3) dod(6) internet (1) private(4) enterprise(1)
spelio(9363) software(1) asn1c(5) test(1) 163 major-version-1(1) super-minor-version(1) super-minor-version(1)}
DEFINITIONS AUTOMATIC TAGS ::=
BEGIN

IMPORTS -- nothing --;

ImportedType ::= INTEGER

END

0 comments on commit 29ef59b

Please sign in to comment.