blob: e75c2709cd956507df15791c7b602996fbced9a8 [file] [log] [blame]
Lev Walkin20696a42017-10-17 21:27:33 -07001/*
2 * Copyright (c) 2003-2017 Lev Walkin <vlm@lionet.info>. All rights reserved.
Lev Walkindc06f6b2004-10-20 15:50:55 +00003 * Redistribution and modifications are permitted subject to BSD license.
4 */
Lev Walkinc6cac8e2016-03-14 02:57:07 -07005#ifndef ASN_CODECS_H
6#define ASN_CODECS_H
Lev Walkindc06f6b2004-10-20 15:50:55 +00007
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/*
Lev Walkin1d9e8dd2005-12-07 05:46:03 +000015 * This structure defines a set of parameters that may be passed
16 * to every ASN.1 encoder or decoder function.
Lev Walkindc06f6b2004-10-20 15:50:55 +000017 * WARNING: if max_stack_size member is set, and you are calling the
Lev Walkin1d9e8dd2005-12-07 05:46:03 +000018 * function pointers of the asn_TYPE_descriptor_t directly,
19 * this structure must be ALLOCATED ON THE STACK!
20 * If you can't always satisfy this requirement, use ber_decode(),
21 * xer_decode() and uper_decode() functions instead.
Lev Walkindc06f6b2004-10-20 15:50:55 +000022 */
23typedef struct asn_codec_ctx_s {
24 /*
25 * Limit the decoder routines to use no (much) more stack than a given
26 * number of bytes. Most of decoders are stack-based, and this
27 * would protect against stack overflows if the number of nested
28 * encodings is high.
29 * The OCTET STRING, BIT STRING and ANY BER decoders are heap-based,
30 * and are safe from this kind of overflow.
31 * A value from getrlimit(RLIMIT_STACK) may be used to initialize
32 * this variable. Be careful in multithreaded environments, as the
33 * stack size is rather limited.
34 */
35 size_t max_stack_size; /* 0 disables stack bounds checking */
36} asn_codec_ctx_t;
37
38/*
39 * Type of the return value of the encoding functions (der_encode, xer_encode).
40 */
41typedef struct asn_enc_rval_s {
42 /*
43 * Number of bytes encoded.
44 * -1 indicates failure to encode the structure.
45 * In this case, the members below this one are meaningful.
46 */
47 ssize_t encoded;
48
49 /*
50 * Members meaningful when (encoded == -1), for post mortem analysis.
51 */
52
53 /* Type which cannot be encoded */
Lev Walkin20696a42017-10-17 21:27:33 -070054 const struct asn_TYPE_descriptor_s *failed_type;
Lev Walkindc06f6b2004-10-20 15:50:55 +000055
56 /* Pointer to the structure of that type */
Lev Walkin20696a42017-10-17 21:27:33 -070057 const void *structure_ptr;
Lev Walkindc06f6b2004-10-20 15:50:55 +000058} asn_enc_rval_t;
Lev Walkin7c1dc052016-03-14 03:08:15 -070059#define ASN__ENCODE_FAILED do { \
Lev Walkin21b41ac2005-07-24 09:03:44 +000060 asn_enc_rval_t tmp_error; \
61 tmp_error.encoded = -1; \
62 tmp_error.failed_type = td; \
63 tmp_error.structure_ptr = sptr; \
Lev Walkin46755ce2013-03-19 16:16:28 -070064 ASN_DEBUG("Failed to encode element %s", td ? td->name : ""); \
Lev Walkin86859bc2004-10-26 09:59:37 +000065 return tmp_error; \
Lev Walkindc06f6b2004-10-20 15:50:55 +000066} while(0)
Lev Walkin7c1dc052016-03-14 03:08:15 -070067#define ASN__ENCODED_OK(rval) do { \
Lev Walkin59b176e2005-11-26 11:25:14 +000068 rval.structure_ptr = 0; \
69 rval.failed_type = 0; \
70 return rval; \
71} while(0)
Lev Walkindc06f6b2004-10-20 15:50:55 +000072
73/*
74 * Type of the return value of the decoding functions (ber_decode, xer_decode)
75 *
76 * Please note that the number of consumed bytes is ALWAYS meaningful,
77 * even if code==RC_FAIL. This is to indicate the number of successfully
78 * decoded bytes, hence providing a possibility to fail with more diagnostics
79 * (i.e., print the offending remainder of the buffer).
80 */
81enum asn_dec_rval_code_e {
82 RC_OK, /* Decoded successfully */
83 RC_WMORE, /* More data expected, call again */
84 RC_FAIL /* Failure to decode data */
85};
86typedef struct asn_dec_rval_s {
87 enum asn_dec_rval_code_e code; /* Result code */
88 size_t consumed; /* Number of bytes consumed */
89} asn_dec_rval_t;
Lev Walkin7c1dc052016-03-14 03:08:15 -070090#define ASN__DECODE_FAILED do { \
Lev Walkin59b176e2005-11-26 11:25:14 +000091 asn_dec_rval_t tmp_error; \
92 tmp_error.code = RC_FAIL; \
93 tmp_error.consumed = 0; \
Lev Walkin46755ce2013-03-19 16:16:28 -070094 ASN_DEBUG("Failed to decode element %s", td ? td->name : ""); \
Lev Walkin59b176e2005-11-26 11:25:14 +000095 return tmp_error; \
96} while(0)
Lev Walkin7c1dc052016-03-14 03:08:15 -070097#define ASN__DECODE_STARVED do { \
Lev Walkin0a8aa602006-09-18 20:05:55 +000098 asn_dec_rval_t tmp_error; \
99 tmp_error.code = RC_WMORE; \
100 tmp_error.consumed = 0; \
101 return tmp_error; \
102} while(0)
Lev Walkindc06f6b2004-10-20 15:50:55 +0000103
Lev Walkin21b41ac2005-07-24 09:03:44 +0000104#ifdef __cplusplus
105}
106#endif
107
Lev Walkinc6cac8e2016-03-14 02:57:07 -0700108#endif /* ASN_CODECS_H */