The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/fs/unicode.h

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /* $NetBSD: unicode.h,v 1.2 2004/12/27 18:14:36 jdolecek Exp $ */
    2 
    3 /*-
    4  * Copyright (c) 2001, 2004 The NetBSD Foundation, Inc.
    5  * All rights reserved.
    6  *
    7  * Redistribution and use in source and binary forms, with or without
    8  * modification, are permitted provided that the following conditions
    9  * are met:
   10  * 1. Redistributions of source code must retain the above copyright
   11  *    notice, this list of conditions and the following disclaimer.
   12  * 2. Redistributions in binary form must reproduce the above copyright
   13  *    notice, this list of conditions and the following disclaimer in the
   14  *    documentation and/or other materials provided with the distribution.
   15  * 3. All advertising materials mentioning features or use of this software
   16  *    must display the following acknowledgement:
   17  *        This product includes software developed by the NetBSD
   18  *        Foundation, Inc. and its contributors.
   19  * 4. Neither the name of The NetBSD Foundation nor the names of its
   20  *    contributors may be used to endorse or promote products derived
   21  *    from this software without specific prior written permission.
   22  *
   23  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
   24  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
   25  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
   26  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
   27  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
   31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   33  * POSSIBILITY OF SUCH DAMAGE.
   34  */
   35 
   36 /*
   37  * Routines for handling Unicode encoded in UTF-8 form, code derived from
   38  * src/lib/libc/locale/utf2.c.
   39  */
   40 
   41 /*
   42  * Read one UTF8-encoded character off the string, shift the string pointer
   43  * and return the character.
   44  */
   45 static u_int16_t
   46 wget_utf8(const char **str, size_t *sz)
   47 {
   48         int c;
   49         u_int16_t rune = 0;
   50         const char *s = *str;
   51         static const int _utf_count[16] = {
   52                 1, 1, 1, 1, 1, 1, 1, 1,
   53                 0, 0, 0, 0, 2, 2, 3, 0,
   54         };
   55 
   56         /* must be called with at least one byte remaining */
   57         KASSERT(*sz > 0);
   58 
   59         c = _utf_count[(s[0] & 0xf0) >> 4];
   60         if (c == 0 || c > *sz) {
   61     decoding_error:
   62                 /*
   63                  * The first character is in range 128-255 and doesn't
   64                  * mark valid a valid UTF-8 sequence. There is not much
   65                  * we can do with this, so handle by returning
   66                  * the first character as if it would be a correctly
   67                  * encoded ISO-8859-1 character.
   68                  */
   69                 c = 1;
   70         }
   71 
   72         switch (c) {
   73         case 1:
   74                 rune = s[0] & 0xff;
   75                 break;
   76         case 2:
   77                 if ((s[1] & 0xc0) != 0x80)
   78                         goto decoding_error;
   79                 rune = ((s[0] & 0x1F) << 6) | (s[1] & 0x3F);
   80                 break;
   81         case 3:
   82                 if ((s[1] & 0xC0) != 0x80 || (s[2] & 0xC0) != 0x80)
   83                         goto decoding_error;
   84                 rune = ((s[0] & 0x1F) << 12) | ((s[1] & 0x3F) << 6)
   85                     | (s[2] & 0x3F);
   86                 break;
   87         }
   88 
   89         *str += c;
   90         *sz -= c;
   91         return rune;
   92 }
   93 
   94 /*
   95  * Encode wide character and write it to the string. 'n' specifies
   96  * how much buffer space remains in 's'. Returns number of bytes written
   97  * to the target string 's'.
   98  */
   99 static int
  100 wput_utf8(char *s, size_t n, u_int16_t wc)
  101 {
  102         if (wc & 0xf800) {
  103                 if (n < 3) {
  104                         /* bound check failure */
  105                         return 0;
  106                 }
  107 
  108                 s[0] = 0xE0 | ((wc >> 12) & 0x0F);
  109                 s[1] = 0x80 | ((wc >> 6) & 0x3F);
  110                 s[2] = 0x80 | ((wc) & 0x3F);
  111                 return 3;
  112         } else if (wc & 0x0780) {
  113                 if (n < 2) {
  114                         /* bound check failure */
  115                         return 0;
  116                 }
  117 
  118                 s[0] = 0xC0 | ((wc >> 6) & 0x1F);
  119                 s[1] = 0x80 | ((wc) & 0x3F);
  120                 return 2;
  121         } else {
  122                 if (n < 1) {
  123                         /* bound check failure */
  124                         return 0;
  125                 }
  126 
  127                 s[0] = wc;
  128                 return 1;
  129         }
  130 }

Cache object: ca7cb02cb77e18453560e29e464501c1


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.