Beispiel #1
0
/* Create a new slowlog entry.
 * Incrementing the ref count of all the objects retained is up to
 * this function. */
static slowlog_entry *slowlog_create_entry(struct msg *r, long long duration) {
    slowlog_entry *se = nc_alloc(sizeof(*se));
    uint32_t j, keys_count, keys_count_input;

    se->cmdtype = r->type;

    keys_count_input = keys_count = r->keys==NULL?0:array_n(r->keys);

    if (keys_count_input > SLOWLOG_ENTRY_MAX_KEYS) keys_count_input = SLOWLOG_ENTRY_MAX_KEYS;
    se->keys_count = (int)keys_count;
    if (keys_count_input > 0) {
        se->keys = array_create(keys_count_input, sizeof(struct string));
        for (j = 0; j < keys_count_input; j ++) {
            struct keypos *kp = array_get(r->keys, j);
            struct string *key = array_push(se->keys);
            uint32_t key_len = (uint32_t)(kp->end-kp->start);
            string_init(key);
            if (key_len > SLOWLOG_ENTRY_MAX_STRING) {
                int len;
                uint8_t buf[SLOWLOG_ENTRY_MAX_STRING+50];
                memcpy(buf,kp->start,SLOWLOG_ENTRY_MAX_STRING);
                len = nc_scnprintf(buf+SLOWLOG_ENTRY_MAX_STRING,
                    50,"... (%lu more bytes)",
                    key_len-SLOWLOG_ENTRY_MAX_STRING);            
                if (len > 0) {
                    string_copy(key,buf,SLOWLOG_ENTRY_MAX_STRING+(uint32_t)len);
                } else {
                    string_copy(key,kp->start,SLOWLOG_ENTRY_MAX_STRING);
                }
            } else {
                string_copy(key,kp->start,key_len);
            }
        }
    }else {
        se->keys = NULL;
    }
    
    se->time = time(NULL);
    se->duration = duration;

    STAILQ_NEXT(se, next) = NULL;
    
    return se;
}
Beispiel #2
0
/*
 * input : aname(STR) [asn(STR) masn(STR) email(STR) state(INT)]
 * return: NORMAL REP_ERR_ALREADYREGIST
 * reply : NULL
 */
