Beispiel #1
0
        byte[] EncodeFrame(Frame f)
        {
            byte[] b = new byte[
                2 + // stuff
                2 + // size
                4 + // mask
                f.payload.Length
                       ];

            b[0] = (byte)(
                (0x1 << 7) + // FIN
                ((f.RSV1 ? 1 : 0) << 6) +
                ((f.RSV2 ? 1 : 0) << 5) +
                ((f.RSV3 ? 1 : 0) << 4) +
                +((int)f.opcode)
                );

            b[1] = (1 << 7) + 126;

            byte[] lengthbytes = bebc.GetBytes((ushort)f.payload.Length);
            Array.Copy(lengthbytes, 0, b, 2, 2);

            Random r = new Random((int)DateTime.Now.Ticks);

            byte[] maskingKey = bebc.GetBytes(r.Next());
            Array.Copy(maskingKey, 0, b, 4, 4);

            for (int i = 0; i < f.payload.Length; i++)
            {
                b[i + 8] = (byte)(f.payload[i] ^ maskingKey[i % 4]);
            }

            return(b);
        }
        public override byte[] encode(object data, BigEndianBitConverter converter)
        {
            Inventory inventory = (Inventory)data;
            //Yes... We're doing this differently from DataOptions
            //This code is an absolute horrid mess
            //And I can only hope that not everything I make in the future builds off this
            //Because I really, really, need to come back to this later once I know what I'm doing.
            //Alyssa 2019-05-25
            //P.S. you have to simplify your substitutions first
            List <byte[]> invData    = new List <byte[]>();
            int           dataLength = 0;

            foreach (Item item in inventory.items)
            {
                byte[] encoded = item.Encode(converter);
                dataLength += encoded.Length + 4;
                invData.Add(converter.GetBytes(encoded.Length));
                invData.Add(encoded);
            }
            byte[] output = new byte[dataLength + 4];

            System.Buffer.BlockCopy(converter.GetBytes(dataLength), 0, output, 0, 4);
            int offset = 4;

            foreach (byte[] bytes in invData)
            {
                System.Buffer.BlockCopy(bytes, 0, output, offset, bytes.Length);
                offset += bytes.Length;
            }

            return(output);
        }
        public override byte[] encode(object data, BigEndianBitConverter converter)
        {
            List <Substitution> substitutions = (List <Substitution>)data;
            //Yes... We're doing this differently from DataOptions
            //This code is an absolute horrid mess
            //And I can only hope that not everything I make in the future builds off this
            //Because I really, really, need to come back to this later once I know what I'm doing.
            //Alyssa 2019-05-25
            //P.S. you have to simplify your substitutions first
            List <byte[]> subData    = new List <byte[]>();
            int           dataLength = 0;

            foreach (Substitution sub in substitutions)
            {
                byte[] encoded = sub.Encode(converter);
                dataLength += encoded.Length + 4;
                subData.Add(converter.GetBytes(encoded.Length));
                subData.Add(encoded);
            }
            byte[] output = new byte[dataLength + 4];

            System.Buffer.BlockCopy(converter.GetBytes(dataLength), 0, output, 0, 4);
            System.Buffer.BlockCopy(subData.ToArray(), 0, output, 4, dataLength);

            return(output);
        }
Beispiel #4
0
            internal void Write(Stream buf)
            {
                var t         = new BigEndianBitConverter();
                var tmpBuffer = t.GetBytes(_signature);

                buf.Write(tmpBuffer, 0, tmpBuffer.Length);

                tmpBuffer = t.GetBytes(_version);
                buf.Write(tmpBuffer, 0, tmpBuffer.Length);

                tmpBuffer = t.GetBytes(Entries);
                buf.Write(tmpBuffer, 0, tmpBuffer.Length);
            }
