Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1 | daniel-mar | 1 | #include "stdafx.h" |
2 | #pragma hdrstop |
||
3 | /* |
||
4 | |||
5 | Copyright (c) 1990-2007 Info-ZIP. All rights reserved. |
||
6 | |||
7 | See the accompanying file LICENSE, version 2007-Mar-4 or later |
||
8 | (the contents of which are also included in zip.h) for terms of use. |
||
9 | If, for some reason, all these files are missing, the Info-ZIP license |
||
10 | also may be found at: ftp://ftp.info-zip.org/pub/infozip/license.html |
||
11 | |||
12 | parts Copyright (C) 1997 Mike White, Eric W. Engler |
||
13 | ************************************************************************ |
||
14 | Copyright (C) 2009, 2010 by Russell J. Peters, Roger Aelbrecht |
||
15 | |||
16 | This file is part of TZipMaster Version 1.9. |
||
17 | |||
18 | TZipMaster is free software: you can redistribute it and/or modify |
||
19 | it under the terms of the GNU Lesser General Public License as published by |
||
20 | the Free Software Foundation, either version 3 of the License, or |
||
21 | (at your option) any later version. |
||
22 | |||
23 | TZipMaster is distributed in the hope that it will be useful, |
||
24 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
25 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
26 | GNU Lesser General Public License for more details. |
||
27 | |||
28 | You should have received a copy of the GNU Lesser General Public License |
||
29 | along with TZipMaster. If not, see <http://www.gnu.org/licenses/>. |
||
30 | |||
31 | contact: problems@delphizip.org (include ZipMaster in the subject). |
||
32 | updates: http://www.delphizip.org |
||
33 | DelphiZip maillist subscribe at http://www.freelists.org/list/delphizip |
||
34 | ************************************************************************/ |
||
35 | #include "ZipDflt.h" |
||
36 | #include "crypt.h" |
||
37 | |||
38 | #include <stdlib.h> |
||
39 | #include <process.h> /* For prototype of getpid() */ |
||
40 | #include "dz_errs.h" |
||
41 | |||
42 | #undef _DZ_FILE_ |
||
43 | #define _DZ_FILE_ DZ_ZCRYPT_CPP |
||
44 | |||
45 | #define ZCR_SEED2 getpid() /* RCV Added; see note in Crypt.c */ |
||
46 | |||
47 | #define zencode(c, t, k) (t = decrypt_byte(k), update_keys(c, k), t^(c)) |
||
48 | |||
49 | // Write encryption header to file zfile using the password passwd and the |
||
50 | // cyclic redundancy check crc. passwd :: Password string. crc :: Crc of file |
||
51 | // being encrypted. zfile :: Where to write header. |
||
52 | #define Putc(c, f) (temp[tcnt++] = (c)) |
||
53 | //void ZipDflt::crypthead(/*HANDLE zfile,*/ const char *passwd, ulg crc) |
||
54 | void ZipDflt::crypthead(const char* passwd, ulg crc) |
||
55 | { |
||
56 | int n; // Index in random header. |
||
57 | int t; // Temporary. |
||
58 | int c; // Random byte. |
||
59 | int ztemp; // Temporary for zencoded value. |
||
60 | uch header[RAND_HEAD_LEN - 2]; // Random header. |
||
61 | uch temp[RAND_HEAD_LEN + 2]; |
||
62 | int tcnt = 0; |
||
63 | unsigned long k; |
||
64 | |||
65 | // First generate RAND_HEAD_LEN - 2 random bytes. We encrypt the output of |
||
66 | // rand() to get less predictability, since rand() is often poorly |
||
67 | // implemented. |
||
68 | |||
69 | if (++fcalls == 1) |
||
70 | { |
||
71 | srand((unsigned)time(NULL) ^ ZCR_SEED2); |
||
72 | } |
||
73 | |||
74 | init_keys(passwd, fkeys); |
||
75 | |||
76 | for (n = 0; n < RAND_HEAD_LEN - 2; n++) |
||
77 | { |
||
78 | c = (rand() >> 7) & 0xFF; |
||
79 | header[n] = (uch) zencode(c, t, fkeys); |
||
80 | } |
||
81 | |||
82 | // Encrypt random header (last two bytes is high word of crc) |
||
83 | init_keys(passwd, fkeys); |
||
84 | |||
85 | for (n = 0; n < RAND_HEAD_LEN - 2; n++) |
||
86 | { |
||
87 | ztemp = zencode(header[n], t, fkeys); |
||
88 | Putc((uch) ztemp, zfile); // V1.5 Added (uch) |
||
89 | } |
||
90 | |||
91 | ztemp = zencode((int)(crc >> 16) & 0xFF, t, fkeys); |
||
92 | |||
93 | Putc((uch) ztemp, zfile); // V1.5 Added (uch) |
||
94 | ztemp = zencode((int)(crc >> 24) & 0xFF, t, fkeys); |
||
95 | Putc((uch) ztemp, zfile); // V1.5 Added (uch) |
||
96 | fZipOutfile->Write(temp, tcnt, &k); |
||
97 | // Assert(k < (RAND_HEAD_LEN + 2), "temp buffer overflow"); |
||
98 | fBytesWritten += k; |
||
99 | } |
||
100 | |||
101 | #define BUF_SIZE (1024 * 16) |
||
102 | // If requested, encrypt the data in buf, and in any case call fwrite() with |
||
103 | // the arguments to zfwrite(). Return what fwrite() returns. buf :: Data |
||
104 | // buffer. item_size :: Size of each item in bytes. nb :: Number of items. f |
||
105 | // :: File to write to. |
||
106 | unsigned __fastcall ZipDflt::zfwrite(const uch *buf, ::extent nb) |
||
107 | { |
||
108 | unsigned long k; |
||
109 | int t; // temporary |
||
110 | unsigned r; |
||
111 | |||
112 | if (fkey) |
||
113 | { |
||
114 | // key is the global password pointer |
||
115 | ulg size; // buffer size |
||
116 | char *p = (char *)buf; // steps through buffer |
||
117 | DZStrA bf; |
||
118 | char * bfr = bf.GetBuffer(BUF_SIZE); |
||
119 | while (nb) |
||
120 | { |
||
121 | ulg tw = BUF_SIZE;// sizeof(fewetmp); |
||
122 | char *d = bfr;//fewetmp; |
||
123 | |||
124 | if (tw > nb) |
||
125 | tw = nb; |
||
126 | |||
127 | // Encrypt data in buffer |
||
128 | for (size = tw; size != 0; p++, size--) |
||
129 | { |
||
130 | *d++ = (char)zencode(*p, t, fkeys); |
||
131 | } |
||
132 | |||
133 | r = fZipOutfile->Write(bfr, tw, &k); |
||
134 | // r = fZipOutfile->Write(fewetmp, tw, &k); |
||
135 | |||
136 | fBytesWritten += k; |
||
137 | nb -= k; |
||
138 | |||
139 | if (!r) |
||
140 | { |
||
141 | diag(_T("zfwrite failed")); |
||
142 | return r; |
||
143 | } |
||
144 | } |
||
145 | |||
146 | return 1; |
||
147 | } |
||
148 | |||
149 | // Write the buffer out |
||
150 | r = fZipOutfile->Write(buf, nb, &k); |
||
151 | if (!r) |
||
152 | Notify(IDIAG, _T("zfwrite failed [%s]"), SysMsg().c_str()); |
||
153 | // diag(_T("zfwrite failed")); |
||
154 | |||
155 | fBytesWritten += k; |
||
156 | return r; |
||
157 | } |
||
158 | |||
159 | /* 30/1/07 */ |
||
160 | |||
161 |