xref: /aoo41x/main/crashrep/source/win32/base64.cpp (revision 79aad27f)
1*e7917716SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*e7917716SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*e7917716SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*e7917716SAndrew Rist  * distributed with this work for additional information
6*e7917716SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*e7917716SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*e7917716SAndrew Rist  * "License"); you may not use this file except in compliance
9*e7917716SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*e7917716SAndrew Rist  *
11*e7917716SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*e7917716SAndrew Rist  *
13*e7917716SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*e7917716SAndrew Rist  * software distributed under the License is distributed on an
15*e7917716SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*e7917716SAndrew Rist  * KIND, either express or implied.  See the License for the
17*e7917716SAndrew Rist  * specific language governing permissions and limitations
18*e7917716SAndrew Rist  * under the License.
19*e7917716SAndrew Rist  *
20*e7917716SAndrew Rist  *************************************************************/
21*e7917716SAndrew Rist 
22*e7917716SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include <stdio.h>
25cdf0e10cSrcweir #include <string.h>
26cdf0e10cSrcweir #include "base64.h"
27cdf0e10cSrcweir 
28cdf0e10cSrcweir static const char base64_tab[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
29cdf0e10cSrcweir 
base64_encode(FILE * fin,FILE * fout)30cdf0e10cSrcweir extern "C" size_t base64_encode( FILE *fin, FILE *fout )
31cdf0e10cSrcweir {
32cdf0e10cSrcweir 	size_t nBytesRead = 0;
33cdf0e10cSrcweir 	size_t nLineLength = 0;
34cdf0e10cSrcweir 	size_t nBytesWritten = 0;
35cdf0e10cSrcweir 
36cdf0e10cSrcweir 	size_t nBytes = 0;
37cdf0e10cSrcweir 
38cdf0e10cSrcweir 	do
39cdf0e10cSrcweir 	{
40cdf0e10cSrcweir 		unsigned char in_buffer[3];
41cdf0e10cSrcweir 
42cdf0e10cSrcweir 		memset( in_buffer, 0, sizeof(in_buffer) );
43cdf0e10cSrcweir 		nBytes = fread( in_buffer, 1, sizeof(in_buffer), fin );
44cdf0e10cSrcweir 		nBytesRead += nBytes;
45cdf0e10cSrcweir 
46cdf0e10cSrcweir 		if ( nBytes )
47cdf0e10cSrcweir 		{
48cdf0e10cSrcweir 			unsigned long value =
49cdf0e10cSrcweir 				((unsigned long)in_buffer[0]) << 16 |
50cdf0e10cSrcweir 				((unsigned long)in_buffer[1]) << 8 |
51cdf0e10cSrcweir 				((unsigned long)in_buffer[2]) << 0;
52cdf0e10cSrcweir 
53cdf0e10cSrcweir 			unsigned char out_buffer[4];
54cdf0e10cSrcweir 
55cdf0e10cSrcweir 			memset( out_buffer, '=', sizeof(out_buffer) );
56cdf0e10cSrcweir 
57cdf0e10cSrcweir 			out_buffer[0] = base64_tab[(value >> 18) & 0x3F];
58cdf0e10cSrcweir 			out_buffer[1] = base64_tab[(value >> 12) & 0x3F];
59cdf0e10cSrcweir 
60cdf0e10cSrcweir 			if ( nBytes > 1 )
61cdf0e10cSrcweir 			{
62cdf0e10cSrcweir 				out_buffer[2] = base64_tab[(value >> 6) & 0x3F];
63cdf0e10cSrcweir 				if ( nBytes > 2 )
64cdf0e10cSrcweir 					out_buffer[3] = base64_tab[(value >> 0) & 0x3F];
65cdf0e10cSrcweir 			}
66cdf0e10cSrcweir 
67cdf0e10cSrcweir 			if ( nLineLength >= 76 )
68cdf0e10cSrcweir 			{
69cdf0e10cSrcweir 				fputs( "\n", fout );
70cdf0e10cSrcweir 				nLineLength = 0;
71cdf0e10cSrcweir 			}
72cdf0e10cSrcweir 
73cdf0e10cSrcweir 			nBytesWritten += fwrite( out_buffer, 1, sizeof(out_buffer), fout );
74cdf0e10cSrcweir 			nLineLength += sizeof(out_buffer);
75cdf0e10cSrcweir 		}
76cdf0e10cSrcweir 	} while ( nBytes );
77cdf0e10cSrcweir 
78cdf0e10cSrcweir 	return nBytesWritten;
79cdf0e10cSrcweir }
80