File: afflib_util.cpp

package info (click to toggle)
afflib 3.7.22-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,584 kB
  • sloc: cpp: 20,911; ansic: 15,912; makefile: 520; sh: 436; python: 192
file content (318 lines) | stat: -rw-r--r-- 7,299 bytes parent folder | download | duplicates (8)
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*
 * utility functions used by AFFLIB.
 * These functions do not actually read or write data into the AFF File.
 * Distributed under the Berkeley 4-part license.
 */

#include "affconfig.h"
#include "afflib.h"
#include "afflib_i.h"

#ifndef HAVE_ERR
#include <stdarg.h>
void err(int eval,const char *fmt,...)
{
  va_list ap;
  va_start(ap,fmt);
  vfprintf(stderr,fmt,ap);
  va_end(ap);
  fprintf(stderr,": %s\n",strerror(errno));
  exit(eval);
}
#endif

void af_err(int eval,const char *fmt,...)
{
  va_list ap;
  va_start(ap,fmt);
  vfprintf(stderr,fmt,ap);
  va_end(ap);
  if(af_error_str[0]) fprintf(stderr,": %s",af_error_str);
  if(errno) fprintf(stderr,": %s",strerror(errno));
  fputc('\n',stderr);
  exit(eval);
}


#ifndef HAVE_ERRX
#include <stdarg.h>
void errx(int eval,const char *fmt,...)
{
  va_list ap;
  va_start(ap,fmt);
  vfprintf(stderr,fmt,ap);
  fprintf(stderr,"%s\n",strerror(errno));
  va_end(ap);
  exit(eval);
}
#endif

#ifndef HAVE_WARN
#include <stdarg.h>
void	warn(const char *fmt, ...)
{
    va_list args;
    va_start(args,fmt);
    vfprintf(stderr,fmt, args);
    fprintf(stderr,": %s",strerror(errno));
}
#endif

#ifndef HAVE_WARNX
#include <stdarg.h>
void warnx(const char *fmt,...)
{
  va_list ap;
  va_start(ap,fmt);
  vfprintf(stderr,fmt,ap);
  va_end(ap);
}
#endif




/*
 * af_hexbuf:
 * Turn a binay string into a hex string, optionally with spaces.
 */

const char *af_hexbuf(char *dst,int dst_len,const unsigned char *bin,int bytes,int flag)
{
    int charcount = 0;
    const char *start = dst;		// remember where the start of the string is
    const char *fmt = (flag & AF_HEXBUF_UPPERCASE) ? "%02X" : "%02x";

    *dst = 0;				// begin with null termination
    while(bytes>0 && dst_len > 3){
	sprintf(dst,fmt,*bin); // convert the next byte
	dst += 2;
	bin += 1;
	dst_len -= 2;
	bytes--;
	charcount++;			// how many characters

	if((flag & AF_HEXBUF_SPACE4) && charcount%2==0){
	    *dst++ = ' ';
	    *dst   = '\000';
	    dst_len -= 1;
	}
    }
    return start;			// return the start
}


/* Add commas
 */
const char *af_commas(char buf[64],int64_t val)
{
    char tmp[64];
    char t2[64];
    int  negative = 0;

    buf[0] = 0;
    if(val==0){
	strcpy(buf,"0");
    }
    if(val<0){
	negative = 1;
	val = -val;
    }

    while(val>0){
	int digits = val % 1000;		// get the residue
	val = val / 1000;		// and shift what's over

	if(val>0){			// we will still have more to do
	    sprintf(tmp,",%03d",digits);	// so pad it with zeros and a comma
	}
	else {
	    sprintf(tmp,"%d",digits);	// otherwise just get the value
	}
	strcpy(t2,buf);			// copy out the buffer
	strcpy(buf,tmp);		// copy in what we just did
	strcat(buf,t2);			// and put back what was there
    }
    if(negative){
	strcpy(t2,buf);
	strcpy(buf,"-");
	strcat(buf,t2);
    }
    return buf;
}

uint64_t af_decode_q(unsigned char buf[8])
{
    struct aff_quad *q = (struct aff_quad *)buf;		// point q to buf.

    assert(sizeof(*q)==8);		// be sure!
    return (((uint64_t)ntohl(q->high)) << 32) + ((uint64_t)ntohl(q->low));
}

/* parse the segment number.
 * The extra %c picks up characters that might be after the number,
 * so that page5_hash doesn't match for page5.
 */
