int infnb_it_next_digit(t_infnb_it *it, int *digit_l, int *digit_r) { if (!it || !digit_l || !digit_r) return (0); if (it->pos_l >= it->left->offset && it->pos_l < it->left->len) { *digit_l = char_index(it->left->data[it->pos_l], it->base); it->pos_l -= 1; if (it->pos_r >= it->right->offset && it->pos_r < it->right->len) { *digit_r = char_index(it->right->data[it->pos_r], it->base); it->pos_r -= 1; } else *digit_r = 0; return (1); } return (0); }
void Swig_fragment_emit(Node *n) { String *code; char *pc, *tok; String *t; String *mangle = 0; String *name = 0; String *type = 0; if (!fragments) { Swig_warning(WARN_FRAGMENT_NOT_FOUND, Getfile(n), Getline(n), "Fragment '%s' not found.\n", name); return; } name = Getattr(n,k_value); if (!name) { name = n; } type = Getattr(n,k_type); if (type) { mangle = Swig_string_mangle(type); } if (debug) Printf(stdout,"looking fragment %s %s\n",name, type); t = Copy(name); tok = Char(t); pc = char_index(tok,','); if (pc) *pc = 0; while (tok) { String *name = NewString(tok); if (mangle) Append(name,mangle); if (looking_fragments && Getattr(looking_fragments,name)) { return; } code = Getattr(fragments,name); if (debug) Printf(stdout,"looking subfragment %s\n", name); if (code && (Strcmp(code,k_ignore) != 0)) { String *section = Getmeta(code,k_section); Hash *nn = Getmeta(code,k_kwargs); if (!looking_fragments) looking_fragments = NewHash(); Setattr(looking_fragments,name,"1"); while (nn) { if (Equal(Getattr(nn,k_name),k_fragment)) { if (debug) Printf(stdout,"emitting fragment %s %s\n",nn, type); Setfile(nn, Getfile(n)); Setline(nn, Getline(n)); Swig_fragment_emit(nn); } nn = nextSibling(nn); } if (section) { File *f = Swig_filebyname(section); if (!f) { Swig_error(Getfile(code),Getline(code), "Bad section '%s' for code fragment '%s'\n", section,name); } else { if (debug) Printf(stdout,"emitting subfragment %s %s\n",name, section); if (debug) Printf(f,"/* begin fragment %s */\n",name); Printf(f,"%s\n",code); if (debug) Printf(f,"/* end fragment %s */\n\n",name); Setattr(fragments,name,k_ignore); Delattr(looking_fragments,name); } } } else if (!code && type) { SwigType *rtype = SwigType_typedef_resolve_all(type); if (!Equal(type,rtype)) { String *name = Copy(Getattr(n,k_value)); String *mangle = Swig_string_mangle(type); Append(name,mangle); Setfile(name, Getfile(n)); Setline(name, Getline(n)); Swig_fragment_emit(name); Delete(mangle); Delete(name); } Delete(rtype); } if (!code) { Swig_warning(WARN_FRAGMENT_NOT_FOUND, Getfile(n), Getline(n), "Fragment '%s' not found.\n", name); } tok = pc ? pc + 1 : 0; if (tok) { pc = char_index(tok,','); if (pc) *pc = 0; } Delete(name); } Delete(t); }
char *get_unused_env_prefix(void) { struct bucket *qhead, *qtail; extern char **environ; char **e; qhead = (struct bucket *)malloc(sizeof(struct bucket)); if (!qhead) { fprintf(stderr, "out of memory\n"); exit(1); } qhead->prefixlen = 0; qhead->first_node = NULL; qhead->next_bucket = NULL; for (e = environ; *e; e++) qhead->first_node = new_node(qhead->first_node, *e, strcspn(*e, "=")); qtail = qhead; while (1) { struct bucket *buckets[FANOUT]; struct node *bucketnode; int i, index; for (i = 0; i < FANOUT; i++) { buckets[i] = (struct bucket *)malloc(sizeof(struct bucket)); if (!buckets[i]) { fprintf(stderr, "out of memory\n"); exit(1); } buckets[i]->prefixlen = qhead->prefixlen + 1; buckets[i]->first_node = NULL; qtail->next_bucket = buckets[i]; qtail = buckets[i]; } qtail->next_bucket = NULL; bucketnode = qhead->first_node; while (bucketnode) { struct node *node = bucketnode; bucketnode = bucketnode->next; if (node->len <= qhead->prefixlen) continue; index = char_index(node->string[qhead->prefixlen]); if (!(index >= 0 && index < FANOUT)) continue; node->prefixlen++; node->next = buckets[index]->first_node; buckets[index]->first_node = node; } for (i = 0; i < FANOUT; i++) { if (!buckets[i]->first_node) { char *ret = malloc(qhead->prefixlen + 2); if (!ret) { fprintf(stderr, "out of memory\n"); exit(1); } memcpy(ret, qhead->first_node->string, qhead->prefixlen); ret[qhead->prefixlen] = i + 'A'; ret[qhead->prefixlen + 1] = '\0'; /* This would be where we freed everything, if we * didn't know it didn't matter because we were * imminently going to exec another program */ return ret; } } qhead = qhead->next_bucket; } }
/** Initializes a read file structure. * Clears a new read file structure, copies the file path, sets the record * size, and sets up its thread mutex. * * @param[in] file The file structure. * @param[in] records_per_block The number of records per block. * @param[in] start Start string. * @param[in] end End string. * @param[in] alphabet Alphabet string to use. */ int brute_force_init(file_st *file, int records_per_block, char *start, char *end, char *alphabet) { brute_force_data_st *bf_st; size_t start_len; size_t end_len; size_t alp_len; int i; start_len = strlen(start); end_len = strlen(end); alp_len = strlen(alphabet); /* Start can not be longer than end */ if (start_len > end_len) { errno = EINVAL; return E_ATTK_SYSTEM; } /* Every character in start must be in the alphabet */ for (i = 0; i < start_len; i++) { if (char_index(alphabet, alp_len, start + i) == NULL) { errno = EINVAL; return E_ATTK_SYSTEM; } } /* Every character in end must be in the alphabet */ for (i = 0; i < end_len; i++) { if (char_index(alphabet, alp_len, end + i) == NULL) { errno = EINVAL; return E_ATTK_SYSTEM; } } /* If they are the same size, start can not be greater than end */ if (start_len == end_len) { for (i = 0; i < start_len; i++) { if (char_index(alphabet, alp_len, start + i) > char_index(alphabet, alp_len, end + i)) { errno = EINVAL; return E_ATTK_SYSTEM; } if (char_index(alphabet, alp_len, start + i) < char_index(alphabet, alp_len, end + i)) { break; } } } /* Clear the structure */ memset(file, 0, sizeof(file_st)); /* Set defaults */ file->record_size = strlen(end) + 1; file->records_per_block = records_per_block; /* Initialize pthread objects */ pthread_mutex_init(&(file->mut), NULL); /* Create brute_force_data_st */ bf_st = malloc(sizeof(brute_force_data_st)); file->file_data = bf_st; /* Create buffers */ bf_st->start = malloc(strlen(start) + 1); bf_st->end = malloc(strlen(end) + 1); bf_st->alphabet = malloc(strlen(alphabet) + 1); bf_st->last = malloc(strlen(end) + 1); memset(bf_st->start, 0, strlen(start) + 1); memset(bf_st->end, 0, strlen(end) + 1); memset(bf_st->alphabet, 0, strlen(alphabet) + 1); memset(bf_st->last, 0, strlen(end) + 1); /* Fill buffers */ memcpy(bf_st->start, start, strlen(start)); memcpy(bf_st->end, end, strlen(end)); memcpy(bf_st->alphabet, alphabet, strlen(alphabet)); /* Setup class methods */ file->open_file = bf_open_file; file->next_block = bf_next_block; file->free_block = bf_free_block; file->close_file = bf_close_file; return 0; }