summaryrefslogtreecommitdiff
path: root/fpcsrc/rtl/darwin/extres_multiarch.inc
blob: 0d80d3555c28940ec7ea4296fc358aaf997161ee (plain)
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
{
    This file is part of the Free Pascal run time library.
    Copyright (c) 2008 by Giulio Bernardi

    Resource support as external files, for Mac OS X

    See the file COPYING.FPC, included in this distribution,
    for details about the copyright.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

 **********************************************************************}
{
  This file is similar to extres.inc when EXTRES_MMAP is defined.
  However, two files are searched (an architecture-dependent one and a shared
  one). They are searched first in Contents/Resources directory of the program
  application bundle and then in the same directory of the program.
}

const
  FPCRES_MAGIC = 'FPCRES';
  FPCRES_VERSION = 1;
  {$IFDEF ENDIAN_BIG}
  FPCRES_ENDIAN = 1;
  {$ENDIF}
  {$IFDEF ENDIAN_LITTLE}
  FPCRES_ENDIAN = 2;
  {$ENDIF}
  FPCRES_EXT = '.fpcres';
  FPCRES_ARCH =
    {$IFDEF CPUI386}
    '.i386';
    {$ELSE}
      {$IFDEF CPUX86_64}
      '.x86_64';
      {$ELSE}
        {$IFDEF CPUPOWERPC32}
        '.powerpc';
        {$ELSE}
          {$IFDEF CPUPOWERPC64}
          '.powerpc64';
          {$ELSE}
            {$IFDEF CPUARM}
            '.arm';
            {$ELSE}
              '';
            {$ENDIF}
          {$ENDIF}
        {$ENDIF}
      {$ENDIF}
    {$ENDIF}

type
  TExtHeader = packed record
    magic : array[0..5] of char;//'FPCRES'
    version : byte;             //EXT_CURRENT_VERSION
    endianess : byte;           //EXT_ENDIAN_BIG or EXT_ENDIAN_LITTLE
    count : longword;           //resource count
    nodesize : longword;        //size of header (up to string table, excluded)
    hdrsize  : longword;        //size of header (up to string table, included)
    reserved1 : longword;
    reserved2 : longword;
    reserved3 : longword;
  end;
  PExtHeader = ^TExtHeader;

  TResInfoNode = packed record
    nameid : longword;          //name offset / integer ID / languageID
    ncounthandle : longword;    //named sub-entries count/resource handle
    idcountsize : longword;     //id sub-entries count / resource size
    subptr : longword;          //first sub-entry offset
  end;
  PResInfoNode = ^TResInfoNode;

  TResFileInfo = record
    ResHeader : PExtHeader;
    fd : integer;
    size : longword;
  end;

var
  ResFileInfo     : TResFileInfo = (ResHeader : nil; fd : 0; size : 0);
  ResFileInfoArch : TResFileInfo = (ResHeader : nil; fd : 0; size : 0);
  reshandles : PPointer = nil;
  usedhandles : longword = 0;
  rescount : longword = 0;

(*****************************************************************************
                             Private Helper Functions
*****************************************************************************)

//resource functions are case insensitive... copied from genstr.inc
function ResStrIComp(Str1, Str2 : PChar): SizeInt;
var
  counter: SizeInt;
  c1, c2: char;
