about summary refs log tree commit diff
path: root/lib/util/nstring.c
blob: 4bbb041f21b7474a0576ba92d2ffae5742cdc791 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/*=============================================================================
                               nstring.c
===============================================================================

  pm_snprintf (and pm_vsnprintf) in this file used to be derived from
  'portable_snprintf' from
  http://www.ijs.si/software/snprintf/snprintf-2.2.tar.gz, because not all
  system C libraries had snprintf.  But in 2013, we extended that snprintf to
  implement %f by calling 'snprintf' in the system C library, just to see if
  it caused any build failures.  As of August 2022, there had been no
  complaints of problems caused by this reliance on the system providing
  snprintf, so we just made pm_snprintf a wrapper of snprintf for everything.

  Eventually we will remove pm_snprintf and pm_vsnprintf altogether and their
  callers will call 'snprintf' and 'vsnprintf' instead

  Note that snprintf is required by the C99 standard.

  The code from which pm_snprintf was formerly derived was protected by
  copyright and licensed to the public under GPL.  A user in August 2022 noted
  that GPL was insufficient for his use of it, making him unable to use
  libnetpbm.

  Code in this file is contributed to the public domain by its authors.
=============================================================================*/
#define _DEFAULT_SOURCE /* New name for SVID & BSD source defines */
#define _XOPEN_SOURCE 500  /* Make sure strdup() is in string.h */
#define _BSD_SOURCE  /* Make sure strdup() is in string.h */
#define _GNU_SOURCE
   /* Because of conditional compilation, this is GNU source only if the C
      library is GNU.
   */
#include <sys/types.h>
#include <limits.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <assert.h>
#include <errno.h>

#include "pm.h"
#include "pm_c_util.h"

#include "nstring.h"

/* MacOS X before 10.7, for one, does not have strnlen */
size_t
pm_strnlen(const char * const s,
           size_t       const maxlen) {

    unsigned int i;

    for (i = 0; i < maxlen && s[i]; ++i) {}

    return i;
}



void
pm_vsnprintf(char *       const str,
             size_t       const maxSize,
             const char * const fmt,
             va_list            ap,
             size_t *     const sizeP) {

    int rc;

    rc = vsnprintf(str, maxSize, fmt, ap);

    assert((size_t)rc == rc);

    *sizeP = (size_t)rc;
}



int
pm_snprintf(char *       const dest,
            size_t       const maxSize,
            const char * const fmt,
            ...) {

    size_t size;
    va_list ap;

    va_start(ap, fmt);

    pm_vsnprintf(dest, maxSize, fmt, ap, &size);

    va_end(ap);

    assert(size <= INT_MAX);

    return size;
}



/* When a function that is supposed to return a malloc'ed string cannot
   get the memory for it, it should return 'pm_strsol'.  That has a much
   better effect on the caller, if the caller doesn't explicitly allow for
   the out of memory case, than returning NULL.  Note that it is very
   rare for the system not to have enough memory to return a small string,
   so it's OK to have somewhat nonsensical behavior when it happens.  We
   just don't want catastrophic behavior.

   'pm_strsol' is an external symbol, so if Caller wants to detect the
   out-of-memory failure, he certainly can.
*/
const char * const pm_strsol = "NO MEMORY TO CREATE STRING!";



const char *
pm_strdup(const char * const arg) {

    const char * const dup = strdup(arg);

    return dup ? dup : pm_strsol;
}



void PM_GNU_PRINTF_ATTR(2,3)
pm_asprintf(const char ** const resultP,
            const char *  const fmt,
            ...) {

    const char * result;
    va_list varargs;

#if HAVE_VASPRINTF
    int rc;
    va_start(varargs, fmt);
    rc = vasprintf((char **)&result, fmt, varargs);
    va_end(varargs);
    if (rc < 0)
        result = pm_strsol;
#else
    size_t dryRunLen;

    va_start(varargs, fmt);

    pm_vsnprintf(NULL, 0, fmt, varargs, &dryRunLen);

    va_end(varargs);

    if (dryRunLen + 1 < dryRunLen)
        /* arithmetic overflow */
        result = pm_strsol;
    else {
        size_t const allocSize = dryRunLen + 1;
        char * buffer;
        buffer = malloc(allocSize);
        if (buffer != NULL) {
            va_list varargs;
            size_t realLen;

            va_start(varargs, fmt);

            pm_vsnprintf(buffer, allocSize, fmt, varargs, &realLen);

            assert(realLen == dryRunLen);
            va_end(varargs);

            result = buffer;
        }
    }
#endif

    if (result == NULL)
        *resultP = pm_strsol;
    else
        *resultP = result;
}



void
pm_strfree(const char * const string) {

    if (string != pm_strsol)
        free((void *) string);
}



