Subversion Repositories freemyipod

Rev

Rev 445 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 445 Rev 447
Line 8... Line 8...
8
 */
8
 */
9
 
9
 
10
#include <assert.h>
10
#include <assert.h>
11
#include <stdio.h>
11
#include <stdio.h>
12
#include <stdlib.h>
12
#include <stdlib.h>
-
 
13
#include <string.h>
13
 
14
 
14
#include <zlib.h>
15
//#include <zlib.h>
-
 
16
#include "ucl/ucl.h"
15
#include "compress.h"
17
#include "compress.h"
16
#include "stubs.h"
18
#include "stubs.h"
17
 
19
 
18
/* Open an (uncompressed) file as a stream.  Return 0 on success, 1 on
20
/* Open an (uncompressed) file as a stream.  Return 0 on success, 1 on
19
   error.
21
   error.
Line 22... Line 24...
22
   String constants work fine.  */
24
   String constants work fine.  */
23
 
25
 
24
int
26
int
25
fopen_stream_u(stream *fp, const char *path, const char *mode)
27
fopen_stream_u(stream *fp, const char *path, const char *mode)
26
{
28
{
27
	fp->u.filep = fopen(path, mode);
29
	fp->filep = fopen(path, mode);
28
	fp->type = (fp->u.filep) ? UNCOMPRESSED : INVALID;
30
	fp->type = (fp->filep) ? UNCOMPRESSED : INVALID;
29
	fp->mode = mode;
31
	fp->mode = mode;
-
 
32
    fp->buffer = NULL;
-
 
33
    fp->bufsize = 0;
-
 
34
    fp->bufused = 0;
30
	return (fp->u.filep) ? 0 : 1;
35
	return (fp->filep) ? 0 : 1;
31
}
36
}
32
 
37
 
33
/* Read from stream.  Return number of elements read.  */
38
/* Read from stream.  Return number of elements read.  */
34
 
39
 
