Skip to content

Commit

Permalink
Merge pull request #3284 from ondrejholy/endianness
Browse files Browse the repository at this point in the history
Endianness fixes
  • Loading branch information
mfleisz committed Aug 25, 2016
2 parents 5394a04 + b8f33aa commit 71765b7
Show file tree
Hide file tree
Showing 12 changed files with 194 additions and 110 deletions.
8 changes: 4 additions & 4 deletions libfreerdp/codec/xcrush.c
Original file line number Diff line number Diff line change
Expand Up @@ -725,7 +725,7 @@ int xcrush_decompress_l1(XCRUSH_CONTEXT* xcrush, BYTE* pSrcData, UINT32 SrcSize,
if ((pSrcData + 2) > pSrcEnd)
return -1003;

MatchCount = *((UINT16*) pSrcData);
Data_Read_UINT16(pSrcData, MatchCount);

MatchDetails = (RDP61_MATCH_DETAILS*) &pSrcData[2];
Literals = (BYTE*) &MatchDetails[MatchCount];
Expand All @@ -736,9 +736,9 @@ int xcrush_decompress_l1(XCRUSH_CONTEXT* xcrush, BYTE* pSrcData, UINT32 SrcSize,

for (MatchIndex = 0; MatchIndex < MatchCount; MatchIndex++)
{
MatchLength = MatchDetails[MatchIndex].MatchLength;
MatchOutputOffset = MatchDetails[MatchIndex].MatchOutputOffset;
MatchHistoryOffset = MatchDetails[MatchIndex].MatchHistoryOffset;
Data_Read_UINT16(&MatchDetails[MatchIndex].MatchLength, MatchLength);
Data_Read_UINT16(&MatchDetails[MatchIndex].MatchOutputOffset, MatchOutputOffset);
Data_Read_UINT32(&MatchDetails[MatchIndex].MatchHistoryOffset, MatchHistoryOffset);

if (MatchOutputOffset < OutputOffset)
return -1005;
Expand Down
80 changes: 40 additions & 40 deletions winpr/include/winpr/endian.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,53 +29,53 @@ extern "C" {
#endif

#define Data_Read_UINT8_NE(_d, _v) do { _v = \
*_d; } while (0)
*((BYTE*) _d); } while (0)

#define Data_Read_UINT8(_d, _v) do { _v = \
*_d; } while (0)
*((BYTE*) _d); } while (0)

#define Data_Read_UINT16_NE(_d, _v) do { _v = \
*((UINT16*) _d); } while (0)

#define Data_Read_UINT16(_d, _v) do { _v = \
(UINT16)(*_d) + \
(((UINT16)(*(_d))) << 8); \
(UINT16)(*((BYTE*) _d)) + \
(((UINT16)(*((BYTE*) _d + 1))) << 8); \
} while (0)

#define Data_Read_UINT16_BE(_d, _v) do { _v = \
(((UINT16)(*_d)) << 8) + \
(UINT16)(*(_d + 1)); \
(((UINT16)(*(BYTE*) _d)) << 8) + \
(UINT16)(*((BYTE*) _d + 1)); \
} while (0)

#define Data_Read_UINT32_NE(_d, _v) do { _v = \
*((UINT32*) _d); } while (0)

#define Data_Read_UINT32(_d, _v) do { _v = \
(UINT32)(*_d) + \
(((UINT32)(*(_d + 1))) << 8) + \
(((UINT32)(*(_d + 2))) << 16) + \
(((UINT32)(*(_d + 3))) << 24); \
(UINT32)(*((BYTE*) _d)) + \
(((UINT32)(*((BYTE*) _d + 1))) << 8) + \
(((UINT32)(*((BYTE*) _d + 2))) << 16) + \
(((UINT32)(*((BYTE*) _d + 3))) << 24); \
} while (0)

#define Data_Read_UINT32_BE(_d, _v) do { _v = \
(((UINT32)(*(_d))) << 24) + \
(((UINT32)(*(_d + 1))) << 16) + \
(((UINT32)(*(_d + 2))) << 8) + \
(((UINT32)(*(_d + 3)))); \
(((UINT32)(*((BYTE*) _d))) << 24) + \
(((UINT32)(*((BYTE*) _d + 1))) << 16) + \
(((UINT32)(*((BYTE*) _d + 2))) << 8) + \
(((UINT32)(*((BYTE*) _d + 3)))); \
} while (0)

