encodings.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. This file is part of libXMLRPC - a C library for xml-encoded function calls.
  3. Author: Dan Libby (dan@libby.com)
  4. Epinions.com may be contacted at feedback@epinions-inc.com
  5. */
  6. /*
  7. Copyright 2000 Epinions, Inc.
  8. Subject to the following 3 conditions, Epinions, Inc. permits you, free
  9. of charge, to (a) use, copy, distribute, modify, perform and display this
  10. software and associated documentation files (the "Software"), and (b)
  11. permit others to whom the Software is furnished to do so as well.
  12. 1) The above copyright notice and this permission notice shall be included
  13. without modification in all copies or substantial portions of the
  14. Software.
  15. 2) THE SOFTWARE IS PROVIDED "AS IS", WITHOUT ANY WARRANTY OR CONDITION OF
  16. ANY KIND, EXPRESS, IMPLIED OR STATUTORY, INCLUDING WITHOUT LIMITATION ANY
  17. IMPLIED WARRANTIES OF ACCURACY, MERCHANTABILITY, FITNESS FOR A PARTICULAR
  18. PURPOSE OR NONINFRINGEMENT.
  19. 3) IN NO EVENT SHALL EPINIONS, INC. BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES OR LOST PROFITS ARISING OUT
  21. OF OR IN CONNECTION WITH THE SOFTWARE (HOWEVER ARISING, INCLUDING
  22. NEGLIGENCE), EVEN IF EPINIONS, INC. IS AWARE OF THE POSSIBILITY OF SUCH
  23. DAMAGES.
  24. */
  25. #include <php.h>
  26. static const char rcsid[] = "#(@) $Id$";
  27. #include <errno.h>
  28. #include <string.h>
  29. #ifdef HAVE_GICONV_H
  30. #include <giconv.h>
  31. #else
  32. #include <iconv.h>
  33. #endif
  34. #include "encodings.h"
  35. #ifndef ICONV_CSNMAXLEN
  36. #define ICONV_CSNMAXLEN 64
  37. #endif
  38. static char* convert(const char* src, int src_len, int *new_len, const char* from_enc, const char* to_enc) {
  39. char* outbuf = 0;
  40. if(src && src_len && from_enc && to_enc) {
  41. size_t outlenleft = src_len;
  42. size_t inlenleft = src_len;
  43. int outlen = src_len;
  44. iconv_t ic;
  45. char* out_ptr = 0;
  46. if(strlen(to_enc) >= ICONV_CSNMAXLEN || strlen(from_enc) >= ICONV_CSNMAXLEN) {
  47. return NULL;
  48. }
  49. ic = iconv_open(to_enc, from_enc);
  50. if(ic != (iconv_t)-1) {
  51. size_t st;
  52. outbuf = (char*)emalloc(outlen + 1);
  53. out_ptr = (char*)outbuf;
  54. while(inlenleft) {
  55. st = iconv(ic, (char**)&src, &inlenleft, &out_ptr, &outlenleft);
  56. if(st == -1) {
  57. if(errno == E2BIG) {
  58. int diff = out_ptr - outbuf;
  59. outlen += inlenleft;
  60. outlenleft += inlenleft;
  61. outbuf = (char*)erealloc(outbuf, outlen + 1);
  62. out_ptr = outbuf + diff;
  63. }
  64. else {
  65. efree(outbuf);
  66. outbuf = 0;
  67. break;
  68. }
  69. }
  70. }
  71. iconv_close(ic);
  72. }
  73. outlen -= outlenleft;
  74. if(new_len) {
  75. *new_len = outbuf ? outlen : 0;
  76. }
  77. if(outbuf) {
  78. outbuf[outlen] = 0;
  79. }
  80. }
  81. return outbuf;
  82. }
  83. /* returns a new string that must be freed */
  84. char* utf8_encode(const char *s, int len, int *newlen, const char* encoding)
  85. {
  86. return convert(s, len, newlen, encoding, "UTF-8");
  87. }
  88. /* returns a new string, possibly decoded */
  89. char* utf8_decode(const char *s, int len, int *newlen, const char* encoding)
  90. {
  91. return convert(s, len, newlen, "UTF-8", encoding);
  92. }