Example #1
0
static int str2bt_addr_le(const char *str, const char *type, bt_addr_le_t *addr)
{
	int i, j;
	uint8_t tmp;

	if (strlen(str) != 17) {
		return -EINVAL;
	}

	for (i = 5, j = 1; *str != '\0'; str++, j++) {
		if (!(j % 3) && (*str != ':')) {
			return -EINVAL;
		} else if (*str == ':') {
			i--;
			continue;
		}

		addr->a.val[i] = addr->a.val[i] << 4;

		if (char2hex(str, &tmp) < 0) {
			return -EINVAL;
		}

		addr->a.val[i] |= tmp;
	}

	if (!strcmp(type, "public") || !strcmp(type, "(public)")) {
		addr->type = BT_ADDR_LE_PUBLIC;
	} else if (!strcmp(type, "random") || !strcmp(type, "(random)")) {
		addr->type = BT_ADDR_LE_RANDOM;
	} else {
		return -EINVAL;
	}

	return 0;
}
//Looks for a basic number entry in a string, reporting as int.
//
//  (int mStart) should be a pointer to the first number.
//
//Will stop looking on discovery of a line control char, null, space, or after 6 chars.
int search_int(char sights[], int mStart)
{
  int i;
  int number = 0;
  int last_char;
  int place = 1;

  for(i = 0; i <= 5; i++)
  {
    if((sights[mStart + i] == 0x20)||(sights[mStart + i] == 0x0A)||(sights[mStart + i] == 0x0D))
    {
      last_char = mStart + i - 1;  //point to last number
      i = 6;  //Bust on outta here!
    }
  }

  //now work backwards building the number
  for(i = last_char; i >= mStart; i--)
  {
	  number += char2hex(sights[i]) * place;
	  place = place * 10;
  }
  return number;
}
Example #3
0
char
str2hex(const char *str)
{
    return (char2hex(str[0]) << 4) | char2hex(str[1]);
}
Example #4
0
std::string urlencode( const std::string &c, URLEncodeType enctype)
{

    std::string escaped;
	int max = c.length();
	for(int i=0; i<max; i++)
	{
            // Unreserved chars
		if ( (48 <= c[i] && c[i] <= 57) ||//0-9
			(65 <= c[i] && c[i] <= 90) ||//ABC...XYZ
			(97 <= c[i] && c[i] <= 122) || //abc...xyz
			(c[i]=='~' || c[i]=='-' || c[i]=='_' || c[i]=='.')
			)
		{
			escaped.append( &c[i], 1);
		}
                else if (c[i] != ':' && c[i] != '/' && c[i] != '?' && c[i] != '#' &&
                    c[i] != '[' && c[i] != ']' && c[i] != '@' && c[i] != '%' &&

                    c[i] != '!' && c[i] != '$' && c[i] != '&' && c[i] != '\'' &&
                    c[i] != '(' && c[i] != ')' && c[i] != '*' && c[i] != '+' &&
                    c[i] != ',' && c[i] != ';' && c[i] != '=')
                {
                    // Characters not in unreserved (first if block) and not in
                    // the reserved set are always encoded.
                    escaped.append("%");
                    escaped.append( char2hex(c[i]) );//converts char 255 to string "FF"
                }
		else
		{
                    // Finally, the reserved set. Encoding here depends on the
                    // context (where in the URI we are, what type of URI, and
                    // which character).

                    bool enc = false;

                    // Always encode reserved gen-delims + '%' (which always
                    // needs encoding
                    if (c[i] == ':' || c[i] == '/' || c[i] == '?' || c[i] == '#' ||
                        c[i] == '[' || c[i] == ']' || c[i] == '@' || c[i] == '%')
                    {
                        enc = true;
                    }
                    else {
                        switch (enctype) {
                          case URLEncode_Everything:
                            enc = true;
                            break;
                          case URLEncode_Path:
                            // Only reserved sub-delim that needs encoding is %,
                            // taken care of above. Otherwise, leave unencoded
                            enc = false;
                            break;
                          case URLEncode_QueryKey:
                            if (c[i] == '&' ||
                                c[i] == '+' ||
                                c[i] == '=')
                                enc = true;
                            else
                                enc = false;
                            break;
                          case URLEncode_QueryValue:
                            if (c[i] == '&' ||
                                c[i] == '+')
                                enc = true;
                            else
                                enc = false;
                            break;
                          default:
                            assert(false && "Unknown urlencode type");
                            break;
                        }
                    }

                    if (enc) {
                        escaped.append("%");
                        escaped.append( char2hex(c[i]) );//converts char 255 to string "FF"
                    } else {
			escaped.append( &c[i], 1);
                    }
		}
	}
	return escaped;
}
Example #5
0
int
main(int argc, char *argv[])
{
    struct afsconf_dir *tdir;
    long code;
    const char *confdir;

    if (argc == 1) {
	fprintf(stderr, "%s: usage is '%s <opcode> options, e.g.\n",
		argv[0], argv[0]);
	fprintf(stderr, "\t%s add <kvno> <keyfile> <princ>\n", argv[0]);
	fprintf(stderr, "\tOR\n\t%s add <kvno> <key>\n", argv[0]);
	fprintf(stderr, "\t\tEx: %s add 0 \"80b6a7cd7a9dadb6\"\n", argv[0]);
	fprintf(stderr, "\t%s delete <kvno>\n", argv[0]);
	fprintf(stderr, "\t%s list\n", argv[0]);
	exit(1);
    }

    confdir = AFSDIR_SERVER_ETC_DIRPATH;

    tdir = afsconf_Open(confdir);
    if (!tdir) {
	fprintf(stderr, "%s: can't initialize conf dir '%s'\n", argv[0],
		confdir);
	exit(1);
    }
    if (strcmp(argv[1], "add")==0) {
	krb5_context context;
	krb5_principal principal;
	krb5_keyblock *key;
	krb5_error_code retval;
	int kvno, keymode = 0;

	if (argc != 5) {
	    if (argc == 4)
		keymode = 1;
	    else {
		fprintf(stderr, "%s add: usage is '%s add <kvno> <keyfile> "
			"<princ>\n", argv[0], argv[0]);
		fprintf(stderr, "\tOR\n\t%s add <kvno> <key>\n", argv[0]);
		fprintf(stderr, "\t\tEx: %s add 0 \"80b6a7cd7a9dadb6\"\n", argv[0]);
		exit(1);
	    }
	}

	kvno = atoi(argv[2]);
	if (keymode) {
	    char tkey[8];
	    int i;
	    char *cp;
	    if (strlen(argv[3]) != 16) {
		printf("key %s is not in right format\n", argv[3]);
		printf(" <key> should be an 8byte hex representation \n");
		printf("  Ex: setkey add 0 \"80b6a7cd7a9dadb6\"\n");
		exit(1);
	    }
	    memset(tkey, 0, sizeof(tkey));
	    for (i = 7, cp = argv[3] + 15; i >= 0; i--, cp -= 2)
		tkey[i] = char2hex(*cp) + char2hex(*(cp - 1)) * 16;
	    code = afsconf_AddKey(tdir, kvno, tkey, 1);
	} else {
	    krb5_init_context(&context);

	    retval = krb5_parse_name(context, argv[4], &principal);
	    if (retval != 0) {
		afs_com_err(argv[0], retval, "while parsing AFS principal");
		exit(1);
	    }
	    retval = krb5_kt_read_service_key(context, argv[3], principal, kvno,
					      ENCTYPE_DES_CBC_CRC, &key);
            if (retval == KRB5_KT_NOTFOUND)
                retval = krb5_kt_read_service_key(context, argv[3], principal, kvno,
                                                   ENCTYPE_DES_CBC_MD5, &key);
            if (retval == KRB5_KT_NOTFOUND)
                retval = krb5_kt_read_service_key(context, argv[3], principal, kvno,
                                                   ENCTYPE_DES_CBC_MD4, &key);
            if (retval == KRB5_KT_NOTFOUND) {
                char * princname = NULL;

                krb5_unparse_name(context, principal, &princname);

                afs_com_err(argv[0], retval,
                            "for keytab entry with Principal %s, kvno %u, DES-CBC-CRC/MD5/MD4",
                            princname ? princname : argv[4],
                            kvno);
                exit(1);
            } else if (retval != 0) {
		afs_com_err(argv[0], retval, "while extracting AFS service key");
		exit(1);
	    }

#ifdef USING_HEIMDAL
#define deref_key_length(key)			\
	    key->keyvalue.length

#define deref_key_contents(key)			\
	    key->keyvalue.data
#else
#define deref_key_length(key)			\
	    key->length

#define deref_key_contents(key)			\
	    key->contents
#endif
	    if (deref_key_length(key) != 8) {
		fprintf(stderr, "Key length should be 8, but is really %u!\n",
			(unsigned int)deref_key_length(key));
		exit(1);
	    }
	    code = afsconf_AddKey(tdir, kvno, (char *) deref_key_contents(key), 1);
	}

	if (code) {
	    fprintf(stderr, "%s: failed to set key, code %ld.\n", argv[0], code);
	    exit(1);
	}
	if (keymode == 0) {
	    krb5_free_principal(context, principal);
	    krb5_free_keyblock(context, key);
	}
    }
    else if (strcmp(argv[1], "delete")==0) {
	long kvno;
	if (argc != 3) {
	    fprintf(stderr, "%s delete: usage is '%s delete <kvno>\n",
		    argv[0], argv[0]);
	    exit(1);
	}
	kvno = atoi(argv[2]);
	code = afsconf_DeleteKey(tdir, kvno);
	if (code) {
	    fprintf(stderr, "%s: failed to delete key %ld, (code %ld)\n",
		    argv[0], kvno, code);
	    exit(1);
	}
    }
    else if (strcmp(argv[1], "list") == 0) {
	struct afsconf_keys tkeys;
	int i, j;

	code = afsconf_GetKeys(tdir, &tkeys);
	if (code) {
	    fprintf(stderr, "%s: failed to get keys, code %ld\n", argv[0], code);
	    exit(1);
	}
	for(i=0;i<tkeys.nkeys;i++) {
	    if (tkeys.key[i].kvno != -1) {
		printf("kvno %4d: key is: ", tkeys.key[i].kvno);
		for (j = 0; j < 8; j++)
			printf("%02x", (unsigned char) tkeys.key[i].key[j]);
		printf("\n");
	    }
	}
	printf("All done.\n");
    }
    else {
	fprintf(stderr, "%s: unknown operation '%s', type '%s' for "
		"assistance\n", argv[0], argv[1], argv[0]);
	exit(1);
    }
    exit(0);
}
Example #6
0
char *convertUCtoUTF8(char *in, char *pos){	
	char *toConvert = malloc(5*sizeof(char));
	char *ob = malloc(40*sizeof(char));
	int i,j;
	size_t inlen, outlen;
	char *inptr;
	char *outptr;
	iconv_t iv = NULL;

	if (strlen(in) > 5) {
		toConvert[0] = '.';
		toConvert[1] = 0;
		printf("echo -n \"\\\".\\\", \"\n");
		return toConvert;
	}
	
	char *toConvertIn = malloc(9*sizeof(char));
	memset(toConvertIn,0,9);
	for (i = 0; i < 8-strlen(in); i++){
		toConvertIn[i] = '0';
	}
	for (j = i; j < 8; j++){
		toConvertIn[j] = in[j-i];
	}
	//sscanf(toConvert,"%x%x%x%x",&toConvert[0],&toConvert[1],&toConvert[2],&toConvert[3]);
	/*
	for (i = 0; i < 8; i++)
		printf("%X",char2hex(toConvertIn[i]));
	*/
	toConvert[0] = (char2hex(toConvertIn[0]) << 4) | char2hex(toConvertIn[1]);
	toConvert[1] = (char2hex(toConvertIn[2]) << 4) | char2hex(toConvertIn[3]);
	toConvert[2] = (char2hex(toConvertIn[4]) << 4) | char2hex(toConvertIn[5]);
	toConvert[3] = (char2hex(toConvertIn[6]) << 4) | char2hex(toConvertIn[7]);
	toConvert[4] = 0;
//	printf("in: %s | toConvertIn: %s | toConvert: %s\n",in,toConvertIn,toConvert);
	
	outptr = ob;
	inptr = toConvert;
	
	memset(ob,0,40);

	inlen = 4;
	outlen = 40;

	/*iv = iconv_open("UTF-32BE","UTF-8");
	if (iv == NULL){
		printf("ERROR opening iconv.\n");
		return NULL;
	}
	iconv(iv,&inptr,&inlen,&outptr,&outlen);
	if (errno == EILSEQ){
		printf("iconv> Input conversion stopped due to an input byte that does not belong to the input codeset.\n"); 
	} else if (errno == E2BIG){
		printf("iconv> Input conversion stopped due to lack of space in the output buffer.\n"); 
	} else if (errno == EINVAL){
		printf("iconv> Input conversion stopped due to an incomplete character or shift sequence at the end of the input buffer.\n"); 
	} else if (errno == EBADF){
		printf("iconv> The cd argument is not a valid open conversion descriptor.\n");
	}*/
	
	//printf("\\x%02X\\x%02X\\x%02X\\x%02X -> %s\n",(uint8_t)toConvert[0],(uint8_t)toConvert[1],(uint8_t)toConvert[2],(uint8_t)toConvert[3],ob);
	printf("echo \"\\\"`echo -n -e \"\\x%02X\\x%02X\\x%02X\\x%02X\" | iconv -f UTF-32BE -t UTF-8`\\\", // %s\" \n",(uint8_t)toConvert[0],(uint8_t)toConvert[1],(uint8_t)toConvert[2],(uint8_t)toConvert[3],pos);
	return outptr;
}
Example #7
0
static unsigned char
str2hex(const char *str)
{
    return (char2hex(str[0]) << 4) | char2hex(str[1]);
}
Example #8
0
int
main(int argc, char **argv)
{
    struct afsconf_dir *tdir;
    afs_int32 code;
    int i;
    char *cp;

    if (argc == 1) {
	printf("setkey: usage is 'setkey <opcode> options, e.g.\n");
	printf("    setkey add <kvno> <key>\n");
	printf("      note: <key> should be an 8byte hex representation \n");
	printf("            Ex: setkey add 0 \"80b6a7cd7a9dadb6\"\n");
	printf("    setkey delete <kvno>\n");
	printf("    setkey list\n");
	exit(1);
    }

    tdir = afsconf_Open(AFSDIR_SERVER_ETC_DIRPATH);
    if (!tdir) {
	printf("setkey: can't initialize conf dir '%s'\n",
	       AFSDIR_SERVER_ETC_DIRPATH);
	exit(1);
    }
    if (strcmp(argv[1], "add") == 0) {
	char tkey[8];
	if (argc != 4) {
	    printf("setkey add: usage is 'setkey add <kvno> <key>\n");
	    exit(1);
	}
	if (strlen(argv[3]) != 16) {
	    printf("key %s is not in right format\n", argv[3]);
	    printf(" <key> should be an 8byte hex representation \n");
	    printf("  Ex: setkey add 0 \"80b6a7cd7a9dadb6\"\n");
	    exit(1);
	}
	memset(tkey, 0, sizeof(tkey));
	for (i = 7, cp = argv[3] + 15; i >= 0; i--, cp -= 2)
	    tkey[i] = char2hex(*cp) + char2hex(*(cp - 1)) * 16;

	code = afsconf_AddKey(tdir, atoi(argv[2]), tkey, 1);
	if (code) {
	    printf("setkey: failed to set key, code %d.\n", (int)code);
	    exit(1);
	}
    } else if (strcmp(argv[1], "delete") == 0) {
	afs_int32 kvno;
	if (argc != 3) {
	    printf("setkey delete: usage is 'setkey delete <kvno>\n");
	    exit(1);
	}
	kvno = atoi(argv[2]);
	code = afsconf_DeleteKey(tdir, kvno);
	if (code) {
	    printf("setkey: failed to delete key %d, (code %d)\n", (int)kvno,
		   (int)code);
	    exit(1);
	}
    } else if (strcmp(argv[1], "list") == 0) {
	struct afsconf_keys tkeys;
	int i;
	char tbuffer[9];

	code = afsconf_GetKeys(tdir, &tkeys);
	if (code) {
	    printf("setkey: failed to get keys, code %d\n", (int)code);
	    exit(1);
	}
	for (i = 0; i < tkeys.nkeys; i++) {
	    if (tkeys.key[i].kvno != -1) {
		char hexbuf[17];
		unsigned char c;
		int j;
		memcpy(tbuffer, tkeys.key[i].key, 8);
		tbuffer[8] = 0;
		for (j = 0; j < 8; j++) {
		    c = tbuffer[j];
		    hexbuf[j * 2] = hex2char(c / 16);
		    hexbuf[j * 2 + 1] = hex2char(c % 16);
		}
		hexbuf[16] = '\0';
		printf("kvno %4d: key is '%s' (0x%s)\n",
		       (int)tkeys.key[i].kvno, tbuffer, hexbuf);
	    }
	}
	printf("All done.\n");
    } else {
	printf
	    ("setkey: unknown operation '%s', type 'setkey' for assistance\n",
	     argv[1]);
	exit(1);
    }
    exit(0);
}
Example #9
0
void parse(std::basic_istream<CharT> &src, HandlerT &handler)
{
    enum Action {
        Stay,
        Skip
    };
    const int eos = -1;
    typedef std::function<Action (int)> parser_t;
    parser_t rule, top, in_string, in_atom, in_comment,
        append_escaped, append_escaped_hex;
    stack<parser_t> ctx;
    unsigned level = 0;

    std::string data;
    int hex_byte;

    auto rule_use = [&](parser_t const &p) {
        data = "";
        data.reserve(256);
        rule = p;
    };

    auto rule_pop = [&]() {
        auto p = ctx.top();
        ctx.pop();
        rule = p;
    };

    auto rule_push = [&](parser_t const &after,
                         parser_t const &current) {
        ctx.push(after);
        rule = current;
    };

    top = [&](int c) -> Action {
        if (c == ')') {
            if (!level)
                throw Error(src, "Unexpected ')'");
            --level;
            handler.on_list_end();
        } else if (c == '(') {
            ++level;
            handler.on_list_begin();
        } else if (c == ';') {
            rule_use(in_comment);
        } else if (::isspace(c)) {
            // do nothing
        } else if (c == '"') {
            rule_use(in_string);
        } else if (c != eos) {
            rule_use(in_atom);
            return Stay;
        }
        return Skip;
    };

    in_comment = [&](int c) -> Action {
        if (c != '\n' && c != eos) {
            data += c;
        } else {
            handler.on_comment(std::move(data));
            rule_use(top);
        }
        return Skip;
    };

    auto in_hex = [&](int c) -> Action {
        if (c != eos) {
            int n = char2hex(c);
            if (n >= 0) {
                if (hex_byte < 0) {
                    hex_byte = (n << 4);
                    return Skip;
                }
                hex_byte |= n;
            }
        }
        rule_pop();
        return Stay;
    };

    append_escaped_hex = [&](int) -> Action {
        if (hex_byte < 0)
            throw Error(src, "Escaped hex is empty");

        data += static_cast<char>(hex_byte);
        rule_pop();
        return Stay;
    };

    auto process_hex = [&](parser_t const &after) -> Action {
        hex_byte = -1;
        rule_push(after, in_hex);
        return Skip;
    };

    append_escaped = [&](int c) -> Action {
        static const std::unordered_map<char, char>
        assoc{{'n', '\n'}, {'t', '\t'}, {'r', '\r'}, {'a', '\a'},
              {'b', '\b'}, {'v', '\v'}};

        if (c == eos)
            throw Error(src, "Expected escaped symbol, got EOS");

        if (c == 'x')
            return process_hex(append_escaped_hex);

        auto p = assoc.find(c);
        if (p != assoc.end()) {
            data += p->second;
        } else {
            data += c;
        }
        rule_pop();
        return Skip;
    };

    auto process_escaped = [&]() -> Action {
        rule_push(rule, append_escaped);
        return Skip;
    };

    in_atom = [&](int c) -> Action {
        static const std::string bound("()");
        if (bound.find(c) != std::string::npos || isspace(c) || c == eos) {
            handler.on_atom(std::move(data));
            rule_use(top);
            return Stay;
        } else if (c == '\\') {
            return process_escaped();
        } else {
            data += c;
        }
        return Skip;
    };

    in_string = [&](int c) -> Action {
        if (c == '"') {
            handler.on_string(std::move(data));
            rule_use(top);
        } else if (c == '\\') {
            return process_escaped();
        } else if (c == eos) {
            throw Error(src, "string is not limited, got EOS");
        } else {
            data += c;
        }
        return Skip;
    };

    rule_use(top);
    try {
        while (true) {
            CharT c = src.get();
            if (src.gcount() == 0) {
                rule(eos);
                break;
            }
            
            while (rule(c) == Stay) {}
        }
    } catch (Error const &e) {
        throw;
    } catch (std::exception const &e) {
        throw;
    }
    handler.on_eof();
}
Example #10
0
void  uart_process(void)
{
  Int8U       num;
  Int16U      adr, d, c, command;
    
    Flags.uart_recieve=  0;
    
  // UART mode
  if( (hex2int(1) != NetAdr) && (hex2int(1) != SERVICE_ADR) ) { RxCounter=0; RXI_ENABLE; return; }
  if(check_sum(RxCounter-2) != hex2char(RxCounter-2))         { RxCounter=0; RXI_ENABLE; return; }
  
  RxCounter=  0;
  adr=        hex2int(7);
  num=        hex2char(11);
  command=    UartBuffer[5]*256 + UartBuffer[6];
       

  UartBuffer[0]=  '!';
  c=              7;
  
  switch(command) 
  {   
    case 'WR': if((num > MAX_DATA_LEN) || ((adr + num) > RAMSIZE) ) goto fail;
               hexbuffer_2_binbuffer(UartBuffer, (pInt8U)(RamBeginAddr + adr), num, 13);
               goto final;               
               
    case 'RI': if(num > MAX_DATA_LEN) goto fail;
               binbuffer_2_hexbuffer(UartBuffer,(Int8U __data20 *)(adr), num, 7);
               c= 7 + num*2;
               goto final;
               
    case 'RR': if(num > MAX_DATA_LEN) goto fail;
               binbuffer_2_hexbuffer(UartBuffer,(Int8U __data20 *)(RamBeginAddr + adr), num, 7);
               c= 7 + num*2;
               goto final;
               
    case 'IR': for(; SerialNumberStr[c-7]; )  
               { 
                 UartBuffer[c]= SerialNumberStr[c-7]; 
                 c++; 
               } 
               UartBuffer[c++]=' ';
               for(; Version[c-16]; )  
               { 
                 UartBuffer[c]= Version[c-16]; 
                 c++; 
               }
               UartBuffer[c++]=' ';
               for(; RxBuffer[c-28]; )  
                { 
                 UartBuffer[c]= RxBuffer[c-21]; 
                 c++; 
               }
               goto final; 

               
fail:
     default:  UartBuffer[0]= '?'; 
final:
               d= char2hex(check_sum(c)); 
               UartBuffer[c]=   d>>8; 
               UartBuffer[c+1]= d; 
               UartBuffer[c+2]= 0x0d;
  }  
void DINGOOGUIWindowImp::prepareFonts() {

    Uint32 rmask, gmask, bmask, amask;

    Config *config=Config::GetInstance() ;
    
   unsigned char r,g,b ;
   const char *value=config->GetValue("BACKGROUND") ;
   if (value) {
      char2hex(value,&r) ;
      char2hex(value+2,&g) ;
      char2hex(value+4,&b) ;
   } else {
      r=0xFF ;
      g=0xFF ;
      b=0xFF ;      
   }
   backgroundColor_=SDL_MapRGB(screen_->format, r,g,b) ;
           
   value=config->GetValue("FOREGROUND") ;
   if (value) {
      char2hex(value,&r) ;
      char2hex(value+2,&g) ;
      char2hex(value+4,&b) ;
   } else {
      r=0x00 ;
      g=0x00 ;
      b=0x00 ;      
   }
   foregroundColor_=SDL_MapRGB(screen_->format, r,g,b) ;
              
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
    rmask = 0xff000000;
    gmask = 0x00ff0000;
    bmask = 0x0000ff00;
    amask = 0x000000ff;
#else
    rmask = 0x000000ff;
    gmask = 0x0000ff00;
    bmask = 0x00ff0000;
    amask = 0xff000000;
#endif


	for (int i=0;i<FONT_COUNT;i++) {
	    fonts[i] = SDL_CreateRGBSurface(SDL_HWSURFACE, 8, 8, 16,
	                                   0, 0, 0, 0);
		if (fonts[i]==NULL) {
			Trace::Dump("Failed to create font surface %d",i) ;
		} else {
			SDL_LockSurface(fonts[i]) ;
			const unsigned char *src=font+i*8 ;
			unsigned short*dest=(unsigned short *)fonts[i]->pixels ;
  	  		for (int y = 0; y < 8; y++) {
    			for (int x = 0; x < 8; x++) {
					*dest =(*src)?backgroundColor_:foregroundColor_;
					dest++;
					src++ ;
            	}
				if (y<7) src+=FONT_WIDTH-8 ;
		  	}
			SDL_UnlockSurface(fonts[i]) ;
		} ;
	};
} ;
Example #12
0
void lbRcv(unsigned char c){
	//posts sensed heading and velocity data to global variable
	static char lf_flag; //checks if line feed ("\n") character has been sent
	static char h_iter; //count iterator for sensed heading
	static char v_iter; //count iterator for sensed velocity
	static char d_iter;	//count iterator for differential distance 
	static char h_flag;
	static char v_flag;
	static char d_flag;
	static signed int h_rough = 0;  //store heading
	static signed int v_rough = 0;  //store velocity
	static signed int d_rough = 0;  //store differential distance 
		//rprintf("%c",c);
		if(1){//c != -1
		//if the data isn't whitespace (0xff), post it
			//rprintf("%c",c);

			if(c == 0x0A){lf_flag = SET;} //line feed detected, the character will be a 'H','V', or 'S'
	
			else if((lf_flag) && (c == 'H')){ //set heading flag
				h_flag = SET;
				h_iter = 0;
				lf_flag = UNSET;
				return;
			} 
			else if((lf_flag) && (c == 'V')){ //set velocity flag
				v_flag = SET;
				v_iter = 0;
				lf_flag = UNSET;
			//	rprintf("%c",c);
				return;
			}
			else if((lf_flag) && (c == 'S')){ //set differential distance  flag
				d_flag = SET;
				d_iter = 0;
				lf_flag = UNSET;
				return;
			}
			else if(h_flag){
				h_rough = (char2hex(c) | (h_rough << 4));	//store then increment	
				h_iter++;
		//		rprintfu08(c);
		//		rprintf(" ITER:%d|",h_iter);
		//		rprintfu16(h_rough);
		//		rprintf("\t");
		//		rprintf("%c",c);
			}	
			else if(v_flag){
				v_rough = (char2hex(c) | (v_rough << 4));	//store then increment	
				v_iter++;
			//	rprintf("%c",c);
			}
			else if(d_flag){
				d_rough = (char2hex(c) | (d_rough << 4));	//store then increment	
				d_iter++;
			//	rprintf("%c",c);
			}

			if(h_iter == 4){
				robot.sen_heading = h_rough;
				h_flag = UNSET;
				h_iter = 0;
		//		rprintf("H: %d",h_rough);
		//		rprintfNum(2, 16, 1, ' ', h_rough);
		//		rprintfu16(h_rough);
		//		rprintf("\n");
				h_rough = 0;
			}
			else if(v_iter == 4){
				robot.sen_vel = v_rough;
				v_flag = UNSET;
				v_iter = 0;
		//		rprintf("V: ");
		//		rprintfu16(v_rough);
		//		rprintf("\n");
				v_rough = 0;
			}
			else if(d_iter == 4){
				robot.dDis += (float)d_rough/10;	//differential distance is added to current differential, retrieving function will reset to zero
				d_flag = UNSET;			//divided by 10 to reintroduce the decimals
				d_iter = 0;
		//		rprintf("D: ");
		//		rprintfu16(d_rough);
		//		rprintf("\n");
				d_rough = 0;
			}
		
		}

		else{rprintf("WR\n");}

}
Example #13
0
int AmZRTP::init() {
  zrtp_log_set_log_engine(zrtp_log);

  AmConfigReader cfg;
  string cfgname=add2path(AmConfig::ModConfigPath, 1,  "zrtp.conf");
  if(cfg.loadFile(cfgname)) {
    ERROR("No %s config file present.\n", cfgname.c_str());
    return -1;
  }

  cache_path = cfg.getParameter("cache_path");
  if (cfg.hasParameter("zid_hex")) {
    string zid_hex = cfg.getParameter("zid_hex");
    if (zid_hex.size() != 2*sizeof(zrtp_instance_zid)) {
      ERROR("zid_hex config parameter in zrtp.conf must be %lu characters long.\n", 
	    sizeof(zrtp_zid_t)*2);
      return -1;
    }

    for (size_t i=0;i<sizeof(zrtp_instance_zid);i++) {
      unsigned int h;
      if (reverse_hex2int(zid_hex.substr(i*2, 2), h)) {
	ERROR("in zid_hex in zrtp.conf: '%s' is no hex number\n", zid_hex.substr(i*2, 2).c_str());
	return -1;
      }

      zrtp_instance_zid[i]=h % 0xff;
    }

  } else if (cfg.hasParameter("zid")) {
    string zid = cfg.getParameter("zid");
    WARN("zid parameter in zrtp.conf is only supported for backwards compatibility. Please use zid_hex\n");
    if (zid.length() != sizeof(zrtp_zid_t)) {
      ERROR("zid config parameter in zrtp.conf must be %lu characters long.\n", 
	    sizeof(zrtp_zid_t));
      return -1;
    }
    for (size_t i=0;i<zid.length();i++)
      zrtp_instance_zid[i]=zid[i];
  } else {
    // generate one
    string zid_hex;
    for (size_t i=0;i<sizeof(zrtp_instance_zid);i++) {
      zrtp_instance_zid[i]=get_random() % 0xff;
      zid_hex+=char2hex(zrtp_instance_zid[i], true);
    }

    WARN("Generated random ZID. To support key continuity through key cache "
	 "on the peers, add this to zrtp.conf: 'zid_hex=\"%s\"'", zid_hex.c_str());
  }


  DBG("initializing ZRTP library with cache path '%s'.\n", cache_path.c_str());

  zrtp_config_defaults(&zrtp_config);

  strcpy(zrtp_config.client_id, SEMS_CLIENT_ID);
  memcpy((char*)zrtp_config.zid, (char*)zrtp_instance_zid, sizeof(zrtp_zid_t));
  zrtp_config.lic_mode = ZRTP_LICENSE_MODE_UNLIMITED;
  
  strncpy(zrtp_config.cache_file_cfg.cache_path, cache_path.c_str(), 256);

  zrtp_config.cb.misc_cb.on_send_packet           = AmZRTP::on_send_packet;
  zrtp_config.cb.event_cb.on_zrtp_secure          = AmZRTP::on_zrtp_secure;
  zrtp_config.cb.event_cb.on_zrtp_security_event  = AmZRTP::on_zrtp_security_event;
  zrtp_config.cb.event_cb.on_zrtp_protocol_event  = AmZRTP::on_zrtp_protocol_event;

  if ( zrtp_status_ok != zrtp_init(&zrtp_config, &zrtp_global) ) {
    ERROR("Error during ZRTP initialization\n");
    return -1;
  }

  size_t rand_bytes = cfg.getParameterInt("random_entropy_bytes", 172);
  if (rand_bytes) {
    INFO("adding %zd bytes entropy from /dev/random to ZRTP entropy pool\n", rand_bytes);
    FILE* fd = fopen("/dev/random", "r");
    if (!fd) {
      ERROR("opening /dev/random for adding entropy to the pool\n");
      return -1;
    }
    void* p = malloc(rand_bytes);
    if (p==NULL)
      return -1;

    size_t read_bytes = fread(p, 1, rand_bytes, fd);
    if (read_bytes != rand_bytes) {
      ERROR("reading %zd bytes from /dev/random\n", rand_bytes);
      return -1;
    }
    zrtp_entropy_add(zrtp_global, (const unsigned char*)p, read_bytes);
    free(p);
  }


  // zrtp_add_entropy(zrtp_global, NULL, 0); // fixme
  DBG("ZRTP initialized ok.\n");

  return 0;
}