8 #ifndef XGBOOST_COMMON_BASE64_H_
9 #define XGBOOST_COMMON_BASE64_H_
11 #include <xgboost/logging.h>
24 read_len_(1), read_ptr_(1) {
25 buffer_.resize(buffer_size);
32 read_len_ = read_ptr_ = 1;
39 if (read_ptr_ < read_len_) {
40 return buffer_[read_ptr_++];
42 read_len_ = stream_->Read(&buffer_[0], buffer_.length());
43 if (read_len_ == 0)
return EOF;
50 return read_len_ == 0;
55 dmlc::Stream *stream_;
67 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
68 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
72 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,
74 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
75 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
77 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38,
78 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,
80 static const char EncodeTable[] =
81 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
88 num_prev = 0; tmp_ch = 0;
98 }
while (isspace(tmp_ch));
102 return num_prev == 0 && (tmp_ch == EOF || isspace(tmp_ch));
104 virtual size_t Read(
void *ptr,
size_t size) {
106 if (size == 0)
return 0;
109 unsigned char *cptr =
static_cast<unsigned char*
>(ptr);
114 *cptr++ = buf_prev[0];
115 *cptr++ = buf_prev[1];
120 *cptr++ = buf_prev[0]; --tlen;
121 buf_prev[0] = buf_prev[1];
126 *cptr++ = buf_prev[0]; --tlen; num_prev = 0;
129 if (tlen == 0)
return size;
133 while (tlen && tmp_ch != EOF && !isspace(tmp_ch)) {
139 CHECK(tmp_ch != EOF && !isspace(tmp_ch)) <<
"invalid base64 format";
141 *cptr++ = (nvalue >> 16) & 0xFF; --tlen;
146 CHECK(tmp_ch != EOF && !isspace(tmp_ch)) <<
"invalid base64 format";
150 CHECK(tmp_ch ==
'=') <<
"invalid base64 format";
152 CHECK(tmp_ch == EOF || isspace(tmp_ch))
153 <<
"invalid base64 format";
158 *cptr++ = (nvalue >> 8) & 0xFF; --tlen;
160 buf_prev[num_prev++] = (nvalue >> 8) & 0xFF;
166 CHECK(tmp_ch != EOF && !isspace(tmp_ch))
167 <<
"invalid base64 format";
170 CHECK(tmp_ch == EOF || isspace(tmp_ch))
171 <<
"invalid base64 format";
176 *cptr++ = nvalue & 0xFF; --tlen;
178 buf_prev[num_prev ++] = nvalue & 0xFF;
185 CHECK_EQ(tlen, 0) <<
"Base64InStream: read incomplete";
189 virtual void Write(
const void *ptr,
size_t size) {
190 LOG(FATAL) <<
"Base64InStream do not support write";
197 unsigned char buf_prev[2];
199 static const bool kStrictCheck =
false;
207 virtual void Write(
const void *ptr,
size_t size) {
208 using base64::EncodeTable;
210 const unsigned char *cptr =
static_cast<const unsigned char*
>(ptr);
212 while (buf_top < 3 && tlen != 0) {
213 buf[++buf_top] = *cptr++; --tlen;
217 PutChar(EncodeTable[buf[1] >> 2]);
218 PutChar(EncodeTable[((buf[1] << 4) | (buf[2] >> 4)) & 0x3F]);
219 PutChar(EncodeTable[((buf[2] << 2) | (buf[3] >> 6)) & 0x3F]);
220 PutChar(EncodeTable[buf[3] & 0x3F]);
225 virtual size_t Read(
void *ptr,
size_t size) {
226 LOG(FATAL) <<
"Base64OutStream do not support read";
234 using base64::EncodeTable;
236 PutChar(EncodeTable[buf[1] >> 2]);
237 PutChar(EncodeTable[(buf[1] << 4) & 0x3F]);
242 PutChar(EncodeTable[buf[1] >> 2]);
243 PutChar(EncodeTable[((buf[1] << 4) | (buf[2] >> 4)) & 0x3F]);
244 PutChar(EncodeTable[(buf[2] << 2) & 0x3F]);
248 if (endch != EOF) PutChar(endch);
255 unsigned char buf[4];
257 static const size_t kBufferSize = 256;
259 inline void PutChar(
char ch) {
261 if (out_buf.length() >= kBufferSize) Flush();
263 inline void Flush(
void) {
264 if (out_buf.length() != 0) {
265 fp->Write(&out_buf[0], out_buf.length());
272 #endif // XGBOOST_COMMON_BASE64_H_