blob: 2f8ed10b7b1bbe521f2071c0b4c038f76a143fb4 [file] [log] [blame]
Lev Walkindc06f6b2004-10-20 15:50:55 +00001/*-
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
8struct asn_TYPE_descriptor_s; /* Forward declaration */
9
10/*
11 * This structure defines a context that may be passed to every ASN.1 encoder
12 * or decoder function.
13 * WARNING: if max_stack_size member is set, and you are calling the
14 * function pointers of the asn_TYPE_descriptor_t directly,
15 * this structure must be ALLOCATED ON THE STACK!
16 */
17typedef struct asn_codec_ctx_s {
18 /*
19 * Limit the decoder routines to use no (much) more stack than a given
20 * number of bytes. Most of decoders are stack-based, and this
21 * would protect against stack overflows if the number of nested
22 * encodings is high.
23 * The OCTET STRING, BIT STRING and ANY BER decoders are heap-based,
24 * and are safe from this kind of overflow.
25 * A value from getrlimit(RLIMIT_STACK) may be used to initialize
26 * this variable. Be careful in multithreaded environments, as the
27 * stack size is rather limited.
28 */
29 size_t max_stack_size; /* 0 disables stack bounds checking */
30} asn_codec_ctx_t;
31
32/*
33 * Type of the return value of the encoding functions (der_encode, xer_encode).
34 */
35typedef struct asn_enc_rval_s {
36 /*
37 * Number of bytes encoded.
38 * -1 indicates failure to encode the structure.
39 * In this case, the members below this one are meaningful.
40 */
41 ssize_t encoded;
42
43 /*
44 * Members meaningful when (encoded == -1), for post mortem analysis.
45 */
46
47 /* Type which cannot be encoded */
48 struct asn_TYPE_descriptor_s *failed_type;
49
50 /* Pointer to the structure of that type */
51 void *structure_ptr;
52} asn_enc_rval_t;
53#define _ASN_ENCODE_FAILED do { \
54 asn_enc_rval_t __er = { -1, td, sptr }; \
55 return __er; \
56} while(0)
57
58/*
59 * Type of the return value of the decoding functions (ber_decode, xer_decode)
60 *
61 * Please note that the number of consumed bytes is ALWAYS meaningful,
62 * even if code==RC_FAIL. This is to indicate the number of successfully
63 * decoded bytes, hence providing a possibility to fail with more diagnostics
64 * (i.e., print the offending remainder of the buffer).
65 */
66enum asn_dec_rval_code_e {
67 RC_OK, /* Decoded successfully */
68 RC_WMORE, /* More data expected, call again */
69 RC_FAIL /* Failure to decode data */
70};
71typedef struct asn_dec_rval_s {
72 enum asn_dec_rval_code_e code; /* Result code */
73 size_t consumed; /* Number of bytes consumed */
74} asn_dec_rval_t;
75
76#endif /* _ASN_CODECS_H_ */