source: XMLIO_V2/external/src/POCO/Foundation.save/Base64Encoder.cpp @ 80

Last change on this file since 80 was 80, checked in by ymipsl, 14 years ago

ajout lib externe

  • Property svn:eol-style set to native
File size: 4.0 KB
Line 
1//
2// Base64Encoder.cpp
3//
4// $Id: //poco/1.3/Foundation/src/Base64Encoder.cpp#2 $
5//
6// Library: Foundation
7// Package: Streams
8// Module:  Base64
9//
10// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
11// and Contributors.
12//
13// Permission is hereby granted, free of charge, to any person or organization
14// obtaining a copy of the software and accompanying documentation covered by
15// this license (the "Software") to use, reproduce, display, distribute,
16// execute, and transmit the Software, and to prepare derivative works of the
17// Software, and to permit third-parties to whom the Software is furnished to
18// do so, all subject to the following:
19//
20// The copyright notices in the Software and this entire statement, including
21// the above license grant, this restriction and the following disclaimer,
22// must be included in all copies of the Software, in whole or in part, and
23// all derivative works of the Software, unless such copies or derivative
24// works are solely in the form of machine-executable object code generated by
25// a source language processor.
26//
27// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
30// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
31// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
32// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
33// DEALINGS IN THE SOFTWARE.
34//
35
36
37#include "Poco/Base64Encoder.h"
38
39
40namespace Poco {
41
42
43const unsigned char Base64EncoderBuf::OUT_ENCODING[64] =
44{
45        'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
46        'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
47        'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
48        'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
49        'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
50        'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
51        'w', 'x', 'y', 'z', '0', '1', '2', '3', 
52        '4', '5', '6', '7', '8', '9', '+', '/'
53};
54
55
56Base64EncoderBuf::Base64EncoderBuf(std::ostream& ostr): 
57        _groupLength(0),
58        _pos(0),
59        _lineLength(72),
60        _ostr(ostr)
61{
62}
63
64
65Base64EncoderBuf::~Base64EncoderBuf()
66{
67        try
68        {
69                close();
70        }
71        catch (...)
72        {
73        }
74}
75
76
77void Base64EncoderBuf::setLineLength(int lineLength)
78{
79        _lineLength = lineLength;
80}
81
82
83int Base64EncoderBuf::getLineLength() const
84{
85        return _lineLength;
86}
87
88
89int Base64EncoderBuf::writeToDevice(char c)
90{
91        _group[_groupLength++] = (unsigned char) c;
92        if (_groupLength == 3)
93        {
94                unsigned char idx;
95                idx = _group[0] >> 2;
96                _ostr.put(OUT_ENCODING[idx]);
97                idx = ((_group[0] & 0x03) << 4) | (_group[1] >> 4);
98                _ostr.put(OUT_ENCODING[idx]);
99                idx = ((_group[1] & 0x0F) << 2) | (_group[2] >> 6);
100                _ostr.put(OUT_ENCODING[idx]);
101                idx = _group[2] & 0x3F;
102                _ostr.put(OUT_ENCODING[idx]);
103                _pos += 4;
104                if (_lineLength > 0 && _pos >= _lineLength) 
105                {
106                        _ostr << "\r\n";
107                        _pos = 0;
108                }
109                _groupLength = 0;
110        }
111        return _ostr ? charToInt(c) : -1;
112}
113
114
115int Base64EncoderBuf::close()
116{
117        sync();
118        if (_groupLength == 1)
119        {
120                _group[1] = 0;
121                unsigned char idx;
122                idx = _group[0] >> 2;
123                _ostr.put(OUT_ENCODING[idx]);
124                idx = ((_group[0] & 0x03) << 4) | (_group[1] >> 4);
125                _ostr.put(OUT_ENCODING[idx]);
126                _ostr << "==";
127        }
128        else if (_groupLength == 2)
129        {
130                _group[2] = 0;
131                unsigned char idx;
132                idx = _group[0] >> 2;
133                _ostr.put(OUT_ENCODING[idx]);
134                idx = ((_group[0] & 0x03) << 4) | (_group[1] >> 4);
135                _ostr.put(OUT_ENCODING[idx]);
136                idx = ((_group[1] & 0x0F) << 2) | (_group[2] >> 6);
137                _ostr.put(OUT_ENCODING[idx]);
138                _ostr.put('=');
139        }
140        _ostr.flush();
141        _groupLength = 0;
142        return _ostr ? 0 : -1;
143}
144
145
146Base64EncoderIOS::Base64EncoderIOS(std::ostream& ostr): _buf(ostr)
147{
148        poco_ios_init(&_buf);
149}
150
151
152Base64EncoderIOS::~Base64EncoderIOS()
153{
154}
155
156
157int Base64EncoderIOS::close()
158{
159        return _buf.close();
160}
161
162
163Base64EncoderBuf* Base64EncoderIOS::rdbuf()
164{
165        return &_buf;
166}
167
168
169Base64Encoder::Base64Encoder(std::ostream& ostr): Base64EncoderIOS(ostr), std::ostream(&_buf)
170{
171}
172
173
174Base64Encoder::~Base64Encoder()
175{
176}
177
178
179} // namespace Poco
Note: See TracBrowser for help on using the repository browser.