BLOWPIPE.CPP 10 KB

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