* xmlOutputBufferWriteBase64: * @out: the xmlOutputBufferPtr * @data: binary data * @len: the number of bytes to encode * * Write base64 encoded data to an xmlOutputBuffer. * Adapted from John Walker's base64.c (http://www.fourmilab.ch/). * * Returns the bytes written (may be 0 because of buffering) or -1 in case of error */
| 1531 | * Returns the bytes written (may be 0 because of buffering) or -1 in case of error |
| 1532 | */ |
| 1533 | static int |
| 1534 | xmlOutputBufferWriteBase64(xmlOutputBufferPtr out, int len, |
| 1535 | const unsigned char *data) |
| 1536 | { |
| 1537 | static const unsigned char dtable[64] = |
| 1538 | {'A','B','C','D','E','F','G','H','I','J','K','L','M', |
| 1539 | 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z', |
| 1540 | 'a','b','c','d','e','f','g','h','i','j','k','l','m', |
| 1541 | 'n','o','p','q','r','s','t','u','v','w','x','y','z', |
| 1542 | '0','1','2','3','4','5','6','7','8','9','+','/'}; |
| 1543 | |
| 1544 | int i; |
| 1545 | int linelen; |
| 1546 | int count; |
| 1547 | int sum; |
| 1548 | |
| 1549 | if ((out == NULL) || (len < 0) || (data == NULL)) |
| 1550 | return(-1); |
| 1551 | |
| 1552 | linelen = 0; |
| 1553 | sum = 0; |
| 1554 | |
| 1555 | i = 0; |
| 1556 | while (1) { |
| 1557 | unsigned char igroup[3]; |
| 1558 | unsigned char ogroup[4]; |
| 1559 | int c; |
| 1560 | int n; |
| 1561 | |
| 1562 | igroup[0] = igroup[1] = igroup[2] = 0; |
| 1563 | for (n = 0; n < 3 && i < len; n++, i++) { |
| 1564 | c = data[i]; |
| 1565 | igroup[n] = (unsigned char) c; |
| 1566 | } |
| 1567 | |
| 1568 | if (n > 0) { |
| 1569 | ogroup[0] = dtable[igroup[0] >> 2]; |
| 1570 | ogroup[1] = dtable[((igroup[0] & 3) << 4) | (igroup[1] >> 4)]; |
| 1571 | ogroup[2] = |
| 1572 | dtable[((igroup[1] & 0xF) << 2) | (igroup[2] >> 6)]; |
| 1573 | ogroup[3] = dtable[igroup[2] & 0x3F]; |
| 1574 | |
| 1575 | if (n < 3) { |
| 1576 | ogroup[3] = '='; |
| 1577 | if (n < 2) { |
| 1578 | ogroup[2] = '='; |
| 1579 | } |
| 1580 | } |
| 1581 | |
| 1582 | if (linelen >= B64LINELEN) { |
| 1583 | count = xmlOutputBufferWrite(out, 2, B64CRLF); |
| 1584 | if (count == -1) |
| 1585 | return -1; |
| 1586 | sum += count; |
| 1587 | linelen = 0; |
| 1588 | } |
| 1589 | count = xmlOutputBufferWrite(out, 4, (const char *) ogroup); |
| 1590 | if (count == -1) |
no test coverage detected