Imported Upstream version 2.7
[ossec-hids.git] / src / shared / math_op.c
1 /* @(#) $Id: ./src/shared/math_op.c, 2011/09/08 dcid Exp $
2  */
3
4 /* Copyright (C) 2009 Trend Micro Inc.
5  * All rights reserved.
6  *
7  * This program is a free software; you can redistribute it
8  * and/or modify it under the terms of the GNU General Public
9  * License (version 2) as published by the FSF - Free Software
10  * Foundation
11  *
12  * License details at the LICENSE file included with OSSEC or
13  * online at: http://www.ossec.net/en/licensing.html
14  */
15
16
17 #include "shared.h"
18
19
20 /** int os_getprime
21  * Get the first available prime after the provided value.
22  * Returns 0 on error.
23  */
24 int os_getprime(int val)
25 {
26     int i;
27     int max_i;
28
29     /* Value can't be even */
30     if((val % 2) == 0)
31     {
32         val++;
33     }
34
35
36     do
37     {
38         /* We just need to check odd numbers up until half
39          * the size of the provided value.
40          */
41         i = 3;
42         max_i = val/2;
43         while(i <= max_i)
44         {
45             /* Not prime */
46             if((val % i) == 0)
47             {
48                 break;
49             }
50             i += 2;
51         }
52
53         /* Prime */
54         if(i >= max_i)
55         {
56             return(val);
57         }
58     }while(val += 2);
59
60     return(0);
61 }
62
63
64 /* EOF */