00001 /* 00002 * SpanDSP - a series of DSP components for telephony 00003 * 00004 * v42bis.h 00005 * 00006 * Written by Steve Underwood <steveu@coppice.org> 00007 * 00008 * Copyright (C) 2005 Steve Underwood 00009 * 00010 * All rights reserved. 00011 * 00012 * This program is free software; you can redistribute it and/or modify 00013 * it under the terms of the GNU General Public License as published by 00014 * the Free Software Foundation; either version 2 of the License, or 00015 * (at your option) any later version. 00016 * 00017 * This program is distributed in the hope that it will be useful, 00018 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00019 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00020 * GNU General Public License for more details. 00021 * 00022 * You should have received a copy of the GNU General Public License 00023 * along with this program; if not, write to the Free Software 00024 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00025 * 00026 * $Id: v42bis.h,v 1.10 2006/01/27 14:29:09 steveu Exp $ 00027 */ 00028 00029 /*! \page v42bis_page V.42bis modem data compression 00030 \section v42bis_page_sec_1 What does it do? 00031 The v.42bis specification defines a data compression scheme, to work in 00032 conjunction with the error correction scheme defined in V.42. 00033 00034 \section v42bis_page_sec_2 How does it work? 00035 */ 00036 00037 #if !defined(_V42BIS_H_) 00038 #define _V42BIS_H_ 00039 00040 #define V42BIS_MAX_BITS 12 00041 #define V42BIS_MAX_CODEWORDS 4096 /* 2^V42BIS_MAX_BITS */ 00042 #define V42BIS_TABLE_SIZE 5021 /* This should be a prime >(2^V42BIS_MAX_BITS) */ 00043 #define V42BIS_MAX_STRING_SIZE 250 00044 00045 enum 00046 { 00047 V42BIS_P0_NEITHER_DIRECTION = 0, 00048 V42BIS_P0_INITIATOR_RESPONDER, 00049 V42BIS_P0_RESPONDER_INITIATOR, 00050 V42BIS_P0_BOTH_DIRECTIONS 00051 }; 00052 00053 typedef void (*v42bis_frame_handler_t)(void *user_data, const uint8_t *pkt, int len); 00054 typedef void (*v42bis_data_handler_t)(void *user_data, const uint8_t *buf, int len); 00055 00056 typedef struct 00057 { 00058 /*! \brief Callback function to handle received frames. */ 00059 v42bis_frame_handler_t handler; 00060 /*! \brief An opaque pointer passed in calls to frame_handler. */ 00061 void *user_data; 00062 /*! \brief The maximum frame length allowed */ 00063 int max_len; 00064 00065 uint32_t string_code; 00066 int string_length; 00067 uint32_t output_bit_buffer; 00068 int output_bit_count; 00069 int output_octet_count; 00070 uint8_t output_buf[1024]; 00071 /*! \brief The code hash table. */ 00072 uint16_t code[V42BIS_TABLE_SIZE]; 00073 /*! \brief The prior code for each defined code. */ 00074 uint16_t prior_code[V42BIS_MAX_CODEWORDS]; 00075 /*! \brief The number of leaf nodes this node has */ 00076 int16_t leaves[V42BIS_MAX_CODEWORDS]; 00077 /*! \brief This leaf octet for each defined code. */ 00078 uint8_t node_octet[V42BIS_MAX_CODEWORDS]; 00079 /*! \brief TRUE if we are in transparent (i.e. uncompressable) mode */ 00080 int transparent; 00081 int compressibility_filter; 00082 00083 /*! \brief Next empty dictionary entry */ 00084 uint32_t v42bis_parm_c1; 00085 /*! \brief Current codeword size */ 00086 int v42bis_parm_c2; 00087 /*! \brief Threshold for codeword size change */ 00088 uint32_t v42bis_parm_c3; 00089 00090 /*! \brief Mark that this is the first octet/code to be processed */ 00091 int first; 00092 uint8_t escape_code; 00093 } v42bis_compress_state_t; 00094 00095 typedef struct 00096 { 00097 /*! \brief Callback function to handle decompressed data. */ 00098 v42bis_data_handler_t handler; 00099 /*! \brief An opaque pointer passed in calls to data_handler. */ 00100 void *user_data; 00101 /*! \brief The maximum decompressed data block length allowed */ 00102 int max_len; 00103 00104 uint32_t old_code; 00105 uint32_t input_bit_buffer; 00106 int input_bit_count; 00107 int octet; 00108 int last_length; 00109 int output_octet_count; 00110 uint8_t output_buf[1024]; 00111 /*! \brief The prior code for each defined code. */ 00112 uint16_t prior_code[V42BIS_MAX_CODEWORDS]; 00113 /*! \brief The number of leaf nodes this node has */ 00114 int16_t leaves[V42BIS_MAX_CODEWORDS]; 00115 /*! \brief This leaf octet for each defined code. */ 00116 uint8_t node_octet[V42BIS_MAX_CODEWORDS]; 00117 /*! \brief This buffer holds the decoded string */ 00118 uint8_t decode_buf[V42BIS_MAX_STRING_SIZE]; 00119 /*! \brief TRUE if we are in transparent (i.e. uncompressable) mode */ 00120 int transparent; 00121 00122 /*! \brief Next empty dictionary entry */ 00123 int v42bis_parm_c1; 00124 /*! \brief Current codeword size */ 00125 int v42bis_parm_c2; 00126 /*! \brief Threshold for codeword size change */ 00127 int v42bis_parm_c3; 00128 00129 /*! \brief Mark that this is the first octet/code to be processed */ 00130 int first; 00131 uint8_t escape_code; 00132 int escaped; 00133 } v42bis_decompress_state_t; 00134 00135 /*! 00136 V.42bis compression/decompression descriptor. This defines the working state for a 00137 single instance of V.42bis compress/decompression. 00138 */ 00139 typedef struct 00140 { 00141 /*! \brief V.42bis data compression directions. */ 00142 int v42bis_parm_p0; 00143 00144 v42bis_compress_state_t compress; 00145 v42bis_decompress_state_t decompress; 00146 00147 /*! \brief Maximum codeword size (bits) */ 00148 int v42bis_parm_n1; 00149 /*! \brief Total number of codewords */ 00150 int v42bis_parm_n2; 00151 /*! \brief Maximum string length */ 00152 int v42bis_parm_n7; 00153 } v42bis_state_t; 00154 00155 #ifdef __cplusplus 00156 extern "C" { 00157 #endif 00158 00159 /*! Compress a block of octets. 00160 \param s The V.42bis context. 00161 \param buf The data to be compressed. 00162 \param len The length of the data buffer. 00163 \return 0 */ 00164 int v42bis_compress(v42bis_state_t *s, const uint8_t *buf, int len); 00165 00166 /*! Flush out any data remaining in a compression buffer. 00167 \param s The V.42bis context. 00168 \return 0 */ 00169 int v42bis_compress_flush(v42bis_state_t *s); 00170 00171 /*! Decompress a block of octets. 00172 \param s The V.42bis context. 00173 \param buf The data to be decompressed. 00174 \param len The length of the data buffer. 00175 \return 0 */ 00176 int v42bis_decompress(v42bis_state_t *s, const uint8_t *buf, int len); 00177 00178 /*! Flush out any data remaining in the decompression buffer. 00179 \param s The V.42bis context. 00180 \return 0 */ 00181 int v42bis_decompress_flush(v42bis_state_t *s); 00182 00183 /*! Initialise a V.42bis context. 00184 \param s The V.42bis context. 00185 \param negotiated_p0 The negotiated P0 parameter, from the V.42bis spec. 00186 \param negotiated_p1 The negotiated P1 parameter, from the V.42bis spec. 00187 \param negotiated_p2 The negotiated P2 parameter, from the V.42bis spec. 00188 \param frame_handler . 00189 \param frame_user_data . 00190 \param max_frame_len The maximum length that should be passed to the frame handler. 00191 \param data_handler . 00192 \param data_user_data . 00193 \param max_data_len The maximum length that should be passed to the data handler. 00194 \return The V.42bis context. */ 00195 v42bis_state_t *v42bis_init(v42bis_state_t *s, 00196 int negotiated_p0, 00197 int negotiated_p1, 00198 int negotiated_p2, 00199 v42bis_frame_handler_t frame_handler, 00200 void *frame_user_data, 00201 int max_frame_len, 00202 v42bis_data_handler_t data_handler, 00203 void *data_user_data, 00204 int max_data_len); 00205 00206 /*! Release a V.42bis context. 00207 \param s The V.42bis context. 00208 \return 0 if OK */ 00209 int v42bis_release(v42bis_state_t *s); 00210 00211 #ifdef __cplusplus 00212 } 00213 #endif 00214 00215 #endif 00216 /*- End of file ------------------------------------------------------------*/