B64PIPE.CPP 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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/B64PIPE.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 : B64PIPE.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. * Base64Pipe::Put -- Processes a block of data through the pipe. *
  32. * Base64Pipe::Flush -- Flushes the final pending data through the pipe. *
  33. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  34. #include "b64pipe.h"
  35. #include "base64.h"
  36. #include <string.h>
  37. /***********************************************************************************************
  38. * Base64Pipe::Put -- Processes a block of data through the pipe. *
  39. * *
  40. * This will take the data submitted and either Base64 encode or decode it (as specified *
  41. * in the pipe's constructor). The nature of Base64 encoding means that the data will *
  42. * grow 30% in size when encoding and decrease by a like amount when decoding. *
  43. * *
  44. * INPUT: source -- Pointer to the data to be translated. *
  45. * *
  46. * length -- The number of bytes to translate. *
  47. * *
  48. * OUTPUT: Returns with the actual number of bytes output at the far distant final end of *
  49. * the pipe chain. *
  50. * *
  51. * WARNINGS: none *
  52. * *
  53. * HISTORY: *
  54. * 07/03/1996 JLB : Created. *
  55. *=============================================================================================*/
  56. int Base64Pipe::Put(void const * source, int slen)
  57. {
  58. if (source == NULL || slen < 1) {
  59. return(Pipe::Put(source, slen));
  60. }
  61. int total = 0;
  62. char * from;
  63. int fromsize;
  64. char * to;
  65. int tosize;
  66. if (Control == ENCODE) {
  67. from = PBuffer;
  68. fromsize = sizeof(PBuffer);
  69. to = CBuffer;
  70. tosize = sizeof(CBuffer);
  71. } else {
  72. from = CBuffer;
  73. fromsize = sizeof(CBuffer);
  74. to = PBuffer;
  75. tosize = sizeof(PBuffer);
  76. }
  77. if (Counter > 0) {
  78. int len = (slen < (fromsize-Counter)) ? slen : (fromsize-Counter);
  79. memmove(&from[Counter], source, len);
  80. Counter += len;
  81. slen -= len;
  82. source = ((char *)source) + len;
  83. if (Counter == fromsize) {
  84. int outcount;
  85. if (Control == ENCODE) {
  86. outcount = Base64_Encode(from, fromsize, to, tosize);
  87. } else {
  88. outcount = Base64_Decode(from, fromsize, to, tosize);
  89. }
  90. total += Pipe::Put(to, outcount);
  91. Counter = 0;
  92. }
  93. }
  94. while (slen >= fromsize) {
  95. int outcount;
  96. if (Control == ENCODE) {
  97. outcount = Base64_Encode(source, fromsize, to, tosize);
  98. } else {
  99. outcount = Base64_Decode(source, fromsize, to, tosize);
  100. }
  101. source = ((char *)source) + fromsize;
  102. total += Pipe::Put(to, outcount);
  103. slen -= fromsize;
  104. }
  105. if (slen > 0) {
  106. memmove(from, source, slen);
  107. Counter = slen;
  108. }
  109. return(total);
  110. }
  111. /***********************************************************************************************
  112. * Base64Pipe::Flush -- Flushes the final pending data through the pipe. *
  113. * *
  114. * If there is any non-processed data accumulated in the holding buffer (quite likely when *
  115. * encoding), then it will be processed and flushed out the end of the pipe. *
  116. * *
  117. * INPUT: none *
  118. * *
  119. * OUTPUT: Returns with the number of bytes output at the far distant final end of the pipe *
  120. * chain. *
  121. * *
  122. * WARNINGS: none *
  123. * *
  124. * HISTORY: *
  125. * 07/03/1996 JLB : Created. *
  126. *=============================================================================================*/
  127. int Base64Pipe::Flush(void)
  128. {
  129. int len = 0;
  130. if (Counter) {
  131. if (Control == ENCODE) {
  132. int chars = Base64_Encode(PBuffer, Counter, CBuffer, sizeof(CBuffer));
  133. len += Pipe::Put(CBuffer, chars);
  134. } else {
  135. int chars = Base64_Decode(CBuffer, Counter, PBuffer, sizeof(PBuffer));
  136. len += Pipe::Put(PBuffer, chars);
  137. }
  138. Counter = 0;
  139. }
  140. len += Pipe::Flush();
  141. return(len);
  142. }