/* $Id: kLdrHlpStr.c 2955 2007-02-07 07:07:16Z bird $ */ /** @file * * kLdr - The Dynamic Loader, String Helper Functions. * * Copyright (c) 2006 knut st. osmundsen * * * This file is part of kLdr. * * kLdr is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * kLdr 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with kLdr; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */ /******************************************************************************* * Header Files * *******************************************************************************/ #include #include "kLdrHlp.h" /** * Converts an signed integer to an ascii string. * * @returns psz. * @param psz Pointer to the output buffer. * @param cch The size of the output buffer. * @param lVal The value. * @param iBase The base to format it. (2,8,10 or 16) */ char *kldrHlpInt2Ascii(char *psz, size_t cch, long lVal, unsigned iBase) { static const char s_szDigits[] = "0123456789abcdefghijklmnopqrstuvwxyz"; char *pszRet = psz; if (cch >= (lVal < 0 ? 3U : 2U) && psz) { /* prefix */ if (lVal < 0) { *psz++ = '-'; cch--; lVal = -lVal; } /* the digits */ do { *psz++ = s_szDigits[lVal % iBase]; cch--; lVal /= iBase; } while (lVal && cch > 1); /* overflow indicator */ if (lVal) psz[-1] = '+'; } else if (!pszRet) return pszRet; else if (cch < 1 || !pszRet) return pszRet; else *psz++ = '+'; *psz = '\0'; return pszRet; } size_t kLdrHlpStrNLen(const char *psz, size_t cchMax) { const char * const pszStart = psz; while (*psz && cchMax--) psz++; return psz - pszStart; } int kLdrHlpMemIComp(const void *pv1, const void *pv2, size_t cb) { const uint8_t *pb1 = (const uint8_t *)pv1; const uint8_t *pb2 = (const uint8_t *)pv2; while (cb-- > 0) { if (*pb1 != *pb2) { const uint8_t u1 = *pb1 >= 'a' && *pb1 <= 'z' ? *pb1 - 'a' : *pb1; const uint8_t u2 = *pb2 >= 'a' && *pb2 <= 'z' ? *pb2 - 'a' : *pb2; if (u1 != u2) return (int)*pb1 - (int)*pb2; } pb1++; pb2++; } return 0; } int kLdrHlpStrIComp(const char *pv1, const char *pv2) { const uint8_t *pb1 = (const uint8_t *)pv1; const uint8_t *pb2 = (const uint8_t *)pv2; for (;;) { if (*pb1 != *pb2) { const uint8_t u1 = *pb1 >= 'a' && *pb1 <= 'z' ? *pb1 - 'a' : *pb1; const uint8_t u2 = *pb2 >= 'a' && *pb2 <= 'z' ? *pb2 - 'a' : *pb2; if (u1 != u2) return (int)*pb1 - (int)*pb2; } if (!*pb1) break; pb1++; pb2++; } return 0; } #ifdef kLdrHlpStrChr_needed char *kLdrHlpStrChr(const char *psz, int ch) { while (*psz) { if (*psz == ch) return (char *)psz; psz++; } return NULL; } #endif #ifdef kLdrHlpMemChr_needed void *kLdrHlpMemChr(const void *pv, int ch, size_t cb) { const uint8_t *pb = (const uint8_t *)pv; const uint8_t b = (uint8_t)ch; while (cb-- > 0) { if (*pb == b) return (void *)pb; pb++; } return NULL; } #endif #ifdef kLdrHlpMemMove_needed void *kLdrHlpMemMove(void *pv1, const void *pv2, size_t cb) { uint8_t *pbDst = (uint8_t *)pv1; const uint8_t *pbSrc = (const uint8_t *)pv2; while (cb-- > 0) { const uint8_t b = *pbSrc++; *pbDst++ = b; } return pv1; } #endif #ifdef kLdrHlpStrNComp_needed int kLdrHlpStrNComp(const char *psz1, const char *psz2, size_t cch) { while (cch-- > 0) { if (*psz1 != *psz2) return (int)*psz1 - (int)*psz2; if (!*psz1) break; psz1++; psz2++; } return 0; } #endif