large_int.c 15.5 KB
Newer Older
1 2 3 4
/*
 * Large integer functions
 *
 * Copyright 2000 Alexandre Julliard
5
 * Copyright 2003 Thomas Mertes
6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 21
 */

22 23 24
#include <stdarg.h>

#include "ntstatus.h"
25
#define WIN32_NO_STATUS
26
#include "windef.h"
27
#include "winternl.h"
28 29 30 31 32 33 34 35 36 37 38 39 40

/*
 * Note: we use LONGLONG instead of LARGE_INTEGER, because
 * the latter is a structure and the calling convention for
 * returning a structure would not be binary-compatible.
 *
 * FIXME: for platforms that don't have a native LONGLONG type,
 * we should define LONGLONG as a structure similar to LARGE_INTEGER
 * and do everything by hand. You are welcome to do it...
 */

/******************************************************************************
 *        RtlLargeIntegerAdd   (NTDLL.@)
Jon Griffiths's avatar
Jon Griffiths committed
41 42 43 44 45 46 47 48 49
 *
 * Add two 64 bit integers.
 *
 * PARAMS
 *  a [I] Initial number.
 *  b [I] Number to add to a.
 *
 * RETURNS
 *  The sum of a and b.
50 51 52 53 54 55 56 57 58
 */
LONGLONG WINAPI RtlLargeIntegerAdd( LONGLONG a, LONGLONG b )
{
    return a + b;
}


/******************************************************************************
 *        RtlLargeIntegerSubtract   (NTDLL.@)
Jon Griffiths's avatar
Jon Griffiths committed
59 60 61 62 63 64 65 66 67
 *
 * Subtract two 64 bit integers.
 *
 * PARAMS
 *  a [I] Initial number.
 *  b [I] Number to subtract from a.
 *
 * RETURNS
 *  The difference of a and b.
68 69 70 71 72 73 74 75 76
 */
LONGLONG WINAPI RtlLargeIntegerSubtract( LONGLONG a, LONGLONG b )
{
    return a - b;
}


/******************************************************************************
 *        RtlLargeIntegerNegate   (NTDLL.@)
Jon Griffiths's avatar
Jon Griffiths committed
77 78 79 80 81 82 83 84
 *
 * Negate a 64 bit integer.
 *
 * PARAMS
 *  a     [I] Initial number.
 *
 * RETURNS
 *  The value of a negated.
85 86 87 88 89 90 91 92 93
 */
LONGLONG WINAPI RtlLargeIntegerNegate( LONGLONG a )
{
    return -a;
}


/******************************************************************************
 *        RtlLargeIntegerShiftLeft   (NTDLL.@)
Jon Griffiths's avatar
Jon Griffiths committed
94 95 96 97 98 99 100 101 102
 *
 * Perform a shift left on a 64 bit integer.
 *
 * PARAMS
 *  a     [I] Initial number.
 *  count [I] Number of bits to shift by
 *
 * RETURNS
 *  The value of a following the shift.
103 104 105 106 107 108 109 110 111
 */
LONGLONG WINAPI RtlLargeIntegerShiftLeft( LONGLONG a, INT count )
{
    return a << count;
}


/******************************************************************************
 *        RtlLargeIntegerShiftRight   (NTDLL.@)
Jon Griffiths's avatar
Jon Griffiths committed
112 113 114 115 116 117 118 119 120
 *
 * Perform a shift right on a 64 bit integer.
 *
 * PARAMS
 *  a     [I] Initial number.
 *  count [I] Number of bits to shift by
 *
 * RETURNS
 *  The value of a following the shift.
121 122 123 124 125 126 127 128 129
 */
LONGLONG WINAPI RtlLargeIntegerShiftRight( LONGLONG a, INT count )
{
    return (ULONGLONG)a >> count;
}


/******************************************************************************
 *        RtlLargeIntegerArithmeticShift   (NTDLL.@)
Jon Griffiths's avatar
Jon Griffiths committed
130 131 132 133 134 135 136 137 138
 *
 * Perform an arithmetic shift right on a 64 bit integer.
 *
 * PARAMS
 *  a     [I] Initial number.
 *  count [I] Number of bits to shift by
 *
 * RETURNS
 *  The value of a following the shift.
139 140 141 142 143 144 145 146 147 148 149
 */
