]>
git.saurik.com Git - apple/xnu.git/blob - libkern/kxld/kxld_copyright.c
3 #include <AssertMacros.h>
9 #include "kxld_types.h"
11 #include <libkern/libkern.h>
12 #include <libkern/kxld.h>
13 #include <libkern/kxld_types.h>
16 #include "kxld_util.h"
18 /******************************************************************************
20 ******************************************************************************/
22 #define kCopyrightToken "Copyright © "
23 #define kRightsToken " Apple Inc. All rights reserved."
25 /******************************************************************************
27 ******************************************************************************/
31 #include <CoreFoundation/CoreFoundation.h>
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.")
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.")
52 extern char *createUTF8CStringForCFString(CFStringRef aString
);
56 /******************************************************************************
58 ******************************************************************************/
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
));
73 /******************************************************************************
74 ******************************************************************************/
76 is_space(const char c
)
91 /******************************************************************************
92 ******************************************************************************/
94 is_token_delimiter(const char c
)
96 return (is_space(c
) || (',' == c
) || ('\0' == c
));
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 ******************************************************************************/
104 is_token_break(const char *str
)
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.
109 return (!is_token_delimiter(str
[0]) && is_token_delimiter(str
[1]));
112 /******************************************************************************
113 * A year is defined by the following regular expression:
115 ******************************************************************************/
118 token_is_year(const char *str
)
120 boolean_t result
= FALSE
;
123 for (i
= 0; i
< kYearLen
- 1; ++i
) {
124 if (str
[i
] < '0' || str
[i
] > '9') goto finish
;
127 if (str
[i
] != '\0') goto finish
;
134 /******************************************************************************
135 * A year range is defined by the following regular expression:
136 * /[0-9]{4}[-][0-9]{4}$/
137 ******************************************************************************/
138 #define kYearRangeLen 10
140 token_is_yearRange(const char *str
)
142 boolean_t result
= FALSE
;
145 for (i
= 0; i
< kYearLen
- 1; ++i
) {
146 if (str
[i
] < '0' || str
[i
] > '9') goto finish
;
149 if (str
[i
] != '-') goto finish
;
151 for (i
= kYearLen
; i
< kYearRangeLen
- 1; ++i
) {
152 if (str
[i
] < '0' || str
[i
] > '9') goto finish
;
155 if (str
[i
] != '\0') goto finish
;
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
166 ******************************************************************************/
168 dates_are_valid(const char *str
, const u_long len
)
170 boolean_t result
= FALSE
;
171 const char *token_ptr
= NULL
;
172 char token_buffer
[kYearRangeLen
];
173 u_int token_index
= 0;
176 for (token_ptr
= str
; token_ptr
< str
+ len
; ++token_ptr
) {
177 if (is_token_delimiter(*token_ptr
) && !token_index
) continue;
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.
183 if (token_index
== kYearRangeLen
) goto finish
;
185 token_buffer
[token_index
++] = *token_ptr
;
186 if (is_token_break(token_ptr
)) {
187 if (!token_index
) continue;
189 token_buffer
[token_index
++] = '\0';
191 if (!token_is_year(token_buffer
) &&
192 !token_is_yearRange(token_buffer
))
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
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 ******************************************************************************/
217 kxld_validate_copyright_string(const char *str
)
219 boolean_t result
= FALSE
;
220 const char *copyright
= NULL
;
221 const char *rights
= NULL
;
222 char *date_str
= NULL
;
225 copyright
= kxld_strstr(str
, kCopyrightToken
);
226 rights
= kxld_strstr(str
, kRightsToken
);
228 if (!copyright
|| !rights
|| copyright
> rights
) goto finish
;
230 str
= copyright
+ const_strlen(kCopyrightToken
);
233 date_str
= kxld_alloc(len
);
234 if (!date_str
) goto finish
;
236 strncpy(date_str
, str
, len
);
237 date_str
[len
] = '\0';
239 if (!dates_are_valid(date_str
, len
)) goto finish
;
243 if (date_str
) kxld_free(date_str
, len
);
249 /******************************************************************************
250 ******************************************************************************/
252 main(int argc __unused
, char *argv
[] __unused
)
255 CFStringRef the_string
= NULL
;
256 const char *str
= NULL
;
259 printf("The following %lu strings should pass\n",
260 const_array_len(passes
));
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
;
268 (kxld_validate_copyright_string(str
)) ? "pass" : "fail", str
);
271 printf("\nThe following %lu strings should fail\n",
272 const_array_len(fails
));
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
;
280 (kxld_validate_copyright_string(str
)) ? "pass" : "fail", str
);