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