From aca1916fd815a55a58e948eb826940766f939f4f Mon Sep 17 00:00:00 2001
From: Rafael <Syping@users.noreply.github.com>
Date: Thu, 31 Mar 2016 23:41:53 +0200
Subject: [PATCH] add UTF-16 Support for titles

---
 SavegameData.cpp     | 35 ++----------------------
 SnapmaticPicture.cpp | 10 ++++---
 StringParser.cpp     | 65 ++++++++++++++++++++++++++++++++++++++++++++
 StringParser.h       | 32 ++++++++++++++++++++++
 gta5sync.pro         |  2 ++
 5 files changed, 107 insertions(+), 37 deletions(-)
 create mode 100755 StringParser.cpp
 create mode 100755 StringParser.h

diff --git a/SavegameData.cpp b/SavegameData.cpp
index 3fd0620..0c6ff04 100755
--- a/SavegameData.cpp
+++ b/SavegameData.cpp
@@ -16,6 +16,7 @@
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *****************************************************************************/
 
+#include "StringParser.h"
 #include "SavegameData.h"
 #include <QTextCodec>
 #include <QDebug>
@@ -72,43 +73,11 @@ bool SavegameData::readingSavegame()
 
 QString SavegameData::getSavegameDataString(QByteArray savegameHeader)
 {
-    QString savegameTitle;
     QByteArray savegameBytes = savegameHeader.left(savegameHeaderLength);
     QList<QByteArray> savegameBytesList = savegameBytes.split(char(0x01));
     savegameBytes = savegameBytesList.at(1);
-
-    int savegameLength = savegameBytes.length();
-    int parsedBytes = 0;
-
-    while (parsedBytes <= savegameLength)
-    {
-        QList<QByteArray> parseByteList;
-        parseByteList.append(savegameBytes.mid(parsedBytes-1, 1));
-        parseByteList.append(savegameBytes.mid(parsedBytes-2, 1));
-        if (parseByteList.at(0).toHex() == "00")
-        {
-            // Latin character
-            savegameTitle.append(QString::fromLatin1(parseByteList.at(1)));
-        }
-        else if (parseByteList.at(0).toHex() == "30")
-        {
-            // Japanese character
-            QByteArray japaneseHex;
-            japaneseHex.append(QByteArray::fromHex("A5"));
-            japaneseHex.append(parseByteList.at(1));
-            savegameTitle.append(QTextCodec::codecForName("EUC-JP")->toUnicode(japaneseHex));
-        }
-        else
-        {
-            // Unsupported
-        }
-        parsedBytes = parsedBytes + 2;
-        parseByteList.clear();
-    }
-
     savegameBytesList.clear();
-    savegameBytes.clear();
-    return savegameTitle;
+    return StringParser::parseTitleString(savegameBytes, savegameBytes.length());
 }
 
 bool SavegameData::readingSavegameFromFile(QString fileName)
diff --git a/SnapmaticPicture.cpp b/SnapmaticPicture.cpp
index c59f05d..c7e408e 100755
--- a/SnapmaticPicture.cpp
+++ b/SnapmaticPicture.cpp
@@ -17,6 +17,7 @@
 *****************************************************************************/
 
 #include "SnapmaticPicture.h"
+#include "StringParser.h"
 #include <QJsonDocument>
 #include <QJsonObject>
 #include <QVariantMap>
@@ -179,10 +180,11 @@ bool SnapmaticPicture::readingPicture()
 
 QString SnapmaticPicture::getSnapmaticPictureString(QByteArray snapmaticHeader)
 {
-    QByteArray snapmaticUsefulBytes = snapmaticHeader.left(snapmaticUsefulLength);
-    snapmaticUsefulBytes.replace((char)0x00, "");
-    snapmaticUsefulBytes.replace((char)0x01, "");
-    return QString::fromLatin1(snapmaticUsefulBytes);
+    QByteArray snapmaticBytes = snapmaticHeader.left(snapmaticUsefulLength);
+    QList<QByteArray> snapmaticBytesList = snapmaticBytes.split(char(0x01));
+    snapmaticBytes = snapmaticBytesList.at(1);
+    snapmaticBytesList.clear();
+    return StringParser::parseTitleString(snapmaticBytes, snapmaticBytes.length());
 }
 
 QString SnapmaticPicture::getSnapmaticJSONString(QByteArray jsonBytes)
