1
0

encode 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. .TH ENCODE 2
  2. .EQ
  3. delim $$
  4. .EN
  5. .SH NAME
  6. dec64, enc64, dec32, enc32, dec16, enc16, encodefmt \- encoding byte arrays as strings
  7. .SH SYNOPSIS
  8. .B #include <u.h>
  9. .br
  10. .B #include <libc.h>
  11. .PP
  12. .B
  13. int dec64(uchar *out, int lim, char *in, int n)
  14. .PP
  15. .B
  16. int enc64(char *out, int lim, uchar *in, int n)
  17. .PP
  18. .B
  19. int dec32(uchar *out, int lim, char *in, int n)
  20. .PP
  21. .B
  22. int enc32(char *out, int lim, uchar *in, int n)
  23. .PP
  24. .B
  25. int dec16(uchar *out, int lim, char *in, int n)
  26. .PP
  27. .B
  28. int enc16(char *out, int lim, uchar *in, int n)
  29. .PP
  30. .B
  31. int encodefmt(Fmt*)
  32. .SH DESCRIPTION
  33. .IR Enc16 ,
  34. .I enc32
  35. and
  36. .I enc64
  37. create null terminated strings. They return the size of the
  38. encoded string (without the null) or -1 if the encoding fails.
  39. The encoding fails if
  40. .IR lim ,
  41. the length of the output buffer, is too small.
  42. They require
  43. $2 n + 1$, ${ 8 n + 4 } over 5 + 1$ and
  44. $4 { { n + 2 } over 3 } + 1$ bytes,
  45. respectively.
  46. .PP
  47. .IR Dec16 ,
  48. .I dec32
  49. and
  50. .I dec64
  51. return the number of bytes decoded or -1 if the decoding fails.
  52. The decoding fails if the output buffer is not large enough or,
  53. for base 32, if the input buffer length is not a multiple
  54. of 8.
  55. .PP
  56. .I Encodefmt
  57. can be used with
  58. .IR fmtinstall (2)
  59. and
  60. .IR print (2)
  61. to print encoded representations of byte arrays.
  62. The verbs are
  63. .TP 3
  64. .B H
  65. base 16 (i.e. hexadecimal). The default encoding is
  66. in upper case. The
  67. .B l
  68. flag forces lower case.
  69. .TP
  70. .B <
  71. base 32
  72. .TP
  73. .B [
  74. base 64 (same as MIME)
  75. .PD
  76. .PP
  77. The length of the array is specified as
  78. .IR f2 .
  79. For example, to display a 15 byte array as hex:
  80. .IP
  81. .EX
  82. char x[15];
  83. fmtinstall('H', encodefmt);
  84. print("%.*H\\n", sizeof x, x);
  85. .EE
  86. .SH SOURCE
  87. .B /sys/src/libc/port/u[136][246].c