monotone

monotone Mtn Source Tree

Root/cryptopp/basecode.cpp

1// basecode.cpp - written and placed in the public domain by Wei Dai
2
3#include "pch.h"
4#include "basecode.h"
5#include "fltrimpl.h"
6#include <ctype.h>
7
8NAMESPACE_BEGIN(CryptoPP)
9
10void BaseN_Encoder::IsolatedInitialize(const NameValuePairs &parameters)
11{
12parameters.GetRequiredParameter("BaseN_Encoder", "EncodingLookupArray", m_alphabet);
13
14parameters.GetRequiredIntParameter("BaseN_Encoder", "Log2Base", m_bitsPerChar);
15if (m_bitsPerChar <= 0 || m_bitsPerChar >= 8)
16throw InvalidArgument("BaseN_Encoder: Log2Base must be between 1 and 7 inclusive");
17
18byte padding;
19bool pad;
20if (parameters.GetValue("PaddingByte", padding))
21pad = parameters.GetValueWithDefault("Pad", true);
22else
23pad = false;
24m_padding = pad ? padding : -1;
25
26m_bytePos = m_bitPos = 0;
27
28int i = 8;
29while (i%m_bitsPerChar != 0)
30i += 8;
31m_outputBlockSize = i/m_bitsPerChar;
32
33m_outBuf.New(m_outputBlockSize);
34}
35
36unsigned int BaseN_Encoder::Put2(const byte *begin, unsigned int length, int messageEnd, bool blocking)
37{
38FILTER_BEGIN;
39while (m_inputPosition < length)
40{
41if (m_bytePos == 0)
42memset(m_outBuf, 0, m_outputBlockSize);
43
44{
45unsigned int b = begin[m_inputPosition++], bitsLeftInSource = 8;
46while (true)
47{
48assert(m_bitPos < m_bitsPerChar);
49unsigned int bitsLeftInTarget = m_bitsPerChar-m_bitPos;
50m_outBuf[m_bytePos] |= b >> (8-bitsLeftInTarget);
51if (bitsLeftInSource >= bitsLeftInTarget)
52{
53m_bitPos = 0;
54++m_bytePos;
55bitsLeftInSource -= bitsLeftInTarget;
56if (bitsLeftInSource == 0)
57break;
58b <<= bitsLeftInTarget;
59b &= 0xff;
60}
61else
62{
63m_bitPos += bitsLeftInSource;
64break;
65}
66}
67}
68
69assert(m_bytePos <= m_outputBlockSize);
70if (m_bytePos == m_outputBlockSize)
71{
72int i;
73for (i=0; i<m_bytePos; i++)
74{
75assert(m_outBuf[i] < (1 << m_bitsPerChar));
76m_outBuf[i] = m_alphabet[m_outBuf[i]];
77}
78FILTER_OUTPUT(1, m_outBuf, m_outputBlockSize, 0);
79
80m_bytePos = m_bitPos = 0;
81}
82}
83if (messageEnd)
84{
85if (m_bitPos > 0)
86++m_bytePos;
87
88int i;
89for (i=0; i<m_bytePos; i++)
90m_outBuf[i] = m_alphabet[m_outBuf[i]];
91
92if (m_padding != -1 && m_bytePos > 0)
93{
94memset(m_outBuf+m_bytePos, m_padding, m_outputBlockSize-m_bytePos);
95m_bytePos = m_outputBlockSize;
96}
97FILTER_OUTPUT(2, m_outBuf, m_bytePos, messageEnd);
98m_bytePos = m_bitPos = 0;
99}
100FILTER_END_NO_MESSAGE_END;
101}
102
103void BaseN_Decoder::IsolatedInitialize(const NameValuePairs &parameters)
104{
105parameters.GetRequiredParameter("BaseN_Decoder", "DecodingLookupArray", m_lookup);
106
107parameters.GetRequiredIntParameter("BaseN_Decoder", "Log2Base", m_bitsPerChar);
108if (m_bitsPerChar <= 0 || m_bitsPerChar >= 8)
109throw InvalidArgument("BaseN_Decoder: Log2Base must be between 1 and 7 inclusive");
110
111m_bytePos = m_bitPos = 0;
112
113int i = m_bitsPerChar;
114while (i%8 != 0)
115i += m_bitsPerChar;
116m_outputBlockSize = i/8;
117
118m_outBuf.New(m_outputBlockSize);
119}
120
121unsigned int BaseN_Decoder::Put2(const byte *begin, unsigned int length, int messageEnd, bool blocking)
122{
123FILTER_BEGIN;
124while (m_inputPosition < length)
125{
126unsigned int value;
127value = m_lookup[begin[m_inputPosition++]];
128if (value >= 256)
129continue;
130
131if (m_bytePos == 0 && m_bitPos == 0)
132memset(m_outBuf, 0, m_outputBlockSize);
133
134{
135int newBitPos = m_bitPos + m_bitsPerChar;
136if (newBitPos <= 8)
137m_outBuf[m_bytePos] |= value << (8-newBitPos);
138else
139{
140m_outBuf[m_bytePos] |= value >> (newBitPos-8);
141m_outBuf[m_bytePos+1] |= value << (16-newBitPos);
142}
143
144m_bitPos = newBitPos;
145while (m_bitPos >= 8)
146{
147m_bitPos -= 8;
148++m_bytePos;
149}
150}
151
152if (m_bytePos == m_outputBlockSize)
153{
154FILTER_OUTPUT(1, m_outBuf, m_outputBlockSize, 0);
155m_bytePos = m_bitPos = 0;
156}
157}
158if (messageEnd)
159{
160FILTER_OUTPUT(2, m_outBuf, m_bytePos, messageEnd);
161m_bytePos = m_bitPos = 0;
162}
163FILTER_END_NO_MESSAGE_END;
164}
165
166void BaseN_Decoder::InitializeDecodingLookupArray(int *lookup, const byte *alphabet, unsigned int base, bool caseInsensitive)
167{
168std::fill(lookup, lookup+256, -1);
169
170for (unsigned int i=0; i<base; i++)
171{
172if (caseInsensitive && isalpha(alphabet[i]))
173{
174assert(lookup[toupper(alphabet[i])] == -1);
175lookup[toupper(alphabet[i])] = i;
176assert(lookup[tolower(alphabet[i])] == -1);
177lookup[tolower(alphabet[i])] = i;
178}
179else
180{
181assert(lookup[alphabet[i]] == -1);
182lookup[alphabet[i]] = i;
183}
184}
185}
186
187void Grouper::IsolatedInitialize(const NameValuePairs &parameters)
188{
189m_groupSize = parameters.GetIntValueWithDefault("GroupSize", 0);
190ConstByteArrayParameter separator, terminator;
191if (m_groupSize)
192parameters.GetRequiredParameter("Grouper", "Separator", separator);
193parameters.GetValue("Terminator", terminator);
194
195m_separator.Assign(separator.begin(), separator.size());
196m_terminator.Assign(terminator.begin(), terminator.size());
197m_counter = 0;
198}
199
200unsigned int Grouper::Put2(const byte *begin, unsigned int length, int messageEnd, bool blocking)
201{
202FILTER_BEGIN;
203if (m_groupSize)
204{
205while (m_inputPosition < length)
206{
207if (m_counter == m_groupSize)
208{
209FILTER_OUTPUT(1, m_separator, m_separator.size(), 0);
210m_counter = 0;
211}
212
213unsigned int len;
214FILTER_OUTPUT2(2, len = STDMIN(length-m_inputPosition, m_groupSize-m_counter),
215begin+m_inputPosition, len, 0);
216m_inputPosition += len;
217m_counter += len;
218}
219}
220else
221FILTER_OUTPUT(3, begin, length, 0);
222
223if (messageEnd)
224FILTER_OUTPUT(4, m_terminator, m_terminator.size(), messageEnd);
225FILTER_END_NO_MESSAGE_END
226}
227
228NAMESPACE_END

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status