xref: /xnu-8796.121.2/libkern/kxld/kxld_copyright.c (revision c54f35ca767986246321eb901baf8f5ff7923f6a)
1 /*
2  * Copyright (c) 2009 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
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.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
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.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 
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"
38 #else
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[] = {
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.")
71 };
72 
73 CFStringRef fails[] = {
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.")
78 };
79 
80 extern char *createUTF8CStringForCFString(CFStringRef aString);
81 
82 #endif /* TEST */
83 
84 /******************************************************************************
85 * Prototypes
86 ******************************************************************************/
87 
88 static boolean_t is_space(const char c)
89 __attribute__((const));
90 static boolean_t is_token_delimiter(const char c)
91 __attribute__((const));
92 static boolean_t is_token_break(const char *str)
93 __attribute__((pure, nonnull));
94 static boolean_t token_is_year(const char *str)
95 __attribute__((pure, nonnull));
96 static boolean_t token_is_yearRange(const char *str)
97 __attribute__((pure, nonnull));
98 static boolean_t dates_are_valid(const char *str, const u_long len)
99 __attribute__((pure, nonnull));
100 
101 /******************************************************************************
102 ******************************************************************************/
103 static boolean_t
is_space(const char c)104 is_space(const char c)
105 {
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;
117 }
118 
119 /******************************************************************************
120 ******************************************************************************/
121 static boolean_t
is_token_delimiter(const char c)122 is_token_delimiter(const char c)
123 {
124 	return is_space(c) || (',' == c) || ('\0' == c);
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
is_token_break(const char * str)132 is_token_break(const char *str)
133 {
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]);
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
token_is_year(const char * str)146 token_is_year(const char *str)
147 {
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 	}
156 
157 	if (str[i] != '\0') {
158 		goto finish;
159 	}
160 
161 	result = TRUE;
162 finish:
163 	return result;
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
token_is_yearRange(const char * str)172 token_is_yearRange(const char *str)
173 {
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;
198 finish:
199 	return result;
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
dates_are_valid(const char * str,const u_long len)208 dates_are_valid(const char *str, const u_long len)
209 {
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;
247 finish:
248 	return result;
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
257 *
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 ******************************************************************************/
261 boolean_t
kxld_validate_copyright_string(const char * str)262 kxld_validate_copyright_string(const char *str)
263 {
264 	boolean_t result = FALSE;
265 	const char *copyright = NULL;
266 	const char *rights = NULL;
267 	char *date_str = NULL;
268 	size_t len = 0;
269 
270 	len = strlen(str);
271 	copyright = strnstr(str, kCopyrightToken, len);
272 	rights = strnstr(str, kRightsToken, len);
273 
274 	if (!copyright || !rights || copyright > rights) {
275 		goto finish;
276 	}
277 
278 	str = copyright + const_strlen(kCopyrightToken);
279 
280 	len = rights - str;
281 	date_str = kxld_alloc(len + 1);
282 	if (!date_str) {
283 		goto finish;
284 	}
285 
286 	strncpy(date_str, str, len);
287 	date_str[len] = '\0';
288 
289 	if (!dates_are_valid(date_str, len)) {
290 		goto finish;
291 	}
292 
293 	result = TRUE;
294 finish:
295 	if (date_str) {
296 		kxld_free(date_str, len + 1);
297 	}
298 	return result;
299 }
300 
301 #if TEST
302 
303 /******************************************************************************
304 ******************************************************************************/
305 int
main(int argc __unused,char * argv[]__unused)306 main(int argc __unused, char *argv[] __unused)
307 {
308 	int result = 1;
309 	CFStringRef the_string = NULL;
310 	const char *str = NULL;
311 	u_int i = 0;
312 
313 	printf("The following %lu strings should pass\n",
314 	    const_array_len(passes));
315 
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 		}
322 
323 		printf("%s: %s\n",
324 		    (kxld_validate_copyright_string(str)) ? "pass" : "fail", str);
325 	}
326 
327 	printf("\nThe following %lu strings should fail\n",
328 	    const_array_len(fails));
329 
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 		}
336 
337 		printf("%s: %s\n",
338 		    (kxld_validate_copyright_string(str)) ? "pass" : "fail", str);
339 	}
340 
341 	result = 0;
342 
343 finish:
344 	return result;
345 }
346 #endif /* TEST */
347