#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#define MAX_LINELEN 45
#define LINE_BUF_SIZE 62
#define UUDECODE_READ_SIZE 62
#define ENCODE_BYTE(b) (((b) == 0) ? 0x60 : ((b) + 0x20))
#define DECODE_BYTE(b) ((b == 0x60) ? 0 : b - 0x20)
typedef unsigned char BYTE;
int main (int argc, char *argv [])
{
if (argc!=3)
{
printf("Usage:%s src_file target.uue\n",argv[0]);
return 0;
}
FILE *infile = NULL;
FILE *outfile = NULL;
int linelen;
int linecnt;
BYTE inbuf [MAX_LINELEN];
BYTE *inbytep;
char outbuf [5];
infile = fopen (argv[1], "rb");
outfile = fopen (argv[2], "wt");
do
{
linelen = fread (inbuf, 1, MAX_LINELEN, infile);
/* Write the line length byte */
fputc (ENCODE_BYTE (linelen), outfile);
/* Encode the line */
for (linecnt = linelen, inbytep = inbuf;
linecnt > 0;
linecnt -= 3, inbytep += 3)
{
/* Encode 3 bytes from the input buffer */
outbuf [0] = ENCODE_BYTE ((inbytep [0] & 0xFC) >> 2);
outbuf [1] = ENCODE_BYTE (((inbytep [0] & 0x03) << 4) +
((inbytep [1] & 0xF0) >> 4));
outbuf [2] = ENCODE_BYTE (((inbytep [1] & 0x0F) << 2) +
((inbytep [2] & 0xC0) >> 6));
outbuf [3] = ENCODE_BYTE (inbytep [2] & 0x3F);
outbuf [4] = '\0';
/* Write the 4 encoded bytes to the file */
fprintf (outfile, "%s", outbuf);
}
fprintf (outfile, "\n");
} while (linelen != 0);
fclose (infile);
return 0;
}
#define MAX_LINELEN 45
#define ENCODE_BYTE(b) (((b) == 0) ? 0x60 : ((b) + 0x20))
int UUDecode(char *pSrc, long ulLen, char*pDest)
{
int offset=0; //转换输出偏移
int cpOffSet = 0;//拷贝偏移
char linebuf[LINE_BUF_SIZE];
char *linep = NULL;
char *tempcp= NULL;
int linelen= 0;
int linecnt= 0;
unsigned char outbyte[3];
//dprintf("@@@-@@@-@@@ UUDecode InBuf Length: %d", strlen(pSrc));
do
{
//每次都拷贝62字节
//因为编码时取45字节转成60并写入分隔符2字节
if(ulLen >= 0)
{
ulLen -= UUDECODE_READ_SIZE;
}
else
{
break;
}
//取62Bytes
memcpy(linebuf, pSrc + cpOffSet, UUDECODE_READ_SIZE);
cpOffSet += UUDECODE_READ_SIZE;
//解码后长度
linelen = DECODE_BYTE (linebuf [0]);
linep = linebuf + 1;
for (linecnt = linelen; linecnt > 0; linecnt -= 3, linep += 4)
{
if ((linep [0] == '\0') || (linep [1] == '\0') ||
(linep [2] == '\0') || (linep [3] == '\0'))
{
printf("found 0,offset %d",offset);
}
outbyte [0] = DECODE_BYTE (linep [0]);
outbyte [1] = DECODE_BYTE (linep [1]);
outbyte [0] <<= 2;
outbyte [0] |= (outbyte [1] >> 4) & 0x03;
outbyte [1] <<= 4;
outbyte [2] = DECODE_BYTE (linep [2]);
outbyte [1] |= (outbyte [2] >> 2) & 0x0F;
outbyte [2] <<= 6;
outbyte [2] |= DECODE_BYTE (linep [3]) & 0x3F;
//写回Buffer
if (linecnt > 3)
{
memcpy(pDest + offset,outbyte,3);
offset += 3;
}
else
{
memcpy(pDest + offset,outbyte,linecnt);
offset += linecnt;
linecnt = 3;
}
}
} while (linelen != 0);
//置结束标志
pDest[offset] = '\0';
return offset;
}
int UUDecode(const char *pSrc, long ulLen, unsigned char*pDest)
{
int offset=0; //转换输出偏移
int cpOffSet = 0;//拷贝偏移
char linebuf[LINE_BUF_SIZE];
char *linep = NULL;
char *tempcp= NULL;
int linelen= 0;
int linecnt= 0;
unsigned char outbyte[3];
//UUMemCopy(pDest, pSrc, ulLen);
do
{
//每次都拷贝62字节
//因为编码时取45字节转成60并写入分隔符2字节
if(ulLen >= 0)
{
ulLen -= UUDECODE_READ_SIZE;
}
else
{
break;
}
//取62Bytes
memcpy(linebuf, pSrc + cpOffSet, UUDECODE_READ_SIZE);
cpOffSet += UUDECODE_READ_SIZE;
//解码后长度
linelen = DECODE_BYTE (linebuf [0]);
linep = linebuf + 1;
for (linecnt = linelen; linecnt > 0; linecnt -= 3, linep += 4)
{
outbyte [0] = DECODE_BYTE (linep [0]);
outbyte [1] = DECODE_BYTE (linep [1]);
outbyte [0] <<= 2;
outbyte [0] |= (outbyte [1] >> 4) & 0x03;
outbyte [1] <<= 4;
outbyte [2] = DECODE_BYTE (linep [2]);
outbyte [1] |= (outbyte [2] >> 2) & 0x0F;
outbyte [2] <<= 6;
outbyte [2] |= DECODE_BYTE (linep [3]) & 0x3F;
//写回Buffer
if (linecnt > 3)
{
memcpy(pDest + offset,outbyte,3);
offset += 3;
}
else
{
memcpy(pDest + offset,outbyte,linecnt);
offset += linecnt;
linecnt = 3;
}
}
} while (linelen != 0);
//置结束标志
memset(pDest + offset, 0, 1 );
return offset;
}