コード例 #1
0
ファイル: lr0.c プロジェクト: AlexZhao/freebsd
void
lr0_leaks(void)
{
    DO_FREE(derives[start_symbol]);
    DO_FREE(derives);
    DO_FREE(nullable);
}
コード例 #2
0
ファイル: mkpar.c プロジェクト: adtools/yacc
void mkpar_leaks(void)
{
    DO_FREE(defred);
    DO_FREE(rules_used);
    DO_FREE(SRconflicts);
    DO_FREE(RRconflicts);
}
コード例 #3
0
ファイル: main.c プロジェクト: daolong/cencrypt
static void http_test() 
{
  DEBUG("\n\n== http_test ==\n");  
  size_t resp_size = 0;
  char buffer[1024];
  size_t out_len = 0;
  DO_CLEAR(buffer, 0, 1024);
  sprintf(buffer, "name=%s&number=%s&class=%s&memo=%s", base64encode(TEST_NAME, &out_len),  base64encode(TEST_NUMBER, &out_len),
     base64encode(TEST_CLASS, &out_len),  base64encode(TEST_MEMO, &out_len));
  DEBUG("param = %s\n", buffer);
  http_global_init();
  char *res = http_post(URL, (const char *)&buffer, NULL, &resp_size);
  http_global_release();
  if (res != NULL) {
    DEBUG("%s : response =\n%s \n", __func__, res);
    char *content = getContent(res, resp_size);
    if (content != NULL) {
      DEBUG("content base64 = %s\n", content);
      char *decode = base64_decode((const unsigned char *)content, strlen(content), &out_len);
      if (decode != NULL) {
        DEBUG("content plaintext = %s \n", decode);
        DO_FREE(decode);
      }
      DO_FREE(content);
    }
    DO_FREE(res);
  }
}
コード例 #4
0
ファイル: output.c プロジェクト: Der-Jan/freebsd-crypto
void
output_leaks(void)
{
    DO_FREE(tally);
    DO_FREE(width);
    DO_FREE(order);
}
コード例 #5
0
ファイル: tftp.c プロジェクト: basecq/q2dos
/**
 * Free allocated memory.
 */
