Ejemplo n.º 1
0
    /* EmbeddedData search */

    public List <EmbeddedData> FindEmbeddedData(EmbeddedDataType type)
    {
        MessagePackObjectDictionary worldDict = worldObject.AsDictionary();

        CheckWorldValid(worldDict);

        List <EmbeddedData> dataList = new List <EmbeddedData>();

        try
        {
            foreach (var data in IterateEmbeddedData(worldDict, type))
            {
                dataList.Add(data);
            }
        }
        catch (MapReadException e)
        {
            throw e;
        }
        catch (Exception e)
        {
            throw new MapReadException("Error reading world file", e);
        }
        return(dataList);
    }
Ejemplo n.º 2
0
    public List <EmbeddedData> FindEmbeddedData(EmbeddedDataType type)
    {
        List <EmbeddedData> dataList = new List <EmbeddedData>();

        if (type == EmbeddedDataType.Audio)
        {
            dataList.Add(data);
        }
        return(dataList);
    }
Ejemplo n.º 3
0
    public static List <EmbeddedData> ReadEmbeddedData(string filePath, EmbeddedDataType type)
    {
        WorldFileReader reader;

        try
        {
            using (FileStream stream = File.Open(filePath, FileMode.Open))
                reader = ReadStream(stream);
        }
        catch (System.Exception e)
        {
            throw new MapReadException("Error opening file", e);
        }
        return(reader.FindEmbeddedData(type));
    }
Ejemplo n.º 4
0
    private IEnumerable <EmbeddedData> IterateEmbeddedData(
        MessagePackObjectDictionary worldDict, EmbeddedDataType type)
    {
        var typeString = type.ToString();

        foreach (var propList in IterateProperties(worldDict))
        {
            if (!propList[1].IsList)
            {
                continue;
            }
            var dataList = propList[1].AsList();
            if (dataList.Count >= 3 && dataList[1].UnderlyingType == typeof(string) &&
                dataList[1].AsString() == typeString)
            {
                var name  = dataList[0].AsString();
                var bytes = dataList[2].AsBinary();
                yield return(new EmbeddedData(name, bytes, type));
            }
        }
    }
Ejemplo n.º 5
0
    public static PropertyGUI EmbeddedData(EmbeddedDataType type, AudioPlayerFactory playerFactory = null)
    {
        return((Property property) =>
        {
            var embeddedData = (EmbeddedData)property.value;

            GUILayout.BeginHorizontal();
            AlignedLabel(property);
            if (GUILayout.Button(embeddedData.name, GUI.skin.textField))
            {
                var import = GUIManager.guiGameObject.AddComponent <DataImportGUI>();
                import.title = "Select " + property.name;
                import.type = type;
                import.playerFactory = playerFactory;
                import.dataAction = (data) =>
                {
                    property.value = data;
                };
            }
            GUILayout.EndHorizontal();
        });
    }
Ejemplo n.º 6
0
 public EmbeddedData(string name, byte[] bytes, EmbeddedDataType type)
 {
     this.name  = name;
     this.bytes = bytes;
     this.type  = type;
 }
Ejemplo n.º 7
0
 public EmbeddedData()
 {
     name  = "(empty)";
     bytes = new byte[0];
     type  = EmbeddedDataType.None;
 }
Ejemplo n.º 8
0
    public static List <EmbeddedData> ReadEmbeddedData(Stream stream, EmbeddedDataType type)
    {
        WorldFileReader reader = ReadStream(stream);

        return(reader.FindEmbeddedData(type));
    }
Ejemplo n.º 9
0
 public List <EmbeddedData> FindEmbeddedData(EmbeddedDataType type)
 {
     return(new List <EmbeddedData>());
 }
