Add and fix some stubs.
[wine] / dlls / ntdll / large_int.c
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 <stdarg.h>
23
24 #include "ntstatus.h"
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winreg.h"
28 #include "winternl.h"
29
30 /*
31  * Note: we use LONGLONG instead of LARGE_INTEGER, because
32  * the latter is a structure and the calling convention for
33  * returning a structure would not be binary-compatible.
34  *
35  * FIXME: for platforms that don't have a native LONGLONG type,
36  * we should define LONGLONG as a structure similar to LARGE_INTEGER
37  * and do everything by hand. You are welcome to do it...
38  */
39
40 /******************************************************************************
41  *        RtlLargeIntegerAdd   (NTDLL.@)
42  *
43  * Add two 64 bit integers.
44  *
45  * PARAMS
46  *  a [I] Initial number.
47  *  b [I] Number to add to a.
48  *
49  * RETURNS
50  *  The sum of a and b.
51  */
52 LONGLONG WINAPI RtlLargeIntegerAdd( LONGLONG a, LONGLONG b )
53 {
54     return a + b;
55 }
56
57
58 /******************************************************************************
59  *        RtlLargeIntegerSubtract   (NTDLL.@)
60  *
61  * Subtract two 64 bit integers.
62  *
63  * PARAMS
64  *  a [I] Initial number.
65  *  b [I] Number to subtract from a.
66  *
67  * RETURNS
68  *  The difference of a and b.
69  */
70 LONGLONG WINAPI RtlLargeIntegerSubtract( LONGLONG a, LONGLONG b )
71 {
72     return a - b;
73 }
74
75
76 /******************************************************************************
77  *        RtlLargeIntegerNegate   (NTDLL.@)
78  *
79  * Negate a 64 bit integer.
80  *
81  * PARAMS
82  *  a     [I] Initial number.
83  *
84  * RETURNS
85  *  The value of a negated.
86  */
87 LONGLONG WINAPI RtlLargeIntegerNegate( LONGLONG a )
88 {
89     return -a;
90 }
91
92
93 /******************************************************************************
94  *        RtlLargeIntegerShiftLeft   (NTDLL.@)
95  *
96  * Perform a shift left on a 64 bit integer.
97  *
98  * PARAMS
99  *  a     [I] Initial number.
100  *  count [I] Number of bits to shift by
101  *
102  * RETURNS
103  *  The value of a following the shift.
104  */
105 LONGLONG WINAPI RtlLargeIntegerShiftLeft( LONGLONG a, INT count )
106 {
107     return a << count;
108 }
109
110
111 /******************************************************************************
112  *        RtlLargeIntegerShiftRight   (NTDLL.@)
113  *
114  * Perform a shift right on a 64 bit integer.
115  *
116  * PARAMS
117  *  a     [I] Initial number.
118  *  count [I] Number of bits to shift by
119  *
120  * RETURNS
121  *  The value of a following the shift.
122  */
123 LONGLONG WINAPI RtlLargeIntegerShiftRight( LONGLONG a, INT count )
124 {
125     return (ULONGLONG)a >> count;
126 }
127
128
129 /******************************************************************************
130  *        RtlLargeIntegerArithmeticShift   (NTDLL.@)
131  *
132  * Perform an arithmetic shift right on a 64 bit integer.
133  *
134  * PARAMS
135  *  a     [I] Initial number.
136  *  count [I] Number of bits to shift by
137  *
138  * RETURNS
139  *  The value of a following the shift.
140  */
141 LONGLONG WINAPI RtlLargeIntegerArithmeticShift( LONGLONG a, INT count )
142 {
143     /* FIXME: gcc does arithmetic shift here, but it may not be true on all platforms */
144     return a >> count;
145 }
146
147
148 /******************************************************************************
149  *        RtlLargeIntegerDivide   (NTDLL.@)
150  *
151  * Divide one 64 bit unsigned integer by another, with remainder.
152  *
153  * PARAMS
154  *  a   [I] Initial number.
155  *  b   [I] Number to divide a by
156  *  rem [O] Destination for remainder
157  *
158  * RETURNS
159  *  The dividend of a and b. If rem is non-NULL it is set to the remainder.
160  *
161  * FIXME
162  *  Should it be signed division instead?
163  */
164 ULONGLONG WINAPI RtlLargeIntegerDivide( ULONGLONG a, ULONGLONG b, ULONGLONG *rem )
165 {
166     ULONGLONG ret = a / b;
167     if (rem) *rem = a - ret * b;
168     return ret;
169 }
170
171
172 /******************************************************************************
173  *        RtlConvertLongToLargeInteger   (NTDLL.@)
174  *
175  * Convert a 32 bit integer into 64 bits.
176  *
177  * PARAMS
178  *  a [I] Number to convert
179  *
180  * RETURNS
181  *  a.
182  */
183 LONGLONG WINAPI RtlConvertLongToLargeInteger( LONG a )
184 {
185     return a;
186 }
187
188
189 /******************************************************************************
190  *        RtlConvertUlongToLargeInteger   (NTDLL.@)
191  *
192  * Convert a 32 bit unsigned integer into 64 bits.
193  *
194  * PARAMS
195  *  a [I] Number to convert
196  *
197  * RETURNS
198  *  a.
199  */
200 ULONGLONG WINAPI RtlConvertUlongToLargeInteger( ULONG a )
201 {
202     return a;
203 }
204
205
206 /******************************************************************************
207  *        RtlEnlargedIntegerMultiply   (NTDLL.@)
208  *
209  * Multiply two integers giving a 64 bit integer result.
210  *
211  * PARAMS
212  *  a [I] Initial number.
213  *  b [I] Number to multiply a by.
214  *
215  * RETURNS
216  *  The product of a and b.
217  */
218 LONGLONG WINAPI RtlEnlargedIntegerMultiply( INT a, INT b )
219 {
220     return (LONGLONG)a * b;
221 }
222
223
224 /******************************************************************************
225  *        RtlEnlargedUnsignedMultiply   (NTDLL.@)
226  *
227  * Multiply two unsigned integers giving a 64 bit unsigned integer result.
228  *
229  * PARAMS
230  *  a [I] Initial number.
231  *  b [I] Number to multiply a by.
232  *
233  * RETURNS
234  *  The product of a and b.
235  */
236 ULONGLONG WINAPI RtlEnlargedUnsignedMultiply( UINT a, UINT b )
237 {
238     return (ULONGLONG)a * b;
239 }
240
241
242 /******************************************************************************
243  *        RtlEnlargedUnsignedDivide   (NTDLL.@)
244  *
245  * Divide one 64 bit unsigned integer by a 32 bit unsigned integer, with remainder.
246  *
247  * PARAMS
248  *  a      [I] Initial number.
249  *  b      [I] Number to divide a by
250  *  remptr [O] Destination for remainder
251  *
252  * RETURNS
253  *  The dividend of a and b. If remptr is non-NULL it is set to the remainder.
254  */
255 UINT WINAPI RtlEnlargedUnsignedDivide( ULONGLONG a, UINT b, UINT *remptr )
256 {
257 #if defined(__i386__) && defined(__GNUC__)
258     UINT ret, rem, p1, p2;
259
260     p1 = a >> 32;
261     p2 = a &  0xffffffffLL;
262
263     __asm__("div %4,%%eax"
264             : "=a" (ret), "=d" (rem)
265             : "0" (p2), "1" (p1), "g" (b) );
266     if (remptr) *remptr = rem;
267     return ret;
268 #else
269     UINT ret = a / b;
270     if (remptr) *remptr = a % b;
271     return ret;
272 #endif
273 }
274
275
276 /******************************************************************************
277  *        RtlExtendedLargeIntegerDivide   (NTDLL.@)
278  *
279  * Divide one 64 bit integer by a 32 bit integer, with remainder.
280  *
281  * PARAMS
282  *  a   [I] Initial number.
283  *  b   [I] Number to divide a by
284  *  rem [O] Destination for remainder
285  *
286  * RETURNS
287  *  The dividend of a and b. If rem is non-NULL it is set to the remainder.
288  */
289 LONGLONG WINAPI RtlExtendedLargeIntegerDivide( LONGLONG a, INT b, INT *rem )
290 {
291     LONGLONG ret = a / b;
292     if (rem) *rem = a - b * ret;
293     return ret;
294 }
295
296
297 /******************************************************************************
298  *        RtlExtendedIntegerMultiply   (NTDLL.@)
299  *
300  * Multiply one 64 bit integer by another 32 bit integer.
301  *
302  * PARAMS
303  *  a [I] Initial number.
304  *  b [I] Number to multiply a by.
305  *
306  * RETURNS
307  *  The product of a and b.
308  */
309 LONGLONG WINAPI RtlExtendedIntegerMultiply( LONGLONG a, INT b )
310 {
311     return a * b;
312 }
313
314
315 /******************************************************************************
316  *        RtlExtendedMagicDivide   (NTDLL.@)
317  *
318  * Allows replacing a division by a longlong constant with a multiplication by
319  * the inverse constant.
320  *
321  * RETURNS
322  *  (dividend * inverse_divisor) >> (64 + shift)
323  *
324  * NOTES
325  *  If the divisor of a division is constant, the constants inverse_divisor and
326  *  shift must be chosen such that inverse_divisor = 2^(64 + shift) / divisor.
327  *  Then we have RtlExtendedMagicDivide(dividend,inverse_divisor,shift) ==
328  *  dividend * inverse_divisor / 2^(64 + shift) == dividend / divisor.
329  *
330  *  The Parameter inverse_divisor although defined as LONGLONG is used as
331  *  ULONGLONG.
332  */
333 #define LOWER_32(A) ((A) & 0xffffffff)
334 #define UPPER_32(A) ((A) >> 32)
335 LONGLONG WINAPI RtlExtendedMagicDivide(
336     LONGLONG dividend,        /* [I] Dividend to be divided by the constant divisor */
337     LONGLONG inverse_divisor, /* [I] Constant computed manually as 2^(64+shift) / divisor */
338     INT shift)                /* [I] Constant shift chosen to make inverse_divisor as big as possible for 64 bits */
339 {
340     ULONGLONG dividend_high;
341     ULONGLONG dividend_low;
342     ULONGLONG inverse_divisor_high;
343     ULONGLONG inverse_divisor_low;
344     ULONGLONG ah_bl;
345     ULONGLONG al_bh;
346     LONGLONG result;
347     int positive;
348
349     if (dividend < 0) {
350         dividend_high = UPPER_32((ULONGLONG) -dividend);
351         dividend_low =  LOWER_32((ULONGLONG) -dividend);
352         positive = 0;
353     } else {
354         dividend_high = UPPER_32((ULONGLONG) dividend);
355         dividend_low =  LOWER_32((ULONGLONG) dividend);
356         positive = 1;
357     } /* if */
358     inverse_divisor_high = UPPER_32((ULONGLONG) inverse_divisor);
359     inverse_divisor_low =  LOWER_32((ULONGLONG) inverse_divisor);
360
361     ah_bl = dividend_high * inverse_divisor_low;
362     al_bh = dividend_low * inverse_divisor_high;
363
364     result = (LONGLONG) ((dividend_high * inverse_divisor_high +
365             UPPER_32(ah_bl) +
366             UPPER_32(al_bh) +
367             UPPER_32(LOWER_32(ah_bl) + LOWER_32(al_bh) +
368                      UPPER_32(dividend_low * inverse_divisor_low))) >> shift);
369
370     if (positive) {
371         return result;
372     } else {
373         return -result;
374     } /* if */
375 }
376
377
378 /******************************************************************************
379  *      RtlLargeIntegerToChar   [NTDLL.@]
380  *
381  * Convert an unsigned large integer to a character string.
382  *
383  * RETURNS
384  *  Success: STATUS_SUCCESS. str contains the converted number
385  *  Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
386  *           STATUS_BUFFER_OVERFLOW, if str would be larger than length.
387  *           STATUS_ACCESS_VIOLATION, if str is NULL.
388  *
389  * NOTES
390  *  Instead of base 0 it uses 10 as base.
391  *  Writes at most length characters to the string str.
392  *  Str is '\0' terminated when length allowes it.
393  *  When str fits exactly in length characters the '\0' is ommitted.
394  *  If value_ptr is NULL it crashes, as the native function does.
395  *
396  * DIFFERENCES
397  * - Accept base 0 as 10 instead of crashing as native function does.
398  * - The native function does produce garbage or STATUS_BUFFER_OVERFLOW for
399  *   base 2, 8 and 16 when the value is larger than 0xFFFFFFFF.
400  */
401 NTSTATUS WINAPI RtlLargeIntegerToChar(
402     const ULONGLONG *value_ptr, /* [I] Pointer to the value to be converted */
403     ULONG base,                 /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
404     ULONG length,               /* [I] Length of the str buffer in bytes */
405     PCHAR str)                  /* [O] Destination for the converted value */
406 {
407     ULONGLONG value = *value_ptr;
408     CHAR buffer[65];
409     PCHAR pos;
410     CHAR digit;
411     ULONG len;
412
413     if (base == 0) {
414         base = 10;
415     } else if (base != 2 && base != 8 && base != 10 && base != 16) {
416         return STATUS_INVALID_PARAMETER;
417     } /* if */
418
419     pos = &buffer[64];
420     *pos = '\0';
421
422     do {
423         pos--;
424         digit = value % base;
425         value = value / base;
426         if (digit < 10) {
427             *pos = '0' + digit;
428         } else {
429             *pos = 'A' + digit - 10;
430         } /* if */
431     } while (value != 0L);
432
433     len = &buffer[64] - pos;
434     if (len > length) {
435         return STATUS_BUFFER_OVERFLOW;
436     } else if (str == NULL) {
437         return STATUS_ACCESS_VIOLATION;
438     } else if (len == length) {
439         memcpy(str, pos, len);
440     } else {
441         memcpy(str, pos, len + 1);
442     } /* if */
443     return STATUS_SUCCESS;
444 }
445
446
447 /**************************************************************************
448  *      RtlInt64ToUnicodeString (NTDLL.@)
449  *
450  * Convert a large unsigned integer to a '\0' terminated unicode string.
451  *
452  * RETURNS
453  *  Success: STATUS_SUCCESS. str contains the converted number
454  *  Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
455  *           STATUS_BUFFER_OVERFLOW, if str is too small to hold the string
456  *                  (with the '\0' termination). In this case str->Length
457  *                  is set to the length, the string would have (which can
458  *                  be larger than the MaximumLength).
459  *
460  * NOTES
461  *  Instead of base 0 it uses 10 as base.
462  *  If str is NULL it crashes, as the native function does.
463  *
464  * DIFFERENCES
465  * - Accept base 0 as 10 instead of crashing as native function does.
466  * - Do not return STATUS_BUFFER_OVERFLOW when the string is long enough.
467  *   The native function does this when the string would be longer than 31
468  *   characters even when the string parameter is long enough.
469  * - The native function does produce garbage or STATUS_BUFFER_OVERFLOW for
470  *   base 2, 8 and 16 when the value is larger than 0xFFFFFFFF. 
471  */
472 NTSTATUS WINAPI RtlInt64ToUnicodeString(
473     ULONGLONG value,     /* [I] Value to be converted */
474     ULONG base,          /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
475     UNICODE_STRING *str) /* [O] Destination for the converted value */
476 {
477     WCHAR buffer[65];
478     PWCHAR pos;
479     WCHAR digit;
480
481     if (base == 0) {
482         base = 10;
483     } else if (base != 2 && base != 8 && base != 10 && base != 16) {
484         return STATUS_INVALID_PARAMETER;
485     } /* if */
486
487     pos = &buffer[64];
488     *pos = '\0';
489
490     do {
491         pos--;
492         digit = value % base;
493         value = value / base;
494         if (digit < 10) {
495             *pos = '0' + digit;
496         } else {
497             *pos = 'A' + digit - 10;
498         } /* if */
499     } while (value != 0L);
500
501     str->Length = (&buffer[64] - pos) * sizeof(WCHAR);
502     if (str->Length >= str->MaximumLength) {
503         return STATUS_BUFFER_OVERFLOW;
504     } else {
505         memcpy(str->Buffer, pos, str->Length + sizeof(WCHAR));
506     } /* if */
507     return STATUS_SUCCESS;
508 }
509
510
511 /******************************************************************************
512  *        _alldiv   (NTDLL.@)
513  *
514  * Divide two 64 bit unsigned integers.
515  *
516  * PARAMS
517  *  a [I] Initial number.
518  *  b [I] Number to multiply a by.
519  *
520  * RETURNS
521  *  The dividend of a and b.
522  */
523 LONGLONG WINAPI _alldiv( LONGLONG a, LONGLONG b )
524 {
525     return a / b;
526 }
527
528
529 /******************************************************************************
530  *        _allmul   (NTDLL.@)
531  *
532  * Multiply two 64 bit integers.
533  *
534  * PARAMS
535  *  a [I] Initial number.
536  *  b [I] Number to multiply a by.
537  *
538  * RETURNS
539  *  The product of a and b.
540  */
541 LONGLONG WINAPI _allmul( LONGLONG a, LONGLONG b )
542 {
543     return a * b;
544 }
545
546
547 /******************************************************************************
548  *        _allrem   (NTDLL.@)
549  *
550  * Calculate the remainder after dividing two 64 bit integers.
551  *
552  * PARAMS
553  *  a [I] Initial number.
554  *  b [I] Number to divide a by.
555  *
556  * RETURNS
557  *  The remainder of a divided by b.
558  */
559 LONGLONG WINAPI _allrem( LONGLONG a, LONGLONG b )
560 {
561     return a % b;
562 }
563
564
565 /******************************************************************************
566  *        _aulldiv   (NTDLL.@)
567  *
568  * Divide two 64 bit unsigned integers.
569  *
570  * PARAMS
571  *  a [I] Initial number.
572  *  b [I] Number to multiply a by.
573  *
574  * RETURNS
575  *  The dividend of a and b.
576  */
577 ULONGLONG WINAPI _aulldiv( ULONGLONG a, ULONGLONG b )
578 {
579     return a / b;
580 }
581
582
583 /******************************************************************************
584  *        _aullrem   (NTDLL.@)
585  *
586  * Calculate the remainder after dividing two 64 bit unsigned integers.
587  *
588  * PARAMS
589  *  a [I] Initial number.
590  *  b [I] Number to divide a by.
591  *
592  * RETURNS
593  *  The remainder of a divided by b.
594  */
595 ULONGLONG WINAPI _aullrem( ULONGLONG a, ULONGLONG b )
596 {
597     return a % b;
598 }