stringEncodingConverter.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #include <string.h>
  2. #include <stdlib.h>
  3. #if defined(__APPLE__)
  4. #include <CoreFoundation/CoreFoundation.h>
  5. #elif defined(_WIN32)
  6. #include <windows.h>
  7. #else
  8. #include <iconv.h>
  9. #include <errno.h>
  10. #endif
  11. #include "stringEncodingConverter.h"
  12. char *convertShiftJISToUTF8(const char *in, size_t inLength)
  13. {
  14. #if defined(__APPLE__)
  15. CFStringRef inStr = CFStringCreateWithBytes(
  16. NULL,
  17. (const UInt8 *)in,
  18. inLength,
  19. kCFStringEncodingDOSJapanese,
  20. false
  21. );
  22. if (!inStr) return NULL;
  23. CFIndex outLength;
  24. CFRange range = CFRangeMake(0, CFStringGetLength(inStr));
  25. CFStringGetBytes(inStr, range, kCFStringEncodingUTF8, '?', false, NULL, 0, &outLength);
  26. char *out = calloc(outLength+1, 1);
  27. CFStringGetBytes(inStr, range, kCFStringEncodingUTF8, '?', false, (UInt8 *)out, outLength, NULL);
  28. CFRelease(inStr);
  29. return out;
  30. #elif defined(_WIN32)
  31. int wideLength = MultiByteToWideChar(932, 0, in, inLength, NULL, 0);
  32. if (wideLength == 0) return NULL;
  33. LPWSTR inWideStr = (LPWSTR)malloc(wideLength * sizeof(WCHAR));
  34. wideLength = MultiByteToWideChar(932, 0, in, inLength, inWideStr, wideLength);
  35. int outLength = WideCharToMultiByte(CP_UTF8, 0, inWideStr, wideLength, NULL, 0, NULL, NULL);
  36. char *out = calloc(outLength+1, 1);
  37. WideCharToMultiByte(CP_UTF8, 0, inWideStr, wideLength, out, outLength, NULL, NULL);
  38. free(inWideStr);
  39. return out;
  40. #else
  41. size_t inBytesLeft, outBytesLeft;
  42. inBytesLeft = inLength;
  43. outBytesLeft = inBytesLeft*3+1;
  44. char *out = (char *)malloc(outBytesLeft);
  45. char *out_p = out;
  46. iconv_t cd = iconv_open("UTF-8", "CP932");
  47. while (1) {
  48. int ret = iconv(cd, (char **)&in, &inBytesLeft, &out_p, &outBytesLeft);
  49. if (ret != -1) {
  50. break;
  51. }
  52. else if (errno == EILSEQ) {
  53. in += 2;
  54. inBytesLeft -= 2;
  55. *out_p++ = '?';
  56. outBytesLeft--;
  57. }
  58. else break;
  59. }
  60. iconv_close(cd);
  61. if (!out_p) {
  62. free(out);
  63. return NULL;
  64. }
  65. *out_p = 0;
  66. return out;
  67. #endif
  68. }