BLWSTRAW.CPP 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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/BLWSTRAW.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 : BLWSTRAW.CPP *
  22. * *
  23. * Programmer : Joe L. Bostic *
  24. * *
  25. * Start Date : 07/02/96 *
  26. * *
  27. * Last Update : July 3, 1996 [JLB] *
  28. * *
  29. *---------------------------------------------------------------------------------------------*
  30. * Functions: *
  31. * BlowStraw::Get -- Fetch a block of data from the straw. *
  32. * BlowStraw::Key -- Submit a key to the Blowfish straw. *
  33. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  34. #include "blwstraw.h"
  35. #include <string.h>
  36. #include <assert.h>
  37. /***********************************************************************************************
  38. * BlowStraw::Get -- Fetch a block of data from the straw. *
  39. * *
  40. * This routine will take a block of data from the straw and process it according to the *
  41. * encrypt/decrypt flag and the key supplied. Prior to a key be supplied, the data passes *
  42. * through this straw unchanged. *
  43. * *
  44. * INPUT: source -- Pointer to the buffer to hold the data being requested. *
  45. * *
  46. * length -- The length of the data being requested. *
  47. * *
  48. * OUTPUT: Returns with the actual number of bytes stored into the buffer. If the number *
  49. * returned is less than the number requested, then this indicates that the data *
  50. * source has been exhausted. *
  51. * *
  52. * WARNINGS: none *
  53. * *
  54. * HISTORY: *
  55. * 07/03/1996 JLB : Created. *
  56. *=============================================================================================*/
  57. int BlowStraw::Get(void * source, int slen)
  58. {
  59. /*
  60. ** Verify the parameter for legality.
  61. */
  62. if (source == NULL || slen <= 0) {
  63. return(0);
  64. }
  65. /*
  66. ** If there is no blowfish engine present, then merely pass the data through
  67. ** unchanged.
  68. */
  69. if (BF == NULL) {
  70. return(Straw::Get(source, slen));
  71. }
  72. int total = 0;
  73. while (slen > 0) {
  74. /*
  75. ** If there are any left over bytes in the buffer, pass them
  76. ** through first.
  77. */
  78. if (Counter > 0) {
  79. int sublen = (slen < Counter) ? slen : Counter;
  80. memmove(source, &Buffer[sizeof(Buffer)-Counter], sublen);
  81. Counter -= sublen;
  82. source = ((char *)source) + sublen;
  83. slen -= sublen;
  84. total += sublen;
  85. }
  86. if (slen == 0) break;
  87. /*
  88. ** Fetch and encrypt/decrypt the next block.
  89. */
  90. int incount = Straw::Get(Buffer, sizeof(Buffer));
  91. if (incount == 0) break;
  92. /*
  93. ** Only full blocks are processed. Partial blocks are
  94. ** merely passed through unchanged.
  95. */
  96. if (incount == sizeof(Buffer)) {
  97. if (Control == DECRYPT) {
  98. BF->Decrypt(Buffer, incount, Buffer);
  99. } else {
  100. BF->Encrypt(Buffer, incount, Buffer);
  101. }
  102. } else {
  103. memmove(&Buffer[sizeof(Buffer)-incount], Buffer, incount);
  104. }
  105. Counter = incount;
  106. }
  107. /*
  108. ** Return with the total number of bytes placed into the buffer.
  109. */
  110. return(total);
  111. }
  112. /***********************************************************************************************
  113. * BlowStraw::Key -- Submit a key to the Blowfish straw. *
  114. * *
  115. * This will take the key specified and use it to process the data that flows through this *
  116. * straw segment. Prior to a key being submitted, the data will flow through unchanged. *
  117. * *
  118. * INPUT: key -- Pointer to the key to submit. *
  119. * *
  120. * length-- The length of the key. The length must not exceed 56 bytes. *
  121. * *
  122. * OUTPUT: none *
  123. * *
  124. * WARNINGS: none *
  125. * *
  126. * HISTORY: *
  127. * 07/03/1996 JLB : Created. *
  128. *=============================================================================================*/
  129. void BlowStraw::Key(void const * key, int length)
  130. {
  131. /*
  132. ** Create the blowfish engine if one isn't already present.
  133. */
  134. if (BF == NULL) {
  135. BF = new BlowfishEngine;
  136. }
  137. assert(BF != NULL);
  138. if (BF != NULL) {
  139. BF->Submit_Key(key, length);
  140. }
  141. }