vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1 | /*- |
| 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 | |
| 10 | struct 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 | */ |
vlm | 1ff928d | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 21 | enum 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 | }; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 26 | typedef struct ber_dec_rval_s { |
vlm | 1ff928d | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 27 | enum ber_dec_rval_code_e code; /* Result code */ |
| 28 | size_t consumed; /* Number of bytes consumed */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 29 | } ber_dec_rval_t; |
| 30 | |
| 31 | /* |
| 32 | * A context for decoding BER across buffer boundaries. |
| 33 | */ |
| 34 | typedef 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 | */ |
| 45 | ber_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 | */ |
| 54 | typedef 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 | */ |
| 70 | ber_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_ */ |