aboutsummaryrefslogtreecommitdiff
path: root/wcsmbs/mbrtoc8.c
blob: e745a49e096457604e9d682167f8b066f47d8ab7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/* Multibyte to UTF-8 conversion.
   Copyright (C) 2022 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <https://www.gnu.org/licenses/>.  */

#include <assert.h>
#include <dlfcn.h>
#include <errno.h>
#include <gconv.h>
#include <uchar.h>
#include <wcsmbsload.h>

#ifndef EILSEQ
# define EILSEQ EINVAL
#endif


/* This is the private state used if PS is NULL.  */
static mbstate_t state;

size_t
mbrtoc8 (char8_t *pc8, const char *s, size_t n, mbstate_t *ps)
{
  /* This implementation depends on the converter invoked by mbrtowc not
     needing to retain state in either the top most bit of ps->__count or
     in ps->__value between invocations.  This implementation uses the
     top most bit of ps->__count to indicate that trailing code units are
     yet to be written and uses ps->__value to store those code units.  */

  if (ps == NULL)
    ps = &state;

  /* If state indicates that trailing code units are yet to be written, write
     those first regardless of whether 's' is a null pointer.  */
  if (ps->__count & 0x80000000)
    {
      /* ps->__value.__wchb[3] stores the index of the next code unit to
         write.  Code units are stored in reverse order.  */
      size_t i = ps->__value.__wchb[3];
      if (pc8 != NULL)
	{
	  *pc8 = ps->__value.__wchb[i];
	}
      if (i == 0)
	{
	  ps->__count &= 0x7fffffff;
	  ps->__value.__wch = 0;
	}
      else
	--ps->__value.__wchb[3];
      return -3;
    }

  if (s == NULL)
    {
      /* if 's' is a null pointer, behave as if a null pointer was passed for
         'pc8', an empty string was passed for 's', and 1 passed for 'n'.  */
      pc8 = NULL;
      s = "";
      n = 1;
    }

  wchar_t wc;
  size_t result;

  result = mbrtowc (&wc, s, n, ps);
  if (result <= n)
    {
      if (wc <= 0x7F)
	{
	  if (pc8 != NULL)
	    *pc8 = wc;
	}
      else if (wc <= 0x7FF)
	{
	  if (pc8 != NULL)
	    *pc8 = 0xC0 + ((wc >> 6) & 0x1F);
	  ps->__value.__wchb[0] = 0x80 + (wc & 0x3F);
	  ps->__value.__wchb[3] = 0;
	  ps->__count |= 0x80000000;
	}
      else if (wc <= 0xFFFF)
	{
	  if (pc8 != NULL)
	    *pc8 = 0xE0 + ((wc >> 12) & 0x0F);
	  ps->__value.__wchb[1] = 0x80 + ((wc >> 6) & 0x3F);
	  ps->__value.__wchb[0] = 0x80 + (wc & 0x3F);
	  ps->__value.__wchb[3] = 1;
	  ps->__count |= 0x80000000;
	}
      else if (wc <= 0x10FFFF)
	{
	  if (pc8 != NULL)
	    *pc8 = 0xF0 + ((wc >> 18) & 0x07);
	  ps->__value.__wchb[2] = 0x80 + ((wc >> 12) & 0x3F);
	  ps->__value.__wchb[1] = 0x80 + ((wc >> 6) & 0x3F);
	  ps->__value.__wchb[0] = 0x80 + (wc & 0x3F);
	  ps->__value.__wchb[3] = 2;
	  ps->__count |= 0x80000000;
	}
    }
  if (result == 0 && wc != 0)
    {
      /* mbrtowc() never returns -3.  When a MB sequence converts to multiple
         WCs, no input is consumed when writing the subsequent WCs resulting
         in a result of 0 even if a null character wasn't written.  */
      result = -3;
    }

  return result;
}