forked from azatoth/minidlna
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.c
436 lines (383 loc) · 8.58 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
/* MiniDLNA media server
* Copyright (C) 2008-2009 Justin Maggard
*
* This file is part of MiniDLNA.
*
* MiniDLNA is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* MiniDLNA 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 MiniDLNA. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include "minidlnatypes.h"
#include "upnpglobalvars.h"
#include "log.h"
inline int
strcatf(struct string_s *str, const char *fmt, ...)
{
int ret;
va_list ap;
va_start(ap, fmt);
ret = vsnprintf(str->data + str->off, str->size - str->off, fmt, ap);
str->off += ret;
va_end(ap);
return ret;
}
inline void
strncpyt(char *dst, const char *src, size_t len)
{
strncpy(dst, src, len);
dst[len-1] = '\0';
}
inline int
xasprintf(char **strp, char *fmt, ...)
{
va_list args;
int ret;
va_start(args, fmt);
ret = vasprintf(strp, fmt, args);
va_end(args);
if( ret < 0 )
{
DPRINTF(E_WARN, L_GENERAL, "xasprintf: allocation failed\n");
*strp = NULL;
}
return ret;
}
int
ends_with(const char * haystack, const char * needle)
{
const char * end;
int nlen = strlen(needle);
int hlen = strlen(haystack);
if( nlen > hlen )
return 0;
end = haystack + hlen - nlen;
return (strcasecmp(end, needle) ? 0 : 1);
}
char *
trim(char *str)
{
int i;
int len;
if (!str)
return(NULL);
len = strlen(str);
for (i=len-1; i >= 0 && isspace(str[i]); i--)
{
str[i] = '\0';
len--;
}
while (isspace(*str))
{
str++;
len--;
}
if (str[0] == '"' && str[len-1] == '"')
{
str[0] = '\0';
str[len-1] = '\0';
str++;
}
return str;
}
/* Find the first occurrence of p in s, where s is terminated by t */
char *
strstrc(const char *s, const char *p, const char t)
{
char *endptr;
size_t slen, plen;
endptr = strchr(s, t);
if (!endptr)
return strstr(s, p);
plen = strlen(p);
slen = endptr - s;
while (slen >= plen)
{
if (*s == *p && strncmp(s+1, p+1, plen-1) == 0)
return (char*)s;
s++;
slen--;
}
return NULL;
}
char *
strcasestrc(const char *s, const char *p, const char t)
{
char *endptr;
size_t slen, plen;
endptr = strchr(s, t);
if (!endptr)
return strcasestr(s, p);
plen = strlen(p);
slen = endptr - s;
while (slen >= plen)
{
if (*s == *p && strncasecmp(s+1, p+1, plen-1) == 0)
return (char*)s;
s++;
slen--;
}
return NULL;
}
char *
modifyString(char * string, const char * before, const char * after, short like)
{
int oldlen, newlen, chgcnt = 0;
char *s, *p, *t;
oldlen = strlen(before);
newlen = strlen(after);
if( newlen+like > oldlen )
{
s = string;
while( (p = strstr(s, before)) )
{
chgcnt++;
s = p+oldlen;
}
s = realloc(string, strlen(string)+((newlen-oldlen)*chgcnt)+1+like);
/* If we failed to realloc, return the original alloc'd string */
if( s )
string = s;
else
return string;
}
s = string;
while( s )
{
p = strcasestr(s, before);
if( !p )
return string;
memmove(p + newlen, p + oldlen, strlen(p + oldlen) + 1);
memcpy(p, after, newlen);
if( like )
{
t = p+newlen;
while( isspace(*t) )
t++;
if( *t == '"' )
{
if( like == 2 )
{
memmove(t+2, t+1, strlen(t+1)+1);
*++t = '%';
}
while( *++t != '"' )
continue;
memmove(t+1, t, strlen(t)+1);
*t = '%';
}
}
s = p + newlen;
}
return string;
}
char *
escape_tag(const char *tag, int force_alloc)
{
char *esc_tag = NULL;
if( strchr(tag, '&') || strchr(tag, '<') || strchr(tag, '>') || strchr(tag, '"') )
{
esc_tag = strdup(tag);
esc_tag = modifyString(esc_tag, "&", "&amp;", 0);
esc_tag = modifyString(esc_tag, "<", "&lt;", 0);
esc_tag = modifyString(esc_tag, ">", "&gt;", 0);
esc_tag = modifyString(esc_tag, "\"", "&quot;", 0);
}
else if( force_alloc )
esc_tag = strdup(tag);
return esc_tag;
}
void
strip_ext(char * name)
{
char * period;
period = strrchr(name, '.');
if( period )
*period = '\0';
}
/* Code basically stolen from busybox */
int
make_dir(char * path, mode_t mode)
{
char * s = path;
char c;
struct stat st;
do {
c = '\0';
/* Before we do anything, skip leading /'s, so we don't bother
* trying to create /. */
while (*s == '/')
++s;
/* Bypass leading non-'/'s and then subsequent '/'s. */
while (*s) {
if (*s == '/') {
do {
++s;
} while (*s == '/');
c = *s; /* Save the current char */
*s = '\0'; /* and replace it with nul. */
break;
}
++s;
}
if (mkdir(path, mode) < 0) {
/* If we failed for any other reason than the directory
* already exists, output a diagnostic and return -1.*/
if (errno != EEXIST || (stat(path, &st) < 0 || !S_ISDIR(st.st_mode))) {
DPRINTF(E_WARN, L_GENERAL, "make_dir: cannot create directory '%s'\n", path);
if (c)
*s = c;
return -1;
}
}
if (!c)
return 0;
/* Remove any inserted nul from the path. */
*s = c;
} while (1);
}
/* Simple, efficient hash function from Daniel J. Bernstein */
unsigned int
DJBHash(const char *str, int len)
{
unsigned int hash = 5381;
unsigned int i = 0;
for(i = 0; i < len; str++, i++)
{
hash = ((hash << 5) + hash) + (*str);
}
return hash;
}
int
is_video(const char * file)
{
return (ends_with(file, ".mpg") || ends_with(file, ".mpeg") ||
ends_with(file, ".avi") || ends_with(file, ".divx") ||
ends_with(file, ".asf") || ends_with(file, ".wmv") ||
ends_with(file, ".mp4") || ends_with(file, ".m4v") ||
ends_with(file, ".mts") || ends_with(file, ".m2ts") ||
ends_with(file, ".m2t") || ends_with(file, ".mkv") ||
ends_with(file, ".vob") || ends_with(file, ".ts") ||
ends_with(file, ".flv") || ends_with(file, ".xvid") ||
#ifdef TIVO_SUPPORT
ends_with(file, ".TiVo") ||
#endif
ends_with(file, ".mov") || ends_with(file, ".3gp"));
}
int
is_audio(const char * file)
{
return (ends_with(file, ".mp3") || ends_with(file, ".flac") ||
ends_with(file, ".wma") || ends_with(file, ".asf") ||
ends_with(file, ".fla") || ends_with(file, ".flc") ||
ends_with(file, ".m4a") || ends_with(file, ".aac") ||
ends_with(file, ".mp4") || ends_with(file, ".m4p") ||
ends_with(file, ".wav") || ends_with(file, ".ogg") ||
ends_with(file, ".pcm") || ends_with(file, ".3gp"));
}
int
is_image(const char * file)
{
return (ends_with(file, ".jpg") || ends_with(file, ".jpeg"));
}
int
is_playlist(const char * file)
{
return (ends_with(file, ".m3u") || ends_with(file, ".pls"));
}
int
is_album_art(const char * name)
{
struct album_art_name_s * album_art_name;
/* Check if this file name matches one of the default album art names */
for( album_art_name = album_art_names; album_art_name; album_art_name = album_art_name->next )
{
if( album_art_name->wildcard )
{
if( strncmp(album_art_name->name, name, strlen(album_art_name->name)) == 0 )
break;
}
else
{
if( strcmp(album_art_name->name, name) == 0 )
break;
}
}
return (album_art_name ? 1 : 0);
}
int
resolve_unknown_type(const char * path, enum media_types dir_type)
{
struct stat entry;
unsigned char type = TYPE_UNKNOWN;
char str_buf[PATH_MAX];
ssize_t len;
if( lstat(path, &entry) == 0 )
{
if( S_ISLNK(entry.st_mode) )
{
if( (len = readlink(path, str_buf, PATH_MAX-1)) > 0 )
{
str_buf[len] = '\0';
//DEBUG DPRINTF(E_DEBUG, L_GENERAL, "Checking for recursive symbolic link: %s (%s)\n", path, str_buf);
if( strncmp(path, str_buf, strlen(str_buf)) == 0 )
{
DPRINTF(E_DEBUG, L_GENERAL, "Ignoring recursive symbolic link: %s (%s)\n", path, str_buf);
return type;
}
}
stat(path, &entry);
}
if( S_ISDIR(entry.st_mode) )
{
type = TYPE_DIR;
}
else if( S_ISREG(entry.st_mode) )
{
switch( dir_type )
{
case ALL_MEDIA:
if( is_image(path) ||
is_audio(path) ||
is_video(path) ||
is_playlist(path) )
type = TYPE_FILE;
break;
case AUDIO_ONLY:
if( is_audio(path) ||
is_playlist(path) )
type = TYPE_FILE;
break;
case VIDEO_ONLY:
if( is_video(path) )
type = TYPE_FILE;
break;
case IMAGES_ONLY:
if( is_image(path) )
type = TYPE_FILE;
break;
default:
break;
}
}
}
return type;
}