| 1 | /* $Id: kLdrHlpStr.c 2955 2007-02-07 07:07:16Z bird $ */
 | 
|---|
| 2 | /** @file
 | 
|---|
| 3 |  *
 | 
|---|
| 4 |  * kLdr - The Dynamic Loader, String Helper Functions.
 | 
|---|
| 5 |  *
 | 
|---|
| 6 |  * Copyright (c) 2006 knut st. osmundsen <bird-kbuild-src@anduin.net>
 | 
|---|
| 7 |  *
 | 
|---|
| 8 |  *
 | 
|---|
| 9 |  * This file is part of kLdr.
 | 
|---|
| 10 |  *
 | 
|---|
| 11 |  * kLdr is free software; you can redistribute it and/or modify
 | 
|---|
| 12 |  * it under the terms of the GNU General Public License as published by
 | 
|---|
| 13 |  * the Free Software Foundation; either version 2 of the License, or
 | 
|---|
| 14 |  * (at your option) any later version.
 | 
|---|
| 15 |  *
 | 
|---|
| 16 |  * kLdr is distributed in the hope that it will be useful,
 | 
|---|
| 17 |  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
|---|
| 18 |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
|---|
| 19 |  * GNU General Public License for more details.
 | 
|---|
| 20 |  *
 | 
|---|
| 21 |  * You should have received a copy of the GNU General Public License
 | 
|---|
| 22 |  * along with kLdr; if not, write to the Free Software
 | 
|---|
| 23 |  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 | 
|---|
| 24 |  *
 | 
|---|
| 25 |  */
 | 
|---|
| 26 | 
 | 
|---|
| 27 | 
 | 
|---|
| 28 | /*******************************************************************************
 | 
|---|
| 29 | *   Header Files                                                               *
 | 
|---|
| 30 | *******************************************************************************/
 | 
|---|
| 31 | #include <kLdr.h>
 | 
|---|
| 32 | #include "kLdrHlp.h"
 | 
|---|
| 33 | 
 | 
|---|
| 34 | 
 | 
|---|
| 35 | /**
 | 
|---|
| 36 |  * Converts an signed integer to an ascii string.
 | 
|---|
| 37 |  *
 | 
|---|
| 38 |  * @returns psz.
 | 
|---|
| 39 |  * @param   psz         Pointer to the output buffer.
 | 
|---|
| 40 |  * @param   cch         The size of the output buffer.
 | 
|---|
| 41 |  * @param   lVal        The value.
 | 
|---|
| 42 |  * @param   iBase       The base to format it. (2,8,10 or 16)
 | 
|---|
| 43 |  */
 | 
|---|
| 44 | char *kldrHlpInt2Ascii(char *psz, size_t cch, long lVal, unsigned iBase)
 | 
|---|
| 45 | {
 | 
|---|
| 46 |     static const char s_szDigits[] = "0123456789abcdefghijklmnopqrstuvwxyz";
 | 
|---|
| 47 |     char *pszRet = psz;
 | 
|---|
| 48 | 
 | 
|---|
| 49 |     if (cch >= (lVal < 0 ? 3U : 2U) && psz)
 | 
|---|
| 50 |     {
 | 
|---|
| 51 |         /* prefix */
 | 
|---|
| 52 |         if (lVal < 0)
 | 
|---|
| 53 |         {
 | 
|---|
| 54 |             *psz++ = '-';
 | 
|---|
| 55 |             cch--;
 | 
|---|
| 56 |             lVal = -lVal;
 | 
|---|
| 57 |         }
 | 
|---|
| 58 | 
 | 
|---|
| 59 |         /* the digits */
 | 
|---|
| 60 |         do
 | 
|---|
| 61 |         {
 | 
|---|
| 62 |             *psz++ = s_szDigits[lVal % iBase];
 | 
|---|
| 63 |             cch--;
 | 
|---|
| 64 |             lVal /= iBase;
 | 
|---|
| 65 |         } while (lVal && cch > 1);
 | 
|---|
| 66 | 
 | 
|---|
| 67 |         /* overflow indicator */
 | 
|---|
| 68 |         if (lVal)
 | 
|---|
| 69 |             psz[-1] = '+';
 | 
|---|
| 70 |     }
 | 
|---|
| 71 |     else if (!pszRet)
 | 
|---|
| 72 |         return pszRet;
 | 
|---|
| 73 |     else if (cch < 1 || !pszRet)
 | 
|---|
| 74 |         return pszRet;
 | 
|---|
| 75 |     else
 | 
|---|
| 76 |         *psz++ = '+';
 | 
|---|
| 77 |     *psz = '\0';
 | 
|---|
| 78 | 
 | 
|---|
| 79 |     return pszRet;
 | 
|---|
| 80 | }
 | 
