1/* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements.  See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License.  You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define APR_WANT_MEMFUNC
18#include "apr_want.h"
19#include "apr_general.h"
20#include "apr_private.h"
21
22#if APR_HAS_RANDOM
23
24#include <nks/plat.h>
25
26static int NXSeedRandomInternal( size_t width, void *seed )
27{
28    static int init = 0;
29    int                        *s = (int *) seed;
30    union { int x; char y[4]; } u;
31
32    if (!init) {
33        srand(NXGetSystemTick());
34        init = 1;
35    }
36
37    if (width > 3)
38    {
39        do
40        {
41            *s++ = rand();
42        }
43        while ((width -= 4) > 3);
44    }
45
46    if (width > 0)
47    {
48        char *p = (char *) s;
49
50        u.x = rand();
51
52        while (width > 0)
53           *p++ = u.y[width--];
54    }
55
56    return APR_SUCCESS;
57}
58
59APR_DECLARE(apr_status_t) apr_generate_random_bytes(unsigned char *buf,
60                                                    apr_size_t length)
61{
62    if (NXSeedRandom(length, buf) != 0) {
63        return NXSeedRandomInternal (length, buf);
64    }
65    return APR_SUCCESS;
66}
67
68
69
70#endif /* APR_HAS_RANDOM */
71