GList * cockpit_template_expand (GBytes *input, CockpitTemplateFunc func, gpointer user_data) { GList *output = NULL; const gchar *data; const gchar *end; const gchar *before; const gchar *after; GBytes *bytes; gchar *name; g_return_val_if_fail (func != NULL, NULL); data = g_bytes_get_data (input, NULL); end = data + g_bytes_get_size (input); for (;;) { name = find_variable (data, end, &before, &after); if (name == NULL) break; if (before != data) { g_assert (before > data); bytes = g_bytes_new_with_free_func (data, before - data, (GDestroyNotify)g_bytes_unref, g_bytes_ref (input)); output = g_list_prepend (output, bytes); } bytes = (func) (name, user_data); g_free (name); if (!bytes) { g_assert (after > before); bytes = g_bytes_new_with_free_func (before, after - before, (GDestroyNotify)g_bytes_unref, g_bytes_ref (input)); } output = g_list_prepend (output, bytes); g_assert (after <= end); data = after; } if (data != end) { g_assert (end > data); bytes = g_bytes_new_with_free_func (data, end - data, (GDestroyNotify)g_bytes_unref, g_bytes_ref (input)); output = g_list_prepend (output, bytes); } return g_list_reverse (output); }
/** * cockpit_stream_write: * @self: the pipe * @data: the data to write * * Write @data to the pipe. This is not done immediately, it's * queued and written when the pipe is ready. * * If you cockpit_stream_close() with a @problem, then queued data * will be discarded. * * Calling this function on a closed or closing pipe (one on which * cockpit_stream_close() has been called) is invalid. * * Zero length data blocks are ignored, it doesn't makes sense to * write zero bytes to a pipe. */ void cockpit_stream_write (CockpitStream *self, GBytes *data) { g_return_if_fail (COCKPIT_IS_STREAM (self)); g_return_if_fail (!self->priv->closing); g_return_if_fail (!self->priv->closed); if (g_bytes_get_size (data) == 0) { g_debug ("%s: ignoring zero byte data block", self->priv->name); return; } g_queue_push_tail (self->priv->out_queue, g_bytes_ref (data)); if (!self->priv->out_source && !self->priv->out_closed) { start_output (self); } /* * If this becomes thread-safe, then something like this is needed: * g_main_context_wakeup (g_source_get_context (self->priv->source)); */ }
static gboolean gkm_xdg_trust_real_load (GkmSerializable *base, GkmSecret *login, GBytes *data) { GkmXdgTrust *self = GKM_XDG_TRUST (base); GNode *asn = NULL; if (g_bytes_get_size (data) == 0) return FALSE; asn = egg_asn1x_create (xdg_asn1_tab, "trust-1"); g_return_val_if_fail (asn, FALSE); if (!egg_asn1x_decode (asn, data)) { g_warning ("couldn't parse trust data: %s", egg_asn1x_message (asn)); egg_asn1x_destroy (asn); return FALSE; } /* Next parse out all the pairs */ if (!load_assertions (self, asn)) { egg_asn1x_destroy (asn); return FALSE; } /* Take ownership of this new data */ if (self->pv->bytes) g_bytes_unref (self->pv->bytes); self->pv->bytes = g_bytes_ref (data); egg_asn1x_destroy (self->pv->asn); self->pv->asn = asn; return TRUE; }
static gboolean on_transport_recv (CockpitTransport *transport, const gchar *channel_id, GBytes *data, gpointer user_data) { CockpitChannel *self = user_data; CockpitChannelClass *klass; if (g_strcmp0 (channel_id, self->priv->id) != 0) return FALSE; if (self->priv->ready) { klass = COCKPIT_CHANNEL_GET_CLASS (self); g_assert (klass->recv); (klass->recv) (self, data); } else { if (!self->priv->received) self->priv->received = g_queue_new (); g_queue_push_tail (self->priv->received, g_bytes_ref (data)); } return TRUE; }
static void cockpit_http_stream_recv (CockpitChannel *channel, GBytes *message) { CockpitHttpStream *self = (CockpitHttpStream *)channel; self->request = g_list_prepend (self->request, g_bytes_ref (message)); }
/** * g_variant_new_from_bytes: * @type: a #GVariantType * @bytes: a #GBytes * @trusted: if the contents of @bytes are trusted * * Constructs a new serialised-mode #GVariant instance. This is the * inner interface for creation of new serialised values that gets * called from various functions in gvariant.c. * * A reference is taken on @bytes. * * Returns: a new #GVariant with a floating reference * * Since: 2.36 */ GVariant * g_variant_new_from_bytes (const GVariantType *type, GBytes *bytes, gboolean trusted) { GVariant *value; guint alignment; gsize size; value = g_variant_alloc (type, TRUE, trusted); value->contents.serialised.bytes = g_bytes_ref (bytes); g_variant_type_info_query (value->type_info, &alignment, &size); if (size && g_bytes_get_size (bytes) != size) { /* Creating a fixed-sized GVariant with a bytes of the wrong * size. * * We should do the equivalent of pulling a fixed-sized child out * of a brozen container (ie: data is NULL size is equal to the correct * fixed size). */ value->contents.serialised.data = NULL; value->size = size; } else { value->contents.serialised.data = g_bytes_get_data (bytes, &value->size); } return value; }
void _ostree_static_delta_part_execute_async (OstreeRepo *repo, GVariant *header, GBytes *partdata, gboolean trusted, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data) { StaticDeltaPartExecuteAsyncData *asyncdata; asyncdata = g_new0 (StaticDeltaPartExecuteAsyncData, 1); asyncdata->repo = g_object_ref (repo); asyncdata->header = g_variant_ref (header); asyncdata->partdata = g_bytes_ref (partdata); asyncdata->trusted = trusted; asyncdata->cancellable = cancellable ? g_object_ref (cancellable) : NULL; asyncdata->result = g_simple_async_result_new ((GObject*) repo, callback, user_data, _ostree_static_delta_part_execute_async); g_simple_async_result_set_op_res_gpointer (asyncdata->result, asyncdata, static_delta_part_execute_async_data_free); g_simple_async_result_run_in_thread (asyncdata->result, static_delta_part_execute_thread, G_PRIORITY_DEFAULT, cancellable); g_object_unref (asyncdata->result); }
static GBytes * check_utf8_and_force_if_necessary (GBytes *input) { const gchar *data; const gchar *end; gsize length; GString *string; data = g_bytes_get_data (input, &length); if (g_utf8_validate (data, length, &end)) return g_bytes_ref (input); string = g_string_sized_new (length + 16); do { /* Valid part of the string */ g_string_append_len (string, data, end - data); /* Replacement character */ g_string_append (string, "\xef\xbf\xbd"); length -= (end - data) + 1; data = end + 1; } while (!g_utf8_validate (data, length, &end)); if (length) g_string_append_len (string, data, length); return g_string_free_to_bytes (string); }
gboolean um_realm_login_finish (UmRealmObject *realm, GAsyncResult *result, GBytes **credentials, GError **error) { GTask *task; LoginClosure *login; g_return_val_if_fail (g_task_is_valid (result, realm), FALSE); g_return_val_if_fail (error == NULL || *error == NULL, FALSE); task = G_TASK (result); if (!g_task_propagate_boolean (task, error)) return FALSE; login = g_task_get_task_data (task); if (credentials) { if (login->credentials) *credentials = g_bytes_ref (login->credentials); else *credentials = NULL; } return TRUE; }
gboolean _ostree_static_delta_part_execute (OstreeRepo *repo, GVariant *header, GBytes *part_bytes, GCancellable *cancellable, GError **error) { gboolean ret = FALSE; gsize partlen; const guint8*partdata; gs_unref_bytes GBytes *part_payload_bytes = NULL; gs_unref_bytes GBytes *payload_data = NULL; gs_unref_variant GVariant *payload = NULL; guint8 comptype; partdata = g_bytes_get_data (part_bytes, &partlen); if (partlen < 1) { g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Corrupted 0 length delta part"); goto out; } /* First byte is compression type */ comptype = partdata[0]; /* Then the rest may be compressed or uncompressed */ part_payload_bytes = g_bytes_new_from_bytes (part_bytes, 1, partlen - 1); switch (comptype) { case 0: /* No compression */ payload_data = g_bytes_ref (part_payload_bytes); break; case 'x': { gs_unref_object GConverter *decomp = (GConverter*) _ostree_lzma_decompressor_new (); if (!decompress_all (decomp, part_payload_bytes, &payload_data, cancellable, error)) goto out; } break; default: g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Invalid compression type '%u'", comptype); goto out; } payload = g_variant_new_from_bytes (G_VARIANT_TYPE (OSTREE_STATIC_DELTA_PART_PAYLOAD_FORMAT_V0), payload_data, FALSE); if (!_ostree_static_delta_part_execute_raw (repo, header, payload, cancellable, error)) goto out; ret = TRUE; out: return ret; }
/* Create MMS notification task */ MMSTask* mms_task_notification_new( MMSSettings* settings, MMSHandler* handler, const char* imsi, GBytes* bytes, GError** error) { MMSPdu* pdu = mms_decode_bytes(bytes); MMS_ASSERT(!error || !(*error)); if (pdu) { MMSTaskNotification* ind; /* Looks like a legitimate MMS Push PDU */ #if MMS_LOG_DEBUG MMS_DEBUG(" MMS version: %u.%u", (pdu->version & 0x70) >> 4, pdu->version & 0x0f); if (pdu->transaction_id) { MMS_DEBUG(" MMS transaction id: %s", pdu->transaction_id); } #endif /* MMS_LOG_DEBUG */ ind = mms_task_alloc(MMS_TYPE_TASK_NOTIFICATION, settings, handler, "Notification", NULL, imsi); ind->push = g_bytes_ref(bytes); ind->pdu = pdu; return &ind->task; } else {
/** * dfu_firmware_to_raw: (skip) * @firmware: a #DfuFirmware * @error: a #GError, or %NULL * * Packs raw firmware * * Returns: (transfer full): the packed data **/ GBytes * dfu_firmware_to_raw (DfuFirmware *firmware, GError **error) { DfuElement *element; DfuImage *image; GBytes *contents; image = dfu_firmware_get_image_default (firmware); if (image == NULL) { g_set_error_literal (error, FWUPD_ERROR, FWUPD_ERROR_NOT_FOUND, "no firmware image data to write"); return NULL; } element = dfu_image_get_element (image, 0); if (element == NULL) { g_set_error_literal (error, FWUPD_ERROR, FWUPD_ERROR_NOT_FOUND, "no firmware element data to write"); return NULL; } contents = dfu_element_get_contents (element); return g_bytes_ref (contents); }
static JSBool from_gbytes_func(JSContext *context, unsigned argc, jsval *vp) { JS::CallArgs argv = JS::CallArgsFromVp (argc, vp); JSObject *bytes_obj; GBytes *gbytes; ByteArrayInstance *priv; JSObject *obj; JSBool ret = JS_FALSE; if (!gjs_parse_call_args(context, "overrides_gbytes_to_array", "o", argv, "bytes", &bytes_obj)) return JS_FALSE; if (!gjs_typecheck_boxed(context, bytes_obj, NULL, G_TYPE_BYTES, TRUE)) return JS_FALSE; gbytes = (GBytes*) gjs_c_struct_from_boxed(context, bytes_obj); obj = byte_array_new(context); if (obj == NULL) return JS_FALSE; priv = priv_from_js(context, obj); g_assert (priv != NULL); priv->bytes = g_bytes_ref(gbytes); ret = JS_TRUE; argv.rval().set(OBJECT_TO_JSVAL(obj)); return ret; }
/** * g_variant_get_data_as_bytes: * @value: a #GVariant * * Returns a pointer to the serialised form of a #GVariant instance. * The semantics of this function are exactly the same as * g_variant_get_data(), except that the returned #GBytes holds * a reference to the variant data. * * Returns: (transfer full): A new #GBytes representing the variant data * * Since: 2.36 */ GBytes * g_variant_get_data_as_bytes (GVariant *value) { g_variant_lock (value); g_variant_ensure_serialised (value); g_variant_unlock (value); return g_bytes_ref (value->contents.serialised.bytes); }
static void cockpit_auth_session_login_async (CockpitAuth *self, const gchar *path, GHashTable *headers, const gchar *remote_peer, GAsyncReadyCallback callback, gpointer user_data) { GSimpleAsyncResult *result; SessionLoginData *sl; GBytes *input; gchar *application; gchar *type = NULL; result = g_simple_async_result_new (G_OBJECT (self), callback, user_data, cockpit_auth_session_login_async); application = cockpit_auth_parse_application (path); input = cockpit_auth_parse_authorization (headers, &type); if (input && application) { sl = g_new0 (SessionLoginData, 1); sl->remote_peer = g_strdup (remote_peer); sl->auth_type = type; sl->authorization = g_bytes_ref (input); sl->application = application; application = NULL; g_simple_async_result_set_op_res_gpointer (result, sl, session_login_data_free); sl->session_pipe = spawn_session_process (type, input, remote_peer, &sl->auth_pipe); if (sl->session_pipe) { g_signal_connect (sl->auth_pipe, "close", G_CALLBACK (on_session_login_done), g_object_ref (result)); } else { g_simple_async_result_set_error (result, COCKPIT_ERROR, COCKPIT_ERROR_FAILED, "Internal error starting session process"); g_simple_async_result_complete_in_idle (result); } } else { g_free (type); g_simple_async_result_set_error (result, COCKPIT_ERROR, COCKPIT_ERROR_AUTHENTICATION_FAILED, "Authentication required"); g_simple_async_result_complete_in_idle (result); } g_bytes_unref (input); g_free (application); g_object_unref (result); }
/** * g_variant_get_child_value: * @value: a container #GVariant * @index_: the index of the child to fetch * * Reads a child item out of a container #GVariant instance. This * includes variants, maybes, arrays, tuples and dictionary * entries. It is an error to call this function on any other type of * #GVariant. * * It is an error if @index_ is greater than the number of child items * in the container. See g_variant_n_children(). * * The returned value is never floating. You should free it with * g_variant_unref() when you're done with it. * * This function is O(1). * * Returns: (transfer full): the child at the specified index * * Since: 2.24 **/ GVariant * g_variant_get_child_value (GVariant *value, gsize index_) { g_return_val_if_fail (index_ < g_variant_n_children (value), NULL); if (~g_atomic_int_get (&value->state) & STATE_SERIALISED) { g_variant_lock (value); if (~value->state & STATE_SERIALISED) { GVariant *child; child = g_variant_ref (value->contents.tree.children[index_]); g_variant_unlock (value); return child; } g_variant_unlock (value); } { GVariantSerialised serialised = { value->type_info, (gpointer) value->contents.serialised.data, value->size }; GVariantSerialised s_child; GVariant *child; /* get the serialiser to extract the serialised data for the child * from the serialised data for the container */ s_child = g_variant_serialised_get_child (serialised, index_); /* create a new serialised instance out of it */ child = g_slice_new (GVariant); #ifdef GSTREAMER_LITE if (child == NULL) { return NULL; } #endif // GSTREAMER_LITE child->type_info = s_child.type_info; child->state = (value->state & STATE_TRUSTED) | STATE_SERIALISED; child->size = s_child.size; child->ref_count = 1; child->contents.serialised.bytes = g_bytes_ref (value->contents.serialised.bytes); child->contents.serialised.data = s_child.data; return child; } }
GVariant * ot_gvariant_new_ay_bytes (GBytes *bytes) { gsize size; gconstpointer data; data = g_bytes_get_data (bytes, &size); g_bytes_ref (bytes); return g_variant_new_from_data (G_VARIANT_TYPE ("ay"), data, size, TRUE, (GDestroyNotify)g_bytes_unref, bytes); }
/** * flatpak_remote_set_gpg_key: * @self: a #FlatpakRemote * @gpg_key: a #GBytes with gpg binary key data * * Sets the trusted gpg key for this remote. * * Note: This is a local modification of this object, you must commit changes * using flatpak_installation_modify_remote() for the changes to take * effect. */ void flatpak_remote_set_gpg_key (FlatpakRemote *self, GBytes *gpg_key) { FlatpakRemotePrivate *priv = flatpak_remote_get_instance_private (self); if (priv->local_gpg_key != NULL) g_bytes_unref (priv->local_gpg_key); priv->local_gpg_key = g_bytes_ref (gpg_key); }
static gboolean on_transport_control (CockpitTransport *transport, const char *command, const gchar *channel, JsonObject *options, GBytes *payload, gpointer user_data) { CockpitPortalFlags flags = COCKPIT_PORTAL_NORMAL; CockpitPortal *self = user_data; gboolean relay = FALSE; if (g_str_equal (command, "init")) { if (self->last_init) g_bytes_unref (self->last_init); self->last_init = g_bytes_ref (payload); return FALSE; } if (channel) { if (self->channels && g_hash_table_contains (self->channels, channel)) relay = TRUE; } if (!relay) { g_assert (self->filter_func != NULL); relay = self->filter_func (self, command, channel, options); } if (relay) { if (channel && self->channels) flags = GPOINTER_TO_INT (g_hash_table_lookup (self->channels, channel)); if (self->state == PORTAL_FAILED && !(flags & COCKPIT_PORTAL_FALLBACK)) { if (channel) { if (self->channels) g_hash_table_remove (self->channels, channel); send_close_channel (self, channel, self->problem); } return TRUE; } else { return send_to_portal (self, NULL, payload, flags); } } return FALSE; }
static CockpitPortalMessage * cockpit_portal_message_new (const gchar *channel, GBytes *payload, CockpitPortalFlags flags) { CockpitPortalMessage *message = g_slice_new (CockpitPortalMessage); message->channel = channel; /* channel must have been interned */ message->payload = g_bytes_ref (payload); message->flags = flags; return message; }
static FrozenMessage * frozen_message_new (GBytes *payload, JsonObject *control) { FrozenMessage *frozen = g_slice_new0 (FrozenMessage); if (payload) frozen->payload = g_bytes_ref (payload); if (control) frozen->control = json_object_ref (control); return frozen; }
/** * dfu_element_set_contents: * @element: a #DfuElement * @contents: element data * * Sets the element data. * * Since: 0.5.4 **/ void dfu_element_set_contents (DfuElement *element, GBytes *contents) { DfuElementPrivate *priv = GET_PRIVATE (element); g_return_if_fail (DFU_IS_ELEMENT (element)); g_return_if_fail (contents != NULL); if (priv->contents == contents) return; if (priv->contents != NULL) g_bytes_unref (priv->contents); priv->contents = g_bytes_ref (contents); }
/** * g_bytes_new_from_bytes: * @bytes: a #GBytes * @offset: offset which subsection starts at * @length: length of subsection * * Creates a #GBytes which is a subsection of another #GBytes. The @offset + * @length may not be longer than the size of @bytes. * * A reference to @bytes will be held by the newly created #GBytes until * the byte data is no longer needed. * * Returns: (transfer full): a new #GBytes * * Since: 2.32 */ GBytes * g_bytes_new_from_bytes (GBytes *bytes, gsize offset, gsize length) { g_return_val_if_fail (bytes != NULL, NULL); g_return_val_if_fail (offset <= bytes->size, NULL); g_return_val_if_fail (offset + length <= bytes->size, NULL); return g_bytes_new_with_free_func ((gchar *)bytes->data + offset, length, (GDestroyNotify)g_bytes_unref, g_bytes_ref (bytes)); }
/** * gdk_pixbuf_read_pixel_bytes: * @pixbuf: A pixbuf * * Returns: (transfer full): A new reference to a read-only copy of * the pixel data. Note that for mutable pixbufs, this function will * incur a one-time copy of the pixel data for conversion into the * returned #GBytes. * * Since: 2.32 */ GBytes * gdk_pixbuf_read_pixel_bytes (const GdkPixbuf *pixbuf) { g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL); if (pixbuf->bytes) { return g_bytes_ref (pixbuf->bytes); } else { return g_bytes_new (pixbuf->pixels, gdk_pixbuf_get_byte_length (pixbuf)); } }
GBytes * gjs_byte_array_get_bytes (JSContext *context, JSObject *object) { ByteArrayInstance *priv; priv = priv_from_js(context, object); g_assert(priv != NULL); byte_array_ensure_gbytes(priv); return g_bytes_ref (priv->bytes); }
/** * as_icon_set_data: * @icon: a #AsIcon instance. * @data: the #GBytes, or %NULL * * Sets the icon data. * * Since: 0.3.1 **/ void as_icon_set_data (AsIcon *icon, GBytes *data) { AsIconPrivate *priv = GET_PRIVATE (icon); if (priv->data != NULL) g_bytes_unref (priv->data); if (data == NULL) { priv->data = NULL; return; } priv->data = g_bytes_ref (data); }
/** * ostree_bloom_seal: * @bloom: an #OstreeBloom * * Seal a constructed bloom filter, so that elements may no longer be added to * it, and queries can now be performed against it. The serialised form of the * bloom filter is returned as a bit array. Note that this does not include * information about the filter hash function or parameters; the caller is * responsible for serialising those separately if appropriate. * * It is safe to call this function multiple times. * * Returns: (transfer full): a #GBytes containing the immutable filter data * Since: 2017.8 */ GBytes * ostree_bloom_seal (OstreeBloom *bloom) { g_return_val_if_fail (bloom != NULL, NULL); g_return_val_if_fail (bloom->ref_count >= 1, NULL); if (bloom->is_mutable) { bloom->is_mutable = FALSE; bloom->immutable_bytes = g_bytes_new_take (g_steal_pointer (&bloom->mutable_bytes), bloom->n_bytes); } return g_bytes_ref (bloom->immutable_bytes); }
/** * gdk_pixbuf_buffer_queue_peek_buffer: * @queue: a #GdkPixbufBufferQueue * * Gets the first buffer out of @queue and returns it. This function is * equivalent to calling gdk_pixbuf_buffer_queue_peek() with the size of the * first buffer in it. * * Returns: The first buffer in @queue or %NULL if @queue is empty. Use * g_bytes_unref() after use. **/ GBytes * gdk_pixbuf_buffer_queue_peek_buffer (GdkPixbufBufferQueue * queue) { GBytes *bytes; g_return_val_if_fail (queue != NULL, NULL); if (queue->first_buffer == NULL) return NULL; bytes = queue->first_buffer->data; return g_bytes_ref (bytes); }
static void queue_bytes (CockpitWebResponse *self, GBytes *block) { g_queue_push_tail (self->queue, g_bytes_ref (block)); self->count++; if (!self->source) { self->source = g_pollable_output_stream_create_source (self->out, NULL); g_source_set_callback (self->source, (GSourceFunc)on_response_output, self, NULL); g_source_attach (self->source, NULL); } }
static void add_assertion_to_trust (GkmXdgTrust *self, GkmAssertion *assertion, GkmTransaction *transaction) { GBytes *key; key = lookup_or_create_assertion_key (assertion); g_assert (key != NULL); g_hash_table_insert (self->pv->assertions, g_bytes_ref (key), g_object_ref (assertion)); gkm_object_expose (GKM_OBJECT (assertion), gkm_object_is_exposed (GKM_OBJECT (self))); if (transaction != NULL) gkm_transaction_add (transaction, self, complete_add_assertion, g_object_ref (assertion)); }