b7c8f4b6dcb97ac865ab94d97bb516b828c0abb4
[debian/amanda] / xfer-src / source-random.c
1 /*
2  * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3  * Copyright (c) 2008, 2009, 2010 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 #include "simpleprng.h"
25
26 /*
27  * Class declaration
28  *
29  * This declaration is entirely private; nothing but xfer_source_random() references
30  * it directly.
31  */
32
33 GType xfer_source_random_get_type(void);
34 #define XFER_SOURCE_RANDOM_TYPE (xfer_source_random_get_type())
35 #define XFER_SOURCE_RANDOM(obj) G_TYPE_CHECK_INSTANCE_CAST((obj), xfer_source_random_get_type(), XferSourceRandom)
36 #define XFER_SOURCE_RANDOM_CONST(obj) G_TYPE_CHECK_INSTANCE_CAST((obj), xfer_source_random_get_type(), XferSourceRandom const)
37 #define XFER_SOURCE_RANDOM_CLASS(klass) G_TYPE_CHECK_CLASS_CAST((klass), xfer_source_random_get_type(), XferSourceRandomClass)
38 #define IS_XFER_SOURCE_RANDOM(obj) G_TYPE_CHECK_INSTANCE_TYPE((obj), xfer_source_random_get_type ())
39 #define XFER_SOURCE_RANDOM_GET_CLASS(obj) G_TYPE_INSTANCE_GET_CLASS((obj), xfer_source_random_get_type(), XferSourceRandomClass)
40
41 static GObjectClass *parent_class = NULL;
42
43 /*
44  * Main object structure
45  */
46
47 typedef struct XferSourceRandom {
48     XferElement __parent__;
49
50     gboolean limited_length;
51     guint64 length;
52     simpleprng_state_t prng;
53 } XferSourceRandom;
54
55 /*
56  * Class definition
57  */
58
59 typedef struct {
60     XferElementClass __parent__;
61
62     guint32 (*get_seed)(XferSourceRandom *elt);
63 } XferSourceRandomClass;
64
65 /*
66  * Implementation
67  */
68
69 static guint32
70 get_seed_impl(
71     XferSourceRandom *self)
72 {
73     return simpleprng_get_seed(&self->prng);
74 }
75
76 static gpointer
77 pull_buffer_impl(
78     XferElement *elt,
79     size_t *size)
80 {
81     XferSourceRandom *self = (XferSourceRandom *)elt;
82     char *buf;
83
84     if (elt->cancelled || (self->limited_length && self->length == 0)) {
85         *size = 0;
86         return NULL;
87     }
88
89     if (self->limited_length) {
90         *size = MIN(10240, self->length);
91         self->length -= *size;
92     } else {
93         *size = 10240;
94     }
95
96     buf = g_malloc(*size);
97     simpleprng_fill_buffer(&self->prng, buf, *size);
98
99     return buf;
100 }
101
102 static void
103 instance_init(
104     XferElement *elt)
105 {
106     elt->can_generate_eof = TRUE;
107 }
108
109 static void
110 class_init(
111     XferSourceRandomClass * selfc)
112 {
113     XferElementClass *klass = XFER_ELEMENT_CLASS(selfc);
114     static xfer_element_mech_pair_t mech_pairs[] = {
115         { XFER_MECH_NONE, XFER_MECH_PULL_BUFFER, XFER_NROPS(1), XFER_NTHREADS(0) },
116         { XFER_MECH_NONE, XFER_MECH_NONE, XFER_NROPS(0), XFER_NTHREADS(0) },
117     };
118
119     selfc->get_seed = get_seed_impl;
120     klass->pull_buffer = pull_buffer_impl;
121
122     klass->perl_class = "Amanda::Xfer::Source::Random";
123     klass->mech_pairs = mech_pairs;
124
125     parent_class = g_type_class_peek_parent(selfc);
126 }
127
128 GType
129 xfer_source_random_get_type (void)
130 {
131     static GType type = 0;
132
133     if G_UNLIKELY(type == 0) {
134         static const GTypeInfo info = {
135             sizeof (XferSourceRandomClass),
136             (GBaseInitFunc) NULL,
137             (GBaseFinalizeFunc) NULL,
138             (GClassInitFunc) class_init,
139             (GClassFinalizeFunc) NULL,
140             NULL /* class_data */,
141             sizeof (XferSourceRandom),
142             0 /* n_preallocs */,
143             (GInstanceInitFunc) instance_init,
144             NULL
145         };
146
147         type = g_type_register_static (XFER_ELEMENT_TYPE, "XferSourceRandom", &info, 0);
148     }
149
150     return type;
151 }
152
153 guint32
154 xfer_source_random_get_seed(
155     XferElement *elt)
156 {
157     XferSourceRandomClass *klass;
158     g_assert(IS_XFER_SOURCE_RANDOM(elt));
159
160     klass = XFER_SOURCE_RANDOM_GET_CLASS(elt);
161     return klass->get_seed(XFER_SOURCE_RANDOM(elt));
162 }
163
164 /* create an element of this class; prototype is in xfer-element.h */
165 XferElement *
166 xfer_source_random(
167     guint64 length,
168     guint32 prng_seed)
169 {
170     XferSourceRandom *xsr = (XferSourceRandom *)g_object_new(XFER_SOURCE_RANDOM_TYPE, NULL);
171     XferElement *elt = XFER_ELEMENT(xsr);
172
173     xsr->length = length;
174     xsr->limited_length = (length != 0);
175     simpleprng_seed(&xsr->prng, prng_seed);
176
177     return elt;
178 }