-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathio.c
More file actions
146 lines (120 loc) · 3.7 KB
/
io.c
File metadata and controls
146 lines (120 loc) · 3.7 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
#include "io.h"
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
/*
* Reads from the compound document the stream that corresponds to the given directory
* entry. It allocates and returns the data of the stream in the `buffer'.
*/
int
comp_doc_read_stream(comp_doc_file_t *file, comp_doc_directory_t *dir, unsigned char **buffer)
{
if(!IS_DIR_STREAM(dir))
return COMP_DOC_NO_STREAM;
unsigned char *buf, *pos;
uint32_t total, count;
ssize_t bytes_read;
int err;
err = COMP_DOC_SUCCESS;
total = 0;
buf = malloc(dir->size);
if(buf == NULL)
{
err = COMP_DOC_NO_MEM;
goto _error;
}
pos = buf;
if(dir->size >= file->hdr->stream_min_size)
{
//then the stream is divided in regular sectors
//use SAT to construct it
comp_doc_sector_id_t *sector = &file->sat->secids[dir->first_sector];
if(lseek(file->fd, sector_position(file->hdr, dir->first_sector), SEEK_SET) == ((off_t)-1))
{
err = COMP_DOC_SEEK_ERR;
goto _error;
}
if(CALC_SECTOR_SIZE(file->hdr->ssz) > dir->size)
count = dir->size;
else
count = CALC_SECTOR_SIZE(file->hdr->ssz);
if((bytes_read = read_exactly(file->fd, buf, count)) < 0)
{
err = COMP_DOC_READ_ERR;
goto _error;
}
total += bytes_read;
pos += bytes_read;
while(sector->next != NULL)
{
if(lseek(file->fd, sector_position(file->hdr, sector->value), SEEK_SET) == (off_t)-1)
{
err = COMP_DOC_SEEK_ERR;
goto _error;
}
if(CALC_SECTOR_SIZE(file->hdr->ssz) > dir->size - total)
count = dir->size - total;
else
count = CALC_SECTOR_SIZE(file->hdr->ssz);
//printf("Copying: %x\n", count);
if((bytes_read = read_exactly(file->fd, pos, count)) < 0)
{
err = COMP_DOC_READ_ERR;
goto _error;
}
total += bytes_read;
pos += bytes_read;
sector = sector->next;
}
}
else
{
// the stream consists of short sectors
comp_doc_sector_id_t *sector = &file->ssat->secids[dir->first_sector];
if(lseek(file->fd, short_sector_position(file, dir->first_sector), SEEK_SET) < 0)
{
err = COMP_DOC_SEEK_ERR;
goto _error;
}
if(CALC_SHORT_SECTOR_SIZE(file->hdr->sssz) > dir->size)
count = dir->size;
else
count = CALC_SHORT_SECTOR_SIZE(file->hdr->sssz);
if((bytes_read = read_exactly(file->fd, buf, count)) < 0)
{
err = COMP_DOC_READ_ERR;
goto _error;
}
total += bytes_read;
pos += bytes_read;
while(sector->next != NULL)
{
if(lseek(file->fd, short_sector_position(file, sector->value), SEEK_SET) < 0)
{
err = COMP_DOC_SEEK_ERR;
goto _error;
}
if(CALC_SHORT_SECTOR_SIZE(file->hdr->sssz) > dir->size - total)
count = dir->size - total;
else
count = CALC_SHORT_SECTOR_SIZE(file->hdr->sssz);
if((bytes_read = read_exactly(file->fd, pos, count)) < 0)
{
err = COMP_DOC_READ_ERR;
goto _error;
}
total += bytes_read;
pos += bytes_read;
sector = sector->next;
}
}
*buffer = buf;
_error:
if(err != COMP_DOC_SUCCESS)
{
if(buf)
free(buf);
}
return err;
}