rand.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. #define APR_WANT_MEMFUNC
  17. #include "fspr_want.h"
  18. #include "fspr_general.h"
  19. #include "fspr_private.h"
  20. #if APR_HAS_RANDOM
  21. #include <nks/plat.h>
  22. static int NXSeedRandomInternal( size_t width, void *seed )
  23. {
  24. static int init = 0;
  25. int *s = (int *) seed;
  26. union { int x; char y[4]; } u;
  27. if (!init) {
  28. srand(NXGetSystemTick());
  29. init = 1;
  30. }
  31. if (width > 3)
  32. {
  33. do
  34. {
  35. *s++ = rand();
  36. }
  37. while ((width -= 4) > 3);
  38. }
  39. if (width > 0)
  40. {
  41. char *p = (char *) s;
  42. u.x = rand();
  43. while (width > 0)
  44. *p++ = u.y[width--];
  45. }
  46. return APR_SUCCESS;
  47. }
  48. APR_DECLARE(fspr_status_t) fspr_generate_random_bytes(unsigned char *buf,
  49. fspr_size_t length)
  50. {
  51. if (NXSeedRandom(length, buf) != 0) {
  52. return NXSeedRandomInternal (length, buf);
  53. }
  54. return APR_SUCCESS;
  55. }
  56. #endif /* APR_HAS_RANDOM */