示例#1
0
文件: cbc.c 项目: ColinIanKing/zfs
int
cbc_init_ctx(cbc_ctx_t *cbc_ctx, char *param, size_t param_len,
    size_t block_size, void (*copy_block)(uint8_t *, uint64_t *))
{
	/*
	 * Copy IV into context.
	 *
	 * If cm_param == NULL then the IV comes from the
	 * cd_miscdata field in the crypto_data structure.
	 */
	if (param != NULL) {
		ASSERT(param_len == block_size);
		copy_block((uchar_t *)param, cbc_ctx->cbc_iv);
	}

	cbc_ctx->cbc_lastp = (uint8_t *)&cbc_ctx->cbc_iv[0];
	cbc_ctx->cbc_flags |= CBC_MODE;
	return (CRYPTO_SUCCESS);
}
示例#2
0
文件: mix_edit.cpp 项目: ChangerR/xcc
int Cmix_edit::compact()
{
	t_block_map block_map = Cmix_edit::block_map();
	int error = 0;
	int offset = cb_header(m_index.size());
	for (auto& i : block_map)
	{
		if (i.second->offset != offset)
		{
			assert(i.second->offset > offset);
			error = copy_block(m_f, i.second->offset, m_f, offset, i.second->size);
			if (error)
				break;
			i.second->offset = offset;
		}
		offset += i.second->size;
	}
	error = error ? write_index(), error : write_index();
	return error;
}
示例#3
0
/*
 * Moves block to empty space, only one of dx or dy can be set
 * to nonzero value and the value could only be 1 or -1.
 */
static int try_move_block(struct bogoman_map *map,
                           int x, int y, int dx, int dy)
{
	int new_x = (int)x + dx;
	int new_y = (int)y + dy;

	if (bogoman_coord_in_map(map, new_x, new_y)) {

		if (!bogoman_is_empty(map, new_x, new_y))
			return 0;

		copy_block(map, new_x, new_y, x, y);
	} else {
		DEBUG(1, "Block moved out of screen %ux%u -> %ix%i",
		      x, y, new_x, new_y);
	}

	clear_block(map, x, y);
	return 1;
}
示例#4
0
/* #pragma omp task/taskwait version of SWEEP. */
void sweep (int nx, int ny, double dx, double dy, double *f_,
            int itold, int itnew, double *u_, double *unew_, int block_size)
{
    int it;
    int block_x, block_y;

    if (block_size == 0)
        block_size = nx;

    int max_blocks_x = (nx / block_size);
    int max_blocks_y = (ny / block_size);

#pragma omp parallel \
    shared(u_, unew_, f_, max_blocks_x, max_blocks_y, nx, ny, dx, dy, itold, itnew, block_size) \
    private(it, block_x, block_y)
#pragma omp single
    {
        for (it = itold + 1; it <= itnew; it++) {
            // Save the current estimate.
            for (block_x = 0; block_x < max_blocks_x; block_x++) {
                for (block_y = 0; block_y < max_blocks_y; block_y++) {
#pragma omp task shared(u_, unew_, nx, ny, block_size) firstprivate(block_x, block_y)
                    copy_block(nx, ny, block_x, block_y, u_, unew_, block_size);
                }
            }

#pragma omp taskwait

            // Compute a new estimate.
            for (block_x = 0; block_x < max_blocks_x; block_x++) {
                for (block_y = 0; block_y < max_blocks_y; block_y++) {
#pragma omp task default(none) shared(u_, unew_, f_, dx, dy, nx, ny, block_size) firstprivate(block_x, block_y)
                    compute_estimate(block_x, block_y, u_, unew_, f_, dx, dy,
                                     nx, ny, block_size);
                }
            }

#pragma omp taskwait
        }
    }
}
示例#5
0
int
crypto_update_iov(void *ctx, crypto_data_t *input, crypto_data_t *output,
    int (*cipher)(void *, caddr_t, size_t, crypto_data_t *),
    void (*copy_block)(uint8_t *, uint64_t *))
{
	common_ctx_t *common_ctx = ctx;
	int rv;

	if (input->cd_miscdata != NULL) {
		copy_block((uint8_t *)input->cd_miscdata,
		    &common_ctx->cc_iv[0]);
	}

	if (input->cd_raw.iov_len < input->cd_length)
		return (CRYPTO_ARGUMENTS_BAD);

	rv = (cipher)(ctx, input->cd_raw.iov_base + input->cd_offset,
	    input->cd_length, (input == output) ? NULL : output);

	return (rv);
}
示例#6
0
文件: group.c 项目: lilliemarck/kloss
void merge_group(struct group *group, struct group *child)
{
    struct groupdata *groupdata = group->data;
    struct groupdata *childdata = child->data;

    for (size_t i = 0; i < ptrarray_count(childdata->blocks); ++i)
    {
        struct block *copy = copy_block(get_ptrarray(childdata->blocks, i));
        translate_block(copy, &child->position);
        push_ptrarray(groupdata->blocks, copy);
    }

    for (size_t i = 0; i < ptrarray_count(childdata->groups); ++i)
    {
        struct group *copy = copy_group(get_ptrarray(childdata->groups, i));
        vec3_add(&copy->position, &copy->position, &child->position);
        insert_group(group, copy);
    }

    destroy_group(child);
    update_group_vertexarray(group);
}
示例#7
0
/**
 * Tests whether the block I/O included a valid superblock. If it is not valid, return 1.
 * If there is an error, return an error code. If it is valid, return 0. After calling,
 * buf will contain the superblock (or what would have been the superblock if it had been
 * valid.
 */
extern int valid_ext3_superblock(struct bio *bio, char *buf) {
	/* TODO: make more sophisticated */
	struct ext3_super_block *sb;
	size_t start_offset;
	int ret;

	JPRINTK("testing superblock for validity");
	if (!bio_contains(bio, 2, 2)) {
		/* bio doesn't contain sector 2 or 3 */
		JPRINTK("no");
		return 1;
	}
	JPRINTK("yes");

	/* compute the first byte of the superblock's expected location */
	start_offset = (2 - bio->bi_sector) * 512;

	/* traverse data and load into buffer */
	if ((ret = copy_block(bio, buf, start_offset, sizeof(struct ext3_super_block))))
		return ret;
	sb = (struct ext3_super_block *) buf;

	/* do some sanity checks */
	/* TODO: this is pretty scant at best; also would be nice to detect when the 
	 * superblock is corrupt and wait until the OS repairs it */
	JPRINTK("inodes count: %d. blocks count: %d.", le32_to_cpu(sb->s_inodes_count), le32_to_cpu(sb->s_blocks_count));
	if (!(sb->s_inodes_count && 
	      sb->s_blocks_count &&
	      sb->s_inode_size &&
	      !(sb->s_inode_size & (sb->s_inode_size - 1)) // check power of 2
	      )) {
		JPRINTK("one was false!");
		JPRINTK("%d", le32_to_cpu(sb->s_inodes_count));
		JPRINTK("%d", le32_to_cpu(sb->s_blocks_count));
		return 1;
	}

	return 0;
}
示例#8
0
static struct lp_value *copy_value(struct lp_value *v) {
  struct lp_value *result = calloc(1, sizeof(struct lp_value));
  memcpy(result, v, sizeof(struct lp_value));
  switch(v->t) {
  case S:
    result->v.s = strdup(v->v.s);
    break;

  case LIST:
    result->v.l = copy_list(v->v.l);
    break;

  case BLOCK:
    result->v.b = copy_block(v->v.b);
    break;

  default:
    break;
  }
  
