13c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/*------------------------------------------------------------------------- 23c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * drawElements Base Portability Library 33c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * ------------------------------------- 43c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * 53c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Copyright 2014 The Android Open Source Project 63c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * 73c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Licensed under the Apache License, Version 2.0 (the "License"); 83c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * you may not use this file except in compliance with the License. 93c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * You may obtain a copy of the License at 103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * 113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * http://www.apache.org/licenses/LICENSE-2.0 123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * 133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Unless required by applicable law or agreed to in writing, software 143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * distributed under the License is distributed on an "AS IS" BASIS, 153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * See the License for the specific language governing permissions and 173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * limitations under the License. 183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * 193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *//*! 203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \file 213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \brief Basic string operations. 223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *//*--------------------------------------------------------------------*/ 233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "deString.h" 253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include <string.h> 273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include <stdlib.h> 283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include <stdio.h> 303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include <stdarg.h> 313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 323c827367444ee418f129b2c238299f49d3264554Jarkko PoyryDE_BEGIN_EXTERN_C 333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/*--------------------------------------------------------------------*//*! 353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \brief Compute hash from string. 363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \param str String to compute hash value for. 373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \return Computed hash value. 383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *//*--------------------------------------------------------------------*/ 393c827367444ee418f129b2c238299f49d3264554Jarkko PoyrydeUint32 deStringHash (const char* str) 403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{ 413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry /* \note [pyry] This hash is used in DT_GNU_HASH and is proven 423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry to be robust for symbol hashing. */ 433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry /* \see http://sources.redhat.com/ml/binutils/2006-06/msg00418.html */ 443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry deUint32 hash = 5381; 453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry unsigned int c; 463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry DE_ASSERT(str); 483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry while ((c = (unsigned int)*str++) != 0) 493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry hash = (hash << 5) + hash + c; 503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry return hash; 523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} 533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 543c827367444ee418f129b2c238299f49d3264554Jarkko PoyrydeUint32 deStringHashLeading (const char* str, int numLeadingChars) 553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{ 563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry deUint32 hash = 5381; 573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry unsigned int c; 583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 593c827367444ee418f129b2c238299f49d3264554Jarkko Poyry DE_ASSERT(str); 603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry while (numLeadingChars-- && (c = (unsigned int)*str++) != 0) 613c827367444ee418f129b2c238299f49d3264554Jarkko Poyry hash = (hash << 5) + hash + c; 623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 633c827367444ee418f129b2c238299f49d3264554Jarkko Poyry return hash; 643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} 653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 668eb43bbbb25645f6765f79608da7fea6f2e9d86cJarkko PöyrydeUint32 deMemoryHash (const void* ptr, size_t numBytes) 673c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{ 683c827367444ee418f129b2c238299f49d3264554Jarkko Poyry /* \todo [2010-05-10 pyry] Better generic hash function? */ 693c827367444ee418f129b2c238299f49d3264554Jarkko Poyry const deUint8* input = (const deUint8*)ptr; 703c827367444ee418f129b2c238299f49d3264554Jarkko Poyry deUint32 hash = 5381; 713c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry DE_ASSERT(ptr); 733c827367444ee418f129b2c238299f49d3264554Jarkko Poyry while (numBytes--) 743c827367444ee418f129b2c238299f49d3264554Jarkko Poyry hash = (hash << 5) + hash + *input++; 753c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 763c827367444ee418f129b2c238299f49d3264554Jarkko Poyry return hash; 773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} 783c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 798eb43bbbb25645f6765f79608da7fea6f2e9d86cJarkko PöyrydeBool deMemoryEqual (const void* ptr, const void* cmp, size_t numBytes) 803c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{ 813c827367444ee418f129b2c238299f49d3264554Jarkko Poyry return memcmp(ptr, cmp, numBytes) == 0; 823c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} 833c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 843c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/*--------------------------------------------------------------------*//*! 853c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \brief Compare two strings for equality. 863c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \param a First string. 873c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \param b Second string. 883c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \return True if strings equal, false otherwise. 893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *//*--------------------------------------------------------------------*/ 903c827367444ee418f129b2c238299f49d3264554Jarkko PoyrydeBool deStringEqual (const char* a, const char* b) 913c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{ 923c827367444ee418f129b2c238299f49d3264554Jarkko Poyry DE_ASSERT(a && b); 933c827367444ee418f129b2c238299f49d3264554Jarkko Poyry return (strcmp(a, b) == 0); 943c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} 953c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 963c827367444ee418f129b2c238299f49d3264554Jarkko PoyrydeBool deStringBeginsWith (const char* str, const char* lead) 973c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{ 983c827367444ee418f129b2c238299f49d3264554Jarkko Poyry const char* a = str; 993c827367444ee418f129b2c238299f49d3264554Jarkko Poyry const char* b = lead; 1003c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 1013c827367444ee418f129b2c238299f49d3264554Jarkko Poyry while (*b) 1023c827367444ee418f129b2c238299f49d3264554Jarkko Poyry { 1033c827367444ee418f129b2c238299f49d3264554Jarkko Poyry if (*a++ != *b++) 1043c827367444ee418f129b2c238299f49d3264554Jarkko Poyry return DE_FALSE; 1053c827367444ee418f129b2c238299f49d3264554Jarkko Poyry } 1063c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 1073c827367444ee418f129b2c238299f49d3264554Jarkko Poyry return DE_TRUE; 1083c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} 1093c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 1103c827367444ee418f129b2c238299f49d3264554Jarkko Poyryint deVsprintf (char* string, size_t size, const char* format, va_list list) 1113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{ 1123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry int res; 1133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 1143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry DE_ASSERT(string && format); 1153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 1163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#if (DE_COMPILER == DE_COMPILER_MSC) 1173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry# if (DE_OS == DE_OS_WINCE) 1183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry res = _vsnprintf(string, size, format, list); 1193fdee359c9eee4d6c1d823b23f7f64631b5945f8Jarkko Pöyry# else 1203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry res = vsnprintf_s(string, size, _TRUNCATE, format, list); 1213fdee359c9eee4d6c1d823b23f7f64631b5945f8Jarkko Pöyry# endif 1223fdee359c9eee4d6c1d823b23f7f64631b5945f8Jarkko Pöyry#else 1233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry res = vsnprintf(string, size, format, list); 1243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#endif 1253fdee359c9eee4d6c1d823b23f7f64631b5945f8Jarkko Pöyry 1263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry return res; 1273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} 1283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 1293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/*--------------------------------------------------------------------*//*! 130653ad0e8a4209754304cbd5b5ceb4fdc7b29c01aPyry Haulos * \brief Safe string print 1313fdee359c9eee4d6c1d823b23f7f64631b5945f8Jarkko Pöyry * \note This has the new safe signature, i.e., string length is a 1323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * required parameter. 1333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *//*--------------------------------------------------------------------*/ 1343c827367444ee418f129b2c238299f49d3264554Jarkko Poyryint deSprintf (char* string, size_t size, const char* format, ...) 1353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{ 1363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry va_list list; 1373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry int res; 1383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 1393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry DE_ASSERT(string && format); 1403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 1413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry va_start(list, format); 1423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 1433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry res = deVsprintf(string, size, format, list); 1443fdee359c9eee4d6c1d823b23f7f64631b5945f8Jarkko Pöyry 1453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry va_end(list); 1463fdee359c9eee4d6c1d823b23f7f64631b5945f8Jarkko Pöyry 1473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry return res; 1483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} 1493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 1503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/*--------------------------------------------------------------------*//*! 1513fdee359c9eee4d6c1d823b23f7f64631b5945f8Jarkko Pöyry * \note This has the new safe signature, i.e., string length is a 1523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * required parameter. 1533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *//*--------------------------------------------------------------------*/ 1543c827367444ee418f129b2c238299f49d3264554Jarkko Poyrychar* deStrcpy (char* dst, size_t size, const char* src) 1553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{ 1563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#if (DE_COMPILER == DE_COMPILER_MSC) && (DE_OS != DE_OS_WINCE) 1573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry (void)strcpy_s(dst, size, src); 1583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry return dst; 1593c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#else 1603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry return strncpy(dst, src, size); 1613c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#endif 1623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} 1633c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 1643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/*--------------------------------------------------------------------*//*! 1653fdee359c9eee4d6c1d823b23f7f64631b5945f8Jarkko Pöyry * \note This has the new safe signature, i.e., string length is a 1663c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * required parameter. 1673c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *//*--------------------------------------------------------------------*/ 1683fdee359c9eee4d6c1d823b23f7f64631b5945f8Jarkko Pöyrychar* deStrcat (char* s1, size_t size, const char* s2) 1693c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{ 1703c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#if (DE_COMPILER == DE_COMPILER_MSC) && (DE_OS != DE_OS_WINCE) 1713c827367444ee418f129b2c238299f49d3264554Jarkko Poyry (void)strcat_s(s1, size, s2); 1723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry return s1; 1733c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#else 1743c827367444ee418f129b2c238299f49d3264554Jarkko Poyry return strncat(s1, s2, size); 1753c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#endif 1763c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} 1773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry 17833878d45346dddf85669f610783939cad0d32817Maciej Jesionowskisize_t deStrnlen (const char* string, size_t maxSize) 17933878d45346dddf85669f610783939cad0d32817Maciej Jesionowski{ 18049306465b5a5312058e9ae52b85fafc42e14f0c6Elliott Hughes#if ((DE_COMPILER == DE_COMPILER_MSC) && (DE_OS != DE_OS_WINCE)) 18133878d45346dddf85669f610783939cad0d32817Maciej Jesionowski return strnlen_s(string, maxSize); 18233878d45346dddf85669f610783939cad0d32817Maciej Jesionowski#else 183febb0bbd97bc3004df7e81f90e9a5ddb49504aacPyry Haulos size_t len = 0; 184038db60bcbb1cdaf0157e5176af6422bda13ff28Pyry Haulos while (len < maxSize && string[len] != 0) 185febb0bbd97bc3004df7e81f90e9a5ddb49504aacPyry Haulos ++len; 186febb0bbd97bc3004df7e81f90e9a5ddb49504aacPyry Haulos return len; 18733878d45346dddf85669f610783939cad0d32817Maciej Jesionowski#endif 18833878d45346dddf85669f610783939cad0d32817Maciej Jesionowski} 18933878d45346dddf85669f610783939cad0d32817Maciej Jesionowski 1903c827367444ee418f129b2c238299f49d3264554Jarkko PoyryDE_END_EXTERN_C 191