3434#define CONTENT_FILE_NAME "benchmark-content.bin"
3535#define ENCODED_FILE_NAME "test-stream-dec.p7b"
3636#define DECODED_FILE_NAME "benchmark-decrypted.bin"
37- #define TEST_STREAM_CHUNK_SIZE 1000
37+ static int chunkSz = 1000 ;
3838
3939struct timeval startTime ;
4040
@@ -104,7 +104,7 @@ int CreateContentFile(double contentSz)
104104typedef struct BENCHMARK_IO {
105105 FILE * in ;
106106 FILE * out ;
107- byte buf [ TEST_STREAM_CHUNK_SIZE ] ;
107+ byte * buf ;
108108} BENCHMARK_IO ;
109109
110110
@@ -120,7 +120,7 @@ static int GetContentCB(PKCS7* pkcs7, byte** content, void* ctx)
120120 BENCHMARK_IO * io = (BENCHMARK_IO * )ctx ;
121121
122122 if (io != NULL ) {
123- ret = fread (io -> buf , 1 , TEST_STREAM_CHUNK_SIZE , io -> in );
123+ ret = fread (io -> buf , 1 , chunkSz , io -> in );
124124 if (ret > 0 ) {
125125 * content = io -> buf ;
126126 }
@@ -203,7 +203,8 @@ static int EncodePKCS7Bundle(double contentSz, WC_RNG* rng)
203203 if (ret == 0 ) {
204204 io .in = fopen (CONTENT_FILE_NAME , "rb" );
205205 io .out = fopen (ENCODED_FILE_NAME , "wb" );
206- if (io .in == NULL || io .out == NULL ) {
206+ io .buf = (byte * )malloc (chunkSz );
207+ if (io .in == NULL || io .out == NULL || io .buf == NULL ) {
207208 printf ("Failed to open the IO files\n" );
208209 ret = -1 ;
209210 }
@@ -240,6 +241,9 @@ static int EncodePKCS7Bundle(double contentSz, WC_RNG* rng)
240241 if (io .in != NULL ) {
241242 fclose (io .in );
242243 }
244+ if (io .buf != NULL ) {
245+ free (io .buf );
246+ }
243247 wc_PKCS7_Free (pkcs7 );
244248
245249 return 0 ;
@@ -275,9 +279,15 @@ static int DecodePKCS7Bundle(void)
275279 FILE * f = NULL ;
276280 FILE * out = NULL ;
277281 double totalSz = 0 ;
278- byte testStreamBuffer [ TEST_STREAM_CHUNK_SIZE ] ;
282+ byte * testStreamBuffer ;
279283 int testStreamBufferSz = 0 ;
280284
285+ testStreamBuffer = (byte * )malloc (chunkSz );
286+ if (testStreamBuffer == NULL ) {
287+ printf ("Failed to malloc temporary buffer to hold data to process\n" );
288+ ret = -1 ;
289+ }
290+
281291 if (ret == 0 ) {
282292 f = fopen (ENCODED_FILE_NAME , "rb" );
283293 if (f == NULL ) {
@@ -323,8 +333,7 @@ static int DecodePKCS7Bundle(void)
323333 if (ret == 0 ) {
324334 rewind (f ); /* start from the beginning of the file */
325335 do {
326- testStreamBufferSz = (int )XFREAD (testStreamBuffer , 1 ,
327- sizeof (testStreamBuffer ), f );
336+ testStreamBufferSz = (int )XFREAD (testStreamBuffer , 1 , chunkSz , f );
328337 if (testStreamBufferSz == 0 ) {
329338 printf ("Read 0 bytes from file..." );
330339 if (feof (f )) {
@@ -354,6 +363,10 @@ static int DecodePKCS7Bundle(void)
354363 fclose (out );
355364 }
356365
366+ if (testStreamBuffer != NULL ) {
367+ free (testStreamBuffer );
368+ }
369+
357370 wc_PKCS7_Free (pkcs7 );
358371
359372 if (ret == 0 ) {
@@ -373,7 +386,16 @@ int main(int argc, char** argv)
373386 int ret ;
374387
375388 if (argc > 1 ) {
389+ if (strcmp (argv [1 ], "-h" ) == 0 ) {
390+ printf ("USAGE: %s <content data size> <chunk to read at once>\n" ,
391+ argv [0 ]);
392+ return 1 ;
393+ }
376394 contentSz = atof (argv [1 ]);
395+
396+ if (argc > 2 ) {
397+ chunkSz = atoi (argv [2 ]);
398+ }
377399 }
378400
379401 ret = wolfCrypt_Init ();
@@ -388,8 +410,7 @@ int main(int argc, char** argv)
388410
389411 if (ret == 0 ) {
390412 printf ("Benchmarking with content size of %.0f bytes\n" , contentSz );
391- printf ("Reading and writing files in chuncks of %d bytes\n" ,
392- TEST_STREAM_CHUNK_SIZE );
413+ printf ("Reading and writing files in chunks of %d bytes\n" , chunkSz );
393414 printf ("Using AES-256 CBC encryption\n" );
394415 printf ("Using RSA-2048 key\n\n" );
395416
0 commit comments