#define Data_Read_UINT64_NE(_d, _v) do { _v = \
*((UINT64*) _d); } while (0)

#define Data_Read_UINT64(_d, _v) do { _v = \
(UINT64)(*_d) + \
(((UINT64)(*(_d + 1))) << 8) + \
(((UINT64)(*(_d + 2))) << 16) + \
(((UINT64)(*(_d + 3))) << 24) + \
(((UINT64)(*(_d + 4))) << 32) + \
(((UINT64)(*(_d + 5))) << 40) + \
(((UINT64)(*(_d + 6))) << 48) + \
(((UINT64)(*(_d + 7))) << 56); \
(UINT64)(*((BYTE*) _d)) + \
(((UINT64)(*((BYTE*) _d + 1))) << 8) + \
(((UINT64)(*((BYTE*) _d + 2))) << 16) + \
(((UINT64)(*((BYTE*) _d + 3))) << 24) + \
(((UINT64)(*((BYTE*) _d + 4))) << 32) + \
(((UINT64)(*((BYTE*) _d + 5))) << 40) + \
(((UINT64)(*((BYTE*) _d + 6))) << 48) + \
(((UINT64)(*((BYTE*) _d + 7))) << 56); \
} while (0)

#define Data_Write_UINT8_NE(_d, _v) do { \
Expand All @@ -88,42 +88,42 @@ extern "C" {
*((UINT16*) _d) = _v; } while (0)

#define Data_Write_UINT16(_d, _v) do { \
*(_d) = (_v) & 0xFF; \
*(_d + 1) = ((_v) >> 8) & 0xFF; \
*((BYTE*) _d) = (_v) & 0xFF; \
*((BYTE*) _d + 1) = ((_v) >> 8) & 0xFF; \
} while (0)

#define Data_Write_UINT16_BE(_d, _v) do { \
*(_d) = ((_v) >> 8) & 0xFF; \
*(_d + 1) = (_v) & 0xFF; \
*((BYTE*) _d) = ((_v) >> 8) & 0xFF; \
*((BYTE*) _d + 1) = (_v) & 0xFF; \
} while (0)

#define Data_Write_UINT32_NE(_d, _v) do { \
*((UINT32*) _d) = _v; } while (0)

#define Data_Write_UINT32(_d, _v) do { \
*(_d) = (_v) & 0xFF; \
*(_d + 1) = ((_v) >> 8) & 0xFF; \
*(_d + 2) = ((_v) >> 16) & 0xFF; \
*(_d + 3) = ((_v) >> 24) & 0xFF; \
*((BYTE*) _d) = (_v) & 0xFF; \
*((BYTE*) _d + 1) = ((_v) >> 8) & 0xFF; \
*((BYTE*) _d + 2) = ((_v) >> 16) & 0xFF; \
*((BYTE*) _d + 3) = ((_v) >> 24) & 0xFF; \
} while (0)

#define Data_Write_UINT32_BE(_d, _v) do { \
Data_Write_UINT16_BE(_d, ((_v) >> 16 & 0xFFFF)); \
Data_Write_UINT16_BE(_d + 2, ((_v) & 0xFFFF)); \
Data_Write_UINT16_BE((BYTE*) _d, ((_v) >> 16 & 0xFFFF)); \
Data_Write_UINT16_BE((BYTE*) _d + 2, ((_v) & 0xFFFF)); \
} while (0)

#define Data_Write_UINT64_NE(_d, _v) do { \
*((UINT64*) _d) = _v; } while (0)