LONGLONG WINAPI RtlLargeIntegerArithmeticShift( LONGLONG a, INT count )
{
    /* FIXME: gcc does arithmetic shift here, but it may not be true on all platforms */
    return a >> count;
}


/******************************************************************************
 *        RtlLargeIntegerDivide   (NTDLL.@)
 *
Jon Griffiths's avatar
Jon Griffiths committed
150 151 152 153 154 155 156 157 158 159 160 161
 * Divide one 64 bit unsigned integer by another, with remainder.
 *
 * PARAMS
 *  a   [I] Initial number.
 *  b   [I] Number to divide a by
 *  rem [O] Destination for remainder
 *
 * RETURNS
 *  The dividend of a and b. If rem is non-NULL it is set to the remainder.
 *
 * FIXME
 *  Should it be signed division instead?
162 163 164 165 166 167 168 169 170 171 172
 */
ULONGLONG WINAPI RtlLargeIntegerDivide( ULONGLONG a, ULONGLONG b, ULONGLONG *rem )
{
    ULONGLONG ret = a / b;
    if (rem) *rem = a - ret * b;
    return ret;
}


/******************************************************************************
 *        RtlConvertLongToLargeInteger   (NTDLL.@)
Jon Griffiths's avatar
Jon Griffiths committed
173 174 175 176 177 178 179 180
 *
 * Convert a 32 bit integer into 64 bits.
 *
 * PARAMS
 *  a [I] Number to convert
 *
 * RETURNS
 *  a.
181 182 183 184 185 186 187 188 189
 */
LONGLONG WINAPI RtlConvertLongToLargeInteger( LONG a )
{
    return a;
}


/******************************************************************************
 *        RtlConvertUlongToLargeInteger   (NTDLL.@)
Jon Griffiths's avatar
Jon Griffiths committed
190 191 192 193 194 195 196 197
 *
 * Convert a 32 bit unsigned integer into 64 bits.
 *
 * PARAMS
 *  a [I] Number to convert
 *
 * RETURNS
 *  a.
198 199 200 201 202 203 204 205 206
 */
ULONGLONG WINAPI RtlConvertUlongToLargeInteger( ULONG a )
{
    return a;
}


/******************************************************************************
 *        RtlEnlargedIntegerMultiply   (NTDLL.@)
Jon Griffiths's avatar
Jon Griffiths committed
207 208 209 210 211 212 213 214 215
 *
 * Multiply two integers giving a 64 bit integer result.
 *
 * PARAMS
 *  a [I] Initial number.
 *  b [I] Number to multiply a by.
 *
 * RETURNS
 *  The product of a and b.
216 217 218 219 220 221 222 223 224
 */
LONGLONG WINAPI RtlEnlargedIntegerMultiply( INT a, INT b )
{
    return (LONGLONG)a * b;
}


/******************************************************************************
 *        RtlEnlargedUnsignedMultiply   (NTDLL.@)
Jon Griffiths's avatar
Jon Griffiths committed
225 226 227 228 229 230 231 232 233
 *
 * Multiply two unsigned integers giving a 64 bit unsigned integer result.
 *
 * PARAMS
 *  a [I] Initial number.
 *  b [I] Number to multiply a by.
 *
 * RETURNS
 *  The product of a and b.
234
 */
235
ULONGLONG WINAPI RtlEnlargedUnsignedMultiply( UINT a, UINT b )
236 237 238 239 240 241 242
{
    return (ULONGLONG)a * b;
}


/******************************************************************************
 *        RtlEnlargedUnsignedDivide   (NTDLL.@)
Jon Griffiths's avatar
Jon Griffiths committed
243 244 245 246 247 248 249 250 251 252
 *
 * Divide one 64 bit unsigned integer by a 32 bit unsigned integer, with remainder.
 *
 * PARAMS
 *  a      [I] Initial number.
 *  b      [I] Number to divide a by
 *  remptr [O] Destination for remainder
 *
 * RETURNS
 *  The dividend of a and b. If remptr is non-NULL it is set to the remainder.
253 254 255 256
 */