static NEOERR* aic_cmd_appup(struct queue_entry *q, struct cache *cd, mdb_conn *db)
{
    char *aname;
    int aid, pid = 0, tune = -1;
    STRING str;    string_init(&str);
    NEOERR *err;
    
    REQ_GET_PARAM_STR(q->hdfrcv, "aname", aname);
    REQ_FETCH_PARAM_INT(q->hdfrcv, "tune", tune);
    if (tune != -1) {
        if (hdf_get_int_value(q->hdfrcv, "tuneop", 0)) {
            /* set tune bit */
            string_appendf(&str, " tune=tune | %d ", tune);
        } else {
            /* unset tune bit */
            string_appendf(&str, " tune=tune & %d ", ~tune);
        }
    }

    aid = hash_string(aname);
    err = aic_cmd_appinfo(q, cd, db);
    if (err != STATUS_OK) return nerr_pass(err);

    if (!hdf_get_obj(q->hdfsnd, "state"))
        return nerr_raise(REP_ERR_NREGIST, "%s %d hasn't regist", aname, aid);
    pid = hdf_get_int_value(q->hdfsnd, "pid", 0);

    err = mcs_build_upcol(q->hdfrcv,
                          hdf_get_obj(g_cfg, CONFIG_PATH".UpdateCol.appinfo"), &str);
    if (err != STATUS_OK) return nerr_pass(err);
    
    MDB_EXEC(db, NULL, "UPDATE appinfo SET %s WHERE aid=%d;", NULL, str.buf, aid);

    /* TODO memory leak, if exec() failure */
    string_clear(&str);
    
    cache_delf(cd, PREFIX_APPINFO"%d", aid);
    if (pid > 0) {
        cache_delf(cd, PREFIX_APPOUSER"%d_0", pid);
    }

    return STATUS_OK;
}
Beispiel #3
0
PyObject * p_neo_error (NEOERR *err)
{
  STRING str;

  string_init (&str);
  if (nerr_match(err, NERR_PARSE))
  {
    nerr_error_string (err, &str);
    PyErr_SetString (NeoParseError, str.buf);
  }
  else
  {
    nerr_error_traceback (err, &str);
    PyErr_SetString (NeoError, str.buf);
  }
  string_clear (&str);
  nerr_ignore(&err);
  return NULL;
}
Beispiel #4
0
static NEOERR* aux_cmd_memoryadd(struct aux_entry *e, QueueEntry *q)
{
	STRING str; string_init(&str);
	NEOERR *err;
    
    mdb_conn *db = e->db;

    err = mdb_build_incol(q->hdfrcv,
                          hdf_get_obj(g_cfg, CONFIG_PATH".InsertCol.memory"), &str);
	if (err != STATUS_OK) return nerr_pass(err);
    
    MDB_EXEC(db, NULL, "INSERT INTO memory %s", NULL, str.buf);

    string_clear(&str);

    m_memory_maxid++;

    return STATUS_OK;
}
Beispiel #5
0
NEOERR* pos_data_mod(CGI *cgi, HASH *dbh, HASH *evth, session_t *ses)
{
	STRING str; string_init(&str);
    mdb_conn *db = hash_lookup(dbh, "city");
	NEOERR *err;

    if (!cgi || !cgi->hdf || !db) return nerr_raise(NERR_ASSERT, "paramter null");

    int id = hdf_get_int_value(cgi->hdf, PRE_QUERY".id", 0);
    
    err = mdb_build_upcol(hdf_get_obj(cgi->hdf, PRE_QUERY),
                          hdf_get_obj(g_cfg, "Db.UpdateCol.city"), &str);
	if (err != STATUS_OK) return nerr_pass(err);

    MDB_EXEC(db, NULL, "UPDATE city SET %s WHERE id=%d;", NULL, str.buf, id);
    

    return STATUS_OK;
}
NEOERR *ne_net_read_line(NSOCK *sock, char **buf)
{
  NEOERR *err;
  STRING str;
  UINT8 *nl;
  int l;
  
  string_init(&str);

  while (1)
  {
    if (sock->il - sock->ib > 0)
    {
      nl = memchr(sock->ibuf + sock->ib, '\n', sock->il - sock->ib);
      if (nl == NULL)
      {
	l = sock->il - sock->ib;
	err = string_appendn(&str, (char *)(sock->ibuf + sock->ib), l);
	sock->ib += l;
	if (err) break;
      }
      else
      {
	l = nl - (sock->ibuf + sock->ib);
	err = string_appendn(&str, (char *)(sock->ibuf + sock->ib), l);
	sock->ib += l;
	if (err) break;

	*buf = str.buf;
	return STATUS_OK;
      }
    }
    else
    {
      err = ne_net_fill(sock);
      if (err) break;
      if (sock->il == 0) return STATUS_OK;
    }
  }
  string_clear(&str);
  return nerr_pass(err);
}
Beispiel #7
0
static PyObject * p_html_ws_strip (PyObject *self, PyObject *args)
{
  char *s;
  NEOERR *err;
  PyObject *rv;
  int len;
  int lvl;
  STRING html;

  if (!PyArg_ParseTuple(args, "s#i:htmlStrip(str, level)", &s, &len, &lvl))
    return NULL;

  string_init (&html);
  err = string_appendn (&html, s, len);
  if (err) return p_neo_error (err);
  cgi_html_ws_strip (&html, lvl);
  rv = Py_BuildValue ("s", html.buf);
  string_clear (&html);
  return rv;
}
Beispiel #8
0
static NEOERR* aux_cmd_mailadd(struct queue_entry *q, struct cache *cd, mdb_conn *db)
{
    STRING str; string_init(&str);
    char sum[LEN_MD5], *content;
    NEOERR *err;

    REQ_GET_PARAM_STR(q->hdfrcv, "content", content);
    mutil_md5_str(content, sum);
    hdf_set_value(q->hdfrcv, "checksum", sum);

    err = mcs_build_incol(q->hdfrcv,
                          hdf_get_obj(g_cfg, CONFIG_PATH".InsertCol.email"),
                          &str);
    if (err != STATUS_OK) return nerr_pass(err);

    MDB_EXEC(db, NULL, "INSERT INTO email %s", NULL, str.buf);
    string_clear(&str);

    return STATUS_OK;
}
Beispiel #9
0
static BINDING_FUNCTION(binding_toggle_input)
{
    if (input_size == 1) {
        input_size = MULTILINE_INPUT_SIZE;
        ncurses_input_update(line_index);
    } else {
        string_t s = string_init((""));
        char *p, *tmp;
        int i;

        for (i = 0; lines[i]; i++) {
            char *tmp;

            string_append(s, (tmp = wcs_to_normal(lines[i])));
            free_utf(tmp);
            if (lines[i + 1])
                string_append(s, ("\r\n"));
        }

        tmp = string_free(s, 0);

        add_to_history();

        input_size = 1;
        ncurses_input_update(0);

        for (p=tmp; *p && isspace(*p); p++);
        if (*p || config_send_white_lines)
            command_exec(window_current->target, window_current->session, tmp, 0);

        if (!tmp[0] || tmp[0] == '/' || !window_current->target)
            ncurses_typing_mod		= 1;
        else {
            ncurses_typing_win		= NULL;
            window_current->out_active	= 1;
        }

        curs_set(1);
        xfree(tmp);
    }
}
Beispiel #10
0
static NEOERR* aic_cmd_appuserup(struct queue_entry *q, struct cache *cd, mdb_conn *db)
{
    STRING str; string_init(&str);
    int uid, aid;
    NEOERR *err;

    REQ_GET_PARAM_INT(q->hdfrcv, "uid", uid);
    REQ_GET_PARAM_INT(q->hdfrcv, "aid", aid);

    err = mcs_build_upcol(q->hdfrcv,
                          hdf_get_obj(g_cfg, CONFIG_PATH".UpdateCol.userinfo"), &str);
    if (err != STATUS_OK) return nerr_pass(err);

    MDB_EXEC(db, NULL, "UPDATE userinfo SET %s WHERE uid=%d AND aid=%d;",
             NULL, str.buf, uid, aid);
    string_clear(&str);

    cache_delf(cd, PREFIX_USERLIST"%d", aid);
    
    return STATUS_OK;
}
Beispiel #11
0
JNIEXPORT jstring JNICALL Java_org_clearsilver_HDF__1dump(
    JNIEnv *env, jclass objClass,
    jint hdf_obj_ptr) {
  HDF *hdf = (HDF *)hdf_obj_ptr;
  NEOERR *err;
  STRING str;
  jstring retval;

  string_init(&str);
  err = hdf_dump_str(hdf, NULL, 0, &str);
  if (err != STATUS_OK) {
    // Throw an exception
    jNeoErr(env, err);
    retval = NULL;
  } else {
    retval =  (*env)->NewStringUTF(env,str.buf);
  }
  string_clear(&str);

  return retval;
}
Beispiel #12
0
void add_java_home_to_path(void)
{
	const char *java_home = get_java_home();
	struct string *new_path = string_init(32), *buffer;
	const char *env;

	if (!java_home)
		return;
	buffer = string_initf("%s/bin", java_home);
	if (dir_exists(buffer->buffer))
		string_append_path_list(new_path, buffer->buffer);
	string_setf(buffer, "%s/jre/bin", java_home);
	if (dir_exists(buffer->buffer))
		string_append_path_list(new_path, buffer->buffer);

	env = getenv("PATH");
	string_append_path_list(new_path, env ? env : get_ij_dir());
	setenv_or_exit("PATH", new_path->buffer, 1);
	string_release(buffer);
	string_release(new_path);
}
static int icq_offline_message_end(session_t *s, unsigned char *buf, int len, private_data_t **info) {
	/*
	 * SNAC(15,03)/0042 SRV_END_OF_OFFLINE_MSGS End-of-offline messages reply
	 *
	 * This is the last SNAC in server response to CLI_OFFLINE_MSGS_REQ SNAC(15,02)/003C.
	 * It doesn't contain message - it is only end_of_sequence marker.
	 */
	debug_function("icq_offline_message_end()\n");

	/* SNAC(15,02)/003E CLI_DELETE_OFFLINE_MSGS_REQ Delete offline messages request
	 *
	 * Client sends this SNAC when wants to delete offline messages from
	 * server. But first you should request them from server using
	 * SNAC(15,02)/003C. If you doesn't delete messages server will send them
	 * again after client request.
	 */
	string_t pkt = string_init(NULL);
	icq_makemetasnac(s, pkt, CLI_DELETE_OFFLINE_MSGS_REQ, 0, NULL, NULL);
	icq_send_pkt(s, pkt);

	return 0;
}
Beispiel #14
0
int main()
{
	string *s = string_init();
	string *o;
	string *subs;
	string *q = string_init_cstring("quit");
	
	string_read_line(s);
	
	while(string_cmp(s, q))
	{
		string_print(s);
		
		string_reset(s);
		string_read_line(s);
	}

	string_append(s, q);
	o = string_cat(s, q);
	string_print(s);
	string_print(o);
	string_append_cstring(o, "hello");
	string_append_char(o, 'J');
	string_print(o);
	subs = string_substring(o, 5, 6);

	printf("--\n");
	printf("%c\n", string_get(s, 5));
	printf("%d\n", string_index(o, 'o'));
	string_print(subs);
	printf("--\n");

	o = string_free(o);
	s = string_free(s);
	q = string_free(q);
	subs = string_free(subs);

	return 0;
}
Beispiel #15
0
static NEOERR* hdf_read_file_internal (HDF *hdf, const char *path,
                                       int include_handle)
{
  NEOERR *err;
  int lineno = 0;
  char fpath[PATH_BUF_SIZE];
  char *ibuf = NULL;
  const char *ptr = NULL;
  HDF *top = hdf->top;
  STRING line;

  string_init(&line);

  if (path == NULL)
    return nerr_raise(NERR_ASSERT, "Can't read NULL file");

  if (top->fileload)
  {
    err = top->fileload(top->fileload_ctx, hdf, path, &ibuf);
  }
  else
  {
    if (path[0] != '/')
    {
      err = hdf_search_path (hdf, path, fpath, PATH_BUF_SIZE);
      if (err != STATUS_OK) return nerr_pass(err);
      path = fpath;
    }

    err = ne_load_file (path, &ibuf);
  }
  if (err) return nerr_pass(err);

  ptr = ibuf;
  err = _hdf_read_string(hdf, &ptr, &line, path, &lineno, include_handle);
  free(ibuf);
  string_clear(&line);
  return nerr_pass(err);
}
Beispiel #16
0
NEOERR* hdf_read_file (HDF *hdf, const char *path)
{
  NEOERR *err;
  int lineno = 0;
  char fpath[_POSIX_PATH_MAX];
  char *ibuf = NULL;
  const char *ptr = NULL;
  HDF *top = hdf->top;
  STRING line;

  string_init(&line);

  if (path == NULL) 
    return nerr_raise(NERR_ASSERT, "Can't read NULL file");

  if (top->fileload)
  {
    err = top->fileload(top->fileload_ctx, hdf, path, &ibuf);
  }
  else
  {
    if (path[0] != '/')
    {
      err = hdf_search_path (hdf, path, fpath);
      if (err != STATUS_OK) return nerr_pass(err);
      path = fpath;
    }

    err = ne_load_file (path, &ibuf);
  }
  if (err) return nerr_pass(err);

  ptr = ibuf;
  err = _hdf_read_string(hdf, &ptr, &line, path, &lineno, INCLUDE_FILE);
  free(ibuf);
  string_clear(&line);
  return nerr_pass(err);
}
Beispiel #17
0
void do_cache_get(void) {
  struct string key;
  string_init(&key);
  read_into_string(&key);

  struct cache_entry *entry = cache_lookup(&key);
  if (entry == NULL) {
    write(STDOUT_FILENO, &kNotFound, sizeof(kNotFound));
    return;
  }

  write(STDOUT_FILENO, &kFound, sizeof(kFound));
  write_string(entry->value);

  --entry->lifetime;
  if (entry->lifetime <= 0) {
    // The cache entry is now expired.
    fprintf(stderr, "Destroying key\n");
    string_destroy(entry->key);
    fprintf(stderr, "Destroying value\n");
    string_destroy(entry->value);
  }
}
Beispiel #18
0
static NEOERR* aux_cmd_impadd(struct queue_entry *q, struct cache *cd, mdb_conn *db)
{
    STRING str; string_init(&str);
    NEOERR *err;
    char *aname;
    int aid;

    REQ_GET_PARAM_STR(q->hdfrcv, "aname", aname);
    aid = hash_string(aname);
    hdf_set_int_value(q->hdfrcv, "aid", aid);

    err = mcs_build_incol(q->hdfrcv,
                          hdf_get_obj(g_cfg, CONFIG_PATH".InsertCol.improve"),
                          &str);
    if (err != STATUS_OK) return nerr_pass(err);

    MDB_EXEC(db, NULL, "INSERT INTO improve %s", NULL, str.buf);
    string_clear(&str);

    cache_delf(cd, PREFIX_IMP"%d_0", aid);
    
    return STATUS_OK;
}
Beispiel #19
0
static NEOERR* aux_cmd_memorymod(struct aux_entry *e, QueueEntry *q)
{
	STRING str; string_init(&str);
    int id;
	NEOERR *err;

    mdb_conn *db = e->db;
    struct cache *cd = e->cd;

    REQ_GET_PARAM_INT(q->hdfrcv, "id", id);
    
    err = mdb_build_upcol(q->hdfrcv,
                          hdf_get_obj(g_cfg, CONFIG_PATH".UpdateCol.memory"), &str);
	if (err != STATUS_OK) return nerr_pass(err);

    MDB_EXEC(db, NULL, "UPDATE memory SET %s WHERE id=%d;", NULL, str.buf, id);

    string_clear(&str);

    cache_delf(cd, PREFIX_MEMORY"%d", id);

    return STATUS_OK;
}
Beispiel #20
0
// Throws a runtime exception back to the Java VM appropriate for the type of
// error and frees the NEOERR that is passed in.
// TODO: throw more specific exceptions for errors like NERR_IO and NERR_NOMEM
int jNeoErr(JNIEnv *env, NEOERR *err) {
  STRING str;

  string_init(&str);
  if (nerr_match(err, NERR_PARSE)) {
    nerr_error_string(err, &str);
    throwRuntimeException(env, str.buf);
  } else if (nerr_match(err, NERR_IO)) {
    nerr_error_string(err, &str);
    throwIOException(env, str.buf);
  } else if (nerr_match(err, NERR_NOMEM)) {
    nerr_error_string(err, &str);
    throwOutOfMemoryError(env, str.buf);
  } else {
    nerr_error_traceback(err, &str);
    throwRuntimeException(env, str.buf);
  }

  nerr_ignore(&err);  // free err, otherwise it would leak
  string_clear(&str);

  return 0;
}
Beispiel #21
0
NEOERR* ltpl_render(CGI *cgi, HASH *tplh, session_t *ses)
{
    STRING str; string_init(&str);
    CSPARSE *cs;
    HDF *dhdf;
    NEOERR *err;

    char *render = NULL;

    render = ses->render;
    cs = (CSPARSE*)hash_lookup(tplh, render);
    dhdf = (HDF*)hash_lookupf(tplh, "%s_hdf", render);

    if (!cs) return nerr_raise(LERR_MISS_TPL, "render %s not found", render);
    if (dhdf) hdf_copy(cgi->hdf, NULL, dhdf);
    
    ltpl_prepare_rend(cgi->hdf, hdf_get_value(cgi->hdf,
                                              PRE_RESERVE"."PRE_CFG_LAYOUT,
                                              "layout.html"));
    
    if (ses->tm_cache_browser > 0) {
        hdf_set_valuef(cgi->hdf, "cgiout.other.cache=Cache-Control: max-age=%lu",
                       ses->tm_cache_browser);
    }
    cs->hdf = cgi->hdf;

    err = cs_render(cs, &str, mcs_strcb);
    if (err != STATUS_OK) return nerr_pass(err);

    err = cgi_output(cgi, &str);
    if (err != STATUS_OK) return nerr_pass(err);

    cs->hdf = NULL;
    string_clear(&str);

    return STATUS_OK;
}
Beispiel #22
0
/*
 * we don't pass tplh parameter to it,
 * because walker don't have it. so, tplh stored in the g_datah
 */
