how to decode base64 encoded zpl data back to ASCII/EBCDIC

Other open source tools published on ScottKlement.com
sbehera
Posts: 24
Joined: Tue Sep 07, 2021 10:46 pm

how to decode base64 encoded zpl data back to ASCII/EBCDIC

Post by sbehera »

Hi all,
i have base64 encoded zpl (label to print using zebra printer) getting from HTTPAPI call. Using base64_decode to decode and the result what i see \is not in ASCII/EBCDIC format, looks like may be in binary format. Need to convert to the format that can be print to a spool file. Please help.
sbehera
Posts: 24
Joined: Tue Sep 07, 2021 10:46 pm

Re: how to decode base64 encoded zpl data back to ASCII/EBCDIC

Post by sbehera »

This is my base64 encoded string. Talking to folks where i am getting this, zpl text is base64 encoded (this is not a label image). When i put this base64 encoded string in the website https://www.base64decode.org/, it does get converted to zpl. Does base64_decode routing should able to do same?


Base64 string:
XlhBCl5MTDEyMTgKXlBXODEyCl5QT04KXkxIMCwwCl5DSTI3Cl5GTzQ5NSwxOV5HQjI4NSwxNDAsNCxCLDBeRlMKXkZPNzA4LDQ5M15HQjgzLDQyLDIsQiwwXkZTCl5GTzIwMiwtMl5HQjAsMjAyLDQsQixMXkZTCl5GTzAsMjAwXkdCODExLDAsNCxCLExeRlMKXkZPMCwyODJeR0I4MTEsMCw0LEIsTF5GUwpeRk8wLDgwN15HQjgxMSwwLDgsQixMXkZTCl5GTzAsMTEwOF5HQjgxMSwwLDgsQixMXkZTCl5GTzMwLDMwCl5GQjE2MiwxLDAsTCwwCl5BME4sMTk3LDE5NwpeRkQKRgpeRlMKXkZPNTA1LDI5Cl5GQjI2NCw1LDAsQywwCl5BME4sMTksMTkKXkZEClNBTVBMRSAtIERPIE5PVCBNQUlMISEhIVwmRklSU1QtQ0xBU1MgTUFJTFwmVS5TLiBQT1NUQUdFIFBBSURcJklOVEVSTkFUSU9OQUwgQlJJREdFLCBJTkMuXCZlUE9TVEFHRQpeRlMKXkZPMTAsMjIzCl5GQjc5MSwxLDAsQywwCl5BME4sNTYsNTYKXkZEClVTUFMgRklSU1QtQ0xBU1MgUEtHCl5GUw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nJvbSA5NDAyOFwmMiBvelwmCl5GUwpeRk8xNjcsMTE2MgpeRkIxODEsMSwwLEwsMApeQTBOLDE2LDE2Cl5GRApJbnRlcm5hdGlvbmFsIEJyaWRnZSBCbHVlCl5GUwpeRk80NTMsMTE0NApeRkIzNDksMywwLEwsMApeQTBOLDE0LDE0Cl5GRApQcmludCBzaGlwcGluZyBsYWJlbHMgZnJvbSBob21lXCYkMCBzaWdudXAgZmVlcywgJDAgbW9udGhseSBmZWVzLCAkMCBzdXJjaGFyZ2VzXCZTY2FuIHRoZSBRUiBjb2RlIHRvIGZpbmQgb3V0IG1vcmUKXkZTCl5GTzg4LDg4NwpeQlkzCl5CQ04sMTUyLE4sTixOLEReRkQ0MjA5NTgxOD44OTIwMDEwMDAwMDAwMDAwMDcyNDc0NF5GUwpeRk8zNjksMTEzMgpeQlFOLDIsMl5GRE1BLGJsdWUubXlpYi5jb20/dXRtX3NvdXJjZT1xcl5GUwpeRk8wLC0yXkdEODEyLDEyMTgsNCxCLExeRlMKXkZPMCwtMl5HRDgxMiwxMjE4LDQsQixSXkZTCl5YWg==
jonboy49
Posts: 97
Joined: Wed Jul 28, 2021 8:18 pm

Re: how to decode base64 encoded zpl data back to ASCII/EBCDIC

Post by jonboy49 »

How about showing us the relevant parts of your code. Data defs and invocation.

You're asking us to just guess in the dark right now.
Scott Klement
Site Admin
Posts: 305
Joined: Sun Jul 04, 2021 5:12 am

Re: how to decode base64 encoded zpl data back to ASCII/EBCDIC

Post by Scott Klement »

