Imported Upstream version 3.2.0
[debian/amanda] / xfer-src / filter-xor.c
1 /*
2  * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3  * Copyright (c) 2008,2009 Zmanda, Inc.  All Rights Reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 as published
7  * by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17  *
18  * Contact information: Zmanda Inc., 465 S. Mathilda Ave., Suite 300
19  * Sunnyvale, CA 94085, USA, or: http://www.zmanda.com
20  */
21
22 #include "amanda.h"
23 #include "amxfer.h"
24
25 /*
26  * Class declaration
27  *
28  * This declaration is entirely private; nothing but xfer_filter_xor() references
29  * it directly.
30  */
31
32 GType xfer_filter_xor_get_type(void);
33 #define XFER_FILTER_XOR_TYPE (xfer_filter_xor_get_type())
34 #define XFER_FILTER_XOR(obj) G_TYPE_CHECK_INSTANCE_CAST((obj), xfer_filter_xor_get_type(), XferFilterXor)
35 #define XFER_FILTER_XOR_CONST(obj) G_TYPE_CHECK_INSTANCE_CAST((obj), xfer_filter_xor_get_type(), XferFilterXor const)
36 #define XFER_FILTER_XOR_CLASS(klass) G_TYPE_CHECK_CLASS_CAST((klass), xfer_filter_xor_get_type(), XferFilterXorClass)
37 #define IS_XFER_FILTER_XOR(obj) G_TYPE_CHECK_INSTANCE_TYPE((obj), xfer_filter_xor_get_type ())
38 #define XFER_FILTER_XOR_GET_CLASS(obj) G_TYPE_INSTANCE_GET_CLASS((obj), xfer_filter_xor_get_type(), XferFilterXorClass)
39
40 static GObjectClass *parent_class = NULL;
41
42 /*
43  * Main object structure
44  */
45
46 typedef struct XferFilterXor {
47     XferElement __parent__;
48
49     char xor_key;
50 } XferFilterXor;
51
52 /*
53  * Class definition
54  */
55
56 typedef struct {
57     XferElementClass __parent__;
58 } XferFilterXorClass;
59
60
61 /*
62  * Utilities
63  */
64
65 static void
66 apply_xor(
67     char *buf,
68     size_t len,
69     char xor_key)
70 {
71     size_t i;
72
73     /* Apply XOR.  This is a pretty sophisticated encryption algorithm! */
74     for (i = 0; i < len; i++) {
75         buf[i] ^= xor_key;
76     }
77 }
78
79 /*
80  * Implementation
81  */
82
83 static gpointer
84 pull_buffer_impl(
85     XferElement *elt,
86     size_t *size)
87 {
88     XferFilterXor *self = (XferFilterXor *)elt;
89     char *buf;
90
91     if (elt->cancelled) {
92         /* drain our upstream only if we're expecting an EOF */
93         if (elt->expect_eof) {
94             xfer_element_drain_by_pulling(XFER_ELEMENT(self)->upstream);
95         }
96
97         /* return an EOF */
98         *size = 0;
99         return NULL;
100     }
101
102     /* get a buffer from upstream, xor it, and hand it back */
103     buf = xfer_element_pull_buffer(XFER_ELEMENT(self)->upstream, size);
104     if (buf)
105         apply_xor(buf, *size, self->xor_key);
106     return buf;
107 }
108
109 static void
110 push_buffer_impl(
111     XferElement *elt,
112     gpointer buf,
113     size_t len)
114 {
115     XferFilterXor *self = (XferFilterXor *)elt;
116
117     /* drop the buffer if we've been cancelled */
118     if (elt->cancelled) {
119         amfree(buf);
120         return;
121     }
122
123     /* xor the given buffer and pass it downstream */
124     if (buf)
125         apply_xor(buf, len, self->xor_key);
126
127     xfer_element_push_buffer(XFER_ELEMENT(self)->downstream, buf, len);
128 }
129
130 static void
131 instance_init(
132     XferElement *elt)
133 {
134     elt->can_generate_eof = TRUE;
135 }
136
137 static void
138 class_init(
139     XferFilterXorClass * selfc)
140 {
141     XferElementClass *klass = XFER_ELEMENT_CLASS(selfc);
142     static xfer_element_mech_pair_t mech_pairs[] = {
143         { XFER_MECH_PULL_BUFFER, XFER_MECH_PULL_BUFFER, 1, 0},
144         { XFER_MECH_PUSH_BUFFER, XFER_MECH_PUSH_BUFFER, 1, 0},
145         { XFER_MECH_NONE, XFER_MECH_NONE, 0, 0},
146     };
147
148     klass->push_buffer = push_buffer_impl;
149     klass->pull_buffer = pull_buffer_impl;
150
151     klass->perl_class = "Amanda::Xfer::Filter::Xor";
152     klass->mech_pairs = mech_pairs;
153
154     parent_class = g_type_class_peek_parent(selfc);
155 }
156
157 GType
158 xfer_filter_xor_get_type (void)
159 {
160     static GType type = 0;
161
162     if G_UNLIKELY(type == 0) {
163         static const GTypeInfo info = {
164             sizeof (XferFilterXorClass),
165             (GBaseInitFunc) NULL,
166             (GBaseFinalizeFunc) NULL,
167             (GClassInitFunc) class_init,
168             (GClassFinalizeFunc) NULL,
169             NULL /* class_data */,
170             sizeof (XferFilterXor),
171             0 /* n_preallocs */,
172             (GInstanceInitFunc) instance_init,
173             NULL
174         };
175
176         type = g_type_register_static (XFER_ELEMENT_TYPE, "XferFilterXor", &info, 0);
177     }
178
179     return type;
180 }
181
182 /* create an element of this class; prototype is in xfer-element.h */
183 XferElement *
184 xfer_filter_xor(
185     unsigned char xor_key)
186 {
187     XferFilterXor *xfx = (XferFilterXor *)g_object_new(XFER_FILTER_XOR_TYPE, NULL);
188     XferElement *elt = XFER_ELEMENT(xfx);
189
190     xfx->xor_key = xor_key;
191
192     return elt;
193 }