encode.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. #pragma GCC diagnostic push
  62. #pragma GCC diagnostic ignored "-Wanalyzer-out-of-bounds"
  63. for (; i != srclen; i--)
  64. *--p = '=';
  65. #pragma GCC diagnostic pop
  66. return r;
  67. }
  68. const unsigned char pr2six[256] = {
  69. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  70. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 64, 64, 63,
  71. 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,
  72. 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 64, 64, 64, 64, 64, 64, 26, 27,
  73. 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,
  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, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  77. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  78. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  79. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64};
  80. static char *
  81. _uudecode(const char *bufcoded)
  82. {
  83. const char *bufin = bufcoded;
  84. unsigned char *bufout;
  85. int nprbytes;
  86. unsigned char *bufplain;
  87. int nbytesdecoded;
  88. /* Find the length */
  89. while (pr2six[(int)*(bufin++)] <= 63)
  90. ;
  91. nprbytes = bufin - bufcoded - 1;
  92. nbytesdecoded = ((nprbytes + 3) / 4) * 3;
  93. bufout = (unsigned char *)malloc(nbytesdecoded + 1);
  94. if (bufout == NULL) {
  95. return NULL;
  96. }
  97. bufplain = bufout;
  98. bufin = bufcoded;
  99. while (nprbytes > 0) {
  100. *(bufout++) = (unsigned char)(pr2six[(int)(*bufin)] << 2 | pr2six[(int)bufin[1]] >> 4);
  101. *(bufout++) = (unsigned char)(pr2six[(int)bufin[1]] << 4 | pr2six[(int)bufin[2]] >> 2);
  102. *(bufout++) = (unsigned char)(pr2six[(int)bufin[2]] << 6 | pr2six[(int)bufin[3]]);
  103. bufin += 4;
  104. nprbytes -= 4;
  105. }
  106. if (nprbytes & 03) {
  107. if (pr2six[(int)bufin[-2]] > 63)
  108. nbytesdecoded -= 2;
  109. else
  110. nbytesdecoded -= 1;
  111. }
  112. bufplain[nbytesdecoded] = '\0';
  113. return (char *)bufplain;
  114. }
  115. char *
  116. dbconf_encodeval(const char *val)
  117. {
  118. int len = strlen(val);
  119. char *dst = (char *)malloc(2 * len);
  120. if (dst) {
  121. do_uuencode((unsigned char *)val, (unsigned char *)dst, len);
  122. }
  123. return dst;
  124. }
  125. char *
  126. dbconf_decodeval(const char *val)
  127. {
  128. return _uudecode(val);
  129. }