4 * Copyright (C) 2001 Rex Jolliff
5 * Copyright (C) 2001 Eric Kohl
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include "../freeldr.h"
23 #include "../stdlib.h"
24 #include "../memory.h"
31 #define REGISTRY_FILE_MAGIC "REGEDIT4"
33 #define INVALID_HANDLE_VALUE NULL
37 checkAndSkipMagic (PCHAR regChunk
)
39 if (strncmp (regChunk
,
41 strlen (REGISTRY_FILE_MAGIC
)) != 0)
44 printf("incorrect magic number in registry chunk. expected: %s got:%.*s\n",
46 strlen (REGISTRY_FILE_MAGIC
),
51 regChunk
+= strlen (REGISTRY_FILE_MAGIC
);
53 printf("Found registry chunk magic value\n");
60 skipWhitespaceInChunk (PCHAR regChunk
)
62 while (*regChunk
&& isspace (*regChunk
))
65 return *regChunk
? regChunk
: 0;
69 computeKeyNameSize (PCHAR regChunk
)
73 while (*regChunk
!= 0 && *regChunk
!= ']')
83 allocateKeyName(PCHAR
*newKeyName
, int newKeySize
)
85 if (*newKeyName
!= NULL
)
86 FreeMemory(*newKeyName
);
88 *newKeyName
= AllocateMemory(newKeySize
+ 1);
89 if (*newKeyName
== NULL
)
92 memset(*newKeyName
, 0, newKeySize
+ 1);
98 skipToNextKeyInChunk (PCHAR regChunk
)
100 while (*regChunk
!= 0 && *regChunk
!= '[')
102 while (*regChunk
!= 0 && *regChunk
!= '\n')
109 return *regChunk
? regChunk
: 0;
113 getKeyNameFromChunk (PCHAR regChunk
, PCHAR newKeyName
)
117 while (*regChunk
!= 0 && *regChunk
!= ']')
119 newKeyName
[index
++] = *regChunk
++;
121 newKeyName
[index
] = '\0';
123 return *regChunk
? regChunk
: 0;
127 createNewKey (PCHAR newKeyName
)
129 HKEY handleToReturn
= NULL
;
132 printf("Adding new key '%s'\n", newKeyName
);
140 printf(" returned handle: 0x%x\n", handleToReturn
);
143 return handleToReturn
;
147 skipToNextKeyValueInChunk (PCHAR regChunk
)
149 while (*regChunk
!= 0 && *regChunk
!= '\n')
151 regChunk
= skipWhitespaceInChunk (regChunk
);
157 computeKeyValueNameSize (PCHAR regChunk
)
161 if (*regChunk
!= '\"')
164 while (*regChunk
!= 0 && *regChunk
!= '\"')
170 return regChunk
? size
: 0;
174 getKeyValueNameFromChunk (PCHAR regChunk
, PCHAR newKeyName
)
179 while (*regChunk
!= 0 && *regChunk
!= '\"')
181 newKeyName
[index
++] = *regChunk
++;
183 newKeyName
[index
] = '\0';
186 return *regChunk
? regChunk
: 0;
190 getKeyValueTypeFromChunk (PCHAR regChunk
, PCHAR dataFormat
, int *keyValueType
)
192 if (*regChunk
== '\"')
194 strcpy (dataFormat
, "string");
195 *keyValueType
= REG_SZ
;
197 else if (strncmp (regChunk
, "hex", 3) == 0)
199 strcpy (dataFormat
, "hex");
201 if (*regChunk
== '(')
204 *keyValueType
= atoi (regChunk
);
205 while (*regChunk
!= 0 && *regChunk
!= ')')
210 *keyValueType
= REG_BINARY
;
211 if (*regChunk
== ':')
214 else if (strncmp (regChunk
, "dword", 5) == 0)
216 strcpy (dataFormat
, "dword");
217 *keyValueType
= REG_DWORD
;
219 if (*regChunk
== ':')
227 return *regChunk
? regChunk
: 0;
231 computeKeyValueDataSize (PCHAR regChunk
, PCHAR dataFormat
)
235 if (strcmp (dataFormat
, "string") == 0)
238 while (*regChunk
!= 0 && *regChunk
!= '\"')
245 else if (strcmp (dataFormat
, "hex") == 0)
247 while (*regChunk
!= 0 && isxdigit(*regChunk
))
252 if (*regChunk
== ',')
255 if (*regChunk
== '\\')
258 regChunk
= skipWhitespaceInChunk (regChunk
);
263 else if (strcmp (dataFormat
, "dword") == 0)
265 dataSize
= sizeof(DWORD
);
266 while (*regChunk
!= 0 && isxdigit(*regChunk
))
280 allocateDataBuffer (PVOID
* data
, int * dataBufferSize
, int dataSize
)
282 if (*dataBufferSize
< dataSize
)
284 if (*dataBufferSize
> 0)
286 *data
= AllocateMemory(dataSize
);
287 *dataBufferSize
= dataSize
;
294 getKeyValueDataFromChunk (PCHAR regChunk
, PCHAR dataFormat
, PCHAR data
)
300 if (strcmp (dataFormat
, "string") == 0)
302 /* convert quoted string to zero-terminated Unicode string */
305 while (*regChunk
!= 0 && *regChunk
!= '\"')
307 *ptr
++ = (CHAR
)*regChunk
++;
312 else if (strcmp (dataFormat
, "hex") == 0)
314 while (*regChunk
!= 0 && isxdigit (*regChunk
))
316 dataValue
= (isdigit (*regChunk
) ? *regChunk
- '0' :
317 tolower(*regChunk
) - 'a') << 4;
319 dataValue
+= (isdigit (*regChunk
) ? *regChunk
- '0' :
320 tolower(*regChunk
) - 'a');
323 if (*regChunk
== ',')
326 if (*regChunk
== '\\')
329 regChunk
= skipWhitespaceInChunk (regChunk
);
334 else if (strcmp (dataFormat
, "dword") == 0)
337 while (*regChunk
!= 0 && isxdigit(*regChunk
))
339 dataValue
= (isdigit (*regChunk
) ? *regChunk
- '0' :
340 tolower(*regChunk
) - 'a');
341 ulValue
= (ulValue
<< 4) + dataValue
;
344 memcpy(data
, &ulValue
, sizeof(ULONG
));
351 return *regChunk
? regChunk
: 0;
355 setKeyValue (HKEY currentKey
,
364 printf("Adding value (%s) to current key, with data type %d size %d\n",
369 status
= RegSetValue(currentKey
,
374 if (status
!= ERROR_SUCCESS
)
377 printf("could not set key value, rc:%d\n", status
);
387 RegImportHive(PCHAR ChunkBase
,
390 HKEY currentKey
= NULL
;
392 char *newKeyName
= NULL
;
393 char dataFormat
[10];
396 int dataBufferSize
= 0;
401 printf("ChunkBase 0x%x ChunkSize %x\n", ChunkBase
, ChunkSize
);
404 regChunk
= checkAndSkipMagic (ChunkBase
);
408 while (regChunk
!= 0 && *regChunk
!= 0 && (((ULONG
)regChunk
-(ULONG
)ChunkBase
) < ChunkSize
))
410 regChunk
= skipWhitespaceInChunk (regChunk
);
414 if (*regChunk
== '[')
417 printf("Line: %s\n", regChunk
);
419 if (currentKey
!= NULL
)
422 printf("Closing current key: 0x%lx\n", currentKey
);
429 newKeySize
= computeKeyNameSize (regChunk
);
430 if (!allocateKeyName (&newKeyName
, newKeySize
))
436 regChunk
= getKeyNameFromChunk (regChunk
, newKeyName
);
440 currentKey
= createNewKey (newKeyName
);
441 if (currentKey
== NULL
)
443 regChunk
= skipToNextKeyInChunk (regChunk
);
451 if (currentKey
== NULL
)
453 regChunk
= skipToNextKeyInChunk (regChunk
);
457 newKeySize
= computeKeyValueNameSize(regChunk
);
458 if (!allocateKeyName (&newKeyName
, newKeySize
))
464 regChunk
= getKeyValueNameFromChunk (regChunk
, newKeyName
);
468 if (*regChunk
!= '=')
470 regChunk
= skipToNextKeyValueInChunk (regChunk
);
475 regChunk
= getKeyValueTypeFromChunk (regChunk
, dataFormat
, &keyValueType
);
479 dataSize
= computeKeyValueDataSize (regChunk
, dataFormat
);
480 if (!allocateDataBuffer (&data
, &dataBufferSize
, dataSize
))
486 regChunk
= getKeyValueDataFromChunk (regChunk
, dataFormat
, data
);
490 if (!setKeyValue (currentKey
, newKeyName
, keyValueType
, data
, dataSize
))
498 if (newKeyName
!= NULL
)
500 FreeMemory(newKeyName
);
514 bprintf(char *buffer
, char *format
, ... )
516 int *dataptr
= (int *) &format
;
517 char c
, *ptr
, str
[16];
522 while ((c
= *(format
++)))
530 switch (c
= *(format
++))
532 case 'd': case 'u': case 'x':
533 *convert_to_ascii(str
, c
, *((unsigned long *) dataptr
++)) = 0;
545 *p
= (*(dataptr
++))&0xff;
550 ptr
= (char *)(*(dataptr
++));
552 while ((c
= *(ptr
++)))
565 RegExportHive(PCHAR ChunkBase
, PULONG ChunkSize
)