Using encoder64

Other open source tools published on ScottKlement.com
Post Reply
kjarrig
Posts: 6
Joined: Tue Apr 18, 2023 8:31 am

Using encoder64

Post by kjarrig »

Hi,
I have a xmldata

Code: Select all

<FtsSkilagrein>
	<Greidandi>
		<Tolvupostfang>noreply@ss.is</Tolvupostfang>
		<Vidtakandi>
			<Kennitala>XXXXXXX</Kennitala>
			<Fjarmagnstekjur>
				<UpprunaAudkenni>XXXXXXX</UpprunaAudkenni>
				<Tegund>Vextir</Tegund>
				<Undirtegund>Stofnsjóður</Undirtegund>
				<Tekjur>36566</Tekjur>
				<Stadgreidsla>8045</Stadgreidsla>
			</Fjarmagnstekjur>
		</Vidtakandi>
		<Vidtakandi>
When I use the base64_encode I get the result

Code: Select all

TMajouKSiZOBh5mFiZVuJUzHmYWJhIGVhIluJUzjlpOlpJeWoqOGgZWHbpWW
mYWXk6isoqJLiaJMYeOWk6Wkl5aio4aBlYduJUzliYSjgZKBlYSJbiVM0oWV
lYmjgZOBbvb38Pj49vHz8flMYdKFlZWJo4GTgW4lTMaRgZmUgYeVoqOFkpGk
mW4lTOSXl5mklYHBpISShZWViW729/D4+Pbx8/H5TGHkl5eZpJWBwaSEkoWV
lYluJUzjhYeklYRu5YWno4mZTGHjhYeklYRuJUzklYSJmaOFh6SVhG7io5aG
laKRznmkmUxh5JWEiZmjhYeklYRuJUzjhZKRpJlu8/b19vZMYeOFkpGkmW4l
instead of

Code: Select all

PEZ0c1NraWxhZ3JlaW4+DQoJPEdyZWlkYW5kaT4NCgkJPFRvbHZ1cG9zdGZhbmc+bm9yZXBseUBzcy5pczwvVG9sdnVwb3N0ZmFuZz4NCgkJPFZpZHRha2FuZGk+DQoJCQk8S2Vubml0YWxhPlhYWFhYWFg8L0tlbm5pdGFsYT4NCgkJCTxGamFybWFnbnN0ZWtqdXI+DQoJCQkJPFVwcHJ1bmFBdWRrZW5uaT5YWFhYWFhYPC9VcHBydW5hQXVka2Vubmk+DQoJCQkJPFRlZ3VuZD5WZXh0aXI8L1RlZ3VuZD4NCgkJCQk8VW5kaXJ0ZWd1bmQ+U3RvZm5zasOzw7B1cjwvVW5kaXJ0ZWd1bmQ+DQoJCQkJPFRla2p1cj4zNjU2NjwvVGVranVyPg0KCQkJCTxTdGFkZ3JlaWRzbGE+ODA0NTwvU3RhZGdyZWlkc2xhPg0KCQkJPC9GamFybWFnbnN0ZWtqdXI+DQoJCTwvVmlkdGFrYW5kaT4NCgkJPFZpZHRha2FuZGk+
The part of the code is:

Code: Select all

   dcl-s xmldata varchar(1500000); // ccsid(1208);
   dcl-s Xmlencode64 like(xmlData);
   Dcl-s xmlPointer pointer;
   Dcl-s xmlLength int(10:0);
   dcl-s xmlOutput pointer;
   dcl-s xmlOutputlen int(10:0);

     xmldata = %str(xmlPointer:xmlLength);
     xmlOutputlen = %len(xmlEncode64: *max);
     xmlOutput = %addr(Xmlencode64: *data);
     wwEnclen = base64_encode(xmlPointer:xmlLength:
       xmlOutput : xmlOutputlen);
     Xmlencode64 = %str(xmlOutput: xmlOutputlen);
I'm quite lost why I don't get the right result with the base64_encode function. Any help is greatly appriciated.
jonboy49
Posts: 221
Joined: Wed Jul 28, 2021 8:18 pm

Re: Using encoder64

Post by jonboy49 »

I'm still wrestling with Base64 and RPG myself but ...

The simple answer is that you are encoding an EBCDIC string and expecting the results to match that of performing the same task with ASCII or UTF-8.

Base64 encoding is designed to preserve a _binary_ pattern. Which we can see most easily as hex. The string "<xmlstuff>" is represented in hex as shown below for UTF-8 and EBCDIC. As you can see they are very different and will produce different results. Since you are probably communicating with a system using UTF-8 the data needs to be in that form in your RPG program. See the code sample below which is loosely based on yours. You will see that the results will be what you expect. Whether you need to convert the output to UTF-8 depends on what you are going to do with it.

UTF8Data: 3C 78 6D 6C 73 74 75 66 66 3E
ebcdicData: 4C A7 94 93 A2 A3 A4 86 86 6E

Code: Select all

**Free
ctl-opt bnddir('BASE64/BASE64BND');

/copy base64/qrpglesrc,base64_h

dcl-s UTF8Data varchar(1000) ccsid(1208)
    inz('<xmlstuff>value here</xmldata>'); 

Dcl-s ebcdicData char(30)
    inz('<xmlstuff>value here</xmldata>'); 

dcl-s encoded  char(45);

dcl-s wait char(1);

dcl-s outputlen int(10:0);

outputlen = base64_encode( %addr(UTF8Data:*data)
                          : %Len(UTF8Data)
                          : %addr(encoded)
                          : %Len(encoded) );

Dsply ('Data: ' + encoded ) ' ' wait;
Note that I only put ebcdicData in the program so I could see the hex values in debug.
kjarrig
Posts: 6
Joined: Tue Apr 18, 2023 8:31 am

Re: Using encoder64

Post by kjarrig »

Thanks jonboy49, you got me on right track
Post Reply