File: pass1c45.cc

package info (click to toggle)
wp2latex 3.112%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 5,916 kB
  • sloc: cpp: 49,708; ansic: 9,318; asm: 5,429; makefile: 542; sh: 22
file content (407 lines) | stat: -rw-r--r-- 11,133 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/******************************************************************************
 * program:     wp2latex                                                      *
 * function:    Decrypt encrypted WordPerfect 3.x, 4.x and 5.x files          *
 * modul:       pass1C45.cc                                                   *
 * description: This modul decrypts file and call a regular wpX.Y converter.  *
 * licency:     GPL		                                              *
 ******************************************************************************/
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

#include "stringa.h"
#include "wp2latex.h"
#include "struct.h"


#if defined(__UNIX__)||defined(__DJGPP__)
 #include <unistd.h>
#endif


class TconvertedPass1_xWP5:public TconvertedPass1
     {
public:
     virtual int Convert_first_pass(void);
     };
class TconvertedPass1_xWP4:public TconvertedPass1
     {
public:
     virtual int Convert_first_pass(void);
     };
class TconvertedPass1_xWP3:public TconvertedPass1
     {
public:
     virtual int Convert_first_pass(void);
     };


/*Register translators here*/
TconvertedPass1 *Factory_xWP3(void) {return new TconvertedPass1_xWP3;}
TconvertedPass1 *Factory_xWP4(void) {return new TconvertedPass1_xWP4;}
TconvertedPass1 *Factory_xWP5(void) {return new TconvertedPass1_xWP5;}

FFormatTranslator FormatWP3encrypted("WP3.x encrypted",Factory_xWP3);
FFormatTranslator FormatWP4encrypted("WP4.x encrypted",Factory_xWP4); //also works for WP1.x
FFormatTranslator FormatWP5encrypted("WP5.x encrypted",Factory_xWP5);

/*----------Decrypting wp file--------------*/
extern string wpd_password,wpd_filename;
void ObtainPassword(string & password);

const char *InputPasswordAgain="\nWarning! Password checksum does not match file checksum! Again Y/n/i?";


/** This function computes checksum for the given password. */
static WORD CheckSum(char *PassWord)
{
WORD csum;

csum=0;
if(PassWord==NULL) return(0);

while(*PassWord!=0)
	{
	if( *PassWord>='a' && *PassWord<='z')    /* convert to upper case */
			*PassWord -= 'a'-'A';

	csum = ( (csum >> 1) | ( csum << 15) ) ^ ( ((WORD)*PassWord)<<8 );
	PassWord++;
	}
return(csum);
}


/** Decrypt a block encrypted by WP simple encryption. */
static void DecryptWP345(FILE *Encrypted, FILE *Decrypted, const string & PassWord, FILE *ErrorFile)
{
char *buf;
WORD idx;                     /* index into buffer */
WORD pidx;                    /* index down password */
BYTE xmask;                   /* XOR mask */
DWORD len;
DWORD pos = ftell(Encrypted);
DWORD fsize = FileSize(Encrypted);

 if(ErrorFile==NULL) ErrorFile=stderr;
 if((buf=(char *)malloc(4096))==NULL) return;
 if(PassWord.isEmpty())
 {
   if(ErrorFile)
     fprintf(ErrorFile,_("\nError: Password is empty."));
   return;
 }

 pidx = 0;                      /* start of password */
 xmask = length(PassWord)+1;	/* start at password length+1 */

 if(Verbosing >= 1) printf(_("\nDecrypting ...\n\n"));

 while(pos<fsize)
	 {
	 if(fsize-pos > 4096) len = 4096;
			 else len = fsize-pos;

	 if(fread( buf, 1, len, Encrypted ) !=len)
		 {
                 if(ErrorFile)
		   fprintf(ErrorFile,_("\nError: Unexpected end of the input file."));
		 break;
		 }

	  for( idx=0; idx<len; idx++ )	/* now decrypt */
		 {
		 buf[idx] ^= PassWord()[pidx++] ^ xmask++;
		 if( pidx == length(PassWord) ) pidx = 0;
		 }

	   if(Decrypted)
		 {
		 if(fwrite( buf, 1, len, Decrypted ) !=len)
			 {
			 fprintf(ErrorFile,_("\nError: Couldn't write %lu bytes to the temporary file.\n"), (long unsigned)len);
			 break;
			 }
		 }
		 pos += len;
	 } /* while */

 if(buf!=NULL) free(buf);
}


