summaryrefslogtreecommitdiffstats
path: root/include/sh_utils.h
blob: 2f4519c638128f751e234dc930dd9c0b50914501 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/* SAMHAIN file system integrity testing                                   */
/* Copyright (C) 1999 Rainer Wichmann                                      */
/*                                                                         */
/*  This program is free software; you can redistribute it                 */
/*  and/or modify                                                          */
/*  it under the terms of the GNU General Public License as                */
/*  published by                                                           */
/*  the Free Software Foundation; either version 2 of the License, or      */
/*  (at your option) any later version.                                    */
/*                                                                         */
/*  This program is distributed in the hope that it will be useful,        */
/*  but WITHOUT ANY WARRANTY; without even the implied warranty of         */
/*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          */
/*  GNU General Public License for more details.                           */
/*                                                                         */
/*  You should have received a copy of the GNU General Public License      */
/*  along with this program; if not, write to the Free Software            */
/*  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              */


#ifndef SH_UTILS_H
#define SH_UTILS_H

#include <stdarg.h>

#include "slib.h"

#include "sh_error.h"
#include "sh_unix.h"

#define S_FMT_STRING   1
#define S_FMT_ULONG    2
#define S_FMT_TIME     3
#define S_FMT_LONG     4


typedef struct ft_struc {
  char            fchar;
  int             type;
  unsigned long   data_ulong;
  long            data_long;
  char           *data_str;
} st_format;

/* returns allocated string
 */
char * sh_util_formatted (const char * fmt, st_format * ftab);

typedef struct sh_timeout_struct {
  UINT64   time_last;
  UINT64   time_dist;
  int      flag_ok;
} SH_TIMEOUT;

int sh_util_timeout_check (SH_TIMEOUT * sh_timer);

/*  This is a maximally equidistributed combined Tausworthe
 *  generator. 
 */
UINT32 taus_get            (void);  
double taus_get_double     (void *vstate);  /* fast */
int    taus_seed           (void);

/* returns allocated memory
 */
char * sh_util_strdup (const char * str) SH_GNUC_MALLOC;
char * sh_util_strdup_track (const char * str, 
			     char * file, int line) SH_GNUC_MALLOC;

/* returns allocated memory
 */
char * sh_util_strdup_l (const char * str, size_t len) SH_GNUC_MALLOC;

/* returns pointer within str
 */
char * sh_util_strsep (char **str, const char *delim);

/* compactify verbose acl text, returns allocated memory
 */
char * sh_util_acl_compact (char * buf, ssize_t len);

/* set signature type HASH-TIGER/HMAC-TIGER
 */
int sh_util_sigtype (const char * c);

/* compute a signature
 */
char * sh_util_siggen (char * hexkey,  
		       char * text, size_t textlen, 
		       char * sigbuf, size_t sigbuflen);

/* eval boolean input
 */
int sh_util_flagval(const char * c, int * fval);

/* ask if a file should be updated (returns S_TRUE/S_FALSE)
 */
int sh_util_ask_update(const char * path);
int sh_util_set_interactive(const char * str);
int sh_util_update_file (const char * str);

/* don't log output files
 */
int sh_util_hidesetup(const char * c);

/* valif utf-8 string
 */
int sh_util_valid_utf8 (const unsigned char * str);

/* filenames are utf8
 */
int sh_util_obscure_utf8 (const char * c);

/* exceptions to obscure name check
 */
int sh_util_obscure_ok (const char * str);

/* output a hexchar[2]; i2h must be char[2]
 */
char * sh_util_charhex( unsigned char c, char * i2h );

/* read a hexchar, return int value (0-15)
 */
int sh_util_hexchar( char c ) SH_GNUC_CONST;

/* change verifier 
 */
int sh_util_set_newkey (const char * str);

/* server mode 
 */
int sh_util_setserver (const char * dummy);

/* a simple compressor
 */
size_t sh_util_compress (char * dest, char * src, size_t dest_size);

/* an even simpler en-/decoder 
 */
void sh_util_encode (char * data, char * salt, int mode, char fill);

/* copy len ( < 4) bytes from (char *) (long src) to (char *) dest,
 * determine the four LSB's and use them (independent of sizeof(long))
 */
void sh_util_cpylong (char * dest, const char * src, int len );

/* set timer for main loop
 */
int sh_util_setlooptime (const char * str);

/* whether init or check the database
 */
int  sh_util_setchecksum (const char * str);

/* compare an in_string against a regular expression regex_str
   return GOOD on successful match
*/
int sh_util_regcmp (char * regex_str, char * in_str);


/* returns freshly allocated memory, return value should be free'd.
 * Argument list must be NULL terminated.
 */
char * sh_util_strconcat (const char * arg1, ...) SH_GNUC_MALLOC SH_GNUC_SENTINEL;

/* check if string is numeric only
 */
int sh_util_isnum (const char *str) SH_GNUC_PURE;

/* init a key w/random string
 */
int sh_util_keyinit (char * buf, long size);


/* returns freshly allocated memory, return value should be free'd
 */
char * sh_util_dirname(const char * fullpath);

/* returns freshly allocated memory, return value should be free'd
 */
char * sh_util_safe_name (const char * name) SH_GNUC_MALLOC SH_GNUC_PURE;

char * sh_util_safe_name_keepspace (const char * name) SH_GNUC_MALLOC SH_GNUC_PURE;

/* check max size of printf result string
 */
int sh_util_printf_maxlength (const char * fmt, va_list  vl);

/* check for obscure filenames
 */
int sh_util_obscurename (ShErrLevel level, const char * name, int flag);

/* returns freshly allocated memory, return value should be free'd
 */
char * sh_util_basename(const char * fullpath);

/* required size (including terminating NULL) for string of strlen l
 */
#define SH_B64_SIZ(l)  (1 + ((((l) + 2) / 3) * 4))

/* return len of encoded string
 */
size_t sh_util_base64_enc (unsigned char * out, const unsigned char * instr, 
			   size_t lin);

/* return allocated encoded string in out, return its len
 */
size_t sh_util_base64_enc_alloc (char **out, const char *in, size_t inlen);

/* return len of decoded string
 */  
size_t sh_util_base64_dec (unsigned char *out, const unsigned char *in, size_t lin);

/* return allocated decoded string in out, return its len
 */  
size_t sh_util_base64_dec_alloc (unsigned char **out, const unsigned char *in, size_t lin);

#endif