diff --git a/Handle/BNDataBuffer.cs b/Handle/BNDataBuffer.cs index 5765f78..8cbd3ee 100644 --- a/Handle/BNDataBuffer.cs +++ b/Handle/BNDataBuffer.cs @@ -2,6 +2,7 @@ using System.Collections; using System.Collections.Generic; using System.Runtime.InteropServices; +using System.Text; using Microsoft.Win32.SafeHandles; namespace BinaryNinja @@ -179,6 +180,27 @@ public string ToBase64() ); } + /// + /// Convert the data buffer contents to a base64-encoded string. Mirrors Python + /// DataBuffer.base64_encode. + /// + public string Base64Encode() + { + return this.ToBase64(); + } + + /// + /// Decode the ASCII base64 text stored in this buffer. + /// + public DataBuffer Base64Decode() + { + string encoded = Encoding.UTF8.GetString(this.Contents); + + return DataBuffer.MustTakeHandle( + NativeMethods.BNDecodeBase64(encoded) + ); + } + /// /// Convert the data buffer contents to an escaped string representation. /// @@ -189,6 +211,78 @@ public string ToEscapedString(bool nullTerminates = false , bool escapePrintable ); } + /// + /// Convert the buffer contents to Binary Ninja's escaped string representation. Mirrors + /// Python DataBuffer.escape. + /// + public string Escape(bool nullTerminates = false, bool escapePrintable = false) + { + return this.ToEscapedString(nullTerminates, escapePrintable); + } + + /// + /// Decode Binary Ninja escaped-string text stored in this buffer. + /// + public DataBuffer Unescape() + { + string escaped = Encoding.UTF8.GetString(this.Contents); + + return DataBuffer.MustTakeHandle( + NativeMethods.BNDecodeEscapedString(escaped) + ); + } + + /// + /// Compress this buffer using zlib, or return null when the core cannot compress it. + /// + public DataBuffer? ZlibCompress() + { + return DataBuffer.TakeHandle( + NativeMethods.BNZlibCompress(this.handle) + ); + } + + /// + /// Decompress this zlib buffer, or return null when the data is not valid zlib data. + /// + public DataBuffer? ZlibDecompress() + { + return DataBuffer.TakeHandle( + NativeMethods.BNZlibDecompress(this.handle) + ); + } + + /// + /// Decompress this LZMA stream, or return null when the data is not valid LZMA data. + /// + public DataBuffer? LzmaDecompress() + { + return DataBuffer.TakeHandle( + NativeMethods.BNLzmaDecompress(this.handle) + ); + } + + /// + /// Decompress this raw LZMA2 stream, or return null when the data is not valid LZMA2 + /// data. + /// + public DataBuffer? Lzma2Decompress() + { + return DataBuffer.TakeHandle( + NativeMethods.BNLzma2Decompress(this.handle) + ); + } + + /// + /// Decompress this XZ stream, or return null when the data is not valid XZ data. + /// + public DataBuffer? XzDecompress() + { + return DataBuffer.TakeHandle( + NativeMethods.BNXzDecompress(this.handle) + ); + } + public DataBuffer GetSlice(ulong start , ulong length) { return DataBuffer.MustTakeHandle( @@ -220,4 +314,4 @@ public void Assign(DataBuffer other) NativeMethods.BNAssignDataBuffer(this.handle , other.DangerousGetHandle()); } } -} \ No newline at end of file +}