encode.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2021 Red Hat, Inc.
  4. * All rights reserved.
  5. *
  6. * License: GPL (version 3 or any later version).
  7. * See LICENSE for details.
  8. * END COPYRIGHT BLOCK **/
  9. #ifdef HAVE_CONFIG_H
  10. #include <config.h>
  11. #endif
  12. /* This was malloc.h - but it's moved to stdlib.h on most platforms, and FBSD is strict */
  13. /* Make it stdlib.h, and revert to malloc.h with ifdefs if we have issues here. WB 2016 */
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <ldaputil/certmap.h>
  17. #include <ldaputil/encode.h>
  18. /* The magic set of 64 chars in the uuencoded data */
  19. static unsigned char uuset[] = {
  20. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
  21. 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
  22. 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7',
  23. '8', '9', '+', '/'};
  24. static int
  25. do_uuencode(unsigned char *src, unsigned char *dst, int srclen)
  26. {
  27. int i, r;
  28. unsigned char *p;
  29. /* To uuencode, we snip 8 bits from 3 bytes and store them as
  30. 6 bits in 4 bytes. 6*4 == 8*3 (get it?) and 6 bits per byte
  31. yields nice clean bytes
  32. It goes like this:
  33. AAAAAAAA BBBBBBBB CCCCCCCC
  34. turns into the standard set of uuencode ascii chars indexed by numbers:
  35. 00AAAAAA 00AABBBB 00BBBBCC 00CCCCCC
  36. Snip-n-shift, snip-n-shift, etc....
  37. */
  38. for (p = dst, i = 0; i < srclen; i += 3) {
  39. /* Do 3 bytes of src */
  40. char b0, b1, b2;
  41. b0 = src[0];
  42. if (i == srclen - 1)
  43. b1 = b2 = '\0';
  44. else if (i == srclen - 2) {
  45. b1 = src[1];
  46. b2 = '\0';
  47. } else {
  48. b1 = src[1];
  49. b2 = src[2];
  50. }
  51. *p++ = uuset[b0 >> 2];
  52. *p++ = uuset[(((b0 & 0x03) << 4) | ((b1 & 0xf0) >> 4))];
  53. *p++ = uuset[(((b1 & 0x0f) << 2) | ((b2 & 0xc0) >> 6))];
  54. *p++ = uuset[b2 & 0x3f];
  55. src += 3;
  56. }
  57. *p = 0; /* terminate the string */
  58. r = (unsigned char *)p - (unsigned char *)dst; /* remember how many we did */
  59. /* Always do 4-for-3, but if not round threesome, have to go
  60. clean up the last extra bytes */
  61. for (; i != srclen; i--)
  62. *--p = '=';
  63. return r;
  64. }
  65. const unsigned char pr2six[256] = {
  66. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  67. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 64, 64, 63,
  68. 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 64, 64, 64, 64, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
  69. 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 64, 64, 64, 64, 64, 64, 26, 27,
  70. 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,
  71. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  72. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  73. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  74. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  75. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  76. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64};
  77. static char *
  78. _uudecode(const char *bufcoded)
  79. {
  80. const char *bufin = bufcoded;
  81. unsigned char *bufout;
  82. int nprbytes;
  83. unsigned char *bufplain;
  84. int nbytesdecoded;
  85. /* Find the length */
  86. while (pr2six[(int)*(bufin++)] <= 63)
  87. ;
  88. nprbytes = bufin - bufcoded - 1;
  89. nbytesdecoded = ((nprbytes + 3) / 4) * 3;
  90. bufout = (unsigned char *)malloc(nbytesdecoded + 1);
  91. bufplain = bufout;
  92. bufin = bufcoded;
  93. while (nprbytes > 0) {
  94. *(bufout++) = (unsigned char)(pr2six[(int)(*bufin)] << 2 | pr2six[(int)bufin[1]] >> 4);
  95. *(bufout++) = (unsigned char)(pr2six[(int)bufin[1]] << 4 | pr2six[(int)bufin[2]] >> 2);
  96. *(bufout++) = (unsigned char)(pr2six[(int)bufin[2]] << 6 | pr2six[(int)bufin[3]]);
  97. bufin += 4;
  98. nprbytes -= 4;
  99. }
  100. if (nprbytes & 03) {
  101. if (pr2six[(int)bufin[-2]] > 63)
  102. nbytesdecoded -= 2;
  103. else
  104. nbytesdecoded -= 1;
  105. }
  106. bufplain[nbytesdecoded] = '\0';
  107. return (char *)bufplain;
  108. }
  109. char *
  110. dbconf_encodeval(const char *val)
  111. {
  112. int len = strlen(val);
  113. char *dst = (char *)malloc(2 * len);
  114. if (dst) {
  115. do_uuencode((unsigned char *)val, (unsigned char *)dst, len);
  116. }
  117. return dst;
  118. }
  119. char *
  120. dbconf_decodeval(const char *val)
  121. {
  122. return _uudecode(val);
  123. }