Beispiel #5
0
        void CalculateData()
        {
            worldData = new byte[height * width * worldWordSize];
            bumpData  = new byte[height * width * bumpWordSize];
            BigEndianBitConverter converter = new BigEndianBitConverter();

            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    byte[] worldDataUnit;
                    byte[] bumpDataUnit;

                    switch (worldWordSize)
                    {
                    case 1:
                        worldDataUnit = new byte[] { (Byte)worldMap[y, x] };
                        break;

                    case 2:
                        worldDataUnit = converter.GetBytes((ushort)worldMap[y, x]);
                        break;

                    default:
                        worldDataUnit = converter.GetBytes((uint)worldMap[y, x]);
                        break;
                    }

                    switch (bumpWordSize)
                    {
                    case 1:
                        bumpDataUnit = new byte[] { (Byte)bumpMap[y, x] };
                        break;

                    case 2:
                        bumpDataUnit = converter.GetBytes((ushort)bumpMap[y, x]);
                        break;

                    default:
                        bumpDataUnit = converter.GetBytes((uint)bumpMap[y, x]);
                        break;
                    }
                    //Console.WriteLine("Copying {0} to pos {1} S={2}", BitConverter.ToString(worldDataUnit), ((y * width) + x) * worldWordSize, worldWordSize);
                    System.Buffer.BlockCopy(worldDataUnit, 0, worldData, ((y * width) + x) * worldWordSize, worldWordSize);
                    System.Buffer.BlockCopy(bumpDataUnit, 0, bumpData, ((y * width) + x) * bumpWordSize, bumpWordSize);
                }
            }
        }
Beispiel #6
0
        public override byte[] ToByteArray() =>
        new byte[]
        {
            (byte)ItemType
        }

        .Concat(BigEndianBitConverter.GetBytes(Value.Length)).Concat(Value.SelectMany(v => v.ToByteArray())).ToArray();
        private void WriteVbrHeader(bool isPlaceholder)
        {
            var buffer = new byte[GetFrameLength(_mpegVersion, 64000, _sampleRate, 0)];

            if (!isPlaceholder)
            {
                uint header     = _firstFrameHeader;
                int  dataOffset = GetFrameDataOffset(_mpegVersion, _channelMode);
                header &= 0xFFFE0DFF;                                                                                   // Clear CRC, bitrate, and padding fields
                header |= (uint)(_mpegVersion == 3 ? 5 : 8) << 12;                                                      // 64 kbit/sec
                BitHelper.CopyBytes(buffer, 0, BigEndianBitConverter.GetBytes(header));
                BitHelper.CopyBytes(buffer, dataOffset, BigEndianBitConverter.GetBytes(0x58696E67));                    // "Xing"
                BitHelper.CopyBytes(buffer, dataOffset + 4, BigEndianBitConverter.GetBytes((uint)0x7));                 // Flags
                BitHelper.CopyBytes(buffer, dataOffset + 8, BigEndianBitConverter.GetBytes((uint)_frameOffsets.Count)); // Frame count
                BitHelper.CopyBytes(buffer, dataOffset + 12, BigEndianBitConverter.GetBytes(_totalFrameLength));        // File length

                for (int i = 0; i < 100; i++)
                {
                    int frameIndex = (int)((i / 100.0) * _frameOffsets.Count);

                    buffer[dataOffset + 16 + i] = (byte)(_frameOffsets[frameIndex] / (double)_totalFrameLength * 256.0);
                }
            }

            _fileStream.Write(buffer, 0, buffer.Length);
        }
Beispiel #8
0
        /// <inheritdoc />
        /// <summary>Returns a byte array of the hash value.</summary>
        public byte[] Final()
        {
            ushort crc = 0;

            switch (_useNative)
            {
            case true when _useCcitt:
                crc16_ccitt_final(_nativeContext, ref crc);
                crc16_ccitt_free(_nativeContext);

                break;

            case true when _useIbm:
                crc16_final(_nativeContext, ref crc);
                crc16_free(_nativeContext);

                break;

            default:
            {
                if (_inverse)
                {
                    crc = (ushort)~(_hashInt ^ _finalSeed);
                }
                else
                {
                    crc = (ushort)(_hashInt ^ _finalSeed);
                }

                break;
            }
            }

            return(BigEndianBitConverter.GetBytes(crc));
        }
        private static void RunTestGetBytesToUInt64(ulong value, string expectedHex)
        {
            var expectedBytes = StringToBytesConverter.ConvertToByteArray(expectedHex);
            var result        = BigEndianBitConverter.GetBytes(value);

            Assert2.AreElementsEqual(expectedBytes, result);
        }
