annotate src/crypt_zip.c @ 7683:ec434c82f72c v7.4.1140

commit https://github.com/vim/vim/commit/e266d6d664d6d743c79797af400b2c01ec746216 Author: Bram Moolenaar <Bram@vim.org> Date: Tue Jan 19 20:51:32 2016 +0100 patch 7.4.1140 Problem: Recognizing <sid> does not work when the language is Turkish. (Christian Brabandt) Solution: Use MB_STNICMP() instead of STNICMP().
author Christian Brabandt <cb@256bit.org>
date Tue, 19 Jan 2016 21:00:07 +0100
parents 18ac55444b37
children af3c41a3c53f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6122
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1 /* vi:set ts=8 sts=4 sw=4:
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
2 *
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
3 * VIM - Vi IMproved by Bram Moolenaar
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
4 *
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
5 * Do ":help uganda" in Vim to read copying and usage conditions.
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
6 * Do ":help credits" in Vim to see a list of people who contributed.
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
7 * See README.txt for an overview of the Vim source code.
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
8 */
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
9
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
10 /*
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
11 * crypt_zip.c: Zip encryption support.
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
12 */
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
13 #include "vim.h"
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
14
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
15 #if defined(FEAT_CRYPT) || defined(PROTO)
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
16 /*
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
17 * Optional encryption support.
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
18 * Mohsin Ahmed, mosh@sasi.com, 98-09-24
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
19 * Based on zip/crypt sources.
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
20 *
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
21 * NOTE FOR USA: Since 2000 exporting this code from the USA is allowed to
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
22 * most countries. There are a few exceptions, but that still should not be a
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
23 * problem since this code was originally created in Europe and India.
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
24 */
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
25
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
26 /* Need a type that should be 32 bits. 64 also works but wastes space. */
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
27 # if VIM_SIZEOF_INT >= 4
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
28 typedef unsigned int u32_T; /* int is at least 32 bits */
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
29 # else
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
30 typedef unsigned long u32_T; /* long should be 32 bits or more */
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
31 # endif
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
32
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
33 /* The state of encryption, referenced by cryptstate_T. */
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
34 typedef struct {
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
35 u32_T keys[3];
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
36 } zip_state_T;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
37
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
38
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
39 static void make_crc_tab __ARGS((void));
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
40
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
41 static u32_T crc_32_table[256];
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
42
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
43 /*
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
44 * Fill the CRC table, if not done already.
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
45 */
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
46 static void
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
47 make_crc_tab()
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
48 {
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
49 u32_T s, t, v;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
50 static int done = FALSE;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
51
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
52 if (done)
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
53 return;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
54 for (t = 0; t < 256; t++)
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
55 {
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
56 v = t;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
57 for (s = 0; s < 8; s++)
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
58 v = (v >> 1) ^ ((v & 1) * (u32_T)0xedb88320L);
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
59 crc_32_table[t] = v;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
60 }
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
61 done = TRUE;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
62 }
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
63
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
64 #define CRC32(c, b) (crc_32_table[((int)(c) ^ (b)) & 0xff] ^ ((c) >> 8))
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
65
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
66 /*
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
67 * Return the next byte in the pseudo-random sequence.
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
68 */
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
69 #define DECRYPT_BYTE_ZIP(keys, t) { \
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
70 short_u temp = (short_u)keys[2] | 2; \
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
71 t = (int)(((unsigned)(temp * (temp ^ 1U)) >> 8) & 0xff); \
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
72 }
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
73
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
74 /*
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
75 * Update the encryption keys with the next byte of plain text.
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
76 */
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
77 #define UPDATE_KEYS_ZIP(keys, c) { \
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
78 keys[0] = CRC32(keys[0], (c)); \
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
79 keys[1] += keys[0] & 0xff; \
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
80 keys[1] = keys[1] * 134775813L + 1; \
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
81 keys[2] = CRC32(keys[2], (int)(keys[1] >> 24)); \
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
82 }
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
83
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
84 /*
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
85 * Initialize for encryption/decryption.
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
86 */
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
87 void
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
88 crypt_zip_init(state, key, salt, salt_len, seed, seed_len)
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
89 cryptstate_T *state;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
90 char_u *key;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
91 char_u *salt UNUSED;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
92 int salt_len UNUSED;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
93 char_u *seed UNUSED;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
94 int seed_len UNUSED;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
95 {
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
96 char_u *p;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
97 zip_state_T *zs;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
98
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
99 zs = (zip_state_T *)alloc(sizeof(zip_state_T));
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
100 state->method_state = zs;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
101
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
102 make_crc_tab();
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
103 zs->keys[0] = 305419896L;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
104 zs->keys[1] = 591751049L;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
105 zs->keys[2] = 878082192L;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
106 for (p = key; *p != NUL; ++p)
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
107 {
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
108 UPDATE_KEYS_ZIP(zs->keys, (int)*p);
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
109 }
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
110 }
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
111
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
112 /*
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
113 * Encrypt "from[len]" into "to[len]".
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
114 * "from" and "to" can be equal to encrypt in place.
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
115 */
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
116 void
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
117 crypt_zip_encode(state, from, len, to)
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
118 cryptstate_T *state;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
119 char_u *from;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
120 size_t len;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
121 char_u *to;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
122 {
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
123 zip_state_T *zs = state->method_state;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
124 size_t i;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
125 int ztemp, t;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
126
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
127 for (i = 0; i < len; ++i)
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
128 {
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
129 ztemp = from[i];
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
130 DECRYPT_BYTE_ZIP(zs->keys, t);
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
131 UPDATE_KEYS_ZIP(zs->keys, ztemp);
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
132 to[i] = t ^ ztemp;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
133 }
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
134 }
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
135
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
136 /*
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
137 * Decrypt "from[len]" into "to[len]".
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
138 */
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
139 void
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
140 crypt_zip_decode(state, from, len, to)
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
141 cryptstate_T *state;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
142 char_u *from;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
143 size_t len;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
144 char_u *to;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
145 {
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
146 zip_state_T *zs = state->method_state;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
147 size_t i;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
148 short_u temp;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
149
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
150 for (i = 0; i < len; ++i)
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
151 {
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
152 temp = (short_u)zs->keys[2] | 2;
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
153 temp = (int)(((unsigned)(temp * (temp ^ 1U)) >> 8) & 0xff);
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
154 UPDATE_KEYS_ZIP(zs->keys, to[i] = from[i] ^ temp);
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
155 }
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
156 }
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
157
18ac55444b37 updated for version 7.4.399
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
158 #endif /* FEAT_CRYPT */