int main() { struct table *shapes; int loops, planecount, pointcount, x, y, z; /* Get number of bulks to process. */ scanf("%d", &loops); while(loops--) { /* allocate the shapes table and create the * initial shape. */ shapes = table_new(shape_comparator); struct shape *newshape = shape_alloc(); table_add(shapes, newshape); /* Get the plane count. */ scanf("%d", &planecount); while(planecount--) { /* Create a new plane and add it to the plane table inside * the shape. */ struct plane *newplane = plane_alloc(); table_add(newshape->planes, newplane); /* Get the point count. */ scanf("%d", &pointcount); while(pointcount--) { /* Add point to the shape. */ scanf("%d %d %d", &x, &y, &z); struct point *newpoint = point_alloc(x, y, z); if (!table_add(newshape->points, newpoint)) { int loc; table_find(newshape->points, newpoint, &loc); free(newpoint); newpoint = table_get(newshape->points, loc); } /* Add mapping between plane and point. */ table_add(newshape->ppmaps, ppmap_alloc(newplane, newpoint)); } /* Add the plane to the planes table inside shape. */ table_add(newshape->planes, newplane); } resolve_planes(shapes); resolve_shapes(shapes); coallece_planes(shapes); } }
void room_add_character(struct room *r, character *ch, int reason_code) { table_iterator *t; /* Broadcast to each character in the room that a * new character has entered the room. */ t = table_iterate_over(r->chars); while(1) { character *tc; const char *name, *reason; tc = table_iterate(t); if(!tc) break; name = character_username(ch); reason = reasons[reason_code]; chprintf(tc, "\r\n%s entered the room via %s.\r\n", name, reason ); character_prompt(tc); } table_add(r->chars, character_username(ch), ch); }
struct room *room_get(const char *name) { FILE *f; char buf[128]; struct room *r; /* See if the room is already loaded */ r = table_get(rooms, name); if(r) return r; /* Room wasn't already loaded - try to find it on disk */ snprintf(buf, 128, "rooms/%s.txt", name); f = fopen(buf, "rb"); if(!f) return NULL; /* -ENOROOM */ r = room_parse(f); if(!r) { printf("F**k, couldn't parse room %s\n", name); return NULL; } r->name = strdup(name); table_add(rooms, name, r); fclose(f); return r; }
static void parse_line(struct room *r, char *buf) { int type; char *key, *value; type = buf[0]; key = buf+1; value = strchr(buf, '='); if(value) *value++ = '\0'; /* Null terminate key */ else value = key; switch(type) { case '_': /* Room description */ r->desc = strdup(key); break; case '#': /* furniture */ table_add(r->furniture, strdup(key), strdup(value)); break; case '$': /* TODO: Load from objects/name */ break; case '!': /* NPC */ /* TODO Load NPC */ break; } }
/** * Finalize MULTIPART processing. * * @param d */ int htp_ch_multipart_callback_request_body_data(htp_tx_data_t *d) { if (d->data != NULL) { // Process one chunk of data htp_mpartp_parse(d->tx->request_mpartp, d->data, d->len); } else { // Finalize parsing htp_mpartp_finalize(d->tx->request_mpartp); d->tx->request_params_body = d->tx->cfg->create_table(list_size(d->tx->request_mpartp->parts)); // TODO RC // Extract parameters d->tx->request_params_body_reused = 1; htp_mpart_part_t *part = NULL; list_iterator_reset(d->tx->request_mpartp->parts); while ((part = (htp_mpart_part_t *) list_iterator_next(d->tx->request_mpartp->parts)) != NULL) { // Only use text parameters if (part->type == MULTIPART_PART_TEXT) { if (d->tx->connp->cfg->parameter_processor == NULL) { table_add(d->tx->request_params_body, part->name, part->value); } else { d->tx->connp->cfg->parameter_processor(d->tx->request_params_body, part->name, part->value); } } } } return HOOK_OK; }
/** * Proccess an outcomming packet and delete old records * * @param internal_ip The internal IP address * @param internal_mac The internal MAC address * @param internal_port The internal port * @param external_ip The external IP address * @return The corresponding record */ struct table_record *table_outbound(uint32_t internal_ip, uint8_t *internal_mac, uint16_t internal_port, uint32_t external_ip) { struct table_record *record = table; struct table_record *before = NULL; while (record) { if (record->internal_ip == internal_ip && record->internal_port == internal_port && record->external_ip == external_ip) { record->touch = time(NULL); /* touch! */ return record; } /* obsolete record */ if (before && record->touch < time(NULL) + RECORD_TIMEOUT) { before->next = record->next; free(record); } before = record; record = record->next; } return table_add(internal_ip, internal_mac, internal_port, external_ip); }
int random_get_state_wrapper(void) { int stateid; /* State handle. */ THREAD_LOCK(state_table_lock); if (!state_table) state_table=table_new(); stateid = table_add(state_table, (vpointer) current_state); /* FIXME: Need to copy state. */ THREAD_UNLOCK(state_table_lock); return stateid; }
int timer_new_slang(void) { chrono_t *t=s_malloc(sizeof(chrono_t)); int t_handle; THREAD_LOCK(chrono_table_lock); if (chrono_table==NULL) chrono_table=table_new(); t_handle = table_add(chrono_table, (vpointer) t); THREAD_UNLOCK(chrono_table_lock); return (int) t_handle; }
void intt_test (void) { int_table it; unsigned i, j; TABLE_INIT (it); for (j = 0; j < 10; j++) { i = table_add (&it); assert (i != 0); TABLE_ITEM (it, i) = j + 5; printf ("Add: it[%u]=%d\r\n", i, TABLE_ITEM (it, i)); } int_table_dump (&it); table_remove (&it, 2); printf ("Remove: it[%u]\r\n", 2); table_remove (&it, 4); printf ("Remove: it[%u]\r\n", 4); int_table_dump (&it); for (j = 2; j < 12; j++) { i = table_add (&it); assert (i != 0); TABLE_ITEM (it, i) = j; printf ("Add: it[%u]=%d\r\n", i, TABLE_ITEM (it, i)); } int_table_dump (&it); TABLE_REQUIRE (it, 2, ~0); table_reset (&it); int_table_dump (&it); for (j = 0; j < 5; j++) { i = table_add (&it); assert (i != 0); TABLE_ITEM (it, i) = j + 5; printf ("Add: it[%u]=%d\r\n", i, TABLE_ITEM (it, i)); } int_table_dump (&it); }
/* * read and hash the passwd file or NIS map */ void plt_init(void) { struct passwd *pent_p; if (plt_reset() < 0) /* could not reset table. skip. */ return; plog(XLOG_INFO, "reading password map"); hlfsd_setpwent(); /* prepare to read passwd entries */ while ((pent_p = hlfsd_getpwent()) != (struct passwd *) NULL) { table_add(pent_p->pw_uid, pent_p->pw_dir, pent_p->pw_name); if (STREQ("root", pent_p->pw_name)) { int len; if (root_home) XFREE(root_home); root_home = strdup(pent_p->pw_dir); len = strlen(root_home); /* remove any trailing '/' chars from root's home (even if just one) */ while (len > 0 && root_home[len - 1] == '/') { len--; root_home[len] = '\0'; } } } hlfsd_endpwent(); qsort((char *) pwtab, cur_pwtab_num, sizeof(uid2home_t), plt_compare_fxn); qsort((char *) untab, cur_pwtab_num, sizeof(username2uid_t), unt_compare_fxn); if (!root_home) root_home = strdup(""); plog(XLOG_INFO, "password map read and sorted"); }
static adlb_code build_worker2host(const struct xlb_hostnames *hostnames, const xlb_layout *layout, int my_workers, int **worker2host, int *host_count) { *worker2host = malloc(sizeof((*worker2host)[0]) * (size_t)my_workers); ADLB_MALLOC_CHECK(*worker2host); struct table host_name_idx_map; bool ok = table_init(&host_name_idx_map, 128); CHECK_MSG(ok, "Table init failed"); *host_count = 0; for (int i = 0; i < my_workers; i++) { int rank = xlb_rank_from_my_worker_idx(layout, i); const char *host_name = xlb_hostnames_lookup(hostnames, rank); CHECK_MSG(host_name != NULL, "Unexpected error looking up host for " "rank %i", rank); unsigned long host_idx; if (!table_search(&host_name_idx_map, host_name, (void**)&host_idx)) { host_idx = (unsigned long)(*host_count)++; ok = table_add(&host_name_idx_map, host_name, (void*)host_idx); CHECK_MSG(ok, "Table add failed"); } (*worker2host)[i] = (int)host_idx; DEBUG("host_name_idx_map: my worker %i (rank %i) -> host %i (%s)", i, xlb_rank_from_my_worker_idx(layout, i), (int)host_idx, host_name); } table_free_callback(&host_name_idx_map, false, NULL); return ADLB_SUCCESS; }
/* * read and hash the passwd file or NIS map */ void plt_init(void) { struct passwd *pent_p; if (plt_reset() < 0) /* could not reset table. skip. */ return; plog(XLOG_INFO, "reading password map"); hlfsd_setpwent(); /* prepare to read passwd entries */ while ((pent_p = hlfsd_getpwent()) != (struct passwd *) NULL) { table_add(pent_p->pw_uid, pent_p->pw_dir, pent_p->pw_name); } hlfsd_endpwent(); qsort((char *) pwtab, cur_pwtab_num, sizeof(uid2home_t), plt_compare_fxn); qsort((char *) untab, cur_pwtab_num, sizeof(username2uid_t), unt_compare_fxn); plog(XLOG_INFO, "password map read and sorted"); }
int test_table(){ write_cstr(1, ">>> table >>>\n"); struct table *tbl; tbl = table_alloc(); int hash = 0; puts("--- testing hash ---"); hash = hash_key(tbl, "hi"); printf("result hi %d\n", hash); hash = hash_key(tbl, "hello"); printf("result hello %d\n", hash); hash = hash_key(tbl, "denial"); printf("result denial %d\n", hash); hash = hash_key(tbl, "I was a donkey once upon a time"); printf("result I was a donkey once upon a time %d\n", hash); hash = hash_key(tbl, "dominence"); printf("result what's dominence %d\n", hash); hash = hash_key(tbl, "power up it's drinking time"); printf("result power up it's drinking time %d\n", hash); puts("--- testing size by level ---"); printf("size by level 1:%d\n", size_by_level(1)); printf("size by level 2:%d\n", size_by_level(2)); printf("size by level 3:%d\n", size_by_level(3)); puts("--- testing values and add ---"); table_add(tbl, string_copy("hello"), string_copy("there")); table_print_debug(tbl, stdout); table_add(tbl, string_copy("apple"), string_copy("tree")); table_print_debug(tbl, stdout); table_add(tbl, string_copy("bannana"), string_copy("split")); table_print_debug(tbl, stdout); table_add(tbl, string_copy("lash"), string_copy("O'Ninetails")); table_print_debug(tbl, stdout); table_add(tbl, string_copy("fire"), string_copy("crow")); table_print_debug(tbl, stdout); table_add(tbl, string_copy("denial"), string_copy("is hidden")); table_print_debug(tbl, stdout); table_add(tbl, string_copy("one"), string_copy("1")); table_print_debug(tbl, stdout); table_add(tbl, string_copy("two"), string_copy("2")); table_print_debug(tbl, stdout); table_add(tbl, string_copy("three"), string_copy("3")); table_print_debug(tbl, stdout); table_add(tbl, string_copy("four"), string_copy("4")); table_print_debug(tbl, stdout); table_add(tbl, string_copy("five"), string_copy("5")); table_print_debug(tbl, stdout); table_add(tbl, string_copy("six"), string_copy("6")); table_print_debug(tbl, stdout); table_add(tbl, string_copy("seven"), string_copy("7")); table_print_debug(tbl, stdout); table_add(tbl, string_copy("eight"), string_copy("8")); table_print_debug(tbl, stdout); table_add(tbl, string_copy("nine"), string_copy("9")); table_print_debug(tbl, stdout); table_add(tbl, string_copy("ten"), string_copy("10")); table_print_debug(tbl, stdout); table_add(tbl, string_copy("eleven"), string_copy("11")); table_print_debug(tbl, stdout); table_add(tbl, string_copy("twelve"), string_copy("12")); table_print_debug(tbl, stdout); table_add(tbl, string_copy("thirteen"), string_copy("13")); table_print_debug(tbl, stdout); table_add(tbl, string_copy("fourteen"), string_copy("14")); table_print_debug(tbl, stdout); table_add(tbl, string_copy("fifteen"), string_copy("15")); table_print_debug(tbl, stdout); table_add(tbl, string_copy("sixteen"), string_copy("16")); table_print_debug(tbl, stdout); table_add(tbl, string_copy("seventeen"), string_copy("17")); table_print_debug(tbl, stdout); table_add(tbl, string_copy("eighteen"), string_copy("18")); table_print_debug(tbl, stdout); table_add(tbl, string_copy("nineteen"), string_copy("19")); table_print_debug(tbl, stdout); table_add(tbl, string_copy("twenty"), string_copy("20")); table_print_debug(tbl, stdout); puts("--- testing get ---"); struct table_item *item = table_get(tbl, "hello"); if(item != NULL){ fprintf(stdout, "%s->%s\n", item->key_val, item->content); } struct table_item *item2 = table_get(tbl, "not here"); if(item2 != NULL){ fprintf(stdout, "%s->%s\n", item2->key_val, item2->content); }else{ puts("item is null"); } struct table_item *item3 = table_get(tbl, "bannana"); if(item3 != NULL){ fprintf(stdout, "%s->%s\n", item3->key_val, item3->content); } struct table_item *item4 = table_get(tbl, "one"); if(item4 != NULL){ fprintf(stdout, "%s->%s\n", item4->key_val, item4->content); } return 0; }
/* * php3_header() flushes the header info built up using calls to * the Header() function. If type is 1, a redirect to str is done. * Otherwise type should be 0 and str NULL. * * The function returns non-zero if output is allowed after the * call, and zero otherwise. Any call to php3_header() must check * the return status and if false, no output must be sent. This * is in order to correctly handle HEAD requests. */ PHPAPI int php3_header(void) { #if APACHE CookieList *cookie; int len = 0; time_t t; char *dt, *cookievalue = NULL; #endif #if APACHE || defined(USE_SAPI) || FHTTPD char *tempstr; #endif TLS_VARS; if (GLOBAL(header_is_being_sent)) { return 0; } else { GLOBAL(header_is_being_sent) = 1; } #if APACHE if (!GLOBAL(php3_rqst)) { /* we're not in a request, allow output */ GLOBAL(header_is_being_sent) = 0; return 1; } if ((GLOBAL(php3_PrintHeader) && !GLOBAL(php3_HeaderPrinted)) || (GLOBAL(php3_PrintHeader) && GLOBAL(php3_HeaderPrinted) == 2)) { if (!(GLOBAL(initialized) & INIT_ENVIRONMENT) && GLOBAL(request_info).request_method) { if(!strcasecmp(GLOBAL(request_info).request_method, "post")) php3_treat_data(PARSE_POST, NULL); /* POST Data */ else { if(!strcasecmp(GLOBAL(request_info).request_method, "put")) php3_treat_data(PARSE_PUT, NULL); /* PUT Data */ } } cookie = php3_PopCookieList(); while (cookie) { if (cookie->name) len += strlen(cookie->name); if (cookie->value) { cookievalue = _php3_urlencode(cookie->value, strlen (cookie->value)); len += strlen(cookievalue); } if (cookie->path) len += strlen(cookie->path); if (cookie->domain) len += strlen(cookie->domain); tempstr = emalloc(len + 100); if (!cookie->value || (cookie->value && !*cookie->value)) { /* * MSIE doesn't delete a cookie when you set it to a null value * so in order to force cookies to be deleted, even on MSIE, we * pick an expiry date 1 year and 1 second in the past */ sprintf(tempstr, "%s=deleted", cookie->name); t = time(NULL) - 31536001; strcat(tempstr, "; expires="); dt = php3_std_date(t); strcat(tempstr, dt); efree(dt); } else { /* FIXME: XXX: this is not binary data safe */ sprintf(tempstr, "%s=%s", cookie->name, cookie->value ? cookievalue : ""); if (cookie->name) efree(cookie->name); if (cookie->value) efree(cookie->value); if (cookievalue) efree(cookievalue); cookie->name=NULL; cookie->value=NULL; cookievalue=NULL; if (cookie->expires > 0) { strcat(tempstr, "; expires="); dt = php3_std_date(cookie->expires); strcat(tempstr, dt); efree(dt); } } if (cookie->path && strlen(cookie->path)) { strcat(tempstr, "; path="); strcat(tempstr, cookie->path); efree(cookie->path); cookie->path=NULL; } if (cookie->domain && strlen(cookie->domain)) { strcat(tempstr, "; domain="); strcat(tempstr, cookie->domain); efree(cookie->domain); cookie->domain=NULL; } if (cookie->secure) { strcat(tempstr, "; secure"); } table_add(GLOBAL(php3_rqst)->headers_out, "Set-Cookie", tempstr); if (cookie->domain) efree(cookie->domain); if (cookie->path) efree(cookie->path); if (cookie->name) efree(cookie->name); if (cookie->value) efree(cookie->value); if (cookievalue) efree(cookievalue); efree(cookie); cookie = php3_PopCookieList(); efree(tempstr); } GLOBAL(php3_HeaderPrinted) = 1; GLOBAL(header_called) = 1; send_http_header(GLOBAL(php3_rqst)); if (GLOBAL(php3_rqst)->header_only) { GLOBAL(shutdown_requested) = NORMAL_SHUTDOWN; GLOBAL(header_is_being_sent) = 0; return(0); } } #else if (GLOBAL(php3_PrintHeader) && !GLOBAL(php3_HeaderPrinted)) { if (!(GLOBAL(initialized) & INIT_ENVIRONMENT) && GLOBAL(request_info).request_method) { if(!strcasecmp(GLOBAL(request_info).request_method, "post")) php3_treat_data(PARSE_POST, NULL); /* POST Data */ else { if(!strcasecmp(GLOBAL(request_info).request_method, "put")) php3_treat_data(PARSE_PUT, NULL); /* PUT Data */ } } if (php3_ini.expose_php) { PUTS("X-Powered-By: PHP/" PHP_VERSION "\r\n"); } if (!GLOBAL(cont_type)) { #if USE_SAPI GLOBAL(sapi_rqst)->header(GLOBAL(sapi_rqst)->scid,"Content-type: text/html\015\012\015\012"); #else /* CGI BINARY or FHTTPD */ #if FHTTPD php3_fhttpd_puts_header("Content-type: text/html\r\n"); #else PUTS("Content-type: text/html\015\012\015\012"); #endif #endif /* endif SAPI */ } else { #if 0 /*WIN32|WINNT / *M$ does us again*/ if (!strcmp(GLOBAL(cont_type),"text/html")){ #endif #if USE_SAPI tempstr=emalloc(strlen(GLOBAL(cont_type))+18); sprintf(tempstr,"Content-type: %s\015\012\015\012",GLOBAL(cont_type)); GLOBAL(sapi_rqst)->header(GLOBAL(sapi_rqst)->scid,tempstr); efree(tempstr); #else /* CGI_BINARY or FHTTPD */ #if FHTTPD tempstr = emalloc(strlen(GLOBAL(cont_type)) + sizeof("Content-type:") + 2); if(tempstr) { strcpy(tempstr, "Content-type:"); strcpy(tempstr + sizeof("Content-type:") - 1, GLOBAL(cont_type)); strcat(tempstr, "\r\n"); php3_fhttpd_puts_header(tempstr); efree(tempstr); } #else PUTS("Content-type:"); PUTS(GLOBAL(cont_type)); PUTS("\015\012\015\012"); #endif #endif /* endif SAPI */ efree(GLOBAL(cont_type)); #if 0 /*WIN32|WINNT / *M$ does us again*/ } else { PUTS("\015\012"); }/*end excluding output of text/html*/ #endif } #if USE_SAPI GLOBAL(sapi_rqst)->flush(GLOBAL(sapi_rqst)->scid); #else fflush(stdout); #endif GLOBAL(php3_HeaderPrinted) = 1; GLOBAL(header_called) = 1; } #endif GLOBAL(header_is_being_sent) = 0; return(1); }
/** * Extract one request header. A header can span multiple lines, in * which case they will be folded into one before parsing is attempted. * * @param connp * @return HTP_OK or HTP_ERROR */ int htp_process_request_header_apache_2_2(htp_connp_t *connp) { bstr *tempstr = NULL; unsigned char *data = NULL; size_t len = 0; // Create new header structure htp_header_t *h = calloc(1, sizeof (htp_header_t)); if (h == NULL) return HTP_ERROR; // Ensure we have the necessary header data in a single buffer if (connp->in_header_line_index + 1 == connp->in_header_line_counter) { // Single line htp_header_line_t *hl = list_get(connp->in_tx->request_header_lines, connp->in_header_line_index); if (hl == NULL) { // Internal error htp_log(connp, HTP_LOG_MARK, HTP_LOG_ERROR, 0, "Process request header (Apache 2.2): Internal error"); free(h); return HTP_ERROR; } data = (unsigned char *) bstr_ptr(hl->line); len = bstr_len(hl->line); hl->header = h; } else { // Multiple lines (folded) int i = 0; for (i = connp->in_header_line_index; i < connp->in_header_line_counter; i++) { htp_header_line_t *hl = list_get(connp->in_tx->request_header_lines, i); if (hl == NULL) { // Internal error htp_log(connp, HTP_LOG_MARK, HTP_LOG_ERROR, 0, "Process request header (Apache 2.2): Internal error"); free(h); return HTP_ERROR; } len += bstr_len(hl->line); } tempstr = bstr_alloc(len); if (tempstr == NULL) { htp_log(connp, HTP_LOG_MARK, HTP_LOG_ERROR, 0, "Process request header (Apache 2.2): Failed to allocate bstring of %d bytes", len); free(h); return HTP_ERROR; } for (i = connp->in_header_line_index; i < connp->in_header_line_counter; i++) { htp_header_line_t *hl = list_get(connp->in_tx->request_header_lines, i); if (hl == NULL) { // Internal error htp_log(connp, HTP_LOG_MARK, HTP_LOG_ERROR, 0, "Process request header (Apache 2.2): Internal error"); bstr_free(tempstr); free(h); return HTP_ERROR; } char *data = bstr_ptr(hl->line); size_t len = bstr_len(hl->line); htp_chomp((unsigned char *)data, &len); bstr_add_mem_noex(tempstr, data, len); hl->header = h; } data = (unsigned char *) bstr_ptr(tempstr); } // Now try to parse the header if (htp_parse_request_header_apache_2_2(connp, h, data, len) != HTP_OK) { // Note: downstream responsible for error logging if (tempstr != NULL) { free(tempstr); } free(h); return HTP_ERROR; } // Do we already have a header with the same name? htp_header_t *h_existing = table_get(connp->in_tx->request_headers, h->name); if (h_existing != NULL) { // TODO Do we want to keep a list of the headers that are // allowed to be combined in this way? // Add to existing header h_existing->value = bstr_expand(h_existing->value, bstr_len(h_existing->value) + 2 + bstr_len(h->value)); bstr_add_mem_noex(h_existing->value, ", ", 2); bstr_add_str_noex(h_existing->value, h->value); // The header is no longer needed if (h->name != NULL) free(h->name); if (h->value != NULL) free(h->value); free(h); // Keep track of same-name headers h_existing->flags |= HTP_FIELD_REPEATED; } else { // Add as a new header table_add(connp->in_tx->request_headers, h->name, h); } if (tempstr != NULL) { free(tempstr); } return HTP_OK; }
/** * Parses one part header. * * @param data * @param len * @param Success indication */ int htp_mpartp_parse_header(htp_mpart_part_t *part, unsigned char *data, size_t len) { size_t name_start, name_end; size_t value_start, value_end; name_start = 0; // Look for the colon size_t colon_pos = 0; while ((colon_pos < len) && (data[colon_pos] != ':')) colon_pos++; if (colon_pos == len) { // Missing colon // TODO Error message return -1; } if (colon_pos == 0) { // Empty header name // TODO Error message } name_end = colon_pos; // Ignore LWS after field-name size_t prev = name_end; while ((prev > name_start) && (htp_is_lws(data[prev - 1]))) { prev--; name_end--; // LWS after field name // TODO Error message } // Value value_start = colon_pos; // Go over the colon if (value_start < len) { value_start++; } // Ignore LWS before field-content while ((value_start < len) && (htp_is_lws(data[value_start]))) { value_start++; } // Look for the end of field-content value_end = value_start; while (value_end < len) value_end++; // Ignore LWS after field-content prev = value_end - 1; while ((prev > value_start) && (htp_is_lws(data[prev]))) { prev--; value_end--; } // Check that the header name is a token size_t i = name_start; while (i < name_end) { if (!htp_is_token(data[i])) { // Request field is not a token // TODO Error message break; } i++; } // Now extract the name and the value htp_header_t *h = calloc(1, sizeof (htp_header_t)); if (h == NULL) return -1; h->name = bstr_dup_mem((char *) data + name_start, name_end - name_start); h->value = bstr_dup_mem((char *) data + value_start, value_end - value_start); // Check if the header already exists htp_header_t * h_existing = table_get(part->headers, h->name); if (h_existing != NULL) { // Add to existing header bstr *new_value = bstr_expand(h_existing->value, bstr_len(h_existing->value) + 2 + bstr_len(h->value)); if (new_value == NULL) { bstr_free(&h->name); bstr_free(&h->value); free(h); return -1; } h_existing->value = new_value; bstr_add_mem_noex(h_existing->value, ", ", 2); bstr_add_noex(h_existing->value, h->value); // The header is no longer needed bstr_free(&h->name); bstr_free(&h->value); free(h); // Keep track of same-name headers h_existing->flags |= HTP_FIELD_REPEATED; } else { // Add as a new header table_add(part->headers, h->name, h); } return 1; }
static int table_static_parse(struct table *t, const char *config, enum table_type type) { FILE *fp; char *buf, *lbuf; size_t flen; char *keyp; char *valp; size_t ret = 0; fp = fopen(config, "r"); if (fp == NULL) return 0; lbuf = NULL; while ((buf = fgetln(fp, &flen))) { if (buf[flen - 1] == '\n') buf[flen - 1] = '\0'; else { lbuf = xmalloc(flen + 1, "table_config_parse"); memcpy(lbuf, buf, flen); lbuf[flen] = '\0'; buf = lbuf; } keyp = buf; while (isspace((int)*keyp)) ++keyp; if (*keyp == '\0' || *keyp == '#') continue; valp = keyp; strsep(&valp, " \t:"); if (valp) { while (*valp) { if (!isspace(*valp) && !(*valp == ':' && isspace(*(valp + 1)))) break; ++valp; } if (*valp == '\0') valp = NULL; } /**/ if (t->t_type == 0) t->t_type = (valp == keyp || valp == NULL) ? T_LIST : T_HASH; if (!(t->t_type & type)) goto end; if ((valp == keyp || valp == NULL) && t->t_type == T_LIST) table_add(t, keyp, NULL); else if ((valp != keyp && valp != NULL) && t->t_type == T_HASH) table_add(t, keyp, valp); else goto end; } /* Accept empty alias files; treat them as hashes */ if (t->t_type == T_NONE && t->t_backend->services & K_ALIAS) t->t_type = T_HASH; ret = 1; end: free(lbuf); fclose(fp); return ret; }
ssize_t dict_add(dict *d, char *str) { uint64_t h = hash(str) % d->sz; table *t = table_init(str); d->arr[h] = table_add(d->arr[h], t); return h; }