tst-iconv5.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /* Copyright (C) 2004-2016 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by GOTO Masanori <gotom@debian.or.jp>, 2004
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <string.h>
  16. #include <stdlib.h>
  17. #include <stdio.h>
  18. #include <errno.h>
  19. #if defined __UCLIBC_HAS_LIBICONV__ || (defined(__GLIBC__) && !defined __UCLIBC__)
  20. #include <iconv.h>
  21. #define SIZE 256 /* enough room for conversion */
  22. #define SAMPLESTR "abc"
  23. struct unalign
  24. {
  25. char str1[1];
  26. char str2[SIZE];
  27. };
  28. struct convcode
  29. {
  30. const char *tocode;
  31. const char *fromcode;
  32. };
  33. /* test builtin transformation */
  34. static const struct convcode testcode[] = {
  35. #if defined __UCLIBC_HAS_LIBICONV__ || (defined(__GLIBC__) && !defined __UCLIBC__)
  36. {"ASCII", "ASCII"},
  37. {"UTF-8", "ASCII"},
  38. #endif
  39. #if defined(__GLIBC__) && !defined __UCLIBC__
  40. {"UCS-2BE", "ASCII"},
  41. {"UCS-2LE", "ASCII"},
  42. {"UCS-4BE", "ASCII"},
  43. {"UCS-4LE", "ASCII"},
  44. #endif
  45. };
  46. static const int number = (int) sizeof (testcode) / sizeof (struct convcode);
  47. static int
  48. convert (const char *tocode, const char *fromcode, char *inbufp,
  49. size_t inbytesleft, char *outbufp, size_t outbytesleft)
  50. {
  51. iconv_t *ic;
  52. size_t outbytes = outbytesleft;
  53. int ret;
  54. ic = iconv_open (tocode, fromcode);
  55. if (ic == (iconv_t *) - 1)
  56. {
  57. printf ("iconv_open failed: from: %s, to: %s: %s",
  58. fromcode, tocode, strerror (errno));
  59. return -1;
  60. }
  61. while (inbytesleft > 0)
  62. {
  63. ret = iconv (ic, &inbufp, &inbytesleft, &outbufp, &outbytes);
  64. if (ret == -1)
  65. {
  66. printf ("iconv failed: from: %s, to: %s: %s",
  67. fromcode, tocode, strerror (errno));
  68. return -1;
  69. }
  70. }
  71. ret = iconv_close (ic);
  72. if (ret == -1)
  73. {
  74. printf ("iconv_close failed: from: %s, to: %s: %s",
  75. fromcode, tocode, strerror (errno));
  76. return -1;
  77. }
  78. return outbytesleft - outbytes;
  79. }
  80. static int
  81. test_unalign (const struct convcode *codes, const char *str, int len)
  82. {
  83. struct unalign *inbufp, *outbufp;
  84. char *inbuf, *outbuf;
  85. size_t inbytesleft, outbytesleft;
  86. int retlen;
  87. /* allocating unaligned buffer for both inbuf and outbuf */
  88. inbufp = (struct unalign *) malloc (sizeof (struct unalign));
  89. if (!inbufp)
  90. {
  91. printf ("no memory available\n");
  92. exit (1);
  93. }
  94. inbuf = inbufp->str2;
  95. outbufp = (struct unalign *) malloc (sizeof (struct unalign));
  96. if (!outbufp)
  97. {
  98. printf ("no memory available\n");
  99. exit (1);
  100. }
  101. outbuf = outbufp->str2;
  102. /* first iconv phase */
  103. memcpy (inbuf, str, len);
  104. inbytesleft = len;
  105. outbytesleft = sizeof (struct unalign);
  106. retlen = convert (codes->tocode, codes->fromcode, inbuf, inbytesleft,
  107. outbuf, outbytesleft);
  108. if (retlen == -1) /* failed */
  109. return 1;
  110. /* second round trip iconv phase */
  111. memcpy (inbuf, outbuf, retlen);
  112. inbytesleft = retlen;
  113. outbytesleft = sizeof (struct unalign);
  114. retlen = convert (codes->fromcode, codes->tocode, inbuf, inbytesleft,
  115. outbuf, outbytesleft);
  116. if (retlen == -1) /* failed */
  117. return 1;
  118. free (inbufp);
  119. free (outbufp);
  120. return 0;
  121. }
  122. #endif
  123. static int
  124. do_test (void)
  125. {
  126. #if defined __UCLIBC_HAS_LIBICONV__ || (defined(__GLIBC__) && !defined __UCLIBC__)
  127. int i;
  128. int ret = 0;
  129. for (i = 0; i < number; i++)
  130. {
  131. ret = test_unalign (&testcode[i], (char *) SAMPLESTR, sizeof (SAMPLESTR));
  132. if (ret)
  133. break;
  134. printf ("iconv: %s <-> %s: ok\n",
  135. testcode[i].fromcode, testcode[i].tocode);
  136. }
  137. if (ret == 0)
  138. printf ("Succeeded.\n");
  139. return ret;
  140. #else
  141. return 23;
  142. #endif
  143. }
  144. #define TEST_FUNCTION do_test ()
  145. #include "../test-skeleton.c"