static void tftp_exit (void)
{
  if (!_watt_fatal_error)
  {
    DO_FREE (tftp_boot_remote_file);
    DO_FREE (tftp_openmode);
  }
}
コード例 #6
0
ファイル: lr0.c プロジェクト: AaronNGray/byacc
void lr0_leaks(void) {
    if (derives) {
        if (derives[start_symbol] != rules)
            DO_FREE(derives[start_symbol]);
        DO_FREE(derives);
        DO_FREE(rules);
    }
    DO_FREE(nullable);
}
コード例 #7
0
ファイル: winpcap.c プロジェクト: ya-mouse/dos-utils
int pkt_release (void)
{
  ADAPTER *adapter;
  DWORD    status = 1;

  if (!_pkt_inf || _watt_fatal_error)
     return (0);

  adapter = (ADAPTER*) _pkt_inf->adapter;

  TCP_CONSOLE_MSG (2, ("pkt_release(): adapter %08lX\n", (DWORD)adapter));

  if (adapter && adapter != INVALID_HANDLE_VALUE)
  {
    /* Don't close the adapter before telling the thread about it.
     */
    if (_pkt_inf->recv_thread)
    {
      FILETIME ctime, etime, ktime, utime;
      DWORD    err = 0;

      _pkt_inf->stop_thread = TRUE;
      SetEvent (adapter->ReadEvent);
      Sleep (50);

      GetExitCodeThread (_pkt_inf->recv_thread, &status);
      if (status == STILL_ACTIVE)
           TCP_CONSOLE_MSG (2, ("pkt_release(): killing thread.\n"));
      else TCP_CONSOLE_MSG (2, ("pkt_release(): thread exit code %lu.\n",
                            status));

      if (!GetThreadTimes (_pkt_inf->recv_thread, &ctime, &etime, &ktime, &utime))
         err = GetLastError();
      TerminateThread (_pkt_inf->recv_thread, 1);
      CloseHandle (_pkt_inf->recv_thread);

      if (err)
         TCP_CONSOLE_MSG (2, ("  GetThreadTime() %s, ", win_strerror(err)));
      TCP_CONSOLE_MSG (2, ("  kernel time %.6fs, user time %.6fs\n",
                       filetime_sec(&ktime), filetime_sec(&utime)));
    }

    PacketCloseAdapter (adapter);

    _pkt_inf->adapter = NULL;
  }

  DO_FREE (_pkt_inf->npf_buf);
  DO_FREE (_pkt_inf);

  PacketInitModule (FALSE, dump_file);
  if (dump_file)
     fclose (dump_file);
  dump_file = NULL;
  return (int) status;
}
コード例 #8
0
ファイル: main.c プロジェクト: daolong/cencrypt
// HACK : encrypted data from server decrypt fail!!!
// TODO : check difference between local and server?
static void auth_rsa_test()
{
  DEBUG("\n\n== auth_rsa_test ==\n");  
  size_t resp_size = 0;
  char buffer[512];
  DO_CLEAR(buffer, 0, 512);
  size_t out_len = 0;
  sprintf(buffer, "{\"name\"=\"%s\", \"number\"=\"%s\", \"class\"=\"%s\", \"memo\"=\"%s\"}", 
    TEST_NAME, TEST_NUMBER, TEST_CLASS, TEST_MEMO);
  DEBUG("param = %s\n", buffer);  
  char *cipher = encrypt_publickey_fromcode(buffer);   
  if (cipher != NULL) {
    DEBUG("The cipher text = %s \n", cipher);
    char *encoded = base64encode_rsa(cipher, &out_len);
    if (encoded != NULL) {
      DO_CLEAR(buffer, 0, 512);
      sprintf(buffer, "data=%s", encoded);
      DEBUG("%s : encoded = %s \n", __func__, encoded);
      http_global_init();
      char *res = http_post(URL, (const char *)&buffer, NULL, &resp_size);
      http_global_release();
      if (res != NULL) {
        DEBUG("%s : response =\n%s \n", __func__, res);
        char *content = getContent(res, resp_size);
        if (content != NULL) {
          DEBUG("content base64 = %s\n", content);
          char *decode = base64_decode((const unsigned char *)content, strlen(content), &out_len);
          if (decode != NULL) {
            DEBUG("content encrypted len = %d \n", out_len);
            DEBUG("content encrypted = %s \n", decode);
            dump(decode);
            //char *plaintext = decrypt_privatekey_fromcode(decode);
            char *plaintext = decrypt_publickey_fromcode(decode);
            if (plaintext != NULL) {
                DEBUG("content decrypted = %s \n", plaintext);
                DO_FREE(plaintext);
            }
            DO_FREE(decode);
          }
          DO_FREE(content);
        }
        DO_FREE(res);
      }
    }
    
    char *plaintext_local = decrypt_privatekey_fromcode(cipher);
    if (plaintext_local != NULL) {
        DEBUG("content decrypted local = %s \n", plaintext_local);
        DO_FREE(plaintext_local);
    }
    
    free(cipher);
    cipher = NULL;
  } 
}
コード例 #9
0
ファイル: event_watcher.c プロジェクト: gtk-gnutella/gwc
void
ev_watcher_destruct(ev_watcher_t *w)
{
  if (w) {
    ev_watcher_check(w);
    if (-1 != w->ev_fd) {
      close(w->ev_fd);
      w->ev_fd = -1;
    }
    DO_FREE(w->ev_arr);
    DO_FREE(w);
  }
}
コード例 #10
0
ファイル: http.c プロジェクト: gtk-gnutella/gwc
int
http_set_useragent(const char *s)
{
  char *p;
  static const char ua_prefix[] = "User-Agent: ";
  static const char server_prefix[] = "Server: ";
  size_t len, size;
  
  if (!s) {
    if (http_useragent_header != http_default_useragent_header) {
      DO_FREE(http_useragent_header);
      http_server_header = deconstify_char_ptr(http_default_server_header);
    }
    if (http_server_header != http_default_server_header) {
      DO_FREE(http_server_header);
      http_useragent_header = deconstify_char_ptr(http_default_useragent_header);
    }
    return 0;
  }

  len = strlen(s);
  size = sizeof ua_prefix + len + sizeof "\r\n";
  p = calloc(1, size);
  if (!p) {
    WARN("calloc() failed");
    return -1;
  }
  if (http_useragent_header != http_default_useragent_header) {
    DO_FREE(http_useragent_header);
  }
  http_useragent_header = p;
  p = append_string(p, &size, ua_prefix);
  p = append_string(p, &size, s);
  p = APPEND_CRLF(p, &size);
 
  size = sizeof server_prefix + len + sizeof "\r\n";
  p = calloc(1, size);
  if (!p) {
    WARN("calloc() failed");
    return -1;
  }
  if (http_server_header != http_default_server_header) {
    DO_FREE(http_server_header);
  }
  http_server_header = p;
  p = append_string(p, &size, server_prefix);
  p = append_string(p, &size, s);
  p = APPEND_CRLF(p, &size);
  
  return 0;
}
コード例 #11
0
ファイル: main.c プロジェクト: Mirocow/balancer
void done(int k)
{
    DO_CLOSE(input_file);
    DO_CLOSE(output_file);

    DO_CLOSE(action_file);
    DO_CLOSE(defines_file);
    DO_CLOSE(graph_file);
    DO_CLOSE(text_file);
    DO_CLOSE(union_file);
    DO_CLOSE(verbose_file);

	if (action_file_name)
		unlink(action_file_name);
	FREE(action_file_name);
	if (text_file_name)
		unlink(text_file_name);
	FREE(text_file_name);
	if (union_file_name)
		unlink(union_file_name);
	FREE(union_file_name);

    if (got_intr)
        exit(EXIT_FAILURE);

#ifdef NO_LEAKS
    if (rflag)
	DO_FREE(code_file_name);

    if (dflag)
	DO_FREE(defines_file_name);

    if (oflag)
	DO_FREE(output_file_name);

    if (vflag)
	DO_FREE(verbose_file_name);

    if (gflag)
	DO_FREE(graph_file_name);

    lr0_leaks();
    lalr_leaks();
    mkpar_leaks();
    output_leaks();
    reader_leaks();
#endif

    exit(k);
}
コード例 #12
0
/*
 * We use the debug_mem_mutex here because we need to lock it anyway to call
 * free. This is probably a bug somewhere else in the code, but when I call
 * malloc/free outside of any lock, I have endless trouble with malloc
 * appearing to return the same pointer twice. Since we have to lock it
 * anyway, we might as well use it as the lock around the al_free_list.
 * Note that we can't call Free with the debug_mem_mutex locked.
 */
