1 | n/a | /* |
---|
2 | n/a | * Copyright (c) 2008-2016 Stefan Krah. All rights reserved. |
---|
3 | n/a | * |
---|
4 | n/a | * Redistribution and use in source and binary forms, with or without |
---|
5 | n/a | * modification, are permitted provided that the following conditions |
---|
6 | n/a | * are met: |
---|
7 | n/a | * |
---|
8 | n/a | * 1. Redistributions of source code must retain the above copyright |
---|
9 | n/a | * notice, this list of conditions and the following disclaimer. |
---|
10 | n/a | * |
---|
11 | n/a | * 2. Redistributions in binary form must reproduce the above copyright |
---|
12 | n/a | * notice, this list of conditions and the following disclaimer in the |
---|
13 | n/a | * documentation and/or other materials provided with the distribution. |
---|
14 | n/a | * |
---|
15 | n/a | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND |
---|
16 | n/a | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
17 | n/a | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
18 | n/a | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
---|
19 | n/a | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
20 | n/a | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
21 | n/a | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
22 | n/a | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
23 | n/a | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
24 | n/a | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
25 | n/a | * SUCH DAMAGE. |
---|
26 | n/a | */ |
---|
27 | n/a | |
---|
28 | n/a | |
---|
29 | n/a | #include "mpdecimal.h" |
---|
30 | n/a | #include <stdio.h> |
---|
31 | n/a | #include <stdlib.h> |
---|
32 | n/a | #include "typearith.h" |
---|
33 | n/a | #include "mpalloc.h" |
---|
34 | n/a | |
---|
35 | n/a | |
---|
36 | n/a | #if defined(_MSC_VER) |
---|
37 | n/a | #pragma warning(disable : 4232) |
---|
38 | n/a | #endif |
---|
39 | n/a | |
---|
40 | n/a | |
---|
41 | n/a | /* Guaranteed minimum allocation for a coefficient. May be changed once |
---|
42 | n/a | at program start using mpd_setminalloc(). */ |
---|
43 | n/a | mpd_ssize_t MPD_MINALLOC = MPD_MINALLOC_MIN; |
---|
44 | n/a | |
---|
45 | n/a | /* Custom allocation and free functions */ |
---|
46 | n/a | void *(* mpd_mallocfunc)(size_t size) = malloc; |
---|
47 | n/a | void *(* mpd_reallocfunc)(void *ptr, size_t size) = realloc; |
---|
48 | n/a | void *(* mpd_callocfunc)(size_t nmemb, size_t size) = calloc; |
---|
49 | n/a | void (* mpd_free)(void *ptr) = free; |
---|
50 | n/a | |
---|
51 | n/a | |
---|
52 | n/a | /* emulate calloc if it is not available */ |
---|
53 | n/a | void * |
---|
54 | n/a | mpd_callocfunc_em(size_t nmemb, size_t size) |
---|
55 | n/a | { |
---|
56 | n/a | void *ptr; |
---|
57 | n/a | size_t req; |
---|
58 | n/a | mpd_size_t overflow; |
---|
59 | n/a | |
---|
60 | n/a | #if MPD_SIZE_MAX < SIZE_MAX |
---|
61 | n/a | /* full_coverage test only */ |
---|
62 | n/a | if (nmemb > MPD_SIZE_MAX || size > MPD_SIZE_MAX) { |
---|
63 | n/a | return NULL; |
---|
64 | n/a | } |
---|
65 | n/a | #endif |
---|
66 | n/a | |
---|
67 | n/a | req = mul_size_t_overflow((mpd_size_t)nmemb, (mpd_size_t)size, |
---|
68 | n/a | &overflow); |
---|
69 | n/a | if (overflow) { |
---|
70 | n/a | return NULL; |
---|
71 | n/a | } |
---|
72 | n/a | |
---|
73 | n/a | ptr = mpd_mallocfunc(req); |
---|
74 | n/a | if (ptr == NULL) { |
---|
75 | n/a | return NULL; |
---|
76 | n/a | } |
---|
77 | n/a | /* used on uint32_t or uint64_t */ |
---|
78 | n/a | memset(ptr, 0, req); |
---|
79 | n/a | |
---|
80 | n/a | return ptr; |
---|
81 | n/a | } |
---|
82 | n/a | |
---|
83 | n/a | |
---|
84 | n/a | /* malloc with overflow checking */ |
---|
85 | n/a | void * |
---|
86 | n/a | mpd_alloc(mpd_size_t nmemb, mpd_size_t size) |
---|
87 | n/a | { |
---|
88 | n/a | mpd_size_t req, overflow; |
---|
89 | n/a | |
---|
90 | n/a | req = mul_size_t_overflow(nmemb, size, &overflow); |
---|
91 | n/a | if (overflow) { |
---|
92 | n/a | return NULL; |
---|
93 | n/a | } |
---|
94 | n/a | |
---|
95 | n/a | return mpd_mallocfunc(req); |
---|
96 | n/a | } |
---|
97 | n/a | |
---|
98 | n/a | /* calloc with overflow checking */ |
---|
99 | n/a | void * |
---|
100 | n/a | mpd_calloc(mpd_size_t nmemb, mpd_size_t size) |
---|
101 | n/a | { |
---|
102 | n/a | mpd_size_t overflow; |
---|
103 | n/a | |
---|
104 | n/a | (void)mul_size_t_overflow(nmemb, size, &overflow); |
---|
105 | n/a | if (overflow) { |
---|
106 | n/a | return NULL; |
---|
107 | n/a | } |
---|
108 | n/a | |
---|
109 | n/a | return mpd_callocfunc(nmemb, size); |
---|
110 | n/a | } |
---|
111 | n/a | |
---|
112 | n/a | /* realloc with overflow checking */ |
---|
113 | n/a | void * |
---|
114 | n/a | mpd_realloc(void *ptr, mpd_size_t nmemb, mpd_size_t size, uint8_t *err) |
---|
115 | n/a | { |
---|
116 | n/a | void *new; |
---|
117 | n/a | mpd_size_t req, overflow; |
---|
118 | n/a | |
---|
119 | n/a | req = mul_size_t_overflow(nmemb, size, &overflow); |
---|
120 | n/a | if (overflow) { |
---|
121 | n/a | *err = 1; |
---|
122 | n/a | return ptr; |
---|
123 | n/a | } |
---|
124 | n/a | |
---|
125 | n/a | new = mpd_reallocfunc(ptr, req); |
---|
126 | n/a | if (new == NULL) { |
---|
127 | n/a | *err = 1; |
---|
128 | n/a | return ptr; |
---|
129 | n/a | } |
---|
130 | n/a | |
---|
131 | n/a | return new; |
---|
132 | n/a | } |
---|
133 | n/a | |
---|
134 | n/a | /* struct hack malloc with overflow checking */ |
---|
135 | n/a | void * |
---|
136 | n/a | mpd_sh_alloc(mpd_size_t struct_size, mpd_size_t nmemb, mpd_size_t size) |
---|
137 | n/a | { |
---|
138 | n/a | mpd_size_t req, overflow; |
---|
139 | n/a | |
---|
140 | n/a | req = mul_size_t_overflow(nmemb, size, &overflow); |
---|
141 | n/a | if (overflow) { |
---|
142 | n/a | return NULL; |
---|
143 | n/a | } |
---|
144 | n/a | |
---|
145 | n/a | req = add_size_t_overflow(req, struct_size, &overflow); |
---|
146 | n/a | if (overflow) { |
---|
147 | n/a | return NULL; |
---|
148 | n/a | } |
---|
149 | n/a | |
---|
150 | n/a | return mpd_mallocfunc(req); |
---|
151 | n/a | } |
---|
152 | n/a | |
---|
153 | n/a | |
---|
154 | n/a | /* Allocate a new decimal with a coefficient of length 'nwords'. In case |
---|
155 | n/a | of an error the return value is NULL. */ |
---|
156 | n/a | mpd_t * |
---|
157 | n/a | mpd_qnew_size(mpd_ssize_t nwords) |
---|
158 | n/a | { |
---|
159 | n/a | mpd_t *result; |
---|
160 | n/a | |
---|
161 | n/a | nwords = (nwords < MPD_MINALLOC) ? MPD_MINALLOC : nwords; |
---|
162 | n/a | |
---|
163 | n/a | result = mpd_alloc(1, sizeof *result); |
---|
164 | n/a | if (result == NULL) { |
---|
165 | n/a | return NULL; |
---|
166 | n/a | } |
---|
167 | n/a | |
---|
168 | n/a | result->data = mpd_alloc(nwords, sizeof *result->data); |
---|
169 | n/a | if (result->data == NULL) { |
---|
170 | n/a | mpd_free(result); |
---|
171 | n/a | return NULL; |
---|
172 | n/a | } |
---|
173 | n/a | |
---|
174 | n/a | result->flags = 0; |
---|
175 | n/a | result->exp = 0; |
---|
176 | n/a | result->digits = 0; |
---|
177 | n/a | result->len = 0; |
---|
178 | n/a | result->alloc = nwords; |
---|
179 | n/a | |
---|
180 | n/a | return result; |
---|
181 | n/a | } |
---|
182 | n/a | |
---|
183 | n/a | /* Allocate a new decimal with a coefficient of length MPD_MINALLOC. |
---|
184 | n/a | In case of an error the return value is NULL. */ |
---|
185 | n/a | mpd_t * |
---|
186 | n/a | mpd_qnew(void) |
---|
187 | n/a | { |
---|
188 | n/a | return mpd_qnew_size(MPD_MINALLOC); |
---|
189 | n/a | } |
---|
190 | n/a | |
---|
191 | n/a | /* Allocate new decimal. Caller can check for NULL or MPD_Malloc_error. |
---|
192 | n/a | Raises on error. */ |
---|
193 | n/a | mpd_t * |
---|
194 | n/a | mpd_new(mpd_context_t *ctx) |
---|
195 | n/a | { |
---|
196 | n/a | mpd_t *result; |
---|
197 | n/a | |
---|
198 | n/a | result = mpd_qnew(); |
---|
199 | n/a | if (result == NULL) { |
---|
200 | n/a | mpd_addstatus_raise(ctx, MPD_Malloc_error); |
---|
201 | n/a | } |
---|
202 | n/a | return result; |
---|
203 | n/a | } |
---|
204 | n/a | |
---|
205 | n/a | /* |
---|
206 | n/a | * Input: 'result' is a static mpd_t with a static coefficient. |
---|
207 | n/a | * Assumption: 'nwords' >= result->alloc. |
---|
208 | n/a | * |
---|
209 | n/a | * Resize the static coefficient to a larger dynamic one and copy the |
---|
210 | n/a | * existing data. If successful, the value of 'result' is unchanged. |
---|
211 | n/a | * Otherwise, set 'result' to NaN and update 'status' with MPD_Malloc_error. |
---|
212 | n/a | */ |
---|
213 | n/a | int |
---|
214 | n/a | mpd_switch_to_dyn(mpd_t *result, mpd_ssize_t nwords, uint32_t *status) |
---|
215 | n/a | { |
---|
216 | n/a | mpd_uint_t *p = result->data; |
---|
217 | n/a | |
---|
218 | n/a | assert(nwords >= result->alloc); |
---|
219 | n/a | |
---|
220 | n/a | result->data = mpd_alloc(nwords, sizeof *result->data); |
---|
221 | n/a | if (result->data == NULL) { |
---|
222 | n/a | result->data = p; |
---|
223 | n/a | mpd_set_qnan(result); |
---|
224 | n/a | mpd_set_positive(result); |
---|
225 | n/a | result->exp = result->digits = result->len = 0; |
---|
226 | n/a | *status |= MPD_Malloc_error; |
---|
227 | n/a | return 0; |
---|
228 | n/a | } |
---|
229 | n/a | |
---|
230 | n/a | memcpy(result->data, p, result->alloc * (sizeof *result->data)); |
---|
231 | n/a | result->alloc = nwords; |
---|
232 | n/a | mpd_set_dynamic_data(result); |
---|
233 | n/a | return 1; |
---|
234 | n/a | } |
---|
235 | n/a | |
---|
236 | n/a | /* |
---|
237 | n/a | * Input: 'result' is a static mpd_t with a static coefficient. |
---|
238 | n/a | * |
---|
239 | n/a | * Convert the coefficient to a dynamic one that is initialized to zero. If |
---|
240 | n/a | * malloc fails, set 'result' to NaN and update 'status' with MPD_Malloc_error. |
---|
241 | n/a | */ |
---|
242 | n/a | int |
---|
243 | n/a | mpd_switch_to_dyn_zero(mpd_t *result, mpd_ssize_t nwords, uint32_t *status) |
---|
244 | n/a | { |
---|
245 | n/a | mpd_uint_t *p = result->data; |
---|
246 | n/a | |
---|
247 | n/a | result->data = mpd_calloc(nwords, sizeof *result->data); |
---|
248 | n/a | if (result->data == NULL) { |
---|
249 | n/a | result->data = p; |
---|
250 | n/a | mpd_set_qnan(result); |
---|
251 | n/a | mpd_set_positive(result); |
---|
252 | n/a | result->exp = result->digits = result->len = 0; |
---|
253 | n/a | *status |= MPD_Malloc_error; |
---|
254 | n/a | return 0; |
---|
255 | n/a | } |
---|
256 | n/a | |
---|
257 | n/a | result->alloc = nwords; |
---|
258 | n/a | mpd_set_dynamic_data(result); |
---|
259 | n/a | |
---|
260 | n/a | return 1; |
---|
261 | n/a | } |
---|
262 | n/a | |
---|
263 | n/a | /* |
---|
264 | n/a | * Input: 'result' is a static or a dynamic mpd_t with a dynamic coefficient. |
---|
265 | n/a | * Resize the coefficient to length 'nwords': |
---|
266 | n/a | * Case nwords > result->alloc: |
---|
267 | n/a | * If realloc is successful: |
---|
268 | n/a | * 'result' has a larger coefficient but the same value. Return 1. |
---|
269 | n/a | * Otherwise: |
---|
270 | n/a | * Set 'result' to NaN, update status with MPD_Malloc_error and return 0. |
---|
271 | n/a | * Case nwords < result->alloc: |
---|
272 | n/a | * If realloc is successful: |
---|
273 | n/a | * 'result' has a smaller coefficient. result->len is undefined. Return 1. |
---|
274 | n/a | * Otherwise (unlikely): |
---|
275 | n/a | * 'result' is unchanged. Reuse the now oversized coefficient. Return 1. |
---|
276 | n/a | */ |
---|
277 | n/a | int |
---|
278 | n/a | mpd_realloc_dyn(mpd_t *result, mpd_ssize_t nwords, uint32_t *status) |
---|
279 | n/a | { |
---|
280 | n/a | uint8_t err = 0; |
---|
281 | n/a | |
---|
282 | n/a | result->data = mpd_realloc(result->data, nwords, sizeof *result->data, &err); |
---|
283 | n/a | if (!err) { |
---|
284 | n/a | result->alloc = nwords; |
---|
285 | n/a | } |
---|
286 | n/a | else if (nwords > result->alloc) { |
---|
287 | n/a | mpd_set_qnan(result); |
---|
288 | n/a | mpd_set_positive(result); |
---|
289 | n/a | result->exp = result->digits = result->len = 0; |
---|
290 | n/a | *status |= MPD_Malloc_error; |
---|
291 | n/a | return 0; |
---|
292 | n/a | } |
---|
293 | n/a | |
---|
294 | n/a | return 1; |
---|
295 | n/a | } |
---|
296 | n/a | |
---|
297 | n/a | |
---|