1 | /*
|
---|
2 | * Large integer functions
|
---|
3 | *
|
---|
4 | * Copyright 2000 Alexandre Julliard
|
---|
5 | * Copyright 2003 Thomas Mertes
|
---|
6 | *
|
---|
7 | * This library is free software; you can redistribute it and/or
|
---|
8 | * modify it under the terms of the GNU Lesser General Public
|
---|
9 | * License as published by the Free Software Foundation; either
|
---|
10 | * version 2.1 of the License, or (at your option) any later version.
|
---|
11 | *
|
---|
12 | * This library is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
15 | * Lesser General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU Lesser General Public
|
---|
18 | * License along with this library; if not, write to the Free Software
|
---|
19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
---|
20 | */
|
---|
21 |
|
---|
22 | #include "windef.h"
|
---|
23 | #include "winternl.h"
|
---|
24 |
|
---|
25 | /*
|
---|
26 | * Note: we use LONGLONG instead of LARGE_INTEGER, because
|
---|
27 | * the latter is a structure and the calling convention for
|
---|
28 | * returning a structure would not be binary-compatible.
|
---|
29 | *
|
---|
30 | * FIXME: for platforms that don't have a native LONGLONG type,
|
---|
31 | * we should define LONGLONG as a structure similar to LARGE_INTEGER
|
---|
32 | * and do everything by hand. You are welcome to do it...
|
---|
33 | */
|
---|
34 |
|
---|
35 | /******************************************************************************
|
---|
36 | * RtlLargeIntegerAdd (NTDLL.@)
|
---|
37 | */
|
---|
38 | LONGLONG WINAPI RtlLargeIntegerAdd( LONGLONG a, LONGLONG b )
|
---|
39 | {
|
---|
40 | return a + b;
|
---|
41 | }
|
---|
42 |
|
---|
43 |
|
---|
44 | /******************************************************************************
|
---|
45 | * RtlLargeIntegerSubtract (NTDLL.@)
|
---|
46 | */
|
---|
47 | LONGLONG WINAPI RtlLargeIntegerSubtract( LONGLONG a, LONGLONG b )
|
---|
48 | {
|
---|
49 | return a - b;
|
---|
50 | }
|
---|
51 |
|
---|
52 |
|
---|
53 | /******************************************************************************
|
---|
54 | * RtlLargeIntegerNegate (NTDLL.@)
|
---|
55 | */
|
---|
56 | LONGLONG WINAPI RtlLargeIntegerNegate( LONGLONG a )
|
---|
57 | {
|
---|
58 | return -a;
|
---|
59 | }
|
---|
60 |
|
---|
61 |
|
---|
62 | /******************************************************************************
|
---|
63 | * RtlLargeIntegerShiftLeft (NTDLL.@)
|
---|
64 | */
|
---|
65 | LONGLONG WINAPI RtlLargeIntegerShiftLeft( LONGLONG a, INT count )
|
---|
66 | {
|
---|
67 | return a << count;
|
---|
68 | }
|
---|
69 |
|
---|
70 |
|
---|
71 | /******************************************************************************
|
---|
72 | * RtlLargeIntegerShiftRight (NTDLL.@)
|
---|
73 | */
|
---|
74 | LONGLONG WINAPI RtlLargeIntegerShiftRight( LONGLONG a, INT count )
|
---|
75 | {
|
---|
76 | return (ULONGLONG)a >> count;
|
---|
77 | }
|
---|
78 |
|
---|
79 |
|
---|
80 | /******************************************************************************
|
---|
81 | * RtlLargeIntegerArithmeticShift (NTDLL.@)
|
---|
82 | */
|
---|
83 | LONGLONG WINAPI RtlLargeIntegerArithmeticShift( LONGLONG a, INT count )
|
---|
84 | {
|
---|
85 | /* FIXME: gcc does arithmetic shift here, but it may not be true on all platforms */
|
---|
86 | return a >> count;
|
---|
87 | }
|
---|
88 |
|
---|
89 |
|
---|
90 | /******************************************************************************
|
---|
91 | * RtlLargeIntegerDivide (NTDLL.@)
|
---|
92 | *
|
---|
93 | * FIXME: should it be signed division instead?
|
---|
94 | */
|
---|
95 | ULONGLONG WINAPI RtlLargeIntegerDivide( ULONGLONG a, ULONGLONG b, ULONGLONG *rem )
|
---|
96 | {
|
---|
97 | ULONGLONG ret = a / b;
|
---|
98 | if (rem) *rem = a - ret * b;
|
---|
99 | return ret;
|
---|
100 | }
|
---|
101 |
|
---|
102 |
|
---|
103 | /******************************************************************************
|
---|
104 | * RtlConvertLongToLargeInteger (NTDLL.@)
|
---|
105 | */
|
---|
106 | LONGLONG WINAPI RtlConvertLongToLargeInteger( LONG a )
|
---|
107 | {
|
---|
108 | return a;
|
---|
109 | }
|
---|
110 |
|
---|
111 |
|
---|
112 | /******************************************************************************
|
---|
113 | * RtlConvertUlongToLargeInteger (NTDLL.@)
|
---|
114 | */
|
---|
115 | ULONGLONG WINAPI RtlConvertUlongToLargeInteger( ULONG a )
|
---|
116 | {
|
---|
117 | return a;
|
---|
118 | }
|
---|
119 |
|
---|
120 |
|
---|
121 | /******************************************************************************
|
---|
122 | * RtlEnlargedIntegerMultiply (NTDLL.@)
|
---|
123 | */
|
---|
124 | LONGLONG WINAPI RtlEnlargedIntegerMultiply( INT a, INT b )
|
---|
125 | {
|
---|
126 | return (LONGLONG)a * b;
|
---|
127 | }
|
---|
128 |
|
---|
129 |
|
---|
130 | /******************************************************************************
|
---|
131 | * RtlEnlargedUnsignedMultiply (NTDLL.@)
|
---|
132 | */
|
---|
133 | ULONGLONG WINAPI RtlEnlargedUnsignedMultiply( UINT a, UINT b )
|
---|
134 | {
|
---|
135 | return (ULONGLONG)a * b;
|
---|
136 | }
|
---|
137 |
|
---|
138 |
|
---|
139 | /******************************************************************************
|
---|
140 | * RtlEnlargedUnsignedDivide (NTDLL.@)
|
---|
141 | */
|
---|
142 | UINT WINAPI RtlEnlargedUnsignedDivide( ULONGLONG a, UINT b, UINT *remptr )
|
---|
143 | {
|
---|
144 | #if defined(__i386__) && defined(__GNUC__)
|
---|
145 | UINT ret, rem, p1, p2;
|
---|
146 |
|
---|
147 | p1 = a >> 32;
|
---|
148 | p2 = a & 0xffffffffLL;
|
---|
149 |
|
---|
150 | __asm__("div %4,%%eax"
|
---|
151 | : "=a" (ret), "=d" (rem)
|
---|
152 | : "0" (p2), "1" (p1), "g" (b) );
|
---|
153 | if (remptr) *remptr = rem;
|
---|
154 | return ret;
|
---|
155 | #else
|
---|
156 | UINT ret = a / b;
|
---|
157 | if (remptr) *remptr = a % b;
|
---|
158 | return ret;
|
---|
159 | #endif
|
---|
160 | }
|
---|
161 |
|
---|
162 |
|
---|
163 | /******************************************************************************
|
---|
164 | * RtlExtendedLargeIntegerDivide (NTDLL.@)
|
---|
165 | */
|
---|
166 | LONGLONG WINAPI RtlExtendedLargeIntegerDivide( LONGLONG a, INT b, INT *rem )
|
---|
167 | {
|
---|
168 | LONGLONG ret = a / b;
|
---|
169 | if (rem) *rem = a - b * ret;
|
---|
170 | return ret;
|
---|
171 | }
|
---|
172 |
|
---|
173 |
|
---|
174 | /******************************************************************************
|
---|
175 | * RtlExtendedIntegerMultiply (NTDLL.@)
|
---|
176 | */
|
---|
177 | LONGLONG WINAPI RtlExtendedIntegerMultiply( LONGLONG a, INT b )
|
---|
178 | {
|
---|
179 | return a * b;
|
---|
180 | }
|
---|
181 |
|
---|
182 |
|
---|
183 | /******************************************************************************
|
---|
184 | * RtlExtendedMagicDivide (NTDLL.@)
|
---|
185 | *
|
---|
186 | * This function computes (a * b) >> (64 + shift)
|
---|
187 | *
|
---|
188 | * RETURNS
|
---|
189 | * (a * b) >> (64 + shift)
|
---|
190 | *
|
---|
191 | * NOTES
|
---|
192 | * This allows replacing a division by a longlong constant
|
---|
193 | * by a multiplication by the inverse constant.
|
---|
194 | *
|
---|
195 | * If 'c' is the constant divisor, the constants 'b' and 'shift'
|
---|
196 | * must be chosen such that b = 2^(64+shift) / c.
|
---|
197 | * Then we have RtlExtendedMagicDivide(a,b,shift) == a * b / 2^(64+shift) == a / c.
|
---|
198 | *
|
---|
199 | * The Parameter b although defined as LONGLONG is used as ULONGLONG.
|
---|
200 | */
|
---|
201 | #define LOWER_32(A) ((A) & 0xffffffff)
|
---|
202 | #define UPPER_32(A) ((A) >> 32)
|
---|
203 | LONGLONG WINAPI RtlExtendedMagicDivide(
|
---|
204 | LONGLONG a, /* [I] Dividend to be divided by the constant divisor */
|
---|
205 | LONGLONG b, /* [I] Constant computed manually as 2^(64+shift) / divisor */
|
---|
206 | INT shift) /* [I] Constant shift chosen to make b as big as possible for 64 bits */
|
---|
207 | {
|
---|
208 | ULONGLONG a_high;
|
---|
209 | ULONGLONG a_low;
|
---|
210 | ULONGLONG b_high;
|
---|
211 | ULONGLONG b_low;
|
---|
212 | ULONGLONG ah_bl;
|
---|
213 | ULONGLONG al_bh;
|
---|
214 | LONGLONG result;
|
---|
215 | int positive;
|
---|
216 |
|
---|
217 | if (a < 0) {
|
---|
218 | a_high = UPPER_32((ULONGLONG) -a);
|
---|
219 | a_low = LOWER_32((ULONGLONG) -a);
|
---|
220 | positive = 0;
|
---|
221 | } else {
|
---|
222 | a_high = UPPER_32((ULONGLONG) a);
|
---|
223 | a_low = LOWER_32((ULONGLONG) a);
|
---|
224 | positive = 1;
|
---|
225 | } /* if */
|
---|
226 | b_high = UPPER_32((ULONGLONG) b);
|
---|
227 | b_low = LOWER_32((ULONGLONG) b);
|
---|
228 |
|
---|
229 | ah_bl = a_high * b_low;
|
---|
230 | al_bh = a_low * b_high;
|
---|
231 |
|
---|
232 | result = (LONGLONG) ((a_high * b_high +
|
---|
233 | UPPER_32(ah_bl) +
|
---|
234 | UPPER_32(al_bh) +
|
---|
235 | UPPER_32(LOWER_32(ah_bl) + LOWER_32(al_bh) + UPPER_32(a_low * b_low))) >> shift);
|
---|
236 |
|
---|
237 | if (positive) {
|
---|
238 | return result;
|
---|
239 | } else {
|
---|
240 | return -result;
|
---|
241 | } /* if */
|
---|
242 | }
|
---|
243 |
|
---|
244 |
|
---|
245 | /******************************************************************************
|
---|
246 | * RtlLargeIntegerToChar [NTDLL.@]
|
---|
247 | *
|
---|
248 | * Convert an unsigned large integer to a character string.
|
---|
249 | *
|
---|
250 | * RETURNS
|
---|
251 | * Success: STATUS_SUCCESS. str contains the converted number
|
---|
252 | * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
|
---|
253 | * STATUS_BUFFER_OVERFLOW, if str would be larger than length.
|
---|
254 | * STATUS_ACCESS_VIOLATION, if str is NULL.
|
---|
255 | *
|
---|
256 | * NOTES
|
---|
257 | * Instead of base 0 it uses 10 as base.
|
---|
258 | * Writes at most length characters to the string str.
|
---|
259 | * Str is '\0' terminated when length allowes it.
|
---|
260 | * When str fits exactly in length characters the '\0' is ommitted.
|
---|
261 | * If value_ptr is NULL it crashes, as the native function does.
|
---|
262 | *
|
---|
263 | * DIFFERENCES
|
---|
264 | * - Accept base 0 as 10 instead of crashing as native function does.
|
---|
265 | * - The native function does produce garbage or STATUS_BUFFER_OVERFLOW for
|
---|
266 | * base 2, 8 and 16 when the value is larger than 0xFFFFFFFF.
|
---|
267 | */
|
---|
268 | NTSTATUS WINAPI RtlLargeIntegerToChar(
|
---|
269 | const ULONGLONG *value_ptr, /* [I] Pointer to the value to be converted */
|
---|
270 | ULONG base, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
|
---|
271 | ULONG length, /* [I] Length of the str buffer in bytes */
|
---|
272 | PCHAR str) /* [O] Destination for the converted value */
|
---|
273 | {
|
---|
274 | ULONGLONG value = *value_ptr;
|
---|
275 | CHAR buffer[65];
|
---|
276 | PCHAR pos;
|
---|
277 | CHAR digit;
|
---|
278 | ULONG len;
|
---|
279 |
|
---|
280 | if (base == 0) {
|
---|
281 | base = 10;
|
---|
282 | } else if (base != 2 && base != 8 && base != 10 && base != 16) {
|
---|
283 | return STATUS_INVALID_PARAMETER;
|
---|
284 | } /* if */
|
---|
285 |
|
---|
286 | pos = &buffer[64];
|
---|
287 | *pos = '\0';
|
---|
288 |
|
---|
289 | do {
|
---|
290 | pos--;
|
---|
291 | digit = value % base;
|
---|
292 | value = value / base;
|
---|
293 | if (digit < 10) {
|
---|
294 | *pos = '0' + digit;
|
---|
295 | } else {
|
---|
296 | *pos = 'A' + digit - 10;
|
---|
297 | } /* if */
|
---|
298 | } while (value != 0L);
|
---|
299 |
|
---|
300 | len = &buffer[64] - pos;
|
---|
301 | if (len > length) {
|
---|
302 | return STATUS_BUFFER_OVERFLOW;
|
---|
303 | } else if (str == NULL) {
|
---|
304 | return STATUS_ACCESS_VIOLATION;
|
---|
305 | } else if (len == length) {
|
---|
306 | memcpy(str, pos, len);
|
---|
307 | } else {
|
---|
308 | memcpy(str, pos, len + 1);
|
---|
309 | } /* if */
|
---|
310 | return STATUS_SUCCESS;
|
---|
311 | }
|
---|
312 |
|
---|
313 |
|
---|
314 | /**************************************************************************
|
---|
315 | * RtlInt64ToUnicodeString (NTDLL.@)
|
---|
316 | *
|
---|
317 | * Convert a large unsigned integer to a '\0' terminated unicode string.
|
---|
318 | *
|
---|
319 | * RETURNS
|
---|
320 | * Success: STATUS_SUCCESS. str contains the converted number
|
---|
321 | * Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
|
---|
322 | * STATUS_BUFFER_OVERFLOW, if str is too small to hold the string
|
---|
323 | * (with the '\0' termination). In this case str->Length
|
---|
324 | * is set to the length, the string would have (which can
|
---|
325 | * be larger than the MaximumLength).
|
---|
326 | *
|
---|
327 | * NOTES
|
---|
328 | * Instead of base 0 it uses 10 as base.
|
---|
329 | * If str is NULL it crashes, as the native function does.
|
---|
330 | *
|
---|
331 | * DIFFERENCES
|
---|
332 | * - Accept base 0 as 10 instead of crashing as native function does.
|
---|
333 | * - Do not return STATUS_BUFFER_OVERFLOW when the string is long enough.
|
---|
334 | * The native function does this when the string would be longer than 31
|
---|
335 | * characters even when the string parameter is long enough.
|
---|
336 | * - The native function does produce garbage or STATUS_BUFFER_OVERFLOW for
|
---|
337 | * base 2, 8 and 16 when the value is larger than 0xFFFFFFFF.
|
---|
338 | */
|
---|
339 | NTSTATUS WINAPI RtlInt64ToUnicodeString(
|
---|
340 | ULONGLONG value, /* [I] Value to be converted */
|
---|
341 | ULONG base, /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
|
---|
342 | UNICODE_STRING *str) /* [O] Destination for the converted value */
|
---|
343 | {
|
---|
344 | WCHAR buffer[65];
|
---|
345 | PWCHAR pos;
|
---|
346 | WCHAR digit;
|
---|
347 |
|
---|
348 | if (base == 0) {
|
---|
349 | base = 10;
|
---|
350 | } else if (base != 2 && base != 8 && base != 10 && base != 16) {
|
---|
351 | return STATUS_INVALID_PARAMETER;
|
---|
352 | } /* if */
|
---|
353 |
|
---|
354 | pos = &buffer[64];
|
---|
355 | *pos = '\0';
|
---|
356 |
|
---|
357 | do {
|
---|
358 | pos--;
|
---|
359 | digit = value % base;
|
---|
360 | value = value / base;
|
---|
361 | if (digit < 10) {
|
---|
362 | *pos = '0' + digit;
|
---|
363 | } else {
|
---|
364 | *pos = 'A' + digit - 10;
|
---|
365 | } /* if */
|
---|
366 | } while (value != 0L);
|
---|
367 |
|
---|
368 | str->Length = (&buffer[64] - pos) * sizeof(WCHAR);
|
---|
369 | if (str->Length >= str->MaximumLength) {
|
---|
370 | return STATUS_BUFFER_OVERFLOW;
|
---|
371 | } else {
|
---|
372 | memcpy(str->Buffer, pos, str->Length + sizeof(WCHAR));
|
---|
373 | } /* if */
|
---|
374 | return STATUS_SUCCESS;
|
---|
375 | }
|
---|
376 |
|
---|
377 |
|
---|
378 | /******************************************************************************
|
---|
379 | * _alldiv (NTDLL.@)
|
---|
380 | */
|
---|
381 | LONGLONG WINAPI _alldiv( LONGLONG a, LONGLONG b )
|
---|
382 | {
|
---|
383 | return a / b;
|
---|
384 | }
|
---|
385 |
|
---|
386 |
|
---|
387 | /******************************************************************************
|
---|
388 | * _allmul (NTDLL.@)
|
---|
389 | */
|
---|
390 | LONGLONG WINAPI _allmul( LONGLONG a, LONGLONG b )
|
---|
391 | {
|
---|
392 | return a * b;
|
---|
393 | }
|
---|
394 |
|
---|
395 |
|
---|
396 | /******************************************************************************
|
---|
397 | * _allrem (NTDLL.@)
|
---|
398 | */
|
---|
399 | LONGLONG WINAPI _allrem( LONGLONG a, LONGLONG b )
|
---|
400 | {
|
---|
401 | return a % b;
|
---|
402 | }
|
---|
403 |
|
---|
404 |
|
---|
405 | /******************************************************************************
|
---|
406 | * _aulldiv (NTDLL.@)
|
---|
407 | */
|
---|
408 | ULONGLONG WINAPI _aulldiv( ULONGLONG a, ULONGLONG b )
|
---|
409 | {
|
---|
410 | return a / b;
|
---|
411 | }
|
---|
412 |
|
---|
413 |
|
---|
414 | /******************************************************************************
|
---|
415 | * _aullrem (NTDLL.@)
|
---|
416 | */
|
---|
417 | ULONGLONG WINAPI _aullrem( ULONGLONG a, ULONGLONG b )
|
---|
418 | {
|
---|
419 | return a % b;
|
---|
420 | }
|
---|