Leaked source code of windows server 2003
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.

343 lines
7.3 KiB

  1. #include "precomp.h"
  2. DEBUG_FILEZONE(ZONE_T120_T123PSTN);
  3. /* PSTNFram.cpp
  4. *
  5. * Copyright (c) 1993-1995 by DataBeam Corporation, Lexington, KY
  6. *
  7. * Abstract:
  8. * This is the implementation file for the PSTN Frame class.
  9. *
  10. * Private Instance Variables:
  11. * All of these variables are used for decoding a packet.
  12. *
  13. * Source_Address - Address of source buffer
  14. * Source_Length - Length of source buffer
  15. *
  16. * Dest_Address - Address of destination buffer
  17. * Dest_Length - Length of destination buffer
  18. *
  19. * Source_Byte_Count - Running Source byte count
  20. * Dest_Byte_Count - Running Dest byte count
  21. *
  22. * Escape_Found - TRUE if the last byte decoded was an ESCAPE
  23. * First_Flag_Found - TRUE if the first flag has been found
  24. *
  25. * Caveats:
  26. * None.
  27. *
  28. * Authors:
  29. * James W. Lawwill
  30. */
  31. #include "pstnfram.h"
  32. /*
  33. * PSTNFrame::PSTNFrame (void)
  34. *
  35. * Public
  36. *
  37. * Functional Description:
  38. * PSTN Framer constructor. Initializes internal variable
  39. */
  40. PSTNFrame::PSTNFrame (void)
  41. {
  42. Source_Address = NULL;
  43. Source_Length = 0;
  44. Dest_Address = NULL;
  45. Dest_Length = 0;
  46. Source_Byte_Count = 0;
  47. Dest_Byte_Count = 0;
  48. Escape_Found = FALSE;
  49. First_Flag_Found = FALSE;
  50. }
  51. /*
  52. * PSTNFrame::~PSTNFrame(void)
  53. *
  54. * Public
  55. *
  56. * Functional Description:
  57. * PSTN Framer destructor. This routine does nothing
  58. */
  59. PSTNFrame::~PSTNFrame (void)
  60. {
  61. }
  62. /*
  63. * PacketFrameError PSTNFrame::PacketEncode (
  64. * LPBYTE source_address,
  65. * USHORT source_length,
  66. * LPBYTE dest_address,
  67. * USHORT dest_length,
  68. * BOOL prepend_flag,
  69. * BOOL append_flag,
  70. * USHORT * packet_size)
  71. *
  72. * Public
  73. *
  74. * Functional Description:
  75. * This function takes the passed in buffer and encodes it.
  76. */
  77. PacketFrameError PSTNFrame::PacketEncode (
  78. LPBYTE source_address,
  79. USHORT source_length,
  80. LPBYTE dest_address,
  81. USHORT dest_length,
  82. BOOL prepend_flag,
  83. BOOL append_flag,
  84. USHORT * packet_size)
  85. {
  86. UChar input_byte;
  87. USHORT byte_count;
  88. PacketFrameError return_value = PACKET_FRAME_NO_ERROR;
  89. /*
  90. ** If the prepend_flag is set, attach a flag to the dest buffer
  91. */
  92. if (prepend_flag)
  93. {
  94. *(dest_address++) = FLAG;
  95. *packet_size = 1;
  96. }
  97. else
  98. *packet_size = 0;
  99. byte_count = 0;
  100. /*
  101. ** Go thru each byte looking for a FLAG or an ESCAPE, encode this
  102. ** properly.
  103. */
  104. while ((byte_count < source_length) &&
  105. (return_value == PACKET_FRAME_NO_ERROR))
  106. {
  107. input_byte = *(source_address + byte_count);
  108. switch (input_byte)
  109. {
  110. case FLAG:
  111. case ESCAPE:
  112. /*
  113. ** If you find a FLAG or an ESCAPE, put an ESCAPE in the
  114. ** destination buffer and negate the 6th bit of the input_byte
  115. **
  116. */
  117. if (((*packet_size) + 2) > dest_length)
  118. {
  119. return_value = PACKET_FRAME_DEST_BUFFER_TOO_SMALL;
  120. break;
  121. }
  122. *(dest_address++) = ESCAPE;
  123. *(dest_address++) = input_byte & NEGATE_COMPLEMENT_BIT;
  124. *packet_size = (*packet_size) + 2;
  125. break;
  126. default:
  127. if (((*packet_size) + 1) > dest_length)
  128. {
  129. return_value = PACKET_FRAME_DEST_BUFFER_TOO_SMALL;
  130. break;
  131. }
  132. *(dest_address++) = input_byte;
  133. *packet_size = (*packet_size) + 1;
  134. break;
  135. }
  136. byte_count++;
  137. }
  138. /*
  139. ** Put a FLAG on the end of the packet
  140. */
  141. if (append_flag)
  142. {
  143. *(dest_address++) = FLAG;
  144. *packet_size = (*packet_size) + 1;
  145. }
  146. return (return_value);
  147. }
  148. /*
  149. * PacketFrameError PSTNFrame::PacketDecode (
  150. * LPBYTE source_address,
  151. * USHORT source_length,
  152. * LPBYTE dest_address,
  153. * USHORT dest_length,
  154. * USHORT * bytes_accepted,
  155. * USHORT * packet_size,
  156. * BOOL continue_packet)
  157. *
  158. * Public
  159. *
  160. * Functional Description:
  161. * This function decodes the input buffer looking for a packet.
  162. */
  163. PacketFrameError PSTNFrame::PacketDecode (
  164. LPBYTE source_address,
  165. USHORT source_length,
  166. LPBYTE dest_address,
  167. USHORT dest_length,
  168. USHORT * bytes_accepted,
  169. USHORT * packet_size,
  170. BOOL continue_packet)
  171. {
  172. UChar input_byte;
  173. PacketFrameError return_value = PACKET_FRAME_NO_ERROR;
  174. *bytes_accepted = 0;
  175. /*
  176. ** Source address is changing
  177. */
  178. if (source_address != NULL)
  179. {
  180. Source_Address = source_address;
  181. Source_Length = source_length;
  182. Source_Byte_Count = 0;
  183. }
  184. /*
  185. ** Destination address is changing
  186. */
  187. if (dest_address != NULL)
  188. {
  189. Dest_Address = dest_address;
  190. Dest_Length = dest_length;
  191. Dest_Byte_Count = 0;
  192. }
  193. /*
  194. ** Continue working on this packet?
  195. */
  196. if (continue_packet == FALSE)
  197. Escape_Found = FALSE;
  198. if (First_Flag_Found == FALSE)
  199. {
  200. /*
  201. ** Go thru the input data looking for a starting flag
  202. */
  203. while (Source_Byte_Count < Source_Length)
  204. {
  205. if (*(Source_Address + Source_Byte_Count) == FLAG)
  206. {
  207. First_Flag_Found = TRUE;
  208. Source_Byte_Count++;
  209. *bytes_accepted += 1;
  210. break;
  211. }
  212. Source_Byte_Count++;
  213. *bytes_accepted += 1;
  214. }
  215. }
  216. /*
  217. ** Go thru the input data stream looking for a FLAG or an ESCAPE
  218. */
  219. while ((Source_Byte_Count < Source_Length) &&
  220. (return_value == PACKET_FRAME_NO_ERROR))
  221. {
  222. input_byte = *(Source_Address + Source_Byte_Count);
  223. if (input_byte == FLAG)
  224. {
  225. /*
  226. ** End of packet found
  227. */
  228. Escape_Found = FALSE;
  229. Source_Byte_Count++;
  230. *bytes_accepted += 1;
  231. /*
  232. ** If we find a FLAG but the number of bytes in the dest buffer
  233. ** is 0, consider it the first flag in the packet and continue.
  234. */
  235. if (Dest_Byte_Count == 0)
  236. continue;
  237. else
  238. {
  239. /*
  240. ** End of packet found, set the packet size and break out
  241. */
  242. Dest_Address = NULL;
  243. *packet_size = Dest_Byte_Count;
  244. return_value = PACKET_FRAME_PACKET_DECODED;
  245. break;
  246. }
  247. }
  248. /*
  249. ** If the last byte was an ESCAPE, complement the 6th bit of the input
  250. ** byte and continue
  251. */
  252. if (Escape_Found)
  253. {
  254. input_byte ^= COMPLEMENT_BIT;
  255. Escape_Found = FALSE;
  256. }
  257. else
  258. {
  259. /*
  260. ** If the input byte is the ESCAPE, set the flag and continue.
  261. */
  262. if (input_byte == ESCAPE)
  263. {
  264. Escape_Found = TRUE;
  265. Source_Byte_Count++;
  266. *bytes_accepted += 1;
  267. continue;
  268. }
  269. }
  270. /*
  271. ** Put the input byte into our buffer.
  272. */
  273. if (Dest_Byte_Count < Dest_Length)
  274. {
  275. *(Dest_Address + Dest_Byte_Count) = input_byte;
  276. Dest_Byte_Count++;
  277. }
  278. else
  279. {
  280. First_Flag_Found = FALSE;
  281. return_value = PACKET_FRAME_DEST_BUFFER_TOO_SMALL;
  282. }
  283. Source_Byte_Count++;
  284. *bytes_accepted += 1;
  285. }
  286. return (return_value);
  287. }
  288. /*
  289. * void PSTNFrame::GetOverhead (
  290. * USHORT original_packet_size,
  291. * USHORT * max_packet_size)
  292. *
  293. * Public
  294. *
  295. * Functional Description:
  296. * This function gives the user some idea of the overhead added by this
  297. * process.
  298. */
  299. void PSTNFrame::GetOverhead (
  300. USHORT original_packet_size,
  301. USHORT * max_packet_size)
  302. {
  303. /*
  304. ** The overhead produced by this framer is 2 times the original packet
  305. ** size plus 2 bytes for the flags
  306. */
  307. *max_packet_size = (original_packet_size * 2) + 2;
  308. }