void
rf_FreeAllocList(RF_AllocListElem_t *l)
{
	int i;
	RF_AllocListElem_t *temp, *p;

	for (p = l; p; p = p->next) {
		RF_ASSERT(p->numPointers >= 0 &&
		    p->numPointers <= RF_POINTERS_PER_ALLOC_LIST_ELEMENT);
		for (i = 0; i < p->numPointers; i++) {
			RF_ASSERT(p->pointers[i]);
			RF_Free(p->pointers[i], p->sizes[i]);
		}
	}
	while (l) {
		temp = l;
		l = l->next;
		if (al_free_list_count > RF_AL_FREELIST_MAX) {
			DO_FREE(temp, sizeof(*temp));
		} else {
			temp->next = al_free_list;
			al_free_list = temp;
			al_free_list_count++;
		}
	}
}
コード例 #13
0
ファイル: dns.c プロジェクト: gtk-gnutella/guppy
void
dnslookup_ctx_free(dnslookup_ctx_t *ctx)
{
  RUNTIME_ASSERT(ctx);
  dnslookup_ctx_reset(ctx);
  DO_FREE(ctx);
}
コード例 #14
0
ファイル: main.c プロジェクト: daolong/cencrypt
static void getcontent_test() {
  DEBUG("\n\n== getcontent_test ==\n");  
  char *content = getContent(gResponseTestData, strlen(gResponseTestData));
  if (content != NULL) {
    DEBUG("content = %s\n", content);
    DO_FREE(content);
  }
}
コード例 #15
0
ファイル: tftp.c プロジェクト: basecq/q2dos
/*
 * Close the TFTP connection
 */
