diff options
author | Jens-Heiner Rechtien <hr@openoffice.org> | 2000-09-18 14:18:43 +0000 |
---|---|---|
committer | Jens-Heiner Rechtien <hr@openoffice.org> | 2000-09-18 14:18:43 +0000 |
commit | 9399c662f36c385b0c705eb34e636a9aec450282 (patch) | |
tree | f502e9d9258960ff214ab90e98e31d0075e60196 /sal/rtl |
initial import
Diffstat (limited to 'sal/rtl')
-rw-r--r-- | sal/rtl/source/alloc.c | 187 | ||||
-rw-r--r-- | sal/rtl/source/byteseq.c | 263 | ||||
-rw-r--r-- | sal/rtl/source/cipher.c | 1177 | ||||
-rw-r--r-- | sal/rtl/source/crc.c | 203 | ||||
-rw-r--r-- | sal/rtl/source/digest.c | 1464 | ||||
-rw-r--r-- | sal/rtl/source/locale.c | 367 | ||||
-rw-r--r-- | sal/rtl/source/makefile.mk | 131 | ||||
-rw-r--r-- | sal/rtl/source/memory.c | 124 | ||||
-rw-r--r-- | sal/rtl/source/random.c | 383 | ||||
-rw-r--r-- | sal/rtl/source/rtl_process.c | 83 | ||||
-rw-r--r-- | sal/rtl/source/strbuf.c | 192 | ||||
-rw-r--r-- | sal/rtl/source/string.c | 1015 | ||||
-rw-r--r-- | sal/rtl/source/ustrbuf.c | 234 | ||||
-rw-r--r-- | sal/rtl/source/ustring.c | 1774 |
14 files changed, 7597 insertions, 0 deletions
diff --git a/sal/rtl/source/alloc.c b/sal/rtl/source/alloc.c new file mode 100644 index 000000000000..70b839e2c77a --- /dev/null +++ b/sal/rtl/source/alloc.c @@ -0,0 +1,187 @@ +/************************************************************************* + * + * $RCSfile: alloc.c,v $ + * + * $Revision: 1.1.1.1 $ + * + * last change: $Author: hr $ $Date: 2000-09-18 15:17:24 $ + * + * The Contents of this file are made available subject to the terms of + * either of the following licenses + * + * - GNU Lesser General Public License Version 2.1 + * - Sun Industry Standards Source License Version 1.1 + * + * Sun Microsystems Inc., October, 2000 + * + * GNU Lesser General Public License Version 2.1 + * ============================================= + * Copyright 2000 by Sun Microsystems, Inc. + * 901 San Antonio Road, Palo Alto, CA 94303, USA + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, + * MA 02111-1307 USA + * + * + * Sun Industry Standards Source License Version 1.1 + * ================================================= + * The contents of this file are subject to the Sun Industry Standards + * Source License Version 1.1 (the "License"); You may not use this file + * except in compliance with the License. You may obtain a copy of the + * License at http://www.openoffice.org/license.html. + * + * Software provided under this License is provided on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, + * WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS, + * MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING. + * See the License for the specific provisions governing your rights and + * obligations concerning the Software. + * + * The Initial Developer of the Original Code is: Sun Microsystems, Inc. + * + * Copyright: 2000 by Sun Microsystems, Inc. + * + * All Rights Reserved. + * + * Contributor(s): _______________________________________ + * + * + ************************************************************************/ + +#ifndef MAC + #ifdef MACOSX + #include <sys/types.h> + #include <sys/malloc.h> + #else + #include <malloc.h> +#endif +#else +#include <stdlib.h> +#endif +#include <string.h> + +#include <rtl/alloc.h> + +/* + * rtl_allocateMemory. + */ +void * SAL_CALL rtl_allocateMemory(sal_uInt32 Bytes) +{ + if (Bytes == 0) + return (NULL); + +#ifdef _WIN16 + return (farmalloc(Bytes)); +#else + return (malloc(Bytes)); +#endif +} + +/* + * rtl_reallocateMemory. + */ +void * SAL_CALL rtl_reallocateMemory(void *Ptr, sal_uInt32 Bytes) +{ + if (Bytes == 0) + { + if (Ptr != NULL) + rtl_freeMemory(Ptr); + + return (NULL); + } + + if (Ptr == NULL) + return (rtl_allocateMemory(Bytes)); + +#ifdef _WIN16 + return (farrealloc(Ptr, Bytes)); +#else + return (realloc(Ptr, Bytes)); +#endif +} + +/* + * rtl_freeMemory. + */ +void SAL_CALL rtl_freeMemory(void *Ptr) +{ + if (Ptr != NULL) +#ifdef _WIN16 + farfree(Ptr); +#else + free(Ptr); +#endif +} + +/* + * rtl_allocateZeroMemory. + */ +void * SAL_CALL rtl_allocateZeroMemory (sal_uInt32 Bytes) +{ + if (Bytes == 0) + return (NULL); + +#ifdef _WIN16 + return (farcalloc (Bytes)); +#else + return (calloc (Bytes, 1)); +#endif +} + +/* + * rtl_freeZeroMemory. + */ +void SAL_CALL rtl_freeZeroMemory (void *Ptr, sal_uInt32 Bytes) +{ + if (Ptr != NULL) + { +#ifdef _WIN16 + _fmemset (Ptr, 0, Bytes); + farfree (Ptr); +#else + memset (Ptr, 0, Bytes); + free (Ptr); +#endif + } +} + +#ifdef LINUX + +#if defined(MALLOC_HOOKS) +extern void *(*__malloc_hook) (size_t __size, const void* caller); +#endif + +void * calloc (size_t n, size_t elem_size) +{ + size_t sz; + void *mem; + + sz = n * elem_size; + +# if defined(MALLOC_HOOKS) + if ( __malloc_hook != NULL ) + mem = (*__malloc_hook) (sz, __builtin_return_address(0) ); + else +# endif + + mem = malloc(sz); + + if (mem != NULL) + memset (mem, 0, sz); + + return mem; +} + +#endif /* LINUX */ + diff --git a/sal/rtl/source/byteseq.c b/sal/rtl/source/byteseq.c new file mode 100644 index 000000000000..28ad9f2b2c4b --- /dev/null +++ b/sal/rtl/source/byteseq.c @@ -0,0 +1,263 @@ +/************************************************************************* + * + * $RCSfile: byteseq.c,v $ + * + * $Revision: 1.1.1.1 $ + * + * last change: $Author: hr $ $Date: 2000-09-18 15:17:24 $ + * + * The Contents of this file are made available subject to the terms of + * either of the following licenses + * + * - GNU Lesser General Public License Version 2.1 + * - Sun Industry Standards Source License Version 1.1 + * + * Sun Microsystems Inc., October, 2000 + * + * GNU Lesser General Public License Version 2.1 + * ============================================= + * Copyright 2000 by Sun Microsystems, Inc. + * 901 San Antonio Road, Palo Alto, CA 94303, USA + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, + * MA 02111-1307 USA + * + * + * Sun Industry Standards Source License Version 1.1 + * ================================================= + * The contents of this file are subject to the Sun Industry Standards + * Source License Version 1.1 (the "License"); You may not use this file + * except in compliance with the License. You may obtain a copy of the + * License at http://www.openoffice.org/license.html. + * + * Software provided under this License is provided on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, + * WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS, + * MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING. + * See the License for the specific provisions governing your rights and + * obligations concerning the Software. + * + * The Initial Developer of the Original Code is: Sun Microsystems, Inc. + * + * Copyright: 2000 by Sun Microsystems, Inc. + * + * All Rights Reserved. + * + * Contributor(s): _______________________________________ + * + * + ************************************************************************/ + +#include <osl/diagnose.h> +#include <osl/interlck.h> + +#include <rtl/byteseq.h> +#include <rtl/alloc.h> +#include <rtl/memory.h> + +/* static data to be referenced by all empty strings + * the refCount is predefined to 1 and must never become 0 ! + */ +static sal_Sequence aEmpty_rtl_ByteSeq = +{ + 1, /* sal_Int32 refCount; */ + 0, /* sal_Int32 length; */ + 0 /* sal_Unicode buffer[1]; */ +}; + +//================================================================================================== +void SAL_CALL rtl_byte_sequence_reference2One( + sal_Sequence ** ppSequence ) +{ + sal_Sequence * pSequence, * pNew; + sal_Int32 nElements; + + OSL_ENSHURE( ppSequence, "### null ptr!" ); + pSequence = *ppSequence; + + if (pSequence->nRefCount > 1) + { + nElements = pSequence->nElements; + if (nElements) + { + pNew = (sal_Sequence *)rtl_allocateMemory( SAL_SEQUENCE_HEADER_SIZE + nElements ); + + rtl_copyMemory( pNew->elements, pSequence->elements, nElements ); + + if (! osl_decrementInterlockedCount( &pSequence->nRefCount )) + rtl_freeMemory( pSequence ); + } + else + { + pNew = (sal_Sequence *)rtl_allocateMemory( SAL_SEQUENCE_HEADER_SIZE ); + } + + pNew->nRefCount = 1; + pNew->nElements = nElements; + *ppSequence = pNew; + } +} + +//================================================================================================== +void SAL_CALL rtl_byte_sequence_realloc( + sal_Sequence ** ppSequence, sal_Int32 nSize ) +{ + sal_Sequence * pSequence, * pNew; + sal_Int32 nElements; + + OSL_ENSHURE( ppSequence, "### null ptr!" ); + pSequence = *ppSequence; + nElements = pSequence->nElements; + + if (nElements == nSize) + return; + + if (pSequence->nRefCount > 1) // split + { + pNew = (sal_Sequence *)rtl_allocateMemory( SAL_SEQUENCE_HEADER_SIZE + nSize ); + + if (nSize > nElements) + { + rtl_copyMemory( pNew->elements, pSequence->elements, nElements ); + rtl_zeroMemory( pNew->elements + nElements, nSize - nElements ); + } + else + { + rtl_copyMemory( pNew->elements, pSequence->elements, nElements ); + } + + if (! osl_decrementInterlockedCount( &pSequence->nRefCount )) + rtl_freeMemory( pSequence ); + + pSequence = pNew; + } + else + { + pSequence = (sal_Sequence *)rtl_reallocateMemory( + pSequence, SAL_SEQUENCE_HEADER_SIZE + nSize ); + } + pSequence->nRefCount = 1; + pSequence->nElements = nSize; + *ppSequence = pSequence; +} + +//================================================================================================== +void SAL_CALL rtl_byte_sequence_acquire( sal_Sequence *pSequence ) +{ + OSL_ASSERT( pSequence ); + osl_incrementInterlockedCount( &(pSequence->nRefCount) ); +} + +//================================================================================================== +void SAL_CALL rtl_byte_sequence_release( sal_Sequence *pSequence ) +{ + OSL_ASSERT( pSequence ); + if (! osl_decrementInterlockedCount( &(pSequence->nRefCount )) ) + { + rtl_freeMemory( pSequence ); + } +} + +//================================================================================================== +void SAL_CALL rtl_byte_sequence_construct( sal_Sequence **ppSequence , sal_Int32 nLength ) +{ + OSL_ASSERT( ppSequence ); + if( *ppSequence ) + { + rtl_byte_sequence_release( *ppSequence ); + *ppSequence = 0; + } + + if( nLength ) + { + *ppSequence = (sal_Sequence *) rtl_allocateZeroMemory( SAL_SEQUENCE_HEADER_SIZE + nLength ); + + (*ppSequence)->nRefCount = 1; + (*ppSequence)->nElements = nLength; + } + else + { + *ppSequence = &aEmpty_rtl_ByteSeq; + rtl_byte_sequence_acquire( *ppSequence ); + } +} + +//================================================================================================== +void SAL_CALL rtl_byte_sequence_constructNoDefault( sal_Sequence **ppSequence , sal_Int32 nLength ) +{ + OSL_ASSERT( ppSequence ); + if( *ppSequence ) + { + rtl_byte_sequence_release( *ppSequence ); + *ppSequence = 0; + } + *ppSequence = (sal_Sequence *) rtl_allocateMemory( SAL_SEQUENCE_HEADER_SIZE + nLength ); + + (*ppSequence)->nRefCount = 1; + (*ppSequence)->nElements = nLength; +} + +//================================================================================================== +void SAL_CALL rtl_byte_sequence_constructFromArray( + sal_Sequence **ppSequence, const sal_Int8 *pData , sal_Int32 nLength ) +{ + rtl_byte_sequence_constructNoDefault( ppSequence , nLength ); + rtl_copyMemory( (*ppSequence)->elements, pData, nLength ); +} + +//================================================================================================== +void SAL_CALL rtl_byte_sequence_assign( sal_Sequence **ppSequence , sal_Sequence *pSequence ) +{ + if ( *ppSequence != pSequence) + { + if( *ppSequence ) + { + rtl_byte_sequence_release( *ppSequence ); + } + *ppSequence = pSequence; + rtl_byte_sequence_acquire( *ppSequence ); + } +// else +// nothing to do + +} + +//================================================================================================== +sal_Bool SAL_CALL rtl_byte_sequence_equals( sal_Sequence *pSequence1 , sal_Sequence *pSequence2 ) +{ + OSL_ASSERT( pSequence1 ); + OSL_ASSERT( pSequence2 ); + if (pSequence1 == pSequence2) + { + return sal_True; + } + if (pSequence1->nElements != pSequence2->nElements) + { + return sal_False; + } + return (0 == rtl_compareMemory( pSequence1->elements, pSequence2->elements, pSequence1->nElements )); +} + + +//================================================================================================== +const sal_Int8 *SAL_CALL rtl_byte_sequence_getConstArray( sal_Sequence *pSequence ) +{ + return pSequence->elements; +} + +//================================================================================================== +sal_Int32 SAL_CALL rtl_byte_sequence_getLength( sal_Sequence *pSequence ) +{ + return pSequence->nElements; +} diff --git a/sal/rtl/source/cipher.c b/sal/rtl/source/cipher.c new file mode 100644 index 000000000000..86c1747ef10b --- /dev/null +++ b/sal/rtl/source/cipher.c @@ -0,0 +1,1177 @@ +/************************************************************************* + * + * $RCSfile: cipher.c,v $ + * + * $Revision: 1.1.1.1 $ + * + * last change: $Author: hr $ $Date: 2000-09-18 15:17:24 $ + * + * The Contents of this file are made available subject to the terms of + * either of the following licenses + * + * - GNU Lesser General Public License Version 2.1 + * - Sun Industry Standards Source License Version 1.1 + * + * Sun Microsystems Inc., October, 2000 + * + * GNU Lesser General Public License Version 2.1 + * ============================================= + * Copyright 2000 by Sun Microsystems, Inc. + * 901 San Antonio Road, Palo Alto, CA 94303, USA + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, + * MA 02111-1307 USA + * + * + * Sun Industry Standards Source License Version 1.1 + * ================================================= + * The contents of this file are subject to the Sun Industry Standards + * Source License Version 1.1 (the "License"); You may not use this file + * except in compliance with the License. You may obtain a copy of the + * License at http://www.openoffice.org/license.html. + * + * Software provided under this License is provided on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, + * WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS, + * MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING. + * See the License for the specific provisions governing your rights and + * obligations concerning the Software. + * + * The Initial Developer of the Original Code is: Sun Microsystems, Inc. + * + * Copyright: 2000 by Sun Microsystems, Inc. + * + * All Rights Reserved. + * + * Contributor(s): _______________________________________ + * + * + ************************************************************************/ + +#define _RTL_CIPHER_C_ "$Revision: 1.1.1.1 $" + +#ifndef _SAL_TYPES_H_ +#include <sal/types.h> +#endif + +#ifndef _RTL_ALLOC_H_ +#include <rtl/alloc.h> +#endif +#ifndef _RTL_MEMORY_H_ +#include <rtl/memory.h> +#endif + +#ifndef _RTL_CIPHER_H_ +#include <rtl/cipher.h> +#endif + +/*======================================================================== + * + * rtlCipher internals. + * + *======================================================================*/ +#define RTL_CIPHER_NTOHL(c, l) \ + ((l) = ((sal_uInt32)(*((c)++))) << 24L, \ + (l) |= ((sal_uInt32)(*((c)++))) << 16L, \ + (l) |= ((sal_uInt32)(*((c)++))) << 8L, \ + (l) |= ((sal_uInt32)(*((c)++)))) + +#define RTL_CIPHER_HTONL(l, c) \ + (*((c)++) = (sal_uInt8)(((l) >> 24L) & 0xff), \ + *((c)++) = (sal_uInt8)(((l) >> 16L) & 0xff), \ + *((c)++) = (sal_uInt8)(((l) >> 8L) & 0xff), \ + *((c)++) = (sal_uInt8)(((l) ) & 0xff)) + +#define RTL_CIPHER_NTOHL64(c, xl, xr, n) \ +{ \ + (xl) = (xr) = 0; \ + (c) += (n); \ + switch ((n)) \ + { \ + case 8: (xr) = ((sal_uInt32)(*(--(c)))); \ + case 7: (xr) |= ((sal_uInt32)(*(--(c)))) << 8L; \ + case 6: (xr) |= ((sal_uInt32)(*(--(c)))) << 16L; \ + case 5: (xr) |= ((sal_uInt32)(*(--(c)))) << 24L; \ + case 4: (xl) = ((sal_uInt32)(*(--(c)))); \ + case 3: (xl) |= ((sal_uInt32)(*(--(c)))) << 8L; \ + case 2: (xl) |= ((sal_uInt32)(*(--(c)))) << 16L; \ + case 1: (xl) |= ((sal_uInt32)(*(--(c)))) << 24L; \ + } \ +} + +#define RTL_CIPHER_HTONL64(xl, xr, c, n) \ +{ \ + (c) += (n); \ + switch ((n)) \ + { \ + case 8: *(--(c)) = (sal_uInt8)(((xr) ) & 0xff); \ + case 7: *(--(c)) = (sal_uInt8)(((xr) >> 8L) & 0xff); \ + case 6: *(--(c)) = (sal_uInt8)(((xr) >> 16L) & 0xff); \ + case 5: *(--(c)) = (sal_uInt8)(((xr) >> 24L) & 0xff); \ + case 4: *(--(c)) = (sal_uInt8)(((xl) ) & 0xff); \ + case 3: *(--(c)) = (sal_uInt8)(((xl) >> 8L) & 0xff); \ + case 2: *(--(c)) = (sal_uInt8)(((xl) >> 16L) & 0xff); \ + case 1: *(--(c)) = (sal_uInt8)(((xl) >> 24L) & 0xff); \ + } \ +} + +typedef rtlCipherError (SAL_CALL cipher_init_t) ( + rtlCipher Cipher, + rtlCipherDirection Direction, + const sal_uInt8 *pKeyData, sal_uInt32 nKeyLen, + const sal_uInt8 *pArgData, sal_uInt32 nArgLen); + +typedef rtlCipherError (SAL_CALL cipher_update_t) ( + rtlCipher Cipher, + const void *pData, sal_uInt32 nDatLen, + sal_uInt8 *pBuffer, sal_uInt32 nBufLen); + +typedef void (SAL_CALL cipher_delete_t) (rtlCipher Cipher); + +/** Cipher_Impl. + */ +typedef struct cipher_impl_st +{ + rtlCipherAlgorithm m_algorithm; + rtlCipherDirection m_direction; + rtlCipherMode m_mode; + + cipher_init_t *m_init; + cipher_update_t *m_encode; + cipher_update_t *m_decode; + cipher_delete_t *m_delete; +} Cipher_Impl; + +/*======================================================================== + * + * rtlCipher implementation. + * + *======================================================================*/ +/* + * rtl_cipher_create. + */ +rtlCipher SAL_CALL rtl_cipher_create ( + rtlCipherAlgorithm Algorithm, + rtlCipherMode Mode) +{ + rtlCipher Cipher = (rtlCipher)NULL; + switch (Algorithm) + { + case rtl_Cipher_AlgorithmBF: + Cipher = rtl_cipher_createBF (Mode); + break; + + default: /* rtl_Cipher_AlgorithmInvalid */ + break; + } + return Cipher; +} + +/* + * rtl_cipher_init. + */ +rtlCipherError SAL_CALL rtl_cipher_init ( + rtlCipher Cipher, + rtlCipherDirection Direction, + const sal_uInt8 *pKeyData, sal_uInt32 nKeyLen, + const sal_uInt8 *pArgData, sal_uInt32 nArgLen) +{ + Cipher_Impl *pImpl = (Cipher_Impl*)Cipher; + if (pImpl == NULL) + return rtl_Cipher_E_Argument; + if (pImpl->m_init == NULL) + return rtl_Cipher_E_Unknown; + + return (pImpl->m_init)( + Cipher, Direction, pKeyData, nKeyLen, pArgData, nArgLen); +} + +/* + * rtl_cipher_encode. + */ +rtlCipherError SAL_CALL rtl_cipher_encode ( + rtlCipher Cipher, + const void *pData, sal_uInt32 nDatLen, + sal_uInt8 *pBuffer, sal_uInt32 nBufLen) +{ + Cipher_Impl *pImpl = (Cipher_Impl*)Cipher; + if (pImpl == NULL) + return rtl_Cipher_E_Argument; + if (pImpl->m_encode == NULL) + return rtl_Cipher_E_Unknown; + + return (pImpl->m_encode)(Cipher, pData, nDatLen, pBuffer, nBufLen); +} + +/* + * rtl_cipher_decode. + */ +rtlCipherError SAL_CALL rtl_cipher_decode ( + rtlCipher Cipher, + const void *pData, sal_uInt32 nDatLen, + sal_uInt8 *pBuffer, sal_uInt32 nBufLen) +{ + Cipher_Impl *pImpl = (Cipher_Impl*)Cipher; + if (pImpl == NULL) + return rtl_Cipher_E_Argument; + if (pImpl->m_decode == NULL) + return rtl_Cipher_E_Unknown; + + return (pImpl->m_decode)(Cipher, pData, nDatLen, pBuffer, nBufLen); +} + +/* + * rtl_cipher_destroy. + */ +void SAL_CALL rtl_cipher_destroy (rtlCipher Cipher) +{ + Cipher_Impl *pImpl = (Cipher_Impl*)Cipher; + if (pImpl && pImpl->m_delete) + pImpl->m_delete (Cipher); +} + +/*======================================================================== + * + * rtl_cipherBF (Blowfish) internals. + * + *======================================================================*/ +#define CIPHER_ROUNDS_BF 16 + +typedef struct cipherBF_key_st +{ + sal_uInt32 m_S[4][256]; + sal_uInt32 m_P[CIPHER_ROUNDS_BF + 2]; +} CipherKeyBF; + +typedef struct cipherBF_context_st +{ + CipherKeyBF m_key; + union + { + sal_uInt32 m_long[2]; + sal_uInt8 m_byte[8]; + } m_iv; + sal_uInt32 m_offset; +} CipherContextBF; + +typedef struct cipherBF_impl_st +{ + Cipher_Impl m_cipher; + CipherContextBF m_context; +} CipherBF_Impl; + +/** __rtl_cipherBF_init. + */ +static rtlCipherError __rtl_cipherBF_init ( + CipherContextBF *ctx, + rtlCipherMode eMode, + const sal_uInt8 *pKeyData, sal_uInt32 nKeyLen, + const sal_uInt8 *pArgData, sal_uInt32 nArgLen); + +/** __rtl_cipherBF_update. + */ +static rtlCipherError __rtl_cipherBF_update ( + CipherContextBF *ctx, + rtlCipherMode eMode, + rtlCipherDirection eDirection, + const sal_uInt8 *pData, sal_uInt32 nDatLen, + sal_uInt8 *pBuffer, sal_uInt32 nBufLen); + +/** __rtl_cipherBF_updateECB. + */ +static void __rtl_cipherBF_updateECB ( + CipherContextBF *ctx, + rtlCipherDirection direction, + const sal_uInt8 *pData, + sal_uInt8 *pBuffer, + sal_uInt32 nLength); + +/** __rtl_cipherBF_updateCBC. + */ +static void __rtl_cipherBF_updateCBC ( + CipherContextBF *ctx, + rtlCipherDirection direction, + const sal_uInt8 *pData, + sal_uInt8 *pBuffer, + sal_uInt32 nLength); + +/** __rtl_cipherBF_updateCFB. + */ +static void __rtl_cipherBF_updateCFB ( + CipherContextBF *ctx, + rtlCipherDirection direction, + const sal_uInt8 *pData, + sal_uInt8 *pBuffer, + sal_uInt32 nLength); + +/** __rtl_cipher_encode. + */ +static void __rtl_cipherBF_encode ( + CipherKeyBF *key, sal_uInt32 *xl, sal_uInt32 *xr); + +/** __rtl_cipherBF_decode. + */ +static void __rtl_cipherBF_decode ( + CipherKeyBF *key, sal_uInt32 *xl, sal_uInt32 *xr); + +/** __rtl_cipherBF. + */ +static sal_uInt32 __rtl_cipherBF ( + CipherKeyBF *key, sal_uInt32 x); + +/** __rtl_cipherBF_key. + */ +static const CipherKeyBF __rtl_cipherBF_key = +{ + /* S */ + { + /* S[0] */ + { + /* 0 */ + 0xD1310BA6L, 0x98DFB5ACL, 0x2FFD72DBL, 0xD01ADFB7L, + 0xB8E1AFEDL, 0x6A267E96L, 0xBA7C9045L, 0xF12C7F99L, + 0x24A19947L, 0xB3916CF7L, 0x0801F2E2L, 0x858EFC16L, + 0x636920D8L, 0x71574E69L, 0xA458FEA3L, 0xF4933D7EL, + + /* 1 */ + 0x0D95748FL, 0x728EB658L, 0x718BCD58L, 0x82154AEEL, + 0x7B54A41DL, 0xC25A59B5L, 0x9C30D539L, 0x2AF26013L, + 0xC5D1B023L, 0x286085F0L, 0xCA417918L, 0xB8DB38EFL, + 0x8E79DCB0L, 0x603A180EL, 0x6C9E0E8BL, 0xB01E8A3EL, + + /* 2 */ + 0xD71577C1L, 0xBD314B27L, 0x78AF2FDAL, 0x55605C60L, + 0xE65525F3L, 0xAA55AB94L, 0x57489862L, 0x63E81440L, + 0x55CA396AL, 0x2AAB10B6L, 0xB4CC5C34L, 0x1141E8CEL, + 0xA15486AFL, 0x7C72E993L, 0xB3EE1411L, 0x636FBC2AL, + + /* 3 */ + 0x2BA9C55DL, 0x741831F6L, 0xCE5C3E16L, 0x9B87931EL, + 0xAFD6BA33L, 0x6C24CF5CL, 0x7A325381L, 0x28958677L, + 0x3B8F4898L, 0x6B4BB9AFL, 0xC4BFE81BL, 0x66282193L, + 0x61D809CCL, 0xFB21A991L, 0x487CAC60L, 0x5DEC8032L, + + /* 4 */ + 0xEF845D5DL, 0xE98575B1L, 0xDC262302L, 0xEB651B88L, + 0x23893E81L, 0xD396ACC5L, 0x0F6D6FF3L, 0x83F44239L, + 0x2E0B4482L, 0xA4842004L, 0x69C8F04AL, 0x9E1F9B5EL, + 0x21C66842L, 0xF6E96C9AL, 0x670C9C61L, 0xABD388F0L, + + /* 5 */ + 0x6A51A0D2L, 0xD8542F68L, 0x960FA728L, 0xAB5133A3L, + 0x6EEF0B6CL, 0x137A3BE4L, 0xBA3BF050L, 0x7EFB2A98L, + 0xA1F1651DL, 0x39AF0176L, 0x66CA593EL, 0x82430E88L, + 0x8CEE8619L, 0x456F9FB4L, 0x7D84A5C3L, 0x3B8B5EBEL, + + /* 6 */ + 0xE06F75D8L, 0x85C12073L, 0x401A449FL, 0x56C16AA6L, + 0x4ED3AA62L, 0x363F7706L, 0x1BFEDF72L, 0x429B023DL, + 0x37D0D724L, 0xD00A1248L, 0xDB0FEAD3L, 0x49F1C09BL, + 0x075372C9L, 0x80991B7BL, 0x25D479D8L, 0xF6E8DEF7L, + + /* 7 */ + 0xE3FE501AL, 0xB6794C3BL, 0x976CE0BDL, 0x04C006BAL, + 0xC1A94FB6L, 0x409F60C4L, 0x5E5C9EC2L, 0x196A2463L, + 0x68FB6FAFL, 0x3E6C53B5L, 0x1339B2EBL, 0x3B52EC6FL, + 0x6DFC511FL, 0x9B30952CL, 0xCC814544L, 0xAF5EBD09L, + + /* 8 */ + 0xBEE3D004L, 0xDE334AFDL, 0x660F2807L, 0x192E4BB3L, + 0xC0CBA857L, 0x45C8740FL, 0xD20B5F39L, 0xB9D3FBDBL, + 0x5579C0BDL, 0x1A60320AL, 0xD6A100C6L, 0x402C7279L, + 0x679F25FEL, 0xFB1FA3CCL, 0x8EA5E9F8L, 0xDB3222F8L, + + /* 9 */ + 0x3C7516DFL, 0xFD616B15L, 0x2F501EC8L, 0xAD0552ABL, + 0x323DB5FAL, 0xFD238760L, 0x53317B48L, 0x3E00DF82L, + 0x9E5C57BBL, 0xCA6F8CA0L, 0x1A87562EL, 0xDF1769DBL, + 0xD542A8F6L, 0x287EFFC3L, 0xAC6732C6L, 0x8C4F5573L, + + /* A */ + 0x695B27B0L, 0xBBCA58C8L, 0xE1FFA35DL, 0xB8F011A0L, + 0x10FA3D98L, 0xFD2183B8L, 0x4AFCB56CL, 0x2DD1D35BL, + 0x9A53E479L, 0xB6F84565L, 0xD28E49BCL, 0x4BFB9790L, + 0xE1DDF2DAL, 0xA4CB7E33L, 0x62FB1341L, 0xCEE4C6E8L, + + /* B */ + 0xEF20CADAL, 0x36774C01L, 0xD07E9EFEL, 0x2BF11FB4L, + 0x95DBDA4DL, 0xAE909198L, 0xEAAD8E71L, 0x6B93D5A0L, + 0xD08ED1D0L, 0xAFC725E0L, 0x8E3C5B2FL, 0x8E7594B7L, + 0x8FF6E2FBL, 0xF2122B64L, 0x8888B812L, 0x900DF01CL, + + /* C */ + 0x4FAD5EA0L, 0x688FC31CL, 0xD1CFF191L, 0xB3A8C1ADL, + 0x2F2F2218L, 0xBE0E1777L, 0xEA752DFEL, 0x8B021FA1L, + 0xE5A0CC0FL, 0xB56F74E8L, 0x18ACF3D6L, 0xCE89E299L, + 0xB4A84FE0L, 0xFD13E0B7L, 0x7CC43B81L, 0xD2ADA8D9L, + + /* D */ + 0x165FA266L, 0x80957705L, 0x93CC7314L, 0x211A1477L, + 0xE6AD2065L, 0x77B5FA86L, 0xC75442F5L, 0xFB9D35CFL, + 0xEBCDAF0CL, 0x7B3E89A0L, 0xD6411BD3L, 0xAE1E7E49L, + 0x00250E2DL, 0x2071B35EL, 0x226800BBL, 0x57B8E0AFL, + + /* E */ + 0x2464369BL, 0xF009B91EL, 0x5563911DL, 0x59DFA6AAL, + 0x78C14389L, 0xD95A537FL, 0x207D5BA2L, 0x02E5B9C5L, + 0x83260376L, 0x6295CFA9L, 0x11C81968L, 0x4E734A41L, + 0xB3472DCAL, 0x7B14A94AL, 0x1B510052L, 0x9A532915L, + + /* F */ + 0xD60F573FL, 0xBC9BC6E4L, 0x2B60A476L, 0x81E67400L, + 0x08BA6FB5L, 0x571BE91FL, 0xF296EC6BL, 0x2A0DD915L, + 0xB6636521L, 0xE7B9F9B6L, 0xFF34052EL, 0xC5855664L, + 0x53B02D5DL, 0xA99F8FA1L, 0x08BA4799L, 0x6E85076AL + }, + + /* S[1] */ + { + 0x4B7A70E9L, 0xB5B32944L, 0xDB75092EL, 0xC4192623L, + 0xAD6EA6B0L, 0x49A7DF7DL, 0x9CEE60B8L, 0x8FEDB266L, + 0xECAA8C71L, 0x699A17FFL, 0x5664526CL, 0xC2B19EE1L, + 0x193602A5L, 0x75094C29L, 0xA0591340L, 0xE4183A3EL, + + 0x3F54989AL, 0x5B429D65L, 0x6B8FE4D6L, 0x99F73FD6L, + 0xA1D29C07L, 0xEFE830F5L, 0x4D2D38E6L, 0xF0255DC1L, + 0x4CDD2086L, 0x8470EB26L, 0x6382E9C6L, 0x021ECC5EL, + 0x09686B3FL, 0x3EBAEFC9L, 0x3C971814L, 0x6B6A70A1L, + + 0x687F3584L, 0x52A0E286L, 0xB79C5305L, 0xAA500737L, + 0x3E07841CL, 0x7FDEAE5CL, 0x8E7D44ECL, 0x5716F2B8L, + 0xB03ADA37L, 0xF0500C0DL, 0xF01C1F04L, 0x0200B3FFL, + 0xAE0CF51AL, 0x3CB574B2L, 0x25837A58L, 0xDC0921BDL, + + 0xD19113F9L, 0x7CA92FF6L, 0x94324773L, 0x22F54701L, + 0x3AE5E581L, 0x37C2DADCL, 0xC8B57634L, 0x9AF3DDA7L, + 0xA9446146L, 0x0FD0030EL, 0xECC8C73EL, 0xA4751E41L, + 0xE238CD99L, 0x3BEA0E2FL, 0x3280BBA1L, 0x183EB331L, + + 0x4E548B38L, 0x4F6DB908L, 0x6F420D03L, 0xF60A04BFL, + 0x2CB81290L, 0x24977C79L, 0x5679B072L, 0xBCAF89AFL, + 0xDE9A771FL, 0xD9930810L, 0xB38BAE12L, 0xDCCF3F2EL, + 0x5512721FL, 0x2E6B7124L, 0x501ADDE6L, 0x9F84CD87L, + + 0x7A584718L, 0x7408DA17L, 0xBC9F9ABCL, 0xE94B7D8CL, + 0xEC7AEC3AL, 0xDB851DFAL, 0x63094366L, 0xC464C3D2L, + 0xEF1C1847L, 0x3215D908L, 0xDD433B37L, 0x24C2BA16L, + 0x12A14D43L, 0x2A65C451L, 0x50940002L, 0x133AE4DDL, + + 0x71DFF89EL, 0x10314E55L, 0x81AC77D6L, 0x5F11199BL, + 0x043556F1L, 0xD7A3C76BL, 0x3C11183BL, 0x5924A509L, + 0xF28FE6EDL, 0x97F1FBFAL, 0x9EBABF2CL, 0x1E153C6EL, + 0x86E34570L, 0xEAE96FB1L, 0x860E5E0AL, 0x5A3E2AB3L, + + 0x771FE71CL, 0x4E3D06FAL, 0x2965DCB9L, 0x99E71D0FL, + 0x803E89D6L, 0x5266C825L, 0x2E4CC978L, 0x9C10B36AL, + 0xC6150EBAL, 0x94E2EA78L, 0xA5FC3C53L, 0x1E0A2DF4L, + 0xF2F74EA7L, 0x361D2B3DL, 0x1939260FL, 0x19C27960L, + + 0x5223A708L, 0xF71312B6L, 0xEBADFE6EL, 0xEAC31F66L, + 0xE3BC4595L, 0xA67BC883L, 0xB17F37D1L, 0x018CFF28L, + 0xC332DDEFL, 0xBE6C5AA5L, 0x65582185L, 0x68AB9802L, + 0xEECEA50FL, 0xDB2F953BL, 0x2AEF7DADL, 0x5B6E2F84L, + + 0x1521B628L, 0x29076170L, 0xECDD4775L, 0x619F1510L, + 0x13CCA830L, 0xEB61BD96L, 0x0334FE1EL, 0xAA0363CFL, + 0xB5735C90L, 0x4C70A239L, 0xD59E9E0BL, 0xCBAADE14L, + 0xEECC86BCL, 0x60622CA7L, 0x9CAB5CABL, 0xB2F3846EL, + + 0x648B1EAFL, 0x19BDF0CAL, 0xA02369B9L, 0x655ABB50L, + 0x40685A32L, 0x3C2AB4B3L, 0x319EE9D5L, 0xC021B8F7L, + 0x9B540B19L, 0x875FA099L, 0x95F7997EL, 0x623D7DA8L, + 0xF837889AL, 0x97E32D77L, 0x11ED935FL, 0x16681281L, + + 0x0E358829L, 0xC7E61FD6L, 0x96DEDFA1L, 0x7858BA99L, + 0x57F584A5L, 0x1B227263L, 0x9B83C3FFL, 0x1AC24696L, + 0xCDB30AEBL, 0x532E3054L, 0x8FD948E4L, 0x6DBC3128L, + 0x58EBF2EFL, 0x34C6FFEAL, 0xFE28ED61L, 0xEE7C3C73L, + + 0x5D4A14D9L, 0xE864B7E3L, 0x42105D14L, 0x203E13E0L, + 0x45EEE2B6L, 0xA3AAABEAL, 0xDB6C4F15L, 0xFACB4FD0L, + 0xC742F442L, 0xEF6ABBB5L, 0x654F3B1DL, 0x41CD2105L, + 0xD81E799EL, 0x86854DC7L, 0xE44B476AL, 0x3D816250L, + + 0xCF62A1F2L, 0x5B8D2646L, 0xFC8883A0L, 0xC1C7B6A3L, + 0x7F1524C3L, 0x69CB7492L, 0x47848A0BL, 0x5692B285L, + 0x095BBF00L, 0xAD19489DL, 0x1462B174L, 0x23820E00L, + 0x58428D2AL, 0x0C55F5EAL, 0x1DADF43EL, 0x233F7061L, + + 0x3372F092L, 0x8D937E41L, 0xD65FECF1L, 0x6C223BDBL, + 0x7CDE3759L, 0xCBEE7460L, 0x4085F2A7L, 0xCE77326EL, + 0xA6078084L, 0x19F8509EL, 0xE8EFD855L, 0x61D99735L, + 0xA969A7AAL, 0xC50C06C2L, 0x5A04ABFCL, 0x800BCADCL, + + 0x9E447A2EL, 0xC3453484L, 0xFDD56705L, 0x0E1E9EC9L, + 0xDB73DBD3L, 0x105588CDL, 0x675FDA79L, 0xE3674340L, + 0xC5C43465L, 0x713E38D8L, 0x3D28F89EL, 0xF16DFF20L, + 0x153E21E7L, 0x8FB03D4AL, 0xE6E39F2BL, 0xDB83ADF7L + }, + + /* S[2] */ + { + 0xE93D5A68L, 0x948140F7L, 0xF64C261CL, 0x94692934L, + 0x411520F7L, 0x7602D4F7L, 0xBCF46B2EL, 0xD4A20068L, + 0xD4082471L, 0x3320F46AL, 0x43B7D4B7L, 0x500061AFL, + 0x1E39F62EL, 0x97244546L, 0x14214F74L, 0xBF8B8840L, + + 0x4D95FC1DL, 0x96B591AFL, 0x70F4DDD3L, 0x66A02F45L, + 0xBFBC09ECL, 0x03BD9785L, 0x7FAC6DD0L, 0x31CB8504L, + 0x96EB27B3L, 0x55FD3941L, 0xDA2547E6L, 0xABCA0A9AL, + 0x28507825L, 0x530429F4L, 0x0A2C86DAL, 0xE9B66DFBL, + + 0x68DC1462L, 0xD7486900L, 0x680EC0A4L, 0x27A18DEEL, + 0x4F3FFEA2L, 0xE887AD8CL, 0xB58CE006L, 0x7AF4D6B6L, + 0xAACE1E7CL, 0xD3375FECL, 0xCE78A399L, 0x406B2A42L, + 0x20FE9E35L, 0xD9F385B9L, 0xEE39D7ABL, 0x3B124E8BL, + + 0x1DC9FAF7L, 0x4B6D1856L, 0x26A36631L, 0xEAE397B2L, + 0x3A6EFA74L, 0xDD5B4332L, 0x6841E7F7L, 0xCA7820FBL, + 0xFB0AF54EL, 0xD8FEB397L, 0x454056ACL, 0xBA489527L, + 0x55533A3AL, 0x20838D87L, 0xFE6BA9B7L, 0xD096954BL, + + 0x55A867BCL, 0xA1159A58L, 0xCCA92963L, 0x99E1DB33L, + 0xA62A4A56L, 0x3F3125F9L, 0x5EF47E1CL, 0x9029317CL, + 0xFDF8E802L, 0x04272F70L, 0x80BB155CL, 0x05282CE3L, + 0x95C11548L, 0xE4C66D22L, 0x48C1133FL, 0xC70F86DCL, + + 0x07F9C9EEL, 0x41041F0FL, 0x404779A4L, 0x5D886E17L, + 0x325F51EBL, 0xD59BC0D1L, 0xF2BCC18FL, 0x41113564L, + 0x257B7834L, 0x602A9C60L, 0xDFF8E8A3L, 0x1F636C1BL, + 0x0E12B4C2L, 0x02E1329EL, 0xAF664FD1L, 0xCAD18115L, + + 0x6B2395E0L, 0x333E92E1L, 0x3B240B62L, 0xEEBEB922L, + 0x85B2A20EL, 0xE6BA0D99L, 0xDE720C8CL, 0x2DA2F728L, + 0xD0127845L, 0x95B794FDL, 0x647D0862L, 0xE7CCF5F0L, + 0x5449A36FL, 0x877D48FAL, 0xC39DFD27L, 0xF33E8D1EL, + + 0x0A476341L, 0x992EFF74L, 0x3A6F6EABL, 0xF4F8FD37L, + 0xA812DC60L, 0xA1EBDDF8L, 0x991BE14CL, 0xDB6E6B0DL, + 0xC67B5510L, 0x6D672C37L, 0x2765D43BL, 0xDCD0E804L, + 0xF1290DC7L, 0xCC00FFA3L, 0xB5390F92L, 0x690FED0BL, + + 0x667B9FFBL, 0xCEDB7D9CL, 0xA091CF0BL, 0xD9155EA3L, + 0xBB132F88L, 0x515BAD24L, 0x7B9479BFL, 0x763BD6EBL, + 0x37392EB3L, 0xCC115979L, 0x8026E297L, 0xF42E312DL, + 0x6842ADA7L, 0xC66A2B3BL, 0x12754CCCL, 0x782EF11CL, + + 0x6A124237L, 0xB79251E7L, 0x06A1BBE6L, 0x4BFB6350L, + 0x1A6B1018L, 0x11CAEDFAL, 0x3D25BDD8L, 0xE2E1C3C9L, + 0x44421659L, 0x0A121386L, 0xD90CEC6EL, 0xD5ABEA2AL, + 0x64AF674EL, 0xDA86A85FL, 0xBEBFE988L, 0x64E4C3FEL, + + 0x9DBC8057L, 0xF0F7C086L, 0x60787BF8L, 0x6003604DL, + 0xD1FD8346L, 0xF6381FB0L, 0x7745AE04L, 0xD736FCCCL, + 0x83426B33L, 0xF01EAB71L, 0xB0804187L, 0x3C005E5FL, + 0x77A057BEL, 0xBDE8AE24L, 0x55464299L, 0xBF582E61L, + + 0x4E58F48FL, 0xF2DDFDA2L, 0xF474EF38L, 0x8789BDC2L, + 0x5366F9C3L, 0xC8B38E74L, 0xB475F255L, 0x46FCD9B9L, + 0x7AEB2661L, 0x8B1DDF84L, 0x846A0E79L, 0x915F95E2L, + 0x466E598EL, 0x20B45770L, 0x8CD55591L, 0xC902DE4CL, + + 0xB90BACE1L, 0xBB8205D0L, 0x11A86248L, 0x7574A99EL, + 0xB77F19B6L, 0xE0A9DC09L, 0x662D09A1L, 0xC4324633L, + 0xE85A1F02L, 0x09F0BE8CL, 0x4A99A025L, 0x1D6EFE10L, + 0x1AB93D1DL, 0x0BA5A4DFL, 0xA186F20FL, 0x2868F169L, + + 0xDCB7DA83L, 0x573906FEL, 0xA1E2CE9BL, 0x4FCD7F52L, + 0x50115E01L, 0xA70683FAL, 0xA002B5C4L, 0x0DE6D027L, + 0x9AF88C27L, 0x773F8641L, 0xC3604C06L, 0x61A806B5L, + 0xF0177A28L, 0xC0F586E0L, 0x006058AAL, 0x30DC7D62L, + + 0x11E69ED7L, 0x2338EA63L, 0x53C2DD94L, 0xC2C21634L, + 0xBBCBEE56L, 0x90BCB6DEL, 0xEBFC7DA1L, 0xCE591D76L, + 0x6F05E409L, 0x4B7C0188L, 0x39720A3DL, 0x7C927C24L, + 0x86E3725FL, 0x724D9DB9L, 0x1AC15BB4L, 0xD39EB8FCL, + + 0xED545578L, 0x08FCA5B5L, 0xD83D7CD3L, 0x4DAD0FC4L, + 0x1E50EF5EL, 0xB161E6F8L, 0xA28514D9L, 0x6C51133CL, + 0x6FD5C7E7L, 0x56E14EC4L, 0x362ABFCEL, 0xDDC6C837L, + 0xD79A3234L, 0x92638212L, 0x670EFA8EL, 0x406000E0L + }, + + /* S[3] */ + { + 0x3A39CE37L, 0xD3FAF5CFL, 0xABC27737L, 0x5AC52D1BL, + 0x5CB0679EL, 0x4FA33742L, 0xD3822740L, 0x99BC9BBEL, + 0xD5118E9DL, 0xBF0F7315L, 0xD62D1C7EL, 0xC700C47BL, + 0xB78C1B6BL, 0x21A19045L, 0xB26EB1BEL, 0x6A366EB4L, + + 0x5748AB2FL, 0xBC946E79L, 0xC6A376D2L, 0x6549C2C8L, + 0x530FF8EEL, 0x468DDE7DL, 0xD5730A1DL, 0x4CD04DC6L, + 0x2939BBDBL, 0xA9BA4650L, 0xAC9526E8L, 0xBE5EE304L, + 0xA1FAD5F0L, 0x6A2D519AL, 0x63EF8CE2L, 0x9A86EE22L, + + 0xC089C2B8L, 0x43242EF6L, 0xA51E03AAL, 0x9CF2D0A4L, + 0x83C061BAL, 0x9BE96A4DL, 0x8FE51550L, 0xBA645BD6L, + 0x2826A2F9L, 0xA73A3AE1L, 0x4BA99586L, 0xEF5562E9L, + 0xC72FEFD3L, 0xF752F7DAL, 0x3F046F69L, 0x77FA0A59L, + + 0x80E4A915L, 0x87B08601L, 0x9B09E6ADL, 0x3B3EE593L, + 0xE990FD5AL, 0x9E34D797L, 0x2CF0B7D9L, 0x022B8B51L, + 0x96D5AC3AL, 0x017DA67DL, 0xD1CF3ED6L, 0x7C7D2D28L, + 0x1F9F25CFL, 0xADF2B89BL, 0x5AD6B472L, 0x5A88F54CL, + + 0xE029AC71L, 0xE019A5E6L, 0x47B0ACFDL, 0xED93FA9BL, + 0xE8D3C48DL, 0x283B57CCL, 0xF8D56629L, 0x79132E28L, + 0x785F0191L, 0xED756055L, 0xF7960E44L, 0xE3D35E8CL, + 0x15056DD4L, 0x88F46DBAL, 0x03A16125L, 0x0564F0BDL, + + 0xC3EB9E15L, 0x3C9057A2L, 0x97271AECL, 0xA93A072AL, + 0x1B3F6D9BL, 0x1E6321F5L, 0xF59C66FBL, 0x26DCF319L, + 0x7533D928L, 0xB155FDF5L, 0x03563482L, 0x8ABA3CBBL, + 0x28517711L, 0xC20AD9F8L, 0xABCC5167L, 0xCCAD925FL, + + 0x4DE81751L, 0x3830DC8EL, 0x379D5862L, 0x9320F991L, + 0xEA7A90C2L, 0xFB3E7BCEL, 0x5121CE64L, 0x774FBE32L, + 0xA8B6E37EL, 0xC3293D46L, 0x48DE5369L, 0x6413E680L, + 0xA2AE0810L, 0xDD6DB224L, 0x69852DFDL, 0x09072166L, + + 0xB39A460AL, 0x6445C0DDL, 0x586CDECFL, 0x1C20C8AEL, + 0x5BBEF7DDL, 0x1B588D40L, 0xCCD2017FL, 0x6BB4E3BBL, + 0xDDA26A7EL, 0x3A59FF45L, 0x3E350A44L, 0xBCB4CDD5L, + 0x72EACEA8L, 0xFA6484BBL, 0x8D6612AEL, 0xBF3C6F47L, + + 0xD29BE463L, 0x542F5D9EL, 0xAEC2771BL, 0xF64E6370L, + 0x740E0D8DL, 0xE75B1357L, 0xF8721671L, 0xAF537D5DL, + 0x4040CB08L, 0x4EB4E2CCL, 0x34D2466AL, 0x0115AF84L, + 0xE1B00428L, 0x95983A1DL, 0x06B89FB4L, 0xCE6EA048L, + + 0x6F3F3B82L, 0x3520AB82L, 0x011A1D4BL, 0x277227F8L, + 0x611560B1L, 0xE7933FDCL, 0xBB3A792BL, 0x344525BDL, + 0xA08839E1L, 0x51CE794BL, 0x2F32C9B7L, 0xA01FBAC9L, + 0xE01CC87EL, 0xBCC7D1F6L, 0xCF0111C3L, 0xA1E8AAC7L, + + 0x1A908749L, 0xD44FBD9AL, 0xD0DADECBL, 0xD50ADA38L, + 0x0339C32AL, 0xC6913667L, 0x8DF9317CL, 0xE0B12B4FL, + 0xF79E59B7L, 0x43F5BB3AL, 0xF2D519FFL, 0x27D9459CL, + 0xBF97222CL, 0x15E6FC2AL, 0x0F91FC71L, 0x9B941525L, + + 0xFAE59361L, 0xCEB69CEBL, 0xC2A86459L, 0x12BAA8D1L, + 0xB6C1075EL, 0xE3056A0CL, 0x10D25065L, 0xCB03A442L, + 0xE0EC6E0EL, 0x1698DB3BL, 0x4C98A0BEL, 0x3278E964L, + 0x9F1F9532L, 0xE0D392DFL, 0xD3A0342BL, 0x8971F21EL, + + 0x1B0A7441L, 0x4BA3348CL, 0xC5BE7120L, 0xC37632D8L, + 0xDF359F8DL, 0x9B992F2EL, 0xE60B6F47L, 0x0FE3F11DL, + 0xE54CDA54L, 0x1EDAD891L, 0xCE6279CFL, 0xCD3E7E6FL, + 0x1618B166L, 0xFD2C1D05L, 0x848FD2C5L, 0xF6FB2299L, + + 0xF523F357L, 0xA6327623L, 0x93A83531L, 0x56CCCD02L, + 0xACF08162L, 0x5A75EBB5L, 0x6E163697L, 0x88D273CCL, + 0xDE966292L, 0x81B949D0L, 0x4C50901BL, 0x71C65614L, + 0xE6C6C7BDL, 0x327A140AL, 0x45E1D006L, 0xC3F27B9AL, + + 0xC9AA53FDL, 0x62A80F00L, 0xBB25BFE2L, 0x35BDD2F6L, + 0x71126905L, 0xB2040222L, 0xB6CBCF7CL, 0xCD769C2BL, + 0x53113EC0L, 0x1640E3D3L, 0x38ABBD60L, 0x2547ADF0L, + 0xBA38209CL, 0xF746CE76L, 0x77AFA1C5L, 0x20756060L, + + 0x85CBFE4EL, 0x8AE88DD8L, 0x7AAAF9B0L, 0x4CF9AA7EL, + 0x1948C25CL, 0x02FB8A8CL, 0x01C36AE4L, 0xD6EBE1F9L, + 0x90D4F869L, 0xA65CDEA0L, 0x3F09252DL, 0xC208E69FL, + 0xB74E6132L, 0xCE77E25BL, 0x578FDFE3L, 0x3AC372E6L + } + }, + + /* P */ + { + 0x243F6A88L, 0x85A308D3L, 0x13198A2EL, 0x03707344L, + 0xA4093822L, 0x299F31D0L, 0x082EFA98L, 0xEC4E6C89L, + 0x452821E6L, 0x38D01377L, 0xBE5466CFL, 0x34E90C6CL, + 0xC0AC29B7L, 0xC97C50DDL, 0x3F84D5B5L, 0xB5470917L, + 0x9216D5D9L, 0x8979FB1BL + } +}; + +/* + * __rtl_cipherBF_init. + */ +static rtlCipherError __rtl_cipherBF_init ( + CipherContextBF *ctx, + rtlCipherMode eMode, + const sal_uInt8 *pKeyData, sal_uInt32 nKeyLen, + const sal_uInt8 *pArgData, sal_uInt32 nArgLen) +{ + CipherKeyBF *key; + sal_uInt32 D, DL, DR; + sal_uInt16 i, j, k; + + key = &(ctx->m_key); + + rtl_copyMemory (key, &__rtl_cipherBF_key, sizeof (CipherKeyBF)); + rtl_zeroMemory (&(ctx->m_iv), sizeof(ctx->m_iv)); + ctx->m_offset = 0; + + for (i = 0, k = 0; i < CIPHER_ROUNDS_BF + 2; ++i) + { + D = 0; + for (j = 0; j < 4; ++j) + { + D = ((D << 8) | pKeyData[k]); + k++; + if (k >= nKeyLen) + k = 0; + } + key->m_P[i] ^= D; + } + + DL = 0; + DR = 0; + + for (i = 0; i < CIPHER_ROUNDS_BF + 2; i += 2) + { + __rtl_cipherBF_encode (key, &DL, &DR); + key->m_P[i ] = DL; + key->m_P[i + 1] = DR; + } + + for (i = 0; i < 4; ++i) + { + for (k = 0; k < 256; k += 2) + { + __rtl_cipherBF_encode (key, &DL, &DR); + key->m_S[i][k ] = DL; + key->m_S[i][k + 1] = DR; + } + } + + if (pArgData && nArgLen) + { + nArgLen = ((nArgLen < 8) ? nArgLen : 8); + if (eMode == rtl_Cipher_ModeStream) + { + rtl_copyMemory (ctx->m_iv.m_byte, pArgData, nArgLen); + } + else + { + RTL_CIPHER_NTOHL64 (pArgData, DL, DR, nArgLen); + ctx->m_iv.m_long[0] = DL; + ctx->m_iv.m_long[1] = DR; + } + } + + D = DL = DR = 0; + return rtl_Cipher_E_None; +} + +/* + * __rtl_cipherBF_update. + */ +static rtlCipherError __rtl_cipherBF_update ( + CipherContextBF *ctx, + rtlCipherMode eMode, + rtlCipherDirection eDirection, + const sal_uInt8 *pData, sal_uInt32 nDatLen, + sal_uInt8 *pBuffer, sal_uInt32 nBufLen) +{ + /* Check arguments. */ + if ((pData == NULL) || (pBuffer == NULL)) + return rtl_Cipher_E_Argument; + + if (!((0 < nDatLen) && (nDatLen <= nBufLen))) + return rtl_Cipher_E_BufferSize; + + /* Update. */ + if (eMode == rtl_Cipher_ModeECB) + { + /* Block mode. */ + while (nDatLen > 8) + { + __rtl_cipherBF_updateECB (ctx, eDirection, pData, pBuffer, 8); + nDatLen -= 8; + pData += 8; + pBuffer += 8; + } + __rtl_cipherBF_updateECB (ctx, eDirection, pData, pBuffer, nDatLen); + } + else if (eMode == rtl_Cipher_ModeCBC) + { + /* Block mode. */ + while (nDatLen > 8) + { + __rtl_cipherBF_updateCBC (ctx, eDirection, pData, pBuffer, 8); + nDatLen -= 8; + pData += 8; + pBuffer += 8; + } + __rtl_cipherBF_updateCBC (ctx, eDirection, pData, pBuffer, nDatLen); + } + else + { + /* Stream mode. */ + while (nDatLen > 0) + { + __rtl_cipherBF_updateCFB (ctx, eDirection, pData, pBuffer, 1); + nDatLen -= 1; + pData += 1; + pBuffer += 1; + } + } + return rtl_Cipher_E_None; +} + +/* + * __rtl_cipherBF_updateECB. + */ +static void __rtl_cipherBF_updateECB ( + CipherContextBF *ctx, + rtlCipherDirection direction, + const sal_uInt8 *pData, + sal_uInt8 *pBuffer, + sal_uInt32 nLength) +{ + CipherKeyBF *key; + sal_uInt32 DL, DR; + + key = &(ctx->m_key); + if (direction == rtl_Cipher_DirectionEncode) + { + RTL_CIPHER_NTOHL64(pData, DL, DR, nLength); + + __rtl_cipherBF_encode (key, &DL, &DR); + + RTL_CIPHER_HTONL(DL, pBuffer); + RTL_CIPHER_HTONL(DR, pBuffer); + } + else + { + RTL_CIPHER_NTOHL(pData, DL); + RTL_CIPHER_NTOHL(pData, DR); + + __rtl_cipherBF_decode (key, &DL, &DR); + + RTL_CIPHER_HTONL64(DL, DR, pBuffer, nLength); + } + DL = DR = 0; +} + +/* + * __rtl_cipherBF_updateCBC. + */ +static void __rtl_cipherBF_updateCBC ( + CipherContextBF *ctx, + rtlCipherDirection direction, + const sal_uInt8 *pData, + sal_uInt8 *pBuffer, + sal_uInt32 nLength) +{ + CipherKeyBF *key; + sal_uInt32 DL, DR; + + key = &(ctx->m_key); + if (direction == rtl_Cipher_DirectionEncode) + { + RTL_CIPHER_NTOHL64(pData, DL, DR, nLength); + + DL ^= ctx->m_iv.m_long[0]; + DR ^= ctx->m_iv.m_long[1]; + + __rtl_cipherBF_encode (key, &DL, &DR); + + ctx->m_iv.m_long[0] = DL; + ctx->m_iv.m_long[1] = DR; + + RTL_CIPHER_HTONL(DL, pBuffer); + RTL_CIPHER_HTONL(DR, pBuffer); + } + else + { + sal_uInt32 IVL, IVR; + + RTL_CIPHER_NTOHL(pData, DL); + RTL_CIPHER_NTOHL(pData, DR); + + IVL = DL; + IVR = DR; + + __rtl_cipherBF_decode (key, &DL, &DR); + + DL ^= ctx->m_iv.m_long[0]; + DR ^= ctx->m_iv.m_long[1]; + + ctx->m_iv.m_long[0] = IVL; + ctx->m_iv.m_long[1] = IVR; + + RTL_CIPHER_HTONL64(DL, DR, pBuffer, nLength); + IVL = IVR = 0; + } + DL = DR = 0; +} + +/* + * __rtl_cipherBF_updateCFB. + */ +static void __rtl_cipherBF_updateCFB ( + CipherContextBF *ctx, + rtlCipherDirection direction, + const sal_uInt8 *pData, + sal_uInt8 *pBuffer, + sal_uInt32 nLength) +{ + sal_uInt8 *iv; + sal_uInt32 k; + + iv = ctx->m_iv.m_byte; + k = ctx->m_offset; + + if (k == 0) + { + sal_uInt32 IVL, IVR; + + RTL_CIPHER_NTOHL64(iv, IVL, IVR, 8); + __rtl_cipherBF_encode (&(ctx->m_key), &IVL, &IVR); + RTL_CIPHER_HTONL64(IVL, IVR, iv, 8); + + IVL = IVR = 0; + } + + if (direction == rtl_Cipher_DirectionEncode) + { + iv[k] ^= *pData; + *pBuffer = iv[k]; + } + else + { + sal_uInt8 c = iv[k]; + iv[k] = *pData; + *pBuffer = *pData ^ c; + c = 0; + } + + ctx->m_offset = ((k + 1) & 0x07); + iv = NULL; +} + +/* + * __rtl_cipherBF_encode. + */ +static void __rtl_cipherBF_encode ( + CipherKeyBF *key, sal_uInt32 *xl, sal_uInt32 *xr) +{ + sal_uInt32 t, XL, XR; + sal_uInt16 i; + + XL = *xl; + XR = *xr; + + for (i = 0; i < CIPHER_ROUNDS_BF; ++i) + { + XL ^= key->m_P[i]; + XR ^= __rtl_cipherBF (key, XL); + + t = XL; + XL = XR; + XR = t; + } + + t = XL; + XL = XR; + XR = t; + + XR ^= key->m_P[CIPHER_ROUNDS_BF ]; + XL ^= key->m_P[CIPHER_ROUNDS_BF + 1]; + + *xl = XL; + *xr = XR; + + t = XL = XR = 0; +} + +/* + * __rtl_cipherBF_decode. + */ +static void __rtl_cipherBF_decode ( + CipherKeyBF *key, sal_uInt32 *xl, sal_uInt32 *xr) +{ + sal_uInt32 t, XL, XR; + sal_uInt16 i; + + XL = *xl; + XR = *xr; + + for (i = CIPHER_ROUNDS_BF + 1; i > 1; --i) + { + XL ^= key->m_P[i]; + XR ^= __rtl_cipherBF (key, XL); + + t = XL; + XL = XR; + XR = t; + } + + t = XL; + XL = XR; + XR = t; + + XR ^= key->m_P[1]; + XL ^= key->m_P[0]; + + *xl = XL; + *xr = XR; + + t = XL = XR = 0; +} + +/* + * __rtl_cipherBF. + */ +static sal_uInt32 __rtl_cipherBF (CipherKeyBF *key, sal_uInt32 x) +{ + sal_uInt16 a, b, c, d; + sal_uInt32 y; + + d = (sal_uInt16)(x & 0x00ff); + x >>= 8; + c = (sal_uInt16)(x & 0x00ff); + x >>= 8; + b = (sal_uInt16)(x & 0x00ff); + x >>= 8; + a = (sal_uInt16)(x & 0x00ff); + + y = key->m_S[0][a]; + y += key->m_S[1][b]; + y ^= key->m_S[2][c]; + y += key->m_S[3][d]; + + return y; +} + +/*======================================================================== + * + * rtl_cipherBF (Blowfish) implementation. + * + * Reference: + * Bruce Schneier: Applied Cryptography, 2nd edition, ch. 14.3 + * + *======================================================================*/ +/* + * rtl_cipher_createBF. + */ +rtlCipher SAL_CALL rtl_cipher_createBF (rtlCipherMode Mode) +{ + CipherBF_Impl *pImpl = (CipherBF_Impl*)NULL; + + if (Mode == rtl_Cipher_ModeInvalid) + return ((rtlCipher)NULL); + + pImpl = ((CipherBF_Impl*)rtl_allocateZeroMemory (sizeof (CipherBF_Impl))); + if (pImpl) + { + pImpl->m_cipher.m_algorithm = rtl_Cipher_AlgorithmBF; + pImpl->m_cipher.m_direction = rtl_Cipher_DirectionInvalid; + pImpl->m_cipher.m_mode = Mode; + + pImpl->m_cipher.m_init = rtl_cipher_initBF; + pImpl->m_cipher.m_encode = rtl_cipher_encodeBF; + pImpl->m_cipher.m_decode = rtl_cipher_decodeBF; + pImpl->m_cipher.m_delete = rtl_cipher_destroyBF; + } + return ((rtlCipher)pImpl); +} + +/* + * rtl_cipher_initBF. + */ +rtlCipherError SAL_CALL rtl_cipher_initBF ( + rtlCipher Cipher, + rtlCipherDirection Direction, + const sal_uInt8 *pKeyData, sal_uInt32 nKeyLen, + const sal_uInt8 *pArgData, sal_uInt32 nArgLen) +{ + CipherBF_Impl *pImpl = (CipherBF_Impl*)Cipher; + + if ((pImpl == NULL) || (pKeyData == NULL)) + return rtl_Cipher_E_Argument; + + if (!(pImpl->m_cipher.m_algorithm == rtl_Cipher_AlgorithmBF)) + return rtl_Cipher_E_Algorithm; + + if (!(Direction == rtl_Cipher_DirectionInvalid)) + pImpl->m_cipher.m_direction = Direction; + else + return rtl_Cipher_E_Direction; + + return __rtl_cipherBF_init ( + &(pImpl->m_context), pImpl->m_cipher.m_mode, + pKeyData, nKeyLen, pArgData, nArgLen); +} + +/* + * rtl_cipher_encodeBF. + */ +rtlCipherError SAL_CALL rtl_cipher_encodeBF ( + rtlCipher Cipher, + const void *pData, sal_uInt32 nDatLen, + sal_uInt8 *pBuffer, sal_uInt32 nBufLen) +{ + CipherBF_Impl *pImpl = (CipherBF_Impl*)Cipher; + if (pImpl == NULL) + return rtl_Cipher_E_Argument; + + if (!(pImpl->m_cipher.m_algorithm == rtl_Cipher_AlgorithmBF)) + return rtl_Cipher_E_Algorithm; + + if (pImpl->m_cipher.m_direction == rtl_Cipher_DirectionInvalid) + return rtl_Cipher_E_Direction; + if (pImpl->m_cipher.m_direction == rtl_Cipher_DirectionDecode) + return rtl_Cipher_E_Direction; + + return __rtl_cipherBF_update ( + &(pImpl->m_context), pImpl->m_cipher.m_mode, + rtl_Cipher_DirectionEncode, + (const sal_uInt8*)pData, nDatLen, pBuffer, nBufLen); +} + +/* + * rtl_cipher_decodeBF. + */ +rtlCipherError SAL_CALL rtl_cipher_decodeBF ( + rtlCipher Cipher, + const void *pData, sal_uInt32 nDatLen, + sal_uInt8 *pBuffer, sal_uInt32 nBufLen) +{ + CipherBF_Impl *pImpl = (CipherBF_Impl*)Cipher; + if (pImpl == NULL) + return rtl_Cipher_E_Argument; + + if (!(pImpl->m_cipher.m_algorithm == rtl_Cipher_AlgorithmBF)) + return rtl_Cipher_E_Algorithm; + + if (pImpl->m_cipher.m_direction == rtl_Cipher_DirectionInvalid) + return rtl_Cipher_E_Direction; + if (pImpl->m_cipher.m_direction == rtl_Cipher_DirectionEncode) + return rtl_Cipher_E_Direction; + + return __rtl_cipherBF_update ( + &(pImpl->m_context), pImpl->m_cipher.m_mode, + rtl_Cipher_DirectionDecode, + (const sal_uInt8*)pData, nDatLen, pBuffer, nBufLen); +} + +/* + * rtl_cipher_destroyBF. + */ +void SAL_CALL rtl_cipher_destroyBF (rtlCipher Cipher) +{ + CipherBF_Impl *pImpl = (CipherBF_Impl*)Cipher; + if (pImpl) + { + if (pImpl->m_cipher.m_algorithm == rtl_Cipher_AlgorithmBF) + rtl_freeZeroMemory (pImpl, sizeof (CipherBF_Impl)); + else + rtl_freeMemory (pImpl); + } +} + diff --git a/sal/rtl/source/crc.c b/sal/rtl/source/crc.c new file mode 100644 index 000000000000..7fe5bc4e4d2f --- /dev/null +++ b/sal/rtl/source/crc.c @@ -0,0 +1,203 @@ +/************************************************************************* + * + * $RCSfile: crc.c,v $ + * + * $Revision: 1.1.1.1 $ + * + * last change: $Author: hr $ $Date: 2000-09-18 15:17:24 $ + * + * The Contents of this file are made available subject to the terms of + * either of the following licenses + * + * - GNU Lesser General Public License Version 2.1 + * - Sun Industry Standards Source License Version 1.1 + * + * Sun Microsystems Inc., October, 2000 + * + * GNU Lesser General Public License Version 2.1 + * ============================================= + * Copyright 2000 by Sun Microsystems, Inc. + * 901 San Antonio Road, Palo Alto, CA 94303, USA + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, + * MA 02111-1307 USA + * + * + * Sun Industry Standards Source License Version 1.1 + * ================================================= + * The contents of this file are subject to the Sun Industry Standards + * Source License Version 1.1 (the "License"); You may not use this file + * except in compliance with the License. You may obtain a copy of the + * License at http://www.openoffice.org/license.html. + * + * Software provided under this License is provided on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, + * WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS, + * MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING. + * See the License for the specific provisions governing your rights and + * obligations concerning the Software. + * + * The Initial Developer of the Original Code is: Sun Microsystems, Inc. + * + * Copyright: 2000 by Sun Microsystems, Inc. + * + * All Rights Reserved. + * + * Contributor(s): _______________________________________ + * + * + ************************************************************************/ + +#define _RTL_CRC_C_ "$Revision: 1.1.1.1 $" + +#ifndef _SAL_TYPES_H_ +#include <sal/types.h> +#endif + +#ifndef _RTL_CRC_H_ +#include <rtl/crc.h> +#endif + +/*======================================================================== + * + * rtl_crc32Table (CRC polynomial 0xEDB88320). + * + *======================================================================*/ +static const sal_uInt32 rtl_crc32Table[256] = +{ + /* 0 */ + 0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA, + 0x076DC419, 0x706AF48F, 0xE963A535, 0x9E6495A3, + 0x0EDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988, + 0x09B64C2B, 0x7EB17CBD, 0xE7B82D07, 0x90BF1D91, + + /* 1 */ + 0x1DB71064, 0x6AB020F2, 0xF3B97148, 0x84BE41DE, + 0x1ADAD47D, 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7, + 0x136C9856, 0x646BA8C0, 0xFD62F97A, 0x8A65C9EC, + 0x14015C4F, 0x63066CD9, 0xFA0F3D63, 0x8D080DF5, + + /* 2 */ + 0x3B6E20C8, 0x4C69105E, 0xD56041E4, 0xA2677172, + 0x3C03E4D1, 0x4B04D447, 0xD20D85FD, 0xA50AB56B, + 0x35B5A8FA, 0x42B2986C, 0xDBBBC9D6, 0xACBCF940, + 0x32D86CE3, 0x45DF5C75, 0xDCD60DCF, 0xABD13D59, + + /* 3 */ + 0x26D930AC, 0x51DE003A, 0xC8D75180, 0xBFD06116, + 0x21B4F4B5, 0x56B3C423, 0xCFBA9599, 0xB8BDA50F, + 0x2802B89E, 0x5F058808, 0xC60CD9B2, 0xB10BE924, + 0x2F6F7C87, 0x58684C11, 0xC1611DAB, 0xB6662D3D, + + /* 4 */ + 0x76DC4190, 0x01DB7106, 0x98D220BC, 0xEFD5102A, + 0x71B18589, 0x06B6B51F, 0x9FBFE4A5, 0xE8B8D433, + 0x7807C9A2, 0x0F00F934, 0x9609A88E, 0xE10E9818, + 0x7F6A0DBB, 0x086D3D2D, 0x91646C97, 0xE6635C01, + + /* 5 */ + 0x6B6B51F4, 0x1C6C6162, 0x856530D8, 0xF262004E, + 0x6C0695ED, 0x1B01A57B, 0x8208F4C1, 0xF50FC457, + 0x65B0D9C6, 0x12B7E950, 0x8BBEB8EA, 0xFCB9887C, + 0x62DD1DDF, 0x15DA2D49, 0x8CD37CF3, 0xFBD44C65, + + /* 6 */ + 0x4DB26158, 0x3AB551CE, 0xA3BC0074, 0xD4BB30E2, + 0x4ADFA541, 0x3DD895D7, 0xA4D1C46D, 0xD3D6F4FB, + 0x4369E96A, 0x346ED9FC, 0xAD678846, 0xDA60B8D0, + 0x44042D73, 0x33031DE5, 0xAA0A4C5F, 0xDD0D7CC9, + + /* 7 */ + 0x5005713C, 0x270241AA, 0xBE0B1010, 0xC90C2086, + 0x5768B525, 0x206F85B3, 0xB966D409, 0xCE61E49F, + 0x5EDEF90E, 0x29D9C998, 0xB0D09822, 0xC7D7A8B4, + 0x59B33D17, 0x2EB40D81, 0xB7BD5C3B, 0xC0BA6CAD, + + /* 8 */ + 0xEDB88320, 0x9ABFB3B6, 0x03B6E20C, 0x74B1D29A, + 0xEAD54739, 0x9DD277AF, 0x04DB2615, 0x73DC1683, + 0xE3630B12, 0x94643B84, 0x0D6D6A3E, 0x7A6A5AA8, + 0xE40ECF0B, 0x9309FF9D, 0x0A00AE27, 0x7D079EB1, + + /* 9 */ + 0xF00F9344, 0x8708A3D2, 0x1E01F268, 0x6906C2FE, + 0xF762575D, 0x806567CB, 0x196C3671, 0x6E6B06E7, + 0xFED41B76, 0x89D32BE0, 0x10DA7A5A, 0x67DD4ACC, + 0xF9B9DF6F, 0x8EBEEFF9, 0x17B7BE43, 0x60B08ED5, + + /* A */ + 0xD6D6A3E8, 0xA1D1937E, 0x38D8C2C4, 0x4FDFF252, + 0xD1BB67F1, 0xA6BC5767, 0x3FB506DD, 0x48B2364B, + 0xD80D2BDA, 0xAF0A1B4C, 0x36034AF6, 0x41047A60, + 0xDF60EFC3, 0xA867DF55, 0x316E8EEF, 0x4669BE79, + + /* B */ + 0xCB61B38C, 0xBC66831A, 0x256FD2A0, 0x5268E236, + 0xCC0C7795, 0xBB0B4703, 0x220216B9, 0x5505262F, + 0xC5BA3BBE, 0xB2BD0B28, 0x2BB45A92, 0x5CB36A04, + 0xC2D7FFA7, 0xB5D0CF31, 0x2CD99E8B, 0x5BDEAE1D, + + /* C */ + 0x9B64C2B0, 0xEC63F226, 0x756AA39C, 0x026D930A, + 0x9C0906A9, 0xEB0E363F, 0x72076785, 0x05005713, + 0x95BF4A82, 0xE2B87A14, 0x7BB12BAE, 0x0CB61B38, + 0x92D28E9B, 0xE5D5BE0D, 0x7CDCEFB7, 0x0BDBDF21, + + /* D */ + 0x86D3D2D4, 0xF1D4E242, 0x68DDB3F8, 0x1FDA836E, + 0x81BE16CD, 0xF6B9265B, 0x6FB077E1, 0x18B74777, + 0x88085AE6, 0xFF0F6A70, 0x66063BCA, 0x11010B5C, + 0x8F659EFF, 0xF862AE69, 0x616BFFD3, 0x166CCF45, + + /* E */ + 0xA00AE278, 0xD70DD2EE, 0x4E048354, 0x3903B3C2, + 0xA7672661, 0xD06016F7, 0x4969474D, 0x3E6E77DB, + 0xAED16A4A, 0xD9D65ADC, 0x40DF0B66, 0x37D83BF0, + 0xA9BCAE53, 0xDEBB9EC5, 0x47B2CF7F, 0x30B5FFE9, + + /* F */ + 0xBDBDF21C, 0xCABAC28A, 0x53B39330, 0x24B4A3A6, + 0xBAD03605, 0xCDD70693, 0x54DE5729, 0x23D967BF, + 0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94, + 0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D +}; + +/*======================================================================== + * + * rtl_crc32 implementation. + * + *======================================================================*/ +#define UPDCRC32(crc, octet) \ + (rtl_crc32Table[((crc) ^ (octet)) & 0xff] ^ ((crc) >> 8)) + +/* + * rtl_crc32. + */ +sal_uInt32 SAL_CALL rtl_crc32 ( + sal_uInt32 Crc, + const void *Data, sal_uInt32 DatLen) +{ + if (Data) + { + register const sal_uInt8 *p = (const sal_uInt8 *)Data; + register const sal_uInt8 *q = p + DatLen; + + Crc = ~Crc; + while (p < q) + Crc = UPDCRC32(Crc, *(p++)); + Crc = ~Crc; + } + return Crc; +} + diff --git a/sal/rtl/source/digest.c b/sal/rtl/source/digest.c new file mode 100644 index 000000000000..643b994b6b4d --- /dev/null +++ b/sal/rtl/source/digest.c @@ -0,0 +1,1464 @@ +/************************************************************************* + * + * $RCSfile: digest.c,v $ + * + * $Revision: 1.1.1.1 $ + * + * last change: $Author: hr $ $Date: 2000-09-18 15:17:24 $ + * + * The Contents of this file are made available subject to the terms of + * either of the following licenses + * + * - GNU Lesser General Public License Version 2.1 + * - Sun Industry Standards Source License Version 1.1 + * + * Sun Microsystems Inc., October, 2000 + * + * GNU Lesser General Public License Version 2.1 + * ============================================= + * Copyright 2000 by Sun Microsystems, Inc. + * 901 San Antonio Road, Palo Alto, CA 94303, USA + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, + * MA 02111-1307 USA + * + * + * Sun Industry Standards Source License Version 1.1 + * ================================================= + * The contents of this file are subject to the Sun Industry Standards + * Source License Version 1.1 (the "License"); You may not use this file + * except in compliance with the License. You may obtain a copy of the + * License at http://www.openoffice.org/license.html. + * + * Software provided under this License is provided on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, + * WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS, + * MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING. + * See the License for the specific provisions governing your rights and + * obligations concerning the Software. + * + * The Initial Developer of the Original Code is: Sun Microsystems, Inc. + * + * Copyright: 2000 by Sun Microsystems, Inc. + * + * All Rights Reserved. + * + * Contributor(s): _______________________________________ + * + * + ************************************************************************/ + +#define _RTL_DIGEST_C_ "$Revision: 1.1.1.1 $" + +#ifndef _SAL_TYPES_H_ +#include <sal/types.h> +#endif + +#ifndef _RTL_ALLOC_H_ +#include <rtl/alloc.h> +#endif + +#ifndef _RTL_MEMORY_H_ +#include <rtl/memory.h> +#endif + +#ifndef _RTL_DIGEST_H_ +#include <rtl/digest.h> +#endif + +/*======================================================================== + * + * rtlDigest internals. + * + *======================================================================*/ +#ifndef SAL_W32 +#define RTL_DIGEST_ROTL(a,n) (((a) << (n)) | ((a) >> (32 - (n)))) +#else +#define RTL_DIGEST_ROTL(a,n) (_rotl((a), (n))) +#endif + +#define RTL_DIGEST_SWAPLONG(x) \ + ((((x) >> 24) & 0x000000ff) | (((x) & 0x00ff0000) >> 8) | \ + (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24) ) + +#define RTL_DIGEST_HTONL(l,c) \ + (*((c)++) = (sal_uInt8)(((l) >> 24L) & 0xff), \ + *((c)++) = (sal_uInt8)(((l) >> 16L) & 0xff), \ + *((c)++) = (sal_uInt8)(((l) >> 8L) & 0xff), \ + *((c)++) = (sal_uInt8)(((l) ) & 0xff)) + +#define RTL_DIGEST_LTOC(l,c) \ + (*((c)++) = (sal_uInt8)(((l) ) & 0xff), \ + *((c)++) = (sal_uInt8)(((l) >> 8L) & 0xff), \ + *((c)++) = (sal_uInt8)(((l) >> 16L) & 0xff), \ + *((c)++) = (sal_uInt8)(((l) >> 24L) & 0xff)) + +typedef void* (SAL_CALL Digest_new_t) (void); +typedef void (SAL_CALL Digest_delete_t) (void *ctx); + +typedef rtlDigestError (SAL_CALL Digest_update_t) ( + void *ctx, const void *Data, sal_uInt32 DatLen); + +typedef rtlDigestError (SAL_CALL Digest_get_t) ( + void *ctx, sal_uInt8 *Buffer, sal_uInt32 BufLen); + +typedef struct digest_impl_st +{ + rtlDigestAlgorithm m_algorithm; + sal_uInt32 m_length; + + Digest_new_t *m_new; + Digest_delete_t *m_delete; + Digest_update_t *m_update; + Digest_get_t *m_get; +} Digest_Impl; + +/*======================================================================== + * + * rtlDigest implementation. + * + *======================================================================*/ +/* + * rtl_digest_create. + */ +rtlDigest SAL_CALL rtl_digest_create (rtlDigestAlgorithm Algorithm) +{ + rtlDigest Digest = (rtlDigest)NULL; + switch (Algorithm) + { + case rtl_Digest_AlgorithmMD2: + Digest = rtl_digest_createMD2(); + break; + + case rtl_Digest_AlgorithmMD5: + Digest = rtl_digest_createMD5(); + break; + + case rtl_Digest_AlgorithmSHA: + Digest = rtl_digest_createSHA(); + break; + + case rtl_Digest_AlgorithmSHA1: + Digest = rtl_digest_createSHA1(); + break; + + default: /* rtl_Digest_AlgorithmInvalid */ + break; + } + return Digest; +} + +/* + * rtl_digest_queryAlgorithm. + */ +rtlDigestAlgorithm SAL_CALL rtl_digest_queryAlgorithm (rtlDigest Digest) +{ + Digest_Impl *pImpl = (Digest_Impl *)Digest; + if (pImpl) + return pImpl->m_algorithm; + else + return rtl_Digest_AlgorithmInvalid; +} + +/* + * rtl_digest_queryLength. + */ +sal_uInt32 SAL_CALL rtl_digest_queryLength (rtlDigest Digest) +{ + Digest_Impl *pImpl = (Digest_Impl *)Digest; + if (pImpl) + return pImpl->m_length; + else + return 0; +} + +/* + * rtl_digest_update. + */ +rtlDigestError SAL_CALL rtl_digest_update ( + rtlDigest Digest, const void *pData, sal_uInt32 nDatLen) +{ + Digest_Impl *pImpl = (Digest_Impl *)Digest; + if (pImpl && pImpl->m_update) + return pImpl->m_update (Digest, pData, nDatLen); + else + return rtl_Digest_E_Argument; +} + +/* + * rtl_digest_get. + */ +rtlDigestError SAL_CALL rtl_digest_get ( + rtlDigest Digest, sal_uInt8 *pBuffer, sal_uInt32 nBufLen) +{ + Digest_Impl *pImpl = (Digest_Impl *)Digest; + if (pImpl && pImpl->m_get) + return pImpl->m_get (Digest, pBuffer, nBufLen); + else + return rtl_Digest_E_Argument; +} + +/* + * rtl_digest_destroy. + */ +void SAL_CALL rtl_digest_destroy (rtlDigest Digest) +{ + Digest_Impl *pImpl = (Digest_Impl *)Digest; + if (pImpl && pImpl->m_delete) + pImpl->m_delete (Digest); +} + +/*======================================================================== + * + * rtl_digest_MD2 internals. + * + *======================================================================*/ +#define DIGEST_CBLOCK_MD2 16 +#define DIGEST_LBLOCK_MD2 16 + +typedef struct digestMD2_context_st +{ + sal_uInt32 m_nDatLen; + sal_uInt8 m_pData[DIGEST_CBLOCK_MD2]; + sal_uInt32 m_state[DIGEST_LBLOCK_MD2]; + sal_uInt32 m_chksum[DIGEST_LBLOCK_MD2]; +} DigestContextMD2; + +typedef struct digestMD2_impl_st +{ + Digest_Impl m_digest; + DigestContextMD2 m_context; +} DigestMD2_Impl; + +static void __rtl_digest_initMD2 (DigestContextMD2 *ctx); +static void __rtl_digest_updateMD2 (DigestContextMD2 *ctx); +static void __rtl_digest_endMD2 (DigestContextMD2 *ctx); + +static const sal_uInt32 S[256] = +{ + 0x29, 0x2E, 0x43, 0xC9, 0xA2, 0xD8, 0x7C, 0x01, + 0x3D, 0x36, 0x54, 0xA1, 0xEC, 0xF0, 0x06, 0x13, + 0x62, 0xA7, 0x05, 0xF3, 0xC0, 0xC7, 0x73, 0x8C, + 0x98, 0x93, 0x2B, 0xD9, 0xBC, 0x4C, 0x82, 0xCA, + 0x1E, 0x9B, 0x57, 0x3C, 0xFD, 0xD4, 0xE0, 0x16, + 0x67, 0x42, 0x6F, 0x18, 0x8A, 0x17, 0xE5, 0x12, + 0xBE, 0x4E, 0xC4, 0xD6, 0xDA, 0x9E, 0xDE, 0x49, + 0xA0, 0xFB, 0xF5, 0x8E, 0xBB, 0x2F, 0xEE, 0x7A, + 0xA9, 0x68, 0x79, 0x91, 0x15, 0xB2, 0x07, 0x3F, + 0x94, 0xC2, 0x10, 0x89, 0x0B, 0x22, 0x5F, 0x21, + 0x80, 0x7F, 0x5D, 0x9A, 0x5A, 0x90, 0x32, 0x27, + 0x35, 0x3E, 0xCC, 0xE7, 0xBF, 0xF7, 0x97, 0x03, + 0xFF, 0x19, 0x30, 0xB3, 0x48, 0xA5, 0xB5, 0xD1, + 0xD7, 0x5E, 0x92, 0x2A, 0xAC, 0x56, 0xAA, 0xC6, + 0x4F, 0xB8, 0x38, 0xD2, 0x96, 0xA4, 0x7D, 0xB6, + 0x76, 0xFC, 0x6B, 0xE2, 0x9C, 0x74, 0x04, 0xF1, + 0x45, 0x9D, 0x70, 0x59, 0x64, 0x71, 0x87, 0x20, + 0x86, 0x5B, 0xCF, 0x65, 0xE6, 0x2D, 0xA8, 0x02, + 0x1B, 0x60, 0x25, 0xAD, 0xAE, 0xB0, 0xB9, 0xF6, + 0x1C, 0x46, 0x61, 0x69, 0x34, 0x40, 0x7E, 0x0F, + 0x55, 0x47, 0xA3, 0x23, 0xDD, 0x51, 0xAF, 0x3A, + 0xC3, 0x5C, 0xF9, 0xCE, 0xBA, 0xC5, 0xEA, 0x26, + 0x2C, 0x53, 0x0D, 0x6E, 0x85, 0x28, 0x84, 0x09, + 0xD3, 0xDF, 0xCD, 0xF4, 0x41, 0x81, 0x4D, 0x52, + 0x6A, 0xDC, 0x37, 0xC8, 0x6C, 0xC1, 0xAB, 0xFA, + 0x24, 0xE1, 0x7B, 0x08, 0x0C, 0xBD, 0xB1, 0x4A, + 0x78, 0x88, 0x95, 0x8B, 0xE3, 0x63, 0xE8, 0x6D, + 0xE9, 0xCB, 0xD5, 0xFE, 0x3B, 0x00, 0x1D, 0x39, + 0xF2, 0xEF, 0xB7, 0x0E, 0x66, 0x58, 0xD0, 0xE4, + 0xA6, 0x77, 0x72, 0xF8, 0xEB, 0x75, 0x4B, 0x0A, + 0x31, 0x44, 0x50, 0xB4, 0x8F, 0xED, 0x1F, 0x1A, + 0xDB, 0x99, 0x8D, 0x33, 0x9F, 0x11, 0x83, 0x14, +}; + +/* + * __rtl_digest_initMD2. + */ +static void __rtl_digest_initMD2 (DigestContextMD2 *ctx) +{ + rtl_zeroMemory (ctx, sizeof (DigestContextMD2)); +} + +/* + * __rtl_digest_updateMD2. + */ +static void __rtl_digest_updateMD2 (DigestContextMD2 *ctx) +{ + register sal_uInt8 *X; + register sal_uInt32 *sp1, *sp2; + register sal_uInt32 i, k, t; + + sal_uInt32 state[48]; + + X = ctx->m_pData; + sp1 = ctx->m_state; + sp2 = ctx->m_chksum; + + k = sp2[DIGEST_LBLOCK_MD2 - 1]; + for (i = 0; i < 16; i++) + { + state[i + 0] = sp1[i]; + state[i + 16] = t = X[i]; + state[i + 32] = t ^ sp1[i]; + k = sp2[i] ^= S[t^k]; + } + + t = 0; + for (i = 0; i < 18; i++) + { + for (k = 0; k < 48; k += 8) + { + t = state[k + 0] ^= S[t]; + t = state[k + 1] ^= S[t]; + t = state[k + 2] ^= S[t]; + t = state[k + 3] ^= S[t]; + t = state[k + 4] ^= S[t]; + t = state[k + 5] ^= S[t]; + t = state[k + 6] ^= S[t]; + t = state[k + 7] ^= S[t]; + } + t = ((t + i) & 0xff); + } + + rtl_copyMemory (sp1, state, 16 * sizeof(sal_uInt32)); + rtl_zeroMemory (state, 48 * sizeof(sal_uInt32)); +} + +/* + * __rtl_digest_endMD2. + */ +static void __rtl_digest_endMD2 (DigestContextMD2 *ctx) +{ + register sal_uInt8 *X; + register sal_uInt32 *C; + sal_uInt32 i, n; + + X = ctx->m_pData; + C = ctx->m_chksum; + n = DIGEST_CBLOCK_MD2 - ctx->m_nDatLen; + + for (i = ctx->m_nDatLen; i < DIGEST_CBLOCK_MD2; i++) + X[i] = (sal_uInt8)(n & 0xff); + __rtl_digest_updateMD2 (ctx); + + for (i = 0; i < DIGEST_CBLOCK_MD2; i++) + X[i] = (sal_uInt8)(C[i] & 0xff); + __rtl_digest_updateMD2 (ctx); +} + +/*======================================================================== + * + * rtl_digest_MD2 implementation. + * + *======================================================================*/ +/* + * rtl_digest_MD2. + */ +rtlDigestError SAL_CALL rtl_digest_MD2 ( + const void *pData, sal_uInt32 nDatLen, + sal_uInt8 *pBuffer, sal_uInt32 nBufLen) +{ + rtlDigestError Result = rtl_Digest_E_Memory; + rtlDigest Digest = rtl_digest_createMD2(); + if (Digest) + { + Result = rtl_digest_updateMD2 (Digest, pData, nDatLen); + if (Result == rtl_Digest_E_None) + Result = rtl_digest_getMD2 (Digest, pBuffer, nBufLen); + rtl_digest_destroyMD2 (Digest); + } + return Result; +} + +/* + * rtl_digest_createMD2. + */ +rtlDigest SAL_CALL rtl_digest_createMD2 (void) +{ + DigestMD2_Impl *pImpl = (DigestMD2_Impl*)NULL; + pImpl = ((DigestMD2_Impl*)rtl_allocateZeroMemory (sizeof(DigestMD2_Impl))); + if (pImpl) + { + pImpl->m_digest.m_algorithm = rtl_Digest_AlgorithmMD2; + pImpl->m_digest.m_length = RTL_DIGEST_LENGTH_MD2; + + pImpl->m_digest.m_delete = rtl_digest_destroyMD2; + pImpl->m_digest.m_update = rtl_digest_updateMD2; + pImpl->m_digest.m_get = rtl_digest_getMD2; + + __rtl_digest_initMD2 (&(pImpl->m_context)); + } + return ((rtlDigest)pImpl); +} + +/* + * rtl_digest_updateMD2. + */ +rtlDigestError SAL_CALL rtl_digest_updateMD2 ( + rtlDigest Digest, const void *pData, sal_uInt32 nDatLen) +{ + DigestMD2_Impl *pImpl = (DigestMD2_Impl *)Digest; + const sal_uInt8 *d = (const sal_uInt8 *)pData; + + DigestContextMD2 *ctx; + + if ((pImpl == NULL) || (pData == NULL)) + return rtl_Digest_E_Argument; + + if (!(pImpl->m_digest.m_algorithm == rtl_Digest_AlgorithmMD2)) + return rtl_Digest_E_Algorithm; + + if (nDatLen == 0) + return rtl_Digest_E_None; + + ctx = &(pImpl->m_context); + + if (ctx->m_nDatLen) + { + sal_uInt8 *p = ctx->m_pData + ctx->m_nDatLen; + sal_uInt32 n = DIGEST_CBLOCK_MD2 - ctx->m_nDatLen; + + if (nDatLen < n) + { + rtl_copyMemory (p, d, nDatLen); + ctx->m_nDatLen += nDatLen; + + return rtl_Digest_E_None; + } + + rtl_copyMemory (p, d, n); + d += n; + nDatLen -= n; + + __rtl_digest_updateMD2 (ctx); + ctx->m_nDatLen = 0; + } + + while (nDatLen >= DIGEST_CBLOCK_MD2) + { + rtl_copyMemory (ctx->m_pData, d, DIGEST_CBLOCK_MD2); + d += DIGEST_CBLOCK_MD2; + nDatLen -= DIGEST_CBLOCK_MD2; + + __rtl_digest_updateMD2 (ctx); + } + + rtl_copyMemory (ctx->m_pData, d, nDatLen); + ctx->m_nDatLen = nDatLen; + + return rtl_Digest_E_None; +} + +/* + * rtl_digest_getMD2. + */ +rtlDigestError SAL_CALL rtl_digest_getMD2 ( + rtlDigest Digest, sal_uInt8 *pBuffer, sal_uInt32 nBufLen) +{ + DigestMD2_Impl *pImpl = (DigestMD2_Impl *)Digest; + sal_uInt32 i; + + DigestContextMD2 *ctx; + + if ((pImpl == NULL) || (pBuffer == NULL)) + return rtl_Digest_E_Argument; + + if (!(pImpl->m_digest.m_algorithm == rtl_Digest_AlgorithmMD2)) + return rtl_Digest_E_Algorithm; + + if (!(pImpl->m_digest.m_length <= nBufLen)) + return rtl_Digest_E_BufferSize; + + ctx = &(pImpl->m_context); + + __rtl_digest_endMD2 (ctx); + for (i = 0; i < DIGEST_CBLOCK_MD2; i++) + pBuffer[i] = (sal_uInt8)(ctx->m_state[i] & 0xff); + __rtl_digest_initMD2 (ctx); + + return rtl_Digest_E_None; +} + +/* + * rtl_digest_destroyMD2. + */ +void SAL_CALL rtl_digest_destroyMD2 (rtlDigest Digest) +{ + DigestMD2_Impl *pImpl = (DigestMD2_Impl *)Digest; + if (pImpl) + { + if (pImpl->m_digest.m_algorithm == rtl_Digest_AlgorithmMD2) + rtl_freeZeroMemory (pImpl, sizeof (DigestMD2_Impl)); + else + rtl_freeMemory (pImpl); + } +} + +/*======================================================================== + * + * rtl_digest_MD5 internals. + * + *======================================================================*/ +#define DIGEST_CBLOCK_MD5 64 +#define DIGEST_LBLOCK_MD5 16 + +typedef struct digestMD5_context_st +{ + sal_uInt32 m_nDatLen; + sal_uInt32 m_pData[DIGEST_LBLOCK_MD5]; + sal_uInt32 m_nA, m_nB, m_nC, m_nD; + sal_uInt32 m_nL, m_nH; +} DigestContextMD5; + +typedef struct digestMD5_impl_st +{ + Digest_Impl m_digest; + DigestContextMD5 m_context; +} DigestMD5_Impl; + +static void __rtl_digest_initMD5 (DigestContextMD5 *ctx); +static void __rtl_digest_updateMD5 (DigestContextMD5 *ctx); +static void __rtl_digest_endMD5 (DigestContextMD5 *ctx); + +#define F(x,y,z) ((((y) ^ (z)) & (x)) ^ (z)) +#define G(x,y,z) ((((x) ^ (y)) & (z)) ^ (y)) +#define H(x,y,z) ((x) ^ (y) ^ (z)) +#define I(x,y,z) (((x) | (~(z))) ^ (y)) + +#define R0(a,b,c,d,k,s,t) { \ + a += ((k) + (t) + F((b), (c), (d))); \ + a = RTL_DIGEST_ROTL(a, s); \ + a += b; } + +#define R1(a,b,c,d,k,s,t) { \ + a += ((k) + (t) + G((b), (c), (d))); \ + a = RTL_DIGEST_ROTL(a, s); \ + a += b; } + +#define R2(a,b,c,d,k,s,t) { \ + a += ((k) + (t) + H((b), (c), (d))); \ + a = RTL_DIGEST_ROTL(a, s); \ + a += b; } + +#define R3(a,b,c,d,k,s,t) { \ + a += ((k) + (t) + I((b), (c), (d))); \ + a = RTL_DIGEST_ROTL(a, s); \ + a += b; } + +/* + * __rtl_digest_initMD5. + */ +static void __rtl_digest_initMD5 (DigestContextMD5 *ctx) +{ + rtl_zeroMemory (ctx, sizeof (DigestContextMD5)); + + ctx->m_nA = (sal_uInt32)0x67452301L; + ctx->m_nB = (sal_uInt32)0xefcdab89L; + ctx->m_nC = (sal_uInt32)0x98badcfeL; + ctx->m_nD = (sal_uInt32)0x10325476L; +} + +/* + * __rtl_digest_updateMD5. + */ +static void __rtl_digest_updateMD5 (DigestContextMD5 *ctx) +{ + register sal_uInt32 A, B, C, D; + register sal_uInt32 *X; + + A = ctx->m_nA; + B = ctx->m_nB; + C = ctx->m_nC; + D = ctx->m_nD; + X = ctx->m_pData; + + R0 (A, B, C, D, X[ 0], 7, 0xd76aa478L); + R0 (D, A, B, C, X[ 1], 12, 0xe8c7b756L); + R0 (C, D, A, B, X[ 2], 17, 0x242070dbL); + R0 (B, C, D, A, X[ 3], 22, 0xc1bdceeeL); + R0 (A, B, C, D, X[ 4], 7, 0xf57c0fafL); + R0 (D, A, B, C, X[ 5], 12, 0x4787c62aL); + R0 (C, D, A, B, X[ 6], 17, 0xa8304613L); + R0 (B, C, D, A, X[ 7], 22, 0xfd469501L); + R0 (A, B, C, D, X[ 8], 7, 0x698098d8L); + R0 (D, A, B, C, X[ 9], 12, 0x8b44f7afL); + R0 (C, D, A, B, X[10], 17, 0xffff5bb1L); + R0 (B, C, D, A, X[11], 22, 0x895cd7beL); + R0 (A, B, C, D, X[12], 7, 0x6b901122L); + R0 (D, A, B, C, X[13], 12, 0xfd987193L); + R0 (C, D, A, B, X[14], 17, 0xa679438eL); + R0 (B, C, D, A, X[15], 22, 0x49b40821L); + + R1 (A, B, C, D, X[ 1], 5, 0xf61e2562L); + R1 (D, A, B, C, X[ 6], 9, 0xc040b340L); + R1 (C, D, A, B, X[11], 14, 0x265e5a51L); + R1 (B, C, D, A, X[ 0], 20, 0xe9b6c7aaL); + R1 (A, B, C, D, X[ 5], 5, 0xd62f105dL); + R1 (D, A, B, C, X[10], 9, 0x02441453L); + R1 (C, D, A, B, X[15], 14, 0xd8a1e681L); + R1 (B, C, D, A, X[ 4], 20, 0xe7d3fbc8L); + R1 (A, B, C, D, X[ 9], 5, 0x21e1cde6L); + R1 (D, A, B, C, X[14], 9, 0xc33707d6L); + R1 (C, D, A, B, X[ 3], 14, 0xf4d50d87L); + R1 (B, C, D, A, X[ 8], 20, 0x455a14edL); + R1 (A, B, C, D, X[13], 5, 0xa9e3e905L); + R1 (D, A, B, C, X[ 2], 9, 0xfcefa3f8L); + R1 (C, D, A, B, X[ 7], 14, 0x676f02d9L); + R1 (B, C, D, A, X[12], 20, 0x8d2a4c8aL); + + R2 (A, B, C, D, X[ 5], 4, 0xfffa3942L); + R2 (D, A, B, C, X[ 8], 11, 0x8771f681L); + R2 (C, D, A, B, X[11], 16, 0x6d9d6122L); + R2 (B, C, D, A, X[14], 23, 0xfde5380cL); + R2 (A, B, C, D, X[ 1], 4, 0xa4beea44L); + R2 (D, A, B, C, X[ 4], 11, 0x4bdecfa9L); + R2 (C, D, A, B, X[ 7], 16, 0xf6bb4b60L); + R2 (B, C, D, A, X[10], 23, 0xbebfbc70L); + R2 (A, B, C, D, X[13], 4, 0x289b7ec6L); + R2 (D, A, B, C, X[ 0], 11, 0xeaa127faL); + R2 (C, D, A, B, X[ 3], 16, 0xd4ef3085L); + R2 (B, C, D, A, X[ 6], 23, 0x04881d05L); + R2 (A, B, C, D, X[ 9], 4, 0xd9d4d039L); + R2 (D, A, B, C, X[12], 11, 0xe6db99e5L); + R2 (C, D, A, B, X[15], 16, 0x1fa27cf8L); + R2 (B, C, D, A, X[ 2], 23, 0xc4ac5665L); + + R3 (A, B, C, D, X[ 0], 6, 0xf4292244L); + R3 (D, A, B, C, X[ 7], 10, 0x432aff97L); + R3 (C, D, A, B, X[14], 15, 0xab9423a7L); + R3 (B, C, D, A, X[ 5], 21, 0xfc93a039L); + R3 (A, B, C, D, X[12], 6, 0x655b59c3L); + R3 (D, A, B, C, X[ 3], 10, 0x8f0ccc92L); + R3 (C, D, A, B, X[10], 15, 0xffeff47dL); + R3 (B, C, D, A, X[ 1], 21, 0x85845dd1L); + R3 (A, B, C, D, X[ 8], 6, 0x6fa87e4fL); + R3 (D, A, B, C, X[15], 10, 0xfe2ce6e0L); + R3 (C, D, A, B, X[ 6], 15, 0xa3014314L); + R3 (B, C, D, A, X[13], 21, 0x4e0811a1L); + R3 (A, B, C, D, X[ 4], 6, 0xf7537e82L); + R3 (D, A, B, C, X[11], 10, 0xbd3af235L); + R3 (C, D, A, B, X[ 2], 15, 0x2ad7d2bbL); + R3 (B, C, D, A, X[ 9], 21, 0xeb86d391L); + + ctx->m_nA += A; + ctx->m_nB += B; + ctx->m_nC += C; + ctx->m_nD += D; +} + +/* + * __rtl_digest_endMD5. + */ +static void __rtl_digest_endMD5 (DigestContextMD5 *ctx) +{ + static const sal_uInt8 end[4] = + { + 0x80, 0x00, 0x00, 0x00 + }; + register const sal_uInt8 *p = end; + + register sal_uInt32 *X; + register int i; + + X = ctx->m_pData; + i = (ctx->m_nDatLen >> 2); + + switch (ctx->m_nDatLen & 0x03) + { + case 0: X[i] = ((sal_uInt32)(*(p++))) << 0L; + case 1: X[i] |= ((sal_uInt32)(*(p++))) << 8L; + case 2: X[i] |= ((sal_uInt32)(*(p++))) << 16L; + case 3: X[i] |= ((sal_uInt32)(*(p++))) << 24L; + } + i += 1; + + if (i >= (DIGEST_LBLOCK_MD5 - 2)) + { + for (; i < DIGEST_LBLOCK_MD5; i++) + X[i] = 0; + __rtl_digest_updateMD5 (ctx); + i = 0; + } + + for (; i < (DIGEST_LBLOCK_MD5 - 2); i++) + X[i] = 0; + + X[DIGEST_LBLOCK_MD5 - 2] = ctx->m_nL; + X[DIGEST_LBLOCK_MD5 - 1] = ctx->m_nH; + + __rtl_digest_updateMD5 (ctx); +} + +/*======================================================================== + * + * rtl_digest_MD5 implementation. + * + *======================================================================*/ +/* + * rtl_digest_MD5. + */ +rtlDigestError SAL_CALL rtl_digest_MD5 ( + const void *pData, sal_uInt32 nDatLen, + sal_uInt8 *pBuffer, sal_uInt32 nBufLen) +{ + rtlDigestError Result = rtl_Digest_E_Memory; + rtlDigest Digest = rtl_digest_createMD5(); + if (Digest) + { + Result = rtl_digest_updateMD5 (Digest, pData, nDatLen); + if (Result == rtl_Digest_E_None) + Result = rtl_digest_getMD5 (Digest, pBuffer, nBufLen); + rtl_digest_destroyMD5 (Digest); + } + return Result; +} + +/* + * rtl_digest_createMD5. + */ +rtlDigest SAL_CALL rtl_digest_createMD5 (void) +{ + DigestMD5_Impl *pImpl = (DigestMD5_Impl*)NULL; + pImpl = ((DigestMD5_Impl*)rtl_allocateZeroMemory (sizeof(DigestMD5_Impl))); + if (pImpl) + { + pImpl->m_digest.m_algorithm = rtl_Digest_AlgorithmMD5; + pImpl->m_digest.m_length = RTL_DIGEST_LENGTH_MD5; + + pImpl->m_digest.m_delete = rtl_digest_destroyMD5; + pImpl->m_digest.m_update = rtl_digest_updateMD5; + pImpl->m_digest.m_get = rtl_digest_getMD5; + + __rtl_digest_initMD5 (&(pImpl->m_context)); + } + return ((rtlDigest)pImpl); +} + +/* + * rtl_digest_updateMD5. + */ +rtlDigestError SAL_CALL rtl_digest_updateMD5 ( + rtlDigest Digest, const void *pData, sal_uInt32 nDatLen) +{ + DigestMD5_Impl *pImpl = (DigestMD5_Impl *)Digest; + const sal_uInt8 *d = (const sal_uInt8 *)pData; + + DigestContextMD5 *ctx; + sal_uInt32 len; + + if ((pImpl == NULL) || (pData == NULL)) + return rtl_Digest_E_Argument; + + if (!(pImpl->m_digest.m_algorithm == rtl_Digest_AlgorithmMD5)) + return rtl_Digest_E_Algorithm; + + if (nDatLen == 0) + return rtl_Digest_E_None; + + ctx = &(pImpl->m_context); + + len = ctx->m_nL + (nDatLen << 3); + if (len < ctx->m_nL) ctx->m_nH += 1; + ctx->m_nH += (nDatLen >> 29); + ctx->m_nL = len; + + if (ctx->m_nDatLen) + { + sal_uInt8 *p = (sal_uInt8 *)(ctx->m_pData) + ctx->m_nDatLen; + sal_uInt32 n = DIGEST_CBLOCK_MD5 - ctx->m_nDatLen; + + if (nDatLen < n) + { + rtl_copyMemory (p, d, nDatLen); + ctx->m_nDatLen += nDatLen; + + return rtl_Digest_E_None; + } + + rtl_copyMemory (p, d, n); + d += n; + nDatLen -= n; + + __rtl_digest_updateMD5 (ctx); + ctx->m_nDatLen = 0; + } + + while (nDatLen >= DIGEST_CBLOCK_MD5) + { + rtl_copyMemory (ctx->m_pData, d, DIGEST_CBLOCK_MD5); + d += DIGEST_CBLOCK_MD5; + nDatLen -= DIGEST_CBLOCK_MD5; + + __rtl_digest_updateMD5 (ctx); + } + + rtl_copyMemory (ctx->m_pData, d, nDatLen); + ctx->m_nDatLen = nDatLen; + + return rtl_Digest_E_None; +} + +/* + * rtl_digest_getMD5. + */ +rtlDigestError SAL_CALL rtl_digest_getMD5 ( + rtlDigest Digest, sal_uInt8 *pBuffer, sal_uInt32 nBufLen) +{ + DigestMD5_Impl *pImpl = (DigestMD5_Impl *)Digest; + sal_uInt8 *p = pBuffer; + + DigestContextMD5 *ctx; + + if ((pImpl == NULL) || (pBuffer == NULL)) + return rtl_Digest_E_Argument; + + if (!(pImpl->m_digest.m_algorithm == rtl_Digest_AlgorithmMD5)) + return rtl_Digest_E_Algorithm; + + if (!(pImpl->m_digest.m_length <= nBufLen)) + return rtl_Digest_E_BufferSize; + + ctx = &(pImpl->m_context); + + __rtl_digest_endMD5 (ctx); + RTL_DIGEST_LTOC (ctx->m_nA, p); + RTL_DIGEST_LTOC (ctx->m_nB, p); + RTL_DIGEST_LTOC (ctx->m_nC, p); + RTL_DIGEST_LTOC (ctx->m_nD, p); + __rtl_digest_initMD5 (ctx); + + return rtl_Digest_E_None; +} + +/* + * rtl_digest_destroyMD5. + */ +void SAL_CALL rtl_digest_destroyMD5 (rtlDigest Digest) +{ + DigestMD5_Impl *pImpl = (DigestMD5_Impl *)Digest; + if (pImpl) + { + if (pImpl->m_digest.m_algorithm == rtl_Digest_AlgorithmMD5) + rtl_freeZeroMemory (pImpl, sizeof (DigestMD5_Impl)); + else + rtl_freeMemory (pImpl); + } +} + +/*======================================================================== + * + * rtl_digest_(SHA|SHA1) common internals. + * + *======================================================================*/ +#define DIGEST_CBLOCK_SHA 64 +#define DIGEST_LBLOCK_SHA 16 + +typedef sal_uInt32 DigestSHA_update_t (sal_uInt32 x); + +static sal_uInt32 __rtl_digest_updateSHA_0 (sal_uInt32 x); +static sal_uInt32 __rtl_digest_updateSHA_1 (sal_uInt32 x); + +typedef struct digestSHA_context_st +{ + DigestSHA_update_t *m_update; + sal_uInt32 m_nDatLen; + sal_uInt32 m_pData[DIGEST_LBLOCK_SHA]; + sal_uInt32 m_nA, m_nB, m_nC, m_nD, m_nE; + sal_uInt32 m_nL, m_nH; +} DigestContextSHA; + +typedef struct digestSHA_impl_st +{ + Digest_Impl m_digest; + DigestContextSHA m_context; +} DigestSHA_Impl; + +static void __rtl_digest_initSHA ( + DigestContextSHA *ctx, DigestSHA_update_t *fct); + +static void __rtl_digest_updateSHA (DigestContextSHA *ctx); +static void __rtl_digest_endSHA (DigestContextSHA *ctx); + +static void __rtl_digest_swapLong (sal_uInt32 *pData, sal_uInt32 nDatLen); + +#define K_00_19 0x5a827999L +#define K_20_39 0x6ed9eba1L +#define K_40_59 0x8f1bbcdcL +#define K_60_79 0xca62c1d6L + +#define F_00_19(b,c,d) ((((c) ^ (d)) & (b)) ^ (d)) +#define F_20_39(b,c,d) ((b) ^ (c) ^ (d)) +#define F_40_59(b,c,d) (((b) & (c)) | ((b) & (d)) | ((c) & (d))) +#define F_60_79(b,c,d) F_20_39(b,c,d) + +#define BODY_X(i) \ + (X[(i)&0x0f] ^ X[((i)+2)&0x0f] ^ X[((i)+8)&0x0f] ^ X[((i)+13)&0x0f]) + +#define BODY_00_15(u,i,a,b,c,d,e,f) \ + (f) = X[i]; \ + (f) += (e) + K_00_19 + RTL_DIGEST_ROTL((a), 5) + F_00_19((b), (c), (d)); \ + (b) = RTL_DIGEST_ROTL((b), 30); + +#define BODY_16_19(u,i,a,b,c,d,e,f) \ + (f) = BODY_X((i)); \ + (f) = X[(i)&0x0f] = (u)((f)); \ + (f) += (e) + K_00_19 + RTL_DIGEST_ROTL((a), 5) + F_00_19((b), (c), (d)); \ + (b) = RTL_DIGEST_ROTL((b), 30); + +#define BODY_20_39(u,i,a,b,c,d,e,f) \ + (f) = BODY_X((i)); \ + (f) = X[(i)&0x0f] = (u)((f)); \ + (f) += (e) + K_20_39 + RTL_DIGEST_ROTL((a), 5) + F_20_39((b), (c), (d)); \ + (b) = RTL_DIGEST_ROTL((b), 30); + +#define BODY_40_59(u,i,a,b,c,d,e,f) \ + (f) = BODY_X((i)); \ + (f) = X[(i)&0x0f] = (u)((f)); \ + (f) += (e) + K_40_59 + RTL_DIGEST_ROTL((a), 5) + F_40_59((b), (c), (d)); \ + (b) = RTL_DIGEST_ROTL((b), 30); + +#define BODY_60_79(u,i,a,b,c,d,e,f) \ + (f) = BODY_X((i)); \ + (f) = X[(i)&0x0f] = (u)((f)); \ + (f) += (e) + K_60_79 + RTL_DIGEST_ROTL((a), 5) + F_60_79((b), (c), (d)); \ + (b) = RTL_DIGEST_ROTL((b), 30); + +/* + * __rtl_digest_initSHA. + */ +static void __rtl_digest_initSHA ( + DigestContextSHA *ctx, DigestSHA_update_t *fct) +{ + rtl_zeroMemory (ctx, sizeof (DigestContextSHA)); + ctx->m_update = fct; + + ctx->m_nA = (sal_uInt32)0x67452301L; + ctx->m_nB = (sal_uInt32)0xefcdab89L; + ctx->m_nC = (sal_uInt32)0x98badcfeL; + ctx->m_nD = (sal_uInt32)0x10325476L; + ctx->m_nE = (sal_uInt32)0xc3d2e1f0L; +} + +/* + * __rtl_digest_updateSHA. + */ +static void __rtl_digest_updateSHA (DigestContextSHA *ctx) +{ + register sal_uInt32 A, B, C, D, E, T; + register sal_uInt32 *X; + + register DigestSHA_update_t *U; + U = ctx->m_update; + + A = ctx->m_nA; + B = ctx->m_nB; + C = ctx->m_nC; + D = ctx->m_nD; + E = ctx->m_nE; + X = ctx->m_pData; + + BODY_00_15 (U, 0, A, B, C, D, E, T); + BODY_00_15 (U, 1, T, A, B, C, D, E); + BODY_00_15 (U, 2, E, T, A, B, C, D); + BODY_00_15 (U, 3, D, E, T, A, B, C); + BODY_00_15 (U, 4, C, D, E, T, A, B); + BODY_00_15 (U, 5, B, C, D, E, T, A); + BODY_00_15 (U, 6, A, B, C, D, E, T); + BODY_00_15 (U, 7, T, A, B, C, D, E); + BODY_00_15 (U, 8, E, T, A, B, C, D); + BODY_00_15 (U, 9, D, E, T, A, B, C); + BODY_00_15 (U, 10, C, D, E, T, A, B); + BODY_00_15 (U, 11, B, C, D, E, T, A); + BODY_00_15 (U, 12, A, B, C, D, E, T); + BODY_00_15 (U, 13, T, A, B, C, D, E); + BODY_00_15 (U, 14, E, T, A, B, C, D); + BODY_00_15 (U, 15, D, E, T, A, B, C); + BODY_16_19 (U, 16, C, D, E, T, A, B); + BODY_16_19 (U, 17, B, C, D, E, T, A); + BODY_16_19 (U, 18, A, B, C, D, E, T); + BODY_16_19 (U, 19, T, A, B, C, D, E); + + BODY_20_39 (U, 20, E, T, A, B, C, D); + BODY_20_39 (U, 21, D, E, T, A, B, C); + BODY_20_39 (U, 22, C, D, E, T, A, B); + BODY_20_39 (U, 23, B, C, D, E, T, A); + BODY_20_39 (U, 24, A, B, C, D, E, T); + BODY_20_39 (U, 25, T, A, B, C, D, E); + BODY_20_39 (U, 26, E, T, A, B, C, D); + BODY_20_39 (U, 27, D, E, T, A, B, C); + BODY_20_39 (U, 28, C, D, E, T, A, B); + BODY_20_39 (U, 29, B, C, D, E, T, A); + BODY_20_39 (U, 30, A, B, C, D, E, T); + BODY_20_39 (U, 31, T, A, B, C, D, E); + BODY_20_39 (U, 32, E, T, A, B, C, D); + BODY_20_39 (U, 33, D, E, T, A, B, C); + BODY_20_39 (U, 34, C, D, E, T, A, B); + BODY_20_39 (U, 35, B, C, D, E, T, A); + BODY_20_39 (U, 36, A, B, C, D, E, T); + BODY_20_39 (U, 37, T, A, B, C, D, E); + BODY_20_39 (U, 38, E, T, A, B, C, D); + BODY_20_39 (U, 39, D, E, T, A, B, C); + + BODY_40_59 (U, 40, C, D, E, T, A, B); + BODY_40_59 (U, 41, B, C, D, E, T, A); + BODY_40_59 (U, 42, A, B, C, D, E, T); + BODY_40_59 (U, 43, T, A, B, C, D, E); + BODY_40_59 (U, 44, E, T, A, B, C, D); + BODY_40_59 (U, 45, D, E, T, A, B, C); + BODY_40_59 (U, 46, C, D, E, T, A, B); + BODY_40_59 (U, 47, B, C, D, E, T, A); + BODY_40_59 (U, 48, A, B, C, D, E, T); + BODY_40_59 (U, 49, T, A, B, C, D, E); + BODY_40_59 (U, 50, E, T, A, B, C, D); + BODY_40_59 (U, 51, D, E, T, A, B, C); + BODY_40_59 (U, 52, C, D, E, T, A, B); + BODY_40_59 (U, 53, B, C, D, E, T, A); + BODY_40_59 (U, 54, A, B, C, D, E, T); + BODY_40_59 (U, 55, T, A, B, C, D, E); + BODY_40_59 (U, 56, E, T, A, B, C, D); + BODY_40_59 (U, 57, D, E, T, A, B, C); + BODY_40_59 (U, 58, C, D, E, T, A, B); + BODY_40_59 (U, 59, B, C, D, E, T, A); + + BODY_60_79 (U, 60, A, B, C, D, E, T); + BODY_60_79 (U, 61, T, A, B, C, D, E); + BODY_60_79 (U, 62, E, T, A, B, C, D); + BODY_60_79 (U, 63, D, E, T, A, B, C); + BODY_60_79 (U, 64, C, D, E, T, A, B); + BODY_60_79 (U, 65, B, C, D, E, T, A); + BODY_60_79 (U, 66, A, B, C, D, E, T); + BODY_60_79 (U, 67, T, A, B, C, D, E); + BODY_60_79 (U, 68, E, T, A, B, C, D); + BODY_60_79 (U, 69, D, E, T, A, B, C); + BODY_60_79 (U, 70, C, D, E, T, A, B); + BODY_60_79 (U, 71, B, C, D, E, T, A); + BODY_60_79 (U, 72, A, B, C, D, E, T); + BODY_60_79 (U, 73, T, A, B, C, D, E); + BODY_60_79 (U, 74, E, T, A, B, C, D); + BODY_60_79 (U, 75, D, E, T, A, B, C); + BODY_60_79 (U, 76, C, D, E, T, A, B); + BODY_60_79 (U, 77, B, C, D, E, T, A); + BODY_60_79 (U, 78, A, B, C, D, E, T); + BODY_60_79 (U, 79, T, A, B, C, D, E); + + ctx->m_nA += E; + ctx->m_nB += T; + ctx->m_nC += A; + ctx->m_nD += B; + ctx->m_nE += C; +} + +/* + * __rtl_digest_endSHA. + */ +static void __rtl_digest_endSHA (DigestContextSHA *ctx) +{ + static const sal_uInt8 end[4] = + { + 0x80, 0x00, 0x00, 0x00 + }; + register const sal_uInt8 *p = end; + + register sal_uInt32 *X; + register int i; + + X = ctx->m_pData; + i = (ctx->m_nDatLen >> 2); + + switch (ctx->m_nDatLen & 0x03) + { + case 0: X[i] = ((sal_uInt32)(*(p++))) << 0L; + case 1: X[i] |= ((sal_uInt32)(*(p++))) << 8L; + case 2: X[i] |= ((sal_uInt32)(*(p++))) << 16L; + case 3: X[i] |= ((sal_uInt32)(*(p++))) << 24L; + } + i += 1; + + __rtl_digest_swapLong (X, i); + + if (i >= (DIGEST_LBLOCK_SHA - 2)) + { + for (; i < DIGEST_LBLOCK_SHA; i++) + X[i] = 0; + __rtl_digest_updateSHA (ctx); + i = 0; + } + + for (; i < (DIGEST_LBLOCK_SHA - 2); i++) + X[i] = 0; + + X[DIGEST_LBLOCK_SHA - 2] = ctx->m_nH; + X[DIGEST_LBLOCK_SHA - 1] = ctx->m_nL; + + __rtl_digest_updateSHA (ctx); +} + +/* + * __rtl_digest_swapLong. + */ +static void __rtl_digest_swapLong (sal_uInt32 *pData, sal_uInt32 nDatLen) +{ + register sal_uInt32 *X; + register int i, n; + + X = pData; + n = nDatLen; + + for (i = 0; i < n; i++) + X[i] = RTL_DIGEST_SWAPLONG(X[i]); +} + +/*======================================================================== + * + * rtl_digest_SHA internals. + * + *======================================================================*/ +/* + * __rtl_digest_updateSHA_0. + */ +static sal_uInt32 __rtl_digest_updateSHA_0 (sal_uInt32 x) +{ + return x; +} + +/*======================================================================== + * + * rtl_digest_SHA implementation. + * + *======================================================================*/ +/* + * rtl_digest_SHA. + */ +rtlDigestError SAL_CALL rtl_digest_SHA ( + const void *pData, sal_uInt32 nDatLen, + sal_uInt8 *pBuffer, sal_uInt32 nBufLen) +{ + rtlDigestError Result = rtl_Digest_E_Memory; + rtlDigest Digest = rtl_digest_createSHA(); + if (Digest) + { + Result = rtl_digest_updateSHA (Digest, pData, nDatLen); + if (Result == rtl_Digest_E_None) + Result = rtl_digest_getSHA (Digest, pBuffer, nBufLen); + rtl_digest_destroySHA (Digest); + } + return Result; +} + +/* + * rtl_digest_createSHA. + */ +rtlDigest SAL_CALL rtl_digest_createSHA (void) +{ + DigestSHA_Impl *pImpl = (DigestSHA_Impl*)NULL; + pImpl = ((DigestSHA_Impl*)rtl_allocateZeroMemory (sizeof(DigestSHA_Impl))); + if (pImpl) + { + pImpl->m_digest.m_algorithm = rtl_Digest_AlgorithmSHA; + pImpl->m_digest.m_length = RTL_DIGEST_LENGTH_SHA; + + pImpl->m_digest.m_delete = rtl_digest_destroySHA; + pImpl->m_digest.m_update = rtl_digest_updateSHA; + pImpl->m_digest.m_get = rtl_digest_getSHA; + + __rtl_digest_initSHA (&(pImpl->m_context), __rtl_digest_updateSHA_0); + } + return ((rtlDigest)pImpl); +} + +/* + * rtl_digest_updateSHA. + */ +rtlDigestError SAL_CALL rtl_digest_updateSHA ( + rtlDigest Digest, const void *pData, sal_uInt32 nDatLen) +{ + DigestSHA_Impl *pImpl = (DigestSHA_Impl *)Digest; + const sal_uInt8 *d = (const sal_uInt8 *)pData; + + DigestContextSHA *ctx; + sal_uInt32 len; + + if ((pImpl == NULL) || (pData == NULL)) + return rtl_Digest_E_Argument; + + if (!(pImpl->m_digest.m_algorithm == rtl_Digest_AlgorithmSHA)) + return rtl_Digest_E_Algorithm; + + if (nDatLen == 0) + return rtl_Digest_E_None; + + ctx = &(pImpl->m_context); + + len = ctx->m_nL + (nDatLen << 3); + if (len < ctx->m_nL) ctx->m_nH += 1; + ctx->m_nH += (nDatLen >> 29); + ctx->m_nL = len; + + if (ctx->m_nDatLen) + { + sal_uInt8 *p = (sal_uInt8 *)(ctx->m_pData) + ctx->m_nDatLen; + sal_uInt32 n = DIGEST_CBLOCK_SHA - ctx->m_nDatLen; + + if (nDatLen < n) + { + rtl_copyMemory (p, d, nDatLen); + ctx->m_nDatLen += nDatLen; + + return rtl_Digest_E_None; + } + + rtl_copyMemory (p, d, n); + d += n; + nDatLen -= n; + + __rtl_digest_swapLong (ctx->m_pData, DIGEST_LBLOCK_SHA); + __rtl_digest_updateSHA (ctx); + ctx->m_nDatLen = 0; + } + + while (nDatLen >= DIGEST_CBLOCK_SHA) + { + rtl_copyMemory (ctx->m_pData, d, DIGEST_CBLOCK_SHA); + d += DIGEST_CBLOCK_SHA; + nDatLen -= DIGEST_CBLOCK_SHA; + + __rtl_digest_swapLong (ctx->m_pData, DIGEST_LBLOCK_SHA); + __rtl_digest_updateSHA (ctx); + } + + rtl_copyMemory (ctx->m_pData, d, nDatLen); + ctx->m_nDatLen = nDatLen; + + return rtl_Digest_E_None; +} + +/* + * rtl_digest_getSHA. + */ +rtlDigestError SAL_CALL rtl_digest_getSHA ( + rtlDigest Digest, sal_uInt8 *pBuffer, sal_uInt32 nBufLen) +{ + DigestSHA_Impl *pImpl = (DigestSHA_Impl *)Digest; + sal_uInt8 *p = pBuffer; + + DigestContextSHA *ctx; + + if ((pImpl == NULL) || (pBuffer == NULL)) + return rtl_Digest_E_Argument; + + if (!(pImpl->m_digest.m_algorithm == rtl_Digest_AlgorithmSHA)) + return rtl_Digest_E_Algorithm; + + if (!(pImpl->m_digest.m_length <= nBufLen)) + return rtl_Digest_E_BufferSize; + + ctx = &(pImpl->m_context); + + __rtl_digest_endSHA (ctx); + RTL_DIGEST_HTONL (ctx->m_nA, p); + RTL_DIGEST_HTONL (ctx->m_nB, p); + RTL_DIGEST_HTONL (ctx->m_nC, p); + RTL_DIGEST_HTONL (ctx->m_nD, p); + RTL_DIGEST_HTONL (ctx->m_nE, p); + __rtl_digest_initSHA (ctx, __rtl_digest_updateSHA_0); + + return rtl_Digest_E_None; +} + +/* + * rtl_digest_destroySHA. + */ +void SAL_CALL rtl_digest_destroySHA (rtlDigest Digest) +{ + DigestSHA_Impl *pImpl = (DigestSHA_Impl *)Digest; + if (pImpl) + { + if (pImpl->m_digest.m_algorithm == rtl_Digest_AlgorithmSHA) + rtl_freeZeroMemory (pImpl, sizeof (DigestSHA_Impl)); + else + rtl_freeMemory (pImpl); + } +} + +/*======================================================================== + * + * rtl_digest_SHA1 internals. + * + *======================================================================*/ +/* + * __rtl_digest_updateSHA_1. + */ +static sal_uInt32 __rtl_digest_updateSHA_1 (sal_uInt32 x) +{ + return RTL_DIGEST_ROTL (x, 1); +} + +/*======================================================================== + * + * rtl_digest_SHA1 implementation. + * + *======================================================================*/ +/* + * rtl_digest_SHA1. + */ +rtlDigestError SAL_CALL rtl_digest_SHA1 ( + const void *pData, sal_uInt32 nDatLen, + sal_uInt8 *pBuffer, sal_uInt32 nBufLen) +{ + rtlDigestError Result = rtl_Digest_E_Memory; + rtlDigest Digest = rtl_digest_createSHA1(); + if (Digest) + { + Result = rtl_digest_updateSHA1 (Digest, pData, nDatLen); + if (Result == rtl_Digest_E_None) + Result = rtl_digest_getSHA1 (Digest, pBuffer, nBufLen); + rtl_digest_destroySHA1 (Digest); + } + return Result; +} + +/* + * rtl_digest_createSHA1. + */ +rtlDigest SAL_CALL rtl_digest_createSHA1 (void) +{ + DigestSHA_Impl *pImpl = (DigestSHA_Impl*)NULL; + pImpl = ((DigestSHA_Impl*)rtl_allocateZeroMemory (sizeof(DigestSHA_Impl))); + if (pImpl) + { + pImpl->m_digest.m_algorithm = rtl_Digest_AlgorithmSHA1; + pImpl->m_digest.m_length = RTL_DIGEST_LENGTH_SHA1; + + pImpl->m_digest.m_delete = rtl_digest_destroySHA1; + pImpl->m_digest.m_update = rtl_digest_updateSHA1; + pImpl->m_digest.m_get = rtl_digest_getSHA1; + + __rtl_digest_initSHA (&(pImpl->m_context), __rtl_digest_updateSHA_1); + } + return ((rtlDigest)pImpl); +} + +/* + * rtl_digest_updateSHA1. + */ +rtlDigestError SAL_CALL rtl_digest_updateSHA1 ( + rtlDigest Digest, const void *pData, sal_uInt32 nDatLen) +{ + DigestSHA_Impl *pImpl = (DigestSHA_Impl *)Digest; + const sal_uInt8 *d = (const sal_uInt8 *)pData; + + DigestContextSHA *ctx; + sal_uInt32 len; + + if ((pImpl == NULL) || (pData == NULL)) + return rtl_Digest_E_Argument; + + if (!(pImpl->m_digest.m_algorithm == rtl_Digest_AlgorithmSHA1)) + return rtl_Digest_E_Algorithm; + + if (nDatLen == 0) + return rtl_Digest_E_None; + + ctx = &(pImpl->m_context); + + len = ctx->m_nL + (nDatLen << 3); + if (len < ctx->m_nL) ctx->m_nH += 1; + ctx->m_nH += (nDatLen >> 29); + ctx->m_nL = len; + + if (ctx->m_nDatLen) + { + sal_uInt8 *p = (sal_uInt8 *)(ctx->m_pData) + ctx->m_nDatLen; + sal_uInt32 n = DIGEST_CBLOCK_SHA - ctx->m_nDatLen; + + if (nDatLen < n) + { + rtl_copyMemory (p, d, nDatLen); + ctx->m_nDatLen += nDatLen; + + return rtl_Digest_E_None; + } + + rtl_copyMemory (p, d, n); + d += n; + nDatLen -= n; + + __rtl_digest_swapLong (ctx->m_pData, DIGEST_LBLOCK_SHA); + __rtl_digest_updateSHA (ctx); + ctx->m_nDatLen = 0; + } + + while (nDatLen >= DIGEST_CBLOCK_SHA) + { + rtl_copyMemory (ctx->m_pData, d, DIGEST_CBLOCK_SHA); + d += DIGEST_CBLOCK_SHA; + nDatLen -= DIGEST_CBLOCK_SHA; + + __rtl_digest_swapLong (ctx->m_pData, DIGEST_LBLOCK_SHA); + __rtl_digest_updateSHA (ctx); + } + + rtl_copyMemory (ctx->m_pData, d, nDatLen); + ctx->m_nDatLen = nDatLen; + + return rtl_Digest_E_None; +} + +/* + * rtl_digest_getSHA1. + */ +rtlDigestError SAL_CALL rtl_digest_getSHA1 ( + rtlDigest Digest, sal_uInt8 *pBuffer, sal_uInt32 nBufLen) +{ + DigestSHA_Impl *pImpl = (DigestSHA_Impl *)Digest; + sal_uInt8 *p = pBuffer; + + DigestContextSHA *ctx; + + if ((pImpl == NULL) || (pBuffer == NULL)) + return rtl_Digest_E_Argument; + + if (!(pImpl->m_digest.m_algorithm == rtl_Digest_AlgorithmSHA1)) + return rtl_Digest_E_Algorithm; + + if (!(pImpl->m_digest.m_length <= nBufLen)) + return rtl_Digest_E_BufferSize; + + ctx = &(pImpl->m_context); + + __rtl_digest_endSHA (ctx); + RTL_DIGEST_HTONL (ctx->m_nA, p); + RTL_DIGEST_HTONL (ctx->m_nB, p); + RTL_DIGEST_HTONL (ctx->m_nC, p); + RTL_DIGEST_HTONL (ctx->m_nD, p); + RTL_DIGEST_HTONL (ctx->m_nE, p); + __rtl_digest_initSHA (ctx, __rtl_digest_updateSHA_1); + + return rtl_Digest_E_None; +} + +/* + * rtl_digest_destroySHA1. + */ +void SAL_CALL rtl_digest_destroySHA1 (rtlDigest Digest) +{ + DigestSHA_Impl *pImpl = (DigestSHA_Impl *)Digest; + if (pImpl) + { + if (pImpl->m_digest.m_algorithm == rtl_Digest_AlgorithmSHA1) + rtl_freeZeroMemory (pImpl, sizeof (DigestSHA_Impl)); + else + rtl_freeMemory (pImpl); + } +} + diff --git a/sal/rtl/source/locale.c b/sal/rtl/source/locale.c new file mode 100644 index 000000000000..d99e298f79c2 --- /dev/null +++ b/sal/rtl/source/locale.c @@ -0,0 +1,367 @@ +/************************************************************************* + * + * $RCSfile: locale.c,v $ + * + * $Revision: 1.1.1.1 $ + * + * last change: $Author: hr $ $Date: 2000-09-18 15:17:24 $ + * + * The Contents of this file are made available subject to the terms of + * either of the following licenses + * + * - GNU Lesser General Public License Version 2.1 + * - Sun Industry Standards Source License Version 1.1 + * + * Sun Microsystems Inc., October, 2000 + * + * GNU Lesser General Public License Version 2.1 + * ============================================= + * Copyright 2000 by Sun Microsystems, Inc. + * 901 San Antonio Road, Palo Alto, CA 94303, USA + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, + * MA 02111-1307 USA + * + * + * Sun Industry Standards Source License Version 1.1 + * ================================================= + * The contents of this file are subject to the Sun Industry Standards + * Source License Version 1.1 (the "License"); You may not use this file + * except in compliance with the License. You may obtain a copy of the + * License at http://www.openoffice.org/license.html. + * + * Software provided under this License is provided on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, + * WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS, + * MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING. + * See the License for the specific provisions governing your rights and + * obligations concerning the Software. + * + * The Initial Developer of the Original Code is: Sun Microsystems, Inc. + * + * Copyright: 2000 by Sun Microsystems, Inc. + * + * All Rights Reserved. + * + * Contributor(s): _______________________________________ + * + * + ************************************************************************/ + +#ifndef _OSL_INTERLOCK_H_ +#include <osl/interlck.h> +#endif + +#ifndef _RTL_LOCALE_H_ +#include <rtl/locale.h> +#endif + +#ifndef _RTL_MEMORY_H_ +#include <rtl/memory.h> +#endif + +#ifndef _RTL_ALLOC_H_ +#include <rtl/alloc.h> +#endif + +static sal_Int32 RTL_HASHTABLE_SIZE[] = +{ + 7, 31, 127, 251, 509, 1021, 2039, 4093 +}; + +typedef struct rtl_hashentry RTL_HASHENTRY; + +struct rtl_hashentry +{ + rtl_Locale* Entry; + RTL_HASHENTRY* Next; +}; + +typedef struct rtl_hashtable +{ + sal_Int8 iSize; + sal_Int32 Size; + sal_Int32 Elements; + RTL_HASHENTRY** Table; +} RTL_HASHTABLE; + +static RTL_HASHTABLE* pLocaleTable = NULL; + +static rtl_Locale* pDefaultLocale = NULL; + + +void rtl_hashentry_destroy(RTL_HASHENTRY* entry) +{ + rtl_uString_release(entry->Entry->Language); + rtl_uString_release(entry->Entry->Country); + rtl_uString_release(entry->Entry->Variant); + if (entry->Next) + rtl_hashentry_destroy(entry->Next); + + rtl_freeMemory(entry->Entry); +} + +void rtl_hashtable_destroy(RTL_HASHTABLE* table) +{ + sal_Int32 size = 0; + + if (!table) + return; + + size = table->Size; + + while (size) + { + if (table->Table[size - 1]) + rtl_hashentry_destroy(table->Table[size - 1]); + size--; + } + + rtl_freeMemory(table->Table); + rtl_freeMemory(table); +} + +void rtl_hashtable_init(RTL_HASHTABLE** table, sal_Int8 sizeIndex) +{ + sal_Int32 nSize = RTL_HASHTABLE_SIZE[sizeIndex]; + + if (*table) + rtl_hashtable_destroy(*table); + + *table = (RTL_HASHTABLE*)rtl_allocateMemory( sizeof(RTL_HASHTABLE) ); + + (*table)->iSize = sizeIndex; + (*table)->Size = nSize; + (*table)->Elements = 0; + (*table)->Table = (RTL_HASHENTRY**)rtl_allocateMemory( (*table)->Size * sizeof(RTL_HASHENTRY*) ); + + while (nSize) + { + (*table)->Table[nSize - 1] = NULL; + nSize--; + } +} + +sal_Int32 rtl_hashfunc(RTL_HASHTABLE* table, sal_Int32 key) +{ + return (key % table->Size); +} + +sal_Bool rtl_hashtable_grow(RTL_HASHTABLE** table); + +rtl_Locale* rtl_hashtable_add(RTL_HASHTABLE** table, rtl_Locale* value) +{ + sal_Int32 key = 0; + + if (!(*table)) + return NULL; + + if ((*table)->Elements > ((*table)->Size / 2)) + rtl_hashtable_grow(table); + + key = rtl_hashfunc(*table, value->HashCode); + + if (!(*table)->Table[key]) + { + RTL_HASHENTRY *newEntry = (RTL_HASHENTRY*)rtl_allocateMemory( sizeof(RTL_HASHENTRY) ); + newEntry->Entry = value; + newEntry->Next = NULL; + (*table)->Table[key] = newEntry; + (*table)->Elements++; + return NULL; + } else + { + RTL_HASHENTRY *pEntry = (*table)->Table[key]; + RTL_HASHENTRY *newEntry = NULL; + + while (pEntry) + { + if (value->HashCode == pEntry->Entry->HashCode) + return pEntry->Entry; + + if (!pEntry->Next) + break; + + pEntry = pEntry->Next; + } + + newEntry = (RTL_HASHENTRY*)rtl_allocateMemory( sizeof(RTL_HASHENTRY) ); + newEntry->Entry = value; + newEntry->Next = NULL; + pEntry->Next = newEntry; + (*table)->Elements++; + return NULL; + } +} + +sal_Bool rtl_hashtable_grow(RTL_HASHTABLE** table) +{ + RTL_HASHTABLE* pNewTable = NULL; + sal_Int32 i = 0; + + rtl_hashtable_init(&pNewTable, (sal_Int8)((*table)->iSize + 1)); + + while (i < (*table)->Size) + { + if ((*table)->Table[i]) + { + RTL_HASHENTRY *pNext; + RTL_HASHENTRY *pEntry = (*table)->Table[i]; + + rtl_hashtable_add(&pNewTable, pEntry->Entry); + + while (pEntry->Next) + { + rtl_hashtable_add(&pNewTable, pEntry->Next->Entry); + pNext = pEntry->Next; + rtl_freeMemory(pEntry); + pEntry = pNext; + } + + rtl_freeMemory(pEntry); + } + i++; + } + + rtl_freeMemory((*table)->Table); + rtl_freeMemory((*table)); + (*table) = pNewTable; + + return sal_True; +} + +sal_Bool rtl_hashtable_find(sal_Int32 key, sal_Int32 hashCode, rtl_Locale** pValue) +{ + if (!pLocaleTable) + return sal_False; + + if (pLocaleTable->Table[key]) + { + RTL_HASHENTRY *pEntry = pLocaleTable->Table[key]; + + while (pEntry && hashCode != pEntry->Entry->HashCode) + pEntry = pEntry->Next; + + if (pEntry) + *pValue = pEntry->Entry; + else + return sal_False; + } else + return sal_False; + + return sal_True; +} + +/************************************************************************* + * rtl_locale_register + */ +rtl_Locale * SAL_CALL rtl_locale_register( const sal_Unicode * language, const sal_Unicode * country, const sal_Unicode * variant ) +{ + sal_Unicode c = 0; + rtl_uString* sLanguage = NULL; + rtl_uString* sCountry = NULL; + rtl_uString* sVariant = NULL; + rtl_Locale *newLocale = NULL; + sal_Int32 hashCode = -1; + sal_Int32 key = 0; + + if ( !country ) + country = &c; + if ( !variant ) + variant = &c; + + if (!pLocaleTable) + rtl_hashtable_init(&pLocaleTable, 1); + + hashCode = rtl_ustr_hashCode(language) ^ rtl_ustr_hashCode(country) ^ rtl_ustr_hashCode(variant); + key = rtl_hashfunc(pLocaleTable, hashCode); + + if (rtl_hashtable_find(key, hashCode, &newLocale)) + return newLocale; + + rtl_uString_newFromStr(&sLanguage, language); + rtl_uString_newFromStr(&sCountry, country); + rtl_uString_newFromStr(&sVariant, variant); + + newLocale = (rtl_Locale*)rtl_allocateMemory( sizeof(rtl_Locale) ); + + newLocale->Language = sLanguage; + newLocale->Country = sCountry; + newLocale->Variant = sVariant; + newLocale->HashCode = hashCode; + + rtl_hashtable_add(&pLocaleTable, newLocale); + + return newLocale; +} + +/************************************************************************* + * rtl_locale_getDefault + */ +rtl_Locale * SAL_CALL rtl_locale_getDefault() +{ + return pDefaultLocale; +} + +/************************************************************************* + * rtl_locale_setDefault + */ +void SAL_CALL rtl_locale_setDefault( const sal_Unicode * language, const sal_Unicode * country, const sal_Unicode * variant ) +{ + pDefaultLocale = rtl_locale_register(language, country, variant); +} + +/************************************************************************* + * rtl_locale_getLanguage + */ +rtl_uString * SAL_CALL rtl_locale_getLanguage( rtl_Locale * This ) +{ + rtl_uString_acquire(This->Language); + return This->Language; +} + +/************************************************************************* + * rtl_locale_getCountry + */ +rtl_uString * SAL_CALL rtl_locale_getCountry( rtl_Locale * This ) +{ + rtl_uString_acquire(This->Country); + return This->Country; +} + +/************************************************************************* + * rtl_locale_getVariant + */ +rtl_uString * SAL_CALL rtl_locale_getVariant( rtl_Locale * This ) +{ + rtl_uString_acquire(This->Variant); + return This->Variant; +} + +/************************************************************************* + * rtl_locale_hashCode + */ +sal_Int32 SAL_CALL rtl_locale_hashCode( rtl_Locale * This ) +{ + return This->HashCode; +} + +/************************************************************************* + * rtl_locale_equals + */ +sal_Int32 SAL_CALL rtl_locale_equals( rtl_Locale * This, rtl_Locale * obj ) +{ + return This == obj; +} + diff --git a/sal/rtl/source/makefile.mk b/sal/rtl/source/makefile.mk new file mode 100644 index 000000000000..e3e3fb1a23d2 --- /dev/null +++ b/sal/rtl/source/makefile.mk @@ -0,0 +1,131 @@ +#************************************************************************* +# +# $RCSfile: makefile.mk,v $ +# +# $Revision: 1.1.1.1 $ +# +# last change: $Author: hr $ $Date: 2000-09-18 15:17:24 $ +# +# The Contents of this file are made available subject to the terms of +# either of the following licenses +# +# - GNU Lesser General Public License Version 2.1 +# - Sun Industry Standards Source License Version 1.1 +# +# Sun Microsystems Inc., October, 2000 +# +# GNU Lesser General Public License Version 2.1 +# ============================================= +# Copyright 2000 by Sun Microsystems, Inc. +# 901 San Antonio Road, Palo Alto, CA 94303, USA +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License version 2.1, as published by the Free Software Foundation. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, +# MA 02111-1307 USA +# +# +# Sun Industry Standards Source License Version 1.1 +# ================================================= +# The contents of this file are subject to the Sun Industry Standards +# Source License Version 1.1 (the "License"); You may not use this file +# except in compliance with the License. You may obtain a copy of the +# License at http://www.openoffice.org/license.html. +# +# Software provided under this License is provided on an "AS IS" basis, +# WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, +# WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS, +# MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING. +# See the License for the specific provisions governing your rights and +# obligations concerning the Software. +# +# The Initial Developer of the Original Code is: Sun Microsystems, Inc. +# +# Copyright: 2000 by Sun Microsystems, Inc. +# +# All Rights Reserved. +# +# Contributor(s): _______________________________________ +# +# +# +#************************************************************************* + +PRJ=..$/.. + +PRJNAME=sal +TARGET=cpprtl +USE_LDUMP2=TRUE + +PROJECTPCH4DLL=TRUE +PROJECTPCH=cont_pch +PROJECTPCHSOURCE=cont_pch + +# --- Settings ----------------------------------------------------- + +.INCLUDE : svpre.mk +.INCLUDE : settings.mk +.INCLUDE : sv.mk + +# --- Files -------------------------------------------------------- + +.IF "$(header)" == "" + +SLOFILES= $(SLO)$/alloc.obj \ + $(SLO)$/memory.obj \ + $(SLO)$/cipher.obj \ + $(SLO)$/crc.obj \ + $(SLO)$/digest.obj \ + $(SLO)$/random.obj \ + $(SLO)$/char.obj \ + $(SLO)$/ustring.obj \ + $(SLO)$/locale.obj \ + $(SLO)$/rtl_process.obj \ + $(SLO)$/string.obj \ + $(SLO)$/strbuf.obj \ + $(SLO)$/uuid.obj \ + $(SLO)$/ustrbuf.obj \ + $(SLO)$/byteseq.obj + +.IF "$(GUI)" == "WIN" +SLOFILES+= $(SLO)$/tcwin16.obj +.ENDIF + +#.IF "$(UPDATER)"=="YES" +OBJFILES= $(OBJ)$/alloc.obj \ + $(OBJ)$/memory.obj \ + $(OBJ)$/cipher.obj \ + $(OBJ)$/crc.obj \ + $(OBJ)$/digest.obj \ + $(OBJ)$/random.obj \ + $(OBJ)$/char.obj \ + $(OBJ)$/ustring.obj \ + $(OBJ)$/locale.obj \ + $(OBJ)$/rtl_process.obj \ + $(OBJ)$/string.obj \ + $(OBJ)$/strbuf.obj \ + $(OBJ)$/uuid.obj \ + $(OBJ)$/ustrbuf.obj \ + $(OBJ)$/byteseq.obj +#.ENDIF + +.IF "$(GUI)" == "WIN" +OBJFILES+= $(OBJ)$/tcwin16.obj + +.ENDIF + +.ENDIF + +# --- Targets ------------------------------------------------------ + +.INCLUDE : target.mk + diff --git a/sal/rtl/source/memory.c b/sal/rtl/source/memory.c new file mode 100644 index 000000000000..ea31af3db75b --- /dev/null +++ b/sal/rtl/source/memory.c @@ -0,0 +1,124 @@ +/************************************************************************* + * + * $RCSfile: memory.c,v $ + * + * $Revision: 1.1.1.1 $ + * + * last change: $Author: hr $ $Date: 2000-09-18 15:17:24 $ + * + * The Contents of this file are made available subject to the terms of + * either of the following licenses + * + * - GNU Lesser General Public License Version 2.1 + * - Sun Industry Standards Source License Version 1.1 + * + * Sun Microsystems Inc., October, 2000 + * + * GNU Lesser General Public License Version 2.1 + * ============================================= + * Copyright 2000 by Sun Microsystems, Inc. + * 901 San Antonio Road, Palo Alto, CA 94303, USA + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, + * MA 02111-1307 USA + * + * + * Sun Industry Standards Source License Version 1.1 + * ================================================= + * The contents of this file are subject to the Sun Industry Standards + * Source License Version 1.1 (the "License"); You may not use this file + * except in compliance with the License. You may obtain a copy of the + * License at http://www.openoffice.org/license.html. + * + * Software provided under this License is provided on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, + * WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS, + * MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING. + * See the License for the specific provisions governing your rights and + * obligations concerning the Software. + * + * The Initial Developer of the Original Code is: Sun Microsystems, Inc. + * + * Copyright: 2000 by Sun Microsystems, Inc. + * + * All Rights Reserved. + * + * Contributor(s): _______________________________________ + * + * + ************************************************************************/ + + +#ifndef MAC +#include <memory.h> +#endif +#include <string.h> + +#include <rtl/memory.h> + +void SAL_CALL rtl_zeroMemory(void *Ptr, sal_uInt32 Bytes) +{ +#ifdef _WIN16 + _fmemset(Ptr, 0, Bytes); +#else + memset(Ptr, 0, Bytes); +#endif +} + +void SAL_CALL rtl_fillMemory(void *Ptr, sal_uInt32 Bytes, sal_uInt8 Fill) +{ +#ifdef _WIN16 + _fmemset(Ptr, Fill, Bytes); +#else + memset(Ptr, Fill, Bytes); +#endif +} + +void SAL_CALL rtl_copyMemory(void *Dst, const void *Src, sal_uInt32 Bytes) +{ +#ifdef _WIN16 + _fmemcpy(Dst, Src, Bytes); +#else + memcpy(Dst, Src, Bytes); +#endif +} + +void SAL_CALL rtl_moveMemory(void *Dst, const void *Src, sal_uInt32 Bytes) +{ +#ifdef _WIN16 + _fmemmove(Dst, Src, Bytes); +#else + memmove(Dst, Src, Bytes); +#endif +} + +sal_Int32 SAL_CALL rtl_compareMemory(const void *MemA, const void *MemB, sal_uInt32 Bytes) +{ +#ifdef _WIN16 + return _fmemcmp(MemA, MemB, Bytes); +#else + return memcmp(MemA, MemB, Bytes); +#endif +} + +void* SAL_CALL rtl_findInMemory(const void *MemA, sal_uInt8 ch, sal_uInt32 Bytes) +{ +#ifdef _WIN16 + return _fmemchr(MemA, ch, Bytes); +#else + return memchr(MemA, ch, Bytes); +#endif +} + + diff --git a/sal/rtl/source/random.c b/sal/rtl/source/random.c new file mode 100644 index 000000000000..8b7e102fc3e5 --- /dev/null +++ b/sal/rtl/source/random.c @@ -0,0 +1,383 @@ +/************************************************************************* + * + * $RCSfile: random.c,v $ + * + * $Revision: 1.1.1.1 $ + * + * last change: $Author: hr $ $Date: 2000-09-18 15:17:24 $ + * + * The Contents of this file are made available subject to the terms of + * either of the following licenses + * + * - GNU Lesser General Public License Version 2.1 + * - Sun Industry Standards Source License Version 1.1 + * + * Sun Microsystems Inc., October, 2000 + * + * GNU Lesser General Public License Version 2.1 + * ============================================= + * Copyright 2000 by Sun Microsystems, Inc. + * 901 San Antonio Road, Palo Alto, CA 94303, USA + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, + * MA 02111-1307 USA + * + * + * Sun Industry Standards Source License Version 1.1 + * ================================================= + * The contents of this file are subject to the Sun Industry Standards + * Source License Version 1.1 (the "License"); You may not use this file + * except in compliance with the License. You may obtain a copy of the + * License at http://www.openoffice.org/license.html. + * + * Software provided under this License is provided on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, + * WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS, + * MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING. + * See the License for the specific provisions governing your rights and + * obligations concerning the Software. + * + * The Initial Developer of the Original Code is: Sun Microsystems, Inc. + * + * Copyright: 2000 by Sun Microsystems, Inc. + * + * All Rights Reserved. + * + * Contributor(s): _______________________________________ + * + * + ************************************************************************/ + +#define _RTL_RANDOM_C_ "$Revision: 1.1.1.1 $" + +#ifndef _SAL_TYPES_H_ +#include <sal/types.h> +#endif + +#ifndef _OSL_THREAD_H_ +#include <osl/thread.h> +#endif + +#ifndef _OSL_TIME_H_ +#include <osl/time.h> +#endif + +#ifndef _RTL_ALLOC_H_ +#include <rtl/alloc.h> +#endif + +#ifndef _RTL_DIGEST_H_ +#include <rtl/digest.h> +#endif + +#ifndef _RTL_RANDOM_H_ +#include <rtl/random.h> +#endif + +/*======================================================================== + * + * rtlRandom internals. + * + *======================================================================*/ +#define RTL_RANDOM_RNG_1(a) ((a) * 16807L) +#define RTL_RANDOM_RNG_2(a) ((a) * 65539L) + +#define RTL_RANDOM_RNG(x, y, z) \ +{ \ + (x) = 170 * ((x) % 178) - 63 * ((x) / 178); \ + if ((x) < 0) (x) += 30328L; \ + \ + (y) = 171 * ((y) % 177) - 2 * ((y) / 177); \ + if ((y) < 0) (y) += 30269L; \ + \ + (z) = 172 * ((z) % 176) - 35 * ((z) / 176); \ + if ((z) < 0) (z) += 30307L; \ +} + +/** RandomData_Impl. + */ +typedef struct random_data_impl_st +{ + sal_Int16 m_nX; + sal_Int16 m_nY; + sal_Int16 m_nZ; +} RandomData_Impl; + +/** __rtl_random_data. + */ +static double __rtl_random_data (RandomData_Impl *pImpl); + +/** RandomPool_Impl. + */ +#define RTL_RANDOM_DIGEST rtl_Digest_AlgorithmMD5 +#define RTL_RANDOM_SIZE_DIGEST RTL_DIGEST_LENGTH_MD5 +#define RTL_RANDOM_SIZE_POOL 1023 + +typedef struct random_pool_impl_st +{ + rtlDigest m_hDigest; + sal_uInt8 m_pDigest[RTL_RANDOM_SIZE_DIGEST]; + sal_uInt8 m_pData[RTL_RANDOM_SIZE_POOL + 1]; + sal_uInt32 m_nData; + sal_uInt32 m_nIndex; + sal_uInt32 m_nCount; +} RandomPool_Impl; + +/** __rtl_random_initPool. + */ +static sal_Bool __rtl_random_initPool ( + RandomPool_Impl *pImpl); + +/** __rtl_random_seedPool. + */ +static void __rtl_random_seedPool ( + RandomPool_Impl *pImpl, const sal_uInt8 *pBuffer, sal_uInt32 nBufLen); + +/** __rtl_random_readPool. + */ +static void __rtl_random_readPool ( + RandomPool_Impl *pImpl, sal_uInt8 *pBuffer, sal_uInt32 nBufLen); + +/* + * __rtl_random_data. + */ +static double __rtl_random_data (RandomData_Impl *pImpl) +{ + register double random; + + RTL_RANDOM_RNG (pImpl->m_nX, pImpl->m_nY, pImpl->m_nZ); + random = (((double)(pImpl->m_nX) / 30328.0) + + ((double)(pImpl->m_nY) / 30269.0) + + ((double)(pImpl->m_nZ) / 30307.0) ); + + random -= ((double)((sal_uInt32)(random))); + return (random); +} + +/* + * __rtl_random_initPool. + */ +static sal_Bool __rtl_random_initPool (RandomPool_Impl *pImpl) +{ + pImpl->m_hDigest = rtl_digest_create (RTL_RANDOM_DIGEST); + if (pImpl->m_hDigest) + { + oslThreadIdentifier id; + TimeValue tv; + RandomData_Impl rd; + double seed; + + __rtl_random_seedPool (pImpl, (sal_uInt8*)&id, sizeof(id)); + __rtl_random_seedPool (pImpl, (sal_uInt8*)&tv, sizeof(tv)); + __rtl_random_seedPool (pImpl, (sal_uInt8*)&rd, sizeof(rd)); + + id = osl_getThreadIdentifier (NULL); + id = RTL_RANDOM_RNG_2(RTL_RANDOM_RNG_1(id)); + __rtl_random_seedPool (pImpl, (sal_uInt8*)&id, sizeof(id)); + + osl_getSystemTime (&tv); + tv.Seconds = RTL_RANDOM_RNG_2(tv.Seconds); + tv.Nanosec = RTL_RANDOM_RNG_2(tv.Nanosec); + __rtl_random_seedPool (pImpl, (sal_uInt8*)&tv, sizeof(tv)); + + rd.m_nX = (sal_Int16)(((id >> 1) << 1) + 1); + rd.m_nY = (sal_Int16)(((tv.Seconds >> 1) << 1) + 1); + rd.m_nZ = (sal_Int16)(((tv.Nanosec >> 1) << 1) + 1); + __rtl_random_seedPool (pImpl, (sal_uInt8*)&rd, sizeof(rd)); + + while (pImpl->m_nData < RTL_RANDOM_SIZE_POOL) + { + seed = __rtl_random_data (&rd); + __rtl_random_seedPool (pImpl, (sal_uInt8*)&seed, sizeof(seed)); + } + return sal_True; + } + return sal_False; +} + +/* + * __rtl_random_seedPool. + */ +static void __rtl_random_seedPool ( + RandomPool_Impl *pImpl, const sal_uInt8 *pBuffer, sal_uInt32 nBufLen) +{ + sal_uInt32 i; + sal_Int32 j, k; + + for (i = 0; i < nBufLen; i += RTL_RANDOM_SIZE_DIGEST) + { + j = nBufLen - i; + if (j > RTL_RANDOM_SIZE_DIGEST) + j = RTL_RANDOM_SIZE_DIGEST; + + rtl_digest_update ( + pImpl->m_hDigest, pImpl->m_pDigest, RTL_RANDOM_SIZE_DIGEST); + + k = (pImpl->m_nIndex + j) - RTL_RANDOM_SIZE_POOL; + if (k > 0) + { + rtl_digest_update ( + pImpl->m_hDigest, &(pImpl->m_pData[pImpl->m_nIndex]), j - k); + rtl_digest_update ( + pImpl->m_hDigest, &(pImpl->m_pData[0]), k); + } + else + { + rtl_digest_update ( + pImpl->m_hDigest, &(pImpl->m_pData[pImpl->m_nIndex]), j); + } + + rtl_digest_update (pImpl->m_hDigest, pBuffer, j); + pBuffer += j; + + rtl_digest_get ( + pImpl->m_hDigest, pImpl->m_pDigest, RTL_RANDOM_SIZE_DIGEST); + for (k = 0; k < j; k++) + { + pImpl->m_pData[pImpl->m_nIndex++] ^= pImpl->m_pDigest[k]; + if (pImpl->m_nIndex >= RTL_RANDOM_SIZE_POOL) + { + pImpl->m_nData = RTL_RANDOM_SIZE_POOL; + pImpl->m_nIndex = 0; + } + } + } + + if (pImpl->m_nIndex > pImpl->m_nData) + pImpl->m_nData = pImpl->m_nIndex; +} + +/* + * __rtl_random_readPool. + */ +static void __rtl_random_readPool ( + RandomPool_Impl *pImpl, sal_uInt8 *pBuffer, sal_uInt32 nBufLen) +{ + sal_Int32 j, k; + + while (nBufLen > 0) + { + j = nBufLen; + if (j > RTL_RANDOM_SIZE_DIGEST/2) + j = RTL_RANDOM_SIZE_DIGEST/2; + nBufLen -= j; + + rtl_digest_update ( + pImpl->m_hDigest, + &(pImpl->m_pDigest[RTL_RANDOM_SIZE_DIGEST/2]), + RTL_RANDOM_SIZE_DIGEST/2); + rtl_digest_update ( + pImpl->m_hDigest, pBuffer, j); + + k = (pImpl->m_nIndex + j) - pImpl->m_nData; + if (k > 0) + { + rtl_digest_update ( + pImpl->m_hDigest, &(pImpl->m_pData[pImpl->m_nIndex]), j - k); + rtl_digest_update ( + pImpl->m_hDigest, &(pImpl->m_pData[0]), k); + } + else + { + rtl_digest_update ( + pImpl->m_hDigest, &(pImpl->m_pData[pImpl->m_nIndex]), j); + } + + rtl_digest_get ( + pImpl->m_hDigest, pImpl->m_pDigest, RTL_RANDOM_SIZE_DIGEST); + for (k = 0; k < j; k++) + { + if (pImpl->m_nIndex >= pImpl->m_nData) pImpl->m_nIndex = 0; + pImpl->m_pData[pImpl->m_nIndex++] ^= pImpl->m_pDigest[k]; + *pBuffer++ = pImpl->m_pDigest[k + RTL_RANDOM_SIZE_DIGEST/2]; + } + } + + pImpl->m_nCount++; + rtl_digest_update ( + pImpl->m_hDigest, &(pImpl->m_nCount), sizeof(pImpl->m_nCount)); + rtl_digest_update ( + pImpl->m_hDigest, pImpl->m_pDigest, RTL_RANDOM_SIZE_DIGEST); + rtl_digest_get ( + pImpl->m_hDigest, pImpl->m_pDigest, RTL_RANDOM_SIZE_DIGEST); +} + +/*======================================================================== + * + * rtlRandom implementation. + * + *======================================================================*/ +/* + * rtl_random_createPool. + */ +rtlRandomPool SAL_CALL rtl_random_createPool (void) +{ + RandomPool_Impl *pImpl = (RandomPool_Impl*)NULL; + pImpl = (RandomPool_Impl*)rtl_allocateZeroMemory (sizeof(RandomPool_Impl)); + if (pImpl) + { + if (!__rtl_random_initPool (pImpl)) + { + rtl_freeZeroMemory (pImpl, sizeof(RandomPool_Impl)); + pImpl = (RandomPool_Impl*)NULL; + } + } + return ((rtlRandomPool)pImpl); +} + +/* + * rtl_random_destroyPool. + */ +void SAL_CALL rtl_random_destroyPool (rtlRandomPool Pool) +{ + RandomPool_Impl *pImpl = (RandomPool_Impl *)Pool; + if (pImpl) + { + rtl_digest_destroy (pImpl->m_hDigest); + rtl_freeZeroMemory (pImpl, sizeof (RandomPool_Impl)); + } +} + +/* + * rtl_random_addBytes. + */ +rtlRandomError SAL_CALL rtl_random_addBytes ( + rtlRandomPool Pool, const void *Buffer, sal_uInt32 Bytes) +{ + RandomPool_Impl *pImpl = (RandomPool_Impl *)Pool; + const sal_uInt8 *pBuffer = (const sal_uInt8 *)Buffer; + + if ((pImpl == NULL) || (pBuffer == NULL)) + return rtl_Random_E_Argument; + + __rtl_random_seedPool (pImpl, pBuffer, Bytes); + return rtl_Random_E_None; +} + +/* + * rtl_random_getBytes. + */ +rtlRandomError SAL_CALL rtl_random_getBytes ( + rtlRandomPool Pool, void *Buffer, sal_uInt32 Bytes) +{ + RandomPool_Impl *pImpl = (RandomPool_Impl *)Pool; + sal_uInt8 *pBuffer = (sal_uInt8 *)Buffer; + + if ((pImpl == NULL) || (pBuffer == NULL)) + return rtl_Random_E_Argument; + + __rtl_random_readPool (pImpl, pBuffer, Bytes); + return rtl_Random_E_None; +} + diff --git a/sal/rtl/source/rtl_process.c b/sal/rtl/source/rtl_process.c new file mode 100644 index 000000000000..ad804a772be8 --- /dev/null +++ b/sal/rtl/source/rtl_process.c @@ -0,0 +1,83 @@ +/************************************************************************* + * + * $RCSfile: rtl_process.c,v $ + * + * $Revision: 1.1.1.1 $ + * + * last change: $Author: hr $ $Date: 2000-09-18 15:17:24 $ + * + * The Contents of this file are made available subject to the terms of + * either of the following licenses + * + * - GNU Lesser General Public License Version 2.1 + * - Sun Industry Standards Source License Version 1.1 + * + * Sun Microsystems Inc., October, 2000 + * + * GNU Lesser General Public License Version 2.1 + * ============================================= + * Copyright 2000 by Sun Microsystems, Inc. + * 901 San Antonio Road, Palo Alto, CA 94303, USA + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, + * MA 02111-1307 USA + * + * + * Sun Industry Standards Source License Version 1.1 + * ================================================= + * The contents of this file are subject to the Sun Industry Standards + * Source License Version 1.1 (the "License"); You may not use this file + * except in compliance with the License. You may obtain a copy of the + * License at http://www.openoffice.org/license.html. + * + * Software provided under this License is provided on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, + * WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS, + * MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING. + * See the License for the specific provisions governing your rights and + * obligations concerning the Software. + * + * The Initial Developer of the Original Code is: Sun Microsystems, Inc. + * + * Copyright: 2000 by Sun Microsystems, Inc. + * + * All Rights Reserved. + * + * Contributor(s): _______________________________________ + * + * + ************************************************************************/ +#include <string.h> +#include <osl/mutex.h> +#include <rtl/uuid.h> + + +void SAL_CALL rtl_getGlobalProcessId( sal_uInt8 *pTargetUUID ) +{ + static sal_uInt8 *pUuid = 0; + if( ! pUuid ) + { + osl_acquireMutex( * osl_getGlobalMutex() ); + if( ! pUuid ) + { + static sal_uInt8 aUuid[16]; + rtl_createUuid( aUuid , 0 , sal_False ); + pUuid = aUuid; + } + osl_releaseMutex( * osl_getGlobalMutex() ); + } + memcpy( pTargetUUID , pUuid , 16 ); +} + + diff --git a/sal/rtl/source/strbuf.c b/sal/rtl/source/strbuf.c new file mode 100644 index 000000000000..8afb054d96c1 --- /dev/null +++ b/sal/rtl/source/strbuf.c @@ -0,0 +1,192 @@ +/************************************************************************* + * + * $RCSfile: strbuf.c,v $ + * + * $Revision: 1.1.1.1 $ + * + * last change: $Author: hr $ $Date: 2000-09-18 15:17:24 $ + * + * The Contents of this file are made available subject to the terms of + * either of the following licenses + * + * - GNU Lesser General Public License Version 2.1 + * - Sun Industry Standards Source License Version 1.1 + * + * Sun Microsystems Inc., October, 2000 + * + * GNU Lesser General Public License Version 2.1 + * ============================================= + * Copyright 2000 by Sun Microsystems, Inc. + * 901 San Antonio Road, Palo Alto, CA 94303, USA + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, + * MA 02111-1307 USA + * + * + * Sun Industry Standards Source License Version 1.1 + * ================================================= + * The contents of this file are subject to the Sun Industry Standards + * Source License Version 1.1 (the "License"); You may not use this file + * except in compliance with the License. You may obtain a copy of the + * License at http://www.openoffice.org/license.html. + * + * Software provided under this License is provided on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, + * WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS, + * MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING. + * See the License for the specific provisions governing your rights and + * obligations concerning the Software. + * + * The Initial Developer of the Original Code is: Sun Microsystems, Inc. + * + * Copyright: 2000 by Sun Microsystems, Inc. + * + * All Rights Reserved. + * + * Contributor(s): _______________________________________ + * + * + ************************************************************************/ + +#ifndef _OSL_INTERLOCK_H_ +#include <osl/interlck.h> +#endif + +#ifndef _RTL_STRING_HXX_ +#include <rtl/strbuf.hxx> +#endif + +#ifndef _RTL_MEMORY_H_ +#include <rtl/memory.h> +#endif + +/* +#ifndef _RTL_ALLOC_H_ +#include <rtl/alloc.h> +#endif +*/ + + + +/************************************************************************* + * rtl_stringbuffer_newFromStr_WithLength + */ +void SAL_CALL rtl_stringbuffer_newFromStr_WithLength( rtl_String ** newStr, + const sal_Char * value, + sal_Int32 count ) +{ + if (!value) + { + rtl_string_new_WithLength( newStr, 16 ); + return; + } + + if (*newStr) + rtl_string_release(*newStr); + + rtl_string_new_WithLength( newStr, count + 16 ); + (*newStr)->length = count; + rtl_copyMemory( (*newStr)->buffer, value, count ); + return; +} + +/************************************************************************* + * rtl_stringbuffer_newFromStringBuffer + */ +sal_Int32 SAL_CALL rtl_stringbuffer_newFromStringBuffer( rtl_String ** newStr, + sal_Int32 capacity, + rtl_String * oldStr ) +{ + sal_Int32 newCapacity = capacity; + + if (*newStr) + rtl_string_release(*newStr); + + if (newCapacity < oldStr->length) + newCapacity = oldStr->length; + + rtl_string_new_WithLength( newStr, newCapacity ); + (*newStr)->length = oldStr->length; + rtl_copyMemory( (*newStr)->buffer, oldStr->buffer, oldStr->length ); + return newCapacity; +} + +/************************************************************************* + * rtl_stringbuffer_ensureCapacity + */ +void SAL_CALL rtl_stringbuffer_ensureCapacity + (rtl_String ** This, sal_Int32* capacity, sal_Int32 minimumCapacity) +{ + if (minimumCapacity > *capacity) + { + rtl_String * pTmp = *This; + rtl_String * pNew = NULL; + *capacity = ((*This)->length + 1) * 2; + if (minimumCapacity > *capacity) + /* still lower, set to the minimum capacity */ + *capacity = minimumCapacity; + + rtl_string_new_WithLength(&pNew, *capacity); + pNew->length = (*This)->length; + *This = pNew; + + rtl_copyMemory( (*This)->buffer, pTmp->buffer, pTmp->length ); + rtl_string_release( pTmp ); + } +} + +/************************************************************************* + * rtl_stringbuffer_insert + */ +void SAL_CALL rtl_stringbuffer_insert( rtl_String ** This, + sal_Int32 * capacity, + sal_Int32 offset, + const sal_Char * str, + sal_Int32 len ) +{ + sal_Int32 nOldLen; + sal_Char * pBuf; + sal_Int32 n; + if( len != 0 ) + { + if (*capacity < (*This)->length + len) + rtl_stringbuffer_ensureCapacity( This, capacity, (*This)->length + len ); + + /* + if( len == 1 ) + This->buffer + */ + nOldLen = (*This)->length; + pBuf = (*This)->buffer; + + /* copy the tail */ + n = (nOldLen - offset); + if( n == 1 ) + /* optimized for 1 character */ + pBuf[offset + len] = pBuf[offset]; + else if( n > 1 ) + rtl_moveMemory( pBuf + offset + len, pBuf + offset, n * sizeof(sal_Char) ); + + /* insert the new characters */ + n = len; + if( len == 1 ) + /* optimized for 1 character */ + pBuf[offset] = *str; + else if( n > 1 ) + rtl_copyMemory( pBuf + offset, str, len * sizeof(sal_Char) ); + (*This)->length = nOldLen + len; + pBuf[ nOldLen + len ] = 0; + } +} + diff --git a/sal/rtl/source/string.c b/sal/rtl/source/string.c new file mode 100644 index 000000000000..a81ea1c69dd1 --- /dev/null +++ b/sal/rtl/source/string.c @@ -0,0 +1,1015 @@ +/************************************************************************* + * + * $RCSfile: string.c,v $ + * + * $Revision: 1.1.1.1 $ + * + * last change: $Author: hr $ $Date: 2000-09-18 15:17:24 $ + * + * The Contents of this file are made available subject to the terms of + * either of the following licenses + * + * - GNU Lesser General Public License Version 2.1 + * - Sun Industry Standards Source License Version 1.1 + * + * Sun Microsystems Inc., October, 2000 + * + * GNU Lesser General Public License Version 2.1 + * ============================================= + * Copyright 2000 by Sun Microsystems, Inc. + * 901 San Antonio Road, Palo Alto, CA 94303, USA + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, + * MA 02111-1307 USA + * + * + * Sun Industry Standards Source License Version 1.1 + * ================================================= + * The contents of this file are subject to the Sun Industry Standards + * Source License Version 1.1 (the "License"); You may not use this file + * except in compliance with the License. You may obtain a copy of the + * License at http://www.openoffice.org/license.html. + * + * Software provided under this License is provided on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, + * WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS, + * MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING. + * See the License for the specific provisions governing your rights and + * obligations concerning the Software. + * + * The Initial Developer of the Original Code is: Sun Microsystems, Inc. + * + * Copyright: 2000 by Sun Microsystems, Inc. + * + * All Rights Reserved. + * + * Contributor(s): _______________________________________ + * + * + ************************************************************************/ + +#ifndef _OSL_INTERLOCK_H_ +#include <osl/interlck.h> +#endif + +#ifndef _OSL_DIAGNOSE_H_ +#include <osl/diagnose.h> +#endif + +#ifndef _RTL_STRING_H_ +#include <rtl/string.h> +#endif + +#ifndef _RTL_STRING_HXX_ +#include <rtl/string.hxx> +#endif + +#ifndef _RTL_MEMORY_H_ +#include <rtl/memory.h> +#endif + +#ifndef _RTL_ALLOC_H_ +#include <rtl/alloc.h> +#endif + +static sal_Char RTL_STR_DIGITS[] = +{ + '0' , '1' , '2' , '3' , '4' , '5' , + '6' , '7' , '8' , '9' , 'a' , 'b' , + 'c' , 'd' , 'e' , 'f' , 'g' , 'h' , + 'i' , 'j' , 'k' , 'l' , 'm' , 'n' , + 'o' , 'p' , 'q' , 'r' , 's' , 't' , + 'u' , 'v' , 'w' , 'x' , 'y' , 'z' +}; + +/* static data to be referenced by all empty strings + * the refCount is predefined to 1 and must never become 0 ! + */ +static rtl_String aEmpty_rtl_String = +{ + 1, /* sal_Int32 refCount; */ + 0, /* sal_Int32 length; */ + '\0' /* sal_Char buffer[1]; */ +}; + +/************************************************************************* + * rtl_str_getLength + */ +sal_Int32 SAL_CALL rtl_str_getLength( const sal_Char * str ) +{ + const sal_Char * pTempStr = (sal_Char *)rtl_findInMemory(str, '\0', 0x80000000); + + return pTempStr - str; +} + +/************************************************************************* + * rtl_str_compareIgnoreCase_WithLength + */ +sal_Int32 SAL_CALL +rtl_str_compareIgnoreCase_WithLength( const sal_Char *first, + const sal_Char *second, + sal_Int32 len ) +{ + sal_Char a; + sal_Char b; + + while ( len ) + { + a = *first; + b = *second; + if ( (a >= 97) && (a <= 122) ) + a -= 32; + + if ( (b >= 97) && (b <= 122) ) + b -= 32; + + /* Naechstes Zeichen */ + first++; + second++; + len--; + + /* If characters don't match but case may be ignored, + try converting both characters to uppercase. + If the results match, then the comparison scan should + continue. */ + + if ( a == b ) + continue; + + return (sal_Int32)a - (sal_Int32)b; + } + + return 0; /* strings are equal */ +} + +/************************************************************************* + * rtl_str_compareIgnoreCase + */ +sal_Int32 SAL_CALL +rtl_str_compareIgnoreCase( const sal_Char * first, const sal_Char * second ) +{ + sal_Int32 firstLen = rtl_str_getLength(first); + sal_Int32 secondLen = rtl_str_getLength(second); + sal_Int32 shortestLen = 1 + (firstLen < secondLen ? firstLen : secondLen); + + return rtl_str_compareIgnoreCase_WithLength( first, second, shortestLen ); +} + +/************************************************************************* + * rtl_str_equalsIgnoreCase_WithLength + */ +sal_Bool SAL_CALL +rtl_str_equalsIgnoreCase_WithLength( const sal_Char * first, + sal_Int32 firstLen, + const sal_Char * second, + sal_Int32 secondLen ) +{ + if (firstLen != secondLen) + return sal_False; + + return rtl_str_compareIgnoreCase_WithLength(first, second, firstLen) == 0; +} + + +/************************************************************************* + * rtl_str_equalsIgnoreCase + */ +sal_Bool SAL_CALL +rtl_str_equalsIgnoreCase( const sal_Char * first, const sal_Char * second ) +{ + return rtl_str_equalsIgnoreCase_WithLength( + first, rtl_str_getLength(first), + second, rtl_str_getLength(second)); +} + +/************************************************************************* + * rtl_str_compare_WithLength + */ +sal_Int32 SAL_CALL rtl_str_compare_WithLength( const sal_Char * first, + sal_Int32 firstLen, + const sal_Char * second, + sal_Int32 secondLen ) +{ + const sal_Char * firstEnd = first + firstLen; + const sal_Char * secondEnd = second + secondLen; + sal_Int32 nResult = 0; + while( first < firstEnd && second < secondEnd + && (0 == (nResult = (sal_Int32)*first++ - (sal_Int32)*second++ ) ) ) + ; + if( nResult ) + return nResult; + + return firstLen - secondLen; +} + +/************************************************************************* + * rtl_str_compare + */ +sal_Int32 SAL_CALL rtl_str_compare( const sal_Char * first, const sal_Char * second ) +{ + sal_Int32 nRet; + while ( ((nRet = ((sal_Int32)(*first))-((sal_Int32)(*second))) == 0) && + *second ) + { + first++; + second++; + } + + return nRet; +} + +/************************************************************************* + * rtl_str_hashCode_WithLength + */ +sal_Int32 SAL_CALL rtl_str_hashCode_WithLength( const sal_Char * str, sal_Int32 len ) +{ + sal_Int32 h = 0; + const sal_Char * pVal = str; + sal_Int32 length = len; + + if( length < 16 ) + { + while( *pVal ) + h = (h * 37) + *pVal++; + } + else + { + sal_Int32 off = 0; + sal_Int32 i; + /* only sample some characters */ + sal_Int32 skip = length / 8; + for( i = length ; i > 0; i -= skip, off += skip ) + { + h = (h * 39) + pVal[off]; + } + } + + return h; +} + +/************************************************************************* + * rtl_str_hashCode + */ +sal_Int32 SAL_CALL rtl_str_hashCode( const sal_Char * str ) +{ + return rtl_str_hashCode_WithLength(str, rtl_str_getLength(str)); +} + +/************************************************************************* + * rtl_str_indexOfChar_WithLength + */ +sal_Int32 SAL_CALL rtl_str_indexOfChar_WithLength( const sal_Char * str, + sal_Int32 len, + sal_Char ch ) +{ + sal_Char* pTmp = (sal_Char*) rtl_findInMemory(str, ch, len); + + if (pTmp == NULL) + /* Zeichen nicht gefunden */ + return (-1); + else + return(pTmp - str); +} + +/************************************************************************* + * rtl_str_indexOfChar + */ +sal_Int32 SAL_CALL rtl_str_indexOfChar( const sal_Char * str, sal_Char ch ) +{ + return rtl_str_indexOfChar_WithLength(str, rtl_str_getLength(str), ch); +} + +/************************************************************************* + * rtl_str_lastIndexOfChar_WithLength + */ +sal_Int32 SAL_CALL rtl_str_lastIndexOfChar_WithLength( const sal_Char * str, + sal_Int32 len, + sal_Char ch ) +{ + sal_Int32 index = len - 1; + + while ( index >= 0 ) + { + if ( *(str + index) == ch ) + /* Zeichen gefunden */ + return index; + + /* Naechstes Zeichen */ + index--; + } + + /* Zeichen nicht gefunden */ + return -1; +} + +/************************************************************************* + * rtl_str_lastIndexOfChar + */ +sal_Int32 SAL_CALL rtl_str_lastIndexOfChar( const sal_Char * str, sal_Char ch ) +{ + return rtl_str_lastIndexOfChar_WithLength(str, rtl_str_getLength(str), ch); +} + +/************************************************************************* + * rtl_str_indexOfStr_WithLength + */ +sal_Int32 SAL_CALL rtl_str_indexOfStr_WithLength( const sal_Char * str, + sal_Int32 len, + const sal_Char * subStr, + sal_Int32 subLen ) +{ + sal_Int32 index = 0; + + if (len < subLen) + return -1; + + while ( *str ) + { + if ( *str == *subStr) + { + sal_Int32 offset = 0; + + /* wenn die restliche Laenge kleiner als der zu suchende string ist + kann der nicht mehr gefunden werden */ + if ((len - index) < subLen) + return -1; + + while ( offset != subLen) + { + if ( *(str + offset) != *(subStr + offset)) + break; + + offset++; + } + + /* Schleife komplett durchlaufen, d.h. string gefunden */ + if ( offset == subLen) + return index; + } + + index++; + str++; + } + + return -1; +} + +/************************************************************************* + * rtl_str_indexOfStr + */ +sal_Int32 SAL_CALL rtl_str_indexOfStr( const sal_Char * str, const sal_Char * subStr ) +{ + return rtl_str_indexOfStr_WithLength( str, rtl_str_getLength(str), + subStr, rtl_str_getLength(subStr) ); +} + +/************************************************************************* + * rtl_str_lastIndexOfStr_WithLength + */ +sal_Int32 SAL_CALL rtl_str_lastIndexOfStr_WithLength( const sal_Char * pSource, + sal_Int32 len, + const sal_Char * pSearch, + sal_Int32 nSearchLen ) +{ + const sal_Char * pRunSource = pSource + len -1; + while( pRunSource >= pSource && pRunSource - pSource >= nSearchLen ) + { + const sal_Char * pRunSearch = pSearch + nSearchLen -1; + const sal_Char * pSaveRunSource = pRunSource; + while( pRunSearch >= pSearch && *pRunSearch == *(pRunSource--) ) + pRunSearch--; + + if( pRunSearch < pSearch ) + return pRunSource - pSource +1; + pRunSource = pSaveRunSource; + pRunSource--; + } + return -1; +} + +/************************************************************************* + * rtl_str_lastIndexOfStrPtr + */ +sal_Int32 SAL_CALL rtl_str_lastIndexOfStr( const sal_Char * str, const sal_Char * subStr ) +{ + return rtl_str_lastIndexOfStr_WithLength( str, rtl_str_getLength(str), + subStr, rtl_str_getLength(subStr) ); +} + +/************************************************************************* + * rtl_str_replaceChar_WithLength + */ +void SAL_CALL rtl_str_replaceChar_WithLength( sal_Char * str, + sal_Int32 len, + sal_Char oldChar, + sal_Char newChar) +{ + while ( *str ) + { + if ( *str == oldChar) + *str = newChar; + + str++; + } +} + +/************************************************************************* + * rtl_str_replaceChar + */ +void SAL_CALL rtl_str_replaceChar( sal_Char * str, sal_Char oldChar, sal_Char newChar) +{ + while ( *str ) + { + if ( *str == oldChar) + *str = newChar; + + str++; + } +} + +/************************************************************************* + * rtl_str_toAsciiLowerCase_WithLength + */ +void SAL_CALL rtl_str_toAsciiLowerCase_WithLength( sal_Char * str, sal_Int32 len ) +{ + while ( *str ) + { + if ( (*str >= 65) && (*str <= 90) ) + *str += 32; + + str++; + } +} + +/************************************************************************* + * rtl_str_toAsciiLowerCase + */ +void SAL_CALL rtl_str_toAsciiLowerCase( sal_Char * str ) +{ + while ( *str ) + { + if ( (*str >= 65) && (*str <= 90) ) + *str += 32; + + str++; + } +} + +/************************************************************************* + * rtl_str_toAsciiUpperCase_WithLength + */ +void SAL_CALL rtl_str_toAsciiUpperCase_WithLength( sal_Char * str, sal_Int32 len ) +{ + while ( *str ) + { + if ( (*str >= 97) && (*str <= 122) ) + *str -= 32; + + str++; + } +} + +/************************************************************************* + * rtl_str_toAsciiUpperCase + */ +void SAL_CALL rtl_str_toAsciiUpperCase( sal_Char * str ) +{ + while ( *str ) + { + if ( (*str >= 97) && (*str <= 122) ) + *str -= 32; + + str++; + } +} + +/************************************************************************* + * rtl_str_trim_WithLength + */ +sal_Int32 SAL_CALL rtl_str_trim_WithLength( sal_Char * str, sal_Int32 len ) +{ + sal_Int32 preSpaces = 0; + sal_Int32 postSpaces = 0; + sal_Int32 newLen = 0; + sal_Int32 index = len - 1; + + while ( (preSpaces < len) && (*(str+preSpaces) == ' ') ) + preSpaces++; + + while ( (index > preSpaces) && (*(str+index) == ' ') ) + { + postSpaces++; + index--; + } + + newLen = len - preSpaces - postSpaces; + + if ( newLen != len ) + { + sal_Char *newStr = str + preSpaces; + index = 0; + + while ( index != newLen ) + { + *(str + index) = *(newStr + index); + index++; + } + *(str + index) = '\0'; + } + + return newLen; +} + +/************************************************************************* + * rtl_str_trim + */ +sal_Int32 SAL_CALL rtl_str_trim( sal_Char * str ) +{ + return rtl_str_trim_WithLength(str, rtl_str_getLength(str)); +} + +/************************************************************************* + * rtl_str_valueOfosl_BOOL + */ +sal_Int32 SAL_CALL rtl_str_valueOfBoolean( sal_Char * str, sal_Bool b ) +{ + if (b) + { + sal_Char *tmpStr = "True"; + rtl_copyMemory(str, tmpStr, 5); + return 4; + } else + { + sal_Char *tmpStr = "False"; + rtl_copyMemory(str, tmpStr, 6); + return 5; + } +} + +/************************************************************************* + * rtl_str_valueOfChar + */ +sal_Int32 SAL_CALL rtl_str_valueOfChar( sal_Char * str, sal_Char ch ) +{ + str[0] = ch; + str[1] = L'\0'; + return 1; +} + +/************************************************************************* + * rtl_str_valueOfosl_INT32 + */ +sal_Int32 SAL_CALL rtl_str_valueOfInt32(sal_Char * str, sal_Int32 i, sal_Int16 radix ) +{ + sal_Char buf[RTL_STR_MAX_VALUEOFINT32]; + sal_Bool negative = (i < 0); + sal_Int32 charPos = RTL_STR_MAX_VALUEOFINT32 - 1; + + if (radix < RTL_STR_MIN_RADIX || radix > RTL_STR_MAX_RADIX) + radix = 10; + + if (!negative) { + i = -i; + } + + while (i <= -radix) { + buf[charPos--] = RTL_STR_DIGITS[-(i % radix)]; + i = i / radix; + } + buf[charPos] = RTL_STR_DIGITS[-i]; + + if (negative) { + buf[--charPos] = '-'; + } + + rtl_copyMemory(str, buf + charPos, RTL_STR_MAX_VALUEOFINT32 - charPos); + + return (RTL_STR_MAX_VALUEOFINT32 - charPos); +} + +/************************************************************************* + * rtl_str_valueOfosl_INT64 + */ +sal_Int32 SAL_CALL rtl_str_valueOfInt64(sal_Char * str, sal_Int64 l, sal_Int16 radix ) +{ +#ifndef SAL_INT64_IS_STRUCT + sal_Char buf[RTL_STR_MAX_VALUEOFINT64]; + sal_Int32 charPos = RTL_STR_MAX_VALUEOFINT64 - 1; + sal_Bool negative = (l < 0); + + if (radix < RTL_STR_MIN_RADIX || radix > RTL_STR_MAX_RADIX) + radix = 10; + + if (!negative) { + l = -l; + } + + while (l <= -radix) { + buf[charPos--] = RTL_STR_DIGITS[(sal_Int32)(-(l % radix))]; + l = l / radix; + } + buf[charPos] = RTL_STR_DIGITS[(sal_Int32)(-l)]; + + if (negative) { + buf[--charPos] = '-'; + } + + rtl_copyMemory(str, buf + charPos, RTL_STR_MAX_VALUEOFINT64 - charPos ); + + return (RTL_STR_MAX_VALUEOFINT64 - charPos); +#else + return 0; +#endif +} + +/************************************************************************* + * rtl_str_valueOfFloat + */ +sal_Int32 SAL_CALL rtl_str_valueOfFloat( sal_Char * This, float f) +{ + return 0; +} + +/************************************************************************* + * rtl_str_valueOfDouble + */ +sal_Int32 SAL_CALL rtl_str_valueOfDouble( sal_Char * This, double d) +{ + return 0; +} + +/************************************************************************* + * + * rtl_string_XXX Functions + * +/************************************************************************* + +/************************************************************************* + * rtl_string_acquire + */ +void SAL_CALL rtl_string_acquire( rtl_String * value ) +{ + osl_incrementInterlockedCount(&value->refCount); +} + +/************************************************************************* + * rtl_string_release + */ +void SAL_CALL rtl_string_release( rtl_String * value ) +{ + if( 0 == osl_decrementInterlockedCount(&value->refCount) ) + { + OSL_ENSHURE( value != &aEmpty_rtl_String, "static empty string: refCount==0"); + rtl_freeMemory(value); + } +} + +/************************************************************************* + * rtl_string_new + */ +void SAL_CALL rtl_string_new( rtl_String ** newStr ) +{ + if (*newStr) + rtl_string_release(*newStr); + + *newStr = &aEmpty_rtl_String; + rtl_string_acquire( *newStr ); +} + +/************************************************************************* + * rtl_string_new_WithLength + */ +void SAL_CALL rtl_string_new_WithLength( rtl_String ** newStr, sal_Int32 nLen ) +{ + if (*newStr) + rtl_string_release(*newStr); + + *newStr = (rtl_String*)rtl_allocateMemory( sizeof(rtl_String) + nLen ); + + if (!(*newStr)) return; + + (*newStr)->refCount = 1; + (*newStr)->length = 0; + + rtl_zeroMemory((*newStr)->buffer, nLen + 1); +} + +/************************************************************************* + * rtl_string_newFromString + */ +void SAL_CALL rtl_string_newFromString( rtl_String ** newStr, rtl_String * value ) +{ + rtl_String * pOrg; + + if (value->length == 0) + { + rtl_string_new(newStr); + return; + } + + pOrg = *newStr; + *newStr = (rtl_String*)rtl_allocateMemory( sizeof(rtl_String) + value->length ); + if ( *newStr ) + { + (*newStr)->refCount = 1; + (*newStr)->length = value->length; + rtl_copyMemory( (*newStr)->buffer, value->buffer, value->length+1 ); + } + + /* must be done at least, if left == *newStr */ + if ( pOrg ) + rtl_string_release(pOrg); +} + +/************************************************************************* + * rtl_string_newFromStr + */ +void SAL_CALL rtl_string_newFromStr( rtl_String ** newStr, const sal_Char * value ) +{ + rtl_String * pOrg; + sal_Int32 length; + + if (!value) + { + rtl_string_new(newStr); + return; + } + + length = rtl_str_getLength(value); + + pOrg = *newStr; + *newStr = (rtl_String*)rtl_allocateMemory( sizeof(rtl_String) + length ); + if ( *newStr ) + { + (*newStr)->refCount = 1; + (*newStr)->length = length; + rtl_copyMemory((*newStr)->buffer, value, length+1 ); + } + + /* must be done at least, if left == *newStr */ + if ( pOrg ) + rtl_string_release(pOrg); +} + +/************************************************************************* + * rtl_string_newFromStr_WithLength + */ +void SAL_CALL rtl_string_newFromStr_WithLength( rtl_String ** newStr, const sal_Char * value, sal_Int32 len ) +{ + rtl_String * pOrg; + + if ( !value || len < 0 ) + { + rtl_string_new(newStr); + return; + } + + pOrg = *newStr; + *newStr = (rtl_String*)rtl_allocateMemory( sizeof(rtl_String) + len ); + if ( *newStr ) + { + (*newStr)->refCount = 1; + (*newStr)->length = len; + rtl_copyMemory((*newStr)->buffer, value, len ); + (*newStr)->buffer[len] = 0; + } + + /* must be done at least, if left == *newStr */ + if ( pOrg ) + rtl_string_release(pOrg); +} + +/************************************************************************* + * rtl_string_assign + */ +void SAL_CALL rtl_string_assign( /*inout*/rtl_String ** str, rtl_String * rightValue ) +{ + rtl_string_acquire(rightValue); + + if (*str) + rtl_string_release(*str); + + *str = rightValue; +} + +/************************************************************************* + * rtl_string_getLength + */ +sal_Int32 SAL_CALL rtl_string_getLength( rtl_String * str ) +{ + return str->length; +} + +/************************************************************************* + * rtl_string_getStr + */ +sal_Char * SAL_CALL rtl_string_getStr( rtl_String * str ) +{ + return str->buffer; +} + +/************************************************************************* + * rtl_string_newConcat + */ +void SAL_CALL rtl_string_newConcat( rtl_String ** newStr, rtl_String * left, rtl_String * right ) +{ + rtl_String * pOrg = *newStr; + if( !right->length ) + { + *newStr = left; + rtl_string_acquire( left ); + } + else if( !left->length ) + { + *newStr = right; + rtl_string_acquire( right ); + } + else + { + rtl_String *tmpStr = NULL; + sal_Int32 nNewLen = left->length + right->length; + rtl_string_new_WithLength(&tmpStr, nNewLen); + tmpStr->length = nNewLen; + + rtl_copyMemory( tmpStr->buffer, left->buffer, left->length ); + rtl_copyMemory( tmpStr->buffer + left->length, right->buffer, right->length ); + tmpStr->buffer[nNewLen] = 0; + *newStr = tmpStr; + } + /* must be done at least, if left == *newStr */ + if( pOrg ) + rtl_string_release(pOrg); +} + +/************************************************************************* + * rtl_string_newReplace + */ +void SAL_CALL rtl_string_newReplace( rtl_String ** newStr, + rtl_String * str, + sal_Char oldChar, + sal_Char newChar) +{ + rtl_string_newFromString(newStr, str); + + if (!(*newStr)) return; + + rtl_str_replaceChar_WithLength( (*newStr)->buffer, (*newStr)->length, oldChar, newChar ); +} + +/************************************************************************* + * rtl_string_newReplaceStrAt + */ +void SAL_CALL rtl_string_newReplaceStrAt( rtl_String ** newStr, + rtl_String * str, + sal_Int32 index, + sal_Int32 count, + rtl_String * newSub) +{ + sal_Char *pBuffer = NULL; + + if (count == 0 && newSub->length == 0) + { + rtl_string_newFromString(newStr, str); + return; + } + + if (count == 0) + { + rtl_string_new_WithLength(newStr, str->length + newSub->length); + if (!(*newStr)) return; + pBuffer = (*newStr)->buffer; + (*newStr)->length = str->length + newSub->length; + rtl_copyMemory(pBuffer, str->buffer, index); + rtl_copyMemory(pBuffer + index, newSub->buffer, newSub->length); + rtl_copyMemory(pBuffer + index + newSub->length, str->buffer + index, str->length - index); + + return; + } + + if (newSub->length == 0) + { + rtl_string_new_WithLength(newStr, str->length - count); + if (!(*newStr)) return; + pBuffer = (*newStr)->buffer; + (*newStr)->length = str->length - count; + rtl_copyMemory(pBuffer, str->buffer, index); + rtl_copyMemory(pBuffer + index, str->buffer + index + count, str->length - index - count); + + return; + } + + rtl_string_new_WithLength(newStr, str->length - count + newSub->length); + if (!(*newStr)) return; + pBuffer = (*newStr)->buffer; + (*newStr)->length = str->length - count + newSub->length; + rtl_copyMemory(pBuffer, str->buffer, index); + rtl_copyMemory(pBuffer + index, newSub->buffer, newSub->length); + rtl_copyMemory(pBuffer + index + newSub->length, str->buffer + index + count, + str->length - index - count); + + return; +} + +/************************************************************************* + * rtl_String_newToLowerCase + */ +void SAL_CALL rtl_string_newToLowerCase( rtl_String ** newStr, rtl_String * str) +{ + rtl_string_newFromString(newStr, str); + + if (!(*newStr)) return; + + rtl_str_toAsciiLowerCase_WithLength((*newStr)->buffer, (*newStr)->length); +} + +/************************************************************************* + * rtl_String_newToUpperCase + */ +void SAL_CALL rtl_string_newToUpperCase( rtl_String ** newStr, rtl_String * str) +{ + rtl_string_newFromString(newStr, str); + + if (!(*newStr)) return; + + rtl_str_toAsciiUpperCase_WithLength((*newStr)->buffer, (*newStr)->length); +} + +/************************************************************************* + * rtl_string_newTrim + */ +void SAL_CALL rtl_string_newTrim( rtl_String ** newStr, rtl_String * str ) +{ + rtl_string_newFromString(newStr, str); + + if (!(*newStr)) return; + + (*newStr)->length = rtl_str_trim_WithLength((*newStr)->buffer, (*newStr)->length); +} + +/************************************************************************* + * rtl_string_getTokenCount + */ +sal_Int32 SAL_CALL rtl_string_getTokenCount( rtl_String * str , sal_Char cTok) +{ + sal_Int32 count = 0; + sal_Int32 index1 = 0; + sal_Int32 index2 = 0; + sal_Char *buffer = str->buffer; + + while ((index2 = rtl_str_indexOfChar_WithLength(buffer + index1, str->length - index1, cTok)) >= 0) + { + if (index2 <= (str->length - index1) && index2 >= 0) + count++; + + index1 += index2 + 1; + } + + if (index2 < 0 && index1 <= str->length) + count++; + + return count; +} + +/************************************************************************* + * rtl_string_getToken + */ +void SAL_CALL rtl_string_getToken( rtl_String ** newStr , rtl_String * str, sal_Int32 nToken, sal_Char cTok) +{ + sal_Int32 count = 0; + sal_Int32 index1 = 0; + sal_Int32 index2 = 0; + sal_Char *buffer = str->buffer; + + while ((index2 = rtl_str_indexOfChar_WithLength(buffer + index1, str->length - index1, cTok)) >= 0) + { + if (count == nToken) + { + rtl_string_newFromStr_WithLength( newStr, buffer + index1, index2 ); + return; + } + + if (index2 <= (str->length - index1) && index2 >= 0) + count++; + + index1 += index2 + 1; + } + + if (nToken == 0 || nToken == count) + rtl_string_newFromStr_WithLength( newStr, buffer + index1, str->length - index1 ); + else + rtl_string_new(newStr); + + return; +} + diff --git a/sal/rtl/source/ustrbuf.c b/sal/rtl/source/ustrbuf.c new file mode 100644 index 000000000000..7e31c50af8c2 --- /dev/null +++ b/sal/rtl/source/ustrbuf.c @@ -0,0 +1,234 @@ +/************************************************************************* + * + * $RCSfile: ustrbuf.c,v $ + * + * $Revision: 1.1.1.1 $ + * + * last change: $Author: hr $ $Date: 2000-09-18 15:17:24 $ + * + * The Contents of this file are made available subject to the terms of + * either of the following licenses + * + * - GNU Lesser General Public License Version 2.1 + * - Sun Industry Standards Source License Version 1.1 + * + * Sun Microsystems Inc., October, 2000 + * + * GNU Lesser General Public License Version 2.1 + * ============================================= + * Copyright 2000 by Sun Microsystems, Inc. + * 901 San Antonio Road, Palo Alto, CA 94303, USA + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, + * MA 02111-1307 USA + * + * + * Sun Industry Standards Source License Version 1.1 + * ================================================= + * The contents of this file are subject to the Sun Industry Standards + * Source License Version 1.1 (the "License"); You may not use this file + * except in compliance with the License. You may obtain a copy of the + * License at http://www.openoffice.org/license.html. + * + * Software provided under this License is provided on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, + * WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS, + * MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING. + * See the License for the specific provisions governing your rights and + * obligations concerning the Software. + * + * The Initial Developer of the Original Code is: Sun Microsystems, Inc. + * + * Copyright: 2000 by Sun Microsystems, Inc. + * + * All Rights Reserved. + * + * Contributor(s): _______________________________________ + * + * + ************************************************************************/ + +#ifndef _OSL_INTERLOCK_H_ +#include <osl/interlck.h> +#endif + +#ifndef _RTL_STRING_HXX_ +#include <rtl/ustrbuf.hxx> +#endif + +#ifndef _RTL_MEMORY_H_ +#include <rtl/memory.h> +#endif + +/* +#ifndef _RTL_ALLOC_H_ +#include <rtl/alloc.h> +#endif +*/ + + + +/************************************************************************* + * rtl_uStringbuffer_newFromStr_WithLength + */ +void SAL_CALL rtl_uStringbuffer_newFromStr_WithLength( rtl_uString ** newStr, + const sal_Unicode * value, + sal_Int32 count) +{ + if (!value) + { + rtl_uString_new_WithLength( newStr, 16 ); + return; + } + + if (*newStr) + rtl_uString_release(*newStr); + + rtl_uString_new_WithLength( newStr, count + 16 ); + (*newStr)->length = count; + rtl_copyMemory( (*newStr)->buffer, value, count * sizeof(sal_Unicode)); + return; +} + +/************************************************************************* + * rtl_uStringbuffer_newFromStringBuffer + */ +sal_Int32 SAL_CALL rtl_uStringbuffer_newFromStringBuffer( rtl_uString ** newStr, + sal_Int32 capacity, + rtl_uString * oldStr ) +{ + sal_Int32 newCapacity = capacity; + + if (*newStr) + rtl_uString_release(*newStr); + + if (newCapacity < oldStr->length) + newCapacity = oldStr->length; + + rtl_uString_new_WithLength( newStr, newCapacity ); + (*newStr)->length = oldStr->length; + rtl_copyMemory( (*newStr)->buffer, oldStr->buffer, oldStr->length * sizeof(sal_Unicode)); + return newCapacity; +} + +/************************************************************************* + * rtl_uStringbuffer_ensureCapacity + */ +void SAL_CALL rtl_uStringbuffer_ensureCapacity + (rtl_uString ** This, sal_Int32* capacity, sal_Int32 minimumCapacity) +{ + if (minimumCapacity > *capacity) + { + rtl_uString * pTmp = *This; + rtl_uString * pNew = NULL; + *capacity = ((*This)->length + 1) * 2; + if (minimumCapacity > *capacity) + /* still lower, set to the minimum capacity */ + *capacity = minimumCapacity; + + rtl_uString_new_WithLength(&pNew, *capacity); + pNew->length = (*This)->length; + *This = pNew; + + rtl_copyMemory( (*This)->buffer, pTmp->buffer, pTmp->length * sizeof(sal_Unicode) ); + rtl_uString_release( pTmp ); + } +} + +/************************************************************************* + * rtl_uStringbuffer_insert + */ +void SAL_CALL rtl_uStringbuffer_insert( rtl_uString ** This, + sal_Int32 * capacity, + sal_Int32 offset, + const sal_Unicode * str, + sal_Int32 len) +{ + sal_Int32 nOldLen; + sal_Unicode * pBuf; + sal_Int32 n; + if( len != 0 ) + { + if (*capacity < (*This)->length + len) + rtl_uStringbuffer_ensureCapacity( This, capacity, (*This)->length + len ); + + /* + if( len == 1 ) + This->buffer + */ + nOldLen = (*This)->length; + pBuf = (*This)->buffer; + + /* copy the tail */ + n = (nOldLen - offset); + if( n == 1 ) + /* optimized for 1 character */ + pBuf[offset + len] = pBuf[offset]; + else if( n > 1 ) + rtl_moveMemory( pBuf + offset + len, pBuf + offset, n * sizeof(sal_Unicode) ); + + /* insert the new characters */ + if( len == 1 ) + /* optimized for 1 character */ + pBuf[offset] = *str; + else if( len > 1 ) + rtl_copyMemory( pBuf + offset, str, len * sizeof(sal_Unicode) ); + (*This)->length = nOldLen + len; + pBuf[ nOldLen + len ] = 0; + } +} + +/************************************************************************* + * rtl_uStringbuffer_insert_ascii + */ +void SAL_CALL rtl_uStringbuffer_insert_ascii( /*inout*/rtl_uString ** This, + /*inout*/sal_Int32 * capacity, + sal_Int32 offset, + const sal_Char * str, + sal_Int32 len) +{ + sal_Int32 nOldLen; + sal_Unicode * pBuf; + sal_Int32 n; + if( len != 0 ) + { + if (*capacity < (*This)->length + len) + rtl_uStringbuffer_ensureCapacity( This, capacity, (*This)->length + len ); + + nOldLen = (*This)->length; + pBuf = (*This)->buffer; + + /* copy the tail */ + n = (nOldLen - offset); + if( n == 1 ) + /* optimized for 1 character */ + pBuf[offset + len] = pBuf[offset]; + else if( n > 1 ) + rtl_moveMemory( pBuf + offset + len, pBuf + offset, n * sizeof(sal_Unicode) ); + + /* insert the new characters */ + for( n = 0; n < len; n++ ) + { + /* Check ASCII range */ + OSL_ENSHURE( (*str & 0x80) == 0, "Found ASCII char > 127"); + + pBuf[offset + n] = (sal_Unicode)*(str++); + } + + (*This)->length = nOldLen + len; + pBuf[ nOldLen + len ] = 0; + } +} + + diff --git a/sal/rtl/source/ustring.c b/sal/rtl/source/ustring.c new file mode 100644 index 000000000000..fe97fedb7aa2 --- /dev/null +++ b/sal/rtl/source/ustring.c @@ -0,0 +1,1774 @@ +/************************************************************************* + * + * $RCSfile: ustring.c,v $ + * + * $Revision: 1.1.1.1 $ + * + * last change: $Author: hr $ $Date: 2000-09-18 15:17:24 $ + * + * The Contents of this file are made available subject to the terms of + * either of the following licenses + * + * - GNU Lesser General Public License Version 2.1 + * - Sun Industry Standards Source License Version 1.1 + * + * Sun Microsystems Inc., October, 2000 + * + * GNU Lesser General Public License Version 2.1 + * ============================================= + * Copyright 2000 by Sun Microsystems, Inc. + * 901 San Antonio Road, Palo Alto, CA 94303, USA + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, + * MA 02111-1307 USA + * + * + * Sun Industry Standards Source License Version 1.1 + * ================================================= + * The contents of this file are subject to the Sun Industry Standards + * Source License Version 1.1 (the "License"); You may not use this file + * except in compliance with the License. You may obtain a copy of the + * License at http://www.openoffice.org/license.html. + * + * Software provided under this License is provided on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, + * WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS, + * MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING. + * See the License for the specific provisions governing your rights and + * obligations concerning the Software. + * + * The Initial Developer of the Original Code is: Sun Microsystems, Inc. + * + * Copyright: 2000 by Sun Microsystems, Inc. + * + * All Rights Reserved. + * + * Contributor(s): _______________________________________ + * + * + ************************************************************************/ + +#ifndef _OSL_INTERLOCK_H_ +#include <osl/interlck.h> +#endif + +#ifndef _OSL_DIAGNOSE_H_ +#include <osl/diagnose.h> +#endif + +#ifndef _SAL_CONFIG_H_ +#include <sal/config.h> +#endif + +#ifndef _RTL_CHAR_H_ +#include <rtl/char.h> +#endif + +#ifndef _RTL_USTRING_H_ +#include <rtl/ustring.h> +#endif + +#ifndef _RTL_USTRING_ +#include <rtl/ustring> +#endif + +#ifndef _RTL_LOCALE_H_ +#include <rtl/locale.h> +#endif + +#ifndef _RTL_MEMORY_H_ +#include <rtl/memory.h> +#endif + +#ifndef _RTL_ALLOC_H_ +#include <rtl/alloc.h> +#endif + +#ifndef _RTL_TEXTCVT_H +#include <rtl/textcvt.h> +#endif + +#ifndef _RTL_TENCINFO_H +#include <rtl/tencinfo.h> +#endif + +#include <math.h> + + +static sal_Char const RTL_STR_DIGITS[] = +{ + '0' , '1' , '2' , '3' , '4' , '5' , + '6' , '7' , '8' , '9' , 'a' , 'b' , + 'c' , 'd' , 'e' , 'f' , 'g' , 'h' , + 'i' , 'j' , 'k' , 'l' , 'm' , 'n' , + 'o' , 'p' , 'q' , 'r' , 's' , 't' , + 'u' , 'v' , 'w' , 'x' , 'y' , 'z' +}; + +/* static data to be referenced by all empty strings + * the refCount is predefined to 1 and must never become 0 ! + */ +static rtl_uString aEmpty_rtl_wString = +{ + 1, /* sal_Int32 refCount; */ + 0, /* sal_Int32 length; */ + 0 /* sal_Unicode buffer[1]; */ +}; + +/************************************************************************* + * rtl_ustr_getLength + */ +sal_Int32 SAL_CALL rtl_ustr_getLength( const sal_Unicode * str ) +{ + const sal_Unicode * pTempStr = str; + while( *pTempStr ) pTempStr++; + return pTempStr - str; +} + +/************************************************************************* + * rtl_ustr_equalsIgnoreCase_WithLength + */ +sal_Bool SAL_CALL rtl_ustr_equalsIgnoreCase_WithLength( const sal_Unicode * first, + sal_Int32 firstLen, + const sal_Unicode * second, + sal_Int32 secondLen ) +{ + sal_Unicode a; + sal_Unicode b; + + if (firstLen != secondLen) + return sal_False; + + while ( firstLen ) + { + a = rtl_char_toUpperCase(*first); + b = rtl_char_toUpperCase(*second); + + /* Naechstes Zeichen */ + first++; + second++; + firstLen--; + + /* If characters don't match but case may be ignored, + try converting both characters to uppercase. + If the results match, then the comparison scan should + continue. */ + if ( a == b ) + continue; + + /* Unfortunately, conversion to uppercase does not work properly + for the Georgian alphabet, which has strange rules about case + conversion. So we need to make one last check before + exiting. */ + if ( rtl_char_toLowerCase(a) == rtl_char_toLowerCase(b) ) + continue; + + return sal_False; + } + + return sal_True; /* Strings sind gleich */ +} + +/************************************************************************* + * rtl_ustr_equalsIgnoreCase + */ +sal_Bool SAL_CALL rtl_ustr_equalsIgnoreCase( const sal_Unicode * first, const sal_Unicode * second ) +{ + return rtl_ustr_equalsIgnoreCase_WithLength( first, rtl_ustr_getLength(first), + second, rtl_ustr_getLength(second) ); +} + +/************************************************************************* + * rtl_ustr_compare_WithLength + */ +sal_Int32 SAL_CALL rtl_ustr_compare_WithLength( const sal_Unicode * first, + sal_Int32 firstLen, + const sal_Unicode * second, + sal_Int32 secondLen ) +{ + const sal_Unicode * firstEnd = first + firstLen; + const sal_Unicode * secondEnd = second + secondLen; + sal_Int32 nResult = 0; + while( first < firstEnd && second < secondEnd + && (0 == (nResult = (sal_Int32)*first++ - (sal_Int32)*second++ ) ) ) + ; + if( nResult ) + return nResult; + return firstLen - secondLen; +} + +/************************************************************************* + * rtl_ustr_compare_WithLength + */ +sal_Int32 SAL_CALL rtl_ustr_shortenedCompare_WithLength( const sal_Unicode * first, + sal_Int32 firstLen, + const sal_Unicode * second, + sal_Int32 secondLen, + sal_Int32 shortenedLength ) +{ + const sal_Unicode * firstEnd = first + firstLen; + const sal_Unicode * secondEnd = second + secondLen; + sal_Int32 nResult = 0; + while( shortenedLength-- && first < firstEnd && second < secondEnd + && (0 == (nResult = (sal_Int32)*first++ - (sal_Int32)*second++ ) ) ) + ; + if( nResult || !(shortenedLength != -1) ) + return nResult; + return firstLen - secondLen; +} + +/************************************************************************* + * rtl_ustr_compare + */ +sal_Int32 SAL_CALL rtl_ustr_compare( const sal_Unicode * first, const sal_Unicode * second ) +{ + sal_Int32 nRet; + while ( ((nRet = ((sal_Int32)*first)-((sal_Int32)*second)) == 0) && + *second ) + { + first++; + second++; + } + + return nRet; +} + +/************************************************************************* + * rtl_ustr_ascii_compare_WithLength + */ +sal_Int32 SAL_CALL rtl_ustr_ascii_compare_WithLength( const sal_Unicode * first, + sal_Int32 firstLen, + const sal_Char * second ) +{ + return rtl_ustr_ascii_shortenedCompare_WithLength( first, firstLen, second, 0x7FFFFFF ); +} + +/************************************************************************* + * rtl_ustr_ascii_compare + */ +sal_Int32 SAL_CALL rtl_ustr_ascii_compare( const sal_Unicode * first, const sal_Char * second ) +{ + sal_Int32 nRet; + while ( ((nRet = ((sal_Int32)*first)-((sal_Int32)*second)) == 0) && + *second ) + { + first++; + second++; + } + + return nRet; +} + +/************************************************************************* + * rtl_ustr_ascii_shortenedCompare_WithLength + */ +sal_Int32 SAL_CALL rtl_ustr_ascii_shortenedCompare_WithLength( const sal_Unicode * first, + sal_Int32 firstLen, + const sal_Char * second, + sal_Int32 shortenedLength ) +{ + const sal_Unicode * firstEnd = first + firstLen; + sal_Int32 nResult = 0; + while( shortenedLength-- + && first < firstEnd + && *second // necessary if 0 is allowed in Unicode + && (0 == (nResult = (sal_Int32)*first++ - (sal_Int32)*second++ ) ) ) + { + /* Check ASCII range */ + OSL_ENSHURE( (*(second-1) & 0x80) == 0, "Found ASCII char > 127"); + } + if( !nResult && (shortenedLength != -1) ) + { + if( *second ) + { + OSL_ENSHURE( first == firstEnd, "first == firstEnd failed" ); + // first is a substring of the second string => less (negative value) + nResult = -1; + } + else + // greater or equal + nResult = firstEnd - first; + } + + return nResult; +} + +/************************************************************************* + * rtl_ustr_asciil_reverseCompare_WithLength + */ +sal_Int32 SAL_CALL rtl_ustr_asciil_reverseCompare_WithLength( const sal_Unicode * first, sal_Int32 firstLen, + const sal_Char * second, sal_Int32 secondLen ) +{ + const sal_Unicode * firstRun = first + firstLen; + const sal_Char * secondRun = second + secondLen; + sal_Int32 nResult = 0; + while( first < firstRun && second < secondRun + && (0 == (nResult = (sal_Int32)*--firstRun - (sal_Int32)*--secondRun ) ) ) + ; + if( nResult ) + return nResult; + return firstLen - secondLen; +} + + +/************************************************************************* + * rtl_ustr_hashCode_WithLength + */ +sal_Int32 SAL_CALL rtl_ustr_hashCode_WithLength( const sal_Unicode * str, sal_Int32 len ) +{ + sal_Int32 h = 0; + const sal_Unicode * pVal = str; + sal_Int32 length = len; + + if( length < 16 ) + { + while( *pVal ) + h = (h * 37) + *pVal++; + } + else + { + sal_Int32 off = 0; + sal_Int32 i; + /* only sample some characters */ + sal_Int32 skip = length / 8; + for( i = length ; i > 0; i -= skip, off += skip ) + { + h = (h * 39) + pVal[off]; + } + } + + return h; +} + +/************************************************************************* + * rtl_ustr_hashCode + */ +sal_Int32 SAL_CALL rtl_ustr_hashCode( const sal_Unicode * str ) +{ + return rtl_ustr_hashCode_WithLength(str, rtl_ustr_getLength(str)); +} + +/************************************************************************* + * rtl_ustr_indexOfChar_WithLength + */ +sal_Int32 SAL_CALL rtl_ustr_indexOfChar_WithLength( const sal_Unicode * str, + sal_Int32 len, + sal_Unicode ch ) +{ + sal_Int32 index = 0; + + while ( index != len ) + { + if (!(*str)) + return -1;; + + if ( *str == ch ) + /* Zeichen gefunden */ + return index; + + /* Naechstes Zeichen */ + index++; + str++; + } + + /* Zeichen nicht gefunden */ + return -1; +} + +/************************************************************************* + * rtl_ustr_indexOfChar + */ +sal_Int32 SAL_CALL rtl_ustr_indexOfChar( const sal_Unicode * str, sal_Unicode ch ) +{ + return rtl_ustr_indexOfChar_WithLength(str, rtl_ustr_getLength(str), ch); +} + +/************************************************************************* + * rtl_ustr_lastIndexOfChar_WithLength + */ +sal_Int32 SAL_CALL rtl_ustr_lastIndexOfChar_WithLength( const sal_Unicode * str, + sal_Int32 len, + sal_Unicode ch ) +{ + sal_Int32 index = len - 1; + + while ( index >= 0 ) + { + if ( *(str + index) == ch ) + /* Zeichen gefunden */ + return index; + + /* Naechstes Zeichen */ + index--; + } + + /* Zeichen nicht gefunden */ + return -1; +} + +/************************************************************************* + * rtl_ustr_lastIndexOfChar + */ +sal_Int32 SAL_CALL rtl_ustr_lastIndexOfChar( const sal_Unicode * str, sal_Unicode ch ) +{ + return rtl_ustr_lastIndexOfChar_WithLength(str, rtl_ustr_getLength(str), ch); +} + +/************************************************************************* + * rtl_ustr_indexOfStr_WithLength + */ +sal_Int32 SAL_CALL rtl_ustr_indexOfStr_WithLength( const sal_Unicode * str, + sal_Int32 len, + const sal_Unicode * subStr, + sal_Int32 subLen ) +{ + sal_Int32 index = 0; + + if (len < subLen) + return -1; + + while ( *str ) + { + if ( *str == *subStr) + { + sal_Int32 offset = 0; + + /* wenn die restliche Laenge kleiner als der zu suchende string ist + kann der nicht mehr gefunden werden */ + if ((len - index) < subLen) + return -1; + + while ( offset != subLen) + { + if ( *(str + offset) != *(subStr + offset)) + break; + + offset++; + } + + /* Schleife komplett durchlaufen, d.h. string gefunden */ + if ( offset == subLen) + return index; + } + + index++; + str++; + } + + return -1; +} + +/************************************************************************* + * rtl_ustr_indexOfStr + */ +sal_Int32 SAL_CALL rtl_ustr_indexOfStr( const sal_Unicode * str, const sal_Unicode * subStr ) +{ + return rtl_ustr_indexOfStr_WithLength( str, rtl_ustr_getLength(str), + subStr, rtl_ustr_getLength(subStr) ); +} + +/************************************************************************* + * rtl_ustr_lastIndexOfStr_WithLength + */ +sal_Int32 SAL_CALL rtl_ustr_lastIndexOfStr_WithLength( const sal_Unicode * pSource, + sal_Int32 len, + const sal_Unicode * pSearch, + sal_Int32 nSearchLen ) +{ + const sal_Unicode * pRunSource = pSource + len -1; + while( pRunSource >= pSource && pRunSource - pSource >= nSearchLen ) + { + const sal_Unicode * pRunSearch = pSearch + nSearchLen -1; + const sal_Unicode * pSaveRunSource = pRunSource; + while( pRunSearch >= pSearch && *(pRunSearch--) == *(pRunSource--) ) + ; + if( pRunSearch < pSearch ) + return pRunSource - pSource +1; + pRunSource = pSaveRunSource; + pRunSource--; + } + return -1; +} + +/************************************************************************* + * rtl_ustr_lastIndexOfStrPtr + */ +sal_Int32 SAL_CALL rtl_ustr_lastIndexOfStr( const sal_Unicode * str, const sal_Unicode * subStr ) +{ + return rtl_ustr_lastIndexOfStr_WithLength( str, rtl_ustr_getLength(str), + subStr, rtl_ustr_getLength(subStr) ); +} + +/************************************************************************* + * rtl_ustr_replaceChar_WithLength + */ +void SAL_CALL rtl_ustr_replaceChar_WithLength( sal_Unicode * str, + sal_Int32 len, + sal_Unicode oldChar, + sal_Unicode newChar) +{ + while ( *str ) + { + if ( *str == oldChar) + *str = newChar; + + str++; + } +} + +/************************************************************************* + * rtl_ustr_replaceChar + */ +void SAL_CALL rtl_ustr_replaceChar( sal_Unicode * str, sal_Unicode oldChar, sal_Unicode newChar) +{ + while ( *str ) + { + if ( *str == oldChar) + *str = newChar; + + str++; + } +} + +/************************************************************************* + * rtl_ustr_toAsciiLowerCase_WithLength + */ +void SAL_CALL rtl_ustr_toAsciiLowerCase_WithLength( sal_Unicode * str, sal_Int32 len ) +{ + sal_Unicode ch; + + while ( *str ) + { + ch = *str; + *str = rtl_char_toLowerCase(ch); + str++; + } +} + +/************************************************************************* + * rtl_ustr_toAsciiLowerCase + */ +void SAL_CALL rtl_ustr_toAsciiLowerCase( sal_Unicode * str ) +{ + sal_Unicode ch; + + while ( *str ) + { + ch = *str; + *str = rtl_char_toLowerCase(ch); + str++; + } +} + +/************************************************************************* + * rtl_ustr_toAsciiUpperCase_WithLength + */ +void SAL_CALL rtl_ustr_toAsciiUpperCase_WithLength(sal_Unicode * str, sal_Int32 len) +{ + sal_Unicode ch; + + while ( *str ) + { + ch = *str; + *str = rtl_char_toUpperCase(ch); + str++; + } +} + +/************************************************************************* + * rtl_ustr_toAsciiUpperCase + */ +void SAL_CALL rtl_ustr_toAsciiUpperCase(sal_Unicode * str) +{ + sal_Unicode ch; + + while ( *str ) + { + ch = *str; + *str = rtl_char_toUpperCase(ch); + str++; + } +} + +/************************************************************************* + * rtl_ustr_trim_WithLength + */ +sal_Int32 SAL_CALL rtl_ustr_trim_WithLength( sal_Unicode * str, sal_Int32 len ) +{ + sal_Int32 preSpaces = 0; + sal_Int32 postSpaces = 0; + sal_Int32 newLen = 0; + sal_Int32 index = len - 1; + + while ( (preSpaces < len) && rtl_char_isWhitespace(*(str+preSpaces)) ) + preSpaces++; + + while ( (index > preSpaces) && rtl_char_isWhitespace(*(str+index)) ) + { + postSpaces++; + index--; + } + + newLen = len - preSpaces - postSpaces; + + if ( newLen != len ) + { + sal_Unicode *newStr = str + preSpaces; + index = 0; + + while ( index != newLen ) + { + *(str + index) = *(newStr + index); + index++; + } + *(str + index) = 0; + } + + return newLen; +} + +/************************************************************************* + * rtl_ustr_trim + */ +sal_Int32 SAL_CALL rtl_ustr_trim( sal_Unicode * str ) +{ + return rtl_ustr_trim_WithLength(str, rtl_ustr_getLength(str)); +} + +/************************************************************************* + * rtl_ustr_valueOfosl_BOOL + */ +sal_Int32 SAL_CALL rtl_ustr_valueOfBoolean( sal_Unicode * str, sal_Bool b ) +{ + if (b) + { + sal_Unicode *tmpStr = L"True"; + rtl_copyMemory(str, tmpStr, 5 * sizeof(sal_Unicode)); + return 4; + } else + { + sal_Unicode *tmpStr = L"False"; + rtl_copyMemory(str, tmpStr, 6 * sizeof(sal_Unicode)); + return 5; + } +} + +/************************************************************************* + * rtl_ustr_valueOfChar + */ +sal_Int32 SAL_CALL rtl_ustr_valueOfChar( sal_Unicode * str, sal_Unicode ch ) +{ + str[0] = ch; + str[1] = L'\0'; + return 1; +} + +/************************************************************************* + * rtl_ustr_valueOfosl_INT32 + */ +sal_Int32 SAL_CALL rtl_ustr_valueOfInt32(sal_Unicode * str, sal_Int32 i, sal_Int16 radix ) +{ + sal_Unicode buf[RTL_USTR_MAX_VALUEOFINT32]; + sal_Bool negative = (i < 0); + sal_Int32 charPos = RTL_USTR_MAX_VALUEOFINT32 - 1; + + if (radix < RTL_USTR_MIN_RADIX || radix > RTL_USTR_MAX_RADIX) + radix = 10; + + if (!negative) { + i = -i; + } + + while (i <= -radix) { + buf[charPos--] = RTL_STR_DIGITS[-(i % radix)]; + i = i / radix; + } + buf[charPos] = RTL_STR_DIGITS[-i]; + + if (negative) { + buf[--charPos] = '-'; + } + + rtl_copyMemory(str, buf + charPos, (RTL_USTR_MAX_VALUEOFINT32 - charPos) * sizeof(sal_Unicode)); + + return (RTL_USTR_MAX_VALUEOFINT32 - charPos); +} + +/************************************************************************* + * rtl_ustr_valueOfosl_INT64 + */ +sal_Int32 SAL_CALL rtl_ustr_valueOfInt64(sal_Unicode * str, sal_Int64 l, sal_Int16 radix ) +{ +#ifndef SAL_INT64_IS_STRUCT + sal_Unicode buf[RTL_USTR_MAX_VALUEOFINT64]; + sal_Int32 charPos = RTL_USTR_MAX_VALUEOFINT64 - 1; + sal_Bool negative = (l < 0); + + if (radix < RTL_USTR_MIN_RADIX || radix > RTL_USTR_MAX_RADIX) + radix = 10; + + if (!negative) { + l = -l; + } + + while (l <= -radix) { + buf[charPos--] = RTL_STR_DIGITS[(sal_Int32)(-(l % radix))]; + l = l / radix; + } + buf[charPos] = RTL_STR_DIGITS[(sal_Int32)(-l)]; + + if (negative) { + buf[--charPos] = '-'; + } + + rtl_copyMemory(str, buf + charPos, (RTL_USTR_MAX_VALUEOFINT64 - charPos) * sizeof(sal_Unicode)); + + return (RTL_USTR_MAX_VALUEOFINT64 - charPos); +#else + return 0; +#endif +} + +/************************************************************************* + * numberToStringImpl + */ + +#define FLOAT_MAX_POS ( 3.402823e+38) +#define FLOAT_MAX_NEG (-3.402823e+38) +#define FLOAT_MIN_POS ( 1.175494351e-38) +#define FLOAT_MIN_NEG (-1.175494351e-38) + + +sal_Int32 SAL_CALL numberToStringImpl(sal_Unicode * str, double d, sal_Int16 significantDigits ) +{ + /* Make it very simple without any formatting, + * (similar to Double.toString() in Java) */ + sal_Unicode buf[ RTL_USTR_MAX_VALUEOFDOUBLE ]; + sal_Unicode* charPos = buf; + sal_Int16 i, len, dig, dotPos; + sal_Int16 lastNonZeroPos; + sal_Int16 nExpDigits; + sal_Bool bExp; + double dExp; + + if( d == 0 ) + { + *(charPos++) = L'0'; + *(charPos++) = L'.'; + *(charPos++) = L'0'; + } + else + { + if( d < 0 ) + { + *(charPos++) = L'-'; + d = -d; + } + + dExp = log10( d ); + bExp = sal_False; + if( dExp > 7 || dExp <= -7 ) + bExp = sal_True; + + dExp = floor( dExp ); + d /= pow( 10, dExp ); + while( d > 10 ) + { + d /= 10.0; + dExp += 1.0; + } + + if( d < 1.0 ) + significantDigits++; + + dotPos = bExp ? 0 : (sal_Int16)dExp; + lastNonZeroPos = 0; + for( i = 0 ; i < significantDigits ; i++ ) + { + dig = (sal_Int16)d; + if( dig ) + lastNonZeroPos = i; + *(charPos++) = L'0' + dig; + if( i == dotPos ) + *(charPos++) = L'.'; + d -= (double)dig; + d *= 10.0; + } + + /* Kill trailing zeros */ + if( lastNonZeroPos > dotPos + 1 ) + charPos -= (significantDigits - 1 - lastNonZeroPos); + + /* exponent */ + if( bExp ) + { + *(charPos++) = L'E'; + if( dExp < 0.0 ) + { + dExp = -dExp; + *(charPos++) = L'-'; + } + + nExpDigits = 1; + while( dExp >= 10.0 ) + { + nExpDigits++; + dExp /= 10; + } + for( i = 0 ; i < nExpDigits ; i++ ) + { + dig = (sal_Int16)dExp; + *(charPos++) = L'0' + dig; + dExp -= (double)dig; + dExp *= 10.0; + } + } + } + + *(charPos++) = 0; + len = charPos - buf; + rtl_copyMemory( str, buf, len * sizeof(sal_Unicode) ); + return len - 1; +} + +/*************************************************************************/ + +sal_Int32 SAL_CALL getInfinityStr(sal_Unicode * This, sal_Bool bNeg) +{ + sal_Char InfinityStr[] = "-Infinity"; + sal_Char* pStr = bNeg ? InfinityStr : InfinityStr + 1; + sal_Int32 len = bNeg ? 9 : 8; + sal_Int32 i; + for ( i = 0; i < len+1; i++ ) + *(This+i) = *(pStr+i); + return len; +} + +sal_Int32 SAL_CALL getNaNStr(sal_Unicode * This) +{ + sal_Char NaNStr[] = "NaN"; + sal_Int32 len = 3; + sal_Int32 i; + for ( i = 0; i < len+1; i++ ) + *(This+i) = NaNStr[i]; + return len; +} + +/************************************************************************* + * rtl_ustr_valueOfFloat + */ +#define singleSignMask ((sal_uInt32)0x80000000) +#define singleExpMask ((sal_uInt32)0x7f800000) +#define singleFractMask (~(singleSignMask|singleExpMask)) +#define singleExpShift ((sal_uInt32)23) + +sal_Int32 SAL_CALL rtl_ustr_valueOfFloat(sal_Unicode * This, float f) +{ + /* Discover obvious special cases of NaN and Infinity + * (like in Java Ctor FloatingDecimal( float f ) ) */ + sal_uInt32 fBits = *(sal_uInt32*)(&f); + sal_uInt32 binExp = (sal_uInt32)( (fBits & singleExpMask) >> singleExpShift ); + sal_uInt32 fractBits = fBits & singleFractMask; + if ( binExp == (sal_uInt32)(singleExpMask>>singleExpShift) ) + { + if ( fractBits == 0L ) + return getInfinityStr(This, (sal_Bool)(f < 0.0)); + else + return getNaNStr(This); + } + return numberToStringImpl( This, (double)f, 8 ); +} + +/************************************************************************* + * rtl_ustr_valueOfDouble + */ +#ifndef WNT +#define signMask ((sal_uInt64)0x8000000000000000LL) +#define expMask ((sal_uInt64)0x7ff0000000000000LL) +#else +#define signMask ((sal_uInt64)0x8000000000000000L) +#define expMask ((sal_uInt64)0x7ff0000000000000L) +#endif + +#define fractMask (~(signMask|expMask)) +#define expShift ((sal_uInt32)52) + +sal_Int32 SAL_CALL rtl_ustr_valueOfDouble(sal_Unicode * This, double d) +{ +#ifndef SAL_INT64_IS_STRUCT + /* Discover obvious special cases of NaN and Infinity. + * (like in Java Ctor FloatingDecimal( double d ) ) */ + sal_uInt64 dBits = *(sal_uInt64*)(&d); + sal_uInt32 binExp = (sal_uInt32)( (dBits & expMask) >> expShift ); + sal_uInt64 fractBits = dBits & fractMask; + if ( binExp == (int)(expMask >> expShift) ) + { + if ( fractBits == 0L ) + return getInfinityStr(This, (sal_Bool)(d < 0.0)); + else + return getNaNStr(This); + } +#endif + return numberToStringImpl( This, d, 17 ); +} + +/************************************************************************* + * rtl_ustr_toInt32 + */ +sal_Int32 SAL_CALL rtl_ustr_toInt32( sal_Unicode * str, sal_Int16 radix ) +{ + sal_Char* stopstring; + sal_Char* CharStr; + sal_Char* pChar; + sal_Int32 length, i, nVal; + sal_Unicode wc; + + /* simple conversion to 8 bit string */ + length = rtl_ustr_getLength( str ); + CharStr = (sal_Char*)rtl_allocateMemory( length + 1 ); + pChar = CharStr; + for( i = 0 ; i < length ; i++ ) + { + wc = *(str++); + *(pChar++) = ( wc <= 255 ) ? (sal_Char)wc : 0; + } + CharStr[ length ] = 0; + nVal = strtol( CharStr, &stopstring, radix ); + rtl_freeMemory( CharStr ); + return nVal; +} + +/************************************************************************* + * Help function for rtl_ustr_toInt64 + */ +sal_Int16 getDigit( sal_Unicode ch, sal_Int16 radix ) +{ + sal_Int16 value = -1; + if( ch >= L'0' && ch <= L'9' ) + value = ch - L'0'; + else if( ch >= L'a' && ch <= L'z' ) + value = ch - L'a' + 10; + else if( ch >= L'A' && ch <= L'Z' ) + value = ch - L'A' + 10; + return ( value < radix ) ? value : -1; +} + +#ifndef WNT +#define INT64_MIN_VALUE 0x8000000000000000LL; +#define INT64_MAX_VALUE 0x7fffffffffffffffLL; +#else +#define INT64_MIN_VALUE 0x8000000000000000L; +#define INT64_MAX_VALUE 0x7fffffffffffffffL; +#endif + +/************************************************************************* + * rtl_ustr_toInt64 (like in Java) + */ +sal_Int64 SAL_CALL rtl_ustr_toInt64( sal_Unicode * str, sal_Int16 radix ) +{ +#ifndef SAL_INT64_IS_STRUCT + sal_Int64 result = 0; + sal_Bool negative = sal_False; + sal_Int32 i = 0; + sal_Int32 max = rtl_ustr_getLength( str ); + sal_Int64 limit; + sal_Int64 multmin; + sal_Int32 digit; + + if( max == 0 ) + return 0; + + if( radix < RTL_USTR_MIN_RADIX || radix > RTL_USTR_MAX_RADIX ) + radix = 10; + + while( i < max && (str[i] == L' ' || str[i] == L'\t') ) + i++; + + if( str[i] == L'-' ) + { + negative = sal_True; + limit = INT64_MIN_VALUE; + i++; + } + else + { + limit = -INT64_MAX_VALUE; + } + multmin = limit / radix; + if( i < max ) + { + digit = getDigit( str[i++], radix ); + if( digit < 0 ) + return 0; + result = -digit; + } + while( i < max ) + { + // Accumulating negatively avoids surprises near MAX_VALUE + digit = getDigit( str[i++], radix ); + if( digit < 0 ) + break; + + if( result < multmin ) + break; + + result *= radix; + if( result < limit + digit ) + break; + + result -= digit; + } + if( negative ) + return result; + else + return -result; + +#endif + return 0; +} + +/************************************************************************* + * rtl_ustr_toFloat + */ +float SAL_CALL rtl_ustr_toFloat( sal_Unicode * str ) +{ + sal_Char* stopstring; + sal_Char* CharStr; + sal_Char* pChar; + sal_Int32 length, i; + sal_Unicode wc; + double dVal; + + /* simple conversion to 8 bit string */ + length = rtl_ustr_getLength( str ); + CharStr = (sal_Char*)rtl_allocateMemory( length + 1); + pChar = CharStr; + for( i = 0 ; i < length ; i++ ) + { + wc = *(str++); + *(pChar++) = ( wc <= 255 ) ? (sal_Char)wc : 0; + } + CharStr[ length ] = 0; + dVal = strtod( CharStr, &stopstring ); + + /* float range? */ + if( dVal > FLOAT_MAX_POS || + dVal < FLOAT_MAX_NEG || + ( dVal > 0 && dVal < FLOAT_MIN_POS ) || + ( dVal < 0 && dVal > FLOAT_MIN_NEG ) ) + { + dVal = 0.0; + } + rtl_freeMemory( CharStr ); + return (float)dVal; +} + +/************************************************************************* + * rtl_ustr_toDouble + */ +double SAL_CALL rtl_ustr_toDouble( sal_Unicode * str ) +{ + sal_Char* stopstring; + sal_Char* CharStr; + sal_Char* pChar; + sal_Int32 length, i; + sal_Unicode wc; + double dVal; + + /* simple conversion to 8 bit string */ + length = rtl_ustr_getLength( str ); + CharStr = (sal_Char*)rtl_allocateMemory( length + 1 ); + pChar = CharStr; + for( i = 0 ; i < length ; i++ ) + { + wc = *(str++); + *(pChar++) = ( wc <= 255 ) ? (sal_Char)wc : 0; + } + CharStr[ length ] = 0; + dVal = strtod( CharStr, &stopstring ); + rtl_freeMemory( CharStr ); + return dVal; +} + + +/************************************************************************* + * + * rtl_uString_XXX Functions + * +/************************************************************************* + +/************************************************************************* + * rtl_uString_acquire + */ +void SAL_CALL rtl_uString_acquire( rtl_uString * value ) +{ + osl_incrementInterlockedCount(&value->refCount); +} + +/************************************************************************* + * rtl_uString_release + */ +void SAL_CALL rtl_uString_release( rtl_uString * value ) +{ + if( 0 == osl_decrementInterlockedCount(&value->refCount) ) + { + OSL_ENSHURE( value != &aEmpty_rtl_wString, "static empty string: refCount==0"); + rtl_freeMemory(value); + } +} + +/************************************************************************* + * rtl_uString_new + */ +void SAL_CALL rtl_uString_new( rtl_uString ** newStr ) +{ + if (*newStr) + rtl_uString_release(*newStr); + + *newStr = &aEmpty_rtl_wString; + rtl_uString_acquire( *newStr ); +} + +/************************************************************************* + * rtl_uString_new_WithLength + */ +void SAL_CALL rtl_uString_new_WithLength( rtl_uString ** newStr, sal_Int32 nLen ) +{ + if (*newStr) + rtl_uString_release(*newStr); + + *newStr = (rtl_uString*)rtl_allocateMemory( sizeof(rtl_uString) + (nLen * sizeof(sal_Unicode)) ); + + if (!(*newStr)) return; + + (*newStr)->refCount = 1; + (*newStr)->length = 0; + *((*newStr)->buffer + nLen) = L'\0'; + + rtl_zeroMemory((*newStr)->buffer, nLen * sizeof(sal_Unicode)); +} + +/************************************************************************* + * rtl_uString_newFromString + */ +void SAL_CALL rtl_uString_newFromString( rtl_uString ** newStr, rtl_uString * value ) +{ + rtl_uString * pOrg; + + if (value->length == 0) + { + rtl_uString_new(newStr); + return; + } + + pOrg = *newStr; + *newStr = (rtl_uString*)rtl_allocateMemory( sizeof(rtl_uString) + (value->length * sizeof(sal_Unicode)) ); + if ( *newStr ) + { + (*newStr)->refCount = 1; + (*newStr)->length = value->length; + rtl_copyMemory((*newStr)->buffer, value->buffer, (value->length+1) * sizeof(sal_Unicode)); + } + + /* must be done at least, if left == *newStr */ + if ( pOrg ) + rtl_uString_release( pOrg ); +} + +/************************************************************************* + * rtl_uString_newFromStr + */ +void SAL_CALL rtl_uString_newFromStr( rtl_uString ** newStr, const sal_Unicode * value ) +{ + rtl_uString * pOrg; + sal_Int32 length; + + if (!value) + { + rtl_uString_new(newStr); + return; + } + + length = rtl_ustr_getLength(value); + + pOrg = *newStr; + *newStr = (rtl_uString*)rtl_allocateMemory( sizeof(rtl_uString) + (length * sizeof(sal_Unicode)) ); + if ( *newStr ) + { + (*newStr)->refCount = 1; + (*newStr)->length = length; + rtl_copyMemory((*newStr)->buffer, value, (length+1) * sizeof(sal_Unicode)); + } + + /* must be done at least, if left == *newStr */ + if ( pOrg ) + rtl_uString_release( pOrg ); +} + +/************************************************************************* + * rtl_uString_newFromStr_WithLength + */ +void SAL_CALL rtl_uString_newFromStr_WithLength( rtl_uString ** newStr, + const sal_Unicode * value, + sal_Int32 len) +{ + rtl_uString * pOrg; + + if (!value || len < 0 ) + { + rtl_uString_new(newStr); + return; + } + + pOrg = *newStr; + *newStr = (rtl_uString*)rtl_allocateMemory( sizeof(rtl_uString) + (len * sizeof(sal_Unicode)) ); + if ( *newStr ) + { + (*newStr)->refCount = 1; + (*newStr)->length = len; + rtl_copyMemory((*newStr)->buffer, value, len * sizeof(sal_Unicode)); + (*newStr)->buffer[len] = 0; + } + + /* must be done at least, if left == *newStr */ + if ( pOrg ) + rtl_uString_release( pOrg ); +} + +/************************************************************************* + * rtl_wstr_getLength + */ +static sal_Int32 SAL_CALL rtl_wstr_getLength( const wchar_t* str ) +{ + const wchar_t * pTempStr = str; + while( *pTempStr ) pTempStr++; + return pTempStr - str; +} + +/************************************************************************* + * rtl_uString_newFromWStr + */ +void SAL_CALL rtl_uString_newFromWStr( rtl_uString ** newStr, const wchar_t * value ) +{ + sal_Int32 length; + sal_Unicode* p; + + if (!value) + { + rtl_uString_new(newStr); + return; + } + + length = rtl_wstr_getLength(value); + + if (*newStr) + rtl_uString_release(*newStr); + + *newStr = (rtl_uString*)rtl_allocateMemory( sizeof(rtl_uString) + (length * sizeof(sal_Unicode)) ); + if ( *newStr ) + { + (*newStr)->refCount = 1; + (*newStr)->length = length; + + p = (*newStr)->buffer; + while ( length ) + { + *p = (sal_Unicode)(*value); + p++; + value++; + length--; + } + *p = 0; + } +} + +/************************************************************************* + * rtl_uString_newFromWStr_WithLength + */ +void SAL_CALL rtl_uString_newFromWStr_WithLength( rtl_uString ** newStr, + const wchar_t * value, + sal_Int32 len) +{ + sal_Unicode* p; + + if (!value || len < 0) + { + rtl_uString_new(newStr); + return; + } + + if (*newStr) + rtl_uString_release(*newStr); + + *newStr = (rtl_uString*)rtl_allocateMemory( sizeof(rtl_uString) + (len * sizeof(sal_Unicode)) ); + if ( *newStr ) + { + (*newStr)->refCount = 1; + (*newStr)->length = len; + + p = (*newStr)->buffer; + while ( len ) + { + *p = (sal_Unicode)(*value); + p++; + value++; + len--; + } + *p = 0; + } +} + +/************************************************************************* + * rtl_uString_newFromASCII + */ +void SAL_CALL rtl_uString_newFromAscii( rtl_uString ** newStr, const sal_Char * value ) +{ + sal_Int32 length; + sal_Unicode* p; + + if (!value) + { + rtl_uString_new(newStr); + return; + } + + length = rtl_str_getLength( value ); + + if (*newStr) + rtl_uString_release(*newStr); + + *newStr = (rtl_uString*)rtl_allocateMemory( sizeof(rtl_uString) + (length * sizeof(sal_Unicode)) ); + if ( *newStr ) + { + (*newStr)->refCount = 1; + (*newStr)->length = length; + + p = (*newStr)->buffer; + while ( length ) + { + /* Check ASCII range */ + OSL_ENSHURE( (*value & 0x80) == 0, "Found ASCII char > 127"); + + *p = (sal_Unicode)(*value); + p++; + value++; + length--; + } + *p = 0; + } +} + +/************************************************************************* + * rtl_uString_assign + */ +void SAL_CALL rtl_uString_assign( /*inout*/rtl_uString ** str, rtl_uString * rightValue ) +{ + rtl_uString_acquire(rightValue); + + if (*str) + rtl_uString_release(*str); + + *str = rightValue; +} + +/************************************************************************* + * rtl_uString_getLength + */ +sal_Int32 SAL_CALL rtl_uString_getLength( rtl_uString * str ) +{ + return str->length; +} + +/************************************************************************* + * rtl_uString_getStr + */ +sal_Unicode * SAL_CALL rtl_uString_getStr( rtl_uString * str ) +{ + return str->buffer; +} + +/************************************************************************* + * rtl_uString_newConcat + */ +void SAL_CALL rtl_uString_newConcat( rtl_uString ** newStr, rtl_uString * left, rtl_uString * right ) +{ + rtl_uString * pOrg = *newStr; + if( !right->length ) + { + *newStr = left; + rtl_uString_acquire( left ); + } + else if( !left->length ) + { + *newStr = right; + rtl_uString_acquire( right ); + } + else + { + rtl_uString *tmpStr = NULL; + sal_Int32 nNewLen = left->length + right->length; + rtl_uString_new_WithLength(&tmpStr, nNewLen); + tmpStr->length = nNewLen; + + rtl_copyMemory( tmpStr->buffer, left->buffer, left->length * sizeof(sal_Unicode)); + rtl_copyMemory( tmpStr->buffer + left->length, right->buffer, right->length * sizeof(sal_Unicode)); + tmpStr->buffer[nNewLen] = 0; + *newStr = tmpStr; + } + /* must be done at least, if left == *newStr */ + if( pOrg ) + rtl_uString_release(pOrg); +} + +/************************************************************************* + * rtl_uString_newReplace + */ +void SAL_CALL rtl_uString_newReplace( rtl_uString ** newStr, + rtl_uString * str, + sal_Unicode oldChar, + sal_Unicode newChar) +{ + rtl_uString_newFromString(newStr, str); + + if (!(*newStr)) return; + + rtl_ustr_replaceChar_WithLength((*newStr)->buffer, (*newStr)->length, oldChar, newChar); +} + +/************************************************************************* + * rtl_uString_newReplaceStrAt + */ +void SAL_CALL rtl_uString_newReplaceStrAt( rtl_uString ** newStr, + rtl_uString * str, + sal_Int32 index, + sal_Int32 count, + rtl_uString * newSub) +{ + sal_Unicode *pBuffer = NULL; + sal_Int32 size = sizeof(sal_Unicode); + + if (count == 0 && newSub->length == 0) + { + rtl_uString_newFromString(newStr, str); + return; + } + + if (count == 0) + { + rtl_uString_new_WithLength(newStr, str->length + newSub->length); + if (!(*newStr)) return; + pBuffer = (*newStr)->buffer; + (*newStr)->length = str->length + newSub->length; + rtl_copyMemory(pBuffer, str->buffer, index * size); + rtl_copyMemory(pBuffer + index, newSub->buffer, newSub->length * size); + rtl_copyMemory(pBuffer + index + newSub->length, str->buffer + index, + (str->length - index) * size); + + return; + } + + if (newSub->length == 0) + { + rtl_uString_new_WithLength(newStr, str->length - count); + if (!(*newStr)) return; + pBuffer = (*newStr)->buffer; + (*newStr)->length = str->length - count; + rtl_copyMemory(pBuffer, str->buffer, index * size); + rtl_copyMemory(pBuffer + index, str->buffer + index + count, + (str->length - index - count) * size); + + return; + } + + rtl_uString_new_WithLength(newStr, str->length - count + newSub->length); + if (!(*newStr)) return; + pBuffer = (*newStr)->buffer; + (*newStr)->length = str->length - count + newSub->length; + rtl_copyMemory(pBuffer, str->buffer, index * size); + rtl_copyMemory(pBuffer + index, newSub->buffer, newSub->length * size); + rtl_copyMemory(pBuffer + index + newSub->length, str->buffer + index + count, + (str->length - index - count) * size); + + return; +} + +/************************************************************************* + * rtl_uString_newToLowerCase + */ +void SAL_CALL rtl_uString_newToLowerCase( rtl_uString ** newStr, + rtl_uString * str, + struct _rtl_Locale * locale ) +{ + if (locale && locale->Language->length == 2 + && locale->Language->buffer[0] == 't' + && locale->Language->buffer[0] == 'r' ) + { + sal_Int32 i; + sal_Int32 len = str->length; + + rtl_uString_new_WithLength(newStr, str->length); + + if (!(*newStr)) return; + + (*newStr)->length = str->length; + + /* special loop for Turkey */ + for (i = 0; i < len; i++) + { + sal_Unicode ch = str->buffer[i]; + if (ch == L'I') + { + (*newStr)->buffer[i] = (sal_Unicode)0x0131; /* dotless small i */ + continue; + } + if ((sal_uInt16)ch == 0x0130) /* dotted I */ + { + (*newStr)->buffer[i] = L'i'; /* dotted i */ + continue; + } + (*newStr)->buffer[i] = rtl_char_toLowerCase(ch); + } + } else + { + rtl_uString_newFromString(newStr, str); + + if (!(*newStr)) return; + + rtl_ustr_toAsciiLowerCase_WithLength((*newStr)->buffer, (*newStr)->length); + } +} + +/************************************************************************* + * rtl_uString_newToUpperCase + */ +void SAL_CALL rtl_uString_newToUpperCase( rtl_uString ** newStr, + rtl_uString * str, + struct _rtl_Locale * locale ) +{ + sal_Int32 i; + sal_Int32 len = str->length; + sal_Int32 resultOffset = 0; + sal_Unicode *result = (sal_Unicode*)rtl_allocateMemory((len + 1)* sizeof(sal_Unicode)); + + if (locale && locale->Language->length == 2 + && locale->Language->buffer[0] == 't' + && locale->Language->buffer[0] == 'r' ) + { + /* special loop for Turkey */ + for (i = 0; i < len; i++) + { + sal_Unicode ch = str->buffer[i]; + if ((sal_uInt16)ch == 0x0131) /* dotless i */ + { + result[i+resultOffset] = L'I'; /* cap I */ + continue; + } + if (ch == L'i') + { + result[i+resultOffset] = (sal_Unicode)0x0130; /* dotted cap i */ + continue; + } + if ((sal_uInt16)ch == 0x00DF) /* sharp s */ + { + /* Grow result. */ + sal_Unicode *result2 = (sal_Unicode*)rtl_allocateMemory((len + 1 + resultOffset + 1) * sizeof(sal_Unicode)); + rtl_copyMemory(result2, result, (i + resultOffset) * sizeof(sal_Unicode)); + result2[i+resultOffset] = L'S'; + resultOffset++; + result2[i+resultOffset] = L'S'; + rtl_freeMemory(result); + result = result2; + continue; + } + result[i+resultOffset] = rtl_char_toUpperCase(ch); + } + } else + { + for (i = 0; i < len; i++) + { + sal_Unicode ch = str->buffer[i]; + + if ((sal_uInt16)ch == 0x00DF) /* sharp s */ + { + /* Grow result. */ + sal_Unicode *result2 = (sal_Unicode*)rtl_allocateMemory((len + 1 + resultOffset + 1) * sizeof(sal_Unicode)); + rtl_copyMemory(result2, result, (i + resultOffset) * sizeof(sal_Unicode)); + result2[i+resultOffset] = L'S'; + resultOffset++; + result2[i+resultOffset] = L'S'; + rtl_freeMemory(result); + result = result2; + continue; + } + result[i+resultOffset] = rtl_char_toUpperCase(ch); + } + + } + + result[len + resultOffset] = L'\0'; + rtl_uString_newFromStr_WithLength(newStr, result, len + resultOffset); + rtl_freeMemory(result); +} + +/************************************************************************* + * rtl_uString_newTrim + */ +void SAL_CALL rtl_uString_newTrim( rtl_uString ** newStr, rtl_uString * str ) +{ + rtl_uString_newFromString(newStr, str); + + if (!(*newStr)) return; + + (*newStr)->length = rtl_ustr_trim_WithLength((*newStr)->buffer, (*newStr)->length); +} + +/************************************************************************* + * rtl_uString_getTokenCount + */ +sal_Int32 SAL_CALL rtl_uString_getTokenCount( rtl_uString * str , sal_Unicode cTok) +{ + sal_Int32 count = 0; + sal_Int32 index1 = 0; + sal_Int32 index2 = 0; + sal_Unicode * buffer = str->buffer; + + while ((index2 = rtl_ustr_indexOfChar_WithLength(buffer + index1, str->length - index1, cTok)) >= 0) + { + if (index2 <= (str->length - index1) && index2 >= 0) + count++; + + index1 += index2 + 1; + } + + if (index2 < 0 && index1 <= str->length) + count++; + + return count; +} + +/************************************************************************* + * rtl_uString_getToken + */ +void SAL_CALL rtl_uString_getToken( rtl_uString ** newStr , rtl_uString * str, sal_Int32 nToken, sal_Unicode cTok) +{ + sal_Int32 count = 0; + sal_Int32 index1 = 0; + sal_Int32 index2 = 0; + sal_Unicode * buffer = str->buffer; + + while ((index2 = rtl_ustr_indexOfChar_WithLength(buffer + index1, str->length - index1, cTok)) >= 0) + { + if (count == nToken) + { + rtl_uString_newFromStr_WithLength( newStr, buffer + index1, index2 ); + return; + } + + if (index2 <= (str->length - index1) && index2 >= 0) + count++; + + index1 += index2 + 1; + } + + if (nToken == 0 || nToken == count) + rtl_uString_newFromStr_WithLength( newStr, buffer + index1, str->length - index1 ); + else + rtl_uString_new(newStr); + + return; +} + + +void SAL_CALL rtl_string2UString( rtl_uString** newStr, const sal_Char* pStr, sal_Int32 nLen, + rtl_TextEncoding encoding, sal_uInt32 nCvtFlags ) +{ + if (nLen) + { + sal_Unicode* pData; + rtl_TextEncodingInfo aTextEncInfo; + rtl_TextToUnicodeConverter hConverter = rtl_createTextToUnicodeConverter( encoding ); + sal_uInt32 nInfo; + sal_Size nSrcBytes; + sal_Size nDestChars; + sal_Size nNewLen; + + /* get TextEncodingInfo */ + aTextEncInfo.StructSize = sizeof( aTextEncInfo ); + rtl_getTextEncodingInfo( encoding, &aTextEncInfo ); + + /* Zuerst konvertieren wir mit der wahrscheinlichen Anzahl */ + /* der zu konvertierenden Zeichen */ + nNewLen = nLen; /* (aTextEncInfo.AverageCharSize ? aTextEncInfo.AverageCharSize : 1); */ + + pData = (sal_Unicode*)rtl_allocateMemory( (1 + nNewLen) * sizeof(sal_Unicode) ); + nDestChars = rtl_convertTextToUnicode( hConverter, 0, + pStr, nLen, + pData, nNewLen, + nCvtFlags, + &nInfo, &nSrcBytes ); + + /* String entsprechend der durch das Konvertieren tatsaechlich */ + /* entstehenden Bytes anpassen */ + pData[nDestChars] = L'\0'; + + rtl_uString_newFromStr_WithLength(newStr, pData, nDestChars); + + rtl_destroyTextToUnicodeConverter( hConverter ); + rtl_freeMemory( pData ); + } else + { + rtl_uString_new(newStr); + } +} + +void SAL_CALL rtl_uString2String( rtl_String** newStr, const sal_Unicode * pWStr, sal_Int32 nWLen, + rtl_TextEncoding encoding, sal_uInt32 nCvtFlags ) +{ + if (nWLen) + { + sal_Char* pData; + rtl_TextEncodingInfo aTextEncInfo; + rtl_UnicodeToTextConverter hConverter = rtl_createUnicodeToTextConverter( encoding ); + sal_Size nSrcChars; + sal_Size nDestBytes; + sal_Size nNewLen; + sal_uInt32 nInfo; + + /* get TextEncodingInfo */ + aTextEncInfo.StructSize = sizeof( aTextEncInfo ); + rtl_getTextEncodingInfo( encoding, &aTextEncInfo ); + + /* Zuerst konvertieren wir mit der wahrscheinlichen Anzahl */ + /* der zu konvertierenden Zeichen */ + nNewLen = nWLen * (aTextEncInfo.AverageCharSize ? aTextEncInfo.AverageCharSize : 1) * 6; + + pData = (sal_Char*)rtl_allocateMemory( nNewLen ); + nDestBytes = rtl_convertUnicodeToText( hConverter, 0, + pWStr, nWLen, + pData, nNewLen, + nCvtFlags, + &nInfo, &nSrcChars ); + pData[nDestBytes] = '\0'; + + rtl_string_newFromStr_WithLength(newStr, pData, nDestBytes); + + rtl_destroyUnicodeToTextConverter( hConverter ); + rtl_freeMemory( pData ); + } else + { + rtl_string_new(newStr); + } +} + + |