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();
}
예제 #2
0
/*============================================================================*
 *                                 Global                                     *
 *============================================================================*/
Efl_Egueb_IO_Request * efl_egueb_io_request_new(Egueb_Dom_String *location,
		const Efl_Egueb_IO_Request_Descriptor *descriptor, void *data)
{
	Efl_Egueb_IO_Request *thiz;
	const char *filename;


	if (!location) return NULL;
	filename = egueb_dom_string_chars_get(location);

	thiz = calloc(1, sizeof(Efl_Egueb_IO_Request));
	thiz->descriptor = descriptor;
	thiz->data = data;

	if (!strncmp(filename, "file://", 7))
	{
		Enesim_Stream *s;

		s = enesim_stream_file_new(filename + 7, "r");
		if (s)
		{
			DBG("Data '%s' loaded correctly", filename);
			thiz->in_event = EINA_TRUE;
			if (thiz->descriptor->completion)
				thiz->descriptor->completion(thiz, s);
			enesim_stream_unref(s);
			thiz->in_event = EINA_FALSE;
		}
	}
	else if (!strncmp(filename, "http://", 7))
	{
		thiz->conn = ecore_con_url_new(filename);
		thiz->binbuf = eina_binbuf_new();

		ecore_event_handler_add(ECORE_CON_EVENT_URL_COMPLETE,
				_efl_egueb_io_request_url_completion_cb,
				thiz);
		ecore_event_handler_add(ECORE_CON_EVENT_URL_DATA,
				_efl_egueb_io_request_url_data_cb,
				thiz);
		ecore_con_url_get(thiz->conn);
	}
	else
	{
		WRN("Unsupported schema '%s'", filename);
		free(thiz);
		thiz = NULL;
		goto done;
	}

	if (thiz->destroy)
	{
		efl_egueb_io_request_free(thiz);
		return NULL;		
	}
done:
	return thiz;
}
예제 #3
0
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
}
예제 #4
0
Elm_Url *
_elm_url_download(const char *url, Elm_Url_Done done_cb, Elm_Url_Cancel cancel_cb, Elm_Url_Progress progress_cb, const void *data)
{
   Ecore_Con_Url *target;
   Elm_Url *r;

   ecore_con_url_init();

   target = ecore_con_url_new(url);
   if (!target) goto on_error;

#if defined(HAVE_GETUID) && defined(HAVE_GETEUID)
   if (getuid() == geteuid())
#endif
     {
        if (getenv("http_proxy")) ecore_con_url_proxy_set(target, getenv("http_proxy"));
        if (getenv("https_proxy")) ecore_con_url_proxy_set(target, getenv("https_proxy"));
        if (getenv("ftp_proxy")) ecore_con_url_proxy_set(target, getenv("ftp_proxy"));
     }

   r = malloc(sizeof (Elm_Url));
   if (!r) goto on_error;

   r->url = eina_stringshare_add(url);
   r->cb.done = done_cb;
   r->cb.cancel = cancel_cb;
   r->cb.progress = progress_cb;
   r->data = data;

   r->download = eina_binbuf_new();
   r->target = target;
   r->handler.progress = ecore_event_handler_add(ECORE_CON_EVENT_URL_PROGRESS, _elm_url_progress, r);
   r->handler.done = ecore_event_handler_add(ECORE_CON_EVENT_URL_COMPLETE, _elm_url_done, r);
   r->handler.data = ecore_event_handler_add(ECORE_CON_EVENT_URL_DATA, _elm_url_data, r);

   if (!ecore_con_url_get(r->target))
     {
       _elm_url_free(r);
       cancel_cb((void*) data, NULL, -1);
       return NULL;
     }

   return r;

 on_error:
   ecore_con_url_shutdown();

   cancel_cb((void*) data, NULL, -1);
   return NULL;
}
예제 #5
0
파일: tag.c 프로젝트: Deepspeed/moksha
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;
}
예제 #6
0
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;
}
예제 #7
0
파일: pop_list.c 프로젝트: Limsik/e17
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;
}
예제 #8
0
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;
    }
}
예제 #9
0
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();
}
예제 #11
0
/**
 * @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;
}