|---|
| 81 | 
 | 
|---|
| 82 | 
 | 
|---|
| 83 | size_t  kLdrHlpStrNLen(const char *psz, size_t cchMax)
 | 
|---|
| 84 | {
 | 
|---|
| 85 |     const char * const pszStart = psz;
 | 
|---|
| 86 |     while (*psz && cchMax--)
 | 
|---|
| 87 |         psz++;
 | 
|---|
| 88 |     return psz - pszStart;
 | 
|---|
| 89 | }
 | 
|---|
| 90 | 
 | 
|---|
| 91 | 
 | 
|---|
| 92 | int     kLdrHlpMemIComp(const void *pv1, const void *pv2, size_t cb)
 | 
|---|
| 93 | {
 | 
|---|
| 94 |     const uint8_t *pb1 = (const uint8_t *)pv1;
 | 
|---|
| 95 |     const uint8_t *pb2 = (const uint8_t *)pv2;
 | 
|---|
| 96 |     while (cb-- > 0)
 | 
|---|
| 97 |     {
 | 
|---|
| 98 |         if (*pb1 != *pb2)
 | 
|---|
| 99 |         {
 | 
|---|
| 100 |             const uint8_t u1 = *pb1 >= 'a' && *pb1 <= 'z' ? *pb1 - 'a' : *pb1;
 | 
|---|
| 101 |             const uint8_t u2 = *pb2 >= 'a' && *pb2 <= 'z' ? *pb2 - 'a' : *pb2;
 | 
|---|
| 102 |             if (u1 != u2)
 | 
|---|
| 103 |                 return (int)*pb1 - (int)*pb2;
 | 
|---|
| 104 |         }
 | 
|---|
| 105 |         pb1++;
 | 
|---|
| 106 |         pb2++;
 | 
|---|
| 107 |     }
 | 
|---|
| 108 |     return 0;
 | 
|---|
| 109 | }
 | 
|---|
| 110 | 
 | 
|---|
| 111 | 
 | 
|---|
| 112 | int     kLdrHlpStrIComp(const char *pv1, const char *pv2)
 | 
|---|
| 113 | {
 | 
|---|
| 114 |     const uint8_t *pb1 = (const uint8_t *)pv1;
 | 
|---|
| 115 |     const uint8_t *pb2 = (const uint8_t *)pv2;
 | 
|---|
| 116 |     for (;;)
 | 
|---|
| 117 |     {
 | 
|---|
| 118 |         if (*pb1 != *pb2)
 | 
|---|
| 119 |         {
 | 
|---|
| 120 |             const uint8_t u1 = *pb1 >= 'a' && *pb1 <= 'z' ? *pb1 - 'a' : *pb1;
 | 
|---|
| 121 |             const uint8_t u2 = *pb2 >= 'a' && *pb2 <= 'z' ? *pb2 - 'a' : *pb2;
 | 
|---|
| 122 |             if (u1 != u2)
 | 
|---|
| 123 |                 return (int)*pb1 - (int)*pb2;
 | 
|---|
| 124 |         }
 | 
|---|
| 125 |         if (!*pb1)
 | 
|---|
| 126 |             break;
 | 
|---|
| 127 |         pb1++;
 | 
|---|
| 128 |         pb2++;
 | 
|---|
| 129 |     }
 | 
|---|
| 130 |     return 0;
 | 
|---|
| 131 | }
 | 
