//Ping_decision compares the nb of members in our network and in the neighbor's network. //If we are larger than them, we send the networK. //Else if it is equal, we have to start an election... void ping_decision(uint8_t neighbor, uint8_t membersInHisNetwork, uint8_t interface){ int8_t membersInMyNetwork = getMembersInNetwork(); if(membersInHisNetwork < membersInMyNetwork){ election_send_network(neighbor, membersInHisNetwork, interface); } else if(membersInHisNetwork == membersInMyNetwork){ for(int i=0; i<4; i ++){ if(!block_in(i)){ //Starting an election. uint8_t a = 0; xQueueSend(xElectionQueue, &a,0); break; } } } }
static int ok_read(BIO *b, char *out, int outl) { int ret = 0, i, n; BIO_OK_CTX *ctx; if (out == NULL) return (0); ctx = (BIO_OK_CTX *)b->ptr; if ((ctx == NULL) || (b->next_bio == NULL) || (b->init == 0)) return (0); while (outl > 0) { /* copy clean bytes to output buffer */ if (ctx->blockout) { i = ctx->buf_len - ctx->buf_off; if (i > outl) i = outl; memcpy(out, &(ctx->buf[ctx->buf_off]), i); ret += i; out += i; outl -= i; ctx->buf_off += i; /* all clean bytes are out */ if (ctx->buf_len == ctx->buf_off) { ctx->buf_off = 0; /* * copy start of the next block into proper place */ if (ctx->buf_len_save - ctx->buf_off_save > 0) { ctx->buf_len = ctx->buf_len_save - ctx->buf_off_save; memmove(ctx->buf, &(ctx->buf[ctx->buf_off_save]), ctx->buf_len); } else { ctx->buf_len = 0; } ctx->blockout = 0; } } /* output buffer full -- cancel */ if (outl == 0) break; /* no clean bytes in buffer -- fill it */ n = IOBS - ctx->buf_len; i = BIO_read(b->next_bio, &(ctx->buf[ctx->buf_len]), n); if (i <= 0) break; /* nothing new */ ctx->buf_len += i; /* no signature yet -- check if we got one */ if (ctx->sigio == 1) { if (!sig_in(b)) { BIO_clear_retry_flags(b); return 0; } } /* signature ok -- check if we got block */ if (ctx->sigio == 0) { if (!block_in(b)) { BIO_clear_retry_flags(b); return 0; } } /* invalid block -- cancel */ if (ctx->cont <= 0) break; } BIO_clear_retry_flags(b); BIO_copy_next_retry(b); return (ret); }
void ref_test(const char *in_file, const unsigned int it_cnt, enum test_type t_type, AESREF alg) { u4byte i, kl, test_no, cnt, e_cnt; u1byte key[32], pt[16], iv[16], ect[16], act[32]; char str[128], tstr[16]; int ty; IFILE inf; con_string("\nTest file: "); con_string(in_file); con_string("\nStatus: \n"); if(!(inf = open_ifile(inf, in_file))) // reference file for test vectors { // if file is not present con_string("error in running test\n"); return; } cnt = 0; e_cnt = test_no = 0; for(;;) // while there are tests { ty = find_line(inf, str); // input a line if(ty < 0) // until end of file break; switch(ty) // process line type { case 0: kl = get_dec(str + 8); continue; // key length case 1: test_no = get_dec(str + 2); continue; // test number case 2: block_in(iv, str + 3); continue; // init vector case 3: block_in(key, str + 4); continue; // key case 4: block_in(pt, str + 3); // plaintext if(t_type != ecb_md && t_type != cbc_md) continue; break; case 5: block_in(ect, str + 3); // ciphertext if(t_type == ecb_md || t_type == cbc_md) continue; break; } if(serpent_hack) block_reverse(key, kl / 8); alg.set_key(key, kl, both); // set the key if(it_cnt > 100) OUT_DOTS(test_no); if(t_type == ecb_md || t_type == cbc_md) { block_copy(act, ect, 16); // encrypted text to low block if(t_type == cbc_md) // CBC Monte Carlo decryption { block_copy(act + 16, iv, 16); // IV to high block for(i = 0; i < it_cnt; i += 2) // do decryptions two at a time { if(serpent_hack) block_reverse(act, 16); alg.decrypt(act, ect); // decrypt low block if(serpent_hack) { block_reverse(act, 16); block_reverse(ect, 16); } block_xor(act + 16, ect, 16);// xor into high block if(serpent_hack) block_reverse(act + 16, 16); alg.decrypt(act + 16, ect); // decrypt high block if(serpent_hack) { block_reverse(ect, 16); block_reverse(act + 16, 16); } block_xor(act, ect, 16); // xor into low block } } else // ECB Monte Carlo decryption { if(serpent_hack) block_reverse(act, 16); for(i = 0; i < it_cnt; ++i) alg.decrypt(act, act); if(serpent_hack) block_reverse(act, 16); } if(!block_cmp(pt, act, 16)) { con_string("\n\ndecryption error on test "); put_dec(tstr, test_no); con_string(tstr); e_cnt++; } if(t_type == ecb_md) // test encryption if ECB mode { if(serpent_hack) block_reverse(act, 16); for(i = 0; i < it_cnt; ++i) alg.encrypt(act, act); if(serpent_hack) block_reverse(act, 16); if(!block_cmp(ect, act, 16)) { con_string("\n\nencryption error on test "); put_dec(tstr, test_no); con_string(tstr); e_cnt++; } } } else // if(t_type == ecb_me || t_type == cbc_me || ecb_vk || ecb_vt) { if(t_type == cbc_me) // CBC Monte Carlo encryption { block_copy(act, iv, 16); block_copy(act + 16, pt, 16); // copy IV and plaintext for(i = 0; i < it_cnt; i += 2) { block_xor(act + 16, act, 16); // xor low block into high block if(serpent_hack) block_reverse(act + 16, 16); alg.encrypt(act + 16, act + 16); // encrypt high block if(serpent_hack) block_reverse(act + 16, 16); block_xor(act, act + 16, 16); // xor high block into low block if(serpent_hack) block_reverse(act, 16); alg.encrypt(act, act); // encrypt low block if(serpent_hack) block_reverse(act, 16); } } else // ECB Monte Carlo encryption { block_copy(act, pt, 16); if(serpent_hack) block_reverse(act, 16); for(i = 0; i < it_cnt; ++i) alg.encrypt(act, act); if(serpent_hack) block_reverse(act, 16); } if(!block_cmp(ect, act, 16)) { con_string("\n\nencryption error on test "); put_dec(tstr, test_no); con_string(tstr); e_cnt++; } if(t_type != cbc_me) // if ECB mode test decrytpion { if(serpent_hack) block_reverse(act, 16); for(i = 0; i < it_cnt; ++i) alg.decrypt(act, act); if(serpent_hack) block_reverse(act, 16); if(!block_cmp(pt, act, 16)) { con_string("\n\ndecryption error on test "); put_dec(tstr, test_no); con_string(tstr); e_cnt++; } } } } close_ifile(inf); if(e_cnt > 0) // report any errors { put_dec(tstr, e_cnt); con_string("\n"); con_string(tstr); con_string(" errors during test\n"); } else // else report all is well con_string("\nall tests correct\n"); }
/*! indicates that a block of data is received */ void TEPty::dataReceived(KProcess *, char *buf, int len) { emit block_in(buf, len); }
static gboolean mk_reg(FILE * in, struct rdup *e, GHashTable * uidhash, GHashTable * gidhash) { FILE *out = NULL; char *buf; gchar *parent; size_t bytes; gboolean ok = TRUE; gboolean old_dry = opt_dry; struct stat *st; /* with opt_dry we can't just return TRUE; as we may * need to suck in the file's content - which is thrown * away in that case */ if (!e->f_name) { /* fake an opt_dry */ opt_dry = TRUE; } if (!opt_dry) { if (!rm(e->f_name)) { opt_dry = old_dry; return FALSE; } } if (!opt_dry && !(out = fopen(e->f_name, "w"))) { if (errno == EACCES) { parent = dir_parent(e->f_name); st = dir_write(parent); if (!(out = fopen(e->f_name, "w"))) { msgd(__func__, __LINE__, _("Failed to open file `%s\': %s"), e->f_name, strerror(errno)); g_free(parent); ok = FALSE; } dir_restore(parent, st); g_free(parent); } else { msgd(__func__, __LINE__, _("Failed to open file `%s\': %s"), e->f_name, strerror(errno)); ok = FALSE; } } /* we need to read the input to not upset * the flow into rdup-up, but we are not * creating anything when opt_dry is active */ buf = g_malloc(BUFSIZE + 1); while ((bytes = block_in_header(in)) > 0) { if (block_in(in, bytes, buf) == -1) { if (out) fclose(out); opt_dry = old_dry; g_free(buf); return FALSE; } if (ok && !opt_dry) { if (fwrite(buf, sizeof(char), bytes, out) != bytes) { msgd(__func__, __LINE__, _("Write failure `%s\': %s"), e->f_name, strerror(errno)); if (out) fclose(out); opt_dry = old_dry; g_free(buf); return FALSE; } } } g_free(buf); if (ok && out) fclose(out); if (ok && !opt_dry) mk_meta(e, uidhash, gidhash); #ifdef DEBUG msgd(__func__, __LINE__, "Wrote file `%s\'", e->f_name); #endif /* DEBUG */ opt_dry = old_dry; return TRUE; }
Block MergeSortingBlockInputStream::readImpl() { /** Algorithm: * - read to memory blocks from source stream; * - if too much of them and if external sorting is enabled, * - merge all blocks to sorted stream and write it to temporary file; * - at the end, merge all sorted streams from temporary files and also from rest of blocks in memory. */ /// If has not read source blocks. if (!impl) { while (Block block = children.back()->read()) { if (!sample_block) { sample_block = block.cloneEmpty(); removeConstantsFromSortDescription(sample_block, description); } /// If there were only const columns in sort description, then there is no need to sort. /// Return the blocks as is. if (description.empty()) return block; removeConstantsFromBlock(block); blocks.push_back(block); sum_bytes_in_blocks += block.bytes(); /** If too much of them and if external sorting is enabled, * will merge blocks that we have in memory at this moment and write merged stream to temporary (compressed) file. * NOTE. It's possible to check free space in filesystem. */ if (max_bytes_before_external_sort && sum_bytes_in_blocks > max_bytes_before_external_sort) { temporary_files.emplace_back(new Poco::TemporaryFile(tmp_path)); const std::string & path = temporary_files.back()->path(); WriteBufferFromFile file_buf(path); CompressedWriteBuffer compressed_buf(file_buf); NativeBlockOutputStream block_out(compressed_buf); MergeSortingBlocksBlockInputStream block_in(blocks, description, max_merged_block_size, limit); LOG_INFO(log, "Sorting and writing part of data into temporary file " + path); ProfileEvents::increment(ProfileEvents::ExternalSortWritePart); copyData(block_in, block_out, &is_cancelled); /// NOTE. Possibly limit disk usage. LOG_INFO(log, "Done writing part of data into temporary file " + path); blocks.clear(); sum_bytes_in_blocks = 0; } } if ((blocks.empty() && temporary_files.empty()) || isCancelled()) return Block(); if (temporary_files.empty()) { impl = std::make_unique<MergeSortingBlocksBlockInputStream>(blocks, description, max_merged_block_size, limit); } else { /// If there was temporary files. ProfileEvents::increment(ProfileEvents::ExternalSortMerge); LOG_INFO(log, "There are " << temporary_files.size() << " temporary sorted parts to merge."); /// Create sorted streams to merge. for (const auto & file : temporary_files) { temporary_inputs.emplace_back(std::make_unique<TemporaryFileStream>(file->path())); inputs_to_merge.emplace_back(temporary_inputs.back()->block_in); } /// Rest of blocks in memory. if (!blocks.empty()) inputs_to_merge.emplace_back(std::make_shared<MergeSortingBlocksBlockInputStream>(blocks, description, max_merged_block_size, limit)); /// Will merge that sorted streams. impl = std::make_unique<MergingSortedBlockInputStream>(inputs_to_merge, description, max_merged_block_size, limit); } } Block res = impl->read(); if (res) enrichBlockWithConstants(res, sample_block); return res; }