Beispiel #10
0
        public void BigEndianBitConverter_Decimal()
        {
            const int n = sizeof(Decimal);

            const Decimal pos  = 3.1415926535897932384626433833m;
            const Decimal zero = 0;
            const Decimal neg  = -pos;

            var posRepr = GetCanonicalRepresentation(pos);
            var negRepr = GetCanonicalRepresentation(neg);

            // --------------------------------------------------------------

            var buffer = new byte[n];

            BigEndianBitConverter.FillBytes(pos, buffer);
            Assert.IsTrue(buffer.SequenceEqual(posRepr));
            Assert.IsTrue(BigEndianBitConverter.GetBytes(pos).SequenceEqual(buffer));
            Assert.AreEqual(pos, BigEndianBitConverter.ToDecimal(buffer));

            BigEndianBitConverter.FillBytes(zero, buffer);
            Assert.IsTrue(buffer.SequenceEqual(new byte[n]));
            Assert.IsTrue(BigEndianBitConverter.GetBytes(zero).SequenceEqual(buffer));
            Assert.AreEqual(zero, BigEndianBitConverter.ToDecimal(buffer));

            BigEndianBitConverter.FillBytes(neg, buffer);
            Assert.IsTrue(buffer.SequenceEqual(negRepr));
            Assert.IsTrue(BigEndianBitConverter.GetBytes(neg).SequenceEqual(buffer));
            Assert.AreEqual(neg, BigEndianBitConverter.ToDecimal(buffer));
        }
Beispiel #11
0
        /// <summary>
        ///     Gets the hash of the specified data buffer.
        /// </summary>
        /// <param name="data">Data buffer.</param>
        /// <param name="len">Length of the data buffer to hash.</param>
        /// <param name="hash">Byte array of the hash value.</param>
        public static string Data(byte[] data, uint len, out byte[] hash)
        {
            byte localSum1 = 0xFF;
            byte localSum2 = 0xFF;

            for (int i = 0; i < len; i++)
            {
                localSum1 = (byte)((localSum1 + data[i]) % FLETCHER_MODULE);
                localSum2 = (byte)((localSum2 + localSum1) % FLETCHER_MODULE);
            }

            ushort finalSum = (ushort)((localSum2 << 8) | localSum1);

            BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian;
            hash = BigEndianBitConverter.GetBytes(finalSum);

            StringBuilder adlerOutput = new StringBuilder();

            foreach (byte h in hash)
            {
                adlerOutput.Append(h.ToString("x2"));
            }

            return(adlerOutput.ToString());
        }
Beispiel #12
0
        public void BigEndianBitConverter_Int32()
        {
            const int n = sizeof(Int32);

            const Int32 pos  = 0x11223344;
            const Int32 zero = 0;
            const Int32 neg  = -pos;

            var posRepr = new byte[] { 0x11, 0x22, 0x33, 0x44 };

            // --------------------------------------------------------------

            var buffer = new byte[n];

            BigEndianBitConverter.FillBytes(pos, buffer);
            Assert.IsTrue(buffer.SequenceEqual(posRepr));
            Assert.IsTrue(BigEndianBitConverter.GetBytes(pos).SequenceEqual(buffer));
            Assert.AreEqual(pos, BigEndianBitConverter.ToInt32(buffer));

            BigEndianBitConverter.FillBytes(zero, buffer);
            Assert.IsTrue(buffer.SequenceEqual(new byte[n]));
            Assert.IsTrue(BigEndianBitConverter.GetBytes(zero).SequenceEqual(buffer));
            Assert.AreEqual(zero, BigEndianBitConverter.ToInt32(buffer));

            BigEndianBitConverter.FillBytes(neg, buffer);
            Assert.IsTrue(buffer.SequenceEqual(Neg(posRepr)));
            Assert.IsTrue(BigEndianBitConverter.GetBytes(neg).SequenceEqual(buffer));
            Assert.AreEqual(neg, BigEndianBitConverter.ToInt32(buffer));
        }
Beispiel #13
0
        /// <summary>
        ///     Returns a byte array of the hash value.
        /// </summary>
        public byte[] Final()
        {
            uint finalSum = (uint)((sum2 << 16) | sum1);

            BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian;
            return(BigEndianBitConverter.GetBytes(finalSum));
        }
