ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Decoding Base64 data does not return the original string value

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Decoding Base64 data does not return the original string value

    What I'm supposed to do is read a base64 encoded value

    eyJzb3VyY2UiOiJDQ0RWIiwidHJhY2VpZCI6IjNiM2MxZDZjZT U2Y2QxZTBkNThlNDA4NDY2YWVkZWE3In0=

    from a field in a database file and decode it to obtain the original JSON statement

    {"source":"CCDV","traceid":"3b3c1d6ce56cd1e0d58e40 8466aedea7"}

    I tested it in an online converter https://codebeautify.org/base64-to-json-converter and it worked perfectly.

    The result is quite different when I try to decode it in my RPG ILE program. I thought: if I encode the original JSON statement and then decode the result i should get the JSON statement again. Well, it doesn't! Why?

    I tried treating the data as binary. The base64 encoding is very different from the previous one but when I decode it I get the original JSON statement.
    What am I doing wrong?
    Here follows the program code. It was compiled for Version 7.3. Thank you for your attention.

    **FREE

    DCL-S sCode char(512) inz;
    DCL-S uCode uns(10) inz;
    DCL-S sDcode char(255) inz;
    DCL-S uDcode uns(10) inz;
    DCL-S uLen uns(10) inz;

    DCL-S sVar char(512) inz;

    DCL-S sJSON char(255) inz('{"source":"CCDV","traceid":"3b3c1d6ce56cd1e0d 58e408466aedea7"}');

    DCL-PR API_APR_Code64 INT(10) EXTPROC('apr_base64_encode');
    xsCode64 char(65535) options(*VARSIZE);
    xpOriginal pointer value options(*STRING);
    xnCode64 int(10) value;
    END-PR;

    DCL-PR API_APR_Decode64 INT(10) EXTPROC('apr_base64_decode');
    xsPlainText char(65535) options(*VARSIZE);
    xpCode64 pointer value options(*STRING);
    END-PR;

    DCL-PR API_APR_Code64Binary INT(10) EXTPROC('apr_base64_encode_binary');
    xsCode64 char(65535) options(*VARSIZE);
    xpOriginal pointer value options(*STRING);
    xnCode64 int(10) value;
    END-PR;

    DCL-PR API_APR_Decode64Binary INT(10) EXTPROC('apr_base64_decode_binary');
    xsPlainText char(65535) options(*VARSIZE);
    xpCode64 pointer value options(*STRING);
    END-PR;

    sVar = sJSON;
    uLen = %LEN(%TRIMR(sJSON));

    // sCode = eyJzb3VyY2UiOiJDQ0RWIiwidHJhY2VpZCI6IjNiM2MxZDZjZT U2Y2QxZTBkNThlNDA4NDY2YWVkZWE3In0=
    uCode = API_APR_Code64(sCode : %subst(sVar : 1 : uLen) : uLen) - 1;

    // sDcode = #?Ë?ÍÊÄÁ???ääàî???ÈÊ/ÄÁÑÀ????Â?Ä?À?ÄÁ??ÄÀ?Á?À??Á??????/ÁÀÁ/??'
    uDcode = API_APR_Decode64(sDcode : %subst(sCode : 1 : uCode));

    sVar = sJSON;
    uLen = %LEN(%TRIMR(sJSON));

    // sCode = wH+ilqSZg4V/en/Dw8Tlf2t/o5mBg4WJhH96f/OC84PxhPaDhfX2g4TxhfCE9fiF9PD49Pb2gYWEhYH3f9A
    uCode = API_APR_Code64Binary(sCode : %subst(sVar : 1 : uLen) : uLen) - 1;

    // sDcode = {"source":"CCDV","traceid":"3b3c1d6ce56cd1e0d58 e40 8466aedea7"}
    uDcode = API_APR_Decode64Binary(sDcode : %subst(sCode : 1 : uCode));


    *INLR = *ON;

  • #2
    See discussion here: https://www.scottklement.com/forums/viewtopic.php?t=148

    Comment

    Working...
    X