BLOWPIPE.CPP 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. //
  2. // Copyright 2020 Electronic Arts Inc.
  3. //
  4. // TiberianDawn.DLL and RedAlert.dll and corresponding source code is free
  5. // software: you can redistribute it and/or modify it under the terms of
  6. // the GNU General Public License as published by the Free Software Foundation,
  7. // either version 3 of the License, or (at your option) any later version.
  8. // TiberianDawn.DLL and RedAlert.dll and corresponding source code is distributed
  9. // in the hope that it will be useful, but with permitted additional restrictions
  10. // under Section 7 of the GPL. See the GNU General Public License in LICENSE.TXT
  11. // distributed with this program. You should have received a copy of the
  12. // GNU General Public License along with permitted additional restrictions
  13. // with this program. If not, see https://github.com/electronicarts/CnC_Remastered_Collection
  14. /* $Header: /CounterStrike/BLOWPIPE.CPP 1 3/03/97 10:24a Joe_bostic $ */
  15. /***********************************************************************************************
  16. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  17. ***********************************************************************************************
  18. * *
  19. * Project Name : Command & Conquer *
  20. * *
  21. * File Name : BLOWPIPE.CPP *
  22. * *
  23. * Programmer : Joe L. Bostic *
  24. * *
  25. * Start Date : 06/30/96 *
  26. * *
  27. * Last Update : July 3, 1996 [JLB] *
  28. * *
  29. *---------------------------------------------------------------------------------------------*
  30. * Functions: *
  31. * BlowPipe::Flush -- Flushes any pending data out the pipe. *
  32. * BlowPipe::Key -- Submit a key to the blowfish pipe handler. *
  33. * BlowPipe::Put -- Submit a block of data for encrypt/decrypt. *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "blowpipe.h"
  36. #include <string.h>
  37. #include <assert.h>
  38. /***********************************************************************************************
  39. * BlowPipe::Flush -- Flushes any pending data out the pipe. *
  40. * *
  41. * If there is any pending data in the holding buffer, then this routine will force it to *
  42. * be flushed out the end of the pipe. *
  43. * *
  44. * INPUT: none *
  45. * *
  46. * OUTPUT: Returns with the actual number of bytes output at the end final distant pipe *
  47. * segment in the chain. *
  48. * *
  49. * WARNINGS: none *
  50. * *
  51. * HISTORY: *
  52. * 07/03/1996 JLB : Created. *
  53. *=============================================================================================*/
  54. int BlowPipe::Flush(void)
  55. {
  56. int total = 0;
  57. if (Counter > 0 && BF != NULL) {
  58. total += Pipe::Put(Buffer, Counter);
  59. }
  60. Counter = 0;
  61. total += Pipe::Flush();
  62. return(total);
  63. }
  64. /***********************************************************************************************
  65. * BlowPipe::Put -- Submit a block of data for encrypt/decrypt. *
  66. * *
  67. * This will take the data block specified and process it before passing it on to the next *
  68. * link in the pipe chain. A key must be submitted before this routine will actually perform*
  69. * any processing. Prior to key submission, the data is passed through unchanged. *
  70. * *
  71. * INPUT: source -- Pointer to the buffer that contains the data to pass through. *
  72. * *
  73. * length -- The length of the data in the buffer. *
  74. * *
  75. * OUTPUT: Returns with then actual number of bytes output at the final distant end link in *
  76. * the pipe chain. *
  77. * *
  78. * WARNINGS: none *
  79. * *
  80. * HISTORY: *
  81. * 07/03/1996 JLB : Created. *
  82. *=============================================================================================*/
  83. int BlowPipe::Put(void const * source, int slen)
  84. {
  85. if (source == NULL || slen < 1) {
  86. return(Pipe::Put(source, slen));
  87. }
  88. /*
  89. ** If there is no blowfish engine present, then merely pass the data through
  90. ** unchanged in any way.
  91. */
  92. if (BF == NULL) {
  93. return(Pipe::Put(source, slen));
  94. }
  95. int total = 0;
  96. /*
  97. ** If there is a partial block accumulated, then tag on the new data to
  98. ** this block and process it if the block is full. Proceed with the bulk
  99. ** processing if there are any left over bytes from this step. This step
  100. ** can be skipped if there are no pending bytes in the buffer.
  101. */
  102. if (Counter) {
  103. int sublen = ((int)sizeof(Buffer)-Counter < slen) ? (sizeof(Buffer)-Counter) : slen;
  104. memmove(&Buffer[Counter], source, sublen);
  105. Counter += sublen;
  106. source = ((char *)source) + sublen;
  107. slen -= sublen;
  108. if (Counter == sizeof(Buffer)) {
  109. if (Control == DECRYPT) {
  110. BF->Decrypt(Buffer, sizeof(Buffer), Buffer);
  111. } else {
  112. BF->Encrypt(Buffer, sizeof(Buffer), Buffer);
  113. }
  114. total += Pipe::Put(Buffer, sizeof(Buffer));
  115. Counter = 0;
  116. }
  117. }
  118. /*
  119. ** Process the input data in blocks until there is not enough
  120. ** source data to fill a full block of data.
  121. */
  122. while (slen >= sizeof(Buffer)) {
  123. if (Control == DECRYPT) {
  124. BF->Decrypt(source, sizeof(Buffer), Buffer);
  125. } else {
  126. BF->Encrypt(source, sizeof(Buffer), Buffer);
  127. }
  128. total += Pipe::Put(Buffer, sizeof(Buffer));
  129. source = ((char *)source) + sizeof(Buffer);
  130. slen -= sizeof(Buffer);
  131. }
  132. /*
  133. ** If there are any left over bytes, then they must be less than the size of
  134. ** the staging buffer. Store the bytes in the staging buffer for later
  135. ** processing.
  136. */
  137. if (slen > 0) {
  138. memmove(Buffer, source, slen);
  139. Counter = slen;
  140. }
  141. /*
  142. ** Return with the total number of bytes flushed out to the final end of the
  143. ** pipe chain.
  144. */
  145. return(total);
  146. }
  147. /***********************************************************************************************
  148. * BlowPipe::Key -- Submit a key to the blowfish pipe handler. *
  149. * *
  150. * This routine will take the key provided and use it to process the data that passes *
  151. * through this pipe. Prior to a key being submitted, the data passes through the pipe *
  152. * unchanged. *
  153. * *
  154. * INPUT: key -- Pointer to the key data to use. *
  155. * *
  156. * length-- The length of the key. The key length must not be greater than 56 bytes. *
  157. * *
  158. * OUTPUT: none *
  159. * *
  160. * WARNINGS: none *
  161. * *
  162. * HISTORY: *
  163. * 07/03/1996 JLB : Created. *
  164. *=============================================================================================*/
  165. void BlowPipe::Key(void const * key, int length)
  166. {
  167. /*
  168. ** Create the blowfish engine if one isn't already present.
  169. */
  170. if (BF == NULL) {
  171. BF = new BlowfishEngine;
  172. }
  173. assert(BF != NULL);
  174. if (BF != NULL) {
  175. BF->Submit_Key(key, length);
  176. }
  177. }