Beispiel #14
0
            /// <summary>
            ///     Gets a byte array representation of this tag
            /// </summary>
            public byte[] GetBytes()
            {
                byte[] tagBytes = new byte[24];

                BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian;

                byte[] tmp = BigEndianBitConverter.GetBytes(Version);
                Array.Copy(tmp, 0, tagBytes, 0, 2);
                tagBytes[2] = (byte)(Kind << 6);
                tagBytes[3] = Volume;
                tmp         = BigEndianBitConverter.GetBytes(FileId);
                Array.Copy(tmp, 0, tagBytes, 4, 2);
                tmp = BigEndianBitConverter.GetBytes((ushort)(UsedBytes & 0x7FFF));
                Array.Copy(tmp, 0, tagBytes, 6, 2);
                if (ValidChk)
                {
                    tagBytes[6] += 0x80;
                }
                tmp = BigEndianBitConverter.GetBytes(AbsPage);
                Array.Copy(tmp, 1, tagBytes, 8, 3);
                tagBytes[11] = Checksum;
                tmp          = BigEndianBitConverter.GetBytes(RelPage);
                Array.Copy(tmp, 0, tagBytes, 12, 2);
                tmp = BigEndianBitConverter.GetBytes(IsLast ? 0xFFFFFF : NextBlock);
                Array.Copy(tmp, 1, tagBytes, 14, 3);
                tmp = BigEndianBitConverter.GetBytes(IsFirst ? 0xFFFFFF : PrevBlock);
                Array.Copy(tmp, 1, tagBytes, 17, 3);
                tmp = BigEndianBitConverter.GetBytes(DiskSize);
                Array.Copy(tmp, 0, tagBytes, 20, 4);

                return(tagBytes);
            }
Beispiel #15
0
        public void BigEndianBitConverter_Double()
        {
            const int n = sizeof(Double);

            const Double pos  = Math.PI;
            const Double zero = 0;
            const Double neg  = -pos;

            var posRepr = GetCanonicalRepresentation(pos);
            var negRepr = GetCanonicalRepresentation(neg);

            // --------------------------------------------------------------

            var buffer = new byte[n];

            BigEndianBitConverter.FillBytes(pos, buffer);
            Assert.IsTrue(buffer.SequenceEqual(posRepr));
            Assert.IsTrue(BigEndianBitConverter.GetBytes(pos).SequenceEqual(buffer));
            Assert.AreEqual(pos, BigEndianBitConverter.ToDouble(buffer));

            BigEndianBitConverter.FillBytes(zero, buffer);
            Assert.IsTrue(buffer.SequenceEqual(new byte[n]));
            Assert.IsTrue(BigEndianBitConverter.GetBytes(zero).SequenceEqual(buffer));
            Assert.AreEqual(zero, BigEndianBitConverter.ToDouble(buffer));

            BigEndianBitConverter.FillBytes(neg, buffer);
            Assert.IsTrue(buffer.SequenceEqual(negRepr));
            Assert.IsTrue(BigEndianBitConverter.GetBytes(neg).SequenceEqual(buffer));
            Assert.AreEqual(neg, BigEndianBitConverter.ToDouble(buffer));
        }
        /// <summary>
        ///     Gets the hash of a file in hexadecimal and as a byte array.
        /// </summary>
        /// <param name="filename">File path.</param>
        /// <param name="hash">Byte array of the hash value.</param>
        public static string File(string filename, out byte[] hash)
        {
            FileStream fileStream = new FileStream(filename, FileMode.Open);

            ushort localSum1 = 1;
            ushort localSum2 = 0;

            for (int i = 0; i < fileStream.Length; i++)
            {
                localSum1 = (ushort)((localSum1 + fileStream.ReadByte()) % ADLER_MODULE);
                localSum2 = (ushort)((localSum2 + localSum1) % ADLER_MODULE);
            }

            uint finalSum = (uint)((localSum2 << 16) | localSum1);

            hash = BigEndianBitConverter.GetBytes(finalSum);

            StringBuilder adlerOutput = new StringBuilder();

            foreach (byte h in hash)
            {
                adlerOutput.Append(h.ToString("x2"));
            }

            fileStream.Close();

            return(adlerOutput.ToString());
        }
Beispiel #17
0
        /// <summary>
        ///     Gets the hash of a file in hexadecimal and as a byte array.
        /// </summary>
        /// <param name="filename">File path.</param>
        /// <param name="hash">Byte array of the hash value.</param>
        public static string File(string filename, out byte[] hash)
        {
            FileStream fileStream = new FileStream(filename, FileMode.Open);

            byte localSum1 = 0xFF;
            byte localSum2 = 0xFF;

            for (int i = 0; i < fileStream.Length; i++)
            {
                localSum1 = (byte)((localSum1 + fileStream.ReadByte()) % FLETCHER_MODULE);
                localSum2 = (byte)((localSum2 + localSum1) % FLETCHER_MODULE);
            }

            ushort finalSum = (ushort)((localSum2 << 8) | localSum1);

            BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian;
            hash = BigEndianBitConverter.GetBytes(finalSum);

            StringBuilder fletcherOutput = new StringBuilder();

            foreach (byte h in hash)
            {
                fletcherOutput.Append(h.ToString("x2"));
            }

            fileStream.Close();

            return(fletcherOutput.ToString());
        }
