Line data Source code
1 : #include "burp.h"
2 : #include "pathcmp.h"
3 :
4 : // Return a number indicating the number of directories matched.
5 : // 0 if it is not a sub-directory.
6 : // Two paths the same counts as a subdirectory.
7 282 : int is_subdir(const char *dir, const char *sub)
8 : {
9 282 : int count=1;
10 282 : const char *d=NULL;
11 282 : const char *s=NULL;
12 282 : const char *dl=NULL;
13 282 : const char *sl=NULL;
14 282 : if(!sub || !dir) return 0;
15 4135 : for(s=sl=sub, dl=d=dir; *s && *d; s++, d++)
16 : {
17 4178 : if(*s!=*d) break;
18 4135 : sl=s;
19 4135 : dl=d;
20 4135 : if(*s=='/') count++;
21 : }
22 279 : if(!*d && !*s) return count; // Paths were exactly the same.
23 252 : if(!*d && *s=='/')
24 : return count; // 'dir' ended without a slash, for example:
25 : // dir=/bin sub=/bin/bash
26 92 : if(*dl=='/' && *sl=='/' && *(sl+1) && !*(dl+1)) return count;
27 83 : return 0;
28 : }
29 :
30 154002 : int pathcmp(const char *a, const char *b)
31 : {
32 : // This should have used 'unsigned chars', but now its too late and
33 : // everybody has backups with odd sorting. Will have to live with it.
34 154002 : const char *x=NULL;
35 154002 : const char *y=NULL;
36 154002 : if(!a && !b)
37 : return 0; // equal
38 101093 : if( a && !b)
39 : return 1; // a is longer
40 101092 : if(!a && b)
41 : return -1; // b is longer
42 5169716 : for(x=a, y=b; *x && *y ; x++, y++)
43 : {
44 5227170 : if(*x==*y)
45 5169716 : continue;
46 57454 : if(*x=='/' && *y!='/')
47 : return -1;
48 57452 : if(*x!='/' && *y=='/')
49 : return 1;
50 : // Need to make sure the comparisons are signed.
51 : // Not doing this caused problems on raspberry pis.
52 57451 : if((int8_t)*x<(int8_t)*y)
53 : return -1;
54 30658 : if((int8_t)*x>(int8_t)*y)
55 : return 1;
56 : }
57 43637 : if(!*x && !*y)
58 : return 0; // equal
59 302 : if( *x && !*y)
60 : return 1; // x is longer
61 14 : return -1; // y is longer
62 : }
63 :
64 : // Not really pathcmp functions, but there is nowhere better to put them.
65 22118 : int has_dot_component(const char *path)
66 : {
67 22118 : const char *p=NULL;
68 577404 : for(p=path; *p; p++)
69 : {
70 555306 : if(*p!='.')
71 555256 : continue;
72 : // Check for single dot.
73 50 : if((p==path || *(p-1)=='/') && (*(p+1)=='/' || !*(p+1)))
74 : return 1;
75 : // Check for double dot.
76 40 : if(*(p+1)=='.'
77 22 : && (p==path || *(p-1)=='/') && (*(p+2)=='/' || !*(p+2)))
78 : return 1;
79 : }
80 : return 0;
81 : }
82 :
83 108 : int is_absolute(const char *path)
84 : {
85 108 : if(has_dot_component(path))
86 : return 0;
87 : // This is being run on the server too, where you can enter paths for the
88 : // clients, so need to allow windows style paths for windows and unix.
89 106 : return (isalpha(*path) && *(path+1)==':')
90 : #ifndef HAVE_WIN32
91 : // Windows does not need to check for unix style paths.
92 191 : || *path=='/'
93 : #endif
94 : ;
95 : }
|