home *** CD-ROM | disk | FTP | other *** search
/ PC-Online 1998 February / PCOnline_02_1998.iso / filesbbs / win3x / pgpjn.exe / JNSOURCE.ZIP / PGPJN.H < prev    next >
C/C++ Source or Header  |  1995-12-11  |  9KB  |  325 lines

  1. //    PGPJN.H
  2. //    Runtime Loadable Encryptor Extension for Pegasus Mail for Windows.
  3. //    Copyright (c) 1995, David Harris, All Rights Reserved.
  4. //
  5. //    Pretty Good Privacy modifications by John Navas
  6. //
  7. //    Copyright (c) 1995, John Navas, All Rights Reserved.
  8. //
  9. //    The author grants explicit permission for this source code to be
  10. //    used or modified as required, subject only to the conditions that
  11. //    the copyright notices above are preserved; that this source code
  12. //    not be used in any product distributed in competition with this
  13. //  product; that by using this code you agree that the code is
  14. //    provided without warranty of any kind, either explicit or implied,
  15. //    and you use it at your own risk.
  16. //
  17. //    This module implements an interface to Pretty Good Privacy (tm)
  18. //    and (c) Copyright 1990-1994 Philip Zimmermann. All rights reserved.
  19. //
  20. //    The Massachusetts Institute of Technology is the distributor of PGP
  21. //    version 2.6, for distribution in the USA only.  It is available from
  22. //    "net-dist.mit.edu," a controlled FTP site that has restrictions and
  23. //    limitations, similar to those used by RSA Data Security, Inc., to comply
  24. //    with export control requirements.  The software resides in the directory
  25. //    /pub/PGP.
  26.  
  27. #define STRICT
  28. #include <windows.h>
  29. #include <shellapi.h>
  30. #include <ctl3d.h>
  31.  
  32. #include <ctype.h>
  33. #include <fcntl.h>
  34. #include <io.h>
  35. #include <malloc.h>
  36. #include <stdarg.h>
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #include <sys\types.h>
  41. #include <sys\stat.h>
  42.  
  43. #include "..\wpmforms.h"
  44.  
  45. ///////////////////////////////////////////////////////////////////////////////
  46.  
  47. #define MINRES    (12)                    // minimum system resources needed
  48. #define MAXTAG    (80)                    // maximum INI file tag length
  49. #define MAXNAME    (90)                    // maximum name length
  50.  
  51. extern HINSTANCE hLibInstance;            // set in LibMain, used throughout the DLL
  52. extern HWND hwndParent;                    // handle of WinPMail parent window
  53. typedef enum { PUB, SIG } MODE;            // my Form mode type
  54. extern MODE MyMode;                        // my Form mode
  55. extern char MyDir[_MAX_DRIVE+_MAX_DIR];    // my directory
  56. extern char PGPpath[_MAX_PATH];            // PGP executable
  57. extern char* TEMP;                        // TEMP directory for PGPJNDOS
  58.  
  59. extern char PGPEXE[_MAX_FNAME+_MAX_EXT];    // PGP executable
  60. extern char PGPJNDOS[_MAX_FNAME+_MAX_EXT];    // my special EXE
  61.  
  62. extern char PGPBegin[MAXTAG];            // start of PGP encrypted section
  63. extern char PGPEnd[MAXTAG];                // end of PGP encrypted section
  64. extern char PGPBeginKey[MAXTAG];        // start of PGP public key
  65. extern char PGPEndKey[MAXTAG];            // end of PGP public key
  66.  
  67. extern char SigGood[MAXTAG];            // PGP good sig message
  68. extern char SigBad[MAXTAG];                // PGP bad sig message
  69. extern char SigNoKey[MAXTAG];            // PGP missing key to verify sig
  70.  
  71. extern char PGPBeginAdvise[MAXTAG];        // start of PGP public key
  72. extern char PGPEndAdvise[MAXTAG];        // end of PGP public key
  73.  
  74. extern char PGPJNKey[128];                // Master Key option
  75.  
  76. ///////////////////////////////////////////////////////////////////////////////
  77.  
  78. #ifdef _DEBUG
  79. void _cdecl MyDebugOutput(LPCSTR lpszFmt, ...);
  80. #endif // _DEBUG
  81.  
  82. ///////////////////////////////////////////////////////////////////////////////
  83.  
  84. char* AddFileName(char* path, const char* file);    // add file name to dir path
  85. char* FrameIt(char* string);                        // frame string with blank
  86.  
  87. ///////////////////////////////////////////////////////////////////////////////
  88. // MY CLASS FOR TEMPORARY BUFFER
  89. // automatically deleted when it goes out of scope
  90.  
  91. class MyBuffer {
  92.     char* buf;
  93.     size_t siz;
  94. public:
  95.     // constructor
  96.     MyBuffer(int req, const char* string = NULL) {
  97.         int len = string ? (lstrlen(string) + 1) : 0 ;
  98.         
  99.         siz = __max(req, len);
  100.         buf = (char*) malloc(siz);
  101.         if (buf != NULL) {
  102.             if (string != NULL)
  103.                 lstrcpy(buf, string);
  104.         }
  105.         else {
  106. #ifdef _DEBUG
  107.             MyDebugOutput("malloc failed on %d bytes\n", siz);
  108. #endif // _DEBUG
  109.             siz = 0;
  110.         }
  111.     }
  112.     // destructor
  113.     ~MyBuffer() {
  114.         if (buf)
  115.             free(buf);
  116.     }
  117.     // access
  118.     operator char*() {
  119.         return buf;
  120.     }
  121.     size_t GetSize() {
  122.         return siz;
  123.     }
  124. };
  125.  
  126. ///////////////////////////////////////////////////////////////////////////////
  127. // MY CLASS FOR A TEMPORARY FILE
  128. // automatically deleted when it goes out of scope
  129.  
  130. class MyTempFile {
  131.     char name[_MAX_PATH];
  132. public:
  133.     // constructor
  134.     MyTempFile(char* dir = NULL) {
  135.         OFSTRUCT ofs;
  136.         
  137.         lstrcpy(name, dir ? dir : TEMP);
  138.         AddFileName(name, "JNXXXXXX");
  139.         _mktemp(name);
  140.         HFILE tmp = OpenFile(name, &ofs, OF_WRITE | OF_CREATE);
  141.         if (tmp != HFILE_ERROR)
  142.             _lclose (tmp);
  143.         else
  144.             MessageBox(
  145.                 hwndParent,                        /* handle of parent window    */
  146.                 _strerror(name),                /* address of text in message box    */
  147.                 "PGP Interface",                /* address of title of message box    */
  148.                 MB_ICONEXCLAMATION | MB_OK);    /* style of message box    */
  149.     }
  150.     // destructor
  151.     ~MyTempFile() {
  152.         remove(name);
  153.     }
  154.     // access
  155.     operator char*() {
  156.         return name;
  157.     }
  158. };
  159.  
  160. ///////////////////////////////////////////////////////////////////////////////
  161. // MY CLASS FOR FILE I/O AT LEVEL 1
  162. // automatically closed when it goes out of scope
  163.  
  164. class MyFile1 {
  165.     HFILE hfile;
  166. public:
  167.     // constructor
  168.     MyFile1(LPCSTR name, UINT mode) {
  169.         OFSTRUCT ofs = {
  170.             sizeof(ofs)
  171.         };
  172.  
  173.         hfile = OpenFile(name, &ofs, mode);
  174. #ifdef _DEBUG
  175.         if (HFILE_ERROR == hfile)
  176.             MyDebugOutput("OpenFile failed on file1 '%s' with error code %u\n",
  177.                 name, ofs.nErrCode);
  178. #endif // _DEBUG
  179.     }
  180.     // destructor
  181.     ~MyFile1() {
  182.         if (hfile != HFILE_ERROR)
  183.             _lclose(hfile);
  184.     }
  185.     // access
  186.     operator HFILE() {
  187.         return hfile;
  188.     }
  189.     // logic
  190.     LONG GetLength() {
  191.         LONG here = _llseek(hfile, 0, 1);    // save location
  192.         LONG end = _llseek(hfile, 0, 2);    // seek to end and get length
  193.         _llseek(hfile, here, 0);            // seek back to where i was
  194.         return end;                            // return length
  195.     }
  196. };
  197.  
  198. ///////////////////////////////////////////////////////////////////////////////
  199. // MY CLASS FOR FILE I/O AT LEVEL 2
  200. // automatically closed when it goes out of scope
  201.  
  202. class MyFile2 {
  203.     FILE* file;
  204.     char name[_MAX_PATH];
  205. public:
  206.     // constructor
  207.     MyFile2(LPCSTR fname, LPCSTR mode, size_t bufsiz = BUFSIZ) {
  208.         lstrcpy(name, fname);
  209.         file = fopen(name, mode);
  210.         if (file != NULL) {
  211.             if (bufsiz != BUFSIZ)
  212.                 setvbuf(file, NULL, _IOFBF, bufsiz);
  213.         }
  214.         else
  215.             MessageBox(
  216.                 hwndParent,                        /* handle of parent window    */
  217.                 _strerror(name),                /* address of text in message box    */
  218.                 "PGP Interface",                /* address of title of message box    */
  219.                 MB_ICONEXCLAMATION | MB_OK);    /* style of message box    */
  220.     }
  221.     // destructor
  222.     ~MyFile2() {
  223.         if (file)
  224.             fclose(file);
  225.     }
  226.     // access
  227. //    operator FILE*() {
  228. //        return file;
  229. //    }
  230.     // logic
  231.     void Close() {
  232.         if (file) {
  233.             fclose(file);
  234.             file = NULL;
  235.         }
  236.     }
  237.     BOOL Eof() {
  238.         return file ? feof(file) : TRUE;
  239.     }
  240.     BOOL Error() {
  241.         return file ? ferror(file) : TRUE ;
  242.     }
  243.     long GetLength() {
  244.         return _filelength(_fileno(file));    // return length
  245.     }
  246.     char* Gets(char* string, int n) {
  247.         char* ptr = fgets(string, n, file);
  248.         
  249.         if (ferror(file))
  250.             MessageBox(
  251.                 hwndParent,                        /* handle of parent window    */
  252.                 _strerror(name),                /* address of text in message box    */
  253.                 "PGP Interface",                /* address of title of message box    */
  254.                 MB_ICONEXCLAMATION | MB_OK);    /* style of message box    */
  255.         return ptr;
  256.     }
  257.     size_t Read(void *buffer, size_t size, size_t count) {
  258.         size_t n = fread(buffer, size, count, file);
  259.  
  260.         if (ferror(file))
  261.             MessageBox(
  262.                 hwndParent,                        /* handle of parent window    */
  263.                 _strerror(name),                /* address of text in message box    */
  264.                 "PGP Interface",                /* address of title of message box    */
  265.                 MB_ICONEXCLAMATION | MB_OK);    /* style of message box    */
  266.         return n;
  267.     }
  268.     int Puts(char* string) {
  269.         if (file) {
  270.             int ret = fputs(string, file);
  271.             
  272.             if (EOF == ret) {
  273.                 MessageBox(
  274.                     hwndParent,                        /* handle of parent window    */
  275.                     _strerror(name),                /* address of text in message box    */
  276.                     "PGP Interface",                /* address of title of message box    */
  277.                     MB_ICONEXCLAMATION | MB_OK);    /* style of message box    */
  278.                 Close();
  279.             }
  280.             return ret;
  281.         }
  282.         return EOF;
  283.     }
  284.     int Printf(const char* format, ...) {
  285.         va_list arg;
  286.         int ret;
  287.         
  288.         va_start(arg, format);
  289.         ret = vfprintf(file, format, arg);
  290.         va_end(arg);
  291.         return ret;
  292.     }
  293.     int Flush() {
  294.         return fflush(file);
  295.     }
  296.     int Seek(long offset, int origin) {
  297.         return fseek(file, offset, origin);
  298.     }
  299.     long Tell() {
  300.         return ftell(file);
  301.     }
  302.     int Trunc(long size) {
  303.         return _chsize(_fileno(file), size);
  304.     }
  305.     BOOL Erase() {                        // overwrite a file (TRUE if error)
  306.         rewind(file);
  307.         for (long len = GetLength(); len; --len)    // do every byte in file
  308.             putc('\0', file);                // clear it to zero
  309.         Flush();
  310.         if (Error()) {
  311.             MessageBox(
  312.                 hwndParent,                        /* handle of parent window    */
  313.                 _strerror(name),                /* address of text in message box    */
  314.                 "PGP Interface",                /* address of title of message box    */
  315.                 MB_ICONEXCLAMATION | MB_OK);    /* style of message box    */
  316.             return TRUE;
  317.         }
  318.         return FALSE;
  319.     }
  320. };
  321.  
  322. ///////////////////////////////////////////////////////////////////////////////
  323.  
  324. //    PGPJN.H
  325.