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.4 2006/05/20 08:28:27 yamt 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  * Copyright (c) 1993
   38  *      The Regents of the University of California.  All rights reserved.
   39  *
   40  * This code is derived from software contributed to Berkeley by
   41  * Paul Borman at Krystal Technologies.
   42  *
   43  * Redistribution and use in source and binary forms, with or without
   44  * modification, are permitted provided that the following conditions
   45  * are met:
   46  * 1. Redistributions of source code must retain the above copyright
   47  *    notice, this list of conditions and the following disclaimer.
   48  * 2. Redistributions in binary form must reproduce the above copyright
   49  *    notice, this list of conditions and the following disclaimer in the
   50  *    documentation and/or other materials provided with the distribution.
   51  * 3. All advertising materials mentioning features or use of this software
   52  *    must display the following acknowledgement:
   53  *      This product includes software developed by the University of
   54  *      California, Berkeley and its contributors.
   55  * 4. Neither the name of the University nor the names of its contributors
   56  *    may be used to endorse or promote products derived from this software
   57  *    without specific prior written permission.
   58  *
   59  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   60  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   61  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   62  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   63  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   64  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   65  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   66  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   67  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   68  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   69  * SUCH DAMAGE.
   70  */
   71 
   72 /*
   73  * Routines for handling Unicode encoded in UTF-8 form, code derived from
   74  * src/lib/libc/locale/utf2.c.
   75  */
   76 
   77 /*
   78  * Read one UTF8-encoded character off the string, shift the string pointer
   79  * and return the character.
   80  */
   81 static u_int16_t
   82 wget_utf8(const char **str, size_t *sz)
   83 {
   84         int c;
   85         u_int16_t rune = 0;
   86         const char *s = *str;
   87         static const int _utf_count[16] = {
   88                 1, 1, 1, 1, 1, 1, 1, 1,
   89                 0, 0, 0, 0, 2, 2, 3, 0,
   90         };
   91 
   92         /* must be called with at least one byte remaining */
   93         KASSERT(*sz > 0);
   94 
   95         c = _utf_count[(s[0] & 0xf0) >> 4];
   96         if (c == 0 || c > *sz) {
   97     decoding_error:
   98                 /*
   99                  * The first character is in range 128-255 and doesn't
  100                  * mark valid a valid UTF-8 sequence. There is not much
  101                  * we can do with this, so handle by returning
  102                  * the first character as if it would be a correctly
  103                  * encoded ISO-8859-1 character.
  104                  */
  105                 c = 1;
  106         }
  107 
  108         switch (c) {
  109         case 1:
  110                 rune = s[0] & 0xff;
  111                 break;
  112         case 2:
  113                 if ((s[1] & 0xc0) != 0x80)
  114                         goto decoding_error;
  115                 rune = ((s[0] & 0x1F) << 6) | (s[1] & 0x3F);
  116                 break;
  117         case 3:
  118                 if ((s[1] & 0xC0) != 0x80 || (s[2] & 0xC0) != 0x80)
  119                         goto decoding_error;
  120                 rune = ((s[0] & 0x1F) << 12) | ((s[1] & 0x3F) << 6)
  121                     | (s[2] & 0x3F);
  122                 break;
  123         }
  124 
  125         *str += c;
  126         *sz -= c;
  127         return rune;
  128 }
  129 
  130 /*
  131  * Encode wide character and write it to the string. 'n' specifies
  132  * how much buffer space remains in 's'. Returns number of bytes written
  133  * to the target string 's'.
  134  */
  135 static int
  136 wput_utf8(char *s, size_t n, u_int16_t wc)
  137 {
  138         if (wc & 0xf800) {
  139                 if (n < 3) {
  140                         /* bound check failure */
  141                         return 0;
  142                 }
  143 
  144                 s[0] = 0xE0 | ((wc >> 12) & 0x0F);
  145                 s[1] = 0x80 | ((wc >> 6) & 0x3F);
  146                 s[2] = 0x80 | ((wc) & 0x3F);
  147                 return 3;
  148         } else if (wc & 0x0780) {
  149                 if (n < 2) {
  150                         /* bound check failure */
  151                         return 0;
  152                 }
  153 
  154                 s[0] = 0xC0 | ((wc >> 6) & 0x1F);
  155                 s[1] = 0x80 | ((wc) & 0x3F);
  156                 return 2;
  157         } else {
  158                 if (n < 1) {
  159                         /* bound check failure */
  160                         return 0;
  161                 }
  162 
  163                 s[0] = wc;
  164                 return 1;
  165         }
  166 }

Cache object: f6e5923c0086fbc4d04640a10d87fd07


[ 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.