Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

232 lines
7.3 KiB

  1. /*++
  2. Copyright (c) 1995 Microsoft Corporation
  3. Module Name:
  4. base64.cxx
  5. Abstract:
  6. This module supports functions for file caching for servers
  7. Author:
  8. Murali R. Krishnan ( MuraliK ) 11-Oct-1995
  9. Environment:
  10. Win32 Apps
  11. Project:
  12. Internet Services Common DLL
  13. Functions Exported:
  14. Revision History:
  15. Obtained from old inetsvcs.dll
  16. --*/
  17. #define INCL_INETSRV_INCS
  18. #include "smtpinc.h"
  19. #include <iis64.h>
  20. //
  21. // Taken from NCSA HTTP and wwwlib.
  22. //
  23. // NOTE: These conform to RFC1113, which is slightly different then the Unix
  24. // uuencode and uudecode!
  25. //
  26. const int _pr2six[256]={
  27. 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,
  28. 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,62,64,64,64,63,
  29. 52,53,54,55,56,57,58,59,60,61,64,64,64,64,64,64,64,0,1,2,3,4,5,6,7,8,9,
  30. 10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,64,64,64,64,64,64,26,27,
  31. 28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,
  32. 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,
  33. 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,
  34. 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,
  35. 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,
  36. 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,
  37. 64,64,64,64,64,64,64,64,64,64,64,64,64
  38. };
  39. char _six2pr[64] = {
  40. 'A','B','C','D','E','F','G','H','I','J','K','L','M',
  41. 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
  42. 'a','b','c','d','e','f','g','h','i','j','k','l','m',
  43. 'n','o','p','q','r','s','t','u','v','w','x','y','z',
  44. '0','1','2','3','4','5','6','7','8','9','+','/'
  45. };
  46. const int _pr2six64[256]={
  47. 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,
  48. 64,64,64,64,64,64,64,64, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15,
  49. 16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,
  50. 40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,
  51. 0,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,
  52. 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,
  53. 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,
  54. 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,
  55. 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,
  56. 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,
  57. 64,64,64,64,64,64,64,64,64,64,64,64,64
  58. };
  59. char _six2pr64[64] = {
  60. '`','!','"','#','$','%','&','\'','(',')','*','+',',',
  61. '-','.','/','0','1','2','3','4','5','6','7','8','9',
  62. ':',';','<','=','>','?','@','A','B','C','D','E','F',
  63. 'G','H','I','J','K','L','M','N','O','P','Q','R','S',
  64. 'T','U','V','W','X','Y','Z','[','\\',']','^','_'
  65. };
  66. BOOL uudecode(char * bufcoded,
  67. BUFFER * pbuffdecoded,
  68. DWORD * pcbDecoded,
  69. BOOL fBase64
  70. )
  71. {
  72. int nbytesin = 0;
  73. int nbytesdecoded;
  74. char *bufin = bufcoded;
  75. unsigned char *bufout;
  76. int nprbytes;
  77. int *pr2six = (int*)(fBase64 ? _pr2six64 : _pr2six);
  78. /*NimishK ** : If it aint divisible by 4 it aint base 64 */
  79. if(!fBase64)
  80. {
  81. nbytesin = lstrlen(bufcoded);
  82. if(nbytesin % 4)
  83. return FALSE;
  84. }
  85. /* Strip leading whitespace. */
  86. while(*bufcoded==' ' || *bufcoded == '\t') bufcoded++;
  87. /* Figure out how many characters are in the input buffer.
  88. * If this would decode into more bytes than would fit into
  89. * the output buffer, adjust the number of input bytes downwards.
  90. */
  91. bufin = bufcoded;
  92. while(pr2six[*(bufin++)] <= 63);
  93. nprbytes = DIFF(bufin - bufcoded) - 1;
  94. nbytesdecoded = ((nprbytes+3)/4) * 3;
  95. if ( !pbuffdecoded->Resize( nbytesdecoded + 4 ))
  96. return FALSE;
  97. bufout = (unsigned char *) pbuffdecoded->QueryPtr();
  98. bufin = bufcoded;
  99. while (nprbytes > 0) {
  100. *(bufout++) =
  101. (unsigned char) (pr2six[*bufin] << 2 | pr2six[bufin[1]] >> 4);
  102. *(bufout++) =
  103. (unsigned char) (pr2six[bufin[1]] << 4 | pr2six[bufin[2]] >> 2);
  104. *(bufout++) =
  105. (unsigned char) (pr2six[bufin[2]] << 6 | pr2six[bufin[3]]);
  106. bufin += 4;
  107. nprbytes -= 4;
  108. }
  109. if(nprbytes & 03) {
  110. if(pr2six[bufin[-2]] > 63)
  111. nbytesdecoded -= 2;
  112. else
  113. nbytesdecoded -= 1;
  114. }
  115. ((CHAR *)pbuffdecoded->QueryPtr())[nbytesdecoded] = '\0';
  116. if ( pcbDecoded )
  117. *pcbDecoded = nbytesdecoded;
  118. return TRUE;
  119. }
  120. //
  121. // NOTE NOTE NOTE
  122. // If the buffer length isn't a multiple of 3, we encode one extra byte beyond the
  123. // end of the buffer. This garbage byte is stripped off by the uudecode code, but
  124. // -IT HAS TO BE THERE- for uudecode to work. This applies not only our uudecode, but
  125. // to every uudecode() function that is based on the lib-www distribution [probably
  126. // a fairly large percentage].
  127. //
  128. BOOL uuencode( BYTE * bufin,
  129. DWORD nbytes,
  130. BUFFER * pbuffEncoded,
  131. BOOL fBase64 )
  132. {
  133. unsigned char *outptr;
  134. unsigned int i;
  135. char *six2pr = fBase64 ? _six2pr64 : _six2pr;
  136. BOOL fOneByteDiff = FALSE;
  137. BOOL fTwoByteDiff = FALSE;
  138. unsigned int iRemainder = 0;
  139. unsigned int iClosestMultOfThree = 0;
  140. //
  141. // Resize the buffer to 133% of the incoming data
  142. //
  143. if ( !pbuffEncoded->Resize( nbytes + ((nbytes + 3) / 3) + 4))
  144. return FALSE;
  145. outptr = (unsigned char *) pbuffEncoded->QueryPtr();
  146. iRemainder = nbytes % 3; //also works for nbytes == 1, 2
  147. fOneByteDiff = (iRemainder == 1 ? TRUE : FALSE);
  148. fTwoByteDiff = (iRemainder == 2 ? TRUE : FALSE);
  149. iClosestMultOfThree = ((nbytes - iRemainder)/3) * 3 ;
  150. //
  151. // Encode bytes in buffer up to multiple of 3 that is closest to nbytes.
  152. //
  153. for (i=0; i< iClosestMultOfThree ; i += 3) {
  154. *(outptr++) = six2pr[*bufin >> 2]; /* c1 */
  155. *(outptr++) = six2pr[((*bufin << 4) & 060) | ((bufin[1] >> 4) & 017)]; /*c2*/
  156. *(outptr++) = six2pr[((bufin[1] << 2) & 074) | ((bufin[2] >> 6) & 03)];/*c3*/
  157. *(outptr++) = six2pr[bufin[2] & 077]; /* c4 */
  158. bufin += 3;
  159. }
  160. //
  161. // We deal with trailing bytes by pretending that the input buffer has been padded with
  162. // zeros. Expressions are thus the same as above, but the second half drops off b'cos
  163. // ( a | ( b & 0) ) = ( a | 0 ) = a
  164. //
  165. if (fOneByteDiff)
  166. {
  167. *(outptr++) = six2pr[*bufin >> 2]; /* c1 */
  168. *(outptr++) = six2pr[((*bufin << 4) & 060)]; /* c2 */
  169. //pad with '='
  170. *(outptr++) = '='; /* c3 */
  171. *(outptr++) = '='; /* c4 */
  172. }
  173. else if (fTwoByteDiff)
  174. {
  175. *(outptr++) = six2pr[*bufin >> 2]; /* c1 */
  176. *(outptr++) = six2pr[((*bufin << 4) & 060) | ((bufin[1] >> 4) & 017)]; /*c2*/
  177. *(outptr++) = six2pr[((bufin[1] << 2) & 074)];/*c3*/
  178. //pad with '='
  179. *(outptr++) = '='; /* c4 */
  180. }
  181. //encoded buffer must be zero-terminated
  182. *outptr = '\0';
  183. return TRUE;
  184. }
  185. /************************ End of File ***********************/
  186.