1 #ifndef __PEDROUTIL_H__
2 #define __PEDROUTIL_H__
3 /*
4 * Support classes for the Pedro mini-XMPP client.
5 *
6 * Authors:
7 * Bob Jamison
8 *
9 * Copyright (C) 2005-2007 Bob Jamison
10 *
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 */
26 #include <stdio.h>
27 #include <vector>
29 #include <string>
31 #include "pedrodom.h"
34 #ifdef HAVE_SSL
35 #include <openssl/ssl.h>
36 #include <openssl/err.h>
37 #endif
41 namespace Pedro
42 {
47 //########################################################################
48 //########################################################################
49 //# B A S E 6 4
50 //########################################################################
51 //########################################################################
54 //#################
55 //# ENCODER
56 //#################
59 /**
60 * This class is for Base-64 encoding
61 */
62 class Base64Encoder
63 {
65 public:
67 Base64Encoder()
68 {
69 reset();
70 }
72 virtual ~Base64Encoder()
73 {}
75 virtual void reset()
76 {
77 outBuf = 0L;
78 bitCount = 0;
79 buf = "";
80 }
82 virtual void append(int ch);
84 virtual void append(char *str);
86 virtual void append(unsigned char *str, int len);
88 virtual void append(const DOMString &str);
90 virtual DOMString finish();
92 static DOMString encode(const DOMString &str);
95 private:
98 unsigned long outBuf;
100 int bitCount;
102 DOMString buf;
104 };
109 //#################
110 //# DECODER
111 //#################
113 class Base64Decoder
114 {
115 public:
116 Base64Decoder()
117 {
118 reset();
119 }
121 virtual ~Base64Decoder()
122 {}
124 virtual void reset()
125 {
126 inCount = 0;
127 buf.clear();
128 }
131 virtual void append(int ch);
133 virtual void append(char *str);
135 virtual void append(const DOMString &str);
137 std::vector<unsigned char> finish();
139 static std::vector<unsigned char> decode(const DOMString &str);
141 static DOMString decodeToString(const DOMString &str);
143 private:
145 int inBytes[4];
146 int inCount;
147 std::vector<unsigned char> buf;
148 };
153 //########################################################################
154 //########################################################################
155 //### S H A 1 H A S H I N G
156 //########################################################################
157 //########################################################################
159 /**
160 *
161 */
162 class Sha1
163 {
164 public:
166 /**
167 *
168 */
169 Sha1()
170 { init(); }
172 /**
173 *
174 */
175 virtual ~Sha1()
176 {}
179 /**
180 * Static convenience method. This would be the most commonly used
181 * version;
182 * @parm digest points to a bufer of 20 unsigned chars
183 */
184 static void hash(unsigned char *dataIn, int len, unsigned char *digest);
186 /**
187 * Static convenience method. This will fill a string with the hex
188 * codex string.
189 */
190 static DOMString hashHex(unsigned char *dataIn, int len);
192 /**
193 * Initialize the context (also zeroizes contents)
194 */
195 virtual void init();
197 /**
198 *
199 */
200 virtual void append(unsigned char *dataIn, int len);
202 /**
203 *
204 * @parm digest points to a bufer of 20 unsigned chars
205 */
206 virtual void finish(unsigned char *digest);
209 private:
211 void hashblock();
213 unsigned long H[5];
214 unsigned long W[80];
215 unsigned long sizeHi,sizeLo;
216 int lenW;
218 };
224 //########################################################################
225 //########################################################################
226 //### M D 5 H A S H I N G
227 //########################################################################
228 //########################################################################
230 /**
231 *
232 */
233 class Md5
234 {
235 public:
237 /**
238 *
239 */
240 Md5()
241 { init(); }
243 /**
244 *
245 */
246 virtual ~Md5()
247 {}
249 /**
250 * Static convenience method.
251 * @parm digest points to an buffer of 16 unsigned chars
252 */
253 static void hash(unsigned char *dataIn,
254 unsigned long len, unsigned char *digest);
256 static DOMString hashHex(unsigned char *dataIn, unsigned long len);
258 /**
259 * Initialize the context (also zeroizes contents)
260 */
261 virtual void init();
263 /**
264 *
265 */
266 virtual void append(unsigned char dataIn);
268 /**
269 *
270 */
271 virtual void append(unsigned char *dataIn, unsigned long len);
273 /**
274 *
275 */
276 virtual void append(const DOMString &str);
278 /**
279 * Finalize and output the hash.
280 * @parm digest points to an buffer of 16 unsigned chars
281 */
282 virtual void finish(unsigned char *digest);
285 /**
286 * Same as above , but hex to an output String
287 */
288 virtual DOMString finishHex();
290 private:
292 void transform(unsigned long *buf, unsigned long *in);
294 unsigned long buf[4];
295 unsigned long bits[2];
296 unsigned char in[64];
298 };
306 //########################################################################
307 //########################################################################
308 //### T H R E A D
309 //########################################################################
310 //########################################################################
314 /**
315 * This is the interface for a delegate class which can
316 * be run by a Thread.
317 * Thread thread(runnable);
318 * thread.start();
319 */
320 class Runnable
321 {
322 public:
324 Runnable()
325 {}
326 virtual ~Runnable()
327 {}
329 /**
330 * The method of a delegate class which can
331 * be run by a Thread. Thread is completed when this
332 * method is done.
333 */
334 virtual void run() = 0;
336 };
340 /**
341 * A simple wrapper of native threads in a portable class.
342 * It can be used either to execute its own run() method, or
343 * delegate to a Runnable class's run() method.
344 */
345 class Thread
346 {
347 public:
349 /**
350 * Create a thread which will execute its own run() method.
351 */
352 Thread()
353 { runnable = NULL ; started = false; }
355 /**
356 * Create a thread which will run a Runnable class's run() method.
357 */
358 Thread(const Runnable &runner)
359 { runnable = (Runnable *)&runner; started = false; }
361 /**
362 * This does not kill a spawned thread.
363 */
364 virtual ~Thread()
365 {}
367 /**
368 * Static method to pause the current thread for a given
369 * number of milliseconds.
370 */
371 static void sleep(unsigned long millis);
373 /**
374 * This method will be executed if the Thread was created with
375 * no delegated Runnable class. The thread is completed when
376 * the method is done.
377 */
378 virtual void run()
379 {}
381 /**
382 * Starts the thread.
383 */
384 virtual void start();
386 /**
387 * Calls either this class's run() method, or that of a Runnable.
388 * A user would normally not call this directly.
389 */
390 virtual void execute()
391 {
392 started = true;
393 if (runnable)
394 runnable->run();
395 else
396 run();
397 }
399 private:
401 Runnable *runnable;
403 bool started;
405 };
412 //########################################################################
413 //########################################################################
414 //### S O C K E T
415 //########################################################################
416 //########################################################################
420 /**
421 * A socket wrapper that provides cross-platform capability, plus SSL
422 */
423 class TcpSocket
424 {
425 public:
427 TcpSocket();
429 TcpSocket(const std::string &hostname, int port);
431 TcpSocket(const char *hostname, int port);
433 TcpSocket(const TcpSocket &other);
435 virtual ~TcpSocket();
437 bool isConnected();
439 void enableSSL(bool val);
441 bool getEnableSSL();
443 bool getHaveSSL();
445 bool connect(const std::string &hostname, int portno);
447 bool connect(const char *hostname, int portno);
449 bool startTls();
451 bool connect();
453 bool disconnect();
455 bool setReceiveTimeout(unsigned long millis);
457 long available();
459 bool write(int ch);
461 bool write(char *str);
463 bool write(const std::string &str);
465 int read();
467 std::string readLine();
469 private:
470 void init();
472 std::string hostname;
473 int portno;
474 int sock;
475 bool connected;
477 bool sslEnabled;
479 unsigned long receiveTimeout;
481 #ifdef HAVE_SSL
482 SSL_CTX *sslContext;
483 SSL *sslStream;
484 #endif
486 };
493 } //namespace Pedro
495 #endif /* __PEDROUTIL_H__ */
497 //########################################################################
498 //# E N D O F F I L E
499 //########################################################################