Ejemplo n.º 10
0
        static void Main(string[] args)
        {
            //args = new string[] { "/e", "cat.jpg", "BBC123", "cat_enc.png", "/b", "4", "/p", "William Rågstad", "/i" };
            //args = new string[] { "/d", "cat_enc.png", "/b", "4", "/p", "William Rågstad", "/i" };

            //args = new string[] { "/e", "cat.jpg", "secret message.txt", "cat_file.png", "/f", "/b", "4", "/p", "William Rågstad", "/i" };
            //args = new string[] { "/d", "cat_file.png", "/b", "4", "/p", "William Rågstad", "/i" };

            //args = new string[] { "/e", "husky.png", "cat.jpg", "husky_cat.png", "/f" };
            //args = new string[] { "/d", "husky_cat.png" };
            // args = new[] {"/e", "cat.png", "hacker.jpg", "hidden.png", "/f", "/b", "4"};

            Arguments a = Arguments.Parse(args);

            _showDebugInfo = a.ContainsPattern("i");

            string password = null;

            if (a.ContainsPattern("p", typeof(string)))
            {
                password = a["p"][0];
            }

            int bitsEncoded = 2;
            int b           = -1;

            if (a.ContainsPattern("b", typeof(int)))
            {
                b = int.Parse(a["b"][0]);
            }
            if (b > 0 && 8 % b == 0)
            {
                bitsEncoded = b;
            }

            if (a.ContainsPattern("e", typeof(string), typeof(string), typeof(string)))
            {
                // Check for /f
                bool dataIsFile = a.ContainsPattern("f");

                string[] p = a["e"];

                Bitmap image = GetImage(p);
                if (image == null)
                {
                    return;
                }

                string data   = p[1];
                string output = p[2];

                if (dataIsFile)
                {
                    // Data is file
                    if (!File.Exists(data))
                    {
                        Console.WriteLine("Error! Data must be a valid filepath! (Don't use /f for messages)");
                        return;
                    }

                    if (_showDebugInfo && password != null)
                    {
                        Console.WriteLine($"Encrypting filename with password: \"{password}\"...");
                    }
                    byte[] encryptedName = Encrypt(BitEncoding.GetBytes(data), password);

                    if (_showDebugInfo && password != null)
                    {
                        Console.WriteLine($"Encrypting file binary with password: \"{password}\"...");
                    }

                    //string fileContent = File.ReadAllText(data);
                    //byte[] fileData = BitEncoding.GetBytes(fileContent);
                    byte[] fileData      = File.ReadAllBytes(data);
                    byte[] encryptedData = Encrypt(fileData, password);

                    if (_showDebugInfo)
                    {
                        Console.WriteLine($"Encoding encrypted data into \"{p[0]}\"...");
                    }

                    EncodeFile(image, encryptedName, encryptedData, output, bitsEncoded);
                }
                else
                {
                    // Data is message
                    byte[] messageBytes     = BitEncoding.GetBytes(data);
                    byte[] encryptedMessage = Encrypt(messageBytes, password);
                    if (_showDebugInfo && password != null)
                    {
                        Console.WriteLine($"Encrypted: \"{data}\" with password: \"{password}\"");
                    }

                    if (_showDebugInfo)
                    {
                        Console.WriteLine($"Encoding encrypted message into \"{p[0]}\"...");
                    }

                    EncodeMessage(image, encryptedMessage, output, bitsEncoded);
                }
                if (_showDebugInfo)
                {
                    Console.WriteLine($"Writing to file \"{output}\"...");
                }
                if (_showDebugInfo)
                {
                    Console.WriteLine("Done!");
                }
            }
            else if (a.ContainsPattern("d", typeof(string)))
            {
                string[] p = a["d"];

                Bitmap image = GetImage(p);
                if (image == null)
                {
                    return;
                }

                if (_showDebugInfo)
                {
                    Console.WriteLine($"Decoding encrypted data in \"{p[0]}\"...");
                }

                EmbeddedDataType embeddedDataType = Decode(image, bitsEncoded, out var encryptedData, out var encryptedFilename);

                switch (embeddedDataType)
                {
                case EmbeddedDataType.Message:
                    if (_showDebugInfo)
                    {
                        Console.WriteLine($"Decrypting message...");
                    }
                    byte[] message = Decrypt(encryptedData, password);
                    if (_showDebugInfo)
                    {
                        Console.Write("\nHidden message: ");
                    }
                    Console.WriteLine(BitEncoding.GetString(message));
                    break;

                case EmbeddedDataType.File:
                    string filename = BitEncoding.GetString(Decrypt(encryptedFilename, password));
                    if (_showDebugInfo)
                    {
                        Console.WriteLine($"Found file: \"{filename}\"...");
                    }

                    if (!OverrideFile(filename))
                    {
                        break;
                    }

                    byte[] data = Decrypt(encryptedData, password);
                    File.WriteAllBytes(filename, data);
                    //string content = BitEncoding.GetString(data);
                    //File.WriteAllText(filename, content);
                    Console.WriteLine($"Successfully extracted file \"{filename}\"!");
                    break;
                }
            }
            else
            {
                Console.WriteLine(Usage);
            }
        }
