END_TEST START_TEST(binbuf_manage_read_only_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 */ eina_init(); buf = eina_binbuf_manage_read_only_new_length(_cbuf, size); fail_if(!buf); eina_binbuf_free(buf); buf = eina_binbuf_manage_read_only_new_length(_cbuf, size); fail_if(!buf); fail_if(eina_binbuf_string_get(buf) != cbuf); 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(); }
void efl_egueb_io_request_free(Efl_Egueb_IO_Request *thiz) { /* just mark it for later */ if (thiz->in_event) { thiz->destroy = EINA_TRUE; thiz->in_event = EINA_FALSE; return; } if (thiz->data) { if (thiz->descriptor->free) thiz->descriptor->free(thiz); thiz->data = NULL; } if (thiz->binbuf) { eina_binbuf_free(thiz->data); thiz->data = NULL; } if (thiz->conn) { ecore_con_url_free(thiz->conn); thiz->conn = NULL; } free(thiz); }
void cserve2_client_deliver(Client *client) { size_t sent, size; const char *str; if (!client->msg.pending) { Fd_Flags cur_flags; cserve2_fd_watch_flags_get(client->socket, &cur_flags); cur_flags ^= FD_WRITE; cserve2_fd_watch_flags_set(client->socket, cur_flags); return; } size = eina_binbuf_length_get(client->msg.pending); str = (const char *)eina_binbuf_string_get(client->msg.pending); sent = cserve2_client_write(client, str, size); if (sent == size) { eina_binbuf_free(client->msg.pending); client->msg.pending = NULL; return; } eina_binbuf_remove(client->msg.pending, 0, sent); }
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(); }
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 #pragma GCC diagnostic pop START_TEST(binbuf_insert) { #if 0 Eina_Binbuf *buf; eina_init(); buf = eina_binbuf_new(); fail_if(!buf); eina_binbuf_insert(buf, "abc", 10); fail_if(strlen(eina_binbuf_string_get(buf)) != eina_binbuf_length_get(buf)); fail_if(strcmp(eina_binbuf_string_get(buf), "abc")); eina_binbuf_insert(buf, "123", 0); fail_if(strlen(eina_binbuf_string_get(buf)) != eina_binbuf_length_get(buf)); fail_if(strcmp(eina_binbuf_string_get(buf), "123abc")); eina_binbuf_insert(buf, "xyz", eina_binbuf_length_get(buf)); fail_if(strlen(eina_binbuf_string_get(buf)) != eina_binbuf_length_get(buf)); fail_if(strcmp(eina_binbuf_string_get(buf), "123abcxyz")); eina_binbuf_insert(buf, "xyz", 1); fail_if(strlen(eina_binbuf_string_get(buf)) != eina_binbuf_length_get(buf)); fail_if(strcmp(eina_binbuf_string_get(buf), "1xyz23abcxyz")); eina_binbuf_insert_n(buf, "ABCDEF", 2, 1); fail_if(strlen(eina_binbuf_string_get(buf)) != eina_binbuf_length_get(buf)); fail_if(strcmp(eina_binbuf_string_get(buf), "1ABxyz23abcxyz")); eina_binbuf_insert_n(buf, "EINA", 2, 3); fail_if(strlen(eina_binbuf_string_get(buf)) != eina_binbuf_length_get(buf)); fail_if(strcmp(eina_binbuf_string_get(buf), "1ABEIxyz23abcxyz")); eina_binbuf_insert_escaped(buf, "678", 3); fail_if(strlen(eina_binbuf_string_get(buf)) != eina_binbuf_length_get(buf)); fail_if(strncmp(eina_binbuf_string_get(buf) + 3, "678", 3)); eina_binbuf_insert_escaped(buf, "089 '\\", 9); fail_if(strlen(eina_binbuf_string_get( buf)) != eina_binbuf_length_get(buf)); fail_if(strncmp(eina_binbuf_string_get(buf) + 9, "089\\ \\'\\\\", strlen("089\\ \\'\\\\"))); eina_binbuf_reset(buf); eina_binbuf_free(buf); eina_shutdown(); #endif }
static void _elm_url_free(Elm_Url *r) { ecore_con_url_free(r->target); eina_binbuf_free(r->download); ecore_event_handler_del(r->handler.progress); ecore_event_handler_del(r->handler.done); ecore_event_handler_del(r->handler.data); eina_stringshare_del(r->url); free(r); }
Eet_Error eet_decipher(const void *data, unsigned int size, const char *key, unsigned int length, void **result, unsigned int *result_length) { Eina_Binbuf *out; Eina_Binbuf *in; in = eina_binbuf_manage_new(data, size, EINA_TRUE); out = emile_binbuf_decipher(EMILE_AES256_CBC, in, key, length); if (result_length) *result_length = out ? eina_binbuf_length_get(out) : 0; if (result) *result = out ? eina_binbuf_string_steal(out) : NULL; eina_binbuf_free(out); eina_binbuf_free(in); return out ? EET_ERROR_NONE : EET_ERROR_DECRYPT_FAILED; }
Eina_Bool ecore_con_local_win32_client_flush(Ecore_Con_Client *cl) { Ecore_Con_Type type; int num; BOOL res; DWORD written; type = cl->host_server->type & ECORE_CON_TYPE; /* This check should never be true */ if (type == ECORE_CON_LOCAL_ABSTRACT) return EINA_TRUE; if ((type != ECORE_CON_LOCAL_USER) && (type != ECORE_CON_LOCAL_SYSTEM)) return EINA_FALSE; num = eina_binbuf_length_get(cl->buf) - cl->buf_offset; if (num <= 0) return EINA_TRUE; res = WriteFile(cl->host_server->pipe, eina_binbuf_string_get(cl->buf) + cl->buf_offset, num, &written, NULL); if (!res) { char *msg; msg = evil_last_error_get(); if (msg) { ecore_con_event_client_error(cl, msg); free(msg); } if (!cl->delete_me) ecore_con_event_client_del(cl); cl->dead = EINA_TRUE; } cl->buf_offset += written; if (cl->buf_offset >= eina_binbuf_length_get(cl->buf)) { cl->buf_offset = 0; eina_binbuf_free(cl->buf); cl->buf = NULL; cl->host_server->want_write = 0; } else if (written < (DWORD)num) cl->host_server->want_write = 1; return EINA_TRUE; }
Eina_Bool ecore_con_local_win32_client_flush(Ecore_Con_Client *obj) { Ecore_Con_Client_Data *cl = eo_data_scope_get(obj, ECORE_CON_CLIENT_CLASS); Ecore_Con_Type type; size_t num; BOOL res; DWORD written; Ecore_Con_Server_Data *svr = eo_data_scope_get(cl->host_server, ECORE_CON_SERVER_CLASS); type = svr->type & ECORE_CON_TYPE; /* This check should never be true */ if (type == ECORE_CON_LOCAL_ABSTRACT) return EINA_TRUE; if ((type != ECORE_CON_LOCAL_USER) && (type != ECORE_CON_LOCAL_SYSTEM)) return EINA_FALSE; num = eina_binbuf_length_get(cl->buf) - cl->buf_offset; if (num <= 0) return EINA_TRUE; res = WriteFile(svr->pipe, eina_binbuf_string_get(cl->buf) + cl->buf_offset, num, &written, NULL); if (!res) { char *msg; msg = evil_last_error_get(); if (msg) { ecore_con_event_client_error(obj, msg); free(msg); } _ecore_con_client_kill(obj); } cl->buf_offset += written; if (cl->buf_offset >= eina_binbuf_length_get(cl->buf)) { cl->buf_offset = 0; eina_binbuf_free(cl->buf); cl->buf = NULL; svr->want_write = 0; } else if (written < (DWORD)num) svr->want_write = 1; return EINA_TRUE; }
Eina_Bool ecore_con_local_win32_server_flush(Ecore_Con_Server *svr) { int num; BOOL res; DWORD written; /* This check should never be true */ if ((svr->type & ECORE_CON_TYPE) == ECORE_CON_LOCAL_ABSTRACT) return EINA_TRUE; if (((svr->type & ECORE_CON_TYPE) != ECORE_CON_LOCAL_USER) && ((svr->type & ECORE_CON_TYPE) != ECORE_CON_LOCAL_SYSTEM)) return EINA_FALSE; num = eina_binbuf_length_get(svr->buf) - svr->write_buf_offset; if (num <= 0) return EINA_TRUE; res = WriteFile(svr->pipe, eina_binbuf_string_get(svr->buf) + svr->write_buf_offset, num, &written, NULL); if (!res) { char *msg; msg = evil_last_error_get(); if (msg) { ecore_con_event_server_error(svr, msg); free(msg); } if (!svr->delete_me) ecore_con_event_server_del(svr); svr->dead = EINA_TRUE; } svr->write_buf_offset += written; if (svr->write_buf_offset >= eina_binbuf_length_get(svr->buf)) { svr->write_buf_offset = 0; eina_binbuf_free(svr->buf); svr->buf = NULL; svr->want_write = 0; } else if (written < (DWORD)num) svr->want_write = 1; return EINA_TRUE; }
static void _slave_write_cb(int fd __UNUSED__, Fd_Flags flags __UNUSED__, void *data) { Slave_Proc *s = data; size_t sent; size_t size; const char *str; size = eina_binbuf_length_get(s->pending); str = (const char *)eina_binbuf_string_get(s->pending); sent = _slave_write(s, str, size); if (sent == size) { eina_binbuf_free(s->pending); s->pending = NULL; cserve2_fd_watch_del(s->write_fd); return; } eina_binbuf_remove(s->pending, 0, sent); }
Eina_Bool ecore_con_local_win32_server_flush(Ecore_Con_Server *obj) { Efl_Network_Server_Data *svr = efl_data_scope_get(obj, EFL_NETWORK_SERVER_CLASS); size_t num; BOOL res; DWORD written; /* This check should never be true */ if ((svr->type & ECORE_CON_TYPE) == ECORE_CON_LOCAL_ABSTRACT) return EINA_TRUE; if (((svr->type & ECORE_CON_TYPE) != ECORE_CON_LOCAL_USER) && ((svr->type & ECORE_CON_TYPE) != ECORE_CON_LOCAL_SYSTEM)) return EINA_FALSE; num = eina_binbuf_length_get(svr->buf) - svr->write_buf_offset; if (num == 0) return EINA_TRUE; res = WriteFile(svr->pipe, eina_binbuf_string_get(svr->buf) + svr->write_buf_offset, num, &written, NULL); if (!res) { ecore_con_event_server_error(obj, evil_last_error_get()); _ecore_con_server_kill(obj); } svr->write_buf_offset += written; if (svr->write_buf_offset >= eina_binbuf_length_get(svr->buf)) { svr->write_buf_offset = 0; eina_binbuf_free(svr->buf); svr->buf = NULL; svr->want_write = 0; } else if (written < (DWORD)num) svr->want_write = 1; return EINA_TRUE; }
static void _slave_free(Slave_Proc *s) { if (s->write_fd) close(s->write_fd); if (s->read_fd) { cserve2_fd_watch_del(s->read_fd); close(s->read_fd); } free(s->read.buf); if (s->pending) eina_binbuf_free(s->pending); if (s->dead_cb) s->dead_cb(s, (void *)s->data); eina_stringshare_del(s->name); free(s); }
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; }
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; }
/** * @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; }
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(); }