Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygenbinhive.c
Go to the documentation of this file.
00001 /* 00002 * FreeLoader 00003 * 00004 * Copyright (C) 2001 Rex Jolliff 00005 * Copyright (C) 2001 Eric Kohl 00006 * 00007 * This program is free software; you can redistribute it and/or modify 00008 * it under the terms of the GNU General Public License as published by 00009 * the Free Software Foundation; either version 2 of the License, or 00010 * (at your option) any later version. 00011 * 00012 * This program is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 * GNU General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU General Public License along 00018 * with this program; if not, write to the Free Software Foundation, Inc., 00019 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 00020 */ 00021 00022 #include <freeldr.h> 00023 #include <cmlib.h> 00024 #include <debug.h> 00025 00026 #define REG_DATA_SIZE_MASK 0x7FFFFFFF 00027 #define REG_DATA_IN_OFFSET 0x80000000 00028 00029 DBG_DEFAULT_CHANNEL(REGISTRY); 00030 00031 /* FUNCTIONS ****************************************************************/ 00032 00033 static PVOID 00034 NTAPI 00035 CmpAllocate (SIZE_T Size, BOOLEAN Paged, ULONG Tag) 00036 { 00037 return MmHeapAlloc(Size); 00038 } 00039 00040 00041 static VOID 00042 NTAPI 00043 CmpFree (PVOID Ptr, IN ULONG Quota) 00044 { 00045 MmHeapFree(Ptr); 00046 } 00047 00048 static BOOLEAN 00049 RegImportValue (PHHIVE Hive, 00050 PCM_KEY_VALUE ValueCell, 00051 FRLDRHKEY Key) 00052 { 00053 PVOID DataCell; 00054 PWCHAR wName; 00055 LONG Error; 00056 ULONG DataLength; 00057 ULONG i; 00058 00059 if (ValueCell->Signature != CM_KEY_VALUE_SIGNATURE) 00060 { 00061 ERR("Invalid key cell!\n"); 00062 return FALSE; 00063 } 00064 00065 if (ValueCell->Flags & VALUE_COMP_NAME) 00066 { 00067 wName = MmHeapAlloc ((ValueCell->NameLength + 1)*sizeof(WCHAR)); 00068 for (i = 0; i < ValueCell->NameLength; i++) 00069 { 00070 wName[i] = ((PCHAR)ValueCell->Name)[i]; 00071 } 00072 wName[ValueCell->NameLength] = 0; 00073 } 00074 else 00075 { 00076 wName = MmHeapAlloc (ValueCell->NameLength + sizeof(WCHAR)); 00077 memcpy (wName, 00078 ValueCell->Name, 00079 ValueCell->NameLength); 00080 wName[ValueCell->NameLength / sizeof(WCHAR)] = 0; 00081 } 00082 00083 DataLength = ValueCell->DataLength & REG_DATA_SIZE_MASK; 00084 00085 TRACE("ValueName: '%S'\n", wName); 00086 TRACE("DataLength: %u\n", DataLength); 00087 00088 if (DataLength <= sizeof(HCELL_INDEX) && (ValueCell->DataLength & REG_DATA_IN_OFFSET)) 00089 { 00090 Error = RegSetValue(Key, 00091 wName, 00092 ValueCell->Type, 00093 (PCHAR)&ValueCell->Data, 00094 DataLength); 00095 if (Error != ERROR_SUCCESS) 00096 { 00097 ERR("RegSetValue() failed!\n"); 00098 MmHeapFree (wName); 00099 return FALSE; 00100 } 00101 } 00102 else 00103 { 00104 DataCell = (PVOID)HvGetCell (Hive, ValueCell->Data); 00105 TRACE("DataCell: %x\n", DataCell); 00106 00107 Error = RegSetValue (Key, 00108 wName, 00109 ValueCell->Type, 00110 DataCell, 00111 DataLength); 00112 00113 if (Error != ERROR_SUCCESS) 00114 { 00115 ERR("RegSetValue() failed!\n"); 00116 MmHeapFree (wName); 00117 return FALSE; 00118 } 00119 } 00120 00121 MmHeapFree (wName); 00122 00123 return TRUE; 00124 } 00125 00126 static BOOLEAN 00127 RegImportSubKey(PHHIVE Hive, 00128 PCM_KEY_NODE KeyCell, 00129 FRLDRHKEY ParentKey); 00130 00131 static BOOLEAN 00132 RegImportIndexSubKey(PHHIVE Hive, 00133 PCM_KEY_INDEX IndexCell, 00134 FRLDRHKEY ParentKey) 00135 { 00136 ULONG i; 00137 00138 TRACE("IndexCell: %x\n", IndexCell); 00139 00140 /* Enumerate and add subkeys */ 00141 if (IndexCell->Signature == CM_KEY_INDEX_ROOT || 00142 IndexCell->Signature == CM_KEY_INDEX_LEAF) 00143 { 00144 for (i = 0; i < IndexCell->Count; i++) 00145 { 00146 PCM_KEY_INDEX SubIndexCell = HvGetCell(Hive, IndexCell->List[i]); 00147 if (!RegImportIndexSubKey(Hive, SubIndexCell, ParentKey)) 00148 return FALSE; 00149 } 00150 } 00151 else if (IndexCell->Signature == CM_KEY_FAST_LEAF || 00152 IndexCell->Signature == CM_KEY_HASH_LEAF) 00153 { 00154 PCM_KEY_FAST_INDEX HashCell = (PCM_KEY_FAST_INDEX)IndexCell; 00155 for (i = 0; i < HashCell->Count; i++) 00156 { 00157 PCM_KEY_NODE SubKeyCell = HvGetCell(Hive, HashCell->List[i].Cell); 00158 if (!RegImportSubKey(Hive, SubKeyCell, ParentKey)) 00159 return FALSE; 00160 } 00161 } 00162 else 00163 { 00164 ASSERT(FALSE); 00165 } 00166 00167 return TRUE; 00168 } 00169 00170 00171 static BOOLEAN 00172 RegImportSubKey(PHHIVE Hive, 00173 PCM_KEY_NODE KeyCell, 00174 FRLDRHKEY ParentKey) 00175 { 00176 PCM_KEY_INDEX IndexCell; 00177 PVALUE_LIST_CELL ValueListCell; 00178 PCM_KEY_VALUE ValueCell = NULL; 00179 PWCHAR wName; 00180 FRLDRHKEY SubKey; 00181 LONG Error; 00182 ULONG i; 00183 00184 00185 TRACE("KeyCell: %x\n", KeyCell); 00186 TRACE("KeyCell->Signature: %x\n", KeyCell->Signature); 00187 if (KeyCell->Signature != CM_KEY_NODE_SIGNATURE) 00188 { 00189 ERR("Invalid key cell Signature!\n"); 00190 return FALSE; 00191 } 00192 00193 if (KeyCell->Flags & KEY_COMP_NAME) 00194 { 00195 wName = MmHeapAlloc ((KeyCell->NameLength + 1) * sizeof(WCHAR)); 00196 for (i = 0; i < KeyCell->NameLength; i++) 00197 { 00198 wName[i] = ((PCHAR)KeyCell->Name)[i]; 00199 } 00200 wName[KeyCell->NameLength] = 0; 00201 } 00202 else 00203 { 00204 wName = MmHeapAlloc (KeyCell->NameLength + sizeof(WCHAR)); 00205 memcpy (wName, 00206 KeyCell->Name, 00207 KeyCell->NameLength); 00208 wName[KeyCell->NameLength/sizeof(WCHAR)] = 0; 00209 } 00210 00211 TRACE("KeyName: '%S'\n", wName); 00212 00213 /* Create new sub key */ 00214 Error = RegCreateKey (ParentKey, 00215 wName, 00216 &SubKey); 00217 MmHeapFree (wName); 00218 if (Error != ERROR_SUCCESS) 00219 { 00220 ERR("RegCreateKey() failed!\n"); 00221 return FALSE; 00222 } 00223 TRACE("Subkeys: %u\n", KeyCell->SubKeyCounts); 00224 TRACE("Values: %u\n", KeyCell->ValueList.Count); 00225 00226 /* Enumerate and add values */ 00227 if (KeyCell->ValueList.Count > 0) 00228 { 00229 ValueListCell = (PVALUE_LIST_CELL) HvGetCell (Hive, KeyCell->ValueList.List); 00230 TRACE("ValueListCell: %x\n", ValueListCell); 00231 00232 for (i = 0; i < KeyCell->ValueList.Count; i++) 00233 { 00234 TRACE("ValueOffset[%d]: %x\n", i, ValueListCell->ValueOffset[i]); 00235 00236 ValueCell = (PCM_KEY_VALUE) HvGetCell (Hive, ValueListCell->ValueOffset[i]); 00237 00238 TRACE("ValueCell[%d]: %x\n", i, ValueCell); 00239 00240 if (!RegImportValue(Hive, ValueCell, SubKey)) 00241 return FALSE; 00242 } 00243 } 00244 00245 /* Enumerate and add subkeys */ 00246 if (KeyCell->SubKeyCounts[Stable] > 0) 00247 { 00248 IndexCell = HvGetCell (Hive, KeyCell->SubKeyLists[Stable]); 00249 00250 if (!RegImportIndexSubKey(Hive, IndexCell, SubKey)) 00251 return FALSE; 00252 } 00253 00254 return TRUE; 00255 } 00256 00257 00258 BOOLEAN 00259 RegImportBinaryHive(PCHAR ChunkBase, 00260 ULONG ChunkSize) 00261 { 00262 PCM_KEY_NODE KeyCell; 00263 PCM_KEY_FAST_INDEX HashCell; 00264 PCM_KEY_NODE SubKeyCell; 00265 FRLDRHKEY SystemKey; 00266 ULONG i; 00267 LONG Error; 00268 PCMHIVE CmHive; 00269 PHHIVE Hive; 00270 NTSTATUS Status; 00271 00272 TRACE("RegImportBinaryHive(%x, %u) called\n",ChunkBase,ChunkSize); 00273 00274 CmHive = CmpAllocate(sizeof(CMHIVE), TRUE, 0); 00275 Status = HvInitialize (&CmHive->Hive, 00276 HINIT_FLAT, 00277 0, 00278 0, 00279 ChunkBase, 00280 CmpAllocate, 00281 CmpFree, 00282 NULL, 00283 NULL, 00284 NULL, 00285 NULL, 00286 1, 00287 NULL); 00288 if (!NT_SUCCESS(Status)) 00289 { 00290 CmpFree(CmHive, 0); 00291 ERR("Invalid hive Signature!\n"); 00292 return FALSE; 00293 } 00294 00295 Hive = &CmHive->Hive; 00296 KeyCell = (PCM_KEY_NODE)HvGetCell (Hive, Hive->BaseBlock->RootCell); 00297 TRACE("KeyCell: %x\n", KeyCell); 00298 TRACE("KeyCell->Signature: %x\n", KeyCell->Signature); 00299 if (KeyCell->Signature != CM_KEY_NODE_SIGNATURE) 00300 { 00301 ERR("Invalid key cell Signature!\n"); 00302 return FALSE; 00303 } 00304 00305 TRACE("Subkeys: %u\n", KeyCell->SubKeyCounts); 00306 TRACE("Values: %u\n", KeyCell->ValueList.Count); 00307 00308 /* Open 'System' key */ 00309 Error = RegOpenKey(NULL, 00310 L"\\Registry\\Machine\\SYSTEM", 00311 &SystemKey); 00312 if (Error != ERROR_SUCCESS) 00313 { 00314 ERR("Failed to open 'system' key!\n"); 00315 return FALSE; 00316 } 00317 00318 /* Enumerate and add subkeys */ 00319 if (KeyCell->SubKeyCounts[Stable] > 0) 00320 { 00321 HashCell = (PCM_KEY_FAST_INDEX)HvGetCell (Hive, KeyCell->SubKeyLists[Stable]); 00322 TRACE("HashCell: %x\n", HashCell); 00323 TRACE("SubKeyCounts: %x\n", KeyCell->SubKeyCounts[Stable]); 00324 00325 for (i = 0; i < KeyCell->SubKeyCounts[Stable]; i++) 00326 { 00327 TRACE("Cell[%d]: %x\n", i, HashCell->List[i].Cell); 00328 00329 SubKeyCell = (PCM_KEY_NODE)HvGetCell (Hive, HashCell->List[i].Cell); 00330 00331 TRACE("SubKeyCell[%d]: %x\n", i, SubKeyCell); 00332 00333 if (!RegImportSubKey(Hive, SubKeyCell, SystemKey)) 00334 return FALSE; 00335 } 00336 } 00337 00338 return TRUE; 00339 } 00340 00341 /* EOF */ Generated on Sun May 27 2012 04:19:17 for ReactOS by
1.7.6.1
|