array and pointers

Alberto reda at zioale.it
Thu Jan 25 03:36:53 PST 2007


hi, I have just started to play with D, and I have some little problem.

In C I did something like:

#define uchar  unsigned char

void aes_ecb_decrypt(aes_context *ctx, uchar *input, uchar *output, uint
len ) {
    int n;
    uchar *pin, *pout;

    // test to validate parameters..

    n = len;
    pin = input;
    pout = output;

    while( n > 0 ) {
        decrypt_block(ctx, pin, pout );
        pin  += 16;
        pout += 16;
        n -= 16;
    }
}

in D:

void decrypt_block(ubyte[] input, ubyte[] output) ;

ubyte[] ecb_decrypt(in ubyte[] input)
	in {
		assert(input.length > 0);
	}
	out(output) {
		assert(output.length == input.length);
	}
	body {
		int i,n, idx;
		ubyte[] pin;
		ubyte[] pout;
		ubyte[] output;
		
		n = input.length;
		output.length = n;
		idx=0;
			
		while( n > 0 ) {
			if (idx+BLOCK_SIZE >= input.length) {
				//we must decrypt less then BLOCK_SIZE
				decrypt_block(input[idx..$], output[idx..$]);
			} else {
				decrypt_block( input[idx..idx+BLOCK_SIZE], output[idx..idx+BLOCK_SIZE]);
			}
			idx += BLOCK_SIZE;
			n -= BLOCK_SIZE;
		}
		return output;
	}
	
but I don't think that is the right way..
There is a simpler way using pointers like in C?
I have tried to use something like:


	int n;
	ubyte *pin;
	ubyte *pout;
	ubyte[] output;
			
	n = input.length;
	output.length = n;
	pin = input.ptr;
	pout = output.ptr;
			
	while( n > 0 ) {
		decrypt_block(cast(ubyte[])pin, cast(ubyte[])pout);
		pin  += 16;
		pout += 16;
		n -= 16;
	}

but I get:
test_aes.d(158): Error: e2ir: cannot cast from ubyte* to ubyte[]
test_aes.d(158): Error: e2ir: cannot cast from ubyte* to ubyte[]

yes I can modify decrypt_block, but this is not the point, I want just
understand how can I solve it, in a D way.




More information about the Digitalmars-d-learn mailing list