new upstream release (3.3.0); modify package compatibility for Stretch
[ossec-hids.git] / src / shared / math_op.c
1 /* Copyright (C) 2009 Trend Micro Inc.
2  * All rights reserved.
3  *
4  * This program is a free software; you can redistribute it
5  * and/or modify it under the terms of the GNU General Public
6  * License (version 2) as published by the FSF - Free Software
7  * Foundation
8  */
9
10 #include "shared.h"
11
12
13 /* Get the first available prime after the provided value
14  * Returns 0 on error
15  */
16 unsigned int os_getprime(unsigned int val)
17 {
18     unsigned int i;
19     unsigned int max_i;
20
21     /* Value can't be even */
22     if ((val % 2) == 0) {
23         val++;
24     }
25
26     do {
27         /* We just need to check odd numbers up until half
28          * the size of the provided value
29          */
30         i = 3;
31         max_i = val / 2;
32         while (i <= max_i) {
33             /* Not prime */
34             if ((val % i) == 0) {
35                 break;
36             }
37             i += 2;
38         }
39
40         /* Prime */
41         if (i >= max_i) {
42             return (val);
43         }
44     } while (val += 2);
45
46     return (0);
47 }
48