I was inspecting the following C code and my knowledge is that when you increment a pointer (by += 2 etc) it will increment the pointer's address by 2 (in this example) * the size of the pointer type. The following code is from a console application which works but this knowledge seems not to apply here?
void some_function(data_type_for_something *c, unsigned long *data, int blocks)
{
unsigned long *d;
int i;
d = data;
for (i = 0; i < blocks; i++)
{
change_values_at_d_and_d_plus_one_to_new_ones(c, d, d+1);
d += 2;
}
}
The blocks parameter is always the size of the data buffer (the amount of longs inside the buffer), as being called here:
some_function(some_value, (void *)data, datalen / 8);
Where the data is a buffer of unsigned chars. If you take a took at the some_function you can see it will access memory outside of the buffer. What could be the reason for this?
Im trying to make this code in java:
public static long[] some_function(the_data_type bc, long[] data, int blocks) {
long[] ret = new long[data.length];
int index = 0;
for(int i = 0;i<blocks; i++) {
Pair<Long,Long> r = function_with_long_name(bc, data[index], data[index+1]);
ret[index]=r.getFirst();
ret[index+1]=r.getSecond();
index+=2;
}
return ret;
}
I get an array out of bounds exception (which makes sense) Please tell me if there is anything else I should include in the question.
Note, the unsigned longs can be just longs in java as the values will never be bigger than the largest long type which is positive.
Full code is https://www.di-mgt.com.au/blowfish.c.html
if needed