1*c2c66affSColin Finck ////////////////////////////////////////////////////////////////////
2*c2c66affSColin Finck // Copyright (C) Alexander Telyatnikov, Ivan Keliukh, Yegor Anchishkin, SKIF Software, 1999-2013. Kiev, Ukraine
3*c2c66affSColin Finck // All rights reserved
4*c2c66affSColin Finck // This file was released under the GPLv2 on June 2015.
5*c2c66affSColin Finck ////////////////////////////////////////////////////////////////////
6*c2c66affSColin Finck 
7*c2c66affSColin Finck extern "C"
8*c2c66affSColin Finck ULONG
MyRtlCompareMemory(PVOID s1,PVOID s2,ULONG len)9*c2c66affSColin Finck MyRtlCompareMemory(
10*c2c66affSColin Finck     PVOID s1,
11*c2c66affSColin Finck     PVOID s2,
12*c2c66affSColin Finck     ULONG len
13*c2c66affSColin Finck     )
14*c2c66affSColin Finck {
15*c2c66affSColin Finck     ULONG i;
16*c2c66affSColin Finck 
17*c2c66affSColin Finck     for(i=0; i<len; i++) {
18*c2c66affSColin Finck         if( ((char*)s1)[i] != ((char*)s2)[i] )
19*c2c66affSColin Finck             break;
20*c2c66affSColin Finck     }
21*c2c66affSColin Finck     return i;
22*c2c66affSColin Finck }
23*c2c66affSColin Finck 
24*c2c66affSColin Finck #define STRING_BUFFER_ALIGNMENT  (32)
25*c2c66affSColin Finck #define STRING_BUFFER_ALIGN(sz)  (((sz)+STRING_BUFFER_ALIGNMENT)&(~((ULONG)(STRING_BUFFER_ALIGNMENT-1))))
26*c2c66affSColin Finck 
27*c2c66affSColin Finck #ifndef NT_NATIVE_MODE
28*c2c66affSColin Finck 
29*c2c66affSColin Finck extern "C"
30*c2c66affSColin Finck ULONG
RtlCompareUnicodeString(PUNICODE_STRING s1,PUNICODE_STRING s2,BOOLEAN UpCase)31*c2c66affSColin Finck RtlCompareUnicodeString(
32*c2c66affSColin Finck     PUNICODE_STRING s1,
33*c2c66affSColin Finck     PUNICODE_STRING s2,
34*c2c66affSColin Finck     BOOLEAN UpCase
35*c2c66affSColin Finck     )
36*c2c66affSColin Finck {
37*c2c66affSColin Finck     ULONG i;
38*c2c66affSColin Finck 
39*c2c66affSColin Finck     if(s1->Length != s2->Length) return (-1);
40*c2c66affSColin Finck     i = memcmp(s1->Buffer, s2->Buffer, (s1->Length) ? (s1->Length) : (s2->Length));
41*c2c66affSColin Finck     return i;
42*c2c66affSColin Finck }
43*c2c66affSColin Finck 
44*c2c66affSColin Finck extern "C"
45*c2c66affSColin Finck NTSTATUS
RtlUpcaseUnicodeString(PUNICODE_STRING dst,PUNICODE_STRING src,BOOLEAN Alloc)46*c2c66affSColin Finck RtlUpcaseUnicodeString(
47*c2c66affSColin Finck     PUNICODE_STRING dst,
48*c2c66affSColin Finck     PUNICODE_STRING src,
49*c2c66affSColin Finck     BOOLEAN Alloc
50*c2c66affSColin Finck     )
51*c2c66affSColin Finck {
52*c2c66affSColin Finck //    if(s1->Length != s2->Length) return (-1);
53*c2c66affSColin Finck     memcpy(dst->Buffer, src->Buffer, src->Length);
54*c2c66affSColin Finck     dst->Buffer[src->Length/sizeof(WCHAR)] = 0;
55*c2c66affSColin Finck     dst->Length = src->Length;
56*c2c66affSColin Finck     _wcsupr(dst->Buffer);
57*c2c66affSColin Finck     return STATUS_SUCCESS;
58*c2c66affSColin Finck }
59*c2c66affSColin Finck 
60*c2c66affSColin Finck extern "C"
61*c2c66affSColin Finck NTSTATUS
RtlAppendUnicodeToString(IN PUNICODE_STRING Str1,IN PWSTR Str2)62*c2c66affSColin Finck RtlAppendUnicodeToString(
63*c2c66affSColin Finck     IN PUNICODE_STRING Str1,
64*c2c66affSColin Finck     IN PWSTR Str2
65*c2c66affSColin Finck     )
66*c2c66affSColin Finck {
67*c2c66affSColin Finck     PWCHAR tmp;
68*c2c66affSColin Finck     USHORT i;
69*c2c66affSColin Finck 
70*c2c66affSColin Finck #ifdef _X86_
71*c2c66affSColin Finck 
72*c2c66affSColin Finck     __asm push  ebx
73*c2c66affSColin Finck     __asm push  esi
74*c2c66affSColin Finck     __asm xor   ebx,ebx
75*c2c66affSColin Finck     __asm mov   esi,Str2
76*c2c66affSColin Finck Scan_1:
77*c2c66affSColin Finck     __asm cmp   [word ptr esi+ebx],0
78*c2c66affSColin Finck     __asm je    EO_Scan
79*c2c66affSColin Finck     __asm add   ebx,2
80*c2c66affSColin Finck     __asm jmp   Scan_1
81*c2c66affSColin Finck EO_Scan:
82*c2c66affSColin Finck     __asm mov   i,bx
83*c2c66affSColin Finck     __asm pop   esi
84*c2c66affSColin Finck     __asm pop   ebx
85*c2c66affSColin Finck 
86*c2c66affSColin Finck #else   // NO X86 optimization, use generic C/C++
87*c2c66affSColin Finck 
88*c2c66affSColin Finck     i=0;
89*c2c66affSColin Finck     while(Str2[i]) {
90*c2c66affSColin Finck        i++;
91*c2c66affSColin Finck     }
92*c2c66affSColin Finck     i *= sizeof(WCHAR);
93*c2c66affSColin Finck 
94*c2c66affSColin Finck #endif // _X86_
95*c2c66affSColin Finck 
96*c2c66affSColin Finck     tmp = Str1->Buffer;
97*c2c66affSColin Finck     ASSERT(Str1->MaximumLength);
98*c2c66affSColin Finck     if((Str1->Length+i+sizeof(WCHAR)) > Str1->MaximumLength) {
99*c2c66affSColin Finck         PWCHAR tmp2 = (PWCHAR)ExAllocatePoolWithTag(NonPagedPool, STRING_BUFFER_ALIGN(i + Str1->Length + sizeof(WCHAR))*2, 'ilTS');
100*c2c66affSColin Finck         if(!tmp2)
101*c2c66affSColin Finck             return STATUS_INSUFFICIENT_RESOURCES;
102*c2c66affSColin Finck         memcpy(tmp2, tmp, Str1->MaximumLength);
103*c2c66affSColin Finck         ExFreePool(tmp);
104*c2c66affSColin Finck         tmp = tmp2;
105*c2c66affSColin Finck         Str1->MaximumLength = STRING_BUFFER_ALIGN(i + sizeof(WCHAR))*2;
106*c2c66affSColin Finck         Str1->Buffer = tmp;
107*c2c66affSColin Finck     }
108*c2c66affSColin Finck     RtlCopyMemory(((PCHAR)tmp)+Str1->Length, Str2, i);
109*c2c66affSColin Finck     i+=Str1->Length;
110*c2c66affSColin Finck     tmp[(i / sizeof(WCHAR))] = 0;
111*c2c66affSColin Finck     Str1->Length = i;
112*c2c66affSColin Finck 
113*c2c66affSColin Finck     return STATUS_SUCCESS;
114*c2c66affSColin Finck 
115*c2c66affSColin Finck #undef UDF_UNC_STR_TAG
116*c2c66affSColin Finck 
117*c2c66affSColin Finck } // end RtlAppendUnicodeToString()
118*c2c66affSColin Finck 
119*c2c66affSColin Finck #endif //NT_NATIVE_MODE
120*c2c66affSColin Finck 
121*c2c66affSColin Finck #ifdef CDRW_W32
122*c2c66affSColin Finck NTSTATUS
MyInitUnicodeString(IN PUNICODE_STRING Str1,IN PCWSTR Str2)123*c2c66affSColin Finck MyInitUnicodeString(
124*c2c66affSColin Finck     IN PUNICODE_STRING Str1,
125*c2c66affSColin Finck     IN PCWSTR Str2
126*c2c66affSColin Finck     )
127*c2c66affSColin Finck {
128*c2c66affSColin Finck 
129*c2c66affSColin Finck     USHORT i;
130*c2c66affSColin Finck 
131*c2c66affSColin Finck #ifdef _X86_
132*c2c66affSColin Finck 
133*c2c66affSColin Finck     __asm push  ebx
134*c2c66affSColin Finck     __asm push  esi
135*c2c66affSColin Finck     __asm xor   ebx,ebx
136*c2c66affSColin Finck     __asm mov   esi,Str2
137*c2c66affSColin Finck Scan_1:
138*c2c66affSColin Finck     __asm cmp   [word ptr esi+ebx],0
139*c2c66affSColin Finck     __asm je    EO_Scan
140*c2c66affSColin Finck     __asm add   ebx,2
141*c2c66affSColin Finck     __asm jmp   Scan_1
142*c2c66affSColin Finck EO_Scan:
143*c2c66affSColin Finck     __asm mov   i,bx
144*c2c66affSColin Finck     __asm pop   esi
145*c2c66affSColin Finck     __asm pop   ebx
146*c2c66affSColin Finck 
147*c2c66affSColin Finck #else   // NO X86 optimization, use generic C/C++
148*c2c66affSColin Finck 
149*c2c66affSColin Finck     i=0;
150*c2c66affSColin Finck     while(Str2[i]) {
151*c2c66affSColin Finck        i++;
152*c2c66affSColin Finck     }
153*c2c66affSColin Finck     i *= sizeof(WCHAR);
154*c2c66affSColin Finck 
155*c2c66affSColin Finck #endif // _X86_
156*c2c66affSColin Finck 
157*c2c66affSColin Finck     Str1->MaximumLength = STRING_BUFFER_ALIGN((Str1->Length = i) + sizeof(WCHAR));
158*c2c66affSColin Finck     Str1->Buffer = (PWCHAR)MyAllocatePool__(NonPagedPool, Str1->MaximumLength);
159*c2c66affSColin Finck     if(!Str1->Buffer)
160*c2c66affSColin Finck         return STATUS_INSUFFICIENT_RESOURCES;
161*c2c66affSColin Finck     RtlCopyMemory(Str1->Buffer, Str2, i);
162*c2c66affSColin Finck     Str1->Buffer[i/sizeof(WCHAR)] = 0;
163*c2c66affSColin Finck     return STATUS_SUCCESS;
164*c2c66affSColin Finck 
165*c2c66affSColin Finck } // end MyInitUnicodeString()
166*c2c66affSColin Finck #endif //CDRW_W32
167