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