NEOERR* ltpl_render2file(CGI *cgi, char *render, char *fname)
{
    STRING str; string_init(&str);
    HASH *tplh;
    CSPARSE *cs;
    HDF *dhdf;
    NEOERR *err;

    MCS_NOT_NULLC(cgi->hdf, render, fname);
    
    tplh = hash_lookup(g_datah, "runtime_templates");
    MCS_NOT_NULLA(tplh);

    cs = hash_lookup(tplh, render);
    MCS_NOT_NULLA(cs);

    dhdf = hash_lookupf(tplh, "%s_hdf", render);
    if (dhdf) hdf_copy(cgi->hdf, NULL, dhdf);

    ltpl_prepare_rend(cgi->hdf, hdf_get_value(cgi->hdf,
                                              PRE_RESERVE"."PRE_CFG_LAYOUT,
                                              "layout.html"));
    cs->hdf = cgi->hdf;

    err = cs_render(cs, &str, mcs_strcb);
    if (err != STATUS_OK) return nerr_pass(err);

    err = mfile_makesure_dir(fname);
    if (err != STATUS_OK) return nerr_pass(err);
    
    err = mcs_str2file(str, fname);
    if (err != STATUS_OK) return nerr_pass(err);

    string_clear(&str);

    return STATUS_OK;
}
Beispiel #23
0
static void
stats_metric_init(struct stats_metric *stm)
{
    switch (stm->type) {
    case STATS_COUNTER:
        stm->value.counter = 0LL;
        break;

    case STATS_GAUGE:
        stm->value.counter = 0LL;
        break;

    case STATS_TIMESTAMP:
        stm->value.timestamp = 0LL;
        break;

    case STATS_STRING:
        string_init(&stm->value.str);
        break;

    default:
        NOT_REACHED();
    }
}
Beispiel #24
0
void parse_header(http_connect_t * con) {
 	request *in;
	buffer *header;
	pool_t *p;
	char * start, *end;


	p = (pool_t *)con->p;
	in = con->in;
	header = in->header;
	start = (char *)(header->ptr);
	end = (char *)header->ptr + (header->used - 1);
	if(strncasecmp(start,"put", 3) == 0) {
		in->http_method = _PUT;
		start += 3;
	}if(strncasecmp(start,"get", 3) == 0) {
		in->http_method = _GET;
		start += 3;
	}
	else if(strncasecmp(start, "post", 4) == 0) {
		in->http_method = _POST;
		start += 4;
	}

	start = skip_space(start, end);
	in->uri = string_init(p);
	in->uri->ptr = start;
	start = find_line(start, end);
	in->uri->len = start - in->uri->ptr;
	start++;


	in->content_length = atoi(start);
	//test_print_header(in);
	return ;
}
Beispiel #25
0
void lerr_opfinish_json(NEOERR *err, HDF *hdf)
{
    if (err == STATUS_OK) {
        hdf_set_value(hdf, PRE_SUCCESS, "1");
        return;
    }
    
    hdf_remove_tree(hdf, PRE_SUCCESS);
    
    NEOERR *neede = mcs_err_valid(err);
    /* set PRE_ERRXXX with the most recently err */
    hdf_set_int_value(hdf, PRE_ERRCODE, neede->error);
    if (!hdf_get_obj(hdf, PRE_ERRMSG)) {
        hdf_set_valuef(hdf, "%s=%s:%d %s",
                       PRE_ERRMSG, neede->file, neede->lineno, neede->desc);
    }

    STRING str; string_init(&str);
    nerr_error_traceback(err, &str);
    mtc_err("%s", str.buf);
    hdf_set_value(hdf, PRE_ERRTRACE, str.buf);
    nerr_ignore(&err);
    string_clear(&str);
}
Beispiel #26
0
/**
 * Inserts an array of unique element into a avl tree.
 */