static void tftp_close (void)
{
  if (tftp_terminator)
    (*tftp_terminator)();

  if (debug_on)
     outs ("\n");

  if (sock)
  {
    sock_close (sock);
    free (sock);
    sock = NULL;
  }
  DO_FREE (inbuf);
  DO_FREE (outbuf);
}
コード例 #16
0
/*********************************************************************************
 * The contents of this file are subject to the Common Public Attribution
 * License Version 1.0 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.openemm.org/cpal1.html. The License is based on the Mozilla
 * Public License Version 1.1 but Sections 14 and 15 have been added to cover
 * use of software over a computer network and provide for limited attribution
 * for the Original Developer. In addition, Exhibit A has been modified to be
 * consistent with Exhibit B.
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License.
 * 
 * The Original Code is OpenEMM.
 * The Original Developer is the Initial Developer.
 * The Initial Developer of the Original Code is AGNITAS AG. All portions of
 * the code written by AGNITAS AG are Copyright (c) 2007 AGNITAS AG. All Rights
 * Reserved.
 * 
 * Contributor(s): AGNITAS AG. 
 ********************************************************************************/
# include	<stdlib.h>
# include	<string.h>
# include	"xmlback.h"

mailtype_t *
mailtype_alloc (void) /*{{{*/
{
	mailtype_t	*m;
	
	if (m = (mailtype_t *) malloc (sizeof (mailtype_t))) {
		m -> ident = NULL;
		m -> idnr = 0;
		m -> offline = false;
		DO_ZERO (m, blockspec);
	}
	return m;
}/*}}}*/
mailtype_t *
mailtype_free (mailtype_t *m) /*{{{*/
{
	if (m) {
		if (m -> ident)
			free (m -> ident);
		DO_FREE (m, blockspec);
		free (m);
	}
	return NULL;
}/*}}}*/
コード例 #17
0
ファイル: hashlist.c プロジェクト: gtk-gnutella/guppy
void
hashlist_destruct(hashlist_t *hl)
{
  size_t i;

  HASHLIST_CHECK(hl);
  for (i = 0; i < hl->num_bins; i++) {
    hash_item_t *item = hl->bins[i];

    while (item) {
      hash_item_t *bnext;

      bnext = item->bnext;
      hash_item_free(hl, item);
      item = bnext;
    }
  }

  DO_FREE(hl->bins);
  DO_FREE(hl);
}
コード例 #18
0
ファイル: main.c プロジェクト: daolong/cencrypt
static void flow_test() {
  DEBUG("\n\n== flow_test ==\n");  
  size_t resp_size = 0;
  size_t out_len = 0;
  char buffer[1024];
  DO_CLEAR(buffer, 0, 1024);
  sprintf(buffer, "{\"name\"=\"%s\", \"number\"=\"%s\", \"class\"=\"%s\", \"memo\"=\"%s\"}", 
    TEST_NAME, TEST_NUMBER, TEST_CLASS, TEST_MEMO);
  DEBUG("param = %s\n", buffer);  
  int len = strlen(buffer);
  char *cipher = encrypt_publickey_fromcode(buffer);   
  if (cipher != NULL) {
    DEBUG("The cipher text = %s \n", cipher);
    dump((const unsigned char *)cipher);
    char *encoded = base64encode_rsa(cipher,&out_len);
    if (encoded != NULL) {
      DEBUG("%s : encoded = %s \n", __func__, encoded);
      char *decode = base64decode((const unsigned char *)encoded, &out_len);
      if (decode != NULL) {
        DEBUG("local : content encrypted = %s \n", decode);
        dump((const unsigned char *)decode);
        char *plaintext = decrypt_privatekey_fromcode(decode);
        if (plaintext != NULL) {
            DEBUG("local : content decrypted = %s \n", plaintext);
            DO_FREE(plaintext);
        }
        DO_FREE(decode);
      }
    }
    
    char *plaintext_local = decrypt_privatekey_fromcode(cipher);
    if (plaintext_local != NULL) {
        DEBUG("content decrypted local = %s \n", plaintext_local);
        DO_FREE(plaintext_local);
    }
    
    free(cipher);
    cipher = NULL;
  }   
}
コード例 #19
0
ファイル: main.c プロジェクト: daolong/cencrypt
static void des_test_withmode(int mode)
{
  DEBUG("\n\n== des_test_withmode (%s) ==\n", des_getmode(mode));  
  char *testData = des_padding(gTestData0);
  if (testData == NULL) return NULL;
  size_t out_len = 0;
  char *encrypted = encrypt_des((const char*)testData, strlen(testData), gPassword, mode, &out_len);
  if (encrypted != NULL) {
    des_dump(encrypted, out_len);
    char *plain = decrypt_des(encrypted, out_len, gPassword, mode, &out_len);
    if (plain != NULL) {
      DEBUG("%s : plain text =\n%s \n", __func__, plain);
      if (strncmp(plain, testData, strlen(testData)) == 0) {
        DEBUG("%s : Success \n", __func__);
      } else {
        DEBUG("%s : Fail \n", __func__);
      }
      DO_FREE(plain);
    }
    DO_FREE(encrypted);
  }
  DO_FREE(testData);
}
コード例 #20
0
ファイル: lalr.c プロジェクト: 0mp/freebsd
void
lalr_leaks(void)
{
    int i;

    if (includes != 0)
    {
	for (i = 0; i < ngotos; i++)
	{
	    free(includes[i]);
	}
	DO_FREE(includes);
    }
}
コード例 #21
0
ファイル: http.c プロジェクト: gtk-gnutella/gwc
void
http_destruct(http_t *ctx)
{
  DO_FREE(ctx->range_set);
  mem_chunk_free(ctx->host, ctx->host_size);
  ctx->host = NULL;

  /* Free the extra headers, if any */
  while (ctx->extra_headers) {
    snode_t *next;
  
    next = ctx->extra_headers->next;
    snode_free(ctx->extra_headers);
    ctx->extra_headers = next;
  }
}
コード例 #22
0
void
rf_ShutdownAllocList(void *ignored)
{
	RF_AllocListElem_t *p, *pt;

	for (p = al_free_list; p;) {
		pt = p;
		p = p->next;
		DO_FREE(pt, sizeof(*pt));
	}
	rf_mutex_destroy(&alist_mutex);
	/*
	 * printf("Alloclist: Free list hit count %lu (%lu %%) miss count %lu"
	 *     " (%lu %%).\n", fl_hit_count,
	 *     (100*fl_hit_count)/(fl_hit_count+fl_miss_count),
	 *     fl_miss_count, (100*fl_miss_count)/(fl_hit_count+fl_miss_count));
	 */
}
コード例 #23
0
ファイル: main.c プロジェクト: gtk-gnutella/barracuda
static const char *
escape_buffer(const char *src, size_t src_len)
{
  static char *buf;
  static size_t buf_size;

  if (src_len < (size_t) -1 / 4) {
    char *p;
    size_t n;

    n = src_len * 4 + 1;
    if (buf_size < n) {
      buf_size = n;
      DO_FREE(buf);
      buf = malloc(buf_size);
    }
    p = append_escaped_chars(buf, &buf_size, src, src_len);
    *p = '\0';
    return buf;
  } else {
    return NULL;
  }
}
コード例 #24
0
ファイル: bindmaps.c プロジェクト: JoeNotCharles/notion
void ioncore_deinit_bindmaps()
{
    DO_FREE(screen, "WScreen");
    DO_FREE(mplex, "WMPlex");
    DO_FREE(mplex_toplevel, "WMPlex.toplevel");
    DO_FREE(frame, "WFrame");
    DO_FREE(frame_toplevel, "WFrame.toplevel");
    DO_FREE(frame_floating, "WFrame.floating");
    DO_FREE(frame_tiled, "WFrame.tiled");
    DO_FREE(frame_transient, "WFrame.transient");
    DO_FREE(moveres, "WMoveresMode");
    DO_FREE(group, "WGroup");
    DO_FREE(groupcw, "WGroupCW");
    DO_FREE(groupws, "WGroupWS");
    DO_FREE(clientwin, "WClientWin");
    rb_free_tree(known_bindmaps);
    known_bindmaps=NULL;
}
コード例 #25
0
ファイル: res_init.c プロジェクト: basecq/q2dos
static void res_exit (void)
{
  DO_FREE (res_cfg_options);
  DO_FREE (res_cfg_aliases);
}
コード例 #26
0
ファイル: template.c プロジェクト: gtk-gnutella/gwc
template_t *
template_load(const char *filename)
{
  struct template_chunk *chunk;
  FILE *f;
  char *buf = NULL, *q;
  size_t buf_len, line;
  template_t *tpl = NULL;
  int c;
  char entity[128], *e = NULL;

  RUNTIME_ASSERT(filename != NULL);
  f = safer_fopen(filename, SAFER_FOPEN_RD);
  if (!f) {
    WARN("could not open \"%s\": %s", filename, compat_strerror(errno));
    goto failure;
  }

  tpl = calloc(1, sizeof *tpl);
  if (!tpl) {
    CRIT("Out of memory");
    goto failure;
  }
  tpl->chunks = NULL;
  
  buf_len = 4096;
  buf = calloc(1, buf_len);
  if (!buf) {
    CRIT("Out of memory");
    goto failure;
  }
  
  for (line = 1; /* NOTHING */; line++) {
    char *p;
    
    p = fgets(buf, buf_len, f);
    if (!p) {
      if (!ferror(f))
        break;

      CRIT("fgets() failed: %s", compat_strerror(errno));
      goto failure;
    }

    q = strchr(buf, '\n');
    if (!q) {
      CRIT("Line too long or unterminated: \"%s\"", buf);
      goto failure;
    }
    while (isspace((unsigned char) *q)) {
      *q = '\0';
      if (q == buf)
        break;
      q--;
    }
    
    if (q == buf && *q == '\0')
      break;

    if (line == 1 && 0 == strncmp(buf, "HTTP/", sizeof "HTTP/" - 1)) {
      uint64_t code;
      char *endptr;
      int error;
      struct http_response hres;
      size_t size;
      snode_t *sn;

      p = strchr(buf, ' ');
      if (!p) {
        WARN("Invalid HTTP response: \"%s\"", buf);
        goto failure;
      }
      
      p = skip_spaces(p);
      code = parse_uint64(p, &endptr, 10, &error);
      if (code < 100 || code > 999) {
        WARN("Invalid HTTP result code: \"%s\"", buf);
        goto failure;
      }
      p = skip_spaces(endptr);

      hres.code = code;
      size = sizeof hres.msg;
      append_string(hres.msg, &size, p);
      RUNTIME_ASSERT(hres.msg == (char *) &hres);
      chunk = template_raw_chunk_new(chunk_http_response,
                (char *) &hres, sizeof hres);

      if (NULL == (sn = snode_new(chunk))) {
        CRIT("snode_new() failed");
        goto failure;
      }
      tpl->chunks = snode_prepend(tpl->chunks, sn);
    } else {
      size_t len;
      snode_t *sn;

      if (!isalpha((unsigned char) buf[0]) || NULL == strchr(buf, ':')) {
        WARN("Invalid HTTP header: \"%s\"", buf);
        goto failure;
      }

      for (p = buf; (c = (unsigned char) *p) != ':'; ++p)
        if (!isalpha(c) && c != '-') {
          WARN("Invalid character HTTP in header name: \"%s\"", buf);
          goto failure;
        }
      
      len = strlen(buf) + 1;
      chunk = template_raw_chunk_new(chunk_http_header, buf, len);
      if (NULL == (sn = snode_new(chunk))) {
        CRIT("snode_new() failed");
        goto failure;
      }
      tpl->chunks = snode_prepend(tpl->chunks, sn);
    }
  }

  if (feof(f)) {
    tpl->chunks = snode_reverse(tpl->chunks);
    return tpl;
  }
  
  q = buf;
  e = NULL;
  
  for (;;) {
    c = fgetc(f);
    if (c == EOF) {
      if (!ferror(f))
        break;
      
      CRIT("fgetc() failed: %s", compat_strerror(errno));
      goto failure;
    }
    
    if ((size_t) (q - buf) >= buf_len) {
      char *n;
      
      buf_len += 4096;
      n = realloc(buf, buf_len);
      if (!n) {
        CRIT("Out of memory");
        goto failure;
      }
      q = &n[q - buf];
      buf = n;
    }
    *q++ = c;

    if (c == ';' && e != NULL) {
      RUNTIME_ASSERT(e >= entity && e < &entity[sizeof entity]);
      *e = '\0';
      
      chunk = template_chunk_new(entity);
      if (chunk) {
        struct template_chunk *data;
        size_t data_len;
        snode_t *sn;
     
        data_len = (q - buf) - strlen(entity) - 2;
        RUNTIME_ASSERT(data_len <= INT_MAX);
        if (data_len > 0) {
          data = template_raw_chunk_new(chunk_data, buf, data_len);
          if (NULL == (sn = snode_new(data))) {
            CRIT("snode_new() failed");
            goto failure;
          }
          tpl->chunks = snode_prepend(tpl->chunks, sn);
      
          buf_len = 4096;
          buf = calloc(1, buf_len);
          if (!buf) {
            CRIT("Out of memory");
            goto failure;
          }
        }
        q = buf;
    
        if (NULL == (sn = snode_new(chunk))) {
          CRIT("snode_new() failed");
          goto failure;
        }
        tpl->chunks = snode_prepend(tpl->chunks, sn);
      }

      e = NULL;
    }
    
    if (e) {
      bool b = isalnum(c) || c == '.' || c == '_';
      
      if (b && e < &entity[sizeof entity - 1]) {
        *e++ = c;
      } else {
        e = NULL;
      }
    }
    
    if (c == '&') {
      e = entity;
    }
  }
  fclose(f);
  f = NULL;

  if (q != buf) {
    snode_t *sn;

    chunk = template_raw_chunk_new(chunk_data, buf, q - buf);
    if (NULL == (sn = snode_new(chunk))) {
      CRIT("snode_new() failed");
      goto failure;
    }
    tpl->chunks = snode_prepend(tpl->chunks, sn);
  }
  DO_FREE(buf);
 
  tpl->chunks = snode_reverse(tpl->chunks);
  
#if 0
  {
    size_t n = 0;
    
    snode_t *sn = tpl->chunks;
    while (sn) {
      struct gwc_data_chunk *data = sn->ptr;
      DBUG("data->type=%d; data->buf=%p; data->size=%d",
          (int) data->type, data->buf, (int) data->size);
      sn = sn->next;
      n += data->size;
    }
    
    DBUG("n=%d", (int) n);
  }
#endif
  
  return tpl;

failure:

  CRIT("Loading data template from \"%s\" failed.", filename);

  if (f) {
    fclose(f);
    f = NULL;
  }
  DO_FREE(buf);
  if (tpl) {
    while (tpl->chunks != NULL) {
      snode_t *sn = tpl->chunks->next;
    
      DO_FREE(tpl->chunks);
      tpl->chunks = sn;
    }
    DO_FREE(tpl);
  }
  return NULL;
}
コード例 #27
0
ファイル: http.c プロジェクト: gtk-gnutella/gwc
/**
 * Parses the payload of a HTTP "Range" header.
 *
 * A range with an "open end", is indicated by "end == (uint64_t) -1".
 *
 * @param s a NUL-terminated string containing the payload of a Ranger header.
 * @return 0 on failure, otherwise the amount of byte ranges in the header.
 */
