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) 2006 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, unsigned long len);
268 /**
269 *
270 */
271 virtual void append(const DOMString &str);
273 /**
274 * Finalize and output the hash.
275 * @parm digest points to an buffer of 16 unsigned chars
276 */
277 virtual void finish(unsigned char *digest);
280 /**
281 * Same as above , but hex to an output String
282 */
283 virtual DOMString finishHex();
285 private:
287 void transform(unsigned long *buf, unsigned long *in);
289 unsigned long buf[4];
290 unsigned long bits[2];
291 unsigned char in[64];
293 };
301 //########################################################################
302 //########################################################################
303 //### T H R E A D
304 //########################################################################
305 //########################################################################
309 /**
310 * This is the interface for a delegate class which can
311 * be run by a Thread.
312 * Thread thread(runnable);
313 * thread.start();
314 */
315 class Runnable
316 {
317 public:
319 Runnable()
320 {}
321 virtual ~Runnable()
322 {}
324 /**
325 * The method of a delegate class which can
326 * be run by a Thread. Thread is completed when this
327 * method is done.
328 */
329 virtual void run() = 0;
331 };
335 /**
336 * A simple wrapper of native threads in a portable class.
337 * It can be used either to execute its own run() method, or
338 * delegate to a Runnable class's run() method.
339 */
340 class Thread
341 {
342 public:
344 /**
345 * Create a thread which will execute its own run() method.
346 */
347 Thread()
348 { runnable = NULL ; started = false; }
350 /**
351 * Create a thread which will run a Runnable class's run() method.
352 */
353 Thread(const Runnable &runner)
354 { runnable = (Runnable *)&runner; started = false; }
356 /**
357 * This does not kill a spawned thread.
358 */
359 virtual ~Thread()
360 {}
362 /**
363 * Static method to pause the current thread for a given
364 * number of milliseconds.
365 */
366 static void sleep(unsigned long millis);
368 /**
369 * This method will be executed if the Thread was created with
370 * no delegated Runnable class. The thread is completed when
371 * the method is done.
372 */
373 virtual void run()
374 {}
376 /**
377 * Starts the thread.
378 */
379 virtual void start();
381 /**
382 * Calls either this class's run() method, or that of a Runnable.
383 * A user would normally not call this directly.
384 */
385 virtual void execute()
386 {
387 started = true;
388 if (runnable)
389 runnable->run();
390 else
391 run();
392 }
394 private:
396 Runnable *runnable;
398 bool started;
400 };
407 //########################################################################
408 //########################################################################
409 //### S O C K E T
410 //########################################################################
411 //########################################################################
415 /**
416 * A socket wrapper that provides cross-platform capability, plus SSL
417 */
418 class TcpSocket
419 {
420 public:
422 TcpSocket();
424 TcpSocket(const std::string &hostname, int port);
426 TcpSocket(const char *hostname, int port);
428 TcpSocket(const TcpSocket &other);
430 virtual ~TcpSocket();
432 bool isConnected();
434 void enableSSL(bool val);
436 bool getEnableSSL();
438 bool connect(const std::string &hostname, int portno);
440 bool connect(const char *hostname, int portno);
442 bool startTls();
444 bool connect();
446 bool disconnect();
448 bool setReceiveTimeout(unsigned long millis);
450 long available();
452 bool write(int ch);
454 bool write(char *str);
456 bool write(const std::string &str);
458 int read();
460 std::string readLine();
462 private:
463 void init();
465 std::string hostname;
466 int portno;
467 int sock;
468 bool connected;
470 bool sslEnabled;
472 unsigned long receiveTimeout;
474 #ifdef HAVE_SSL
475 SSL_CTX *sslContext;
476 SSL *sslStream;
477 #endif
479 };
486 } //namespace Pedro
488 #endif /* __PEDROUTIL_H__ */
490 //########################################################################
491 //# E N D O F F I L E
492 //########################################################################