|---|
| 132 | 
 | 
|---|
| 133 | 
 | 
|---|
| 134 | #ifdef kLdrHlpStrChr_needed
 | 
|---|
| 135 | char   *kLdrHlpStrChr(const char *psz, int ch)
 | 
|---|
| 136 | {
 | 
|---|
| 137 |     while (*psz)
 | 
|---|
| 138 |     {
 | 
|---|
| 139 |         if (*psz == ch)
 | 
|---|
| 140 |             return (char *)psz;
 | 
|---|
| 141 |         psz++;
 | 
|---|
| 142 |     }
 | 
|---|
| 143 |     return NULL;
 | 
|---|
| 144 | }
 | 
|---|
| 145 | #endif
 | 
|---|
| 146 | 
 | 
|---|
| 147 | 
 | 
|---|
| 148 | #ifdef kLdrHlpMemChr_needed
 | 
|---|
| 149 | void   *kLdrHlpMemChr(const void *pv, int ch, size_t cb)
 | 
|---|
| 150 | {
 | 
|---|
| 151 |     const uint8_t *pb = (const uint8_t *)pv;
 | 
|---|
| 152 |     const uint8_t  b = (uint8_t)ch;
 | 
|---|
| 153 |     while (cb-- > 0)
 | 
|---|
| 154 |     {
 | 
|---|
| 155 |         if (*pb == b)
 | 
|---|
| 156 |             return (void *)pb;
 | 
|---|
| 157 |         pb++;
 | 
|---|
| 158 |     }
 | 
|---|
| 159 |     return NULL;
 | 
|---|
| 160 | }
 | 
|---|
| 161 | #endif
 | 
|---|
| 162 | 
 | 
|---|
| 163 | 
 | 
|---|
| 164 | #ifdef kLdrHlpMemMove_needed
 | 
|---|
| 165 | void   *kLdrHlpMemMove(void *pv1, const void *pv2, size_t cb)
 | 
|---|
| 166 | {
 | 
|---|
| 167 |     uint8_t        *pbDst = (uint8_t *)pv1;
 | 
|---|
| 168 |     const uint8_t  *pbSrc = (const uint8_t *)pv2;
 | 
|---|
| 169 |     while (cb-- > 0)
 | 
|---|
| 170 |     {
 | 
|---|
| 171 |         const uint8_t b = *pbSrc++;
 | 
|---|
| 172 |         *pbDst++ = b;
 | 
|---|
| 173 |     }
 | 
|---|
| 174 |     return pv1;
 | 
|---|
| 175 | }
 | 
|---|
| 176 | #endif
 | 
|---|
| 177 | 
 | 
|---|
| 178 | 
 | 
|---|
| 179 | #ifdef kLdrHlpStrNComp_needed
 | 
|---|
| 180 | int     kLdrHlpStrNComp(const char *psz1, const char *psz2, size_t cch)
 | 
|---|
| 181 | {
 | 
|---|
| 182 |     while (cch-- > 0)
 | 
|---|
| 183 |     {
 | 
|---|
| 184 |         if (*psz1 != *psz2)
 | 
|---|
| 185 |             return (int)*psz1 - (int)*psz2;
 | 
|---|
| 186 |         if (!*psz1)
 | 
|---|
| 187 |             break;
 | 
|---|
| 188 |         psz1++;
 | 
|---|
| 189 |         psz2++;
 | 
|---|
| 190 |     }
 | 
|---|
| 191 |     return 0;
 | 
|---|
| 192 | }
 | 
|---|
| 193 | #endif 
 | 
|---|
| 194 | 
 | 
|---|