⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 base64.c

📁 linux下的E_MAIL客户端源码
💻 C
📖 第 1 页 / 共 2 页
字号:
//      if ( bufsize == 3 ) {	 Encode64(buffer, ostr);//// Add a newline if necessary//	 linesize += 4;	 if ( linesize > 71 ) {	    *ostr += '\n';            linesize = 0;	 }	 bufsize = 0;      } // End if buffer is full   } // End for each input character//// Handle trailing newline in text mode//   if (LFpending) {      buffer[bufsize] = LF_CHAR;      bufsize++;   }//// Write out any partial buffer.//   if ( bufsize > 0 ) {      Encode64(buffer, bufsize, ostr);      linesize += 4;   } // End if there is a partial buffer//// Add a final newline if necessary//   if ( linesize > 0 )      *ostr += '\n';   if ( closeInput  ) fclose(ifp);   if ( c == EOF && length > 0 ) {      StringC	errmsg("Unexpected end of file \"");      errmsg += ifile;      errmsg += "\" while Base64 encoding.\n";      errmsg += "Expected ";      errmsg += (int)length;      errmsg += " more byte";      if ( length > 1 ) errmsg += 's';      errmsg += '.';      halApp->PopupMessage(errmsg);      return False;   }   return True;} // End FileToText64/*------------------------------------------------------------------------- * Lookup table for base-64 decoding */static unsigned char index_64[128] = {    255,255,255,255, 255,255,255,255, 255,255,255,255, 255,255,255,255,    255,255,255,255, 255,255,255,255, 255,255,255,255, 255,255,255,255,    255,255,255,255, 255,255,255,255, 255,255,255,62, 255,255,255,63,    52,53,54,55, 56,57,58,59, 60,61,255,255, 255,255,255,255,    255, 0, 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,255, 255,255,255,255,    255,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,255, 255,255,255,255};//// Base-64 decoding macro//#define CHAR_64(C)	((((u_int)(C)) > 127) ? 255 : index_64[C])/*------------------------------------------------------------------------- * Function to output a decoded character to a file with conversion of *    CRLF to CR for text. */inline BooleanOutput64(char c, FILE *fp){   int	status = 0;   if ( IsText ) {      if ( CRpending ) {	 if ( c == LF_CHAR ) {	    status = putc('\n', fp);	    CRpending = False;	 }	 else {	    status = putc(CR_CHAR, fp);	    if ( status != EOF && c != CR_CHAR ) {	       status = putc(c, fp);	       CRpending = False;	    }	 }      }      else if ( c == CR_CHAR )	 CRpending = True;      else	 status = putc(c, fp);   } // End if we need newline conversion   else      status = putc(c, fp);   return (status != EOF);} // End Output64/*------------------------------------------------------------------------- * Function to add a decoded character to a string with conversion of *    CRLF to CR for text. */inline voidOutput64(char c, StringC *str){   int	status = 0;   if ( IsText ) {      if ( CRpending ) {	 if ( c == LF_CHAR ) {	    *str += '\n';	    CRpending = False;	 }	 else {	    *str += (char)CR_CHAR;	    if ( status != EOF && c != CR_CHAR ) {	       *str += c;	       CRpending = False;	    }	 }      }      else if ( c == CR_CHAR )	 CRpending = True;      else	 *str += c;   } // End if we need newline conversion   else      *str += c;} // End Output64/*------------------------------------------------------------------------- * Function to convert 4 encoded ints into 3 decoded ints and write the *    result to a file. * * i1 = ..aabbcc * i2 = ..ddeeff * i3 = ..gghhii * i4 = ..jjkkll * * o1 = aabbccdd * o2 = eeffgghh * o3 = iijjkkll */inline BooleanDecode64(char buffer[4], FILE *fp, Boolean *done){//// '=' marks the end of the data//   if ( buffer[0] == '=' || buffer[1] == '=' ) {      *done = True;      return True;   }//// Decode the buffer//   u_int	i1 = CHAR_64(buffer[0]);   u_int	i2 = CHAR_64(buffer[1]);   u_int	i3 = CHAR_64(buffer[2]);   u_int	i4 = CHAR_64(buffer[3]);//// Take the lower 6 bits of each input byte and shift them together//   u_int	o1 = ((i1 & 0x3f) << 2) | ((i2 & 0x30) >> 4);   u_int	o2 = ((i2 & 0x0f) << 4) | ((i3 & 0x3c) >> 2);   u_int	o3 = ((i3 & 0x03) << 6) | ((i4 & 0x3f));   if ( !Output64(o1, fp) ) {      *done  = True;      return False;   }//// Check for end//   if ( buffer[2] == '=' ) {      *done = True;      return True;   }   if ( !Output64(o2, fp) ) {      *done = True;      return False;   }//// Check for end//   if ( buffer[3] == '=' ) {      *done = True;      return True;   }   if ( !Output64(o3, fp) ) {      *done = True;      return False;   }   return True;} // End Decode64/*------------------------------------------------------------------------- * Function to convert 4 encoded ints into 3 decoded ints and add the *    result to a string. */inline voidDecode64(char buffer[4], StringC *str, Boolean *done){//// '=' marks the end of the data//   if ( buffer[0] == '=' || buffer[1] == '=' ) {      *done = True;      return;   }//// Decode the buffer//   u_int	i1 = CHAR_64(buffer[0]);   u_int	i2 = CHAR_64(buffer[1]);   u_int	i3 = CHAR_64(buffer[2]);   u_int	i4 = CHAR_64(buffer[3]);//// Take the lower 6 bits of each input byte and shift them together//   u_int	o1 = ((i1 & 0x3f) << 2) | ((i2 & 0x30) >> 4);   u_int	o2 = ((i2 & 0x0f) << 4) | ((i3 & 0x3c) >> 2);   u_int	o3 = ((i3 & 0x03) << 6) | ((i4 & 0x3f));   Output64(o1, str);//// Check for end//   if ( buffer[2] == '=' ) {      *done = True;      return;   }   Output64(o2, str);//// Check for end//   if ( buffer[3] == '=' ) {      *done = True;      return;   }   Output64(o3, str);} // End Decode64/*------------------------------------------------------------------------- * Function to decode a base-64 encoded character array and place the decoded *    output in a string */BooleanText64ToText(CharC istr, StringC *ostr, Boolean isText){   IsText    = isText;   CRpending = False;   istr.Trim();		// skip leading and trailing blank lines//// Loop through data 4 chars at a time//   char		buffer[4];   int		bufsize = 0;   Boolean	done  = False;   for (int i=0; !done && i<istr.Length(); i++) {//// Skip whitespace characters//      if ( isspace(istr[i]) || istr[i] == '\n' )	 continue;//// Add this character to the buffer//      buffer[bufsize++] = istr[i];//// Process the buffer if it is full//      if ( bufsize == 4 ) {	 Decode64(buffer, ostr, &done);	 bufsize = 0;      }   } // End for each input character   return True;} // End Text64ToText/*------------------------------------------------------------------------- * Function to decode a base-64 encoded file and place the decoded *    output in a file */BooleanFile64ToFile(const char *ifile, const char *ofile, Boolean isText,	     FILE *ifp, FILE *ofp, u_int offset, u_int length){   IsText    = isText;   CRpending = False;   Boolean	closeInput  = (ifp == NULL);   Boolean	closeOutput = (ofp == NULL);   if ( !ifp ) {//// Open input file//      ifp = fopen(ifile, "r");      if ( !ifp ) {	 StringC	errmsg("Could not open file \"");	 errmsg += ifile;	 errmsg += "\" for Base64 decoding.\n";	 errmsg += SystemErrorMessage(errno);	 halApp->PopupMessage(errmsg);	 return False;      }//// Initialize length//      if ( length == 0 ) {	 fseek(ifp, 0, SEEK_END);	 length = (u_int)ftell(ifp);      }   } // End if input file is not open//// Move to offset in input file//   if ( fseek(ifp, offset, SEEK_SET) != 0 ) {      StringC	errmsg("Could not seek to offset ");      errmsg += (int)offset;      errmsg += " in input file ";      errmsg += ifile;      errmsg += "\" for Base64 decoding.\n";      errmsg += SystemErrorMessage(errno);      halApp->PopupMessage(errmsg);      if ( closeInput ) fclose(ifp);      return False;   }   if ( !ofp ) {//// Create an output file//      ofp = fopen(ofile, "w+");      if ( !ofp ) {	 StringC	errmsg("Could not create file \"");	 errmsg += ofile;	 errmsg += "\" for Base64 encoding.\n";	 errmsg += SystemErrorMessage(errno);	 halApp->PopupMessage(errmsg);	 if ( closeInput ) fclose(ifp);	 return False;      }   }//// Loop through data 4 chars at a time//   char		buffer[4];   int		bufsize = 0;   Boolean	done  = False;   Boolean	error = False;   int		c;   while ( !done && length>0 && (c=getc(ifp)) != EOF ) {      length--;//// Skip whitespace characters//      if ( isspace(c) || c == '\n' )	 continue;//// Add this character to the buffer//      buffer[bufsize++] = (char)c;//// Process the buffer if it is full//      if ( bufsize == 4 ) {	 error = !Decode64(buffer, ofp, &done);	 bufsize = 0;      }   } // End for each input character   if ( error ) {      StringC	errmsg("Could not write file \"");      errmsg += ofile;      errmsg += "\" for Base64 encoding.\n";      errmsg += SystemErrorMessage(errno);      halApp->PopupMessage(errmsg);   }   else if ( c == EOF && length > 0 ) {      StringC	errmsg("Unexpected end of file \"");      errmsg += ifile;      errmsg += "\" while Base64 decoding.\n";      errmsg += "Expected ";      errmsg += (int)length;      errmsg += " more byte";      if ( length > 1 ) errmsg += 's';      errmsg += '.';      halApp->PopupMessage(errmsg);      error = True;   }   if ( closeInput  ) fclose(ifp);   if ( closeOutput ) fclose(ofp);   return !error;} // End File64ToFile/*------------------------------------------------------------------------- * Function to decode a base-64 encoded character array and place the decoded *    output in a file */BooleanText64ToFile(CharC istr, const char *ofile, Boolean isText, FILE *ofp){   IsText    = isText;   CRpending = False;   Boolean	closeOutput = (ofp == NULL);   if ( !ofp ) {//// Create an output file//      ofp = fopen(ofile, "w+");      if ( !ofp ) {	 StringC	errmsg("Could not create file \"");	 errmsg += ofile;	 errmsg += "\" for Base64 encoding.\n";	 errmsg += SystemErrorMessage(errno);	 halApp->PopupMessage(errmsg);	 return False;      }   }   istr.Trim();		// skip leading and trailing blank lines//// Loop through data 4 chars at a time//   char		buffer[4];   int		bufsize = 0;   Boolean	done  = False;   Boolean	error = False;   for (int i=0; !done && i<istr.Length(); i++) {//// Skip whitespace characters//      if ( isspace(istr[i]) || istr[i] == '\n' )	 continue;//// Add this character to the buffer//      buffer[bufsize++] = istr[i];//// Process the buffer if it is full//      if ( bufsize == 4 ) {	 error = !Decode64(buffer, ofp, &done);	 bufsize = 0;      }   } // End for each input character   if ( error ) {      StringC	errmsg("Could not write file \"");      errmsg += ofile;      errmsg += "\" for Base64 encoding.\n";      errmsg += SystemErrorMessage(errno);      halApp->PopupMessage(errmsg);   }   if ( closeOutput ) fclose(ofp);   return !error;} // End Text64ToFile/*------------------------------------------------------------------------- * Function to decode a base-64 encoded file and place the decoded *    output in a text string */BooleanFile64ToText(const char *ifile, StringC *ostr, Boolean isText, FILE *ifp,	     u_int offset, u_int length){   IsText    = isText;   CRpending = False;   Boolean	closeInput  = (ifp == NULL);   if ( !ifp ) {//// Open input file//      ifp = fopen(ifile, "r");      if ( !ifp ) {	 StringC	errmsg("Could not open file \"");	 errmsg += ifile;	 errmsg += "\" for Base64 decoding.\n";	 errmsg += SystemErrorMessage(errno);	 halApp->PopupMessage(errmsg);	 return False;      }//// Initialize length//      if ( length == 0 ) {	 fseek(ifp, 0, SEEK_END);	 length = (u_int)ftell(ifp);      }   } // End if input file is not open//// Move to offset in input file//   if ( fseek(ifp, offset, SEEK_SET) != 0 ) {      StringC	errmsg("Could not seek to offset ");      errmsg += (int)offset;      errmsg += " in input file ";      errmsg += ifile;      errmsg += "\" for Base64 decoding.\n";      errmsg += SystemErrorMessage(errno);      halApp->PopupMessage(errmsg);      if ( closeInput ) fclose(ifp);      return False;   }//// Loop through data 4 chars at a time//   char		buffer[4];   int		bufsize = 0;   Boolean	done  = False;   int		c;   while ( length>0 && (c=getc(ifp)) != EOF ) {      length--;//// Skip whitespace characters//      if ( isspace(c) || c == '\n' )	 continue;//// Add this character to the buffer//      buffer[bufsize++] = (char)c;//// Process the buffer if it is full//      if ( bufsize == 4 ) {	 Decode64(buffer, ostr, &done);	 bufsize = 0;      }   } // End for each input character   if ( closeInput  ) fclose(ifp);   if ( c == EOF && length > 0 ) {      StringC	errmsg("Unexpected end of file \"");      errmsg += ifile;      errmsg += "\" while Base64 decoding.\n";      errmsg += "Expected ";      errmsg += (int)length;      errmsg += " more byte";      if ( length > 1 ) errmsg += 's';      errmsg += '.';      halApp->PopupMessage(errmsg);      return False;   }   return True;} // End File64ToText/*----------------------------------------------------------------------- * Function to encode the given string using RFC1522 B encoding and *    store the result in a string */#define MAX_1522_WORD_LENGTH	75BooleanTextToText1522B(CharC istr, CharC charset, StringC *ostr){//// Encode the input//   StringC	result;   if ( !TextToText64(istr, &result, True/*is text*/, False/*no line break*/) )      return False;//// If the result is too long, we may have to split it.//   int		max = MAX_1522_WORD_LENGTH - charset.Length() - 7;   CharC	remaining = result;   while ( remaining.Length() > max ) {//// Add this word//      *ostr += "=?";      *ostr += charset;      *ostr += "?B?";      *ostr += remaining(0, max);      *ostr += "?= "; // An extra space is required between split words//// Remove this word from the remaining//      remaining.CutBeg(max);   } // End for each output word required//// Add whatever's left//   *ostr += "=?";   *ostr += charset;   *ostr += "?B?";   *ostr += remaining;   *ostr += "?=";   return True;} // End TextToText1522B

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -