nls_utf8.c 618 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. /*
  2. * Copyright (c) 2020 Actions Corporation.
  3. * Copy from Linux
  4. *
  5. * SPDX-License-Identifier: Apache-2.0
  6. */
  7. #include <errno.h>
  8. #include "nls.h"
  9. int nls_utf8_uni2char(u16_t uni, u8_t *out, int boundlen)
  10. {
  11. int n;
  12. if (boundlen <= 0)
  13. return -ENAMETOOLONG;
  14. n = utf32_to_utf8(uni, out, boundlen);
  15. if (n < 0) {
  16. *out = '?';
  17. return -EINVAL;
  18. }
  19. return n;
  20. }
  21. int nls_utf8_char2uni(const u8_t *rawstring, int boundlen, u16_t *uni)
  22. {
  23. int n;
  24. unicode_t u;
  25. n = utf8_to_utf32(rawstring, boundlen, &u);
  26. if (n < 0 || u > MAX_WCHAR_T) {
  27. *uni = 0x003f; /* ? */
  28. return -EINVAL;
  29. }
  30. *uni = (u16_t) u;
  31. return n;
  32. }