Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

base 64 encoding

dennisparkerdennisparker Posts: 478Member
Our team developed the following base 64 encoding routine to encode bitmap images to be sent over the internet. When the encoded file reaches the email destination, the header is mangled and the colors of the bitmap are altered.

We are trying to track down whether it is the encoding code that has a flaw, or whether the TCP/IP code is mangling the file. Does anyone see any problems with the following code?

[code]
#include "WrNet.h"

signed long b64chunk(unsigned long ibuf, unsigned long obuf, signed char number){
unsigned long temp;
char* in = (char *)ibuf;
char* out = (char *)obuf;
char* base64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789/+";

if (number >= 1)
temp = in[0] * 65536;
if (number >= 2)
temp = temp + in[1] * 256;
if (number == 3)
temp = temp + in[2];

out[3] = base64[((temp % 64))];
out[2] = base64[((temp % 4096) / 64)];
out[1] = base64[((temp % 262144) / 4096)];
out[0] = base64[((temp % 16777216) / 262144)];

if(number < 3)
out[3] = '=';
if(number < 2)
out[2] = '=';

return 0;
}

signed long b64line(unsigned long ibuf, unsigned long obuf, signed char number){
unsigned int offset = 0;
unsigned int outchars = 0;
char* out = (char *)obuf;

if (number > 57)
number = 57;

while (number > offset) {
if ((number - offset) >= 3) {
b64chunk(ibuf + offset, obuf + outchars, 3);
offset = offset + 3;
outchars = outchars + 4;
} else {
b64chunk(ibuf + offset, obuf + outchars, (number - offset));
offset = number;
outchars = outchars + 4;
}
}
out[outchars] = '
';
out[outchars+1] = '';
return 0;
}[/code]


Sign In or Register to comment.