/* the actual read is "slow" because it calls the underlying transport */ gint32 thrift_framed_transport_read_slow (ThriftTransport *transport, gpointer buf, guint32 len, GError **error) { ThriftFramedTransport *t = THRIFT_FRAMED_TRANSPORT (transport); guint32 want = len; guint32 have = t->r_buf->len; // we shouldn't hit this unless the buffer doesn't have enough to read assert (t->r_buf->len < want); // first copy what we have in our buffer, if there is anything left if (have > 0) { memcpy (buf, t->r_buf, t->r_buf->len); want -= t->r_buf->len; t->r_buf = g_byte_array_remove_range (t->r_buf, 0, t->r_buf->len); } // read a frame of input and buffer it thrift_framed_transport_read_frame (transport, error); // hand over what we have up to what the caller wants guint32 give = want < t->r_buf->len ? want : t->r_buf->len; // copy the data into the buffer memcpy (buf + len - want, t->r_buf->data, give); t->r_buf = g_byte_array_remove_range (t->r_buf, 0, give); want -= give; return (len - want); }
/* the actual read is "slow" because it calls the underlying transport */ gint32 thrift_buffered_transport_read_slow (ThriftTransport *transport, gpointer buf, guint32 len, GError **error) { ThriftBufferedTransport *t = THRIFT_BUFFERED_TRANSPORT (transport); gint ret = 0; guint32 want = len; guint32 got = 0; guchar tmpdata[len]; guint32 have = t->r_buf->len; // we shouldn't hit this unless the buffer doesn't have enough to read assert (t->r_buf->len < want); // first copy what we have in our buffer. if (have > 0) { memcpy (buf, t->r_buf, t->r_buf->len); want -= t->r_buf->len; t->r_buf = g_byte_array_remove_range (t->r_buf, 0, t->r_buf->len); } // if the buffer is still smaller than what we want to read, then just // read it directly. otherwise, fill the buffer and then give out // enough to satisfy the read. if (t->r_buf_size < want) { if ((ret = THRIFT_TRANSPORT_GET_CLASS (t->transport)->read (t->transport, tmpdata, want, error)) < 0) { return ret; } got += ret; // copy the data starting from where we left off memcpy (buf + have, tmpdata, got); return got + have; } else { if ((ret = THRIFT_TRANSPORT_GET_CLASS (t->transport)->read (t->transport, tmpdata, want, error)) < 0) { return ret; } got += ret; t->r_buf = g_byte_array_append (t->r_buf, tmpdata, got); // hand over what we have up to what the caller wants guint32 give = want < t->r_buf->len ? want : t->r_buf->len; memcpy (buf + len - want, t->r_buf->data, give); t->r_buf = g_byte_array_remove_range (t->r_buf, 0, give); want -= give; return (len - want); } }
/* implements thrift_transport_flush */ gboolean thrift_framed_transport_flush(ThriftTransport *transport, GError **error) { ThriftFramedTransport *t = THRIFT_FRAMED_TRANSPORT (transport); gint32 sz_hbo, sz_nbo; // get the size of the frame in host and network byte order sz_hbo = t->w_buf->len + sizeof(sz_nbo); sz_nbo = (gint32) htonl((guint32) t->w_buf->len); // copy the size of the frame and then the frame itself guchar tmpdata[sz_hbo]; memcpy(tmpdata, (guint8 *) &sz_nbo, sizeof(sz_nbo)); if (t->w_buf->len > 0) { memcpy(tmpdata + sizeof(sz_nbo), t->w_buf->data, t->w_buf->len); t->w_buf = g_byte_array_remove_range(t->w_buf, 0, t->w_buf->len); } // write the buffer and then empty it THRIFT_TRANSPORT_GET_CLASS (t->transport)->write(t->transport, tmpdata, sz_hbo, error); if (error && *error) { printf("GError:%s\n", (*error)->message); return FALSE; } THRIFT_TRANSPORT_GET_CLASS (t->transport)->flush(t->transport, error); if (error && *error) { printf("GError:%s\n", (*error)->message); return FALSE; } return TRUE; }
/* implements thrift_transport_flush */ gboolean thrift_framed_transport_flush (ThriftTransport *transport, GError **error) { ThriftFramedTransport *t = THRIFT_FRAMED_TRANSPORT (transport); gint32 sz_hbo, sz_nbo; guchar *tmpdata; /* get the size of the frame in host and network byte order */ sz_hbo = t->w_buf->len + sizeof(sz_nbo); sz_nbo = (gint32) htonl ((guint32) t->w_buf->len); /* copy the size of the frame and then the frame itself */ tmpdata = g_alloca (sz_hbo); memcpy (tmpdata, (guint8 *) &sz_nbo, sizeof (sz_nbo)); if (t->w_buf->len > 0) { memcpy (tmpdata + sizeof (sz_nbo), t->w_buf->data, t->w_buf->len); t->w_buf = g_byte_array_remove_range (t->w_buf, 0, t->w_buf->len); } /* write the buffer and then empty it */ THRIFT_TRANSPORT_GET_CLASS (t->transport)->write (t->transport, tmpdata, sz_hbo, error); THRIFT_TRANSPORT_GET_CLASS (t->transport)->flush (t->transport, error); return TRUE; }
static gboolean parse_response (MMSerialPort *port, GByteArray *response, GError **error) { MMAtSerialPort *self = MM_AT_SERIAL_PORT (port); MMAtSerialPortPrivate *priv = MM_AT_SERIAL_PORT_GET_PRIVATE (self); gboolean found; GString *string; g_return_val_if_fail (priv->response_parser_fn != NULL, FALSE); /* Construct the string that AT-parsing functions expect */ string = g_string_sized_new (response->len + 1); g_string_append_len (string, (const char *) response->data, response->len); /* Parse it */ found = priv->response_parser_fn (priv->response_parser_user_data, string, error); /* And copy it back into the response array after the parser has removed * matches and cleaned it up. */ if (response->len) g_byte_array_remove_range (response, 0, response->len); g_byte_array_append (response, (const guint8 *) string->str, string->len); g_string_free (string, TRUE); return found; }
static void serial_command_ready (MMPortSerial *port, GAsyncResult *res, GSimpleAsyncResult *simple) { GByteArray *response_buffer; GError *error = NULL; GString *response; response_buffer = mm_port_serial_command_finish (port, res, &error); if (!response_buffer) { g_simple_async_result_take_error (simple, error); g_simple_async_result_complete (simple); g_object_unref (simple); return; } /* Build a GString just with the response we need, and clear the * processed range from the response buffer */ response = g_string_new_len ((const gchar *)response_buffer->data, response_buffer->len); if (response_buffer->len > 0) g_byte_array_remove_range (response_buffer, 0, response_buffer->len); g_byte_array_unref (response_buffer); g_simple_async_result_set_op_res_gpointer (simple, response, (GDestroyNotify)string_free); g_simple_async_result_complete (simple); g_object_unref (simple); }
static guint gst_dshowaudiosrc_read (GstAudioSrc * asrc, gpointer data, guint length) { GstDshowAudioSrc *src = GST_DSHOWAUDIOSRC (asrc); guint ret = 0; if (!src->is_running) return -1; if (src->gbarray) { test: if (src->gbarray->len >= length) { g_mutex_lock (src->gbarray_lock); memcpy (data, src->gbarray->data + (src->gbarray->len - length), length); g_byte_array_remove_range (src->gbarray, src->gbarray->len - length, length); ret = length; g_mutex_unlock (src->gbarray_lock); } else { if (src->is_running) { Sleep (100); goto test; } } } return ret; }
static guint gst_dshowaudiosrc_read (GstAudioSrc * asrc, gpointer data, guint length, GstClockTime *timestamp) { GstDshowAudioSrc *src = GST_DSHOWAUDIOSRC (asrc); guint ret = 0; if (!src->is_running) return -1; if (src->gbarray) { test: if (src->gbarray->len >= length) { g_mutex_lock (&src->gbarray_lock); memcpy (data, src->gbarray->data + (src->gbarray->len - length), length); g_byte_array_remove_range (src->gbarray, src->gbarray->len - length, length); ret = length; g_mutex_unlock (&src->gbarray_lock); } else { if (src->is_running) { Sleep (GST_AUDIO_BASE_SRC(src)->ringbuffer->spec.latency_time / GST_MSECOND / 10); goto test; } } } return ret; }
static void test_decode_header_multi(void) { GObexHeader *header; GByteArray *buf; size_t parsed; GError *err = NULL; buf = g_byte_array_sized_new(sizeof(hdr_connid) + sizeof(hdr_name_ascii) + sizeof(hdr_actionid) + sizeof(hdr_body)); g_byte_array_append(buf, hdr_connid, sizeof(hdr_connid)); g_byte_array_append(buf, hdr_name_ascii, sizeof(hdr_name_ascii)); g_byte_array_append(buf, hdr_actionid, sizeof(hdr_actionid)); g_byte_array_append(buf, hdr_body, sizeof(hdr_body)); header = g_obex_header_decode(buf->data, buf->len, G_OBEX_DATA_REF, &parsed, &err); g_assert_no_error(err); g_assert_cmpuint(parsed, ==, sizeof(hdr_connid)); g_byte_array_remove_range(buf, 0, parsed); g_obex_header_free(header); header = g_obex_header_decode(buf->data, buf->len, G_OBEX_DATA_REF, &parsed, &err); g_assert_no_error(err); g_assert_cmpuint(parsed, ==, sizeof(hdr_name_ascii)); g_byte_array_remove_range(buf, 0, parsed); g_obex_header_free(header); header = g_obex_header_decode(buf->data, buf->len, G_OBEX_DATA_REF, &parsed, &err); g_assert_no_error(err); g_assert_cmpuint(parsed, ==, sizeof(hdr_actionid)); g_byte_array_remove_range(buf, 0, parsed); g_obex_header_free(header); header = g_obex_header_decode(buf->data, buf->len, G_OBEX_DATA_REF, &parsed, &err); g_assert_no_error(err); g_assert_cmpuint(parsed, ==, sizeof(hdr_body)); g_byte_array_remove_range(buf, 0, parsed); g_obex_header_free(header); g_byte_array_unref(buf); }
static size_t gst_curl_smtp_sink_flush_data_unlocked (GstCurlBaseSink * bcsink, void *curl_ptr, size_t block_size, gboolean new_file) { GstCurlSmtpSink *sink = GST_CURL_SMTP_SINK (bcsink); Base64Chunk *chunk = sink->base64_chunk; gint state = chunk->state; gint save = chunk->save; GByteArray *array = chunk->chunk_array; size_t bytes_to_send; gint len; gchar *data_out; if ((bcsink->is_live && (sink->nbr_attachments_left == sink->nbr_attachments)) || (sink->nbr_attachments == 1) || sink->eos) { bcsink->is_live = FALSE; sink->reset_transfer_options = TRUE; GST_DEBUG ("returning 0, no more data to send in this transfer"); return 0; } /* it will need up to 5 bytes if line-breaking is enabled, however an * additional byte is needed for <CR> as it is not automatically added by glib */ data_out = g_malloc (6); len = g_base64_encode_close (TRUE, data_out, &state, &save); chunk->state = state; chunk->save = save; /* workaround */ data_out[len - 1] = '\r'; data_out[len] = '\n'; /* +1 for CR */ g_byte_array_append (array, (guint8 *) data_out, (guint) (len + 1)); g_free (data_out); if (new_file) { sink->nbr_attachments_left--; bcsink->is_live = TRUE; if (sink->nbr_attachments_left <= 1) { sink->nbr_attachments_left = sink->nbr_attachments; } /* reset flag */ bcsink->new_file = FALSE; /* set payload headers for new file */ gst_curl_smtp_sink_set_payload_headers_unlocked (bcsink); } bytes_to_send = MIN (block_size, array->len); memcpy ((guint8 *) curl_ptr, array->data, bytes_to_send); g_byte_array_remove_range (array, 0, bytes_to_send); return bytes_to_send; }
static void gst_dshowaudiosrc_reset (GstAudioSrc * asrc) { GstDshowAudioSrc *src = GST_DSHOWAUDIOSRC (asrc); g_mutex_lock (src->gbarray_lock); g_byte_array_remove_range (src->gbarray, 0, src->gbarray->len); g_mutex_unlock (src->gbarray_lock); }
void mbus_data_set( MObject * mdata, const guint8 * str, guint len ) { GByteArray * array; M_OBJECT_ASSERT( mdata, MDATA ); array = M_DATA( mdata )->array; g_byte_array_remove_range( array, 0, array->len ); g_byte_array_append( array, str, len ); }
gboolean thrift_buffered_transport_write_slow (ThriftTransport *transport, gpointer buf, guint32 len, GError **error) { ThriftBufferedTransport *t = THRIFT_BUFFERED_TRANSPORT (transport); guint32 have_bytes = t->w_buf->len; guint32 space = t->w_buf_size - t->w_buf->len; // we need two syscalls because the buffered data plus the buffer itself // is too big. if ((have_bytes + len >= 2*t->w_buf_size) || (have_bytes == 0)) { if (have_bytes > 0) { if (!THRIFT_TRANSPORT_GET_CLASS (t->transport)->write (t->transport, t->w_buf->data, have_bytes, error)) { return FALSE; } t->w_buf = g_byte_array_remove_range (t->w_buf, 0, have_bytes); } if (!THRIFT_TRANSPORT_GET_CLASS (t->transport)->write (t->transport, buf, len, error)) { return FALSE; } return TRUE; } t->w_buf = g_byte_array_append (t->w_buf, buf, space); if (!THRIFT_TRANSPORT_GET_CLASS (t->transport)->write (t->transport, t->w_buf->data, t->w_buf->len, error)) { return FALSE; } t->w_buf = g_byte_array_remove_range (t->w_buf, 0, t->w_buf->len); t->w_buf = g_byte_array_append (t->w_buf, buf+space, len-space); return TRUE; }
static gboolean on_read_bytes (GPollableInputStream * stream, Client * client) { gssize r; gchar data[4096]; GError *err = NULL; do { r = g_pollable_input_stream_read_nonblocking (G_POLLABLE_INPUT_STREAM (client->istream), data, sizeof (data), NULL, &err); if (r > 0) g_byte_array_append (client->current_message, (guint8 *) data, r); } while (r > 0); if (r == 0) { remove_client (client); return FALSE; } else if (g_error_matches (err, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK)) { guint8 *tmp = client->current_message->data; g_clear_error (&err); while (client->current_message->len > 3) { if (tmp[0] == 0x0d && tmp[1] == 0x0a && tmp[2] == 0x0d && tmp[3] == 0x0a) { guint len; g_byte_array_append (client->current_message, (const guint8 *) "\0", 1); len = tmp - client->current_message->data + 5; client_message (client, (gchar *) client->current_message->data, len); g_byte_array_remove_range (client->current_message, 0, len); tmp = client->current_message->data; } else { tmp++; } } if (client->current_message->len >= 1024 * 1024) { g_print ("No complete request after 1MB of data\n"); remove_client (client); return FALSE; } return TRUE; } else { g_print ("Read error %s\n", err->message); g_clear_error (&err); remove_client (client); return FALSE; } return FALSE; }
static ssize_t array_read(GByteArray *array, void *buf, size_t count) { ssize_t len; if (array->len == 0) return 0; len = MIN(array->len, count); memcpy(buf, array->data, len); array = g_byte_array_remove_range(array, 0, len); return len; }
static MMPortSerialResponseType parse_response (MMPortSerial *port, GByteArray *response, GByteArray **parsed_response, GError **error) { MMPortSerialAt *self = MM_PORT_SERIAL_AT (port); GString *string; gsize parsed_len; GError *inner_error = NULL; g_return_val_if_fail (self->priv->response_parser_fn != NULL, FALSE); /* Remove echo */ if (self->priv->remove_echo) mm_port_serial_at_remove_echo (response); /* If there's no response to receive, we're done; e.g. if we only got * unsolicited messages */ if (!response->len) return MM_PORT_SERIAL_RESPONSE_NONE; /* Construct the string that AT-parsing functions expect */ string = g_string_sized_new (response->len + 1); g_string_append_len (string, (const char *) response->data, response->len); /* Fully cleanup the response array, we'll consider the contents we got * as the full reply that the command may expect. */ g_byte_array_remove_range (response, 0, response->len); /* Parse it; returns FALSE if there is nothing we can do with this * response yet. */ if (!self->priv->response_parser_fn (self->priv->response_parser_user_data, string, &inner_error)) { /* Copy what we got back in the response buffer. */ g_byte_array_append (response, (const guint8 *) string->str, string->len); g_string_free (string, TRUE); return MM_PORT_SERIAL_RESPONSE_NONE; } /* If we got an error, propagate it without any further response string */ if (inner_error) { g_propagate_error (error, inner_error); return MM_PORT_SERIAL_RESPONSE_ERROR; } /* Otherwise, build a new GByteArray considered as parsed response */ parsed_len = string->len; *parsed_response = g_byte_array_new_take ((guint8 *) g_string_free (string, FALSE), parsed_len); return MM_PORT_SERIAL_RESPONSE_BUFFER; }
void fb_mqtt_message_reset(FbMqttMessage *msg) { FbMqttMessagePrivate *priv; g_return_if_fail(FB_IS_MQTT_MESSAGE(msg)); priv = msg->priv; if (priv->offset > 0) { g_byte_array_remove_range(priv->bytes, 0, priv->offset); priv->offset = 0; priv->pos = 0; } }
/* implements thrift_transport_read */ gint32 thrift_framed_transport_read(ThriftTransport *transport, gpointer buf, guint32 len, GError **error) { ThriftFramedTransport *t = THRIFT_FRAMED_TRANSPORT (transport); /* if we have enough buffer data to fulfill the read, just use * a memcpy from the buffer */ if (len <= t->r_buf->len) { memcpy(buf, t->r_buf->data, len); g_byte_array_remove_range(t->r_buf, 0, len); return len; } return thrift_framed_transport_read_slow(transport, buf, len, error); }
static void run_input_source_fd_callbacks (SourceFD *sfd) { while (sfd->base.callback) { char *start = (char *) sfd->buffer->data; char *nl = memchr (start, sfd->separator_char, sfd->buffer->len); if (nl == NULL) break; *nl = 0; DEBUG_ONLY (g_message ("calling back %s", (char*)sfd->buffer->data)); sfd->base.callback (&sfd->base, start, sfd->base.trap_data); g_byte_array_remove_range (sfd->buffer, 0, (nl + 1) - start); } }
static void parse_unsolicited (MMPortSerial *port, GByteArray *response) { MMPortSerialAt *self = MM_PORT_SERIAL_AT (port); GSList *iter; /* Remove echo */ if (self->priv->remove_echo) mm_port_serial_at_remove_echo (response); for (iter = self->priv->unsolicited_msg_handlers; iter; iter = iter->next) { MMAtUnsolicitedMsgHandler *handler = (MMAtUnsolicitedMsgHandler *) iter->data; GMatchInfo *match_info; gboolean matches; if (!handler->enable) continue; matches = g_regex_match_full (handler->regex, (const char *) response->data, response->len, 0, 0, &match_info, NULL); if (handler->callback) { while (g_match_info_matches (match_info)) { handler->callback (self, match_info, handler->user_data); g_match_info_next (match_info, NULL); } } g_match_info_free (match_info); if (matches) { /* Remove matches */ char *str; int result_len = response->len; str = g_regex_replace_eval (handler->regex, (const char *) response->data, response->len, 0, 0, remove_eval_cb, &result_len, NULL); g_byte_array_remove_range (response, 0, response->len); g_byte_array_append (response, (const guint8 *) str, result_len); g_free (str); } } }
static gboolean bcas_stream_sync(BCASStream *self) { guint8 *p; guint left_size, skip_size; gboolean is_synced = FALSE; /* バッファ長が絶対にパケットとして成立しない長さだと同期は取れない */ if (self->raw_stream->len < PACKET_MIN_SIZE) { /* g_debug("[bcas_stream_sync] not enough stream (len=%d, MIN=%d) at %u", */ /* self->raw_stream->len, PACKET_MIN_SIZE, self->pos); */ return FALSE; } /* ECMコマンドが現われる場所を探す */ left_size = self->raw_stream->len - (PACKET_HEADER_SIZE + 4); skip_size = 0; for (p = self->raw_stream->data; left_size > 0; ++p, ++skip_size, --left_size) { if (IS_ECM_REQUEST(p)) { is_synced = TRUE; break; } } if (is_synced) { self->pos += skip_size; /* ストリーム先頭の中途半端なパケットを削る */ if (skip_size > 0) { GString *dump = hexdump(self->raw_stream->data, skip_size, TRUE); g_debug("[bcas_stream_sync] skip %d bytes and dump [%s]", skip_size, dump->str); g_string_free(dump, TRUE); self->raw_stream = g_byte_array_remove_range(self->raw_stream, 0, skip_size); } /* パケットサイズ分のデータが残っていなければまだ同期完了にしない */ if (left_size < PACKET_HEADER_SIZE + p[PACKET_LEN_INDEX] + 1/* header + payload + checksum */) { /* g_debug("[bcas_stream_sync] not enough stream (expect %d bytes but left %d bytes) at %u", */ /* p[PACKET_LEN_INDEX] + 1, left_size, self->pos); */ is_synced = FALSE; } if (is_synced) g_message("[bcas_stream_sync] synced with %d bytes skipped at %u", skip_size, self->pos); } return is_synced; }
static gboolean write_input (int fd, GByteArray *buffer) { gssize result; g_return_val_if_fail (fd >= 0, FALSE); for (;;) { result = write (fd, buffer->data, buffer->len); if (result < 0) { if (errno == EINTR || errno == EAGAIN) continue; return FALSE; } else { g_byte_array_remove_range (buffer, 0, result); return TRUE; } } }
static size_t transfer_payload_headers (GstCurlSmtpSink * sink, void *curl_ptr, size_t block_size) { size_t bytes_to_send; GByteArray *headers = sink->payload_headers; bytes_to_send = MIN (block_size, headers->len); memcpy ((guint8 *) curl_ptr, headers->data, bytes_to_send); g_byte_array_remove_range (headers, 0, bytes_to_send); if (headers->len == 0) { g_byte_array_free (headers, TRUE); sink->payload_headers = NULL; } return bytes_to_send; }
void mm_port_serial_at_remove_echo (GByteArray *response) { guint i; if (response->len <= 2) return; for (i = 0; i < (response->len - 1); i++) { /* If there is any content before the first * <CR><LF>, assume it's echo or garbage, and skip it */ if (response->data[i] == '\r' && response->data[i + 1] == '\n') { if (i > 0) g_byte_array_remove_range (response, 0, i); /* else, good, we're already started with <CR><LF> */ break; } } }
/* implements thrift_transport_flush */ gboolean thrift_buffered_transport_flush (ThriftTransport *transport, GError **error) { ThriftBufferedTransport *t = THRIFT_BUFFERED_TRANSPORT (transport); if (t->w_buf != NULL && t->w_buf->len > 0) { // write the buffer and then empty it THRIFT_TRANSPORT_GET_CLASS (t->transport)->write (t->transport, t->w_buf->data, t->w_buf->len, error); t->w_buf = g_byte_array_remove_range (t->w_buf, 0, t->w_buf->len); } THRIFT_TRANSPORT_GET_CLASS (t->transport)->flush (t->transport, error); return TRUE; }
static void parse_unsolicited (MMSerialPort *port, GByteArray *response) { MMAtSerialPort *self = MM_AT_SERIAL_PORT (port); MMAtSerialPortPrivate *priv = MM_AT_SERIAL_PORT_GET_PRIVATE (self); GSList *iter; for (iter = priv->unsolicited_msg_handlers; iter; iter = iter->next) { MMAtUnsolicitedMsgHandler *handler = (MMAtUnsolicitedMsgHandler *) iter->data; GMatchInfo *match_info; gboolean matches; matches = g_regex_match_full (handler->regex, (const char *) response->data, response->len, 0, 0, &match_info, NULL); if (handler->callback) { while (g_match_info_matches (match_info)) { handler->callback (self, match_info, handler->user_data); g_match_info_next (match_info, NULL); } } g_match_info_free (match_info); if (matches) { /* Remove matches */ char *str; int result_len = response->len; str = g_regex_replace_eval (handler->regex, (const char *) response->data, response->len, 0, 0, remove_eval_cb, &result_len, NULL); g_byte_array_remove_range (response, 0, response->len); g_byte_array_append (response, (const guint8 *) str, result_len); g_free (str); } } }
gssize g_tls_connection_base_read (GTlsConnectionBase *tls, void *buffer, gsize count, gboolean blocking, GCancellable *cancellable, GError **error) { GTlsConnectionBaseStatus status; gssize nread; do { if (!claim_op (tls, G_TLS_CONNECTION_BASE_OP_READ, blocking, cancellable, error)) return -1; if (tls->app_data_buf && !tls->handshaking) { nread = MIN (count, tls->app_data_buf->len); memcpy (buffer, tls->app_data_buf->data, nread); if (nread == tls->app_data_buf->len) g_clear_pointer (&tls->app_data_buf, g_byte_array_unref); else g_byte_array_remove_range (tls->app_data_buf, 0, nread); status = G_TLS_CONNECTION_BASE_OK; } else { status = G_TLS_CONNECTION_BASE_GET_CLASS (tls)-> read_fn (tls, buffer, count, blocking, &nread, cancellable, error); } yield_op (tls, G_TLS_CONNECTION_BASE_OP_READ, status); } while (status == G_TLS_CONNECTION_BASE_REHANDSHAKE); if (status == G_TLS_CONNECTION_BASE_OK) return nread; else return -1; }
/* implements thrift_transport_read */ gint32 thrift_memory_buffer_read (ThriftTransport *transport, gpointer buf, guint32 len, GError **error) { THRIFT_UNUSED_VAR (error); ThriftMemoryBuffer *t = THRIFT_MEMORY_BUFFER (transport); guint32 give = len; /* if the requested bytes are more than what we have available, * just give all that we have the buffer */ if (t->buf->len < len) { give = t->buf->len; } memcpy (buf, t->buf->data, give); g_byte_array_remove_range (t->buf, 0, give); return give; }
/** * Compresses a #GByteArray with zlib. The returned #GByteArray should * be freed with #g_byte_array_free() when no longer needed. * * @param bytes The #GByteArray. * * @return The resulting #GByteArray, or NULL on error. **/ GByteArray *fb_util_zcompress(const GByteArray *bytes) { GByteArray *ret; z_stream zs; gsize size; gint res; g_return_val_if_fail(bytes != NULL, NULL); memset(&zs, 0, sizeof zs); zs.zalloc = fb_util_zalloc; zs.zfree = fb_util_zfree; zs.next_in = bytes->data; zs.avail_in = bytes->len; if (deflateInit(&zs, Z_BEST_COMPRESSION) != Z_OK) return NULL; size = compressBound(bytes->len); ret = g_byte_array_new(); g_byte_array_set_size(ret, size); zs.next_out = ret->data; zs.avail_out = size; res = deflate(&zs, Z_FINISH); if (res != Z_STREAM_END) { deflateEnd(&zs); g_byte_array_free(ret, TRUE); return NULL; } size -= zs.avail_out; g_byte_array_remove_range(ret, size, ret->len - size); deflateEnd(&zs); return ret; }
static void fb_mqtt_cb_write(gpointer data, gint fd, PurpleInputCondition cond) { FbMqtt *mqtt = data; FbMqttPrivate *priv = mqtt->priv; gssize wize; wize = purple_ssl_write(priv->gsc, priv->wbuf->data, priv->wbuf->len); if (wize < 0) { fb_mqtt_error(mqtt, FB_MQTT_ERROR_GENERAL, _("Failed to write data")); return; } if (wize > 0) { g_byte_array_remove_range(priv->wbuf, 0, wize); } if (priv->wbuf->len < 1) { priv->wev = 0; } }