1 /*****************************************************************
2 |
3 |    AP4 - Win32 Random Byte Generator implementation
4 |
5 |    Copyright 2002-2011 Axiomatic Systems, LLC
6 |
7 |
8 |    This file is part of Bento4/AP4 (MP4 Atom Processing Library).
9 |
10 |    Unless you have obtained Bento4 under a difference license,
11 |    this version of Bento4 is Bento4|GPL.
12 |    Bento4|GPL is free software; you can redistribute it and/or modify
13 |    it under the terms of the GNU General Public License as published by
14 |    the Free Software Foundation; either version 2, or (at your option)
15 |    any later version.
16 |
17 |    Bento4|GPL is distributed in the hope that it will be useful,
18 |    but WITHOUT ANY WARRANTY; without even the implied warranty of
19 |    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 |    GNU General Public License for more details.
21 |
22 |    You should have received a copy of the GNU General Public License
23 |    along with Bento4|GPL; see the file COPYING.  If not, write to the
24 |    Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
25 |    02111-1307, USA.
26 |
27 ****************************************************************/
28 
29 /*----------------------------------------------------------------------
30 |   includes
31 +---------------------------------------------------------------------*/
32 #define _CRT_RAND_S
33 #include <stdlib.h>
34 
35 #include "Ap4Utils.h"
36 
37 /*----------------------------------------------------------------------
38 |   AP4_System_GenerateRandomBytes
39 +---------------------------------------------------------------------*/
40 AP4_Result
AP4_System_GenerateRandomBytes(AP4_UI08 * buffer,AP4_Size buffer_size)41 AP4_System_GenerateRandomBytes(AP4_UI08* buffer, AP4_Size buffer_size)
42 {
43     AP4_SetMemory(buffer, 0, buffer_size);
44 
45 	while (buffer_size) {
46 		unsigned int r;
47 		errno_t result = rand_s(&r);
48 		if (result != 0) return AP4_FAILURE;
49 
50 		unsigned int chunk = buffer_size < sizeof(r) ? buffer_size : sizeof(r);
51 		memcpy(buffer, (void*)&r, chunk);
52 		buffer += chunk;
53 		buffer_size -= chunk;
54 	}
55 
56     return AP4_SUCCESS;
57 }
58 
59 
60 
61 
62 
63 
64 
65 
66 
67 
68 
69