comparison mupdf-source/source/fitz/crypt-arc4.c @ 2:b50eed0cc0ef upstream

ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4. The directory name has changed: no version number in the expanded directory now.
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 15 Sep 2025 11:43:07 +0200
parents
children
comparison
equal deleted inserted replaced
1:1d09e1dec1d9 2:b50eed0cc0ef
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 "mupdf/fitz.h"
25 #include <string.h>
26
27 void
28 fz_arc4_init(fz_arc4 *arc4, const unsigned char *key, size_t keylen)
29 {
30 unsigned int t, u;
31 size_t 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 {
43 state[counter] = counter;
44 }
45
46 keyindex = 0;
47 stateindex = 0;
48
49 for (counter = 0; counter < 256; counter++)
50 {
51 t = state[counter];
52 stateindex = (stateindex + key[keyindex] + t) & 0xff;
53 u = state[stateindex];
54
55 state[stateindex] = t;
56 state[counter] = u;
57
58 if (++keyindex >= keylen)
59 {
60 keyindex = 0;
61 }
62 }
63 }
64
65 static unsigned char
66 fz_arc4_next(fz_arc4 *arc4)
67 {
68 unsigned int x;
69 unsigned int y;
70 unsigned int sx, sy;
71 unsigned char *state;
72
73 state = arc4->state;
74
75 x = (arc4->x + 1) & 0xff;
76 sx = state[x];
77 y = (sx + arc4->y) & 0xff;
78 sy = state[y];
79
80 arc4->x = x;
81 arc4->y = y;
82
83 state[y] = sx;
84 state[x] = sy;
85
86 return state[(sx + sy) & 0xff];
87 }
88
89 void
90 fz_arc4_encrypt(fz_arc4 *arc4, unsigned char *dest, const unsigned char *src, size_t len)
91 {
92 size_t i;
93 for (i = 0; i < len; i++)
94 {
95 unsigned char x;
96 x = fz_arc4_next(arc4);
97 dest[i] = src[i] ^ x;
98 }
99 }
100
101 void fz_arc4_final(fz_arc4 *state)
102 {
103 memset(state, 0, sizeof(*state));
104 }