C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28629
Number of posts: 94611

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
base 64 encoding Posted by dennisparker on 2 Feb 2006 at 2:25 PM
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?

#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] = '\n';
  out[outchars+1] = '\0';
  return 0;
}






 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.