foxygit / fake-08-xbox360 Log in
commit 8f3d78b11d2d816ac2580ea2900e417ff20fe7bf
Author:     Jon Bell <jondbell@protonmail.com>
AuthorDate: Tue Apr 28 21:40:24 2020 -0600
Commit:     Jon Bell <jondbell@protonmail.com>
CommitDate: Tue Apr 28 21:40:24 2020 -0600

    Bring over emoji handling from tac08 along with hi utf8-util (MIT licensed)
---
 Makefile                     |   4 +-
 libs/utf8-util/LICENSE       |  21 +++++
 libs/utf8-util/README.md     |   6 ++
 libs/utf8-util/utf8-util.cpp |  76 ++++++++++++++++++
 libs/utf8-util/utf8-util.h   | 181 +++++++++++++++++++++++++++++++++++++++++++
 source/cart.cpp              |  73 +++++++++++++++++
 6 files changed, 359 insertions(+), 2 deletions(-)

diff --git a/Makefile b/Makefile
index 862e17a..ee7bfad 100644
--- a/Makefile
+++ b/Makefile
@@ -33,9 +33,9 @@ include $(DEVKITARM)/3ds_rules
 #---------------------------------------------------------------------------------
 TARGET		:=	$(notdir $(CURDIR))
 BUILD		:=	build
-SOURCES		:=	source source/tests libs/lua-5.3.2/src
+SOURCES		:=	source source/tests libs/lua-5.3.2/src libs/utf8-util
 DATA		:=	data
-INCLUDES	:=	include libs/lua-5.3.2/src
+INCLUDES	:=	include libs/lua-5.3.2/src libs/utf8-util
 GRAPHICS	:=	gfx
 GFXBUILD	:=	$(BUILD)
 #ROMFS		:=	romfs
