void load_into_memory(){
	int printIndex;
	int real_address;
	int real_instruct_mem;

	STRING bufferString = (STRING) malloc( (sizeof(char)) * 10 );
	char memAddress[3];
	char instructMem[3];

	int i;

	//load into memory
	while( fgets(bufferString, 10, input1) != NULL ){

		if(checkEP(bufferString) == FALSE){
			
			for(i = 0; i < 3 ; i++){
				memAddress[i] = bufferString[i];
			}

			real_address = char_to_hex(memAddress);

			
			for(i = 5; i < 8 ; i++){
				instructMem[i-5] = bufferString[i];
			}

			//printf("AAA%sAAA\n", instructMem);
			real_instruct_mem= char_to_hex(instructMem);

			my_memory[real_address] = real_instruct_mem & 0xFFF;
		}

	}


	bufferString[0] = '\0';
	free(bufferString);



	//Print to check stuff inside memory -debugmem
	// for(printIndex = 0; printIndex < 4096; printIndex++){
	// 	if (my_memory[printIndex] != 0){
	// 		printf("index: %x  %x\n", printIndex, my_memory[printIndex]);
	// 	}
	// }

}
Exemplo n.º 2
0
int  str_to_hex(uint8_t *str, uint8_t *hex, int swap)
{
    int         i, count = 0, actual_len;
    uint8_t     val8;

    while (*str) {
        if (!is_hex_char(*str)) {
            //sysprintf("ERROR - not hex!!\n");
            return count;
        }

        val8 = char_to_hex(*str);
        str++;

        if (!is_hex_char(*str)) {
            //sysprintf("ERROR - not hex!!\n");
            return count;
        }

        val8 = (val8 << 4) | char_to_hex(*str);
        str++;

        hex[count] = val8;
        //sysprintf("hex = 0x%x\n", val8);
        count++;
    }

    actual_len = count;

    for ( ; count % 4 ; count++)
        hex[count] = 0;

    if (!swap)
        return actual_len;

    // SWAP
    for (i = 0; i < count; i+=4) {
        val8 = hex[i];
        hex[i] = hex[i+3];
        hex[i+3] = val8;

        val8 = hex[i+1];
        hex[i+1] = hex[i+2];
        hex[i+2] = val8;
    }

    return actual_len;
}
Boolean checkEP(STRING bufferString){
	char temp[3];
	int i;

	if( (bufferString[0] == 'E') && 
		(bufferString[1] == 'P') &&
		(bufferString[2] == ':') &&
		(bufferString[3] == ' ') &&
		( check_char_within_range(bufferString[4]) ) &&
		( check_char_within_range(bufferString[5]) ) &&
		( check_char_within_range(bufferString[6]) )    ){

		for(i = 4; i < 7 ; i++){
			temp[i-4] = bufferString[i];
		}

		//printf("SSS  %s\n", temp);
		program_counter = char_to_hex(temp);

		//printf("LAlll %lx\n",x );
		//printf("LAlll %ld\n",x );

		return TRUE;
	}


	return FALSE;
}
Exemplo n.º 4
0
char* unescape_string(char* string) {
	
	/*
	 * Description: given an input stream converts escaped characters into their appropriate char representation and returns it as a new string
	 * Parameters: string to be converted
	 * Modifies: nothing
	 * Returns: resulting string with all the escaped characters with their appropriate values
	 * 
	 */

	//printf("unecaping: %s\n", string);
	char* unescaped_string = (char*)malloc(strlen(string) * sizeof(char) + 1);
	int current_position = 0;
	int unescaped_string_position = 0;
	unsigned char escape_character = 0;	
	
	for(current_position = 0; current_position < strlen(string); current_position++) {	
			escape_character = *(string + current_position);
			if(*(string + current_position) == '\\') {
				if(*(string + current_position + 1) == 'x') {
					current_position++;
					int hex_count;
					escape_character = 0;
					for(hex_count = 1; hex_count <= MAX_HEX_CHARS; hex_count++) {
						if(!isxdigit(*(string + current_position + hex_count))) {
							break;
						}
						escape_character = escape_character * 16 + char_to_hex(*(string + current_position + hex_count));
					}
					hex_count--;
					current_position += hex_count;
				} else if(is_oct_digit((*(string + current_position + 1))) == 1) {
					int oct_count;
					escape_character = 0;
					for(oct_count = 1; oct_count <= MAX_OCT_CHARS; oct_count++) {
						if(is_oct_digit(*(string + current_position + oct_count)) == 0) {
							break;
						}
						escape_character = escape_character * 8 + char_to_oct(*(string + current_position + oct_count));
					}
					current_position += oct_count;
				} else {
					escape_character = is_escape_character(*(string + current_position + 1));
					
					if(escape_character == 0) {
						escape_character = *(string + current_position);
					} else {
						current_position++;
					}
				}
			}
			*(unescaped_string + unescaped_string_position) = escape_character;
			unescaped_string_position++;
			escape_character = 0;
	}
	
	*(unescaped_string + unescaped_string_position + 1) = '\0';
	
	return unescaped_string;
}
Exemplo n.º 5
0
/* Calculates a new opaque value. Please note that opaque needs to be at least (MD5_DIGEST_LENGTH * 2) + 1 large. */
void calculate_opaque(IN_ADDR_T addr, char *opaque)
{
	char noncetmp[128];
	unsigned char md5tmp[MD5_DIGEST_LENGTH];
	snprintf(noncetmp, sizeof(noncetmp), "%d:%s:%d", (int32_t)time(NULL), cs_inet_ntoa(addr), (int16_t)rand());
	char_to_hex(MD5((unsigned char *)noncetmp, strlen(noncetmp), md5tmp), MD5_DIGEST_LENGTH, (unsigned char *)opaque);
}
Exemplo n.º 6
0
char* charString_to_hexString(char *s)
{
	int length = 0;
	char *x;
	char *y = s;

	while(*y != '\0')
	{
		length += 1;
		y += 1;
	}

	x = (char *) malloc((2 * length + 1) * sizeof(char));

	y = s;  // shifting the address to the original string

	for(int i = 0; i < length; ++i)
	{
	    sprintf(x + 2 * i, "%s", char_to_hex(y[i]));
	}
	
	x[2 * length] = '\0';   // making last element null character

	return x;
}
Exemplo n.º 7
0
bool hex_decode(const char *str, size_t slen, void *buf, size_t bufsize)
{
	unsigned char v1, v2;
	unsigned char *p = buf;

	while (slen > 1) {
		if (!char_to_hex(&v1, str[0]) || !char_to_hex(&v2, str[1]))
			return false;
		if (!bufsize)
			return false;
		*(p++) = (v1 << 4) | v2;
		str += 2;
		slen -= 2;
		bufsize--;
	}
	return slen == 0 && bufsize == 0;
}
Exemplo n.º 8
0
    string bytes_as_hex(const char* data, size_t size)
    {
        string str(size * 2, ' ');
        char* str_data = (char*)str.data();
        for (size_t i = 0; i < size; ++i)
        {
            char_to_hex(data[i], str_data + i * 2);
        }

        return str;
    }
