#include <string.h>
#include <sys/types.h>
#include <AssertMacros.h>
#if !KERNEL
#include <stdio.h>
#include <stdlib.h>
#include "kxld.h"
#include "kxld_types.h"
#else
#include <libkern/libkern.h>
#include <libkern/kxld.h>
#include <libkern/kxld_types.h>
#endif
#include "kxld_util.h"
#define kCopyrightToken "Copyright © "
#define kRightsToken " Apple Inc. All rights reserved."
#if TEST
#include <CoreFoundation/CoreFoundation.h>
CFStringRef passes[] = {
CFSTR("Copyright © 2008 Apple Inc. All rights reserved."),
CFSTR("Copyright © 2004-2008 Apple Inc. All rights reserved."),
CFSTR("Copyright © 2004,2006 Apple Inc. All rights reserved."),
CFSTR("Copyright © 2004,2006-2008 Apple Inc. All rights reserved."),
CFSTR("Copyright © 2004 , 2006-2008 Apple Inc. All rights reserved."),
CFSTR("Copyright © 1998,2000-2002,2004,2006-2008 Apple Inc. All rights reserved."),
CFSTR("IOPCIFamily 2.1; Copyright © 2004,2006-2008 Apple Inc. All rights reserved."),
CFSTR("Copyright © 2004,2006-2008 Apple Inc. All rights reserved. The quick brown fox jumped over the lazy dog."),
CFSTR("IOPCIFamily 2.1; Copyright © 2004,2006-2008 Apple Inc. All rights reserved. The quick brown fox jumped over the lazy dog.")
};
CFStringRef fails[] = {
CFSTR("Copyright © 2007-08 Apple Inc. All rights reserved."),
CFSTR("Copyright (c) 2007 Apple Inc. All rights reserved."),
CFSTR("Copyright © 2007- Apple Inc. All rights reserved."),
CFSTR("Copyright © 2007 - 2008 Apple Inc. All rights reserved.")
};
extern char *createUTF8CStringForCFString(CFStringRef aString);
#endif
static boolean_t is_space(const char c)
__attribute__((const));
static boolean_t is_token_delimiter(const char c)
__attribute__((const));
static boolean_t is_token_break(const char *str)
__attribute__((pure, nonnull));
static boolean_t token_is_year(const char *str)
__attribute__((pure, nonnull));
static boolean_t token_is_yearRange(const char *str)
__attribute__((pure, nonnull));
static boolean_t dates_are_valid(const char *str, const u_long len)
__attribute__((pure, nonnull));
static boolean_t
is_space(const char c)
{
switch (c) {
case ' ':
case '\t':
case '\n':
case '\v':
case '\f':
case '\r':
return TRUE;
}
return FALSE;
}
static boolean_t
is_token_delimiter(const char c)
{
return (is_space(c) || (',' == c) || ('\0' == c));
}
static boolean_t
is_token_break(const char *str)
{
return (!is_token_delimiter(str[0]) && is_token_delimiter(str[1]));
}
#define kYearLen 5
static boolean_t
token_is_year(const char *str)
{
boolean_t result = FALSE;
u_int i = 0;
for (i = 0; i < kYearLen - 1; ++i) {
if (str[i] < '0' || str[i] > '9') goto finish;
}
if (str[i] != '\0') goto finish;
result = TRUE;
finish:
return result;
}
#define kYearRangeLen 10
static boolean_t
token_is_yearRange(const char *str)
{
boolean_t result = FALSE;
u_int i = 0;
for (i = 0; i < kYearLen - 1; ++i) {
if (str[i] < '0' || str[i] > '9') goto finish;
}
if (str[i] != '-') goto finish;
for (i = kYearLen; i < kYearRangeLen - 1; ++i) {
if (str[i] < '0' || str[i] > '9') goto finish;
}
if (str[i] != '\0') goto finish;
result = TRUE;
finish:
return result;
}
static boolean_t
dates_are_valid(const char *str, const u_long len)
{
boolean_t result = FALSE;
const char *token_ptr = NULL;
char token_buffer[kYearRangeLen];
u_int token_index = 0;
token_index = 0;
for (token_ptr = str; token_ptr < str + len; ++token_ptr) {
if (is_token_delimiter(*token_ptr) && !token_index) continue;
if (token_index == kYearRangeLen) goto finish;
token_buffer[token_index++] = *token_ptr;
if (is_token_break(token_ptr)) {
if (!token_index) continue;
token_buffer[token_index++] = '\0';
if (!token_is_year(token_buffer) &&
!token_is_yearRange(token_buffer))
{
goto finish;
}
token_index = 0;
}
}
result = TRUE;
finish:
return result;
}
boolean_t
kxld_validate_copyright_string(const char *str)
{
boolean_t result = FALSE;
const char *copyright = NULL;
const char *rights = NULL;
char *date_str = NULL;
u_long len = 0;
copyright = kxld_strstr(str, kCopyrightToken);
rights = kxld_strstr(str, kRightsToken);
if (!copyright || !rights || copyright > rights) goto finish;
str = copyright + const_strlen(kCopyrightToken);
len = rights - str;
date_str = kxld_alloc(len);
if (!date_str) goto finish;
strncpy(date_str, str, len);
date_str[len] = '\0';
if (!dates_are_valid(date_str, len)) goto finish;
result = TRUE;
finish:
if (date_str) kxld_free(date_str, len);
return result;
}
#if TEST
int
main(int argc __unused, char *argv[] __unused)
{
int result = 1;
CFStringRef the_string = NULL;
const char *str = NULL;
u_int i = 0;
printf("The following %lu strings should pass\n",
const_array_len(passes));
for (i = 0; i < const_array_len(passes); ++i) {
the_string = passes[i];
str = createUTF8CStringForCFString(the_string);
if (!str) goto finish;
printf("%s: %s\n",
(kxld_validate_copyright_string(str)) ? "pass" : "fail", str);
}
printf("\nThe following %lu strings should fail\n",
const_array_len(fails));
for (i = 0; i < const_array_len(fails); ++i) {
the_string = fails[i];
str = createUTF8CStringForCFString(the_string);
if (!str) goto finish;
printf("%s: %s\n",
(kxld_validate_copyright_string(str)) ? "pass" : "fail", str);
}
result = 0;
finish:
return result;
}
#endif