| 1 |
|
| /* |
| 2 |
|
| * Converts a string into a int128_t |
| 3 |
|
| * |
| 4 |
|
| * based on OpenBSD: strtoll.c,v 1.6 2005/11/10 10:00:17 espie Exp $ |
| 5 |
|
| */ |
| 6 |
|
| |
| 7 |
|
| /*- |
| 8 |
|
| * Copyright (c) 1992 The Regents of the University of California. |
| 9 |
|
| * All rights reserved. |
| 10 |
|
| * |
| 11 |
|
| * Redistribution and use in source and binary forms, with or without |
| 12 |
|
| * modification, are permitted provided that the following conditions |
| 13 |
|
| * are met: |
| 14 |
|
| * 1. Redistributions of source code must retain the above copyright |
| 15 |
|
| * notice, this list of conditions and the following disclaimer. |
| 16 |
|
| * 2. Redistributions in binary form must reproduce the above copyright |
| 17 |
|
| * notice, this list of conditions and the following disclaimer in the |
| 18 |
|
| * documentation and/or other materials provided with the distribution. |
| 19 |
|
| * 3. Neither the name of the University nor the names of its contributors |
| 20 |
|
| * may be used to endorse or promote products derived from this software |
| 21 |
|
| * without specific prior written permission. |
| 22 |
|
| * |
| 23 |
|
| * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 24 |
|
| * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 25 |
|
| * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 26 |
|
| * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 27 |
|
| * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 28 |
|
| * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 29 |
|
| * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 30 |
|
| * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 31 |
|
| * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 32 |
|
| * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 33 |
|
| * SUCH DAMAGE. |
| 34 |
|
| */ |
| 35 |
|
| |
| 36 |
|
| #include "perl.h" |
| 37 |
|
| #include <ctype.h> |
| 38 |
|
| |
| 39 |
|
| /* |
| 40 |
|
| * Convert a string to an int128_t/uint128_t. |
| 41 |
|
| */ |
| 42 |
|
| |
| 43 |
|
| static uint128_t |
| 44 |
23 |
| strtoint128(pTHX_ const char *s, STRLEN len, int base, int sign) { |
| 45 |
23 |
| uint128_t acc = 0; |
| 46 |
23 |
| int c, neg, between = 0; |
| 47 |
23 |
| const char *top = s + len; |
| 48 |
23 |
| uint128_t upper_mul_limit = 0; |
| 49 |
|
| |
| 50 |
|
| /* |
| 51 |
|
| * Skip white space and pick up leading +/- sign if any. |
| 52 |
|
| * If base is 0, allow 0x for hex and 0 for octal, else |
| 53 |
|
| * assume decimal; if base is already 16, allow 0x. |
| 54 |
|
| */ |
| 55 |
|
| do { |
| 56 |
23 |
▶ | if (s >= top) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (s >= top) return 0; |
0 |
23 |
23 |
|
| 57 |
23 |
| c = *s++; |
| 58 |
23 |
▶ | } while (isspace(c)); |
Branch: 1 branch
| Branch | True | False | Total |
| } while (isspace(c)); |
0 |
23 |
23 |
|
| 59 |
|
| |
| 60 |
23 |
▶ | if (c == '-') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == '-') { |
4 |
19 |
23 |
|
| 61 |
4 |
▶ | if (!sign) overflow(aTHX_ "negative sign found when parsing unsigned number"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!sign) overflow(aTHX_ "negative sign found when parsing unsigned number"); |
0 |
4 |
4 |
|
| 62 |
4 |
▶ | if (s >= top) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (s >= top) return 0; |
0 |
4 |
4 |
|
| 63 |
4 |
| neg = 1; |
| 64 |
4 |
| c = *s++; |
| 65 |
|
| } else { |
| 66 |
19 |
| neg = 0; |
| 67 |
19 |
▶ | if (c == '+') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == '+') { |
0 |
19 |
19 |
|
| 68 |
0 |
▶ | if (s >= top) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (s >= top) return 0; |
0 |
0 |
0 |
|
| 69 |
0 |
| c = *s++; |
| 70 |
|
| } |
| 71 |
|
| } |
| 72 |
23 |
▶ | if (((base == 0) || (base == 16)) && |
Branch: 3 branches
| Branch | True | False | Total |
| if (((base == 0) || (base == 16)) && |
9 |
14 |
23 |
| if (((base == 0) || (base == 16)) && |
0 |
9 |
9 |
| if (((base == 0) || (base == 16)) && |
13 |
1 |
14 |
|
| 73 |
13 |
| (c == '0') && |
| 74 |
13 |
▶ | (s + 1 < top) && ((*s == 'x') || (*s == 'X'))) { |
Branch: 3 branches
| Branch | True | False | Total |
| (s + 1 < top) && ((*s == 'x') || (*s == 'X'))) { |
13 |
0 |
13 |
| (s + 1 < top) && ((*s == 'x') || (*s == 'X'))) { |
0 |
13 |
13 |
| (s + 1 < top) && ((*s == 'x') || (*s == 'X'))) { |
0 |
0 |
0 |
|
| 75 |
13 |
| c = s[1]; |
| 76 |
13 |
| s += 2; |
| 77 |
13 |
| base = 16; |
| 78 |
|
| } |
| 79 |
23 |
▶ | if (base == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (base == 0) |
1 |
22 |
23 |
|
| 80 |
1 |
▶ | base = ((c == '0') ? 8 : 10); |
Branch: 1 branch
| Branch | True | False | Total |
| base = ((c == '0') ? 8 : 10); |
0 |
1 |
1 |
|
| 81 |
|
| |
| 82 |
723 |
▶ | for (;s <= top; c = (unsigned char) *s++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (;s <= top; c = (unsigned char) *s++) { |
700 |
23 |
723 |
|
| 83 |
700 |
▶ | if (isdigit(c)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (isdigit(c)) |
332 |
368 |
700 |
|
| 84 |
332 |
| c -= '0'; |
| 85 |
368 |
▶ | else if (isalpha(c)) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (isalpha(c)) |
284 |
84 |
368 |
|
| 86 |
284 |
▶ | c -= isupper(c) ? 'A' - 10 : 'a' - 10; |
Branch: 1 branch
| Branch | True | False | Total |
| c -= isupper(c) ? 'A' - 10 : 'a' - 10; |
0 |
284 |
284 |
|
| 87 |
84 |
▶ | else if ((c == '_') && between) |
Branch: 2 branches
| Branch | True | False | Total |
| else if ((c == '_') && between) |
84 |
0 |
84 |
| else if ((c == '_') && between) |
84 |
0 |
84 |
|
| 88 |
84 |
| continue; /* ignore underscores as Perl does */ |
| 89 |
|
| else |
| 90 |
|
| break; |
| 91 |
616 |
▶ | if (c >= base) |
Branch: 1 branch
| Branch | True | False | Total |
| if (c >= base) |
0 |
616 |
616 |
|
| 92 |
0 |
| break; |
| 93 |
616 |
▶ | if (may_die_on_overflow) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (may_die_on_overflow) { |
71 |
545 |
616 |
|
| 94 |
71 |
| redo: |
| 95 |
73 |
▶ | if (acc > upper_mul_limit) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (acc > upper_mul_limit) { |
2 |
71 |
73 |
|
| 96 |
2 |
▶ | if (!upper_mul_limit) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!upper_mul_limit) { |
2 |
0 |
2 |
|
| 97 |
2 |
| upper_mul_limit = UINT128_MAX / base; |
| 98 |
2 |
| goto redo; |
| 99 |
|
| } |
| 100 |
0 |
▶ | overflow(aTHX_ (sign ? out_of_bounds_error_s : out_of_bounds_error_u)); |
Branch: 1 branch
| Branch | True | False | Total |
| overflow(aTHX_ (sign ? out_of_bounds_error_s : out_of_bounds_error_u)); |
0 |
0 |
0 |
|
| 101 |
|
| } |
| 102 |
71 |
| acc *= base; |
| 103 |
71 |
▶ | if (UINT128_MAX - acc < c) overflow(aTHX_ (sign ? out_of_bounds_error_s : out_of_bounds_error_u)); |
Branch: 2 branches
| Branch | True | False | Total |
| if (UINT128_MAX - acc < c) overflow(aTHX_ (sign ? out_of_bounds_error_s : out_of_bounds_error_u)); |
0 |
71 |
71 |
| if (UINT128_MAX - acc < c) overflow(aTHX_ (sign ? out_of_bounds_error_s : out_of_bounds_error_u)); |
0 |
0 |
0 |
|
| 104 |
71 |
| acc += c; |
| 105 |
|
| } |
| 106 |
|
| else { |
| 107 |
545 |
| acc = acc * base + c; |
| 108 |
|
| } |
| 109 |
616 |
| between = 1; |
| 110 |
|
| } |
| 111 |
23 |
▶ | if ( may_die_on_overflow && sign && |
Branch: 3 branches
| Branch | True | False | Total |
| if ( may_die_on_overflow && sign && |
2 |
21 |
23 |
| if ( may_die_on_overflow && sign && |
0 |
2 |
2 |
| if ( may_die_on_overflow && sign && |
0 |
0 |
0 |
|
| 112 |
0 |
▶ | ( acc > (neg ? (~(uint128_t)INT128_MIN + 1) : INT128_MAX) ) ) overflow(aTHX_ out_of_bounds_error_s); |
Branch: 1 branch
| Branch | True | False | Total |
| ( acc > (neg ? (~(uint128_t)INT128_MIN + 1) : INT128_MAX) ) ) overflow(aTHX_ out_of_bounds_error_s); |
0 |
0 |
0 |
|
| 113 |
|
| |
| 114 |
23 |
▶ | return (neg ? ~acc + 1 : acc); |
Branch: 1 branch
| Branch | True | False | Total |
| return (neg ? ~acc + 1 : acc); |
4 |
19 |
23 |
|
| 115 |
|
| } |
| 116 |
|
| |