Topic
5 replies Latest Post - ‏2013-09-11T20:39:53Z by Nitin_Jha
terza
terza
99 Posts
ACCEPTED ANSWER

Pinned topic Decoding base64 text that contains special characters

‏2009-05-28T10:39:40Z |
Hi,
I have a situation where I need to extract base64 encoded text that is actually a new XML document. This I can easily do by using dp:parse() function. The problem comes when the base64 encoded XML document contains special national characters such as ä or ö. Does anyone know how could I avoid this?
Here is the input document:
<?xml version="1.0" encoding="UTF-8"?>
<foo>
        <base64encodedDocument>PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz4NCjxiYXI+PGNvbnRlbnQ+SGVyZSB5b3UgaGF2ZSBzb21lIHNwZWNpYWwgY2hhcmNhdGVyczogw7bDpMOlPC9jb250ZW50PjwvYmFyPg==</base64encodedDocument>
</foo>

And here is what I would like to get out from <base64encodedDocument> after decoding:
<?xml version="1.0" encoding="UTF-8"?>
<bar><content>Here you have some special charcaters: öäå</content></bar>


Anyone have any ideas how to do this?
Updated on 2014-03-25T03:50:15Z at 2014-03-25T03:50:15Z by iron-man
  • Nitin_Jha
    Nitin_Jha
    20 Posts
    ACCEPTED ANSWER

    Re: Decoding base64 text that contains special characters

    ‏2013-09-05T21:28:13Z  in response to terza

    I have a similar issue, while trying to decode a base 64 encoded string, I am getting the below mentioned error.

    Valid base64 passed to dp:decode resulted in non UTF8:

    I am using   dp:decode($Request,'base-64'), I tried dp:binary-decode as well, that does not work either,

    Please advise

     

    • HermannSW
      HermannSW
      4128 Posts
      ACCEPTED ANSWER

      Re: Decoding base64 text that contains special characters

      ‏2013-09-06T07:27:10Z  in response to Nitin_Jha

      Hi Nitin,

      DataPower internal encoding is UTF-8, and therefore dp:decode(_, 'base-64') has to ensure this.

      You cannot safely decode other base64 encoded strings.

      For the basics pleas hear into 1st WSTE webcasts on Non-XML processing in DataPower stylesheets:
      http://www-01.ibm.com/support/docview.wss?uid=swg27022977

      Options for converting the based64 encoded data in case it is just in another encoding are discussed in 2nd webcast:
      http://www-01.ibm.com/support/docview.wss?uid=swg27022979

      Both webcasts have attached .pdf, .mp3 and all samples in .zip .


      If you would paste in a small sample base64 string that would allow to say more on the options you have.


      Hermann<myXsltBlog/> <myXsltTweets/> <myCE/>

      Updated on 2013-09-06T07:29:30Z at 2013-09-06T07:29:30Z by HermannSW
      • Nitin_Jha
        Nitin_Jha
        20 Posts
        ACCEPTED ANSWER

        Re: Decoding base64 text that contains special characters

        ‏2013-09-06T22:20:56Z  in response to HermannSW

        Thanks for the update Hermann, here's the base 64 encoded string that I am trying to decode.

        AAAg8Ev/CHrK/VNv37AKL+2Qxv3PuAn/lLwEPQAsFOWdiK0AdHsiYXBzIjp7InNvdW5kIjoiZGVm
        YXVsdCIsImFsZXJ0Ijp7ImFjdGlvbi1sb2Mta2V5IjoiVmlldyIsImxvYy1rZXkiOiJGaW5hbGx5
        IHN1Y2Nlc3MgaW4gUHVzaGluZyBQYXlsb2FkIHRvIEFQTlMifX19

        • HermannSW
          HermannSW
          4128 Posts
          ACCEPTED ANSWER

          Re: Decoding base64 text that contains special characters

          ‏2013-09-08T12:37:11Z  in response to Nitin_Jha

          Your base64 string contains a lot of Non-XML characters in the begiining, inclusive some 0x00 bytes.

          You cannot decode that, working on that base64 encoded or hex encoded string is the onloy option.

          But after that binary data starting at byte offset 38 there is JSON in above base64 string.

          Here you can see how to output that:

          $ coproc2 Nitin.xsl Nitin.xml http://dp6-l3:2223 ; echo
          {"aps":{"sound":"default","alert":{"action-loc-key":"View","loc-key":"Finally success in Pushing Payload to APNS"}}}
          $
          $ cat Nitin.xml
          <b64>AAAg8Ev/CHrK/VNv37AKL+2Qxv3PuAn/lLwEPQAsFOWdiK0AdHsiYXBzIjp7InNvdW5kIjoiZGVmYXVsdCIsImFsZXJ0Ijp7ImFjdGlvbi1sb2Mta2V5IjoiVmlldyIsImxvYy1rZXkiOiJGaW5hbGx5IHN1Y2Nlc3MgaW4gUHVzaGluZyBQYXlsb2FkIHRvIEFQTlMifX19</b64>
          $
          $ cat Nitin.xsl
          <xsl:stylesheet version="1.0"
            xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
            xmlns:dp="http://www.datapower.com/extensions"
            extension-element-prefixes="dp"
          >
            <xsl:template match="/">
              <!-- hex encoded (binary) input -->    
              <xsl:variable name="input16"
                  select="substring(dp:radix-convert(concat('8AAA', b64), 64, 16), 7)"/>

              <!-- JSON starting at byte ofset 38 -->
              <xsl:variable name="result16" select="substring($input16, 2*38-1)"/>

              <!-- JSON output -->
              <xsl:copy-of
                select="dp:decode(dp:radix-convert($result16, 16, 64), 'base-64')"/>
            </xsl:template>
           
          </xsl:stylesheet>
          $

           

          Hermann<myXsltBlog/> <myXsltTweets/> <myCE/>

          • Nitin_Jha
            Nitin_Jha
            20 Posts
            ACCEPTED ANSWER

            Re: Decoding base64 text that contains special characters

            ‏2013-09-11T20:39:53Z  in response to HermannSW

            Thanks for the clarification Herman, But I need to be able to send the complete decoded string to the back-end server. So I have changed my approach.

            Now I am receiving the request (encoded string) from the consumer at Message Broker. The Message Broker the decodes the complete string and send the compete message to Datapower over MQ. Datapower picks the message and sends it to the back-end server over TCP (SSL). And this is working fine.