iconv_open.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /* Get descriptor for character set conversion.
  2. Copyright (C) 1997-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  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. <https://www.gnu.org/licenses/>. */
  15. #include <alloca.h>
  16. #include <errno.h>
  17. #include <iconv.h>
  18. #include <stdbool.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <gconv_int.h>
  22. #include "gconv_charset.h"
  23. iconv_t
  24. iconv_open (const char *tocode, const char *fromcode)
  25. {
  26. __gconv_t cd;
  27. struct gconv_spec conv_spec;
  28. if (__gconv_create_spec (&conv_spec, fromcode, tocode) == NULL)
  29. return (iconv_t) -1;
  30. int res = __gconv_open (&conv_spec, &cd, 0);
  31. __gconv_destroy_spec (&conv_spec);
  32. if (__builtin_expect (res, __GCONV_OK) != __GCONV_OK)
  33. {
  34. /* We must set the error number according to the specs. */
  35. if (res == __GCONV_NOCONV || res == __GCONV_NODB)
  36. __set_errno (EINVAL);
  37. cd = (iconv_t) -1;
  38. }
  39. return (iconv_t) cd;
  40. }