Ejemplo n.º 11
0
        private static EmbeddedDataType Decode(Bitmap image, int encodedBits, out byte[] data, out byte[] filename)
        {
            //byte[] mBuff = new byte[(int)Math.Ceiling(image.Width * (double)image.Height / encodedBits)];
            //int bIndex  = 0;
            EmbeddedDataType embeddedDataType = EmbeddedDataType.Unknown;

            byte[]      embeddedFilename = null;
            List <byte> mBuff            = new List <byte>();
            byte        cBuff            = 0;
            int         counter          = 0;

            byte[] matchEndOfDataStream = new byte[EndOfDataStreamSequence.Length];

            bool isFirstByte = true;

            bool AddPart(byte part)
            {
                cBuff = (byte)((cBuff << encodedBits) + part);
                counter++;

                if (counter >= 8 / encodedBits)
                {
                    byte dByte = DecodeByte(cBuff, encodedBits);

addByte:
                    if (isFirstByte)
                    {
                        isFirstByte = false;
                        // try to convert byte to EmbeddedDataType
                        embeddedDataType = (EmbeddedDataType)dByte;
                        if (!Enum.IsDefined(typeof(EmbeddedDataType), embeddedDataType) && !embeddedDataType.ToString().Contains(","))
                        {
                            embeddedDataType = EmbeddedDataType.Message;
                            // The byte was unknown, therefore it must be part of the message
                            goto addByte;
                        }
                        else
                        {
                            // Reset all variables
                            cBuff   = 0;
                            counter = 0;
                        }
                        if (_showDebugInfo)
                        {
                            Console.WriteLine($"Classifies embedded data as: {embeddedDataType.ToString()}...");
                        }
                    }
                    else
                    {
                        // Check for en of transmission
                        if (matchEndOfDataStream.SequenceEqual(EndOfDataStreamSequence)) // Was last byte of message
                        {
                            if (embeddedDataType == EmbeddedDataType.Message)
                            {
                                return(true);
                            }
                            else if (embeddedFilename == null)
                            {
                                embeddedFilename = mBuff.GetRange(0, mBuff.Count - EndOfDataStreamSequence.Length).ToArray();
                                mBuff            = new List <byte>();
                                mBuff.Add(dByte);
                                cBuff   = 0;
                                counter = 0;

                                ShiftAndAddByte(ref matchEndOfDataStream, dByte);
                                return(false);
                            }
                            else
                            {
                                return(true);
                            }
                        }
                        mBuff.Add(dByte);
                        cBuff   = 0;
                        counter = 0;

                        ShiftAndAddByte(ref matchEndOfDataStream, dByte);
                    }
                }
                return(false); // Was not last byte of message
            }

            EmbeddedDataType OnReturn(out byte[] d, out byte[] f)
            {
                d = mBuff.GetRange(0, mBuff.Count - EndOfDataStreamSequence.Length).ToArray();
                f = embeddedFilename;
                return(embeddedDataType);
            }

            for (int h = 0; h < image.Height; h++)
            {
                for (int w = 0; w < image.Width; w++)
                {
                    Color pixel = image.GetPixel(w, h);
                    byte  r     = (byte)(pixel.R ^ ((pixel.R >> encodedBits) << encodedBits));
                    byte  g     = (byte)(pixel.G ^ ((pixel.G >> encodedBits) << encodedBits));
                    byte  b     = (byte)(pixel.B ^ ((pixel.B >> encodedBits) << encodedBits));

                    if (AddPart(r))
                    {
                        return(OnReturn(out data, out filename));
                    }
                    if (AddPart(g))
                    {
                        return(OnReturn(out data, out filename));
                    }
                    if (AddPart(b))
                    {
                        return(OnReturn(out data, out filename));
                    }
                }
            }

            //return message;
            return(OnReturn(out data, out filename));
        }