1 /*****************************************************************************
3 # Copyright 2019 AT&T Intellectual Property *
5 # Licensed under the Apache License, Version 2.0 (the "License"); *
6 # you may not use this file except in compliance with the License. *
7 # You may obtain a copy of the License at *
9 # http://www.apache.org/licenses/LICENSE-2.0 *
11 # Unless required by applicable law or agreed to in writing, software *
12 # distributed under the License is distributed on an "AS IS" BASIS, *
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
14 # See the License for the specific language governing permissions and *
15 # limitations under the License. *
17 ******************************************************************************/
20 * Copyright (c) 2003-2017 Lev Walkin <vlm@lionet.info>. All rights reserved.
21 * Redistribution and modifications are permitted subject to BSD license.
30 struct asn_TYPE_descriptor_s; /* Forward declaration */
33 * This structure defines a set of parameters that may be passed
34 * to every ASN.1 encoder or decoder function.
35 * WARNING: if max_stack_size member is set, and you are calling the
36 * function pointers of the asn_TYPE_descriptor_t directly,
37 * this structure must be ALLOCATED ON THE STACK!
38 * If you can't always satisfy this requirement, use ber_decode(),
39 * xer_decode() and uper_decode() functions instead.
41 typedef struct asn_codec_ctx_s {
43 * Limit the decoder routines to use no (much) more stack than a given
44 * number of bytes. Most of decoders are stack-based, and this
45 * would protect against stack overflows if the number of nested
47 * The OCTET STRING, BIT STRING and ANY BER decoders are heap-based,
48 * and are safe from this kind of overflow.
49 * A value from getrlimit(RLIMIT_STACK) may be used to initialize
50 * this variable. Be careful in multithreaded environments, as the
51 * stack size is rather limited.
53 size_t max_stack_size; /* 0 disables stack bounds checking */
57 * Type of the return value of the encoding functions (der_encode, xer_encode).
59 typedef struct asn_enc_rval_s {
61 * Number of bytes encoded.
62 * -1 indicates failure to encode the structure.
63 * In this case, the members below this one are meaningful.
68 * Members meaningful when (encoded == -1), for post mortem analysis.
71 /* Type which cannot be encoded */
72 const struct asn_TYPE_descriptor_s *failed_type;
74 /* Pointer to the structure of that type */
75 const void *structure_ptr;
77 #define ASN__ENCODE_FAILED do { \
78 asn_enc_rval_t tmp_error; \
79 tmp_error.encoded = -1; \
80 tmp_error.failed_type = td; \
81 tmp_error.structure_ptr = sptr; \
82 ASN_DEBUG("Failed to encode element %s", td ? td->name : ""); \
85 #define ASN__ENCODED_OK(rval) do { \
86 rval.structure_ptr = 0; \
87 rval.failed_type = 0; \
92 * Type of the return value of the decoding functions (ber_decode, xer_decode)
94 * Please note that the number of consumed bytes is ALWAYS meaningful,
95 * even if code==RC_FAIL. This is to indicate the number of successfully
96 * decoded bytes, hence providing a possibility to fail with more diagnostics
97 * (i.e., print the offending remainder of the buffer).
99 enum asn_dec_rval_code_e {
100 RC_OK, /* Decoded successfully */
101 RC_WMORE, /* More data expected, call again */
102 RC_FAIL /* Failure to decode data */
104 typedef struct asn_dec_rval_s {
105 enum asn_dec_rval_code_e code; /* Result code */
106 size_t consumed; /* Number of bytes consumed */
108 #define ASN__DECODE_FAILED do { \
109 asn_dec_rval_t tmp_error; \
110 tmp_error.code = RC_FAIL; \
111 tmp_error.consumed = 0; \
112 ASN_DEBUG("Failed to decode element %s", td ? td->name : ""); \
115 #define ASN__DECODE_STARVED do { \
116 asn_dec_rval_t tmp_error; \
117 tmp_error.code = RC_WMORE; \
118 tmp_error.consumed = 0; \
126 #endif /* ASN_CODECS_H */