File: lha_basic_reader.c

package info (click to toggle)
lhasa 0.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 12,364 kB
  • sloc: ansic: 6,796; sh: 1,938; makefile: 168; python: 129
file content (158 lines) | stat: -rw-r--r-- 3,536 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
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
/*

Copyright (c) 2011, 2012, Simon Howard

Permission to use, copy, modify, and/or distribute this software
for any purpose with or without fee is hereby granted, provided
that the above copyright notice and this permission notice appear
in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "crc16.h"

#include "lha_decoder.h"
#include "lha_basic_reader.h"

struct _LHABasicReader {
	LHAInputStream *stream;
	LHAFileHeader *curr_file;
	size_t curr_file_remaining;
	int eof;
};

LHABasicReader *lha_basic_reader_new(LHAInputStream *stream)
{
	LHABasicReader *reader;

	reader = calloc(1, sizeof(LHABasicReader));

	if (reader == NULL) {
		return NULL;
	}

	reader->stream = stream;
	reader->curr_file = NULL;
	reader->curr_file_remaining = 0;
	reader->eof = 0;

	return reader;
}

void lha_basic_reader_free(LHABasicReader *reader)
{
	if (reader->curr_file != NULL) {
		lha_file_header_free(reader->curr_file);
	}

	free(reader);
}

LHAFileHeader *lha_basic_reader_curr_file(LHABasicReader *reader)
{
	return reader->curr_file;
}

LHAFileHeader *lha_basic_reader_next_file(LHABasicReader *reader)
{
	// Free the current file header and skip over any remaining
	// compressed data that hasn't been read yet.

	if (reader->curr_file != NULL) {
		lha_file_header_free(reader->curr_file);
		reader->curr_file = NULL;

		if (!lha_input_stream_skip(reader->stream,
		                           reader->curr_file_remaining)) {
			reader->eof = 1;
		}
	}

	if (reader->eof) {
		return NULL;
	}

	// Read the header for the next file.

	reader->curr_file = lha_file_header_read(reader->stream);

	if (reader->curr_file == NULL) {
		reader->eof = 1;
		return NULL;
	}

	reader->curr_file_remaining = reader->curr_file->compressed_length;

	return reader->curr_file;
}

size_t lha_basic_reader_read_compressed(LHABasicReader *reader, void *buf,
                                        size_t buf_len)
{
	size_t bytes;

	if (reader->eof || reader->curr_file_remaining == 0) {
		return 0;
	}

	// Read up to the number of bytes of compressed data remaining.

	if (buf_len > reader->curr_file_remaining) {
		bytes = reader->curr_file_remaining;
	} else {
		bytes = buf_len;
	}

	if (!lha_input_stream_read(reader->stream, buf, bytes)) {
		reader->eof = 1;
		return 0;
	}

	// Update counter and return success.

	reader->curr_file_remaining -= bytes;

	return bytes;
}

static size_t decoder_callback(void *buf, size_t buf_len, void *user_data)
{
	return lha_basic_reader_read_compressed(user_data, buf, buf_len);
}

// Create the decoder structure to decode the current file.

LHADecoder *lha_basic_reader_decode(LHABasicReader *reader)
{
	const LHADecoderType *dtype;

	if (reader->curr_file == NULL) {
		return NULL;
	}

	// Look up the decoder to use for this compression method.

	dtype = lha_decoder_for_name(reader->curr_file->compress_method);

	if (dtype == NULL) {
		return NULL;
	}

	// Create decoder.

	return lha_decoder_new(dtype, decoder_callback, reader,
	                       reader->curr_file->length);
}