Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

287 lines
10 KiB

  1. /*++
  2. Copyright (c) 1996 Microsoft Corporation
  3. Module Name:
  4. arap.c
  5. Abstract:
  6. Header file for all the v42bis stuff useed by ARAP (adapted from fcr's code)
  7. Author:
  8. Shirish Koti
  9. Revision History:
  10. 15 Nov 1996 Initial Version
  11. --*/
  12. // v42bis stuff begin
  13. // #define PRIVATE static
  14. // v42bis stuff end
  15. /* negotiated parameters in XID */
  16. #define PARM_GROUP_ID 0xf0 /* ISB 8885, Addendum 3 */
  17. #define PARM_PARM_ID_V42 0x00
  18. #define PARM_PARM_ID_P0 0x01
  19. #define PARM_PARM_ID_P1 0x02
  20. #define PARM_PARM_ID_P2 0x03
  21. /* control code words (compression mode) */
  22. #define CCW_ETM 0x00 /* enter transparent mode */
  23. #define CCW_FLUSH 0x01 /* flush data */
  24. #define CCW_STEPUP 0x02 /* stepup code word size */
  25. /* command code words (transparent mode) */
  26. #define CCW_ECM 0x00 /* enter compression mode */
  27. #define CCW_EID 0x01 /* escape character in data */
  28. #define CCW_RESET 0x02 /* force reinitialization */
  29. /* escape char cycling */
  30. #define ESCAPE_CYCLE 51
  31. /*
  32. * v.42bis dictionary node
  33. */
  34. typedef struct {
  35. UCHAR byte; /* character */
  36. USHORT parent; /* ptr to parent node */
  37. USHORT node; /* chain of nodes w/same parent */
  38. USHORT leaf; /* chain of leafs w/same parent */
  39. } node_t;
  40. /*
  41. * v.42bis state block
  42. */
  43. typedef struct {
  44. /* connection */
  45. void *connection;
  46. /* values from spec */
  47. SHORT n1; /* maximum codeword size (bits) */
  48. SHORT n2; /* total number of codewords */
  49. #define N3 8 /* character size (bits) */
  50. #define N4 256 /* characters in alphabet (2^n3) */
  51. #define N5 (N4+N6) /* index # of 1st entry to store a string */
  52. #define N6 3 /* # of control words */
  53. UCHAR n7; /* maximum string length */
  54. /* dictionary */
  55. #define CODES 2048 /* max # of codewords */
  56. #define LOG2_CODES 11 /* log2(CODES) (max # of codeword bits) */
  57. node_t dictionary[CODES];
  58. #define DICT(i) (&state->dictionary[i])
  59. #define CODE(n) ((n) - state->dictionary)
  60. USHORT c1; /* next dictionary entry */
  61. UCHAR c2; /* current codeword size */
  62. USHORT c3; /* threshhold for codeword size change */
  63. UCHAR string_size; /* # bytes in string so far */
  64. USHORT last_match; /* index of last match of "string" */
  65. USHORT last_new; /* index of last new node */
  66. USHORT last_decode;
  67. UCHAR last_decode_size;
  68. UCHAR escape; /* escape character */
  69. BOOLEAN transparent; /* are we in transparent mode? */
  70. BOOLEAN decode_only; /* are we decode side ? */
  71. #if DEBUG
  72. UCHAR dump_indent; /* indentation dumping dict. tree */
  73. BOOLEAN debug_encode_bytes;
  74. BOOLEAN debug_encode;
  75. BOOLEAN debug_decode_bytes;
  76. BOOLEAN debug_decode;
  77. BOOLEAN debug_flow;
  78. #endif
  79. UCHAR word_size; /* local # bits to decode into */
  80. BOOLEAN exception_next; /* do exception processing; next ch */
  81. BOOLEAN escaped; /* have we just gotten an escape? */
  82. BOOLEAN just_flushed; /* did we just flush? */
  83. BOOLEAN dict_full; /* is dictionary full? */
  84. /* decode bytes->codeword state */
  85. DWORD bits_waiting; /* decode holder */
  86. UCHAR bits_remaining; /* # bits waiting in holder now */
  87. UCHAR *input_ptr;
  88. USHORT input_size;
  89. /* encode codeword->bytes state */
  90. DWORD bits_acc; /* encode accumulator */
  91. UCHAR bits_used; /* # bits packed in acc now */
  92. UCHAR *output_buffer; /* ptr to work buffer */
  93. UCHAR *output_ptr; /* current ptr into buffer */
  94. USHORT output_size; /* current work size */
  95. USHORT output_max; /* size of work buffer */
  96. /* i/o */
  97. void *push_context;
  98. //void (*push_func)(void *a, u_char *b, int c, int d);
  99. void (*push_func)();
  100. /* statistics for compressibility */
  101. DWORD bytes_in; /* total bytes input to compress */
  102. DWORD bytes_out; /* total bytes output from compress */
  103. long bits_out_other_mode; /* output if we were in other mode */
  104. long bits_out_this_mode; /* since last transition */
  105. USHORT bytes_since_last_check; /* since last compression test */
  106. UCHAR *OverFlowBuf;
  107. UCHAR OverFlowBytes;
  108. #define bits_out_if_compressed bits_out_other_mode
  109. #define bits_out_while_compressed bits_out_this_mode
  110. #define bits_out_if_transparent bits_out_other_mode
  111. #define bits_out_while_transparent bits_out_this_mode
  112. } v42bis_t;
  113. /*
  114. define hysteresis for compressed/transparent mode switch
  115. WINDOW_FULL defines how many bits we look at
  116. WINDOW_MIN_BITS is the min bits of difference required for a change
  117. */
  118. #define WINDOW_FULL(n) (n & 0xfffffc00) /* 1024 bits */
  119. #define WINDOW_MIN_BITS 16*N3 /* 128 bits */
  120. #define WINDOW_CHECK_BYTES 32 /* check every 32 */
  121. #ifdef DEBUG
  122. # define V_FLOW(s) if (state->debug_flow) logf s;
  123. # define EN_DEBUG(s) \
  124. if (state->debug_encode) { \
  125. logf_prefix(state->decode_only ? "decode: " : "encode: "); \
  126. logf s; }
  127. //# define EN_S_DEBUG(s) \
  128. // if (state->debug_encode > 1) { \
  129. // logf_prefix(state->decode_only ? "decode: " : "encode: "); \
  130. // logf s; }
  131. # define EN_S_DEBUG(s) \
  132. if (state->debug_encode > 1) { \
  133. DBGPRINT(DBG_COMP_RAS, DBG_LEVEL_ERR,s);
  134. # define EN_DEBUG_ON (state->debug_encode)
  135. # define DE_DEBUG(s) \
  136. if (state->debug_decode) { logf_prefix("decode: "); logf s; }
  137. # define DE_DEBUG_ON (state->debug_decode)
  138. # define E_DEBUG(s) if (state->debug_encode_bytes) logf s;
  139. # define D_DEBUG(s) if (state->debug_decode_bytes) logf s;
  140. #else
  141. # define V_FLOW(s) /* #s */
  142. # define EN_DEBUG(s) /* #s */
  143. # define DE_DEBUG(s) /* #s */
  144. # define E_DEBUG(s) /* #s */
  145. # define D_DEBUG(s) /* #s */
  146. # define EN_S_DEBUG(s)
  147. # define EN_DEBUG_ON FALSE
  148. # define DE_DEBUG_ON FALSE
  149. #endif
  150. /*
  151. * v42bis connection type
  152. */
  153. typedef struct {
  154. /* negotiated options */
  155. UCHAR neg_p0; /* negotiated value of p0 */
  156. USHORT neg_p1; /* negotiated value of p1 */
  157. UCHAR neg_p2; /* negotiated value of p2 */
  158. UCHAR default_p0; /* default value of p0 */
  159. USHORT default_p1; /* default value of p1 */
  160. #define MIN_P1 512
  161. #define DEF_P1 2048
  162. USHORT default_p2; /* default value of p2 */
  163. #define MIN_P2 6
  164. /*#define DEF_P2 8 */
  165. #define DEF_P2 250
  166. #define MAX_P2 250
  167. BOOLEAN compress_init_resp; /* comp. in initiator->responder dir */
  168. BOOLEAN compress_resp_init; /* comp. in responder->initiator dir */
  169. BOOLEAN got_p0; /* got negitated XID options */
  170. BOOLEAN got_p1;
  171. BOOLEAN got_p2;
  172. BOOLEAN got_unknown_p; /* got unknown option */
  173. v42bis_t encode; /* encode state */
  174. v42bis_t decode; /* decode state */
  175. } v42bis_connection_t;
  176. /* turn a "state" into a connection */
  177. #define CONN(s) ((v42bis_connection_t *)(s)->connection)
  178. #define PUT(ch) \
  179. { \
  180. if (state->output_size < state->output_max) \
  181. { \
  182. *state->output_ptr++ = (ch); \
  183. state->output_size++; \
  184. } \
  185. else \
  186. { \
  187. /* put this byte in the overflow buffer: we'll recover later */ \
  188. if (state == &((v42bis_connection_t *)state->connection)->decode) \
  189. { \
  190. *(state->OverFlowBuf + state->OverFlowBytes) = (ch); \
  191. state->OverFlowBytes++; \
  192. \
  193. ASSERT(state->OverFlowBytes <= MAX_P2); \
  194. } \
  195. \
  196. /* we don't have overflow buffer for encode side!! */ \
  197. else \
  198. { \
  199. DBGPRINT(DBG_COMP_RAS, DBG_LEVEL_ERR, \
  200. ("Arap v42bis: buf overflow on encode!! (%ld)\n", \
  201. state->output_size)); \
  202. \
  203. ASSERT(0); \
  204. } \
  205. } \
  206. }
  207. /* local routines */
  208. int decode_xid_params (v42bis_t *state, PUCHAR params, int len);
  209. DWORD v42bis_encode_codeword (v42bis_t *state, USHORT value);
  210. DWORD v42bis_c_error (v42bis_t *state, char *msg);
  211. DWORD v42bis_transition_to_compressed (v42bis_t *state);
  212. DWORD v42bis_transition_to_transparent (v42bis_t *state);
  213. DWORD v42bis_disconnect(v42bis_t *state, char *reason_string);
  214. DWORD v42bis_init_dictionary(v42bis_t *state);
  215. DWORD exit_handler( void );
  216. DWORD v42bis_init(v42bis_t *state);
  217. USHORT v42bis_decode_codeword(v42bis_t *state, UCHAR value);
  218. USHORT v42bis_decode_codeword_flush(v42bis_t *state);
  219. DWORD v42bis_encode_codeword_flush(v42bis_t *state);
  220. DWORD v42bis_encode_codeword_flush(v42bis_t *state);
  221. DWORD v42bis_encode_value(v42bis_t *state, USHORT value);
  222. DWORD v42bis_apply_compression_test(v42bis_t *state);
  223. DWORD v42bis_encode_buffer(v42bis_t *state, PUCHAR string, int insize);
  224. DWORD v42bis_encode_flush(v42bis_t *state);
  225. DWORD v42bis_signal_reset(v42bis_t *state);
  226. DWORD v42bis_decode_match(v42bis_t *state, USHORT codeword, int *psize, UCHAR *pRetChar);
  227. DWORD v42bis_decode_buffer(v42bis_t *state, PUCHAR data, int *pDataSize);
  228. DWORD v42bis_decode_flush(v42bis_t *state);
  229. DWORD v42bis_init_buffer(v42bis_t *state, PUCHAR buf, int size);
  230. DWORD v42bis_connection_init(v42bis_connection_t *conn);
  231. DWORD v42bis_connection_init_buffers(v42bis_connection_t *conn, PUCHAR e_buf,
  232. int e_size, PUCHAR d_buf, int d_size);
  233. DWORD v42bis_connection_init_push(v42bis_connection_t *conn, void *context,
  234. void (*e_push)(), void (*d_push)());