Base64 Decode Issue

XypherXypher Member Posts: 297
Why is it I am able to decode Base64 data that is just ASCII text but I cannot do the same for binary data (images, files, etc) ?


Function I made...
Base64CharSet   Text
bt_buffer       Text
LabelBT         BigText
dbyte           Char
blob_IN         InStream
blob_OUT        OutStream
i               Integer
ii              Integer
shift           Integer
six_bit	      Integer
p_shift         Integer
padding         Integer


DecodeBLOB(blob_IN : InStream;blob_OUT : OutStream)
  Base64CharSet := 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';

  LabelBT.READ(blob_IN);

  IF (LabelBT.LENGTH MOD 4) > 0 THEN
    EXIT(FALSE);

  REPEAT
    i+=1;
    LabelBT.GETSUBTEXT(bt_buffer,i,1);

    IF (STRPOS(Base64CharSet,bt_buffer) > 0) OR (bt_buffer = '=') THEN BEGIN
      IF bt_buffer = '=' THEN
        padding += 1;

      IF padding > 2 THEN
        EXIT;

      six_bit := STRPOS(Base64CharSet,bt_buffer)-1;
      six_bit := (six_bit+ABS(six_bit))/2;
    END ELSE
      six_bit := -1;

    IF six_bit > 0 THEN BEGIN              // <------- BAD IF,  Read Post Below
      shift += 6;
      ii *= 64;
      ii += six_bit;

      IF shift >= 8 THEN BEGIN
        shift -= 8;
        p_shift := POWER(2,shift);
        dbyte := ii DIV p_shift;
        ii := ii - (ii DIV p_shift)*p_shift;
        blob_OUT.WRITE(dbyte);
      END;
    END;
  UNTIL (i = LabelBT.LENGTH) OR ((i+padding) = LabelBT.LENGTH);

Answers

  • XypherXypher Member Posts: 297
    Woops, found my own mistake.

    Feel free to use my Base64 Decode function if anyone wants to.


    Mistake Was Here...
    IF six_bit > 0 THEN BEGIN
    

    Should Be...
    IF six_bit >= 0 THEN BEGIN
    

    :|
Sign In or Register to comment.