Menu

[ecd09e]: / res2coff / resimage.c  Maximize  Restore  History

Download this file

335 lines (273 with data), 6.9 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*
* RESimage.c
*
* This module is devoted to treating the .RES files
*/
#include "res2coff.h"
// Initialise the Resource Information Tree
void InitResourceInfo(void)
{
int i;
for (i=0;i<MAX_TYPES;i++) {
ResInfo[i].count = ResInfo[i].named = ResInfo[i].unnamed = 0;
ResInfo[i].RName = ResInfo[i].RID = NULL;
}
}
// Free the Resource Information Tree
void FreeResourceInfo(void)
{
int i;
#ifdef NDEBUG
if (verbose)
printf("Resources found in file\n Named Unnamed Type\n");
#endif
for (i=0;i<MAX_TYPES;i++)
{
#ifdef NDEBUG
if ((verbose) && (ResInfo[i].count))
printf(" %4d %6d %s\n",
ResInfo[i].named,
ResInfo[i].unnamed,
szResourceTypes[i]);
#endif
if (ResInfo[i].RName)
free(ResInfo[i].RName);
if (ResInfo[i].RID)
free(ResInfo[i].RID);
}
}
// Add a named resource of a given type to the Resource Information Tree
int AddNamedResource(int type,PRESOURCEHEAD pNewResource)
{
PRESOURCEHEAD *pTemp;
int result;
int last = ResInfo[type].named;
pTemp = (PRESOURCEHEAD *)realloc(ResInfo[type].RName,
sizeof(PRESOURCEHEAD)*(last+1));
ResInfo[type].RName = pTemp;
if (result = (pTemp) ? TRUE : FALSE)
{
ResInfo[type].RName = pTemp;
ResInfo[type].count++;
ResInfo[type].named++;
pTemp[last] = pNewResource;
}
return result;
}
// Add a numbered resource of a given type to the Resource Information Tree
int AddIDResource(int type,PRESOURCEHEAD pNewResource)
{
PRESOURCEHEAD *pTemp;
int result;
int last = ResInfo[type].unnamed;
pTemp = (PRESOURCEHEAD *)realloc(ResInfo[type].RID,
sizeof(PRESOURCEHEAD)*(last+1));
ResInfo[type].RID = pTemp;
if (result = (pTemp) ? TRUE : FALSE)
{
ResInfo[type].RID = pTemp;
ResInfo[type].count++;
ResInfo[type].unnamed++;
pTemp[last] = pNewResource;
}
return result;
}
// Functions to analyse a resource header structure in a RES32 file
int IsTypeCoded(PRESOURCEHEAD this)
{
WORD *pTemp;
pTemp = (WORD *)&(this->Type);
return (*pTemp == 0xffff) ? TRUE : FALSE;
}
int TypeCode(PRESOURCEHEAD this)
{
WORD *pTemp;
pTemp = (WORD *)&(this->Type);
if (*pTemp != 0xffff)
return 0x7fff; /* RT_ERROR */
return *(++pTemp);
}
int IsNameCoded(PRESOURCEHEAD this)
{
WORD *pTemp;
pTemp = (WORD *)&(this->Type);
if (*pTemp++ == 0xffff)
pTemp++;
else
while (*pTemp++)
;
return (*pTemp == 0xffff) ? TRUE : FALSE;
}
int IDCode(PRESOURCEHEAD this)
{
WORD *pTemp;
pTemp = (WORD *)&(this->Type);
if (*pTemp++ == 0xffff)
pTemp++;
else
while (*pTemp++)
;
if (*pTemp++ == 0xffff)
return *pTemp;
else
return 0x7fff; /* RT_ERROR */
}
// This function returns the pointer to the start of the
// name in a named resource
wchar_t *ResName(PRESOURCEHEAD this)
{
wchar_t *result;
result = (wchar_t *)&this->Type;
if (*result == 0xffff)
return result + 2;
while (*result)
result++;
return result;
}
// This function returns the Codepage information
WORD LanguageID(PRESOURCEHEAD this)
{
return 0x434;
}
// This function parses a resource, including it in the
// Resource Information Tree and returns a pointer to the
// next resource in the RES32 file image.
// Returns NULL on error
PRESOURCEHEAD ParseResource(PRESOURCEHEAD this)
{
#ifndef NDEBUG
char *MkAString(wchar_t *lstr)
{
char *result,*p;
int Alen = lstrlenW(lstr) + 1;
result = malloc(Alen);
if (result)
{
p = result;
do
{
*(p++) = (char)(*(lstr++));
}
while (p[-1]);
}
return result;
}
char *PascalWString(wchar_t *PWStr)
{
char *result,*p;
int len;
len = *PWStr;
PWStr++;
if (len == 0)
return "";
result = malloc(len+1);
if (result == NULL)
return "Alloc error!!!";
p=result;
while (len--)
*p++ = (char) *PWStr++;
*p = 0;
return result;
}
#endif
BYTE *pNext;
int rest,Offset2Next;
int typecode = TypeCode(this);
Offset2Next = this->DataSize + this->HeaderSize;
if (rest = Offset2Next % sizeof(DWORD))
Offset2Next += sizeof(DWORD) - rest;
pNext = (BYTE *) this + Offset2Next;
if (!IsTypeCoded(this)) {
printf(" ERROR: Named resource types not yet supported!\n");
return NULL;
}
if (IsNameCoded(this))
{
#ifndef NDEBUG
printf("Unnamed resource of type %d (%s) : [%04x]\n",
typecode,
szResourceTypes[typecode],
IDCode(this));
#endif
AddIDResource(typecode,(PRESOURCEHEAD)this);
}
else
{
#ifndef NDEBUG
printf("Named resource of type %d (%s) : [%s]\n",
typecode,
szResourceTypes[typecode],
MkAString(ResName(this)));
#endif
AddNamedResource(TypeCode(this),(PRESOURCEHEAD)this);
}
#ifndef NDEBUG
if (typecode == 6) /* String table */
{
int code,firstcode;
wchar_t *StringFromTable;
if (!IsNameCoded(this))
printf("Named string block !!!???\n");
else
{
StringFromTable = (wchar_t *)this;
StringFromTable += 16;
code = (IDCode(this)-1) * 16;
do
{
printf(" %6d %s\n",code++,PascalWString(StringFromTable));
StringFromTable += (*StringFromTable) + 1;
}
while (code & 0x000f);
}
}
#endif
return (PRESOURCEHEAD) pNext;
}
// This function checks the whole resource file image
// for conformancy to the RES32 file format and creates
// the Resource Information Tree. Returns TRUE on compliance
// or FALSE if error detected
int Check32bitResourceFile(PRESOURCEHEAD FileBuf)
{
LPVOID pTemp;
// The first record is an invalid record with
// predefined contents.
int result = ((FileBuf->DataSize == 0) &&
(FileBuf->HeaderSize == 32) &&
(FileBuf->Type == 0xffff) &&
(FileBuf->Name == 0xffff) &&
(FileBuf->DataVersion == 0) &&
(FileBuf->MemoryFlag == 0) &&
(FileBuf->LanguageID == 0) &&
(FileBuf->Version == 0) &&
(FileBuf->Characteristics == 0)) ? TRUE : FALSE;
// the '32' in 'FileBuf + 32' is the size of the
// Resource32Header aligned to DWORD boundary
if (result)
{
pTemp = (BYTE *)FileBuf + 32;
while ( (pTemp) && (pTemp < RESviewEnd) )
pTemp = ParseResource((PRESOURCEHEAD) pTemp);
result = (pTemp) ? TRUE : FALSE;
}
return result;
}
/* This procedure sorts the resource tree in order to have */
/* the unnamed resources sorted in ascending order */
void SortUnnamedResources(void)
{
int type;
int ResourceCmp(const void *a,const void *b)
{
int AName = ((*(PRESOURCEHEAD*)a)->Name) >> 16;
int BName = ((*(PRESOURCEHEAD*)b)->Name) >> 16;
return AName-BName;
}
for (type = 1;type < 17;type++)
if (ResInfo[type].unnamed)
qsort(ResInfo[type].RID,
ResInfo[type].unnamed,
sizeof(PRESOURCEHEAD),
ResourceCmp);
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.