Title: FW: Problem with BIO_f_base64


 -----Original Message-----
From:   liou, leo 
Sent:   Thursday, June 10, 2004 4:37 PM
To:     '[EMAIL PROTECTED]'
Subject:        Problem with BIO_f_base64

Guys,
I have written the following simple test program for testing our BASE64 on RH9.0.
Well, the BIO_read in decodeB64() keeps returning 0 in the number of bytes read.
I went over it again and again.  For the life of me, I just cannot see what I missed, where I screwed up.
I need another pair of eyes to point out my problem. Thanks in advance.

#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <openssl/evp.h>
#include <openssl/bio.h>

void
dumpStringX(char* s, int n) {
  int i;
  for (i = 0; i < n; i++) {
    printf("[%i]%c", s[i], s[i]);
  }
  printf("\n");
}

char*
encodeB64(char *bin, int len)
{
  int total, written;
  BIO *b64, *mem;
  char *bfr, *out;
  unsigned long err;

  printf("----- attempting B64 encoding(%d)\n", len);
  dumpStringX(bin, len);

  mem = BIO_new(BIO_s_mem());
  b64 = BIO_new(BIO_f_base64());

  /* Assemble the BIO chain ->b64->mem */
  BIO_push(b64, mem);

  /* write loop */
  for (total = 0; total < len; total += written) {
    if ((written = BIO_write(b64, bin + total, len - total)) <= 0) {
      if (BIO_should_retry(b64)) {
        written = 0;
        continue;
      }
      err = ERR_get_error();
      printf("problem with BIO_write error(%d)\n", err);
      break;
    }
  }
  BIO_flush(b64);

  BIO_get_mem_data(mem, &bfr);

  out = malloc(len+1);
  memset(out, 0, len+1);
  memcpy(out, bfr, len);

  printf("B64encoded(%X)(%s)\n", out, out);

  BIO_free_all(b64);

  return out;
}

char*
decodeB64(char *bin, int len)
{
  int total, nread, written;
  BIO *b64, *mem;
  char *bfr;
  unsigned long err;
  char *q;

  printf("----- attempting B64 decoding(%d)(%s)\n", len, bin);

  bfr = malloc(len+1);
  memset(bfr, 0, len+1);

  mem = BIO_new_mem_buf(bin, len);
  b64 = BIO_new(BIO_f_base64());

  /* Assemble the BIO chain <-b64<-mem */
  BIO_push(b64, mem);

  /* check BIO content */
  total = BIO_ctrl_pending(b64);
  BIO_get_mem_data(b64, &q);
  printf("verify_b64(%d)(%s)\n", total, q);
 
  /* read loop of the BIO chain */
  for (total = 0; total < len; total += nread) {
    if ( (nread = BIO_read(b64, bfr + total, len - total)) <= 0 ) {
      if (BIO_should_retry(b64)) {
        nread = 0;
        continue;
      }
      err = ERR_get_error();
      printf("problem with BIO_read error(%d) nread(%d) total(%d)\n", err, nread, total);
      break;
    }
  }

  dumpStringX(bfr, len);

  BIO_free_all(b64);

  return bfr;
}

int
main(int argc, char *argv[])
{
  char* encoded;
  char* decoded;
  char* tstr = "Testing 1 2 3";
  char* encoded2 = "VyehF89zpotG14njahbu/A4KA8SYXe2RI+j3gun0dGM=";
 
  int l = strlen(tstr);

  //encoded = encodeB64(tstr, l);
  //decoded = decodeB64(encoded, l);

  decoded = decodeB64(encoded2, strlen(encoded2));

  return 0;
}



Leo Liou
"If it sounds too good to be true, it probably is ..."


Reply via email to