  return result;
}
示例#9
0
/* process group descriptors */
static int process_group_desc(
		struct bio *bio, 
		struct xen_vbd *vbd, 
		struct label *label
) {
	struct ext3_group_desc *desc;
	struct ljx_ext3_superblock *lsb = vbd->superblock;
	struct label *tlabel;
	unsigned int num_groups;
	int i, ret;
	void *buf;

	num_groups = label->nr_sec * SECTOR_SIZE / sizeof(struct ext3_group_desc);
	num_groups = lsb->groups_count;
	buf = kzalloc(num_groups * sizeof(struct ext3_group_desc), GFP_KERNEL);
	if ((ret = copy_block(bio, buf, 0, num_groups * sizeof(struct ext3_group_desc))))
		return ret;

	JPRINTK("scanning %u groups", num_groups);
	for (i = 0; i < num_groups; i++) {
		desc = (struct ext3_group_desc *) (buf + i * sizeof(struct ext3_group_desc));
		tlabel = insert_label(
				&vbd->label_list,
				le32_to_cpu(desc->bg_inode_table) * lsb->block_size,
				lsb->inodes_per_group * lsb->inode_size / SECTOR_SIZE,
				INODE_BLOCK);
		tlabel->processor = &process_inode_block;
		JPRINTK("group %u desc:", i);
		JPRINTK("\tblock_bitmap: %u, inode_bitmap: %u, used_dirs: %u",
				le32_to_cpu(desc->bg_block_bitmap),
				(unsigned int) le32_to_cpu(desc->bg_inode_bitmap),
				(unsigned int) le32_to_cpu(desc->bg_used_dirs_count));
	}

	kfree(buf);

	return 0;
}
示例#10
0
文件: c93.c 项目: markjreed/vice-emu
static int decode_frame(AVCodecContext *avctx, void *data,
                        int *got_frame, AVPacket *avpkt)
{
    const uint8_t *buf = avpkt->data;
    int buf_size = avpkt->size;
    C93DecoderContext * const c93 = avctx->priv_data;
    AVFrame * const newpic = c93->pictures[c93->currentpic];
    AVFrame * const oldpic = c93->pictures[c93->currentpic^1];
    GetByteContext gb;
    uint8_t *out;
    int stride, ret, i, x, y, b, bt = 0;

    if ((ret = ff_set_dimensions(avctx, WIDTH, HEIGHT)) < 0)
        return ret;

    c93->currentpic ^= 1;

    if ((ret = ff_reget_buffer(avctx, newpic)) < 0)
        return ret;

    stride = newpic->linesize[0];

    bytestream2_init(&gb, buf, buf_size);
    b = bytestream2_get_byte(&gb);
    if (b & C93_FIRST_FRAME) {
        newpic->pict_type = AV_PICTURE_TYPE_I;
        newpic->key_frame = 1;
    } else {
        newpic->pict_type = AV_PICTURE_TYPE_P;
        newpic->key_frame = 0;
    }

    for (y = 0; y < HEIGHT; y += 8) {
        out = newpic->data[0] + y * stride;
        for (x = 0; x < WIDTH; x += 8) {
            uint8_t *copy_from = oldpic->data[0];
            unsigned int offset, j;
            uint8_t cols[4], grps[4];
            C93BlockType block_type;

            if (!bt)
                bt = bytestream2_get_byte(&gb);

            block_type= bt & 0x0F;
            switch (block_type) {
            case C93_8X8_FROM_PREV:
                offset = bytestream2_get_le16(&gb);
                if ((ret = copy_block(avctx, out, copy_from, offset, 8, stride)) < 0)
                    return ret;
                break;

            case C93_4X4_FROM_CURR:
                copy_from = newpic->data[0];
            case C93_4X4_FROM_PREV:
                for (j = 0; j < 8; j += 4) {
                    for (i = 0; i < 8; i += 4) {
                        int offset = bytestream2_get_le16(&gb);
                        int from_x = offset % WIDTH;
                        int from_y = offset / WIDTH;
                        if (block_type == C93_4X4_FROM_CURR && from_y == y+j &&
                            (FFABS(from_x - x-i) < 4 || FFABS(from_x - x-i) > WIDTH-4)) {
                            avpriv_request_sample(avctx, "block overlap %d %d %d %d\n", from_x, x+i, from_y, y+j);
                            return AVERROR_INVALIDDATA;
                        }
                        if ((ret = copy_block(avctx, &out[j*stride+i],
                                              copy_from, offset, 4, stride)) < 0)
                            return ret;
                    }
                }
                break;

            case C93_8X8_2COLOR:
                bytestream2_get_buffer(&gb, cols, 2);
                for (i = 0; i < 8; i++) {
                    draw_n_color(out + i*stride, stride, 8, 1, 1, cols,
                                     NULL, bytestream2_get_byte(&gb));
                }

                break;

            case C93_4X4_2COLOR:
            case C93_4X4_4COLOR:
            case C93_4X4_4COLOR_GRP:
                for (j = 0; j < 8; j += 4) {
                    for (i = 0; i < 8; i += 4) {
                        if (block_type == C93_4X4_2COLOR) {
                            bytestream2_get_buffer(&gb, cols, 2);
                            draw_n_color(out + i + j*stride, stride, 4, 4,
                                    1, cols, NULL, bytestream2_get_le16(&gb));
                        } else if (block_type == C93_4X4_4COLOR) {
                            bytestream2_get_buffer(&gb, cols, 4);
                            draw_n_color(out + i + j*stride, stride, 4, 4,
                                    2, cols, NULL, bytestream2_get_le32(&gb));
                        } else {
                            bytestream2_get_buffer(&gb, grps, 4);
                            draw_n_color(out + i + j*stride, stride, 4, 4,
                                    1, cols, grps, bytestream2_get_le16(&gb));
                        }
                    }
                }
                break;

            case C93_NOOP:
                break;

            case C93_8X8_INTRA:
                for (j = 0; j < 8; j++)
                    bytestream2_get_buffer(&gb, out + j*stride, 8);
                break;

            default:
                av_log(avctx, AV_LOG_ERROR, "unexpected type %x at %dx%d\n",
                       block_type, x, y);
                return AVERROR_INVALIDDATA;
            }
            bt >>= 4;
            out += 8;
        }
    }

    if (b & C93_HAS_PALETTE) {
        uint32_t *palette = (uint32_t *) newpic->data[1];
        for (i = 0; i < 256; i++) {
            palette[i] = 0xFFU << 24 | bytestream2_get_be24(&gb);
        }
        newpic->palette_has_changed = 1;
    } else {
        if (oldpic->data[1])
            memcpy(newpic->data[1], oldpic->data[1], 256 * 4);
    }

    if ((ret = av_frame_ref(data, newpic)) < 0)
        return ret;
    *got_frame = 1;

    return buf_size;
}
示例#11
0
int crypto_aead_encrypt(
unsigned char *c,unsigned long long *clen,
const unsigned char *m,unsigned long long mlen,
const unsigned char *ad,unsigned long long adlen,
const unsigned char *nsec,
const unsigned char *npub,
const unsigned char *k
)
{
	u8 param[]={0x06,0,0,0,0x80,0,0,0}; 
	
	block L, W, Delta_0, Delta_1, Delta_2, blk, result, CS;
	int i; u8 zeroes[16], ozs[16], blen = 16, Is_final = 0, Is_complete =1;
	unsigned long long cnt;
	for(i=0; i<16; i++)	{zeroes[i]=0x00;}   	
	for(i=1; i<16; i++)	{ozs[i]=0x00;} ozs[0] = 0x80; 	


	cnt = (8+mlen-1)/16 + 1;
	*clen = (cnt +1)* 16 + 1;

	if((mlen+8)%16 == 0) 
		c[*clen-1] = 0x00; 
	else 
		c[*clen-1] = 0x01; 

	key_schedule(k);

	/* ==========  Generate the Masks =========== */
	AES(6, ENCRYPT, blk, zeroes, &aes_key1);
	AES(6, ENCRYPT, L, blk, &aes_key1);

	mult_3(Delta_0, L); 
	mult_inv2(Delta_0, Delta_0); 

	mult_inv2(Delta_1, L); 	

	mult_3(Delta_2, L); 
	mult_3(Delta_2, Delta_2); 
	mult_inv2(Delta_2, Delta_2);

	/* ======  Process Associated Data ======== */
	for(i=0; i<16; i++)
		W[i]=0x00;
	process_AD(W, Delta_0, npub, param, ad, adlen);


	/* ================  Process Successive Message Blocks ==================== */

	
	/* ====== Process the first Message block, whose first 8 byte is the secret message number ===== */
	
	if(mlen < 8){ Is_complete = 0; }
	if(mlen <= 8) { blen = 8 + mlen; }
	load_block(blk, nsec, m, 8, blen-8); copy_block(CS, blk); 
	process_block(Delta_1, Delta_2, result, blk, W, Is_complete, ENCRYPT, Is_final, MESSAGE);
	
	store_bytes(c, result, 0, 15); c +=16;
	if(mlen >= 8)  {mlen -= 8; m +=8;}
	else mlen = 0;

	/* ============= Process Message blocks ================== */
	while(mlen > 0){
		if(mlen >= 16){
			load_block(blk, m, ozs, 16, 0);
			if(mlen == 16) {xor_block(blk, blk, CS); }
			else xor_block(CS, CS, blk);
			blen = 16; mlen -= 16; m+=16; 
		}
		else 	{Is_complete = 0; blen = mlen; mlen = 0; 
			load_block(blk, m, ozs, blen, 0); xor_block(blk, CS, blk);
			
		}	
		process_block(Delta_1, Delta_2, result, blk, W, Is_complete, ENCRYPT, Is_final, MESSAGE); 
		store_bytes(c, result, 0, 15); c +=16;
		
	}

	/* ================ Process checksum block ====================== */
	process_block(Delta_1, Delta_2, result, blk, W, 1, ENCRYPT, 1, MESSAGE); 
	store_bytes(c, result, 0, 15);
	return 0;
}
示例#12
0
void cryptonight_hash(const char* input, char* output, uint32_t len) {
    uint8_t long_state[MEMORY];
    union cn_slow_hash_state state;
    uint8_t text[INIT_SIZE_BYTE];
    uint8_t a[AES_BLOCK_SIZE];
    uint8_t b[AES_BLOCK_SIZE];
    uint8_t c[AES_BLOCK_SIZE];
    uint8_t d[AES_BLOCK_SIZE];
    size_t i, j;
    uint8_t aes_key[AES_KEY_SIZE];
    OAES_CTX* aes_ctx;

    hash_process(&state.hs, (const uint8_t*) input, len);
    memcpy(text, state.init, INIT_SIZE_BYTE);
    memcpy(aes_key, state.hs.b, AES_KEY_SIZE);
    aes_ctx = oaes_alloc();

    oaes_key_import_data(aes_ctx, aes_key, AES_KEY_SIZE);
    for (i = 0; i < MEMORY / INIT_SIZE_BYTE; i++) {
        for (j = 0; j < INIT_SIZE_BLK; j++) {
            oaes_pseudo_encrypt_ecb(aes_ctx, &text[AES_BLOCK_SIZE * j]);
        }
        memcpy(&long_state[i * INIT_SIZE_BYTE], text, INIT_SIZE_BYTE);
    }

    for (i = 0; i < 16; i++) {
        a[i] = state.k[i] ^ state.k[32 + i];
        b[i] = state.k[16 + i] ^ state.k[48 + i];
    }

    for (i = 0; i < ITER / 2; i++) {
        /* Dependency chain: address -> read value ------+
         * written value <-+ hard function (AES or MUL) <+
         * next address  <-+
         */
        /* Iteration 1 */
        j = e2i(a, MEMORY / AES_BLOCK_SIZE);
        copy_block(c, &long_state[j * AES_BLOCK_SIZE]);
        oaes_encryption_round(a, c);
        xor_blocks(b, c);
        swap_blocks(b, c);
        copy_block(&long_state[j * AES_BLOCK_SIZE], c);
        assert(j == e2i(a, MEMORY / AES_BLOCK_SIZE));
        swap_blocks(a, b);
        /* Iteration 2 */
        j = e2i(a, MEMORY / AES_BLOCK_SIZE);
        copy_block(c, &long_state[j * AES_BLOCK_SIZE]);
        mul(a, c, d);
        sum_half_blocks(b, d);
        swap_blocks(b, c);
        xor_blocks(b, c);
        copy_block(&long_state[j * AES_BLOCK_SIZE], c);
        swap_blocks(a, b);
    }

    memcpy(text, state.init, INIT_SIZE_BYTE);
    oaes_key_import_data(aes_ctx, &state.hs.b[32], AES_KEY_SIZE);
    for (i = 0; i < MEMORY / INIT_SIZE_BYTE; i++) {
        for (j = 0; j < INIT_SIZE_BLK; j++) {
            xor_blocks(&text[j * AES_BLOCK_SIZE],
                       &long_state[i * INIT_SIZE_BYTE + j * AES_BLOCK_SIZE]);
            oaes_pseudo_encrypt_ecb(aes_ctx, &text[j * AES_BLOCK_SIZE]);
        }
    }
    memcpy(state.init, text, INIT_SIZE_BYTE);
    hash_permutation(&state.hs);
    /*memcpy(hash, &state, 32);*/
    extra_hashes[state.hs.b[0] & 3](&state, 200, output);
    oaes_free(&aes_ctx);
}
int crypto_aead_encrypt(
unsigned char *c,unsigned long long *clen,
const unsigned char *m,unsigned long long mlen,
const unsigned char *ad,unsigned long long adlen,
const unsigned char *nsec,
const unsigned char *npub,
const unsigned char *k
)
{
	u8 param[]={0x06,0,0x7f,0x81,0x80,0,0,0}; 
	
	block L, W, Delta_0, Delta_1, Delta_2, blk, result, CS;
	int i; 
	unsigned long long h, blk_ctr=0, blk_ctr1=0 ;
	u8 zeroes[16], ozs[16], blen = 16, Is_final = 0, Is_complete =1;
	for(i=0; i<16; i++)	{zeroes[i]=0x00;}   	
	for(i=1; i<16; i++)	{ozs[i]=0x00;} ozs[0] = 0x80; 	

	h = (mlen+8-1) / (IT_GAP * 16);
	
	if(h > IT_MAX)
		h = IT_MAX;

	*clen = mlen + 24 + h*16 ;  

	key_schedule(k);

	/* ==========  Generate the Masks =========== */
	AES(6, ENCRYPT, blk, zeroes, &aes_key1);
	AES(6, ENCRYPT, L, blk, &aes_key1);

	mult_3(Delta_0, L); 
	mult_inv2(Delta_0, Delta_0); 

	mult_inv2(Delta_1, L); 	

	mult_3(Delta_2, L); 
	mult_3(Delta_2, Delta_2); 
	mult_inv2(Delta_2, Delta_2);

	/* ======  Process Associated Data ======== */
	for(i=0; i<16; i++)
		W[i]=0x00;
	process_AD(W, Delta_0, npub, param, ad, adlen);


	/* ================  Process Message Blocks ==================== */

	
	/* ====== Process the first Message block, whose first 8 byte is the secret message number ===== */
	
	if(mlen < 8){ Is_complete = 0; }
	if(mlen <= 8) { blen = 8 + mlen; }
	load_block(blk, nsec, m, 8, blen-8); copy_block(CS, blk); 
	process_block(Delta_1, Delta_2, result, blk, W, Is_complete, ENCRYPT, Is_final, MESSAGE);
	
	store_bytes(c, result, 0, 15); c +=16; blk_ctr++; 
	if(mlen >= 8)  {mlen -= 8; m +=8;}
	else mlen = 0;

	/* ============= Process Message blocks ================== */
	while(mlen > 0){
		if(mlen >= 16){
			load_block(blk, m, ozs, 16, 0); 
			if(mlen == 16){xor_block(blk, CS, blk); }
			else xor_block(CS, CS, blk);
			//xor_block(CS, CS, blk); 
			blen = 16; mlen -= 16; m+=16; 
		}
		else 	{Is_complete = 0; blen = mlen; mlen = 0; 
			load_block(blk, m, ozs, blen, 0); xor_block(blk, CS, blk);
			
		}	
		process_block(Delta_1, Delta_2, result, blk, W, Is_complete, ENCRYPT, Is_final, MESSAGE); 
		store_bytes(c, result, 0, 15); c +=16; blk_ctr++;
		
		if(blk_ctr == IT_GAP && blk_ctr1 < IT_MAX && mlen>0) {
			AES(6, DECRYPT, result, W, &aes_key2); 	
			mask(Delta_2, result, result, 1); 
			store_bytes(c, result, 0, 15); c +=16; blk_ctr =0; blk_ctr1++;
		}
	}

	/* ================ Process checksum block ====================== */
	process_block(Delta_1, Delta_2, result, blk, W, 1, ENCRYPT, 1, MESSAGE); 
	store_bytes(c, result, 0, blen-1);

	return 0;
}
示例#14
0
/* #pragma omp task/taskwait version of SWEEP. */
void sweep (int nx, int ny, double dx, double dy, double *f_,
        int itold, int itnew, double *u_, double *unew_, int block_size)
{
#ifdef _OPENMP
    double (*f)[nx][ny] = (double (*)[nx][ny])f_;
    double (*u)[nx][ny] = (double (*)[nx][ny])u_;
    double (*unew)[nx][ny] = (double (*)[nx][ny])unew_;
#endif

    if (block_size == 0)
        block_size = nx;

    int max_blocks_x = (nx / block_size);
    int max_blocks_y = (ny / block_size);

#pragma omp parallel
#pragma omp single
    {
        int it;
        int block_x, block_y;
        for (it = itold + 1; it <= itnew; it++) {
            /*
            for (block_x = 0; block_x < max_blocks_x; block_x++) {
                for (block_y = 0; block_y < max_blocks_y; block_y++) {
#pragma omp task shared(u_, unew_, block_size, nx, ny) firstprivate(block_x, block_y) \
                    depend(in: unew[block_x * block_size: block_size][block_y * block_size: block_size]) \
                    depend(out: u[block_x * block_size: block_size][block_y * block_size: block_size])
                    copy_block(nx, ny, block_x, block_y, u_, unew_, block_size);
                }
            }
            */
            // Compute a new estimate.
            for (block_x = 0; block_x < max_blocks_x; block_x++) {
                for (block_y = 0; block_y < max_blocks_y; block_y++) {
                    int xdm1 = block_x == 0 ? 0 : 1;
                    int xdp1 = block_x == max_blocks_x-1 ? 0 : +1;
                    int ydp1 = block_y == max_blocks_y-1 ? 0 : +1;
                    int ydm1 = block_y == 0 ? 0 : 1;
#pragma omp task shared(u_, unew_, f_, dx, dy, nx, ny, block_size) \
                 firstprivate(block_x, block_y, xdm1, xdp1, ydp1, ydm1) \
                 depend(inout: unew[ block_x         * block_size][ block_y         * block_size], \
                                  u[ block_x         * block_size][ block_y         * block_size]) \
                 depend(in   :    f[ block_x         * block_size][ block_y         * block_size], \
                                  u[(block_x - xdm1) * block_size][ block_y         * block_size], \
                                  u[ block_x         * block_size][(block_y + ydp1) * block_size], \
                                  u[ block_x         * block_size][(block_y - ydm1) * block_size], \
                                  u[(block_x + xdp1) * block_size][ block_y         * block_size])  
                    { //compute_estimate(block_x, block_y, u_, unew_, f_, dx, dy, nx, ny, block_size);
                        copy_block(nx, ny, block_x, block_y, u_, unew_, block_size);
                        int i, j;
                        int start_i = block_x * block_size;
                        int start_j = block_y * block_size;
                        for (i = start_i; i < start_i + block_size; i++) {
                            for (j = start_j; j < start_j + block_size; j++) {
                                if (i == 0 || j == 0 || i == nx - 1 || j == ny - 1) {
                                    (*unew)[i][j] = (*f)[i][j];
                                } else {
                                    (*unew)[i][j] = 0.25 * ( (*u)[i-1][j] + (*u)[i][j+1]
                                                           + (*u)[i][j-1] + (*u)[i+1][j]
                                                           + (*f)[i][j] * dx * dy       );
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
示例#15
0
int main(int argc, char **argv){
	int ret;
	unsigned minor;
	size_t snap_size, bytes, blocks_to_read;
	sector_t total_chunks, total_blocks, i, j, blocks_done = 0, count = 0, err_count = 0;
	FILE *cow = NULL, *snap = NULL, *img = NULL;
	uint64_t *mappings = NULL;
	char *snap_path;
	char snap_path_buf[PATH_MAX];

	if(argc != 4) print_help(argv[0], EINVAL);

	//open snapshot
	snap = fopen(argv[1], "r");
	if(!snap){
		ret = errno;
		errno = 0;
		fprintf(stderr, "error opening snapshot\n");
		goto error;
	}

	//open cow file
	cow = fopen(argv[2], "r");
	if(!cow){
		ret = errno;
		errno = 0;
		fprintf(stderr, "error opening cow file\n");
		goto error;
	}

	//open original image
	img = fopen(argv[3], "r+");
	if(!img){
		ret = errno;
		errno = 0;
		fprintf(stderr, "error opening image\n");
		goto error;
	}

	//get the full path of the cow file
	snap_path = realpath(argv[1], snap_path_buf);
	if(!snap_path){
		ret = errno;
		errno = 0;
		fprintf(stderr, "error determining full path of snapshot\n");
		goto error;
	}

	//get the minor number of the snapshot
	ret = sscanf(snap_path, "/dev/datto%u", &minor);
	if(ret != 1){
		ret = errno;
		errno = 0;
		fprintf(stderr, "snapshot does not appear to be a dattobd snapshot device\n");
		goto error;
	}

	//verify all of the inputs before attempting to merge
	ret = verify_files(cow, minor);
	if(ret) goto error;

	//get size of snapshot, calculate other needed sizes
	fseeko(snap, 0, SEEK_END);
	snap_size = ftello(snap);
	total_blocks = (snap_size + COW_BLOCK_SIZE - 1) / COW_BLOCK_SIZE;
	total_chunks = (total_blocks + INDEX_BUFFER_SIZE - 1) / INDEX_BUFFER_SIZE;
	rewind(snap);

	printf("snapshot is %llu blocks large\n", total_blocks);

	//allocate mappings array
	mappings = malloc(INDEX_BUFFER_SIZE * sizeof(uint64_t));
	if(!mappings){
		ret = ENOMEM;
		fprintf(stderr, "error allocating mappings\n");
		goto error;
	}

	//count number of blocks changed while performing merge
	printf("copying blocks\n");
	for(i = 0; i < total_chunks; i++){
		//read a chunk of mappings from the cow file
		blocks_to_read = MIN(INDEX_BUFFER_SIZE, total_blocks - blocks_done);

		bytes = pread(fileno(cow), mappings, blocks_to_read * sizeof(uint64_t), COW_HEADER_SIZE + (INDEX_BUFFER_SIZE * sizeof(uint64_t) * i));
		if(bytes != blocks_to_read * sizeof(uint64_t)){
			ret = errno;
			errno = 0;
			fprintf(stderr, "error reading mappings into memory\n");
			goto error;
		}

		//copy blocks where the mapping is set
		for(j = 0; j < blocks_to_read; j++){
			if(!mappings[j]) continue;

			ret = copy_block(snap, img, (INDEX_BUFFER_SIZE * i) + j);
			if(ret) err_count++;

			count++;
		}

		blocks_done += blocks_to_read;
	}

	//print number of blocks changed
	printf("copying complete: %llu blocks changed, %llu errors\n", count, err_count);

	free(mappings);
	fclose(cow);
	fclose(snap);
	fclose(img);

	return 0;

error:
	if(mappings) free(mappings);
	if(cow) fclose(cow);
	if(snap) fclose(snap);
	if(img) fclose(img);

	return ret;
}
示例#16
0
void cryptonight_hash(const char* input, char* output, uint32_t len, int variant, uint64_t height) {
    struct cryptonight_ctx *ctx = alloca(sizeof(struct cryptonight_ctx));
    hash_process(&ctx->state.hs, (const uint8_t*) input, len);
    memcpy(ctx->text, ctx->state.init, INIT_SIZE_BYTE);
    memcpy(ctx->aes_key, ctx->state.hs.b, AES_KEY_SIZE);
    ctx->aes_ctx = (oaes_ctx*) oaes_alloc();
    size_t i, j;

    VARIANT1_INIT();
    VARIANT2_INIT(ctx->b, ctx->state);
    VARIANT4_RANDOM_MATH_INIT(ctx->state);

    oaes_key_import_data(ctx->aes_ctx, ctx->aes_key, AES_KEY_SIZE);
    for (i = 0; i < MEMORY / INIT_SIZE_BYTE; i++) {
        for (j = 0; j < INIT_SIZE_BLK; j++) {
            aesb_pseudo_round(&ctx->text[AES_BLOCK_SIZE * j],
                    &ctx->text[AES_BLOCK_SIZE * j],
                    ctx->aes_ctx->key->exp_data);
        }
        memcpy(&ctx->long_state[i * INIT_SIZE_BYTE], ctx->text, INIT_SIZE_BYTE);
    }

    for (i = 0; i < 16; i++) {
        ctx->a[i] = ctx->state.k[i] ^ ctx->state.k[32 + i];
        ctx->b[i] = ctx->state.k[16 + i] ^ ctx->state.k[48 + i];
    }

    for (i = 0; i < ITER / 2; i++) {
        /* Dependency chain: address -> read value ------+
         * written value <-+ hard function (AES or MUL) <+
         * next address  <-+
         */
        /* Iteration 1 */
        j = e2i(ctx->a);
        aesb_single_round(&ctx->long_state[j * AES_BLOCK_SIZE], ctx->c, ctx->a);
        VARIANT2_SHUFFLE_ADD(ctx->long_state, j * AES_BLOCK_SIZE, ctx->a, ctx->b, ctx->c);
        xor_blocks_dst(ctx->c, ctx->b, &ctx->long_state[j * AES_BLOCK_SIZE]);
        VARIANT1_1((uint8_t*)&ctx->long_state[j * AES_BLOCK_SIZE]);
        /* Iteration 2 */
        j = e2i(ctx->c);

        uint64_t* dst = (uint64_t*)&ctx->long_state[j * AES_BLOCK_SIZE];

        uint64_t t[2];
        t[0] = dst[0];
        t[1] = dst[1];

        VARIANT2_INTEGER_MATH(t, ctx->c);
        copy_block(ctx->a1, ctx->a);
        VARIANT4_RANDOM_MATH(ctx->a, t, r, ctx->b, ctx->b + AES_BLOCK_SIZE);

        uint64_t hi;
        uint64_t lo = mul128(((uint64_t*)ctx->c)[0], t[0], &hi);

        VARIANT2_2();
        VARIANT2_SHUFFLE_ADD(ctx->long_state, j * AES_BLOCK_SIZE, ctx->a1, ctx->b, ctx->c);

        ((uint64_t*)ctx->a)[0] += hi;
        ((uint64_t*)ctx->a)[1] += lo;

        dst[0] = ((uint64_t*)ctx->a)[0];
        dst[1] = ((uint64_t*)ctx->a)[1];

        ((uint64_t*)ctx->a)[0] ^= t[0];
        ((uint64_t*)ctx->a)[1] ^= t[1];

        VARIANT1_2((uint8_t*)&ctx->long_state[j * AES_BLOCK_SIZE]);
        copy_block(ctx->b + AES_BLOCK_SIZE, ctx->b);
        copy_block(ctx->b, ctx->c);
    }

    memcpy(ctx->text, ctx->state.init, INIT_SIZE_BYTE);
    oaes_key_import_data(ctx->aes_ctx, &ctx->state.hs.b[32], AES_KEY_SIZE);
    for (i = 0; i < MEMORY / INIT_SIZE_BYTE; i++) {
        for (j = 0; j < INIT_SIZE_BLK; j++) {
            xor_blocks(&ctx->text[j * AES_BLOCK_SIZE],
                    &ctx->long_state[i * INIT_SIZE_BYTE + j * AES_BLOCK_SIZE]);
            aesb_pseudo_round(&ctx->text[j * AES_BLOCK_SIZE],
                    &ctx->text[j * AES_BLOCK_SIZE],
                    ctx->aes_ctx->key->exp_data);
        }
    }
    memcpy(ctx->state.init, ctx->text, INIT_SIZE_BYTE);
    hash_permutation(&ctx->state.hs);
    /*memcpy(hash, &state, 32);*/
    extra_hashes[ctx->state.hs.b[0] & 3](&ctx->state, 200, output);
    oaes_free((OAES_CTX **) &ctx->aes_ctx);
}
示例#17
0
/* ===========================================================================
 * Determine the best encoding for the current block: dynamic trees, static
 * trees or store, and output the encoded block to the zip file. This function
 * returns the total compressed length for the file so far.
 */
word32 CodeTree::flush_block(byte *buf, word32 stored_len, int eof)
{
   word32 opt_lenb, static_lenb; /* opt_len and static_len in bytes */
   int max_blindex;  /* index of last bit length code of non zero freq */

   flag_buf[last_flags] = flags; /* Save the flags for the last 8 items */

   /* Construct the literal and distance trees */
   build_tree(&l_desc);
//   Tracev((stderr, "\nlit data: dyn %ld, stat %ld", opt_len, static_len));

   build_tree(&d_desc);
//   Tracev((stderr, "\ndist data: dyn %ld, stat %ld", opt_len, static_len));
   /* At this point, opt_len and static_len are the total bit lengths of
	* the compressed block data, excluding the tree representations.
	*/

   /* Build the bit length tree for the above two trees, and get the index
	* in bl_order of the last bit length code to send.
	*/
   max_blindex = build_bl_tree();

   /* Determine the best encoding. Compute first the block length in bytes */
   opt_lenb = (opt_len+3+7)>>3;
   static_lenb = (static_len+3+7)>>3;
   input_len += stored_len; /* for debugging only */

//   Trace((stderr, "\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u dist %u ",
//           opt_lenb, opt_len, static_lenb, static_len, stored_len,
//           last_lit, last_dist));

   if (static_lenb <= opt_lenb) opt_lenb = static_lenb;

#ifdef FORCE_METHOD
   if (level == 2 && buf) /* force stored block */
#else
   if (stored_len+4 <= opt_lenb && buf) /* 4: two words for the lengths */
#endif
   {
	   /* The test buf != NULL is only necessary if LIT_BUFSIZE > WSIZE.
		* Otherwise we can't have processed more than WSIZE input bytes since
		* the last block flush, because compression would have been
		* successful. If LIT_BUFSIZE <= WSIZE, it is never too late to
		* transform a block into a stored block.
		*/
	   /* send block type */
	   send_bits((STORED_BLOCK<<1)+eof, 3);
	   compressed_len = (compressed_len + 3 + 7) & ~7L;
	   compressed_len += (stored_len + 4) << 3;
	   /* with header */
	   copy_block(buf, (unsigned)stored_len, 1);
   }
#ifdef FORCE_METHOD
   else if (level == 3) /* force static trees */
#else
   else if (static_lenb == opt_lenb)
#endif
   {
	   send_bits((STATIC_TREES<<1)+eof, 3);
	   compress_block(static_ltree,static_dtree);
	   compressed_len += 3 + static_len;
   } else {
	   send_bits((DYN_TREES<<1)+eof, 3);
	   send_all_trees(l_desc.max_code+1, d_desc.max_code+1, max_blindex+1);
	   compress_block(dyn_ltree,dyn_dtree);
	   compressed_len += 3 + opt_len;
   }
//   assert (compressed_len == bits_sent);
   init_block();

   if (eof) {
//      assert (input_len == isize);
	  bi_windup();
	  compressed_len += 7;  /* align on byte boundary */
   }
//   Tracev((stderr,"\ncomprlen %lu(%lu) ", compressed_len>>3,
//          compressed_len-7*eof));

   return compressed_len >> 3;
}
示例#18
0
文件: cbc.c 项目: ColinIanKing/zfs
/*
 * Algorithm independent CBC functions.
 */
int
cbc_encrypt_contiguous_blocks(cbc_ctx_t *ctx, char *data, size_t length,
    crypto_data_t *out, size_t block_size,
    int (*encrypt)(const void *, const uint8_t *, uint8_t *),
    void (*copy_block)(uint8_t *, uint8_t *),
    void (*xor_block)(uint8_t *, uint8_t *))
{
	size_t remainder = length;
	size_t need = 0;
	uint8_t *datap = (uint8_t *)data;
	uint8_t *blockp;
	uint8_t *lastp;
	void *iov_or_mp;
	offset_t offset;
	uint8_t *out_data_1;
	uint8_t *out_data_2;
	size_t out_data_1_len;

	if (length + ctx->cbc_remainder_len < block_size) {
		/* accumulate bytes here and return */
		bcopy(datap,
		    (uint8_t *)ctx->cbc_remainder + ctx->cbc_remainder_len,
		    length);
		ctx->cbc_remainder_len += length;
		ctx->cbc_copy_to = datap;
		return (CRYPTO_SUCCESS);
	}

	lastp = (uint8_t *)ctx->cbc_iv;
	if (out != NULL)
		crypto_init_ptrs(out, &iov_or_mp, &offset);

	do {
		/* Unprocessed data from last call. */
		if (ctx->cbc_remainder_len > 0) {
			need = block_size - ctx->cbc_remainder_len;

			if (need > remainder)
				return (CRYPTO_DATA_LEN_RANGE);

			bcopy(datap, &((uint8_t *)ctx->cbc_remainder)
			    [ctx->cbc_remainder_len], need);

			blockp = (uint8_t *)ctx->cbc_remainder;
		} else {
			blockp = datap;
		}

		if (out == NULL) {
			/*
			 * XOR the previous cipher block or IV with the
			 * current clear block.
			 */
			xor_block(lastp, blockp);
			encrypt(ctx->cbc_keysched, blockp, blockp);

			ctx->cbc_lastp = blockp;
			lastp = blockp;

			if (ctx->cbc_remainder_len > 0) {
				bcopy(blockp, ctx->cbc_copy_to,
				    ctx->cbc_remainder_len);
				bcopy(blockp + ctx->cbc_remainder_len, datap,
				    need);
			}
		} else {
			/*
			 * XOR the previous cipher block or IV with the
			 * current clear block.
			 */
			xor_block(blockp, lastp);
			encrypt(ctx->cbc_keysched, lastp, lastp);
			crypto_get_ptrs(out, &iov_or_mp, &offset, &out_data_1,
			    &out_data_1_len, &out_data_2, block_size);

			/* copy block to where it belongs */
			if (out_data_1_len == block_size) {
				copy_block(lastp, out_data_1);
			} else {
				bcopy(lastp, out_data_1, out_data_1_len);
				if (out_data_2 != NULL) {
					bcopy(lastp + out_data_1_len,
					    out_data_2,
					    block_size - out_data_1_len);
				}
			}
			/* update offset */
			out->cd_offset += block_size;
		}

		/* Update pointer to next block of data to be processed. */
		if (ctx->cbc_remainder_len != 0) {
			datap += need;
			ctx->cbc_remainder_len = 0;
		} else {
			datap += block_size;
		}

		remainder = (size_t)&data[length] - (size_t)datap;

		/* Incomplete last block. */
		if (remainder > 0 && remainder < block_size) {
			bcopy(datap, ctx->cbc_remainder, remainder);
			ctx->cbc_remainder_len = remainder;
			ctx->cbc_copy_to = datap;
			goto out;
		}
		ctx->cbc_copy_to = NULL;

	} while (remainder > 0);

out:
	/*
	 * Save the last encrypted block in the context.
	 */
	if (ctx->cbc_lastp != NULL) {
		copy_block((uint8_t *)ctx->cbc_lastp, (uint8_t *)ctx->cbc_iv);
		ctx->cbc_lastp = (uint8_t *)ctx->cbc_iv;
	}

	return (CRYPTO_SUCCESS);
}
示例#19
0
文件: c93.c 项目: 0x0B501E7E/ffmpeg
static int decode_frame(AVCodecContext *avctx, void *data,
                            int *data_size, AVPacket *avpkt)
{
    const uint8_t *buf = avpkt->data;
    int buf_size = avpkt->size;
    C93DecoderContext * const c93 = avctx->priv_data;
    AVFrame * const newpic = &c93->pictures[c93->currentpic];
    AVFrame * const oldpic = &c93->pictures[c93->currentpic^1];
    AVFrame *picture = data;
    GetByteContext gb;
    uint8_t *out;
    int stride, i, x, y, b, bt = 0;

    c93->currentpic ^= 1;

    newpic->reference = 3;
    newpic->buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE |
                         FF_BUFFER_HINTS_REUSABLE | FF_BUFFER_HINTS_READABLE;
    if (avctx->reget_buffer(avctx, newpic)) {
        av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
        return -1;
    }

    stride = newpic->linesize[0];

    bytestream2_init(&gb, buf, buf_size);
    b = bytestream2_get_byte(&gb);
    if (b & C93_FIRST_FRAME) {
        newpic->pict_type = AV_PICTURE_TYPE_I;
        newpic->key_frame = 1;
    } else {
        newpic->pict_type = AV_PICTURE_TYPE_P;
        newpic->key_frame = 0;
    }

    for (y = 0; y < HEIGHT; y += 8) {
        out = newpic->data[0] + y * stride;
        for (x = 0; x < WIDTH; x += 8) {
            uint8_t *copy_from = oldpic->data[0];
            unsigned int offset, j;
            uint8_t cols[4], grps[4];
            C93BlockType block_type;

            if (!bt)
                bt = bytestream2_get_byte(&gb);

            block_type= bt & 0x0F;
            switch (block_type) {
            case C93_8X8_FROM_PREV:
                offset = bytestream2_get_le16(&gb);
                if (copy_block(avctx, out, copy_from, offset, 8, stride))
                    return -1;
                break;

            case C93_4X4_FROM_CURR:
                copy_from = newpic->data[0];
            case C93_4X4_FROM_PREV:
                for (j = 0; j < 8; j += 4) {
                    for (i = 0; i < 8; i += 4) {
                        offset = bytestream2_get_le16(&gb);
                        if (copy_block(avctx, &out[j*stride+i],
                                           copy_from, offset, 4, stride))
                            return -1;
                    }
                }
                break;

            case C93_8X8_2COLOR:
                bytestream2_get_buffer(&gb, cols, 2);
                for (i = 0; i < 8; i++) {
                    draw_n_color(out + i*stride, stride, 8, 1, 1, cols,
                                     NULL, bytestream2_get_byte(&gb));
                }

                break;

            case C93_4X4_2COLOR:
            case C93_4X4_4COLOR:
            case C93_4X4_4COLOR_GRP:
                for (j = 0; j < 8; j += 4) {
                    for (i = 0; i < 8; i += 4) {
                        if (block_type == C93_4X4_2COLOR) {
                            bytestream2_get_buffer(&gb, cols, 2);
                            draw_n_color(out + i + j*stride, stride, 4, 4,
                                    1, cols, NULL, bytestream2_get_le16(&gb));
                        } else if (block_type == C93_4X4_4COLOR) {
                            bytestream2_get_buffer(&gb, cols, 4);
                            draw_n_color(out + i + j*stride, stride, 4, 4,
                                    2, cols, NULL, bytestream2_get_le32(&gb));
                        } else {
                            bytestream2_get_buffer(&gb, grps, 4);
                            draw_n_color(out + i + j*stride, stride, 4, 4,
                                    1, cols, grps, bytestream2_get_le16(&gb));
                        }
                    }
                }
                break;

            case C93_NOOP:
                break;

            case C93_8X8_INTRA:
                for (j = 0; j < 8; j++)
                    bytestream2_get_buffer(&gb, out + j*stride, 8);
                break;

            default:
                av_log(avctx, AV_LOG_ERROR, "unexpected type %x at %dx%d\n",
                       block_type, x, y);
                return -1;
            }
            bt >>= 4;
            out += 8;
        }
    }

    if (b & C93_HAS_PALETTE) {
        uint32_t *palette = (uint32_t *) newpic->data[1];
        for (i = 0; i < 256; i++) {
            palette[i] = 0xFFU << 24 | bytestream2_get_be24(&gb);
        }
    } else {
        if (oldpic->data[1])
            memcpy(newpic->data[1], oldpic->data[1], 256 * 4);
    }

    *picture = *newpic;
    *data_size = sizeof(AVFrame);

    return buf_size;
}
示例#20
0
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
    
    int block_idx = (int) (mxGetScalar(prhs[1]));
    int translation = (int) (mxGetScalar(prhs[2]));
    int rotation = (int) (mxGetScalar(prhs[3]));
    double* map0 = mxGetPr(prhs[0]);
    int block[4][2];
    int block_pos[2];
    int new_block_pos[2];
    int map_2D[19][12];
    
    double* game_over;
    double* map1;
    
    int i;
    
    plhs[0] =mxCreateDoubleMatrix(19,12, mxREAL); /*resulting board*/
    plhs[1] =mxCreateDoubleMatrix(1,1, mxREAL); /*game over or not*/
    
    map1 = mxGetPr(plhs[0]);
    game_over = mxGetPr(plhs[1]);
    
    /* // default outcomes: */
    game_over[0] = 1;
    copy_map(map1, map0);
    
        
    if(block_idx <1 || block_idx > 7){
        printf("illegal block_idx: %d\n", block_idx);
        return;
    }
    
    if(translation > 10 || translation < 1){
        if(translation>10)
            translation = 10;
        if(translation < 1)
            translation = 1;
    }
    
    if(rotation < 0 || rotation > 3){
        while(rotation < 0)
            rotation+=4;
        while(rotation > 3)
            rotation -= 4;
    }
    
    build_2D_map(map0, map_2D);

/* //    printf("printing before block placement\n"); */
/* //    print_2D_map(map_2D); */

    copy_block(blocks[block_idx-1], block);
        
    game_over[0] = 0;
    
    block_pos[1] = BoxY-3;
    block_pos[0] = (int) (floor(BoxX/2));
    
    /*% rotate action*/
    for (i=1; i<=rotation; ++i){
        rotate_block(block);
    }
    
    
    /* check if game over*/
    if(collision(map_2D, block_pos, block)){
        copy_map(map1, map0);
        game_over[0] = 1;
        return;
    }

    

    
    /*% translate action*/
    while( translation < block_pos[0]){
        new_block_pos[0] = block_pos[0]-1;
        new_block_pos[1] = block_pos[1];
        
        if (!collision(map_2D, new_block_pos, block)){
            block_pos[0] = new_block_pos[0];
        } else {
            break;
        }
    }
    
    while( translation > block_pos[0]){
        new_block_pos[0] = block_pos[0]+1;
        new_block_pos[1] = block_pos[1];
        
        if (!collision(map_2D, new_block_pos, block)){
            block_pos[0] = new_block_pos[0];
        } else {
            break;
        }
    }
    
    /*%drop shape:*/
    new_block_pos[0] = block_pos[0];
    new_block_pos[1] = block_pos[1];
    while(!collision(map_2D, new_block_pos, block)){
        new_block_pos[1] = new_block_pos[1] - 1;
    }
    block_pos[1] = new_block_pos[1]+1;
    
    
    
    place_block_in_map(map_2D, block_pos, block);
/* //    printf("printing after block placement\n"); */
/* //    print_2D_map(map_2D); */
        
    /* % check for filled rows */
    check_map_for_filled_rows(map_2D);
    
/* //    printf("printing after row clearing\n"); */
/* //    print_2D_map(map_2D); */
    
    write_2D_map_to_1D(map_2D, map1);

    game_over[0] = 0;
}
示例#21
0
int
crypto_update_uio(void *ctx, crypto_data_t *input, crypto_data_t *output,
    int (*cipher)(void *, caddr_t, size_t, crypto_data_t *),
    void (*copy_block)(uint8_t *, uint64_t *))
{
	common_ctx_t *common_ctx = ctx;
	uio_t *uiop = input->cd_uio;
	off_t offset = input->cd_offset;
	size_t length = input->cd_length;
	uint_t vec_idx;
	size_t cur_len;

	if (input->cd_miscdata != NULL) {
		copy_block((uint8_t *)input->cd_miscdata,
		    &common_ctx->cc_iv[0]);
	}

	if (input->cd_uio->uio_segflg != UIO_SYSSPACE) {
		return (CRYPTO_ARGUMENTS_BAD);
	}

	/*
	 * Jump to the first iovec containing data to be
	 * processed.
	 */
	for (vec_idx = 0; vec_idx < uiop->uio_iovcnt &&
	    offset >= uiop->uio_iov[vec_idx].iov_len;
	    offset -= uiop->uio_iov[vec_idx++].iov_len)
		;
	if (vec_idx == uiop->uio_iovcnt) {
		/*
		 * The caller specified an offset that is larger than the
		 * total size of the buffers it provided.
		 */
		return (CRYPTO_DATA_LEN_RANGE);
	}

	/*
	 * Now process the iovecs.
	 */
	while (vec_idx < uiop->uio_iovcnt && length > 0) {
		cur_len = MIN(uiop->uio_iov[vec_idx].iov_len -
		    offset, length);

		(cipher)(ctx, uiop->uio_iov[vec_idx].iov_base + offset,
		    cur_len, (input == output) ? NULL : output);

		length -= cur_len;
		vec_idx++;
		offset = 0;
	}

	if (vec_idx == uiop->uio_iovcnt && length > 0) {
		/*
		 * The end of the specified iovec's was reached but
		 * the length requested could not be processed, i.e.
		 * The caller requested to digest more data than it provided.
		 */

		return (CRYPTO_DATA_LEN_RANGE);
	}

	return (CRYPTO_SUCCESS);
}
示例#22
0
void cn_slow_hash(const void *data, size_t length, char *hash) {
  uint8_t long_state[MEMORY];
  union cn_slow_hash_state state;
  uint8_t text[INIT_SIZE_BYTE];
  uint8_t a[AES_BLOCK_SIZE];
  uint8_t b[AES_BLOCK_SIZE];
  uint8_t c[AES_BLOCK_SIZE];
  uint8_t d[AES_BLOCK_SIZE];
  size_t i, j;
  uint8_t aes_key[AES_KEY_SIZE];
  oaes_ctx *aes_ctx;

  hash_process(&state.hs, data, length);
  memcpy(text, state.init, INIT_SIZE_BYTE);
  memcpy(aes_key, state.hs.b, AES_KEY_SIZE);
  aes_ctx = (oaes_ctx *) oaes_alloc();

  oaes_key_import_data(aes_ctx, aes_key, AES_KEY_SIZE);
  for (i = 0; i < MEMORY / INIT_SIZE_BYTE; i++) {
    for (j = 0; j < INIT_SIZE_BLK; j++) {
      aesb_pseudo_round(&text[AES_BLOCK_SIZE * j], &text[AES_BLOCK_SIZE * j], aes_ctx->key->exp_data);
	}
    memcpy(&long_state[i * INIT_SIZE_BYTE], text, INIT_SIZE_BYTE);
  }

  for (i = 0; i < 16; i++) {
    a[i] = state.k[     i] ^ state.k[32 + i];
    b[i] = state.k[16 + i] ^ state.k[48 + i];
  }

  for (i = 0; i < ITER / 2; i++) {
    /* Dependency chain: address -> read value ------+
     * written value <-+ hard function (AES or MUL) <+
     * next address  <-+
     */
    /* Iteration 1 */
    j = e2i(a, MEMORY / AES_BLOCK_SIZE);
    copy_block(c, &long_state[j * AES_BLOCK_SIZE]);
    aesb_single_round(c, c, a);
    xor_blocks(b, c);
    swap_blocks(b, c);
    copy_block(&long_state[j * AES_BLOCK_SIZE], c);
    //assert(j == e2i(a, MEMORY / AES_BLOCK_SIZE));
    swap_blocks(a, b);
    /* Iteration 2 */
    j = e2i(a, MEMORY / AES_BLOCK_SIZE);
    copy_block(c, &long_state[j * AES_BLOCK_SIZE]);
    mul(a, c, d);
    sum_half_blocks(b, d);
    swap_blocks(b, c);
    xor_blocks(b, c);
    copy_block(&long_state[j * AES_BLOCK_SIZE], c);
    //assert(j == e2i(a, MEMORY / AES_BLOCK_SIZE));
    swap_blocks(a, b);
  }

  memcpy(text, state.init, INIT_SIZE_BYTE);
  oaes_key_import_data(aes_ctx, &state.hs.b[32], AES_KEY_SIZE);
  for (i = 0; i < MEMORY / INIT_SIZE_BYTE; i++) {
    for (j = 0; j < INIT_SIZE_BLK; j++) {
      xor_blocks(&text[j * AES_BLOCK_SIZE], &long_state[i * INIT_SIZE_BYTE + j * AES_BLOCK_SIZE]);
      aesb_pseudo_round(&text[AES_BLOCK_SIZE * j], &text[AES_BLOCK_SIZE * j], aes_ctx->key->exp_data);
    }
  }
  memcpy(state.init, text, INIT_SIZE_BYTE);
  hash_permutation(&state.hs);
  extra_hashes[state.hs.b[0] & 3](&state, 200, hash);
  oaes_free((OAES_CTX **) &aes_ctx);
}