END_TEST START_TEST(binbuf_manage_simple) { Eina_Binbuf *buf; const char *_cbuf = "12\0 456 78\0 abcthis is some more random junk here!"; const unsigned char *cbuf = (const unsigned char *) _cbuf; size_t size = sizeof(cbuf) - 1; /* We don't care about the real NULL */ unsigned char *alloc_buf = malloc(size); memcpy(alloc_buf, cbuf, size); eina_init(); buf = eina_binbuf_manage_new_length(alloc_buf, size); fail_if(!buf); fail_if(memcmp(eina_binbuf_string_get(buf), cbuf, size)); fail_if(size != eina_binbuf_length_get(buf)); eina_binbuf_append_length(buf, cbuf, size); fail_if(memcmp(eina_binbuf_string_get(buf), cbuf, size)); fail_if(memcmp(eina_binbuf_string_get(buf) + size, cbuf, size)); fail_if(2 * size != eina_binbuf_length_get(buf)); eina_binbuf_free(buf); eina_shutdown(); }
END_TEST START_TEST(binbuf_remove) { Eina_Binbuf *buf; const unsigned char cbuf[] = "12\0 456 78\0 abcthis is some more random junk here!"; size_t size = sizeof(cbuf) - 1; /* We don't care about the real NULL */ eina_init(); buf = eina_binbuf_new(); fail_if(!buf); eina_binbuf_append_length(buf, cbuf, size); fail_if(size != eina_binbuf_length_get(buf)); eina_binbuf_remove(buf, 0, 4); fail_if(size - 4 != eina_binbuf_length_get(buf)); eina_binbuf_remove(buf, 8, 1000); fail_if(8 != eina_binbuf_length_get(buf)); eina_binbuf_remove(buf, 7, eina_binbuf_length_get(buf)); fail_if(7 != eina_binbuf_length_get(buf)); eina_binbuf_remove(buf, 2, 4); fail_if(5 != eina_binbuf_length_get(buf)); eina_binbuf_remove(buf, 4, 1); fail_if(5 != eina_binbuf_length_get(buf)); eina_binbuf_remove(buf, 0, eina_binbuf_length_get(buf)); fail_if(0 != eina_binbuf_length_get(buf)); eina_binbuf_free(buf); eina_shutdown(); }
END_TEST /* eina_binbuf_manage_new_length and eina_binbuf_manage_read_only_new_length * are deprecated */ #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wdeprecated-declarations" START_TEST(binbuf_manage_simple) { Eina_Binbuf *buf; const char *_cbuf = "12\0 456 78\0 abcthis is some more random junk here!"; const unsigned char *cbuf = (const unsigned char *) _cbuf; size_t size = sizeof(cbuf) - 1; /* We don't care about the real NULL */ unsigned char *alloc_buf = malloc(size); memcpy(alloc_buf, cbuf, size); eina_init(); buf = eina_binbuf_manage_new_length(alloc_buf, size); fail_if(!buf); fail_if(memcmp(eina_binbuf_string_get(buf), cbuf, size)); fail_if(size != eina_binbuf_length_get(buf)); eina_binbuf_append_length(buf, cbuf, size); fail_if(memcmp(eina_binbuf_string_get(buf), cbuf, size)); fail_if(memcmp(eina_binbuf_string_get(buf) + size, cbuf, size)); fail_if(2 * size != eina_binbuf_length_get(buf)); eina_binbuf_free(buf); eina_shutdown(); }
/*============================================================================* * Local * *============================================================================*/ static Eina_Bool _efl_egueb_io_request_url_data_cb(void *data, int type EINA_UNUSED, void *event) { Efl_Egueb_IO_Request *thiz = data; Ecore_Con_Event_Url_Data *ev = event; if (ev->url_con != thiz->conn) return EINA_TRUE; if (ev->size > 0) eina_binbuf_append_length(thiz->binbuf, ev->data, ev->size); return EINA_TRUE; }
static Eina_Bool _elm_url_data(void *data, int type EINA_UNUSED, void *event) { Ecore_Con_Event_Url_Data *url_data = event; Elm_Url *r = data; if (url_data->url_con != r->target) return EINA_TRUE; eina_binbuf_append_length(r->download, url_data->data, url_data->size); return EINA_TRUE; }
ssize_t cserve2_client_send(Client *client, const void *data, size_t size) { ssize_t sent; debug_msg("sent", data, size); if (client->msg.pending) { eina_binbuf_append_length (client->msg.pending, (unsigned char *)data, size); return size; } sent = cserve2_client_write(client, data, size); if ((sent < 0) && ((errno != EAGAIN) && (errno != EWOULDBLOCK))) { // FIXME: Big error when writing on the socket to the client, // so we must close the connection to the client and remove // its references inside our cache. WRN("Error on socket with client %d: %s", client->id, strerror(errno)); if (client->msg.reading) _client_msg_free(client); cserve2_client_del(client); return sent; } if (sent < 0) sent = 0; if (sent < (int)size) { Fd_Flags cur_flags; client->msg.pending = eina_binbuf_new(); eina_binbuf_append_length (client->msg.pending, (unsigned char *)data + sent, size - sent); cserve2_fd_watch_flags_get(client->socket, &cur_flags); cur_flags |= FD_WRITE; cserve2_fd_watch_flags_set(client->socket, cur_flags); } return size; }
uint8_t * untag_arbitrary(Pulse_Tag *tag, Eina_Binbuf **val) { uint8_t *ret; uint32_t len; if (!untag_uint32(tag, &len)) return 0; ret = tag->data + tag->size; if (*ret != PA_TAG_ARBITRARY) return 0; ret += PA_TAG_SIZE_ARBITRARY; *val = eina_binbuf_new(); eina_binbuf_append_length(*val, ret, len); ret += len; tag->size = ret - tag->data; return ret; }
Eina_Bool email_pop3_list_read(Email *e, Ecore_Con_Event_Server_Data *ev) { Email_List_Cb cb; Eina_List *list = NULL; Email_List_Item *it; const char *p, *n; const unsigned char *data; size_t size; if ((!e->buf) && (!email_op_ok(ev->data, ev->size))) { ERR("Error with LIST"); cb = eina_list_data_get(e->cbs); if (cb) cb(e, NULL); return EINA_TRUE; } if (e->buf) { eina_binbuf_append_length(e->buf, ev->data, ev->size); data = eina_binbuf_string_get(e->buf); size = eina_binbuf_length_get(e->buf); } else { data = ev->data; size = ev->size; } for (n = (char*)memchr(data + 3, '\n', size - 3), size -= (n - (char*)data); n && (size > 1); p = n, n = (char*)memchr(n, '\n', size - 1), size -= (n - (char*)data)) { it = calloc(1, sizeof(Email_List_Item)); if (sscanf(++n, "%u %zu", &it->id, &it->size) != 2) { free(it); break; } INF("Message %u: %zu octets", it->id, it->size); list = eina_list_append(list, it); } if (n[0] == '.') { cb = eina_list_data_get(e->cbs); INF("LIST returned %u messages", eina_list_count(list)); if (cb) cb(e, list); EINA_LIST_FREE(list, it) free(it); if (e->buf) { eina_binbuf_free(e->buf); e->buf = NULL; } } else if (!e->buf) { e->buf = eina_binbuf_new(); eina_binbuf_append_length(e->buf, (const unsigned char*)(n), size - (n - (char*)data)); } return EINA_TRUE; }
void email_login_pop(Email *e, Ecore_Con_Event_Server_Data *ev) { char *buf; size_t size; switch (e->state) { case EMAIL_STATE_SSL: if (!email_op_ok(ev->data, ev->size)) { ERR("Could not create secure connection!"); ecore_con_server_del(ev->server); return; } ecore_con_ssl_server_upgrade(e->svr, ECORE_CON_USE_MIXED); ecore_con_ssl_server_verify_basic(e->svr); e->flags = ECORE_CON_USE_MIXED; return; case EMAIL_STATE_INIT: if (!email_op_ok(ev->data, ev->size)) { ERR("Not a POP3 server!"); ecore_con_server_del(ev->server); return; } if (ev->size > 20) { const unsigned char *end; end = memrchr(ev->data + 3, '>', ev->size - 3); if (end) { const unsigned char *start; start = memrchr(ev->data + 3, '<', end - (unsigned char*)ev->data); if (start) { e->features.pop_features.apop = EINA_TRUE; e->features.pop_features.apop_str = eina_binbuf_new(); eina_binbuf_append_length(e->features.pop_features.apop_str, start, end - start + 1); } } } if (e->secure && (!e->flags)) { email_write(e, "STLS\r\n", sizeof("STLS\r\n") - 1); e->state++; return; } e->state = EMAIL_STATE_USER; ev = NULL; case EMAIL_STATE_USER: if (!ev) { unsigned char digest[16]; char md5buf[33]; if (!e->features.pop_features.apop) { INF("Beginning AUTH PLAIN"); size = sizeof(char) * (sizeof("USER ") - 1 + sizeof("\r\n") - 1 + strlen(e->username)) + 1; buf = alloca(size); snprintf(buf, size, "USER %s\r\n", e->username); email_write(e, buf, size - 1); return; } INF("Beginning AUTH APOP"); e->state++; eina_binbuf_append_length(e->features.pop_features.apop_str, (unsigned char*)e->password, strlen(e->password)); md5_buffer((char*)eina_binbuf_string_get(e->features.pop_features.apop_str), eina_binbuf_length_get(e->features.pop_features.apop_str), digest); email_md5_digest_to_str(digest, md5buf); size = sizeof(char) * (sizeof("APOP ") - 1 + sizeof("\r\n") - 1 + strlen(e->username)) + sizeof(md5buf); buf = alloca(size); snprintf(buf, size, "APOP %s %s\r\n", e->username, md5buf); email_write(e, buf, size - 1); return; } if (!email_op_ok(ev->data, ev->size)) { ERR("Username invalid!"); ecore_con_server_del(e->svr); return; } size = sizeof(char) * (sizeof("PASS ") - 1 + sizeof("\r\n") - 1 + strlen(e->password)) + 1; buf = alloca(size); snprintf(buf, size, "PASS %s\r\n", e->password); DBG("Sending password"); ecore_con_server_send(e->svr, buf, size - 1); e->state++; return; case EMAIL_STATE_PASS: if (!email_op_ok(ev->data, ev->size)) { ERR("Credentials invalid!"); ecore_con_server_del(e->svr); return; } INF("Logged in successfully!"); e->state++; ecore_event_add(EMAIL_EVENT_CONNECTED, e, (Ecore_End_Cb)email_fake_free, NULL); default: break; } }
Eina_Bool email_pop3_list_read(Email *e, Ecore_Con_Event_Server_Data *ev) { Email_List_Cb cb; Eina_List *next, *list = NULL; Email_List_Item *it; const char *n; unsigned char *data; int len; size_t size; if ((!e->buf) && (!email_op_ok(ev->data, ev->size))) { ERR("Error with LIST"); cb = e->cbs->data; e->cbs = eina_list_remove_list(e->cbs, e->cbs); if (cb) cb(e, NULL); return EINA_TRUE; } next = e->ev ? e->ev : list; if (e->buf) { eina_binbuf_append_length(e->buf, ev->data, ev->size); data = (unsigned char*)eina_binbuf_string_get(e->buf); len = eina_binbuf_length_get(e->buf); } else { data = ev->data; len = ev->size; } for (n = (char*)memchr(data + 3, '\n', len - 3), size = len - (n - (char*)data); n && (size > 1); n = (char*)memchr(n, '\n', size - 1), size = len - (n - (char*)data)) { it = calloc(1, sizeof(Email_List_Item)); if (sscanf(++n, "%u %zu", &it->id, &it->size) != 2) { free(it); break; } INF("Message %u: %zu octets", it->id, it->size); list = eina_list_append(list, it); } if (!memcmp(n - 2, "\r\n.\r\n", 5)) { cb = e->cbs->data; e->cbs = eina_list_remove_list(e->cbs, e->cbs); INF("LIST returned %u messages", eina_list_count(list)); if (cb) cb(e, list); EINA_LIST_FREE(list, it) free(it); if (e->buf) { eina_binbuf_free(e->buf); e->buf = NULL; } return EINA_TRUE; } else if (!e->buf) { e->buf = eina_binbuf_new(); eina_binbuf_append_length(e->buf, (unsigned char*)n, ev->size - (n - (char*)ev->data)); } return EINA_FALSE; }
END_TEST START_TEST(binbuf_realloc) { Eina_Binbuf *buf; unsigned char pattern[1024 * 16]; unsigned int i; size_t sz; for (i = 0; i < sizeof(pattern) - 1; i++) { if (i % 27 == 26) pattern[i] = '\0'; else pattern[i] = 'a' + (i % 27); } pattern[i] = '\0'; eina_init(); buf = eina_binbuf_new(); fail_if(!buf); sz = 0; eina_binbuf_append_length(buf, pattern, 1); fail_if(eina_binbuf_length_get(buf) != sz + 1); fail_if(memcmp(eina_binbuf_string_get(buf) + sz, pattern, 1)); sz += 1; eina_binbuf_append_length(buf, pattern, 32); fail_if(eina_binbuf_length_get(buf) != sz + 32); fail_if(memcmp(eina_binbuf_string_get(buf) + sz, pattern, 32)); sz += 32; eina_binbuf_append_length(buf, pattern, 64); fail_if(eina_binbuf_length_get(buf) != sz + 64); fail_if(memcmp(eina_binbuf_string_get(buf) + sz, pattern, 64)); sz += 64; eina_binbuf_append_length(buf, pattern, 128); fail_if(eina_binbuf_length_get(buf) != sz + 128); fail_if(memcmp(eina_binbuf_string_get(buf) + sz, pattern, 128)); sz += 128; eina_binbuf_append_length(buf, pattern, 4096); fail_if(eina_binbuf_length_get(buf) != sz + 4096); fail_if(memcmp(eina_binbuf_string_get(buf) + sz, pattern, 4096)); sz += 4096; eina_binbuf_append_length(buf, pattern, sizeof(pattern) - 1); fail_if(eina_binbuf_length_get(buf) != sz + sizeof(pattern) - 1); fail_if(memcmp(eina_binbuf_string_get(buf) + sz, pattern, sizeof(pattern) - 1)); sz += sizeof(pattern) - 1; eina_binbuf_remove(buf, 1024, 1024 + 1234); fail_if(eina_binbuf_length_get(buf) != sz - 1234); sz -= 1234; eina_binbuf_remove(buf, 0, 0 + 8192); fail_if(eina_binbuf_length_get(buf) != sz - 8192); sz -= 8192; eina_binbuf_remove(buf, 0, 0 + 32); fail_if(eina_binbuf_length_get(buf) != sz - 32); sz -= 32; eina_binbuf_free(buf); eina_shutdown(); }
/** * @brief Send data to a client * * This function is used to queue arbitrary data to send to a client through its module. It will automatically * generate all http header strings from @p net (if provided) including the content-length (based on @p data). * @param module The client's #Azy_Server_Module object (NOT NULL) * @param net An #Azy_Net object containing http information to use * @param data The data to send * @return EINA_TRUE on success, else EINA_FALSE */ Eina_Bool azy_server_module_send(Azy_Server_Module *module, Azy_Net *net, const Azy_Net_Data *data) { Eina_Strbuf *header; char chunk_size[20]; Eina_Binbuf *chunk_data; Eina_Bool nullify = EINA_FALSE; if (!AZY_MAGIC_CHECK(module, AZY_MAGIC_SERVER_MODULE)) { AZY_MAGIC_FAIL(module, AZY_MAGIC_SERVER_MODULE); return EINA_FALSE; } if (net) { if (!module->client->current) { module->client->current = net; nullify = EINA_TRUE; } if (net->headers_sent) goto post_header; Eina_Bool s; if ((data) && (net->http.transfer_encoding != AZY_NET_TRANSFER_ENCODING_CHUNKED)) azy_net_content_length_set(net, data->size); if (!net->http.res.http_code) azy_net_code_set(net, 200); /* OK */ azy_net_type_set(net, AZY_NET_TYPE_RESPONSE); EINA_SAFETY_ON_TRUE_RETURN_VAL(!(header = azy_net_header_create(net)), EINA_FALSE); EINA_SAFETY_ON_NULL_RETURN_VAL(module->client->current->conn, EINA_FALSE); s = !!ecore_con_client_send(module->client->current->conn, eina_strbuf_string_get(header), eina_strbuf_length_get(header)); eina_strbuf_free(header); if (!s) { ERR("Could not queue header for sending!"); return EINA_FALSE; } net->headers_sent = EINA_TRUE; } post_header: if ((!net) || (net->http.transfer_encoding != AZY_NET_TRANSFER_ENCODING_CHUNKED)) { if (!data || !data->data) return EINA_TRUE; EINA_SAFETY_ON_NULL_RETURN_VAL(module->client->current->conn, EINA_FALSE); EINA_SAFETY_ON_TRUE_RETURN_VAL(!ecore_con_client_send(module->client->current->conn, data->data, data->size), EINA_FALSE); goto post_send; } if (!data || !data->data) { EINA_SAFETY_ON_NULL_RETURN_VAL(module->client->current->conn, EINA_FALSE); EINA_SAFETY_ON_TRUE_RETURN_VAL(!ecore_con_client_send(module->client->current->conn, "0\r\n\r\n", 5), EINA_FALSE); goto post_send; } net->refcount++; sprintf((char *)chunk_size, "%" PRIx64 "\r\n", data->size); chunk_data = eina_binbuf_new(); eina_binbuf_append_length(chunk_data, (unsigned char *)chunk_size, strlen(chunk_size)); eina_binbuf_append_length(chunk_data, data->data, data->size); eina_binbuf_append_length(chunk_data, (unsigned char *)"\r\n", 2); EINA_SAFETY_ON_NULL_RETURN_VAL(module->client->current->conn, EINA_FALSE); EINA_SAFETY_ON_TRUE_RETURN_VAL(!ecore_con_client_send(module->client->current->conn, eina_binbuf_string_get(chunk_data), eina_binbuf_length_get(chunk_data)), EINA_FALSE); eina_binbuf_free(chunk_data); post_send: if (nullify) module->client->current = NULL; return EINA_TRUE; }