File: MIME.cpp

package info (click to toggle)
althea 0.5.5-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,196 kB
  • ctags: 725
  • sloc: cpp: 8,194; makefile: 214; sh: 44
file content (165 lines) | stat: -rw-r--r-- 4,457 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
/********************************************************
 * MIME.cpp						*
 * 05.04.00						*
 * Keith Resar						*
 *							*
 * functions for decoding and encoding MIME in		*
 * in base64						*
 ********************************************************/

#include "MIME.h"

// the table to lookup our base64 en/decoding scheme
const string table = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

unsigned int LookUp(const char base64Val)  {
   unsigned int counter;
   for (counter=0; counter<table.length(); counter++)  {
      if (table[counter]==base64Val)
	 return(counter);
   }

   // wasn't a valid charcter, ignore
   return(99);
}

int encode64(ifstream &file, string &encodedFile)  {
   // defs
   char readIn;
   char readInAltered;
   int base64Val;
   int n;
   int bits2;
   int bits4;

   // mess around
   n = 0;
   encodedFile = "";
   do  {
      file.get(readIn);
      n++;

      switch (n)  {
         case 1:
	    // we have no extra bits laying around.
	    // read in 8, must store 2.
	    readInAltered = readIn >> 2;
            base64Val = readInAltered & 63;
	    bits2 = (readIn & 3) << 4;
	    break;
         case 2:
	    // we have 2 bits laying around
	    // then we read in 8 bits.
	    // we want the higher 2 bits laying around, 
	    // and the lower 4 bits we just read.
	    // store our extra 4 bits
	    readInAltered = readIn >> 4;
            base64Val = (readInAltered | bits2) & 63;
	    bits4 = (readIn << 2) & 63;
	    break;
         case 3:
	    // we have 4 bits laying around.
	    // then we read in 8 bits.
	    // we want the lower 4 we saved and
	    // 2 of the bits we read in.
	    // save the upper 6 bits we read in
	    readInAltered = readIn >> 6;
            base64Val = (readInAltered | bits4) & 63;
	    encodedFile += table.c_str()[base64Val];
            base64Val = readIn & 63;
            n = 0;
	    break;
      }

      // store what we've got
      encodedFile += table.c_str()[base64Val];
   } while (file); 

   // make sure we have a multiple of 4 chars
   switch (n)  {
      case 1:
         encodedFile += "==";
	 break;
      case 2:
         encodedFile += "=";
	 break;
   }

   // return the encoded string
   //cout << "encoded file: " << encodedFile << endl;
   return(0);
}

void check(unsigned int convertedVal, ifstream &fileIn, int loc, int counter)  {
   char c;

   fileIn.get(c);
   if ((unsigned int)c!=convertedVal)  {
      cout << "was:  " << c << "\tnow:  " << convertedVal 
           << "\tat:  " << loc << "\tcounter: " << counter << endl;
   }
}

int decode64(string &encodedText, ofstream &fileOut)  {
   // defs
   unsigned int convertedVal;
   unsigned int partialVal;
   int n;
   unsigned int bits2;
   unsigned int bits4;
   unsigned int bits6;
   unsigned int counter;
   unsigned int lookedUp;
   //char c;
   //fileIn.get(c);

   // mess around
   n = 0;
   unsigned int encodedTextLength = encodedText.length();
   for (counter=0; counter<encodedTextLength && encodedText[counter]!='='; 
	           counter++)  {
      lookedUp = LookUp(encodedText[counter]);
      if (lookedUp<70)  {
	 n++;
         switch (n)  {
            case 1:
	       // we have no extra bits laying around.
               // only have 6 bits, so just save them until
               // we have a read with enough bits
	       bits6 = lookedUp << 2;
	       break;
            case 2:
	       // we have 6 bits stored.  
	       // our next char has 6 bits.
	       // write out 8 of these bits,
	       // store 4 of these bits
               partialVal = (lookedUp & 48) >> 4;
               convertedVal = bits6 + partialVal;
               bits4 = lookedUp << 4;
               fileOut.put((unsigned char)convertedVal); 
	       break;
            case 3:
	       // we have 4 bits stored
	       // our next char has 6 bits
	       // write out 8 of these bits,
	       // store 2 of these bits
               partialVal = (lookedUp & 60) >> 2;
               convertedVal = bits4 + partialVal;
               bits2 = lookedUp << 6;
               fileOut.put((unsigned char)convertedVal); 
               break;
	    case 4:
	       // we have 2 bits stored
	       // our nextchar has 6 bits
	       // write out all 8 bits
	       convertedVal = lookedUp + bits2;
               n = 0;
               fileOut.put((unsigned char)convertedVal); 
	       break;
         }
      }  
   }

   // return the encoded string
   return(0);
}