Beispiel #18
0
        public bool WriteMediaTag(byte[] data, MediaTagType tag)
        {
            if (!IsWriting)
            {
                ErrorMessage = "Tried to write on a non-writable image";
                return(false);
            }

            switch (tag)
            {
            case MediaTagType.CD_FullTOC:
                if (isDvd)
                {
                    ErrorMessage = $"Unsupported media tag {tag} for medium type {imageInfo.MediaType}";
                    return(false);
                }

                byte[] fullTocSize = BigEndianBitConverter.GetBytes((short)data.Length);
                fullToc = new byte[data.Length + 2];
                Array.Copy(data, 0, fullToc, 2, data.Length);
                fullToc[0] = fullTocSize[0];
                fullToc[1] = fullTocSize[1];
                return(true);

            case MediaTagType.DVD_PFI:
                if (!isDvd)
                {
                    ErrorMessage = $"Unsupported media tag {tag} for medium type {imageInfo.MediaType}";
                    return(false);
                }

                pfi = data;
                return(true);

            case MediaTagType.DVD_DMI:
                if (!isDvd)
                {
                    ErrorMessage = $"Unsupported media tag {tag} for medium type {imageInfo.MediaType}";
                    return(false);
                }

                dmi = data;
                return(true);

            case MediaTagType.DVD_BCA:
                if (!isDvd)
                {
                    ErrorMessage = $"Unsupported media tag {tag} for medium type {imageInfo.MediaType}";
                    return(false);
                }

                bca = data;
                return(true);

            default:
                ErrorMessage = $"Unsupported media tag {tag}";
                return(false);
            }
        }
Beispiel #19
0
        public bool Close()
        {
            if (!IsWriting)
            {
                ErrorMessage = "Image is not opened for writing";

                return(false);
            }

            _writingStream.Seek(0, SeekOrigin.Begin);
            _writingStream.Write(BigEndianBitConverter.GetBytes(_qHdr.magic), 0, 4);
            _writingStream.Write(BigEndianBitConverter.GetBytes(_qHdr.version), 0, 4);
            _writingStream.Write(BigEndianBitConverter.GetBytes(_qHdr.backing_file_offset), 0, 8);
            _writingStream.Write(BigEndianBitConverter.GetBytes(_qHdr.backing_file_size), 0, 4);
            _writingStream.Write(BigEndianBitConverter.GetBytes(_qHdr.cluster_bits), 0, 4);
            _writingStream.Write(BigEndianBitConverter.GetBytes(_qHdr.size), 0, 8);
            _writingStream.Write(BigEndianBitConverter.GetBytes(_qHdr.crypt_method), 0, 4);
            _writingStream.Write(BigEndianBitConverter.GetBytes(_qHdr.l1_size), 0, 4);
            _writingStream.Write(BigEndianBitConverter.GetBytes(_qHdr.l1_table_offset), 0, 8);
            _writingStream.Write(BigEndianBitConverter.GetBytes(_qHdr.refcount_table_offset), 0, 8);
            _writingStream.Write(BigEndianBitConverter.GetBytes(_qHdr.refcount_table_clusters), 0, 4);
            _writingStream.Write(BigEndianBitConverter.GetBytes(_qHdr.nb_snapshots), 0, 4);
            _writingStream.Write(BigEndianBitConverter.GetBytes(_qHdr.snapshots_offset), 0, 8);

            if (_qHdr.version == QCOW_VERSION3)
            {
                _writingStream.Write(BigEndianBitConverter.GetBytes(_qHdr.features), 0, 8);
                _writingStream.Write(BigEndianBitConverter.GetBytes(_qHdr.compat_features), 0, 8);
                _writingStream.Write(BigEndianBitConverter.GetBytes(_qHdr.autoclear_features), 0, 8);
                _writingStream.Write(BigEndianBitConverter.GetBytes(_qHdr.refcount_order), 0, 4);
                _writingStream.Write(BigEndianBitConverter.GetBytes(_qHdr.header_length), 0, 4);
            }

            _writingStream.Seek((long)_qHdr.refcount_table_offset, SeekOrigin.Begin);

            for (long i = 0; i < _refCountTable.LongLength; i++)
            {
                _writingStream.Write(BigEndianBitConverter.GetBytes(_refCountTable[i]), 0, 8);
            }

            _writingStream.Seek((long)_qHdr.l1_table_offset, SeekOrigin.Begin);

            for (long i = 0; i < _l1Table.LongLength; i++)
            {
                _l1Table[i] = Swapping.Swap(_l1Table[i]);
            }

            byte[] l1TableB = MemoryMarshal.Cast <ulong, byte>(_l1Table).ToArray();
            _writingStream.Write(l1TableB, 0, l1TableB.Length);

            _writingStream.Flush();
            _writingStream.Close();

            IsWriting    = false;
            ErrorMessage = "";

            return(true);
        }
