forked from buaazp/zimg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zutil.c
336 lines (311 loc) · 7.56 KB
/
zutil.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
/*
* zimg - high performance image storage and processing system.
* http://zimg.buaa.us
*
* Copyright (c) 2013, Peter Zhao <zp@buaa.us>.
* All rights reserved.
*
* Use and distribution licensed under the BSD license.
* See the LICENSE file for full text.
*
*/
/**
* @file zutil.c
* @brief A suit of related functions.
* @author 招牌疯子 zp@buaa.us
* @version 1.0
* @date 2013-07-19
*/
#include <sys/syscall.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "zutil.h"
#include "zlog.h"
//functions list
pid_t gettid();
static void kmp_init(const char *pattern, int pattern_size);
int kmp(const char *matcher, int mlen, const char *pattern, int plen);
int get_type(const char *filename, char *type);
int is_img(const char *filename);
int is_dir(const char *path);
int mk_dir(const char *path);
int mk_dirs(const char *dir);
int is_md5(char *s);
static int htoi(char s[]);
int str_hash(const char *str);
// this data is for KMP searching
static int pi[128];
pid_t gettid()
{
return syscall(SYS_gettid);
}
/* KMP for searching */
static void kmp_init(const char *pattern, int pattern_size) // prefix-function
{
pi[0] = 0; // pi[0] always equals to 0 by defination
int k = 0; // an important pointer
int q;
for(q = 1; q < pattern_size; q++) // find each pi[q] for pattern[q]
{
while(k>0 && pattern[k+1]!=pattern[q])
k = pi[k]; // use previous prefixes to match pattern[0..q]
if(pattern[k+1] == pattern[q]) // if pattern[0..(k+1)] is a prefix
k++; // let k = k + 1
pi[q] = k; // be ware, (0 <= k <= q), and (pi[k] < k)
}
// The worst-case time complexity of this procedure is O(pattern_size)
}
/**
* @brief kmp The kmp algorithm.
*
* @param matcher The buffer.
* @param mlen Buffer length.
* @param pattern The pattern.
* @param plen Pattern length.
*
* @return The place of pattern in buffer.
*/
int kmp(const char *matcher, int mlen, const char *pattern, int plen)
{
// this function does string matching using the KMP algothm.
// matcher and pattern are pointers to BINARY sequencies, while mlen
// and plen are their lengths respectively.
// if a match is found, return its position immediately.
// return -1 if no match can be found.
if(!mlen || !plen || mlen < plen) // take care of illegal parameters
return -1;
kmp_init(pattern, plen); // prefix-function
int i=0, j=0;
while(i < mlen && j < plen) // don't increase i and j at this level
{
if(matcher[i+j] == pattern[j])
j++;
else if(j == 0) // dismatch: matcher[i] vs pattern[0]
i++;
else // dismatch: matcher[i+j] vs pattern[j], and j>0
{
i = i + j - pi[j-1]; // i: jump forward by (j - pi[j-1])
j = pi[j-1]; // j: reset to the proper position
}
}
if(j == plen) // found a match!!
{
return i;
}
else // if no match was found
return -1;
}
/**
* @brief get_type It tell you the type of a file.
*
* @param filename The name of the file.
* @param type Save the type string.
*
* @return 1 for success and -1 for fail.
*/
int get_type(const char *filename, char *type)
{
char *flag, *tmp;
if((flag = strchr(filename, '.')) == 0)
{
LOG_PRINT(LOG_WARNING, "FileName [%s] Has No '.' in It.", filename);
return -1;
}
while((tmp = strchr(flag + 1, '.')) != 0)
{
flag = tmp;
}
flag++;
sprintf(type, "%s", flag);
return 1;
}
/**
* @brief is_img Check a file is a image we support(jpg, png, gif).
*
* @param filename The name of the file.
*
* @return 1 for success and 0 for fail.
*/
int is_img(const char *filename)
{
char *imgType[] = {"jpg", "jpeg", "png", "gif"};
char *lower= (char *)malloc(strlen(filename) + 1);
char *tmp;
int i;
int isimg = 0;
for(i = 0; i < strlen(filename); i++)
{
lower[i] = tolower(filename[i]);
}
lower[strlen(filename)] = '\0';
for(i = 0; i < 4; i++)
{
LOG_PRINT(LOG_INFO, "compare %s - %s.", lower, imgType[i]);
if((tmp = strstr(lower, imgType[i])) == lower)
{
isimg = 1;
break;
}
}
free(lower);
return isimg;
}
/**
* @brief is_dir Check a path is a directory.
*
* @param path The path input.
*
* @return 1 for yes and -1 for no.
*/
int is_dir(const char *path)
{
struct stat st;
if(stat(path, &st)<0)
{
LOG_PRINT(LOG_WARNING, "Path[%s] is Not Existed!", path);
return -1;
}
if(S_ISDIR(st.st_mode))
{
LOG_PRINT(LOG_INFO, "Path[%s] is A Dir.", path);
return 1;
}
else
return -1;
}
/**
* @brief mk_dir It create a new directory with the path input.
*
* @param path The path you want to create.
*
* @return 1 for success and -1 for fail.
*/
int mk_dir(const char *path)
{
if(access(path, 0) == -1)
{
LOG_PRINT(LOG_INFO, "Begin to mk_dir()...");
int status = mkdir(path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
if(status == -1)
{
LOG_PRINT(LOG_WARNING, "mkdir[%s] Failed!", path);
return -1;
}
LOG_PRINT(LOG_INFO, "mkdir[%s] sucessfully!", path);
return 1;
}
else
{
LOG_PRINT(LOG_WARNING, "Path[%s] is Existed!", path);
return -1;
}
}
/**
* @brief mk_dirs It creates a multi-level directory like ./img/330/28/557.
*
* @param dir The path of a multi-level directory.
*
* @return 1 for success and -1 for fail.
*/
int mk_dirs(const char *dir)
{
char tmp[512];
char *p;
if (strlen(dir) == 0 || dir == NULL)
{
LOG_PRINT(LOG_WARNING, "strlen(dir) is 0 or dir is NULL.");
return -1;
}
memset(tmp, 0, sizeof(tmp));
strncpy(tmp, dir, strlen(dir));
if (tmp[0] == '/' && tmp[1]== '/')
p = strchr(tmp + 2, '/');
else
p = strchr(tmp, '/');
if (p)
{
*p = '\0';
mkdir(tmp,0777);
chdir(tmp);
}
else
{
mkdir(tmp,0777);
chdir(tmp);
return 1;
}
mk_dirs(p + 1);
return 1;
}
/**
* @brief is_md5 Check the string is a md5 style.
*
* @param s The string.
*
* @return 1 for yes and -1 for no.
*/
int is_md5(char *s)
{
int rst = -1;
int i = 0;
for (; (s[i] >= '0' && s[i] <= '9') || (s[i] >= 'a' && s[i] <= 'f') || (s[i] >='A' && s[i] <= 'F');++i)
{
}
if(i == 32 && s[i] == '\0')
rst = 1;
return rst;
}
/**
* @brief htoi Exchange a hexadecimal string to a number.
*
* @param s[] The hexadecimal string.
*
* @return The number in the string.
*/
static int htoi(char s[])
{
int i;
int n = 0;
if (s[0] == '0' && (s[1]=='x' || s[1]=='X'))
{
i = 2;
}
else
{
i = 0;
}
for (; (s[i] >= '0' && s[i] <= '9') || (s[i] >= 'a' && s[i] <= 'z') || (s[i] >='A' && s[i] <= 'Z');++i)
{
if (s[i] > '9')
{
n = 16 * n + (10 + s[i] - 'a');
}
else
{
n = 16 * n + (s[i] - '0');
}
}
return n;
}
/**
* @brief str_hash Hash algorithm of processing a md5 string.
*
* @param str The md5 string.
*
* @return The number less than 1024.
*/
int str_hash(const char *str)
{
char c[4];
strncpy(c, str, 3);
c[3] = '\0';
LOG_PRINT(LOG_INFO, "str = %s.", c);
//int d = htoi(c);
int d = strtol(c, NULL, 16);
LOG_PRINT(LOG_INFO, "str(3)_to_d = %d.", d);
d = d / 4;
LOG_PRINT(LOG_INFO, "str(3)/4 = %d.", d);
return d;
}