Base64 will always preserve the exact same binary values that the data was encoded from. That's it's purpose.

So if it was encoded as ASCII, when you decode it, it'll also be ASCII. If when you encoded it, it was UTF-8, it will decode to UTF-8. If you encoded it as part of an image file, it will decode to the same part of the same image file.

My best guess is that you are encoding it in either ASCII or UTF-8 (I don't know what it was encoded from -- you'd have to tell me!) but expecting it'll magically be EBDCIC when you decode it. But, I dont' really know, since you have provided virtually no information about what the data is in your posts, what you are getting, or how you are approaching calling the tools.
HarryWhitehouse
Posts: 3
Joined: Thu Jan 13, 2022 11:07 pm

Re: how to decode base64 encoded zpl data back to ASCII/EBCDIC

Post by HarryWhitehouse »

Hoping that this helps in some way -- the encoding of the ZPL text was accomplished using this Ruby module:

https://ruby-doc.org/stdlib-2.5.3/libdo ... ase64.html

I'd also point out that a true image file will begin with a few bytes that indicate the MIME type and those bytes will be encoded to BASE64.

"jpeg" => "FFD8",
"png" => "89504E470D0A1A0A",
"gif" => "474946",
"bmp" => "424D",
"tiff" => "4949",
"tiff" => "4D4D"

In the case of ZPL text, there is no MIME type in the header. So if the base64 decoder is assuming it's an image, it could be confused when those bytes don't appear.

The Base64 in the original question fails to decode if you try this web site which is trying to convert a Base64 assuming it's an image:

https://codebeautify.org/base64-to-image-converter

But if you go to a site that doesn't assume an image, the ZPL decodes without a problem:

https://www.base64decode.org


Regards,

Harry
sbehera
Posts: 24
Joined: Tue Sep 07, 2021 10:46 pm

Re: how to decode base64 encoded zpl data back to ASCII/EBCDIC

Post by sbehera »

Here is part of the code that is relevant to find the issue: After base64_decode called when i look at value of ZplOut using debug, data looks like binary which i am giving below (value of B64In value before call to base64 routine was given in original post).

Code: Select all

D JdsNode         s                   like(yajl_Val)   
D oNode           s                   like(yajl_Val)   
D list            s                   like(yajl_Val)   
D B64In           s          20000A   
D ZplOut          s          20000A   
D B64DErr         s            100A
D JdsRerr         s            500A   varying
result = http_string( 'POST': url: postdata: 'application/json');
JdsNode = yajl_buf_load_tree(%Addr(result): %Len(%Trim(result)):JdsRerr);
If JdsRerr = *Blank; 
   list = yajl_object_find( jdsNode: 'base64_labels');                 
   i = 0;                                                
   DoW YAJL_ARRAY_LOOP(list: i: oNode);                  
       B64In = yajl_get_string(oNode);                   
       If B64In <> *Blank;                               
          base64_decode( %addr(B64In)                    
                         : %len(%trimr(B64In))           
                         : %addr(ZplOut)                 
                        : %size(ZplOut) );               
          If (%size(ZplOut) < 0);                          
             B64DErr  = 'LBL B64 DECODE ERROR';  
          Endif;                                 
       Endif;                                    
   EndDo;                                        
