B64STRAW.CPP 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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/B64STRAW.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 : B64STRAW.CPP *
  26. * *
  27. * Programmer : Joe L. Bostic *
  28. * *
  29. * Start Date : 07/02/96 *
  30. * *
  31. * Last Update : July 3, 1996 [JLB] *
  32. * *
  33. *---------------------------------------------------------------------------------------------*
  34. * Functions: *
  35. * Base64Straw::Get -- Fetch data and convert it to/from base 64 encoding. *
  36. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  37. #include "b64straw.h"
  38. #include "base64.h"
  39. #include <string.h>
  40. /***********************************************************************************************
  41. * Base64Straw::Get -- Fetch data and convert it to/from base 64 encoding. *
  42. * *
  43. * This routine will fetch the number of bytes requested and perform any conversion as *
  44. * necessary upon the data. The nature of Base 64 encoding means that the data will *
  45. * increase in size by 30% when encoding and decrease in like manner when decoding. *
  46. * *
  47. * INPUT: source -- The buffer to hold the processed data. *
  48. * *
  49. * length -- The number of bytes requested. *
  50. * *
  51. * OUTPUT: Returns with the number of bytes stored into the buffer. If the number is less *
  52. * than requested, then this indicates that the data stream has been exhausted. *
  53. * *
  54. * WARNINGS: none *
  55. * *
  56. * HISTORY: *
  57. * 07/03/1996 JLB : Created. *
  58. *=============================================================================================*/
  59. int Base64Straw::Get(void * source, int 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. /*
  78. ** Process the byte request in code blocks until there are either
  79. ** no more source bytes available or the request has been fulfilled.
  80. */
  81. while (slen > 0) {
  82. /*
  83. ** Transfer any processed bytes available to the request buffer.
  84. */
  85. if (Counter > 0) {
  86. int len = (slen < Counter) ? slen : Counter;
  87. memmove(source, &to[tosize-Counter], len);
  88. Counter -= len;
  89. slen -= len;
  90. source = ((char *)source) + len;
  91. total += len;
  92. }
  93. if (slen == 0) break;
  94. /*
  95. ** More bytes are needed, so fetch and process another base 64 block.
  96. */
  97. int incount = Straw::Get(from, fromsize);
  98. if (Control == ENCODE) {
  99. Counter = Base64_Encode(from, incount, to, tosize);
  100. } else {
  101. Counter = Base64_Decode(from, incount, to, tosize);
  102. }
  103. if (Counter == 0) break;
  104. }
  105. return(total);
  106. }