xref: /minix3/crypto/external/bsd/openssl/dist/demos/tunala/breakage.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1ebfedea0SLionel Sambuc #include "tunala.h"
2ebfedea0SLionel Sambuc 
int_strtoul(const char * str,unsigned long * val)3ebfedea0SLionel Sambuc int int_strtoul(const char *str, unsigned long *val)
4ebfedea0SLionel Sambuc {
5ebfedea0SLionel Sambuc #ifdef HAVE_STRTOUL
6ebfedea0SLionel Sambuc     char *tmp;
7ebfedea0SLionel Sambuc     unsigned long ret = strtoul(str, &tmp, 10);
8ebfedea0SLionel Sambuc     if ((str == tmp) || (*tmp != '\0'))
9ebfedea0SLionel Sambuc         /* The value didn't parse cleanly */
10ebfedea0SLionel Sambuc         return 0;
11ebfedea0SLionel Sambuc     if (ret == ULONG_MAX)
12ebfedea0SLionel Sambuc         /* We hit a limit */
13ebfedea0SLionel Sambuc         return 0;
14ebfedea0SLionel Sambuc     *val = ret;
15ebfedea0SLionel Sambuc     return 1;
16ebfedea0SLionel Sambuc #else
17ebfedea0SLionel Sambuc     char buf[2];
18ebfedea0SLionel Sambuc     unsigned long ret = 0;
19ebfedea0SLionel Sambuc     buf[1] = '\0';
20ebfedea0SLionel Sambuc     if (str == '\0')
21ebfedea0SLionel Sambuc         /* An empty string ... */
22ebfedea0SLionel Sambuc         return 0;
23ebfedea0SLionel Sambuc     while (*str != '\0') {
24*0a6a1f1dSLionel Sambuc         /*
25*0a6a1f1dSLionel Sambuc          * We have to multiply 'ret' by 10 before absorbing the next digit.
26*0a6a1f1dSLionel Sambuc          * If this will overflow, catch it now.
27*0a6a1f1dSLionel Sambuc          */
28ebfedea0SLionel Sambuc         if (ret && (((ULONG_MAX + 10) / ret) < 10))
29ebfedea0SLionel Sambuc             return 0;
30ebfedea0SLionel Sambuc         ret *= 10;
31ebfedea0SLionel Sambuc         if (!isdigit(*str))
32ebfedea0SLionel Sambuc             return 0;
33ebfedea0SLionel Sambuc         buf[0] = *str;
34ebfedea0SLionel Sambuc         ret += atoi(buf);
35ebfedea0SLionel Sambuc         str++;
36ebfedea0SLionel Sambuc     }
37ebfedea0SLionel Sambuc     *val = ret;
38ebfedea0SLionel Sambuc     return 1;
39ebfedea0SLionel Sambuc #endif
40ebfedea0SLionel Sambuc }
41ebfedea0SLionel Sambuc 
42ebfedea0SLionel Sambuc #ifndef HAVE_STRSTR
int_strstr(const char * haystack,const char * needle)43ebfedea0SLionel Sambuc char *int_strstr(const char *haystack, const char *needle)
44ebfedea0SLionel Sambuc {
45ebfedea0SLionel Sambuc     const char *sub_haystack = haystack, *sub_needle = needle;
46ebfedea0SLionel Sambuc     unsigned int offset = 0;
47ebfedea0SLionel Sambuc     if (!needle)
48ebfedea0SLionel Sambuc         return haystack;
49ebfedea0SLionel Sambuc     if (!haystack)
50ebfedea0SLionel Sambuc         return NULL;
51ebfedea0SLionel Sambuc     while ((*sub_haystack != '\0') && (*sub_needle != '\0')) {
52ebfedea0SLionel Sambuc         if (sub_haystack[offset] == sub_needle) {
53ebfedea0SLionel Sambuc             /* sub_haystack is still a candidate */
54ebfedea0SLionel Sambuc             offset++;
55ebfedea0SLionel Sambuc             sub_needle++;
56ebfedea0SLionel Sambuc         } else {
57ebfedea0SLionel Sambuc             /* sub_haystack is no longer a possibility */
58ebfedea0SLionel Sambuc             sub_haystack++;
59ebfedea0SLionel Sambuc             offset = 0;
60ebfedea0SLionel Sambuc             sub_needle = needle;
61ebfedea0SLionel Sambuc         }
62ebfedea0SLionel Sambuc     }
63ebfedea0SLionel Sambuc     if (*sub_haystack == '\0')
64ebfedea0SLionel Sambuc         /* Found nothing */
65ebfedea0SLionel Sambuc         return NULL;
66ebfedea0SLionel Sambuc     return sub_haystack;
67ebfedea0SLionel Sambuc }
68ebfedea0SLionel Sambuc #endif
69