diff --git a/libs/utf8-util/LICENSE b/libs/utf8-util/LICENSE
new file mode 100644
index 0000000..74829da
--- /dev/null
+++ b/libs/utf8-util/LICENSE
@@ -0,0 +1,21 @@
+The MIT License (MIT)
+
+Copyright (c) 2014 L Witek
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
\ No newline at end of file
diff --git a/libs/utf8-util/README.md b/libs/utf8-util/README.md
new file mode 100644
index 0000000..fb86b05
--- /dev/null
+++ b/libs/utf8-util/README.md
@@ -0,0 +1,6 @@
+https://github.com/0xcafed00d/utf8-util
+
+utf8-util
+=========
+
+c++ utilites for handling utf8 strings
diff --git a/libs/utf8-util/utf8-util.cpp b/libs/utf8-util/utf8-util.cpp
new file mode 100644
index 0000000..8f3fd2e
--- /dev/null
+++ b/libs/utf8-util/utf8-util.cpp
@@ -0,0 +1,76 @@
+#include <stdexcept>
+#include "utf8-util.h"
+
+namespace utf8
+{
+    namespace impl
+    {
+        int processLeading (char c, char32_t& initialcp, bool throwOnErr)
+        {
+            if ((c & 0x80) == 0)
+            {
+                initialcp = c;
+                return  0;
+            }
+
+            if ((c & 0xe0) == 0xc0)
+            {
+                initialcp = c & 0x1f;
+                return 1;
+            }
+            else if ((c & 0xf0) == 0xe0)
+            {
+                initialcp = c & 0x0f;
+                return 2;
+            }
+            else if ((c & 0xf8) == 0xf0)
+            {
+                initialcp = c & 0x07;
+                return 3;
+            }
+            else
+            {
+                if (throwOnErr)
+                    throw std::runtime_error("invalid continuation count in leading byte");
+                return -1;
+            }
+        }
+
+        int countContinuations (char c, bool throwOnErr)
+        {
+            if ((c & 0x80) == 0)
+                return  0;
+            else if ((c & 0xe0) == 0xc0)
+                return 1;
+            else if ((c & 0xf0) == 0xe0)
+                return 2;
+            else if ((c & 0xf8) == 0xf0)
+                return 3;
+            else
+            {
+                if (throwOnErr)
+                    throw std::runtime_error("invalid continuation count in leading byte");
+
+                return -1;
+            }
+        }
+
+        bool isContinuation (char c)
+        {
+            return (c & 0xc0) == 0x80;
+        }
+
+        bool addContinuation (char c, char32_t& cp, bool throwOnError)
+        {
+            if ((c & 0xc0) == 0x80)
+            {
+                cp = (cp << 6) + (c & 0x3f);
+                return true;
+            }
+            if (throwOnErr)
+                throw std::runtime_error("expected continuation byte not found");
+            return false;
+        }
+    }
+}
+
diff --git a/libs/utf8-util/utf8-util.h b/libs/utf8-util/utf8-util.h
new file mode 100644
index 0000000..61d5ec0
--- /dev/null
+++ b/libs/utf8-util/utf8-util.h
@@ -0,0 +1,181 @@
+#pragma once
+
+#include <cstdint>
+#include <stdexcept>
+
+namespace utf8
+{
+    bool isValidUTF8 (const char* str);
+
+    namespace impl
+    {
+        const bool throwOnErr = true;
+
+        int processLeading (char c, char32_t& initialcp, bool throwOnErr = false);
+        bool addContinuation (char c, char32_t& cp, bool throwOnErr = false);
+        int countContinuations (char c, bool throwOnErr = false);
+        bool isContinuation (char c);
+
+        template <typename container_t>
+        class CodepointIteratorImpl
+        {
+            private:
+                const container_t& m_container;
+
+            public:
+                class iterator_impl
+                {
+                    private:
+                        typename container_t::const_iterator m_itr;
+                        typename container_t::const_iterator m_endItr;
+
+                        void verifyItrInc (typename container_t::const_iterator& i, const char* errmsg)
+                        {
+                            if (i == m_endItr)
+                                throw std::runtime_error(errmsg);
+
+                            ++i;
+                        }
+
+                    public:
+                        iterator_impl (const typename container_t::const_iterator& i, const typename container_t::const_iterator& end) : m_itr (i), m_endItr (end) {}
+
+                        char32_t operator* ()
+                        {
+                            auto itr = m_itr;
+                            char32_t cp = 0;
+                            int ncont = impl::processLeading(*itr, cp, throwOnErr);
+                            while (ncont)
+                            {
+                                verifyItrInc(itr, "incomplete codepoint at end of input");
+                                ncont--;
+                                impl::addContinuation(*itr, cp, throwOnErr);
+                            }
+                            return cp;
+                        }
+
+                        iterator_impl& operator++()
+                        {
+                            int ncont = impl::countContinuations(*m_itr, throwOnErr);
+                            verifyItrInc(m_itr, "attempt to move past end of input");
+                            while (ncont)
+                            {
+                                verifyItrInc(m_itr, "incomplete codepoint at end of input");
+                                ncont--;
+                            }
+                            return *this;
+                        }
+
+                        bool operator== (const iterator_impl& that)
+                        {
+                            return m_itr == that.m_itr;
+                        }
+
+                        bool operator!= (const iterator_impl& that)
+                        {
+                            return m_itr != that.m_itr;
+                        }
+                };
+
+                CodepointIteratorImpl (const container_t& c) : m_container(c)
+                {
+                }
+
+                iterator_impl begin () const
+                {
+                    return iterator_impl(m_container.cbegin(), m_container.cend());
+                }
+
+                iterator_impl end () const
+                {
+                    return iterator_impl(m_container.cend(), m_container.cend());
+                }
+        };
+    }
+
+    template <typename container_t>
+    inline impl::CodepointIteratorImpl<container_t> CodepointIterator (const container_t& c)
+    {
+        return impl::CodepointIteratorImpl<container_t>(c);
+    }
+
+    template <typename iterator_t>
+    bool isValidUTF8 (iterator_t begin, iterator_t end)
+    {
+        int cc = 0;
+        while (begin != end)
+        {
+            cc = impl::countContinuations(*begin);
+            if (cc < 0)
+                return false;
+
+            ++begin;
+            while (cc)
+            {
+                if ((begin != end) && impl::isContinuation(*begin))
+                {
+                    ++begin;
+                    cc--;
+                }
+                else
+                    return false;
+            }
+        }
+        return cc == 0;
+    }
+
+    template <typename container_t>
+    bool isValidUTF8 (const container_t& c)
+    {
+        return isValidUTF8(c.begin(), c.end());
+    }
+
+    namespace impl
+    {
+        template <typename value_t>
+        char32_t appendContinuation (char32_t codepoint, value_t& c)
+        {
+            c = static_cast<value_t>((codepoint & 0x3f) | 0x80);
+            return codepoint >> 6;
+        }
+    }
+
+    template <typename container_t>
+    void encodeCodepoint (char32_t codepoint, container_t& container)
+    {
+        if (codepoint > 0x10ffff)
+            throw std::runtime_error ("codepoint out of range");
+
+        if (codepoint < 0x80)
+        {
+            container.push_back(static_cast<typename container_t::value_type>(codepoint));
+        }
+        else if (codepoint < 0x800)
+        {
+            typename container_t::value_type c;
+            codepoint = impl::appendContinuation(codepoint, c);
+            container.push_back(static_cast<typename container_t::value_type>((codepoint & 0x1f) | 0xc0 ));
+            container.push_back(c);
+        }
+        else if (codepoint < 0x8000)
+        {
+            typename container_t::value_type c[2];
+            codepoint = impl::appendContinuation(codepoint, c[1]);
+            codepoint = impl::appendContinuation(codepoint, c[0]);
+            container.push_back(static_cast<typename container_t::value_type>((codepoint & 0x0f) | 0xe0));
+            container.push_back(c[0]);
+            container.push_back(c[1]);
+        }
+        else // codepoint < 0x80000
+        {
+            typename container_t::value_type c[3];
+            codepoint = impl::appendContinuation(codepoint, c[2]);
+            codepoint = impl::appendContinuation(codepoint, c[1]);
+            codepoint = impl::appendContinuation(codepoint, c[0]);
+            container.push_back(static_cast<typename container_t::value_type>((codepoint & 0x07) | 0xf0));
+            container.push_back(c[0]);
+            container.push_back(c[1]);
+            container.push_back(c[2]);
+        }
+    }
+}
diff --git a/source/cart.cpp b/source/cart.cpp
index 3c62f13..88edca5 100644
--- a/source/cart.cpp
+++ b/source/cart.cpp
@@ -1,5 +1,6 @@
 #include <string>
 #include <sstream>
