[3745] | 1 | /**************************************************************************
|
---|
| 2 | *
|
---|
| 3 | * local_strings -- provides some extra string routines
|
---|
| 4 | * Copyright (C) 1994 Authors
|
---|
| 5 | *
|
---|
| 6 | * This program is free software; you can redistribute it and/or modify
|
---|
| 7 | * it under the terms of the GNU General Public License as published by
|
---|
| 8 | * the Free Software Foundation; either version 2 of the License, or
|
---|
| 9 | * (at your option) any later version.
|
---|
| 10 | *
|
---|
| 11 | * This program is distributed in the hope that it will be useful,
|
---|
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 14 | * GNU General Public License for more details.
|
---|
| 15 | *
|
---|
| 16 | * You should have received a copy of the GNU General Public License
|
---|
| 17 | * along with this program; if not, write to the Free Software
|
---|
| 18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
| 19 | *
|
---|
| 20 | * $Id: local_strings.c 3745 2003-02-20 21:20:24Z mdewsnip $
|
---|
| 21 | *
|
---|
| 22 | **************************************************************************/
|
---|
| 23 |
|
---|
| 24 | static char *RCSID = "$Id: local_strings.c 3745 2003-02-20 21:20:24Z mdewsnip $";
|
---|
| 25 |
|
---|
| 26 | #include "sysfuncs.h"
|
---|
| 27 |
|
---|
| 28 | #include "local_strings.h"
|
---|
| 29 | #include "memlib.h"
|
---|
| 30 |
|
---|
| 31 | /* =========================================================================
|
---|
| 32 | * Function: arg_atoi
|
---|
| 33 | * Description:
|
---|
| 34 | * Converts an ascii string to an integer
|
---|
| 35 | * but allows a scale string immediately following.
|
---|
| 36 | * i.e. "10Mb" or "20Kb" or "40Megabytes"
|
---|
| 37 | * There must be no gap between the last digit and the scale.
|
---|
| 38 | * Only the fist letter is significant.
|
---|
| 39 | * K = Kilo
|
---|
| 40 | * M = Mega
|
---|
| 41 | * G = Giga
|
---|
| 42 | * Input:
|
---|
| 43 | * string of form: xxxxxxK or xxxxxxxM or xxxxxxxG
|
---|
| 44 | * where x is a digit
|
---|
| 45 | * Output:
|
---|
| 46 | * a scaled integer
|
---|
| 47 | * ========================================================================= */
|
---|
| 48 |
|
---|
| 49 | #define KILO 1024
|
---|
| 50 | #define MEGA 1024*1024
|
---|
| 51 | #define GIGA 1024*1024*1024
|
---|
| 52 |
|
---|
| 53 | int
|
---|
| 54 | arg_atoi (char *str)
|
---|
| 55 | {
|
---|
| 56 | char *str_ptr = str;
|
---|
| 57 | char ch = '\0';
|
---|
| 58 | long scale = 1;
|
---|
| 59 |
|
---|
| 60 | /* find any scale letter immediately after digits */
|
---|
| 61 | ch = *str_ptr;
|
---|
| 62 | while (!isalpha (ch) && (ch != '\0'))
|
---|
| 63 | ch = *str_ptr++;
|
---|
| 64 |
|
---|
| 65 | /* replace scale letter by end-of-string character */
|
---|
| 66 | *str_ptr = '\0';
|
---|
| 67 |
|
---|
| 68 | /* set scale appropriately */
|
---|
| 69 | switch (ch)
|
---|
| 70 | {
|
---|
| 71 | case 'K':
|
---|
| 72 | scale = KILO;
|
---|
| 73 | break;
|
---|
| 74 | case 'M':
|
---|
| 75 | scale = MEGA;
|
---|
| 76 | break;
|
---|
| 77 | case 'G':
|
---|
| 78 | scale = GIGA;
|
---|
| 79 | break;
|
---|
| 80 | default:
|
---|
| 81 | scale = 1;
|
---|
| 82 | break;
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | return (atoi (str) * scale);
|
---|
| 86 |
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | /* ===============================================================================
|
---|
| 90 | * Function: compare
|
---|
| 91 | * Description: Compare two strings.
|
---|
| 92 | * Input: Two binary strings, first byte of string indicates length.
|
---|
| 93 | * Output: Like standard strcmp.
|
---|
| 94 | * = 0 if s1 = s2
|
---|
| 95 | * < 0 if s1 < s2
|
---|
| 96 | * > 0 if s1 > s2
|
---|
| 97 | * =============================================================================== */
|
---|
| 98 | int
|
---|
| 99 | compare (u_char * s1, u_char * s2)
|
---|
| 100 | {
|
---|
| 101 | int l1 = *s1, l2 = *s2;
|
---|
| 102 | int l = (l1 < l2) ? l1 : l2;
|
---|
| 103 |
|
---|
| 104 | while (l--)
|
---|
| 105 | if (*++s1 != *++s2)
|
---|
| 106 | return (*s1 - *s2);
|
---|
| 107 | return (l1 - l2);
|
---|
| 108 |
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | /*
|
---|
| 112 | * This array is designed for mapping upper and lower case letter
|
---|
| 113 | * together for a case independent comparison. The mappings are
|
---|
| 114 | * based upon ascii character sequences.
|
---|
| 115 | */
|
---|
| 116 | static unsigned char casecharmap[] = {
|
---|
| 117 | '\000', '\001', '\002', '\003', '\004', '\005', '\006', '\007',
|
---|
| 118 | '\010', '\011', '\012', '\013', '\014', '\015', '\016', '\017',
|
---|
| 119 | '\020', '\021', '\022', '\023', '\024', '\025', '\026', '\027',
|
---|
| 120 | '\030', '\031', '\032', '\033', '\034', '\035', '\036', '\037',
|
---|
| 121 | '\040', '\041', '\042', '\043', '\044', '\045', '\046', '\047',
|
---|
| 122 | '\050', '\051', '\052', '\053', '\054', '\055', '\056', '\057',
|
---|
| 123 | '\060', '\061', '\062', '\063', '\064', '\065', '\066', '\067',
|
---|
| 124 | '\070', '\071', '\072', '\073', '\074', '\075', '\076', '\077',
|
---|
| 125 | '\100', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
|
---|
| 126 | '\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157',
|
---|
| 127 | '\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167',
|
---|
| 128 | '\170', '\171', '\172', '\133', '\134', '\135', '\136', '\137',
|
---|
| 129 | '\140', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
|
---|
| 130 | '\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157',
|
---|
| 131 | '\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167',
|
---|
| 132 | '\170', '\171', '\172', '\173', '\174', '\175', '\176', '\177',
|
---|
| 133 | '\200', '\201', '\202', '\203', '\204', '\205', '\206', '\207',
|
---|
| 134 | '\210', '\211', '\212', '\213', '\214', '\215', '\216', '\217',
|
---|
| 135 | '\220', '\221', '\222', '\223', '\224', '\225', '\226', '\227',
|
---|
| 136 | '\230', '\231', '\232', '\233', '\234', '\235', '\236', '\237',
|
---|
| 137 | '\240', '\241', '\242', '\243', '\244', '\245', '\246', '\247',
|
---|
| 138 | '\250', '\251', '\252', '\253', '\254', '\255', '\256', '\257',
|
---|
| 139 | '\260', '\261', '\262', '\263', '\264', '\265', '\266', '\267',
|
---|
| 140 | '\270', '\271', '\272', '\273', '\274', '\275', '\276', '\277',
|
---|
| 141 | '\300', '\341', '\342', '\343', '\344', '\345', '\346', '\347',
|
---|
| 142 | '\350', '\351', '\352', '\353', '\354', '\355', '\356', '\357',
|
---|
| 143 | '\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367',
|
---|
| 144 | '\370', '\371', '\372', '\333', '\334', '\335', '\336', '\337',
|
---|
| 145 | '\340', '\341', '\342', '\343', '\344', '\345', '\346', '\347',
|
---|
| 146 | '\350', '\351', '\352', '\353', '\354', '\355', '\356', '\357',
|
---|
| 147 | '\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367',
|
---|
| 148 | '\370', '\371', '\372', '\373', '\374', '\375', '\376', '\377',
|
---|
| 149 | };
|
---|
| 150 |
|
---|
| 151 | /* ===============================================================================
|
---|
| 152 | * Function: casecompare [RPAP - Jan 97: Stem Index Change]
|
---|
| 153 | * Description: Compare two strings in dictionary order
|
---|
| 154 | * Input: Two binary strings, first byte of string indicates length.
|
---|
| 155 | * Output:
|
---|
| 156 | * = 0 if s1 = s2
|
---|
| 157 | * < 0 if s1 < s2
|
---|
| 158 | * > 0 if s1 > s2
|
---|
| 159 | * =============================================================================== */
|
---|
| 160 | int
|
---|
| 161 | casecompare (u_char * s1, u_char * s2)
|
---|
| 162 | {
|
---|
| 163 | int l1 = *s1, l2 = *s2;
|
---|
| 164 | int l = (l1 < l2) ? l1 : l2;
|
---|
| 165 | int pos = 0;
|
---|
| 166 | register int diff = 0;
|
---|
| 167 |
|
---|
| 168 | while (l--)
|
---|
| 169 | if ((diff = casecharmap[*++s1] - casecharmap[*++s2]) != 0)
|
---|
| 170 | return (diff);
|
---|
| 171 | else if ((diff = *s1 - *s2) != 0)
|
---|
| 172 | if (!pos)
|
---|
| 173 | pos = diff;
|
---|
| 174 |
|
---|
| 175 | return ((l1 - l2) ? (l1 - l2) : (pos));
|
---|
| 176 |
|
---|
| 177 | }
|
---|
| 178 |
|
---|
| 179 | /* =========================================================================
|
---|
| 180 | * Function: copy_string
|
---|
| 181 | * Description:
|
---|
| 182 | * Copies a string with the len in 1st byte
|
---|
| 183 | * Input:
|
---|
| 184 | * Output:
|
---|
| 185 | * ========================================================================= */
|
---|
| 186 |
|
---|
| 187 | u_char *
|
---|
| 188 | copy_string (u_char * src_str)
|
---|
| 189 | {
|
---|
| 190 | u_char *copy = Xmalloc (src_str[0] + 1);
|
---|
| 191 |
|
---|
| 192 | if (!copy)
|
---|
| 193 | return NULL;
|
---|
| 194 |
|
---|
| 195 | bcopy ((char *) src_str, (char *) (copy), src_str[0] + 1);
|
---|
| 196 | return copy;
|
---|
| 197 |
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 |
|
---|
| 201 | /* ===============================================================================
|
---|
| 202 | * Function: prefixlen
|
---|
| 203 | * Description: Returns length of common prefix string between s1 and s2.
|
---|
| 204 | * Input: two binary strings
|
---|
| 205 | * Output: prefix length
|
---|
| 206 | * =============================================================================== */
|
---|
| 207 | int
|
---|
| 208 | prefixlen (u_char * s1, u_char * s2)
|
---|
| 209 | {
|
---|
| 210 | int l = (*s1 < *s2) ? *s1 : *s2;
|
---|
| 211 | int i = 0;
|
---|
| 212 |
|
---|
| 213 | while (i < l && *++s1 == *++s2)
|
---|
| 214 | i++;
|
---|
| 215 | return (i);
|
---|
| 216 | }
|
---|
| 217 |
|
---|
| 218 |
|
---|
| 219 | /* ===============================================================================
|
---|
| 220 | * Function: char2str
|
---|
| 221 | * Description:
|
---|
| 222 | * Ensures that non-printable characters are converted to a string
|
---|
| 223 | * which is printable i.e. it indentifies the character.
|
---|
| 224 | * e.g. carriage-return = "\n".
|
---|
| 225 | * Input: character (may be non-printable)
|
---|
| 226 | * Output: pointer to static string
|
---|
| 227 | * =============================================================================== */
|
---|
| 228 |
|
---|
| 229 | char *
|
---|
| 230 | char2str (u_char c)
|
---|
| 231 | {
|
---|
| 232 | static char buf[10];
|
---|
| 233 | switch (c)
|
---|
| 234 | {
|
---|
| 235 | case '\n':
|
---|
| 236 | sprintf (buf, "\\n");
|
---|
| 237 | break;
|
---|
| 238 | case '\b':
|
---|
| 239 | sprintf (buf, "\\b");
|
---|
| 240 | break;
|
---|
| 241 | case '\f':
|
---|
| 242 | sprintf (buf, "\\f");
|
---|
| 243 | break;
|
---|
| 244 | case '\t':
|
---|
| 245 | sprintf (buf, "\\t");
|
---|
| 246 | break;
|
---|
| 247 | default:
|
---|
| 248 | if (c >= ' ' && c <= 126)
|
---|
| 249 | switch (c)
|
---|
| 250 | {
|
---|
| 251 | case '\\':
|
---|
| 252 | sprintf (buf, "\\\\");
|
---|
| 253 | break;
|
---|
| 254 | case '\"':
|
---|
| 255 | sprintf (buf, "\\\"");
|
---|
| 256 | break;
|
---|
| 257 | case '\'':
|
---|
| 258 | sprintf (buf, "\\\'");
|
---|
| 259 | break;
|
---|
| 260 | default:
|
---|
| 261 | buf[0] = c;
|
---|
| 262 | buf[1] = '\0';
|
---|
| 263 | break;
|
---|
| 264 | }
|
---|
| 265 | else
|
---|
| 266 | sprintf (buf, "\\x%02x", c);
|
---|
| 267 | break;
|
---|
| 268 | }
|
---|
| 269 | return buf;
|
---|
| 270 | }
|
---|
| 271 |
|
---|
| 272 | /* ===============================================================================
|
---|
| 273 | * Function: word2str
|
---|
| 274 | * Description:
|
---|
| 275 | * Converts a string with possible non-printable characters into a string
|
---|
| 276 | * consisting entirely of printable characters.
|
---|
| 277 | * Input: string, length stored in first byte
|
---|
| 278 | * Output: pointer to static string
|
---|
| 279 | * =============================================================================== */
|
---|
| 280 |
|
---|
| 281 | char *
|
---|
| 282 | word2str (u_char * s)
|
---|
| 283 | {
|
---|
| 284 | static char buf[1024];
|
---|
| 285 | char *p = buf;
|
---|
| 286 | int i, len = (int) *s++;
|
---|
| 287 |
|
---|
| 288 | bzero (buf, sizeof (buf));
|
---|
| 289 | for (i = 0; i < len; i++)
|
---|
| 290 | {
|
---|
| 291 | strcpy (p, char2str (s[i]));
|
---|
| 292 | p = strchr (p, '\0');
|
---|
| 293 | }
|
---|
| 294 | return buf;
|
---|
| 295 | }
|
---|
| 296 |
|
---|
| 297 |
|
---|
| 298 | /* =========================================================================
|
---|
| 299 | * Function: de_escape_string
|
---|
| 300 | * Description:
|
---|
| 301 | * Turn a string containing \ escapes into real characters
|
---|
| 302 | * Note this destroys the string s in the process
|
---|
| 303 | * Input: string to de-escape
|
---|
| 304 | * Output: the de-escaped string
|
---|
| 305 | * ========================================================================= */
|
---|
| 306 | char *
|
---|
| 307 | de_escape_string (char *s)
|
---|
| 308 | {
|
---|
| 309 | char *org = s;
|
---|
| 310 | char *d = s;
|
---|
| 311 | while (*s)
|
---|
| 312 | {
|
---|
| 313 | if (*s == '\\')
|
---|
| 314 | {
|
---|
| 315 | s++;
|
---|
| 316 | switch (*s++)
|
---|
| 317 | {
|
---|
| 318 | case '\0':
|
---|
| 319 | *d++ = '\\';
|
---|
| 320 | s--;
|
---|
| 321 | break;
|
---|
| 322 | case '\\':
|
---|
| 323 | *d++ = '\\';
|
---|
| 324 | break;
|
---|
| 325 | case 'b':
|
---|
| 326 | *d++ = '\b';
|
---|
| 327 | break;
|
---|
| 328 | case 'f':
|
---|
| 329 | *d++ = '\f';
|
---|
| 330 | break;
|
---|
| 331 | case 'n':
|
---|
| 332 | *d++ = '\n';
|
---|
| 333 | break;
|
---|
| 334 | case 'r':
|
---|
| 335 | *d++ = '\r';
|
---|
| 336 | break;
|
---|
| 337 | case 't':
|
---|
| 338 | *d++ = '\r';
|
---|
| 339 | break;
|
---|
| 340 | case '\"':
|
---|
| 341 | *d++ = '\"';
|
---|
| 342 | break;
|
---|
| 343 | case '\'':
|
---|
| 344 | *d++ = '\'';
|
---|
| 345 | break;
|
---|
| 346 | case 'x':
|
---|
| 347 | {
|
---|
| 348 | int num = 0;
|
---|
| 349 | if (isxdigit (*s))
|
---|
| 350 | {
|
---|
| 351 | char ch = toupper (*s++);
|
---|
| 352 | num = ch <= '9' ? ch - '0' : ch - 'A' + 10;
|
---|
| 353 | }
|
---|
| 354 | if (isxdigit (*s))
|
---|
| 355 | {
|
---|
| 356 | char ch = toupper (*s++);
|
---|
| 357 | num = (num << 4) + (ch <= '9' ? ch - '0' : ch - 'A' + 10);
|
---|
| 358 | }
|
---|
| 359 | *d++ = num;
|
---|
| 360 | break;
|
---|
| 361 | }
|
---|
| 362 | case '0':
|
---|
| 363 | case '1':
|
---|
| 364 | case '2':
|
---|
| 365 | case '3':
|
---|
| 366 | case '4':
|
---|
| 367 | case '5':
|
---|
| 368 | case '6':
|
---|
| 369 | case '7':
|
---|
| 370 | {
|
---|
| 371 | int num = *(s - 1) - '0';
|
---|
| 372 | if (*s >= '0' && *s <= '7')
|
---|
| 373 | num = (num << 3) + *s++ - '0';
|
---|
| 374 | if (*s >= '0' && *s <= '7')
|
---|
| 375 | num = (num << 3) + *s++ - '0';
|
---|
| 376 | *d++ = num;
|
---|
| 377 | break;
|
---|
| 378 | }
|
---|
| 379 | }
|
---|
| 380 | }
|
---|
| 381 | else
|
---|
| 382 | *d++ = *s++;
|
---|
| 383 | }
|
---|
| 384 | *d = '\0';
|
---|
| 385 | return (org);
|
---|
| 386 | }
|
---|
| 387 |
|
---|
| 388 | /* =========================================================================
|
---|
| 389 | * Function: str255_to_string
|
---|
| 390 | * Description:
|
---|
| 391 | * Input:
|
---|
| 392 | * Output:
|
---|
| 393 | * ========================================================================= */
|
---|
| 394 |
|
---|
| 395 | char *
|
---|
| 396 | str255_to_string (u_char * str255, char *str)
|
---|
| 397 | {
|
---|
| 398 | static char buf[256];
|
---|
| 399 | char *str_base = NULL;
|
---|
| 400 | int len = *str255++;
|
---|
| 401 | int i = 0;
|
---|
| 402 |
|
---|
| 403 | /* if haven't been given a str to write to then use local buffer */
|
---|
| 404 | if (!str)
|
---|
| 405 | str = &(buf[0]);
|
---|
| 406 |
|
---|
| 407 | str_base = str;
|
---|
| 408 | for (i = 0; i < len; i++)
|
---|
| 409 | {
|
---|
| 410 | *str++ = *str255++;
|
---|
| 411 | }
|
---|
| 412 | *str = '\0';
|
---|
| 413 |
|
---|
| 414 | return str_base;
|
---|
| 415 | }
|
---|