mirror of
https://github.com/gnustep/libs-base.git
synced 2025-05-30 16:30:41 +00:00
Relax check for bad characters ... not a concern if the string data is
identifiably unicode (ie file begins with unicode BOM) and will therefore not be interpreted as data in the default encoding. git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@19629 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
parent
ca1857a281
commit
85935d3662
1 changed files with 41 additions and 9 deletions
|
@ -28,6 +28,44 @@
|
||||||
#include <Foundation/NSDebug.h>
|
#include <Foundation/NSDebug.h>
|
||||||
#include <Foundation/NSAutoreleasePool.h>
|
#include <Foundation/NSAutoreleasePool.h>
|
||||||
|
|
||||||
|
/*
|
||||||
|
* If there is any non-ascii characrer in the string,
|
||||||
|
* and the file data did not begin with a unicode BOM to identify
|
||||||
|
* it as unicode data, we return the location of the first
|
||||||
|
* bad character, otherwise return -1;
|
||||||
|
*/
|
||||||
|
static int
|
||||||
|
firstBadCharacter(NSString *file, NSString *content)
|
||||||
|
{
|
||||||
|
static NSCharacterSet *cs = nil;
|
||||||
|
NSData *d;
|
||||||
|
NSRange r;
|
||||||
|
|
||||||
|
if (cs == nil)
|
||||||
|
{
|
||||||
|
cs = [NSCharacterSet characterSetWithRange: NSMakeRange(1, 127)];
|
||||||
|
cs = RETAIN([cs invertedSet]);
|
||||||
|
}
|
||||||
|
|
||||||
|
r = [content rangeOfCharacterFromSet: cs];
|
||||||
|
if (r.length == 0)
|
||||||
|
{
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
d = [NSData dataWithContentsOfFile: file];
|
||||||
|
if ([d length] > 2)
|
||||||
|
{
|
||||||
|
const unsigned char *ptr = (const unsigned char*)[d bytes];
|
||||||
|
|
||||||
|
if ((ptr[0] == 0xff && ptr[1] == 0xfe) // UCS2
|
||||||
|
|| (ptr[0] == 0xfe && ptr[1] == 0xff) // UCS2
|
||||||
|
|| (ptr[0] == 0xef && ptr[1] == 0xbb && ptr[2] == 0xbf)) // UTF8
|
||||||
|
{
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return r.location;
|
||||||
|
}
|
||||||
|
|
||||||
/** <p>
|
/** <p>
|
||||||
This tool checks that a file contains a valid text property-list.
|
This tool checks that a file contains a valid text property-list.
|
||||||
|
@ -61,12 +99,6 @@ main(int argc, char** argv, char **env)
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
NSCharacterSet *cs;
|
|
||||||
|
|
||||||
|
|
||||||
cs = [NSCharacterSet characterSetWithRange: NSMakeRange(1, 127)];
|
|
||||||
cs = [cs invertedSet];
|
|
||||||
|
|
||||||
for (i = 1; i < [args count]; i++)
|
for (i = 1; i < [args count]; i++)
|
||||||
{
|
{
|
||||||
NSString *file = [args objectAtIndex: i];
|
NSString *file = [args objectAtIndex: i];
|
||||||
|
@ -75,14 +107,14 @@ main(int argc, char** argv, char **env)
|
||||||
{
|
{
|
||||||
NSString *myString;
|
NSString *myString;
|
||||||
id result;
|
id result;
|
||||||
NSRange r;
|
int bad;
|
||||||
|
|
||||||
myString = [NSString stringWithContentsOfFile: file];
|
myString = [NSString stringWithContentsOfFile: file];
|
||||||
if (myString == nil)
|
if (myString == nil)
|
||||||
GSPrintf(stderr, @"Parsing '%@' - not valid string\n", file);
|
GSPrintf(stderr, @"Parsing '%@' - not valid string\n", file);
|
||||||
else if ((r = [myString rangeOfCharacterFromSet: cs]).length > 0)
|
else if ((bad = firstBadCharacter(file, myString)) >= 0)
|
||||||
GSPrintf(stderr, @"Parsing '%@' - bad char '\\U%04x' at %d\n",
|
GSPrintf(stderr, @"Parsing '%@' - bad char '\\U%04x' at %d\n",
|
||||||
file, [myString characterAtIndex: r.location], r.location);
|
file, [myString characterAtIndex: bad], bad);
|
||||||
else if ((result = [myString propertyList]) == nil)
|
else if ((result = [myString propertyList]) == nil)
|
||||||
GSPrintf(stderr, @"Parsing '%@' - nil property list\n", file);
|
GSPrintf(stderr, @"Parsing '%@' - nil property list\n", file);
|
||||||
else if ([result isKindOfClass: [NSDictionary class]] == YES)
|
else if ([result isKindOfClass: [NSDictionary class]] == YES)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue