Download YAZ User's Guide and Reference

Transcript
Chapter 9. The ODR Module
bool_t *other;
} u;
};
And your function could look like this:
int myChoice(ODR o, MyChoice **p, int optional, const char *name)
{
static Odr_arm arm[] =
{
{-1, -1, -1, MyChoice_untagged, odr_integer, "untagged"},
{ODR_IMPLICIT, ODR_CONTEXT, 99, MyChoice_tagged, odr_integer,
"tagged"},
{-1, -1, -1, MyChoice_other, odr_boolean, "other"},
{-1, -1, -1, -1, 0}
};
if (o->direction == ODR_DECODE)
*p = odr_malloc(o, sizeof(**p);
else if (!*p)
return optional && odr_ok(o);
if (odr_choice(o, arm, &(*p)->u, &(*p)->which), name)
return 1;
*p = 0;
return optional && odr_ok(o);
}
In some cases (say, a non-optional choice which is a member of a sequence), you can "embed" the union
and its discriminator in the structure belonging to the enclosing type, and you won’t need to fiddle with
memory allocation to create a separate structure to wrap the discriminator and union.
The corresponding function is somewhat nicer in the Sun XDR interface. Most of the complexity of this
interface comes from the possibility of declaring sequence elements (including CHOICEs) optional.
The ASN.1 specifications naturally requires that each member of a CHOICE have a distinct tag, so they
can be told apart on decoding. Sometimes it can be useful to define a CHOICE that has multiple types
that share the same tag. You’ll need some other mechanism, perhaps keyed to the context of the CHOICE
type. In effect, we would like to introduce a level of context-sensitiveness to our ASN.1 specification.
When encoding an internal representation, we have no problem, as long as each CHOICE member has a
distinct discriminator value. For decoding, we need a way to tell the choice function to look for a specific
arm of the table. The function
void odr_choice_bias(ODR o, int what);
provides this functionality. When called, it leaves a notice for the next call to odr_choice() to be
called on the decoding stream o that only the arm entry with a which field equal to what should be tried.
86