comparison C++/Base64.h @ 242:a9883eeb9757

Add tests for Base64
author David Demelier <markand@malikania.fr>
date Thu, 11 Sep 2014 21:09:58 +0200
parents b97d75a78e22
children 73e5381d7baf
comparison
equal deleted inserted replaced
241:d9409b338f2f 242:a9883eeb9757
65 65
66 for (count = 0; count < 3 && input != end; ++count) 66 for (count = 0; count < 3 && input != end; ++count)
67 inputbuf[count] = *input++; 67 inputbuf[count] = *input++;
68 68
69 *output++ = lookup(inputbuf[0] >> 2 & 0x3f); 69 *output++ = lookup(inputbuf[0] >> 2 & 0x3f);
70 *output++ = lookup(inputbuf[0] << 4 & 0x3f | inputbuf[1] >> 4 & 0x0f); 70 *output++ = lookup((inputbuf[0] << 4 & 0x3f) | (inputbuf[1] >> 4 & 0x0f));
71 *output++ = (count < 2) ? '=' : lookup(inputbuf[1] << 2 & 0x3c | inputbuf[2] >> 6 & 0x03); 71 *output++ = (count < 2) ? '=' : lookup((inputbuf[1] << 2 & 0x3c) | (inputbuf[2] >> 6 & 0x03));
72 *output++ = (count < 3) ? '=' : lookup(inputbuf[2] & 0x3f); 72 *output++ = (count < 3) ? '=' : lookup(inputbuf[2] & 0x3f);
73 } 73 }
74 } 74 }
75 75
76 /** 76 /**
96 } 96 }
97 97
98 if (count != 4) 98 if (count != 4)
99 throw std::invalid_argument("truncated string"); 99 throw std::invalid_argument("truncated string");
100 100
101 *output++ = inputbuf[0] << 2 & 0xfc | inputbuf[1] >> 4 & 0x03; 101 *output++ = (inputbuf[0] << 2 & 0xfc) | (inputbuf[1] >> 4 & 0x03);
102 102
103 if (inputbuf[2] != '=') 103 if (inputbuf[2] != '=')
104 *output++ = inputbuf[1] << 4 & 0xf0 | inputbuf[2] >> 2 & 0x0f; 104 *output++ = (inputbuf[1] << 4 & 0xf0) | (inputbuf[2] >> 2 & 0x0f);
105 if (inputbuf[3] != '=') 105 if (inputbuf[3] != '=')
106 *output++ = inputbuf[2] << 6 & 0xc0 | inputbuf[3] & 0x3f; 106 *output++ = (inputbuf[2] << 6 & 0xc0) | (inputbuf[3] & 0x3f);
107 } 107 }
108 } 108 }
109 109
110 /** 110 /**
111 * Encode a string. 111 * Encode a string.