int64_t	af_segname_page_number(const char *name)
{
#ifdef KERNEL_LIBRARY
#define PAGE_NAME "page"
	if(_strnicmp(name,PAGE_NAME,strlen(PAGE_NAME))==0)
	{
		int64_t pagenum;
		for (int i=strlen(PAGE_NAME);i<strlen(name);i++)
			if (!isdigit(name[i])) return -1;

		pagenum = _atoi64(name+strlen(PAGE_NAME));
		return pagenum;
	}
#define SEG_NAME "seg"
	if(_strnicmp(name,SEG_NAME,strlen(SEG_NAME))==0)
	{
		int64_t pagenum;
		for (int i=strlen(SEG_NAME);i<strlen(name);i++)
			if (!isdigit(name[i])) return -1;

		pagenum = _atoi64(name+strlen(SEG_NAME));
		return pagenum;
	}
	return -1;
#else
    int64_t pagenum;
    char  ch;
    if(sscanf(name,AF_PAGE"%c",&pagenum,&ch)==1){
	return pagenum;			// new-style page number
    }
    if(sscanf(name,AF_SEG_D"%c",&pagenum,&ch)==1){
	return pagenum;			// old-style page number
    }
    return -1;
#endif
}

int64_t	af_segname_hash_page_number(const char *name,char *hash,int hashlen)
{
    char copy[AF_MAX_NAME_LEN];
    const char *cc = strchr((char *)name,'_');
    if(!cc) return -1;			// not possibly correct
    strlcpy(copy,name,sizeof(copy));
    char *dd = strchr(copy,'_');
    if(!dd) return -1;		        // really weird; shouldn't happen
    *dd++ = '\000';			// terminate at _
    if(strcmp(dd,"md5")!=0) return -1;	// not a valid hash
    int64_t page = af_segname_page_number(copy);
    if(page<0) return -1;		// wasn't what we wanted
    strlcpy(hash,dd,hashlen);
    return page;
}

int af_hasmeta(const char *infile)
{
    /* return 1 if the string has shell metacharacters */
    for(const char *cc = infile;*cc;cc++){
	switch(*cc){
	case '?': return 1;
	case '*': return 1;
	case '&': return 1;
	case '`': return 1;
	case '(': return 1;
	case ')': return 1;
	}
    }
    return 0;
}

/* It is a filestream if the filename begins file:// or has no "://" in it */
int af_is_filestream(const char *filename)
{
    if(strncmp(filename,"file://",7)==0) return 1;
    if(strstr(filename,"://")==0) return 1;
    return 0;
}






#ifndef HAVE_STRLCPY
size_t strlcpy(char *dest,const char *src,size_t dest_size)
{
    strncpy(dest,src,dest_size);
    dest[dest_size-1] = '\000';
    return strlen(dest);
}
#endif

#ifndef HAVE_STRLCAT
size_t strlcat(char *dest,const char *src,size_t dest_size)
{
    int dest_len = strlen(dest);
    int src_len  = strlen(src);
    int room     = dest_size - (dest_len +src_len+1);
    if(room>0){
	/* There is room; just copy over what we have and return */
	strcat(dest,src);
	return strlen(dest);
    }
    /* Not room; figure out how many bytes we can copy... */
    int left = dest_size - (dest_len+1);
    strncpy(dest+dest_len,src,left);
    dest[dest_len-1] = '\000';
    return strlen(dest);
}
#endif

/* Parse a URL. Allocate the parts if requested. Default protocol is "file", of course*/
void af_parse_url(const char *url,char **protocol,char **hostname,char **username,char **password,
		 int *port,char **path)
{
    const char *p1 = strstr(url,"://");
    if(!p1){
	if(protocol) *protocol = strdup("file");
	if(path)     *path     = strdup(url);
	return;
    }
    if(protocol){
	int len = p1-url;
	*protocol = (char *)malloc(len+1);
	strncpy(*protocol,url,len);
    }
    url = p1+3;		// move past ://

    const char *at = strchr(url,'@');
    if(at){					// we may have a username and/or password
	char *scratch = (char *)malloc(at-url+1);
	strncpy(scratch,url,at-url);
	scratch[at-url]='\000';
	char *colon = strchr(scratch,':');
	if(colon){
	    *colon = '\000';
	}
	if(username) *username = strdup(scratch);
	if(colon){
	    if(password) *password = strdup(colon+1);
	}
	free(scratch);
	url = at+1;
    }

    /* Process hostname if it exists */
    const char *slash = strchr(url,'/');
    if(slash){
	char *scratch = (char *)malloc(slash-url+1);
	strncpy(scratch,url,slash-url);
	scratch[slash-url]='\000';
	char *colon = strchr(scratch,':');
	if(colon){
	    *colon = '\000';
	}
	if(hostname) *hostname = strdup(scratch);
	if(colon){
	    if(port) *port = atoi(colon+1);
	}
	free(scratch);
	url = slash+1;
    }
    if(path) *path = strdup(url);	                // remember file name
}