From ea373db68cd05be2afbd10fdb3cc76f7a1d44527 Mon Sep 17 00:00:00 2001 From: weidai Date: Thu, 16 Jan 2003 00:47:48 +0000 Subject: fix bug to allow base32 coding git-svn-id: svn://svn.code.sf.net/p/cryptopp/code/trunk/c5@26 57ff6487-cd31-0410-9ec3-f628ee90f5f0 --- basecode.cpp | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) (limited to 'basecode.cpp') diff --git a/basecode.cpp b/basecode.cpp index 6a0bfad..7418a85 100644 --- a/basecode.cpp +++ b/basecode.cpp @@ -41,17 +41,32 @@ unsigned int BaseN_Encoder::Put2(const byte *begin, unsigned int length, int mes if (m_bytePos == 0) memset(m_outBuf, 0, m_outputBlockSize); - m_outBuf[m_bytePos] |= begin[m_inputPosition] >> (8-m_bitsPerChar+m_bitPos); - m_outBuf[m_bytePos+1] |= ((begin[m_inputPosition] << (m_bitsPerChar-m_bitPos)) & 0xff) >> (8-m_bitsPerChar); - ++m_inputPosition; - - m_bitPos += 8; - while (m_bitPos >= m_bitsPerChar) { - m_bitPos -= m_bitsPerChar; - ++m_bytePos; + unsigned int b = begin[m_inputPosition++], bitsLeftInSource = 8; + while (true) + { + assert(m_bitPos < m_bitsPerChar); + unsigned int bitsLeftInTarget = m_bitsPerChar-m_bitPos; + m_outBuf[m_bytePos] |= b >> (8-bitsLeftInTarget); + if (bitsLeftInSource >= bitsLeftInTarget) + { + m_bitPos = 0; + ++m_bytePos; + bitsLeftInSource -= bitsLeftInTarget; + if (bitsLeftInSource == 0) + break; + b <<= bitsLeftInTarget; + b &= 0xff; + } + else + { + m_bitPos += bitsLeftInSource; + break; + } + } } + assert(m_bytePos <= m_outputBlockSize); if (m_bytePos == m_outputBlockSize) { int i; -- cgit v1.2.1