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.

179 lines
5.2 KiB

  1. //+----------------------------------------------------------------------------
  2. //
  3. // File: uufuncs.cpp
  4. //
  5. // Module: CMSECURE.LIB
  6. //
  7. // Synopsis: uuencode and uudecode support
  8. //
  9. // Copyright (c) 1994-1998 Microsoft Corporation
  10. //
  11. // Author: quintinb created header 08/18/99
  12. //
  13. //+----------------------------------------------------------------------------
  14. #include <windows.h>
  15. #include "cmuufns.h"
  16. #include "cmdebug.h"
  17. //
  18. // Taken from NCSA HTTP and wwwlib.
  19. //
  20. // NOTE: These conform to RFC1113, which is slightly different then the Unix
  21. // uuencode and uudecode!
  22. //
  23. static const int pr2six[256]={
  24. 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,
  25. 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,62,64,64,64,63,
  26. 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,
  27. 10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,64,64,64,64,64,64,26,27,
  28. 28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,
  29. 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,
  30. 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,
  31. 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,
  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
  35. };
  36. static const char six2pr[64] = {
  37. 'A','B','C','D','E','F','G','H','I','J','K','L','M',
  38. 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
  39. 'a','b','c','d','e','f','g','h','i','j','k','l','m',
  40. 'n','o','p','q','r','s','t','u','v','w','x','y','z',
  41. '0','1','2','3','4','5','6','7','8','9','+','/'
  42. };
  43. BOOL uudecode(
  44. const char * bufcoded,
  45. CHAR * pbuffdecoded,
  46. LPDWORD pcbDecoded )
  47. {
  48. DWORD nbytesdecoded;
  49. const char *bufin = bufcoded;
  50. unsigned char *bufout;
  51. INT32 nprbytes;
  52. MYDBGASSERT(pcbDecoded);
  53. if (!pcbDecoded)
  54. return FALSE;
  55. /* Strip leading whitespace. */
  56. while(*bufcoded==' ' || *bufcoded == '\t') bufcoded++;
  57. /* Figure out how many characters are in the input buffer.
  58. * If this would decode into more bytes than would fit into
  59. * the output buffer, adjust the number of input bytes downwards.
  60. */
  61. bufin = bufcoded;
  62. while(pr2six[*(bufin++)] <= 63);
  63. nprbytes = (INT32)(bufin - bufcoded - 1);
  64. nbytesdecoded = ((nprbytes+3)/4) * 3;
  65. if (*pcbDecoded < (nbytesdecoded + 4 ))
  66. return FALSE;
  67. bufout = (unsigned char *) pbuffdecoded;
  68. bufin = bufcoded;
  69. while (nprbytes > 0)
  70. {
  71. *(bufout++) =
  72. (unsigned char) (pr2six[*bufin] << 2 | pr2six[bufin[1]] >> 4);
  73. *(bufout++) =
  74. (unsigned char) (pr2six[bufin[1]] << 4 | pr2six[bufin[2]] >> 2);
  75. *(bufout++) =
  76. (unsigned char) (pr2six[bufin[2]] << 6 | pr2six[bufin[3]]);
  77. bufin += 4;
  78. nprbytes -= 4;
  79. }
  80. if (nprbytes & 03)
  81. {
  82. if (pr2six[bufin[-2]] > 63)
  83. nbytesdecoded -= 2;
  84. else
  85. nbytesdecoded -= 1;
  86. }
  87. *pcbDecoded = nbytesdecoded;
  88. pbuffdecoded[nbytesdecoded] = '\0';
  89. return TRUE;
  90. }
  91. BOOL uuencode( const BYTE* bufin,
  92. DWORD nbytes,
  93. CHAR * pbuffEncoded,
  94. DWORD outbufmax)
  95. {
  96. MYDBGASSERT(!IsBadReadPtr(bufin, nbytes));
  97. MYDBGASSERT(!IsBadWritePtr(pbuffEncoded, outbufmax));
  98. unsigned char *outptr;
  99. unsigned int i;
  100. //
  101. // Resize the buffer to 133% of the incoming data
  102. //
  103. if (outbufmax < (nbytes + ((nbytes + 3) / 3) + 4))
  104. {
  105. CMASSERTMSG(FALSE, "The outputbuf for uuencode is not large enough");
  106. return FALSE;
  107. }
  108. outptr = (unsigned char *) pbuffEncoded;
  109. //
  110. // Encode 3 byte at a time
  111. //
  112. for (i=0; i<(nbytes/3)*3; i += 3)
  113. {
  114. *(outptr++) = six2pr[bufin[i] >> 2]; /* c1 */
  115. *(outptr++) = six2pr[((bufin[i] << 4) & 060) | ((bufin[i+1] >> 4) & 017)]; /*c2*/
  116. *(outptr++) = six2pr[((bufin[i+1] << 2) & 074) | ((bufin[i+2] >> 6) & 03)];/*c3*/
  117. *(outptr++) = six2pr[bufin[i+2] & 077]; /* c4 */
  118. }
  119. /* If nbytes was not a multiple of 3, then we have encoded too
  120. * many characters. Adjust appropriately.
  121. */
  122. if (i+2 == nbytes)
  123. {
  124. /* There were only 2 bytes in that last group */
  125. *(outptr++) = six2pr[bufin[i] >> 2]; /* c1 */
  126. *(outptr++) = six2pr[((bufin[i] << 4) & 060) | ((bufin[i+1] >> 4) & 017)]; /*c2*/
  127. *(outptr++) = six2pr[(bufin[i+1] << 2) & 074];/*c3*/
  128. *(outptr++) = '='; /* c4 */
  129. }
  130. else
  131. {
  132. if (i+1 == nbytes)
  133. {
  134. /* There was only 1 byte in that last group */
  135. *(outptr++) = six2pr[bufin[i] >> 2]; /* c1 */
  136. *(outptr++) = six2pr[(bufin[i] << 4) & 060]; /*c2*/
  137. *(outptr++) = '='; /*c3*/
  138. *(outptr++) = '='; /*c4*/
  139. }
  140. else
  141. {
  142. MYDBGASSERT(i == nbytes);
  143. }
  144. }
  145. *outptr = '\0';
  146. return TRUE;
  147. }