To store or transfer binary data in environments which only support text one has to encode the binary data by mapping the input bytes to characters in the range allowed for storing or transfering. SVID systems (and nowadays XPG compliant systems) provide minimal support for this task.
l64a
is undefined if n is negative. In the GNU
implementation, l64a
treats its argument as unsigned, so it will
return a sensible encoding for any nonzero n; however, portable
programs should not rely on this.
To encode a large buffer l64a
must be called in a loop, once for
each 32-bit word of the buffer. For example, one could do something
like this:
char * encode (const void *buf, size_t len) { /* We know in advance how long the buffer has to be. */ unsigned char *in = (unsigned char *) buf; char *out = malloc (6 + ((len + 3) / 4) * 6 + 1); char *cp = out; /* Encode the length. */ /* Using `htonl' is necessary so that the data can be decoded even on machines with different byte order. */ cp = mempcpy (cp, l64a (htonl (len)), 6); while (len > 3) { unsigned long int n = *in++; n = (n << 8) | *in++; n = (n << 8) | *in++; n = (n << 8) | *in++; len -= 4; if (n) cp = mempcpy (cp, l64a (htonl (n)), 6); else /* `l64a' returns the empty string for n==0, so we must generate its encoding ("......") by hand. */ cp = stpcpy (cp, "......"); } if (len > 0) { unsigned long int n = *in++; if (--len > 0) { n = (n << 8) | *in++; if (--len > 0) n = (n << 8) | *in; } memcpy (cp, l64a (htonl (n)), 6); cp += 6; } *cp = '\0'; return out; }
It is strange that the library does not provide the complete functionality needed but so be it.
To decode data produced with l64a
the following function should be
used.
l64a
. The function processes at least 6 characters of
this string, and decodes the characters it finds according to the table
below. It stops decoding when it finds a character not in the table,
rather like atoi
; if you have a buffer which has been broken into
lines, you must be careful to skip over the end-of-line characters.
The decoded number is returned as a long int
value.
The l64a
and a64l
functions use a base 64 encoding, in
which each character of an encoded string represents six bits of an
input word. These symbols are used for the base 64 digits:
@multitable {xxxxx} {xxx} {xxx} {xxx} {xxx} {xxx} {xxx} {xxx} {xxx}
.
@tab /
@tab 0
@tab 1
@tab 2
@tab 3
@tab 4
@tab 5
6
@tab 7
@tab 8
@tab 9
@tab A
@tab B
@tab C
@tab D
E
@tab F
@tab G
@tab H
@tab I
@tab J
@tab K
@tab L
M
@tab N
@tab O
@tab P
@tab Q
@tab R
@tab S
@tab T
U
@tab V
@tab W
@tab X
@tab Y
@tab Z
@tab a
@tab b
c
@tab d
@tab e
@tab f
@tab g
@tab h
@tab i
@tab j
k
@tab l
@tab m
@tab n
@tab o
@tab p
@tab q
@tab r
s
@tab t
@tab u
@tab v
@tab w
@tab x
@tab y
@tab z
This encoding scheme is not standard. There are some other encoding
methods which are much more widely used (UU encoding, MIME encoding).
Generally, it is better to use one of these encodings.
Go to the first, previous, next, last section, table of contents.