EndIf;         
yajl_tree_free(JdsNode);  
Using debug EVAL zplout:c 4000 value as given below:
;ì ;<<;&ï;&|+;<ç;äñ;ã|;åââ
;ãë;ã|;åââ;ãë;ã|;åââ<;ãë
;ã|;åââ<;ãë;ã|;åââ<;ãë;ã|
;åââ<;ãë;ã|;åââ<;ãë;ã|;ãâ
<; +;ãàã;ãë;ã|;ãâä; 
+;ãàë (&<áà|+|è( ñ<*ãñêëèä< ëë( ñ<*íë
&|ëè åá& ñà*ñ+èáê+ èñ|+ <âêñàåáñ+ä*Á&|ëè åá;ãë;ã|
;ãâä; +;ãàíë&ëãñêëèä< ëë&.å;ã
ë;ã|;ãâ<; +;ãàç êêßïçñèáç|íëá*
ñ+èá+ê èñ|+ <âêñàåá*ç ßãñá<àëêà*&|êè|< î <<áßä 
;ãë;ã|;ãâ<; +;ãà& èêñä
.ïçñèáç|íëá*&ê|àíäèêáèíê+ëï êáç|íëá*êà îá*ë äê 
(á+è|ä ;ãë;ã|;ãâä; +
;ãàíë&ëèê ä.ñ+åá&;ãë;ã|;ãâä; +
;ãà;ãë;ã|;ãâ
<; +;ãà;ãë;ã|;ãâ<; +
;ãà;ãë;ã|;ãâä; +;ãàä;ã
ë;ã|;ãâä; +;ãà;ãë;ã|;
ãâ<; +;ãà;ãë;ã|;ãâ<
; +;ãà;ãë;ã|;ãâ<; +;ãà
;ãë;ã|;ãâê; +;ãà*(
/Ñ%ÁÀÃÊ?_*?:*;ãë;ã|;ãâ<; +
;ãàñ>ÈÁÊ>/ÈÑ?>/%âÊÑÀÅÁâ%ÍÁ;ãë;ã|;ãâ
<; +;ãà&ÊÑ>ÈËÇÑøøÑ>Å%/ÂÁ%ËÃÊ?_Ç?_Á*Ë
ÑÅ>ÍøÃÁÁË_?>ÈÇ%`ÃÁÁËËÍÊÄÇ/ÊÅÁË*ëÄ/>ÈÇÁéêÄ?ÀÁ
È?ÃÑ>À?ÍÈ_?ÊÁ;ãë;ã|;âß;âä++++à;ãà
;ãë;ã|;âé+;ãà( Â%
ÍÁ_`ÑÂÄ?_ÍÈ_¬Ë?ÍÊÄÁÉÊ;ãë;ã|;åàâ<;ãë;ã|
;åàâê;ãë;ì!
Scott Klement
Site Admin
Posts: 305
Joined: Sun Jul 04, 2021 5:12 am

Re: how to decode base64 encoded zpl data back to ASCII/EBCDIC

Post by Scott Klement »

Let's examine this code:

Code: Select all

D ZplOut          s          20000A   
          :
          :
          base64_decode( %addr(B64In)                    
                         : %len(%trimr(B64In))           
                         : %addr(ZplOut)                 
                        : %size(ZplOut) );               
          If (%size(ZplOut) < 0);                          
             B64DErr  = 'LBL B64 DECODE ERROR';  
          Endif;                                 
%SIZE returns the number of bytes of memory that a variable occupies. It's not possible for %SIZE to return less than 0. In this example, %SIZE(ZplOut) will always be 20000 since that's how much memory it occupies.

If you want to check for errors, you need to use MONITOR.

Next, you aren't checking the length of the data that's output. That's not optional, the output won't be EBCDIC text, so it's not valid to use %LEN(%TRIM(ZPLOUT)). You need that length in order to work with the data after you decode it. (Although, the code you provided doesn't actually do anything with it, presumably you'll need to actually use it for something at some point?)

Code: Select all

D ZplOut          s          20000A          
D ZplLen          s             10u 0        
     :
     :                                                  
     monitor;                                     
       ZplLen = base64_decode( %addr(B64In)       
                             : %len(%trimr(B64In))
                             : %addr(ZplOut)      
                             : %size(ZplOut) );   
     on-error;                                    
       B64DErr  = 'LBL B64 DECODE ERROR';         
     endmon;                                      


As I said above, the output (in this case, ZplOut) will be on whatever encoding it was originally in. However, the debugger is expecting the data to be EBCDIC. Since it's probably not EBCDIC (I find it VERY hard to believe that a Zebra API outputs EBCDIC) you'll need to treat it as the original encoding instead of EBCDIC. (Or, if you want it to be EBCDIC, you'll need to translate it.)
Scott Klement
Site Admin
Posts: 305
Joined: Sun Jul 04, 2021 5:12 am

Re: how to decode base64 encoded zpl data back to ASCII/EBCDIC

Post by Scott Klement »

I took the base64 encoded data that you provided above and dumped it into a program, then decoded it and wrote it to an ifs file. Trying to follow your lead, I used a coding style from around the 2007-ish.

This seems to work just fine for me:

Code: Select all

     H dftactgrp(*no) bnddir('BASE64')

      /copy ifsio_h
      /copy base64_h

     D stmf            c                   '/tmp/zplData.txt'
     D fd              s             10i 0
     D b64in           s          20000a   varying
     D zplOut          s          20000a
     D zplLen          s             10u 0
     D cmd             s            200a   varying

     D p_errnum        s               *
     D errnum          s             10i 0 based(p_errnum)

     D get_errno       pr              *   extproc('__errno')

     D QCMDEXC         pr                  extpgm('QSYS/QCMDEXC')
     D  command                   32767a   const options(*varsize)
     D  length                       15p 5 const
     D  igc                           3a   const options(*nopass)

      /free

       b64in = 'XlhBCl5MTDEyMTgKXlBXODEyCl5QT04KXkxIMCwwCl5DSTI3Cl5GTzQ5N+
                SwxOV5HQjI4NSwxNDAsNCxCLDBeRlMKXkZPNzA4LDQ5M15HQjgzLDQyLD+
                IsQiwwXkZTCl5GTzIwMiwtMl5HQjAsMjAyLDQsQixMXkZTCl5GTzAsMjA+
                wXkdCODExLDAsNCxCLExeRlMKXkZPMCwyODJeR0I4MTEsMCw0LEIsTF5G+
                UwpeRk8wLDgwN15HQjgxMSwwLDgsQixMXkZTCl5GTzAsMTEwOF5HQjgxM+
                SwwLDgsQixMXkZTCl5GTzMwLDMwCl5GQjE2MiwxLDAsTCwwCl5BME4sMT+
                k3LDE5NwpeRkQKRgpeRlMKXkZPNTA1LDI5Cl5GQjI2NCw1LDAsQywwCl5+
                BME4sMTksMTkKXkZEClNBTVBMRSAtIERPIE5PVCBNQUlMISEhIVwmRklS+
                U1QtQ0xBU1MgTUFJTFwmVS5TLiBQT1NUQUdFIFBBSURcJklOVEVSTkFUS+
                U9OQUwgQlJJREdFLCBJTkMuXCZlUE9TVEFHRQpeRlMKXkZPMTAsMjIzCl+
                5GQjc5MSwxLDAsQywwCl5BME4sNTYsNTYKXkZEClVTUFMgRklSU1QtQ0x+
                BU1MgUEtHCl5GUwpeRk8xMiwyOTQKXkZCNDE2LDQsMCxMLDAKXkEwTiwy+
                MiwyMgpeRkQKSEFSUlkgV0hJVEVIT1VTRVwmSU5URU5SQVRJT05BTCBCU+
                klER0VcJjcwIEhBWUZJRUxEUyBSRFwmUE9SVE9MQSBWQUxMRVksIENBID+
                k0MDI4LTcyNDkKXkZTCl5GTzExMSw1NjAKXkZCNjU5LDQsMCxMLDAKXkE+
                wTiwyOCwyOApeRkQKUEFUUklDSyBXSElURUhPVVNFXCZQUk9EVUNUIFJF+
                VFVSTlMgV0FSRUhPVVNFXCYyMTU3IDNSRCBBVkVcJlNBQ1JBTUVOVE8sI+
                ENBIDk1ODE4LTMxMDEKXkZTCl5GTzUwLDgyNgpeRkI3MTAsMSwwLEMsMA+
                peQTBOLDI1LDI1Cl5GRApVU1BTIFRSQUNLSU5HICMgLSBFUApeRlMKXkZ+
                PNTAsMTA3NwpeRkI3MTAsMSwwLEMsMApeQTBOLDI1LDI1Cl5GRAo5MjAw+
                IDEwMDAgMDAwMCAwMDAwIDcyNDcgNDQKXkZTCl5GTzEzLDQ3NgpeRkI0M+
                TYsMSwwLEwsMApeQTBOLDIyLDIyCl5GRAoKXkZTCl5GTzEzLDUwNwpeRk+
                I0MTYsMSwwLEwsMApeQTBOLDIyLDIyCl5GRAoKXkZTCl5GTzcxNiw1MDE+
                KXkZCNjksMSwwLEMsMApeQTBOLDI4LDI4Cl5GRApDMDE5Cl5GUwpeRk80+
                OTUsMTY4Cl5GQjI4OSwxLDAsQywwCl5BME4sMjUsMjUKXkZECgpeRlMKX+
                kZPNDQ5LDM4OQpeRkIzNTUsMSwwLEwsMApeQTBOLDI4LDI4Cl5GRAoKXk+
                ZTCl5GTzQ0OSw0MjAKXkZCMzU1LDEsMCxMLDAKXkEwTiwyOCwyOApeRkQ+
                KCl5GUwpeRk80NDksNDUyCl5GQjM1NSwxLDAsTCwwCl5BME4sMjgsMjgK+
                XkZECgpeRlMKXkZPNTkyLDI5MwpeRkIyMTMsNCwwLFIsMApeQTBOLDE4L+
                DE4Cl5GRAowMS8yMS8yMDIyXCZNYWlsZWQgZnJvbSA5NDAyOFwmMiBvel+
                wmCl5GUwpeRk8xNjcsMTE2MgpeRkIxODEsMSwwLEwsMApeQTBOLDE2LDE+
                2Cl5GRApJbnRlcm5hdGlvbmFsIEJyaWRnZSBCbHVlCl5GUwpeRk80NTMs+
                MTE0NApeRkIzNDksMywwLEwsMApeQTBOLDE0LDE0Cl5GRApQcmludCBza+
                GlwcGluZyBsYWJlbHMgZnJvbSBob21lXCYkMCBzaWdudXAgZmVlcywgJD+
                AgbW9udGhseSBmZWVzLCAkMCBzdXJjaGFyZ2VzXCZTY2FuIHRoZSBRUiB+
                jb2RlIHRvIGZpbmQgb3V0IG1vcmUKXkZTCl5GTzg4LDg4NwpeQlkzCl5C+
                Q04sMTUyLE4sTixOLEReRkQ0MjA5NTgxOD44OTIwMDEwMDAwMDAwMDAwM+
                DcyNDc0NF5GUwpeRk8zNjksMTEzMgpeQlFOLDIsMl5GRE1BLGJsdWUubX+
                lpYi5jb20/dXRtX3NvdXJjZT1xcl5GUwpeRk8wLC0yXkdEODEyLDEyMTg+
                sNCxCLExeRlMKXkZPMCwtMl5HRDgxMiwxMjE4LDQsQixSXkZTCl5YWg==';

       zplLen = base64_decode( %addr(b64In:*data)
                             : %len(b64In)
                             : %addr(ZplOut)
                             : %size(ZplOut) );

       // Note: This is assuming the data is UTF-8 (CCSID 1208)
       //       but it may be a flavor of ASCII. There's no way for
       //       us to know that -- so the 1208 below may be wrong.
       
       unlink(stmf);
       fd = open( stmf
                : O_CREAT + O_EXCL + O_WRONLY + O_INHERITMODE
                + O_CCSID
                : 0
                : 1208
                : 0 );

       if fd = -1;
         p_errnum = get_errno();
         dsply ('CPE' + %char(errnum) + ' opening file');
         *inlr = *on;
         return;
       endif;

       callp write(fd: %addr(ZplOut): zplLen);
       callp close(fd);

       cmd = 'DSPF STMF(''' + stmf + ''')';
       QCMDEXC(cmd: %len(cmd));

       unlink(stmf);

       *inlr = *on;
 
sbehera
Posts: 24
Joined: Tue Sep 07, 2021 10:46 pm

Re: how to decode base64 encoded zpl data back to ASCII/EBCDIC

Post by sbehera »

Hi Scott,
data written to ifs file i do see as zpl data but when i check value of variable ZplOut using debug, after base64_Decode is called it still shows as binary or hexadecimal. IFS file shows data in UTF-8 because file is created with CCSID 1208?

I need to print this zpl data to spool file to send to printer. How to do that? Here what i am doing (after zpl data written to ifs) to achieve what i need:
- Created a physical file DMYPRTFL with as400 default CCSID 65535: CRTPF FILE(TEST1/DMYPRTFL) RCDLEN(132)
- these few lines added to code

cmd = 'CPY OBJ(' + #Quote + stmf + #Quote + ') ' +
'TOOBJ(' + #Quote + 'dmyprtfl' +
#Quote + ') FROMCCSID(*OBJ) ' +
'TOCCSID(*JOBCCSID) DTAFMT(*TEXT) REPLACE(*YES)';
QCMDEXC(cmd: %len(cmd));
CMD = 'CPYF FROMFILE(dmyprtfl) TOFILE(QSYSPRT)';
QCMDEXC(cmd: %len(cmd));

No data getting copied to DMYPRTFL after CPY OBJ command.
sbehera
Posts: 24
Joined: Tue Sep 07, 2021 10:46 pm

Re: how to decode base64 encoded zpl data back to ASCII/EBCDIC

Post by sbehera »

Data was not getting copied to dmyprtfl because i did not specify full path in toobj parameter. Changed to as given below and it works fine. I am able to send zpl to spool file.
cmd = 'CPY OBJ(' + #Quote + stmf + #Quote + ') ' +
'TOOBJ(' + #Quote + '/qsys.lib/test1.lib/' +
'dmyprtfl.file/dmyprtfl.mbr' +
#Quote + ') FROMCCSID(*OBJ) ' +
'TOCCSID(*JOBCCSID) DTAFMT(*TEXT) REPLACE(*YES)';

Question:
- Is there any better way to print zpl data to spool?
- why zplout variable after encoding does not show data in ascii, if i want to save this value to a table field, will that be in ascii?
Post Reply