UINT WINAPI RtlEnlargedUnsignedDivide( ULONGLONG a, UINT b, UINT *remptr )
{
#if defined(__i386__) && defined(__GNUC__)
257 258 259 260 261
    UINT ret, rem, p1, p2;

    p1 = a >> 32;
    p2 = a &  0xffffffffLL;

262 263
    __asm__("div %4,%%eax"
            : "=a" (ret), "=d" (rem)
264
            : "0" (p2), "1" (p1), "g" (b) );
265 266 267 268 269 270 271 272 273 274 275 276
    if (remptr) *remptr = rem;
    return ret;
#else
    UINT ret = a / b;
    if (remptr) *remptr = a % b;
    return ret;
#endif
}


/******************************************************************************
 *        RtlExtendedLargeIntegerDivide   (NTDLL.@)
Jon Griffiths's avatar
Jon Griffiths committed
277 278 279 280 281 282 283 284 285 286
 *
 * Divide one 64 bit integer by a 32 bit integer, with remainder.
 *
 * PARAMS
 *  a   [I] Initial number.
 *  b   [I] Number to divide a by
 *  rem [O] Destination for remainder
 *
 * RETURNS
 *  The dividend of a and b. If rem is non-NULL it is set to the remainder.
287 288 289 290 291 292 293 294 295 296 297
 */
LONGLONG WINAPI RtlExtendedLargeIntegerDivide( LONGLONG a, INT b, INT *rem )
{
    LONGLONG ret = a / b;
    if (rem) *rem = a - b * ret;
    return ret;
}


/******************************************************************************
 *        RtlExtendedIntegerMultiply   (NTDLL.@)
Jon Griffiths's avatar
Jon Griffiths committed
298 299 300 301 302 303 304 305 306
 *
 * Multiply one 64 bit integer by another 32 bit integer.
 *
 * PARAMS
 *  a [I] Initial number.
 *  b [I] Number to multiply a by.
 *
 * RETURNS
 *  The product of a and b.
307 308 309 310 311 312 313 314 315 316
 */
LONGLONG WINAPI RtlExtendedIntegerMultiply( LONGLONG a, INT b )
{
    return a * b;
}


/******************************************************************************
 *        RtlExtendedMagicDivide   (NTDLL.@)
 *
317 318
 * Allows replacing a division by a longlong constant with a multiplication by
 * the inverse constant.
319
 *
320
 * RETURNS
321
 *  (dividend * inverse_divisor) >> (64 + shift)
322
 *
323
 * NOTES
324 325 326 327
 *  If the divisor of a division is constant, the constants inverse_divisor and
 *  shift must be chosen such that inverse_divisor = 2^(64 + shift) / divisor.
 *  Then we have RtlExtendedMagicDivide(dividend,inverse_divisor,shift) ==
 *  dividend * inverse_divisor / 2^(64 + shift) == dividend / divisor.
328
 *
329 330
 *  The Parameter inverse_divisor although defined as LONGLONG is used as
 *  ULONGLONG.
331
 */
332 333 334
#define LOWER_32(A) ((A) & 0xffffffff)
#define UPPER_32(A) ((A) >> 32)
LONGLONG WINAPI RtlExtendedMagicDivide(
335 336 337
    LONGLONG dividend,        /* [I] Dividend to be divided by the constant divisor */
    LONGLONG inverse_divisor, /* [I] Constant computed manually as 2^(64+shift) / divisor */
    INT shift)                /* [I] Constant shift chosen to make inverse_divisor as big as possible for 64 bits */
338
{
339 340 341 342
    ULONGLONG dividend_high;
    ULONGLONG dividend_low;
    ULONGLONG inverse_divisor_high;
    ULONGLONG inverse_divisor_low;
343 344 345 346 347
    ULONGLONG ah_bl;
    ULONGLONG al_bh;
    LONGLONG result;
    int positive;

348 349 350
    if (dividend < 0) {
	dividend_high = UPPER_32((ULONGLONG) -dividend);
	dividend_low =  LOWER_32((ULONGLONG) -dividend);
351 352
	positive = 0;
    } else {
353 354
	dividend_high = UPPER_32((ULONGLONG) dividend);
	dividend_low =  LOWER_32((ULONGLONG) dividend);
355 356
	positive = 1;
    } /* if */
357 358
    inverse_divisor_high = UPPER_32((ULONGLONG) inverse_divisor);
    inverse_divisor_low =  LOWER_32((ULONGLONG) inverse_divisor);
359

360 361
    ah_bl = dividend_high * inverse_divisor_low;
    al_bh = dividend_low * inverse_divisor_high;
362

363
    result = (LONGLONG) ((dividend_high * inverse_divisor_high +
364 365
	    UPPER_32(ah_bl) +
	    UPPER_32(al_bh) +
366 367
	    UPPER_32(LOWER_32(ah_bl) + LOWER_32(al_bh) +
		     UPPER_32(dividend_low * inverse_divisor_low))) >> shift);
368 369 370 371 372 373 374 375 376 377 378 379 380 381

    if (positive) {
	return result;
    } else {
	return -result;
    } /* if */
}


