Skip to content

Commit 9a0af9d

Browse files
authored
Remove generic invocation of sizeOf (#18)
1 parent 53856e7 commit 9a0af9d

15 files changed

+97
-73
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 0.5.2
2+
3+
* Migrates `dart:ffi`s generic `sizeOf` uses, which will be removed in Dart 2.13.
4+
15
# 0.5.1
26
* Uses `package:ffigen` to generate the Dart bindings.
37
* Bumped SDK constraint to Dart 2.12.

example/pubspec.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ packages:
436436
path: ".."
437437
relative: true
438438
source: path
439-
version: "0.5.1"
439+
version: "0.5.2"
440440
webdriver:
441441
dependency: transitive
442442
description:

lib/src/impl_ffi/impl_ffi.aescbc.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,16 @@ Stream<Uint8List> _aesCbcEncryptOrDecrypt(
5959
const bufSize = 4096;
6060

6161
// Allocate an input buffer
62-
final inBuf = scope.allocate<ffi.Uint8>(count: bufSize);
62+
final inBuf = scope<ffi.Uint8>(bufSize);
6363
final inData = inBuf.asTypedList(bufSize);
6464

6565
// Allocate an output buffer, notice that BoringSSL says output cannot be
6666
// more than input size + blockSize - 1
67-
final outBuf = scope.allocate<ffi.Uint8>(count: bufSize + blockSize);
67+
final outBuf = scope<ffi.Uint8>(bufSize + blockSize);
6868
final outData = outBuf.asTypedList(bufSize + blockSize);
6969

7070
// Allocate and output length integer
71-
final outLen = scope.allocate<ffi.Int32>();
71+
final outLen = scope<ffi.Int32>();
7272

7373
// Process data from source
7474
await for (final data in source) {

lib/src/impl_ffi/impl_ffi.aesctr.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,16 +104,16 @@ Stream<Uint8List> _aesCtrEncryptOrDecrypt(
104104
const bufSize = 4096;
105105

106106
// Allocate an input buffer
107-
final inBuf = scope.allocate<ffi.Uint8>(count: bufSize);
107+
final inBuf = scope<ffi.Uint8>(bufSize);
108108
final inData = inBuf.asTypedList(bufSize);
109109

110110
// Allocate an output buffer, notice that BoringSSL says output cannot be
111111
// more than input size + blockSize - 1
112-
final outBuf = scope.allocate<ffi.Uint8>(count: bufSize + blockSize);
112+
final outBuf = scope<ffi.Uint8>(bufSize + blockSize);
113113
final outData = outBuf.asTypedList(bufSize + blockSize);
114114

115115
// Allocate and output length integer
116-
final outLen = scope.allocate<ffi.Int32>();
116+
final outLen = scope<ffi.Int32>();
117117

118118
// Process data from source
119119
var isBeforeWrapAround = true;

lib/src/impl_ffi/impl_ffi.aesgcm.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ Future<Uint8List> _aesGcmEncryptDecrypt(
7474
);
7575

7676
if (isEncrypt) {
77-
final outLen = scope.allocate<ffi.IntPtr>();
77+
final outLen = scope<ffi.IntPtr>();
7878
final maxOut = data.length + ssl.EVP_AEAD_max_overhead(aead);
7979
return _withOutPointer(maxOut, (ffi.Pointer<ffi.Uint8> out) {
8080
_checkOpIsOne(ssl.EVP_AEAD_CTX_seal(
@@ -91,7 +91,7 @@ Future<Uint8List> _aesGcmEncryptDecrypt(
9191
));
9292
}).sublist(0, outLen.value);
9393
} else {
94-
final outLen = scope.allocate<ffi.IntPtr>();
94+
final outLen = scope<ffi.IntPtr>();
9595
return _withOutPointer(data.length, (ffi.Pointer<ffi.Uint8> out) {
9696
_checkOpIsOne(ssl.EVP_AEAD_CTX_open(
9797
ctx,

lib/src/impl_ffi/impl_ffi.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
library impl_ffi;
1717

1818
import 'dart:async';
19+
import 'dart:ffi' show Allocator;
1920
import 'dart:typed_data';
2021
import 'dart:convert' show utf8, base64Url;
2122
import 'dart:ffi' as ffi;

lib/src/impl_ffi/impl_ffi.ecdsa.dart

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@ Uint8List _convertEcdsaDerSignatureToWebCryptoSignature(
108108
ec,
109109
)));
110110

111-
return _withAllocation(2, (ffi.Pointer<ffi.Pointer<BIGNUM>> RS) {
111+
return _withAllocation(_sslAlloc<ffi.Pointer<BIGNUM>>(2),
112+
(ffi.Pointer<ffi.Pointer<BIGNUM>> RS) {
112113
// Access R and S from the ecdsa signature
113114
final R = RS.elementAt(0);
114115
final S = RS.elementAt(1);
@@ -164,7 +165,8 @@ Uint8List? _convertEcdsaWebCryptoSignatureToDerSignature(
164165
message: 'internal error formatting signature');
165166
scope.defer(() => ssl.ECDSA_SIG_free(ecdsa));
166167

167-
return _withAllocation(2, (ffi.Pointer<ffi.Pointer<BIGNUM>> RS) {
168+
return _withAllocation(_sslAlloc<ffi.Pointer<BIGNUM>>(2),
169+
(ffi.Pointer<ffi.Pointer<BIGNUM>> RS) {
168170
// Access R and S from the ecdsa signature
169171
final R = RS.elementAt(0);
170172
final S = RS.elementAt(1);
@@ -214,7 +216,8 @@ class _EcdsaPrivateKey implements EcdsaPrivateKey {
214216
);
215217

216218
await _streamToUpdate(data, ctx, ssl.EVP_DigestSignUpdate);
217-
return _withAllocation(1, (ffi.Pointer<ffi.IntPtr> len) {
219+
return _withAllocation(_sslAlloc<ffi.IntPtr>(),
220+
(ffi.Pointer<ffi.IntPtr> len) {
218221
len.value = 0;
219222
_checkOpIsOne(ssl.EVP_DigestSignFinal(ctx, ffi.nullptr, len));
220223
return _withOutPointer(len.value, (ffi.Pointer<ffi.Uint8> p) {

lib/src/impl_ffi/impl_ffi.hmac.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@ class _HmacSecretKey implements HmacSecretKey {
126126

127127
final size = ssl.HMAC_size(ctx);
128128
_checkOp(size > 0);
129-
return _withAllocation(1, (ffi.Pointer<ffi.Uint32> psize) async {
129+
return _withAllocation(_sslAlloc<ffi.Uint32>(),
130+
(ffi.Pointer<ffi.Uint32> psize) async {
130131
psize.value = size;
131132
return _withOutPointer(size, (ffi.Pointer<ffi.Uint8> p) {
132133
_checkOp(ssl.HMAC_Final(ctx, p, psize) == 1);

lib/src/impl_ffi/impl_ffi.random.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ void fillRandomBytes(TypedData destination) {
1919
destination.offsetInBytes,
2020
destination.lengthInBytes,
2121
);
22-
_withAllocation(dest.length, (ffi.Pointer<ffi.Uint8> p) {
22+
_withAllocation(_sslAlloc<ffi.Uint8>(dest.length),
23+
(ffi.Pointer<ffi.Uint8> p) {
2324
_checkOp(ssl.RAND_bytes(p, dest.length) == 1);
2425
dest.setAll(0, p.asTypedList(dest.length));
2526
});

lib/src/impl_ffi/impl_ffi.rsa_common.dart

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,8 @@ Map<String, dynamic> _exportJwkRsaPrivateOrPublicKey(
194194
}
195195

196196
// Public key parameters
197-
final n = scope.allocate<ffi.Pointer<BIGNUM>>();
198-
final e = scope.allocate<ffi.Pointer<BIGNUM>>();
197+
final n = scope<ffi.Pointer<BIGNUM>>();
198+
final e = scope<ffi.Pointer<BIGNUM>>();
199199
ssl.RSA_get0_key(rsa, n, e, ffi.nullptr);
200200

201201
if (!isPrivateKey) {
@@ -208,19 +208,19 @@ Map<String, dynamic> _exportJwkRsaPrivateOrPublicKey(
208208
).toJson();
209209
}
210210

211-
final d = scope.allocate<ffi.Pointer<BIGNUM>>();
211+
final d = scope<ffi.Pointer<BIGNUM>>();
212212
ssl.RSA_get0_key(rsa, ffi.nullptr, ffi.nullptr, d);
213213

214214
// p, q, dp, dq, qi is optional in:
215215
// // https://tools.ietf.org/html/rfc7518#section-6.3.2
216216
// but explicitly required when exporting in Web Crypto.
217-
final p = scope.allocate<ffi.Pointer<BIGNUM>>();
218-
final q = scope.allocate<ffi.Pointer<BIGNUM>>();
217+
final p = scope<ffi.Pointer<BIGNUM>>();
218+
final q = scope<ffi.Pointer<BIGNUM>>();
219219
ssl.RSA_get0_factors(rsa, p, q);
220220

221-
final dp = scope.allocate<ffi.Pointer<BIGNUM>>();
222-
final dq = scope.allocate<ffi.Pointer<BIGNUM>>();
223-
final qi = scope.allocate<ffi.Pointer<BIGNUM>>();
221+
final dp = scope<ffi.Pointer<BIGNUM>>();
222+
final dq = scope<ffi.Pointer<BIGNUM>>();
223+
final qi = scope<ffi.Pointer<BIGNUM>>();
224224
ssl.RSA_get0_crt_params(rsa, dp, dq, qi);
225225

226226
return JsonWebKey(

0 commit comments

Comments
 (0)