const char *
pm_strsep(char ** const stringP, const char * const delim) {
    const char * retval;

    if (stringP == NULL || *stringP == NULL)
        retval = NULL;
    else {
        char * p;

        retval = *stringP;

        for (p = *stringP; *p && strchr(delim, *p) == NULL; ++p);

        if (*p) {
            /* We hit a delimiter, not end-of-string.  So null out the
               delimiter and advance user's pointer to the next token
            */
            *p++ = '\0';
            *stringP = p;
        } else {
            /* We ran out of string.  So the end-of-string delimiter is
               already there, and we set the user's pointer to NULL to
               indicate there are no more tokens.
            */
            *stringP = NULL;
        }
    }
    return retval;
}



int
pm_stripeq(const char * const comparand,
           const char * const comparator) {
/*----------------------------------------------------------------------------
  Compare two strings, ignoring leading and trailing white space.

  Return 1 (true) if the strings are identical; 0 (false) otherwise.
-----------------------------------------------------------------------------*/
    const char * p;
    const char * q;
    const char * px;
    const char * qx;
    bool equal;

    /* Make p and q point to the first non-blank character in each string.
       If there are no non-blank characters, make them point to the terminating
       NUL.
    */

    p = &comparand[0];
    while (ISSPACE(*p))
        p++;
    q = &comparator[0];
    while (ISSPACE(*q))
        q++;

    /* Make px and qx point to the last non-blank character in each string.
       If there are no nonblank characters (which implies the string is
       null), make them point to the terminating NUL.
    */

    if (*p == '\0')
        px = p;
    else {
        px = p + strlen(p) - 1;
        while (ISSPACE(*px))
            --px;
    }

    if (*q == '\0')
        qx = q;
    else {
        qx = q + strlen(q) - 1;
        while (ISSPACE(*qx))
            --qx;
    }

    if (px - p != qx - q) {
        /* The stripped strings aren't the same length, so we know they aren't
           equal.
        */
        equal = false;
    } else {
        /* Move p and q through the nonblank characters, comparing. */
        for (equal = true; p <= px; ++p, ++q) {
            assert(q <= qx);  /* Because stripped strings are same length */
            if (*p != *q)
                equal = false;
        }
    }
    return equal ? 1 : 0;
}



const void *
pm_memmem(const void * const haystackArg,
          size_t       const haystacklen,
          const void * const needleArg,
          size_t       const needlelen) {

    const unsigned char * const haystack = haystackArg;
    const unsigned char * const needle   = needleArg;

    /* This does the same as the function of the same name in the GNU
       C library
    */
    const unsigned char * p;

    for (p = haystack; p <= haystack + haystacklen - needlelen; ++p)
        if (memeq(p, needle, needlelen))
            return p;

    return NULL;
}



bool
pm_strishex(const char * const subject) {

    bool retval;
    unsigned int i;

    retval = TRUE;  /* initial assumption */

    for (i = 0; i < strlen(subject); ++i)
        if (!ISXDIGIT(subject[i]))
            retval = FALSE;

    return retval;
}



void
pm_string_to_long(const char *   const string,
                  long *         const longP,
                  const char **  const errorP) {

    if (strlen(string) == 0)
        pm_asprintf(errorP, "Value is a null string");
    else {
        char * tailptr;

        /* strtol() does a bizarre thing where if the number is out
           of range, it returns a clamped value but tells you about it
           by setting errno = ERANGE.  If it is not out of range,
           strtol() leaves errno alone.
        */
        errno = 0;  /* So we can tell if strtol() overflowed */

        *longP = strtol(string, &tailptr, 10);

        if (*tailptr != '\0')
            pm_asprintf(errorP, "Non-numeric crap in string: '%s'", tailptr);
        else {
             if (errno == ERANGE)
                 pm_asprintf(errorP, "Number is too large for computation");
             else
                 *errorP = NULL;
        }
    }
}



void
pm_string_to_int(const char *   const string,
                 int *          const intP,
                 const char **  const errorP) {

    long longValue;

    pm_string_to_long(string, &longValue, errorP);

    if (!*errorP) {
        if ((int)longValue != longValue)
            pm_asprintf(errorP,
                        "Number is too large for computation");
        else {
            *intP = (int)longValue;
            *errorP = NULL;
        }
    }
}



void
pm_string_to_uint(const char *   const string,
                  unsigned int * const uintP,
                  const char **  const errorP) {

    /* We can't use 'strtoul'.  Contrary to expectations, though as
       designed, it returns junk if there is a minus sign.
    */

    long longValue;

    pm_string_to_long(string, &longValue, errorP);

    if (!*errorP) {
        if (longValue < 0)
            pm_asprintf(errorP, "Number is negative");
        else {
            if ((unsigned int)longValue != longValue)
                pm_asprintf(errorP,
                            "Number is too large for computation");
            else {
                *uintP = (unsigned int)longValue;
                *errorP = NULL;
            }
        }
    }
}