File: XADBlockHandle.m

package info (click to toggle)
unar 1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 6,664 kB
  • sloc: ansic: 52,939; objc: 39,563; cpp: 4,074; makefile: 99; perl: 10
file content (128 lines) | stat: -rw-r--r-- 2,357 bytes parent folder | download
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
#import "XADBlockHandle.h"

@implementation XADBlockHandle

-(id)initWithHandle:(CSHandle *)handle blockSize:(int)size
{
	if((self=[super initWithName:[handle name]]))
	{
		parent=[handle retain];
		currpos=0;
		length=CSHandleMaxLength;
		numblocks=0;
		blocksize=size;
		blockoffsets=NULL;
	}
	return self;
}

-(id)initWithHandle:(CSHandle *)handle length:(off_t)maxlength blockSize:(int)size
{
	if((self=[super initWithName:[handle name]]))
	{
		parent=[handle retain];
		currpos=0;
		length=maxlength;
		numblocks=0;
		blocksize=size;
		blockoffsets=NULL;
	}
	return self;
}

-(void)dealloc
{
	[parent release];
	[super dealloc];
}


-(void)setBlockChain:(uint32_t *)blocktable numberOfBlocks:(int)totalblocks
firstBlock:(uint32_t)first headerSize:(off_t)headersize
{
	numblocks=0;
	uint32_t block=first;
	while(block<totalblocks)
	{
		block=blocktable[block];
		numblocks++;
	}

	free(blockoffsets);
	blockoffsets=malloc(numblocks*sizeof(off_t));

	block=first;
	for(int i=0;i<numblocks;i++)
	{
		blockoffsets[i]=headersize+block*blocksize;
		block=blocktable[block];
	}
}

-(off_t)fileSize
{
	if(length<numblocks*blocksize) return length;
	return numblocks*blocksize;
}

-(off_t)offsetInFile
{
	return currpos;
}

-(BOOL)atEndOfFile
{
	if(currpos==numblocks*blocksize) return YES;
	if(currpos==length) return YES;
	return NO;
}

-(void)seekToFileOffset:(off_t)offs
{
	if(offs<0) [self _raiseEOF];
	if(offs>numblocks*blocksize) [self _raiseEOF];
	if(offs>length) [self _raiseEOF];

	int block=(offs-1)/blocksize;

	[parent seekToFileOffset:blockoffsets[block]+offs-block*blocksize];
	currpos=offs;
}

-(void)seekToEndOfFile
{
	if(length!=CSHandleMaxLength) [self seekToFileOffset:length];
	else [self seekToFileOffset:numblocks*blocksize];
}

-(int)readAtMost:(int)num toBuffer:(void *)buffer
{
	uint8_t *bytebuffer=(uint8_t *)buffer;
	int total=0;

	if(currpos+num>length) num=length-currpos;

	while(total<num)
	{
		int blockpos=currpos%blocksize;
		if(blockpos==0)
		{
			int block=currpos/blocksize;
			if(block==numblocks) return total;
			[parent seekToFileOffset:blockoffsets[block]];
		}

		int numbytes=num-total;
		if(numbytes>blocksize-blockpos) numbytes=blocksize-blockpos;

		int actual=[parent readAtMost:numbytes toBuffer:&bytebuffer[total]];
		if(actual==0) return total;

		total+=actual;
		currpos+=actual;
	}

	return total;
}

@end