Subversion Repositories freemyipod

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
65 cmwslw 1
/* uclpack.c -- example program: a simple file packer
2
 
3
   This file is part of the UCL data compression library.
4
 
5
   Copyright (C) 1996-2002 Markus Franz Xaver Johannes Oberhumer
6
   All Rights Reserved.
7
 
8
   The UCL library is free software; you can redistribute it and/or
9
   modify it under the terms of the GNU General Public License as
10
   published by the Free Software Foundation; either version 2 of
11
   the License, or (at your option) any later version.
12
 
13
   The UCL library is distributed in the hope that it will be useful,
14
   but WITHOUT ANY WARRANTY; without even the implied warranty of
15
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
   GNU General Public License for more details.
17
 
18
   You should have received a copy of the GNU General Public License
19
   along with the UCL library; see the file COPYING.
20
   If not, write to the Free Software Foundation, Inc.,
21
   59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22
 
23
   Markus F.X.J. Oberhumer
24
   <markus@oberhumer.com>
25
 */
26
 
27
 
28
/*************************************************************************
29
// NOTE: this is an example program, so do not use to backup your data
30
//
31
// This program lacks things like sophisticated file handling but is
32
// pretty complete regarding compression - it should provide a good
33
// starting point for adaption for you applications.
34
**************************************************************************/
35
 
36
#include <ucl/ucl.h>
37
#include "lutil.h"
38
 
39
static const char *progname = NULL;
40
 
41
static unsigned long total_in = 0;
42
static unsigned long total_out = 0;
43
 
44
/* magic file header for compressed files */
45
static const unsigned char magic[8] =
46
    { 0x00, 0xe9, 0x55, 0x43, 0x4c, 0xff, 0x01, 0x1a };
47
 
48
 
49
/*************************************************************************
50
// file IO
51
**************************************************************************/
52
 
53
ucl_uint xread(FILE *f, ucl_voidp buf, ucl_uint len, ucl_bool allow_eof)
54
{
55
    ucl_uint l;
56
 
57
    l = ucl_fread(f,buf,len);
58
    if (l > len)
59
    {
60
        fprintf(stderr,"\nsomething's wrong with your C library !!!\n");
61
        exit(1);
62
    }
63
    if (l != len && !allow_eof)
64
    {
65
        fprintf(stderr,"\nread error - premature end of file\n");
66
        exit(1);
67
    }
68
    total_in += l;
69
    return l;
70
}
71
 
72
ucl_uint xwrite(FILE *f, const ucl_voidp buf, ucl_uint len)
73
{
74
    ucl_uint l;
75
 
76
    if (f != NULL)
77
    {
78
        l = ucl_fwrite(f,buf,len);
79
        if (l != len)
80
        {
81
            fprintf(stderr,"\nwrite error [%ld %ld]  (disk full ?)\n",
82
                   (long)len, (long)l);
83
            exit(1);
84
        }
85
    }
86
    total_out += len;
87
    return len;
88
}
89
 
90
 
91
int xgetc(FILE *f)
92
{
93
    unsigned char c;
94
    xread(f,(ucl_voidp) &c,1,0);
95
    return c;
96
}
97
 
98
void xputc(FILE *f, int c)
99
{
100
    unsigned char cc = (unsigned char) c;
101
    xwrite(f,(const ucl_voidp) &cc,1);
102
}
103
 
104
/* read and write portable 32-bit integers */
105
 
106
ucl_uint32 xread32(FILE *f)
107
{
108
    unsigned char b[4];
109
    ucl_uint32 v;
110
 
111
    xread(f,b,4,0);
112
    v  = (ucl_uint32) b[3] <<  0;
113
    v |= (ucl_uint32) b[2] <<  8;
114
    v |= (ucl_uint32) b[1] << 16;
115
    v |= (ucl_uint32) b[0] << 24;
116
    return v;
117
}
118
 
119
void xwrite32(FILE *f, ucl_uint32 v)
120
{
121
    unsigned char b[4];
122
 
123
    b[3] = (unsigned char) (v >>  0);
124
    b[2] = (unsigned char) (v >>  8);
125
    b[1] = (unsigned char) (v >> 16);
126
    b[0] = (unsigned char) (v >> 24);
127
    xwrite(f,b,4);
128
}
129
 
130
 
131
/*************************************************************************
132
// compress
133
**************************************************************************/
134
 
