encode.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. #include <malloc.h>
  39. #include <string.h>
  40. #include <ldaputil/certmap.h>
  41. #include <ldaputil/encode.h>
  42. /* The magic set of 64 chars in the uuencoded data */
  43. static unsigned char uuset[] = {
  44. 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T',
  45. 'U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n',
  46. 'o','p','q','r','s','t','u','v','w','x','y','z','0','1','2','3','4','5','6','7',
  47. '8','9','+','/' };
  48. static int do_uuencode(unsigned char *src, unsigned char *dst, int srclen)
  49. {
  50. int i, r;
  51. unsigned char *p;
  52. /* To uuencode, we snip 8 bits from 3 bytes and store them as
  53. 6 bits in 4 bytes. 6*4 == 8*3 (get it?) and 6 bits per byte
  54. yields nice clean bytes
  55. It goes like this:
  56. AAAAAAAA BBBBBBBB CCCCCCCC
  57. turns into the standard set of uuencode ascii chars indexed by numbers:
  58. 00AAAAAA 00AABBBB 00BBBBCC 00CCCCCC
  59. Snip-n-shift, snip-n-shift, etc....
  60. */
  61. for (p=dst,i=0; i < srclen; i += 3) {
  62. /* Do 3 bytes of src */
  63. register char b0, b1, b2;
  64. b0 = src[0];
  65. if (i==srclen-1)
  66. b1 = b2 = '\0';
  67. else if (i==srclen-2) {
  68. b1 = src[1];
  69. b2 = '\0';
  70. }
  71. else {
  72. b1 = src[1];
  73. b2 = src[2];
  74. }
  75. *p++ = uuset[b0>>2];
  76. *p++ = uuset[(((b0 & 0x03) << 4) | ((b1 & 0xf0) >> 4))];
  77. *p++ = uuset[(((b1 & 0x0f) << 2) | ((b2 & 0xc0) >> 6))];
  78. *p++ = uuset[b2 & 0x3f];
  79. src += 3;
  80. }
  81. *p = 0; /* terminate the string */
  82. r = (unsigned char *)p - (unsigned char *)dst;/* remember how many we did */
  83. /* Always do 4-for-3, but if not round threesome, have to go
  84. clean up the last extra bytes */
  85. for( ; i != srclen; i--)
  86. *--p = '=';
  87. return r;
  88. }
  89. const unsigned char pr2six[256]={
  90. 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,
  91. 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,62,64,64,64,63,
  92. 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,
  93. 10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,64,64,64,64,64,64,26,27,
  94. 28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,
  95. 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,
  96. 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,
  97. 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,
  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
  101. };
  102. static char *_uudecode(const char *bufcoded)
  103. {
  104. register const char *bufin = bufcoded;
  105. register unsigned char *bufout;
  106. register int nprbytes;
  107. unsigned char *bufplain;
  108. int nbytesdecoded;
  109. /* Find the length */
  110. while(pr2six[(int)*(bufin++)] <= 63);
  111. nprbytes = bufin - bufcoded - 1;
  112. nbytesdecoded = ((nprbytes+3)/4) * 3;
  113. bufout = (unsigned char *) malloc(nbytesdecoded + 1);
  114. bufplain = bufout;
  115. bufin = bufcoded;
  116. while (nprbytes > 0) {
  117. *(bufout++) = (unsigned char)
  118. (pr2six[(int)(*bufin)] << 2 | pr2six[(int)bufin[1]] >> 4);
  119. *(bufout++) = (unsigned char)
  120. (pr2six[(int)bufin[1]] << 4 | pr2six[(int)bufin[2]] >> 2);
  121. *(bufout++) = (unsigned char)
  122. (pr2six[(int)bufin[2]] << 6 | pr2six[(int)bufin[3]]);
  123. bufin += 4;
  124. nprbytes -= 4;
  125. }
  126. if(nprbytes & 03) {
  127. if(pr2six[(int)bufin[-2]] > 63)
  128. nbytesdecoded -= 2;
  129. else
  130. nbytesdecoded -= 1;
  131. }
  132. bufplain[nbytesdecoded] = '\0';
  133. return (char *)bufplain;
  134. }
  135. char *dbconf_encodeval (const char *val)
  136. {
  137. int len = strlen(val);
  138. char *dst = (char *)malloc(2*len);
  139. if (dst) {
  140. do_uuencode((unsigned char *)val, (unsigned char *)dst, len);
  141. }
  142. return dst;
  143. }
  144. char *dbconf_decodeval (const char *val)
  145. {
  146. return _uudecode(val);
  147. }