resizable_buf rb_new(int init_size) { resizable_buf b = (resizable_buf) malloc_or_die(sizeof(struct st_resizable_buf)) ; b->allocated_size = init_size ; b->size = 0 ; b->data = (char*) malloc_or_die(init_size) ; return b ; }
seq_dumps_t* seq_dumps_create() { seq_dumps_t* d = malloc_or_die(sizeof(seq_dumps_t)); d->n = 0; d->size = 64; d->fns = malloc_or_die(d->size * sizeof(char*)); return d; }
static char *read_string(void) { char buf[BUFSIZ]; char *str = NULL; int size = 0; int i; int r; for (;;) { r = read(input_fd, buf, BUFSIZ); if (r < 0) die("reading input file"); if (!r) die("no data"); for (i = 0; i < r; i++) { if (!buf[i]) break; } if (i < r) break; if (str) { size += BUFSIZ; str = realloc(str, size); if (!str) die("malloc of size %d", size); memcpy(str + (size - BUFSIZ), buf, BUFSIZ); } else { size = BUFSIZ; str = malloc_or_die(size); memcpy(str, buf, size); } } /* trailing \0: */ i++; /* move the file descriptor to the end of the string */ r = lseek(input_fd, -(r - i), SEEK_CUR); if (r < 0) die("lseek"); if (str) { size += i; str = realloc(str, size); if (!str) die("malloc of size %d", size); memcpy(str + (size - i), buf, i); } else { size = i; str = malloc_or_die(i); memcpy(str, buf, i); } return str; }
struct sr_eth_pkt* read_ethernet_frame( uint8_t* frame, unsigned int len) { struct sr_eth_pkt* processed = (struct sr_eth_pkt*) malloc_or_die(len); uint8_t* tmp = (uint8_t*) malloc_or_die(len - ETH_HDR_LEN); tmp = (frame+14); processed->header = (struct sr_ethernet_hdr*) frame; processed->payload = tmp; printf("***************** READ IN THE ETHERNET FRAME\n"); return processed; }
static int decrypt_tarzan() { int fd; int i; char msg[512]; if(argc != 3) { fprintf(stderr, "Usage:\n%s <file1.bin> <result.bin>\n", argv[0]); exit(1); } sprintf(msg, "Open %s", argv[1]); fd = open(argv[1], O_RDONLY); if(fd<0) { perror(msg); exit(2); } file1_size = lseek(fd, 0, SEEK_END); lseek(fd, 0, SEEK_SET); file1_data = malloc_or_die(file1_size); read(fd, file1_data, file1_size); close(fd); result_data = malloc_or_die(file1_size); for(i=0; i<file1_size/2; i++) { UINT16 x = file1_data[i]; if((i & 0x10c0) == 0x0000) x ^= 0x0001; if((i & 0x0010) == 0x0010 || (i & 0x0130) == 0x0020) x ^= 0x0404; if((i & 0x00d0) != 0x0010) x ^= 0x1010; if(((i & 0x0008) == 0x0008)^((i & 0x10c0) == 0x0000)) x ^= 0x0100; result_data[i] = x; } sprintf(msg, "Open %s", argv[2]); fd = open(argv[2], O_RDWR|O_CREAT|O_TRUNC, 0666); if(fd<0) { perror(msg); exit(2); } write(fd, result_data, file1_size); close(fd); return 0; }
static void load_plugin(struct pevent *pevent, const char *path, const char *file, void *data) { struct plugin_list **plugin_list = data; pevent_plugin_load_func func; struct plugin_list *list; struct plugin_option *options; const char *alias; char *plugin; void *handle; plugin = malloc_or_die(strlen(path) + strlen(file) + 2); strcpy(plugin, path); strcat(plugin, "/"); strcat(plugin, file); handle = dlopen(plugin, RTLD_NOW | RTLD_GLOBAL); if (!handle) { warning("cound not load plugin '%s'\n%s\n", plugin, dlerror()); goto out_free; } alias = dlsym(handle, PEVENT_PLUGIN_ALIAS_NAME); if (!alias) alias = file; options = dlsym(handle, PEVENT_PLUGIN_OPTIONS_NAME); if (options) { while (options->name) { update_option(alias, options); options++; } } func = dlsym(handle, PEVENT_PLUGIN_LOADER_NAME); if (!func) { warning("cound not find func '%s' in plugin '%s'\n%s\n", PEVENT_PLUGIN_LOADER_NAME, plugin, dlerror()); goto out_free; } list = malloc_or_die(sizeof(*list)); list->next = *plugin_list; list->handle = handle; list->name = plugin; *plugin_list = list; pr_stat("registering plugin: %s", plugin); func(pevent); return; out_free: free(plugin); }
str_t* str_alloc() { str_t* s = malloc_or_die(sizeof(str_t)); s->s = malloc_or_die(init_str_size); s->n = 0; s->size = init_str_size; str_clear(s); return s; }
static int find_event(struct pevent *pevent, struct event_list **events, char *sys_name, char *event_name) { struct event_format *event; regex_t ereg; regex_t sreg; int match = 0; char *reg; int ret; int i; if (!event_name) { /* if no name is given, then swap sys and name */ event_name = sys_name; sys_name = NULL; } reg = malloc_or_die(strlen(event_name) + 3); sprintf(reg, "^%s$", event_name); ret = regcomp(&ereg, reg, REG_ICASE|REG_NOSUB); free(reg); if (ret) return -1; if (sys_name) { reg = malloc_or_die(strlen(sys_name) + 3); sprintf(reg, "^%s$", sys_name); ret = regcomp(&sreg, reg, REG_ICASE|REG_NOSUB); free(reg); if (ret) { regfree(&ereg); return -1; } } for (i = 0; i < pevent->nr_events; i++) { event = pevent->events[i]; if (event_match(event, sys_name ? &sreg : NULL, &ereg)) { match = 1; add_event(events, event); } } regfree(&ereg); if (sys_name) regfree(&sreg); if (!match) return -1; return 0; }
static void copy_event_system(const char *sys, struct tracepoint_path *tps) { unsigned long long size, check_size; struct dirent *dent; struct stat st; char *format; DIR *dir; int count = 0; int ret; dir = opendir(sys); if (!dir) die("can't read directory '%s'", sys); while ((dent = readdir(dir))) { if (dent->d_type != DT_DIR || strcmp(dent->d_name, ".") == 0 || strcmp(dent->d_name, "..") == 0 || !name_in_tp_list(dent->d_name, tps)) continue; format = malloc_or_die(strlen(sys) + strlen(dent->d_name) + 10); sprintf(format, "%s/%s/format", sys, dent->d_name); ret = stat(format, &st); free(format); if (ret < 0) continue; count++; } write_or_die(&count, 4); rewinddir(dir); while ((dent = readdir(dir))) { if (dent->d_type != DT_DIR || strcmp(dent->d_name, ".") == 0 || strcmp(dent->d_name, "..") == 0 || !name_in_tp_list(dent->d_name, tps)) continue; format = malloc_or_die(strlen(sys) + strlen(dent->d_name) + 10); sprintf(format, "%s/%s/format", sys, dent->d_name); ret = stat(format, &st); if (ret >= 0) { /* unfortunately, you can not stat debugfs files for size */ size = get_size(format); write_or_die(&size, 8); check_size = copy_file(format); if (size != check_size) die("error in size of file '%s'", format); } free(format); } closedir(dir); }
/* Adds a freestanding variable to the variable list. */ void Cvar_RegisterVariable (ConsoleVariable_t *variable,void (*Function)(void)) { char *oldstr; ConsoleVariable_t *cursor,*prev; //johnfitz -- sorted list insert // First check to see if it has allready been defined if (Cvar_FindVar (variable->name)) { Con_Printf ("Can't register variable %s, allready defined\n", variable->name); return; } // Check for overlap with a command if (Cmd_Exists (variable->name)) { Con_Printf ("Cvar_RegisterVariable: %s is a command\n", variable->name); return; } // copy the value off, because future sets will free it oldstr = variable->string; variable->string = (char*)malloc_or_die(strlen(variable->string) + 1); strcpy(variable->string, oldstr); Cvar_UpdateValues(variable); //johnfitz -- save initial value for "reset" command variable->default_string = (char*)malloc_or_die(strlen(variable->string) + 1); strcpy(variable->default_string, oldstr); //johnfitz // link the variable in //johnfitz -- insert each entry in alphabetical order if(!cConsoleVariables || strcmp(variable->name, cConsoleVariables->name) < 0) //insert at front { variable->next = cConsoleVariables; cConsoleVariables = variable; } else //insert later { prev = cConsoleVariables; cursor = cConsoleVariables->next; while (cursor && (strcmp(variable->name, cursor->name) > 0)) { prev = cursor; cursor = cursor->next; } variable->next = prev->next; prev->next = variable; } //johnfitz variable->callback = Function; //johnfitz }
void ip_handle_packet( sr_router* router, byte* ip_packet, interface_t* intf ) { printf(" ** ip_handle_packet(..) called \n"); // construct ip header struct ip* ip_header = make_ip_header(ip_packet); byte* ip_header_raw = (byte*) malloc_or_die(ip_header->ip_hl * 4); memcpy(ip_header_raw, ip_packet, ip_header->ip_hl * 4); // check if ip header is correct bool ret = check_ip_header(ip_header, ip_header_raw); if(ret == TRUE) { printf(" ** ip_handle_packet(..) header sanity checks passed\n"); // display ip header printf(" ** ip_handle_packet(..) packet contents \n"); display_ip_header(ip_header); // get payload uint16_t payload_len = ip_header->ip_len - (ip_header->ip_hl * 4); printf(" ** ip_handle_packet(..) Payload size %u bytes \n", payload_len); byte* payload = (byte*) malloc_or_die(payload_len); memcpy(payload, ip_packet + (ip_header->ip_hl * 4), payload_len); //free(ip_header_raw); // check destination printf(" ** ip_handle_packet(..) checking packet destination \n"); // check if spf broadcast packet char* all_spf_routers = ALL_SPF_ROUTERS_IP; if(check_packet_destination(ip_header->ip_dst, router) == TRUE || ip_header->ip_dst.s_addr == make_ip_addr(all_spf_routers)) { printf(" ** ip_handle_packet(..) yes I'm the destination \n"); // check if ttl expired /* if(ip_header->ip_ttl == 1) { printf(" ** ip_handle_packet(..) ttl expired, sending ICMP ttl error message \n"); uint8_t code = ICMP_TYPE_CODE_TTL_TRANSIT; icmp_type_ttl_send(&code, payload, (uint8_t*)&ip_header->ip_len, ip_header); } else*/ { // check protocol printf(" ** ip_handle_packet(..) checking protocol \n"); check_packet_protocol(router, ip_header, payload, payload_len, intf, ip_packet); } } else { printf(" ** ip_handle_packet(..) no I'm not the destination, send to ip lookup \n"); ip_header->ip_len = htons((ip_header->ip_len)); ip_header->ip_off = htons((ip_header->ip_off)); ip_header->ip_id = htons((ip_header->ip_id)); ip_header->ip_sum = htons((ip_header->ip_sum)); ip_look_up_reply(ip_packet, ip_header, intf); } } else printf(" ** ip_handle_packet(..) header sanity checks failed\n"); //free(ip_packet); //free(ip_header); //free(ip_header_raw); }
hash_table* create_hash_table() { hash_table* T = malloc_or_die(sizeof(hash_table)); T->A = malloc_or_die(INITIAL_TABLE_SIZE * sizeof(hashed_value*)); memset(T->A, 0, INITIAL_TABLE_SIZE * sizeof(hashed_value*)); T->n = INITIAL_TABLE_SIZE; T->m = 0; T->max_m = T->n * MAX_LOAD; return T; }
/* blob= b_new(size) - create a new empty blob with space for size bytes */ INLINE blob_t * b_new(size_t size) { blob_t *b; b = malloc_or_die(sizeof(blob_t)); BLOB_NEXT_set(b, NULL); BLOB_REF_PTR_set(b, malloc_or_die(sizeof(_refcnt_blob_t) + size)); BLOB_REFCNT_set(b, 1); /* overwritten in enqueue_blob_for_transmision */ BLOB_BUF_SIZE_set(b, size); (void)get_time(&BLOB_RECEIVED_TIME(b)); return b; }
fastq_t* fastq_open(FILE* f) { fastq_t* fqf = malloc_or_die(sizeof(fastq_t)); or_die((int)((fqf->file = gzdopen(fileno(f), "rb")) != NULL), "Can not open gzip file."); fqf->state = STATE_ID1; fqf->buf = malloc_or_die(fastq_buf_size); fqf->buf[0] = '\0'; fqf->c = fqf->buf; return fqf; }
seq_array_t* seq_array_create(size_t data_size) { seq_array_t* a = malloc_or_die(sizeof(seq_array_t)); a->size = 1024; a->n = 0; a->seqs = malloc_or_die(a->size * sizeof(seq_t)); a->data_size = data_size; a->data_used = 0; a->data = malloc_or_die(data_size); return a; }
arr_t * arr_new(int n, int item_sz) { arr_t *arr; arr = malloc_or_die(sizeof(*arr)); arr->n = n; arr->item_sz = item_sz; arr->ptr = malloc_or_die(item_sz * n); return arr; }
/* n-way merge sort to stdout */ void merge_sort(const seq_dumps_t* d, int (*cmp)(const void*, const void*)) { FILE** files = malloc_or_die(d->n * sizeof(FILE*)); size_t i; for (i = 0; i < d->n; ++i) { files[i] = fopen(d->fns[i], "rb"); if (files[i] == NULL) { fprintf(stderr, "Cannot open temporary file %s for reading.\n", d->fns[i]); exit(EXIT_FAILURE); } } fastq_t** fs = malloc_or_die(d->n * sizeof(fastq_t*)); seq_t** seqs = malloc_or_die(d->n * sizeof(seq_t*)); for (i = 0; i < d->n; ++i) { fs[i] = fastq_create(files[i]); seqs[i] = seq_create(); } /* A binary heap of indexes to fs. We use this to repeatedly pop the * smallest fastq entry. */ size_t* heap = malloc_or_die(d->n * sizeof(size_t)); /* heap size */ size_t m = 0; for (i = 0; i < d->n; ++i) { if (fastq_read(fs[i], seqs[i])) { heap_push(heap, d->n, &m, seqs, cmp, i); } } while (m > 0) { i = heap_pop(heap, &m, seqs, cmp); fastq_print(stdout, seqs[i]); if (fastq_read(fs[i], seqs[i])) { heap_push(heap, d->n, &m, seqs, cmp, i); } } for (i = 0; i < d->n; ++i) { seq_free(seqs[i]); fastq_free(fs[i]); fclose(files[i]); } free(files); free(fs); }
struct sr_ip_pkt* read_ip_pkt(uint8_t* raw, unsigned int len) { printf("receiving %d bytes\n", len); assert(raw); struct sr_ip_pkt* pkt = (struct sr_ip_pkt*) malloc_or_die(len); assert(pkt); uint8_t* tmp = (uint8_t*) malloc_or_die(len - sizeof(struct ip)); assert(tmp); //tmp will be the payload pkt->header = (struct ip*) raw; tmp = raw + sizeof(struct ip); pkt->payload = tmp; printf("************************ READ IN AN IP PACKET\n"); return pkt; }
static void copy_event_system(const char *sys, struct tracepoint_path *tps) { struct dirent *dent; struct stat st; char *format; DIR *dir; int count = 0; int ret; dir = opendir(sys); if (!dir) die("can't read directory '%s'", sys); while ((dent = readdir(dir))) { if (dent->d_type != DT_DIR || strcmp(dent->d_name, ".") == 0 || strcmp(dent->d_name, "..") == 0 || !name_in_tp_list(dent->d_name, tps)) continue; format = malloc_or_die(strlen(sys) + strlen(dent->d_name) + 10); sprintf(format, "%s/%s/format", sys, dent->d_name); ret = stat(format, &st); free(format); if (ret < 0) continue; count++; } write_or_die(&count, 4); rewinddir(dir); while ((dent = readdir(dir))) { if (dent->d_type != DT_DIR || strcmp(dent->d_name, ".") == 0 || strcmp(dent->d_name, "..") == 0 || !name_in_tp_list(dent->d_name, tps)) continue; format = malloc_or_die(strlen(sys) + strlen(dent->d_name) + 10); sprintf(format, "%s/%s/format", sys, dent->d_name); ret = stat(format, &st); if (ret >= 0) record_file(format, 8); free(format); } closedir(dir); }
static void read_header_files(void) { unsigned long long size; char *header_event; char buf[BUFSIZ]; read_or_die(buf, 12); if (memcmp(buf, "header_page", 12) != 0) die("did not read header page"); size = read8(); skip(size); /* * The size field in the page is of type long, * use that instead, since it represents the kernel. */ long_size = header_page_size_size; read_or_die(buf, 13); if (memcmp(buf, "header_event", 13) != 0) die("did not read header event"); size = read8(); header_event = malloc_or_die(size); read_or_die(header_event, size); free(header_event); }
static mame_file_error mame_fopen_next(const char *pathoption, const char *extension, mame_file **file) { mame_file_error filerr; char *fname; int seq; /* allocate temp space for the name */ fname = malloc_or_die(strlen(Machine->basename) + 1 + 10 + strlen(extension) + 1); /* try until we succeed */ for (seq = 0; ; seq++) { sprintf(fname, "%s" PATH_SEPARATOR "%04d.%s", Machine->basename, seq, extension); filerr = mame_fopen(pathoption, fname, OPEN_FLAG_READ, file); if (filerr != FILERR_NONE) break; mame_fclose(*file); } /* create the final file */ filerr = mame_fopen(pathoption, fname, OPEN_FLAG_WRITE | OPEN_FLAG_CREATE | OPEN_FLAG_CREATE_PATHS, file); /* free the name and get out */ free(fname); return filerr; }
static int read_file(const char *file, char **buffer) { char *buf; int len = 0; int fd; int r; fd = open(file, O_RDONLY); if (fd < 0) return -1; buf = malloc_or_die(BUFSIZ + 1); while ((r = read(fd, buf + len, BUFSIZ)) > 0) { len += r; buf = realloc(buf, len + BUFSIZ + 1); if (!buf) { len = -1; goto out; } } *buffer = buf; buf[len] = 0; out: close(fd); return len; }
filelist filelist_load(Rawdata input, char *header) { char file_buffer[BUFSIZ] ; char *p = input->data ; char *lst = input->data + input->size ; filelist list = (filelist) malloc_or_die(sizeof(struct st_filelist)) ; /* read a line in file_buffer, and return 0 if everything has been read */ int readline() { /*{{{*/ if (p == NULL || p == lst) return FALSE ; char *s = p ; while (p < lst && *p != '\n') p++ ; char *dest = file_buffer ; while (s < p) { if (dest == file_buffer + BUFSIZ - 1) { C_log_error("Buffer overflow...\n") ; exit(-1); } *(dest++) = *(s++) ; } *dest = 0 ; p++ ; return TRUE ; /*}}}*/ } /* Error function */ void malformed() { C_log_error("[filelist_load] Malformed %s file...\n", header); exit(-1); }
resizable_buf rb_empty() { resizable_buf b = (resizable_buf) malloc_or_die(sizeof(struct st_resizable_buf)) ; b->allocated_size = b->size = 0 ; b->data = NULL ; return b ; }
quip_in_t* quip_in_open( quip_reader_t reader, void* reader_data, quip_fmt_t fmt, quip_opt_t opts, const seqmap_t* ref) { quip_in_t* in = malloc_or_die(sizeof(quip_in_t)); in->fmt = fmt; switch (fmt) { case QUIP_FMT_FASTQ: in->x.fastq = quip_fastq_in_open(reader, reader_data, opts); break; case QUIP_FMT_BAM: opts |= QUIP_OPT_SAM_BAM; case QUIP_FMT_SAM: in->x.sam = quip_sam_in_open(reader, reader_data, opts); break; case QUIP_FMT_QUIP: in->x.quip = quip_quip_in_open(reader, reader_data, opts, ref); break; default: break; } return in; }
size_t quip_file_reader(void* param, uint8_t* data, size_t datalen) { FILE* f = (FILE*) param; if (data == NULL) { if (fseek(f, datalen, SEEK_CUR) == 0) return datalen; else { /* The stream is not seekable, so we have * to read and discard datalen bytes. */ const size_t bufsize = 4096; size_t readcnt = 0; uint8_t* buf = malloc_or_die(bufsize); size_t remaining = datalen; while (remaining > 0) { readcnt = fread(buf, 1, remaining < bufsize ? remaining : bufsize, f); if (readcnt == 0) break; else remaining -= readcnt; } free(buf); return datalen - remaining; } } else return fread(data, 1, datalen, (FILE*) param); }
unsigned long* index_with_replacement(rng_t* rng, unsigned long n, unsigned long k) { unsigned long* xs = malloc_or_die(k * sizeof(unsigned long)); size_t i; for (i = 0; i < k; ++i) xs[i] = fastq_rng_uniform_int(rng, n); return xs; }
/** * trace_seq_init - initialize the trace_seq structure * @s: a pointer to the trace_seq structure to initialize */ void trace_seq_init(struct trace_seq *s) { s->len = 0; s->readpos = 0; s->buffer_size = TRACE_SEQ_BUF_SIZE; s->buffer = malloc_or_die(s->buffer_size); }
void trace_util_add_option(const char *name, const char *val) { struct trace_plugin_options *option; char *p; option = malloc_or_die(sizeof(*option)); memset(option, 0, sizeof(*option)); option->next = trace_plugin_options; trace_plugin_options = option; option->option = strdup(name); if (!option->option) die("malloc"); if ((p = strstr(option->option, ":"))) { option->plugin = option->option; *p = '\0'; option->option = strdup(p + 1); if (!option->option) die("malloc"); } if (val) { option->value = strdup(val); if (!option->value) die("malloc"); } }
static LRESULT send_id_string(running_machine *machine, HWND hwnd, LPARAM id) { copydata_id_string *temp; COPYDATASTRUCT copydata; const char *name; int datalen; // id 0 is the name of the game if (id == 0) name = machine->gamedrv->name; else name = output_id_to_name(id); // a NULL name is an empty string if (name == NULL) name = ""; // allocate memory for the message datalen = sizeof(*temp) + strlen(name); temp = malloc_or_die(datalen); temp->id = id; strcpy(temp->string, name); // reply by using SendMessage with WM_COPYDATA copydata.dwData = COPYDATA_MESSAGE_ID_STRING; copydata.cbData = datalen; copydata.lpData = temp; SendMessage(hwnd, WM_COPYDATA, (WPARAM)output_hwnd, (LPARAM)©data); // free the data free(temp); return 0; }