|
| 1 | +/* |
| 2 | + * PROJECT: FreeLoader |
| 3 | + * LICENSE: GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+) |
| 4 | + * PURPOSE: NT Kernel Load Options Support Functions |
| 5 | + * COPYRIGHT: Copyright 2020 Hermes Belusca-Maito |
| 6 | + */ |
| 7 | + |
| 8 | +/* INCLUDES ******************************************************************/ |
| 9 | + |
| 10 | +#include <freeldr.h> |
| 11 | +#include "ntldropts.h" |
| 12 | + |
| 13 | +/* FUNCTIONS *****************************************************************/ |
| 14 | + |
| 15 | +PCSTR |
| 16 | +NtLdrGetNextOption( |
| 17 | + IN OUT PCSTR* Options, |
| 18 | + OUT PULONG OptionLength OPTIONAL) |
| 19 | +{ |
| 20 | + PCSTR NextOption; |
| 21 | + PCSTR Option = NULL; |
| 22 | + ULONG Length = 0; |
| 23 | + |
| 24 | + if (OptionLength) |
| 25 | + *OptionLength = 0; |
| 26 | + |
| 27 | + if (!Options || !*Options) |
| 28 | + return NULL; |
| 29 | + |
| 30 | + /* Loop over each option */ |
| 31 | + NextOption = *Options; |
| 32 | + while (*NextOption) |
| 33 | + { |
| 34 | + /* Skip possible initial whitespace */ |
| 35 | + NextOption += strspn(NextOption, " \t"); |
| 36 | + |
| 37 | + /* Stop now if we have already found an option. |
| 38 | + * NextOption points to the next following option. */ |
| 39 | + if (Option) |
| 40 | + break; |
| 41 | + |
| 42 | + /* |
| 43 | + * Check whether a new option starts. Options are delimited |
| 44 | + * with an option separator '/' or with whitespace. |
| 45 | + */ |
| 46 | + if (*NextOption == '/') |
| 47 | + ++NextOption; |
| 48 | + |
| 49 | + /* Get the actual length of the option until |
| 50 | + * the next whitespace or option separator. */ |
| 51 | + Length = (ULONG)strcspn(NextOption, " \t/"); |
| 52 | + |
| 53 | + /* Retrieve the option if present and go to the beginning of the next one */ |
| 54 | + if (Length != 0) |
| 55 | + Option = NextOption; |
| 56 | + |
| 57 | + /* Restart after the end of the option */ |
| 58 | + NextOption += Length; |
| 59 | + } |
| 60 | + |
| 61 | + *Options = NextOption; |
| 62 | + if (Option && OptionLength) |
| 63 | + *OptionLength = Length; |
| 64 | + return Option; |
| 65 | +} |
| 66 | + |
| 67 | +/* |
| 68 | + * OptionName specifies the option name, without any leading |
| 69 | + * option separator '/', to search for within the Options. |
| 70 | + * The search is made case-insensitive. |
| 71 | + */ |
| 72 | +PCSTR |
| 73 | +NtLdrGetOptionExN( |
| 74 | + IN PCSTR Options, |
| 75 | + IN PCCH OptionName, |
| 76 | + IN ULONG OptNameLength, |
| 77 | + OUT PULONG OptionLength OPTIONAL) |
| 78 | +{ |
| 79 | + PCSTR NextOptions; |
| 80 | + PCSTR Option = NULL; |
| 81 | + ULONG OptLength = 0; |
| 82 | + |
| 83 | + if (OptionLength) |
| 84 | + *OptionLength = 0; |
| 85 | + |
| 86 | + if (!Options || !*Options) |
| 87 | + return NULL; |
| 88 | + if (!OptionName || (OptNameLength == 0) || !*OptionName) |
| 89 | + return NULL; |
| 90 | + |
| 91 | + NextOptions = Options; |
| 92 | + while ((Option = NtLdrGetNextOption(&NextOptions, &OptLength))) |
| 93 | + { |
| 94 | + /* |
| 95 | + * Check whether the option to find exactly matches the current |
| 96 | + * load option, or is a prefix thereof if this is an option with |
| 97 | + * appended data. |
| 98 | + */ |
| 99 | + if ((OptLength >= OptNameLength) && |
| 100 | + (_strnicmp(Option, OptionName, OptNameLength) == 0)) |
| 101 | + { |
| 102 | + if ((OptLength == OptNameLength) || |
| 103 | + (OptionName[OptNameLength-1] == '=') || |
| 104 | + (OptionName[OptNameLength-1] == ':')) |
| 105 | + { |
| 106 | + break; |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + if (Option && OptionLength) |
| 112 | + *OptionLength = OptLength; |
| 113 | + return Option; |
| 114 | +} |
| 115 | + |
| 116 | +PCSTR |
| 117 | +NtLdrGetOptionEx( |
| 118 | + IN PCSTR Options, |
| 119 | + IN PCSTR OptionName, |
| 120 | + OUT PULONG OptionLength OPTIONAL) |
| 121 | +{ |
| 122 | + return NtLdrGetOptionExN(Options, OptionName, strlen(OptionName), OptionLength); |
| 123 | +} |
| 124 | + |
| 125 | +PCSTR |
| 126 | +NtLdrGetOption( |
| 127 | + IN PCSTR Options, |
| 128 | + IN PCSTR OptionName) |
| 129 | +{ |
| 130 | + return NtLdrGetOptionEx(Options, OptionName, NULL); |
| 131 | +} |
0 commit comments