/******************************************************************************
 *      RtlLargeIntegerToChar	[NTDLL.@]
 *
 * Convert an unsigned large integer to a character string.
 *
382 383 384 385 386 387 388 389 390
 * RETURNS
 *  Success: STATUS_SUCCESS. str contains the converted number
 *  Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
 *           STATUS_BUFFER_OVERFLOW, if str would be larger than length.
 *           STATUS_ACCESS_VIOLATION, if str is NULL.
 *
 * NOTES
 *  Instead of base 0 it uses 10 as base.
 *  Writes at most length characters to the string str.
391 392
 *  Str is '\0' terminated when length allows it.
 *  When str fits exactly in length characters the '\0' is omitted.
393
 *  If value_ptr is NULL it crashes, as the native function does.
394
 *
395 396 397
 * DIFFERENCES
 * - Accept base 0 as 10 instead of crashing as native function does.
 * - The native function does produce garbage or STATUS_BUFFER_OVERFLOW for
Jon Griffiths's avatar
Jon Griffiths committed
398
 *   base 2, 8 and 16 when the value is larger than 0xFFFFFFFF.
399 400
 */
NTSTATUS WINAPI RtlLargeIntegerToChar(
401 402 403 404
    const ULONGLONG *value_ptr, /* [I] Pointer to the value to be converted */
    ULONG base,                 /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
    ULONG length,               /* [I] Length of the str buffer in bytes */
    PCHAR str)                  /* [O] Destination for the converted value */
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
{
    ULONGLONG value = *value_ptr;
    CHAR buffer[65];
    PCHAR pos;
    CHAR digit;
    ULONG len;

    if (base == 0) {
	base = 10;
    } else if (base != 2 && base != 8 && base != 10 && base != 16) {
	return STATUS_INVALID_PARAMETER;
    } /* if */

    pos = &buffer[64];
    *pos = '\0';

    do {
	pos--;
	digit = value % base;
	value = value / base;
	if (digit < 10) {
	    *pos = '0' + digit;
	} else {
	    *pos = 'A' + digit - 10;
	} /* if */
    } while (value != 0L);

    len = &buffer[64] - pos;
    if (len > length) {
	return STATUS_BUFFER_OVERFLOW;
    } else if (str == NULL) {
	return STATUS_ACCESS_VIOLATION;
    } else if (len == length) {
	memcpy(str, pos, len);
    } else {
	memcpy(str, pos, len + 1);
    } /* if */
    return STATUS_SUCCESS;
}


/**************************************************************************
 *      RtlInt64ToUnicodeString (NTDLL.@)
 *
449 450 451 452 453 454 455 456 457
 * Convert a large unsigned integer to a '\0' terminated unicode string.
 *
 * RETURNS
 *  Success: STATUS_SUCCESS. str contains the converted number
 *  Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
 *           STATUS_BUFFER_OVERFLOW, if str is too small to hold the string
 *                  (with the '\0' termination). In this case str->Length
 *                  is set to the length, the string would have (which can
 *                  be larger than the MaximumLength).
458
 *
459 460 461
 * NOTES
 *  Instead of base 0 it uses 10 as base.
 *  If str is NULL it crashes, as the native function does.
462
 *
463 464
 * DIFFERENCES
 * - Accept base 0 as 10 instead of crashing as native function does.
465
 * - Do not return STATUS_BUFFER_OVERFLOW when the string is long enough.
466
 *   The native function does this when the string would be longer than 31
467
 *   characters even when the string parameter is long enough.
468
 * - The native function does produce garbage or STATUS_BUFFER_OVERFLOW for
469
 *   base 2, 8 and 16 when the value is larger than 0xFFFFFFFF. 
470
 */
