]>
git.saurik.com Git - apple/xnu.git/blob - libkern/kxld/kxld_copyright.c
2 * Copyright (c) 2009 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
30 #include <sys/types.h>
31 #include <AssertMacros.h>
37 #include "kxld_types.h"
39 #include <libkern/libkern.h>
40 #include <libkern/kxld.h>
41 #include <libkern/kxld_types.h>
44 #include "kxld_util.h"
46 /******************************************************************************
48 ******************************************************************************/
50 #define kCopyrightToken "Copyright © "
51 #define kRightsToken " Apple Inc. All rights reserved."
53 /******************************************************************************
55 ******************************************************************************/
59 #include <CoreFoundation/CoreFoundation.h>
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.")
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.")
80 extern char *createUTF8CStringForCFString(CFStringRef aString
);
84 /******************************************************************************
86 ******************************************************************************/
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
));
101 /******************************************************************************
102 ******************************************************************************/
104 is_space(const char c
)
119 /******************************************************************************
120 ******************************************************************************/
122 is_token_delimiter(const char c
)
124 return is_space(c
) || (',' == c
) || ('\0' == c
);
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 ******************************************************************************/
132 is_token_break(const char *str
)
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.
137 return !is_token_delimiter(str
[0]) && is_token_delimiter(str
[1]);
140 /******************************************************************************
141 * A year is defined by the following regular expression:
143 ******************************************************************************/
146 token_is_year(const char *str
)
148 boolean_t result
= FALSE
;
151 for (i
= 0; i
< kYearLen
- 1; ++i
) {
152 if (str
[i
] < '0' || str
[i
] > '9') {
157 if (str
[i
] != '\0') {
166 /******************************************************************************
167 * A year range is defined by the following regular expression:
168 * /[0-9]{4}[-][0-9]{4}$/
169 ******************************************************************************/
170 #define kYearRangeLen 10
172 token_is_yearRange(const char *str
)
174 boolean_t result
= FALSE
;
177 for (i
= 0; i
< kYearLen
- 1; ++i
) {
178 if (str
[i
] < '0' || str
[i
] > '9') {
187 for (i
= kYearLen
; i
< kYearRangeLen
- 1; ++i
) {
188 if (str
[i
] < '0' || str
[i
] > '9') {
193 if (str
[i
] != '\0') {
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
206 ******************************************************************************/
208 dates_are_valid(const char *str
, const u_long len
)
210 boolean_t result
= FALSE
;
211 const char *token_ptr
= NULL
;
212 char token_buffer
[kYearRangeLen
];
213 u_int token_index
= 0;
216 for (token_ptr
= str
; token_ptr
< str
+ len
; ++token_ptr
) {
217 if (is_token_delimiter(*token_ptr
) && !token_index
) {
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.
225 if (token_index
== kYearRangeLen
) {
229 token_buffer
[token_index
++] = *token_ptr
;
230 if (is_token_break(token_ptr
)) {
235 token_buffer
[token_index
] = '\0';
237 if (!token_is_year(token_buffer
) &&
238 !token_is_yearRange(token_buffer
)) {
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
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 ******************************************************************************/
262 kxld_validate_copyright_string(const char *str
)
264 boolean_t result
= FALSE
;
265 const char *copyright
= NULL
;
266 const char *rights
= NULL
;
267 char *date_str
= NULL
;
271 copyright
= strnstr(str
, kCopyrightToken
, len
);
272 rights
= strnstr(str
, kRightsToken
, len
);
274 if (!copyright
|| !rights
|| copyright
> rights
) {
278 str
= copyright
+ const_strlen(kCopyrightToken
);
281 date_str
= kxld_alloc(len
+ 1);
286 strncpy(date_str
, str
, len
);
287 date_str
[len
] = '\0';
289 if (!dates_are_valid(date_str
, len
)) {
296 kxld_free(date_str
, len
+ 1);
303 /******************************************************************************
304 ******************************************************************************/
306 main(int argc __unused
, char *argv
[] __unused
)
309 CFStringRef the_string
= NULL
;
310 const char *str
= NULL
;
313 printf("The following %lu strings should pass\n",
314 const_array_len(passes
));
316 for (i
= 0; i
< const_array_len(passes
); ++i
) {
317 the_string
= passes
[i
];
318 str
= createUTF8CStringForCFString(the_string
);
324 (kxld_validate_copyright_string(str
)) ? "pass" : "fail", str
);
327 printf("\nThe following %lu strings should fail\n",
328 const_array_len(fails
));
330 for (i
= 0; i
< const_array_len(fails
); ++i
) {
331 the_string
= fails
[i
];
332 str
= createUTF8CStringForCFString(the_string
);
338 (kxld_validate_copyright_string(str
)) ? "pass" : "fail", str
);