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 Load(BuildModule module) { if (_module != null) { throw new InvalidOperationException(); } _module = module; if (_isNew) { if (_bufferLength == 0) { var image = _module.Image; _moduleTableOffset = BuildAssembly.RowSize * image.GetAssemblyCount(); _typeTableOffset = _moduleTableOffset + BuildModule.RowSize; _methodTableOffset = _typeTableOffset + (BuildType.RowSize * image.GetTypeDefCount()); _fieldTableOffset = _methodTableOffset + (BuildMethod.RowSize * image.GetMethodCount()); _propertyTableOffset = _fieldTableOffset + (BuildField.RowSize * image.GetFieldCount()); _eventTableOffset = _propertyTableOffset + (BuildProperty.RowSize * image.GetPropertyCount()); _resourceTableOffset = _eventTableOffset + (BuildEvent.RowSize * image.GetEventCount()); _bufferLength = _resourceTableOffset + (BuildResource.RowSize * image.GetManifestResourceCount()); } _buffer = new byte[_bufferLength]; _blobs = new BlobList(); _strings = new StringSerializer(); _signatures = new SignatureList(_strings); _objects = new ModuleObjectState(this); } else { using (var accessor = new StreamAccessor(new FileStream(_stateFilePath, FileMode.Open, FileAccess.Read, FileShare.None))) { _bufferLength = accessor.Read7BitEncodedInt(); _buffer = accessor.ReadBytes(_bufferLength); _objects = new ModuleObjectState(this, accessor); _blobs = new BlobList(accessor); var stringBlob = new Blob(accessor.ReadBytes(accessor.Read7BitEncodedInt())); StrongCryptoUtils.Decrypt(stringBlob.GetBuffer(), 0, stringBlob.Length); _strings = new StringSerializer(new BlobAccessor(stringBlob)); _signatures = new SignatureList(accessor, _strings); } } _strings.DelayWrite = true; _signatures.DelayWrite = true; }
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; }
private void ReadStrings(IBinaryAccessor accessor, ProjectReadState state) { int blobSize = accessor.Read7BitEncodedInt(); byte[] buffer = accessor.ReadBytes(blobSize); StrongCryptoUtils.Decrypt(buffer, 0, blobSize); var blob = new Blob(buffer); int pos = 0; int count = blob.Read7BitEncodedInt(ref pos); var strings = new string[count]; var encoding = Encoding.UTF8; for (int i = 0; i < count; i++) { strings[i] = blob.ReadLengthPrefixedString(ref pos, encoding); } state.Strings = strings; }
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); }