Esempio n. 1
0
/**
	b64decode
	Decodes a base64-encoded message.  You provide an encoded message,
	and a pointer to somewhere we (the editorial we) can store the length 
	of the decoded message.  A dynamically allocated pointer is returned.
*/
char *b64decode(char *encoded, size_t *newlen)
{
        BIO     *b64, *bmem;
        char    *decoded = NULL;

	if(encoded == NULL) {
		print_loc(); io_debug("NULL data passed!  Bad coder!  Bad!\n");
		return NULL;
	}

	safe_malloc(decoded,*newlen);
        b64 = BIO_new(BIO_f_base64());
        bmem = BIO_new_mem_buf(encoded, -1);
	if(bmem == NULL || b64 == NULL) {
		print_loc(); io_debug("Calls to libssl failed!\n");
		abort();  //I don't think this will ever happen.
	}

        bmem = BIO_push(b64, bmem);
        *newlen = BIO_read(bmem, decoded, *newlen);
	BIO_free_all(bmem);

	decoded[*newlen] = '\0';


	return decoded;
}
Esempio n. 2
0
/**
	b64encode
	Encodes raw data in base64.  Pass the data and the length of the data.
	b64encode returns the base64-encoded data as a dynamically allocated 
	char *.
*/
char *b64encode(char *data, size_t datalen)
{
	BIO	*b64, *bmem;
	BUF_MEM	*bptr;
	char	*encoded = NULL;

	
        b64 = BIO_new(BIO_f_base64());
        bmem = BIO_new(BIO_s_mem());
	if(bmem == NULL || b64 == NULL) {
		print_loc(); io_debug("Calls to libssl failed!\n");
		abort();  //I don't think this will ever happen.
	}

        bmem = BIO_push(b64, bmem);
        BIO_write(bmem, data, datalen);
        BIO_flush(bmem);

        BIO_get_mem_ptr(bmem, &bptr);
        BIO_set_close(bmem, BIO_NOCLOSE);

	safe_malloc(encoded, bptr->length);
	memcpy(encoded, bptr->data, bptr->length);

	encoded[bptr->length] = '\0';

	BIO_free_all(bmem);
	BUF_MEM_free(bptr);

	return encoded;
}
Esempio n. 3
0
/**
	url2http
	Given a url as a string, it returns a pointer to a dynamically allocated
	http_request struct.

	args:
	url = A string of the following form:  
		<scheme>://<user>:<pass>@<host>:<port>/<url-path>
	where <scheme> is either 'http' or 'https', and all parts are optional
	except the scheme, the host and the path.  So, the minimum is something
	like
		<scheme>://<host>/<url-path>
	Returns a pointer to the http_request struct generated from the string.
*/
http_request *url2http(char *url)
{
	http_request	*nml;
	struct in_addr	addr;
	int		ssl = 0;
	parsed_url	purl;

	if (url == NULL) {
		//print_loc(); io_debug("NULL url passed!\n");
		return NULL;
	}

	if(strncmp(url, "http", 4))  { /* Must start with 'http'. */
		print_loc(); io_debug("Bad URL:  %s\n", url);
		return NULL;
	}
	
	purl = url_parse(url);
	addr = resolve_cached(purl.host, NULL, NULL);
	
	if(!strcmp(purl.scheme, "https"))
		ssl = 1;

	return new_http_req(addr, purl.port, purl.host, purl.path, ssl, 
		purl.passwd, purl.user);
}
Esempio n. 4
0
int main(int argc, char *argv[])
{
  if(argc<3){
    fprintf(stderr, "usage: %s adapter car-MAC [verbose]\n",argv[0]);
    exit(0);
  }
  const char* adapter = argv[1];
  const char* car_id  = argv[2];
  int i;
  AnkiHandle h = anki_s_init(adapter, car_id, argc>3);
  if(!h) return 1;
  printf("Setting initial speed 1000\n");
  if(anki_s_set_speed(h,1000,20000)!=0) return 1;
  for(i=0; i<10; i++){ usleep(200000); printf("changing lane -40\n"); anki_s_cancel_lane_change(h); anki_s_change_lane(h,-40,100,1000); print_loc(h); }
  printf("Setting slow speed 500\n");
  anki_s_set_speed(h,500,20000);
  for(i=0; i<5; i++){ usleep(100000); printf("changing lane +20\n");   anki_s_cancel_lane_change(h); anki_s_change_lane(h,20,200,4000);  print_loc(h); }
  //for(i=0; i<20; i++){ usleep(200000);  anki_s_cancel_lane_change(h); anki_s_change_lane(h,-40,100,1000); }
  for(i=0; i<10; i++){ usleep(1000000);  printf("testing locations\n"); print_loc(h);  }
  printf("Uturn\n");
  usleep(1000000);
  printf("Stopping (smoothly)\n"); anki_s_set_speed(h,0,500);
  for(i=0; i<14; i++){ usleep(1000000);  printf("speeding up to %d\n",i*100); anki_s_set_speed(h,i*100,10000);  print_loc(h); }
  for(i=0; i<14; i++){ usleep(1000000);  printf("lowing down to %d\n",1400-i*100); anki_s_set_speed(h,1400-i*100,10000);  print_loc(h); }
  printf("Uturn\n");
  anki_s_uturn(h);
  printf("Some more speed changes and lane changes\n");
  anki_s_set_speed(h,1200,20000);
  for(i=0; i<10; i++){ usleep(300000);  print_loc(h);  }
  anki_s_change_lane(h,-50,100,1000);
  for(i=0; i<10; i++){ usleep(300000);  print_loc(h);  }
  anki_s_set_speed(h,0,20000);
  sleep(1);
  anki_s_close(h);
  //sleep(5);
  //AnkiHandle h2 = anki_s_init(adapter, car_id, argc>3);
  //usleep(500000);
  //if(anki_s_set_speed(h2,1000,20000)!=0) return 1;
  //for(i=0; i<5; i++){ usleep(200000);  print_loc(h);  }
  //anki_s_set_speed(h2,0,20000);
  //usleep(200000);
  //anki_s_close(h2);
  return 0;
}
Esempio n. 5
0
void flx_trace(flx_trace_t* tr,flx_range_srcref_t sr, char const *file, int line, char const *msg)
{
  if(!flx_enable_trace)return;
  flx_global_trace_count++;
  if(tr)
  {
    tr->count++;
    if(tr->enable_trace)
    {
      fprintf(stderr,"%ld : %s\n",tr->count,msg);
      print_loc(stderr,sr,file,line);
    }
  }
  else
  {
    fprintf(stderr,"%ld : %s\n",flx_global_trace_count,msg);
    print_loc(stderr,sr,file,line);
  }
}
Esempio n. 6
0
/**
	valid_dsa_sig
	Given some data, the data length, the public key, the public key 
	length, and the 48-byte signature, we verify it and return 1 for
	true and 0 for false, or -1 for error.
*/
int valid_dsa_sig(const void *data, int dlen, const char sig[48])
{
	int	r;
	char	hash[20];

	SHA1(data, dlen, hash);
	r = DSA_verify(0xdecafbad, hash, 20, sig, 48, dispatch);
	if(!r) {
		print_loc();
		io_err("Couldn't verify signature!  Info follows:\n");
		io_debug("Hexdump of signature:\n");
		io_hexdump(stdout, sig, 48);
		io_debug("\nHexdump of data follows:\n");
		io_hexdump(stdout, data, dlen);
	}
	return r;
}
Esempio n. 7
0
void error_report(location l, std::string message, location prev) {
	print_loc(l);
	std::cerr << ": " << message << " (see ";
	print_loc(prev);
	std::cerr << ")" << std::endl;
}
Esempio n. 8
0
void error_report(location l, std::string message) {
	print_loc(l);
	std::cerr << ": " << message << std::endl;
}
Esempio n. 9
0
int error(int t, loc* lc, const char* s, const ea& a0, const ea& a1, const ea& a2, const ea& a3)
/*
	"int" not "void" because of "pch" in lex.c

	legal error types are:

	not counted in error count:

		'w'		warning
		'd'		debug
		'D'		debug -- no prefix
		'C'		text -- line no. and no newline
		'c'		text -- no line no. and no newline

	counted in error count:

		's'		"not implemented" message
		'l'		"compiler limit exceeded" message
    		0		error 
    		'e'		error -- no newline
    		'i'		internal error (causes abort)
		't'		error while printing error message
*/
{
	if (suppress_error && t!='i' && t!='d') return 0;

	if (in_error++)
		if (t == 't')
			t = 'i';
		else if (4 < in_error) {
			fprintf(stderr,"\nOops!, error while handling error\n");
			ext(13);
		}

	FILE * of = out_file;
	out_file = stderr;

	if (!scan_started || t=='t')
		putch('\n');
	else if (lc != &dummy_loc) {
		if(t != 'D' && t != 'c') lc->put(out_file);
	} else {
		if(t != 'D' && t != 'c') print_loc();
	}

	int user_error = 0;

	switch (t) {
        case 'C':
        case 'c':
		break;
    	case 'e':
		user_error = 1;
		// no break
    	case 0:
		putstring("error: ");
		user_error += 1;
		break;
        case 'd':
		putstring("DEBUG: ");
        case 'D':
		break;
        case 'w':
//		no_of_warnings++;
		putstring("warning: ");
		break;
        case 'l':
		putstring("compiler limit exceeded: ");
		break;
        case 's':
		putstring("sorry, not implemented: ");
		user_error = 1;
		break;
        case 'i':
		if (error_count++) {
			fprintf(out_file,"sorry, cannot recover from earlier errors\n");
			out_file = of; // restore for fflush()
#ifdef TEST_SUITE
			ext(INTERNAL2);
#else
			ext(INTERNAL);
#endif
		}
		else
			fprintf(out_file,"internal %s error: ",prog_name);
        }

	ea argv[4];
	ea* a = argv;
	argv[0] = a0;
	argv[1] = a1;
	argv[2] = a2;
	argv[3] = a3;

	int c;

	while (c = *s++) {
		if ('A'<=c && c<='Z')
			putstring(abbrev_tbl[c-'A']);
		else if (c == '%') {
			switch (c = *s++) {
			case 'k':	// TOK assumed passed as an int
			{	int x = TOK(a->i);
				if (0 < x && x<=MAXTOK && keys[x])
					fprintf(out_file," %s",keys[x]);
				else
					fprintf(out_file," token(%d)",x);
				break;
			}
			case 't':	// Ptype 
			{	Ptype tt = Ptype(a->p);
				if (tt == 0) break;

				putch(' ');
			
				int nt = ntok;
				emode = 1;
				tt->dcl_print(0);
				emode = 0;
				ntok = nt;
				break;
			}
			case 'n':	// Pname
			{	Pname nn = Pname(a->p);
				if (nn && nn->string) {
					// suppress generated class names:
					if (nn->string[0]=='_'
					&& nn->string[1]=='_'
					&& nn->string[2]=='C') break;
					emode = 1;
					putch(' ');
					nn->print();
					emode = 0;
				}
				else
					putstring(" ?");
				break;
			}
			case 'p':	// pointer
			{	char* f = sizeof(char*)==sizeof(int)?" %d":" %ld";
				fprintf(out_file,f,a->p);
				break;
			}
			case 'a':	// fully qualified function 
			{	Pname nn = Pname(a->p);
				if (nn->tp->base!=FCT && nn->tp->base!=OVERLOAD)
					error('i',"%n not function",nn);
				if (nn && nn->string) {
					// suppress generated class names:
					if (nn->string[0]=='_'
					&& nn->string[1]=='_'
					&& nn->string[2]=='C') break;
					emode = 1;
					putch(' ');
					nn->print(1);
					emode = 0;
				}
				else
					putstring(" ?");
				break;
			}
			case 'c':	// char assumed passed as an int
				putch((int)a->i);
				break;

			case 'd':	// int
				fprintf(out_file," %d",a->i);
				break;

			case 'o':	// int
				fprintf(out_file," 0%o",a->i);
				break;

			case 's':	// char*
				{
				char *s = ((char *)a->p);
				if ( s ) putst((char*)a->p);
				break;
				}
			}
			a++;
		}
		else
			putch(c);
	}

/*
	switch (t) {
	case 'd':
	case 't':
	case 'w':
		putch('\n');
		break;
	default:
*/
		if (t != 'c' && t != 'e' && t != 'C')
			print_context();
/*
	}
*/

	if (user_error) 
		basic_inst::head->print_error_loc(user_error==2);

	out_file = of; // restore before ext() for fflush()
	if (!scan_started && t!='d' && t!='w') ext(4);

        // now we may want to carry on 
	switch (t) {
	case 't':
		if (--in_error) {
			fflush(stderr);
			//fflush(out_file);
			return 0;
		}
	case 'i': 
		ext(INTERNAL);
	case 0:
	case 'e':
	case 'l':
	case 's':
#ifdef TEST_SUITE
		if (t == 's')
			ext(SORRY);
#endif
		if (MAXERR<++error_count) {
			fprintf(stderr,"Sorry, too many errors\n");
			ext(7);
		}
	}

	in_error = 0;
	fflush(stderr);
	//fflush(out_file);
	return 0;
}
Esempio n. 10
0
int error(int t, loc* lc, char* s ...)
/*
	"int" not "void" because of "pch" in lex.c
	subsequent arguments fill in %mumble fields 

	legal error types are:
		'w'		warning	 (not counted in error count)
		'd'		debug
		's'		"not implemented" message
    		0		error 
    		'i'		internal error (causes abort)
		't'		error while printing error message
*/
{
	FILE * of = out_file;
	int c;
	char format[3];	/* used for "% mumble" sequences */
	int * a = &t;
	int argn = 3;

	/* check variable argument passing mechanism */
	int si = sizeof(int);
	int scp = sizeof(char*);
	int ssp = sizeof(Pname);

	if (si!=ssp || si!=scp || ssp!=scp || &a[2]!=(int*)&s) {
		fprintf(stderr,
			"\n%s: this c can't handle varargs (%d,%d,%d -- %d %d)\n",
			prog_name, si, scp, ssp, &a[1], &s);
		ext(12);
	}

	if (t == 'w' && warn==0) return 0;

	if (in_error++)
		if (t!='t' || 4<in_error) {
			fprintf(stderr,"\nUPS!, error while handling error\n");
			ext(13);
		}
	else if (t == 't')
		t = 'i';

	out_file = stderr;
	if (!scan_started)
		/*fprintf(out_file,"error during %s initializing: ",prog_name);*/
		putch('\n');
	else if (t=='t')
		putch('\n');
	else if (lc != &dummy_loc)
		lc->put(out_file);
	else
		print_loc();

    switch (t) {
    	case 0:
		fprintf(out_file,"error: ");
		break;
        case 'w':
		no_of_warnings++;
		fprintf(out_file,"warning: ");
		break;
        case 's':
		fprintf(out_file,"sorry, not implemented: ");
		break;
        case 'i':
		if (error_count++) {
			fprintf(out_file,"sorry, %s cannot recover from earlier errors\n",prog_name);
			ext(INTERNAL);
		}
		else
			fprintf(out_file,"internal %s error: ",prog_name);
		break;
        }

    while (c = *s++) {
	if ('A'<=c && c<='Z' && abbrev_tbl['A'])
		putstring(abbrev_tbl[c]);
	else if (c == '%')
		switch (c = *s++) {
		case 'k':
		{	TOK x = a[argn];
			if (0<x && x<MAXTOK && keys[x])
				fprintf(out_file," %s",keys[x]);
			else
				fprintf(out_file," token(%d)",x);
			argn++;
			break;
		}
		case 't':	/* Ptype */
		{	Ptype tt = (Ptype)a[argn];
			if (tt) {
				TOK pm = print_mode;
				extern int ntok;
				int nt = ntok;
				print_mode = ERROR;
				fprintf(out_file," ");
				tt->dcl_print(0);
				print_mode = pm;
				ntok = nt;
				argn++;
			}
			break;
		}
		case 'n':	/* Pname */
		{	Pname nn = (Pname)a[argn];
			if (nn) {
				TOK pm = print_mode;
				print_mode = ERROR;
				fprintf(out_file," ");
				nn->print();
				print_mode = pm;
			}
			else
				fprintf(out_file," ?");
			argn++;
			break;
		}
		default:
			format[0] = '%';
			format[1] = c;
			format[2] = '\0';
			fprintf(out_file,format,a[argn++]);
			break;
		}
		else
			putch(c);
	}

	if (!scan_started) ext(4);

	switch (t) {
	case 'd':
	case 't':
	case 'w':
		putch('\n');
		break;
	default:
		print_context();
	}
	fflush(stderr);
    /* now we may want to carry on */

	out_file = of;

	switch (t) {
	case 't':
		if (--in_error) return 0;
	case 'i': 
		ext(INTERNAL);
	case 0:
	case 's':
		if (MAXERR<++error_count) {
			fprintf(stderr,"Sorry, too many errors\n");
			ext(7);
		}
	}

	in_error = 0;
	return 0;
}