~mmach/netext73/busybox

« back to all changes in this revision

Viewing changes to 0005-bc-support-ibase-up-to-36-GNU-compat.patch

  • Committer: mmach
  • Date: 2021-04-14 13:54:24 UTC
  • Revision ID: netbit73@gmail.com-20210414135424-8x3fxf716zs4wflb
1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
From 680ccd357395ad081afe821ffbeef1ff338fe41d Mon Sep 17 00:00:00 2001
 
2
From: Denys Vlasenko <vda.linux@googlemail.com>
 
3
Date: Mon, 31 Dec 2018 19:42:13 +0100
 
4
Subject: [PATCH 05/84] bc: support ibase up to 36 (GNU compat)
 
5
 
 
6
function                                             old     new   delta
 
7
zxc_program_num                                      995    1018     +23
 
8
 
 
9
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
 
10
---
 
11
 miscutils/bc.c     | 50 +++++++++++++++++++++++++++++++-------------------
 
12
 testsuite/bc.tests |  5 +++++
 
13
 2 files changed, 36 insertions(+), 19 deletions(-)
 
14
 
 
15
diff --git a/miscutils/bc.c b/miscutils/bc.c
 
16
index 72c23542c..798bc0a3e 100644
 
17
--- a/miscutils/bc.c
 
18
+++ b/miscutils/bc.c
 
19
@@ -5,7 +5,6 @@
 
20
  * Original code copyright (c) 2018 Gavin D. Howard and contributors.
 
21
  */
 
22
 //TODO: GNU extensions:
 
23
-// support ibase up to 36
 
24
 // support "define void f()..."
 
25
 // support "define f(*param[])" - "pass array by reference" syntax
 
26
 
 
27
@@ -231,7 +230,7 @@ typedef struct BcNum {
 
28
        bool neg;
 
29
 } BcNum;
 
30
 
 
31
-#define BC_NUM_MAX_IBASE        ((unsigned long) 16)
 
32
+#define BC_NUM_MAX_IBASE        36
 
33
 // larger value might speed up BIGNUM calculations a bit:
 
34
 #define BC_NUM_DEF_SIZE         16
 
35
 #define BC_NUM_PRINT_WIDTH      69
 
36
@@ -2638,32 +2637,33 @@ static void bc_num_parseDecimal(BcNum *n, const char *val)
 
37
 static void bc_num_parseBase(BcNum *n, const char *val, unsigned base_t)
 
38
 {
 
39
        BcStatus s;
 
40
-       BcNum temp, mult, result;
 
41
+       BcNum mult, result;
 
42
+       BcNum temp;
 
43
        BcNum base;
 
44
+       BcDig temp_digs[ULONG_NUM_BUFSIZE];
 
45
        BcDig base_digs[ULONG_NUM_BUFSIZE];
 
46
        BcDig c = '\0';
 
47
-       unsigned long v;
 
48
-       size_t i, digits;
 
49
-
 
50
-       for (i = 0; ; ++i) {
 
51
-               if (val[i] == '\0')
 
52
-                       return;
 
53
-               if (val[i] != '.' && val[i] != '0')
 
54
-                       break;
 
55
-       }
 
56
+       size_t digits;
 
57
 
 
58
-       bc_num_init_DEF_SIZE(&temp);
 
59
        bc_num_init_DEF_SIZE(&mult);
 
60
+
 
61
+       temp.cap = ARRAY_SIZE(temp_digs);
 
62
+       temp.num = temp_digs;
 
63
+
 
64
        base.cap = ARRAY_SIZE(base_digs);
 
65
        base.num = base_digs;
 
66
        bc_num_ulong2num(&base, base_t);
 
67
+       base_t--;
 
68
 
 
69
        for (;;) {
 
70
+               unsigned v;
 
71
+
 
72
                c = *val++;
 
73
                if (c == '\0') goto int_err;
 
74
                if (c == '.') break;
 
75
 
 
76
-               v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10);
 
77
+               v = (unsigned)(c <= '9' ? c - '0' : c - 'A' + 10);
 
78
+               if (v > base_t) v = base_t;
 
79
 
 
80
                s = zbc_num_mul(n, &base, &mult, 0);
 
81
                if (s) goto int_err;
 
82
@@ -2678,11 +2678,14 @@ static void bc_num_parseBase(BcNum *n, const char *val, unsigned base_t)
 
83
 
 
84
        digits = 0;
 
85
        for (;;) {
 
86
+               unsigned v;
 
87
+
 
88
                c = *val++;
 
89
                if (c == '\0') break;
 
90
                digits++;
 
91
 
 
92
-               v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10);
 
93
+               v = (unsigned)(c <= '9' ? c - '0' : c - 'A' + 10);
 
94
+               if (v > base_t) v = base_t;
 
95
 
 
96
                s = zbc_num_mul(&result, &base, &result, 0);
 
97
                if (s) goto err;
 
98
@@ -2707,18 +2710,27 @@ static void bc_num_parseBase(BcNum *n, const char *val, unsigned base_t)
 
99
        bc_num_free(&result);
 
100
  int_err:
 
101
        bc_num_free(&mult);
 
102
-       bc_num_free(&temp);
 
103
 }
 
104
 
 
105
 static BC_STATUS zxc_num_parse(BcNum *n, const char *val, unsigned base_t)
 
106
 {
 
107
+       size_t i;
 
108
+
 
109
        if (!xc_num_strValid(val))
 
110
                RETURN_STATUS(bc_error("bad number string"));
 
111
 
 
112
        bc_num_zero(n);
 
113
-       while (*val == '0') val++;
 
114
+       while (*val == '0')
 
115
+               val++;
 
116
+       for (i = 0; ; ++i) {
 
117
+               if (val[i] == '\0')
 
118
+                       RETURN_STATUS(BC_STATUS_SUCCESS);
 
119
+               if (val[i] != '.' && val[i] != '0')
 
120
+                       break;
 
121
+       }
 
122
 
 
123
-       if (base_t == 10)
 
124
+       if (base_t == 10 || val[1] == '\0')
 
125
+               // Decimal, or single-digit number
 
126
                bc_num_parseDecimal(n, val);
 
127
        else
 
128
                bc_num_parseBase(n, val, base_t);
 
129
@@ -5526,7 +5538,7 @@ static BC_STATUS zxc_num_printBase(BcNum *n)
 
130
 
 
131
        n->neg = false;
 
132
 
 
133
-       if (G.prog.ob_t <= BC_NUM_MAX_IBASE) {
 
134
+       if (G.prog.ob_t <= 16) {
 
135
                width = 1;
 
136
                print = bc_num_printHex;
 
137
        } else {
 
138
diff --git a/testsuite/bc.tests b/testsuite/bc.tests
 
139
index 3fbb49996..1d4545559 100755
 
140
--- a/testsuite/bc.tests
 
141
+++ b/testsuite/bc.tests
 
142
@@ -218,6 +218,11 @@ for(i=1; i<3; i++) {
 
143
 99
 
144
 "
 
145
 
 
146
+testing "bc ibase" \
 
147
+       "bc" \
 
148
+       "99\n1295\n1224\n" \
 
149
+       "" "a=ZZ;a;ibase=36;a=ZZ;a;ibase=Z;a=ZZ;a"
 
150
+
 
151
 tar xJf bc_large.tar.xz
 
152
 
 
153
 for f in bc*.bc; do
 
154
-- 
 
155
2.16.2
 
156