encode.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. #include <malloc.h>
  42. #include <string.h>
  43. #include <ldaputil/certmap.h>
  44. #include <ldaputil/encode.h>
  45. /* The magic set of 64 chars in the uuencoded data */
  46. static unsigned char uuset[] = {
  47. 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T',
  48. 'U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n',
  49. 'o','p','q','r','s','t','u','v','w','x','y','z','0','1','2','3','4','5','6','7',
  50. '8','9','+','/' };
  51. static int do_uuencode(unsigned char *src, unsigned char *dst, int srclen)
  52. {
  53. int i, r;
  54. unsigned char *p;
  55. /* To uuencode, we snip 8 bits from 3 bytes and store them as
  56. 6 bits in 4 bytes. 6*4 == 8*3 (get it?) and 6 bits per byte
  57. yields nice clean bytes
  58. It goes like this:
  59. AAAAAAAA BBBBBBBB CCCCCCCC
  60. turns into the standard set of uuencode ascii chars indexed by numbers:
  61. 00AAAAAA 00AABBBB 00BBBBCC 00CCCCCC
  62. Snip-n-shift, snip-n-shift, etc....
  63. */
  64. for (p=dst,i=0; i < srclen; i += 3) {
  65. /* Do 3 bytes of src */
  66. register char b0, b1, b2;
  67. b0 = src[0];
  68. if (i==srclen-1)
  69. b1 = b2 = '\0';
  70. else if (i==srclen-2) {
  71. b1 = src[1];
  72. b2 = '\0';
  73. }
  74. else {
  75. b1 = src[1];
  76. b2 = src[2];
  77. }
  78. *p++ = uuset[b0>>2];
  79. *p++ = uuset[(((b0 & 0x03) << 4) | ((b1 & 0xf0) >> 4))];
  80. *p++ = uuset[(((b1 & 0x0f) << 2) | ((b2 & 0xc0) >> 6))];
  81. *p++ = uuset[b2 & 0x3f];
  82. src += 3;
  83. }
  84. *p = 0; /* terminate the string */
  85. r = (unsigned char *)p - (unsigned char *)dst;/* remember how many we did */
  86. /* Always do 4-for-3, but if not round threesome, have to go
  87. clean up the last extra bytes */
  88. for( ; i != srclen; i--)
  89. *--p = '=';
  90. return r;
  91. }
  92. const unsigned char pr2six[256]={
  93. 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,
  94. 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,62,64,64,64,63,
  95. 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,
  96. 10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,64,64,64,64,64,64,26,27,
  97. 28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,
  98. 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,
  99. 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,
  100. 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,
  101. 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,
  102. 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,
  103. 64,64,64,64,64,64,64,64,64,64,64,64,64
  104. };
  105. static char *_uudecode(const char *bufcoded)
  106. {
  107. register const char *bufin = bufcoded;
  108. register unsigned char *bufout;
  109. register int nprbytes;
  110. unsigned char *bufplain;
  111. int nbytesdecoded;
  112. /* Find the length */
  113. while(pr2six[(int)*(bufin++)] <= 63);
  114. nprbytes = bufin - bufcoded - 1;
  115. nbytesdecoded = ((nprbytes+3)/4) * 3;
  116. bufout = (unsigned char *) malloc(nbytesdecoded + 1);
  117. bufplain = bufout;
  118. bufin = bufcoded;
  119. while (nprbytes > 0) {
  120. *(bufout++) = (unsigned char)
  121. (pr2six[(int)(*bufin)] << 2 | pr2six[(int)bufin[1]] >> 4);
  122. *(bufout++) = (unsigned char)
  123. (pr2six[(int)bufin[1]] << 4 | pr2six[(int)bufin[2]] >> 2);
  124. *(bufout++) = (unsigned char)
  125. (pr2six[(int)bufin[2]] << 6 | pr2six[(int)bufin[3]]);
  126. bufin += 4;
  127. nprbytes -= 4;
  128. }
  129. if(nprbytes & 03) {
  130. if(pr2six[(int)bufin[-2]] > 63)
  131. nbytesdecoded -= 2;
  132. else
  133. nbytesdecoded -= 1;
  134. }
  135. bufplain[nbytesdecoded] = '\0';
  136. return (char *)bufplain;
  137. }
  138. char *dbconf_encodeval (const char *val)
  139. {
  140. int len = strlen(val);
  141. char *dst = (char *)malloc(2*len);
  142. if (dst) {
  143. do_uuencode((unsigned char *)val, (unsigned char *)dst, len);
  144. }
  145. return dst;
  146. }
  147. char *dbconf_decodeval (const char *val)
  148. {
  149. return _uudecode(val);
  150. }