Fixed typos
[gnumeric.git] / plugins / excel / rc4.c
blobabc1c84a38215a413c0d02ee998db013128f971c
1 #include "rc4.h"
3 static void
4 swap_byte (unsigned char *a, unsigned char *b)
6 unsigned char swapByte;
8 swapByte = *a;
9 *a = *b;
10 *b = swapByte;
13 void
14 prepare_key (unsigned char *key_data_ptr, int key_data_len, RC4_KEY *key)
16 unsigned char index1;
17 unsigned char index2;
18 unsigned char *state;
19 unsigned int counter;
21 state = &key->state[0];
22 for (counter = 0; counter < 256; counter++)
23 state[counter] = counter;
24 key->x = 0;
25 key->y = 0;
26 index1 = 0;
27 index2 = 0;
28 for (counter = 0; counter < 256; counter++)
30 index2 = (key_data_ptr[index1] + state[counter] + index2) % 256;
31 swap_byte (&state[counter], &state[index2]);
33 index1 = (index1 + 1) % key_data_len;
37 void
38 rc4 (unsigned char *buffer_ptr, unsigned buffer_len, RC4_KEY *key)
40 unsigned char x;
41 unsigned char y;
42 unsigned char *state;
43 unsigned char xorIndex;
44 unsigned int counter;
46 x = key->x;
47 y = key->y;
49 state = key->state;
50 for (counter = 0; counter < buffer_len; counter++)
52 x = (x + 1) % 256;
53 y = (state[x] + y) % 256;
54 swap_byte (&state[x], &state[y]);
56 xorIndex = (state[x] + state[y]) % 256;
58 buffer_ptr[counter] ^= state[xorIndex];
60 key->x = x;
61 key->y = y;