예제 #1
0
        private byte[] GetResourceBytes(byte[] buffer)
        {
            byte flags      = 0;
            int  encryptKey = _random.Next(100, int.MaxValue);

            // Compress
            if (buffer.Length > 30)
            {
                buffer = CompressionUtils.GZipCompress(buffer);
                flags |= 1;
            }

            // Encrypt
            StrongCryptoUtils.Encrypt(buffer, encryptKey);

            int pos  = 0;
            var blob = new Blob(buffer.Length + 14);

            blob.Write(ref pos, (int)ResourceSignature);                     // Signature
            blob.Write(ref pos, (int)StrongCryptoUtils.ComputeHash(buffer)); // Hash
            blob.Write(ref pos, (int)encryptKey);                            // Encrypt Key
            blob.Write(ref pos, (byte)flags);                                // Encrypt Key
            blob.Write(ref pos, (byte)0);                                    // Unused
            blob.Write(ref pos, (byte[])buffer);                             // Data

            return(blob.ToArray());
        }
        public int Add(byte[] buffer, int offset, int count, bool encrypt = false, bool compress = false)
        {
            int blobId = NextBlobID;

            byte flags = 0;

            // Compress
            if (compress && count > 30)
            {
                buffer       = CompressionUtils.GZipCompress(buffer, offset, count);
                offset       = 0;
                count        = buffer.Length;
                flags       |= 2;
                _hasCompress = true;
            }

            // Encrypt
            if (encrypt)
            {
                StrongCryptoUtils.Encrypt(buffer, _encryptKey, offset, count);
                flags      |= 1;
                _hasEncrypt = true;
            }

            // Write
            int pos = _blob.Length;

            _blob.Write(ref pos, (byte)flags);
            _blob.Write7BitEncodedInt(ref pos, count);
            _blob.Write(ref pos, buffer, offset, count);

            return(blobId);
        }
        internal void Save()
        {
            if (!_isChanged && !_isNew)
            {
                return;
            }

            if (_isNew)
            {
                // Set paths
                string fileName = _module.NameChanged ? _module.NewName : _module.Name;

                var    assembly   = (BuildAssembly)_module.Assembly;
                string outputPath = assembly.OutputPath;
                if (outputPath == null)
                {
                    outputPath = Path.GetDirectoryName(assembly.Location);
                }

                _outputFilePath = Path.Combine(outputPath, fileName);
                _stateFilePath  = _outputFilePath + ".adstate";
                _buildFilePath  = _outputFilePath + ".adbuild";
            }

            using (var accessor = new StreamAccessor(new FileStream(_stateFilePath, FileMode.Create, FileAccess.Write, FileShare.None)))
            {
                accessor.Write7BitEncodedInt(_bufferLength);
                accessor.Write(_buffer, 0, _bufferLength);

                _objects.Serialize(accessor);

                _blobs.Serialize(accessor);

                var signatureBlob = new Blob();
                _signatures.Serialize(new BlobAccessor(signatureBlob));

                var stringBlob = new Blob();
                _strings.Serialize(new BlobAccessor(stringBlob));
                StrongCryptoUtils.Encrypt(stringBlob.GetBuffer(), 0, stringBlob.Length);
                accessor.Write7BitEncodedInt(stringBlob.Length);
                accessor.Write(stringBlob.GetBuffer(), 0, stringBlob.Length);

                accessor.Write(signatureBlob.GetBuffer(), 0, signatureBlob.Length);
            }

            _isNew     = false;
            _isChanged = false;
        }
예제 #4
0
        private void WriteStrings(IBinaryAccessor accessor, ProjectWriteState state)
        {
            var strings = state.Strings;

            int count = strings.Count;

            var blob = new Blob();
            int pos  = 0;

            blob.Write7BitEncodedInt(ref pos, count);

            var encoding = Encoding.UTF8;

            for (int i = 0; i < count; i++)
            {
                blob.WriteLengthPrefixedString(ref pos, strings[i], encoding);
            }

            StrongCryptoUtils.Encrypt(blob.GetBuffer(), 0, blob.Length);

            accessor.Write7BitEncodedInt(blob.Length);
            accessor.Write(blob.GetBuffer(), 0, blob.Length);
        }