My favorites | Sign in
Project Home Downloads Source
Checkout   Browse   Changes    
 
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
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
#include "Web.h"

NAMESPACE_UPP

String FormatIP(dword _ip)
{
byte ip[4];
Poke32be(ip, _ip);
return Format("%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
}

static const char hex_digits[] = "0123456789ABCDEF";

String UrlEncode(const String& s, const char *specials)
{
int l = (int)strlen(specials);
const char *p = s, *e = s.End();
String out;
for(; p < e; p++)
{
const char *b = p;
while(p < e && (byte)*p > ' ' && (byte)*p < 127 && memchr(specials, *p, l) == 0)
p++;
if(p > b)
out.Cat(b, int(p - b));
if(p >= e)
break;
if(*p == ' ')
out << '+';
else
out << '%' << hex_digits[(*p >> 4) & 15] << hex_digits[*p & 15];
}
return out;
}


bool IsSameTextFile(const char *p, const char *q)
{
for(;;)
{
if(*p == 0)
{
while(*q && (byte)*q <= ' ')
q++;
return *q == 0;
}
if(*q == 0)
{
while(*p && (byte)*p <= ' ')
p++;
return *p == 0;
}
bool pcr = false;
if(*p == '\r' || *p == '\n')
{
if(p[1] == (*p == '\r' ? '\n' : '\r'))
p++;
p++;
pcr = true;
}
bool qcr = false;
if(*q == '\r' || *q == '\n')
{
if(q[1] == (*q == '\r' ? '\n' : '\r'))
q++;
q++;
qcr = true;
}
if(pcr || qcr)
{
if(pcr != qcr)
return false;
}
else if(*p++ != *q++)
return false;
}
}

String StringSample(const char *s, int limit)
{
const char *e = (const char *)memchr(s, 0, limit + 1);
if(e)
return String(s, e);
return String(s, limit) + "...";
}

static String ToQuotedHtml(const String& s)
{
String out;
for(const char *p = s.Begin(), *e = s.End(); p < e; p++)
switch(*p)
{
case 31: out.Cat("&nbsp;"); break;
case '<': out.Cat("&lt;"); break;
case '>': out.Cat("&gt;"); break;
case '&': out.Cat("&amp;"); break;
case '\"': out.Cat("&quot;"); break;
case '\r': break;
case '\t': out.Cat("&#9;"); break;
case '\n': out.Cat("&#10;"); break;
default: out.Cat(*p); break;
}
return out;
}

String GetRandomIdent(int length)
{
StringBuffer ident(length);
char *p = ident;
for(int i = 0; i < length; i++)
*p++ = 'A' + rand() % ('Z' - 'A' + 1);
return ident;
}

static unsigned AddCRC(unsigned old, byte next, unsigned crc)
{
for(int i = 0; i < 8; i++)
{
old = (old << 1) ^ (old & 0x80000000u ? crc : 0) ^ (next & 1);
next >>= 1;
}
return old;
}

enum { CRC = 0x84030625 };

String OtpEncode(const String& password, const String& otp_seed)
{
if(IsNull(password))
return Null;
unsigned crc_sum[4];
memset(crc_sum, 0, sizeof(crc_sum));
for(int j = 0; j < otp_seed.GetLength(); j++)
{
unsigned sum = crc_sum[j & 3];
sum = AddCRC(sum, otp_seed[j], CRC);
for(int i = 0; i < password.GetLength(); i++)
sum = AddCRC(sum, password[i], CRC);
crc_sum[j & 3] = sum;
}
char h[100];
sprintf(h, "%08X%08X%08X%08X", crc_sum[0], crc_sum[1], crc_sum[2], crc_sum[3]);
return h;
}

String EncryptString(const String& password, const String& otp_key)
{
unsigned crc = 0;
int i;
for(i = 0; i < otp_key.GetLength(); i++)
crc = AddCRC(crc, otp_key[i], CRC);
int pl = password.GetLength();
StringBuffer out(pl);
char *p = out;
for(i = 0; i < pl; i++)
{
*p++ = password[i] ^ crc;
crc = AddCRC(crc, otp_key[(int)(crc % otp_key.GetLength())], CRC);
}
return out;
}

String BinHexEncode(const char *s, const char *e)
{
static const char bh[] = "0123456789ABCDEF";
int l = int(e - s);
StringBuffer out(2 * l);
char *p = out;
for(; s < e; s++) {
*p++ = bh[(*s >> 4) & 0xF];
*p++ = bh[*s & 0xF];
}
return out;
}

String BinhexEncode(const char *s, const char *e)
{
static const char bh[] = "0123456789abcdef";
int l = int(e - s);
StringBuffer out(2 * l);
char *p = out;
for(; s < e; s++) {
*p++ = bh[(*s >> 4) & 0xF];
*p++ = bh[*s & 0xF];
}
return out;
}

String BinHexDecode(const char *p, const char *e)
{
StringBuffer out;
if((e - p) & 1)
e--;
for(; p < e; p++)
if(IsXDigit(*p) && IsXDigit(p[1])) {
out.Cat(ctoi(p[0]) * 16 + ctoi(p[1]));
p++;
}
return out;
}

String ASCII85Encode(const byte *p, int length)
{
StringBuffer out;
out.Reserve((length >> 2) * 5 + (length & 3) + 1);
while(length >= 4)
{
dword chunk = (p[0] << 24) + (p[1] << 16) + (p[2] << 8) + p[3];
if(chunk == 0)
out.Cat('z');
else {
out.Cat(33 + chunk / (85 * 85 * 85 * 85));
out.Cat(33 + (chunk / (85 * 85 * 85)) % 85);
out.Cat(33 + (chunk / (85 * 85)) % 85);
out.Cat(33 + (chunk / 85) % 85);
out.Cat(33 + chunk % 85);
}
p += 4;
length -= 4;
}
if(length)
{
dword chunk = *p++ << 24;
if(length > 1) chunk += *p++ << 16;
if(length > 2) chunk += *p++ << 8;
out.Cat(33 + chunk / (85 * 85 * 85 * 85));
out.Cat(33 + (chunk / (85 * 85 * 85)) % 85);
if(length > 1)
out.Cat(33 + (chunk / (85 * 85)) % 85);
if(length > 2)
out.Cat(33 + (chunk / 85) % 85);
}
return out;
}

String ASCII85Decode(const byte *p, int length)
{
byte buffer[5];
int pos = 0;
String out;
for(const byte *e = p + length; p < e; p++)
if(*p >= 33 && *p < 33 + 85)
{
buffer[pos++] = *p - 33;
if(pos >= 5)
{
unsigned v = buffer[4] + 85 * (buffer[3] + 85 * (buffer[2] + 85 * (buffer[1] + 85 * buffer[0])));
out.Cat((byte)(v >> 24));
out.Cat((byte)(v >> 16));
out.Cat((byte)(v >> 8));
out.Cat((byte)v);
pos = 0;
}
}
else if(*p == 'z')
{
out.Cat("\0\0\0", 4);
pos = 0;
}
switch(pos)
{
case 2:
{
unsigned v = 84 + 84 * 85 + 84 * 85 * 85 + 85 * 85 * 85 * (buffer[1] + 85 * buffer[0]);
out.Cat((byte)(v >> 24));
}
break;

case 3:
{
unsigned v = 84 + 84 * 85 + 85 * 85 * (buffer[2] + 85 * (buffer[1] + 85 * buffer[0]));
out.Cat((byte)(v >> 24));
out.Cat((byte)(v >> 16));
}
break;

case 4:
{
unsigned v = 84 + 85 * (buffer[3] + 85 * (buffer[2] + 85 * (buffer[1] + 85 * buffer[0])));
out.Cat((byte)(v >> 24));
out.Cat((byte)(v >> 16));
out.Cat((byte)(v >> 8));
}
break;
}
return out;
}

static const dword *GetCRCTable()
{
enum { CTL = 0xDAA97221 };
static dword table[256];
if(table[1] == 0)
{
for(int i = 1; i < __countof(table); i++)
{
dword v = i << 24;
for(int t = 0; t < 8; t++)
v = (v << 1) ^ (v & (1 << 31) ? CTL : 0);
table[i] = v;
}
}
return table;
}

dword AddCRC(dword crc, const byte *data, int count)
{
const dword *t = GetCRCTable();
for(const byte *e = data + count; data < e; data++)
crc = ((crc << 8) | (*data & 0xFF)) ^ t[(crc >> 24) & 0xFF];
return crc;
}

static void GetLineIndex(String file, HashBase& hash, Vector<String>& lines)
{
const char *p = file;
while(*p)
{
while(*p && *p != '\n' && (byte)*p <= ' ')
p++;
const char *b = p;
while(*p && *p++ != '\n')
;
const char *e = p;
while(e > b && (byte)e[-1] <= ' ')
e--;
String s(b, e);
hash.Add(GetHashValue(s));
lines.Add(s);
}
}

int LocateLine(String old_file, int old_line, String new_file)
{
HashBase old_hash, new_hash;
Vector<String> old_lines, new_lines;
GetLineIndex(old_file, old_hash, old_lines);
GetLineIndex(new_file, new_hash, new_lines);
if(old_line <= 0)
return 0;
if(old_line >= old_lines.GetCount())
return new_lines.GetCount();
String line = old_lines[old_line];
//int hash = old_hash[old_line]; Mirek: unused
//int fore_count = old_lines.GetCount() - old_line - 1;
int best_match = 0, best_value = 0;
for(int r = 0; r < 10 && !best_value; r++)
{
int src = (r & 1 ? old_line + (r >> 1) + 1 : old_line - (r >> 1));
if(src < 0 || src >= old_lines.GetCount())
continue;
dword hash = old_hash[src];
for(int i = new_hash.Find(hash); i >= 0; i = new_hash.FindNext(i))
if(new_lines[i] == old_lines[src])
{
int max_back = min(i, src);
int max_fore = min(new_lines.GetCount() - i, old_lines.GetCount() - src) - 1;
if(max_back + max_fore <= best_value)
continue;
int back = 1;
while(back <= max_back && new_hash[i - back] == old_hash[src - back]
&& new_lines[i - back] == old_lines[src - back])
back++;
int fore = 1;
while(fore < max_fore && new_hash[i + fore] == old_hash[src + fore]
&& new_lines[i + fore] == old_lines[src + fore])
fore++;
if(back + fore > best_value)
{
best_value = back + fore;
best_match = minmax(i, 0, new_lines.GetCount());
}
}
}
return best_match;
}

void AppVersion(const char *ver)
{
#ifdef PLATFORM_WIN32
String out = "set RAW_VERSION=";
out.Cat(ver);
out.Cat("\r\n"
"set APP_VERSION=");
for(; *ver; ver++)
out.Cat(IsAlNum(*ver) ? *ver : '_');
out.Cat("\r\n");
SaveFile("ver.bat", out);
#else
fputs(ver, stdout);
#endif
}

//////////////////////////////////////////////////////////////////////
// HttpQuery::

RefPtr<HttpQuery::Data> HttpQuery::Empty()
{
static RefPtr<Data> emp = new Data;
return emp;
}

void HttpQuery::Serialize(Stream& stream)
{
int version = StreamHeading(stream, 1, 1, 1, "HttpQuery");
if(version >= 1)
{
if(stream.IsLoading())
Clear();
int count = GetCount();
stream / count;
for(int i = 0; i < count; i++)
{
String key, value;
if(stream.IsStoring())
{
key = data -> map.GetKey(i);
value = data -> map[i];
}
stream % key % value;
if(stream.IsLoading())
Set(key, value);
}
}
}

HttpQuery& HttpQuery::SetURL(const String& url)
{
const char *p = url;
while(*p && *p != '\1' && *p != '?')
p++;
p = (*p == '?' ? p + 1 : ~url);

// const char *e = url.End(); Mirek:unused
while(*p)
{
const char *last = p;
while(*p && *p != '=' && *p != '&')
p++;
String key = case_sensitive ? UrlDecode(last, p) : ToUpper(UrlDecode(last, p));
if(*p == '=')
p++;
last = p;
while(*p && *p != '&')
p++;
Set(key, UrlDecode(last, p));
if(*p)
p++;
}
return *this;
}

String HttpQuery::GetHidden() const
{
String out;
for(int i = 0; i < GetCount(); i++)
if(!IsInternal(i))
{
out << "<INPUT TYPE=HIDDEN NAME=\"" << ToHtml(GetKey(i))
<< "\" VALUE=\"" << ToQuotedHtml(GetValue(i)) << "\">\n";
}
return out;
}

String HttpQuery::GetQuery(bool empty) const
{
String result;
for(int i = 0, n = GetCount(); i < n; i++)
if(!IsInternal(i))
{
String key = GetKey(i), value = GetValue(i);
if(empty || !value.IsEmpty())
{
if(!result.IsEmpty()) result.Cat('&');
result.Cat(UrlEncode(key));
result.Cat('=');
result.Cat(UrlEncode(value));
}
}
return result;
}

String HttpQuery::GetQuery(HttpQuery patch, bool empty) const
{
String result;
int i, n;
for(i = 0, n = GetCount(); i < n; i++)
if(!IsInternal(i))
{
String key = GetKey(i), value = GetValue(i);
if(patch.Find(key) < 0)
{
if(empty || !value.IsEmpty())
{
if(!result.IsEmpty()) result.Cat('&');
result.Cat(UrlEncode(key));
result.Cat('=');
result.Cat(UrlEncode(value));
}
}
}
for(i = 0, n = patch.GetCount(); i < n; i++)
if(!patch.IsInternal(i))
{
String key = patch.GetKey(i), value = patch.GetValue(i);
if(empty || !value.IsEmpty())
{
if(!result.IsEmpty()) result.Cat('&');
result.Cat(UrlEncode(key));
result.Cat('=');
result.Cat(UrlEncode(value));
}
}

return result;
}

bool HttpQuery::IsEmpty(String key) const
{
int f = Find(key);
return f < 0 || data -> map[f].IsEmpty();
}

bool HttpQuery::IsInternal(int i) const
{
String key = GetKey(i);
return key[0] == '$' && key[1] == '$';
}

void HttpQuery::Get(const String& key, Ref result) const
{
switch(result.GetType())
{
case BOOL_V: result.SetValue(GetBool(key)); break;
case INT_V: RefInt(result) = GetInt(key); break;
case DOUBLE_V: RefDouble(result) = GetDouble(key); break;
case STRING_V: RefString(result) = GetString(key); break;
case DATE_V: RefDate(result) = GetDate(key); break;
case TIME_V: RefTime(result) = GetTime(key); break;
default: NEVER();
}
}

bool HttpQuery::GetBool(const String& key) const
{
int i = Find(key);
return i >= 0 && data -> map[i] != "0";
}

int HttpQuery::GetInt(const String& key) const
{
int i = Find(key);
return i < 0 || data->map[i].IsEmpty() ? (int)Null : atoi(data->map[i]);
}

double HttpQuery::GetDouble(const String& key) const
{
int i = Find(key);
return i < 0 || data->map[i].IsEmpty() ? (double)Null : atof(data->map[i]);
}

String HttpQuery::GetString(const String& key) const
{
int i = Find(key);
return i >= 0 ? data -> map[i] : String::GetVoid();
}

Date HttpQuery::GetDate(const String& key) const
{
int i = Find(key);
return i >= 0 ? Date(StdConvertDate().Scan(data -> map[i])) : Date(Null);
}

Time HttpQuery::GetTime(const String& key) const
{
int i = Find(key);
return i >= 0 ? Time(StdConvertTime().Scan(data -> map[i])) : Time(Null);
}

Color HttpQuery::GetColor(const String& key) const
{
String s = GetString(key);
if(s.GetLength() >= 6)
{
int digit[6];
int i = 0;
while(i < 6 && (digit[i] = ctoi(s[i])) < 16)
i++;
if(i >= 6)
return Color(digit[0] * 16 + digit[1], digit[2] * 16 + digit[3], digit[4] * 16 + digit[5]);
}
return Null;
}

HttpQuery& HttpQuery::SetColor(const String& key, Color c)
{
if(IsNull(c))
SetString(key, Null);
else
SetString(key, NFormat("%02x%02x%02x", c.GetR(), c.GetG(), c.GetB()));
return *this;
}

bool HttpQuery::GetBool(const String& key, bool dflt) const
{
int i = Find(key);
return i >= 0 ? data -> map[i] != "0" : dflt;
}

int HttpQuery::GetInt(const String& key, int min, int max, int dflt) const
{
int v = GetInt(key);
return !IsNull(v) ? minmax(v, min, max) : dflt;
}

double HttpQuery::GetDouble(const String& key, double min, double max, double dflt) const
{
double v = GetDouble(key);
return !IsNull(v) ? minmax(v, min, max) : dflt;
}

String HttpQuery::GetString(const String& key, const String& dflt) const
{
int i = Find(key);
return i >= 0 && !data -> map[i].IsEmpty() ? data -> map[i] : dflt;
}

HttpQuery& HttpQuery::SetBool(const String& key, bool b)
{
return Set(key, b ? "1" : "");
}

HttpQuery& HttpQuery::SetInt(const String& key, int i)
{
return Set(key, IntStr(i));
}

HttpQuery& HttpQuery::SetDouble(const String& key, double f)
{
return Set(key, DblStr(f));
}

HttpQuery& HttpQuery::Set(const String& key, const String& s)
{
int i = Find(key);
if(i < 0 || data -> map[i] != s)
{
Clone();
if(i < 0)
data -> map.Add(key, s);
else
data -> map[i] = s;
}
return *this;
}

HttpQuery& HttpQuery::SetDate(const String& key, Date d)
{
return Set(key, Format(d));
}

HttpQuery& HttpQuery::SetTime(const String& key, Time t)
{
return Set(key, Format(t));
}

HttpQuery& HttpQuery::SetValue(const String& key, const Value& v)
{
return Set(key, StdFormat(v));
}

HttpQuery& HttpQuery::Set(HttpQuery query)
{
if(int n = query.GetCount())
{
Clone();
RefCon<Data> s = query.data;
for(int i = 0; i < n; i++)
Set(s -> map.GetKey(i), s -> map[i]);
}
return *this;
}

HttpQuery& HttpQuery::Remove(const String& key)
{
int i = Find(key);
if(i < 0)
return *this;
if(data -> map.GetCount() == 1)
Clear();
else
{
Clone();
data -> map.Remove(i);
}
return *this;
}

HttpQuery& HttpQuery::Remove(const Vector<String>& keys)
{
if(!data)
return *this;
Index<int> todo;
for(int i = 0; i < keys.GetCount(); i++)
{
int f = data -> map.Find(keys[i]);
if(f >= 0)
todo.FindAdd(f);
}
if(todo.IsEmpty())
return *this;
Clone();
Vector<int> todo_us = todo.PickKeys();
Sort(todo_us);
while(!todo_us.IsEmpty())
data -> map.Remove(todo_us.Pop());
return *this;
}

HttpQuery& HttpQuery::Remove(const Vector<Id>& keys)
{
if(!data)
return *this;
Index<int> todo;
for(int i = 0; i < keys.GetCount(); i++)
{
int f = data -> map.Find(~keys[i]);
if(f >= 0)
todo.FindAdd(f);
}
if(todo.IsEmpty())
return *this;
Clone();
Vector<int> todo_us = todo.PickKeys();
Sort(todo_us);
while(!todo_us.IsEmpty())
data -> map.Remove(todo_us.Pop());
return *this;
}

String HttpQuery::ToString() const
{
String out;
out << "HttpQuery (" << GetCount() << " items)\n";
for(int i = 0; i < GetCount(); i++)
{
out << "[" << i;
if(IsInternal(i))
out << ":internal";
out << "]: <" << GetKey(i) << "> = <";
String value = GetValue(i);
if(value.GetLength() <= 1000)
out << value;
else
{
out.Cat(value, 1000);
out << " ... (length = " << value.GetLength() << ")";
}
out << ">\n";
}
return out;
}

END_UPP_NAMESPACE

Change log

r4789 by cxl on Apr 15, 2012   Diff
Core: Improved inet utilities
Go to: 
Project members, sign in to write a code review

Older revisions

r4785 by cxl on Apr 15, 2012   Diff
Core: SSL support for Socket,
finishing touches...
r4729 by cxl on Mar 30, 2012   Diff
TcpSocket now in Core
r4580 by cxl on Feb 12, 2012   Diff
HttpQuery: CaseSensitive
All revisions of this file

File info

Size: 16168 bytes, 764 lines

File properties

svn:eol-style
native
Powered by Google Project Hosting