-
Notifications
You must be signed in to change notification settings - Fork 40
/
utils.c
1152 lines (965 loc) · 23.7 KB
/
utils.c
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
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* These are helping routines for the main module of CNTLM
*
* CNTLM is free software; you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version.
*
* CNTLM 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 General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
* St, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2007 David Kubicek
*
*/
#include "config/config.h"
#if config_memset_s == 1
#define __STDC_WANT_LIB_EXT1__ 1
#endif
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <ctype.h>
#include <syslog.h>
#include <assert.h>
#ifdef __CYGWIN__
#include <windows.h>
#include <wincrypt.h>
#endif
#include "swap.h"
#include "utils.h"
#include "socket.h"
#include "globals.h"
static const char hextab[17] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 0};
static const int hexindex[128] = {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,0,1,2,3,4,5,6,7,8,9,-1,-1,-1,-1,-1,-1,
-1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,10,11,12,13,14,15,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1};
void myexit(int rc) {
if (rc)
fprintf(stderr, "Exiting with error. Check daemon logs or run with -v.\n");
exit(rc);
}
void croak(const char *msg, const int console) {
if (console)
printf("%s", msg);
else
syslog(LOG_ERR, "%s", msg);
myexit(1);
}
/*
* Add a new item to a list. Every plist_t variable must be
* initialized to NULL (or pass NULL for "list" when adding
* the first item). This is for simplicity's sake (we don't
* need any plist_new).
*
* This list type allows to store an arbitrary pointer
* associating it with the key.
*/
plist_t plist_add(plist_t list, const unsigned long key, void *aux) {
plist_t tmp;
plist_t t = list;
tmp = malloc(sizeof(struct plist_s));
tmp->key = key;
tmp->aux = aux;
tmp->next = NULL;
if (list == NULL)
return tmp;
while (t->next)
t = t->next;
t->next = tmp;
return list;
}
/*
* Delete an item from the list, possibly returning NULL when
* the list is empty or nothing was found.
*/
plist_t plist_del(plist_t list, const unsigned long key) {
plist_t ot = NULL;
plist_t t = list;
while (t) {
if (t->key == key)
break;
ot = t;
t = t->next;
}
if (t) {
plist_t tmp = t->next;
if (t->aux)
free(t->aux);
free(t);
if (ot == NULL)
return tmp;
ot->next = tmp;
}
return list;
}
/*
* Return true if an item is present in the list.
*/
int plist_in(plist_const_t list, const unsigned long key) {
plist_const_t t = list;
while (t) {
if (t->key == key)
break;
t = t->next;
}
return (t != NULL);
}
/*
* For debugging purposes - dump the entire contents
* of a list.
*/
void plist_dump(plist_const_t list) {
plist_const_t t;
t = list;
while (t) {
printf("List data: %lu => 0x%8p\n", (unsigned long int)t->key, t->aux);
t = t->next;
}
}
/*
* Return the pointer associated with the key.
*/
char *plist_get(plist_const_t list, const unsigned long key) {
plist_const_t t = list;
while (t) {
if (t->key == key)
break;
t = t->next;
}
return (t == NULL ? NULL : t->aux);
}
/*
* Scan the list for an open descriptor (socket), possibly
* discarding all closed ones on the way. Return the first
* match.
*
* Use this method only for lists of descriptors!
*
* In conjunction with plist_add, the list behaves as a FIFO.
* This feature is used for rotating cached connections in the
* list, so that none is left too long unused (proxy timeout).
*
* Returns key value (descriptor) and if aux != NULL, *aux gets
* aux pointer value (which caller must free if != NULL).
*/
int plist_pop(plist_t *list, void **aux) {
plist_t tmp;
plist_t t;
int id = 0;
int ok = 0;
void *a = NULL;
if (list == NULL || *list == NULL)
return 0;
t = *list;
while (!ok && t) {
id = t->key;
a = t->aux;
tmp = t->next;
if (so_closed(id)) {
close(id);
if (t->aux)
free(t->aux);
} else
ok = 1;
free(t);
t = tmp;
}
*list = t;
if (ok) {
if (aux != NULL)
*aux = a;
return id;
}
return 0;
}
/*
* Return the number of items in a list.
*/
int plist_count(plist_const_t list) {
plist_const_t t = list;
int rc = 0;
while (t) {
rc++;
t = t->next;
}
return rc;
}
/*
* Free the list.
*/
plist_t plist_free(plist_t list) {
plist_t t = list;
while (list) {
t = list->next;
if (list->aux)
free(list->aux);
free(list);
list = t;
}
return NULL;
}
/*
* The same as plist_add. Here we have two other arguments.
* They are boolean flags - HLIST_ALLOC means to duplicate a
* key/value, HLIST_NOALLOC means to store the pointer directly.
*
* Caller decides this on a by-call basis. Part of the manipulation
* routines is a "free". That method always deallocates both the
* key and the value. So for static or temporary keys/values,
* the caller can instruct us to duplicate the necessary amount
* of heap. This mechanism is used to minimize memory-related
* bugs throughout the code and tons of free's.
*/
hlist_t hlist_add(hlist_t list, char *key, char *value, hlist_add_t allockey, hlist_add_t allocvalue) {
hlist_t tmp;
hlist_t t = list;
if (key == NULL || value == NULL)
return list;
tmp = malloc(sizeof(struct hlist_s));
tmp->key = (allockey == HLIST_ALLOC ? strdup(key) : key);
tmp->value = (allocvalue == HLIST_ALLOC ? strdup(value) : value);
tmp->next = NULL;
tmp->islist = 0;
if (list == NULL)
return tmp;
while (t->next)
t = t->next;
t->next = tmp;
return list;
}
/*
* Return a duplicate of the list (copy).
*/
hlist_t hlist_dup(hlist_const_t list) {
hlist_t tmp = NULL;
hlist_const_t t = list;
while (t) {
tmp = hlist_add(tmp, t->key, t->value, HLIST_ALLOC, HLIST_ALLOC);
t = t->next;
}
return tmp;
}
/*
* Remove an item from the list.
*/
hlist_t hlist_del(hlist_t list, const char *key) {
hlist_t ot = NULL;
hlist_t t = list;
while (t) {
if (!strcasecmp(t->key, key))
break;
ot = t;
t = t->next;
}
if (t) {
hlist_t tmp = t->next;
free(t->key);
free(t->value);
free(t);
if (ot == NULL)
return tmp;
ot->next = tmp;
}
return list;
}
/*
* Change the value of a key. If add is true, we store it in the
* list if the key is not found. Unlike hlist_add, which offers
* pointer storage or memory duplication for both the key and the
* value separately, hlist_mod always duplicates.
*
* Used to add a header, which might already be present.
*/
hlist_t hlist_mod(hlist_t list, char *key, char *value, int add) {
hlist_t t = list;
while (t) {
if (!strcasecmp(t->key, key))
break;
t = t->next;
}
if (t) {
free(t->value);
t->value = strdup(value);
} else if (add) {
list = hlist_add(list, key, value, HLIST_ALLOC, HLIST_ALLOC);
}
return list;
}
/*
* Return true if the key is in the list.
*/
int hlist_in(hlist_const_t list, const char *key) {
hlist_const_t t = list;
while (t) {
if (!strcasecmp(t->key, key))
break;
t = t->next;
}
return (t != NULL);
}
/*
* Return the number of items in a list.
*/
int hlist_count(hlist_const_t list) {
hlist_const_t t = list;
int rc = 0;
while (t) {
rc++;
t = t->next;
}
return rc;
}
/*
* Return the value for the key.
*/
char *hlist_get(hlist_const_t list, const char *key) {
hlist_const_t t = list;
while (t) {
if (!strcasecmp(t->key, key))
break;
t = t->next;
}
return (t == NULL ? NULL : t->value);
}
/*
* Test if substr is part of the header's value.
* Both case-insensitive.
*/
int hlist_subcmp(hlist_const_t list, const char *key, const char *substr) {
int found = 0;
char *tmp;
char *low;
lowercase(low = strdup(substr));
tmp = hlist_get(list, key);
if (tmp) {
lowercase(tmp = strdup(tmp));
if (strstr(tmp, low))
found = 1;
free(tmp);
}
free(low);
return found;
}
/*
* Test if substr is part of the header's value.
* Both case-insensitive, checks all headers, not just first one.
*/
int hlist_subcmp_all(hlist_const_t list, const char *key, const char *substr) {
hlist_const_t t = list;
int found = 0;
char *tmp;
char *low;
assert(key != NULL);
assert(substr != NULL);
lowercase(low = strdup(substr));
while (t) {
if (!strcasecmp(t->key, key)) {
lowercase(tmp = strdup(t->value));
if (strstr(tmp, low))
found = 1;
free(tmp);
}
t = t->next;
}
free(low);
return found;
}
/*
* Free the list. For more about list memory management,
* see hlist_add.
*/
hlist_t hlist_free(hlist_t list) {
hlist_t t = list;
while (list) {
t = list->next;
free(list->key);
free(list->value);
free(list);
list = t;
}
return NULL;
}
/*
* This is for debugging purposes.
*/
void hlist_dump(hlist_const_t list) {
hlist_const_t t;
t = list;
while (t) {
printf("%-30s => %s\n", t->key, t->value);
t = t->next;
}
}
/*
* Standard substr. To prevent modification of the source
* (terminating \x0), return the result in a new memory.
*/
char *substr(const char *src, int pos, int len) {
int min_len;
char *tmp;
assert(src != NULL);
assert(pos >= 0);
assert(len >= 0);
if (len == 0)
len = strlen(src);
min_len = MIN(len, (int)strlen(src)-pos);
if (min_len <= 0)
return zmalloc(1);
tmp = zmalloc(min_len+1);
strlcpy(tmp, src+pos, min_len+1);
return tmp;
}
/*
* Allocate memory and initialize a new rr_data_t structure.
*/
rr_data_t new_rr_data(void) {
rr_data_t data;
data = malloc(sizeof(struct rr_data_s));
data->req = 0;
data->code = 0;
data->skip_http = 0;
data->body_len = 0;
data->empty = 1;
data->port = 0;
data->http_version = -1;
data->headers = NULL;
data->method = NULL;
data->url = NULL;
data->rel_url = NULL;
data->hostname = NULL;
data->http = NULL;
data->msg = NULL;
data->body = NULL;
data->errmsg = NULL; /* for static strings - we don't free, dup, nor copy */
return data;
}
/*
* Copy the req/res data.
*/
rr_data_t copy_rr_data(rr_data_t dst, const rr_data_const_t src) {
if (dst == NULL) {
fprintf(stderr, "Internal error in copy_rr_data: Pointer dst is NULL\n");
assert(0);
return NULL;
}
if (src == NULL) {
fprintf(stderr, "Internal error in copy_rr_data: Pointer src is NULL\n");
assert(0);
return NULL;
}
reset_rr_data(dst);
dst->req = src->req;
dst->code = src->code;
dst->skip_http = src->skip_http;
dst->body_len = src->body_len;
dst->empty = src->empty;
dst->port = src->port;
dst->http_version = src->http_version;
if (src->headers)
dst->headers = hlist_dup(src->headers);
if (src->method)
dst->method = strdup(src->method);
if (src->url)
dst->url = strdup(src->url);
if (src->rel_url)
dst->rel_url = strdup(src->rel_url);
if (src->hostname)
dst->hostname = strdup(src->hostname);
if (src->http)
dst->http = strdup(src->http);
if (src->msg)
dst->msg = strdup(src->msg);
if (src->body && src->body_len > 0) {
dst->body = zmalloc(src->body_len);
memcpy(dst->body, src->body, src->body_len);
}
return dst;
}
/*
* Duplicate the req/res data.
*/
rr_data_t dup_rr_data(const rr_data_const_t data) {
rr_data_t tmp;
if (data == NULL) {
fprintf(stderr, "Internal error in dup_rr_data: Pointer data is NULL\n");
assert(0);
return NULL;
}
tmp = new_rr_data();
return copy_rr_data(tmp, data);
}
/*
* Reset, freeing if necessary
*/
rr_data_t reset_rr_data(rr_data_t data) {
if (data == NULL) {
fprintf(stderr, "Internal error in reset_rr_data: Pointer data is NULL\n");
assert(0);
return NULL;
}
data->req = 0;
data->code = 0;
data->skip_http = 0;
data->body_len = 0;
data->empty = 1;
data->port = 0;
data->http_version = -1;
if (data->headers) hlist_free(data->headers);
if (data->method) free(data->method);
if (data->url) free(data->url);
if (data->rel_url) free(data->rel_url);
if (data->hostname) free(data->hostname);
if (data->http) free(data->http);
if (data->msg) free(data->msg);
if (data->body) free(data->body);
data->headers = NULL;
data->method = NULL;
data->url = NULL;
data->rel_url = NULL;
data->hostname = NULL;
data->http = NULL;
data->msg = NULL;
data->body = NULL;
data->errmsg = NULL;
return data;
}
/*
* Free rr_data_t structure. We also take care of freeing
* the memory of its members.
*/
void free_rr_data(rr_data_t * pdata) {
if (pdata == NULL) {
fprintf(stderr, "Internal error in free_rr_data: Pointer pdata is NULL\n");
assert(0);
return;
}
rr_data_t data = *pdata;
if (data == NULL)
return;
if (data->headers) hlist_free(data->headers);
if (data->method) free(data->method);
if (data->url) free(data->url);
if (data->rel_url) free(data->rel_url);
if (data->hostname) free(data->hostname);
if (data->http) free(data->http);
if (data->msg) free(data->msg);
if (data->body) free(data->body);
free(data);
data = NULL;
}
/*
* Cut the whitespace at the end of a string.
*/
char *trimr(char * const buf) {
int i;
assert(buf != NULL);
for (i = strlen(buf)-1; i >= 0 && isspace((u_char)buf[i]); --i);
buf[i+1] = 0;
return buf;
}
#if config_strdup == 0
/*
* Our implementation of non-POSIX strdup()
*/
char *strdup(const char *src) {
size_t len;
char *tmp;
if (!src)
return NULL;
len = strlen(src)+1;
tmp = calloc(1, len);
memcpy(tmp, src, len-1);
return tmp;
}
#endif
#if config_strlcpy == 0
/*
* More intuitive version of strncpy with string termination
* from OpenBSD
*/
size_t strlcpy(char *dst, const char *src, size_t siz) {
char *d = dst;
const char *s = src;
size_t n = siz;
assert(dst != NULL);
assert(src != NULL);
/* Copy as many bytes as will fit */
if (n != 0) {
while (--n != 0) {
if ((*d++ = *s++) == '\0')
break;
}
}
/* Not enough room in dst, add NUL and traverse rest of src */
if (n == 0) {
if (siz != 0)
*d = '\0'; /* NUL-terminate dst */
while (*s++);
}
return (s - src - 1); /* count does not include NUL */
}
#endif
#if config_strlcat == 0
/*
* More intuitive version of strncat with string termination
* from OpenBSD
*/
size_t strlcat(char *dst, const char *src, size_t siz) {
char *d = dst;
const char *s = src;
size_t n = siz;
size_t dlen;
assert(dst != NULL);
assert(src != NULL);
assert(siz > 0);
/* Find the end of dst and adjust bytes left but don't go past end */
while (n-- != 0 && *d != '\0')
d++;
dlen = d - dst;
n = siz - dlen;
if (n == 0)
return(dlen + strlen(s));
while (*s != '\0') {
if (n != 1) {
*d++ = *s;
n--;
}
s++;
}
*d = '\0';
return (dlen + (s - src)); /* count does not include NUL */
}
#endif
/*
* Allocates memory and makes sure it is zero initialized.
*/
char *zmalloc(size_t size) {
char *tmp = calloc(1, size);
return tmp;
}
/*
* Self-explanatory.
*/
char *lowercase(char * const str) {
size_t i;
assert(str != NULL);
for (i = 0; i < strlen(str); ++i)
str[i] = tolower(str[i]);
return str;
}
/*
* Self-explanatory.
*/
char *uppercase(char * const str) {
size_t i;
assert(str != NULL);
for (i = 0; i < strlen(str); ++i)
str[i] = toupper(str[i]);
return str;
}
int unicode(char **dst, const char * const src) {
char *tmp;
int l;
int i;
if (!src) {
*dst = NULL;
return 0;
}
l = MIN(BUFSIZE, strlen(src));
tmp = zmalloc(2*l);
for (i = 0; i < l; ++i)
tmp[2*i] = src[i];
*dst = tmp;
return 2*l;
}
char *urlencode(const char * const str) {
char *tmp;
size_t i;
size_t pos;
assert(str != NULL);
const size_t str_length = strlen(str);
const size_t tmp_length = str_length * 3 + 1;
tmp = zmalloc(tmp_length);
for (pos = 0, i = 0; i < str_length; ++i) {
if (isdigit((u_char)str[i]) || (tolower(str[i]) >= 'a' && tolower(str[i]) <= 'z') || str[i] == '.' || str[i] == '-' || str[i] == '_' || str[i] == '~') {
tmp[pos] = str[i];
++pos;
} else {
snprintf(tmp+pos, tmp_length - pos, "%%%X", (unsigned char)str[i]);
pos += 3;
}
}
return tmp;
}
char *printmem(const char * const src, const size_t len, const int bitwidth) {
char *tmp;
size_t i;
tmp = zmalloc(2*len+1);
for (i = 0; i < len; ++i) {
uint8_t val = (uint8_t)src[i] & (0xFF >> (8-bitwidth));
tmp[i*2] = hextab[val >> 4];
tmp[i*2+1] = hextab[val & 0x0F];
}
return tmp;
}
char *scanmem(const char * const src, const int bitwidth) {
int h, l;
size_t i;
size_t bytes;
char *tmp;
if (strlen(src) % 2)
return NULL;
bytes = strlen(src)/2;
tmp = zmalloc(bytes+1);
for (i = 0; i < bytes; ++i) {
h = hexindex[(int)src[i*2]];
l = hexindex[(int)src[i*2+1]];
if (h < 0 || l < 0) {
free(tmp);
return NULL;
}
tmp[i] = ((h << 4) + l) & (0xFF >> (8-bitwidth));
}
tmp[i] = 0;
return tmp;
}
/**
* Checks if the given memory contains only zeros.
*
* @param p_memory Pointer to the memory that is checked.
* @param length Number of bytes to check for zero.
* @return 0 means false (not only zeros), 1 means true (only zeros)
*/
int is_memory_all_zero(const void * const p_memory, const size_t length) {
for(size_t i = 0; i < length; ++i) {
if(((const unsigned char * const)p_memory)[i] != 0) {
return 0;
}
}
return 1;
}
/*
* BASE64 CODE FROM MUTT BEGIN - ORIGINAL COPYRIGHT APPLIES:
*
* Copyright (C) 1996-2001 Michael R. Elkins <[email protected]>
* Copyright (C) 1996-2001 Brandon Long <[email protected]>
* Copyright (C) 1997-2001 Thomas Roessler <[email protected]>
* Copyright (C) 1998-2001 Werner Koch <[email protected]>
* Copyright (C) 1999-2001 Brendan Cully <[email protected]>
* Copyright (C) 1999-2001 Tommi Komulainen <[email protected]>
* Copyright (C) 2000-2001 Edmund Grimley Evans <[email protected]>
*
*/
#define BAD -1
#define base64val(c) index64[(unsigned int)(c)]
static const char base64[64] = {
'A','B','C','D','E','F','G','H','I','J','K','L','M','N',
'O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b',
'c','d','e','f','g','h','i','j','k','l','m','n','o','p',
'q','r','s','t','u','v','w','x','y','z','0','1','2','3',
'4','5','6','7','8','9','+','/'
};
static const int index64[128] = {
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,62,-1,-1,-1,63,52,53,54,55,56,57,58,59,60,
61,-1,-1,-1,-1,-1,-1,-1,0,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,-1,-1,-1,-1,-1,-1,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,-1,-1,-1,-1,-1
};
void to_base64(unsigned char *out, const unsigned char *in, size_t len, size_t olen) {
while (len >= 3 && olen > 10) {
*out++ = base64[in[0] >> 2];
*out++ = base64[((in[0] << 4) & 0x30) | (in[1] >> 4)];
*out++ = base64[((in[1] << 2) & 0x3c) | (in[2] >> 6)];
*out++ = base64[in[2] & 0x3f];
olen -= 4;
len -= 3;
in += 3;
}
/* clean up remainder */
if (len > 0 && olen > 4) {
unsigned char fragment;
*out++ = base64[in[0] >> 2];
fragment = (in[0] << 4) & 0x30;
if (len > 1)
fragment |= in[1] >> 4;
*out++ = base64[fragment];
*out++ = (len < 2) ? '=' : base64[(in[1] << 2) & 0x3c];
*out++ = '=';
}
*out = '\0';
}
/* Convert '\0'-terminated base 64 string to raw bytes.
* Returns length of returned buffer, or -1 on error */
int from_base64(char *out, const char *in)
{
int len = 0;
register unsigned char digit1, digit2, digit3, digit4;
do {
digit1 = in[0];
if (digit1 > 127 || base64val (digit1) == BAD)
return -1;
digit2 = in[1];
if (digit2 > 127 || base64val (digit2) == BAD)
return -1;
digit3 = in[2];
if (digit3 > 127 || ((digit3 != '=') && (base64val (digit3) == BAD)))
return -1;
digit4 = in[3];
if (digit4 > 127 || ((digit4 != '=') && (base64val (digit4) == BAD)))
return -1;