1 /* 2 * COPYRIGHT: See COPYING in the top level directory 3 * PROJECT: ReactOS system libraries 4 * FILE: dll/win32/kernel32/client/console/alias.c 5 * PURPOSE: Win32 Console Client Alias support functions 6 * PROGRAMMERS: David Welch (welch@cwcom.net) (welch@mcmail.com) 7 * Christoph von Wittich (christoph_vw@reactos.org) 8 * Johannes Anderwald (johannes.anderwald@reactos.org) 9 */ 10 11 /* INCLUDES *******************************************************************/ 12 13 #include <k32.h> 14 15 #define NDEBUG 16 #include <debug.h> 17 18 19 /* FUNCTIONS ******************************************************************/ 20 21 static BOOL 22 IntAddConsoleAlias(LPCVOID Source, 23 USHORT SourceBufferLength, 24 LPCVOID Target, 25 USHORT TargetBufferLength, 26 LPCVOID lpExeName, 27 BOOLEAN bUnicode) 28 { 29 CONSOLE_API_MESSAGE ApiMessage; 30 PCONSOLE_ADDGETALIAS ConsoleAliasRequest = &ApiMessage.Data.ConsoleAliasRequest; 31 PCSR_CAPTURE_BUFFER CaptureBuffer; 32 ULONG CapturedStrings; 33 34 USHORT NumChars = (USHORT)(lpExeName ? (bUnicode ? wcslen(lpExeName) : strlen(lpExeName)) : 0); 35 36 if (lpExeName == NULL || NumChars == 0) 37 { 38 SetLastError(ERROR_INVALID_PARAMETER); 39 return FALSE; 40 } 41 42 ConsoleAliasRequest->ConsoleHandle = NtCurrentPeb()->ProcessParameters->ConsoleHandle; 43 44 /* Determine the needed sizes */ 45 ConsoleAliasRequest->SourceLength = SourceBufferLength; 46 ConsoleAliasRequest->ExeLength = NumChars * (bUnicode ? sizeof(WCHAR) : sizeof(CHAR)); 47 ConsoleAliasRequest->Unicode = 48 ConsoleAliasRequest->Unicode2 = bUnicode; 49 50 CapturedStrings = 2; 51 52 if (Target) /* The target can be optional */ 53 { 54 ConsoleAliasRequest->TargetLength = TargetBufferLength; 55 CapturedStrings++; 56 } 57 else 58 { 59 ConsoleAliasRequest->TargetLength = 0; 60 } 61 62 /* Allocate a Capture Buffer */ 63 CaptureBuffer = CsrAllocateCaptureBuffer(CapturedStrings, 64 ConsoleAliasRequest->SourceLength + 65 ConsoleAliasRequest->ExeLength + 66 ConsoleAliasRequest->TargetLength); 67 if (CaptureBuffer == NULL) 68 { 69 DPRINT1("CsrAllocateCaptureBuffer failed!\n"); 70 SetLastError(ERROR_NOT_ENOUGH_MEMORY); 71 return FALSE; 72 } 73 74 /* Capture the strings */ 75 CsrCaptureMessageBuffer(CaptureBuffer, 76 (PVOID)Source, 77 ConsoleAliasRequest->SourceLength, 78 (PVOID*)&ConsoleAliasRequest->Source); 79 80 CsrCaptureMessageBuffer(CaptureBuffer, 81 (PVOID)lpExeName, 82 ConsoleAliasRequest->ExeLength, 83 (PVOID*)&ConsoleAliasRequest->ExeName); 84 85 if (Target) /* The target can be optional */ 86 { 87 CsrCaptureMessageBuffer(CaptureBuffer, 88 (PVOID)Target, 89 ConsoleAliasRequest->TargetLength, 90 (PVOID*)&ConsoleAliasRequest->Target); 91 } 92 else 93 { 94 ConsoleAliasRequest->Target = NULL; 95 } 96 97 CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage, 98 CaptureBuffer, 99 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepAddAlias), 100 sizeof(*ConsoleAliasRequest)); 101 102 CsrFreeCaptureBuffer(CaptureBuffer); 103 104 if (!NT_SUCCESS(ApiMessage.Status)) 105 { 106 BaseSetLastNTError(ApiMessage.Status); 107 return FALSE; 108 } 109 110 return TRUE; 111 } 112 113 114 /* 115 * @implemented 116 */ 117 BOOL 118 WINAPI 119 DECLSPEC_HOTPATCH 120 AddConsoleAliasW(LPCWSTR lpSource, 121 LPCWSTR lpTarget, 122 LPCWSTR lpExeName) 123 { 124 USHORT SourceBufferLength = (USHORT)wcslen(lpSource) * sizeof(WCHAR); 125 USHORT TargetBufferLength = (USHORT)(lpTarget ? wcslen(lpTarget) * sizeof(WCHAR) : 0); 126 127 DPRINT("AddConsoleAliasW entered with lpSource '%S' lpTarget '%S' lpExeName '%S'\n", 128 lpSource, lpTarget, lpExeName); 129 130 return IntAddConsoleAlias(lpSource, 131 SourceBufferLength, 132 lpTarget, 133 TargetBufferLength, 134 lpExeName, 135 TRUE); 136 } 137 138 139 /* 140 * @implemented 141 */ 142 BOOL 143 WINAPI 144 DECLSPEC_HOTPATCH 145 AddConsoleAliasA(LPCSTR lpSource, 146 LPCSTR lpTarget, 147 LPCSTR lpExeName) 148 { 149 USHORT SourceBufferLength = (USHORT)strlen(lpSource) * sizeof(CHAR); 150 USHORT TargetBufferLength = (USHORT)(lpTarget ? strlen(lpTarget) * sizeof(CHAR) : 0); 151 152 DPRINT("AddConsoleAliasA entered with lpSource '%s' lpTarget '%s' lpExeName '%s'\n", 153 lpSource, lpTarget, lpExeName); 154 155 return IntAddConsoleAlias(lpSource, 156 SourceBufferLength, 157 lpTarget, 158 TargetBufferLength, 159 lpExeName, 160 FALSE); 161 } 162 163 164 static DWORD 165 IntGetConsoleAlias(LPVOID Source, 166 USHORT SourceBufferLength, 167 LPVOID Target, 168 USHORT TargetBufferLength, 169 LPVOID lpExeName, 170 BOOLEAN bUnicode) 171 { 172 CONSOLE_API_MESSAGE ApiMessage; 173 PCONSOLE_ADDGETALIAS ConsoleAliasRequest = &ApiMessage.Data.ConsoleAliasRequest; 174 PCSR_CAPTURE_BUFFER CaptureBuffer; 175 176 USHORT NumChars = (USHORT)(lpExeName ? (bUnicode ? wcslen(lpExeName) : strlen(lpExeName)) : 0); 177 178 if (Source == NULL || Target == NULL) 179 { 180 SetLastError(ERROR_INVALID_PARAMETER); 181 return 0; 182 } 183 184 if (lpExeName == NULL || NumChars == 0) 185 { 186 SetLastError(ERROR_INVALID_PARAMETER); 187 return 0; 188 } 189 190 ConsoleAliasRequest->ConsoleHandle = NtCurrentPeb()->ProcessParameters->ConsoleHandle; 191 192 /* Determine the needed sizes */ 193 ConsoleAliasRequest->SourceLength = SourceBufferLength; 194 ConsoleAliasRequest->ExeLength = NumChars * (bUnicode ? sizeof(WCHAR) : sizeof(CHAR)); 195 ConsoleAliasRequest->Unicode = 196 ConsoleAliasRequest->Unicode2 = bUnicode; 197 198 ConsoleAliasRequest->TargetLength = TargetBufferLength; 199 200 /* Allocate a Capture Buffer */ 201 CaptureBuffer = CsrAllocateCaptureBuffer(3, ConsoleAliasRequest->SourceLength + 202 ConsoleAliasRequest->ExeLength + 203 ConsoleAliasRequest->TargetLength); 204 if (!CaptureBuffer) 205 { 206 DPRINT1("CsrAllocateCaptureBuffer failed!\n"); 207 SetLastError(ERROR_NOT_ENOUGH_MEMORY); 208 return 0; 209 } 210 211 /* Capture the strings */ 212 CsrCaptureMessageBuffer(CaptureBuffer, 213 (PVOID)Source, 214 ConsoleAliasRequest->SourceLength, 215 (PVOID*)&ConsoleAliasRequest->Source); 216 217 CsrCaptureMessageBuffer(CaptureBuffer, 218 (PVOID)lpExeName, 219 ConsoleAliasRequest->ExeLength, 220 (PVOID*)&ConsoleAliasRequest->ExeName); 221 222 /* Allocate space for the target buffer */ 223 CsrAllocateMessagePointer(CaptureBuffer, 224 ConsoleAliasRequest->TargetLength, 225 (PVOID*)&ConsoleAliasRequest->Target); 226 227 CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage, 228 CaptureBuffer, 229 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepGetAlias), 230 sizeof(*ConsoleAliasRequest)); 231 if (!NT_SUCCESS(ApiMessage.Status)) 232 { 233 CsrFreeCaptureBuffer(CaptureBuffer); 234 BaseSetLastNTError(ApiMessage.Status); 235 236 if (ApiMessage.Status == STATUS_BUFFER_TOO_SMALL) 237 return ConsoleAliasRequest->TargetLength; 238 else 239 return 0; 240 } 241 242 /* Copy the returned target string into the user buffer */ 243 RtlCopyMemory(Target, 244 ConsoleAliasRequest->Target, 245 ConsoleAliasRequest->TargetLength); 246 247 /* Release the capture buffer and exit */ 248 CsrFreeCaptureBuffer(CaptureBuffer); 249 250 return ConsoleAliasRequest->TargetLength; 251 } 252 253 254 /* 255 * @implemented 256 */ 257 DWORD 258 WINAPI 259 DECLSPEC_HOTPATCH 260 GetConsoleAliasW(LPWSTR lpSource, 261 LPWSTR lpTargetBuffer, 262 DWORD TargetBufferLength, 263 LPWSTR lpExeName) 264 { 265 DPRINT("GetConsoleAliasW entered with lpSource '%S' lpExeName '%S'\n", 266 lpSource, lpExeName); 267 268 return IntGetConsoleAlias(lpSource, 269 (USHORT)wcslen(lpSource) * sizeof(WCHAR), 270 lpTargetBuffer, 271 TargetBufferLength, 272 lpExeName, 273 TRUE); 274 } 275 276 277 /* 278 * @implemented 279 */ 280 DWORD 281 WINAPI 282 DECLSPEC_HOTPATCH 283 GetConsoleAliasA(LPSTR lpSource, 284 LPSTR lpTargetBuffer, 285 DWORD TargetBufferLength, 286 LPSTR lpExeName) 287 { 288 DPRINT("GetConsoleAliasA entered with lpSource '%s' lpExeName '%s'\n", 289 lpSource, lpExeName); 290 291 return IntGetConsoleAlias(lpSource, 292 (USHORT)strlen(lpSource) * sizeof(CHAR), 293 lpTargetBuffer, 294 TargetBufferLength, 295 lpExeName, 296 FALSE); 297 } 298 299 300 static DWORD 301 IntGetConsoleAliases(LPVOID AliasBuffer, 302 DWORD AliasBufferLength, 303 LPVOID lpExeName, 304 BOOLEAN bUnicode) 305 { 306 CONSOLE_API_MESSAGE ApiMessage; 307 PCONSOLE_GETALLALIASES GetAllAliasesRequest = &ApiMessage.Data.GetAllAliasesRequest; 308 PCSR_CAPTURE_BUFFER CaptureBuffer; 309 310 USHORT NumChars = (USHORT)(lpExeName ? (bUnicode ? wcslen(lpExeName) : strlen(lpExeName)) : 0); 311 312 if (lpExeName == NULL || NumChars == 0) 313 { 314 SetLastError(ERROR_INVALID_PARAMETER); 315 return 0; 316 } 317 318 GetAllAliasesRequest->ConsoleHandle = NtCurrentPeb()->ProcessParameters->ConsoleHandle; 319 320 /* Determine the needed sizes */ 321 GetAllAliasesRequest->ExeLength = NumChars * (bUnicode ? sizeof(WCHAR) : sizeof(CHAR)); 322 GetAllAliasesRequest->Unicode = 323 GetAllAliasesRequest->Unicode2 = bUnicode; 324 325 GetAllAliasesRequest->AliasesBufferLength = AliasBufferLength; 326 327 /* Allocate a Capture Buffer */ 328 CaptureBuffer = CsrAllocateCaptureBuffer(2, GetAllAliasesRequest->ExeLength + 329 GetAllAliasesRequest->AliasesBufferLength); 330 if (!CaptureBuffer) 331 { 332 DPRINT1("CsrAllocateCaptureBuffer failed!\n"); 333 SetLastError(ERROR_NOT_ENOUGH_MEMORY); 334 return 0; 335 } 336 337 /* Capture the exe name and allocate space for the aliases buffer */ 338 CsrCaptureMessageBuffer(CaptureBuffer, 339 (PVOID)lpExeName, 340 GetAllAliasesRequest->ExeLength, 341 (PVOID*)&GetAllAliasesRequest->ExeName); 342 343 CsrAllocateMessagePointer(CaptureBuffer, 344 GetAllAliasesRequest->AliasesBufferLength, 345 (PVOID*)&GetAllAliasesRequest->AliasesBuffer); 346 347 CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage, 348 CaptureBuffer, 349 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepGetAliases), 350 sizeof(*GetAllAliasesRequest)); 351 if (!NT_SUCCESS(ApiMessage.Status)) 352 { 353 CsrFreeCaptureBuffer(CaptureBuffer); 354 BaseSetLastNTError(ApiMessage.Status); 355 return 0; 356 } 357 358 /* Copy the returned aliases string into the user buffer */ 359 RtlCopyMemory(AliasBuffer, 360 GetAllAliasesRequest->AliasesBuffer, 361 GetAllAliasesRequest->AliasesBufferLength); 362 363 /* Release the capture buffer and exit */ 364 CsrFreeCaptureBuffer(CaptureBuffer); 365 366 return GetAllAliasesRequest->AliasesBufferLength; 367 } 368 369 370 /* 371 * @implemented 372 */ 373 DWORD 374 WINAPI 375 DECLSPEC_HOTPATCH 376 GetConsoleAliasesW(LPWSTR AliasBuffer, 377 DWORD AliasBufferLength, 378 LPWSTR ExeName) 379 { 380 DPRINT("GetConsoleAliasesW entered with lpExeName '%S'\n", 381 ExeName); 382 383 return IntGetConsoleAliases(AliasBuffer, 384 AliasBufferLength, 385 ExeName, 386 TRUE); 387 } 388 389 390 /* 391 * @implemented 392 */ 393 DWORD 394 WINAPI 395 DECLSPEC_HOTPATCH 396 GetConsoleAliasesA(LPSTR AliasBuffer, 397 DWORD AliasBufferLength, 398 LPSTR ExeName) 399 { 400 DPRINT("GetConsoleAliasesA entered with lpExeName '%s'\n", 401 ExeName); 402 403 return IntGetConsoleAliases(AliasBuffer, 404 AliasBufferLength, 405 ExeName, 406 FALSE); 407 } 408 409 410 static DWORD 411 IntGetConsoleAliasesLength(LPVOID lpExeName, BOOLEAN bUnicode) 412 { 413 CONSOLE_API_MESSAGE ApiMessage; 414 PCONSOLE_GETALLALIASESLENGTH GetAllAliasesLengthRequest = &ApiMessage.Data.GetAllAliasesLengthRequest; 415 PCSR_CAPTURE_BUFFER CaptureBuffer; 416 417 USHORT NumChars = (USHORT)(lpExeName ? (bUnicode ? wcslen(lpExeName) : strlen(lpExeName)) : 0); 418 419 if (lpExeName == NULL || NumChars == 0) 420 { 421 SetLastError(ERROR_INVALID_PARAMETER); 422 return 0; 423 } 424 425 GetAllAliasesLengthRequest->ConsoleHandle = NtCurrentPeb()->ProcessParameters->ConsoleHandle; 426 GetAllAliasesLengthRequest->ExeLength = NumChars * (bUnicode ? sizeof(WCHAR) : sizeof(CHAR)); 427 GetAllAliasesLengthRequest->Unicode = 428 GetAllAliasesLengthRequest->Unicode2 = bUnicode; 429 430 CaptureBuffer = CsrAllocateCaptureBuffer(1, GetAllAliasesLengthRequest->ExeLength); 431 if (!CaptureBuffer) 432 { 433 DPRINT1("CsrAllocateCaptureBuffer failed!\n"); 434 SetLastError(ERROR_NOT_ENOUGH_MEMORY); 435 return 0; 436 } 437 438 CsrCaptureMessageBuffer(CaptureBuffer, 439 (PVOID)lpExeName, 440 GetAllAliasesLengthRequest->ExeLength, 441 (PVOID)&GetAllAliasesLengthRequest->ExeName); 442 443 CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage, 444 CaptureBuffer, 445 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepGetAliasesLength), 446 sizeof(*GetAllAliasesLengthRequest)); 447 448 CsrFreeCaptureBuffer(CaptureBuffer); 449 450 if (!NT_SUCCESS(ApiMessage.Status)) 451 { 452 BaseSetLastNTError(ApiMessage.Status); 453 return 0; 454 } 455 456 return GetAllAliasesLengthRequest->Length; 457 } 458 459 460 /* 461 * @implemented 462 */ 463 DWORD 464 WINAPI 465 DECLSPEC_HOTPATCH 466 GetConsoleAliasesLengthW(LPWSTR lpExeName) 467 { 468 return IntGetConsoleAliasesLength(lpExeName, TRUE); 469 } 470 471 472 /* 473 * @implemented 474 */ 475 DWORD 476 WINAPI 477 DECLSPEC_HOTPATCH 478 GetConsoleAliasesLengthA(LPSTR lpExeName) 479 { 480 return IntGetConsoleAliasesLength(lpExeName, FALSE); 481 } 482 483 484 static DWORD 485 IntGetConsoleAliasExes(PVOID lpExeNameBuffer, 486 DWORD ExeNameBufferLength, 487 BOOLEAN bUnicode) 488 { 489 CONSOLE_API_MESSAGE ApiMessage; 490 PCONSOLE_GETALIASESEXES GetAliasesExesRequest = &ApiMessage.Data.GetAliasesExesRequest; 491 PCSR_CAPTURE_BUFFER CaptureBuffer; 492 493 GetAliasesExesRequest->ConsoleHandle = NtCurrentPeb()->ProcessParameters->ConsoleHandle; 494 GetAliasesExesRequest->Length = ExeNameBufferLength; 495 GetAliasesExesRequest->Unicode = bUnicode; 496 497 CaptureBuffer = CsrAllocateCaptureBuffer(1, ExeNameBufferLength); 498 if (!CaptureBuffer) 499 { 500 DPRINT1("CsrAllocateCaptureBuffer failed!\n"); 501 SetLastError(ERROR_NOT_ENOUGH_MEMORY); 502 return 0; 503 } 504 505 CsrAllocateMessagePointer(CaptureBuffer, 506 ExeNameBufferLength, 507 (PVOID*)&GetAliasesExesRequest->ExeNames); 508 509 CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage, 510 CaptureBuffer, 511 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepGetAliasExes), 512 sizeof(*GetAliasesExesRequest)); 513 if (!NT_SUCCESS(ApiMessage.Status)) 514 { 515 CsrFreeCaptureBuffer(CaptureBuffer); 516 BaseSetLastNTError(ApiMessage.Status); 517 return 0; 518 } 519 520 RtlCopyMemory(lpExeNameBuffer, 521 GetAliasesExesRequest->ExeNames, 522 GetAliasesExesRequest->Length); 523 524 CsrFreeCaptureBuffer(CaptureBuffer); 525 526 return GetAliasesExesRequest->Length; 527 } 528 529 /* 530 * @implemented 531 */ 532 DWORD 533 WINAPI 534 DECLSPEC_HOTPATCH 535 GetConsoleAliasExesW(LPWSTR lpExeNameBuffer, 536 DWORD ExeNameBufferLength) 537 { 538 DPRINT("GetConsoleAliasExesW called\n"); 539 return IntGetConsoleAliasExes(lpExeNameBuffer, ExeNameBufferLength, TRUE); 540 } 541 542 543 /* 544 * @implemented 545 */ 546 DWORD 547 WINAPI 548 DECLSPEC_HOTPATCH 549 GetConsoleAliasExesA(LPSTR lpExeNameBuffer, 550 DWORD ExeNameBufferLength) 551 { 552 DPRINT("GetConsoleAliasExesA called\n"); 553 return IntGetConsoleAliasExes(lpExeNameBuffer, ExeNameBufferLength, FALSE); 554 } 555 556 557 static DWORD 558 IntGetConsoleAliasExesLength(BOOLEAN bUnicode) 559 { 560 CONSOLE_API_MESSAGE ApiMessage; 561 PCONSOLE_GETALIASESEXESLENGTH GetAliasesExesLengthRequest = &ApiMessage.Data.GetAliasesExesLengthRequest; 562 563 GetAliasesExesLengthRequest->ConsoleHandle = NtCurrentPeb()->ProcessParameters->ConsoleHandle; 564 GetAliasesExesLengthRequest->Unicode = bUnicode; 565 566 CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage, 567 NULL, 568 CSR_CREATE_API_NUMBER(CONSRV_SERVERDLL_INDEX, ConsolepGetAliasExesLength), 569 sizeof(*GetAliasesExesLengthRequest)); 570 if (!NT_SUCCESS(ApiMessage.Status)) 571 { 572 BaseSetLastNTError(ApiMessage.Status); 573 return 0; 574 } 575 576 return GetAliasesExesLengthRequest->Length; 577 } 578 579 580 /* 581 * @implemented 582 */ 583 DWORD 584 WINAPI 585 DECLSPEC_HOTPATCH 586 GetConsoleAliasExesLengthW(VOID) 587 { 588 DPRINT("GetConsoleAliasExesLengthW called\n"); 589 return IntGetConsoleAliasExesLength(TRUE); 590 } 591 592 593 /* 594 * @implemented 595 */ 596 DWORD 597 WINAPI 598 DECLSPEC_HOTPATCH 599 GetConsoleAliasExesLengthA(VOID) 600 { 601 DPRINT("GetConsoleAliasExesLengthA called\n"); 602 return IntGetConsoleAliasExesLength(FALSE); 603 } 604 605 /* EOF */ 606