dccf087cfa0290826c549d467d2128eb95698065
[debian/amanda] / xfer-src / source-pattern.c
1 /*
2  * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3  * Copyright (c) 2008 Zmanda Inc.
4  *
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.
9  *
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.
14  *
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
18  */
19
20 #include "amxfer.h"
21 #include "amanda.h"
22 #include "simpleprng.h"
23
24 /*
25  * Class declaration
26  *
27  * This declaration is entirely private; nothing but xfer_source_pattern() references
28  * it directly.
29  */
30
31 GType xfer_source_pattern_get_type(void);
32 #define XFER_SOURCE_PATTERN_TYPE (xfer_source_pattern_get_type())
33 #define XFER_SOURCE_PATTERN(obj) G_TYPE_CHECK_INSTANCE_CAST((obj), xfer_source_pattern_get_type(), XferSourcePattern)
34 #define XFER_SOURCE_PATTERN_CONST(obj) G_TYPE_CHECK_INSTANCE_CAST((obj), xfer_source_pattern_get_type(), XferSourcePattern const)
35 #define XFER_SOURCE_PATTERN_CLASS(klass) G_TYPE_CHECK_CLASS_CAST((klass), xfer_source_pattern_get_type(), XferSourcePatternClass)
36 #define IS_XFER_SOURCE_PATTERN(obj) G_TYPE_CHECK_INSTANCE_TYPE((obj), xfer_source_pattern_get_type ())
37 #define XFER_SOURCE_PATTERN_GET_CLASS(obj) G_TYPE_INSTANCE_GET_CLASS((obj), xfer_source_pattern_get_type(), XferSourcePatternClass)
38
39 static GObjectClass *parent_class = NULL;
40
41 /*
42  * Main object structure
43  */
44
45 typedef struct XferSourcePattern {
46     XferElement __parent__;
47
48     gboolean limited_length;
49     guint64 length;
50     size_t pattern_buffer_length;
51     size_t current_offset;
52     char * pattern;
53 } XferSourcePattern;
54
55 /*
56  * Class definition
57  */
58
59 typedef struct {
60     XferElementClass __parent__;
61 } XferSourcePatternClass;
62
63 /*
64  * Implementation
65  */
66
67 static gpointer
68 pull_buffer_impl(
69     XferElement *elt,
70     size_t *size)
71 {
72     XferSourcePattern *self = (XferSourcePattern *)elt;
73     char *rval;
74     char *s, *d;
75     size_t l;
76     size_t offset;
77
78     /* indicate EOF on an cancel */
79     if (elt->cancelled || (self->limited_length && self->length == 0)) {
80         *size = 0;
81         return NULL;
82     }
83
84     if (self->limited_length) {
85         if (self->length == 0) {
86             *size = 0;
87             return NULL;
88         }
89
90         *size = MIN(10240, self->length);
91         self->length -= *size;
92     } else {
93         *size = 10240;
94     }
95
96     rval = malloc(*size);
97
98     /* fill the buffer "manually", instead of using fancy memcpy techniques, so
99      * that this runs at about the same speed as the random source */
100     l = *size;
101     s = self->pattern;
102     offset = self->current_offset;
103     d = rval;
104     while (l--) {
105         *(d++) = *(s + offset++);
106         if (offset > self->pattern_buffer_length) offset = 0;
107     }
108     self->current_offset = offset;
109
110     return rval;
111 }
112
113 static void
114 instance_init(
115     XferElement *elt)
116 {
117     elt->can_generate_eof = TRUE;
118 }
119
120 static void
121 class_init(
122     XferSourcePatternClass * selfc)
123 {
124     XferElementClass *klass = XFER_ELEMENT_CLASS(selfc);
125     static xfer_element_mech_pair_t mech_pairs[] = {
126         { XFER_MECH_NONE, XFER_MECH_PULL_BUFFER, 1, 0},
127         { XFER_MECH_NONE, XFER_MECH_NONE, 0, 0},
128     };
129
130     klass->pull_buffer = pull_buffer_impl;
131
132     klass->perl_class = "Amanda::Xfer::Source::Pattern";
133     klass->mech_pairs = mech_pairs;
134
135     parent_class = g_type_class_peek_parent(selfc);
136 }
137
138 GType
139 xfer_source_pattern_get_type (void)
140 {
141     static GType type = 0;
142
143     if G_UNLIKELY(type == 0) {
144         static const GTypeInfo info = {
145             sizeof (XferSourcePatternClass),
146             (GBaseInitFunc) NULL,
147             (GBaseFinalizeFunc) NULL,
148             (GClassInitFunc) class_init,
149             (GClassFinalizeFunc) NULL,
150             NULL /* class_data */,
151             sizeof (XferSourcePattern),
152             0 /* n_preallocs */,
153             (GInstanceInitFunc) instance_init,
154             NULL
155         };
156
157         type = g_type_register_static (XFER_ELEMENT_TYPE, "XferSourcePattern", &info, 0);
158     }
159
160     return type;
161 }
162
163 /* create an element of this class; prototype is in xfer-element.h */
164 XferElement * xfer_source_pattern(guint64 length, void * pattern,
165                                   size_t pattern_length) {
166     XferSourcePattern *xsp =
167         (XferSourcePattern *)g_object_new(XFER_SOURCE_PATTERN_TYPE, NULL);
168     XferElement *elt = XFER_ELEMENT(xsp);
169
170     xsp->length = length;
171     xsp->limited_length = (length > 0);
172     xsp->pattern = g_memdup(pattern, pattern_length);
173     xsp->pattern_buffer_length = pattern_length;
174     xsp->current_offset = 0;
175
176     return elt;
177 }