Question

ascii85 has a function to get the maximum length of an encoding MaxEncodedLen().

I think that it should have too a function to get the length at decoding like it has in Base64.

http://golang.org/pkg/encoding/ascii85/

Was it helpful?

Solution

Here's a function to calculate Go package ascii85 MaxDecodedLen() for n encoded bytes.

func MaxDecodedLen(n int) int {
    const binWordLen = 4
    return n * binWordLen
}

If all four bytes of a unencoded group are zero, they are represented by a single byte, the character z, instead of by five exclamation points (!!!!!). In some implementations, an unencoded group of spaces may be represented by the single character y.

ascii85.Decode(), unlike ascii85.Encode(), has number of bytes consumed (nsrc) and flush parameters, in addition to a number of bytes written (ndst) parameter, which allows the programmer to decode multiple blocks or a single block piece-by-piece. Therefore, a destination buffer of less than the MaxDecodedLen() may be used.

OTHER TIPS

In my opinion,While encoding the MaxEncodedLen() is mandatory to know the number of bytes required to hold the encoded output (ie destination buffer size ).
Where in case of decoding ,the caller can pass the destination buffer of size equal to source buffer though the destination buffer can be lesser size.
Alternatively we can use the original source ie plain non-encoded buffer size,if the execution context/scope of encoding and decoding are same. Hence the expected function,MaxDecodedLen() is optional.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top