Rev 4207 | Rev 4209 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4207 | Rev 4208 | ||
---|---|---|---|
Line 38... | Line 38... | ||
38 | #include <string.h> |
38 | #include <string.h> |
39 | #include <print.h> |
39 | #include <print.h> |
40 | #include <cpu.h> |
40 | #include <cpu.h> |
41 | #include <arch/asm.h> |
41 | #include <arch/asm.h> |
42 | #include <arch.h> |
42 | #include <arch.h> |
- | 43 | #include <errno.h> |
|
43 | #include <console/kconsole.h> |
44 | #include <console/kconsole.h> |
44 | 45 | ||
45 | char invalch = '?'; |
46 | char invalch = '?'; |
46 | 47 | ||
47 | /** Byte mask consisting of lowest @n bits (out of eight). */ |
48 | /** Byte mask consisting of lowest @n bits (out of eight). */ |
Line 138... | Line 139... | ||
138 | * @param ch Input character. |
139 | * @param ch Input character. |
139 | * @param str Output buffer. |
140 | * @param str Output buffer. |
140 | * @param offset Offset (in bytes) where to start writing. |
141 | * @param offset Offset (in bytes) where to start writing. |
141 | * @param sz Size of the output buffer. |
142 | * @param sz Size of the output buffer. |
142 | * |
143 | * |
143 | * @return True if the character was encoded successfully or false if there |
144 | * @return EOK if the character was encoded successfully, EOVERFLOW if there |
144 | * was not enough space in the output buffer or the character code |
145 | * was not enough space in the output buffer or EINVAL if the character |
145 | * was invalid. |
146 | * code was invalid. |
146 | */ |
147 | */ |
147 | bool chr_encode(const wchar_t ch, char *str, size_t *offset, size_t sz) |
148 | int chr_encode(const wchar_t ch, char *str, size_t *offset, size_t sz) |
148 | { |
149 | { |
149 | uint32_t cc; /* Unsigned version of ch. */ |
150 | uint32_t cc; /* Unsigned version of ch. */ |
150 | 151 | ||
151 | int cbytes; /* Number of continuation bytes. */ |
152 | int cbytes; /* Number of continuation bytes. */ |
152 | int b0_bits; /* Number of data bits in first byte. */ |
153 | int b0_bits; /* Number of data bits in first byte. */ |
153 | int i; |
154 | int i; |
154 | 155 | ||
155 | if (*offset >= sz) |
156 | if (*offset >= sz) |
156 | return false; |
157 | return EOVERFLOW; |
157 | 158 | ||
158 | if (ch < 0) |
159 | if (ch < 0) |
159 | return false; |
160 | return EINVAL; |
160 | 161 | ||
161 | /* Bit operations should only be done on unsigned numbers. */ |
162 | /* Bit operations should only be done on unsigned numbers. */ |
162 | cc = (uint32_t) ch; |
163 | cc = (uint32_t) ch; |
163 | 164 | ||
164 | /* Determine how many continuation bytes are needed. */ |
165 | /* Determine how many continuation bytes are needed. */ |
Line 174... | Line 175... | ||
174 | } else if ((cc & ~LO_MASK_32(21)) == 0) { |
175 | } else if ((cc & ~LO_MASK_32(21)) == 0) { |
175 | b0_bits = 3; |
176 | b0_bits = 3; |
176 | cbytes = 3; |
177 | cbytes = 3; |
177 | } else { |
178 | } else { |
178 | /* Codes longer than 21 bits are not supported. */ |
179 | /* Codes longer than 21 bits are not supported. */ |
179 | return false; |
180 | return EINVAL; |
180 | } |
181 | } |
181 | 182 | ||
182 | /* Check for available space in buffer. */ |
183 | /* Check for available space in buffer. */ |
183 | if (*offset + cbytes >= sz) |
184 | if (*offset + cbytes >= sz) |
184 | return false; |
185 | return EOVERFLOW; |
185 | 186 | ||
186 | /* Encode continuation bytes. */ |
187 | /* Encode continuation bytes. */ |
187 | for (i = cbytes; i > 0; --i) { |
188 | for (i = cbytes; i > 0; --i) { |
188 | str[*offset + i] = 0x80 | (cc & LO_MASK_32(CONT_BITS)); |
189 | str[*offset + i] = 0x80 | (cc & LO_MASK_32(CONT_BITS)); |
189 | cc = cc >> CONT_BITS; |
190 | cc = cc >> CONT_BITS; |
Line 193... | Line 194... | ||
193 | str[*offset] = (cc & LO_MASK_32(b0_bits)) | HI_MASK_8(8 - b0_bits - 1); |
194 | str[*offset] = (cc & LO_MASK_32(b0_bits)) | HI_MASK_8(8 - b0_bits - 1); |
194 | 195 | ||
195 | /* Advance offset. */ |
196 | /* Advance offset. */ |
196 | *offset += (1 + cbytes); |
197 | *offset += (1 + cbytes); |
197 | 198 | ||
198 | return true; |
199 | return EOK; |
199 | } |
200 | } |
200 | 201 | ||
201 | /** Get size of string, with length limit. |
202 | /** Get size of string, with length limit. |
202 | * |
203 | * |
203 | * Get the number of bytes which are used by up to @a max_len first |
204 | * Get the number of bytes which are used by up to @a max_len first |