root/honeyclient/branches/rel/1.0/Capture2/capture-client-xeno-mod/CaptureGlobal.cpp

Revision 863, 5.6 kB (checked in by xkovah, 1 year ago)

padding out the ISO formatted timestamp…again

Line 
1 #include "CaptureGlobal.h"
2 #include <strsafe.h>
3
4 wstring
5 CaptureGlobal::urlEncode(wstring text)
6 {
7     size_t len = text.length();
8     wstring encoded = L"";
9     for(size_t i = 0; i < len; i++)
10     {
11         wchar_t wch = text.at(i);
12         if ('A' <= wch && wch <= 'Z') {
13             encoded += wch;
14         } else if ('a' <= wch && wch <= 'z') {
15             encoded += wch;
16         } else if ('0' <= wch && wch <= '9') {
17             encoded += wch;
18         } else if (wch == ' ') {
19             encoded += L"+";
20         } else if (wch == '-' || wch == '_'
21             || wch == '.' || wch == '!'
22             || wch == '~' || wch == '*'
23             || wch == '\'' || wch == '('
24             || wch == ')') {
25             encoded += wch;
26         } else if (wch <= 0x007f) {     // other ASCII
27             encoded += hexenc[wch];
28         } else if (wch <= 0x07FF) {     // non-ASCII <= 0x7FF
29             encoded += hexenc[0xc0 | (wch >> 6)];
30             encoded += hexenc[0x80 | (wch & 0x3F)];
31         } else {                    // 0x7FF < ch <= 0xFFFF
32             encoded += hexenc[0xe0 | (wch >> 12)];
33             encoded += hexenc[0x80 | ((wch >> 6) & 0x3F)];
34             encoded += hexenc[0x80 | (wch & 0x3F)];
35         }
36     }
37     return encoded;
38 }
39
40 wstring
41 CaptureGlobal::urlDecode(wstring text)
42 {
43     wstring decoded = L"";
44     wchar_t temp[] = L"0x00";
45     size_t len = text.length();
46     int sequence = 0;
47     wchar_t conwch = 0;
48     for(size_t i = 0; i < len; i++)
49     {   
50         wchar_t wch = text.at(i++);
51         if((wch == '%') && (i+1 < len))
52         {           
53             temp[2] = text.at(i++);
54             temp[3] = text.at(i);
55             long tconwch = wcstol(temp, NULL, 16);
56             if(tconwch <= 0x7F) {
57                 decoded += tconwch; // normal ascii char
58             } else if(tconwch >= 0x80 && tconwch <= 0xBF) { // partial byte
59                 tconwch = tconwch & 0x3F;
60                 if(sequence-- == 2)
61                     tconwch = tconwch << 6;
62                 conwch |= tconwch;
63                 if(sequence == 0)
64                     decoded += conwch;
65             } else if(tconwch >= 0xC0 && tconwch <= 0xDF) {
66                 conwch = (tconwch & 0x1F) << 6; // make space for partial bytes
67                 sequence = 1; // 1 more partial bytes follow
68             } else if(tconwch >= 0xE0 && tconwch <= 0xEF) {
69                 conwch = (tconwch & 0xF) << 12; // make space for partial bytes
70                 sequence = 2; // 2 more partial bytes follow
71             }
72         } else {
73             decoded += text.at(--i);
74         }
75     }
76     return decoded;
77 }
78
79
80 void DebugPrint(LPCTSTR pszFormat, ... )
81 {
82 //#ifdef _DEBUG
83     
84     wchar_t szOutput[MAX_PATH * 2];
85     va_list argList;
86     va_start(argList, pszFormat);
87     StringCchVPrintf(szOutput, MAX_PATH*2, pszFormat, argList);
88     va_end(argList);
89     //printf("%ls\n", szOutput);
90     OutputDebugString(szOutput);
91    
92 //#endif
93 }
94
95 void decode_base64(char* encodedBuffer)
96 {
97     size_t position = 0;
98     char encoded[4];
99     size_t len = strlen(encodedBuffer);
100     for(size_t i = 0; i < len+1; i++)
101     {
102         if((i > 0) && ((i % 4) == 0))
103         {
104             encodedBuffer[position++] = (unsigned char) (b64_index[encoded[0]] << 2 | b64_index[encoded[1]] >> 4);
105             encodedBuffer[position++] = (unsigned char) (b64_index[encoded[1]] << 4 | b64_index[encoded[2]] >> 2); 
106             encodedBuffer[position++] = (unsigned char) (b64_index[encoded[2]] << 6 | b64_index[encoded[3]]);
107         }
108         encoded[i%4] = encodedBuffer[i];
109     }
110     /* Should alway succeed as base64 encoded string length > decoded string length */
111     if(position < len)
112     {
113         encodedBuffer[position] = '\0';
114     }
115 }
116
117 char* encode_base64(char* cleartextBuffer, unsigned int length, size_t* encodedLength)
118 {
119     /* Fairly ineffecient base64 encoding method ... could be a lot better but it works
120        at the moment. If performance is slow when transferring large files this could be
121        the problem */
122     unsigned int len = length; 
123     int nBlocks = len/3;
124     unsigned int remainder = len % 3;
125     int position = 0;
126     if(remainder != 0)
127     {
128         nBlocks++;
129     }
130     char* encodedBuffer = (char*)malloc((nBlocks*4)+2);
131
132     int k = 0;
133     for(unsigned int i = 0; i < len; i+=3)
134     {
135         unsigned int block = 0;
136
137         block |= ((unsigned char)cleartextBuffer[i] << 16);
138         if(i+1 < len)
139             block |= ((unsigned char)cleartextBuffer[i+1] << 8);
140         if(i+2 < len)
141             block |= ((unsigned char)cleartextBuffer[i+2]);
142
143         encodedBuffer[k++] = b64_list[(block >> 18) & 0x3F];
144         encodedBuffer[k++] = b64_list[(block >> 12) & 0x3F];
145         encodedBuffer[k++] = b64_list[(block >> 6) & 0x3F];
146         encodedBuffer[k++] = b64_list[(block & 0x3F)];
147     }
148
149     if(remainder > 0)
150     {
151         for(unsigned int i = remainder; i < 3; i++)
152         {
153             encodedBuffer[k-(3-i)] = '=';
154         }
155     }
156     *encodedLength = k;
157     return encodedBuffer;
158 }
159
160 //Formatting as ISO-8601 time format
161 size_t convertTimefieldsToString(TIME_FIELDS time, wchar_t* buffer, size_t bufferLength)
162 {
163     wchar_t szTime[16];
164     wchar_t wtime[256];
165     ZeroMemory(&szTime, sizeof(szTime));
166     ZeroMemory(&wtime, sizeof(wtime));
167     _itow_s(time.wYear,szTime,16,10);
168     wcscat_s(wtime, 256, szTime);
169     wcscat_s(wtime, 256, L"-");
170     _itow_s(time.wMonth,szTime,16,10);
171     if(time.wMonth < 10) {
172         wcscat_s(wtime, 256, L"0");
173     }
174     wcscat_s(wtime, 256, szTime);
175     wcscat_s(wtime, 256, L"-");
176     _itow_s(time.wDay,szTime,16,10);
177     if(time.wDay < 10) {
178         wcscat_s(wtime, 256, L"0");
179     }
180     wcscat_s(wtime, 256, szTime);
181     wcscat_s(wtime, 256, L" ");
182     _itow_s(time.wHour,szTime,16,10);
183     if(time.wHour < 10) {
184         wcscat_s(wtime, 256, L"0");
185     }
186     wcscat_s(wtime, 256, szTime);
187     wcscat_s(wtime, 256, L":");
188     _itow_s(time.wMinute,szTime,16,10);
189     if(time.wMinute < 10) {
190         wcscat_s(wtime, 256, L"0");
191     }
192     wcscat_s(wtime, 256, szTime);
193     wcscat_s(wtime, 256, L":");
194     _itow_s(time.wSecond,szTime,16,10);
195     if(time.wSecond < 10) {
196         wcscat_s(wtime, 256, L"0");
197     }
198     wcscat_s(wtime, 256, szTime);
199     wcscat_s(wtime, 256, L".");
200     _itow_s(time.wMilliseconds,szTime,16,10);
201     wcscat_s(wtime, 256, szTime);
202     size_t timeLength = wcslen(wtime);
203     if(bufferLength >= timeLength)
204     {
205         wcscpy_s(buffer, bufferLength, wtime);
206         return timeLength;
207     } else {
208         return -1;
209     }
210 }
Note: See TracBrowser for help on using the browser.