Blame gllib/unistr/u8-mbtoucr.c

rpm-build 858c0f
/* Look at first character in UTF-8 string, returning an error code.
rpm-build 858c0f
   Copyright (C) 1999-2002, 2006-2007, 2009-2017 Free Software Foundation, Inc.
rpm-build 858c0f
   Written by Bruno Haible <bruno@clisp.org>, 2001.
rpm-build 858c0f
rpm-build 858c0f
   This program is free software: you can redistribute it and/or modify it
rpm-build 858c0f
   under the terms of the GNU General Public License as published
rpm-build 858c0f
   by the Free Software Foundation; either version 3 of the License, or
rpm-build 858c0f
   (at your option) any later version.
rpm-build 858c0f
rpm-build 858c0f
   This program is distributed in the hope that it will be useful,
rpm-build 858c0f
   but WITHOUT ANY WARRANTY; without even the implied warranty of
rpm-build 858c0f
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
rpm-build 858c0f
   General Public License for more details.
rpm-build 858c0f
rpm-build 858c0f
   You should have received a copy of the GNU General Public License
rpm-build 858c0f
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
rpm-build 858c0f
rpm-build 858c0f
#include <config.h>
rpm-build 858c0f
rpm-build 858c0f
/* Specification.  */
rpm-build 858c0f
#include "unistr.h"
rpm-build 858c0f
rpm-build 858c0f
int
rpm-build 858c0f
u8_mbtoucr (ucs4_t *puc, const uint8_t *s, size_t n)
rpm-build 858c0f
{
rpm-build 858c0f
  uint8_t c = *s;
rpm-build 858c0f
rpm-build 858c0f
  if (c < 0x80)
rpm-build 858c0f
    {
rpm-build 858c0f
      *puc = c;
rpm-build 858c0f
      return 1;
rpm-build 858c0f
    }
rpm-build 858c0f
  else if (c >= 0xc2)
rpm-build 858c0f
    {
rpm-build 858c0f
      if (c < 0xe0)
rpm-build 858c0f
        {
rpm-build 858c0f
          if (n >= 2)
rpm-build 858c0f
            {
rpm-build 858c0f
              if ((s[1] ^ 0x80) < 0x40)
rpm-build 858c0f
                {
rpm-build 858c0f
                  *puc = ((unsigned int) (c & 0x1f) << 6)
rpm-build 858c0f
                         | (unsigned int) (s[1] ^ 0x80);
rpm-build 858c0f
                  return 2;
rpm-build 858c0f
                }
rpm-build 858c0f
              /* invalid multibyte character */
rpm-build 858c0f
            }
rpm-build 858c0f
          else
rpm-build 858c0f
            {
rpm-build 858c0f
              /* incomplete multibyte character */
rpm-build 858c0f
              *puc = 0xfffd;
rpm-build 858c0f
              return -2;
rpm-build 858c0f
            }
rpm-build 858c0f
        }
rpm-build 858c0f
      else if (c < 0xf0)
rpm-build 858c0f
        {
rpm-build 858c0f
          if (n >= 2)
rpm-build 858c0f
            {
rpm-build 858c0f
              if ((s[1] ^ 0x80) < 0x40
rpm-build 858c0f
                  && (c >= 0xe1 || s[1] >= 0xa0)
rpm-build 858c0f
                  && (c != 0xed || s[1] < 0xa0))
rpm-build 858c0f
                {
rpm-build 858c0f
                  if (n >= 3)
rpm-build 858c0f
                    {
rpm-build 858c0f
                      if ((s[2] ^ 0x80) < 0x40)
rpm-build 858c0f
                        {
rpm-build 858c0f
                          *puc = ((unsigned int) (c & 0x0f) << 12)
rpm-build 858c0f
                                 | ((unsigned int) (s[1] ^ 0x80) << 6)
rpm-build 858c0f
                                 | (unsigned int) (s[2] ^ 0x80);
rpm-build 858c0f
                          return 3;
rpm-build 858c0f
                        }
rpm-build 858c0f
                      /* invalid multibyte character */
rpm-build 858c0f
                    }
rpm-build 858c0f
                  else
rpm-build 858c0f
                    {
rpm-build 858c0f
                      /* incomplete multibyte character */
rpm-build 858c0f
                      *puc = 0xfffd;
rpm-build 858c0f
                      return -2;
rpm-build 858c0f
                    }
rpm-build 858c0f
                }
rpm-build 858c0f
              /* invalid multibyte character */
rpm-build 858c0f
            }
rpm-build 858c0f
          else
rpm-build 858c0f
            {
rpm-build 858c0f
              /* incomplete multibyte character */
rpm-build 858c0f
              *puc = 0xfffd;
rpm-build 858c0f
              return -2;
rpm-build 858c0f
            }
rpm-build 858c0f
        }
rpm-build 858c0f
      else if (c < 0xf8)
rpm-build 858c0f
        {
rpm-build 858c0f
          if (n >= 2)
rpm-build 858c0f
            {
rpm-build 858c0f
              if ((s[1] ^ 0x80) < 0x40
rpm-build 858c0f
                  && (c >= 0xf1 || s[1] >= 0x90)
rpm-build 858c0f
                  && (c < 0xf4 || (c == 0xf4 && s[1] < 0x90)))
rpm-build 858c0f
                {
rpm-build 858c0f
                  if (n >= 3)
rpm-build 858c0f
                    {
rpm-build 858c0f
                      if ((s[2] ^ 0x80) < 0x40)
rpm-build 858c0f
                        {
rpm-build 858c0f
                          if (n >= 4)
rpm-build 858c0f
                            {
rpm-build 858c0f
                              if ((s[3] ^ 0x80) < 0x40)
rpm-build 858c0f
                                {
rpm-build 858c0f
                                  *puc = ((unsigned int) (c & 0x07) << 18)
rpm-build 858c0f
                                         | ((unsigned int) (s[1] ^ 0x80) << 12)
rpm-build 858c0f
                                         | ((unsigned int) (s[2] ^ 0x80) << 6)
rpm-build 858c0f
                                         | (unsigned int) (s[3] ^ 0x80);
rpm-build 858c0f
                                  return 4;
rpm-build 858c0f
                                }
rpm-build 858c0f
                              /* invalid multibyte character */
rpm-build 858c0f
                            }
rpm-build 858c0f
                          else
rpm-build 858c0f
                            {
rpm-build 858c0f
                              /* incomplete multibyte character */
rpm-build 858c0f
                              *puc = 0xfffd;
rpm-build 858c0f
                              return -2;
rpm-build 858c0f
                            }
rpm-build 858c0f
                        }
rpm-build 858c0f
                      /* invalid multibyte character */
rpm-build 858c0f
                    }
rpm-build 858c0f
                  else
rpm-build 858c0f
                    {
rpm-build 858c0f
                      /* incomplete multibyte character */
rpm-build 858c0f
                      *puc = 0xfffd;
rpm-build 858c0f
                      return -2;
rpm-build 858c0f
                    }
rpm-build 858c0f
                }
rpm-build 858c0f
              /* invalid multibyte character */
rpm-build 858c0f
            }
rpm-build 858c0f
          else
rpm-build 858c0f
            {
rpm-build 858c0f
              /* incomplete multibyte character */
rpm-build 858c0f
              *puc = 0xfffd;
rpm-build 858c0f
              return -2;
rpm-build 858c0f
            }
rpm-build 858c0f
        }
rpm-build 858c0f
    }
rpm-build 858c0f
  /* invalid multibyte character */
rpm-build 858c0f
  *puc = 0xfffd;
rpm-build 858c0f
  return -1;
rpm-build 858c0f
}