Beispiel #20
0
        public bool Close()
        {
            if (!IsWriting)
            {
                ErrorMessage = "Image is not opened for writing";
                return(false);
            }

            BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian;
            byte[] markerTag = Encoding.UTF8.GetBytes("DiscImageChef " + Version.GetVersion());
            byte[] driveName;
            byte[] driveType      = new byte[3];
            byte[] driveBlocks    = BigEndianBitConverter.GetBytes((uint)imageInfo.Sectors);
            int    longSectorSize = imageInfo.MediaType == MediaType.PriamDataTower ? 536 : 532;

            byte[] blockSize = BigEndianBitConverter.GetBytes((ushort)longSectorSize);

            switch (imageInfo.MediaType)
            {
            case MediaType.AppleProfile when imageInfo.Sectors == 0x4C00:
                driveName = Encoding.ASCII.GetBytes(PROFILE10_NAME);
                break;

            case MediaType.AppleWidget when imageInfo.Sectors == 0x4C00:
                driveType[1] = 0x01;
                driveName    = Encoding.ASCII.GetBytes(PROFILE10_NAME);
                break;

            case MediaType.PriamDataTower when imageInfo.Sectors == 0x22C7C:
                driveType[1] = 0xFF;
                driveName    = Encoding.ASCII.GetBytes(PRIAM_NAME);
                break;

            default:
                driveName = Encoding.ASCII.GetBytes(PROFILE_NAME);
                break;
            }

            writingStream.Seek(0, SeekOrigin.Begin);
            writingStream.Write(driveName, 0, driveName.Length >= 0xD ? 0xD : driveName.Length);
            writingStream.Seek(0xD, SeekOrigin.Begin);
            writingStream.Write(driveType, 0, 3);
            writingStream.Seek(0x12, SeekOrigin.Begin);
            writingStream.Write(driveBlocks, 1, 3);
            writingStream.Seek(0x15, SeekOrigin.Begin);
            writingStream.Write(blockSize, 1, 2);
            writingStream.Seek(512, SeekOrigin.Begin);
            writingStream.Write(markerTag, 0,
                                markerTag.Length >= longSectorSize - 512 ? longSectorSize - 512 : markerTag.Length);

            writingStream.Flush();
            writingStream.Close();

            IsWriting    = false;
            ErrorMessage = "";
            return(true);
        }
Beispiel #21
0
        public override byte[] encode(object data, BigEndianBitConverter converter)
        {
            LegendSharp.World world = (LegendSharp.World)data;
            var heightData          = converter.GetBytes(world.height);
            var widthData           = converter.GetBytes(world.width);
            var worldData           = world.GetWorldBytes();
            var worldLength         = converter.GetBytes(worldData.Length);
            var worldWordSize       = converter.GetBytes(world.GetWorldWordSize());
            //TODO: World encoding
            var output = new byte[heightData.Length + widthData.Length + worldLength.Length + worldData.Length + worldWordSize.Length];

            System.Buffer.BlockCopy(heightData, 0, output, 0, 4);
            System.Buffer.BlockCopy(widthData, 0, output, 4, 4);
            System.Buffer.BlockCopy(worldLength, 0, output, 8, 4);
            System.Buffer.BlockCopy(worldData, 0, output, 12, worldData.Length);
            System.Buffer.BlockCopy(worldWordSize, 0, output, 12 + worldData.Length, 4);

            return(output);
        }
