aboutsummaryrefslogtreecommitdiff
path: root/src/utils.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils.c')
-rw-r--r--src/utils.c155
1 files changed, 155 insertions, 0 deletions
diff --git a/src/utils.c b/src/utils.c
new file mode 100644
index 00000000..1ca797f7
--- /dev/null
+++ b/src/utils.c
@@ -0,0 +1,155 @@
1/* Copyrights 2002 Luis Figueiredo (stdio@netc.pt) All rights reserved.
2 *
3 * See the LICENSE file
4 *
5 * The origin of this software must not be misrepresented, either by
6 * explicit claim or by omission. Since few users ever read sources,
7 * credits must appear in the documentation.
8 *
9 * date: 19:49,07-49-2002
10 *
11 * -- description: utilitys
12 *
13 */
14
15#include "utils.h"
16
17/*********************************************************************************************************/
18/*
19 * search a string in a string ignoring case
20 */
21char *__ILWS_stristr(char *str, const char *nedle) {
22 char *tmp1=str;
23 int ret=1;
24 int snedle=strlen(nedle),sstr=strlen(str);
25 if(strlen(str)<strlen(nedle))return NULL;
26 while((ret=strncasecmp(tmp1,nedle,snedle) && (unsigned int)(tmp1+snedle)<=(unsigned int) (str+sstr))) {
27 tmp1++;
28 };
29 if(strncasecmp(tmp1,nedle,snedle))
30 return NULL;
31 return tmp1;
32};
33
34/*********************************************************************************************************/
35/*
36 * gives a new temporary path(file) name that doesn't exists
37 */
38char *_tmpnameprefix="";
39
40char *__ILWS_tmpfname() {
41 char *ret=NULL;
42 char *tmpdir=NULL;
43 char nam[TMPNAMESIZE+1];
44 int i;
45 struct stat foostat;
46 if(tmpdir==NULL) {
47 tmpdir=getenv("TEMP");
48 };
49 if(tmpdir==NULL) {
50 tmpdir=getenv("TMP");
51 };
52 if(tmpdir==NULL) {
53 tmpdir=getenv("TMPDIR");
54 };
55 if(tmpdir==NULL) {
56 tmpdir=P_tmpdir; // defined in stdio.h
57 };
58 IFDEBUG(fprintf(stderr,"utils.c: Allocating temporary file name: "));
59 if(!(ret=__ILWS_malloc(strlen(tmpdir)+strlen(_tmpnameprefix)+TMPNAMESIZE+2))) {
60 LWSERR(LE_MEMORY);
61 return NULL;
62 };
63 srand(time(NULL)); // seed
64 do {
65 for(i=0;i<TMPNAMESIZE;i++) {
66
67 nam[i]=(rand()%2)?(rand()%26)+'A':(rand()%26)+'a';
68 }
69 nam[i]=0;
70 snprintf(ret,strlen(tmpdir)+strlen(_tmpnameprefix)+TMPNAMESIZE+2,"%s/%s%s",tmpdir,_tmpnameprefix,nam); // include '0'
71 IFDEBUG(fprintf(stderr,"Temporary filename is: %s, stat:%d\n",ret,stat(ret,&foostat)));
72 }while((stat(ret,&foostat)!=-1) && (lstat(ret,&foostat)!=-1)); // redundancy if win32 // <- race condition?
73 return ret;
74};
75
76
77
78/*********************************************************************************************************/
79/*
80 * an date function
81 */
82#define DATE_MAX 100
83char __ILWS_datem[DATE_MAX];
84
85char *__ILWS_date(time_t t,const char *format) {
86 struct tm *tm;
87 tm=localtime(&t);
88 strftime(__ILWS_datem,DATE_MAX,format,tm);
89 return __ILWS_datem;
90}
91
92/*********************************************************************************************************/
93/*
94 * wasn't me, base64decode
95 */
96static const unsigned char __ILWS_chtb[256] = {
97 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
98 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
99 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 64, 64, 63,
100 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 64, 64, 64,
101 64, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
102 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 64, 64, 64, 64, 64,
103 64, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
104 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 64, 64, 64, 64, 64,
105 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
106 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
107 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
108 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
109 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
110 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
111 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
112 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64};
113
114
115int __ILWS_base64decode(char *bufplain, const char *bufcoded){
116 int nb;
117 const unsigned char *in;
118 unsigned char *out;
119 int nprbytes;
120
121 in = (const unsigned char *) bufcoded;
122 while (__ILWS_chtb[*(in++)] <= 63);
123 nprbytes = (in - (const unsigned char *) bufcoded) - 1;
124 nb = ((nprbytes + 3) / 4) * 3;
125
126 out = (unsigned char *) bufplain;
127 in = (const unsigned char *) bufcoded;
128
129 while (nprbytes > 4) {
130 *(out++) =
131 (unsigned char) (__ILWS_chtb[*in] << 2 | __ILWS_chtb[in[1]] >> 4);
132 *(out++) =
133 (unsigned char) (__ILWS_chtb[in[1]] << 4 | __ILWS_chtb[in[2]] >> 2);
134 *(out++) =
135 (unsigned char) (__ILWS_chtb[in[2]] << 6 | __ILWS_chtb[in[3]]);
136 in += 4;
137 nprbytes -= 4;
138 }
139 if (nprbytes > 1) {
140 *(out++) =
141 (unsigned char) (__ILWS_chtb[*in] << 2 | __ILWS_chtb[in[1]] >> 4);
142 }
143 if (nprbytes > 2) {
144 *(out++) =
145 (unsigned char) (__ILWS_chtb[in[1]] << 4 | __ILWS_chtb[in[2]] >> 2);
146 }
147 if (nprbytes > 3) {
148 *(out++) =
149 (unsigned char) (__ILWS_chtb[in[2]] << 6 | __ILWS_chtb[in[3]]);
150 }
151
152 nb -= (4 - nprbytes) & 3;
153 return nb;
154}
155