blob: 8499c6a1af717d643db13054e3697c9b7cac3cc3 [file] [log] [blame]
Lev Walkinf15320b2004-06-03 03:38:44 +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 _BER_DECODER_H_
6#define _BER_DECODER_H_
7
Lev Walkin11c3e172004-09-24 21:00:50 +00008#include <asn_application.h>
Lev Walkinf15320b2004-06-03 03:38:44 +00009
Lev Walkin1cbd2222004-09-29 13:24:10 +000010struct asn_TYPE_descriptor_s; /* Forward declaration */
11struct asn_codec_ctx_s; /* Forward declaration */
Lev Walkinf15320b2004-06-03 03:38:44 +000012
13/*
14 * This structure describes the return value common across the
15 * various BER decoders.
16 *
17 * Please note that the number of consumed bytes is ALWAYS meaningful,
18 * even if code!=RC_OK. This is so to indicate the number of successfully
19 * decoded bytes, hence provide a possibility, to fail with more diagnostics
20 * (i.e., print the offending remainder of the buffer).
21 */
Lev Walkin1cbd2222004-09-29 13:24:10 +000022 enum ber_dec_rval_code_e {
Lev Walkin4d9528c2004-08-11 08:10:13 +000023 RC_OK, /* Decoded successfully */
24 RC_WMORE, /* More data expected, call again */
Lev Walkin8e8078a2004-09-26 13:10:40 +000025 RC_FAIL /* Failure to decode data */
Lev Walkin1cbd2222004-09-29 13:24:10 +000026 };
Lev Walkinf15320b2004-06-03 03:38:44 +000027typedef struct ber_dec_rval_s {
Lev Walkin4d9528c2004-08-11 08:10:13 +000028 enum ber_dec_rval_code_e code; /* Result code */
29 size_t consumed; /* Number of bytes consumed */
Lev Walkinf15320b2004-06-03 03:38:44 +000030} ber_dec_rval_t;
31
32/*
Lev Walkinf15320b2004-06-03 03:38:44 +000033 * The BER decoder of any type.
34 * This function may be invoked directly from the application.
35 */
Lev Walkin1cbd2222004-09-29 13:24:10 +000036ber_dec_rval_t ber_decode(struct asn_codec_ctx_s *opt_codec_ctx,
37 struct asn_TYPE_descriptor_s *type_descriptor,
Lev Walkinf15320b2004-06-03 03:38:44 +000038 void **struct_ptr, /* Pointer to a target structure's pointer */
39 void *buffer, /* Data to be decoded */
40 size_t size /* Size of that buffer */
41 );
42
43/*
44 * Type of generic function which decodes the byte stream into the structure.
45 */
46typedef ber_dec_rval_t (ber_type_decoder_f)(
Lev Walkin1cbd2222004-09-29 13:24:10 +000047 struct asn_codec_ctx_s *opt_codec_ctx,
48 struct asn_TYPE_descriptor_s *type_descriptor,
Lev Walkinf15320b2004-06-03 03:38:44 +000049 void **type_structure, void *buf_ptr, size_t size,
50 int tag_mode);
51
52/*******************************
53 * INTERNALLY USEFUL FUNCTIONS *
54 *******************************/
55
56/*
57 * Check that all tags correspond to the type definition (as given in head).
58 * On return, last_length would contain either a non-negative length of the
59 * value part of the last TLV, or the negative number of expected
60 * "end of content" sequences. The number may only be negative if the
61 * head->last_tag_form is non-zero.
62 */
Lev Walkin1cbd2222004-09-29 13:24:10 +000063ber_dec_rval_t ber_check_tags(
64 struct asn_codec_ctx_s *opt_codec_ctx, /* optional context */
65 struct asn_TYPE_descriptor_s *type_dsc,
66 asn_struct_ctx_t *opt_ctx, /* saved decoding context */
Lev Walkinf15320b2004-06-03 03:38:44 +000067 void *ptr, size_t size,
68 int tag_mode, /* {-1,0,1}: IMPLICIT, no, EXPLICIT */
Lev Walkin8e8078a2004-09-26 13:10:40 +000069 int last_tag_form, /* {-1,0:1}: any, primitive, constr */
Lev Walkinf15320b2004-06-03 03:38:44 +000070 ber_tlv_len_t *last_length,
Lev Walkin1cbd2222004-09-29 13:24:10 +000071 int *opt_tlv_form /* optional tag form */
72 );
Lev Walkinf15320b2004-06-03 03:38:44 +000073
74#endif /* _BER_DECODER_H_ */