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.

349 lines
8.8 KiB

  1. /* File: C:\WACKER\xfer\hpr.c (Created: 25-Jan-1994)
  2. * created from HAWIN source file
  3. * hpr.c -- Functions common to HyperSend and HyperSave routines.
  4. *
  5. * Copyright 1989,1994 by Hilgraeve Inc. -- Monroe, MI
  6. * All rights reserved
  7. *
  8. * $Revision: 1 $
  9. * $Date: 10/05/98 1:16p $
  10. */
  11. #include <windows.h>
  12. #include <setjmp.h>
  13. #include <time.h>
  14. #include <term\res.h>
  15. #include <sys\types.h>
  16. #include <sys\utime.h>
  17. #include <tdll\stdtyp.h>
  18. #include <tdll\mc.h>
  19. #include <tdll\com.h>
  20. #include <tdll\session.h>
  21. #include <tdll\load_res.h>
  22. #include <tdll\xfer_msc.h>
  23. #include <tdll\globals.h>
  24. #include <tdll\file_io.h>
  25. #if !defined(BYTE)
  26. #define BYTE unsigned char
  27. #endif
  28. #include "cmprs.h"
  29. #include "xfr_dsp.h"
  30. #include "xfr_todo.h"
  31. #include "xfr_srvc.h"
  32. #include "xfer.h"
  33. #include "xfer.hh"
  34. #include "xfer_tsc.h"
  35. #include "hpr.h"
  36. #include "hpr.hh"
  37. #include "hpr_sd.hh"
  38. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  39. * Routines to handle building and sending of outgoing messages *
  40. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  41. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  42. * omsg_init
  43. *
  44. * DESCRIPTION:
  45. * Called before using any other omsg_ functions to provide the routines
  46. * with resources.
  47. *
  48. * ARGUMENTS:
  49. * bufr -- A pointer to a memory buffer that the omsg routines can
  50. * use to build messages. It must be large enough for the
  51. * largest message to be sent plus a size byte and two
  52. * check bytes.
  53. * size -- The size of bufr in bytes.
  54. * fPrintable -- TRUE if the message should be sent in printable form. If
  55. * this is TRUE, the only non-printable character sent as
  56. * a part of outgoing messages will be the initial SOH
  57. * character. The size and check bytes are converted to
  58. * printable characters.
  59. * sndfunc -- A pointer to a function that omsg_send can use to transmit
  60. * a message after it has been formatted. The function should
  61. * accepet a single character argument and return VOID.
  62. *
  63. * RETURNS:
  64. * nothing
  65. */
  66. void omsg_init(struct s_hc *hc, int fPrintable, int fEmbedMsg)
  67. {
  68. hc->omsg_printable = fPrintable;
  69. hc->omsg_embed = fEmbedMsg;
  70. omsg_setnum(hc, -1);
  71. }
  72. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  73. * omsg_new
  74. *
  75. * DESCRIPTION:
  76. * Begins formatting a new message. Messages are built up in pieces and
  77. * can be sent more than once. This function discards any old message
  78. * and sets up a new one containing no fields.
  79. *
  80. * ARGUMENTS:
  81. * type -- A single character type character to be used in the message
  82. * type field of the the new message.
  83. *
  84. * RETURNS:
  85. * nothing
  86. */
  87. void omsg_new(struct s_hc *hc, BYTE type)
  88. {
  89. hc->omsg_bufr[0] = type;
  90. hc->omsg_bufr[1] = ' ';
  91. hc->omsg_bufr[2] = ' ';
  92. hc->omsg_bufr[3] = '\0';
  93. }
  94. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  95. * omsg_add
  96. *
  97. * DESCRIPTION:
  98. * Adds a field to a message being built. A prior call to omsg_new will have
  99. * set up an empty message. One or more fields can then be added to the
  100. * message using this funtion. A semi-colon will automatically be appended
  101. * to the field.
  102. *
  103. * ARGUMENTS:
  104. * newfield - A text string containing the field to be added.
  105. *
  106. * RETURNS:
  107. * TRUE if field is added, FALSE if there is insufficient room in the
  108. * message buffer.
  109. */
  110. int omsg_add(struct s_hc *hc, BYTE *newfield)
  111. {
  112. if (strlen(hc->omsg_bufr) + strlen(newfield) > sizeof(hc->omsg_bufr) - 3)
  113. return(FALSE);
  114. strcat(hc->omsg_bufr, newfield);
  115. strcat(hc->omsg_bufr, ";");
  116. return(TRUE);
  117. }
  118. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  119. * omsg_setnum
  120. *
  121. * DESCRIPTION:
  122. * Messages are numbered consecutively as they are sent. This function
  123. * forces a the messages to start at a new number. Since message numbers
  124. * are incremented just before a message is sent, this funtion changes the
  125. * effective number of the last message sent. The next message out will have
  126. * a number one greater than the number specified in this function.
  127. *
  128. * ARGUMENTS:
  129. * n -- The new starting number for outgoing messages.
  130. *
  131. * RETURNS:
  132. * Returns the new message number as a convenience.
  133. */
  134. int omsg_setnum(struct s_hc *hc, int n)
  135. {
  136. return(hc->omsgn = n);
  137. }
  138. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  139. * omsg_send
  140. *
  141. * DESCRIPTION:
  142. * Completes a message and transmits it. The size and check fields of the
  143. * current message are computed and the message is transmitted according to
  144. * instructions. The message number is automatically incremented just before
  145. * transmission.
  146. *
  147. * ARGUMENTS:
  148. * burstcnt -- Number of identical copies of the message to send
  149. * usecrc -- If TRUE, the CRC calculation is used to calculate the
  150. * check bytes. If FALSE, a simple sum is used.
  151. * backspace -- If TRUE, each character out is followed by a backspace to
  152. * keep the messages from showing up on the remote computer
  153. * screen if they haven't started their transfer yet.
  154. *
  155. * RETURNS:
  156. * The number assigned to the message as it was transmitted.
  157. */
  158. int omsg_send(struct s_hc *hc, int burstcnt, int usecrc, int backspace)
  159. {
  160. HCOM hCom;
  161. register unsigned checksum;
  162. unsigned hold_crc = hc->h_crc; /* hold onto data crc & restore at end */
  163. int t;
  164. size_t sl;
  165. register size_t i;
  166. hCom = sessQueryComHdl(hc->hSession);
  167. hc->omsgn = (hc->omsgn + 1) % (hc->omsg_printable ? 94 : 256);
  168. sl = strlen(hc->omsg_bufr);
  169. /* len includes check bytes */
  170. hc->omsg_bufr[1] = (hc->omsg_printable ? tochar(sl) : (BYTE)sl);
  171. hc->omsg_bufr[2] = (hc->omsg_printable ?
  172. tochar(hc->omsgn) : (BYTE)hc->omsgn);
  173. hc->h_crc = checksum = 0;
  174. for (i = 0; i < sl; ++i)
  175. {
  176. checksum += hc->omsg_bufr[i];
  177. if (usecrc)
  178. h_crc_calc(hc, hc->omsg_bufr[i]);
  179. }
  180. if (hc->omsg_printable)
  181. {
  182. hc->omsg_bufr[sl] = (BYTE)tochar(checksum & 0x3F);
  183. hc->omsg_bufr[sl + 1] = (BYTE)tochar((checksum >> 6) & 0x3F);
  184. }
  185. else
  186. {
  187. hc->omsg_bufr[sl] = (BYTE)((usecrc ? hc->h_crc : checksum) % 256);
  188. hc->omsg_bufr[sl + 1] = (BYTE)((usecrc ? hc->h_crc : checksum) / 256);
  189. }
  190. for (t = 0; t < burstcnt; ++t)
  191. {
  192. if (hc->omsg_embed)
  193. {
  194. hs_xmit_(hc, H_MSGCHAR);
  195. if (backspace)
  196. hs_xmit_(hc, '\b');
  197. for(i = 0; i < sl + 2; ++i)
  198. {
  199. if (hc->omsg_bufr[i] == H_MSGCHAR)
  200. {
  201. hs_xmit_(hc, hc->omsg_bufr[i]);
  202. if (backspace)
  203. hs_xmit_(hc, '\b');
  204. }
  205. hs_xmit_(hc, hc->omsg_bufr[i]);
  206. if (backspace)
  207. hs_xmit_(hc, '\b');
  208. }
  209. }
  210. else
  211. {
  212. ComSendChar(hCom, H_MSGCHAR);
  213. if (backspace)
  214. ComSendChar(hCom, '\b');
  215. for(i = 0; i < sl + 2; ++i)
  216. {
  217. if (hc->omsg_bufr[i] == H_MSGCHAR)
  218. {
  219. ComSendChar(hCom, hc->omsg_bufr[i]);
  220. if (backspace)
  221. ComSendChar(hCom, '\b');
  222. }
  223. ComSendChar(hCom, hc->omsg_bufr[i]);
  224. if (backspace)
  225. ComSendChar(hCom, '\b');
  226. }
  227. }
  228. }
  229. if (hc->omsg_embed)
  230. {
  231. if (backspace)
  232. {
  233. hs_xmit_(hc, ' ');
  234. hs_xmit_(hc, '\b');
  235. }
  236. }
  237. else
  238. {
  239. if (backspace)
  240. {
  241. ComSendChar(hCom, ' ');
  242. ComSendChar(hCom, '\b');
  243. }
  244. ComSendWait(hCom);
  245. }
  246. hc->last_omsg = startinterval();
  247. hc->omsg_bufr[sl] = '\0';
  248. hc->h_crc = hold_crc;
  249. return(hc->omsgn);
  250. }
  251. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  252. * omsg_last
  253. *
  254. * DESCRIPTION:
  255. * Returns the time that the last message was sent in a form suitable to
  256. * use with interval(). Passing the returned value to interval() will give
  257. * the time in tenths of a second since the last message was sent.
  258. *
  259. * ARGUMENTS:
  260. * none
  261. *
  262. * RETURNS:
  263. * nothing
  264. */
  265. long omsg_last(struct s_hc *hc)
  266. {
  267. return(hc->last_omsg);
  268. }
  269. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  270. * omsg_number
  271. *
  272. * DESCRIPTION:
  273. * Returns the message number of the last message sent. The value will be
  274. * -1 if no messages have been sent.
  275. *
  276. * ARGUMENTS:
  277. * none
  278. *
  279. * RETURNS:
  280. * The number of the last message.
  281. */
  282. int omsg_number(struct s_hc *hc)
  283. {
  284. return(hc->omsgn);
  285. }
  286. #if FALSE /* this is a 'C' version of the code in hpr_calc.asm */
  287. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  288. * h_crc_calc
  289. *
  290. * DESCRIPTION:
  291. * Does byte-by-byte CRC calcuation for HyperProtocol
  292. *
  293. * ARGUMENTS:
  294. * cc -- Next character to include in CRC calculation. The global value
  295. * h_crc is modified to include the effects of cc
  296. *
  297. * RETURNS:
  298. * nothing
  299. */
  300. void NEAR h_crc_calc(uchar cc)
  301. {
  302. register unsigned q;
  303. q = (h_crc ^ cc) & 0x0F;
  304. h_crc = (h_crc >> 4) ^ (q * 0x1081);
  305. q = (h_crc ^ (cc >> 4)) & 0x0F;
  306. h_crc = (h_crc >> 4) ^ (q * 0x1081);
  307. }
  308. #endif
  309. /********************* end of hpr.c ***********************/