设计模式(6)-适配器模式(Apater) (二)

2014-11-24 09:58:07 · 作者: · 浏览: 1
lse
{
i = 0;
}

/* Buffer remaining input */
memcpy(&_buffer[index], &input[i], length - i);
}

/*
MD5 finalization. Ends an MD5 message-_digest operation, writing the
the message _digest and zeroizing the context.
*/
void MD5::final()
{
byte bits[8];
uint32 oldState[4];
uint32 oldCount[2];
uint32 index, padLen;

/* Save current state and count. */
memcpy(oldState, _state, 16);
memcpy(oldCount, _count, 8);

/* Save number of bits */
encode(_count, bits, 8);

/* Pad out to 56 mod 64. */
index = (uint32)((_count[0] >> 3) & 0x3f);
padLen = (index < 56) (56 - index) : (120 - index);
update(PADDING, padLen);

/* Append length (before padding) */
update(bits, 8);

/* Store state in digest */
encode(_state, _digest, 16);

/* Restore current state and count. */
memcpy(_state, oldState, 16);
memcpy(_count, oldCount, 8);
}

void MD5::update(const byte *input, size_t length)
{
uint32 i, index, partLen;

_finished = false;

/* Compute number of bytes mod 64 */
index = (uint32)((_count[0] >> 3) & 0x3f);//0x3f = 63

/* update number of bits */
if ((_count[0] += ((uint32)length << 3)) < ((uint32)length << 3))
{
++_count[1];

}
_count[1] += ((uint32)length >> 29);

//qDebug()<<_count[0]<<_count[1];

partLen = 64 - index;

/* transform as many times as possible. */
if (length >= partLen)
{

memcpy(&_buffer[index], input, partLen);
transform(_buffer);

for (i = partLen; i + 63 < length; i += 64)
{
transform(&input[i]);
}
index = 0;

}
else
{
i = 0;
}

/* Buffer remaining input */
memcpy(&_buffer[index], &input[i], length - i);
}

/*
MD5 finalization. Ends an MD5 message-_digest operation, writing the
the message _digest and zeroizing the context.
*/
void MD5::final()
{
byte bits[8];
uint32 oldState[4];
uint32 oldCount[2];
uint32 index, padLen;

/* Save current state and count. */
memcpy(oldState, _state, 16);
memcpy(oldCount, _count, 8);

/* Save number of bits */
encode(_count, bits, 8);

/* Pad out to 56 mod 64. */
index = (uint32)((_count[0] >> 3) & 0x3f);
padLen = (index < 56) (56 - index) : (120 - index);
update(PADDING, padLen);

/* Append length (before padding) */
update(bits, 8);

/* Store state in digest */
encode(_state, _digest, 16);

/* Restore current state and count. */
memcpy(_state, oldState, 16);
memcpy(_count, oldCount, 8);
}
其余接口(公有的)采用适配器模式就可以了。

[html] view plaincopyprint void MD5::update(const void *input, size_t length)
{
update((const byte*)input, length);
}

void MD5::update(const QString &str)
{
update((const byte*)str.toLatin1().data(), str.length());
}

void MD5::update(ifstream &in)
{
if (!in)
{
return;
}

std::streamsize length;
char buffer[BUFFER_SIZE];
while (!in.eof())
{
in.read(buffer, BUFFER_SIZE);
length = in.gcount();
if (length > 0)
{
update(buffer, length);
}
}
in.close();
}

void MD5::update(const void *input, size_t length)
{
update((const byte*)input, length);
}

void MD5::update(const QString &str)
{
update((const byte*)str.toLatin1().data(), str.length());
}

void MD5::update(ifstream &in)
{
if (!in)
{
return;
}

std::streamsize length;
char buffer[BUFFER_SIZE];
while (!in.eof())
{
in.read(buffer, BUFFER_SIZE);