-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimage.c
939 lines (831 loc) · 28.3 KB
/
image.c
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
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
#if 0
/*===========================================================================
I M A G E . C
Routines to manage an Image data structure.
imageWrite(): merge a data record from an input file into an
Image, building the Image as required. Includes memory allocation
routines for the internal Image reperesentation. The record must have
already been read and parsed into a Record structure.
imageRead(): get a data block from an Image and put it into a
Record structure. Used primarily to get data to
be reformatted for output.
imageFree(): free all the data memory in an Image.
imageDump(): (debug tool) do a text dump of an Image to an output file.
imageCheck(): (debug tool) perform an internal consistency check on Image.
Copyright 1989 Atari Games. All rights reserved.
Author: Lyle Rains
===========================================================================*/
#endif
/*
* Imported objects, declarations and definitions:
* string.h: NULL, size_t, memcpy(), memset()
* stdio.h: fprintf()
* stdlib.h: EXIT_FAILURE, exit(), malloc(), free()
* limits.h: ULONG_MAX
* stddef.h: offsetof()
* image.h: LogicalAddr, BlockGroup, MemBlock, Chunk, Page, Image
*/
#include "mixit.h"
#include <limits.h>
#ifdef __STDC__
#include <stddef.h>
#endif
#ifndef offsetof
#define offsetof(s, m) (size_t)(&(((s *)0)->m))
#endif
/*
* Exported objects (#include "image.h"):
*
* void imageWrite(Image *image, Record *record);
* void imageRead(Image *image, Record *record);
* void imageFree(Image *image);
* void imageInit(Image *image, int pagesize );
*
* The following are available only when #ifndef NDEBUG
*
* void imageDump(Image *image, FILE *dumpfile);
* void imageCheck(Image *image, FILE *logfile);
*/
static MemBlock* allocBlock(Image *image);
static void freeBlock(Image *image, MemBlock *block);
static Page* newPage(Image *image, LogicalAddr addr, Page **insertlink);
static Chunk* newChunk(Image *image, LogicalAddr addr, size_t nbytes,
Chunk **insertlink, Page *page);
static int init_chunk_read(Chunk *thisChunk);
/*==========================================================================
* The following macros for pageList and chnkList manipulation are UNSAFE for use
* with arguments which have side effects.
*
* SEARCH_FOR_ADDR() will search the list indicated by `*link' for a block
* (page or chunk) which includes the logical address `addr'. The macro uses
* `block' as an intermediate value. At the termination of the macro, if such a
* block exists, `block' will be a pointer to it. Otherwise, `addr' is in a
* currently non-existant block which should be linked between `link' and `block'.
*
* INSERT_BLOCK() will insert a new block between `link' and `block'.
*==========================================================================*/
#define SEARCH_FOR_ADDR(link, block, addr) \
{ for ((block) = *(link); (block) && (block)->end < (addr); (block) = (block)->next) \
(link) = &((block)->next); }
#define INSERT_BLOCK(link, block) \
((block)->next = *(link), *(link) = (block))
/*==========================================================================
* PREV_CHUNK(page, chunklink)
*
* If the link is the root chunkList of the page, the value is NULL,
* else the value is a pointer to the chunk containing the link.
*==========================================================================*/
#ifdef __STDC__
#define PREV_CHUNK(page, link) \
((Chunk *)((link) == &((page)->chunkList) ? 0 \
: ((char *)(link) - (size_t)(offsetof(Chunk, next)))))
#else
/*
* KLUDGE WARNING: The following macro definition makes use of the fact
* that the `.next' field is the first field in a struct chunk, so a
* pointer to the `.next' field is also a pointer to the struct.
*/
#define PREV_CHUNK(page, link) \
((Chunk *)((link) == &((page)->chunkList) ? 0 : (link)))
#endif /* __STDC__ */
/*==========================================================================
* get_memory(): Allocates 'n' bytes of memory and returns a pointer to the
* memory. If memory can't be allocated, exits the program.
*==========================================================================*/
static void* get_memory(int n)
{
void *ptr;
if ( (ptr = (void *)malloc(n)) == (void *)NULL )
err_exit("Not enough memory");
return ptr;
} /* end get_memory */
/*==========================================================================
* If no blocks are on the free list, make some. Abort on allocation error.
*
* Return: an uninitialized chunk from the free list.
*==========================================================================*/
static MemBlock* allocBlock(Image *image)
{
MemBlock *block;
BlockGroup *root;
int i;
#define BLOCK_EXTEND 256
/*
* If we're out of MemBlock structures, make BLOCK_EXTEND more and
* link them into the free list.
*/
if ( !(image->freeBlockList) )
{
root = (BlockGroup *)get_memory(sizeof(BlockGroup) + (sizeof(MemBlock) * BLOCK_EXTEND));
INSERT_BLOCK(&(image->blockGroupList), root);
block = image->freeBlockList = (MemBlock *)(root + 1);
for ( i = 0; i < BLOCK_EXTEND - 1; ++i )
{
block->next = block + 1;
++block;
}
block->next = (MemBlock *)NULL;
}
/* Remove the first chunk from the free list and return it. */
block = image->freeBlockList;
image->freeBlockList = block->next;
return (block);
} /* end allocBlock */
/*==========================================================================
* link a chunk into the head of the free list.
*==========================================================================*/
static void freeBlock(Image *image, MemBlock *block)
{
INSERT_BLOCK(&image->freeBlockList, block);
} /* end freeBlock */
/*==========================================================================
* Allocate, initialize, and link in a new Page. Abort on allocation error.
*
* Return: pointer to newly created page.
*==========================================================================*/
static Page* newPage(Image *image, LogicalAddr addr, Page **insertlink)
{
Page *newpage;
newpage = allocBlock(image);
/*
* Future rev: In a system with limited memory and no virtual memory paging
* (MSDOS), it would be a good idea to be able to manually handle the
* following memory pages by swapping to disk (or EMS memory or whatever).
* Not trivial. Changes would be required here and other places.
*/
newpage->data = (char *)get_memory(image->pageSize);
if ( image->skipBytes )
memset(newpage->data, image->fillChar, image->pageSize);
INSERT_BLOCK(insertlink, newpage);
addr -= addr % image->pageSize;
newpage->begin = addr;
newpage->end = addr + image->pageSize - 1;
if ( newpage->end < newpage->begin )
{
/*
* Oops, user specified weird page size, and we overflowed!
* Trim it back so we don't wrap the address space.
*/
newpage->end = ULONG_MAX;
}
newpage->chunkList = (Chunk *)NULL;
return (newpage);
} /* end newPage */
/*==========================================================================
* See if the requested chunk may be combined with either or both of
* the preceeding and succeeding chunks. If so, then do the combination
* and various fixups on the chunk list, otherwise, create, initialize,
* and link a new chunk into the list.
*
* Return: a pointer to the new or combined chunk containing `addr'.
*==========================================================================*/
static Chunk* newChunk(Image *image, LogicalAddr addr, size_t nbytes,
Chunk **insertlink, Page *page)
{
#define PREV 1
#define NEXT 2
int combine = 0;
Chunk *newchunk;
Chunk *prevchunk;
Chunk *nextchunk;
/* See if we can combine new chunk with previous chunk. */
prevchunk = PREV_CHUNK(page, insertlink);
if ( prevchunk && prevchunk->end + 1 == addr )
combine |= PREV;
/* See if we can combine new chunk with next chunk. */
nextchunk = *insertlink;
if ( nextchunk && (nextchunk->begin) <= addr + nbytes )
combine |= NEXT;
switch (combine)
{
default:
newchunk = allocBlock(image);
INSERT_BLOCK(insertlink, newchunk);
newchunk->begin = addr;
newchunk->end = (nbytes - 1 < page->end - addr)
? addr + (nbytes - 1)
: page->end;
newchunk->data = page->data + (addr - page->begin);
break;
case PREV:
newchunk = prevchunk;
newchunk->end = (nbytes - 1 < page->end - addr)
? addr + (nbytes - 1)
: page->end;
break;
case NEXT:
newchunk = nextchunk;
newchunk->data -= newchunk->begin - addr;
newchunk->begin = addr;
break;
case PREV | NEXT:
newchunk = prevchunk;
newchunk->next = nextchunk->next;
newchunk->end = nextchunk->end;
freeBlock(image, nextchunk);
break;
}
return (newchunk);
} /* end newChunk */
/*==========================================================================
* Initialize an Image structure.
*==========================================================================*/
void imageInit(Image *image, int pagesize)
{
image->pageList = (Page *)NULL;
image->symbolList = (Page *)NULL;
image->oldPageLink = &image->pageList;
image->pageSize = pagesize;
image->blockGroupList = (BlockGroup *)NULL;
image->freeBlockList = (MemBlock *)NULL;
image->skipBytes = 0;
image->fillChar = '\0';
} /* end imageInit */
/*==========================================================================
* Free all the Pages, Chunks, and data memory in the indicated image.
*==========================================================================*/
void imageFree(Image *image)
{
void *mem;
Page *page;
BlockGroup *blocks;
page = image->pageList;
image->pageList = (Page *)NULL;
while ( page )
{
free(page->data);
page = page->next;
}
blocks = image->blockGroupList;
image->blockGroupList = (BlockGroup *)NULL;
while ( blocks )
{
mem = blocks;
blocks = blocks->next;
free(mem);
}
image->freeBlockList = (MemBlock *)NULL;
} /* end imageFree */
/*==========================================================================
* Merge `nbytes' from the `data' buffer into `image' at logical address `addr'.
*
* Assumptions:
*
* (1) The caller should have translated the record to only present data which
* is in range and should also have translated "word" addressing and length
* into appropriately offset "byte" logical addressing and length.
*
* (2) image->pageList points to the first page of a (possibly empty) linked
* list ordered by increasing logical address. Similarly, page->chunkList points
* to the first chunk of a (possibly empty) linked list ordered by increasing
* logical/physical address within the page.
*
* (3) newChunk() will combine/extend existing chunks as necessary and fix up
* the image->chunkList linkage. It also handles allocation errors -- it won't
* return unless memory was properly allocated. It may return less than the
* requested amount, due to page boundaries, but will always allocate a new
* chunk of at least one byte (or die trying). Similarly, newPage() creates
* a new page: if it returns, it succeeded.
*
* (4) Assuming that most records will be at monotonically increasing addresses
* with limited jumping around, we speed our list searches by maintaining the
* previous page and chunk links in static variables in the image structure to
* use as the most likely search starting point. It will still work if this
* assumption isn't true, but will be slower.
*==========================================================================*/
void imageWrite(Image *image, LogicalAddr addr, size_t nbytes, uchar *data)
{
Page **pageLink;
Chunk **chunkLink;
Page *nextPage;
Chunk *nextChunk;
char *dest;
size_t copylen;
int skipcnt = 0;
/*
* Don't bother if there is no data. If we are skipping bytes
* (presumably because we are interleaving several byte files into
* longer word oriented files) then the effective number of bytes
* to be transferred needs to be scaled up.
*/
if ( !nbytes )
{
return;
}
if ( debug )
{
printf("imageWrite(): Saving %4d byte record. RecordAdd=%08lX-%08lX, skipBytes=%d\n",
nbytes, addr, addr+nbytes-1, image->skipBytes );
}
if ( image->skipBytes )
{
nbytes *= image->skipBytes + 1;
nbytes -= image->skipBytes; /* trim trailing skipped bytes */
}
/* Initialize the page link if NULL or if addr is below last page */
pageLink = image->oldPageLink;
chunkLink = image->oldChunkLink;
/*
* We loop because page and chunk boundaries may require us to split
* the data into more than one section.
*/
while ( nbytes )
{
/* Find or create the page containing `addr'. */
if ( !(*pageLink) || addr < (*pageLink)->begin )
pageLink = &(image->pageList);
SEARCH_FOR_ADDR(pageLink, nextPage, addr);
if ( !nextPage || addr < nextPage->begin )
{
nextPage = newPage(image, addr, pageLink);
chunkLink = &(nextPage->chunkList);
}
/* Find or create chunk containing `addr'. */
if ( !(*chunkLink) || (*chunkLink)->begin < nextPage->begin
|| addr < (*chunkLink)->begin )
{
chunkLink = &(nextPage->chunkList);
}
SEARCH_FOR_ADDR(chunkLink, nextChunk, addr);
if ( !nextChunk || addr < nextChunk->begin )
nextChunk = newChunk(image, addr, nbytes, chunkLink, nextPage);
/* Copy as much data as will fit in this chunk. */
dest = nextChunk->data + (addr - nextChunk->begin);
copylen = (nbytes <= (nextChunk->end - addr) + 1)
? nbytes
: nextChunk->end - addr + 1;
if ( image->skipBytes )
{
/*
* Ugh! This loop is slow, but it guarantees that the byte
* allignment stays correct across chunk and page boundaries.
* Possible target of optimization, but use care!
*/
int i;
for ( i = copylen; i > 0; --i )
if ( !skipcnt )
{
*dest++ = *data++;
skipcnt = image->skipBytes;
}
else
{
++dest;
--skipcnt;
}
}
else
{
(void)memcpy(dest, data, copylen);
data += copylen;
}
addr += copylen;
if ( addr == 0 )
{
moan("Input record has logical addr overflow - data truncated");
break;
}
nbytes -= copylen;
}
image->oldPageLink = pageLink;
image->oldChunkLink = chunkLink;
} /* end imageWrite */
/*==========================================================================
* Read 'nbytes' of data from image starting at addr into buffer. Zero is
* used to fill uninitialized areas.
*==========================================================================*/
void imageRead(Image *image, LogicalAddr addr, size_t nbytes, uchar *data)
{
Page **pageLink;
Chunk **chunkLink;
Page *nextPage;
Chunk *nextChunk;
char *src;
size_t copylen;
int skipcnt = 0;
size_t totalCopyLen=0;
LogicalAddr addrSave;
/*
* Don't bother if there is no data. If we are skipping bytes
* (presumably because we are interleaving several byte files into
* longer word oriented files) then the effective number of bytes
* to be transferred needs to be scaled up.
*/
if ( !nbytes )
{
if ( debug )
printf("imageRead(): Looking for 0 byte image record starting at %08lX. Skipped search.\n", addr);
return;
}
if ( image->skipBytes )
{
nbytes *= image->skipBytes + 1;
nbytes -= image->skipBytes; /* trim trailing skipped bytes */
}
/* Initialize the page link if NULL or if addr is below last page */
pageLink = image->oldPageLink;
chunkLink = image->oldChunkLink;
/*
* We loop because page and chunk boundaries may require us to split
* the data into more than one section.
*/
if ( debug )
printf("imageRead(): Looking for %d byte image record starting at 0x%lX ...\n", nbytes, addr);
addrSave = addr;
while ( nbytes )
{
/* Find or create the page containing `addr'. */
if ( !(*pageLink) || addr < (*pageLink)->begin )
pageLink = &(image->pageList);
SEARCH_FOR_ADDR(pageLink, nextPage, addr);
if ( !nextPage || addr < nextPage->begin )
{
memset(data, 0, nbytes);
if ( debug )
{
if ( totalCopyLen )
printf("imageRead(): End of list. Copied %d bytes from 0x%lX-0x%lX\n", totalCopyLen, addrSave, addrSave + totalCopyLen - 1);
else
printf("imageRead(): End of list. Copied 0 bytes\n");
}
return;
}
/* Find or create chunk containing `addr'. */
if ( !(*chunkLink) || (*chunkLink)->begin < nextPage->begin
|| addr < (*chunkLink)->begin )
{
chunkLink = &(nextPage->chunkList);
}
SEARCH_FOR_ADDR(chunkLink, nextChunk, addr);
if ( !nextChunk || addr < nextChunk->begin )
{
memset(data, 0, nbytes);
if ( debug )
{
if ( totalCopyLen )
printf("imageRead(): End of list. Copied %d bytes from 0x%lX-0x%lX\n", totalCopyLen, addrSave, addrSave + totalCopyLen - 1);
else
printf("imageRead(): End of list. Copied 0 bytes\n");
}
return;
}
/* Copy as much data as is in this chunk. */
src = nextChunk->data + (addr - nextChunk->begin);
copylen = (nbytes <= nextChunk->end - addr + 1)
? nbytes
: nextChunk->end - addr + 1;
if ( image->skipBytes )
{
/*
* Ugh! This loop is slow, but it guarantees that the byte
* allignment stays correct across chunk and page boundaries.
* Possible target of optimization, but use care!
*/
int i;
for ( i = copylen; i > 0; --i )
if ( !skipcnt )
{
*data++ = *src++;
skipcnt = image->skipBytes;
}
else
{
++data;
--skipcnt;
}
}
else
{
(void)memcpy(data, src, copylen);
src += copylen;
data += copylen;
}
totalCopyLen += copylen;
addr += copylen;
if ( addr == 0 )
{
moan("Input record has logical addr overflow - data truncated");
break;
}
nbytes -= copylen;
}
image->oldPageLink = pageLink;
image->oldChunkLink = chunkLink;
if ( debug )
{
if ( totalCopyLen )
printf("imageRead(): Copied %d bytes from 0x%lX-0x%lX\n", totalCopyLen, addrSave, addrSave + totalCopyLen - 1);
else
printf("imageRead(): Copied 0 bytes\n");
}
} /* end imageRead */
/*==========================================================================
* Add symbol `sym' into the image symbol list of `image'.
*
* Assumptions:
*
* (1) image->symbolList points to the first record of a (possibly empty)
* linked list ordered by symbol arrival order.
*==========================================================================*/
void symbolWrite(Image *image, uchar *sym, int len)
{
Page *pg;
/* Don't bother if there is no symbol. */
if ( !sym || !*sym )
return;
printf("Symbols(%d): %s", len, sym);
/* Allocate a page and point pg to it */
if ( !image->symbolList )
pg = image->symbolList = get_memory(sizeof(Page));
else
{
for ( pg = image->symbolList; pg->next; pg = pg->next );
pg->next = get_memory(sizeof(Page));
pg = pg->next;
}
pg->chunkList = 0;
pg->begin = 0;
pg->data = get_memory(pg->end = len);
strcpy(pg->data, (char *)sym);
} /* end symbolWrite */
/*===========================================================================*/
/*===========================================================================*/
/*===========================================================================*/
/*===========================================================================*/
/*===========================================================================*/
static Page *curPage;
static Chunk *curChunk;
static ulong chunkBegin, chunkEnd;
static ulong chunkSize;
static uchar *chunkPtr;
/*==========================================================================*
* init_chunk_read - Initializes globals to read the given chunk.
*==========================================================================*/
static int init_chunk_read(Chunk *thisChunk)
{
curChunk = thisChunk;
chunkBegin = curChunk->begin; /* Save chunk's address range ... */
chunkEnd = curChunk->end;
chunkSize = chunkEnd - chunkBegin + 1; /* ... and size */
chunkPtr = (uchar *)curChunk->data; /* and point to the data */
return chunkSize != 0;
} /* end init_chunk_read */
/*==========================================================================*
* init_reader - Initializes the ImageRead variables. Call before using
* readImage.
*==========================================================================*/
int init_reader(GPF *gpf)
{
if ( !gpf || !gpf->image.pageList )
return 0;
curPage = gpf->image.pageList; /* Point to the first page in list */
return init_chunk_read(curPage->chunkList);
} /* end init_reader */
/*==========================================================================*
* readImage - This routine extracts data from the image structure init'ed
* by init_reader a record at a time. It handles filling if
* needed.
* Inputs:
* gpf - GPF structure that contains flags & characteristics to apply.
* bufferPtr - Buffer to hold the data.
* bufferSpace - Size of the buffer in bytes.
* low_address - low address of data to retrieve.
* high_address- high address of data to retrieve.
* Outputs:
* buffer - buffer filled with data (and/or fill character).
* new_low - Actual low_address used.
* bytesRead - Number of bytes returned in the buffer.
* returns - 1 for success, 0 for no more bytes, -1 for errors.
*==========================================================================*/
int readImage(GPF *gpf, uchar *bufferPtr, ulong bufferSpace,
ulong low_address, ulong high_address, ulong *new_low,
int *bytesRead)
{
uchar fill = (uchar)(gpf->flags & GPF_M_FILL);
ulong amount, startSpace;
int err, more_bytes;
/*
* Requested data is before this chunk, this implies that there is no
* data before this chunk (would have been handled by a previous call),
* so either return a filled buffer, or an empty buffer.
*/
*new_low = low_address;
if ( high_address < chunkBegin )
{
if ( fill )
{
memset(bufferPtr, gpf->fill_char, *bytesRead = bufferSpace);
return 1;
}
else
{
*new_low = chunkBegin;
*bytesRead = 0;
return 1;
}
}
/*
* If the chunk contains part of the data, we may have to fill some
* of the buffer to account for space before the chunk's data.
*/
/* bufferSpace = high_address - low_address + 1; */
startSpace = bufferSpace;
if ( low_address < chunkBegin )
{
/*
* Calculate how much fill is needed. Fill the start of the buffer
* if needed, then adjust the pointers to get chunk data into the
* correct buffer location. If no fill used, point the actual
* starting address at the beginning of the chunk for the next call,
* and return immediately.
*/
if ( fill )
{
amount = min(bufferSpace, chunkBegin - low_address);
memset(bufferPtr, gpf->fill_char, amount);
bufferSpace -= amount;
bufferPtr += amount;
if ( !bufferSpace )
{
*bytesRead = startSpace;
return 1;
}
}
else
{
*new_low = chunkBegin;
*bytesRead = 0;
return 1;
}
low_address = chunkBegin;
}
/*
* At this point the requested data lies either within the chunk or
* after the chunk. Check for data within the chunk and extract it.
* There may be data that sits in the next chunk, so we have to limit
* extraction to the end of this chunk.
*/
if ( low_address <= chunkEnd )
{
amount = min(bufferSpace, chunkEnd - low_address + 1);
memcpy(bufferPtr, chunkPtr, amount);
bufferPtr += amount;
bufferSpace -= amount;
chunkPtr += amount;
low_address += amount;
if ( !bufferSpace )
{
*bytesRead = startSpace;
return 1;
}
}
/*
* If we are still around, there is no more data in this chunk.
* Move to the next chunk and look there. If there aren't anymore
* chunks, return.
*/
if ( !(curChunk = curChunk->next) )
{
/* No more chunks on this page, look to the next one. */
if ( !(curPage = curPage->next) )
{
/*
* No more pages, fill in the end of the buffer if needed
* and return.
*/
if ( fill )
{
memset(bufferPtr, gpf->fill_char, bufferSpace);
bufferSpace = 0;
}
*bytesRead = startSpace - bufferSpace;
return 0;
}
curChunk = curPage->chunkList;
}
if ( !init_chunk_read(curChunk) )
{
*bytesRead = startSpace - bufferSpace;
return 0;
}
/*
* Now that we are set up for the next chunk, a recursive call should
* be able to handle any remaining data.
*/
err = readImage(gpf, bufferPtr, bufferSpace, low_address, high_address,
&low_address, &more_bytes);
*bytesRead = startSpace - bufferSpace + more_bytes;
return err;
} /* end readImage */
/*===========================================================================*/
/*===========================================================================*/
/*===========================================================================*/
/*===========================================================================*/
/*===========================================================================*/
#ifdef DEBUG
/*==========================================================================
* Dump the Image to a file. Outputs a detailed page and chunk
* description of the entire Image in human decipherable text,
* for use as a debug tool.
*==========================================================================*/
void imageDump(Image *image, FILE *dump)
{
Page *page;
Chunk *chunk;
int i, j, size, nchunk, npage;
char buf[140];
char *cp;
uchar *data;
npage = 0;
for ( page = image->pageList; page; page = page->next )
{
fprintf(dump, " page %d (%04lX-%04lX)\n", ++npage, page->begin, page->end);
nchunk = 0;
for ( chunk = page->chunkList; chunk; chunk = chunk->next )
{
size = chunk->end - chunk->begin + 1;
fprintf(dump, " chunk %d at %04lX-%04lX (%d bytes):\n", ++nchunk, chunk->begin, chunk->end, size);
i = 0 - (int)(chunk->begin & 0xF);
data = (uchar *)(chunk->data);
do
{
cp = buf;
sprintf(cp, " %8.4lX: ", chunk->begin + i);
cp += strlen(cp);
for ( j = i + 16; i < j; ++i )
{
if ( i < 0 || i >= size )
{
sprintf(cp, " ");
cp += strlen(cp);
}
else
{
sprintf(cp, " %02X", *data++);
cp += strlen(cp);
}
}
*cp++ = '\n';
*cp = '\0';
fputs(buf, dump);
} while ( i < size );
}
putc('\n', dump);
}
} /* end imageDump */
/*==========================================================================
* Perform and internal consistency check on an Image and return
* a status code. For use as a debug tool.
*==========================================================================*/
int imageCheck(Image *image, FILE *log)
{
Page *page;
Chunk *chunk;
LogicalAddr addr;
int pagecnt, chunkcnt;
int haserror = 0;
/* Check for pages and chunks in order and non-overlapping. */
addr = 0;
pagecnt = 0;
for ( page = image->pageList; page; page = page->next )
{
++pagecnt;
if ( page->begin < addr )
{
fprintf(log, "Page %d has bad beginning addr 0x%4X\n",
pagecnt, page->begin);
haserror = 1;
addr = page->begin;
}
chunkcnt = 0;
for ( chunk = page->chunkList; chunk; chunk = chunk->next )
{
++chunkcnt;
if ( chunk->begin < addr )
{
fprintf(log, "Chunk %d in Page %d has bad beginning addr 0x%4X\n",
chunkcnt, pagecnt, chunk->begin);
haserror = 1;
addr = chunk->begin;
}
if ( chunk->end < addr )
{
fprintf(log, "Chunk %d in Page %d has bad ending addr 0x%4X\n",
chunkcnt, pagecnt, chunk->end);
haserror = 1;
}
else
addr = chunk->end + 1;
}
if ( page->end < (addr - 1) )
{
fprintf(log, "Page %d has bad ending addr 0x%4X\n",
pagecnt, page->end);
haserror = 1;
}
else
addr = page->end + 1;
}
return (haserror);
} /* end imageCheck */
#endif /* DEBUG */