| 1485 | } |
| 1486 | |
| 1487 | static _Noreturn void |
| 1488 | do_encode (FILE *in, char const *infile, FILE *out, idx_t wrap_column) |
| 1489 | { |
| 1490 | idx_t current_column = 0; |
| 1491 | char *inbuf, *outbuf; |
| 1492 | idx_t sum; |
| 1493 | |
| 1494 | inbuf = xmalloc (ENC_BLOCKSIZE); |
| 1495 | outbuf = xmalloc (BASE_LENGTH (ENC_BLOCKSIZE)); |
| 1496 | |
| 1497 | #if BASE_TYPE == 42 |
| 1498 | /* Initialize encoding context if needed (for base58) */ |
| 1499 | struct base_encode_context encode_ctx; |
| 1500 | bool use_ctx = (base_encode_ctx_init != NULL); |
| 1501 | if (use_ctx) |
| 1502 | base_encode_ctx_init (&encode_ctx); |
| 1503 | #endif |
| 1504 | |
| 1505 | do |
| 1506 | { |
| 1507 | idx_t n; |
| 1508 | |
| 1509 | sum = 0; |
| 1510 | do |
| 1511 | { |
| 1512 | n = fread (inbuf + sum, 1, ENC_BLOCKSIZE - sum, in); |
| 1513 | sum += n; |
| 1514 | } |
| 1515 | while (!feof (in) && !ferror (in) && sum < ENC_BLOCKSIZE); |
| 1516 | |
| 1517 | if (sum > 0) |
| 1518 | { |
| 1519 | #if BASE_TYPE == 42 |
| 1520 | if (use_ctx) |
| 1521 | { |
| 1522 | idx_t outlen = 0; |
| 1523 | base_encode_ctx (&encode_ctx, inbuf, sum, outbuf, &outlen); |
| 1524 | |
| 1525 | wrap_write (outbuf, outlen, wrap_column, ¤t_column, out); |
| 1526 | } |
| 1527 | else |
| 1528 | #endif |
| 1529 | { |
| 1530 | /* Process input one block at a time. Note that ENC_BLOCKSIZE |
| 1531 | is sized so that no pad chars will appear in output. */ |
| 1532 | base_encode (inbuf, sum, outbuf, BASE_LENGTH (sum)); |
| 1533 | |
| 1534 | wrap_write (outbuf, BASE_LENGTH (sum), wrap_column, |
| 1535 | ¤t_column, out); |
| 1536 | } |
| 1537 | } |
| 1538 | } |
| 1539 | while (!feof (in) && !ferror (in) && sum == ENC_BLOCKSIZE); |
| 1540 | |
| 1541 | #if BASE_TYPE == 42 |
| 1542 | if (use_ctx && base_encode_ctx_finalize) |
| 1543 | { |
| 1544 | idx_t outlen = BASE_LENGTH (ENC_BLOCKSIZE); |
no test coverage detected