/**WP5.x decryptor*/
int TconvertedPass1_xWP5::Convert_first_pass(void)
{
#ifdef DEBUG
  fprintf(log,"\n#Convert_pass1_WP5_encrypted() ");fflush(log);
#endif
WORD encryption;
string temp_filename;
DWORD DocumentStart;	/* position in file */
FILE *DecryptedFile = NULL;
BYTE SmallBuf[16];
TconvertedPass1 *cqPass1;
int RetVal=0;

  cqPass1 = GetConverter("WP5.x");
  if(cqPass1==NULL)
	{
	RunError(0x202,"WP5.x");  /* unsupported module wp5 */
	return(-1);
	}

  DocumentStart = ftell(wpd);
  fseek(wpd,12,SEEK_SET);	//check for encrypted documents

  RdWORD_HiEnd(&encryption, wpd);
  if(encryption!=0)
	{
	if(CheckSum(wpd_password())!=encryption)
	   {
           if(length(wpd_password)<=0) 
	       ObtainPassword(wpd_password);	// 1st asking for password, when it is not given in command line.

	   while(CheckSum(wpd_password())!=encryption)
	      {
              if(Verbosing<=-1) return -1;	// Silence mode selected, return immediatelly.

	      switch(YesNoIgnore(_(InputPasswordAgain)))
		 {
		 case 'Y':break;
		 case 'N':return -1;
		 case 'I':goto Ignore;
		 }
              ObtainPassword(wpd_password);
	      }
	   }
Ignore:
	temp_filename = copy(wpd_filename,0,
		GetExtension(wpd_filename())- wpd_filename()) + ".$$$";

	if((DecryptedFile=OpenWrChk(temp_filename,"wb",err))==NULL)
			RunError(6,temp_filename);

	fseek(wpd,0l,SEEK_SET);
	fread( SmallBuf, 1, 16, wpd);
	SmallBuf[12] = SmallBuf[13] = 0;          /* reset encryption=0 */
	fwrite( SmallBuf, 1, 16, DecryptedFile ); /* write out first part of WP5.1 file */

	DecryptWP345(wpd,DecryptedFile,wpd_password,err);
	} /* ver5 */


  if(DecryptedFile!=NULL) /* Now perform normal conversion */
    {
    fclose(DecryptedFile);
    if((DecryptedFile=fopen(temp_filename,"rb"))==NULL) return(0);

    fseek(DecryptedFile,DocumentStart,SEEK_SET);
    cqPass1->InitMe(DecryptedFile, table, strip, log, err);
    RetVal=cqPass1->Convert_first_pass();
    fclose(DecryptedFile);

#ifndef DEBUG
    if(EraseStrip) unlink(temp_filename);
#endif		// DEBUG
    }
  delete cqPass1;

return(RetVal);
}


/**WP3.x decryptor*/
int TconvertedPass1_xWP3::Convert_first_pass(void)
{
#ifdef DEBUG
  fprintf(log,"\n#Convert_pass1_WP3_encrypted() ");fflush(log);
#endif
WORD encryption;
string temp_filename;
DWORD DocumentStart;	/* position in file */
FILE *DecryptedFile=NULL;
BYTE SmallBuf[16];
TconvertedPass1 *cqPass1;
int RetVal=0;

  cqPass1 = GetConverter("WP3.x");
  if(cqPass1==NULL)
	{
	RunError(0x202,"WP3.x");  /* unsupported module wp3 */
	return(-1);
	}

  DocumentStart=ftell(wpd);
  fseek(wpd,12,SEEK_SET);	//check for encrypted documents

  RdWORD_LoEnd(&encryption, wpd);
  if(encryption!=0)
	{
	if(CheckSum(wpd_password())!=encryption)
	   {
           if(length(wpd_password)<=0) 
	       ObtainPassword(wpd_password);

	   while(CheckSum(wpd_password())!=encryption)
	      {
              if(Verbosing<=-1) return -1;
	      switch(YesNoIgnore(_(InputPasswordAgain)))
		 {
		 case 'Y':break;
		 case 'N':return -1;
		 case 'I':goto Ignore;
		 }
              ObtainPassword(wpd_password);
	      }
	   }
Ignore:
	temp_filename = copy(wpd_filename,0,
		GetExtension(wpd_filename())- wpd_filename()) + ".$$$";

	if((DecryptedFile=OpenWrChk(temp_filename,"wb",err))==NULL)
			RunError(6,temp_filename);

	fseek(wpd,0l,SEEK_SET);
	for(unsigned i=0; i<(DocumentStart/16); i++) /* copy everythink till to DocumentStart */
          {
	  fread(SmallBuf, 1, 16, wpd);
	  if(i==0)
	    SmallBuf[12] = SmallBuf[13] = 0;        /* reset encryption=0 */
	  fwrite(SmallBuf, 1, 16, DecryptedFile);
          }
	fread( SmallBuf, 1, DocumentStart%16, wpd);
	fwrite( SmallBuf, 1, DocumentStart%16, DecryptedFile);

	DecryptWP345(wpd,DecryptedFile,wpd_password,err);
	} /* ver3 */

  if(DecryptedFile!=NULL)	/* Now perform a normal conversion. */
    {
    fclose(DecryptedFile);
    if((DecryptedFile=fopen(temp_filename,"rb"))==NULL) return(0);

    fseek(DecryptedFile,DocumentStart,SEEK_SET);
    cqPass1->InitMe(DecryptedFile, table, strip, log, err);
    RetVal = cqPass1->Convert_first_pass();
    fclose(DecryptedFile);

#ifndef DEBUG
    if(EraseStrip) unlink(temp_filename);
#endif		// DEBUG
    }
  delete cqPass1;

return(RetVal);
}