size_t
http_parse_range(const char *s, struct byte_range_set **set_ptr)
{
  char *ep;
  int error;
  size_t n = 0;

  RUNTIME_ASSERT(s);

  if (set_ptr)
    *set_ptr = NULL;

  s = skip_spaces(s);
  s = skip_prefix(s, "bytes=");
  if (!s) {
    DBUG("Expected \"bytes=\"");
    goto failure;
  }
  
  while ('\0' != *s) {
    uint64_t start, end;
    
    s = skip_spaces(s);
    if (',' == *s) {
      s++;
      continue;
    }
    
    if ('-' == *s) {
      start = (uint64_t) -1;
      s++;

      if (!isdigit((unsigned char) *s)) {
        DBUG("Neither start nor end given");
        goto failure;
      }

    } else {
      start = parse_uint64(s, &ep, 10, &error);
      if (error || (uint64_t) -1 == start) {
        DBUG("Invalid range start");
        goto failure;
      }

      if ('-' != *ep) {
        DBUG("Expected '-'");
        goto failure;
      }

      s = ++ep;
    }

    if (!isdigit((unsigned char) *s)) {
      end = (uint64_t) -1;
    } else {
      end = parse_uint64(s, &ep, 10, &error);
      if (error || (uint64_t) -1 == end) {
        DBUG("Invalid range end");
        goto failure;
      }
      s = ep;
    }

    if ((uint64_t) -1 != start && start > end) {
      DBUG("range start is beyond range end");
      goto failure;
    }

    s = skip_spaces(s);
    if (',' == *s) {
      s++;
    } else if ('\0' != *s) {
      DBUG("bad character after byte range");
      goto failure;
    }

    n++;
    if (set_ptr) {
      struct byte_range_set *set = *set_ptr;

      if (!set) {
        const size_t min_num = 8;
        
        set = malloc(min_num * sizeof set->ranges + sizeof *set);
        if (!set) {
          CRIT("malloc() failed");
          goto failure;
        }
        set->n = 0;
        set->size = min_num;
        *set_ptr = set;
      }
      
      RUNTIME_ASSERT(*set_ptr);
      RUNTIME_ASSERT(set);
      
      if (set->n >= set->size) {
        void *p;

        set->size = 0 == set->size ? 8 : 2 * set->size;
        p = realloc(set, set->size * sizeof *set);
        if (!p) {
          CRIT("realloc() failed");
          goto failure;
        }
        *set_ptr = set = p;
      }

      set->ranges[set->n].start = start;
      set->ranges[set->n].end = end;
      set->n++;
    }
  }

  return n;

failure:

  DO_FREE(*set_ptr);
  
  return -1;
}