Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 1 | /*- |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 2 | * Copyright (c) 2003, 2004, 2005 Lev Walkin <vlm@lionet.info>. |
| 3 | * All rights reserved. |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 4 | * Redistribution and modifications are permitted subject to BSD license. |
| 5 | */ |
Lev Walkin | c6cac8e | 2016-03-14 02:57:07 -0700 | [diff] [blame] | 6 | #ifndef ASN_CODECS_H |
| 7 | #define ASN_CODECS_H |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 8 | |
Lev Walkin | 21b41ac | 2005-07-24 09:03:44 +0000 | [diff] [blame] | 9 | #ifdef __cplusplus |
| 10 | extern "C" { |
| 11 | #endif |
| 12 | |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 13 | struct asn_TYPE_descriptor_s; /* Forward declaration */ |
| 14 | |
| 15 | /* |
Lev Walkin | 1d9e8dd | 2005-12-07 05:46:03 +0000 | [diff] [blame] | 16 | * This structure defines a set of parameters that may be passed |
| 17 | * to every ASN.1 encoder or decoder function. |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 18 | * WARNING: if max_stack_size member is set, and you are calling the |
Lev Walkin | 1d9e8dd | 2005-12-07 05:46:03 +0000 | [diff] [blame] | 19 | * function pointers of the asn_TYPE_descriptor_t directly, |
| 20 | * this structure must be ALLOCATED ON THE STACK! |
| 21 | * If you can't always satisfy this requirement, use ber_decode(), |
| 22 | * xer_decode() and uper_decode() functions instead. |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 23 | */ |
| 24 | typedef struct asn_codec_ctx_s { |
| 25 | /* |
| 26 | * Limit the decoder routines to use no (much) more stack than a given |
| 27 | * number of bytes. Most of decoders are stack-based, and this |
| 28 | * would protect against stack overflows if the number of nested |
| 29 | * encodings is high. |
| 30 | * The OCTET STRING, BIT STRING and ANY BER decoders are heap-based, |
| 31 | * and are safe from this kind of overflow. |
| 32 | * A value from getrlimit(RLIMIT_STACK) may be used to initialize |
| 33 | * this variable. Be careful in multithreaded environments, as the |
| 34 | * stack size is rather limited. |
| 35 | */ |
| 36 | size_t max_stack_size; /* 0 disables stack bounds checking */ |
| 37 | } asn_codec_ctx_t; |
| 38 | |
| 39 | /* |
| 40 | * Type of the return value of the encoding functions (der_encode, xer_encode). |
| 41 | */ |
| 42 | typedef struct asn_enc_rval_s { |
| 43 | /* |
| 44 | * Number of bytes encoded. |
| 45 | * -1 indicates failure to encode the structure. |
| 46 | * In this case, the members below this one are meaningful. |
| 47 | */ |
| 48 | ssize_t encoded; |
| 49 | |
| 50 | /* |
| 51 | * Members meaningful when (encoded == -1), for post mortem analysis. |
| 52 | */ |
| 53 | |
| 54 | /* Type which cannot be encoded */ |
| 55 | struct asn_TYPE_descriptor_s *failed_type; |
| 56 | |
| 57 | /* Pointer to the structure of that type */ |
| 58 | void *structure_ptr; |
| 59 | } asn_enc_rval_t; |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 60 | #define ASN__ENCODE_FAILED do { \ |
Lev Walkin | 21b41ac | 2005-07-24 09:03:44 +0000 | [diff] [blame] | 61 | asn_enc_rval_t tmp_error; \ |
| 62 | tmp_error.encoded = -1; \ |
| 63 | tmp_error.failed_type = td; \ |
| 64 | tmp_error.structure_ptr = sptr; \ |
Lev Walkin | 46755ce | 2013-03-19 16:16:28 -0700 | [diff] [blame] | 65 | ASN_DEBUG("Failed to encode element %s", td ? td->name : ""); \ |
Lev Walkin | 86859bc | 2004-10-26 09:59:37 +0000 | [diff] [blame] | 66 | return tmp_error; \ |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 67 | } while(0) |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 68 | #define ASN__ENCODED_OK(rval) do { \ |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 69 | rval.structure_ptr = 0; \ |
| 70 | rval.failed_type = 0; \ |
| 71 | return rval; \ |
| 72 | } while(0) |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 73 | |
| 74 | /* |
| 75 | * Type of the return value of the decoding functions (ber_decode, xer_decode) |
| 76 | * |
| 77 | * Please note that the number of consumed bytes is ALWAYS meaningful, |
| 78 | * even if code==RC_FAIL. This is to indicate the number of successfully |
| 79 | * decoded bytes, hence providing a possibility to fail with more diagnostics |
| 80 | * (i.e., print the offending remainder of the buffer). |
| 81 | */ |
| 82 | enum asn_dec_rval_code_e { |
| 83 | RC_OK, /* Decoded successfully */ |
| 84 | RC_WMORE, /* More data expected, call again */ |
| 85 | RC_FAIL /* Failure to decode data */ |
| 86 | }; |
| 87 | typedef struct asn_dec_rval_s { |
| 88 | enum asn_dec_rval_code_e code; /* Result code */ |
| 89 | size_t consumed; /* Number of bytes consumed */ |
| 90 | } asn_dec_rval_t; |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 91 | #define ASN__DECODE_FAILED do { \ |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 92 | asn_dec_rval_t tmp_error; \ |
| 93 | tmp_error.code = RC_FAIL; \ |
| 94 | tmp_error.consumed = 0; \ |
Lev Walkin | 46755ce | 2013-03-19 16:16:28 -0700 | [diff] [blame] | 95 | ASN_DEBUG("Failed to decode element %s", td ? td->name : ""); \ |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 96 | return tmp_error; \ |
| 97 | } while(0) |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 98 | #define ASN__DECODE_STARVED do { \ |
Lev Walkin | 0a8aa60 | 2006-09-18 20:05:55 +0000 | [diff] [blame] | 99 | asn_dec_rval_t tmp_error; \ |
| 100 | tmp_error.code = RC_WMORE; \ |
| 101 | tmp_error.consumed = 0; \ |
| 102 | return tmp_error; \ |
| 103 | } while(0) |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 104 | |
Lev Walkin | 21b41ac | 2005-07-24 09:03:44 +0000 | [diff] [blame] | 105 | #ifdef __cplusplus |
| 106 | } |
| 107 | #endif |
| 108 | |
Lev Walkin | c6cac8e | 2016-03-14 02:57:07 -0700 | [diff] [blame] | 109 | #endif /* ASN_CODECS_H */ |