]> git.saurik.com Git - apple/xnu.git/blob - libkern/kxld/kxld_copyright.c
xnu-1504.15.3.tar.gz
[apple/xnu.git] / libkern / kxld / kxld_copyright.c
1 #include <string.h>
2 #include <sys/types.h>
3 #include <AssertMacros.h>
4
5 #if !KERNEL
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include "kxld.h"
9 #include "kxld_types.h"
10 #else
11 #include <libkern/libkern.h>
12 #include <libkern/kxld.h>
13 #include <libkern/kxld_types.h>
14 #endif /* KERNEL */
15
16 #include "kxld_util.h"
17
18 /******************************************************************************
19 * Macros
20 ******************************************************************************/
21
22 #define kCopyrightToken "Copyright © "
23 #define kRightsToken " Apple Inc. All rights reserved."
24
25 /******************************************************************************
26 * Globals
27 ******************************************************************************/
28
29 #if TEST
30
31 #include <CoreFoundation/CoreFoundation.h>
32
33 CFStringRef passes[] = {
34 CFSTR("Copyright © 2008 Apple Inc. All rights reserved."),
35 CFSTR("Copyright © 2004-2008 Apple Inc. All rights reserved."),
36 CFSTR("Copyright © 2004,2006 Apple Inc. All rights reserved."),
37 CFSTR("Copyright © 2004,2006-2008 Apple Inc. All rights reserved."),
38 CFSTR("Copyright © 2004 , 2006-2008 Apple Inc. All rights reserved."),
39 CFSTR("Copyright © 1998,2000-2002,2004,2006-2008 Apple Inc. All rights reserved."),
40 CFSTR("IOPCIFamily 2.1; Copyright © 2004,2006-2008 Apple Inc. All rights reserved."),
41 CFSTR("Copyright © 2004,2006-2008 Apple Inc. All rights reserved. The quick brown fox jumped over the lazy dog."),
42 CFSTR("IOPCIFamily 2.1; Copyright © 2004,2006-2008 Apple Inc. All rights reserved. The quick brown fox jumped over the lazy dog.")
43 };
44
45 CFStringRef fails[] = {
46 CFSTR("Copyright © 2007-08 Apple Inc. All rights reserved."),
47 CFSTR("Copyright (c) 2007 Apple Inc. All rights reserved."),
48 CFSTR("Copyright © 2007- Apple Inc. All rights reserved."),
49 CFSTR("Copyright © 2007 - 2008 Apple Inc. All rights reserved.")
50 };
51
52 extern char *createUTF8CStringForCFString(CFStringRef aString);
53
54 #endif /* TEST */
55
56 /******************************************************************************
57 * Prototypes
58 ******************************************************************************/
59
60 static boolean_t is_space(const char c)
61 __attribute__((const));
62 static boolean_t is_token_delimiter(const char c)
63 __attribute__((const));
64 static boolean_t is_token_break(const char *str)
65 __attribute__((pure, nonnull));
66 static boolean_t token_is_year(const char *str)
67 __attribute__((pure, nonnull));
68 static boolean_t token_is_yearRange(const char *str)
69 __attribute__((pure, nonnull));
70 static boolean_t dates_are_valid(const char *str, const u_long len)
71 __attribute__((pure, nonnull));
72
73 /******************************************************************************
74 ******************************************************************************/
75 static boolean_t
76 is_space(const char c)
77 {
78 switch (c) {
79 case ' ':
80 case '\t':
81 case '\n':
82 case '\v':
83 case '\f':
84 case '\r':
85 return TRUE;
86 }
87
88 return FALSE;
89 }
90
91 /******************************************************************************
92 ******************************************************************************/
93 static boolean_t
94 is_token_delimiter(const char c)
95 {
96 return (is_space(c) || (',' == c) || ('\0' == c));
97 }
98
99 /******************************************************************************
100 * A token break is defined to be the boundary where the current character is
101 * not a token delimiter and the next character is a token delimiter.
102 ******************************************************************************/
103 static boolean_t
104 is_token_break(const char *str)
105 {
106 /* This is safe because '\0' is a token delimiter, so the second check
107 * will not execute if we reach the end of the string.
108 */
109 return (!is_token_delimiter(str[0]) && is_token_delimiter(str[1]));
110 }
111
112 /******************************************************************************
113 * A year is defined by the following regular expression:
114 * /[0-9]{4}$/
115 ******************************************************************************/
116 #define kYearLen 5
117 static boolean_t
118 token_is_year(const char *str)
119 {
120 boolean_t result = FALSE;
121 u_int i = 0;
122
123 for (i = 0; i < kYearLen - 1; ++i) {
124 if (str[i] < '0' || str[i] > '9') goto finish;
125 }
126
127 if (str[i] != '\0') goto finish;
128
129 result = TRUE;
130 finish:
131 return result;
132 }
133
134 /******************************************************************************
135 * A year range is defined by the following regular expression:
136 * /[0-9]{4}[-][0-9]{4}$/
137 ******************************************************************************/
138 #define kYearRangeLen 10
139 static boolean_t
140 token_is_yearRange(const char *str)
141 {
142 boolean_t result = FALSE;
143 u_int i = 0;
144
145 for (i = 0; i < kYearLen - 1; ++i) {
146 if (str[i] < '0' || str[i] > '9') goto finish;
147 }
148
149 if (str[i] != '-') goto finish;
150
151 for (i = kYearLen; i < kYearRangeLen - 1; ++i) {
152 if (str[i] < '0' || str[i] > '9') goto finish;
153 }
154
155 if (str[i] != '\0') goto finish;
156
157 result = TRUE;
158 finish:
159 return result;
160 }
161
162 /******************************************************************************
163 * The dates_are_valid function takes as input a comma-delimited list of years
164 * and year ranges, and returns TRUE if all years and year ranges are valid
165 * and well-formed.
166 ******************************************************************************/
167 static boolean_t
168 dates_are_valid(const char *str, const u_long len)
169 {
170 boolean_t result = FALSE;
171 const char *token_ptr = NULL;
172 char token_buffer[kYearRangeLen];
173 u_int token_index = 0;
174
175 token_index = 0;
176 for (token_ptr = str; token_ptr < str + len; ++token_ptr) {
177 if (is_token_delimiter(*token_ptr) && !token_index) continue;
178
179 /* If we exceed the length of a year range, the test will not succeed,
180 * so just fail now. This limits the length of the token buffer that
181 * we have to keep around.
182 */
183 if (token_index == kYearRangeLen) goto finish;
184
185 token_buffer[token_index++] = *token_ptr;
186 if (is_token_break(token_ptr)) {
187 if (!token_index) continue;
188
189 token_buffer[token_index++] = '\0';
190
191 if (!token_is_year(token_buffer) &&
192 !token_is_yearRange(token_buffer))
193 {
194 goto finish;
195 }
196
197 token_index = 0;
198 }
199 }
200
201 result = TRUE;
202 finish:
203 return result;
204 }
205
206 /******************************************************************************
207 * The copyright string is composed of three parts:
208 * 1) A copyright notice, "Copyright ©"
209 * 2) One or more years or year ranges, e.g., "2004,2006-2008"
210 * 3) A rights reserved notice, "Apple Inc. All Rights Reserved."
211 * We check the validity of the string by searching for both the copyright
212
213 * notice and the rights reserved notice. If both are found, we then check that
214 * the text between the two notices contains only valid years and year ranges.
215 ******************************************************************************/
216 boolean_t
217 kxld_validate_copyright_string(const char *str)
218 {
219 boolean_t result = FALSE;
220 const char *copyright = NULL;
221 const char *rights = NULL;
222 char *date_str = NULL;
223 u_long len = 0;
224
225 copyright = kxld_strstr(str, kCopyrightToken);
226 rights = kxld_strstr(str, kRightsToken);
227
228 if (!copyright || !rights || copyright > rights) goto finish;
229
230 str = copyright + const_strlen(kCopyrightToken);
231
232 len = rights - str;
233 date_str = kxld_alloc(len);
234 if (!date_str) goto finish;
235
236 strncpy(date_str, str, len);
237 date_str[len] = '\0';
238
239 if (!dates_are_valid(date_str, len)) goto finish;
240
241 result = TRUE;
242 finish:
243 if (date_str) kxld_free(date_str, len);
244 return result;
245 }
246
247 #if TEST
248
249 /******************************************************************************
250 ******************************************************************************/
251 int
252 main(int argc __unused, char *argv[] __unused)
253 {
254 int result = 1;
255 CFStringRef the_string = NULL;
256 const char *str = NULL;
257 u_int i = 0;
258
259 printf("The following %lu strings should pass\n",
260 const_array_len(passes));
261
262 for (i = 0; i < const_array_len(passes); ++i) {
263 the_string = passes[i];
264 str = createUTF8CStringForCFString(the_string);
265 if (!str) goto finish;
266
267 printf("%s: %s\n",
268 (kxld_validate_copyright_string(str)) ? "pass" : "fail", str);
269 }
270
271 printf("\nThe following %lu strings should fail\n",
272 const_array_len(fails));
273
274 for (i = 0; i < const_array_len(fails); ++i) {
275 the_string = fails[i];
276 str = createUTF8CStringForCFString(the_string);
277 if (!str) goto finish;
278
279 printf("%s: %s\n",
280 (kxld_validate_copyright_string(str)) ? "pass" : "fail", str);
281 }
282
283 result = 0;
284
285 finish:
286 return result;
287 }
288 #endif /* TEST */
289