Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 1 | /*- |
| 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 Walkin | 21b41ac | 2005-07-24 09:03:44 +0000 | [diff] [blame] | 8 | #ifdef __cplusplus |
| 9 | extern "C" { |
| 10 | #endif |
| 11 | |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 12 | struct 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 | */ |
| 21 | typedef 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 | */ |
| 39 | typedef 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 Walkin | 21b41ac | 2005-07-24 09:03:44 +0000 | [diff] [blame] | 58 | asn_enc_rval_t tmp_error; \ |
| 59 | tmp_error.encoded = -1; \ |
| 60 | tmp_error.failed_type = td; \ |
| 61 | tmp_error.structure_ptr = sptr; \ |
Lev Walkin | 86859bc | 2004-10-26 09:59:37 +0000 | [diff] [blame] | 62 | return tmp_error; \ |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 63 | } 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 | */ |
| 73 | enum 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 | }; |
| 78 | typedef 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 Walkin | 21b41ac | 2005-07-24 09:03:44 +0000 | [diff] [blame] | 83 | #ifdef __cplusplus |
| 84 | } |
| 85 | #endif |
| 86 | |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 87 | #endif /* _ASN_CODECS_H_ */ |