]>
git.saurik.com Git - apple/security.git/blob - certificates/ota_cert_tool/ios_ota_cert_tool/CommonBuffering.c
8 #include "CommonBufferingPriv.h"
9 #include <AssertMacros.h>
13 CNBufferCreate(size_t chunksize
)
15 CNBufferRef retval
= CC_XMALLOC(sizeof(CNBuffer
));
16 __Require_Quiet(NULL
!= retval
, errOut
);
17 retval
->chunksize
= chunksize
;
18 retval
->bufferPos
= 0;
19 retval
->buf
= CC_XMALLOC(chunksize
);
20 __Require_Quiet(NULL
!= retval
->buf
, errOut
);
25 if(retval
->buf
) CC_XFREE(retval
->buf
, chunksize
);
26 CC_XFREE(retval
, sizeof(CNBuffer
));
32 CNBufferRelease(CNBufferRef
*bufRef
)
36 __Require_Quiet(NULL
!= bufRef
, out
);
38 if(ref
->buf
) CC_XFREE(ref
->buf
, chunksize
);
39 if(ref
) CC_XFREE(ref
, sizeof(CNBuffer
));
47 CNBufferProcessData(CNBufferRef bufRef
,
48 void *ctx
, const void *in
, const size_t inLen
, void *out
, size_t *outLen
,
49 cnProcessFunction pFunc
, cnSizeFunction sizeFunc
)
51 size_t blocksize
= bufRef
->chunksize
;
52 uint8_t *input
= (uint8_t *) in
, *output
= out
;
53 size_t inputLen
= inLen
, outputLen
, inputUsing
, outputAvailable
;
55 outputAvailable
= outputLen
= *outLen
;
57 if(sizeFunc(ctx
, bufRef
->bufferPos
+ inLen
) > outputAvailable
) return kCNBufferTooSmall
;
59 if(bufRef
->bufferPos
> 0) {
60 inputUsing
= CC_XMIN(blocksize
- bufRef
->bufferPos
, inputLen
);
61 CC_XMEMCPY(&bufRef
->buf
[bufRef
->bufferPos
], in
, inputUsing
);
62 bufRef
->bufferPos
+= inputUsing
;
63 if(bufRef
->bufferPos
< blocksize
) {
66 pFunc(ctx
, bufRef
->buf
, blocksize
, output
, &outputLen
);
67 inputLen
-= inputUsing
; input
+= inputUsing
;
68 output
+= outputLen
; *outLen
= outputLen
; outputAvailable
-= outputLen
;
69 bufRef
->bufferPos
= 0;
72 inputUsing
= inputLen
- inputLen
% blocksize
;
74 outputLen
= outputAvailable
;
75 pFunc(ctx
, input
, inputUsing
, output
, &outputLen
);
76 inputLen
-= inputUsing
; input
+= inputUsing
;
80 if(inputLen
> blocksize
) {
81 return kCNAlignmentError
;
82 } else if(inputLen
> 0) {
83 CC_XMEMCPY(bufRef
->buf
, input
, inputLen
);
84 bufRef
->bufferPos
= inputLen
;
91 CNBufferFlushData(CNBufferRef bufRef
,
92 void *ctx
, void *out
, size_t *outLen
,
93 cnProcessFunction pFunc
, cnSizeFunction sizeFunc
)
95 // size_t outputLen, outputAvailable;
96 // outputAvailable = outputLen = *outLen;
98 if(bufRef
->bufferPos
> 0) {
99 if(bufRef
->bufferPos
> bufRef
->chunksize
) return kCNAlignmentError
;
100 if(sizeFunc(ctx
, bufRef
->bufferPos
) > *outLen
) return kCNBufferTooSmall
;
101 pFunc(ctx
, bufRef
->buf
, bufRef
->bufferPos
, out
, outLen
);
111 CNBufferEmpty(CNBufferRef bufRef
)
113 return bufRef
->bufferPos
== 0;