/** WP1.x and WP4.x decryptor. */
int TconvertedPass1_xWP4::Convert_first_pass(void)
{
WORD encryption, checksum;
string temp_filename;
FILE *DecryptedFile=NULL;
TconvertedPass1 *cqPass1;
int RetVal=0;
const char *Module;
  
  fseek(wpd,4,SEEK_CUR);	// FF FE 61 61
  RdWORD_HiEnd(&encryption, wpd);
  if(encryption!=0)
	{
	checksum = CheckSum(wpd_password());
	if(checksum!=encryption && ((checksum>>8)|((checksum<<8)&0xFF00))!=encryption)
	   {
           if(length(wpd_password)<=0) 
	       ObtainPassword(wpd_password);

	   checksum = CheckSum(wpd_password());
	   while(checksum!=encryption && ((checksum>>8)|((checksum<<8)&0xFF00))!=encryption)
	      {
              if(Verbosing<=-1) return -1;	// Silence mode selected, return immediatelly.

	      switch(YesNoIgnore(_(InputPasswordAgain)))
		 {
		 case 'Y':ObtainPassword(wpd_password);
			  checksum = CheckSum(wpd_password());
			  break;
		 case 'N':return -1;
		 case 'I':goto Ignore;
		 }
	      }
	   }
Ignore:
	temp_filename = copy(wpd_filename,0,
			     GetExtension(wpd_filename)- wpd_filename()) + ".$$$";
	if((DecryptedFile=OpenWrChk(temp_filename,"wb",err))!=NULL)
	   DecryptWP345(wpd,DecryptedFile,wpd_password,err);
	} /* ver4 */
  else
    {      
    RunError(0x203);  /* something gets wrong - no idea */
    return -1;
    }

  if(DecryptedFile!=NULL)
    {
    fclose(DecryptedFile);
    if((DecryptedFile=fopen(temp_filename,"rb"))==NULL)   //reopen file for reading
	return(-1);
    }
  else
    {
    RunError(6,temp_filename());
    return -1;
    }

  if((checksum>>8) == (checksum&0xFF))
    {
    CheckFileFormat(DecryptedFile,FilForD);
    fseek(DecryptedFile,0,SEEK_SET);	    //rewind document
    //printf("\nDocumentType: %s, Converter %s, Probability %g",chk(FilForD.DocumentType),chk(FilForD.Converter),FilForD.Probability);
    Module = FilForD.Converter;
    if(Module==NULL || *Module==0)
      {
      Module="WP4.x";	// WP4 has lo endian
      if(err!=NULL)
	{   /**/
	fprintf(err,_("\nAutodetection is unsure, default: %s."), chk(Module));
	}
      }
    }
  else
    {
    if(checksum==encryption)
      Module="WP4.x";	// WP4 has lo endian
    else
      Module="WP1.x";	// WP1 has swapped bytes - high endian
    }

  cqPass1=GetConverter(Module);
  if(cqPass1==NULL)		// module is not present
	{
	if(DecryptedFile!=NULL)
		{fclose(DecryptedFile);DecryptedFile=NULL;}
#ifndef DEBUG
        if(EraseStrip) unlink(temp_filename);
#endif		// DEBUG
	RunError(0x202,Module);  /* unsupported module WP1 or WP4 */
	return -1;
	}
    
  if(DecryptedFile!=NULL) /* Now perform normal conversion */
    {
    cqPass1->InitMe(DecryptedFile, table, strip, log, err);
    RetVal = cqPass1->Convert_first_pass();
    fclose(DecryptedFile);
#ifndef DEBUG
    if(EraseStrip) unlink(temp_filename);
#endif		// DEBUG
    }
  delete cqPass1;

return(RetVal);
}

/*----- End of pass1c345. -----*/