#include <linux/array_size.h>
#include <linux/build_bug.h>
#include <linux/bsearch.h>
#include <linux/consolemap.h>
#include <linux/math.h>
struct ucs_width16 {
u16 first;
u16 last;
};
struct ucs_width32 {
u32 first;
u32 last;
};
#define UCS_CP_SHIFT 12
#define UCS_NONBMP_W2_FLAG_BIT 11
#define UCS_NONBMP_W2_FLAG (1u << UCS_NONBMP_W2_FLAG_BIT)
#define BMP_0WIDTH(first, last) first, last
#define BMP_2WIDTH(first, last) first, last
#define RANGE_0WIDTH(first, last) \
(u32)(first) << UCS_CP_SHIFT, (u32)(last) << UCS_CP_SHIFT
#define RANGE_2WIDTH(first, last) \
(u32)(first) << UCS_CP_SHIFT, ((u32)(last) << UCS_CP_SHIFT) | UCS_NONBMP_W2_FLAG
#define BMP_2W_BITS(b) (b)
#include "ucs_width_table.h"
static_assert(UCS_NONBMP_BMP_BITS <= UCS_NONBMP_W2_FLAG_BIT,
"BMP bitmap chunk would overlap the per-entry width flag");
static_assert(UCS_NONBMP_W2_FLAG_BIT < UCS_CP_SHIFT,
"Metadata bits collide with the shifted cp value");
static_assert(DIV_ROUND_UP(ARRAY_SIZE(ucs_bmp_ranges), UCS_NONBMP_BMP_BITS)
<= ARRAY_SIZE(ucs_nonbmp_ranges),
"Not enough non-BMP entries to host the BMP width bitmap");
#define UCS_IS_BMP(cp) ((cp) <= 0xffff)
static int width16_cmp(const void *key, const void *element)
{
u16 cp = *(u16 *)key;
const struct ucs_width16 *entry = element;
if (cp < entry->first)
return -1;
if (cp > entry->last)
return 1;
return 0;
}
static int width32_cmp(const void *key, const void *element)
{
u32 k = *(u32 *)key;
const struct ucs_width32 *entry = element;
if (k < entry->first)
return -1;
if (k > entry->last)
return 1;
return 0;
}
unsigned int ucs_get_width(u32 cp)
{
const struct ucs_width16 *e16;
const struct ucs_width32 *e32;
unsigned int idx;
u32 k;
if (UCS_IS_BMP(cp)) {
u16 bmp = cp;
if (bmp < ucs_bmp_ranges[0].first ||
bmp > ucs_bmp_ranges[ARRAY_SIZE(ucs_bmp_ranges) - 1].last)
return 1;
e16 = __inline_bsearch(&bmp, ucs_bmp_ranges,
ARRAY_SIZE(ucs_bmp_ranges),
sizeof(*ucs_bmp_ranges), width16_cmp);
if (!e16)
return 1;
idx = e16 - ucs_bmp_ranges;
return (ucs_nonbmp_ranges[idx / UCS_NONBMP_BMP_BITS].last
>> (idx % UCS_NONBMP_BMP_BITS)) & 1 ? 2 : 0;
}
k = cp << UCS_CP_SHIFT;
if (k < ucs_nonbmp_ranges[0].first ||
k > ucs_nonbmp_ranges[ARRAY_SIZE(ucs_nonbmp_ranges) - 1].last)
return 1;
e32 = __inline_bsearch(&k, ucs_nonbmp_ranges,
ARRAY_SIZE(ucs_nonbmp_ranges),
sizeof(*ucs_nonbmp_ranges), width32_cmp);
if (!e32)
return 1;
return (e32->last & UCS_NONBMP_W2_FLAG) ? 2 : 0;
}
struct ucs_recomposition {
u16 base;
u16 mark;
u16 recomposed;
};
#include "ucs_recompose_table.h"
struct compare_key {
u16 base;
u16 mark;
};
static int recomposition_cmp(const void *key, const void *element)
{
const struct compare_key *search_key = key;
const struct ucs_recomposition *entry = element;
if (search_key->base < entry->base)
return -1;
if (search_key->base > entry->base)
return 1;
if (search_key->mark < entry->mark)
return -1;
if (search_key->mark > entry->mark)
return 1;
return 0;
}
u32 ucs_recompose(u32 base, u32 mark)
{
if (base < UCS_RECOMPOSE_MIN_BASE || base > UCS_RECOMPOSE_MAX_BASE ||
mark < UCS_RECOMPOSE_MIN_MARK || mark > UCS_RECOMPOSE_MAX_MARK)
return 0;
struct compare_key key = { base, mark };
struct ucs_recomposition *result =
__inline_bsearch(&key, ucs_recomposition_table,
ARRAY_SIZE(ucs_recomposition_table),
sizeof(*ucs_recomposition_table),
recomposition_cmp);
return result ? result->recomposed : 0;
}
struct ucs_page_desc {
u8 page;
u8 count;
u16 start;
};
struct ucs_page_entry {
u8 offset;
u8 fallback;
};
#include "ucs_fallback_table.h"
static int ucs_page_desc_cmp(const void *key, const void *element)
{
u8 page = *(u8 *)key;
const struct ucs_page_desc *entry = element;
if (page < entry->page)
return -1;
if (page > entry->page)
return 1;
return 0;
}
static int ucs_page_entry_cmp(const void *key, const void *element)
{
u8 offset = *(u8 *)key;
const struct ucs_page_entry *entry = element;
if (offset < entry->offset)
return -1;
if (entry->fallback == UCS_PAGE_ENTRY_RANGE_MARKER) {
if (offset > entry[1].offset)
return 1;
} else {
if (offset > entry->offset)
return 1;
}
return 0;
}
u32 ucs_get_fallback(u32 cp)
{
const struct ucs_page_desc *page;
const struct ucs_page_entry *entry;
u8 page_idx = cp >> 8, offset = cp;
if (!UCS_IS_BMP(cp))
return 0;
if (cp >= 0xFF01 && cp <= 0xFF5E)
return cp - 0xFF01 + 33;
page = __inline_bsearch(&page_idx, ucs_fallback_pages,
ARRAY_SIZE(ucs_fallback_pages),
sizeof(*ucs_fallback_pages),
ucs_page_desc_cmp);
if (!page)
return 0;
entry = __inline_bsearch(&offset, ucs_fallback_entries + page->start,
page->count, sizeof(*ucs_fallback_entries),
ucs_page_entry_cmp);
if (!entry)
return 0;
if (entry->fallback == UCS_PAGE_ENTRY_RANGE_MARKER)
entry++;
return entry->fallback;
}