b64pipe.cpp 7.6 KB

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