//UU.h #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 #ifndef UUh #define UUh /* Encodes binary files to Unix to Unix (UU) formatted Text and Decodes Unix to Unix (UU) formatted Text to the original binary file. Implements UUEncode and UUDecode functionality. This encoding is used to convert binary files into text files so that they can be sent through e-mail systems. The encoded data is larger than the original data. Any one encoded line of text will not exceed 62 characters. Base64 produces smaller encoded files than Unix to Unix (UU). Usage: CUU::Encode("c:\\tJpg.uu","c:\\t.Jpg" ); CUU::Decode("c:\\t1.Jpg" ,"c:\\tJpg.uu"); (Note that WinZip extracts .uu files) Unix-to-Unix(UU) encode/decode algorithm: uuencoding takes 24 bit from input stream (3 bytes) and splits it into 32 bits (4 bytes) output. Each output byte has the top 2 bits clear, and can be translated into a printable character. The traditional way is to simply add $20 (space). uudecoding simply subtracts $20, strips the top 2 bits from input, and puts things back together. */ class CUU { public: CUU() {} virtual ~CUU() {} // Don't allow canonical behavior (i.e. don't allow this class to be passed by value) CUU(const CUU&) {}; CUU& operator=(const CUU&) {return(*this);}; static BYTE Decode(BYTE c) {return (c-0x20) & 0x3F;} static DWORD Decode(BYTE* dst, const BYTE* src, DWORD DataLen) { BYTE* Base=dst; const BYTE* End=src+DataLen; BYTE c1,c2,c3,c4; c1=c2=c3=c4=0; while(src=End) break; int LineLen=Decode(*src++); if(!LineLen) return dst-Base; //Terminator int LineIdx=0; while(LineIdx>4)); if(LineIdx++<=LineLen) *dst++=((c2<<4)|(c3>>2)); if(LineIdx++<=LineLen) *dst++=((c3<<6)|(c4)); } } return dst-Base; } static bool Decode(const char* Dest, const char* Source) { CFile iFile,oFile; if(!iFile.Open(Source,CFile::modeRead) || !oFile.Open(Dest,CFile::modeWrite|CFile::modeCreate)) return false; BYTE src[63]; BYTE dst[45]; while(DWORD Len=iFile.Read(src,63)) { Len=Decode(dst,src,Len); oFile.Write(dst,Len); } return true; } /* To Decode a file containing multiple UU encoded files: begin 644 Mvc-006f.jpg M_]C_X``02D9)1@`!`0```0`!``#_VP"$``0#`P,#`@0#`P,$!`0%!@H&!@4% ?2KL+9;K]U1V%(@&90NS=PIZTPR-L\QC\QZ>BB@#_V=\/ ` end begin 644 Mvc-006f.jpg M_]C_X``02D9)1@`!`0```0`!``#_VP"$``0#`P,#`@0#`P,$!`0%!@H&!@4% ?2KL+9;K]U1V%(@&90NS=PIZTPR-L\QC\QZ>BB@#_V=\/ ` end */ static bool Decode(const char* Source) { CFile iFile,oFile; if(!iFile.Open(Source,CFile::modeRead)) return false; CArchive ar(&iFile, CArchive::load); CString S; unsigned char Attributes[3]; while(ar.ReadString(S)) { if(S.IsEmpty() || (S.Left(6)!="begin ") || (S.GetLength()<11) || ((Attributes[0]=S[6]-'0')>7) || ((Attributes[1]=S[7]-'0')>7) || ((Attributes[2]=S[8]-'0')>7) || (S[9]!=' ')) continue; if(!oFile.Open(S.Mid(10),CFile::modeWrite|CFile::modeCreate)) return false; BYTE dst[45]; while(ar.ReadString(S)) { if(S=="end") break; DWORD Len=Decode(dst, (const BYTE*)&*S, S.GetLength()); oFile.Write(dst,Len); } } return true; } //>>>>>>>>>>>> Encoders <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< static BYTE Encode(BYTE c) {return c ? (c & 0x3F)+0x20 : '`';} static DWORD Encode(BYTE* dst, const BYTE* src, DWORD DataLen) { BYTE* Base=dst; const BYTE* End=src+DataLen; BYTE b1,b2,b3; b1=b2=b3=0; while(src=45) { // Output bytes 45 at a time *dst++=Encode(45); for(int i=0; i<15; ++i) { b1=*src++; b2=*src++; b3=*src++; *dst++=Encode(b1>>2); *dst++=Encode(((b1<<4) & 0x30) | ((b2>>4) & 0x0F)); *dst++=Encode(((b2<<2) & 0x3C) | ((b3>>6) & 0x03)); *dst++=Encode(b3 & 0x3F); } }else{ *dst++=Encode((BYTE)(End-src)); while(src>2); *dst++=Encode(((b1<<4) & 0x30) | ((b2>>4) & 0x0F)); *dst++=Encode(((b2<<2) & 0x3C) | ((b3>>6) & 0x03)); *dst++=Encode(b3 & 0x3F); } } *dst++='\r'; *dst++='\n'; } return dst-Base; } static bool Encode(const char* Dest, const char* Source) { CFile iFile,oFile; if(!iFile.Open(Source,CFile::modeRead) || !oFile.Open(Dest,CFile::modeWrite|CFile::modeCreate)) return false; BYTE src[45]; BYTE dst[63]; while(DWORD Len=iFile.Read(src,45)) { Len=Encode(dst,src,Len); oFile.Write(dst,Len); } return true; } }; #endif // UUh