diff options
Diffstat (limited to 'xdelta1/libedsio/simple.c')
-rwxr-xr-x | xdelta1/libedsio/simple.c | 179 |
1 files changed, 179 insertions, 0 deletions
diff --git a/xdelta1/libedsio/simple.c b/xdelta1/libedsio/simple.c new file mode 100755 index 0000000..0d327f5 --- /dev/null +++ b/xdelta1/libedsio/simple.c | |||
@@ -0,0 +1,179 @@ | |||
1 | /* -*-Mode: C;-*- | ||
2 | * $Id: simple.c 1.6 Wed, 31 Mar 1999 17:39:58 -0800 jmacd $ | ||
3 | * | ||
4 | * Copyright (C) 1998, 1999, Josh MacDonald. | ||
5 | * All Rights Reserved. | ||
6 | * | ||
7 | * Author: Josh MacDonald <jmacd@CS.Berkeley.EDU> | ||
8 | */ | ||
9 | |||
10 | #include "edsio.h" | ||
11 | |||
12 | /* Simple Source | ||
13 | */ | ||
14 | |||
15 | typedef struct _ByteArraySource ByteArraySource; | ||
16 | |||
17 | struct _ByteArraySource { | ||
18 | SerialSource source; | ||
19 | |||
20 | const guint8* in_data; | ||
21 | guint in_len; | ||
22 | |||
23 | guint read_pos; | ||
24 | guint flags; | ||
25 | }; | ||
26 | |||
27 | static gboolean byte_array_source_close (SerialSource* source); | ||
28 | static gboolean byte_array_source_read (SerialSource* source, guint8 *ptr, guint32 len); | ||
29 | static void byte_array_source_free (SerialSource* source); | ||
30 | |||
31 | SerialSource* | ||
32 | edsio_simple_source (const guint8* data, guint len, guint flags) | ||
33 | { | ||
34 | ByteArraySource* it = g_new0 (ByteArraySource, 1); | ||
35 | SerialSource* src = (SerialSource*) it; | ||
36 | |||
37 | serializeio_source_init (& it->source, | ||
38 | NULL, | ||
39 | byte_array_source_close, | ||
40 | byte_array_source_read, | ||
41 | byte_array_source_free, | ||
42 | NULL, | ||
43 | NULL); | ||
44 | |||
45 | it->in_data = data; | ||
46 | it->in_len = len; | ||
47 | it->flags = flags; | ||
48 | |||
49 | if (flags & SBF_Base64) | ||
50 | src = serializeio_base64_source (src); | ||
51 | |||
52 | if (flags & SBF_Checksum) | ||
53 | src = serializeio_checksum_source (src); | ||
54 | |||
55 | if (flags & SBF_Compress) | ||
56 | src = serializeio_gzip_source (src); | ||
57 | |||
58 | return src; | ||
59 | } | ||
60 | |||
61 | gboolean | ||
62 | byte_array_source_close (SerialSource* source) | ||
63 | { | ||
64 | return TRUE; | ||
65 | } | ||
66 | |||
67 | gboolean | ||
68 | byte_array_source_read (SerialSource* source, guint8 *buf, guint32 len) | ||
69 | { | ||
70 | ByteArraySource* ssource = (ByteArraySource*) source; | ||
71 | |||
72 | if (len + ssource->read_pos > ssource->in_len) | ||
73 | { | ||
74 | edsio_generate_source_event (EC_EdsioSourceEof, source); | ||
75 | return FALSE; | ||
76 | } | ||
77 | |||
78 | memcpy (buf, ssource->in_data + ssource->read_pos, len); | ||
79 | |||
80 | ssource->read_pos += len; | ||
81 | |||
82 | return TRUE; | ||
83 | } | ||
84 | |||
85 | void | ||
86 | byte_array_source_free (SerialSource* source) | ||
87 | { | ||
88 | g_free (source); | ||
89 | } | ||
90 | |||
91 | /* BASE64 Sink | ||
92 | */ | ||
93 | |||
94 | typedef struct _ByteArraySink ByteArraySink; | ||
95 | |||
96 | struct _ByteArraySink { | ||
97 | SerialSink sink; | ||
98 | |||
99 | GByteArray* out; | ||
100 | |||
101 | gpointer data; | ||
102 | guint flags; | ||
103 | gboolean free_result; | ||
104 | void (* success) (gpointer data, GByteArray* result); | ||
105 | }; | ||
106 | |||
107 | static gboolean byte_array_sink_close (SerialSink* sink); | ||
108 | static gboolean byte_array_sink_write (SerialSink* sink, const guint8 *ptr, guint32 len); | ||
109 | static void byte_array_sink_free (SerialSink* sink); | ||
110 | |||
111 | SerialSink* | ||
112 | edsio_simple_sink (gpointer data, | ||
113 | guint flags, | ||
114 | gboolean free_result, | ||
115 | void (* success) (gpointer data, GByteArray* result), | ||
116 | GByteArray **result) | ||
117 | { | ||
118 | ByteArraySink* it = g_new0 (ByteArraySink, 1); | ||
119 | SerialSink* sink = (SerialSink*) it; | ||
120 | |||
121 | serializeio_sink_init (&it->sink, | ||
122 | NULL, | ||
123 | byte_array_sink_close, | ||
124 | byte_array_sink_write, | ||
125 | byte_array_sink_free, | ||
126 | NULL); | ||
127 | |||
128 | it->data = data; | ||
129 | it->out = g_byte_array_new (); | ||
130 | it->flags = flags; | ||
131 | it->free_result = free_result; | ||
132 | it->success = success; | ||
133 | |||
134 | if (result) | ||
135 | (*result) = it->out; | ||
136 | |||
137 | if (flags & SBF_Base64) | ||
138 | sink = serializeio_base64_sink (sink); | ||
139 | |||
140 | if (flags & SBF_Checksum) | ||
141 | sink = serializeio_checksum_sink (sink); | ||
142 | |||
143 | if (flags & SBF_Compress) | ||
144 | sink = serializeio_gzip_sink (sink); | ||
145 | |||
146 | return sink; | ||
147 | } | ||
148 | |||
149 | gboolean | ||
150 | byte_array_sink_close (SerialSink* sink) | ||
151 | { | ||
152 | ByteArraySink* ssink = (ByteArraySink*) sink; | ||
153 | |||
154 | if (ssink->success) | ||
155 | ssink->success (ssink->data, ssink->out); | ||
156 | |||
157 | return TRUE; | ||
158 | } | ||
159 | |||
160 | gboolean | ||
161 | byte_array_sink_write (SerialSink* sink, const guint8 *ptr, guint32 len) | ||
162 | { | ||
163 | ByteArraySink* ssink = (ByteArraySink*) sink; | ||
164 | |||
165 | g_byte_array_append (ssink->out, ptr, len); | ||
166 | |||
167 | return TRUE; | ||
168 | } | ||
169 | |||
170 | void | ||
171 | byte_array_sink_free (SerialSink* sink) | ||
172 | { | ||
173 | ByteArraySink* ssink = (ByteArraySink*) sink; | ||
174 | |||
175 | if (ssink->out && ssink->free_result) | ||
176 | g_byte_array_free (ssink->out, TRUE); | ||
177 | |||
178 | g_free (sink); | ||
179 | } | ||