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.

226 lines
5.6 KiB

  1. /************************************************************
  2. * uuencode/decode functions
  3. ************************************************************/
  4. //
  5. // Taken from NCSA HTTP and wwwlib.
  6. //
  7. // NOTE: These conform to RFC1113, which is slightly different then the Unix
  8. // uuencode and uudecode!
  9. //
  10. #include "stdafx.h"
  11. #include "uuencode.h"
  12. // general purpose dynamic buffer structure
  13. const int pr2six[256]={
  14. 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,
  15. 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,62,64,64,64,63,
  16. 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,
  17. 10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,64,64,64,64,64,64,26,27,
  18. 28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,
  19. 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,
  20. 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,
  21. 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,
  22. 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,
  23. 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,
  24. 64,64,64,64,64,64,64,64,64,64,64,64,64
  25. };
  26. char six2pr[64] = {
  27. 'A','B','C','D','E','F','G','H','I','J','K','L','M',
  28. 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
  29. 'a','b','c','d','e','f','g','h','i','j','k','l','m',
  30. 'n','o','p','q','r','s','t','u','v','w','x','y','z',
  31. '0','1','2','3','4','5','6','7','8','9','+','/'
  32. };
  33. PBYTE BufferQueryPtr( BUFFER * pB )
  34. {
  35. if(pB)
  36. {
  37. return pB->pBuf;
  38. }
  39. else
  40. {
  41. return NULL;
  42. }
  43. }
  44. BOOL CheckBufferSize( BUFFER *pB, DWORD cNewL )
  45. {
  46. PBYTE pN;
  47. if (!pB)
  48. {
  49. return FALSE;
  50. }
  51. if ( cNewL > pB->cLen )
  52. {
  53. return FALSE;
  54. }
  55. return TRUE;
  56. }
  57. BOOL
  58. uudecode(
  59. char * bufcoded,
  60. BUFFER * pbuffdecoded,
  61. DWORD * pcbDecoded )
  62. /*++
  63. Routine Description:
  64. uudecode a string of data
  65. Arguments:
  66. bufcoded pointer to uuencoded data
  67. pbuffdecoded pointer to output BUFFER structure
  68. pcbDecoded number of decode bytes
  69. Return Value:
  70. Returns TRUE is successful; otherwise FALSE is returned.
  71. --*/
  72. {
  73. int nbytesdecoded;
  74. char *bufin = bufcoded;
  75. unsigned char *bufout;
  76. int nprbytes;
  77. if(NULL == bufcoded ||
  78. NULL == pbuffdecoded ||
  79. NULL == pcbDecoded)
  80. {
  81. return FALSE;
  82. }
  83. /* Strip leading whitespace. */
  84. while(*bufcoded==' ' || *bufcoded == '\t') bufcoded++;
  85. /* Figure out how many characters are in the input buffer.
  86. * If this would decode into more bytes than would fit into
  87. * the output buffer, adjust the number of input bytes downwards.
  88. */
  89. bufin = bufcoded;
  90. while(pr2six[*(bufin++)] <= 63);
  91. nprbytes = bufin - bufcoded - 1;
  92. nbytesdecoded = ((nprbytes+3)/4) * 3;
  93. if ( !CheckBufferSize( pbuffdecoded, nbytesdecoded + 4 ))
  94. return FALSE;
  95. bufout = (unsigned char *) BufferQueryPtr(pbuffdecoded);
  96. if( NULL == bufout )
  97. return FALSE;
  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. if ( pcbDecoded )
  116. *pcbDecoded = nbytesdecoded;
  117. ((CHAR *)BufferQueryPtr(pbuffdecoded))[nbytesdecoded] = '\0';
  118. return TRUE;
  119. }
  120. BOOL
  121. uuencode(
  122. BYTE * bufin,
  123. DWORD nbytes,
  124. BUFFER * pbuffEncoded )
  125. /*++
  126. Routine Description:
  127. uuencode a string of data
  128. NOTE: bufin must be either a multiple of three, or be padded at the end to a multiple of three!
  129. Arguments:
  130. bufin pointer to data to encode
  131. nbytes number of bytes to encode
  132. pbuffEncoded pointer to output BUFFER structure
  133. Return Value:
  134. Returns TRUE is successful; otherwise FALSE is returned.
  135. --*/
  136. {
  137. unsigned char *outptr;
  138. unsigned int i;
  139. if(NULL == bufin ||
  140. NULL == pbuffEncoded)
  141. {
  142. return FALSE;
  143. }
  144. //
  145. // Check size the buffer to 133% of the incoming data
  146. //
  147. if ( !CheckBufferSize( pbuffEncoded, nbytes + ((nbytes + 3) / 3) + 4))
  148. return FALSE;
  149. outptr = (unsigned char *) BufferQueryPtr(pbuffEncoded);
  150. if( NULL == outptr )
  151. return FALSE;
  152. for (i = 0; i < nbytes; i += 3) {
  153. *(outptr++) = six2pr[*bufin >> 2]; /* c1 */
  154. *(outptr++) = six2pr[((*bufin << 4) & 060) | ((bufin[1] >> 4) & 017)]; /*c2*/
  155. *(outptr++) = six2pr[((bufin[1] << 2) & 074) | ((bufin[2] >> 6) & 03)];/*c3*/
  156. *(outptr++) = six2pr[bufin[2] & 077]; /* c4 */
  157. bufin += 3;
  158. }
  159. /* If nbytes was not a multiple of 3, then we have encoded too
  160. * many characters. Adjust appropriately.
  161. */
  162. if(i == nbytes+1) {
  163. /* There were only 2 bytes in that last group */
  164. outptr[-1] = '=';
  165. } else if(i == nbytes+2) {
  166. /* There was only 1 byte in that last group */
  167. outptr[-1] = '=';
  168. outptr[-2] = '=';
  169. }
  170. *outptr = '\0';
  171. return TRUE;
  172. }