?login_element?

Subversion Repositories NedoOS

Rev

Blame | Last modification | View Log | Download

  1. /*-------------------------------------------------------------------------
  2.    c32rtomb.c - convert UTF-16 to UTF-8
  3.  
  4.    Copyright (C) 2016, Philipp Klaus Krause, pkk@spth.de
  5.  
  6.    This library is free software; you can redistribute it and/or modify it
  7.    under the terms of the GNU General Public License as published by the
  8.    Free Software Foundation; either version 2, or (at your option) any
  9.    later version.
  10.  
  11.    This library is distributed in the hope that it will be useful,
  12.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.    GNU General Public License for more details.
  15.  
  16.    You should have received a copy of the GNU General Public License
  17.    along with this library; see the file COPYING. If not, write to the
  18.    Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
  19.    MA 02110-1301, USA.
  20.  
  21.    As a special exception, if you link this library with other files,
  22.    some of which are compiled with SDCC, to produce an executable,
  23.    this library does not by itself cause the resulting executable to
  24.    be covered by the GNU General Public License. This exception does
  25.    not however invalidate any other reasons why the executable file
  26.    might be covered by the GNU General Public License.
  27. -------------------------------------------------------------------------*/
  28.  
  29. #include <uchar.h>
  30.  
  31. #include <stdlib.h>
  32. #include <limits.h>
  33. #include <errno.h>
  34.  
  35. size_t c16rtomb(char *restrict s, char16_t c16, mbstate_t *restrict ps)
  36. {
  37.         wchar_t codepoint;
  38.         char buf[MB_LEN_MAX];
  39.         static mbstate_t sps;
  40.  
  41.         if(!s)
  42.                 s = buf;
  43.  
  44.         if(!ps)
  45.                 ps = &sps;
  46.  
  47.         if (!c16) // 0 always resets conversion state.
  48.         {
  49.                 ps->c[1] = ps->c[2] = 0;
  50.                 codepoint = 0;
  51.         }
  52.         else if(ps->c[1] || ps->c[2]) // We already have the high surrogate. Now get the low surrogate
  53.         {
  54.                 char16_t high_surrogate;
  55.  
  56.                 if(c16 < 0xdc00 || c16 > 0xdfff)
  57.                         goto eilseq;
  58.  
  59.                 high_surrogate = ps->c[1] + (ps->c[2] << 8);
  60.                 ps->c[1] = ps->c[2] = 0;
  61.                 codepoint = (high_surrogate << 10) - (0xd800 << 10) + c16 - 0xdc00 + 0x10000;
  62.         }
  63.         else if(c16 < 0xd7ff || c16 >= 0xe000) // Basic multilingual plane.
  64.                 codepoint = c16;
  65.         else // Get the high surrogate
  66.         {
  67.                 if(c16 > 0xdbff)
  68.                         goto eilseq;
  69.  
  70.                 ps->c[1] = c16 & 0xff;
  71.                 ps->c[2] = c16 >> 8;
  72.                 return(0);
  73.         }
  74.  
  75.         return(wctomb(s, codepoint));
  76.  
  77. eilseq:
  78.         errno = EILSEQ;
  79.         return(-1);
  80. }
  81.  
  82.