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

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