1 /* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * gsf-blob.c: a chunk of data
4  *
5  * Copyright (C) 2006 Novell Inc
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of version 2.1 of the GNU Lesser General Public
9  * License as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
19  * USA
20  */
21 
22 #include <gsf-config.h>
23 #include <gsf/gsf-blob.h>
24 #include <gsf/gsf.h>
25 
26 #include <glib/gi18n-lib.h>
27 #include <string.h>
28 
29 struct _GsfBlobClass {
30 	GObjectClass parent_class;
31 };
32 
33 /* Private part of the GsfBlob structure */
34 struct _GsfBlobPrivate {
35 	gsize size;
36 	gpointer data;
37 };
38 
39 static GObjectClass *gsf_blob_parent_class;
40 static void
gsf_blob_finalize(GObject * object)41 gsf_blob_finalize (GObject *object)
42 {
43 	GsfBlob *blob;
44 	GsfBlobPrivate *priv;
45 
46 	blob = GSF_BLOB (object);
47 	priv = blob->priv;
48 
49 	g_free (priv->data);
50 	g_free (priv);
51 
52 	gsf_blob_parent_class->finalize (object);
53 }
54 
55 static void
gsf_blob_class_init(GObjectClass * gobject_class)56 gsf_blob_class_init (GObjectClass *gobject_class)
57 {
58 	gobject_class->finalize = gsf_blob_finalize;
59 
60 	gsf_blob_parent_class = g_type_class_peek_parent (gobject_class);
61 }
62 
63 static void
gsf_blob_init(GsfBlob * blob)64 gsf_blob_init (GsfBlob *blob)
65 {
66 	GsfBlobPrivate *priv;
67 
68 	priv = g_new0 (GsfBlobPrivate, 1);
69 	blob->priv = priv;
70 }
71 
GSF_CLASS(GsfBlob,gsf_blob,gsf_blob_class_init,gsf_blob_init,G_TYPE_OBJECT)72 GSF_CLASS (GsfBlob, gsf_blob,
73 	   gsf_blob_class_init, gsf_blob_init,
74 	   G_TYPE_OBJECT)
75 
76 
77 /**
78  * gsf_blob_new:
79  * @size: Size of the data in bytes.
80  * @data_to_copy: (in) (array length=size) (element-type guint8):
81  * Data which will be copied into the blob, or %NULL if @size is zero.
82  * @error: location to store error, or %NULL.
83  *
84  * Creates a new #GsfBlob object to hold the specified data.  The blob can then
85  * be used as a facility for reference-counting for the data.  The data is
86  * copied internally, so the blob does not hold references to external chunks
87  * of memory.
88  *
89  * Return value: A newly-created #GsfBlob, or %NULL if the data could not be copied.
90  *
91  * Error domain: #GSF_ERROR
92  *
93  * Possible errors: #GSF_ERROR_OUT_OF_MEMORY if the @data_to_copy could not be copied.
94  **/
95 GsfBlob *
96 gsf_blob_new (gsize size, gconstpointer data_to_copy, GError **error)
97 {
98 	GsfBlob *blob;
99 	GsfBlobPrivate *priv;
100 	gpointer data;
101 
102 	g_return_val_if_fail ((size > 0 && data_to_copy != NULL) || (size == 0 && data_to_copy == NULL), NULL);
103 	g_return_val_if_fail (error == NULL || *error == NULL, NULL);
104 
105 	if (data_to_copy) {
106 		data = g_try_malloc (size);
107 		if (!data) {
108 			gchar *size_str;
109 
110 			size_str = g_strdup_printf ("%" G_GSIZE_FORMAT, size);
111 			g_set_error (error,
112 				     GSF_ERROR,
113 				     GSF_ERROR_OUT_OF_MEMORY,
114 				     _("Not enough memory to copy %s bytes of data"),
115 				     size_str);
116 			g_free (size_str);
117 			return NULL;
118 		}
119 
120 		memcpy (data, data_to_copy, size);
121 	} else
122 		data = NULL;
123 
124 	blob = g_object_new (GSF_TYPE_BLOB, NULL);
125 
126 	priv = blob->priv;
127 
128 	priv->size = size;
129 	priv->data = data;
130 
131 	return blob;
132 }
133 
134 /**
135  * gsf_blob_get_size:
136  * @blob: A #GsfBlob.
137  *
138  * Queries the size in bytes of the data stored in the blob.
139  *
140  * Return value: Size in bytes, or 0 if the data is %NULL.
141  **/
142 gsize
gsf_blob_get_size(GsfBlob const * blob)143 gsf_blob_get_size (GsfBlob const *blob)
144 {
145 	GsfBlobPrivate *priv;
146 
147 	g_return_val_if_fail (GSF_IS_BLOB (blob), 0);
148 
149 	priv = blob->priv;
150 	return priv->size;
151 }
152 
153 /**
154  * gsf_blob_peek_data:
155  * @blob: A #GsfBlob.
156  *
157  * Queries a pointer to the data stored in the blob.  This does not
158  * copy the data for you; it returns a pointer to the actual buffer
159  * which the blob uses internally, so you should not free this buffer
160  * on your own.
161  *
162  * Return value: (transfer none): Pointer to the data stored in the
163  * blob, or %NULL if the size of the data is zero.
164  **/
165 gconstpointer
gsf_blob_peek_data(GsfBlob const * blob)166 gsf_blob_peek_data (GsfBlob const *blob)
167 {
168 	GsfBlobPrivate *priv;
169 
170 	g_return_val_if_fail (GSF_IS_BLOB (blob), NULL);
171 
172 	priv = blob->priv;
173 	return priv->data;
174 }
175