Originally Posted by
Salem
Code:
for ( i = 15 ; i >= 0 ; i-- ) {
if ( ++key[i] ) break;
// continues if a value rolls over from 0xff to 0x00
}
Maybe, like this?
Code:
#include <stdio.h>
void incr( unsigned char * );
void show( char *, unsigned char * );
int main( void )
{
static unsigned char keys[16] = { [13] = 0x30, 0x30, 0xff };
show( "before", keys );
incr( keys );
show( "after ", keys );
}
/* OBS: Salem's code is better than this one! */
void incr( unsigned char *p )
{
unsigned int i, t;
/* using 'unsigned int' because has more bits than 'unsigned char'.
Anything bigger then 255 will be used to increment the previous item
in the array (carry). */
t = p[15];
p[15] = ++t;
/* Here, add carry-outs, if any. */
for ( i = 14; i >= 0; i-- )
{
if ( t < 256 )
break;
t = p[i];
p[i] = ++t;
}
}
void show( char *s, unsigned char *p )
{
int i;
printf( "%s: {", s );
for ( i = 0; i < 15; i++ )
printf( "0x%02x, ", p[i] );
printf( "0x%02x }\n", p[i] );
}
If you want to use SSSE3 (on x86):
Code:
void incr2( unsigned char *p )
{
__m128i *a = (__m128i *)p;
__m128i b = _mm_set_epi8( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 );
// the array is backwards (big endian)...
*a = _mm_shuffle_epi8( *a, b );
(*a)++;
*a = _mm_shuffle_epi8( *a, b );
}