| 32 |
#include <poll.h> |
#include <poll.h> |
| 33 |
#endif |
#endif |
| 34 |
|
|
| 35 |
|
enum _io_constant_t |
| 36 |
|
{ |
| 37 |
|
OUTPUT_BUF_SIZE = 8192, |
| 38 |
|
}; |
| 39 |
|
|
| 40 |
const char BBS_default_charset[CHARSET_MAX_LEN + 1] = "UTF-8"; |
const char BBS_default_charset[CHARSET_MAX_LEN + 1] = "UTF-8"; |
| 41 |
char stdio_charset[CHARSET_MAX_LEN + 1] = "UTF-8"; |
char stdio_charset[CHARSET_MAX_LEN + 1] = "UTF-8"; |
| 42 |
|
|
| 51 |
|
|
| 52 |
// static input / output buffer |
// static input / output buffer |
| 53 |
static char stdin_buf[LINE_BUFFER_LEN]; |
static char stdin_buf[LINE_BUFFER_LEN]; |
| 54 |
static char stdout_buf[BUFSIZ]; |
static char stdout_buf[OUTPUT_BUF_SIZE]; |
| 55 |
static int stdin_buf_len = 0; |
static int stdin_buf_len = 0; |
| 56 |
static int stdout_buf_len = 0; |
static int stdout_buf_len = 0; |
| 57 |
static int stdin_buf_offset = 0; |
static int stdin_buf_offset = 0; |
| 58 |
static int stdout_buf_offset = 0; |
static int stdout_buf_offset = 0; |
| 59 |
|
|
| 60 |
static char stdin_conv[LINE_BUFFER_LEN * 2]; |
static char stdin_conv[LINE_BUFFER_LEN * 2]; |
| 61 |
static char stdout_conv[BUFSIZ * 2]; |
static char stdout_conv[OUTPUT_BUF_SIZE * 2]; |
| 62 |
static int stdin_conv_len = 0; |
static int stdin_conv_len = 0; |
| 63 |
static int stdout_conv_len = 0; |
static int stdout_conv_len = 0; |
| 64 |
static int stdin_conv_offset = 0; |
static int stdin_conv_offset = 0; |
| 65 |
static int stdout_conv_offset = 0; |
static int stdout_conv_offset = 0; |
| 66 |
|
|
| 67 |
static iconv_t stdin_cd = NULL; |
static iconv_t stdin_cd = (iconv_t)(-1); |
| 68 |
static iconv_t stdout_cd = NULL; |
static iconv_t stdout_cd = (iconv_t)(-1); |
| 69 |
|
|
| 70 |
int io_init(void) |
int io_init(void) |
| 71 |
{ |
{ |
| 77 |
stdin_epollfd = epoll_create1(0); |
stdin_epollfd = epoll_create1(0); |
| 78 |
if (stdin_epollfd == -1) |
if (stdin_epollfd == -1) |
| 79 |
{ |
{ |
| 80 |
log_error("epoll_create1() error (%d)\n", errno); |
log_error("epoll_create1() error (%d)", errno); |
| 81 |
return -1; |
return -1; |
| 82 |
} |
} |
| 83 |
|
|
| 85 |
ev.data.fd = STDIN_FILENO; |
ev.data.fd = STDIN_FILENO; |
| 86 |
if (epoll_ctl(stdin_epollfd, EPOLL_CTL_ADD, STDIN_FILENO, &ev) == -1) |
if (epoll_ctl(stdin_epollfd, EPOLL_CTL_ADD, STDIN_FILENO, &ev) == -1) |
| 87 |
{ |
{ |
| 88 |
log_error("epoll_ctl(STDIN_FILENO) error (%d)\n", errno); |
log_error("epoll_ctl(STDIN_FILENO) error (%d)", errno); |
| 89 |
if (close(stdin_epollfd) < 0) |
if (close(stdin_epollfd) < 0) |
| 90 |
{ |
{ |
| 91 |
log_error("close(stdin_epollfd) error (%d)\n"); |
log_error("close(stdin_epollfd) error (%d)", errno); |
| 92 |
} |
} |
| 93 |
stdin_epollfd = -1; |
stdin_epollfd = -1; |
| 94 |
return -1; |
return -1; |
| 95 |
} |
} |
| 96 |
|
|
| 97 |
stdin_flags = fcntl(STDIN_FILENO, F_GETFL, 0); |
if ((stdin_flags = fcntl(STDIN_FILENO, F_GETFL, 0)) == -1) |
| 98 |
fcntl(STDIN_FILENO, F_SETFL, stdin_flags | O_NONBLOCK); |
{ |
| 99 |
|
log_error("fcntl(F_GETFL) error (%d)", errno); |
| 100 |
|
if (close(stdin_epollfd) < 0) |
| 101 |
|
{ |
| 102 |
|
log_error("close(stdin_epollfd) error (%d)", errno); |
| 103 |
|
} |
| 104 |
|
stdin_epollfd = -1; |
| 105 |
|
return -1; |
| 106 |
|
} |
| 107 |
|
if ((fcntl(STDIN_FILENO, F_SETFL, stdin_flags | O_NONBLOCK)) == -1) |
| 108 |
|
{ |
| 109 |
|
log_error("fcntl(F_SETFL) error (%d)", errno); |
| 110 |
|
if (close(stdin_epollfd) < 0) |
| 111 |
|
{ |
| 112 |
|
log_error("close(stdin_epollfd) error (%d)", errno); |
| 113 |
|
} |
| 114 |
|
stdin_epollfd = -1; |
| 115 |
|
return -1; |
| 116 |
|
} |
| 117 |
} |
} |
| 118 |
|
|
| 119 |
if (stdout_epollfd == -1) |
if (stdout_epollfd == -1) |
| 121 |
stdout_epollfd = epoll_create1(0); |
stdout_epollfd = epoll_create1(0); |
| 122 |
if (stdout_epollfd == -1) |
if (stdout_epollfd == -1) |
| 123 |
{ |
{ |
| 124 |
log_error("epoll_create1() error (%d)\n", errno); |
log_error("epoll_create1() error (%d)", errno); |
| 125 |
return -1; |
return -1; |
| 126 |
} |
} |
| 127 |
|
|
| 129 |
ev.data.fd = STDOUT_FILENO; |
ev.data.fd = STDOUT_FILENO; |
| 130 |
if (epoll_ctl(stdout_epollfd, EPOLL_CTL_ADD, STDOUT_FILENO, &ev) == -1) |
if (epoll_ctl(stdout_epollfd, EPOLL_CTL_ADD, STDOUT_FILENO, &ev) == -1) |
| 131 |
{ |
{ |
| 132 |
log_error("epoll_ctl(STDOUT_FILENO) error (%d)\n", errno); |
log_error("epoll_ctl(STDOUT_FILENO) error (%d)", errno); |
| 133 |
if (close(stdout_epollfd) < 0) |
if (close(stdout_epollfd) < 0) |
| 134 |
{ |
{ |
| 135 |
log_error("close(stdout_epollfd) error (%d)\n"); |
log_error("close(stdout_epollfd) error (%d)", errno); |
| 136 |
} |
} |
| 137 |
stdout_epollfd = -1; |
stdout_epollfd = -1; |
| 138 |
return -1; |
return -1; |
| 139 |
} |
} |
| 140 |
|
|
| 141 |
stdout_flags = fcntl(STDOUT_FILENO, F_GETFL, 0); |
if ((stdout_flags = fcntl(STDOUT_FILENO, F_GETFL, 0)) == -1) |
| 142 |
fcntl(STDOUT_FILENO, F_SETFL, stdout_flags | O_NONBLOCK); |
{ |
| 143 |
|
log_error("fcntl(F_GETFL) error (%d)", errno); |
| 144 |
|
if (close(stdout_epollfd) < 0) |
| 145 |
|
{ |
| 146 |
|
log_error("close(stdout_epollfd) error (%d)", errno); |
| 147 |
|
} |
| 148 |
|
stdout_epollfd = -1; |
| 149 |
|
return -1; |
| 150 |
|
} |
| 151 |
|
if ((fcntl(STDOUT_FILENO, F_SETFL, stdout_flags | O_NONBLOCK)) == -1) |
| 152 |
|
{ |
| 153 |
|
log_error("fcntl(F_SETFL) error (%d)", errno); |
| 154 |
|
if (close(stdout_epollfd) < 0) |
| 155 |
|
{ |
| 156 |
|
log_error("close(stdout_epollfd) error (%d)", errno); |
| 157 |
|
} |
| 158 |
|
stdout_epollfd = -1; |
| 159 |
|
return -1; |
| 160 |
|
} |
| 161 |
} |
} |
| 162 |
#else |
#else |
| 163 |
if (stdin_flags == 0) |
if (stdin_flags == 0) |
| 164 |
{ |
{ |
| 165 |
stdin_flags = fcntl(STDIN_FILENO, F_GETFL, 0); |
if ((stdin_flags = fcntl(STDIN_FILENO, F_GETFL, 0)) == -1) |
| 166 |
fcntl(STDIN_FILENO, F_SETFL, stdin_flags | O_NONBLOCK); |
{ |
| 167 |
|
log_error("fcntl(F_GETFL) error (%d)", errno); |
| 168 |
|
return -1; |
| 169 |
|
} |
| 170 |
|
if ((fcntl(STDIN_FILENO, F_SETFL, stdin_flags | O_NONBLOCK)) == -1) |
| 171 |
|
{ |
| 172 |
|
log_error("fcntl(F_SETFL) error (%d)", errno); |
| 173 |
|
return -1; |
| 174 |
|
} |
| 175 |
} |
} |
| 176 |
|
|
| 177 |
if (stdout_flags == 0) |
if (stdout_flags == 0) |
| 178 |
{ |
{ |
| 179 |
stdout_flags = fcntl(STDOUT_FILENO, F_GETFL, 0); |
if ((stdout_flags = fcntl(STDOUT_FILENO, F_GETFL, 0)) == -1) |
| 180 |
fcntl(STDOUT_FILENO, F_SETFL, stdout_flags | O_NONBLOCK); |
{ |
| 181 |
|
log_error("fcntl(F_GETFL) error (%d)", errno); |
| 182 |
|
return -1; |
| 183 |
|
} |
| 184 |
|
if ((fcntl(STDOUT_FILENO, F_SETFL, stdout_flags | O_NONBLOCK)) == -1) |
| 185 |
|
{ |
| 186 |
|
log_error("fcntl(F_SETFL) error (%d)", errno); |
| 187 |
|
return -1; |
| 188 |
|
} |
| 189 |
} |
} |
| 190 |
#endif |
#endif |
| 191 |
|
|
| 202 |
|
|
| 203 |
if (close(stdin_epollfd) < 0) |
if (close(stdin_epollfd) < 0) |
| 204 |
{ |
{ |
| 205 |
log_error("close(stdin_epollfd) error (%d)\n"); |
log_error("close(stdin_epollfd) error (%d)", errno); |
| 206 |
} |
} |
| 207 |
stdin_epollfd = -1; |
stdin_epollfd = -1; |
| 208 |
} |
} |
| 214 |
|
|
| 215 |
if (close(stdout_epollfd) < 0) |
if (close(stdout_epollfd) < 0) |
| 216 |
{ |
{ |
| 217 |
log_error("close(stdout_epollfd) error (%d)\n"); |
log_error("close(stdout_epollfd) error (%d)", errno); |
| 218 |
} |
} |
| 219 |
stdout_epollfd = -1; |
stdout_epollfd = -1; |
| 220 |
} |
} |
| 235 |
|
|
| 236 |
int prints(const char *format, ...) |
int prints(const char *format, ...) |
| 237 |
{ |
{ |
| 238 |
char buf[BUFSIZ]; |
char buf[OUTPUT_BUF_SIZE]; |
| 239 |
va_list args; |
va_list args; |
| 240 |
int ret; |
int ret; |
| 241 |
|
|
| 243 |
ret = vsnprintf(buf, sizeof(buf), format, args); |
ret = vsnprintf(buf, sizeof(buf), format, args); |
| 244 |
va_end(args); |
va_end(args); |
| 245 |
|
|
| 246 |
if (ret > 0) |
if (ret >= 0) |
| 247 |
{ |
{ |
| 248 |
if (stdout_buf_len + ret > BUFSIZ) |
int written = (ret >= sizeof(buf) ? (int)sizeof(buf) - 1 : ret); |
| 249 |
|
|
| 250 |
|
if (stdout_buf_len + written > OUTPUT_BUF_SIZE) |
| 251 |
{ |
{ |
| 252 |
iflush(); |
iflush(); |
| 253 |
} |
} |
| 254 |
|
|
| 255 |
if (stdout_buf_len + ret <= BUFSIZ) |
if (stdout_buf_len + written <= OUTPUT_BUF_SIZE) |
| 256 |
{ |
{ |
| 257 |
memcpy(stdout_buf + stdout_buf_len, buf, (size_t)ret); |
memcpy(stdout_buf + stdout_buf_len, buf, (size_t)written); |
| 258 |
stdout_buf_len += ret; |
stdout_buf_len += written; |
| 259 |
|
ret = written; |
| 260 |
} |
} |
| 261 |
else |
else |
| 262 |
{ |
{ |
| 263 |
errno = EAGAIN; |
errno = EAGAIN; |
| 264 |
ret = (BUFSIZ - stdout_buf_len - ret); |
int need = stdout_buf_len + written - OUTPUT_BUF_SIZE; |
| 265 |
log_error("Output buffer is full, additional %d is required\n", ret); |
log_error("Output buffer is full, additional %d bytes are required", need); |
| 266 |
|
ret = -need; |
| 267 |
} |
} |
| 268 |
} |
} |
| 269 |
|
|
| 272 |
|
|
| 273 |
int outc(char c) |
int outc(char c) |
| 274 |
{ |
{ |
| 275 |
int ret; |
int ret = 0; |
| 276 |
|
|
| 277 |
if (stdout_buf_len + 1 > BUFSIZ) |
if (stdout_buf_len + 1 > OUTPUT_BUF_SIZE) |
| 278 |
{ |
{ |
| 279 |
iflush(); |
iflush(); |
| 280 |
} |
} |
| 281 |
|
|
| 282 |
if (stdout_buf_len + 1 <= BUFSIZ) |
if (stdout_buf_len + 1 <= OUTPUT_BUF_SIZE) |
| 283 |
{ |
{ |
| 284 |
stdout_buf[stdout_buf_len] = c; |
stdout_buf[stdout_buf_len] = c; |
| 285 |
stdout_buf_len++; |
stdout_buf_len++; |
| 302 |
#endif |
#endif |
| 303 |
|
|
| 304 |
int nfds; |
int nfds; |
|
int retry; |
|
| 305 |
int ret = 0; |
int ret = 0; |
| 306 |
|
|
| 307 |
// Retry wait / flush for at most 3 times |
// Retry wait / flush for at most 3 times |
| 308 |
retry = 3; |
for (int retry = 3; retry > 0 && !SYS_server_exit; retry--) |
|
while (retry > 0 && !SYS_server_exit) |
|
| 309 |
{ |
{ |
|
retry--; |
|
|
|
|
| 310 |
#ifdef HAVE_SYS_EPOLL_H |
#ifdef HAVE_SYS_EPOLL_H |
| 311 |
nfds = epoll_wait(stdout_epollfd, events, MAX_EVENTS, 100); // 0.1 second |
nfds = epoll_wait(stdout_epollfd, events, MAX_EVENTS, 100); // 0.1 second |
| 312 |
ret = nfds; |
ret = nfds; |
| 322 |
if (errno != EINTR) |
if (errno != EINTR) |
| 323 |
{ |
{ |
| 324 |
#ifdef HAVE_SYS_EPOLL_H |
#ifdef HAVE_SYS_EPOLL_H |
| 325 |
log_error("epoll_wait() error (%d)\n", errno); |
log_error("epoll_wait() error (%d)", errno); |
| 326 |
#else |
#else |
| 327 |
log_error("poll() error (%d)\n", errno); |
log_error("poll() error (%d)", errno); |
| 328 |
#endif |
#endif |
| 329 |
break; |
break; |
| 330 |
} |
} |
| 338 |
for (int i = 0; i < nfds; i++) |
for (int i = 0; i < nfds; i++) |
| 339 |
{ |
{ |
| 340 |
#ifdef HAVE_SYS_EPOLL_H |
#ifdef HAVE_SYS_EPOLL_H |
| 341 |
if (events[i].data.fd == STDOUT_FILENO) |
if (events[i].data.fd == STDOUT_FILENO && (events[i].events & (EPOLLHUP | EPOLLERR))) |
| 342 |
|
#else |
| 343 |
|
if (pfds[i].fd == STDOUT_FILENO && (pfds[i].revents & (POLLHUP | POLLERR))) |
| 344 |
|
#endif |
| 345 |
|
{ |
| 346 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 347 |
|
log_debug("STDOUT error events (%d)", events[i].events); |
| 348 |
|
#else |
| 349 |
|
log_debug("STDOUT error events (%d)", pfds[i].revents); |
| 350 |
|
#endif |
| 351 |
|
retry = 0; |
| 352 |
|
break; |
| 353 |
|
} |
| 354 |
|
|
| 355 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 356 |
|
if (events[i].data.fd == STDOUT_FILENO && (events[i].events & EPOLLOUT)) |
| 357 |
#else |
#else |
| 358 |
if (pfds[i].fd == STDOUT_FILENO && (pfds[i].revents & POLLOUT)) |
if (pfds[i].fd == STDOUT_FILENO && (pfds[i].revents & POLLOUT)) |
| 359 |
#endif |
#endif |
| 363 |
ret = io_buf_conv(stdout_cd, stdout_buf, &stdout_buf_len, &stdout_buf_offset, stdout_conv, sizeof(stdout_conv), &stdout_conv_len); |
ret = io_buf_conv(stdout_cd, stdout_buf, &stdout_buf_len, &stdout_buf_offset, stdout_conv, sizeof(stdout_conv), &stdout_conv_len); |
| 364 |
if (ret < 0) |
if (ret < 0) |
| 365 |
{ |
{ |
| 366 |
log_error("io_buf_conv(stdout, %d, %d, %d) error\n", stdout_buf_len, stdout_buf_offset, stdout_conv_len); |
log_error("io_buf_conv(stdout, %d, %d, %d) error", stdout_buf_len, stdout_buf_offset, stdout_conv_len); |
| 367 |
stdout_buf_len = stdout_buf_offset; // Discard invalid sequence |
stdout_buf_len = stdout_buf_offset; // Discard invalid sequence |
| 368 |
} |
} |
| 369 |
} |
} |
| 375 |
ret = ssh_channel_write(SSH_channel, stdout_conv + stdout_conv_offset, (uint32_t)(stdout_conv_len - stdout_conv_offset)); |
ret = ssh_channel_write(SSH_channel, stdout_conv + stdout_conv_offset, (uint32_t)(stdout_conv_len - stdout_conv_offset)); |
| 376 |
if (ret == SSH_ERROR) |
if (ret == SSH_ERROR) |
| 377 |
{ |
{ |
| 378 |
log_error("ssh_channel_write() error: %s\n", ssh_get_error(SSH_session)); |
log_debug("ssh_channel_write() error: %s", ssh_get_error(SSH_session)); |
| 379 |
retry = 0; |
retry = 0; |
| 380 |
break; |
break; |
| 381 |
} |
} |
| 396 |
} |
} |
| 397 |
else |
else |
| 398 |
{ |
{ |
| 399 |
#ifdef _DEBUG |
log_debug("write(STDOUT) error (%d)", errno); |
|
log_error("write(STDOUT) error (%d)\n", errno); |
|
|
#endif |
|
| 400 |
retry = 0; |
retry = 0; |
| 401 |
break; |
break; |
| 402 |
} |
} |
| 457 |
{ |
{ |
| 458 |
if (SSH_v2 && ssh_channel_is_closed(SSH_channel)) |
if (SSH_v2 && ssh_channel_is_closed(SSH_channel)) |
| 459 |
{ |
{ |
| 460 |
log_error("SSH channel is closed\n"); |
log_debug("SSH channel is closed"); |
| 461 |
loop = 0; |
loop = 0; |
| 462 |
break; |
break; |
| 463 |
} |
} |
| 479 |
if (errno != EINTR) |
if (errno != EINTR) |
| 480 |
{ |
{ |
| 481 |
#ifdef HAVE_SYS_EPOLL_H |
#ifdef HAVE_SYS_EPOLL_H |
| 482 |
log_error("epoll_wait() error (%d)\n", errno); |
log_error("epoll_wait() error (%d)", errno); |
| 483 |
#else |
#else |
| 484 |
log_error("poll() error (%d)\n", errno); |
log_error("poll() error (%d)", errno); |
| 485 |
#endif |
#endif |
| 486 |
break; |
break; |
| 487 |
} |
} |
| 496 |
for (int i = 0; i < nfds; i++) |
for (int i = 0; i < nfds; i++) |
| 497 |
{ |
{ |
| 498 |
#ifdef HAVE_SYS_EPOLL_H |
#ifdef HAVE_SYS_EPOLL_H |
| 499 |
if (events[i].data.fd == STDIN_FILENO) |
if (events[i].data.fd == STDIN_FILENO && (events[i].events & (EPOLLHUP | EPOLLERR))) |
| 500 |
|
#else |
| 501 |
|
if (pfds[i].fd == STDIN_FILENO && (pfds[i].revents & (POLLHUP | POLLERR))) |
| 502 |
|
#endif |
| 503 |
|
{ |
| 504 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 505 |
|
log_debug("STDIN error events (%d)", events[i].events); |
| 506 |
|
#else |
| 507 |
|
log_debug("STDIN error events (%d)", pfds[i].revents); |
| 508 |
|
#endif |
| 509 |
|
loop = 0; |
| 510 |
|
break; |
| 511 |
|
} |
| 512 |
|
|
| 513 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 514 |
|
if (events[i].data.fd == STDIN_FILENO && (events[i].events & EPOLLIN)) |
| 515 |
#else |
#else |
| 516 |
if (pfds[i].fd == STDIN_FILENO && (pfds[i].revents & POLLIN)) |
if (pfds[i].fd == STDIN_FILENO && (pfds[i].revents & POLLIN)) |
| 517 |
#endif |
#endif |
| 530 |
ret = ssh_channel_read_nonblocking(SSH_channel, stdin_buf + stdin_buf_len, sizeof(stdin_buf) - (uint32_t)stdin_buf_len, 0); |
ret = ssh_channel_read_nonblocking(SSH_channel, stdin_buf + stdin_buf_len, sizeof(stdin_buf) - (uint32_t)stdin_buf_len, 0); |
| 531 |
if (ret == SSH_ERROR) |
if (ret == SSH_ERROR) |
| 532 |
{ |
{ |
| 533 |
log_error("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(SSH_session)); |
log_debug("ssh_channel_read_nonblocking() error: %s", ssh_get_error(SSH_session)); |
| 534 |
loop = 0; |
loop = 0; |
| 535 |
break; |
break; |
| 536 |
} |
} |
| 567 |
} |
} |
| 568 |
else |
else |
| 569 |
{ |
{ |
| 570 |
#ifdef _DEBUG |
log_debug("read(STDIN) error (%d)", errno); |
|
log_error("read(STDIN) error (%d)\n", errno); |
|
|
#endif |
|
| 571 |
loop = 0; |
loop = 0; |
| 572 |
break; |
break; |
| 573 |
} |
} |
| 590 |
#ifdef _DEBUG |
#ifdef _DEBUG |
| 591 |
for (int j = stdin_buf_offset; j < stdin_buf_len; j++) |
for (int j = stdin_buf_offset; j < stdin_buf_len; j++) |
| 592 |
{ |
{ |
| 593 |
log_error("Debug input: <--[%u]\n", (stdin_buf[j] + 256) % 256); |
log_debug("input: <--[%u]", (stdin_buf[j] + 256) % 256); |
| 594 |
} |
} |
| 595 |
#endif |
#endif |
| 596 |
} |
} |
| 600 |
ret = io_buf_conv(stdin_cd, stdin_buf, &stdin_buf_len, &stdin_buf_offset, stdin_conv, sizeof(stdin_conv), &stdin_conv_len); |
ret = io_buf_conv(stdin_cd, stdin_buf, &stdin_buf_len, &stdin_buf_offset, stdin_conv, sizeof(stdin_conv), &stdin_conv_len); |
| 601 |
if (ret < 0) |
if (ret < 0) |
| 602 |
{ |
{ |
| 603 |
log_error("io_buf_conv(stdin, %d, %d, %d) error\n", stdin_buf_len, stdin_buf_offset, stdin_conv_len); |
log_error("io_buf_conv(stdin, %d, %d, %d) error", stdin_buf_len, stdin_buf_offset, stdin_conv_len); |
| 604 |
stdin_buf_len = stdin_buf_offset; // Discard invalid sequence |
stdin_buf_len = stdin_buf_offset; // Discard invalid sequence |
| 605 |
} |
} |
| 606 |
|
|
| 608 |
#ifdef _DEBUG |
#ifdef _DEBUG |
| 609 |
for (int j = stdin_conv_offset; j < stdin_conv_len; j++) |
for (int j = stdin_conv_offset; j < stdin_conv_len; j++) |
| 610 |
{ |
{ |
| 611 |
log_error("Debug input_conv: <--[%u]\n", (stdin_conv[j] + 256) % 256); |
log_debug("input_conv: <--[%u]", (stdin_conv[j] + 256) % 256); |
| 612 |
} |
} |
| 613 |
#endif |
#endif |
| 614 |
} |
} |
| 1074 |
#ifdef _DEBUG |
#ifdef _DEBUG |
| 1075 |
if (out != KEY_TIMEOUT && out != KEY_NULL) |
if (out != KEY_TIMEOUT && out != KEY_NULL) |
| 1076 |
{ |
{ |
| 1077 |
log_error("Debug: -->[0x %x]\n", out); |
log_debug("output: -->[0x %x]", out); |
| 1078 |
} |
} |
| 1079 |
#endif |
#endif |
| 1080 |
|
|
| 1112 |
int ret; |
int ret; |
| 1113 |
int in_control = 0; |
int in_control = 0; |
| 1114 |
size_t i = 0; |
size_t i = 0; |
| 1115 |
|
int skip_current = 0; |
| 1116 |
|
|
| 1117 |
if (cd == NULL || p_buf == NULL || p_buf_len == NULL || p_buf_offset == NULL || p_conv == NULL || p_conv_len == NULL) |
if (cd == (iconv_t)(-1) || p_buf == NULL || p_buf_len == NULL || p_buf_offset == NULL || p_conv == NULL || p_conv_len == NULL) |
| 1118 |
{ |
{ |
| 1119 |
log_error("NULL pointer error\n"); |
log_error("NULL pointer error"); |
| 1120 |
return -1; |
return -1; |
| 1121 |
} |
} |
| 1122 |
|
|
| 1136 |
} |
} |
| 1137 |
} |
} |
| 1138 |
|
|
| 1139 |
if (in_control) |
if (in_control || skip_current) |
| 1140 |
{ |
{ |
| 1141 |
|
skip_current = 0; |
| 1142 |
|
|
| 1143 |
if (out_bytes <= 0) |
if (out_bytes <= 0) |
| 1144 |
{ |
{ |
| 1145 |
log_error("No enough free space in p_conv, conv_len=%d, conv_size=%d\n", *p_conv_len, conv_size); |
log_error("No enough free space in p_conv, conv_len=%d, conv_size=%d", *p_conv_len, conv_size); |
| 1146 |
return -2; |
return -2; |
| 1147 |
} |
} |
| 1148 |
|
|
| 1153 |
out_bytes--; |
out_bytes--; |
| 1154 |
|
|
| 1155 |
(*p_buf_offset)++; |
(*p_buf_offset)++; |
| 1156 |
*p_conv_len = (int)(conv_size - out_bytes); |
(*p_conv_len)++; |
| 1157 |
|
|
| 1158 |
i++; |
i++; |
| 1159 |
if (i >= 2) |
if (i >= 2) |
| 1168 |
{ |
{ |
| 1169 |
if (errno == EINVAL) // Incomplete |
if (errno == EINVAL) // Incomplete |
| 1170 |
{ |
{ |
| 1171 |
#ifdef _DEBUG |
log_debug("iconv(inbytes=%zu, outbytes=%zu) error: EINVAL, in_buf[0]=%d", |
| 1172 |
log_error("iconv(inbytes=%d, outbytes=%d) error: EINVAL, in_buf[0]=%d\n", in_bytes, out_bytes, in_buf[0]); |
in_bytes, out_bytes, (unsigned char)in_buf[0]); |
|
#endif |
|
| 1173 |
if (p_buf != in_buf) |
if (p_buf != in_buf) |
| 1174 |
{ |
{ |
| 1175 |
*p_buf_len = (int)(p_buf + *p_buf_len - in_buf); |
*p_buf_len -= (int)(in_buf - p_buf); |
| 1176 |
*p_buf_offset = 0; |
*p_buf_offset = 0; |
| 1177 |
*p_conv_len = (int)(conv_size - out_bytes); |
*p_conv_len = (int)(conv_size - out_bytes); |
| 1178 |
memmove(p_buf, in_buf, (size_t)(*p_buf_len)); |
memmove(p_buf, in_buf, (size_t)(*p_buf_len)); |
| 1182 |
} |
} |
| 1183 |
else if (errno == E2BIG) |
else if (errno == E2BIG) |
| 1184 |
{ |
{ |
| 1185 |
log_error("iconv(inbytes=%d, outbytes=%d) error: E2BIG\n", in_bytes, out_bytes); |
log_error("iconv(inbytes=%zu, outbytes=%zu) error: E2BIG", in_bytes, out_bytes); |
| 1186 |
return -1; |
return -1; |
| 1187 |
} |
} |
| 1188 |
else if (errno == EILSEQ) |
else if (errno == EILSEQ) |
| 1189 |
{ |
{ |
| 1190 |
if (in_bytes > out_bytes || out_bytes <= 0) |
if (in_bytes > out_bytes || out_bytes <= 0) |
| 1191 |
{ |
{ |
| 1192 |
log_error("iconv(inbytes=%d, outbytes=%d) error: EILSEQ and E2BIG\n", in_bytes, out_bytes); |
log_error("iconv(inbytes=%zu, outbytes=%zu) error: EILSEQ", in_bytes, out_bytes); |
| 1193 |
return -2; |
return -2; |
| 1194 |
} |
} |
| 1195 |
|
|
| 1197 |
if (in_bytes == 0) |
if (in_bytes == 0) |
| 1198 |
{ |
{ |
| 1199 |
in_bytes = (size_t)(*p_buf_len - *p_buf_offset); |
in_bytes = (size_t)(*p_buf_len - *p_buf_offset); |
| 1200 |
|
log_debug("Reset in_bytes from 0 to %zu", in_bytes); |
| 1201 |
} |
} |
| 1202 |
|
|
| 1203 |
*out_buf = *in_buf; |
log_debug("iconv(in_bytes=%zu, out_bytes=%zu) error: EILSEQ, in_buf[0]=%d", |
| 1204 |
in_buf++; |
in_bytes, out_bytes, (unsigned char)in_buf[0]); |
| 1205 |
out_buf++; |
skip_current = 1; |
| 1206 |
in_bytes--; |
} |
| 1207 |
out_bytes--; |
else // something strange |
| 1208 |
|
{ |
| 1209 |
continue; |
log_debug("iconv(in_bytes=%zu, out_bytes=%zu) error: %d, in_buf[0]=%d", |
| 1210 |
|
in_bytes, out_bytes, errno, (unsigned char)in_buf[0]); |
| 1211 |
|
*p_buf_offset = (int)(in_buf - p_buf); |
| 1212 |
|
*p_conv_len = (int)(conv_size - out_bytes); |
| 1213 |
|
skip_current = 1; |
| 1214 |
} |
} |
| 1215 |
} |
} |
| 1216 |
else |
else |
| 1217 |
{ |
{ |
| 1218 |
*p_buf_len = 0; |
*p_buf_offset = (int)(in_buf - p_buf); |
|
*p_buf_offset = 0; |
|
| 1219 |
*p_conv_len = (int)(conv_size - out_bytes); |
*p_conv_len = (int)(conv_size - out_bytes); |
|
|
|
|
break; |
|
| 1220 |
} |
} |
| 1221 |
} |
} |
| 1222 |
|
|
| 1223 |
|
if (*p_buf_offset >= *p_buf_len) |
| 1224 |
|
{ |
| 1225 |
|
*p_buf_len = 0; |
| 1226 |
|
*p_buf_offset = 0; |
| 1227 |
|
} |
| 1228 |
|
|
| 1229 |
return 0; |
return 0; |
| 1230 |
} |
} |
| 1231 |
|
|
| 1235 |
|
|
| 1236 |
if (charset == NULL) |
if (charset == NULL) |
| 1237 |
{ |
{ |
| 1238 |
log_error("NULL pointer error\n"); |
log_error("NULL pointer error"); |
| 1239 |
return -1; |
return -1; |
| 1240 |
} |
} |
| 1241 |
|
|
| 1249 |
stdin_cd = iconv_open(tocode, stdio_charset); |
stdin_cd = iconv_open(tocode, stdio_charset); |
| 1250 |
if (stdin_cd == (iconv_t)(-1)) |
if (stdin_cd == (iconv_t)(-1)) |
| 1251 |
{ |
{ |
| 1252 |
log_error("iconv_open(%s->%s) error: %d\n", stdio_charset, tocode, errno); |
log_error("iconv_open(%s->%s) error: %d", stdio_charset, tocode, errno); |
| 1253 |
return -2; |
return -2; |
| 1254 |
} |
} |
| 1255 |
|
|
| 1258 |
stdout_cd = iconv_open(tocode, BBS_default_charset); |
stdout_cd = iconv_open(tocode, BBS_default_charset); |
| 1259 |
if (stdout_cd == (iconv_t)(-1)) |
if (stdout_cd == (iconv_t)(-1)) |
| 1260 |
{ |
{ |
| 1261 |
log_error("iconv_open(%s->%s) error: %d\n", BBS_default_charset, tocode, errno); |
log_error("iconv_open(%s->%s) error: %d", BBS_default_charset, tocode, errno); |
| 1262 |
iconv_close(stdin_cd); |
iconv_close(stdin_cd); |
| 1263 |
|
stdin_cd = (iconv_t)(-1); |
| 1264 |
return -2; |
return -2; |
| 1265 |
} |
} |
| 1266 |
|
|
| 1269 |
|
|
| 1270 |
int io_conv_cleanup(void) |
int io_conv_cleanup(void) |
| 1271 |
{ |
{ |
| 1272 |
if (stdin_cd != NULL) |
if (stdin_cd != (iconv_t)(-1)) |
| 1273 |
{ |
{ |
| 1274 |
iconv_close(stdin_cd); |
iconv_close(stdin_cd); |
| 1275 |
stdin_cd = NULL; |
stdin_cd = (iconv_t)(-1); |
| 1276 |
} |
} |
| 1277 |
if (stdout_cd != NULL) |
if (stdout_cd != (iconv_t)(-1)) |
| 1278 |
{ |
{ |
| 1279 |
iconv_close(stdout_cd); |
iconv_close(stdout_cd); |
| 1280 |
stdout_cd = NULL; |
stdout_cd = (iconv_t)(-1); |
| 1281 |
} |
} |
| 1282 |
|
|
| 1283 |
return 0; |
return 0; |