begin
  counter := 0;
  c1 := upcase(str1[counter]);
  c2 := upcase(str2[counter]);
  while c1 = c2 do
  begin
    if (c1 = #0) or (c2 = #0) then break;
    inc(counter);
    c1 := upcase(str1[counter]);
    c2 := upcase(str2[counter]);
  end;
  ResStrIComp := ord(c1) - ord(c2);
end;

{!fixme!}
//function InternalIsIntResource(aStr : pchar; out aInt : PtrUint) : boolean;
function InternalIsIntResource(aStr : pchar; var aInt : PtrUint) : boolean;
var i : integer;
    s : shortstring;
    code : word;
begin
  InternalIsIntResource:=((PtrUInt(aStr) shr 16)=0);
  if InternalIsIntResource then aInt:=PtrUInt(aStr)
  else
  begin
    //a string like #number specifies an integer id
    if aStr[0]='#' then
    begin
      i:=1;
      while aStr[i]<>#0 do
        inc(i);
      if i>256 then i:=256;
      s[0]:=chr(i-1);
      Move(aStr[1],s[1],i-1);
      Val(s,aInt,code);
      InternalIsIntResource:=code=0;
    end;
  end;
end;

function GetResInfoPtr(base : PExtHeader; const offset : longword) : PResInfoNode; inline;
begin
  GetResInfoPtr:=PResInfoNode(PtrUInt(base)+offset);
end;

function GetPchar(base : PExtHeader; const offset : longword) : Pchar; inline;
begin
  GetPchar:=Pchar(PtrUInt(base)+offset);
end;

function GetPtr(base : PExtHeader; const offset : longword) : Pointer; inline;
begin
  GetPtr:=Pointer(PtrUInt(base)+offset);
end;

procedure FixResEndian(ResHeader : PExtHeader);
var ptr : plongword;
    blockend : plongword;
begin
  //all info nodes reside in a contiguos block of memory.
  //they are all 16 bytes long and made by longwords
  //so, simply swap each longword in the block
  ptr:=GetPtr(ResHeader,sizeof(TExtHeader));
  blockend:=GetPtr(ResHeader,ResHeader^.nodesize);
  while ptr<blockend do
  begin
    ptr^:=SwapEndian(ptr^);
    inc(ptr);
  end;
end;

function GetExtResBasePath : shortstring;
var exename : shortstring;
    len, i, extpos, namepos: integer;
begin
  GetExtResBasePath:=paramstr(0);
  len:=byte(GetExtResBasePath[0]);
  i:=len;
//  writeln('exe name is ',GetExtResBasePath);
  //find position of extension
  while (i>0) and (not (GetExtResBasePath[i] in ['.',DirectorySeparator])) do
    dec(i);
  //find position of last directory separator
  if (i>0) and (GetExtResBasePath[i]='.') then extpos:=i-1
  else extpos:=len;
  while (i>0) and (GetExtResBasePath[i] <> DirectorySeparator) do
    dec(i);
  namepos:=i;
  exename:=copy(GetExtResBasePath,i+1,extpos-i);
  dec(i);
  //is executable in 'MacOS' directory? find previous dir separator...
  while (i>0) and (GetExtResBasePath[i] <> DirectorySeparator) do
    dec(i);
  if i<0 then i:=0;
  //yes, search file in <bundle>/Contents/Resources directory
  if (namepos>i) and (copy(GetExtResBasePath,i+1,namepos-i-1)='MacOS') then
  begin
    GetExtResBasePath[0]:=Chr(i);
    GetExtResBasePath:=GetExtResBasePath+'Resources'+DirectorySeparator+exename;
  end
  else //no, search file in exe directory
    GetExtResBasePath[0]:=Chr(extpos);
//  writeln('base path is ',GetExtResBasePath);
end;

function GetExtResPathArch(const base : shortstring) : pchar;
var len : integer;
begin
  len:=byte(base[0]);
  GetExtResPathArch:=GetMem(len+length(FPCRES_ARCH)+length(FPCRES_EXT)+1);
  Move(base[1],GetExtResPathArch[0],len);
  Move(FPCRES_ARCH[1],GetExtResPathArch[len],length(FPCRES_ARCH));
  inc(len,length(FPCRES_ARCH));
  Move(FPCRES_EXT[1],GetExtResPathArch[len],length(FPCRES_EXT));
  inc(len,length(FPCRES_EXT));
  GetExtResPathArch[len]:=#0;
//  writeln('Arch-dependent resource file is ',GetExtResPathArch);
end;

function GetExtResPath(const base : shortstring) : pchar;
var len : integer;
begin
  len:=byte(base[0]);
  GetExtResPath:=GetMem(len+length(FPCRES_EXT)+1);
  Move(base[1],GetExtResPath[0],len);
  Move(FPCRES_EXT[1],GetExtResPath[len],length(FPCRES_EXT));
  inc(len,length(FPCRES_EXT));
  GetExtResPath[len]:=#0;
//  writeln('Shared resource file is ',GetExtResPath);
end;

procedure MapResFile(var aInfo : TResFileInfo; aName : pchar);
const
  PROT_READ  = 1;
  PROT_WRITE = 2;
var fdstat : stat;
begin
  aInfo.fd:=FpOpen(aName,O_RDONLY,0);
  FreeMem(aName);
//  writeln('fpopen returned ',aInfo.fd);
  if (aInfo.fd=-1) then exit;
  if FpFStat(aInfo.fd,fdstat)<>0 then
  begin
//    writeln('fpfstat failed');
    FpClose(aInfo.fd);
    exit;
  end;
//  writeln('fpfstat suceeded');
  aInfo.size:=fdstat.st_size;
  aInfo.ResHeader:=PExtHeader(Fpmmap(nil,aInfo.size,PROT_READ or PROT_WRITE,
    MAP_PRIVATE,aInfo.fd,0));
//  writeln('fpmmap returned ',PtrInt(aInfo.ResHeader));
  if PtrInt(aInfo.ResHeader)=-1 then
  begin
    FpClose(aInfo.fd);
    exit;
  end;
  if (aInfo.ResHeader^.magic<>FPCRES_MAGIC) or
    (aInfo.ResHeader^.version<>FPCRES_VERSION) then
  begin
    FpClose(aInfo.fd);
    exit;
  end;
//  writeln('magic ok');
  if aInfo.ResHeader^.endianess<>FPCRES_ENDIAN then
  begin
    aInfo.ResHeader^.count:=SwapEndian(aInfo.ResHeader^.count);
    aInfo.ResHeader^.nodesize:=SwapEndian(aInfo.ResHeader^.nodesize);
    aInfo.ResHeader^.hdrsize:=SwapEndian(aInfo.ResHeader^.hdrsize);
    FixResEndian(aInfo.ResHeader);
  end;
  inc(rescount,aInfo.ResHeader^.count);
end;

procedure InitResources;
var respathArch : pchar;
    respath : pchar;
    basepath : shortstring;
begin
  basepath:=GetExtResBasePath;
  respathArch:=GetExtResPathArch(basepath);
  respath:=GetExtResPath(basepath);
  MapResFile(ResFileInfoArch,respathArch);
  MapResFile(ResFileInfo,respath);

  if rescount=0 then exit;
  reshandles:=GetMem(sizeof(Pointer)*rescount);
  FillByte(reshandles^,sizeof(Pointer)*rescount,0);
end;

procedure FinalizeResources;
begin
  if (ResFileInfoArch.Resheader=nil) and (ResFileInfo.Resheader=nil) then exit;
  FreeMem(reshandles);
  if ResFileInfoArch.Resheader<>nil then
  begin
    Fpmunmap(ResFileInfoArch.ResHeader,ResFileInfoArch.size);
    FpClose(ResFileInfoArch.fd);
  end;
  if ResFileInfo.Resheader<>nil then
  begin
    Fpmunmap(ResFileInfo.ResHeader,ResFileInfo.size);
    FpClose(ResFileInfo.fd);
  end;
end;

function BinSearchStr(base : PExtHeader; arr : PResInfoNode; query : pchar;
  left, right : integer) : PResInfoNode;
var pivot, res : integer;
    resstr : pchar;
begin
  BinSearchStr:=nil;
  while left<=right do
  begin
    pivot:=(left+right) div 2;
    resstr:=GetPchar(base,arr[pivot].nameid);
    res:=ResStrIComp(resstr,query);
    if res<0 then left:=pivot+1
    else if res>0 then right:=pivot-1
    else
    begin
      BinSearchStr:=@arr[pivot];
      exit;
    end;
  end;
end;

function BinSearchInt(arr : PResInfoNode; query : pchar; left, right : integer)
: PResInfoNode;
var pivot : integer;
begin
  BinSearchInt:=nil;
  while left<=right do
  begin
    pivot:=(left+right) div 2;
    if arr[pivot].nameid<PtrUInt(query) then left:=pivot+1
    else if arr[pivot].nameid>PtrUInt(query) then right:=pivot-1
    else
    begin
      BinSearchInt:=@arr[pivot];
      exit;
    end;
  end;
end;

function BinSearchRes(base : PExtHeader; root : PResInfoNode; aDesc : PChar)
: PResInfoNode;
var aID : PtrUint;
begin
  if InternalIsIntResource(aDesc,aID) then
    BinSearchRes:=BinSearchInt(GetResInfoPtr(base,root^.subptr),PChar(aID),
      root^.ncounthandle,root^.ncounthandle+root^.idcountsize-1)
  else
    BinSearchRes:=BinSearchStr(base,GetResInfoPtr(base,root^.subptr),aDesc,0,
      root^.ncounthandle-1);
end;

function FindSubLanguage(base : PExtHeader; aPtr : PResInfoNode; aLangID : word;
  aMask: word) : PResInfoNode;
var arr : PResInfoNode;
    i : longword;
begin
  FindSubLanguage:=nil;
  arr:=GetResInfoPtr(base,aPtr^.subptr);
  i:=0;
  while i<aPtr^.idcountsize do
  begin
    if (PtrUInt(arr[i].nameid) and aMask)=(aLangID and aMask) then
    begin
      FindSubLanguage:=@arr[i];
      exit;
    end;
    inc(i);
  end;
end;

//Returns a pointer to a name node.
function InternalFindResource(base : PExtHeader; ResourceName, ResourceType: PChar):
 PResInfoNode;
begin
  InternalFindResource:=nil;
  if base=nil then exit;
  InternalFindResource:=GetResInfoPtr(base,sizeof(TExtHeader));

  InternalFindResource:=BinSearchRes(base,InternalFindResource,ResourceType);
  if InternalFindResource<>nil then
    InternalFindResource:=BinSearchRes(base,InternalFindResource,ResourceName);
end;

function FindResourceSingleFile(ResHeader : PExtHeader; ResourceName,
  ResourceType: PChar) : TFPResourceHandle;
var ptr : PResInfoNode;
begin
  FindResourceSingleFile:=0;
  ptr:=InternalFindResource(ResHeader,ResourceName,ResourceType);
  if ptr=nil then exit;

  //first language id
  ptr:=GetResInfoPtr(ResHeader,ptr^.subptr);
  if ptr^.ncounthandle=0 then
  begin
    reshandles[usedhandles]:=ptr;
    inc(usedhandles);
    ptr^.ncounthandle:=usedhandles;
  end;
  FindResourceSingleFile:=ptr^.ncounthandle;
end;

{!fixme!}
//function FindResourceExSingleFile(ResHeader : PExtHeader; ResourceType,
//  ResourceName: PChar;  Language : word; out precision : integer): TFPResourceHandle;
function FindResourceExSingleFile(ResHeader : PExtHeader; ResourceType,
  ResourceName: PChar;  Language : word; var precision : integer): TFPResourceHandle;
const LANG_NEUTRAL = 0;
      LANG_ENGLISH = 9;
var nameptr,ptr : PResInfoNode;
begin
  FindResourceExSingleFile:=0;
  precision:=-1;
  nameptr:=InternalFindResource(ResHeader,ResourceName,ResourceType);
  if nameptr=nil then exit;

  precision:=4;
  //try exact match
  ptr:=FindSubLanguage(ResHeader,nameptr,Language,$FFFF);
  //try primary language
  if ptr=nil then
  begin
    dec(precision);
    ptr:=FindSubLanguage(ResHeader,nameptr,Language,$3FF);
  end;
  //try language neutral
  if ptr=nil then
  begin
    dec(precision);
    ptr:=FindSubLanguage(ResHeader,nameptr,LANG_NEUTRAL,$3FF);
  end;
  //try english
  if ptr=nil then
  begin
    dec(precision);
    ptr:=FindSubLanguage(ResHeader,nameptr,LANG_ENGLISH,$3FF);
  end;
  //nothing found, return the first one
  if ptr=nil then
  begin
    dec(precision);
    ptr:=GetResInfoPtr(ResHeader,nameptr^.subptr);
  end;

  if ptr^.ncounthandle=0 then
  begin
    reshandles[usedhandles]:=ptr;
    inc(usedhandles);
    ptr^.ncounthandle:=usedhandles;
  end;
  FindResourceExSingleFile:=ptr^.ncounthandle;
end;

function EnumResourceTypesSingleFile(ResHeader,Other : PExtHeader; ModuleHandle
 : TFPResourceHMODULE; EnumFunc : EnumResTypeProc; lParam : PtrInt) : LongBool;
var ptr,otarr : PResInfoNode;
    totn, totid, ottotn, ottotid, i : longword;
    pc : pchar;
begin
  EnumResourceTypesSingleFile:=false;
  if ResHeader=nil then exit;

  ptr:=GetResInfoPtr(Resheader,sizeof(TExtHeader));
  totn:=ptr^.ncounthandle;
  totid:=totn+ptr^.idcountsize;
  ptr:=GetResInfoPtr(Resheader,ptr^.subptr);

  if Other<>nil then
  begin
    otarr:=GetResInfoPtr(Other,sizeof(TExtHeader));
    ottotn:=otarr^.ncounthandle;
    ottotid:=ottotn+otarr^.idcountsize-1;
    otarr:=GetResInfoPtr(Other,otarr^.subptr)
  end;

  EnumResourceTypesSingleFile:=true;
  i:=0;
  while i<totn do //named entries
  begin
    pc:=GetPChar(Resheader,ptr[i].nameid);
    if (Other=nil) or (BinSearchStr(Other,otarr,pc,0,ottotn-1)=nil) then
      if not EnumFunc(ModuleHandle,pc,lParam) then exit;
    inc(i);
  end;
  while i<totid do
  begin
    if (Other=nil) or (BinSearchInt(otarr,PChar(ptr[i].nameid),ottotn,ottotid)=nil) then
      if not EnumFunc(ModuleHandle,PChar(ptr[i].nameid),lParam) then exit;
    inc(i);
  end;
end;

function EnumResourceNamesSingleFile(ResHeader,Other : PExtHeader;
  ModuleHandle : TFPResourceHMODULE; ResourceType : PChar;
  EnumFunc : EnumResNameProc; lParam : PtrInt) : LongBool;
var ptr,otarr : PResInfoNode;
    totn, totid, ottotn, ottotid, i : longword;
    pc : pchar;
begin
  EnumResourceNamesSingleFile:=False;
  if ResHeader=nil then exit;

  ptr:=GetResInfoPtr(ResHeader,sizeof(TExtHeader));
  ptr:=BinSearchRes(ResHeader,ptr,ResourceType);
  if ptr=nil then exit;
  totn:=ptr^.ncounthandle;
  totid:=totn+ptr^.idcountsize;
  ptr:=GetResInfoPtr(ResHeader,ptr^.subptr);

  if Other<>nil then
  begin
    otarr:=GetResInfoPtr(Other,sizeof(TExtHeader));
    otarr:=BinSearchRes(Other,otarr,ResourceType);
    if otarr<>nil then
    begin
      ottotn:=otarr^.ncounthandle;
      ottotid:=ottotn+otarr^.idcountsize-1;
      otarr:=GetResInfoPtr(Other,otarr^.subptr)
    end;
  end
  else otarr:=nil;

  EnumResourceNamesSingleFile:=true;
  i:=0;
  while i<totn do //named entries
  begin
    pc:=GetPChar(ResHeader,ptr[i].nameid);
    if (otarr=nil) or (BinSearchStr(Other,otarr,pc,0,ottotn-1)=nil) then
      if not EnumFunc(ModuleHandle,ResourceType,pc,lParam) then exit;
    inc(i);
  end;
  while i<totid do
  begin
    if (otarr=nil) or (BinSearchInt(otarr,PChar(ptr[i].nameid),ottotn,ottotid)=nil) then
      if not EnumFunc(ModuleHandle,ResourceType,PChar(ptr[i].nameid),lParam) then exit;
    inc(i);
  end;
end;

function EnumResourceLanguagesSingleFile(ResHeader,Other : PExtHeader;
  ModuleHandle : TFPResourceHMODULE; ResourceType, ResourceName : PChar;
  EnumFunc : EnumResLangProc; lParam : PtrInt) : LongBool;
var ptr, otarr : PResInfoNode;
    tot, ottot, i : integer;
begin
  EnumResourceLanguagesSingleFile:=False;
  ptr:=InternalFindResource(ResHeader,ResourceName,ResourceType);
  if ptr=nil then exit;

  tot:=ptr^.idcountsize;
  ptr:=GetResInfoPtr(ResHeader,ptr^.subptr);
  
  if Other<>nil then
  begin
    otarr:=InternalFindResource(Other,ResourceName,ResourceType);
    if otarr<>nil then
    begin
      ottot:=otarr^.idcountsize-1;
      otarr:=GetResInfoPtr(Other,otarr^.subptr)
    end;
  end
  else otarr:=nil;

  EnumResourceLanguagesSingleFile:=true;
  i:=0;
  while i<tot do
  begin
    if (otarr=nil) or (BinSearchInt(otarr,PChar(ptr[i].nameid),0,ottot)=nil) then
      if not EnumFunc(ModuleHandle,ResourceType,ResourceName,PtrUInt(
        ptr[i].nameid),lParam) then exit;
    inc(i);
  end;
end;

(*****************************************************************************
                             Public Resource Functions
*****************************************************************************)

function ExtHINSTANCE : TFPResourceHMODULE;
begin
  ExtHINSTANCE:=0;
end;

function ExtEnumResourceTypes(ModuleHandle : TFPResourceHMODULE; EnumFunc : EnumResTypeProc; lParam : PtrInt) : LongBool;
begin
  ExtEnumResourceTypes:=false;
  if EnumResourceTypesSingleFile(ResFileInfoArch.Resheader,nil,ModuleHandle,
    EnumFunc,lParam) then ExtEnumResourceTypes:=true;
  if EnumResourceTypesSingleFile(ResFileInfo.Resheader,
    ResFileInfoArch.Resheader,ModuleHandle,EnumFunc,lParam) then ExtEnumResourceTypes:=true;
end;

function ExtEnumResourceNames(ModuleHandle : TFPResourceHMODULE; ResourceType : PChar; EnumFunc : EnumResNameProc; lParam : PtrInt) : LongBool;
begin
  ExtEnumResourceNames:=False;
  if EnumResourceNamesSingleFile(ResFileInfoArch.Resheader,nil,ModuleHandle,
    ResourceType,EnumFunc,lParam) then ExtEnumResourceNames:=true;
  if EnumResourceNamesSingleFile(ResFileInfo.Resheader,
    ResFileInfoArch.Resheader,ModuleHandle,ResourceType,EnumFunc,lParam) then ExtEnumResourceNames:=true;
end;

function ExtEnumResourceLanguages(ModuleHandle : TFPResourceHMODULE; ResourceType, ResourceName : PChar; EnumFunc : EnumResLangProc; lParam : PtrInt) : LongBool;
begin
  ExtEnumResourceLanguages:=False;
  if EnumResourceLanguagesSingleFile(ResFileInfoArch.Resheader,nil,ModuleHandle,
    ResourceType,ResourceName,EnumFunc,lParam) then ExtEnumResourceLanguages:=true;
  if EnumResourceLanguagesSingleFile(ResFileInfo.Resheader,
    ResFileInfoArch.Resheader,ModuleHandle,ResourceType,ResourceName,EnumFunc,
    lParam) then ExtEnumResourceLanguages:=true;
end;

function ExtFindResource(ModuleHandle: TFPResourceHMODULE; ResourceName, ResourceType: PChar): TFPResourceHandle;
begin
  //search for resource in architecture-dependent res file first
  ExtFindResource:=FindResourceSingleFile(ResFileInfoArch.ResHeader,ResourceName,ResourceType);
  if ExtFindResource=0 then
    ExtFindResource:=FindResourceSingleFile(ResFileInfo.ResHeader,ResourceName,ResourceType);
end;

function ExtFindResourceEx(ModuleHandle: TFPResourceHMODULE; ResourceType, ResourceName: PChar; Language : word): TFPResourceHandle;
var precar, precsh : integer;
    handlear, handlesh : TResourceHandle;
begin
  //architecture-dependent res file
  handlear:=FindResourceExSingleFile(ResFileInfoArch.ResHeader,ResourceType,
    ResourceName,Language,precar);
  //architecture-independent res file
  handlesh:=FindResourceExSingleFile(ResFileInfo.ResHeader,ResourceType,
    ResourceName,Language,precsh);

  //return architecture-independent resource only if its language id is closer
  //to the one user asked for
  if precsh>precar then ExtFindResourceEx:=handlesh
  else ExtFindResourceEx:=handlear;
end;

function ExtLoadResource(ModuleHandle: TFPResourceHMODULE; ResHandle: TFPResourceHandle): TFPResourceHGLOBAL;
var ptr : PResInfoNode;
    base : PExtHeader;
begin
  ExtLoadResource:=0;
  if (ResHandle<=0) or (ResHandle>usedhandles) then exit;
  ptr:=PResInfoNode(reshandles[ResHandle-1]);
  base:=ResFileInfoArch.ResHeader;
  //if ptr isn't in architecture-dependent file memory area...
  if (base=nil) or (pointer(ptr)<=pointer(base))
    or (pointer(ptr)>=GetPtr(base,base^.hdrsize)) then
      base:=ResFileInfo.ResHeader;
  ExtLoadResource:=TFPResourceHGLOBAL(GetPtr(base,ptr^.subptr));
end;

function ExtSizeofResource(ModuleHandle: TFPResourceHMODULE; ResHandle: TFPResourceHandle): LongWord;
begin
  ExtSizeofResource:=0;
  if (ResHandle<=0) or (ResHandle>usedhandles) then exit;
  ExtSizeofResource:=PResInfoNode(reshandles[ResHandle-1])^.idcountsize;
end;

function ExtLockResource(ResData: TFPResourceHGLOBAL): Pointer;
begin
  ExtLockResource:=Nil;
  if (ResFileInfoArch.Resheader=nil) and (ResFileInfo.Resheader=nil) then exit;
  ExtLockResource:=Pointer(ResData);
end;

function ExtUnlockResource(ResData: TFPResourceHGLOBAL): LongBool;
begin
  ExtUnlockResource:=(ResFileInfoArch.Resheader<>nil) or (ResFileInfo.Resheader<>nil);
end;

function ExtFreeResource(ResData: TFPResourceHGLOBAL): LongBool;
begin
  ExtFreeResource:=(ResFileInfoArch.Resheader<>nil) or (ResFileInfo.Resheader<>nil);
end;

const
  ExternalResourceManager : TResourceManager =
  (
    HINSTANCEFunc : @ExtHINSTANCE;
    EnumResourceTypesFunc : @ExtEnumResourceTypes;
    EnumResourceNamesFunc : @ExtEnumResourceNames;
    EnumResourceLanguagesFunc : @ExtEnumResourceLanguages;
    FindResourceFunc : @ExtFindResource;
    FindResourceExFunc : @ExtFindResourceEx;
    LoadResourceFunc : @ExtLoadResource;
    SizeofResourceFunc : @ExtSizeofResource;
    LockResourceFunc : @ExtLockResource;
    UnlockResourceFunc : @ExtUnlockResource;
    FreeResourceFunc : @ExtFreeResource;
  );