]> git.saurik.com Git - apple/security.git/blob - OSX/utilities/SecBuffer.c
Security-59306.41.2.tar.gz
[apple/security.git] / OSX / utilities / SecBuffer.c
1 //
2 // SecBuffer.c
3 // utilities
4 //
5 // Created by Mitch Adler on 3/6/15.
6 // Copyright © 2015 Apple Inc. All rights reserved.
7 //
8
9 #include <utilities/SecBuffer.h>
10
11 #include <strings.h>
12
13 #define stackBufferSizeLimit 2048
14
15 void PerformWithBuffer(size_t size, void (^operation)(size_t size, uint8_t *buffer)) {
16 if (size == 0) {
17 operation(0, NULL);
18 } else if (size <= stackBufferSizeLimit) {
19 uint8_t buffer[size];
20 operation(size, buffer);
21 } else {
22 uint8_t *buffer = malloc(size);
23
24 operation(size, buffer);
25
26 if (buffer)
27 free(buffer);
28 }
29 }
30
31 void PerformWithBufferAndClear(size_t size, void (^operation)(size_t size, uint8_t *buffer)) {
32 PerformWithBuffer(size, ^(size_t size, uint8_t *buffer) {
33 operation(size, buffer);
34
35 bzero(buffer, size);
36 });
37 }