Beispiel #22
0
        public static byte[] Encode(int data)
        {
            var bytes = converter.GetBytes(data);

            if (data < ShortItemOffset)
            {
                return(new[] { bytes[bytes.Length - 1] });
            }

            return(Encode(data.ToString(CultureInfo.InvariantCulture)));
        }
        /// <summary>
        ///     Returns a hexadecimal representation of the hash value.
        /// </summary>
        public string End()
        {
            StringBuilder crc64Output = new StringBuilder();

            for (int i = 0; i < BigEndianBitConverter.GetBytes(hashInt ^= finalSeed).Length; i++)
            {
                crc64Output.Append(BigEndianBitConverter.GetBytes(hashInt ^= finalSeed)[i].ToString("x2"));
            }

            return(crc64Output.ToString());
        }
Beispiel #24
0
        public bool Close()
        {
            if (!IsWriting)
            {
                ErrorMessage = "Image is not opened for writing";

                return(false);
            }

            // No tags where written
            if (writingStream.Length == 0x54 + header.DataSize)
            {
                header.TagSize = 0;
            }

            writingStream.Seek(0x54, SeekOrigin.Begin);
            byte[] data = new byte[header.DataSize];
            writingStream.Read(data, 0, (int)header.DataSize);
            header.DataChecksum = DC42CheckSum(data);
            writingStream.Seek(0x54 + header.DataSize, SeekOrigin.Begin);
            data = new byte[header.TagSize];
            writingStream.Read(data, 0, (int)header.TagSize);
            header.TagChecksum = DC42CheckSum(data);

            writingStream.Seek(0, SeekOrigin.Begin);

            if (header.DiskName.Length > 63)
            {
                header.DiskName = header.DiskName.Substring(0, 63);
            }

            writingStream.WriteByte((byte)header.DiskName.Length);
            Encoding macRoman = new MacRoman();

            writingStream.Write(macRoman.GetBytes(header.DiskName), 0, header.DiskName.Length);

            writingStream.Seek(64, SeekOrigin.Begin);
            writingStream.Write(BigEndianBitConverter.GetBytes(header.DataSize), 0, 4);
            writingStream.Write(BigEndianBitConverter.GetBytes(header.TagSize), 0, 4);
            writingStream.Write(BigEndianBitConverter.GetBytes(header.DataChecksum), 0, 4);
            writingStream.Write(BigEndianBitConverter.GetBytes(header.TagChecksum), 0, 4);
            writingStream.WriteByte(header.Format);
            writingStream.WriteByte(header.FmtByte);
            writingStream.WriteByte(1);
            writingStream.WriteByte(0);

            writingStream.Flush();
            writingStream.Close();

            IsWriting    = false;
            ErrorMessage = "";

            return(true);
        }
Beispiel #25
0
        public override byte[] encode(object data, BigEndianBitConverter converter)
        {
            byte[] stringData   = System.Text.Encoding.UTF8.GetBytes((string)data);
            byte[] stringLength = converter.GetBytes(stringData.Length);
            byte[] output       = new byte[stringData.Length + 4];

            System.Buffer.BlockCopy(stringLength, 0, output, 0, 4);
            System.Buffer.BlockCopy(stringData, 0, output, 4, stringData.Length);

            return(output);
        }