Exemplo n.º 9
0
int HD_LoadCardState::copy_bitmap_buf(const char *buffer,int buf_len)
{
	unsigned char tmp[512];
	size_t write_len;
	int i,len;
	len = buf_len/2;

	// add 2007-8-16
	// 需要初始化一下
	ACE_OS::memset(tmp,0,sizeof tmp);
	for(i = 0;i < len && i < sizeof(tmp) ;++i)
	{
		tmp[i] = (char_to_hex(buffer[i*2]) << 4) |
			(char_to_hex(buffer[i*2+1]));
	}
	if(i > 0)
		memcpy(_pbuf+_offset,tmp,i);

	_offset += len;
	return 0;
}
TSTRING cCharEncoderUtil::CharStringToHexValue( const TSTRING& str )
{
    TSTRING strOut;
    TSTRING::const_iterator at;

    for( at = str.begin(); at < str.end(); at++ )
    {
        strOut += char_to_hex( *at );
    }

    return strOut;
}
Exemplo n.º 11
0
int competitor_main() {
  size_t hex2ascii_len = 256;
  char** hex2ascii;
  int i;
  hex2ascii = malloc(hex2ascii_len*sizeof(char*));
  for(i=0; i<hex2ascii_len; i++) {
    hex2ascii[i] = malloc(3*sizeof(char));
    snprintf(hex2ascii[i], 3,"%02X", i);
  }
  size_t len = 8;
  const unsigned char a[] = "DO NOT WANT";
  printf("%s\n", char_to_hex((const unsigned char*)a, len, (char**)hex2ascii));
}
Exemplo n.º 12
0
static void unescape_string(char *string)
{
	char	*p;

	for (p=string ; *p ; p++)
	{
		/* check if we have \x */
		if ((*p == '\\') && (*(p+1) == 'x'))
		{
			/* this is not cheap, but it should not occur often */

			/* check if we have two more values following \x */
			if (*(p+2) && *(p+3))
			{
				*p = char_to_hex(*(p+2));
				*p <<= 4;
				*p |= char_to_hex(*(p+3));

				memmove(p + 1, p + 4, strlen(p+4) + 1);
			}
		}
	}
}
Exemplo n.º 13
0
char *
url_encode(char *s)
{
  char *p = s;
  char *buf = g_malloc((strlen(s) * 3) + 1);
  char *pbuf = buf;

  // NOTE: '/' will not be url encoded
  while(*p) {
    if(isalnum(*p) || *p == '/' || *p == '-' || *p == '_' || *p == '.' || *p == '~')
      *pbuf++ = *p;
    else if(*p == ' ')
      *pbuf++ = '+';
    else
      *pbuf++ = '%', *pbuf++ = char_to_hex(*p >> 4), *pbuf++ = char_to_hex(*p & 15);

    p++;
  }

  *pbuf = '\0';
  return buf;
}
Exemplo n.º 14
0
void urlencode(const char * src, int src_len, char * dest,
        int dest_len) {
	unsigned char ch;
	int len = 0;

	while (len < (dest_len - 4) && *src) {
		ch = (unsigned char) *src;
		if (*src == ' ') {
			*dest++ = '+';
		} else if (is_alpha_number_char(ch) || strchr("=!~*'()", ch)) {
			*dest++ = *src;
		} else {
			*dest++ = '%';
			*dest++ = char_to_hex((unsigned char) (ch >> 4));
			*dest++ = char_to_hex((unsigned char) (ch % 16));
		}
		++src;
		++len;
		if(len >= src_len)
			break;
	}
	*dest = 0;
	return;
}
Exemplo n.º 15
0
/* Checks if authentication is correct. Returns -1 if not correct, 1 if correct and 2 if nonce isn't valid anymore.
   Note that authstring will be modified. */
