ENCODE(2)                                               ENCODE(2)

     NAME
          dec64, enc64, dec32, enc32, dec16, enc16, dec64x, enc64x,
          dec32x, enc32x, dec64chr, enc64chr, dec32chr, enc32chr,
          dec16chr, enc16chr, encodefmt - encoding byte arrays as
          strings

     SYNOPSIS
          #include <u.h>
          #include <libc.h>

          int  dec64(uchar *out, int lim, char *in, int n)

          int  dec64x(uchar *out, int lim, char *in, int n, int
          (*map)(int))

          int  enc64(char *out, int lim, uchar *in, int n)

          int  enc64x(char *out, int lim, uchar *in, int n, int
          (*map)(int))

          int  dec32(uchar *out, int lim, char *in, int n)

          int  dec32x(uchar *out, int lim, char *in, int n, int
          (*map)(int))

          int  enc32(char *out, int lim, uchar *in, int n)

          int  enc32x(char *out, int lim, uchar *in, int n, int
          (*map)(int))

          int  dec16(uchar *out, int lim, char *in, int n)

          int  enc16(char *out, int lim, uchar *in, int n)

          int  dec64chr(int c)

          int  enc64chr(int o)

          int  dec32chr(int c)

          int  enc32chr(int o)

          int  dec16chr(int c)

          int  enc16chr(int o)

          int  encodefmt(Fmt*)

     DESCRIPTION
          The functions described here handle encoding and decoding of

     Page 1                       Plan 9            (printed 11/18/24)

     ENCODE(2)                                               ENCODE(2)

          bytes to printable ASCII strings as specified by RFC4648.

          Enc16, enc32 and enc64 create null terminated strings.  They
          return the size of the encoded string (without the null) or
          -1 if the encoding fails.  The encoding fails if lim, the
          length of the output buffer (including null), is too small.

          Dec16, dec32 and dec64 return the number of bytes decoded or
          -1 if the decoding fails.  The decoding fails if the output
          buffer is not large enough or, for base 32, if the input
          buffer length is not a multiple of 8.

          Dec16chr, dec32chr and dec64chr return the value for a sym-
          bol of the alphabet or -1 when the symbol is not in the
          alphabet.

          Enc16chr, enc32chr and enc64chr encode a symbol of the
          alphabet given a value.  if the value is out of range then
          zero is returned.

          The enc64x and enc32x variants are identical to the above,
          except that they take a function mapping from an arbitrary
          index in the alphabet to the encoded character.  For exam-
          ple, in the following 32-character alphabet,
          ABCDEFGHIJKLMNOPQRSTUVWXYZ234567
          the chr function would map the value 3 to the character D.

          The dec64x and dec32x variants are similar to the above,
          however the function passed maps from a character within the
          alphabet to the index within the alphabet.

          Encodefmt can be used with fmtinstall(2) and print(2) to
          print encoded representations of byte arrays.  The verbs are

          H    base 16 (i.e. hexadecimal). The default encoding is in
               upper case.  The l flag forces lower case.

          <    base 32. The default is upper case, same as H.

          [    base 64 (same as MIME)

          The length of the array is specified as f2. For example, to
          display a 15 byte array as hex:

              char x[15];

              fmtinstall('H', encodefmt);
              print("%.*H\n", sizeof x, x);

     SOURCE

     Page 2                       Plan 9            (printed 11/18/24)

     ENCODE(2)                                               ENCODE(2)

          /sys/src/libc/port/u16.c
          /sys/src/libc/port/u32.c
          /sys/src/libc/port/u64.c
          /sys/src/libc/port/encodefmt.c

     HISTORY
          In Jan 2018, base 32 encoding was changed from non-standard
          to standard RFC4648 alphabet.

          old: 23456789abcdefghijkmnpqrstuvwxyz

          new: ABCDEFGHIJKLMNOPQRSTUVWXYZ234567

     Page 3                       Plan 9            (printed 11/18/24)