vlm | 9de248e | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 1 | /*- |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 2 | * Copyright (c) 2003, 2004, 2005 Lev Walkin <vlm@lionet.info>. |
| 3 | * All rights reserved. |
vlm | 9de248e | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 4 | * Redistribution and modifications are permitted subject to BSD license. |
| 5 | */ |
| 6 | #ifndef _ASN_CODECS_H_ |
| 7 | #define _ASN_CODECS_H_ |
| 8 | |
vlm | 3a41758 | 2005-07-24 09:03:44 +0000 | [diff] [blame] | 9 | #ifdef __cplusplus |
| 10 | extern "C" { |
| 11 | #endif |
| 12 | |
vlm | 9de248e | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 13 | struct 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 | */ |
| 22 | typedef 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 | */ |
| 40 | typedef 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 { \ |
vlm | 3a41758 | 2005-07-24 09:03:44 +0000 | [diff] [blame] | 59 | asn_enc_rval_t tmp_error; \ |
| 60 | tmp_error.encoded = -1; \ |
| 61 | tmp_error.failed_type = td; \ |
| 62 | tmp_error.structure_ptr = sptr; \ |
vlm | b882146 | 2004-10-26 09:59:37 +0000 | [diff] [blame] | 63 | return tmp_error; \ |
vlm | 9de248e | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 64 | } while(0) |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 65 | #define _ASN_ENCODED_OK(rval) do { \ |
| 66 | rval.structure_ptr = 0; \ |
| 67 | rval.failed_type = 0; \ |
| 68 | return rval; \ |
| 69 | } while(0) |
vlm | 9de248e | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 70 | |
| 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 | */ |
| 79 | enum 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 | }; |
| 84 | typedef 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; |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 88 | #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) |
vlm | 9de248e | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 94 | |
vlm | 3a41758 | 2005-07-24 09:03:44 +0000 | [diff] [blame] | 95 | #ifdef __cplusplus |
| 96 | } |
| 97 | #endif |
| 98 | |
vlm | 9de248e | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 99 | #endif /* _ASN_CODECS_H_ */ |