iPXE
utf8.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2022 Michael Brown <mbrown@fensystems.co.uk>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  *
19  * You can also choose to distribute this program under the terms of
20  * the Unmodified Binary Distribution Licence (as given in the file
21  * COPYING.UBDL), provided that you have satisfied its requirements.
22  */
23 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 
26 #include <stdint.h>
27 #include <assert.h>
28 #include <ipxe/utf8.h>
29 
30 /** @file
31  *
32  * UTF-8 Unicode encoding
33  *
34  */
35 
36 /**
37  * Accumulate Unicode character from UTF-8 byte sequence
38  *
39  * @v utf8 UTF-8 accumulator
40  * @v byte UTF-8 byte
41  * @ret character Unicode character, or 0 if incomplete
42  */
43 unsigned int utf8_accumulate ( struct utf8_accumulator *utf8, uint8_t byte ) {
44  static unsigned int min[] = {
48  };
49  unsigned int shift;
50  unsigned int len;
51  uint8_t tmp;
52 
53  /* Handle continuation bytes */
54  if ( UTF8_IS_CONTINUATION ( byte ) ) {
55 
56  /* Fail if this is an unexpected continuation byte */
57  if ( utf8->remaining == 0 ) {
58  DBGC ( utf8, "UTF8 %p unexpected %02x\n", utf8, byte );
59  return UTF8_INVALID;
60  }
61 
62  /* Apply continuation byte */
64  utf8->character |= ( byte & UTF8_CONTINUATION_MASK );
65 
66  /* Return 0 if more continuation bytes are expected */
67  if ( --utf8->remaining != 0 )
68  return 0;
69 
70  /* Fail if sequence is illegal */
71  if ( utf8->character < utf8->min ) {
72  DBGC ( utf8, "UTF8 %p illegal %02x\n", utf8,
73  utf8->character );
74  return UTF8_INVALID;
75  }
76 
77  /* Sanity check */
78  assert ( utf8->character != 0 );
79 
80  /* Return completed character */
81  DBGC2 ( utf8, "UTF8 %p accumulated %02x\n",
82  utf8, utf8->character );
83  return utf8->character;
84  }
85 
86  /* Reset state and report failure if this is an unexpected
87  * non-continuation byte. Do not return UTF8_INVALID since
88  * doing so could cause us to drop a valid ASCII character.
89  */
90  if ( utf8->remaining != 0 ) {
91  shift = ( utf8->remaining * UTF8_CONTINUATION_BITS );
92  DBGC ( utf8, "UTF8 %p unexpected %02x (partial %02x/%02x)\n",
93  utf8, byte, ( utf8->character << shift ),
94  ( ( 1 << shift ) - 1 ) );
95  utf8->remaining = 0;
96  }
97 
98  /* Handle initial bytes */
99  if ( ! UTF8_IS_ASCII ( byte ) ) {
100 
101  /* Sanity check */
102  assert ( utf8->remaining == 0 );
103 
104  /* Count total number of bytes in sequence */
105  tmp = byte;
106  len = 0;
107  while ( tmp & UTF8_HIGH_BIT ) {
108  tmp <<= 1;
109  len++;
110  }
111 
112  /* Check for illegal length */
113  if ( len > UTF8_MAX_LEN ) {
114  DBGC ( utf8, "UTF8 %p illegal %02x length %d\n",
115  utf8, byte, len );
116  return UTF8_INVALID;
117  }
118 
119  /* Store initial bits of character */
120  utf8->character = ( tmp >> len );
121 
122  /* Store number of bytes remaining */
123  len--;
124  utf8->remaining = len;
125  assert ( utf8->remaining > 0 );
126 
127  /* Store minimum legal value */
128  utf8->min = min[ len - 1 ];
129  assert ( utf8->min > 0 );
130 
131  /* Await continuation bytes */
132  return 0;
133  }
134 
135  /* Handle ASCII bytes */
136  return byte;
137 }
#define UTF8_MAX_LEN
Maximum length of UTF-8 sequence.
Definition: utf8.h:15
#define UTF8_IS_CONTINUATION(byte)
Check for a continuation byte.
Definition: utf8.h:43
#define UTF8_CONTINUATION_MASK
Bit mask for data bits in a continuation byte.
Definition: utf8.h:33
#define DBGC(...)
Definition: compiler.h:505
#define min(x, y)
Definition: ath.h:34
unsigned int remaining
Number of remaining continuation bytes.
Definition: utf8.h:61
#define UTF8_INVALID
Invalid character returned when decoding fails.
Definition: utf8.h:54
UTF-8 Unicode encoding.
unsigned long tmp
Definition: linux_pci.h:53
#define UTF8_MIN_FOUR
Minimum legal value for four-byte UTF-8 sequence.
Definition: utf8.h:24
#define UTF8_CONTINUATION_BITS
Number of data bits in each continuation byte.
Definition: utf8.h:30
unsigned int min
Minimum legal character.
Definition: utf8.h:63
Assertions.
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
#define UTF8_HIGH_BIT
High bit of UTF-8 bytes.
Definition: utf8.h:27
#define UTF8_IS_ASCII(byte)
Check for an ASCII byte.
Definition: utf8.h:51
unsigned char uint8_t
Definition: stdint.h:10
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
unsigned char byte
Definition: smc9000.h:38
#define UTF8_MIN_THREE
Minimum legal value for three-byte UTF-8 sequence.
Definition: utf8.h:21
A UTF-8 character accumulator.
Definition: utf8.h:57
uint32_t len
Length.
Definition: ena.h:14
#define DBGC2(...)
Definition: compiler.h:522
unsigned int utf8_accumulate(struct utf8_accumulator *utf8, uint8_t byte)
Accumulate Unicode character from UTF-8 byte sequence.
Definition: utf8.c:43
#define UTF8_MIN_TWO
Minimum legal value for two-byte UTF-8 sequence.
Definition: utf8.h:18
unsigned int character
Character in progress.
Definition: utf8.h:59