Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
Changes since v1.5.3:
* Fix external-arena frame aliasing (issue #15): arena frames are now keyed
by (structure address, type specifics) instead of the address alone, so an
inline member at offset 0 (single-member SEQUENCE wrapper) no longer
collides with its parent's frame. asn_ext_ctx_get() and
asn_get_struct_ctx() take an extra type_key argument (the specifics
pointer); all 21 decoder fetch sites pass their specs. Regression covered
by the Wrap type in the 610 module, check-ext-ctx.c and check-ext-ctx.sh.
* Onboard converter-example onto the external decode context (issue #15):
it now decodes through asn_decode_ctx()/uper_decode_ctx()/
aper_decode_ctx() with a file-scope asn_struct_external_ctx_t arena and
frees via ASN_STRUCT_FREE_CTX, so the generated converter works
unchanged with both default and -fno-ctx-members code (chunked RC_WMORE
input included). Covered by tests-asn1c-smoke/check-ext-ctx.sh; arena
helpers unit-tested in tests-skeletons/check-ext-ctx.c. README and the
asn1c man page document the feature for end users.
* Add per-codec external-context decode entry points (issue #15, Phases
1-4): xer_decode_ctx(), jer_decode_ctx(), oer_decode_ctx(),
uper_decode_ctx(), aper_decode_ctx(), cbor_decode_ctx() complement
Expand Down
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,42 @@ the error occurred:

For more details, see [PARTIAL_DECODING.md](PARTIAL_DECODING.md).

## External decode context (`-fno-ctx-members`)

By default, every generated constructed type embeds a mutable parser-state
member (`asn_struct_ctx_t _asn_ctx;`), which adds 24 bytes per nesting level to
each structure. For memory-constrained targets, `asn1c -fno-ctx-members`
omits that member from the generated structures (the type specifics carry an
`ASN_CTX_OFFSET_EXTERNAL` sentinel instead), shrinking e.g. a typical message
struct from 104 to 32 bytes.

Parsing state then lives in a caller-owned arena instead:

```c
asn_struct_external_ctx_t ext_ctx = {0}; /* zero-initialize once */
NetMsg_t *msg = NULL;
asn_dec_rval_t dr = ber_decode_ctx(0, &asn_DEF_NetMsg, (void **)&msg,
&ext_ctx, data, size);
```

Reuse the same arena across `RC_WMORE` resumes (chunked input), and call
`asn_struct_external_ctx_reset(&ext_ctx)` (or `ASN_STRUCT_RESET_CTX` /
`ASN_STRUCT_FREE_CTX`) before decoding fresh data into the same structures.
Every codec has a sibling entry point carrying the arena:
`ber_decode_ctx`, `xer_decode_ctx`, `jer_decode_ctx`, `oer_decode_ctx`,
`uper_decode_ctx`, `aper_decode_ctx`, `cbor_decode_ctx`, and the generic
`asn_decode_ctx` dispatcher. The arena is type-agnostic (8 inline frames plus
heap overflow), so one arena serves all types of a decode operation.

The generated `converter-example` works unchanged in both modes. Note that
plain entry points (`ber_decode()` etc.) cannot resume `-fno-ctx-members`
output for the resumable codecs (BER/CER, XER, JER, OER) and fail with
`RC_FAIL`; use the `_ctx` variants with `-fno-ctx-members` code.

For the design and rationale, see
[docs/external-ctx-design.md](docs/external-ctx-design.md) and the
`-fno-ctx-members` entry in the [asn1c man page](doc/man/asn1c.man.md).

--
Mouse and Lev Walkin
<!-- markdownlint-disable MD033 -->
Expand Down
17 changes: 12 additions & 5 deletions doc/man/asn1c.man.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,18 @@ cargo check --offline --manifest-path generated/Cargo.toml
-fno-ctx-members
: Do not embed the `asn_struct_ctx_t _asn_ctx` member into generated
structures; emit the `ASN_CTX_OFFSET_EXTERNAL` sentinel in the type
specifics instead. Decode such code through `ber_decode_ctx()` /
`asn_decode_ctx()`, passing a caller-owned `asn_struct_external_ctx_t`
arena (zero-initialized once, reused across `RC_WMORE` resumes, reset
with `asn_struct_external_ctx_reset()`); see `constr_TYPE.h`.
Plain `ber_decode()` cannot resume `-fno-ctx-members` output.
specifics instead. Decode such code through the `*_decode_ctx()`
entry points (`ber_decode_ctx()`, `xer_decode_ctx()`, `jer_decode_ctx()`,
`oer_decode_ctx()`, `uper_decode_ctx()`, `aper_decode_ctx()`,
`cbor_decode_ctx()`, or the generic `asn_decode_ctx()` dispatcher),
passing a caller-owned `asn_struct_external_ctx_t` arena
(zero-initialized once, reused across `RC_WMORE` resumes, reset
with `asn_struct_external_ctx_reset()` or `ASN_STRUCT_FREE_CTX` /
`ASN_STRUCT_RESET_CTX`); see `constr_TYPE.h`. The generated
`converter-example` already uses these entry points and works in both
modes. Plain `ber_decode()` & co. cannot resume `-fno-ctx-members`
output for the resumable codecs (BER/CER, XER, JER, OER) and fail with
`RC_FAIL`; UPER/APER/CBOR plain entry points are unaffected.
Design: `docs/external-ctx-design.md` (issue #15).

-fprefer-import-source
Expand Down
32 changes: 26 additions & 6 deletions docs/external-ctx-design.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,19 @@ affecting generated types only.

Defined in `skeletons/constr_TYPE.h` next to `asn_struct_ctx_t`:

- `asn_struct_ctx_frame_t { void *st; asn_struct_ctx_t ctx; }` — one frame per
struct instance currently being parsed; keyed by the instance address.
- `asn_struct_ctx_frame_t { void *st; const void *type_key; asn_struct_ctx_t ctx; }`
— one frame per struct instance currently being parsed; keyed by the
(instance address, type specifics) pair. The type key is required because an
inline member at offset 0 (e.g. a single-member SEQUENCE wrapper) makes the
child structure share the parent's base address; address-only keying aliased
the two frames and broke EXPLICIT-tag decoding of such wrappers (found
2026-11-27 via converter-example on a `SEQUENCE { x [1] EXPLICIT ... }`
fixture; fixed by keying on the specifics pointer, which is unique per type).
- `asn_struct_external_ctx_t` — the arena, one per top-level decode operation:
an inline array of 8 frames (`ASN_EXT_CTX_INLINE_FRAMES = 8`) plus an
optional heap overflow, so the common case needs no allocation.
- Helpers (type-agnostic, no vtable change):
`asn_ext_ctx_get(arena, st)` (find-or-create a zeroed frame),
`asn_ext_ctx_get(arena, st, type_key)` (find-or-create a zeroed frame),
`asn_struct_external_ctx_reset(arena)` (free every frame's `ctx->ptr`
scratch, free the heap overflow, zero the arena),
`asn_get_struct_ctx(opt_codec_ctx, st, ctx_offset)` (dual-path fetch used by
Expand Down Expand Up @@ -189,9 +195,13 @@ symmetry and pass the arena through `asn_codec_ctx_t` unchanged.
regression harness.
2. Phase 1 — XER, JER. Phase 2 — OER. Phase 3 — UPER/APER (SET/SET-OF sites).
Phase 4 — CBOR.
3. `converter-example.c` / PDU-collection helpers stay on the plain entry
points until **all** codecs have `_ctx` versions — deliberately deferred to
avoid tracking partial onboarding (decision recorded 2026-11-26).
3. `converter-example.c` (and thereby the `-pdu=...` PDU-collection mode)
now decodes through `asn_decode_ctx` / `uper_decode_ctx` / `aper_decode_ctx`
with a file-scope arena and frees via `ASN_STRUCT_FREE_CTX`, so it works
unchanged with both default and `-fno-ctx-members` generated code (the arena
takes precedence over embedded members when present). Previously deferred
until all codecs had `_ctx` versions (decision recorded 2026-11-26, lifted
2026-11-27); covered by `tests/tests-asn1c-smoke/check-ext-ctx.sh`.

Each phase lands with tests in `tests/tests-c-compiler`, expected-output tests
in `tests/tests-asn1c-compiler` for `-fno-ctx-members` (`-P` output contains
Expand All @@ -218,6 +228,16 @@ pass the existing suites.
- 2026-11-26: converter-example/PDU helpers deferred until all decoders are
onboarded.
- 2026-11-26: sentinel value `(unsigned)-1` for `ctx_offset`.
- 2026-11-27: arena frames keyed by (struct address, type specifics), not
address alone — inline members at offset 0 alias the parent's address
(regression found by check-BER-retag's `Nested` fixture through the
onboarded converter-example; locked by `Wrap` in the 610 module, the
check-ext-ctx unit test, and the check-ext-ctx smoke test).
- 2026-11-27: `converter-example.c` onboarded onto the `_ctx` entry points
with a file-scope arena; the arena-wins-when-present fetch rule makes the
same binary correct for both codegen modes. Arena helpers unit-tested in
`tests/tests-skeletons/check-ext-ctx.c` (inline capacity, heap overflow,
reset, dual-path fetch).
- 2026-11-26: Phases 1-4 landed as one change: `xer_decode_ctx`,
`jer_decode_ctx`, `oer_decode_ctx`, `uper_decode_ctx`, `aper_decode_ctx`,
`cbor_decode_ctx` all follow the `ber_decode_ctx` pattern (always stack-copy
Expand Down
2 changes: 1 addition & 1 deletion skeletons/OCTET_STRING_ber.c
Original file line number Diff line number Diff line change
Expand Up @@ -1079,7 +1079,7 @@ OCTET_STRING_decode_ber(const asn_codec_ctx_t *opt_codec_ctx,
state.st = (BIT_STRING_t *)(*sptr = CALLOC(1, specs->struct_size));
if(!state.st) return octet_string_ber_result(&state, RC_FAIL);
}
state.ctx = asn_get_struct_ctx(opt_codec_ctx, state.st, specs->ctx_offset);
state.ctx = asn_get_struct_ctx(opt_codec_ctx, state.st, specs->ctx_offset, specs);
if(!state.ctx) return octet_string_ber_result(&state, RC_FAIL);
return octet_string_ber_run(&state, opt_codec_ctx);
}
Expand Down
2 changes: 1 addition & 1 deletion skeletons/OCTET_STRING_jer.c
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ OCTET_STRING__decode_jer(
}

/* Restore parsing context */
ctx = asn_get_struct_ctx(opt_codec_ctx, *sptr, specs->ctx_offset);
ctx = asn_get_struct_ctx(opt_codec_ctx, *sptr, specs->ctx_offset, specs);
if(!ctx) { asn_dec_rval_t _fr = {RC_FAIL, 0}; return _fr; }

return jer_decode_general(opt_codec_ctx, ctx, *sptr,
Expand Down
2 changes: 1 addition & 1 deletion skeletons/OCTET_STRING_xer.c
Original file line number Diff line number Diff line change
Expand Up @@ -1136,7 +1136,7 @@ OCTET_STRING__decode_xer(
}

/* Restore parsing context */
ctx = asn_get_struct_ctx(opt_codec_ctx, *sptr, specs->ctx_offset);
ctx = asn_get_struct_ctx(opt_codec_ctx, *sptr, specs->ctx_offset, specs);
if(!ctx) { asn_dec_rval_t _fr = {RC_FAIL, 0}; return _fr; }

return xer_decode_general(opt_codec_ctx, ctx, *sptr, xml_tag,
Expand Down
2 changes: 1 addition & 1 deletion skeletons/constr_CHOICE_ber.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ CHOICE_decode_ber(const asn_codec_ctx_t *opt_codec_ctx,
/*
* Restore parsing context.
*/
ctx = asn_get_struct_ctx(opt_codec_ctx, st, specs->ctx_offset);
ctx = asn_get_struct_ctx(opt_codec_ctx, st, specs->ctx_offset, specs);
if(!ctx) RETURN(RC_FAIL);

/* Check recursion depth to prevent stack overflow */
Expand Down
2 changes: 1 addition & 1 deletion skeletons/constr_CHOICE_jer.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ CHOICE_decode_jer(const asn_codec_ctx_t *opt_codec_ctx,
/*
* Restore parsing context.
*/
ctx = asn_get_struct_ctx(opt_codec_ctx, st, specs->ctx_offset);
ctx = asn_get_struct_ctx(opt_codec_ctx, st, specs->ctx_offset, specs);
if(!ctx) RETURN(RC_FAIL);

/* Check recursion depth to prevent stack overflow */
Expand Down
2 changes: 1 addition & 1 deletion skeletons/constr_CHOICE_oer.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ CHOICE_decode_oer(const asn_codec_ctx_t *opt_codec_ctx,
/*
* Restore parsing context.
*/
ctx = asn_get_struct_ctx(opt_codec_ctx, st, specs->ctx_offset);
ctx = asn_get_struct_ctx(opt_codec_ctx, st, specs->ctx_offset, specs);
if(!ctx) RETURN(RC_FAIL);

/* Check recursion depth to prevent stack overflow */
Expand Down
2 changes: 1 addition & 1 deletion skeletons/constr_CHOICE_xer.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ CHOICE_decode_xer(const asn_codec_ctx_t *opt_codec_ctx,
/*
* Restore parsing context.
*/
ctx = asn_get_struct_ctx(opt_codec_ctx, st, specs->ctx_offset);
ctx = asn_get_struct_ctx(opt_codec_ctx, st, specs->ctx_offset, specs);
if(!ctx) RETURN(RC_FAIL);

/* Check recursion depth to prevent stack overflow */
Expand Down
4 changes: 2 additions & 2 deletions skeletons/constr_SEQUENCE_ber.c
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ _sequence_decode_group_ber(const asn_codec_ctx_t *codec_ctx,
st = *struct_ptr = CALLOC(1, specs->struct_size);
if(st == 0) GROUP_RETURN(RC_FAIL);
}
ctx = asn_get_struct_ctx(codec_ctx, st, specs->ctx_offset);
ctx = asn_get_struct_ctx(codec_ctx, st, specs->ctx_offset, specs);
if(!ctx) GROUP_RETURN(RC_FAIL);
if(ASN__STACK_OVERFLOW_CHECK(codec_ctx)) GROUP_RETURN(RC_FAIL);
if(ctx->phase == 0) {
Expand Down Expand Up @@ -449,7 +449,7 @@ SEQUENCE_decode_ber(const asn_codec_ctx_t *opt_codec_ctx,
/*
* Restore parsing context.
*/
ctx = asn_get_struct_ctx(opt_codec_ctx, st, specs->ctx_offset);
ctx = asn_get_struct_ctx(opt_codec_ctx, st, specs->ctx_offset, specs);
if(!ctx) RETURN(RC_FAIL);

/*
Expand Down
4 changes: 2 additions & 2 deletions skeletons/constr_SEQUENCE_jer.c
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ SEQUENCE_decode_jer_flat(const asn_codec_ctx_t *codec,
st = *struct_ptr = CALLOC(1, specs->struct_size);
if(!st) FLAT_RETURN(RC_FAIL);
}
ctx = asn_get_struct_ctx(codec, st, specs->ctx_offset);
ctx = asn_get_struct_ctx(codec, st, specs->ctx_offset, specs);
if(!ctx) FLAT_RETURN(RC_FAIL);
if(ASN__STACK_OVERFLOW_CHECK(codec)) FLAT_RETURN(RC_FAIL);
if(ctx->step < 0 || (size_t)ctx->step > count) FLAT_RETURN(RC_FAIL);
Expand Down Expand Up @@ -810,7 +810,7 @@ SEQUENCE_decode_jer(const asn_codec_ctx_t *opt_codec_ctx,
/*
* Restore parsing context.
*/
ctx = asn_get_struct_ctx(opt_codec_ctx, st, specs->ctx_offset);
ctx = asn_get_struct_ctx(opt_codec_ctx, st, specs->ctx_offset, specs);
if(!ctx) RETURN(RC_FAIL);

/* Check recursion depth to prevent stack overflow */
Expand Down
2 changes: 1 addition & 1 deletion skeletons/constr_SEQUENCE_oer.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ SEQUENCE_decode_oer(const asn_codec_ctx_t *opt_codec_ctx,
/*
* Restore parsing context.
*/
ctx = asn_get_struct_ctx(opt_codec_ctx, st, specs->ctx_offset);
ctx = asn_get_struct_ctx(opt_codec_ctx, st, specs->ctx_offset, specs);
if(!ctx) RETURN(RC_FAIL);

/*
Expand Down
2 changes: 1 addition & 1 deletion skeletons/constr_SEQUENCE_xer.c
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ SEQUENCE_decode_xer(const asn_codec_ctx_t *opt_codec_ctx,
/*
* Restore parsing context.
*/
ctx = asn_get_struct_ctx(opt_codec_ctx, st, specs->ctx_offset);
ctx = asn_get_struct_ctx(opt_codec_ctx, st, specs->ctx_offset, specs);
if(!ctx) RETURN(RC_FAIL);

/* Check recursion depth to prevent stack overflow */
Expand Down
2 changes: 1 addition & 1 deletion skeletons/constr_SET_OF_ber.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ SET_OF_decode_ber(const asn_codec_ctx_t *opt_codec_ctx,
/*
* Restore parsing context.
*/
ctx = asn_get_struct_ctx(opt_codec_ctx, st, specs->ctx_offset);
ctx = asn_get_struct_ctx(opt_codec_ctx, st, specs->ctx_offset, specs);
if(!ctx) RETURN(RC_FAIL);

/* Check recursion depth to prevent stack overflow */
Expand Down
2 changes: 1 addition & 1 deletion skeletons/constr_SET_OF_jer.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ SET_OF_decode_jer(const asn_codec_ctx_t *opt_codec_ctx,
/*
* Restore parsing context.
*/
ctx = asn_get_struct_ctx(opt_codec_ctx, st, specs->ctx_offset);
ctx = asn_get_struct_ctx(opt_codec_ctx, st, specs->ctx_offset, specs);
if(!ctx) RETURN(RC_FAIL);

/* Check recursion depth to prevent stack overflow */
Expand Down
2 changes: 1 addition & 1 deletion skeletons/constr_SET_OF_oer.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ SET_OF_decode_oer(const asn_codec_ctx_t *opt_codec_ctx,
/*
* Restore parsing context.
*/
ctx = asn_get_struct_ctx(opt_codec_ctx, st, specs->ctx_offset);
ctx = asn_get_struct_ctx(opt_codec_ctx, st, specs->ctx_offset, specs);
if(!ctx) RETURN(RC_FAIL);

/*
Expand Down
2 changes: 1 addition & 1 deletion skeletons/constr_SET_OF_xer.c
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ SET_OF_decode_xer(const asn_codec_ctx_t *opt_codec_ctx,
/*
* Restore parsing context.
*/
ctx = asn_get_struct_ctx(opt_codec_ctx, st, specs->ctx_offset);
ctx = asn_get_struct_ctx(opt_codec_ctx, st, specs->ctx_offset, specs);
if(!ctx) RETURN(RC_FAIL);

/* Check recursion depth to prevent stack overflow */
Expand Down
4 changes: 2 additions & 2 deletions skeletons/constr_SET_ber.c
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,7 @@ _SET_ber_prepare(const asn_codec_ctx_t *codec,
*st = *struct_ptr = CALLOC(1, (*specs)->struct_size);
if(!*st) return -1;
}
*ctx = asn_get_struct_ctx(codec, *st, (*specs)->ctx_offset);
*ctx = asn_get_struct_ctx(codec, *st, (*specs)->ctx_offset, *specs);
if(!*ctx) return -1;
return 0;
}
Expand Down Expand Up @@ -968,7 +968,7 @@ SET_decode_ber(const asn_codec_ctx_t *codec, const asn_TYPE_descriptor_t *td,
&& ASN_SET_ISPRESENT2((char *)*target + specs->pres_offset, i)
&& _SET_ber_open_dependencies(td, *target)) { result.code = RC_FAIL; return result; }
}
ctx = asn_get_struct_ctx(codec, *target, specs->ctx_offset);
ctx = asn_get_struct_ctx(codec, *target, specs->ctx_offset, specs);
if(!ctx) { result.code = RC_FAIL; return result; }
memset(ctx, 0, sizeof(*ctx));
result = _SET_decode_ber_pass(codec, td, target, input, size, tag_mode, 2);
Expand Down
2 changes: 1 addition & 1 deletion skeletons/constr_SET_jer.c
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ SET_decode_jer(const asn_codec_ctx_t *codec, const asn_TYPE_descriptor_t *td,
st = *struct_ptr;
if(!st) { st = *struct_ptr = CALLOC(1, specs->struct_size); if(!st) return result; }
if(SET_jer_preflight(td, st)) return result;
ctx = asn_get_struct_ctx(codec, st, specs->ctx_offset);
ctx = asn_get_struct_ctx(codec, st, specs->ctx_offset, specs);
if(!ctx) return result;
if(ASN__STACK_OVERFLOW_CHECK(codec)) return result;
if(has_open) return SET_jer_decode_open_object(codec, td, st, ctx, ptr, (size_t)complete);
Expand Down
2 changes: 1 addition & 1 deletion skeletons/constr_SET_oer.c
Original file line number Diff line number Diff line change
Expand Up @@ -753,7 +753,7 @@ SET_oer_prepare_decode(const asn_codec_ctx_t *codec,
if(!st) return -1;
}
if(SET_oer_members_valid(td, st)) return -1;
ctx = asn_get_struct_ctx(codec, st, specs->ctx_offset);
ctx = asn_get_struct_ctx(codec, st, specs->ctx_offset, specs);
if(!ctx) return -1;
if(ctx->phase == SET_OER_PHASE_COMPLETE) return -1;
if(ctx->phase != 0 && ctx->phase != SET_OER_PHASE_INITIAL
Expand Down
2 changes: 1 addition & 1 deletion skeletons/constr_SET_xer.c
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ _SET_xer_prepare(const asn_codec_ctx_t *codec,
*st = *struct_ptr = CALLOC(1, (*specs)->struct_size);
if(!*st) return -1;
}
*ctx = asn_get_struct_ctx(codec, *st, (*specs)->ctx_offset);
*ctx = asn_get_struct_ctx(codec, *st, (*specs)->ctx_offset, *specs);
if(!*ctx) return -1;
return 0;
}
Expand Down
17 changes: 12 additions & 5 deletions skeletons/constr_TYPE.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,23 @@ int get_asn1c_environment_version(void) { return ASN1C_ENVIRONMENT_VERSION; }
* decode operation; frames are keyed by the structure instance address so
* that RC_WMORE resumes find the same frame again.
*/
asn_struct_ctx_t *asn_ext_ctx_get(asn_struct_external_ctx_t *ext, void *st) {
asn_struct_ctx_t *asn_ext_ctx_get(asn_struct_external_ctx_t *ext, void *st,
const void *type_key) {
size_t i;
if(!ext || !st) return 0;
for(i = 0; i < ext->frames_used && i < ASN_EXT_CTX_INLINE_FRAMES; i++)
if(ext->inline_frames[i].st == st)
if(ext->inline_frames[i].st == st
&& ext->inline_frames[i].type_key == type_key)
return &ext->inline_frames[i].ctx;
if(ext->frames_used > ASN_EXT_CTX_INLINE_FRAMES)
for(i = ASN_EXT_CTX_INLINE_FRAMES; i < ext->frames_used; i++)
if(ext->heap_frames[i - ASN_EXT_CTX_INLINE_FRAMES].st == st)
if(ext->heap_frames[i - ASN_EXT_CTX_INLINE_FRAMES].st == st
&& ext->heap_frames[i - ASN_EXT_CTX_INLINE_FRAMES].type_key == type_key)
return &ext->heap_frames[i - ASN_EXT_CTX_INLINE_FRAMES].ctx;
if(ext->frames_used < ASN_EXT_CTX_INLINE_FRAMES) {
asn_struct_ctx_frame_t *f = &ext->inline_frames[ext->frames_used++];
f->st = st;
f->type_key = type_key;
memset(&f->ctx, 0, sizeof(f->ctx));
return &f->ctx;
}
Expand All @@ -48,6 +52,7 @@ asn_struct_ctx_t *asn_ext_ctx_get(asn_struct_external_ctx_t *ext, void *st) {
&ext->heap_frames[ext->frames_used - ASN_EXT_CTX_INLINE_FRAMES];
ext->frames_used++;
f->st = st;
f->type_key = type_key;
memset(&f->ctx, 0, sizeof(f->ctx));
return &f->ctx;
}
Expand All @@ -67,10 +72,12 @@ void asn_struct_external_ctx_reset(asn_struct_external_ctx_t *ext) {
}

asn_struct_ctx_t *asn_get_struct_ctx(const asn_codec_ctx_t *opt_codec_ctx,
void *st, unsigned ctx_offset) {
void *st, unsigned ctx_offset,
const void *type_key) {
if(opt_codec_ctx && opt_codec_ctx->ext_struct_ctx)
return asn_ext_ctx_get(
(asn_struct_external_ctx_t *)opt_codec_ctx->ext_struct_ctx, st);
(asn_struct_external_ctx_t *)opt_codec_ctx->ext_struct_ctx,
st, type_key);
if(!st || ctx_offset == ASN_CTX_OFFSET_EXTERNAL) return 0;
return (asn_struct_ctx_t *)((char *)st + ctx_offset);
}
Expand Down
Loading