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