source: trunk/tools/dbginfo/kHll.cpp@ 3265

Last change on this file since 3265 was 3249, checked in by bird, 26 years ago

Hacking...

File size: 26.1 KB
Line 
1/* $Id: kHll.cpp,v 1.8 2000-03-27 12:52:13 bird Exp $
2 *
3 * kHll - Implementation of the class kHll.
4 * That class is used to create HLL debuginfo.
5 *
6 * Copyright (c) 2000 knut st. osmundsen (knut.stange.osmundsen@pmsc.no)
7 *
8 * Project Odin Software License can be found in LICENSE.TXT
9 *
10 */
11
12
13/*******************************************************************************
14* Defined Constants And Macros *
15*******************************************************************************/
16#define INCL_TYPES
17#define INCL_DOSERRORS
18#define FOR_EXEHDR 1 /* exe386.h flag */
19#define DWORD ULONG /* Used by exe386.h / newexe.h */
20#define WORD USHORT /* Used by exe386.h / newexe.h */
21
22
23/*******************************************************************************
24* Internal Functions *
25*******************************************************************************/
26#include <os2.h>
27#include <newexe.h>
28#include <exe386.h>
29
30#include <malloc.h>
31#include <stdio.h>
32#include <string.h>
33#include <stddef.h>
34#include <stdlib.h>
35#include <assert.h>
36
37#include "hll.h"
38#include "kList.h"
39#include "kHll.h"
40
41
42/*******************************************************************************
43* Internal Functions *
44*******************************************************************************/
45signed long fsize(FILE *phFile);
46
47
48
49
50/*******************************************************************************
51* *
52* kHllBaseEntry *
53* *
54* kHllBaseEntry *
55* *
56*******************************************************************************/
57
58
59/**
60 * Writes a list to disk.
61 * @returns Number of bytes written.
62 * @returns Count of bytes written on success. (includes 0)
63 * -3 Invalid offsets.
64 * -2 Seek error.
65 * -1 Write error.
66 * @param phFile Output filehandle.
67 * @param pEntry Pointer to the start of the list which is to be written.
68 */
69int kHllBaseEntry::writeList(FILE *phFile, kHllBaseEntry *pEntry)
70{
71 int cch;
72 int cchWritten = 0;
73
74 /*
75 * Loop thru the list and write all the entries to disk.
76 */
77 while (pEntry != NULL)
78 {
79 cchWritten += cch = pEntry->write(phFile);
80 if (cch < 0)
81 return cch;
82 if (cch == 0)
83 return -1;
84 pEntry = (kHllBaseEntry*)pEntry->getNext();
85 }
86
87 return cchWritten;
88}
89
90
91
92
93
94
95/*******************************************************************************
96* *
97* kHllPubSymEntry *
98* *
99* kHllPubSymEntry *
100* *
101*******************************************************************************/
102
103
104
105/**
106 * Creates an HLL public symbol entry.
107 * @param pachName Symbol name.
108 * @param cchName Length of symbol name.
109 * @param off Offset into the object.
110 * @param iObject LX Object index.
111 * @param iType Type index. (index into type table)
112 */
113kHllPubSymEntry::kHllPubSymEntry(
114 const char * pachName,
115 int cchName,
116 unsigned long off,
117 unsigned short iObject,
118 unsigned short iType
119 )
120{
121 pPubSym = (PHLLPUBLICSYM)malloc(cchName + sizeof(HLLPUBLICSYM));
122 assert(pPubSym != NULL);
123
124 pPubSym->cchName = cchName;
125 pPubSym->achName[0] = '\0';
126 strncat((char*)&pPubSym->achName[0], pachName, cchName);
127 pPubSym->off = off;
128 pPubSym->iObject = iObject;
129 pPubSym->iType = iType;
130}
131
132
133
134/**
135 * Destructor.
136 */
137kHllPubSymEntry::~kHllPubSymEntry()
138{
139 if (pPubSym != NULL)
140 free(pPubSym);
141 pPubSym = NULL;
142}
143
144
145
146/**
147 * Write this entry to file.
148 * @returns Number of bytes written.
149 * @param phFile File handle.
150 */
151int kHllPubSymEntry::write(FILE *phFile)
152{
153 assert(pPubSym != NULL);
154 return fwrite(pPubSym,
155 1,
156 offsetof(HLLPUBLICSYM, achName) + pPubSym->cchName,
157 phFile);
158}
159
160
161
162
163
164/*******************************************************************************
165* *
166* kHllModuleEntry *
167* *
168* kHllModuleEntry *
169* *
170*******************************************************************************/
171
172
173
174
175
176
177
178/**
179 * Creates an HLL module entry.
180 * @param pszName Module name. (NULL is not allowed!)
181 * @param iLib Library index.
182 * @param cSegInfo Number of objects in the array.
183 * @param paSegInfo Pointer to an array of objects.
184 */
185kHllModuleEntry::kHllModuleEntry(
186 const char * pszName,
187 unsigned short iLib,
188 unsigned char cSegInfo/*= 0 */,
189 PHLLSEGINFO paSegInfo/*= NULL */
190 )
191: fValidOffsetsAndSizes(FALSE)
192{
193 int i;
194 int cchName;
195 PHLLSEGINFO pSegInfo;
196
197 /*
198 * Debug parameter validations.
199 */
200 assert(pszName != NULL);
201 assert(cSegInfo == 0 || paSegInfo != NULL);
202
203 /*
204 * Allocate data storage and fill HLL structure.
205 */
206 cchName = strlen(pszName);
207 pModule = (PHLLMODULE)malloc(sizeof(HLLMODULE) + cchName +
208 sizeof(HLLSEGINFO) * max((cSegInfo - 1), 3));
209 assert(pModule != NULL);
210 memset(pModule, 0, sizeof(*pModule));
211 pModule->cchName = cchName;
212 strcpy((char*)&pModule->achName[0], pszName);
213 pModule->chVerMajor = 4;
214 pModule->chVerMinor = 0;
215 pModule->cSegInfo = cSegInfo;
216 pModule->iLib = iLib;
217 pModule->usDebugStyle = HLL_MOD_STYLE;
218 pModule->overlay = 0;
219 pModule->pad = 0;
220
221 /* objects */
222 if (cSegInfo > 0)
223 {
224 pModule->SegInfo0.iObject = paSegInfo->iObject;
225 pModule->SegInfo0.cb = paSegInfo->cb;
226 pModule->SegInfo0.off = paSegInfo->off;
227
228 for (i = 1, pSegInfo = (PHLLSEGINFO)&pModule->achName[cchName]; i < cSegInfo; i++, pSegInfo++)
229 {
230 pSegInfo->iObject = paSegInfo[i].iObject;
231 pSegInfo->cb = paSegInfo[i].cb;
232 pSegInfo->off = paSegInfo[i].off;
233 }
234 }
235}
236
237
238/**
239 * Destructor - free storage.
240 */
241kHllModuleEntry::~kHllModuleEntry()
242{
243 if (pModule != NULL)
244 free(pModule);
245 pModule = NULL;
246}
247
248
249
250/**
251 * Adds an object to the module.
252 * @returns Success indicator.
253 * @param iObject LX Object index.
254 * @param off Offset into the object to the module data.
255 * @param cb Size of module data (in the object).
256 */
257BOOL kHllModuleEntry::addSegInfo(
258 unsigned short int iObject,
259 unsigned long off,
260 unsigned long cb
261 )
262{
263 assert(pModule != NULL);
264
265 /*
266 * Reallocate? (Note that we'll initially allocated space for 3 objects.)
267 */
268 if (pModule->cSegInfo >= 3)
269 {
270 void *pv = realloc(pModule, sizeof(HLLMODULE) + pModule->cchName
271 + (pModule->cSegInfo + 1) * sizeof(HLLSEGINFO));
272 assert(pv != NULL);
273 if (pv == NULL)
274 return FALSE;
275 pModule = (PHLLMODULE)pv;
276 }
277
278
279 /*
280 * Add module.
281 */
282 if (pModule->cSegInfo == 0)
283 {
284 pModule->SegInfo0.cb = cb;
285 pModule->SegInfo0.off = off;
286 pModule->SegInfo0.iObject = iObject;
287 }
288 else
289 {
290 PHLLSEGINFO pSegInfo = (PHLLSEGINFO)((pModule->cSegInfo - 1) * sizeof(HLLSEGINFO)
291 + &pModule->achName[pModule->cchName]);
292 pSegInfo->cb = cb;
293 pSegInfo->off = off;
294 pSegInfo->iObject = iObject;
295 }
296 pModule->cSegInfo++;
297
298 return TRUE;
299}
300
301
302
303/**
304 * Adds a public symbol.
305 * @returns Handle to the symbol. NULL on error.
306 * @param pszName Symbol name.
307 * @param off Offset into the LX Object of the symbol.
308 * @param iObject LX Object index.
309 * @param pvType Type handle. NULL if not type.
310 */
311const void * kHllModuleEntry::addPublicSymbol(
312 const char * pszName,
313 unsigned long int off,
314 unsigned short int iObject,
315 const void * pvType
316 )
317{
318 assert(pszName != NULL);
319 return addPublicSymbol(pszName, strlen(pszName), off, iObject, pvType);
320}
321
322
323
324/**
325 * Adds a public symbol.
326 * @returns Handle to the symbol. NULL on error.
327 * @param pachName Symbol name.
328 * @param cchName Name length.
329 * @param off Offset into the LX Object of the symbol.
330 * @param iObject LX Object index.
331 * @param pvType Type handle. NULL if not type.
332 */
333const void * kHllModuleEntry::addPublicSymbol(
334 const char * pachName,
335 int cchName,
336 unsigned long int off,
337 unsigned short int iObject,
338 const void * pvType
339 )
340{
341 kHllPubSymEntry * pEntry;
342
343 /* parameter assertion */
344 assert(pachName != NULL);
345
346 /*
347 * Create a public symbol entry
348 * Insert into it's list.
349 * Invalidate offsets.
350 */
351 pEntry = new kHllPubSymEntry(
352 pachName,
353 cchName,
354 off,
355 iObject,
356 pvType == NULL ? 0 : -1 //FIXME/TODO: Types->getIndex(pvType); check if 0 or -1.
357 );
358
359 PublicSymbols.insert(pEntry);
360
361 fValidOffsetsAndSizes = FALSE;
362
363 return pEntry;
364}
365
366
367
368
369/**
370 * Write this HLL entry to file.
371 * @returns Count of bytes written. -1 on error.
372 * @param phFile Filehandle.
373 * @param off Current offset into the HLL data.
374 * This is stored and used when making the directory
375 * entries for this module.
376 */
377int kHllModuleEntry::write(FILE *phFile, unsigned long off)
378{
379 int cch;
380 int cchToWrite;
381 int cchWritten = 0;
382
383 /* validate object state */
384 assert(pModule != NULL);
385
386 /*
387 * Write module HLL data.
388 */
389 offModule = off;
390 cchToWrite = offsetof(HLLMODULE, achName) + pModule->cchName + sizeof(HLLSEGINFO) * max(pModule->cSegInfo-1, 0);
391 cch = fwrite(pModule, 1, cchToWrite, phFile);
392 if (cch != cchToWrite)
393 return -1;
394 cchWritten += cch;
395 cbModule = cch;
396 off += cch;
397
398 /*
399 * Write the lists.
400 * Public Symbols
401 * Types
402 * Symbols
403 * Source
404 */
405 offPublicSymbols = off;
406 cbPublicSymbols = cch = kHllBaseEntry::writeList(phFile, PublicSymbols.getFirst());
407 if (cch < 0)
408 return cch;
409 cchWritten += cch;
410 off += cch;
411
412 /*
413 offTypes = off;
414 cbTypes = cch = kHllBaseEntry::writeList(phFile, Types.getFirst());
415 if (cch < 0)
416 return cch;
417 cchWritten += cch;
418 off += cch;
419
420
421 offSymbols = off;
422 cbSymbols = cch = kHllBaseEntry::writeList(phFile, Symbols.getFirst());
423 if (cch < 0)
424 return cch;
425 cchWritten += cch;
426 off += cch;
427
428 offSource = off;
429 cbSource = cch = kHllBaseEntry::writeList(phFile, Source.getFirst());
430 if (cch < 0)
431 return cch;
432 cchWritten += cch;
433 off += cch;
434 */
435
436 /*
437 * Marks offsets and sizes valid and returns succesfully.
438 */
439 fValidOffsetsAndSizes = TRUE;
440 return cchWritten;
441}
442
443
444
445/**
446 * Writes the directory entries for this module to file.
447 * @returns Count of bytes written on success.
448 * -3 Invalid offsets.
449 * -2 Seek error.
450 * -1 Write error.
451 * 0 no data written (this is an error condition!)
452 * @param phFile Filehandle.
453 * @param iMod Index of this module.
454 */
455int kHllModuleEntry::writeDirEntries(FILE *phFile, unsigned short iMod)
456{
457 HLLDIRENTRY hllDirEntry;
458 int cch;
459 int cchWritten = 0;
460
461 /*
462 * Check that offsets are valid!
463 */
464 assert(fValidOffsetsAndSizes);
465 if (!fValidOffsetsAndSizes)
466 return -3;
467
468 /*
469 * Write Directory Entries.
470 * Module.
471 * Public Symbols. (if any)
472 * Types. (if any)
473 * Symbols. (if any)
474 * Source. (if any)
475 */
476 hllDirEntry.usType = HLL_DE_MODULES;
477 hllDirEntry.cb = cbModule;
478 hllDirEntry.off = offModule;
479 hllDirEntry.iMod = iMod;
480 cch = fwrite(&hllDirEntry, 1, sizeof(hllDirEntry), phFile);
481 if (cch != sizeof(hllDirEntry))
482 return -1;
483 cchWritten += cch;
484
485 if (cbPublicSymbols > 0)
486 {
487 hllDirEntry.usType = HLL_DE_PUBLICS;
488 hllDirEntry.cb = cbPublicSymbols;
489 hllDirEntry.off = offPublicSymbols;
490 hllDirEntry.iMod = iMod;
491 cch = fwrite(&hllDirEntry, 1, sizeof(hllDirEntry), phFile);
492 if (cch != sizeof(hllDirEntry))
493 return -1;
494 cchWritten += cch;
495 }
496
497 /*
498 if (cbTypes > 0)
499 {
500 hllDirEntry.usType = HLL_DE_TYPES;
501 hllDirEntry.cb = cbTypes;
502 hllDirEntry.off = offTypes;
503 hllDirEntry.iMod = iMod;
504 cch = fwrite(&hllDirEntry, 1, sizeof(hllDirEntry), phFile);
505 if (cch != sizeof(hllDirEntry))
506 return -1;
507 cchWritten += cch;
508 }
509
510 if (cbSymbols > 0)
511 {
512 hllDirEntry.usType = HLL_DE_SYMBOLS;
513 hllDirEntry.cb = cbSymbols;
514 hllDirEntry.off = offSymbols;
515 hllDirEntry.iMod = iMod;
516 cch = fwrite(&hllDirEntry, 1, sizeof(hllDirEntry), phFile);
517 if (cch != sizeof(hllDirEntry))
518 return -1;
519 cchWritten += cch;
520 }
521
522 if (cbSource > 0)
523 {
524 hllDirEntry.usType = HLL_DE_IBMSRC;
525 hllDirEntry.cb = cbSource;
526 hllDirEntry.off = offSource;
527 hllDirEntry.iMod = iMod;
528 cch = fwrite(&hllDirEntry, 1, sizeof(hllDirEntry), phFile);
529 if (cch != sizeof(hllDirEntry))
530 return -1;
531 cchWritten += cch;
532 }
533
534 */
535
536 return cchWritten;
537}
538
539
540
541
542
543
544
545
546
547
548
549
550/*******************************************************************************
551* *
552* kHll *
553* *
554* kHll *
555* *
556*******************************************************************************/
557
558
559
560/**
561 * Writes HLL debuginfo to the given file at the current position.
562 * The file should be opened in write mode.
563 * @returns Number of bytes written.
564 * @param phFile Filehandle to output file. Starts writing at current pos.
565 */
566int kHll::write(FILE *phFile)
567{
568 HLLHDR hllHdr;
569 HLLDIR hllDir;
570 kHllModuleEntry * pModule;
571 int cch; /* Number of bytes written to the file in an operation. */
572 int cchWritten = 0; /* Number of bytes written to the file. */
573 long int lPosStart; /* Starting position. */
574 long int lPosDir; /* Directory position. */
575 long int lPos; /* A file position. */
576 int iMod; /* Module index (passed in to writeDirEntries) */
577
578 /* Get starting position. */
579 lPosStart = ftell(phFile);
580
581 /* Make temporary header and write it */
582 memcpy(hllHdr.achSignature, "NB04", 4);
583 hllHdr.offDirectory = 0;
584 cch = fwrite(&hllHdr, 1, sizeof(hllHdr), phFile);
585 if (cch != sizeof(hllHdr))
586 return -1;
587 cchWritten += cch;
588
589
590 /*
591 * Start writing modules
592 */
593 pModule = (kHllModuleEntry*)Modules.getFirst();
594 while (pModule != NULL)
595 {
596 cch = pModule->write(phFile, cchWritten);
597 if (cch <= 0)
598 return cch;
599 cchWritten += cch;
600 pModule = (kHllModuleEntry *)pModule->getNext();
601 }
602
603
604 /*
605 * Write libraries.
606 */
607 //Not implemented yet - TODO/FIXME!
608
609
610 /*
611 * Write directory.
612 * Make and write temporary directory header.
613 * Write directory entries per module.
614 * Write directory entry for libraries.
615 * Remake and rewrite directory header. (correct cEntries)
616 */
617 lPosDir = ftell(phFile);
618 hllDir.cEntries = 0;
619 hllDir.cb = offsetof(HLLDIR, aEntries);
620 hllDir.cbEntry = sizeof(HLLDIRENTRY);
621 cch = fwrite(&hllDir, 1, offsetof(HLLDIR, aEntries), phFile);
622 if (cch != offsetof(HLLDIR, aEntries))
623 return -1;
624 cchWritten += cch;
625
626 iMod = 1;
627 pModule = (kHllModuleEntry*)Modules.getFirst();
628 while (pModule != NULL)
629 {
630 cch = pModule->writeDirEntries(phFile, iMod);
631 if (cch == -1)
632 return -1;
633 cchWritten += cch;
634 pModule = (kHllModuleEntry *)pModule->getNext();
635 iMod++;
636 }
637
638 //Library - TODO/FIXME
639
640 lPos = ftell(phFile);
641 hllDir.cEntries = (lPos - lPosDir - offsetof(HLLDIR, aEntries)) / sizeof(HLLDIRENTRY);
642 if (fseek(phFile, lPosDir, SEEK_SET) != 0)
643 return -2;
644 cch = fwrite(&hllDir, 1, offsetof(HLLDIR, aEntries), phFile);
645 if (cch != offsetof(HLLDIR, aEntries))
646 return -1;
647
648 /*
649 * Rewrite HLL header (with correct directory offset).
650 */
651 hllHdr.offDirectory = lPosDir - lPosStart;
652 if (fseek(phFile, lPosStart, SEEK_SET) != 0)
653 return -2;
654 cch = fwrite(&hllHdr, 1, sizeof(hllHdr), phFile);
655 if (cch != sizeof(hllHdr))
656 return -1;
657
658 /*
659 * Hacking:
660 * Writing an extra HLL header pointing to an non-existing directory
661 * staring at the last byte of this header.
662 */
663 if (fseek(phFile, lPosStart + cchWritten, SEEK_SET) != 0)
664 return -2;
665 hllHdr.offDirectory = cchWritten + sizeof(hllHdr) - 1;
666 cch = fwrite(&hllHdr, 1, sizeof(hllHdr), phFile);
667 if (cch != sizeof(hllHdr))
668 return -1;
669 cchWritten += cch;
670
671 return cchWritten;
672}
673
674
675
676/**
677 * Constructor - Creates an empty HLL object.
678 */
679kHll::kHll()
680{
681}
682
683
684
685/**
686 * Destructor.
687 */
688kHll::~kHll()
689{
690}
691
692
693
694/**
695 * Adds a module.
696 * @returns Pointer to the module object added. NULL on error.
697 * @param pszName Module name
698 * @param pvLib Library module handle.
699 * @param cSegInfo Number of objects in the array.
700 * @param paSegInfo Pointer to an array of objects.
701 */
702kHllModuleEntry * kHll::addModule(
703 const char * pszName,
704 const void * pvLib,
705 unsigned cSegInfo,
706 PHLLSEGINFO paSegInfo)
707{
708 kHllModuleEntry * pEntry;
709 assert(pszName != NULL);
710
711 pEntry = new kHllModuleEntry(
712 pszName,
713 pvLib == NULL ? 0 : -1, //FIXME/TODO: Libs->getIndex(pvLib); check if 0 or -1;
714 cSegInfo,
715 paSegInfo);
716
717 Modules.insert(pEntry);
718 return pEntry;
719}
720
721
722
723/**
724 * Adds a module.
725 * @returns Pointer to the module object added. NULL on error.
726 * @param pachName Module name
727 * @param cchName Length of modulename
728 * @param pvLib Library module handle.
729 * @param cSegInfo Number of objects in the array.
730 * @param paSegInfo Pointer to an array of objects.
731 */
732kHllModuleEntry * kHll::addModule(
733 const char * pachName,
734 unsigned cchName,
735 const void * pvLib,
736 unsigned cSegInfo,
737 PHLLSEGINFO paSegInfo)
738{
739 char szModName[256];
740 kHllModuleEntry * pEntry;
741 assert(pachName != NULL && cchName > 0);
742
743 szModName[0] = '\0';
744 strncat(szModName, pachName, min(cchName, 255));
745 pEntry = new kHllModuleEntry(
746 szModName,
747 pvLib == NULL ? 0 : -1, //FIXME/TODO: Libs->getIndex(pvLib); check if 0 or -1;
748 cSegInfo,
749 paSegInfo);
750
751 Modules.insert(pEntry);
752 return pEntry;
753}
754
755
756
757/**
758 * Writes the HLL info to a file. (Not LX?)
759 * @returns Success indicator.
760 * @param pszFilename Name of the output file.
761 * @remark IMPORTANT! This is mostly for debugging!
762 * It completely overwrites the file if it exists!
763 */
764BOOL kHll::write(
765 const char *pszFilename
766 )
767{
768 FILE * phFile;
769
770 phFile = fopen(pszFilename, "wb");
771 if (phFile != NULL)
772 {
773 int cch = write(phFile);
774 if (cch > 0)
775 {
776 fclose(phFile);
777 return TRUE;
778 }
779 else
780 fprintf(stderr, "write failed with cch=%d\n", cch);
781 fclose(phFile);
782 }
783
784 return FALSE;
785}
786
787
788
789/**
790 * Writes the HLL info to a file. (Not LX?)
791 * Failes if there is debuginfo in the file.
792 * No backup is made. (sorry)
793 * @returns OS2 return code.
794 * @param pszFilename Name of the output file.
795 */
796APIRET kHll::writeToLX(
797 const char *pszFilename
798 )
799{
800 APIRET rc;
801 FILE * phFile;
802
803 phFile = fopen(pszFilename, "rb+");
804 if (phFile != NULL)
805 {
806 struct exe_hdr ehdr;
807 struct e32_exe e32;
808 int cch;
809 long lPosLXHdr;
810 long cbLXFile;
811
812 /*
813 * Read exe header
814 */
815 cch = fread(&ehdr, 1, sizeof(ehdr), phFile);
816 if (cch == sizeof(ehdr))
817 {
818 cbLXFile = fsize(phFile);
819 if (ehdr.e_magic == EMAGIC)
820 lPosLXHdr = ehdr.e_lfanew;
821 else
822 lPosLXHdr = 0;
823 if (fseek(phFile, lPosLXHdr, SEEK_SET) == 0)
824 {
825 cch = fread(&e32, 1, sizeof(e32), phFile);
826 if (cch == sizeof(e32))
827 {
828 if (*(unsigned short*)&e32.e32_magic[0] == E32MAGIC)
829 {
830 /*
831 * Found exeheader.
832 * Check if there is any debug info.
833 */
834 if ((e32.e32_debuginfo == 0 && e32.e32_debuginfo == 0)
835 || (cbLXFile == e32.e32_debuglen + e32.e32_debuginfo)
836 )
837 {
838 long lPosDebug;
839
840 if (e32.e32_debuginfo != 0 && e32.e32_debuglen != 0)
841 lPosDebug = e32.e32_debuginfo;
842 else
843 lPosDebug = cbLXFile;
844
845 /*
846 * Go to debug info position in the file and write debug info.
847 */
848 if (fseek(phFile, lPosDebug, SEEK_SET) == 0)
849 {
850 /*
851 * Write the HLL data to disk.
852 */
853 cch = write(phFile);
854 if (cch > 0)
855 {
856 /*
857 * Update exeheader.
858 */
859 e32.e32_debuglen = cch;
860 e32.e32_debuginfo = lPosDebug;
861 if (fseek(phFile, lPosLXHdr, SEEK_SET) == 0)
862 {
863 /*
864 * Write the updated header to disk.
865 */
866 cch = fwrite(&e32, 1, sizeof(e32), phFile);
867 if (cch == sizeof(e32))
868 rc = NO_ERROR;
869 else
870 rc = ERROR_WRITE_FAULT;
871 }
872 else
873 rc = ERROR_SEEK;
874 }
875 else
876 {
877 fprintf(stderr, "error - write failed with cch=%d\n", cch);
878 rc = ERROR_WRITE_FAULT;
879 }
880 }
881 else
882 rc = ERROR_SEEK;
883 }
884 else
885 {
886 fprintf(stderr, "error - debuginfo exists\n");
887 rc = ERROR_BAD_EXE_FORMAT;
888 }
889
890 }
891 else
892 rc = ERROR_INVALID_EXE_SIGNATURE;
893 }
894 else
895 rc = ERROR_BAD_EXE_FORMAT;
896 }
897 else
898 rc = ERROR_BAD_EXE_FORMAT;
899 }
900 else
901 rc = ERROR_READ_FAULT;
902
903 fclose(phFile);
904 }
905 else
906 rc = ERROR_ACCESS_DENIED; //?
907
908
909 return rc;
910}
911
912
913/**
914 * Find the size of a file.
915 * @returns Size of file. -1 on error.
916 * @param phFile File handle.
917 */
918signed long fsize(FILE *phFile)
919{
920 int ipos;
921 signed long cb;
922
923 if ((ipos = ftell(phFile)) < 0
924 ||
925 fseek(phFile, 0, SEEK_END) != 0
926 ||
927 (cb = ftell(phFile)) < 0
928 ||
929 fseek(phFile, ipos, SEEK_SET) != 0
930 )
931 cb = -1;
932 return cb;
933}
934
Note: See TracBrowser for help on using the repository browser.