Revert "Revert "and added files""
[bcm963xx.git] / userapps / opensource / net-snmp / include / net-snmp / library / tools.h
1 /*
2  * tools.h
3  */
4
5 #ifndef _TOOLS_H
6 #define _TOOLS_H
7
8 #ifdef __cplusplus
9 extern          "C" {
10 #endif
11
12
13
14     /*
15      * General acros and constants.
16      */
17 #ifdef WIN32
18 #  define SNMP_MAXPATH MAX_PATH
19 #else
20 #  ifdef PATH_MAX
21 #    define SNMP_MAXPATH PATH_MAX
22 #  else
23 #    ifdef MAXPATHLEN
24 #      define SNMP_MAXPATH MAXPATHLEN
25 #    else
26 #      define SNMP_MAXPATH 1024         /* Should be safe enough */
27 #    endif
28 #  endif
29 #endif
30
31 #define SNMP_MAXBUF             (1024 * 4)
32 #define SNMP_MAXBUF_MEDIUM      1024
33 #define SNMP_MAXBUF_SMALL       512
34
35 #define SNMP_MAXBUF_MESSAGE     1500
36
37 #define SNMP_MAXOID             64
38
39 #define SNMP_FILEMODE_CLOSED    0600
40 #define SNMP_FILEMODE_OPEN      0644
41
42 #define BYTESIZE(bitsize)       ((bitsize + 7) >> 3)
43 #define ROUNDUP8(x)             ( ( (x+7) >> 3 ) * 8 )
44
45
46
47 #define SNMP_FREE(s)            if (s) { free((void *)s); s=NULL; }
48
49     /*
50      * XXX Not optimal everywhere. 
51      */
52 #define SNMP_MALLOC_STRUCT(s)   (struct s *) calloc(1, sizeof(struct s))
53 #define SNMP_MALLOC_TYPEDEF(td)  (td *) calloc(1, sizeof(td))
54 #define SNMP_ZERO(s,l)          if (s) memset(s, 0, l);
55
56
57 #define TOUPPER(c)      (c >= 'a' && c <= 'z' ? c - ('a' - 'A') : c)
58 #define TOLOWER(c)      (c >= 'A' && c <= 'Z' ? c + ('a' - 'A') : c)
59
60 #define HEX2VAL(s) \
61         ((isalpha(s) ? (TOLOWER(s)-'a'+10) : (TOLOWER(s)-'0')) & 0xf)
62 #define VAL2HEX(s)      ( (s) + (((s) >= 10) ? ('a'-10) : '0') )
63
64
65 #define SNMP_MAX(a,b) ((a) > (b) ? (a) : (b))
66 #define SNMP_MIN(a,b) ((a) > (b) ? (b) : (a))
67
68 #ifndef FALSE
69 #define FALSE 0
70 #endif
71 #ifndef TRUE
72 #define TRUE  1
73 #endif
74
75     /*
76      * QUIT the FUNction:
77      *      e       Error code variable
78      *      l       Label to goto to cleanup and get out of the function.
79      *
80      * XXX  It would be nice if the label could be constructed by the
81      *      preprocessor in context.  Limited to a single error return value.
82      *      Temporary hack at best.
83      */
84 #define QUITFUN(e, l)                   \
85         if ( (e) != SNMPERR_SUCCESS) {  \
86                 rval = SNMPERR_GENERR;  \
87                 goto l ;                \
88         }
89
90     /*
91      * DIFFTIMEVAL
92      *      Set <diff> to the difference between <now> (current) and <then> (past).
93      *
94      * ASSUMES that all inputs are (struct timeval)'s.
95      * Cf. system.c:calculate_time_diff().
96      */
97 #define DIFFTIMEVAL(now, then, diff)                    \
98 {                                                       \
99         now.tv_sec--;                                   \
100         now.tv_usec += 1000000L;                        \
101         diff.tv_sec  = now.tv_sec  - then.tv_sec;       \
102         diff.tv_usec = now.tv_usec - then.tv_usec;      \
103         if (diff.tv_usec > 1000000L){                   \
104                 diff.tv_usec -= 1000000L;               \
105                 diff.tv_sec++;                          \
106         }                                               \
107 }
108
109
110     /*
111      * ISTRANSFORM
112      * ASSUMES the minimum length for ttype and toid.
113      */
114 #define USM_LENGTH_OID_TRANSFORM        10
115
116 #define ISTRANSFORM(ttype, toid)                                        \
117         !snmp_oid_compare(ttype, USM_LENGTH_OID_TRANSFORM,              \
118                 usm ## toid ## Protocol, USM_LENGTH_OID_TRANSFORM)
119
120 #define ENGINETIME_MAX  2147483647      /* ((2^31)-1) */
121 #define ENGINEBOOT_MAX  2147483647      /* ((2^31)-1) */
122
123
124
125
126     /*
127      * Prototypes.
128      */
129
130     int             snmp_realloc(u_char ** buf, size_t * buf_len);
131
132     void            free_zero(void *buf, size_t size);
133
134     u_char         *malloc_random(size_t * size);
135     u_char         *malloc_zero(size_t size);
136     int             memdup(u_char ** to, const u_char * from, size_t size);
137
138     u_int           binary_to_hex(const u_char * input, size_t len,
139                                   char **output);
140     int             hex_to_binary2(const u_char * input, size_t len,
141                                    char **output);
142
143     int             snmp_decimal_to_binary(u_char ** buf, size_t * buf_len,
144                                            size_t * out_len,
145                                            int allow_realloc,
146                                            const char *decimal);
147     int             snmp_hex_to_binary(u_char ** buf, size_t * buf_len,
148                                        size_t * out_len, int allow_realloc,
149                                        const char *hex);
150     int             snmp_strcat(u_char ** buf, size_t * buf_len,
151                                 size_t * out_len, int allow_realloc,
152                                 const u_char * s);
153     char           *netsnmp_strdup_and_null(const u_char * from,
154                                             size_t from_len);
155
156     void            dump_chunk(const char *debugtoken, const char *title,
157                                const u_char * buf, int size);
158     char           *dump_snmpEngineID(const u_char * buf, size_t * buflen);
159
160     typedef void   *marker_t;
161     marker_t        atime_newMarker(void);
162     void            atime_setMarker(marker_t pm);
163     long            atime_diff(marker_t first, marker_t second);
164     u_long          uatime_diff(marker_t first, marker_t second);       /* 1/1000th sec */
165     u_long          uatime_hdiff(marker_t first, marker_t second);      /* 1/100th sec */
166     int             atime_ready(marker_t pm, int deltaT);
167     int             uatime_ready(marker_t pm, unsigned int deltaT);
168
169     int             marker_tticks(marker_t pm);
170     int             timeval_tticks(struct timeval *tv);
171
172 #ifdef __cplusplus
173 }
174 #endif
175 #endif                          /* _TOOLS_H */