Blame src/asn1c/asn_codecs.h

Packit 728676
/*-
Packit 728676
 * Copyright (c) 2003, 2004, 2005 Lev Walkin <vlm@lionet.info>.
Packit 728676
 * All rights reserved.
Packit 728676
 * Redistribution and modifications are permitted subject to BSD license.
Packit 728676
 */
Packit 728676
#ifndef	_ASN_CODECS_H_
Packit 728676
#define	_ASN_CODECS_H_
Packit 728676
Packit 728676
#ifdef __cplusplus
Packit 728676
extern "C" {
Packit 728676
#endif
Packit 728676
Packit 728676
struct asn_TYPE_descriptor_s;	/* Forward declaration */
Packit 728676
Packit 728676
/*
Packit 728676
 * This structure defines a set of parameters that may be passed
Packit 728676
 * to every ASN.1 encoder or decoder function.
Packit 728676
 * WARNING: if max_stack_size member is set, and you are calling the
Packit 728676
 *   function pointers of the asn_TYPE_descriptor_t directly,
Packit 728676
 *   this structure must be ALLOCATED ON THE STACK!
Packit 728676
 *   If you can't always satisfy this requirement, use ber_decode(),
Packit 728676
 *   xer_decode() and uper_decode() functions instead.
Packit 728676
 */
Packit 728676
typedef struct asn_codec_ctx_s {
Packit 728676
	/*
Packit 728676
	 * Limit the decoder routines to use no (much) more stack than a given
Packit 728676
	 * number of bytes. Most of decoders are stack-based, and this
Packit 728676
	 * would protect against stack overflows if the number of nested
Packit 728676
	 * encodings is high.
Packit 728676
	 * The OCTET STRING, BIT STRING and ANY BER decoders are heap-based,
Packit 728676
	 * and are safe from this kind of overflow.
Packit 728676
	 * A value from getrlimit(RLIMIT_STACK) may be used to initialize
Packit 728676
	 * this variable. Be careful in multithreaded environments, as the
Packit 728676
	 * stack size is rather limited.
Packit 728676
	 */
Packit 728676
	size_t  max_stack_size; /* 0 disables stack bounds checking */
Packit 728676
} asn_codec_ctx_t;
Packit 728676
Packit 728676
/*
Packit 728676
 * Type of the return value of the encoding functions (der_encode, xer_encode).
Packit 728676
 */
Packit 728676
typedef struct asn_enc_rval_s {
Packit 728676
	/*
Packit 728676
	 * Number of bytes encoded.
Packit 728676
	 * -1 indicates failure to encode the structure.
Packit 728676
	 * In this case, the members below this one are meaningful.
Packit 728676
	 */
Packit 728676
	ssize_t encoded;
Packit 728676
Packit 728676
	/*
Packit 728676
	 * Members meaningful when (encoded == -1), for post mortem analysis.
Packit 728676
	 */
Packit 728676
Packit 728676
	/* Type which cannot be encoded */
Packit 728676
	struct asn_TYPE_descriptor_s *failed_type;
Packit 728676
Packit 728676
	/* Pointer to the structure of that type */
Packit 728676
	void *structure_ptr;
Packit 728676
} asn_enc_rval_t;
Packit 728676
#define	_ASN_ENCODE_FAILED do {					\
Packit 728676
	asn_enc_rval_t tmp_error;				\
Packit 728676
	tmp_error.encoded = -1;					\
Packit 728676
	tmp_error.failed_type = td;				\
Packit 728676
	tmp_error.structure_ptr = sptr;				\
Packit 728676
	ASN_DEBUG("Failed to encode element %s", td ? td->name : "");	\
Packit 728676
	return tmp_error;					\
Packit 728676
} while(0)
Packit 728676
#define	_ASN_ENCODED_OK(rval) do {				\
Packit 728676
	rval.structure_ptr = 0;					\
Packit 728676
	rval.failed_type = 0;					\
Packit 728676
	return rval;						\
Packit 728676
} while(0)
Packit 728676
Packit 728676
/*
Packit 728676
 * Type of the return value of the decoding functions (ber_decode, xer_decode)
Packit 728676
 * 
Packit 728676
 * Please note that the number of consumed bytes is ALWAYS meaningful,
Packit 728676
 * even if code==RC_FAIL. This is to indicate the number of successfully
Packit 728676
 * decoded bytes, hence providing a possibility to fail with more diagnostics
Packit 728676
 * (i.e., print the offending remainder of the buffer).
Packit 728676
 */
Packit 728676
enum asn_dec_rval_code_e {
Packit 728676
	RC_OK,		/* Decoded successfully */
Packit 728676
	RC_WMORE,	/* More data expected, call again */
Packit 728676
	RC_FAIL		/* Failure to decode data */
Packit 728676
};
Packit 728676
typedef struct asn_dec_rval_s {
Packit 728676
	enum asn_dec_rval_code_e code;	/* Result code */
Packit 728676
	size_t consumed;		/* Number of bytes consumed */
Packit 728676
} asn_dec_rval_t;
Packit 728676
#define	_ASN_DECODE_FAILED do {					\
Packit 728676
	asn_dec_rval_t tmp_error;				\
Packit 728676
	tmp_error.code = RC_FAIL;				\
Packit 728676
	tmp_error.consumed = 0;					\
Packit 728676
	ASN_DEBUG("Failed to decode element %s", td ? td->name : "");	\
Packit 728676
	return tmp_error;					\
Packit 728676
} while(0)
Packit 728676
#define	_ASN_DECODE_STARVED do {				\
Packit 728676
	asn_dec_rval_t tmp_error;				\
Packit 728676
	tmp_error.code = RC_WMORE;				\
Packit 728676
	tmp_error.consumed = 0;					\
Packit 728676
	return tmp_error;					\
Packit 728676
} while(0)
Packit 728676
Packit 728676
#ifdef __cplusplus
Packit 728676
}
Packit 728676
#endif
Packit 728676
Packit 728676
#endif	/* _ASN_CODECS_H_ */