Warning, file /libraries/qca/src/botantools/botan/charset.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002 Copyright (C) 1999-2007 The Botan Project. All rights reserved.
0003 
0004 Redistribution and use in source and binary forms, for any use, with or without
0005 modification, is permitted provided that the following conditions are met:
0006 
0007 1. Redistributions of source code must retain the above copyright notice, this
0008 list of conditions, and the following disclaimer.
0009 
0010 2. Redistributions in binary form must reproduce the above copyright notice,
0011 this list of conditions, and the following disclaimer in the documentation
0012 and/or other materials provided with the distribution.
0013 
0014 THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) "AS IS" AND ANY EXPRESS OR IMPLIED
0015 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
0016 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
0017 
0018 IN NO EVENT SHALL THE AUTHOR(S) OR CONTRIBUTOR(S) BE LIABLE FOR ANY DIRECT,
0019 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
0020 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0021 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
0022 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
0023 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
0024 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0025 */
0026 // LICENSEHEADER_END
0027 namespace QCA { // WRAPNS_LINE
0028 /*************************************************
0029  * Character Set Handling Source File             *
0030  * (C) 1999-2007 The Botan Project                *
0031  *************************************************/
0032 
0033 } // WRAPNS_LINE
0034 #include <botan/charset.h>
0035 namespace QCA { // WRAPNS_LINE
0036 #ifdef BOTAN_TOOLS_ONLY
0037 } // WRAPNS_LINE
0038 #include <botan/exceptn.h>
0039 namespace QCA { // WRAPNS_LINE
0040 #else
0041 } // WRAPNS_LINE
0042 #include <botan/hex.h>
0043 namespace QCA { // WRAPNS_LINE
0044 } // WRAPNS_LINE
0045 #include <botan/base64.h>
0046 namespace QCA { // WRAPNS_LINE
0047 #endif
0048 } // WRAPNS_LINE
0049 #include <botan/libstate.h>
0050 namespace QCA { // WRAPNS_LINE
0051 } // WRAPNS_LINE
0052 #include <cctype>
0053 namespace QCA { // WRAPNS_LINE
0054 } // WRAPNS_LINE
0055 #include <cctype>
0056 namespace QCA { // WRAPNS_LINE
0057 
0058 namespace Botan {
0059 
0060 namespace Charset {
0061 
0062 /*************************************************
0063  * Perform character set transcoding              *
0064  *************************************************/
0065 #ifndef BOTAN_TOOLS_ONLY
0066 std::string transcode(const std::string &str, Character_Set to, Character_Set from)
0067 {
0068     return global_state().transcode(str, to, from);
0069 }
0070 #endif
0071 
0072 /*************************************************
0073  * Check if a character represents a digit        *
0074  *************************************************/
0075 bool is_digit(char c)
0076 {
0077     if (c == '0' || c == '1' || c == '2' || c == '3' || c == '4' || c == '5' || c == '6' || c == '7' || c == '8' ||
0078         c == '9')
0079         return true;
0080     return false;
0081 }
0082 
0083 /*************************************************
0084  * Check if a character represents whitespace     *
0085  *************************************************/
0086 bool is_space(char c)
0087 {
0088     if (c == ' ' || c == '\t' || c == '\n' || c == '\r')
0089         return true;
0090     return false;
0091 }
0092 
0093 /*************************************************
0094  * Convert a character to a digit                 *
0095  *************************************************/
0096 byte char2digit(char c)
0097 {
0098     switch (c) {
0099     case '0':
0100         return 0;
0101     case '1':
0102         return 1;
0103     case '2':
0104         return 2;
0105     case '3':
0106         return 3;
0107     case '4':
0108         return 4;
0109     case '5':
0110         return 5;
0111     case '6':
0112         return 6;
0113     case '7':
0114         return 7;
0115     case '8':
0116         return 8;
0117     case '9':
0118         return 9;
0119     }
0120 
0121     throw Invalid_Argument("char2digit: Input is not a digit character");
0122 }
0123 
0124 /*************************************************
0125  * Convert a digit to a character                 *
0126  *************************************************/
0127 char digit2char(byte b)
0128 {
0129     switch (b) {
0130     case 0:
0131         return '0';
0132     case 1:
0133         return '1';
0134     case 2:
0135         return '2';
0136     case 3:
0137         return '3';
0138     case 4:
0139         return '4';
0140     case 5:
0141         return '5';
0142     case 6:
0143         return '6';
0144     case 7:
0145         return '7';
0146     case 8:
0147         return '8';
0148     case 9:
0149         return '9';
0150     }
0151 
0152     throw Invalid_Argument("digit2char: Input is not a digit");
0153 }
0154 
0155 /*************************************************
0156  * Case-insensitive character comparison          *
0157  *************************************************/
0158 bool caseless_cmp(char a, char b)
0159 {
0160     return (tolower((unsigned char)a) == tolower((unsigned char)b));
0161 }
0162 
0163 }
0164 
0165 #ifndef BOTAN_TOOLS_ONLY
0166 
0167 /*************************************************
0168  * Hex Encoder Lookup Tables                      *
0169  *************************************************/
0170 const byte Hex_Encoder::BIN_TO_HEX_UPPER[16] =
0171     {0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46};
0172 
0173 const byte Hex_Encoder::BIN_TO_HEX_LOWER[16] =
0174     {0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66};
0175 
0176 /*************************************************
0177  * Base64 Encoder Lookup Table                    *
0178  *************************************************/
0179 const byte Base64_Encoder::BIN_TO_BASE64[64] = {
0180     0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50,
0181     0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
0182     0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76,
0183     0x77, 0x78, 0x79, 0x7A, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x2B, 0x2F};
0184 
0185 /*************************************************
0186  * Hex Decoder Lookup Table                       *
0187  *************************************************/
0188 const byte Hex_Decoder::HEX_TO_BIN[256] = {
0189     0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
0190     0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
0191     0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0192     0x09, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x80, 0x80, 0x80, 0x80, 0x80,
0193     0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
0194     0x80, 0x80, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
0195     0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
0196     0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
0197     0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
0198     0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
0199     0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
0200     0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
0201     0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
0202     0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80};
0203 
0204 /*************************************************
0205  * Base64 Decoder Lookup Table                    *
0206  *************************************************/
0207 const byte Base64_Decoder::BASE64_TO_BIN[256] = {
0208     0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
0209     0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
0210     0x80, 0x80, 0x80, 0x80, 0x80, 0x3E, 0x80, 0x80, 0x80, 0x3F, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C,
0211     0x3D, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A,
0212     0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x80, 0x80, 0x80, 0x80,
0213     0x80, 0x80, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A,
0214     0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
0215     0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
0216     0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
0217     0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
0218     0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
0219     0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
0220     0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
0221     0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80};
0222 
0223 #endif
0224 
0225 }
0226 } // WRAPNS_LINE