Blame lib/unistr/u8-uctomb-aux.c

Packit 8f70b4
/* Conversion UCS-4 to UTF-8.
Packit 8f70b4
   Copyright (C) 2002, 2006-2007, 2009-2018 Free Software Foundation, Inc.
Packit 8f70b4
   Written by Bruno Haible <bruno@clisp.org>, 2002.
Packit 8f70b4
Packit 8f70b4
   This program is free software: you can redistribute it and/or modify it
Packit 8f70b4
   under the terms of the GNU General Public License as published
Packit 8f70b4
   by the Free Software Foundation; either version 3 of the License, or
Packit 8f70b4
   (at your option) any later version.
Packit 8f70b4
Packit 8f70b4
   This program is distributed in the hope that it will be useful,
Packit 8f70b4
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 8f70b4
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 8f70b4
   General Public License for more details.
Packit 8f70b4
Packit 8f70b4
   You should have received a copy of the GNU General Public License
Packit 8f70b4
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
Packit 8f70b4
Packit 8f70b4
#include <config.h>
Packit 8f70b4
Packit 8f70b4
/* Specification.  */
Packit 8f70b4
#include "unistr.h"
Packit 8f70b4
Packit 8f70b4
#ifndef FALLTHROUGH
Packit 8f70b4
# if __GNUC__ < 7
Packit 8f70b4
#  define FALLTHROUGH ((void) 0)
Packit 8f70b4
# else
Packit 8f70b4
#  define FALLTHROUGH __attribute__ ((__fallthrough__))
Packit 8f70b4
# endif
Packit 8f70b4
#endif
Packit 8f70b4
Packit 8f70b4
int
Packit 8f70b4
u8_uctomb_aux (uint8_t *s, ucs4_t uc, int n)
Packit 8f70b4
{
Packit 8f70b4
  int count;
Packit 8f70b4
Packit 8f70b4
  if (uc < 0x80)
Packit 8f70b4
    /* The case n >= 1 is already handled by the caller.  */
Packit 8f70b4
    return -2;
Packit 8f70b4
  else if (uc < 0x800)
Packit 8f70b4
    count = 2;
Packit 8f70b4
  else if (uc < 0x10000)
Packit 8f70b4
    {
Packit 8f70b4
      if (uc < 0xd800 || uc >= 0xe000)
Packit 8f70b4
        count = 3;
Packit 8f70b4
      else
Packit 8f70b4
        return -1;
Packit 8f70b4
    }
Packit 8f70b4
  else if (uc < 0x110000)
Packit 8f70b4
    count = 4;
Packit 8f70b4
  else
Packit 8f70b4
    return -1;
Packit 8f70b4
Packit 8f70b4
  if (n < count)
Packit 8f70b4
    return -2;
Packit 8f70b4
Packit 8f70b4
  switch (count) /* note: code falls through cases! */
Packit 8f70b4
    {
Packit 8f70b4
    case 4: s[3] = 0x80 | (uc & 0x3f); uc = uc >> 6; uc |= 0x10000;
Packit 8f70b4
      FALLTHROUGH;
Packit 8f70b4
    case 3: s[2] = 0x80 | (uc & 0x3f); uc = uc >> 6; uc |= 0x800;
Packit 8f70b4
      FALLTHROUGH;
Packit 8f70b4
    case 2: s[1] = 0x80 | (uc & 0x3f); uc = uc >> 6; uc |= 0xc0;
Packit 8f70b4
  /*case 1:*/ s[0] = uc;
Packit 8f70b4
    }
Packit 8f70b4
  return count;
Packit 8f70b4
}