📄 rtl.c
字号:
/* * Copyright (C) 2003-2005 Pontus Fuchs, Giridhar Pemmasani * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program 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 General Public License for more details. * */#include "ntoskernel.h"wstdcall SIZE_T WIN_FUNC(RtlCompareMemory,3) (const void *a, const void *b, SIZE_T len){ size_t i; char *x, *y; ENTER1(""); x = (char *)a; y = (char *)b; /* MSDN says this should return number of bytes that compare as * equal. This can be interpretted as either all bytes that are * equal in 'len' bytes or that only until the bytes compare as * not equal. Initially we had it the former way, but Realtek driver * doesn't like it that way - it takes many attempts to associate * with WPA. ReactOS returns the number of bytes that are equal * upto when they compare as not equal. * According to lords at #reactos, that is the way it should be * and that msdn is wrong about it! */ for (i = 0; i < len && x[i] == y[i]; i++) ; return i;}wstdcall void WIN_FUNC(RtlCopyMemory,3) (void *dst, const void *src, SIZE_T length){ memcpy(dst, src, length);}wstdcall void WIN_FUNC(RtlZeroMemory,2) (void *dst, SIZE_T length){ memset(dst, 0, length);}wstdcall void WIN_FUNC(RtlSecureZeroMemory,2) (void *dst, SIZE_T length){ memset(dst, 0, length);}wstdcall void WIN_FUNC(RtlFillMemory,3) (void *dest, SIZE_T length, UCHAR fill){ memset(dest, fill, length);}wstdcall void WIN_FUNC(RtlMoveMemory,3) (void *dest, const void *src, SIZE_T length){ memmove(dest, src, length);}wstdcall LONG WIN_FUNC(RtlCompareString,3) (const struct ansi_string *s1, const struct ansi_string *s2, BOOLEAN case_insensitive){ unsigned int len; LONG ret = 0; const char *p1, *p2; ENTER2(""); len = min(s1->length, s2->length); p1 = s1->buf; p2 = s2->buf; if (case_insensitive) while (!ret && len--) ret = toupper(*p1++) - toupper(*p2++); else while (!ret && len--) ret = *p1++ - *p2++; if (!ret) ret = s1->length - s2->length; EXIT2(return ret);}wstdcall LONG WIN_FUNC(RtlCompareUnicodeString,3) (const struct unicode_string *s1, const struct unicode_string *s2, BOOLEAN case_insensitive){ unsigned int len; LONG ret = 0; const wchar_t *p1, *p2; ENTER2(""); len = min(s1->length, s2->length) / sizeof(wchar_t); p1 = s1->buf; p2 = s2->buf; if (case_insensitive) while (!ret && len--) ret = toupper((u8)*p1++) - toupper((u8)*p2++); else while (!ret && len--) ret = (u8)*p1++ - (u8)*p2++; if (!ret) ret = s1->length - s2->length; TRACE2("len: %d, ret: %d", len, ret); EXIT2(return ret);}wstdcall BOOLEAN WIN_FUNC(RtlEqualString,3) (const struct ansi_string *s1, const struct ansi_string *s2, BOOLEAN case_insensitive){ ENTER1(""); if (s1->length != s2->length) return FALSE; return !RtlCompareString(s1, s2, case_insensitive);}wstdcall BOOLEAN WIN_FUNC(RtlEqualUnicodeString,3) (const struct unicode_string *s1, const struct unicode_string *s2, BOOLEAN case_insensitive){ if (s1->length != s2->length) return FALSE; return !RtlCompareUnicodeString(s1, s2, case_insensitive);}wstdcall void WIN_FUNC(RtlCopyUnicodeString,2) (struct unicode_string *dst, struct unicode_string *src){ ENTER1("%p, %p", dst, src); if (src && src->buf && dst->buf) { dst->length = min(src->length, dst->max_length); memcpy(dst->buf, src->buf, dst->length); if (dst->length < dst->max_length) dst->buf[dst->length / sizeof(dst->buf[0])] = 0; } else dst->length = 0; EXIT1(return);}wstdcall void WIN_FUNC(RtlCopyString,2) (struct ansi_string *dst, struct ansi_string *src){ ENTER1("%p, %p", dst, src); if (src && src->buf && dst->buf) { dst->length = min(src->length, dst->max_length); memcpy(dst->buf, src->buf, dst->length); if (dst->length < dst->max_length) dst->buf[dst->length] = 0; } else dst->length = 0; EXIT1(return);}wstdcall NTSTATUS WIN_FUNC(RtlAppendUnicodeToString,2) (struct unicode_string *dst, wchar_t *src){ if (src) { int len; for (len = 0; src[len]; len++) ; if (dst->length + (len * sizeof(dst->buf[0])) > dst->max_length) return STATUS_BUFFER_TOO_SMALL; memcpy(&dst->buf[dst->length], src, len * sizeof(dst->buf[0])); dst->length += len * sizeof(dst->buf[0]); if (dst->max_length > dst->length) dst->buf[dst->length / sizeof(dst->buf[0])] = 0; } return STATUS_SUCCESS;}wstdcall NTSTATUS WIN_FUNC(RtlAppendUnicodeStringToString,2) (struct unicode_string *dst, struct unicode_string *src){ if (dst->max_length < src->length + dst->length) return STATUS_BUFFER_TOO_SMALL; if (src->length) { memcpy(&dst->buf[dst->length], src->buf, src->length); dst->length += src->length; if (dst->max_length > dst->length) dst->buf[dst->length / sizeof(dst->buf[0])] = 0; } EXIT2(return STATUS_SUCCESS);}wstdcall ULONG WIN_FUNC(RtlxAnsiStringToUnicodeSize,1) (const struct ansi_string *string){ int i; for (i = 0; i < string->max_length && string->buf[i]; i++) ; return (i * sizeof(wchar_t));}wstdcall ULONG WIN_FUNC(RtlxUnicodeStringToAnsiSize,1) (const struct unicode_string *string){ int i; for (i = 0; i < string->max_length && string->buf[i]; i++) ; return i;}wstdcall NTSTATUS WIN_FUNC(RtlAnsiStringToUnicodeString,3) (struct unicode_string *dst, const struct ansi_string *src, BOOLEAN alloc){ int i, n; n = RtlxAnsiStringToUnicodeSize(src); TRACE2("%d, %d, %d, %d, %p", n, dst->max_length, src->length, src->max_length, src->buf); if (alloc == TRUE) {#if 0 if (n == 0) { dst->length = dst->max_length = 0; dst->buf = NULL; EXIT2(return STATUS_SUCCESS); }#endif dst->max_length = n + sizeof(dst->buf[0]); dst->buf = ExAllocatePoolWithTag(NonPagedPool, dst->max_length, 0); if (!dst->buf) { dst->max_length = dst->length = 0; EXIT2(return STATUS_NO_MEMORY); } } else if (dst->max_length < n) EXIT2(return STATUS_BUFFER_TOO_SMALL); dst->length = n; n /= sizeof(dst->buf[0]); for (i = 0; i < n; i++) dst->buf[i] = src->buf[i]; if (i * sizeof(dst->buf[0]) < dst->max_length) dst->buf[i] = 0; TRACE2("dst: length: %d, max_length: %d, string: %p", dst->length, dst->max_length, src->buf); EXIT2(return STATUS_SUCCESS);}wstdcall NTSTATUS WIN_FUNC(RtlUnicodeStringToAnsiString,3) (struct ansi_string *dst, const struct unicode_string *src, BOOLEAN alloc){ int i, n; n = RtlxUnicodeStringToAnsiSize(src); TRACE2("%d, %d, %d, %d, %p", n, dst->max_length, src->length, src->max_length, src->buf); if (alloc == TRUE) {#if 0 if (n == 0) { dst->length = dst->max_length = 0; dst->buf = NULL; EXIT2(return STATUS_SUCCESS); }#endif dst->max_length = n + sizeof(dst->buf[0]); dst->buf = ExAllocatePoolWithTag(NonPagedPool, dst->max_length, 0); if (!dst->buf) { dst->max_length = dst->length = 0; EXIT1(return STATUS_NO_MEMORY); } } else if (dst->max_length < n) EXIT2(return STATUS_BUFFER_TOO_SMALL); dst->length = n; for (i = 0; i < n; i++) dst->buf[i] = src->buf[i]; if (i < dst->max_length) dst->buf[i] = 0; TRACE2("string: %p, len: %d(%d)", dst->buf, dst->length, dst->max_length); EXIT2(return STATUS_SUCCESS);}wstdcall NTSTATUS WIN_FUNC(RtlUnicodeStringToInteger,3) (struct unicode_string *ustring, ULONG base, ULONG *value){ int i, sign = 1; ULONG res; typeof(ustring->buf) string; if (ustring->length == 0) { *value = 0; return STATUS_SUCCESS; } string = ustring->buf; i = 0; while (i < (ustring->length / sizeof(*string)) && string[i] == ' ') i++; if (string[i] == '+') i++; else if (string[i] == '-') { i++; sign = -1; } if (base == 0) { base = 10; if (i <= ((ustring->length / sizeof(*string)) - 2) && string[i] == '0') { i++; if (string[i] == 'b') { base = 2; i++; } else if (string[i] == 'o') { base = 8; i++; } else if (string[i] == 'x') { base = 16; i++; } } } if (!(base == 2 || base == 8 || base == 10 || base == 16)) EXIT2(return STATUS_INVALID_PARAMETER); for (res = 0; i < (ustring->length / sizeof(*string)); i++) { int v; if (isdigit((char)string[i])) v = string[i] - '0'; else if (isxdigit((char)string[i])) v = tolower((char)string[i]) - 'a' + 10; else v = base; if (v >= base) EXIT2(return STATUS_INVALID_PARAMETER); res = res * base + v; } *value = sign * res; EXIT3(return STATUS_SUCCESS);}wstdcall NTSTATUS WIN_FUNC(RtlCharToInteger,3) (const char *string, ULONG base, ULONG *value)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -