xref: /trunk/main/crashrep/source/win32/base64.cpp (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #include <stdio.h>
29 #include <string.h>
30 #include "base64.h"
31 
32 static const char base64_tab[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
33 
34 extern "C" size_t base64_encode( FILE *fin, FILE *fout )
35 {
36     size_t nBytesRead = 0;
37     size_t nLineLength = 0;
38     size_t nBytesWritten = 0;
39 
40     size_t nBytes = 0;
41 
42     do
43     {
44         unsigned char in_buffer[3];
45 
46         memset( in_buffer, 0, sizeof(in_buffer) );
47         nBytes = fread( in_buffer, 1, sizeof(in_buffer), fin );
48         nBytesRead += nBytes;
49 
50         if ( nBytes )
51         {
52             unsigned long value =
53                 ((unsigned long)in_buffer[0]) << 16 |
54                 ((unsigned long)in_buffer[1]) << 8 |
55                 ((unsigned long)in_buffer[2]) << 0;
56 
57             unsigned char out_buffer[4];
58 
59             memset( out_buffer, '=', sizeof(out_buffer) );
60 
61             out_buffer[0] = base64_tab[(value >> 18) & 0x3F];
62             out_buffer[1] = base64_tab[(value >> 12) & 0x3F];
63 
64             if ( nBytes > 1 )
65             {
66                 out_buffer[2] = base64_tab[(value >> 6) & 0x3F];
67                 if ( nBytes > 2 )
68                     out_buffer[3] = base64_tab[(value >> 0) & 0x3F];
69             }
70 
71             if ( nLineLength >= 76 )
72             {
73                 fputs( "\n", fout );
74                 nLineLength = 0;
75             }
76 
77             nBytesWritten += fwrite( out_buffer, 1, sizeof(out_buffer), fout );
78             nLineLength += sizeof(out_buffer);
79         }
80     } while ( nBytes );
81 
82     return nBytesWritten;
83 }
84