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
22 #include "simpleprng.h"
27 * This declaration is entirely private; nothing but xfer_source_pattern() references
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)
39 static GObjectClass *parent_class = NULL;
42 * Main object structure
45 typedef struct XferSourcePattern {
46 XferElement __parent__;
48 gboolean limited_length;
50 size_t pattern_buffer_length;
51 size_t current_offset;
60 XferElementClass __parent__;
61 } XferSourcePatternClass;
72 XferSourcePattern *self = (XferSourcePattern *)elt;
78 /* indicate EOF on an cancel */
79 if (elt->cancelled || (self->limited_length && self->length == 0)) {
84 if (self->limited_length) {
85 if (self->length == 0) {
90 *size = MIN(10240, self->length);
91 self->length -= *size;
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 */
102 offset = self->current_offset;
105 *(d++) = *(s + offset++);
106 if (offset > self->pattern_buffer_length) offset = 0;
108 self->current_offset = offset;
117 elt->can_generate_eof = TRUE;
122 XferSourcePatternClass * selfc)
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},
130 klass->pull_buffer = pull_buffer_impl;
132 klass->perl_class = "Amanda::Xfer::Source::Pattern";
133 klass->mech_pairs = mech_pairs;
135 parent_class = g_type_class_peek_parent(selfc);
139 xfer_source_pattern_get_type (void)
141 static GType type = 0;
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),
153 (GInstanceInitFunc) instance_init,
157 type = g_type_register_static (XFER_ELEMENT_TYPE, "XferSourcePattern", &info, 0);
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);
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;