Sophie

Sophie

distrib > Mandriva > 2007.0 > i586 > by-pkgid > ad1ba1135a9c9eeffc2e538163e00373 > files > 890

libCommonC++2_1.4-devel-1.4.1-1mdv2007.0.i586.rpm

\section{url1.cpp}


\footnotesize\begin{verbatim}1 #include <cc++/common.h>
2 
3 // This was a test base64 stuff
4 
5 #ifdef  CCXX_NAMESPACES
6 using namespace std;
7 using namespace ost;
8 #endif
9 
10 #define BUFLEN 512
11 char buf1[BUFLEN];
12 char buf2[BUFLEN];
13 
14 bool errorOccurred = false;
15 char status[256] = "";
16 
17 void printBug(const char*msg)
18 {
19         errorOccurred = true;
20         printf("status = %s\n%s!\n",status,msg);
21 }
22 
23 const char fillChar='&';
24 
25 void initBuf(char* buf)
26 {
27         memset(buf,fillChar,BUFLEN);
28 }
29 
30 void checkBuf(char* buf,int prev,int size)
31 {
32         int i;
33         for(i=0;i<prev;++i)
34                 if (buf[i] != fillChar)
35                 {
36                         printBug("buffer overflow founded");
37                         return;
38                 }
39         for(i=prev+size;i<BUFLEN;++i)
40                 if (buf[i] != fillChar)
41                 {
42                         printBug("buffer overflow founded");
43                         return;
44                 }
45 }
46 
47 // check with binary functions
48 void check1(unsigned char* s,size_t len,size_t buflen1,size_t buflen2,bool checkEqual=false)
49 {
50         initBuf(buf1);
51         b64Encode(s,len,buf1+16,buflen1);
52         checkBuf(buf1,16,buflen1);
53         initBuf(buf2);
54         b64Decode(buf1+16,(unsigned char*)buf2+16,buflen2);
55         checkBuf(buf2,16,buflen2);
56         if (checkEqual && memcmp(s,buf2+16,len) != 0)
57                 printBug ("buffer different");
58 }
59 
60 // check with old string
61 void check2(const char* s,size_t buflen,bool checkEqual=false)
62 {
63         if (!buflen) return;
64         initBuf(buf1);
65         b64Encode(s,buf1+16,buflen);
66         checkBuf(buf1,16,buflen);
67         initBuf(buf2);
68         size_t buflen2 = strlen(buf1+16)+1;
69         b64Decode(buf1+16,buf2+16);
70         checkBuf(buf2,16,buflen2);
71         if (checkEqual && strcmp(s,buf2+16) != 0)
72         {
73                 printBug ("buffer different");
74                 printf("'%s' != '%s'\n'%s'\n",s,buf2+16,buf1+16);
75         }
76 }
77 
78 // check buffer overflow on string
79 void checkStringOverflow(char* s,unsigned int len)
80 {
81         bool execCheck2 = (strlen(s) == len);
82         for(unsigned int l1=0;l1<32;++l1)
83         {
84                 sprintf(status,"%s %d",s,l1);
85                 if (execCheck2)
86                         check2(s,l1,l1 >= (len+2)/3*4+1);
87                 for(unsigned int l2=0;l2<32;++l2)
88                         check1((unsigned char*)s,len,l1,l2,
89                                         (l1 >= (len+2)/3*4+1) 
90                                         && (l2 >= len) );
91         }
92 }
93 
94 int main()
95 {
96         checkStringOverflow("",0);
97         checkStringOverflow("aaa",3);
98         if (!errorOccurred)
99                 printf("All seem ok\n");
100         return 0;
101 }
102 
103 
\end{verbatim}
\normalsize