Beispiel #26
0
        public override byte[] Encode(BigEndianBitConverter converter)
        {
            byte[] spriteData      = System.Text.Encoding.UTF8.GetBytes(GetSprite());
            byte[] nameData        = System.Text.Encoding.UTF8.GetBytes(GetName());
            byte[] descriptionData = System.Text.Encoding.UTF8.GetBytes(GetDescription());
            byte[] typeData        = System.Text.Encoding.UTF8.GetBytes(GetItemType());
            byte[] maxStackData    = converter.GetBytes(GetMaxStack());
            byte[] quantityData    = converter.GetBytes(GetQuantity());
            byte[] weaponClassData = System.Text.Encoding.UTF8.GetBytes(GetWeaponClass());
            byte[] damageData      = converter.GetBytes(GetDamage());
            byte[] damageTypeData  = System.Text.Encoding.UTF8.GetBytes(GetDamageType());

            byte[] spriteLength      = converter.GetBytes(spriteData.Length);
            byte[] nameLength        = converter.GetBytes(nameData.Length);
            byte[] descriptionLength = converter.GetBytes(descriptionData.Length);
            byte[] typeLength        = converter.GetBytes(typeData.Length);
            byte[] weaponClassLength = converter.GetBytes(weaponClassData.Length);
            byte[] damageTypeLength  = converter.GetBytes(damageTypeData.Length);

            byte[] output = new byte[40 + spriteData.Length + nameData.Length + descriptionData.Length + typeData.Length + weaponClassData.Length + damageTypeData.Length];

            System.Buffer.BlockCopy(spriteLength, 0, output, 0, 4);
            System.Buffer.BlockCopy(spriteData, 0, output, 4, spriteData.Length);

            System.Buffer.BlockCopy(nameLength, 0, output, 4 + spriteData.Length, 4);
            System.Buffer.BlockCopy(nameData, 0, output, 8 + spriteData.Length, nameData.Length);

            System.Buffer.BlockCopy(descriptionLength, 0, output, 8 + spriteData.Length + nameData.Length, 4);
            System.Buffer.BlockCopy(descriptionData, 0, output, 12 + spriteData.Length + nameData.Length, descriptionData.Length);

            System.Buffer.BlockCopy(typeLength, 0, output, 12 + spriteData.Length + nameData.Length + descriptionData.Length, 4);
            System.Buffer.BlockCopy(typeData, 0, output, 16 + spriteData.Length + nameData.Length + descriptionData.Length, typeData.Length);

            System.Buffer.BlockCopy(maxStackData, 0, output, 16 + spriteData.Length + nameData.Length + descriptionData.Length + typeData.Length, 4);
            System.Buffer.BlockCopy(quantityData, 0, output, 20 + spriteData.Length + nameData.Length + descriptionData.Length + typeData.Length, 4);

            System.Buffer.BlockCopy(weaponClassLength, 0, output, 24 + spriteData.Length + nameData.Length + descriptionData.Length + typeData.Length, 4);
            System.Buffer.BlockCopy(weaponClassData, 0, output, 28 + spriteData.Length + nameData.Length + descriptionData.Length + typeData.Length, weaponClassData.Length);

            System.Buffer.BlockCopy(damageData, 0, output, 28 + spriteData.Length + nameData.Length + descriptionData.Length + typeData.Length + weaponClassData.Length, 8);

            System.Buffer.BlockCopy(damageTypeLength, 0, output, 36 + spriteData.Length + nameData.Length + descriptionData.Length + typeData.Length + weaponClassData.Length, 4);
            System.Buffer.BlockCopy(damageTypeData, 0, output, 40 + spriteData.Length + nameData.Length + descriptionData.Length + typeData.Length + weaponClassData.Length, damageTypeData.Length);

            return(output);
        }
        /// <inheritdoc />
        /// <summary>Returns a hexadecimal representation of the hash value.</summary>
        public string End()
        {
            uint finalSum       = (uint)((_sum2 << 16) | _sum1);
            var  fletcherOutput = new StringBuilder();

            for (int i = 0; i < BigEndianBitConverter.GetBytes(finalSum).Length; i++)
            {
                fletcherOutput.Append(BigEndianBitConverter.GetBytes(finalSum)[i].ToString("x2"));
            }

            return(fletcherOutput.ToString());
        }
Beispiel #28
0
        /// <summary>
        ///     Returns a hexadecimal representation of the hash value.
        /// </summary>
        public string End()
        {
            StringBuilder crc16Output = new StringBuilder();

            BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian;
            for (int i = 0; i < BigEndianBitConverter.GetBytes((ushort)(hashInt ^ finalSeed)).Length; i++)
            {
                crc16Output.Append(BigEndianBitConverter.GetBytes((ushort)(hashInt ^ finalSeed))[i].ToString("x2"));
            }

            return(crc16Output.ToString());
        }
Beispiel #29
0
        /// <summary>
        /// Returns a hexadecimal representation of the hash value.
        /// </summary>
        public string End()
        {
            UInt32        finalSum    = (uint)((sum2 << 16) | sum1);
            StringBuilder adlerOutput = new StringBuilder();

            for (int i = 0; i < BigEndianBitConverter.GetBytes(finalSum).Length; i++)
            {
                adlerOutput.Append(BigEndianBitConverter.GetBytes(finalSum)[i].ToString("x2"));
            }

            return(adlerOutput.ToString());
        }
Beispiel #30
0
        /// <summary>
        /// Returns a hexadecimal representation of the hash value.
        /// </summary>
        public string End()
        {
            UInt16        finalSum       = (UInt16)(sum1 + (sum2 << 8));
            StringBuilder fletcherOutput = new StringBuilder();

            for (int i = 0; i < BigEndianBitConverter.GetBytes(finalSum).Length; i++)
            {
                fletcherOutput.Append(BigEndianBitConverter.GetBytes(finalSum)[i].ToString("x2"));
            }

            return(fletcherOutput.ToString());
        }