Change the translation of the "Help" menu item to "?", so that the menu can be displa...
[reactos.git] / rosapps / smartpdf / fitz / stream / crypt_arc4.c
1 /* This code illustrates a sample implementation
2 * of the Arcfour algorithm
3 * Copyright (c) April 29, 1997 Kalle Kaukonen.
4 * All Rights Reserved.
5 *
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that this copyright
8 * notice and disclaimer are retained.
9 *
10 * THIS SOFTWARE IS PROVIDED BY KALLE KAUKONEN AND CONTRIBUTORS ``AS
11 * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
12 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
13 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KALLE
14 * KAUKONEN OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
15 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
16 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
17 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
18 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
19 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
20 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
21 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22 */
23
24 #include "fitz-base.h"
25 #include "fitz-stream.h"
26
27 void
28 fz_arc4init(fz_arc4 *arc4, unsigned char *key, unsigned keylen)
29 {
30 unsigned int t, u;
31 unsigned int keyindex;
32 unsigned int stateindex;
33 unsigned char *state;
34 unsigned int counter;
35
36 state = arc4->state;
37
38 arc4->x = 0;
39 arc4->y = 0;
40
41 for (counter = 0; counter < 256; counter++) {
42 state[counter] = counter;
43 }
44
45 keyindex = 0;
46 stateindex = 0;
47
48 for (counter = 0; counter < 256; counter++) {
49 t = state[counter];
50 stateindex = (stateindex + key[keyindex] + t) & 0xff;
51 u = state[stateindex];
52
53 state[stateindex] = t;
54 state[counter] = u;
55
56 if (++keyindex >= keylen) {
57 keyindex = 0;
58 }
59 }
60 }
61
62 unsigned char
63 fz_arc4next(fz_arc4 *arc4)
64 {
65 unsigned int x;
66 unsigned int y;
67 unsigned int sx, sy;
68 unsigned char *state;
69
70 state = arc4->state;
71
72 x = (arc4->x + 1) & 0xff;
73 sx = state[x];
74 y = (sx + arc4->y) & 0xff;
75 sy = state[y];
76
77 arc4->x = x;
78 arc4->y = y;
79
80 state[y] = sx;
81 state[x] = sy;
82
83 return state[(sx + sy) & 0xff];
84 }
85
86 void
87 fz_arc4encrypt(fz_arc4 *arc4, unsigned char *dest, unsigned char *src, unsigned len)
88 {
89 unsigned int i;
90 for (i = 0; i < len; i++) {
91 unsigned char x;
92 x = fz_arc4next(arc4);
93 dest[i] = src[i] ^ x;
94 }
95 }
96