int32_t check_auth(char *authstring, char *method, char *path, IN_ADDR_T addr, char *expectednonce, char *opaque)
{
	int32_t authok = 0, uriok = 0;
	char authnonce[(MD5_DIGEST_LENGTH * 2) + 1];
	memset(authnonce, 0, sizeof(authnonce));
	char *authnc = "";
	char *authcnonce = "";
	char *authresponse = "";
	char *uri = "";
	char *username = "";
	char *expectedPassword = cfg.http_pwd;
	char *pch = authstring + 22;
	char *pch2;
	char *saveptr1 = NULL;
	memset(opaque, 0, (MD5_DIGEST_LENGTH * 2) + 1);

	for(pch = strtok_r(pch, ",", &saveptr1); pch; pch = strtok_r(NULL, ",", &saveptr1))
	{
		pch2 = pch;
		while(pch2[0] == ' ' && pch2[0] != '\0') { ++pch2; }
		if(strncmp(pch2, "nonce", 5) == 0)
		{
			cs_strncpy(authnonce, parse_auth_value(pch2), sizeof(authnonce));
		}
		else if(strncmp(pch2, "nc", 2) == 0)
		{
			authnc = parse_auth_value(pch2);
		}
		else if(strncmp(pch2, "cnonce", 6) == 0)
		{
			authcnonce = parse_auth_value(pch2);
		}
		else if(strncmp(pch2, "response", 8) == 0)
		{
			authresponse = parse_auth_value(pch2);
		}
		else if(strncmp(pch2, "uri", 3) == 0)
		{
			uri = parse_auth_value(pch2);
		}
		else if(strncmp(pch2, "username", 8) == 0)
		{
			username = parse_auth_value(pch2);
		}
		else if(strncmp(pch2, "opaque", 6) == 0)
		{
			char *tmp = parse_auth_value(pch2);
			cs_strncpy(opaque, tmp, (MD5_DIGEST_LENGTH * 2) + 1);
		}
	}

	if(strncmp(uri, path, strlen(path)) == 0) { uriok = 1; }
	else
	{
		pch2 = uri;
		for(pch = uri; pch[0] != '\0'; ++pch)
		{
			if(pch[0] == '/') { pch2 = pch; }
			if(strncmp(pch2, path, strlen(path)) == 0) { uriok = 1; }
		}
	}
	if(uriok == 1 && streq(username, cfg.http_user))
	{
		char A1tmp[3 + strlen(username) + strlen(AUTHREALM) + strlen(expectedPassword)];
		char A1[(MD5_DIGEST_LENGTH * 2) + 1], A2[(MD5_DIGEST_LENGTH * 2) + 1], A3[(MD5_DIGEST_LENGTH * 2) + 1];
		unsigned char md5tmp[MD5_DIGEST_LENGTH];
		snprintf(A1tmp, sizeof(A1tmp), "%s:%s:%s", username, AUTHREALM, expectedPassword);
		char_to_hex(MD5((unsigned char *)A1tmp, strlen(A1tmp), md5tmp), MD5_DIGEST_LENGTH, (unsigned char *)A1);

		char A2tmp[2 + strlen(method) + strlen(uri)];
		snprintf(A2tmp, sizeof(A2tmp), "%s:%s", method, uri);
		char_to_hex(MD5((unsigned char *)A2tmp, strlen(A2tmp), md5tmp), MD5_DIGEST_LENGTH, (unsigned char *)A2);

		char A3tmp[10 + strlen(A1) + strlen(A2) + strlen(authnonce) + strlen(authnc) + strlen(authcnonce)];
		snprintf(A3tmp, sizeof(A3tmp), "%s:%s:%s:%s:auth:%s", A1, authnonce, authnc, authcnonce, A2);
		char_to_hex(MD5((unsigned char *)A3tmp, strlen(A3tmp), md5tmp), MD5_DIGEST_LENGTH, (unsigned char *)A3);

		if(strcmp(A3, authresponse) == 0)
		{
			if(strlen(opaque) != MD5_DIGEST_LENGTH * 2) { calculate_opaque(addr, opaque); }
			calculate_nonce(authnonce, expectednonce, opaque);
			if(strcmp(expectednonce, authnonce) == 0) { authok = 1; }
			else
			{
				authok = 2;
				cs_debug_mask(D_TRACE, "WebIf: Received stale header from %s (nonce=%s, expectednonce=%s, opaque=%s).", cs_inet_ntoa(addr), authnonce, expectednonce, opaque);
			}
		}
	}
	return authok;
}
Exemplo n.º 16
0
static int
lws_urldecode_s_process(struct lws_urldecode_stateful *s, const char *in,
			int len)
{
	int n, m, hit = 0;
	char c, was_end = 0;

	while (len--) {
		if (s->pos == s->out_len - s->mp - 1) {
			if (s->output(s->data, s->name, &s->out, s->pos, 0))
				return -1;

			was_end = s->pos;
			s->pos = 0;
		}
		switch (s->state) {

		/* states for url arg style */

		case US_NAME:
			s->inside_quote = 0;
			if (*in == '=') {
				s->name[s->pos] = '\0';
				s->pos = 0;
				s->state = US_IDLE;
				in++;
				continue;
			}
			if (*in == '&') {
				s->name[s->pos] = '\0';
				if (s->output(s->data, s->name, &s->out,
					      s->pos, 1))
					return -1;
				s->pos = 0;
				s->state = US_IDLE;
				in++;
				continue;
			}
			if (s->pos >= (int)sizeof(s->name) - 1) {
				lwsl_notice("Name too long\n");
				return -1;
			}
			s->name[s->pos++] = *in++;
			break;
		case US_IDLE:
			if (*in == '%') {
				s->state++;
				in++;
				continue;
			}
			if (*in == '&') {
				s->out[s->pos] = '\0';
				if (s->output(s->data, s->name, &s->out,
					      s->pos, 1))
					return -1;
				s->pos = 0;
				s->state = US_NAME;
				in++;
				continue;
			}
			if (*in == '+') {
				in++;
				s->out[s->pos++] = ' ';
				continue;
			}
			s->out[s->pos++] = *in++;
			break;
		case US_PC1:
			n = char_to_hex(*in);
			if (n < 0)
				return -1;

			in++;
			s->sum = n << 4;
			s->state++;
			break;

		case US_PC2:
			n = char_to_hex(*in);
			if (n < 0)
				return -1;

			in++;
			s->out[s->pos++] = s->sum | n;
			s->state = US_IDLE;
			break;


		/* states for multipart / mime style */

		case MT_LOOK_BOUND_IN:
retry_as_first:
			if (*in == s->mime_boundary[s->mp] &&
			    s->mime_boundary[s->mp]) {
				in++;
				s->mp++;
				if (!s->mime_boundary[s->mp]) {
					s->mp = 0;
					s->state = MT_IGNORE1;

					if (s->pos || was_end)
						if (s->output(s->data, s->name,
						      &s->out, s->pos, 1))
							return -1;

					s->pos = 0;

					s->content_disp[0] = '\0';
					s->name[0] = '\0';
					s->content_disp_filename[0] = '\0';
					s->boundary_real_crlf = 1;
				}
				continue;
			}
			if (s->mp) {
				n = 0;
				if (!s->boundary_real_crlf)
					n = 2;
				if (s->mp >= n) {
					memcpy(s->out + s->pos,
					       s->mime_boundary + n, s->mp - n);
					s->pos += s->mp;
					s->mp = 0;
					goto retry_as_first;
				}
			}

			s->out[s->pos++] = *in;
			in++;
			s->mp = 0;
			break;

		case MT_HNAME:
			m = 0;
			c =*in;
			if (c >= 'A' && c <= 'Z')
				c += 'a' - 'A';
			for (n = 0; n < (int)LWS_ARRAY_SIZE(mp_hdr); n++)
				if (c == mp_hdr[n][s->mp]) {
					m++;
					hit = n;
				}
			in++;
			if (!m) {
				s->state = MT_IGNORE1; // Unknown header - ignore it
				s->mp = 0;
				continue;
			}

			s->mp++;
			if (m != 1)
				continue;

			if (mp_hdr[hit][s->mp])
				continue;

			s->mp = 0;
			s->temp[0] = '\0';
			s->subname = 0;

			if (hit == 2)
				s->state = MT_LOOK_BOUND_IN;
			else
				s->state += hit + 1;
			break;

		case MT_DISP:
			/* form-data; name="file"; filename="t.txt" */

			if (*in == '\x0d') {
				if (s->content_disp_filename[0])
					if (s->output(s->data, s->name,
						      &s->out, s->pos,
						      LWS_UFS_OPEN))
						return -1;
				s->state = MT_IGNORE2;
				goto done;
			}
			if (*in == ';') {
				s->subname = 1;
				s->temp[0] = '\0';
				s->mp = 0;
				goto done;
			}

			if (*in == '\"') {
				s->inside_quote ^= 1;
				goto done;
			}

			if (s->subname) {
				if (*in == '=') {
					s->temp[s->mp] = '\0';
					s->subname = 0;
					s->mp = 0;
					goto done;
				}
				if (s->mp < (int)sizeof(s->temp) - 1 &&
				    (*in != ' ' || s->inside_quote))
					s->temp[s->mp++] = *in;
				goto done;
			}

			if (!s->temp[0]) {
				if (s->mp < (int)sizeof(s->content_disp) - 1)
					s->content_disp[s->mp++] = *in;
				s->content_disp[s->mp] = '\0';
				goto done;
			}

			if (!strcmp(s->temp, "name")) {
				if (s->mp < (int)sizeof(s->name) - 1)
					s->name[s->mp++] = *in;
				else
					s->mp = (int)sizeof(s->name) - 1;
				s->name[s->mp] = '\0';
				goto done;
			}

			if (!strcmp(s->temp, "filename")) {
				if (s->mp < (int)sizeof(s->content_disp_filename) - 1)
					s->content_disp_filename[s->mp++] = *in;
				s->content_disp_filename[s->mp] = '\0';
				goto done;
			}
done:
			in++;
			break;

		case MT_TYPE:
			if (*in == '\x0d')
				s->state = MT_IGNORE2;
			else {
				if (s->mp < (int)sizeof(s->content_type) - 1)
					s->content_type[s->mp++] = *in;
				s->content_type[s->mp] = '\0';
			}
			in++;
			break;

		case MT_IGNORE1:
			if (*in == '\x0d')
				s->state = MT_IGNORE2;
			if (*in == '-')
				s->state = MT_IGNORE3;
			in++;
			break;

		case MT_IGNORE2:
			s->mp = 0;
			if (*in == '\x0a')
				s->state = MT_HNAME;
			in++;
			break;

		case MT_IGNORE3:
			if (*in == '\x0d')
				s->state = MT_IGNORE1;
			if (*in == '-') {
				s->state = MT_COMPLETED;
				s->wsi->http.rx_content_remain = 0;
			}
			in++;
			break;
		case MT_COMPLETED:
			break;
		}
	}

	return 0;
}
Exemplo n.º 17
0
LWS_VISIBLE int
lws_http_client_read(struct lws *wsi, char **buf, int *len)
{
	int rlen, n;



	rlen = lws_ssl_capable_read(wsi, (unsigned char *)*buf, *len);
	if (rlen < 0)
		return -1;

	*len = rlen;
	if (rlen == 0)
		return 0;

//	lwsl_err("%s: read %d\n", __func__, rlen);

	/* allow the source to signal he has data again next time */
	wsi->client_rx_avail = 0;
	lws_change_pollfd(wsi, 0, LWS_POLLIN);

	/*
	 * server may insist on transfer-encoding: chunked,
	 * so http client must deal with it
	 */
spin_chunks:
	while (wsi->chunked && (wsi->chunk_parser != ELCP_CONTENT) && *len) {
		switch (wsi->chunk_parser) {
		case ELCP_HEX:
			if ((*buf)[0] == '\x0d') {
				wsi->chunk_parser = ELCP_CR;
				break;
			}
			n = char_to_hex((*buf)[0]);
			if (n < 0)
				return -1;
			wsi->chunk_remaining <<= 4;
			wsi->chunk_remaining |= n;
			break;
		case ELCP_CR:
			if ((*buf)[0] != '\x0a')
				return -1;
			wsi->chunk_parser = ELCP_CONTENT;
			lwsl_info("chunk %d\n", wsi->chunk_remaining);
			if (wsi->chunk_remaining)
				break;
			lwsl_info("final chunk\n");
			goto completed;

		case ELCP_CONTENT:
			break;

		case ELCP_POST_CR:
			if ((*buf)[0] != '\x0d')
				return -1;

			wsi->chunk_parser = ELCP_POST_LF;
			break;

		case ELCP_POST_LF:
			if ((*buf)[0] != '\x0a')
				return -1;

			wsi->chunk_parser = ELCP_HEX;
			wsi->chunk_remaining = 0;
			break;
		}
		(*buf)++;
		(*len)--;
	}

	if (wsi->chunked && !wsi->chunk_remaining)
		return 0;

	if (wsi->u.http.content_remain &&
	    (int)wsi->u.http.content_remain < *len)
		n = wsi->u.http.content_remain;
	else
		n = *len;

	if (wsi->chunked && wsi->chunk_remaining &&
	    wsi->chunk_remaining < n)
		n = wsi->chunk_remaining;

#ifdef LWS_WITH_HTTP_PROXY
	/* hubbub */
	if (wsi->perform_rewrite)
		lws_rewrite_parse(wsi->rw, (unsigned char *)*buf, n);
	else
#endif
		if (user_callback_handle_rxflow(wsi->protocol->callback,
				wsi, LWS_CALLBACK_RECEIVE_CLIENT_HTTP_READ,
				wsi->user_space, *buf, n))
			return -1;

	if (wsi->chunked && wsi->chunk_remaining) {
		(*buf) += n;
		wsi->chunk_remaining -= n;
		*len -= n;
	}

	if (wsi->chunked && !wsi->chunk_remaining)
		wsi->chunk_parser = ELCP_POST_CR;

	if (wsi->chunked && *len) {
		goto spin_chunks;
	}

	if (wsi->chunked)
		return 0;

	wsi->u.http.content_remain -= n;
	if (wsi->u.http.content_remain || !wsi->u.http.content_length)
		return 0;

completed:
	if (user_callback_handle_rxflow(wsi->protocol->callback,
			wsi, LWS_CALLBACK_COMPLETED_CLIENT_HTTP,
			wsi->user_space, NULL, 0))
		return -1;

	if (lws_http_transaction_completed_client(wsi))
		return -1;

	return 0;
}
Exemplo n.º 18
0
	CharType dereference() const {
		return char_to_hex(*m_base);
	}
