Update copyright dates
[src/app-framework-binder.git] / src / subpath.c
1 /*
2  Copyright (C) 2015-2020 "IoT.bzh"
3
4  author: José Bollo <jose.bollo@iot.bzh>
5
6  Licensed under the Apache License, Version 2.0 (the "License");
7  you may not use this file except in compliance with the License.
8  You may obtain a copy of the License at
9
10      http://www.apache.org/licenses/LICENSE-2.0
11
12  Unless required by applicable law or agreed to in writing, software
13  distributed under the License is distributed on an "AS IS" BASIS,
14  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  See the License for the specific language governing permissions and
16  limitations under the License.
17 */
18
19 #define _GNU_SOURCE
20
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <unistd.h>
24 #include <fcntl.h>
25 #include <errno.h>
26 #include <assert.h>
27 #include <limits.h>
28 #include <string.h>
29 #include <ctype.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <dirent.h>
33
34 #include "subpath.h"
35
36 /* a valid subpath is a relative path not looking deeper than root using .. */
37 int subpath_is_valid(const char *path)
38 {
39         int l = 0, i = 0;
40
41         /* absolute path is not valid */
42         if (path[i] == '/')
43                 return 0;
44
45         /* inspect the path */
46         while(path[i]) {
47                 switch(path[i++]) {
48                 case '.':
49                         if (!path[i])
50                                 break;
51                         if (path[i] == '/') {
52                                 i++;
53                                 break;
54                         }
55                         if (path[i++] == '.') {
56                                 if (!path[i]) {
57                                         l--;
58                                         break;
59                                 }
60                                 if (path[i++] == '/') {
61                                         l--;
62                                         break;
63                                 }
64                         }
65                 default:
66                         while(path[i] && path[i] != '/')
67                                 i++;
68                         if (l >= 0)
69                                 l++;
70                 case '/':
71                         break;
72                 }
73         }
74         return l >= 0;
75 }
76
77 /*
78  * Return the path or NULL is not valid.
79  * Ensure that the path doesn't start with '/' and that
80  * it does not contains sequence of '..' going deeper than
81  * root.
82  * Returns the path or NULL in case of
83  * invalid path.
84  */
85 const char *subpath(const char *path)
86 {
87         return path && subpath_is_valid(path) ? (path[0] ? path : ".") : NULL;
88 }
89
90 /*
91  * Normalizes and checks the 'path'.
92  * Removes any starting '/' and checks that 'path'
93  * does not contains sequence of '..' going deeper than
94  * root.
95  * Returns the normalized path or NULL in case of
96  * invalid path.
97  */
98 const char *subpath_force(const char *path)
99 {
100         while(path && *path == '/')
101                 path++;
102         return subpath(path);
103 }
104
105 #if defined(TEST_subpath)
106 #include <stdio.h>
107 void t(const char *subpath, int validity) {
108   printf("%s -> %d = %d, %s\n", subpath, validity, subpath_is_valid(subpath), subpath_is_valid(subpath)==validity ? "ok" : "NOT OK");
109 }
110 int main() {
111   t("/",0);
112   t("..",0);
113   t(".",1);
114   t("../a",0);
115   t("a/..",1);
116   t("a/../////..",0);
117   t("a/../b/..",1);
118   t("a/b/c/..",1);
119   t("a/b/c/../..",1);
120   t("a/b/c/../../..",1);
121   t("a/b/c/../../../.",1);
122   t("./..a/././..b/..c/./.././.././../.",1);
123   t("./..a/././..b/..c/./.././.././.././..",0);
124   t("./..a//.//./..b/..c/./.././/./././///.././.././a/a/a/a/a",1);
125   return 0;
126 }
127 #endif
128