#define Data_Write_UINT64(_d, _v) do { \
*(_d) = (UINT64)(_v) & 0xFF; \
*(_d + 1) = ((UINT64)(_v) >> 8) & 0xFF; \
*(_d + 2) = ((UINT64)(_v) >> 16) & 0xFF; \
*(_d + 3) = ((UINT64)(_v) >> 24) & 0xFF; \
*(_d + 4) = ((UINT64)(_v) >> 32) & 0xFF; \
*(_d + 5) = ((UINT64)(_v) >> 40) & 0xFF; \
*(_d + 6) = ((UINT64)(_v) >> 48) & 0xFF; \
*(_d + 7) = ((UINT64)(_v) >> 56) & 0xFF; \
*((BYTE*) _d) = (UINT64)(_v) & 0xFF; \
*((BYTE*) _d + 1) = ((UINT64)(_v) >> 8) & 0xFF; \
*((BYTE*) _d + 2) = ((UINT64)(_v) >> 16) & 0xFF; \
*((BYTE*) _d + 3) = ((UINT64)(_v) >> 24) & 0xFF; \
*((BYTE*) _d + 4) = ((UINT64)(_v) >> 32) & 0xFF; \
*((BYTE*) _d + 5) = ((UINT64)(_v) >> 40) & 0xFF; \
*((BYTE*) _d + 6) = ((UINT64)(_v) >> 48) & 0xFF; \
*((BYTE*) _d + 7) = ((UINT64)(_v) >> 56) & 0xFF; \
} while (0)

#ifdef __cplusplus
Expand Down
6 changes: 3 additions & 3 deletions winpr/include/winpr/wtypes.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,15 @@
#endif

#if WINPR_HAVE_STDINT_H
#if defined(__x86_64__) || defined(__arm64__)
#if __ILP64__ || __LP64__
#define __int3264 int64_t
#define __uint3264 uint64_t
#else
#define __int3264 int32_t
#define __uint3264 uint32_t
#endif
#else
#if defined(__x86_64__) || defined(__arm64__)
#if __ILP64__ || __LP64__
#define __int3264 __int64
#define __uint3264 __uint64
#else
Expand Down Expand Up @@ -285,7 +285,7 @@ typedef void *PVOID64, *LPVOID64;
#if WINPR_HAVE_STDINT_H
typedef intptr_t INT_PTR;
typedef uintptr_t UINT_PTR;
#elif defined (__x86_64__)
#elif __ILP64__ || __LP64__ || __LLP64__
typedef __int64 INT_PTR;
typedef unsigned __int64 UINT_PTR;
#else
Expand Down
42 changes: 34 additions & 8 deletions winpr/libwinpr/crt/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <wctype.h>

#include <winpr/crt.h>
#include <winpr/endian.h>

/* String Manipulation (CRT): http://msdn.microsoft.com/en-us/library/f0151s4x.aspx */

Expand Down Expand Up @@ -86,13 +87,17 @@ int _strnicmp(const char* string1, const char* string2, size_t count)

int _wcscmp(const WCHAR* string1, const WCHAR* string2)
{
WCHAR value1, value2;

while (*string1 && (*string1 == *string2))
{
string1++;
string2++;
}

return *string1 - *string2;
Data_Read_UINT16(string1, value1);
Data_Read_UINT16(string2, value2);
return value1 - value2;
}

/* _wcslen -> wcslen */
Expand All @@ -115,11 +120,13 @@ size_t _wcslen(const WCHAR* str)
WCHAR* _wcschr(const WCHAR* str, WCHAR c)
{
WCHAR* p = (WCHAR*) str;
WCHAR value;

while (*p && (*p != c))
Data_Write_UINT16(&value, c);
while (*p && (*p != value))
p++;

return ((*p == c) ? p : NULL);
return ((*p == value) ? p : NULL);
}

