encode.c 4.4 KB

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