471
NTSTATUS WINAPI RtlInt64ToUnicodeString(
472 473 474
    ULONGLONG value,     /* [I] Value to be converted */
    ULONG base,          /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
    UNICODE_STRING *str) /* [O] Destination for the converted value */
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
{
    WCHAR buffer[65];
    PWCHAR pos;
    WCHAR digit;

    if (base == 0) {
	base = 10;
    } else if (base != 2 && base != 8 && base != 10 && base != 16) {
	return STATUS_INVALID_PARAMETER;
    } /* if */

    pos = &buffer[64];
    *pos = '\0';

    do {
	pos--;
	digit = value % base;
	value = value / base;
	if (digit < 10) {
	    *pos = '0' + digit;
	} else {
	    *pos = 'A' + digit - 10;
	} /* if */
    } while (value != 0L);

    str->Length = (&buffer[64] - pos) * sizeof(WCHAR);
    if (str->Length >= str->MaximumLength) {
	return STATUS_BUFFER_OVERFLOW;
    } else {
504
	memcpy(str->Buffer, pos, str->Length + sizeof(WCHAR));
505 506 507
    } /* if */
    return STATUS_SUCCESS;
}
508 509 510 511


/******************************************************************************
 *        _alldiv   (NTDLL.@)
Jon Griffiths's avatar
Jon Griffiths committed
512 513 514 515 516 517 518 519 520
 *
 * Divide two 64 bit unsigned integers.
 *
 * PARAMS
 *  a [I] Initial number.
 *  b [I] Number to multiply a by.
 *
 * RETURNS
 *  The dividend of a and b.
521 522 523 524 525 526 527 528 529
 */
LONGLONG WINAPI _alldiv( LONGLONG a, LONGLONG b )
{
    return a / b;
}


/******************************************************************************
 *        _allmul   (NTDLL.@)
Jon Griffiths's avatar
Jon Griffiths committed
530 531 532 533 534 535 536 537 538
 *
 * Multiply two 64 bit integers.
 *
 * PARAMS
 *  a [I] Initial number.
 *  b [I] Number to multiply a by.
 *
 * RETURNS
 *  The product of a and b.
539 540 541 542 543 544 545 546 547
 */
LONGLONG WINAPI _allmul( LONGLONG a, LONGLONG b )
{
    return a * b;
}


/******************************************************************************
 *        _allrem   (NTDLL.@)
Jon Griffiths's avatar
Jon Griffiths committed
548 549 550 551 552 553 554 555 556
 *
 * Calculate the remainder after dividing two 64 bit integers.
 *
 * PARAMS
 *  a [I] Initial number.
 *  b [I] Number to divide a by.
 *
 * RETURNS
 *  The remainder of a divided by b.
557 558 559 560 561 562 563 564 565
 */
LONGLONG WINAPI _allrem( LONGLONG a, LONGLONG b )
{
    return a % b;
}


/******************************************************************************
 *        _aulldiv   (NTDLL.@)
Jon Griffiths's avatar
Jon Griffiths committed
566 567 568 569 570 571 572 573 574
 *
 * Divide two 64 bit unsigned integers.
 *
 * PARAMS
 *  a [I] Initial number.
 *  b [I] Number to multiply a by.
 *
 * RETURNS
 *  The dividend of a and b.
575 576 577 578 579 580 581 582 583
 */
ULONGLONG WINAPI _aulldiv( ULONGLONG a, ULONGLONG b )
{
    return a / b;
}


/******************************************************************************
 *        _aullrem   (NTDLL.@)
Jon Griffiths's avatar
Jon Griffiths committed
584 585 586 587 588 589 590 591 592
 *
 * Calculate the remainder after dividing two 64 bit unsigned integers.
 *
 * PARAMS
 *  a [I] Initial number.
 *  b [I] Number to divide a by.
 *
 * RETURNS
 *  The remainder of a divided by b.
593 594 595 596 597
 */
ULONGLONG WINAPI _aullrem( ULONGLONG a, ULONGLONG b )
{
    return a % b;
}