static ssize_t ft5x0x_write(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
{
	int ret = 0, i = 0, send_len = 0;
	static size_t length;	
	E_UPGRADE_ERR_TYPE err = ERR_REV_FILE_FAIL;
	static FTS_BYTE  *to_buf = NULL, *pre_to_buf = NULL, *free_to_buf = NULL;
	
	if (!strcmp(attr->attr.name, "ft5x0xPrintFlag")) {
		printk("buf[0]=%d, buf[1]=%d\n", buf[0], buf[1]);
		if (buf[0] == '0') ft5x0x_printk_enable_flag = 0;
		if (buf[0] == '1') ft5x0x_printk_enable_flag = 1;
		if (buf[0] == '2') {
			ft5x0x_printk_enable_flag=2;
			if(focaltechPdata2->power){
				focaltechPdata2->power(0);
				msleep(50);
				focaltechPdata2->power(1);
				msleep(200);
			}
		}
		if (buf[0] == '3') {
			u8 data;
			ft5x0x_write_reg(0xa5, 0x03);
			printk("set reg[0xa5] = 0x03\n");
			msleep(20);
			ft5x0x_read_reg(0xa5, &data);
			printk("read back: reg[0xa5] = %d\n", data);
    }
  }
	else if (!strcmp(attr->attr.name, "effect")) {    
       printk("buf[0]=%d, buf[1]=%d\n", buf[0], buf[1]);
  }
  else if (!strcmp(attr->attr.name, "upgrade")) {
	     if (length == 0) {        
	       if (free_to_buf == NULL) free_to_buf = (FTS_BYTE *)kmalloc((128*1024-20)*sizeof(FTS_BYTE), GFP_KERNEL);
         printk("start free_to_buf = 0x%x.\n", free_to_buf); 
         if (free_to_buf == NULL) {
           printk("Insufficient memory in upgrade!\n");
           length = 0;
           upgrade_result = ERR_REV_FILE_FAIL;
           return -ERR_REV_FILE_FAIL;
         }         
         pre_to_buf = free_to_buf;  
         to_buf = pre_to_buf;
         upgrade_result = ERR_IN_UPGRADE;  
	     }
	     memcpy(to_buf, buf, count);
	     
	     to_buf += count;
	     length += count;
	    
	   //  printk("upgrade data count = %d.\n", count); 
       //  printk("upgrade data length = %d.\n", length);  
	    
	     if (length > (128*1024-25)) {
	       upgrade_result = ERR_REV_FILE_FAIL;
	       printk("upgrade_result = %d.\n", ERR_REV_FILE_FAIL);
	       printk("end free_to_buf = 0x%x.\n", free_to_buf); 
	       length = 0;
	       if (free_to_buf != NULL) {
	         kfree(free_to_buf);
	         free_to_buf = NULL;
	       }	       
	       return ERR_REV_FILE_FAIL;
	     }     
      
	     if ((to_buf != NULL) && (*(to_buf-1)=='d') && (*(to_buf-2)=='n') && (*(to_buf-3)=='e')) {
	         for (i=0; i<length-3;) {
              if ((*pre_to_buf=='0') && (*(pre_to_buf+1)=='x')) {     
      	        if ((*(pre_to_buf+3)==',') && (*(pre_to_buf+4)==' ')) free_to_buf[send_len++] = char_to_hex('0', *(pre_to_buf+2));    
      	        else free_to_buf[send_len++] = char_to_hex(*(pre_to_buf+2), *(pre_to_buf+3));        
	    			    pre_to_buf += 5; 
      	        i += 5;
	    			  }
	    			  else {
	    			    pre_to_buf++;
	    			  	i++;
	    			  }	    			  		    				    			     			        	   
	    	   }
	    	  
	    	   for (i=0; i<10; i++) {
	    	     printk("free_to_buf[%d] = 0x%x.\n", i, free_to_buf[i]); 
	    	   }	    	  
	    	   for (i=send_len-1; i>=send_len-10; i--) {
	    	  	 printk("free_to_buf[%d] = 0x%x.\n", i, free_to_buf[i]); 
	    	   }
	    	 
	    	   for (i=0; i<5; i++) {
	    	     err = fts_ctpm_fw_upgrade(free_to_buf, send_len);
	    	     if (err == 0) break;
	       	 }
	    	   upgrade_result = err;
	    	   printk("upgrade status is %d.\n", err); 
	    	   printk("upgrade data send_len = %d.\n", send_len); 
	    	   printk("end free_to_buf = 0x%x.\n", free_to_buf); 
	    	   if (free_to_buf != NULL) {
	    	     kfree(free_to_buf);
	    	     free_to_buf = NULL;
	    	   }
	    	   length = 0;	    	      
	     }
	    // #endif                                
   } 
	return count; 
}
Exemplo n.º 20
0
int bin2hex_main(int argc, char ** argv) {
  FILE * rand_fh;
  char lt[256][2];
  unsigned int x,y;
  char * newhex_dest;
  union {
    uint32_t u32;
    uint8_t u8[4];
  } random_num;
  uint16_t i;
  printf("***************** BIN2HEX *****************\n");

  if(bin2hex_init(lt) == 0) {
    // Fail.. Leave
    fprintf(stderr, "Unable to init lookup table.\n");
    return EXIT_FAILURE;
  }

  rand_fh = fopen("/dev/urandom", "r");
  if(!rand_fh) {
    fprintf(stderr, "Unable to open random buffer.\n");
    return EXIT_FAILURE;
  }
  
  printf("Table created: \n");
  for(i=0; i<256; i++) {
    printf("%c%c", lt[i][0], lt[i][1]);
    if(i%15 == 0) printf("\n");
    else printf(" ");
  }

  // Initialize crazy binary array
  uint8_t * crazy_hex = malloc(CRAZY_SIZE);
  for(i=0; i<CRAZY_SIZE-3; i++) {
    if((fread(&random_num.u32, 4, 1, rand_fh) == 0)) {
      fprintf(stderr, "Could not read 4 bytes from file.\n");
      return EXIT_FAILURE;
    }
    crazy_hex[i] = random_num.u8[0];
    crazy_hex[i+1] = random_num.u8[1];
    crazy_hex[i+2] = random_num.u8[2];
    crazy_hex[i+3] = random_num.u8[3];
    i += 4;
  }
  char hex_dest[2 + 2*CRAZY_SIZE]; 
  hex_dest[0] = '0';
  hex_dest[1] = 'x';
  GET_CYCLES(x);
  bin2hex(lt, &hex_dest[2], crazy_hex, CRAZY_SIZE);
  GET_CYCLES(y);
  printf("time=%u, ", y-x);
  printf("Output: %.*s\n", 2 + 2*CRAZY_SIZE, hex_dest);
  printf("***************** BIN2HEX *****************\n");
  printf("***************** BIN2HEX Competition *****************\n");

  size_t hex2ascii_len = 256;
  char** hex2ascii;
  hex2ascii = malloc(hex2ascii_len*sizeof(char*));
  for(i=0; i<hex2ascii_len; i++) {
    hex2ascii[i] = malloc(3*sizeof(char));
    snprintf(hex2ascii[i], 3,"%02X", i);
  }
  size_t len = 8;
  GET_CYCLES(x);
  newhex_dest = char_to_hex((const unsigned char*)crazy_hex, CRAZY_SIZE, (char**)hex2ascii);
  GET_CYCLES(y);
  printf("time=%u, ", y-x);
  printf("Output: 0x%s\n", newhex_dest);

  fclose(rand_fh);

  return EXIT_SUCCESS;
}
Exemplo n.º 21
0
Arquivo: client.c Projeto: 93i/godot
LWS_VISIBLE int
lws_http_client_read(struct lws *wsi, char **buf, int *len)
{
	int rlen, n;

	rlen = lws_ssl_capable_read(wsi, (unsigned char *)*buf, *len);
	*len = 0;

	// lwsl_notice("%s: rlen %d\n", __func__, rlen);

	/* allow the source to signal he has data again next time */
	lws_change_pollfd(wsi, 0, LWS_POLLIN);

	if (rlen == LWS_SSL_CAPABLE_ERROR) {
		lwsl_notice("%s: SSL capable error\n", __func__);
		return -1;
	}

	if (rlen == 0)
		return -1;

	if (rlen < 0)
		return 0;

	*len = rlen;
	wsi->client_rx_avail = 0;

	/*
	 * server may insist on transfer-encoding: chunked,
	 * so http client must deal with it
	 */
spin_chunks:
	while (wsi->chunked && (wsi->chunk_parser != ELCP_CONTENT) && *len) {
		switch (wsi->chunk_parser) {
		case ELCP_HEX:
			if ((*buf)[0] == '\x0d') {
				wsi->chunk_parser = ELCP_CR;
				break;
			}
			n = char_to_hex((*buf)[0]);
			if (n < 0) {
				lwsl_debug("chunking failure\n");
				return -1;
			}
			wsi->chunk_remaining <<= 4;
			wsi->chunk_remaining |= n;
			break;
		case ELCP_CR:
			if ((*buf)[0] != '\x0a') {
				lwsl_debug("chunking failure\n");
				return -1;
			}
			wsi->chunk_parser = ELCP_CONTENT;
			lwsl_info("chunk %d\n", wsi->chunk_remaining);
			if (wsi->chunk_remaining)
				break;
			lwsl_info("final chunk\n");
			goto completed;

		case ELCP_CONTENT:
			break;

		case ELCP_POST_CR:
			if ((*buf)[0] != '\x0d') {
				lwsl_debug("chunking failure\n");

				return -1;
			}

			wsi->chunk_parser = ELCP_POST_LF;
			break;

		case ELCP_POST_LF:
			if ((*buf)[0] != '\x0a')
				return -1;

			wsi->chunk_parser = ELCP_HEX;
			wsi->chunk_remaining = 0;
			break;
		}
		(*buf)++;
		(*len)--;
	}

	if (wsi->chunked && !wsi->chunk_remaining)
		return 0;

	if (wsi->http.rx_content_remain &&
	    wsi->http.rx_content_remain < (unsigned int)*len)
		n = (int)wsi->http.rx_content_remain;
	else
		n = *len;

	if (wsi->chunked && wsi->chunk_remaining &&
	    wsi->chunk_remaining < n)
		n = wsi->chunk_remaining;

#ifdef LWS_WITH_HTTP_PROXY
	/* hubbub */
	if (wsi->http.perform_rewrite)
		lws_rewrite_parse(wsi->http.rw, (unsigned char *)*buf, n);
	else
#endif
	{
		struct lws *wsi_eff = lws_client_wsi_effective(wsi);

		if (user_callback_handle_rxflow(wsi_eff->protocol->callback,
				wsi_eff, LWS_CALLBACK_RECEIVE_CLIENT_HTTP_READ,
				wsi_eff->user_space, *buf, n)) {
			lwsl_debug("%s: RECEIVE_CLIENT_HTTP_READ returned -1\n",
				   __func__);

			return -1;
		}
	}

	if (wsi->chunked && wsi->chunk_remaining) {
		(*buf) += n;
		wsi->chunk_remaining -= n;
		*len -= n;
	}

	if (wsi->chunked && !wsi->chunk_remaining)
		wsi->chunk_parser = ELCP_POST_CR;

	if (wsi->chunked && *len)
		goto spin_chunks;

	if (wsi->chunked)
		return 0;

	/* if we know the content length, decrement the content remaining */
	if (wsi->http.rx_content_length > 0)
		wsi->http.rx_content_remain -= n;

	// lwsl_notice("rx_content_remain %lld, rx_content_length %lld\n",
	//	wsi->http.rx_content_remain, wsi->http.rx_content_length);

	if (wsi->http.rx_content_remain || !wsi->http.rx_content_length)
		return 0;

completed:

	if (lws_http_transaction_completed_client(wsi)) {
		lwsl_notice("%s: transaction completed says -1\n", __func__);
		return -1;
	}

	return 0;
}
Exemplo n.º 22
0
/* Calculates the currently valid nonce value and copies it to result. Please note that nonce (may be NULL), opaque and result needs to be at least (MD5_DIGEST_LENGTH * 2) + 1 large. */
void calculate_nonce(char *nonce, char *result, char *opaque)
{
	struct s_nonce *noncelist, *prev, *foundnonce = NULL, *foundopaque = NULL, *foundexpired = NULL;
	int32_t bucket = opaque[0] % AUTHNONCEHASHBUCKETS;
	time_t now = time(NULL);
	cs_writelock(&nonce_lock[bucket]);
	for(noncelist = nonce_first[bucket], prev = NULL; noncelist; prev = noncelist, noncelist = noncelist->next)
	{
		if(now > noncelist->expirationdate)
		{
			if(prev) { prev->next = NULL; }
			else
			{
				nonce_first[bucket] = NULL;
			}
			foundexpired = noncelist;
			break;
		}
		if(nonce && !memcmp(noncelist->nonce, nonce, (MD5_DIGEST_LENGTH * 2) + 1))
		{
			memcpy(result, noncelist->nonce, (MD5_DIGEST_LENGTH * 2) + 1);
			foundnonce = noncelist;
			if(!noncelist->firstuse) { noncelist->firstuse = now; }
			else if(now - foundnonce->firstuse > AUTHNONCEVALIDSECS)
			{
				if(prev) { prev->next = noncelist->next; }
				else
				{
					nonce_first[bucket] = noncelist->next;
				}
			}
			break;
		}
		else if(!noncelist->firstuse && !memcmp(noncelist->opaque, opaque, (MD5_DIGEST_LENGTH * 2) + 1))
		{
			foundopaque = noncelist;
		}
	}
	if(foundnonce && now - foundnonce->firstuse > AUTHNONCEVALIDSECS)
	{
		NULLFREE(foundnonce);
		foundnonce = NULL;
	}
	if(!foundnonce && foundopaque)
		{ memcpy(result, foundopaque->nonce, (MD5_DIGEST_LENGTH * 2) + 1); }
	if(!foundnonce && !foundopaque)
	{
		char noncetmp[128], randstr[16];
		unsigned char md5tmp[MD5_DIGEST_LENGTH];
		get_random_bytes((uint8_t *)randstr, sizeof(randstr) - 1);
		randstr[sizeof(randstr) - 1] = '\0';
		snprintf(noncetmp, sizeof(noncetmp), "%d:%s:%s", (int32_t)now, randstr, noncekey);
		char_to_hex(MD5((unsigned char *)noncetmp, strlen(noncetmp), md5tmp), MD5_DIGEST_LENGTH, (unsigned char *)result);
		if(cs_malloc(&noncelist, sizeof(struct s_nonce)))
		{
			noncelist->expirationdate = now + AUTHNONCEEXPIRATION;
			memcpy(noncelist->nonce, result, (MD5_DIGEST_LENGTH * 2) + 1);
			memcpy(noncelist->opaque, opaque, (MD5_DIGEST_LENGTH * 2) + 1);
			noncelist->next = nonce_first[bucket];
			nonce_first[bucket] = noncelist;
		}
	}
	cs_writeunlock(&nonce_lock[bucket]);
	while(foundexpired)
	{
		prev = foundexpired;
		foundexpired = foundexpired->next;
		NULLFREE(prev);
	}
}