+#include <map>

 #include "cart.h"
 #include "filehelpers.h"
@@ -9,12 +10,83 @@
 #include "logger.h"

 #include "cartPatcher.h"
+#include "utf8-util.h"

 #include "tests/test_base.h"
 #if _TEST
 #include "tests/cart_test.h"
 #endif

+
+
+
+std::map<char32_t, uint8_t> emoji = {
+    {0x025ae, 0x10}, {0x025a0, 0x11}, {0x025a1, 0x12}, {0x02059, 0x13}, {0x02058, 0x14},
+    {0x02016, 0x15}, {0x025c0, 0x16}, {0x025b6, 0x17}, {0x0300c, 0x18}, {0x0300d, 0x19},
+    {0x000a5, 0x1a}, {0x02022, 0x1b}, {0x03001, 0x1c}, {0x03002, 0x1d}, {0x0309b, 0x1e},
+    {0x0309c, 0x1f}, {0x00020, 0x20}, {0x00021, 0x21}, {0x00022, 0x22}, {0x00023, 0x23},
+    {0x00024, 0x24}, {0x00025, 0x25}, {0x00026, 0x26}, {0x00027, 0x27}, {0x00028, 0x28},
+    {0x00029, 0x29}, {0x0002a, 0x2a}, {0x0002b, 0x2b}, {0x0002c, 0x2c}, {0x0002d, 0x2d},
+    {0x0002e, 0x2e}, {0x0002f, 0x2f}, {0x00030, 0x30}, {0x00031, 0x31}, {0x00032, 0x32},
+    {0x00033, 0x33}, {0x00034, 0x34}, {0x00035, 0x35}, {0x00036, 0x36}, {0x00037, 0x37},
+    {0x00038, 0x38}, {0x00039, 0x39}, {0x0003a, 0x3a}, {0x0003b, 0x3b}, {0x0003c, 0x3c},
+    {0x0003d, 0x3d}, {0x0003e, 0x3e}, {0x0003f, 0x3f}, {0x00040, 0x40}, {0x1d622, 0x41},
+    {0x1d623, 0x42}, {0x1d624, 0x43}, {0x1d625, 0x44}, {0x1d626, 0x45}, {0x1d627, 0x46},
+    {0x1d628, 0x47}, {0x1d629, 0x48}, {0x1d62a, 0x49}, {0x1d62b, 0x4a}, {0x1d62c, 0x4b},
+    {0x1d62d, 0x4c}, {0x1d62e, 0x4d}, {0x1d62f, 0x4e}, {0x1d630, 0x4f}, {0x1d631, 0x50},
+    {0x1d632, 0x51}, {0x1d633, 0x52}, {0x1d634, 0x53}, {0x1d635, 0x54}, {0x1d636, 0x55},
+    {0x1d637, 0x56}, {0x1d638, 0x57}, {0x1d639, 0x58}, {0x1d63a, 0x59}, {0x1d63b, 0x5a},
+    {0x0005b, 0x5b}, {0x0005c, 0x5c}, {0x0005d, 0x5d}, {0x0005e, 0x5e}, {0x0005f, 0x5f},
+    {0x00060, 0x60}, {0x00061, 0x61}, {0x00062, 0x62}, {0x00063, 0x63}, {0x00064, 0x64},
+    {0x00065, 0x65}, {0x00066, 0x66}, {0x00067, 0x67}, {0x00068, 0x68}, {0x00069, 0x69},
+    {0x0006a, 0x6a}, {0x0006b, 0x6b}, {0x0006c, 0x6c}, {0x0006d, 0x6d}, {0x0006e, 0x6e},
+    {0x0006f, 0x6f}, {0x00070, 0x70}, {0x00071, 0x71}, {0x00072, 0x72}, {0x00073, 0x73},
+    {0x00074, 0x74}, {0x00075, 0x75}, {0x00076, 0x76}, {0x00077, 0x77}, {0x00078, 0x78},
+    {0x00079, 0x79}, {0x0007a, 0x7a}, {0x0007b, 0x7b}, {0x0007c, 0x7c}, {0x0007d, 0x7d},
+    {0x0007e, 0x7e}, {0x025cb, 0x7f}, {0x02588, 0x80}, {0x02592, 0x81}, {0x1f431, 0x82},
+    {0x02b07, 0x83}, {0x02591, 0x84}, {0x0273d, 0x85}, {0x025cf, 0x86}, {0x02665, 0x87},
+    {0x02609, 0x88}, {0x0c6c3, 0x89}, {0x02302, 0x8a}, {0x02b05, 0x8b}, {0x1f610, 0x8c},
+    {0x0266a, 0x8d}, {0x1f17e, 0x8e}, {0x025c6, 0x8f}, {0x02026, 0x90}, {0x027a1, 0x91},
+    {0x02605, 0x92}, {0x029d7, 0x93}, {0x02b06, 0x94}, {0x002c7, 0x95}, {0x02227, 0x96},
+    {0x0274e, 0x97}, {0x025a4, 0x98}, {0x025a5, 0x99}, {0x03042, 0x9a}, {0x03044, 0x9b},
+    {0x03046, 0x9c}, {0x03048, 0x9d}, {0x0304a, 0x9e}, {0x0304b, 0x9f}, {0x0304d, 0xa0},
+    {0x0304f, 0xa1}, {0x03051, 0xa2}, {0x03053, 0xa3}, {0x03055, 0xa4}, {0x03057, 0xa5},
+    {0x03059, 0xa6}, {0x0305b, 0xa7}, {0x0305d, 0xa8}, {0x0305f, 0xa9}, {0x03061, 0xaa},
+    {0x03064, 0xab}, {0x03066, 0xac}, {0x03068, 0xad}, {0x0306a, 0xae}, {0x0306b, 0xaf},
+    {0x0306c, 0xb0}, {0x0306d, 0xb1}, {0x0306e, 0xb2}, {0x0306f, 0xb3}, {0x03072, 0xb4},
+    {0x03075, 0xb5}, {0x03078, 0xb6}, {0x0307b, 0xb7}, {0x0307e, 0xb8}, {0x0307f, 0xb9},
+    {0x03080, 0xba}, {0x03081, 0xbb}, {0x03082, 0xbc}, {0x03084, 0xbd}, {0x03086, 0xbe},
+    {0x03088, 0xbf}, {0x03089, 0xc0}, {0x0308a, 0xc1}, {0x0308b, 0xc2}, {0x0308c, 0xc3},
+    {0x0308d, 0xc4}, {0x0308f, 0xc5}, {0x03092, 0xc6}, {0x03093, 0xc7}, {0x03063, 0xc8},
+    {0x03083, 0xc9}, {0x03085, 0xca}, {0x03087, 0xcb}, {0x030a2, 0xcc}, {0x030a4, 0xcd},
+    {0x030a6, 0xce}, {0x030a8, 0xcf}, {0x030aa, 0xd0}, {0x030ab, 0xd1}, {0x030ad, 0xd2},
+    {0x030af, 0xd3}, {0x030b1, 0xd4}, {0x030b3, 0xd5}, {0x030b5, 0xd6}, {0x030b7, 0xd7},
+    {0x030b9, 0xd8}, {0x030bb, 0xd9}, {0x030bd, 0xda}, {0x030bf, 0xdb}, {0x030c1, 0xdc},
+    {0x030c4, 0xdd}, {0x030c6, 0xde}, {0x030c8, 0xdf}, {0x030ca, 0xe0}, {0x030cb, 0xe1},
+    {0x030cc, 0xe2}, {0x030cd, 0xe3}, {0x030ce, 0xe4}, {0x030cf, 0xe5}, {0x030d2, 0xe6},
+    {0x030d5, 0xe7}, {0x030d8, 0xe8}, {0x030db, 0xe9}, {0x030de, 0xea}, {0x030df, 0xeb},
+    {0x030e0, 0xec}, {0x030e1, 0xed}, {0x030e2, 0xee}, {0x030e4, 0xef}, {0x030e6, 0xf0},
+    {0x030e8, 0xf1}, {0x030e9, 0xf2}, {0x030ea, 0xf3}, {0x030eb, 0xf4}, {0x030ec, 0xf5},
+    {0x030ed, 0xf6}, {0x030ef, 0xf7}, {0x030f2, 0xf8}, {0x030f3, 0xf9}, {0x030c3, 0xfa},
+    {0x030e3, 0xfb}, {0x030e5, 0xfc}, {0x030e7, 0xfd}, {0x025dc, 0xfe}, {0x025dd, 0xff},
+};
+
+std::string convert_emojis(const std::string& lua) {
+	std::string res;
+	for (char32_t codepoint : utf8::CodepointIterator(lua)) {
+		if (codepoint < 0x80) {
+			res.push_back(uint8_t(codepoint));
+		} else {
+			auto i = emoji.find(codepoint);
+			if (i != emoji.end()) {
+				res.push_back(i->second);
+			}
+		}
+	}
+	return res;
+}
+
+//tac08 based cart parsing and stripping of emoji
 Cart::Cart(std::string filename){
     Filename = filename;\
     Logger::Write("getting file contents\n");
@@ -29,6 +101,7 @@ Cart::Cart(std::string filename){

     while (std::getline(s, line)) {
 		line = utils::trimright(line, " \n\r");
+        line = convert_emojis(line);

         if (line.length() > 2 && line[0] == '_' && line[1] == '_') {
             currSec = line;