2 * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3 * Copyright (c) 2008-2012 Zmanda, Inc. All Rights Reserved.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 * Contact information: Zmanda Inc., 465 S. Mathilda Ave., Suite 300
20 * Sunnyvale, CA 94085, USA, or: http://www.zmanda.com
25 #include "simpleprng.h"
30 * This declaration is entirely private; nothing but xfer_source_pattern() references
34 GType xfer_source_pattern_get_type(void);
35 #define XFER_SOURCE_PATTERN_TYPE (xfer_source_pattern_get_type())
36 #define XFER_SOURCE_PATTERN(obj) G_TYPE_CHECK_INSTANCE_CAST((obj), xfer_source_pattern_get_type(), XferSourcePattern)
37 #define XFER_SOURCE_PATTERN_CONST(obj) G_TYPE_CHECK_INSTANCE_CAST((obj), xfer_source_pattern_get_type(), XferSourcePattern const)
38 #define XFER_SOURCE_PATTERN_CLASS(klass) G_TYPE_CHECK_CLASS_CAST((klass), xfer_source_pattern_get_type(), XferSourcePatternClass)
39 #define IS_XFER_SOURCE_PATTERN(obj) G_TYPE_CHECK_INSTANCE_TYPE((obj), xfer_source_pattern_get_type ())
40 #define XFER_SOURCE_PATTERN_GET_CLASS(obj) G_TYPE_INSTANCE_GET_CLASS((obj), xfer_source_pattern_get_type(), XferSourcePatternClass)
42 static GObjectClass *parent_class = NULL;
45 * Main object structure
48 typedef struct XferSourcePattern {
49 XferElement __parent__;
51 gboolean limited_length;
53 size_t pattern_buffer_length;
54 size_t current_offset;
63 XferElementClass __parent__;
64 } XferSourcePatternClass;
71 * Utility function to fill a buffer buf of size len with the pattern defined
72 * for this instance. We start each copying from the offset where we left the
75 * Note that performance is important: amtapetype uses this very source and
76 * source-random.c to determine whether a tape device uses compression, and
77 * expects the two to run about the same speed. This is why this is a byte
78 * loop and does not use mempcy() and friends: the random source is also byte
82 static void fill_buffer_with_pattern(XferSourcePattern *self, char *buf,
85 char *src = self->pattern, *dst = buf;
86 size_t plen = self->pattern_buffer_length, offset = self->current_offset;
95 if (G_LIKELY(offset < plen))
102 self->current_offset = offset;
110 XferSourcePattern *self = (XferSourcePattern *)elt;
113 /* indicate EOF on an cancel */
114 if (elt->cancelled) {
119 if (self->limited_length) {
120 if (self->length == 0) {
125 *size = MIN(10240, self->length);
126 self->length -= *size;
131 rval = malloc(*size);
133 fill_buffer_with_pattern(self, rval, *size);
142 elt->can_generate_eof = TRUE;
147 XferSourcePatternClass * selfc)
149 XferElementClass *klass = XFER_ELEMENT_CLASS(selfc);
150 static xfer_element_mech_pair_t mech_pairs[] = {
151 { XFER_MECH_NONE, XFER_MECH_PULL_BUFFER, XFER_NROPS(1), XFER_NTHREADS(0) },
152 { XFER_MECH_NONE, XFER_MECH_NONE, XFER_NROPS(0), XFER_NTHREADS(0) },
155 klass->pull_buffer = pull_buffer_impl;
157 klass->perl_class = "Amanda::Xfer::Source::Pattern";
158 klass->mech_pairs = mech_pairs;
160 parent_class = g_type_class_peek_parent(selfc);
164 xfer_source_pattern_get_type (void)
166 static GType type = 0;
168 if G_UNLIKELY(type == 0) {
169 static const GTypeInfo info = {
170 sizeof (XferSourcePatternClass),
171 (GBaseInitFunc) NULL,
172 (GBaseFinalizeFunc) NULL,
173 (GClassInitFunc) class_init,
174 (GClassFinalizeFunc) NULL,
175 NULL /* class_data */,
176 sizeof (XferSourcePattern),
178 (GInstanceInitFunc) instance_init,
182 type = g_type_register_static (XFER_ELEMENT_TYPE, "XferSourcePattern", &info, 0);
188 /* create an element of this class; prototype is in xfer-element.h */
189 XferElement * xfer_source_pattern(guint64 length, void * pattern,
190 size_t pattern_length) {
191 XferSourcePattern *xsp =
192 (XferSourcePattern *)g_object_new(XFER_SOURCE_PATTERN_TYPE, NULL);
193 XferElement *elt = XFER_ELEMENT(xsp);
195 xsp->length = length;
196 xsp->limited_length = (length > 0);
197 xsp->pattern = g_memdup(pattern, pattern_length);
198 xsp->pattern_buffer_length = pattern_length;
199 xsp->current_offset = 0;