]>
Commit | Line | Data |
---|---|---|
6d2010ae A |
1 | /* |
2 | * Copyright (c) 2009 Apple Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ | |
0a7de745 | 5 | * |
6d2010ae A |
6 | * This file contains Original Code and/or Modifications of Original Code |
7 | * as defined in and that are subject to the Apple Public Source License | |
8 | * Version 2.0 (the 'License'). You may not use this file except in | |
9 | * compliance with the License. The rights granted to you under the License | |
10 | * may not be used to create, or enable the creation or redistribution of, | |
11 | * unlawful or unlicensed copies of an Apple operating system, or to | |
12 | * circumvent, violate, or enable the circumvention or violation of, any | |
13 | * terms of an Apple operating system software license agreement. | |
0a7de745 | 14 | * |
6d2010ae A |
15 | * Please obtain a copy of the License at |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
0a7de745 | 17 | * |
6d2010ae A |
18 | * The Original Code and all software distributed under the License are |
19 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, | |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
22 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. | |
23 | * Please see the License for the specific language governing rights and | |
24 | * limitations under the License. | |
0a7de745 | 25 | * |
6d2010ae A |
26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
27 | */ | |
28 | ||
b0d623f7 A |
29 | #include <string.h> |
30 | #include <sys/types.h> | |
31 | #include <AssertMacros.h> | |
32 | ||
33 | #if !KERNEL | |
34 | #include <stdio.h> | |
35 | #include <stdlib.h> | |
36 | #include "kxld.h" | |
37 | #include "kxld_types.h" | |
0a7de745 | 38 | #else |
b0d623f7 A |
39 | #include <libkern/libkern.h> |
40 | #include <libkern/kxld.h> | |
41 | #include <libkern/kxld_types.h> | |
42 | #endif /* KERNEL */ | |
43 | ||
44 | #include "kxld_util.h" | |
45 | ||
46 | /****************************************************************************** | |
47 | * Macros | |
48 | ******************************************************************************/ | |
49 | ||
50 | #define kCopyrightToken "Copyright © " | |
51 | #define kRightsToken " Apple Inc. All rights reserved." | |
52 | ||
53 | /****************************************************************************** | |
54 | * Globals | |
55 | ******************************************************************************/ | |
56 | ||
57 | #if TEST | |
58 | ||
59 | #include <CoreFoundation/CoreFoundation.h> | |
60 | ||
61 | CFStringRef passes[] = { | |
0a7de745 A |
62 | CFSTR("Copyright © 2008 Apple Inc. All rights reserved."), |
63 | CFSTR("Copyright © 2004-2008 Apple Inc. All rights reserved."), | |
64 | CFSTR("Copyright © 2004,2006 Apple Inc. All rights reserved."), | |
65 | CFSTR("Copyright © 2004,2006-2008 Apple Inc. All rights reserved."), | |
66 | CFSTR("Copyright © 2004 , 2006-2008 Apple Inc. All rights reserved."), | |
67 | CFSTR("Copyright © 1998,2000-2002,2004,2006-2008 Apple Inc. All rights reserved."), | |
68 | CFSTR("IOPCIFamily 2.1; Copyright © 2004,2006-2008 Apple Inc. All rights reserved."), | |
69 | CFSTR("Copyright © 2004,2006-2008 Apple Inc. All rights reserved. The quick brown fox jumped over the lazy dog."), | |
70 | CFSTR("IOPCIFamily 2.1; Copyright © 2004,2006-2008 Apple Inc. All rights reserved. The quick brown fox jumped over the lazy dog.") | |
b0d623f7 A |
71 | }; |
72 | ||
73 | CFStringRef fails[] = { | |
0a7de745 A |
74 | CFSTR("Copyright © 2007-08 Apple Inc. All rights reserved."), |
75 | CFSTR("Copyright (c) 2007 Apple Inc. All rights reserved."), | |
76 | CFSTR("Copyright © 2007- Apple Inc. All rights reserved."), | |
77 | CFSTR("Copyright © 2007 - 2008 Apple Inc. All rights reserved.") | |
b0d623f7 A |
78 | }; |
79 | ||
80 | extern char *createUTF8CStringForCFString(CFStringRef aString); | |
81 | ||
82 | #endif /* TEST */ | |
83 | ||
84 | /****************************************************************************** | |
85 | * Prototypes | |
86 | ******************************************************************************/ | |
87 | ||
0a7de745 A |
88 | static boolean_t is_space(const char c) |
89 | __attribute__((const)); | |
b0d623f7 | 90 | static boolean_t is_token_delimiter(const char c) |
0a7de745 A |
91 | __attribute__((const)); |
92 | static boolean_t is_token_break(const char *str) | |
93 | __attribute__((pure, nonnull)); | |
b0d623f7 | 94 | static boolean_t token_is_year(const char *str) |
0a7de745 | 95 | __attribute__((pure, nonnull)); |
b0d623f7 | 96 | static boolean_t token_is_yearRange(const char *str) |
0a7de745 | 97 | __attribute__((pure, nonnull)); |
b0d623f7 | 98 | static boolean_t dates_are_valid(const char *str, const u_long len) |
0a7de745 | 99 | __attribute__((pure, nonnull)); |
b0d623f7 A |
100 | |
101 | /****************************************************************************** | |
102 | ******************************************************************************/ | |
103 | static boolean_t | |
104 | is_space(const char c) | |
105 | { | |
0a7de745 A |
106 | switch (c) { |
107 | case ' ': | |
108 | case '\t': | |
109 | case '\n': | |
110 | case '\v': | |
111 | case '\f': | |
112 | case '\r': | |
113 | return TRUE; | |
114 | } | |
115 | ||
116 | return FALSE; | |
b0d623f7 A |
117 | } |
118 | ||
119 | /****************************************************************************** | |
120 | ******************************************************************************/ | |
121 | static boolean_t | |
122 | is_token_delimiter(const char c) | |
123 | { | |
0a7de745 | 124 | return is_space(c) || (',' == c) || ('\0' == c); |
b0d623f7 A |
125 | } |
126 | ||
127 | /****************************************************************************** | |
128 | * A token break is defined to be the boundary where the current character is | |
129 | * not a token delimiter and the next character is a token delimiter. | |
130 | ******************************************************************************/ | |
131 | static boolean_t | |
0a7de745 | 132 | is_token_break(const char *str) |
b0d623f7 | 133 | { |
0a7de745 A |
134 | /* This is safe because '\0' is a token delimiter, so the second check |
135 | * will not execute if we reach the end of the string. | |
136 | */ | |
137 | return !is_token_delimiter(str[0]) && is_token_delimiter(str[1]); | |
b0d623f7 A |
138 | } |
139 | ||
140 | /****************************************************************************** | |
141 | * A year is defined by the following regular expression: | |
142 | * /[0-9]{4}$/ | |
143 | ******************************************************************************/ | |
144 | #define kYearLen 5 | |
145 | static boolean_t | |
146 | token_is_year(const char *str) | |
147 | { | |
0a7de745 A |
148 | boolean_t result = FALSE; |
149 | u_int i = 0; | |
150 | ||
151 | for (i = 0; i < kYearLen - 1; ++i) { | |
152 | if (str[i] < '0' || str[i] > '9') { | |
153 | goto finish; | |
154 | } | |
155 | } | |
b0d623f7 | 156 | |
0a7de745 A |
157 | if (str[i] != '\0') { |
158 | goto finish; | |
159 | } | |
b0d623f7 | 160 | |
0a7de745 | 161 | result = TRUE; |
b0d623f7 | 162 | finish: |
0a7de745 | 163 | return result; |
b0d623f7 A |
164 | } |
165 | ||
166 | /****************************************************************************** | |
167 | * A year range is defined by the following regular expression: | |
168 | * /[0-9]{4}[-][0-9]{4}$/ | |
169 | ******************************************************************************/ | |
170 | #define kYearRangeLen 10 | |
171 | static boolean_t | |
172 | token_is_yearRange(const char *str) | |
173 | { | |
0a7de745 A |
174 | boolean_t result = FALSE; |
175 | u_int i = 0; | |
176 | ||
177 | for (i = 0; i < kYearLen - 1; ++i) { | |
178 | if (str[i] < '0' || str[i] > '9') { | |
179 | goto finish; | |
180 | } | |
181 | } | |
182 | ||
183 | if (str[i] != '-') { | |
184 | goto finish; | |
185 | } | |
186 | ||
187 | for (i = kYearLen; i < kYearRangeLen - 1; ++i) { | |
188 | if (str[i] < '0' || str[i] > '9') { | |
189 | goto finish; | |
190 | } | |
191 | } | |
192 | ||
193 | if (str[i] != '\0') { | |
194 | goto finish; | |
195 | } | |
196 | ||
197 | result = TRUE; | |
b0d623f7 | 198 | finish: |
0a7de745 | 199 | return result; |
b0d623f7 A |
200 | } |
201 | ||
202 | /****************************************************************************** | |
203 | * The dates_are_valid function takes as input a comma-delimited list of years | |
204 | * and year ranges, and returns TRUE if all years and year ranges are valid | |
205 | * and well-formed. | |
206 | ******************************************************************************/ | |
207 | static boolean_t | |
208 | dates_are_valid(const char *str, const u_long len) | |
209 | { | |
0a7de745 A |
210 | boolean_t result = FALSE; |
211 | const char *token_ptr = NULL; | |
212 | char token_buffer[kYearRangeLen]; | |
213 | u_int token_index = 0; | |
214 | ||
215 | token_index = 0; | |
216 | for (token_ptr = str; token_ptr < str + len; ++token_ptr) { | |
217 | if (is_token_delimiter(*token_ptr) && !token_index) { | |
218 | continue; | |
219 | } | |
220 | ||
221 | /* If we exceed the length of a year range, the test will not succeed, | |
222 | * so just fail now. This limits the length of the token buffer that | |
223 | * we have to keep around. | |
224 | */ | |
225 | if (token_index == kYearRangeLen) { | |
226 | goto finish; | |
227 | } | |
228 | ||
229 | token_buffer[token_index++] = *token_ptr; | |
230 | if (is_token_break(token_ptr)) { | |
231 | if (!token_index) { | |
232 | continue; | |
233 | } | |
234 | ||
235 | token_buffer[token_index] = '\0'; | |
236 | ||
237 | if (!token_is_year(token_buffer) && | |
238 | !token_is_yearRange(token_buffer)) { | |
239 | goto finish; | |
240 | } | |
241 | ||
242 | token_index = 0; | |
243 | } | |
244 | } | |
245 | ||
246 | result = TRUE; | |
b0d623f7 | 247 | finish: |
0a7de745 | 248 | return result; |
b0d623f7 A |
249 | } |
250 | ||
251 | /****************************************************************************** | |
252 | * The copyright string is composed of three parts: | |
253 | * 1) A copyright notice, "Copyright ©" | |
254 | * 2) One or more years or year ranges, e.g., "2004,2006-2008" | |
255 | * 3) A rights reserved notice, "Apple Inc. All Rights Reserved." | |
256 | * We check the validity of the string by searching for both the copyright | |
0a7de745 | 257 | * |
b0d623f7 A |
258 | * notice and the rights reserved notice. If both are found, we then check that |
259 | * the text between the two notices contains only valid years and year ranges. | |
260 | ******************************************************************************/ | |
0a7de745 | 261 | boolean_t |
b0d623f7 A |
262 | kxld_validate_copyright_string(const char *str) |
263 | { | |
0a7de745 A |
264 | boolean_t result = FALSE; |
265 | const char *copyright = NULL; | |
266 | const char *rights = NULL; | |
267 | char *date_str = NULL; | |
f427ee49 | 268 | size_t len = 0; |
b0d623f7 | 269 | |
f427ee49 A |
270 | len = strlen(str); |
271 | copyright = strnstr(str, kCopyrightToken, len); | |
272 | rights = strnstr(str, kRightsToken, len); | |
b0d623f7 | 273 | |
0a7de745 A |
274 | if (!copyright || !rights || copyright > rights) { |
275 | goto finish; | |
276 | } | |
b0d623f7 | 277 | |
0a7de745 | 278 | str = copyright + const_strlen(kCopyrightToken); |
b0d623f7 | 279 | |
0a7de745 A |
280 | len = rights - str; |
281 | date_str = kxld_alloc(len + 1); | |
282 | if (!date_str) { | |
283 | goto finish; | |
284 | } | |
b0d623f7 | 285 | |
0a7de745 A |
286 | strncpy(date_str, str, len); |
287 | date_str[len] = '\0'; | |
b0d623f7 | 288 | |
0a7de745 A |
289 | if (!dates_are_valid(date_str, len)) { |
290 | goto finish; | |
291 | } | |
b0d623f7 | 292 | |
0a7de745 | 293 | result = TRUE; |
b0d623f7 | 294 | finish: |
0a7de745 A |
295 | if (date_str) { |
296 | kxld_free(date_str, len + 1); | |
297 | } | |
298 | return result; | |
b0d623f7 A |
299 | } |
300 | ||
301 | #if TEST | |
302 | ||
303 | /****************************************************************************** | |
304 | ******************************************************************************/ | |
305 | int | |
306 | main(int argc __unused, char *argv[] __unused) | |
307 | { | |
0a7de745 A |
308 | int result = 1; |
309 | CFStringRef the_string = NULL; | |
310 | const char *str = NULL; | |
311 | u_int i = 0; | |
b0d623f7 | 312 | |
0a7de745 A |
313 | printf("The following %lu strings should pass\n", |
314 | const_array_len(passes)); | |
b0d623f7 | 315 | |
0a7de745 A |
316 | for (i = 0; i < const_array_len(passes); ++i) { |
317 | the_string = passes[i]; | |
318 | str = createUTF8CStringForCFString(the_string); | |
319 | if (!str) { | |
320 | goto finish; | |
321 | } | |
b0d623f7 | 322 | |
0a7de745 A |
323 | printf("%s: %s\n", |
324 | (kxld_validate_copyright_string(str)) ? "pass" : "fail", str); | |
325 | } | |
b0d623f7 | 326 | |
0a7de745 A |
327 | printf("\nThe following %lu strings should fail\n", |
328 | const_array_len(fails)); | |
b0d623f7 | 329 | |
0a7de745 A |
330 | for (i = 0; i < const_array_len(fails); ++i) { |
331 | the_string = fails[i]; | |
332 | str = createUTF8CStringForCFString(the_string); | |
333 | if (!str) { | |
334 | goto finish; | |
335 | } | |
b0d623f7 | 336 | |
0a7de745 A |
337 | printf("%s: %s\n", |
338 | (kxld_validate_copyright_string(str)) ? "pass" : "fail", str); | |
339 | } | |
b0d623f7 | 340 | |
0a7de745 | 341 | result = 0; |
b0d623f7 A |
342 | |
343 | finish: | |
0a7de745 | 344 | return result; |
b0d623f7 A |
345 | } |
346 | #endif /* TEST */ |