char* strtok_s(char* strToken, const char* strDelimit, char** context)
Expand All @@ -130,20 +137,29 @@ char* strtok_s(char* strToken, const char* strDelimit, char** context)
WCHAR* wcstok_s(WCHAR* strToken, const WCHAR* strDelimit, WCHAR** context)
{
WCHAR* nextToken;
WCHAR value;

if (!strToken)
strToken = *context;

while (*strToken && _wcschr(strDelimit, *strToken))
Data_Read_UINT16(strToken, value);
while (*strToken && _wcschr(strDelimit, value))
{
strToken++;
Data_Read_UINT16(strToken, value);
}

if (!*strToken)
return NULL;

nextToken = strToken++;

while (*strToken && !(_wcschr(strDelimit, *strToken)))
Data_Read_UINT16(strToken, value);
while (*strToken && !(_wcschr(strDelimit, value)))
{
strToken++;
Data_Read_UINT16(strToken, value);
}

if (*strToken)
*strToken++ = 0;
Expand Down Expand Up @@ -220,10 +236,13 @@ DWORD CharUpperBuffA(LPSTR lpsz, DWORD cchLength)
DWORD CharUpperBuffW(LPWSTR lpsz, DWORD cchLength)
{
DWORD i;
WCHAR value;

for (i = 0; i < cchLength; i++)
{
lpsz[i] = WINPR_TOUPPERW(lpsz[i]);
Data_Read_UINT16(&lpsz[i], value);
value = WINPR_TOUPPERW(value);
Data_Write_UINT16(&lpsz[i], value);
}

return cchLength;
Expand Down Expand Up @@ -287,10 +306,13 @@ DWORD CharLowerBuffA(LPSTR lpsz, DWORD cchLength)
DWORD CharLowerBuffW(LPWSTR lpsz, DWORD cchLength)
{
DWORD i;
WCHAR value;

for (i = 0; i < cchLength; i++)
{
lpsz[i] = WINPR_TOLOWERW(lpsz[i]);
Data_Read_UINT16(&lpsz[i], value);
value = WINPR_TOLOWERW(value);
Data_Write_UINT16(&lpsz[i], value);
}

return cchLength;
Expand Down Expand Up @@ -380,13 +402,17 @@ int lstrcmpA(LPCSTR lpString1, LPCSTR lpString2)

int lstrcmpW(LPCWSTR lpString1, LPCWSTR lpString2)
{
WCHAR value1, value2;

while (*lpString1 && (*lpString1 == *lpString2))
{
lpString1++;
lpString2++;
}

return *lpString1 - *lpString2;
Data_Read_UINT16(lpString1, value1);
Data_Read_UINT16(lpString2, value2);
return value1 - value2;
}

#endif
Expand Down
18 changes: 18 additions & 0 deletions winpr/libwinpr/crt/test/TestString.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,40 @@ static WCHAR testToken1W[] = { 'q', 'u', 'i', 'c', 'k', '\0' };
static WCHAR testToken2W[] = { 'b', 'r', 'o', 'w', 'n', '\0' };
static WCHAR testToken3W[] = { 'f', 'o', 'x', '\0' };

#define testToken1W_Length ((sizeof(testToken1W) / sizeof(WCHAR)) - 1)
#define testToken2W_Length ((sizeof(testToken2W) / sizeof(WCHAR)) - 1)
#define testToken3W_Length ((sizeof(testToken3W) / sizeof(WCHAR)) - 1)

static WCHAR testTokensW[] =
{
'q', 'u', 'i', 'c', 'k', '\r', '\n',
'b', 'r', 'o', 'w', 'n', '\r', '\n',
'f', 'o', 'x', '\r', '\n', '\0'
};

#define testTokensW_Length ((sizeof(testTokensW) / sizeof(WCHAR)) - 1)

static WCHAR testDelimiter[] = { '\r', '\n', '\0' };

#define testDelimiter_Length ((sizeof(testDelimiter) / sizeof(WCHAR)) - 1)

int TestString(int argc, char* argv[])
{
WCHAR* p;
size_t pos;
size_t length;
WCHAR* context;

#ifdef __BIG_ENDIAN__
/* Be sure that we always use LE encoded string */
ByteSwapUnicode(testStringW, testStringW_Length);
ByteSwapUnicode(testToken1W, testToken1W_Length);
ByteSwapUnicode(testToken2W, testToken2W_Length);
ByteSwapUnicode(testToken3W, testToken3W_Length);
ByteSwapUnicode(testTokensW, testTokensW_Length);
ByteSwapUnicode(testDelimiter, testDelimiter_Length);
#endif

/* _wcslen */

length = _wcslen(testStringW);
Expand Down
Loading

0 comments on commit 71765b7

Please sign in to comment.