home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 2 BBS / 02-BBS.zip / lora299s.zip / UULIB.CPP < prev    next >
C/C++ Source or Header  |  1998-05-12  |  10KB  |  340 lines

  1.  
  2. // LoraBBS Version 2.99 Free Edition
  3. // Copyright (C) 1987-98 Marco Maccaferri
  4. //
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation; either version 2 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program; if not, write to the Free Software
  17. // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  
  19. #include "_ldefs.h"
  20. #include "uulib.h"
  21.  
  22. #define ENC(c) ((c) ? ((c) & 077) + ' ': '`')
  23. #define DEC(c) (((c) - ' ') & 077)
  24.  
  25. CHAR *Table64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  26.  
  27. #define DEC64(c) (((c) == '=') ? 0 : strchr (Table64, c) - Table64)
  28.  
  29. TUULib::TUULib (void)
  30. {
  31.    MaxLines = 0;
  32. }
  33.  
  34. TUULib::~TUULib (void)
  35. {
  36. }
  37.  
  38. USHORT TUULib::Decode (PSZ pszBuffer)
  39. {
  40.    USHORT c1, c2, c3;
  41.    PSZ src = pszBuffer;
  42.    BYTE *dest = Buffer;
  43.  
  44.    Size = 0;
  45.    memset (Buffer, 0, sizeof (Buffer));
  46.    src++;
  47.  
  48.    while (*src != '\0' && src[1] != '\0' && src[2] != '\0') {
  49.       c1 = (USHORT)((DEC (*src) << 2) | (DEC (src[1]) >> 4));
  50.       c2 = (USHORT)((DEC (src[1]) << 4) | (DEC (src[2]) >> 2));
  51.       c3 = (USHORT)((DEC (src[2]) << 6) | (DEC (src[3])));
  52.       *dest++ = (BYTE)c1;
  53.       *dest++ = (BYTE)c2;
  54.       *dest++ = (BYTE)c3;
  55.       src += 4;
  56.       Size += 3;
  57.    }
  58.  
  59.    return (Size);
  60. }
  61.  
  62. USHORT TUULib::Decode64 (PSZ pszBuffer)
  63. {
  64.    USHORT c1, c2, c3;
  65.    PSZ src = pszBuffer;
  66.    BYTE *dest = Buffer;
  67.  
  68.    Size = 0;
  69.    memset (Buffer, 0, sizeof (Buffer));
  70.  
  71.    while (*src != '\0' && src[1] != '\0' && src[2] != '\0') {
  72.       c1 = (USHORT)((DEC64 (*src) << 2) | (DEC64 (src[1]) >> 4));
  73.       c2 = (USHORT)((DEC64 (src[1]) << 4) | (DEC64 (src[2]) >> 2));
  74.       c3 = (USHORT)((DEC64 (src[2]) << 6) | (DEC64 (src[3])));
  75.       *dest++ = (BYTE)c1;
  76.       *dest++ = (BYTE)c2;
  77.       *dest++ = (BYTE)c3;
  78.       src += 4;
  79.       Size += 3;
  80.    }
  81.  
  82.    return (Size);
  83. }
  84.  
  85. USHORT TUULib::Decode (class TCollection &Text)
  86. {
  87.    return (Decode (&Text));
  88. }
  89.  
  90. USHORT TUULib::Decode (class TCollection *Text)
  91. {
  92.    FILE *fpd = NULL;
  93.    USHORT i, RetVal = FALSE, Began = FALSE, IsMIME = FALSE;
  94.    USHORT Checksum, CheckFile, MayBe64 = FALSE;
  95.    ULONG CodeSize, FileSize;
  96.    CHAR Destination[64], *Temp, *p, *a;
  97.  
  98.    CodeSize = FileSize = 0L;
  99.    Checksum = CheckFile = 0;
  100.  
  101.    if ((Temp = (CHAR *)Text->First ()) != NULL)
  102.       do {
  103.          if (!strncmp (Temp, "end", 3) || !strncmp (Temp, "--", 2)) {
  104.             if (Began == TRUE) {
  105.                Began = FALSE;
  106.                for (i = 0; i < strlen (Temp); i++) {
  107.                   Checksum += Temp[i];
  108.                   CodeSize++;
  109.                }
  110.                Checksum += 13;
  111.                CodeSize++;
  112.             }
  113.          }
  114.  
  115.          if (Began == TRUE) {
  116.             if (MayBe64 == TRUE) {
  117.                Decode64 (Temp);
  118.                if (fpd != NULL)
  119.                   fwrite (Buffer, 1, Size, fpd);
  120.             }
  121.             else {
  122.                for (i = 0; i < strlen (Temp); i++) {
  123.                   Checksum += Temp[i];
  124.                   CodeSize++;
  125.                }
  126.                Checksum += 13;
  127.                CodeSize++;
  128.                Decode (Temp);
  129.                for (i = 0; i < Size; i++) {
  130.                   CheckFile += Buffer[i];
  131.                   FileSize++;
  132.                }
  133.                if (fpd != NULL)
  134.                   fwrite (Buffer, 1, Size, fpd);
  135.             }
  136.          }
  137.  
  138.          if (Temp[0] == '\0' && IsMIME == TRUE)
  139.             Began = TRUE;
  140.  
  141.          if (!strncmp (Temp, "begin ", 6)) {
  142.             MayBe64 = FALSE;
  143.             strcpy (Destination, &Temp[10]);
  144.             if ((fpd = fopen (Destination, "wb")) != NULL)
  145.                RetVal = TRUE;
  146.             Began = TRUE;
  147.             for (i = 0; i < strlen (Temp); i++) {
  148.                Checksum += Temp[i];
  149.                CodeSize++;
  150.             }
  151.             Checksum += 13;
  152.             CodeSize++;
  153.          }
  154.          else if (!strcmp (Temp, "Content-Transfer-Encoding: base64"))
  155.             MayBe64 = TRUE;
  156.          else if (!strncmp (Temp, "Content-Disposition:", 20) || !strncmp (Temp, "Content-Type:", 13)) {
  157.             if ((p = strstr (Temp, "filename=")) != NULL) {
  158.                p += 10;
  159.                if ((a = strchr (p, '"')) != NULL)
  160.                   *a = '\0';
  161.                strcpy (Destination, p);
  162.                if (a != NULL)
  163.                   *a = '"';
  164.                if ((fpd = fopen (Destination, "wb")) != NULL)
  165.                   RetVal = TRUE;
  166.                IsMIME = TRUE;
  167.             }
  168.             else if ((p = strstr (Temp, "name=")) != NULL) {
  169.                p += 6;
  170.                if ((a = strchr (p, '"')) != NULL)
  171.                   *a = '\0';
  172.                strcpy (Destination, p);
  173.                if (a != NULL)
  174.                   *a = '"';
  175.                if ((fpd = fopen (Destination, "wb")) != NULL)
  176.                   RetVal = TRUE;
  177.                IsMIME = TRUE;
  178.             }
  179.          }
  180.       } while ((Temp = (CHAR *)Text->Next ()) != NULL);
  181.  
  182.    if (fpd != NULL)
  183.       fclose (fpd);
  184.  
  185.    return (RetVal);
  186. }
  187.  
  188. USHORT TUULib::DecodeFile (PSZ pszSource, PSZ pszDestination)
  189. {
  190.    FILE *fps, *fpd;
  191.    USHORT i, RetVal = FALSE, Began = FALSE;
  192.    USHORT Checksum, CheckFile;
  193.    ULONG CodeSize, FileSize;
  194.    CHAR Temp[128];
  195.  
  196.    CodeSize = FileSize = 0L;
  197.    Checksum = CheckFile = 0;
  198.  
  199.    if ((fps = fopen (pszSource, "rt")) != NULL) {
  200.       if ((fpd = fopen (pszDestination, "wb")) != NULL) {
  201.          RetVal = TRUE;
  202.          fgets (Temp, sizeof (Temp) - 1, fps);
  203.          while (fgets (Temp, sizeof (Temp) - 1, fps) != NULL) {
  204.             Temp[strlen (Temp) - 1] = '\0';
  205.             if (!strncmp (Temp, "end", 3)) {
  206.                Began = FALSE;
  207.                for (i = 0; i < strlen (Temp); i++) {
  208.                   Checksum += Temp[i];
  209.                   CodeSize++;
  210.                }
  211.                Checksum += 13;
  212.                CodeSize++;
  213.             }
  214.  
  215.             if (Began == TRUE) {
  216.                for (i = 0; i < strlen (Temp); i++) {
  217.                   Checksum += Temp[i];
  218.                   CodeSize++;
  219.                }
  220.                Checksum += 13;
  221.                CodeSize++;
  222.                Decode (Temp);
  223.                for (i = 0; i < Size; i++) {
  224.                   CheckFile += Buffer[i];
  225.                   FileSize++;
  226.                }
  227.                fwrite (Buffer, 1, Size, fpd);
  228.             }
  229.  
  230.             if (!strncmp (Temp, "begin ", 6)) {
  231.                Began = TRUE;
  232.                for (i = 0; i < strlen (Temp); i++) {
  233.                   Checksum += Temp[i];
  234.                   CodeSize++;
  235.                }
  236.                Checksum += 13;
  237.                CodeSize++;
  238.             }
  239.          }
  240.  
  241.          fclose (fpd);
  242.       }
  243.       fclose (fps);
  244.    }
  245.  
  246.    return (RetVal);
  247. }
  248.  
  249. VOID TUULib::Encode (BYTE *lpBuffer, USHORT usSize)
  250. {
  251.    USHORT c1, c2, c3, c4;
  252.    BYTE *src = lpBuffer, *dest = Buffer;
  253.  
  254.    *dest++ = (BYTE)ENC (usSize);
  255.    Size = 1;
  256.  
  257.    while (usSize > 0) {
  258.       c1 = (USHORT)(*src >> 2);
  259.       if (usSize >= 2)
  260.          c2 = (USHORT)(((*src << 4) & 060) | ((src[1] >> 4) & 017));
  261.       else
  262.          c2 = 0;
  263.       if (usSize >= 3) {
  264.          c3 = (USHORT)(((src[1] << 2) & 074) | ((src[2] >> 6) & 03));
  265.          c4 = (USHORT)(src[2] & 077);
  266.       }
  267.       else
  268.          c3 = c4 = 0;
  269.       *dest++ = (BYTE)ENC (c1);
  270.       *dest++ = (BYTE)ENC (c2);
  271.       *dest++ = (BYTE)ENC (c3);
  272.       *dest++ = (BYTE)ENC (c4);
  273.       Size += 4;
  274.       src += (usSize >= 3) ? 3 : usSize;
  275.       usSize -= (usSize >= 3) ? 3 : usSize;
  276.    }
  277. }
  278.  
  279. USHORT TUULib::EncodeFile (PSZ pszSource, PSZ pszDestination, PSZ pszRemote)
  280. {
  281.    FILE *fps, *fpd;
  282.    USHORT RetVal = FALSE, Readed, Count = 0;
  283.    BYTE Temp[128];
  284.    CHAR TempFile[64], *p;
  285.  
  286.    strcpy (TempFile, pszDestination);
  287.  
  288.    if ((fps = fopen (pszSource, "rb")) != NULL) {
  289.       if ((fpd = fopen (pszDestination, "wt")) != NULL) {
  290.          RetVal = TRUE;
  291.  
  292.          if (pszRemote != NULL)
  293.             fprintf (fpd, "begin 644 %s\n", pszRemote);
  294.          else
  295.             fprintf (fpd, "begin 644 %s\n", pszSource);
  296.  
  297.          do {
  298.             if ((Readed = (USHORT)fread (Temp, 1, 45, fps)) != 0) {
  299.                Encode (Temp, Readed);
  300.                fwrite (Buffer, 1, Size, fpd);
  301.                fwrite ("\n", 1, 1, fpd);
  302.  
  303.                if (MaxLines != 0 && ++Count >= MaxLines) {
  304.                   fclose (fpd);
  305.                   p = strchr (TempFile, '\0') - 1;
  306.                   if (isdigit (*p)) {
  307.                      if (*p == '9') {
  308.                         *p = '0';
  309.                         p--;
  310.                         if (isdigit (*p)) {
  311.                            if (*p == '9') {
  312.                               p--;
  313.                            }
  314.                            else
  315.                              (*p)++;
  316.                         }
  317.                         else
  318.                            *p = '1';
  319.                      }
  320.                      else
  321.                         (*p)++;
  322.                   }
  323.                   else
  324.                      *p = '0';
  325.                   fpd = fopen (TempFile, "wt");
  326.                   Count = 0;
  327.                }
  328.             }
  329.          } while (Readed == 45);
  330.  
  331.          fprintf (fpd, "'\nend\n");
  332.          fclose (fpd);
  333.       }
  334.       fclose (fps);
  335.    }
  336.  
  337.    return (RetVal);
  338. }
  339.  
  340.