blob: 9250adab231a2e7717d3db0cb33a927f752c57a9 [file] [log] [blame]
Lev Walkindc06f6b2004-10-20 15:50:55 +00001/*-
2 * Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved.
3 * Redistribution and modifications are permitted subject to BSD license.
4 */
5#ifndef _ASN_CODECS_H_
6#define _ASN_CODECS_H_
7
Lev Walkin21b41ac2005-07-24 09:03:44 +00008#ifdef __cplusplus
9extern "C" {
10#endif
11
Lev Walkindc06f6b2004-10-20 15:50:55 +000012struct asn_TYPE_descriptor_s; /* Forward declaration */
13
14/*
15 * This structure defines a context that may be passed to every ASN.1 encoder
16 * or decoder function.
17 * WARNING: if max_stack_size member is set, and you are calling the
18 * function pointers of the asn_TYPE_descriptor_t directly,
19 * this structure must be ALLOCATED ON THE STACK!
20 */
21typedef struct asn_codec_ctx_s {
22 /*
23 * Limit the decoder routines to use no (much) more stack than a given
24 * number of bytes. Most of decoders are stack-based, and this
25 * would protect against stack overflows if the number of nested
26 * encodings is high.
27 * The OCTET STRING, BIT STRING and ANY BER decoders are heap-based,
28 * and are safe from this kind of overflow.
29 * A value from getrlimit(RLIMIT_STACK) may be used to initialize
30 * this variable. Be careful in multithreaded environments, as the
31 * stack size is rather limited.
32 */
33 size_t max_stack_size; /* 0 disables stack bounds checking */
34} asn_codec_ctx_t;
35
36/*
37 * Type of the return value of the encoding functions (der_encode, xer_encode).
38 */
39typedef struct asn_enc_rval_s {
40 /*
41 * Number of bytes encoded.
42 * -1 indicates failure to encode the structure.
43 * In this case, the members below this one are meaningful.
44 */
45 ssize_t encoded;
46
47 /*
48 * Members meaningful when (encoded == -1), for post mortem analysis.
49 */
50
51 /* Type which cannot be encoded */
52 struct asn_TYPE_descriptor_s *failed_type;
53
54 /* Pointer to the structure of that type */
55 void *structure_ptr;
56} asn_enc_rval_t;
57#define _ASN_ENCODE_FAILED do { \
Lev Walkin21b41ac2005-07-24 09:03:44 +000058 asn_enc_rval_t tmp_error; \
59 tmp_error.encoded = -1; \
60 tmp_error.failed_type = td; \
61 tmp_error.structure_ptr = sptr; \
Lev Walkin86859bc2004-10-26 09:59:37 +000062 return tmp_error; \
Lev Walkindc06f6b2004-10-20 15:50:55 +000063} while(0)
64
65/*
66 * Type of the return value of the decoding functions (ber_decode, xer_decode)
67 *
68 * Please note that the number of consumed bytes is ALWAYS meaningful,
69 * even if code==RC_FAIL. This is to indicate the number of successfully
70 * decoded bytes, hence providing a possibility to fail with more diagnostics
71 * (i.e., print the offending remainder of the buffer).
72 */
73enum asn_dec_rval_code_e {
74 RC_OK, /* Decoded successfully */
75 RC_WMORE, /* More data expected, call again */
76 RC_FAIL /* Failure to decode data */
77};
78typedef struct asn_dec_rval_s {
79 enum asn_dec_rval_code_e code; /* Result code */
80 size_t consumed; /* Number of bytes consumed */
81} asn_dec_rval_t;
82
Lev Walkin21b41ac2005-07-24 09:03:44 +000083#ifdef __cplusplus
84}
85#endif
86
Lev Walkindc06f6b2004-10-20 15:50:55 +000087#endif /* _ASN_CODECS_H_ */