[80] | 1 | // |
---|
| 2 | // Base64Decoder.cpp |
---|
| 3 | // |
---|
| 4 | // $Id: //poco/1.3/Foundation/src/Base64Decoder.cpp#3 $ |
---|
| 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/Base64Decoder.h" |
---|
| 38 | #include "Poco/Base64Encoder.h" |
---|
| 39 | #include "Poco/Exception.h" |
---|
| 40 | #include "Poco/Mutex.h" |
---|
| 41 | |
---|
| 42 | |
---|
| 43 | namespace Poco { |
---|
| 44 | |
---|
| 45 | |
---|
| 46 | unsigned char Base64DecoderBuf::IN_ENCODING[256]; |
---|
| 47 | bool Base64DecoderBuf::IN_ENCODING_INIT = false; |
---|
| 48 | |
---|
| 49 | |
---|
| 50 | Base64DecoderBuf::Base64DecoderBuf(std::istream& istr): |
---|
| 51 | _groupLength(0), |
---|
| 52 | _groupIndex(0), |
---|
| 53 | _istr(istr) |
---|
| 54 | { |
---|
| 55 | static FastMutex mutex; |
---|
| 56 | FastMutex::ScopedLock lock(mutex); |
---|
| 57 | if (!IN_ENCODING_INIT) |
---|
| 58 | { |
---|
| 59 | for (unsigned i = 0; i < sizeof(IN_ENCODING); i++) |
---|
| 60 | { |
---|
| 61 | IN_ENCODING[i] = 0xFF; |
---|
| 62 | } |
---|
| 63 | for (unsigned i = 0; i < sizeof(Base64EncoderBuf::OUT_ENCODING); i++) |
---|
| 64 | { |
---|
| 65 | IN_ENCODING[Base64EncoderBuf::OUT_ENCODING[i]] = i; |
---|
| 66 | } |
---|
| 67 | IN_ENCODING[static_cast<unsigned char>('=')] = '\0'; |
---|
| 68 | IN_ENCODING_INIT = true; |
---|
| 69 | } |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | |
---|
| 73 | Base64DecoderBuf::~Base64DecoderBuf() |
---|
| 74 | { |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | |
---|
| 78 | int Base64DecoderBuf::readFromDevice() |
---|
| 79 | { |
---|
| 80 | if (_groupIndex < _groupLength) |
---|
| 81 | { |
---|
| 82 | return _group[_groupIndex++]; |
---|
| 83 | } |
---|
| 84 | else |
---|
| 85 | { |
---|
| 86 | unsigned char buffer[4]; |
---|
| 87 | int c; |
---|
| 88 | if ((c = readOne()) == -1) return -1; |
---|
| 89 | buffer[0] = (unsigned char) c; |
---|
| 90 | if (IN_ENCODING[buffer[0]] == 0xFF) throw DataFormatException(); |
---|
| 91 | if ((c = readOne()) == -1) throw DataFormatException(); |
---|
| 92 | buffer[1] = (unsigned char) c; |
---|
| 93 | if (IN_ENCODING[buffer[1]] == 0xFF) throw DataFormatException(); |
---|
| 94 | if ((c = readOne()) == -1) throw DataFormatException(); |
---|
| 95 | buffer[2] = c; |
---|
| 96 | if (IN_ENCODING[buffer[2]] == 0xFF) throw DataFormatException(); |
---|
| 97 | if ((c = readOne()) == -1) throw DataFormatException(); |
---|
| 98 | buffer[3] = c; |
---|
| 99 | if (IN_ENCODING[buffer[3]] == 0xFF) throw DataFormatException(); |
---|
| 100 | |
---|
| 101 | _group[0] = (IN_ENCODING[buffer[0]] << 2) | (IN_ENCODING[buffer[1]] >> 4); |
---|
| 102 | _group[1] = ((IN_ENCODING[buffer[1]] & 0x0F) << 4) | (IN_ENCODING[buffer[2]] >> 2); |
---|
| 103 | _group[2] = (IN_ENCODING[buffer[2]] << 6) | IN_ENCODING[buffer[3]]; |
---|
| 104 | |
---|
| 105 | if (buffer[2] == '=') |
---|
| 106 | _groupLength = 1; |
---|
| 107 | else if (buffer[3] == '=') |
---|
| 108 | _groupLength = 2; |
---|
| 109 | else |
---|
| 110 | _groupLength = 3; |
---|
| 111 | _groupIndex = 1; |
---|
| 112 | return _group[0]; |
---|
| 113 | } |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | |
---|
| 117 | int Base64DecoderBuf::readOne() |
---|
| 118 | { |
---|
| 119 | int ch = _istr.get(); |
---|
| 120 | while (ch == ' ' || ch == '\r' || ch == '\t' || ch == '\n') |
---|
| 121 | ch = _istr.get(); |
---|
| 122 | return ch; |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | |
---|
| 126 | Base64DecoderIOS::Base64DecoderIOS(std::istream& istr): _buf(istr) |
---|
| 127 | { |
---|
| 128 | poco_ios_init(&_buf); |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | |
---|
| 132 | Base64DecoderIOS::~Base64DecoderIOS() |
---|
| 133 | { |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | |
---|
| 137 | Base64DecoderBuf* Base64DecoderIOS::rdbuf() |
---|
| 138 | { |
---|
| 139 | return &_buf; |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | |
---|
| 143 | Base64Decoder::Base64Decoder(std::istream& istr): Base64DecoderIOS(istr), std::istream(&_buf) |
---|
| 144 | { |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | |
---|
| 148 | Base64Decoder::~Base64Decoder() |
---|
| 149 | { |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | |
---|
| 153 | } // namespace Poco |
---|