From d771ba8ec64e0b6339056590a98a6ec7d47c378a Mon Sep 17 00:00:00 2001 From: Eric Kohl Date: Sat, 1 May 2010 10:43:39 +0000 Subject: [PATCH] [NEWINFLIB] - Change Unicode string functions from wcs* to str*W because glibc (Linux build) provides the wcs* functions but they use a wchar_t size of 32 bits instead of the required 16 bits. - Add a str*W to wcs* wrapper (infrosrtl.c) in order to use the wcs* function for the WIN32 build. - Add required str*W functions to the host library. ATTENTION: This might break the build bot although it has been tested on Windows and Linux!!! svn path=/trunk/; revision=47071 --- reactos/lib/host/wcsfuncs/wcsfuncs.c | 40 ++++++++++++++++++++++ reactos/lib/newinflib/builddep.h | 27 ++++++++------- reactos/lib/newinflib/infcore.c | 28 +++++++-------- reactos/lib/newinflib/infget.c | 24 ++++++------- reactos/lib/newinflib/infhost.h | 6 ---- reactos/lib/newinflib/infhostrtl.c | 4 +-- reactos/lib/newinflib/inflib.rbuild | 1 + reactos/lib/newinflib/infput.c | 2 +- reactos/lib/newinflib/infrosput.c | 6 ++-- reactos/lib/newinflib/infrosrtl.c | 51 ++++++++++++++++++++++++++++ 10 files changed, 138 insertions(+), 51 deletions(-) create mode 100644 reactos/lib/newinflib/infrosrtl.c diff --git a/reactos/lib/host/wcsfuncs/wcsfuncs.c b/reactos/lib/host/wcsfuncs/wcsfuncs.c index fc40b7408c6..58dafe13cd3 100644 --- a/reactos/lib/host/wcsfuncs/wcsfuncs.c +++ b/reactos/lib/host/wcsfuncs/wcsfuncs.c @@ -18,6 +18,17 @@ SIZE_T utf16_wcslen(PCWSTR str) return i; } + +SIZE_T strlenW(PCWSTR str) +{ + SIZE_T i; + + for(i = 0; str[i]; i++); + + return i; +} + + PWSTR utf16_wcschr(PWSTR str, WCHAR c) { SIZE_T i; @@ -30,6 +41,18 @@ PWSTR utf16_wcschr(PWSTR str, WCHAR c) return NULL; } +PWSTR strchrW(PWSTR str, WCHAR c) +{ + SIZE_T i; + + for(i = 0; str[i] && str[i] != c; i++); + + if(str[i]) + return &str[i]; + else + return NULL; +} + INT utf16_wcsncmp(PCWSTR string1, PCWSTR string2, size_t count) { while(count--) @@ -46,3 +69,20 @@ INT utf16_wcsncmp(PCWSTR string1, PCWSTR string2, size_t count) return 0; } + +INT strncmpW(PCWSTR string1, PCWSTR string2, size_t count) +{ + while(count--) + { + if(*string1 != *string2) + return 1; + + if(*string1 == 0) + return 0; + + string1++; + string2++; + } + + return 0; +} diff --git a/reactos/lib/newinflib/builddep.h b/reactos/lib/newinflib/builddep.h index 6930a7d597d..873ab39915f 100644 --- a/reactos/lib/newinflib/builddep.h +++ b/reactos/lib/newinflib/builddep.h @@ -28,20 +28,13 @@ #define INF_STATUS_BUFFER_OVERFLOW E2BIG #define INF_SUCCESS(x) (0 == (x)) -typedef char TCHAR, *PTCHAR, *PTSTR; -typedef const TCHAR *PCTSTR; - -#define _T(x) x -#define _tcsicmp strcasecmp -#define _tcslen strlen -#define _tcscpy strcpy -#define _tcstoul strtoul -#define _tcstol strtol #define STRFMT "%s" -#ifdef _MSC_VER -#define strcasecmp _stricmp -#endif +NTSTATUS NTAPI RtlMultiByteToUnicodeN(IN PWCHAR UnicodeString, + IN ULONG UnicodeSize, IN PULONG ResultSize, IN PCSTR MbString, IN ULONG MbSize); + +BOOLEAN NTAPI RtlIsTextUnicode( PVOID buf, INT len, INT *pf ); + #define IS_TEXT_UNICODE_ASCII16 1 #define IS_TEXT_UNICODE_REVERSE_ASCII16 16 @@ -69,10 +62,18 @@ typedef const TCHAR *PCTSTR; #include #define NTOS_MODE_USER #include -#include extern PVOID InfpHeap; +INT isspaceW(WCHAR c); +INT strlenW(PCWSTR s); +PWSTR strcpyW(PWSTR d, PCWSTR s); +PWSTR strncpyW(PWSTR d, PCWSTR s, SIZE_T c); +INT strcmpiW(PCWSTR s1, PCWSTR s2); +LONG strtolW(PCWSTR s, PWSTR *e, INT r); +ULONG strtoulW(PCWSTR s, PWSTR *e, INT r); + + #define FREE(Area) RtlFreeHeap(InfpHeap, 0, (Area)) #define MALLOC(Size) RtlAllocateHeap(InfpHeap, 0, (Size)) #define ZEROMEMORY(Area, Size) RtlZeroMemory((Area), (Size)) diff --git a/reactos/lib/newinflib/infcore.c b/reactos/lib/newinflib/infcore.c index b70ce795e19..7e187359e2a 100644 --- a/reactos/lib/newinflib/infcore.c +++ b/reactos/lib/newinflib/infcore.c @@ -154,7 +154,7 @@ InfpFindSection(PINFCACHE Cache, Section = Cache->FirstSection; while (Section != NULL) { - if (_wcsicmp (Section->Name, Name) == 0) + if (strcmpiW(Section->Name, Name) == 0) { return Section; } @@ -182,7 +182,7 @@ InfpAddSection(PINFCACHE Cache, /* Allocate and initialize the new section */ Size = (ULONG)FIELD_OFFSET(INFCACHESECTION, - Name[wcslen (Name) + 1]); + Name[strlenW(Name) + 1]); Section = (PINFCACHESECTION)MALLOC(Size); if (Section == NULL) { @@ -193,7 +193,7 @@ InfpAddSection(PINFCACHE Cache, Size); /* Copy section name */ - wcscpy (Section->Name, Name); + strcpyW(Section->Name, Name); /* Append section */ if (Cache->FirstSection == NULL) @@ -266,14 +266,14 @@ InfpAddKeyToLine(PINFCACHELINE Line, return NULL; } - Line->Key = (PWCHAR)MALLOC((wcslen(Key) + 1) * sizeof(WCHAR)); + Line->Key = (PWCHAR)MALLOC((strlenW(Key) + 1) * sizeof(WCHAR)); if (Line->Key == NULL) { DPRINT1("MALLOC() failed\n"); return NULL; } - wcscpy(Line->Key, Key); + strcpyW(Line->Key, Key); return (PVOID)Line->Key; } @@ -287,7 +287,7 @@ InfpAddFieldToLine(PINFCACHELINE Line, ULONG Size; Size = (ULONG)FIELD_OFFSET(INFCACHEFIELD, - Data[wcslen(Data) + 1]); + Data[strlenW(Data) + 1]); Field = (PINFCACHEFIELD)MALLOC(Size); if (Field == NULL) { @@ -296,7 +296,7 @@ InfpAddFieldToLine(PINFCACHELINE Line, } ZEROMEMORY (Field, Size); - wcscpy (Field->Data, Data); + strcpyW(Field->Data, Data); /* Append key */ if (Line->FirstField == NULL) @@ -325,7 +325,7 @@ InfpFindKeyLine(PINFCACHESECTION Section, Line = Section->FirstLine; while (Line != NULL) { - if (Line->Key != NULL && _wcsicmp (Line->Key, Key) == 0) + if (Line->Key != NULL && strcmpiW(Line->Key, Key) == 0) { return Line; } @@ -522,7 +522,7 @@ static const WCHAR *line_start_state( struct parser *parser, const WCHAR *pos ) return p + 1; default: - if (!iswspace(*p)) + if (!isspaceW(*p)) { parser->start = p; set_state( parser, KEY_NAME ); @@ -595,7 +595,7 @@ static const WCHAR *key_name_state( struct parser *parser, const WCHAR *pos ) set_state( parser, EOL_BACKSLASH ); return p; default: - if (!iswspace(*p)) token_end = p + 1; + if (!isspaceW(*p)) token_end = p + 1; else { push_token( parser, p ); @@ -647,7 +647,7 @@ static const WCHAR *value_name_state( struct parser *parser, const WCHAR *pos ) set_state( parser, EOL_BACKSLASH ); return p; default: - if (!isspace(*p)) token_end = p + 1; + if (!isspaceW(*p)) token_end = p + 1; else { push_token( parser, p ); @@ -692,7 +692,7 @@ static const WCHAR *eol_backslash_state( struct parser *parser, const WCHAR *pos return p + 1; default: - if (iswspace(*p)) + if (isspaceW(*p)) continue; push_token( parser, p ); pop_state( parser ); @@ -749,7 +749,7 @@ static const WCHAR *leading_spaces_state( struct parser *parser, const WCHAR *po set_state( parser, EOL_BACKSLASH ); return p; } - if (!iswspace(*p)) + if (!isspaceW(*p)) break; } parser->start = p; @@ -770,7 +770,7 @@ static const WCHAR *trailing_spaces_state( struct parser *parser, const WCHAR *p set_state( parser, EOL_BACKSLASH ); return p; } - if (!iswspace(*p)) + if (!isspaceW(*p)) break; } pop_state( parser ); diff --git a/reactos/lib/newinflib/infget.c b/reactos/lib/newinflib/infget.c index 4238f1ce0b5..25177e1f975 100644 --- a/reactos/lib/newinflib/infget.c +++ b/reactos/lib/newinflib/infget.c @@ -41,7 +41,7 @@ InfpGetSubstitutionString(PINFCACHE Inf, return &percent; } - wcsncpy(ValueName, str, *len); + strncpyW(ValueName, str, *len); ValueName[*len] = 0; DPRINT("Value name: %S\n", ValueName); @@ -94,7 +94,7 @@ InfpGetSubstitutionString(PINFCACHE Inf, if (Status == STATUS_SUCCESS) { - *len = wcslen(Data); + *len = strlenW(Data); DPRINT("Substitute: %S Length: %ul\n", Data, *len); return Data; } @@ -253,7 +253,7 @@ InfpFindFirstMatchLine(PINFCONTEXT ContextIn, CacheLine = ((PINFCACHESECTION)(ContextIn->Section))->FirstLine; while (CacheLine != NULL) { - if (CacheLine->Key != NULL && _wcsicmp (CacheLine->Key, Key) == 0) + if (CacheLine->Key != NULL && strcmpiW (CacheLine->Key, Key) == 0) { if (ContextIn != ContextOut) @@ -289,7 +289,7 @@ InfpFindNextMatchLine(PINFCONTEXT ContextIn, CacheLine = (PINFCACHELINE)ContextIn->Line; while (CacheLine != NULL) { - if (CacheLine->Key != NULL && _wcsicmp (CacheLine->Key, Key) == 0) + if (CacheLine->Key != NULL && strcmpiW (CacheLine->Key, Key) == 0) { if (ContextIn != ContextOut) @@ -329,7 +329,7 @@ InfpGetLineCount(HINF InfHandle, while (CacheSection != NULL) { /* Are the section names the same? */ - if (_wcsicmp(CacheSection->Name, Section) == 0) + if (strcmpiW(CacheSection->Name, Section) == 0) { return CacheSection->LineCount; } @@ -402,7 +402,7 @@ InfpGetBinaryField(PINFCONTEXT Context, Ptr = ReturnBuffer; while (CacheField != NULL) { - *Ptr = (UCHAR)wcstoul(CacheField->Data, NULL, 16); + *Ptr = (UCHAR)strtoulW(CacheField->Data, NULL, 16); Ptr++; CacheField = CacheField->Next; @@ -450,7 +450,7 @@ InfpGetIntField(PINFCONTEXT Context, Ptr = CacheField->Data; } - *IntegerValue = (LONG)wcstol(Ptr, NULL, 0); + *IntegerValue = (LONG)strtolW(Ptr, NULL, 0); return INF_STATUS_SUCCESS; } @@ -493,7 +493,7 @@ InfpGetMultiSzField(PINFCONTEXT Context, Size = 0; while (FieldPtr != NULL) { - Size += ((ULONG)wcslen (FieldPtr->Data) + 1); + Size += ((ULONG)strlenW(FieldPtr->Data) + 1); FieldPtr = FieldPtr->Next; } Size++; @@ -511,9 +511,9 @@ InfpGetMultiSzField(PINFCONTEXT Context, FieldPtr = CacheField; while (FieldPtr != NULL) { - Size = (ULONG)wcslen (FieldPtr->Data) + 1; + Size = (ULONG)strlenW(FieldPtr->Data) + 1; - wcscpy (Ptr, FieldPtr->Data); + strcpyW(Ptr, FieldPtr->Data); Ptr = Ptr + Size; FieldPtr = FieldPtr->Next; @@ -565,7 +565,7 @@ InfpGetStringField(PINFCONTEXT Context, Ptr = CacheField->Data; } -// Size = (ULONG)wcslen (Ptr) + 1; +// Size = (ULONG)strlenW(Ptr) + 1; Size = InfpSubstituteString(Context->Inf, Ptr, NULL, @@ -579,7 +579,7 @@ InfpGetStringField(PINFCONTEXT Context, if (ReturnBufferSize <= Size) return INF_STATUS_BUFFER_OVERFLOW; -// wcscpy (ReturnBuffer, Ptr); +// strcpyW(ReturnBuffer, Ptr); InfpSubstituteString(Context->Inf, Ptr, ReturnBuffer, diff --git a/reactos/lib/newinflib/infhost.h b/reactos/lib/newinflib/infhost.h index 62403da83d8..b1dfaaf8a86 100644 --- a/reactos/lib/newinflib/infhost.h +++ b/reactos/lib/newinflib/infhost.h @@ -14,12 +14,6 @@ extern "C" { #include "infcommon.h" -extern NTSTATUS NTAPI RtlMultiByteToUnicodeN(IN PWCHAR UnicodeString, - IN ULONG UnicodeSize, IN PULONG ResultSize, IN PCSTR MbString, IN ULONG MbSize); - -extern BOOLEAN NTAPI RtlIsTextUnicode( PVOID buf, INT len, INT *pf ); - - extern int InfHostOpenBufferedFile(PHINF InfHandle, void *Buffer, ULONG BufferSize, diff --git a/reactos/lib/newinflib/infhostrtl.c b/reactos/lib/newinflib/infhostrtl.c index c9a522149df..c62d40a8d56 100644 --- a/reactos/lib/newinflib/infhostrtl.c +++ b/reactos/lib/newinflib/infhostrtl.c @@ -115,7 +115,7 @@ RtlIsTextUnicode( PVOID buf, INT len, INT *pf ) { for (i = 0; i < len; i++) { - if (wcschr(std_control_chars, s[i])) + if (strchrW(std_control_chars, s[i])) { out_flags |= IS_TEXT_UNICODE_CONTROLS; break; @@ -127,7 +127,7 @@ RtlIsTextUnicode( PVOID buf, INT len, INT *pf ) { for (i = 0; i < len; i++) { - if (wcschr(byterev_control_chars, s[i])) + if (strchrW(byterev_control_chars, s[i])) { out_flags |= IS_TEXT_UNICODE_REVERSE_CONTROLS; break; diff --git a/reactos/lib/newinflib/inflib.rbuild b/reactos/lib/newinflib/inflib.rbuild index fc7c2cdd7bb..b8c85cf50c2 100644 --- a/reactos/lib/newinflib/inflib.rbuild +++ b/reactos/lib/newinflib/inflib.rbuild @@ -9,6 +9,7 @@ infrosgen.c infrosget.c infrosput.c + infrosrtl.c . diff --git a/reactos/lib/newinflib/infput.c b/reactos/lib/newinflib/infput.c index 2d60af0fb9f..c049f3fdcf5 100644 --- a/reactos/lib/newinflib/infput.c +++ b/reactos/lib/newinflib/infput.c @@ -37,7 +37,7 @@ Output(POUTPUTBUFFER OutBuf, PCWSTR Text) } /* Doesn't fit? */ - Length = (ULONG)wcslen(Text) * sizeof(WCHAR); + Length = (ULONG)strlenW(Text) * sizeof(WCHAR); if (OutBuf->FreeSize < Length + 1 && INF_SUCCESS(OutBuf->Status)) { DPRINT("Out of free space. TotalSize %u FreeSize %u Length %u\n", diff --git a/reactos/lib/newinflib/infrosput.c b/reactos/lib/newinflib/infrosput.c index 4c6ca5a3bca..f5d3df9399e 100644 --- a/reactos/lib/newinflib/infrosput.c +++ b/reactos/lib/newinflib/infrosput.c @@ -64,13 +64,13 @@ InfWriteFile(HINF InfHandle, HeaderBuffer = MALLOC(HeaderBufferSize); if (NULL != HeaderBuffer) { - wcscpy(HeaderBuffer, L"; "); + strcpyW(HeaderBuffer, L"; "); for (Index = 0; Index < HeaderComment->Length / sizeof(WCHAR); Index++) { HeaderBuffer[2 + Index] = HeaderComment->Buffer[Index]; } - wcscpy(HeaderBuffer + (2 + HeaderComment->Length / sizeof(WCHAR)), - L"\r\n\r\n"); + strcpyW(HeaderBuffer + (2 + HeaderComment->Length / sizeof(WCHAR)), + L"\r\n\r\n"); NtWriteFile(FileHandle, NULL, NULL, diff --git a/reactos/lib/newinflib/infrosrtl.c b/reactos/lib/newinflib/infrosrtl.c new file mode 100644 index 00000000000..42a5a5e3aef --- /dev/null +++ b/reactos/lib/newinflib/infrosrtl.c @@ -0,0 +1,51 @@ +/* + * PROJECT: .inf file parser + * LICENSE: GPL - See COPYING in the top level directory + * PROGRAMMER: Eric Kohl + */ + +/* INCLUDES *****************************************************************/ + +#include "inflib.h" +#include "infhost.h" + +#define NDEBUG +#include + + +/* FUNCTIONS ****************************************************************/ + +INT isspaceW(WCHAR c) +{ + return iswspace(c); +} + +INT strlenW(PCWSTR s) +{ + return wcslen(s); +} + +PWSTR strcpyW(PWSTR d, PCWSTR s) +{ + return wcscpy(d, s); +} + +PWSTR strncpyW(PWSTR d, PCWSTR s, SIZE_T c) +{ + return wcsncpy(d, s, c); +} + +INT strcmpiW(PCWSTR s1, PCWSTR s2) +{ + return wcsicmp(s1, s2); +} + +LONG strtolW(PCWSTR s, PWSTR *e, INT r) +{ + return wcstol(s, e, r); +} + +ULONG strtoulW(PCWSTR s, PWSTR *e, INT r) +{ + return wcstoul(s, e, r); +}