blob: 21a04b1665aa9ed728bb0c3a4b650e5c319cf0e8 [file] [log] [blame]
Harry Tran1f1098a2020-03-10 10:40:10 -04001/*****************************************************************************
2# *
3# Copyright 2019 AT&T Intellectual Property *
4# *
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 *
8# *
9# http://www.apache.org/licenses/LICENSE-2.0 *
10# *
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. *
16# *
17******************************************************************************/
18
19/*
20 * Copyright (c) 2003-2017 Lev Walkin <vlm@lionet.info>. All rights reserved.
21 * Redistribution and modifications are permitted subject to BSD license.
22 */
23#ifndef ASN_CODECS_H
24#define ASN_CODECS_H
25
26#ifdef __cplusplus
27extern "C" {
28#endif
29
30struct asn_TYPE_descriptor_s; /* Forward declaration */
31
32/*
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.
40 */
41typedef struct asn_codec_ctx_s {
42 /*
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
46 * encodings is high.
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.
52 */
53 size_t max_stack_size; /* 0 disables stack bounds checking */
54} asn_codec_ctx_t;
55
56/*
57 * Type of the return value of the encoding functions (der_encode, xer_encode).
58 */
59typedef struct asn_enc_rval_s {
60 /*
61 * Number of bytes encoded.
62 * -1 indicates failure to encode the structure.
63 * In this case, the members below this one are meaningful.
64 */
65 ssize_t encoded;
66
67 /*
68 * Members meaningful when (encoded == -1), for post mortem analysis.
69 */
70
71 /* Type which cannot be encoded */
72 const struct asn_TYPE_descriptor_s *failed_type;
73
74 /* Pointer to the structure of that type */
75 const void *structure_ptr;
76} asn_enc_rval_t;
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 : ""); \
83 return tmp_error; \
84} while(0)
85#define ASN__ENCODED_OK(rval) do { \
86 rval.structure_ptr = 0; \
87 rval.failed_type = 0; \
88 return rval; \
89} while(0)
90
91/*
92 * Type of the return value of the decoding functions (ber_decode, xer_decode)
93 *
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).
98 */
99enum 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 */
103};
104typedef struct asn_dec_rval_s {
105 enum asn_dec_rval_code_e code; /* Result code */
106 size_t consumed; /* Number of bytes consumed */
107} asn_dec_rval_t;
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 : ""); \
113 return tmp_error; \
114} while(0)
115#define ASN__DECODE_STARVED do { \
116 asn_dec_rval_t tmp_error; \
117 tmp_error.code = RC_WMORE; \
118 tmp_error.consumed = 0; \
119 return tmp_error; \
120} while(0)
121
122#ifdef __cplusplus
123}
124#endif
125
126#endif /* ASN_CODECS_H */