base64.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * base64 encoding / decoding
  3. *
  4. * $Id$
  5. *
  6. * Copyright (C) 2001-2003 FhG Fokus
  7. *
  8. * This file is part of ser, a free SIP server.
  9. *
  10. * ser is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version
  14. *
  15. * For a license to use the ser software under conditions
  16. * other than those described here, or to purchase support for this
  17. * software, please contact iptel.org by e-mail at the following addresses:
  18. * [email protected]
  19. *
  20. * ser is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  28. */
  29. void base64decode(char* src_buf, int src_len, char* tgt_buf, int* tgt_len) {
  30. int pos, i, n;
  31. unsigned char c[4];
  32. for (pos=0, i=0, *tgt_len=0; pos < src_len; pos++) {
  33. if (src_buf[pos] >= 'A' && src_buf[pos] <= 'Z')
  34. c[i] = src_buf[pos] - 65; /* <65..90> --> <0..25> */
  35. else if (src_buf[pos] >= 'a' && src_buf[pos] <= 'z')
  36. c[i] = src_buf[pos] - 71; /* <97..122> --> <26..51> */
  37. else if (src_buf[pos] >= '0' && src_buf[pos] <= '9')
  38. c[i] = src_buf[pos] + 4; /* <48..56> --> <52..61> */
  39. else if (src_buf[pos] == '+')
  40. c[i] = 62;
  41. else if (src_buf[pos] == '/')
  42. c[i] = 63;
  43. else /* '=' */
  44. c[i] = 64;
  45. i++;
  46. if (pos == src_len-1) {
  47. while (i < 4) {
  48. c[i] = 64;
  49. i++;
  50. }
  51. }
  52. if (i==4) {
  53. if (c[0] == 64)
  54. n = 0;
  55. else if (c[2] == 64)
  56. n = 1;
  57. else if (c[3] == 64)
  58. n = 2;
  59. else
  60. n = 3;
  61. switch (n) {
  62. case 3:
  63. tgt_buf[*tgt_len+2] = (char) (((c[2] & 0x03) << 6) | c[3]);
  64. /* no break */
  65. case 2:
  66. tgt_buf[*tgt_len+1] = (char) (((c[1] & 0x0F) << 4) | (c[2] >> 2));
  67. /* no break */
  68. case 1:
  69. tgt_buf[*tgt_len+0] = (char) ((c[0] << 2) | (c[1] >> 4));
  70. break;
  71. }
  72. i=0;
  73. *tgt_len+= n;
  74. }
  75. }
  76. }
  77. void base64encode(char* src_buf, int src_len, char* tgt_buf, int* tgt_len, int quoted) {
  78. static char code64[64+1] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  79. int pos;
  80. for (pos=0, *tgt_len=0; pos < src_len; pos+=3,*tgt_len+=4) {
  81. tgt_buf[*tgt_len+0] = code64[(unsigned char)src_buf[pos+0] >> 2];
  82. tgt_buf[*tgt_len+1] = code64[(((unsigned char)src_buf[pos+0] & 0x03) << 4) | ((pos+1 < src_len)?((unsigned char)src_buf[pos+1] >> 4):0)];
  83. if (pos+1 < src_len)
  84. tgt_buf[*tgt_len+2] = code64[(((unsigned char)src_buf[pos+1] & 0x0F) << 2) | ((pos+2 < src_len)?((unsigned char)src_buf[pos+2] >> 6):0)];
  85. else {
  86. if(!quoted)
  87. (*tgt_len)--;
  88. else
  89. /* this data is going to be quoted */
  90. tgt_buf[*tgt_len+2] = '=';
  91. }
  92. if (pos+2 < src_len)
  93. tgt_buf[*tgt_len+3] = code64[(unsigned char)src_buf[pos+2] & 0x3F];
  94. else {
  95. if(!quoted)
  96. (*tgt_len)--;
  97. else
  98. tgt_buf[*tgt_len+3] = '=';
  99. }
  100. }
  101. }