35
size_t
40
size_t
Line 37... Line 42...
37
{
42
{
38
	size_t read;
43
	size_t read;
39
 
44
 
40
	switch (str->type) {
45
	switch (str->type) {
41
		case UNCOMPRESSED:
46
		case UNCOMPRESSED:
42
		read = fread(ptr, size, nmemb, str->u.filep);
47
		read = fread(ptr, size, nmemb, str->filep);
43
		break;
48
		break;
44
 
49
 
45
		case COMPRESSED:
50
//		case COMPRESSED:
46
		read = gzread(str->u.gzfilep, ptr, size * nmemb) / size;
51
//		read = gzread(str->u.gzfilep, ptr, size * nmemb) / size;
47
		break;
52
//		break;
48
 
53
 
49
		default:
54
		default:
50
		abort();
55
		abort();
51
	}
56
	}
52
 
57
 
Line 60... Line 65...
60
{
65
{
61
	size_t written;
66
	size_t written;
62
 
67
 
63
	switch (str->type) {
68
	switch (str->type) {
64
		case UNCOMPRESSED:
69
		case UNCOMPRESSED:
65
		written = fwrite(ptr, size, nmemb, str->u.filep);
70
		written = fwrite(ptr, size, nmemb, str->filep);
66
		break;
71
		break;
67
 
72
 
68
		case COMPRESSED:
73
		case COMPRESSED:
-
 
74
        if (str->bufused + size * nmemb > str->bufsize)
-
 
75
        {
-
 
76
            str->bufsize = (str->bufused + size * nmemb + 0xfff) & ~0xfff;
-
 
77
            str->buffer = (char*)realloc(str->buffer, str->bufsize);
-
 
78
        }
-
 
79
        memcpy(&str->buffer[str->bufused], ptr, size * nmemb);
-
 
80
        str->bufused += size * nmemb;
-
 
81
        written = nmemb;
69
		written = gzwrite(str->u.gzfilep, ptr, size * nmemb) / size;
82
		//written = gzwrite(str->u.gzfilep, ptr, size * nmemb) / size;
70
		break;
83
		break;
71
 
84
 
72
		default:
85
		default:
73
		abort();
86
		abort();
74
	}
87
	}
Line 81... Line 94...
81
int
94
int
82
fclose_stream(stream *str)
95
fclose_stream(stream *str)
83
{
96
{
84
	switch (str->type) {
97
	switch (str->type) {
85
		case UNCOMPRESSED:
98
		case UNCOMPRESSED:
86
		return fclose(str->u.filep);
99
		return fclose(str->filep);
87
 
100
 
88
		case COMPRESSED:
101
		case COMPRESSED:
-
 
102
        {
-
 
103
            int outsize = str->bufused + str->bufused / 8 + 256;
-
 
104
            str->buffer = (char*)realloc(str->buffer, str->bufused);
-
 
105
            char* buffer = (char*)malloc(outsize);
-
 
106
            int r = ucl_nrv2e_99_compress(str->buffer, str->bufused, buffer, &outsize, 0, 10, NULL, NULL);
-
 
107
            free(str->buffer);
-
 
108
            if (r != UCL_E_OK)
-
 
109
            {
-
 
110
                free(buffer);
-
 
111
                /* this should NEVER happen */
-
 
112
                printf("internal error - compression failed: %d\n", r);
-
 
113
                return 2;
-
 
114
            }
-
 
115
            buffer = (char*)realloc(buffer, outsize);
-
 
116
            int done = 0;
-
 
117
            while (done < outsize)
-
 
118
            {
-
 
119
                int bytes = fwrite(&buffer[done], 1, outsize - done, str->filep);
-
 
120
                if (bytes <= 0) return 3;
-
 
121
                done += bytes;
-
 
122
            }
-
 
123
            free(buffer);
-
 
124
		    return fclose(str->filep);
-
 
125
        }
89
		return gzclose(str->u.gzfilep);
126
//		return gzclose(str->u.gzfilep);
90
 
127
 
91
		default:
128
		default:
92
		abort();
129
		abort();
93
	}
130
	}
94
 
131
 
Line 98... Line 135...
98
int
135
int
99
ferror_stream(stream *str)
136
ferror_stream(stream *str)
100
{
137
{
101
	switch (str->type) {
138
	switch (str->type) {
102
		case UNCOMPRESSED:
139
		case UNCOMPRESSED:
103
		return ferror(str->u.filep);
140
		return ferror(str->filep);
104
 
141
 
105
		case COMPRESSED:
142
		case COMPRESSED:
-
 
143
		return ferror(str->filep);
106
		{
144
//		{
107
			const char *err;
145
//			const char *err;
108
			int errno;
146
//			int errno;
109
 
147
//
110
			err = gzerror(str->u.gzfilep, &errno);
148
//			err = gzerror(str->u.gzfilep, &errno);
111
			if (errno == Z_OK || errno == Z_STREAM_END)
149
//			if (errno == Z_OK || errno == Z_STREAM_END)
112
				return 0;
150
//				return 0;
113
			else if (errno == Z_ERRNO)
151
//			else if (errno == Z_ERRNO)
114
				return 1;
152
//				return 1;
115
			else {
153
//			else {
116
				fprintf(stderr, "%s\n", err);
154
//				fprintf(stderr, "%s\n", err);
117
				return 1;
155
//				return 1;
118
			}
156
//			}
119
		}
157
//		}
120
		break;
158
//		break;
121
 
159
 
122
		default:
160
		default:
123
		abort();
161
		abort();
124
	}
162
	}
125
 
163
 
Line 129... Line 167...
129
int
167
int
130
fseek_stream(stream *str, long offset, int whence)
168
fseek_stream(stream *str, long offset, int whence)
131
{
169
{
132
	switch (str->type) {
170
	switch (str->type) {
133
		case UNCOMPRESSED:
171
		case UNCOMPRESSED:
134
		return fseek(str->u.filep, offset, whence);
172
		return fseek(str->filep, offset, whence);
135
 
173
 
136
		case COMPRESSED:
174
//		case COMPRESSED:
137
		return gzseek(str->u.gzfilep, offset, whence);
175
//		return gzseek(str->u.gzfilep, offset, whence);
138
 
176
 
139
		default:
177
		default:
140
		abort();
178
		abort();
141
	}
179
	}
142
}
180
}
Line 154... Line 192...
154
		return;
192
		return;
155
 
193
 
156
	if (str->type == INVALID)
194
	if (str->type == INVALID)
157
		abort();
195
		abort();
158
 
196
 
159
	fd = fileno(str->u.filep);
197
	fd = fileno(str->filep);
160
	/* Get current (buffered) file position.  */
198
	/* Get current (buffered) file position.  */
161
	offset = ftell(str->u.filep);
199
	offset = ftell(str->filep);
162
 
200
 
163
	/* Make sure there's nothing left in buffers.  */
201
	/* Make sure there's nothing left in buffers.  */
164
	fflush(str->u.filep);
202
	fflush(str->filep);
165
 
203
 
166
	/* Reposition underlying FD.  (Might be unnecessary?)  */
204
	/* Reposition underlying FD.  (Might be unnecessary?)  */
167
	roffset = lseek(fd, offset, SEEK_SET);
205
	roffset = lseek(fd, offset, SEEK_SET);
168
 
206
 
169
	assert(roffset == offset);
207
	assert(roffset == offset);
170
 
208
 
171
	/* Reopen as compressed stream.  */
209
	/* Reopen as compressed stream.  */
172
	str->u.gzfilep = gzdopen(fd, str->mode);
210
	//str->u.gzfilep = gzdopen(fd, str->mode);
173
	gzsetparams(str->u.gzfilep, 9, Z_DEFAULT_STRATEGY);
211
	//gzsetparams(str->u.gzfilep, 9, Z_DEFAULT_STRATEGY);
174
	str->type = COMPRESSED;
212
	str->type = COMPRESSED;
175
}
213
}
176
 
214
 
177
void
215
void
178
transfer(stream *ifp, stream *ofp, int count)
216
transfer(stream *ifp, stream *ofp, int count)