Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
what does the following code mean?

C++
static void fu(uint8_t param[static 64])
{
...
for (i = 0; i < 16; ++i)
z[i] = method(param + (4 * i));
...
}


where is

C++
static uint32_t method(uint8_t *b)
{
return b[0] + (b[1] << 8) + (b[2] << 16) + (b[3] << 24);
}


I understand everything except method call, what does mean
C++
param+(4*i)
? Program it is about Salsa20.

Thanks
Posted

1 solution

The name of an array is a pointer to the first element of the array (and has been since before C++ was considered, it was part of the original C specification) which means that when you write:
C++
nameArray + offset
you are adding a value to a pointer and generating another pointer value to a different element.
For example:
C++
int ar[10];
int *p0 = ar;
int *p1 = ar + 1;
p0 points to the first element of the array, and p1 to the second.
All your code is doing is passing a pointer to the "i-th * 4" element of the array.
And what that actually does is pass a pointer to each 32 bit integer in turn so it can be converted.

It's the equivalent of saying:
C++
z[i] = method(&(param[4 * i]));



"In first round of the for loop is i=0 and z[0]=method(param[4*]) in second round of the for loop is i=1 and z[1]=method(param[4*1]) and so on... At the last round of the for loop is i=15 and z[15]=method(param[4*15]). In each round is the parameter of the mathod() only uint[i], but method() requires array of the 4 uints?

I'm talking about the size of the input variables of method(). Thanks"



OK - each time you enter method(), it gets a pointer to an 8 bit integer, and each time that pointer has moved by four bytes.
4 bytes == 4 * 8 bits == 32 bits == one 32bit integer
So what method is doing is reassembling the four 8bit bytes into a single 32bit integer.
In theory, you can do this a lot simpler, just by casting the pointer:
C++
static uint32_t method(uint8_t *b)
    {
    return *((uint32*)b);
    }
BUT...there are two potential problems with that.
The first is that the 32bit integer pointer will only work if the address in the 8bit pointer is aligned on a 32bit boundary: the address must end with 00 binary. If it isn't (and doesn't) then your will either get odd results, or an application error.

The second is that there are two ways of converting a 32bit number to four 8bit values, depending on how your hardware organises them: "little endian" and "big endian" and that depends on the processor. "Little endian" systems store the least significant 8 bits of the 32 bit number in the lowest numbered byte address, "big endian" systems store the most significant digits in the lowest numbered byte address. Intel and AMD PC processors are "little endian", but not all processors are, so you can't always assume that data from another system will be in the format you expect.

The code you are using doesn't care about the "endianness" of your system, it manually converts it from "little endian" data to a 32bit integer regardless of the system it runs on. So it gets a poiter to teh leastsignificant byte, and buts it in the low 8 bits of the output. Then the next byte goes in the next 8 bits and so on.
So if your input data was in hex bytes 0x12, 0x34, 0x56, 0x78 then the bytes go into an 32 bit integer to produce a hex value: 0x78563412

See what I mean?
 
Share this answer
 
v2
Comments
vezo11 29-Dec-14 5:31am    
Ok, thanks.. That is what i anticipated, but i was not sure. So, in my case this mean param[0], param[4], param[8], param[12]...param[60]. In the for loop i call method and use one by one byte of the array, but method adopt the array of the 4 bytes, can you explain please?

Thanks
OriginalGriff 29-Dec-14 5:43am    
Explain what?
Sorry, I don't understand you.
vezo11 29-Dec-14 7:35am    
In first round of the for loop is i=0 and z[0]=method(param[4*]) in second round of the for loop is i=1 and z[1]=method(param[4*1]) and so on... At the last round of the for loop is i=15 and z[15]=method(param[4*15]). In each round is the parameter of the mathod() only uint[i], but method() requires array of the 4 uints?

I'm talking about the size of the input variables of method(). Thanks
OriginalGriff 29-Dec-14 7:55am    
Answer updated
vezo11 29-Dec-14 8:57am    
Ok, if i understand, at the firs round method() use first 4bytes (param[0], param[1], param[2], param[3]), in second round method() use another 4bytes (param[4], param[5], param[6], param[7]) and so on.. At the last round the method() use last for bytes (param[60], param[61], param[62], param[63]), is that right? Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900