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