uuDecode problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • py

    uuDecode problem

    Hi,
    I am encoding a string such as...

    Code:
    data = someFile.readlines()
    encoded = []
    for line in data:
    encoded.append(binascii.b2a_uu(stringToEncode))
    return encoded
    ....I then try to decode this by...

    Code:
    def decode(data):
    result = []
    for val in data:
    result.append(binascii.a2b_uu(val))
    return result
    this seems to work sometimes....fo r example a list which has a short
    string in it like ["this is a test"]

    however if the list of data going into the decode function contains a
    bunch of elements I get the following error...

    result.append(b inascii.a2b_uu( val))
    binascii.Error: Trailing garbage

    ...any idea why this is happening? Anyone successfully use the uu to
    encode/decode strings of varying length (even larger strings, more than
    a few hundred characters)?

  • Alex Martelli

    #2
    Re: uuDecode problem

    py <codecraig@gmai l.com> wrote:
    ...[color=blue]
    > encoded.append( binascii.b2a_uu (stringToEncode ))[/color]

    binascii.b2a_uu only works for up to 45 bytes at once; but if you were
    feeding it more than 45 bytes, this should raise a binascii.Error
    itself.
    [color=blue]
    > ..any idea why this is happening? Anyone successfully use the uu to
    > encode/decode strings of varying length (even larger strings, more than
    > a few hundred characters)?[/color]

    Definitely not, given the above limit. But I still don't quite
    understand the exact mechanics of the error you're getting.


    Alex

    Comment

    • py

      #3
      Re: uuDecode problem

      Alex Martelli wrote:[color=blue]
      > binascii.b2a_uu only works for up to 45 bytes at once; but if you were
      > feeding it more than 45 bytes, this should raise a binascii.Error
      > itself.
      > Definitely not, given the above limit. But I still don't quite
      > understand the exact mechanics of the error you're getting.
      >
      >
      > Alex[/color]

      here is an example.

      def doSomething():
      data = aFile.readlines ()
      result = []
      for x in data:
      result.append(e ncode(x))
      return result

      def printResult(enc odedData):
      """encodedD ata is a list of strings which are uu encoded"""
      print decode(encodedD ata)

      encode(data):
      """data is a string"""
      if len(data) > 45:
      tmp = []
      for c in data:
      tmp.append(bina scii.b2a_uu(c))
      return ''.join(tmp)
      else:
      return binascii.b2a_uu (data)


      decode(data):
      """data is a list of strings"""
      result = []
      for val in data
      if len(val) > 45:
      response = []
      for x in val:
      response.append (binascii.a2b_u u(x))
      result.append(r esponse)
      else:
      result.append(b inascii.a2b_uu( val))
      return ''.join(result)

      ....i would use those functions like

      data = doSomething()
      printResult(dat a)

      Now i get this...
      " response.append (binascii.a2b_u u(x))
      java.lang.Strin gIndexOutOfBoun dsException:
      java.lang.Strin gIndexOutOfBoun dsExcep
      tion: String index out of range: 1"

      So the error is in the decode method .....this is in Jython...perhap s
      Jython doesn't handle binascii.a2b_uu ? or perhaps since the actual
      data is being encoded in python, then read in and decoded in my jython
      script..that could be the problem?

      thanks.

      Comment

      • jepler@unpythonic.net

        #4
        Re: uuDecode problem

        Note that you can use the 'uu' encoding, which will handle
        arbitrary-length input and give multi-line uuencoded output, including
        the 'begin' and 'end' lines:[color=blue][color=green][color=darkred]
        >>> print ("\377" * 120).encode("uu ")[/color][/color][/color]


        Otherwise, you should use something like
        encoded = [binascii.b2a_uu (chunk)
        for chunk in iter(lambda: someFile.read(4 5), "")]
        to send at most 45 bytes to each call to b2a_uu.

        Jeff

        -----BEGIN PGP SIGNATURE-----
        Version: GnuPG v1.4.1 (GNU/Linux)

        iD8DBQFDlw/oJd01MZaTXX0RAq IOAJ46t30KNHMT7 tAHULcPQORmqKQ9 PACglBTh
        GBjbFibJBu+GDx6 cbtC53Us=
        =GWjT
        -----END PGP SIGNATURE-----

        Comment

        • Alex Martelli

          #5
          Re: uuDecode problem

          py <codecraig@gmai l.com> wrote:
          ...[color=blue]
          > """data is a string"""
          > if len(data) > 45:
          > tmp = []
          > for c in data:
          > tmp.append(bina scii.b2a_uu(c))[/color]

          You can't decode b2a-encoded data character by character, blindly, as
          you're trying to to here. Each character in the source string can be
          encoded into multiple characters in the target string, and the slicing,
          if slicing is needed, must be appropriate. I suggest a redesign...!


          Alex

          Comment

          • py

            #6
            Re: uuDecode problem

            Alex Martelli wrote:
            I suggest a redesign...!


            What would you suggest? I have to encode/decode in chunks b/c of the
            45 byte limitation.

            Thanks.

            Comment

            • Fredrik Lundh

              #7
              Re: uuDecode problem

              "py" wrote:
              [color=blue]
              > What would you suggest? I have to encode/decode in chunks b/c of the
              > 45 byte limitation.[/color]

              so use 45-byte chunks, instead of one-byte chunks.

              but why are you using UU encoding in a nonstandard way ? why not just
              use the "uu" module to do the chunking for you? the third example on this
              page might be helpful:



              (if you don't want the standard begin/end lines, it's probably a better idea
              to use base64 encoding instead...)

              </F>



              Comment

              • Alex Martelli

                #8
                Re: uuDecode problem

                py <codecraig@gmai l.com> wrote:
                [color=blue]
                > Alex Martelli wrote:
                > I suggest a redesign...!
                >
                >
                > What would you suggest? I have to encode/decode in chunks b/c of the
                > 45 byte limitation.[/color]

                Not quite:
                [color=blue][color=green][color=darkred]
                >>> s=45*'v'
                >>> a=binascii.b2a_ uu(s)
                >>> len(a)[/color][/color][/color]
                62[color=blue][color=green][color=darkred]
                >>> b=binascii.a2b_ uu(a)
                >>> len(b)[/color][/color][/color]
                45[color=blue][color=green][color=darkred]
                >>> b==s[/color][/color][/color]
                True[color=blue][color=green][color=darkred]
                >>>[/color][/color][/color]

                I.e., you can pass to a2b_uu ANY string (and ONLY such a string, not,
                e.g., a slice or single char of it, as you're trying to do) that's a
                result of a b2a_uu call; the length limitation applies only the other
                way.

                I join /F in suggesting yo use binascii the standard way, but, at any
                rate, you should at least redesign your decoding strategy so it only
                calls a2b_uu on strings which are the results of b2a_uu calls.


                Alex

                Comment

                • py

                  #9
                  Re: uuDecode problem

                  Thanks...I think base64 will work just fine...and doesnt seem to have
                  45 byte limitations, etc.

                  Thanks.

                  Comment

                  • Alex Martelli

                    #10
                    Re: uuDecode problem

                    py <codecraig@gmai l.com> wrote:
                    [color=blue]
                    > Thanks...I think base64 will work just fine...and doesnt seem to have
                    > 45 byte limitations, etc.[/color]

                    Sure, base64 is a better encoding by all criteria, unless you
                    specifically need to use uu encoding for compatibility with other old
                    software.


                    Alex

                    Comment

                    Working...