OSDN Git Service

Model、View、Net、Utilの名前空間にクラスを分類する
[kancollesniffer/KancolleSniffer.git] / KancolleSniffer / Util / Ascii85.cs
diff --git a/KancolleSniffer/Util/Ascii85.cs b/KancolleSniffer/Util/Ascii85.cs
new file mode 100644 (file)
index 0000000..58a000f
--- /dev/null
@@ -0,0 +1,55 @@
+// Copyright (C) 2017 Kazuhiro Fujieda <fujieda@users.osdn.me>\r
+//\r
+// Licensed under the Apache License, Version 2.0 (the "License");\r
+// you may not use this file except in compliance with the License.\r
+// You may obtain a copy of the License at\r
+//\r
+//    http://www.apache.org/licenses/LICENSE-2.0\r
+//\r
+// Unless required by applicable law or agreed to in writing, software\r
+// distributed under the License is distributed on an "AS IS" BASIS,\r
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+// See the License for the specific language governing permissions and\r
+// limitations under the License.\r
+\r
+using System;\r
+using System.Text;\r
+\r
+namespace KancolleSniffer.Util\r
+{\r
+    public static class Ascii85\r
+    {\r
+        public static string Encode(byte[] input)\r
+        {\r
+            var count = 0;\r
+            var tuple = 0u;\r
+            var output = new StringBuilder();\r
+            output.Append("<~");\r
+            foreach (byte ch in input)\r
+            {\r
+                tuple |=  (uint)ch << (24 - 8 * count++);\r
+                if (count < 4)\r
+                    continue;\r
+                output.Append(tuple == 0 ? "z" : Encode85(tuple, count));\r
+                tuple = 0;\r
+                count = 0;\r
+            }\r
+            if (count > 0)\r
+                output.Append(Encode85(tuple, count));\r
+            output.Append("~>");\r
+            return output.ToString();\r
+        }\r
+\r
+        private static string Encode85(uint tuple, int count)\r
+        {\r
+            var buf = new char[5];\r
+            for (var j = 4; j >= 0; j--)\r
+            {\r
+                buf[j] = (char)(tuple % 85 + '!');\r
+                tuple /= 85;\r
+            }\r
+            Array.Resize(ref buf, count + 1);\r
+            return new string(buf);\r
+        }\r
+    }\r
+}
\ No newline at end of file