aboutsummaryrefslogtreecommitdiff
path: root/util.c
diff options
context:
space:
mode:
Diffstat (limited to 'util.c')
-rw-r--r--util.c28
1 files changed, 28 insertions, 0 deletions
diff --git a/util.c b/util.c
new file mode 100644
index 0000000..202c750
--- /dev/null
+++ b/util.c
@@ -0,0 +1,28 @@
+static uint32_t
+utf8_encode(uint8_t out[4], uint32_t cp)
+{
+ uint32_t result;
+ if (cp <= 0x7F) {
+ out[0] = cp & 0x7F;
+ result = 1;
+ } else if (cp <= 0x7FF) {
+ result = 2;
+ out[0] = ((cp >> 6) & 0x1F) | 0xC0;
+ out[1] = ((cp >> 0) & 0x3F) | 0x80;
+ } else if (cp <= 0xFFFF) {
+ result = 3;
+ out[0] = ((cp >> 12) & 0x0F) | 0xE0;
+ out[1] = ((cp >> 6) & 0x3F) | 0x80;
+ out[2] = ((cp >> 0) & 0x3F) | 0x80;
+ } else if (cp <= 0x10FFFF) {
+ result = 4;
+ out[0] = ((cp >> 18) & 0x07) | 0xF0;
+ out[1] = ((cp >> 12) & 0x3F) | 0x80;
+ out[2] = ((cp >> 6) & 0x3F) | 0x80;
+ out[3] = ((cp >> 0) & 0x3F) | 0x80;
+ } else {
+ //out[0] = '?';
+ result = 0;
+ }
+ return result;
+}