blob: 4842adb539769eb2f72b9c5af5af761cdf24677c [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
8#include <constr_TYPE.h>
9
10struct asn1_TYPE_descriptor_s; /* Forward declaration */
11
12/*
13 * This structure describes the return value common across the
14 * various BER decoders.
15 *
16 * Please note that the number of consumed bytes is ALWAYS meaningful,
17 * even if code!=RC_OK. This is so to indicate the number of successfully
18 * decoded bytes, hence provide a possibility, to fail with more diagnostics
19 * (i.e., print the offending remainder of the buffer).
20 */
Lev Walkin4d9528c2004-08-11 08:10:13 +000021enum ber_dec_rval_code_e {
22 RC_OK, /* Decoded successfully */
23 RC_WMORE, /* More data expected, call again */
24 RC_FAIL, /* Failure to decode data */
25};
Lev Walkinf15320b2004-06-03 03:38:44 +000026typedef struct ber_dec_rval_s {
Lev Walkin4d9528c2004-08-11 08:10:13 +000027 enum ber_dec_rval_code_e code; /* Result code */
28 size_t consumed; /* Number of bytes consumed */
Lev Walkinf15320b2004-06-03 03:38:44 +000029} ber_dec_rval_t;
30
31/*
32 * A context for decoding BER across buffer boundaries.
33 */
34typedef struct ber_dec_ctx_s {
35 int phase; /* Decoding phase */
36 int step; /* Elementary step of a phase */
37 ber_tlv_len_t left; /* Number of bytes left, -1 for indefinite */
38 void *ptr; /* Decoder-specific stuff */
39} ber_dec_ctx_t;
40
41/*
42 * The BER decoder of any type.
43 * This function may be invoked directly from the application.
44 */
45ber_dec_rval_t ber_decode(struct asn1_TYPE_descriptor_s *type_descriptor,
46 void **struct_ptr, /* Pointer to a target structure's pointer */
47 void *buffer, /* Data to be decoded */
48 size_t size /* Size of that buffer */
49 );
50
51/*
52 * Type of generic function which decodes the byte stream into the structure.
53 */
54typedef ber_dec_rval_t (ber_type_decoder_f)(
55 struct asn1_TYPE_descriptor_s *type_descriptor,
56 void **type_structure, void *buf_ptr, size_t size,
57 int tag_mode);
58
59/*******************************
60 * INTERNALLY USEFUL FUNCTIONS *
61 *******************************/
62
63/*
64 * Check that all tags correspond to the type definition (as given in head).
65 * On return, last_length would contain either a non-negative length of the
66 * value part of the last TLV, or the negative number of expected
67 * "end of content" sequences. The number may only be negative if the
68 * head->last_tag_form is non-zero.
69 */
70ber_dec_rval_t ber_check_tags(struct asn1_TYPE_descriptor_s *type_dsc,
71 ber_dec_ctx_t *ctx, /* saved context */
72 void *ptr, size_t size,
73 int tag_mode, /* {-1,0,1}: IMPLICIT, no, EXPLICIT */
74 ber_tlv_len_t *last_length,
75 int *opt_tlv_form);
76
77#endif /* _BER_DECODER_H_ */