private static byte[] GetDynamicBuffer <THandle>(NegativeSizeReadMethod <THandle> method, THandle handle) { int negativeSize = method(handle, null, 0); if (negativeSize > 0) { throw new CryptographicException(); } byte[] bytes = new byte[-negativeSize]; int ret = method(handle, bytes, bytes.Length); if (ret != 1) { throw new CryptographicException(); } return(bytes); }
private static ArraySegment <byte> RentDynamicBuffer <THandle>(NegativeSizeReadMethod <THandle> method, THandle handle) { int negativeSize = method(handle, null, 0); if (negativeSize > 0) { throw Interop.Crypto.CreateOpenSslCryptographicException(); } int targetSize = -negativeSize; byte[] bytes = CryptoPool.Rent(targetSize); int ret = method(handle, bytes, targetSize); if (ret != 1) { CryptoPool.Return(bytes); throw Interop.Crypto.CreateOpenSslCryptographicException(); } return(new ArraySegment <byte>(bytes, 0, targetSize)); }