173 lines · 3.3 KB · Text
RawDownload
.TH ENCODE 2.SH NAMEdec64, enc64, dec32, enc32, dec16, enc16, \dec64x, enc64x, dec32x, enc32x, \dec64chr, enc64chr, dec32chr, enc32chr, dec16chr, enc16chr, \encodefmt \- encoding byte arrays as strings.SH SYNOPSIS.B #include <u.h>.br.B #include <libc.h>.PP.Bint	dec64(uchar *out, int lim, char *in, int n).PP.Bint	dec64x(uchar *out, int lim, char *in, int n, int (*map)(int)).PP.Bint	enc64(char *out, int lim, uchar *in, int n).PP.Bint	enc64x(char *out, int lim, uchar *in, int n, int (*map)(int)).PP.Bint	dec32(uchar *out, int lim, char *in, int n).PP.Bint	dec32x(uchar *out, int lim, char *in, int n, int (*map)(int)).PP.Bint	enc32(char *out, int lim, uchar *in, int n).PP.Bint	enc32x(char *out, int lim, uchar *in, int n, int (*map)(int)).PP.Bint	dec16(uchar *out, int lim, char *in, int n).PP.Bint	enc16(char *out, int lim, uchar *in, int n).PP.Bint	dec64chr(int c).PP.Bint	enc64chr(int o).PP.Bint	dec32chr(int c).PP.Bint	enc32chr(int o).PP.Bint	dec16chr(int c).PP.Bint	enc16chr(int o).PP.Bint	encodefmt(Fmt*).SH DESCRIPTIONThe functions described here handle encoding and decoding ofbytes to printable ASCII strings as specified by RFC4648. .PP.IR Enc16 ,.I enc32and.I enc64create null terminated strings.  They return the size of theencoded string (without the null) or -1 if the encoding fails.The encoding fails if.IR lim ,the length of the output buffer (including null), is too small..PP.IR Dec16 ,.I dec32and.I dec64return 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 multipleof 8..PP.IR Dec16chr ,.I dec32chrand.I dec64chrreturn the value for a symbol of the alphabet or -1 when thesymbol is not in the alphabet..PP.IR Enc16chr ,.I enc32chrand.I enc64chrencode a symbol of the alphabet given a value.if the value is out of range then zero is returned..PPThe.I enc64xand.I enc32xvariants are identical to the above, except that they take afunction mapping from an arbitrary index in the alphabet tothe encoded character.For example, in the following 32-character alphabet,.EX.I ABCDEFGHIJKLMNOPQRSTUVWXYZ234567.EEthe chr function would map the value.I 3to the character.IR D . The.I dec64xand.I dec32xvariants are similar to the above, however the function passedmaps from a character within the alphabet to the index withinthe alphabet. .PP.I Encodefmtcan be used with.IR fmtinstall (2)and.IR print (2)to print encoded representations of byte arrays.The verbs are.TP.B Hbase 16 (i.e. hexadecimal). The default encoding isin upper case.  The.B lflag forces lower case..TP.B <base 32. The default is upper case, same as.BR H ..TP.B [base 64 (same as MIME).PD.PPThe length of the array is specified as.IR f2 .For example, to display a 15 byte array as hex:.EX     char x[15];     fmtinstall('H', encodefmt);    print("%.*H\\n", sizeof x, x); .EE.SH SOURCE.B /sys/src/libc/port/u16.c.br.B /sys/src/libc/port/u32.c.br.B /sys/src/libc/port/u64.c.br.B /sys/src/libc/port/encodefmt.c.SH HISTORYIn Jan 2018, base 32 encoding was changed from non-standardto standard RFC4648 alphabet..TPold:.B "23456789abcdefghijkmnpqrstuvwxyz".TPnew:.B "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"