Exemple #1
0
 static PROffset32
 search(PROffset32 *I,unsigned char *old,PROffset32 oldsize,
        unsigned char *newbuf,PROffset32 newsize,PROffset32 st,PROffset32 en,PROffset32 *pos)
 {
 	PROffset32 x,y;
 
 	if(en-st<2) {
 		x=matchlen(old+I[st],oldsize-I[st],newbuf,newsize);
 		y=matchlen(old+I[en],oldsize-I[en],newbuf,newsize);
 
 		if(x>y) {
 			*pos=I[st];
 			return x;
 		} else {
 			*pos=I[en];
 			return y;
 		}
 	};
 
 	x=st+(en-st)/2;
 	if(memcmp(old+I[x],newbuf,MIN(oldsize-I[x],newsize))<0) {
 		return search(I,old,oldsize,newbuf,newsize,x,en,pos);
 	} else {
 		return search(I,old,oldsize,newbuf,newsize,st,x,pos);
 	};
 }
Exemple #2
0
static off_t search(off_t *I,u_char *old,off_t oldsize,
		u_char *newp,off_t newsize,off_t st,off_t en,off_t *pos)
{
	off_t x,y;

	if(en-st<2) {
		x=matchlen(old+I[st],oldsize-I[st],newp,newsize);
		y=matchlen(old+I[en],oldsize-I[en],newp,newsize);

		if(x>y) {
			*pos=I[st];
			return x;
		} else {
			*pos=I[en];
			return y;
		}
	};

	x=st+(en-st)/2;
	if(MCMemoryCompare(old+I[x],newp,MIN(oldsize-I[x],newsize))<0) {
		return search(I,old,oldsize,newp,newsize,x,en,pos);
	} else {
		return search(I,old,oldsize,newp,newsize,st,x,pos);
	};
}
Exemple #3
0
off_t search(off_t *I,u_char *old,off_t oldsize,
		u_char *pnew,off_t newsize,off_t st,off_t en,off_t *pos)
{
	off_t x,y;

	if(en-st<2) {
		x=matchlen(old+I[st],oldsize-I[st],pnew,newsize);
		y=matchlen(old+I[en],oldsize-I[en],pnew,newsize);

		if(x>y) {
			*pos=I[st];
			return x;
		} else {
			*pos=I[en];
			return y;
		}
	};

	x=st+(en-st)/2;
	if(memcmp(old+I[x],pnew,MIN(oldsize-I[x],newsize))<0) {
		return search(I,old,oldsize,pnew,newsize,x,en,pos);
	} else {
		return search(I,old,oldsize,pnew,newsize,st,x,pos);
	};
}
Exemple #4
0
char*
prose(char *s, char *desc[][2], short index[])
{
	static char buf[512];
	char *p=buf;
	int i, j, k, max;

	j = 0;
	while(*s){
		if(p >= buf+sizeof buf)
			abort();
		if(*s == ' '){
			if(p>buf && p[-1]!=' ')
				*p++ = ' ';
			s++;
			continue;
		}
		if(*s == ','){
			*p++ = ';', s++;
			continue;
		}
		if(s[0]=='M' && '0'<=s[1] && s[1]<='9'){	/* Messier tag */
			*p++ = *s++;
			continue;	/* below will copy the number */
		}
		if((i=index[(uchar)*s]) == -1){
	Dup:
			switch(*s){
			default:
				while(*s && *s!=',' && *s!=' ')
					*p++=*s++;
				break;

			case '0': case '1': case '2': case '3': case '4':
			case '5': case '6': case '7': case '8': case '9':
				while('0'<=*s && *s<='9')
					*p++ = *s++;
				if(*s=='\'' || *s=='s')
					*p++ = *s++;
				break;

			case '(': case ')':
			case '\'': case '"':
			case '&': case '-': case '+':
				*p++ = *s++;
				break;

			case '*':
				if('0'<=s[1] && s[1]<='9'){
					int flag=0;
					s++;
				Pnumber:
					while('0'<=*s && *s<='9')
						*p++=*s++;
					if(s[0] == '-'){
						*p++ = *s++;
						flag++;
						goto Pnumber;
					}
					if(s[0]==',' && s[1]==' ' && '0'<=s[2] && s[2]<='9'){
						*p++ = *s++;
						s++;	/* skip blank */
						flag++;
						goto Pnumber;
					}
					if(s[0] == '.'){
						if(s[1]=='.' && s[2]=='.'){
							*p++ = '-';
							s += 3;
							flag++;
							goto Pnumber;
						}
						*p++ = *s++;
						goto Pnumber;
					}
					p = append(p, "m star");
					if(flag)
						*p++ = 's';
					*p++ = ' ';
					break;
				}
				if(s[1] == '*'){
					if(s[2] == '*'){
						p = append(p, "triple star ");
						s += 3;
					}else{
						p = append(p, "double star ");
						s += 2;
					}
					break;
				}
				p = append(p, "star ");
				s++;
				break;
			}
			continue;
		}
		for(max=-1; desc[i][0] && desc[i][0][0]==*s; i++){
			k = matchlen(desc[i][0], s);
			if(k > max)
				max = k, j = i;
		}
		if(max == 0)
			goto Dup;
		s += max;
		for(k=0; desc[j][1][k]; k++)
			*p++=desc[j][1][k];
		if(*s == ' ')
			*p++ = *s++;
		else if(*s == ',')
			*p++ = ';', s++;
		else
			*p++ = ' ';
	}
	*p = 0;
	return buf;
}
static char *
sial_ptr(char *fmt, value_t **vals)
{
    /* We need to ensure that we dont overflow our string buffer. Although its unlikely we will overflow it with
       just numbers, strings will easliy overflow. So, lets check for strings and see how long they are.
     */  
int len=0;
char *nfmt=NULL,*ni=NULL;
char *onefmt=NULL, *onei=NULL;
char *p=fmt;
char last=' ';
int curarg=0;
#define NBYTES (len-(nfmt-ni))

int i = 0;

	while(vals[i] != NULL) {
	    if(vals[i]->type.type == V_STRING)
		len+=vals[i]->type.size;
	    i++;
	}
	/* We add a fudge factor of 100, which should cover all the number arguments */
	len+=strlen(fmt) + 100;
	nfmt=sial_alloc(len);
	ni=nfmt;
	onefmt=sial_alloc(len);
	onei=onefmt;



	while(*p) {

		if(*p=='%') {

			static regex_t preg;
			static int done=0;
			regmatch_t matches[NMATCH];

			if(!done) {

				regcomp(&preg, FMTREG, REG_EXTENDED);
				done=1;
			}

			/* build a new format translation */
			onefmt=onei;
			*onefmt++=*p++;

			/* if the returned pointer is (char*)-1 or NULL then something is wrong */
			if(!regexec(&preg, p, NMATCH, matches, 0)) {

				int i, n=matches[0].rm_eo-1;
				int posarg, wpos, ppos;
				char *pi=p; /* save p for ptrto() macro */

				/* check that the width and precision field args point
				   to a int value_t. If they were used */
				wpos=chkforint(ptrto(M_WIDTHARG), vals, &curarg);
				ppos=chkforint(ptrto(M_PRECARG), vals, &curarg);

				/* argument position was specfified ? */
				if(ptrto(M_POSP)) {

					/* we work from 0-n, printf works from 1-n */
					if(sscanf(ptrto(M_POSP), "%d", &posarg)==1) posarg--;

					if(posarg >= BT_MAXARGS || !vals[posarg]) {
						sial_error("Invalid arg position specified [%d]", posarg+1);
					}

				} else posarg=curarg++;

				/* jump over the format spec in the original */
				p+=n;
#if 0
for(i=0;i<NMATCH;i++) {
	char buf[40];

	if(ptrto(i)) {
		int n=matchlen(i);
		strncpy(buf, pi+matches[i].rm_so, n);
		buf[n]='\0';
		printf("match[%d]=[%s]\n", i, buf);
	}
}
#endif

				/* copy all format specs to destination except fmt */
				for(i=0;i<sizeof(addit)/sizeof(addit[0]);i++) {

					switch(addit[i]) {

						case M_WIDTHARG:

							if(wpos >=0 ){

								*onefmt++='*';

							} else goto def;

						break;
						case M_PRECARG:

							if(ppos >=0 ){

								*onefmt++='.';
								*onefmt++='*';

							} else goto def;

						break;
						case M_PREC:
							if(ptrto(addit[i])) *onefmt++='.';
							goto def;
						default:
def:
						if(ptrto(addit[i])) {
							strcpy(onefmt, ptrto(addit[i]));
							onefmt+=matchlen(addit[i]);
						}
					}
				}

				if(*p=='p') {

ref:
					/* if user overrides anything don't do nothing */
					if(ptrto(M_FLAGS)||ptrto(M_WIDTH)||ptrto(M_WIDTHARG)||ptrto(M_PREC)||ptrto(M_PRECARG)||ptrto(M_SIZE)) {
						*onefmt++='p';

					} else {
						if(sial_defbsize()==8) {

							strcpy(onefmt, "016llx");
							onefmt+=6;

						} else {

							strcpy(onefmt, "08x");
							onefmt+=3;
						}
					}
					*onefmt='\0';
					p++;
					nfmt=add_fmt(NBYTES, nfmt, onei, ppos, wpos, posarg, vals);

				} else if(*p=='>') { 

					nfmt--;
					if(sial_defbsize()==8) {

						int i;

						for(i=0;i<8;i++) *nfmt++=last;
					}
					p++;
                                        curarg--;

				} else if(*p=='?') {

					/* put the proper format for the user */
					if(!vals[posarg]) {

						sial_error("Expected additional argument %d\n", posarg+1);

					} else switch(vals[posarg]->type.type) {

						case V_BASE: case V_ENUM:
						{
							if(!ptrto(M_SIZE)) {

								if(vals[posarg]->type.size==8) {

									*onefmt++='l';
									*onefmt++='l';
								}
							}
							if(sial_issigned(vals[posarg]->type.typattr)) {

								*onefmt++='d';

							}else{

								*onefmt++='u';
							}
						}
						break;
						case V_REF:
						{
							*p='p';
							goto ref;
						}
						case V_STRING:
						{
							*onefmt++='s';
						}
						break;
					}
					p++;
					*onefmt='\0';
					nfmt=add_fmt(NBYTES, nfmt, onei, ppos, wpos, posarg, vals);

				} else {

					/* check that format and value_t agree */
					/* can't do a lot more then check for strings vs anything_else */

					if(!vals[posarg]) {

						sial_error("Expected additional argument %d\n", posarg+1);


					} else if(*p=='s') {

						if(vals[posarg]->type.type != V_STRING) {

							sial_error("Expected type 'string' as arg%d", posarg+1);
						}

					} else if(vals[posarg]->type.type == V_STRING) {

						sial_error("Incompatible type 'string' in arg%d", posarg+1);

					}
					*onefmt++=*p++;
					*onefmt='\0';
					nfmt=add_fmt(NBYTES, nfmt, onei, ppos, wpos, posarg, vals);
				}

			} else {

				sial_warning("Malformed format specifier!");

			}

		} else {
	
			last=*p;
			if(nfmt-ni > len) sial_error("format tranlation overflow!");
			*nfmt++=*p++;

		}
	}
	sial_free(onei);
	*nfmt='\0';
	return ni;
}