1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
|
#ifndef __PEDROUTIL_H__
#define __PEDROUTIL_H__
/*
* Support classes for the Pedro mini-XMPP client.
*
* Authors:
* Bob Jamison
*
* Copyright (C) 2005-2007 Bob Jamison
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdio.h>
#include <stdarg.h>
#include <vector>
#include <string>
#include "pedrodom.h"
#ifdef HAVE_SSL
#include <openssl/ssl.h>
#include <openssl/err.h>
#endif
namespace Pedro
{
//########################################################################
//########################################################################
//# B A S E 6 4
//########################################################################
//########################################################################
//#################
//# ENCODER
//#################
/**
* This class is for Base-64 encoding
*/
class Base64Encoder
{
public:
Base64Encoder()
{
reset();
}
virtual ~Base64Encoder()
{}
virtual void reset()
{
outBuf = 0L;
bitCount = 0;
buf = "";
}
virtual void append(int ch);
virtual void append(char *str);
virtual void append(unsigned char *str, int len);
virtual void append(const DOMString &str);
virtual DOMString finish();
static DOMString encode(const DOMString &str);
private:
unsigned long outBuf;
int bitCount;
DOMString buf;
};
//#################
//# DECODER
//#################
class Base64Decoder
{
public:
Base64Decoder()
{
reset();
}
virtual ~Base64Decoder()
{}
virtual void reset()
{
inCount = 0;
buf.clear();
}
virtual void append(int ch);
virtual void append(char *str);
virtual void append(const DOMString &str);
std::vector<unsigned char> finish();
static std::vector<unsigned char> decode(const DOMString &str);
static DOMString decodeToString(const DOMString &str);
private:
int inBytes[4];
int inCount;
std::vector<unsigned char> buf;
};
//########################################################################
//########################################################################
//### S H A 1 H A S H I N G
//########################################################################
//########################################################################
/**
* This class performs a slow SHA1 hash on a stream of input data.
*/
class Sha1
{
public:
/**
* Constructor
*/
Sha1()
{ init(); }
/**
*
*/
virtual ~Sha1()
{ init(); }
/**
* Static convenience method. This would be the most commonly used
* version;
* @parm digest points to a bufer of 20 unsigned chars
*/
static void hash(unsigned char *dataIn, int len, unsigned char *digest);
/**
* Static convenience method. This will fill a string with the hex
* coded string.
*/
static DOMString hashHex(unsigned char *dataIn, int len);
/**
* Static convenience method.
*/
static DOMString hashHex(const DOMString &str);
/**
* Initialize the context (also zeroizes contents)
*/
virtual void init();
/**
* Append a single character
*/
virtual void append(unsigned char ch);
/**
* Append a data buffer
*/
virtual void append(unsigned char *dataIn, int len);
/**
* Append a String
*/
virtual void append(const DOMString &str);
/**
*
* @parm digest points to a bufer of 20 unsigned chars
*/
virtual void finish(unsigned char *digest);
private:
void transform();
unsigned long hashBuf[5];
unsigned long inBuf[80];
unsigned long nrBytesHi;
unsigned long nrBytesLo;
int longNr;
int byteNr;
unsigned long inb[4];
};
//########################################################################
//########################################################################
//### M D 5 H A S H I N G
//########################################################################
//########################################################################
/**
* This is a utility version of a simple MD5 hash algorithm. This is
* neither efficient nor fast. It is intended to be a small simple utility
* for hashing small amounts of data in a non-time-critical place.
*
* Note that this is a rewrite whose purpose is to remove any
* machine dependencies.
*/
class Md5
{
public:
/**
* Constructor
*/
Md5()
{ init(); }
/**
* Destructor
*/
virtual ~Md5()
{}
/**
* Static convenience method.
* @parm digest points to an buffer of 16 unsigned chars
*/
static void hash(unsigned char *dataIn,
unsigned long len, unsigned char *digest);
/**
* Static convenience method.
* Hash a byte array of a given length
*/
static DOMString hashHex(unsigned char *dataIn, unsigned long len);
/**
* Static convenience method.
* Hash a String
*/
static DOMString hashHex(const DOMString &str);
/**
* Initialize the context (also zeroizes contents)
*/
virtual void init();
/*
* Update with one character
*/
virtual void append(unsigned char ch);
/**
* Update with a byte buffer of a given length
*/
virtual void append(unsigned char *dataIn, unsigned long len);
/**
* Update with a string
*/
virtual void append(const DOMString &str);
/**
* Finalize and output the hash.
* @parm digest points to an buffer of 16 unsigned chars
*/
virtual void finish(unsigned char *digest);
/**
* Same as above , but hex to an output String
*/
virtual DOMString finishHex();
private:
void transform();
unsigned long hashBuf[4];
unsigned long inBuf[16];
unsigned long nrBytesHi;
unsigned long nrBytesLo;
unsigned long inb[4]; // Buffer for input bytes as longs
int byteNr; // which byte in long
int longNr; // which long in 8 long segment
};
//########################################################################
//########################################################################
//### T H R E A D
//########################################################################
//########################################################################
/**
* This is the interface for a delegate class which can
* be run by a Thread.
* Thread thread(runnable);
* thread.start();
*/
class Runnable
{
public:
Runnable()
{}
virtual ~Runnable()
{}
/**
* The method of a delegate class which can
* be run by a Thread. Thread is completed when this
* method is done.
*/
virtual void run() = 0;
};
/**
* A simple wrapper of native threads in a portable class.
* It can be used either to execute its own run() method, or
* delegate to a Runnable class's run() method.
*/
class Thread
{
public:
/**
* Create a thread which will execute its own run() method.
*/
Thread()
{ runnable = NULL ; started = false; }
/**
* Create a thread which will run a Runnable class's run() method.
*/
Thread(const Runnable &runner)
{ runnable = (Runnable *)&runner; started = false; }
/**
* This does not kill a spawned thread.
*/
virtual ~Thread()
{}
/**
* Static method to pause the current thread for a given
* number of milliseconds.
*/
static void sleep(unsigned long millis);
/**
* This method will be executed if the Thread was created with
* no delegated Runnable class. The thread is completed when
* the method is done.
*/
virtual void run()
{}
/**
* Starts the thread.
*/
virtual void start();
/**
* Calls either this class's run() method, or that of a Runnable.
* A user would normally not call this directly.
*/
virtual void execute()
{
started = true;
if (runnable)
runnable->run();
else
run();
}
private:
Runnable *runnable;
bool started;
};
//########################################################################
//########################################################################
//### S O C K E T
//########################################################################
//########################################################################
/**
* A socket wrapper that provides cross-platform capability, plus SSL
*/
class TcpSocket
{
public:
TcpSocket();
TcpSocket(const std::string &hostname, int port);
TcpSocket(const char *hostname, int port);
TcpSocket(const TcpSocket &other);
virtual ~TcpSocket();
void error(const char *fmt, ...);
DOMString &getLastError();
bool isConnected();
void enableSSL(bool val);
bool getEnableSSL();
bool getHaveSSL();
bool connect(const std::string &hostname, int portno);
bool connect(const char *hostname, int portno);
bool startTls();
bool connect();
bool disconnect();
bool setReceiveTimeout(unsigned long millis);
long available();
bool write(int ch);
bool write(char *str);
bool write(const std::string &str);
int read();
std::string readLine();
private:
void init();
DOMString lastError;
std::string hostname;
int portno;
int sock;
bool connected;
bool sslEnabled;
unsigned long receiveTimeout;
#ifdef HAVE_SSL
SSL_CTX *sslContext;
SSL *sslStream;
#endif
};
} //namespace Pedro
#endif /* __PEDROUTIL_H__ */
//########################################################################
//# E N D O F F I L E
//########################################################################
|