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