diff --git a/StringParser.cpp b/StringParser.cpp
new file mode 100755
index 0000000..52187b0
--- /dev/null
+++ b/StringParser.cpp
@@ -0,0 +1,65 @@
+/*****************************************************************************
+* gta5sync GRAND THEFT AUTO V SYNC
+* Copyright (C) 2016 Syping
+*
+* This program is free software: you can redistribute it and/or modify
+* it under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* This program is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+* GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*****************************************************************************/
+
+#include <QTextCodec>
+#include <QByteArray>
+#include <QString>
+#include <QList>
+#include "StringParser.h"
+
+StringParser::StringParser()
+{
+
+}
+
+QString StringParser::parseTitleString(QByteArray commitBytes, int maxLength)
+{
+    QString finalString;
+    int parsedBytes = 0;
+
+    while (parsedBytes <= maxLength)
+    {
+        QList<QByteArray> parseByteList;
+        parseByteList.append(commitBytes.mid(parsedBytes-1, 1));
+        parseByteList.append(commitBytes.mid(parsedBytes-2, 1));
+        if (parseByteList.at(0).toHex() == "00")
+        {
+            // Latin character
+            finalString.append(QString::fromLatin1(parseByteList.at(1)));
+        }
+//      else if (parseByteList.at(0).toHex() == "30")
+//      {
+//          // Japanese character
+//          QByteArray japaneseHex;
+//          japaneseHex.append(QByteArray::fromHex("A5"));
+//          japaneseHex.append(parseByteList.at(1));
+//          finalString.append(QTextCodec::codecForName("EUC-JP")->toUnicode(japaneseHex));
+//      }
+        else
+        {
+            QByteArray unsupportedHex;
+            unsupportedHex.append(parseByteList.at(0));
+            unsupportedHex.append(parseByteList.at(1));
+            finalString.append(QTextCodec::codecForName("UTF-16BE")->toUnicode(unsupportedHex));
+        }
+        parsedBytes = parsedBytes + 2;
+        parseByteList.clear();
+    }
+
+    return finalString;
+}
diff --git a/StringParser.h b/StringParser.h
new file mode 100755
index 0000000..5404f82
--- /dev/null
+++ b/StringParser.h
@@ -0,0 +1,32 @@
+/*****************************************************************************
+* gta5sync GRAND THEFT AUTO V SYNC
+* Copyright (C) 2016 Syping
+*
+* This program is free software: you can redistribute it and/or modify
+* it under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* This program is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+* GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*****************************************************************************/
+
+#ifndef STRINGPARSER_H
+#define STRINGPARSER_H
+
+#include <QByteArray>
+#include <QString>
+
+class StringParser
+{
+public:
+    StringParser();
+    static QString parseTitleString(QByteArray commitBytes, int maxLength);
+};
+
+#endif // STRINGPARSER_H
diff --git a/gta5sync.pro b/gta5sync.pro
index fcb878f..08ee038 100755
--- a/gta5sync.pro
+++ b/gta5sync.pro
@@ -39,6 +39,7 @@ SOURCES += main.cpp \
     SnapmaticPicture.cpp \
     SnapmaticWidget.cpp \
     StandardPaths.cpp \
+    StringParser.cpp \
     UserInterface.cpp
 
 HEADERS  += \
@@ -57,6 +58,7 @@ HEADERS  += \
     SnapmaticPicture.h \
     SnapmaticWidget.h \
     StandardPaths.h \
+    StringParser.h \
     UserInterface.h
 
 FORMS    += \