135
int do_compress(FILE *fi, FILE *fo, ucl_uint size)
136
{
137
    int r = 0;
138
    ucl_byte *in = NULL;
139
    ucl_byte *out = NULL;
140
    ucl_uint in_len;
141
    ucl_uint out_len;
142
    ucl_uint overhead = size / 8 + 256;
143
 
144
    total_in = total_out = 0;
145
 
146
/*
147
 * Step 1: allocate compression buffers and work-memory
148
 */
149
    in = (ucl_byte *) ucl_malloc(size);
150
    out = (ucl_byte *) ucl_malloc(size + overhead);
151
    if (in == NULL || out == NULL)
152
    {
153
        printf("%s: out of memory\n", progname);
154
        r = 1;
155
        goto err;
156
    }
157
 
158
/*
159
 * Step 2: compress (single block)
160
 */
161
    /* read block */
162
    in_len = xread(fi,in,size,1);
163
 
164
    r = ucl_nrv2e_99_compress(in,in_len,out,&out_len,0,10,NULL,NULL);
165
    if (r != UCL_E_OK || out_len > in_len + overhead)
166
    {
167
        /* this should NEVER happen */
168
        printf("internal error - compression failed: %d\n", r);
169
        r = 2;
170
        goto err;
171
    }
172
 
173
    xwrite(fo,out,out_len);
174
 
175
    r = 0;
176
err:
177
    ucl_free(out);
178
    ucl_free(in);
179
    return r;
180
}
181
 
182
 
183
/*************************************************************************
184
//
185
**************************************************************************/
186
 
187
static void usage(void)
188
{
189
    printf("usage: %s input-file output-file  (compress)\n", progname);
190
    exit(1);
191
}
192
 
193
 
194
/* open input file */
195
static FILE *xopen_fi(const char *name)
196
{
197
    FILE *f;
198
 
199
    f = fopen(name,"rb");
200
    if (f == NULL)
201
    {
202
        printf("%s: cannot open input file %s\n", progname, name);
203
        exit(1);
204
    }
205
#if defined(HAVE_STAT) && defined(S_ISREG)
206
    {
207
        struct stat st;
208
#if defined(HAVE_LSTAT)
209
        if (lstat(name,&st) != 0 || !S_ISREG(st.st_mode))
210
#else
211
        if (stat(name,&st) != 0 || !S_ISREG(st.st_mode))
212
#endif
213
        {
214
            printf("%s: %s is not a regular file\n", progname, name);
215
            fclose(f);
216
            exit(1);
217
        }
218
    }
219
#endif
220
    return f;
221
}
222
 
223
 
224
/* open output file */
225
static FILE *xopen_fo(const char *name)
226
{
227
    FILE *f;
228
 
229
#if 0
230
    /* this is an example program, so make sure we don't overwrite a file */
231
    f = fopen(name,"rb");
232
    if (f != NULL)
233
    {
234
        printf("%s: file %s already exists -- not overwritten\n", progname, name);
235
        fclose(f);
236
        exit(1);
237
    }
238
#endif
239
    f = fopen(name,"wb");
240
    if (f == NULL)
241
    {
242
        printf("%s: cannot open output file %s\n", progname, name);
243
        exit(1);
244
    }
245
    return f;
246
}
247
 
248
 
249
/*************************************************************************
250
//
251
**************************************************************************/
252
 
253
int main(int argc, char *argv[])
254
{
255
    int i = 1;
256
    int r = 0;
257
    FILE *fi = NULL;
258
    FILE *fo = NULL;
259
    const char *in_name = NULL;
260
    const char *out_name = NULL;
261
    int size;
262
    const char *s;
263
 
264
#if defined(__EMX__)
265
    _response(&argc,&argv);
266
    _wildcard(&argc,&argv);
267
#endif
268
    progname = argv[0];
269
    for (s = progname; *s; s++)
270
        if (*s == '/' || *s == '\\')
271
            progname = s + 1;
272
 
273
    printf("\nUCL real-time data compression library (v%s, %s).\n",
274
            ucl_version_string(), ucl_version_date());
275
    printf("Copyright (C) 1996-2002 Markus Franz Xaver Johannes Oberhumer\n\n");
276
 
277
/*
278
 * Step 1: initialize the UCL library
279
 */
280
    if (ucl_init() != UCL_E_OK)
281
    {
282
        printf("ucl_init() failed !!!\n");
283
        exit(1);
284
    }
285
 
286
    if (i + 2 != argc)
287
        usage();
288
 
289
/*
290
 * Step 2: process file(s)
291
 */
292
    in_name = argv[i++];
293
    out_name = argv[i++];
294
    fi = xopen_fi(in_name);
295
    fo = xopen_fo(out_name);
296
    fseek(fi, 0, SEEK_END);
297
    size = ftell(fi);
298
    fseek(fi, 0, SEEK_SET);
299
    r = do_compress(fi,fo,size);
300
    if (r == 0)
301
        printf("%s: algorithm NRV2E-99/10-singleblk, compressed %ld into %ld bytes\n",
302
                progname, total_in, total_out);
303
 
304
quit:
305
    if (fi) fclose(fi);
306
    if (fo) fclose(fo);
307
    return r;
308
}