]> mj.ucw.cz Git - libucw.git/blob - charset/fb-charconv.c
add Adler32 checksum to compressed V33 buckets
[libucw.git] / charset / fb-charconv.c
1 /*
2  *      Sherlock Library -- Charset Conversion Wrapper for Fast Buffered I/O
3  *
4  *      (c) 2003 Martin Mares <mj@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU Lesser General Public License.
8  */
9
10 #include "lib/lib.h"
11 #include "lib/fastbuf.h"
12 #include "charset/charconv.h"
13 #include "charset/fb-charconv.h"
14
15 #include <stdlib.h>
16
17 #define BUFSIZE 1024
18
19 struct fb_charconv {
20   struct fastbuf fb;
21   struct fastbuf *out;
22   struct conv_context ctxt;
23   byte buf[BUFSIZE];
24 };
25 #define FB_CC(f) ((struct fb_charconv *)(f)->is_fastbuf)
26
27 static void
28 fb_cc_spout(struct fastbuf *f)
29 {
30   struct conv_context *ct = &FB_CC(f)->ctxt;
31   int flags;
32
33   ct->source = f->buffer;
34   ct->source_end = f->bptr;
35   do
36     {
37       flags = conv_run(ct);
38       if (ct->dest > ct->dest_start)
39         bdirect_write_commit(FB_CC(f)->out, ct->dest);
40       uns l = bdirect_write_prepare(FB_CC(f)->out, &ct->dest_start);
41       ct->dest = ct->dest_start;
42       ct->dest_end = ct->dest + l;
43     }
44   while (!(flags & CONV_SOURCE_END));
45
46   f->bptr = f->buffer;
47 }
48
49 static void
50 fb_cc_close(struct fastbuf *f)
51 {
52   bclose(FB_CC(f)->out);
53   xfree(f);
54 }
55
56 struct fastbuf *
57 fb_wrap_charconv_out(struct fastbuf *f, int cs_from, int cs_to)
58 {
59   if (cs_from == cs_to)
60     return f;
61
62   struct fastbuf *g = xmalloc_zero(sizeof(struct fb_charconv));
63   FB_CC(g)->out = f;
64   conv_init(&FB_CC(g)->ctxt);
65   conv_set_charset(&FB_CC(g)->ctxt, cs_from, cs_to);
66   g->name = "<charconv-out>";
67   g->spout = fb_cc_spout;
68   g->close = fb_cc_close;
69   g->buffer = g->bstop = g->bptr = FB_CC(g)->buf;
70   g->bufend = g->buffer + BUFSIZE;
71   return g;
72 }