2 * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3 * Copyright (c) 2008 Zmanda Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 * This declaration is entirely private; nothing but xfer_filter_xor() references
30 GType xfer_filter_xor_get_type(void);
31 #define XFER_FILTER_XOR_TYPE (xfer_filter_xor_get_type())
32 #define XFER_FILTER_XOR(obj) G_TYPE_CHECK_INSTANCE_CAST((obj), xfer_filter_xor_get_type(), XferFilterXor)
33 #define XFER_FILTER_XOR_CONST(obj) G_TYPE_CHECK_INSTANCE_CAST((obj), xfer_filter_xor_get_type(), XferFilterXor const)
34 #define XFER_FILTER_XOR_CLASS(klass) G_TYPE_CHECK_CLASS_CAST((klass), xfer_filter_xor_get_type(), XferFilterXorClass)
35 #define IS_XFER_FILTER_XOR(obj) G_TYPE_CHECK_INSTANCE_TYPE((obj), xfer_filter_xor_get_type ())
36 #define XFER_FILTER_XOR_GET_CLASS(obj) G_TYPE_INSTANCE_GET_CLASS((obj), xfer_filter_xor_get_type(), XferFilterXorClass)
38 static GObjectClass *parent_class = NULL;
41 * Main object structure
44 typedef struct XferFilterXor {
45 XferElement __parent__;
55 XferElementClass __parent__;
71 /* Apply XOR. This is a pretty sophisticated encryption algorithm! */
72 for (i = 0; i < len; i++) {
86 XferFilterXor *self = (XferFilterXor *)elt;
90 /* drain our upstream only if we're expecting an EOF */
91 if (elt->expect_eof) {
92 xfer_element_drain_by_pulling(XFER_ELEMENT(self)->upstream);
100 /* get a buffer from upstream, xor it, and hand it back */
101 buf = xfer_element_pull_buffer(XFER_ELEMENT(self)->upstream, size);
103 apply_xor(buf, *size, self->xor_key);
113 XferFilterXor *self = (XferFilterXor *)elt;
115 /* drop the buffer if we've been cancelled */
116 if (elt->cancelled) {
121 /* xor the given buffer and pass it downstream */
123 apply_xor(buf, len, self->xor_key);
125 xfer_element_push_buffer(XFER_ELEMENT(self)->downstream, buf, len);
132 elt->can_generate_eof = TRUE;
137 XferFilterXorClass * selfc)
139 XferElementClass *klass = XFER_ELEMENT_CLASS(selfc);
140 static xfer_element_mech_pair_t mech_pairs[] = {
141 { XFER_MECH_PULL_BUFFER, XFER_MECH_PULL_BUFFER, 1, 0},
142 { XFER_MECH_PUSH_BUFFER, XFER_MECH_PUSH_BUFFER, 1, 0},
143 { XFER_MECH_NONE, XFER_MECH_NONE, 0, 0},
146 klass->push_buffer = push_buffer_impl;
147 klass->pull_buffer = pull_buffer_impl;
149 klass->perl_class = "Amanda::Xfer::Filter::Xor";
150 klass->mech_pairs = mech_pairs;
152 parent_class = g_type_class_peek_parent(selfc);
156 xfer_filter_xor_get_type (void)
158 static GType type = 0;
160 if G_UNLIKELY(type == 0) {
161 static const GTypeInfo info = {
162 sizeof (XferFilterXorClass),
163 (GBaseInitFunc) NULL,
164 (GBaseFinalizeFunc) NULL,
165 (GClassInitFunc) class_init,
166 (GClassFinalizeFunc) NULL,
167 NULL /* class_data */,
168 sizeof (XferFilterXor),
170 (GInstanceInitFunc) instance_init,
174 type = g_type_register_static (XFER_ELEMENT_TYPE, "XferFilterXor", &info, 0);
180 /* create an element of this class; prototype is in xfer-element.h */
183 unsigned char xor_key)
185 XferFilterXor *xfx = (XferFilterXor *)g_object_new(XFER_FILTER_XOR_TYPE, NULL);
186 XferElement *elt = XFER_ELEMENT(xfx);
188 xfx->xor_key = xor_key;