void _avl_tree_insert_unique_array(_avl_tree_t* pt_avl_tree, const void* cpv_array, size_t t_count)
{
    size_t i = 0;

    assert(pt_avl_tree != NULL);
    assert(_avl_tree_is_inited(pt_avl_tree));
    assert(cpv_array != NULL);

    /*
     * Copy the elements from src array to dest avl tree.
     * The array of c builtin and user define or cstl builtin are different,
     * the elements of c builtin array are element itself, but the elements of 
     * c string, user define or cstl are pointer of element.
     */
    if (strncmp(_GET_AVL_TREE_TYPE_BASENAME(pt_avl_tree), _C_STRING_TYPE, _TYPE_NAME_SIZE) == 0) {
        /*
         * We need built a string_t for c string element.
         */
        string_t* pstr_elem = create_string();
        assert(pstr_elem != NULL);
        string_init(pstr_elem);
        for (i = 0; i < t_count; ++i) {
            string_assign_cstr(pstr_elem, *((const char**)cpv_array + i));
            _avl_tree_insert_unique(pt_avl_tree, pstr_elem);
        }
        string_destroy(pstr_elem);
    } else if (_GET_AVL_TREE_TYPE_STYLE(pt_avl_tree) == _TYPE_C_BUILTIN) {
        for (i = 0; i < t_count; ++i) {
            _avl_tree_insert_unique(pt_avl_tree, (unsigned char*)cpv_array + i * _GET_AVL_TREE_TYPE_SIZE(pt_avl_tree));
        }
    } else {
        for (i = 0; i < t_count; ++i) {
            _avl_tree_insert_unique(pt_avl_tree, *((void**)cpv_array + i));
        }
    }
}
Beispiel #27
0
Datei: main.c Projekt: hiciu/ekg2
static char *fstring_reverse(fstring_t *fstr) {
	const char *str;
	const fstr_attr_t *attr;
	string_t asc;
	int i;

	if (!fstr)
		return NULL;

	attr = fstr->attr;
	str = fstr->str;

	if (!attr || !str)
		return NULL;

	asc = string_init(NULL);

	for (i = 0; str[i]; i++) {
#define prev	attr[i-1]
#define cur	attr[i]
		int reset = 0;

		if (i) {
			if (!(cur & FSTR_BOLD) && (prev & FSTR_BOLD))		reset = 1;
			if (!(cur & FSTR_BLINK) && (prev & FSTR_BLINK))		reset = 1;
			if (!(cur & FSTR_UNDERLINE) && (prev & FSTR_UNDERLINE))	reset = 1;
			if (!(cur & FSTR_REVERSE) && (prev & FSTR_REVERSE))	reset = 1;
			if ((cur & FSTR_NORMAL) && !(prev & FSTR_NORMAL))	reset = 1;	/* colors disappear */

			if (reset)
				string_append(asc, "%n");
		} else
			reset = 1;

	/* attr */
		if ((cur & FSTR_BLINK) &&	(reset || !(prev & FSTR_BLINK)))	string_append(asc, "%i");
//		if ((cur & FSTR_UNDERLINE) &&	(reset || !(prev & FSTR_UNDERLINE)))	string_append(asc, "%");
//		if ((cur & FSTR_REVERSE) &&	(reset || !(prev & FSTR_REVERSE)))	string_append(asc, "%");

		if (!(cur & FSTR_NORMAL)) {
		/* background color XXX */
#define BGCOLOR(x)	-1
			if (0 && ((reset || BGCOLOR(cur) != BGCOLOR(prev)))) {
				string_append_c(asc, '%');
				switch (BGCOLOR(cur)) {
					case (0): string_append_c(asc, 'l'); break;
					case (1): string_append_c(asc, 's'); break;
					case (2): string_append_c(asc, 'h'); break;
					case (3): string_append_c(asc, 'z'); break;
					case (4): string_append_c(asc, 'e'); break;
					case (5): string_append_c(asc, 'q'); break;
					case (6): string_append_c(asc, 'd'); break;
					case (7): string_append_c(asc, 'x'); break;
				}
			}
#undef BGCOLOR

		/* foreground color */
#define FGCOLOR(x)	((!(x & FSTR_NORMAL)) ? (x & FSTR_FOREMASK) : -1)
			if (((reset || FGCOLOR(cur) != FGCOLOR(prev)) || (i && (prev & FSTR_BOLD) != (cur & FSTR_BOLD)))) {
				string_append_c(asc, '%');
				switch ((cur & FSTR_FOREMASK)) {
					case (0): string_append_c(asc, (cur & FSTR_BOLD) ? 'K' : 'k'); break;
					case (1): string_append_c(asc, (cur & FSTR_BOLD) ? 'R' : 'r'); break;
					case (2): string_append_c(asc, (cur & FSTR_BOLD) ? 'G' : 'g'); break;
					case (3): string_append_c(asc, (cur & FSTR_BOLD) ? 'Y' : 'y'); break;
					case (4): string_append_c(asc, (cur & FSTR_BOLD) ? 'B' : 'b'); break;
					case (5): string_append_c(asc, (cur & FSTR_BOLD) ? 'M' : 'm'); break; /* | fioletowy	 | %m/%p  | %M/%P | %q	| */
					case (6): string_append_c(asc, (cur & FSTR_BOLD) ? 'C' : 'c'); break;
					case (7): string_append_c(asc, (cur & FSTR_BOLD) ? 'W' : 'w'); break;
				}
			}
#undef FGCOLOR
		} else {	/* no color */
			if ((cur & FSTR_BOLD) && (reset || !(prev & FSTR_BOLD)))
				string_append(asc, "%T");
		}

	/* str */
		if (str[i] == '%' || str[i] == '\\')
			string_append_c(asc, '\\');
		string_append_c(asc, str[i]);
	}

/* reset, and return. */
	string_append(asc, "%n");
	return string_free(asc, 0);

#undef prev
#undef cur
}
Beispiel #28
0
bool
crypto_aes_decrypt(struct string *ciphertext, struct string *aes_key, struct string *aes_iv, struct string *decrypted)
{
    bool retval = false;
    EVP_CIPHER_CTX ctx;
    int decryptspace;
    int decryptdone;

    EVP_CIPHER_CTX_init(&ctx);
    if (!EVP_DecryptInit_ex(&ctx, EVP_aes_256_cbc(), NULL,
        (unsigned char *)string_get(aes_key),
        (unsigned char *)string_get(aes_iv))) {
        log_err("crypto_aes_decrypt: init failed\n");
        ERR_print_errors_fp(stderr);
        goto bail_out;
    }
    EVP_CIPHER_CTX_set_padding(&ctx, 1);
    
    if (string_length(aes_key) != EVP_CIPHER_CTX_key_length(&ctx)) {
        log_err("crypto_aes_decrypt: invalid key size (%" PRIuPTR " vs expected %d)\n",
                string_length(aes_key), EVP_CIPHER_CTX_key_length(&ctx));
        goto bail_out;
    }
    if (string_length(aes_iv) != EVP_CIPHER_CTX_iv_length(&ctx)) {
        log_err("crypto_aes_decrypt: invalid iv size (%" PRIuPTR " vs expected %d)\n",
                string_length(aes_iv), EVP_CIPHER_CTX_iv_length(&ctx));
        goto bail_out;
    }

    decryptspace = string_length(ciphertext) + EVP_MAX_BLOCK_LENGTH;

    string_free(decrypted); /* free previous buffer */
    string_init(decrypted, decryptspace, 1024);
    if (string_size(decrypted) < decryptspace) {
        log_err("crypto_aes_decrypt: decrypt buffer malloc error\n");
        goto bail_out;
    }
    
    if (EVP_DecryptUpdate(&ctx, (unsigned char*)string_get(decrypted),
            &decryptdone, (unsigned char*)string_get(ciphertext),
            string_length(ciphertext))) {
        /* TODO: need cleaner way: */
        decrypted->_u._s.length = decryptdone;
    } else {
        log_err("crypto_aes_decrypt: decrypt failed\n");
        ERR_print_errors_fp(stderr);
        goto bail_out;
    }
    
    if (EVP_DecryptFinal_ex(&ctx,
            (unsigned char*)string_get(decrypted)+string_length(decrypted),
            &decryptdone)) {
        /* TODO: need cleaner way: */
        decrypted->_u._s.length += decryptdone;
    } else {
        log_err("crypto_aes_decrypt: decrypt final failed\n");
        ERR_print_errors_fp(stderr);
        goto bail_out;
    }

    retval = true;

bail_out:
    EVP_CIPHER_CTX_cleanup(&ctx);
    return retval;
}
Beispiel #29
0
/****************************************************************************
set a string value, allocing the space for the string, and deallocating any 
existing space
****************************************************************************/
BOOL string_set(char **dest,const char *src)
{
  string_free(dest);

  return(string_init(dest,src));
}
Beispiel #30
0
#define CC_eu   0x6575
#define CC_fr   0x6672
#define CC_hk   0x686b
#define CC_tw   0x7477
#define CC_io   0x696f
#define CC_it   0x6974
#define CC_mo   0x6d6f
/* 以网络序(大端)定义Generic Code */
#define GC_com  0x636f6d00
#define GC_net  0x6e657400
#define GC_org  0x6f726700
#define GC_gov  0x676f7600
#define GC_edu  0x65647500
#define GC_biz  0x62697a00

string_t host_wildcard = string_init("*");
/*
 * 检查host的label,将顶级域名和可能存在的一级generic域名合并成一个label
 * 输入的label至少有2项,即label_count >= 2.
 */
static inline int cache_host_parse_deep(string_t label[], int label_count)
{
    int i, ret = -1;
    uint16_t country_code = 0;
    uint32_t generic_code = 0;
    char *p_cc, *p_gc;
    int need_check = 0, need_merge = 0;

    /*
     * 首先,处理顶级域名,label[0],
     * XXX: 只有当顶级域名是国家域名时,才可能需要合并label[0]和label[1]。