Example #1
0
static void url_decode( LPTSTR cp )
{
    for ( ; *cp; cp++ ) {
        if ( *cp == __T('+') )
            *cp = __T(' ');
        else
        if ( *cp == __T('%') ) {
            *cp = (_TCHAR)(hextoint(*(cp+1)) * 16 + hextoint(*(cp+2)));
            memmove( cp+1, cp+3, (_tcslen(cp+3)+1)*sizeof(_TCHAR) );
        }
    }
}
Example #2
0
static struct object *load_xbm( struct pike_string *data )
{
  int width, height;
  int x, y;
  struct buffer buff;
  struct buffer *b = &buff;
  rgb_group *dest;
  struct object *io;

  buff.str = data->str;
  buff.len = data->len;

  if(!buf_search( b, '#' ) || !buf_search( b, ' ' ) || !buf_search( b, ' ' ))
    Pike_error("This is not a XBM image!\n");
  width = atoi(b->str);
  if(width <= 0)
    Pike_error("This is not a XBM image!\n");
  if(!buf_search( b, '#' ) || !buf_search( b, ' ' ) || !buf_search( b, ' ' ))
    Pike_error("This is not a XBM image!\n");
  height = atoi(b->str);
  if(height <= 0)
    Pike_error("This is not a XBM image!\n");
  
  if(!buf_search( b, '{' ))
    Pike_error("This is not a XBM image!\n");


  push_int( width );
  push_int( height );
  io = clone_object( image_program, 2 );
  dest = ((struct image *)get_storage(io, image_program))->img;
  /* .. the code below asumes black if the read fails.. */
  for(y=0; y<height; y++)
  {
    int next_byte, cnt;
    for(x=0; x<width;)
    {
      if(buf_search( b, 'x' ))
      {
        next_byte = (hextoint(buf_getc( b ))*0x10) | hextoint(buf_getc( b ));
        for(cnt=0; cnt<8&&x<width; cnt++,x++)
        {
          if((next_byte&(1<<(x%8))))
            dest->r = dest->g = dest->b = 255;
          dest++;
        }
      } else
	Pike_error("This is not a XBM image!\n");
    }
  }
  return io;
}
Example #3
0
void test_SingleDigit(void) {
        const char *str = "a"; // 10 decimal
        u_long actual;

        TEST_ASSERT_TRUE(hextoint(str, &actual));
        TEST_ASSERT_EQUAL(10, actual);
}
Example #4
0
void test_MaxUnsigned(void) {
        const char *str = "ffffffff"; // 4294967295 decimal
        u_long actual;

        TEST_ASSERT_TRUE(hextoint(str, &actual));
        TEST_ASSERT_EQUAL(4294967295UL, actual);
}
Example #5
0
void test_MultipleDigits(void) {
        const char *str = "8F3"; // 2291 decimal
        u_long actual;

        TEST_ASSERT_TRUE(hextoint(str, &actual));
        TEST_ASSERT_EQUAL(2291, actual);
}
bool client_handler::read_message(handler *h, buffer &buf) {
    int plen = buf.length();

    Log::d("read_message1");
    if (serv->read_chunk(h, &buf)) {
        Log::d("fd(" + inttostr(h->fd.get_fd()) + ") asked for disconnection");
        disconnect();
        return false;
    }
    Log::d("read_message2");
    if (message_type == NOT_EVALUATED) {
        // recalc message len
        int lb = find_double_line_break(buf.string_data(), plen);
        if (lb != -1) {
            //Log::d("plen is " + inttostr(plen) + ", lb is " + inttostr(lb) + " in \n\"" + buf.string_data() + "\"");

            std::string content_length_str;
            int len;
            if (extract_header(buf.string_data(), lb, "Content-Length", content_length_str)) {
                len = strtoint(content_length_str);
                message_type = VIA_CONTENT_LENGTH;
            } else {
                len = 0;
                message_type = extract_header(buf.string_data(), lb, "Transfer-Encoding", content_length_str)
                               ? VIA_TRANSFER_ENCODING : WITHOUT_BODY;
            }
            message_len = len + lb;
        }
    }

    if (message_type != HTTPS_MODE) {
        Log::d("read_message(" + inttostr(buf.length()) + ", " + inttostr(message_len) + ")");
    }

    if (message_type == NOT_EVALUATED) {
        return false;
    }

    if (message_type == VIA_TRANSFER_ENCODING) {
        while (buf.length() > message_len) {
            /*Log::d("kek is  " + inttostr((int) buf.string_data()[message_len - 1]) + " " +
                   inttostr((int) buf.string_data()[message_len]) + " " +
                   inttostr((int) buf.string_data()[message_len + 1]));*/
            if (buf.string_data()[message_len] == '0') {
                return true;
            }
            size_t linebreak = buf.string_data().find("\r\n", (size_t) message_len);
            if (linebreak != std::string::npos) {
                std::string chunklen = buf.string_data().substr((size_t) message_len, linebreak - message_len);
                Log::d("Next chunk len is " + chunklen);
                message_len += chunklen.length() + hextoint(chunklen) + 4;
            } else {
                break;
            }
        }
        return false;
    } else {
        return buf.length() == message_len;
    }
}
long long hexmulthex(const char *hex_str_1, const char *hex_str_2) {
  long long hex_value_1 = hextoint(hex_str_1) ;
  long long hex_value_2 = hextoint(hex_str_2) ;
  
  /** Here we warn in overflow case. **/
  long double value1, value2 ;
  value1 = (long double) hex_value_1 ;
  value2 = (long double) hex_value_2 ;
  char *res_str=calloc(32,sizeof(char)) ;
  sprintf(res_str,"%Lf",value1 * value2) ;
  strip_to_int(res_str) ;
  errno=0 ;
  strtoll(res_str,NULL,0) ; /** In case of overflow errno = ERANGE **/
  free(res_str) ;
  
  return hex_value_1 * hex_value_2 ;
}
long double hexdivhex(const char *hex_str_1, const char *hex_str_2) {
  long double hex_value_1 = (long double) hextoint(hex_str_1) ;
  long double hex_value_2 = (long double) hextoint(hex_str_2) ;
  
  if (hex_value_2 == 0.0) {
    fprintf(stderr,"%s Zero division error\n",__func__) ;
    exit(EXIT_FAILURE) ;
  }
  
  feclearexcept(FE_OVERFLOW|FE_UNDERFLOW);
  
  long double res = hex_value_1 / hex_value_2  ;
  
  if (fetestexcept(FE_OVERFLOW|FE_UNDERFLOW) != 0) {
    errno=ERANGE ;
  }
  
  return res ;
}
Example #9
0
void
process_key (char *s, char *key)
{
    char string[257];
    int i;
    int j;
    int block_count;
    int shift = 1;
    int value = 0;
    int hex_count = 0;
    int nonhex_count = 0;
    int white_count = 0;

    char init[16] = 
    {  0xa1, 0x1f, 0x9d, 0x15, 0x3e, 0xdc, 0xeb, 0x85, 
       0xd0, 0x18, 0xc4, 0xbf, 0xb6, 0xf7, 0xa4, 0x9a
    };

    for (i=0; i<257; i++)
        string[i] = '\0';

    /* check for a hex number */
    for (i = 0; (i < 256) && *s; i++)
    {
        string[i] = *s++;
        if (ishex (string[i]))
            hex_count++;
        else if (string[i] == ' ')
            white_count++;
        else
            nonhex_count++;
    }
    string[i] = '\0';

    if (hex_count == 32 && nonhex_count == 0)
    {                                            /* convert hex number */
        hex_count = 0;
        for (i = 0; string[i] != '\0'; i++)
        {
            if ((value = hextoint (string[i])) != -1)
            {
                if (shift)
                {
                    key[hex_count >> 1] = (unsigned char) value << 4;
                    hex_count++;
                    shift = 0;
                }
                else
                {
                    key[hex_count >> 1] += (unsigned char) value;
                    hex_count++;
                    shift = 1;
                }
            }
        }
Example #10
0
static void
process_key (char *s, char *key)
{
    char string[257];
    int i;
    int j;
    int shift = 1;
    int value = 0;
    int hex_count = 0;
    int nonhex_count = 0;
    int white_count = 0;

    /* zero string */
    for (i=0; i<256; i++)
        string[i] = '\0';

    /* check if a hex number or ascii string was entered */
    for (i = 0; (i < 256) && *s; i++)
    {
        string[i] = *s++;
        if (ishex (string[i]))
            hex_count++;
        else if (string[i] == ' ')
            white_count++;
        else
            nonhex_count++;
    }
    string[i] = '\0';

    /* either convert hex number or process ascii string */
    if (hex_count == 32 && nonhex_count == 0)
    {
        hex_count = 0;
        for (i = 0; string[i] != '\0'; i++)
        {
            if ((value = hextoint (string[i])) != -1)
            {
                if (shift)
                {
                    key[hex_count >> 1] = (unsigned char) value << 4;
                    hex_count++;
                    shift = 0;
                }
                else
                {
                    key[hex_count >> 1] += (unsigned char) value;
                    hex_count++;
                    shift = 1;
                }
            }
            string[i] = '\0';
        }
Example #11
0
unsigned char *ConvertBufferToBinary(unsigned char **pBuffer)
{
    unsigned char *data;
    int i, j, index = 0;

    data = malloc(packetsize*sizeof(unsigned char));

    if (data == NULL)
        return NULL;

    for (i = 0; i < rows; i++)
        for (j = 0; j < 16; j++)
        {
            data[index] = hextoint(pBuffer[i][6 + j*3], pBuffer[i][7 + j*3]);
            ++index;

            if (index >= packetsize)
                break;
        }

    return data;
}
Example #12
0
/*
 * Convert a string containing C character escapes.  Stop at an unescaped
 * space or tab. Copy the converted version to "p", returning its length in
 * *slen. Return updated scan pointer as function result.
 */
static char *getstr(register char *s, register char *p,
					int plen, int *slen)
{
    char *origs = s, *origp = p;
    char *pmax = p + plen - 1;
    register int c;
    register int val;

    while ((c = *s++) != '\0') {
		if (isspace((unsigned char) c))
			break;
		if (p >= pmax) {
			TSRMLS_FETCH();
			if(MIME_MAGIC_G(debug))
				php_error_docref("http://www.php.net/mime_magic" TSRMLS_CC, E_WARNING, "string too long: %s", origs);
			break;
		}
		if (c == '\\') {
			switch (c = *s++) {

			case '\0':
				goto out;

			default:
				*p++ = (char) c;
				break;

			case 'n':
				*p++ = '\n';
				break;

			case 'r':
				*p++ = '\r';
				break;

			case 'b':
				*p++ = '\b';
				break;

			case 't':
				*p++ = '\t';
				break;

			case 'f':
				*p++ = '\f';
				break;

			case 'v':
				*p++ = '\v';
				break;

				/* \ and up to 3 octal digits */
			case '0':
			case '1':
			case '2':
			case '3':
			case '4':
			case '5':
			case '6':
			case '7':
				val = c - '0';
				c = *s++;	/* try for 2 */
				if (c >= '0' && c <= '7') {
					val = (val << 3) | (c - '0');
					c = *s++;	/* try for 3 */
					if (c >= '0' && c <= '7')
						val = (val << 3) | (c - '0');
					else
						--s;
				}
				else
					--s;
				*p++ = (char) val;
				break;

				/* \x and up to 3 hex digits */
			case 'x':
				val = 'x';	/* Default if no digits */
				c = hextoint(*s++);	/* Get next char */
				if (c >= 0) {
					val = c;
					c = hextoint(*s++);
					if (c >= 0) {
						val = (val << 4) + c;
						c = hextoint(*s++);
						if (c >= 0) {
							val = (val << 4) + c;
						}
						else
							--s;
					}
					else
						--s;
				}
				else
					--s;
				*p++ = (char) val;
				break;
			}
		}
		else
			*p++ = (char) c;
    }
 out:
    *p = '\0';
    *slen = p - origp;
    return s;
}
Example #13
0
void test_Overflow(void) {
        const char *str = "100000000"; // Overflow by 1
        u_long actual;

        TEST_ASSERT_FALSE(hextoint(str, &actual));
}
static int hex2toint(char *a)
{
	return (hextoint(a[0]) * 0x10 +  hextoint(a[1])) ;
}
static int hex4toint(char *a)
{
	return (hextoint(a[0]) * 0x1000 +   hextoint(a[1]) * 0x100 + hextoint(a[2])*0x10 +  hextoint(a[3])) ;
}
Example #16
0
static rgba_group decode_color( struct buffer *s )
{
  static struct svalue _parse_color;
  static struct svalue *parse_color;
  rgba_group res;
  res.alpha = 255;

  if(!s->len)
  {
    res.r=res.g=res.b = 0;
    return res;
  }
  if(s->str[0] == '#' && s->len>3)
  {
    switch(s->len)
    {
     default:
       res.r = hextoint(s->str[1])*0x10;
       res.g = hextoint(s->str[2])*0x10;
       res.b = hextoint(s->str[3])*0x10;
       break;
     case 7:
       res.r = hextoint(s->str[1])*0x10 + hextoint(s->str[2]);
       res.g = hextoint(s->str[3])*0x10 + hextoint(s->str[4]);
       res.b = hextoint(s->str[5])*0x10 + hextoint(s->str[6]);
       break;
     case 13:
       res.r = hextoint(s->str[1])*0x10 + hextoint(s->str[2]);
       res.g = hextoint(s->str[5])*0x10 + hextoint(s->str[6]);
       res.b = hextoint(s->str[9])*0x10 + hextoint(s->str[10]);
       break;
    }
    return res;
  } 
  if(s->len==4&&(!strncmp(s->str,"None",4)||!strncmp(s->str,"none",4)))
  {
#ifdef HIDE_WARNINGS
      res.r = res.g = res.b = 0;
#endif
    res.alpha = 0;
    return res;
  }
  if(!parse_color)
  {
    push_text("Image.Color");
    SAFE_APPLY_MASTER( "resolv_or_error", 1 );
    _parse_color = sp[-1];
    parse_color = &_parse_color;
    sp--;
  }
  push_svalue( parse_color );
  push_string(make_shared_binary_string(s->str,s->len));
  f_index( 2 );
  if(sp[-1].type != T_OBJECT) {
    push_int(0);
    stack_swap();
  } else {
    push_constant_text( "array" );
    apply( sp[-2].u.object, "cast", 1 );
  }
  if(sp[-1].type == T_ARRAY && sp[-1].u.array->size == 3)
  {
    res.r = sp[-1].u.array->item[0].u.integer;
    res.g = sp[-1].u.array->item[1].u.integer;
    res.b = sp[-1].u.array->item[2].u.integer;
  } else {
    res.r = res.g = res.b = 0;
  }
  pop_stack(); /* array */
  pop_stack(); /* object */
  return res;
}
Example #17
0
/*
 * Convert a string containing C character escapes.  Stop at an unescaped
 * space or tab.
 * Copy the converted version to "p", returning its length in *slen.
 * Return updated scan pointer as function result.
 */
static const char * getstr(RMagic *ms, const char *s, char *p, int plen, int *slen, int action) {
	const char *origs = s;
	char *origp = p;
	char *pmax = p + plen - 1;
	int c, val;

	while ((c = *s++) != '\0') {
		if (isspace ((ut8) c))
			break;
		if (p >= pmax) {
			file_error(ms, 0, "string too long: `%s'", origs);
			return NULL;
		}
		if (c == '\\') {
			switch ((c = *s++)) {
			case '\0':
				if (action == FILE_COMPILE)
					file_magwarn(ms, "incomplete escape");
				goto out;
			case '\t':
				if (action == FILE_COMPILE) {
					file_magwarn(ms,
					    "escaped tab found, use \\t instead");
					action++;
				}
				/*FALLTHROUGH*/
			default:
				if (action == FILE_COMPILE) {
					if (isprint((ut8)c))
					    file_magwarn(ms,
						"no need to escape `%c'", c);
					else
					    file_magwarn(ms,
						"unknown escape sequence: \\%03o", c);
				}
				/*FALLTHROUGH*/
			/* space, perhaps force people to use \040? */
			case ' ':
#if 0
			/*
			 * Other things people escape, but shouldn't need to,
			 * so we disallow them
			 */
			case '\'':
			case '"':
			case '?':
#endif
			/* Relations */
			case '>':
			case '<':
			case '&':
			case '^':
			case '=':
			case '!':
			/* and baskslash itself */
			case '\\':
				*p++ = (char) c;
				break;
			case 'a': *p++ = '\a'; break;
			case 'b': *p++ = '\b'; break;
			case 'f': *p++ = '\f'; break;
			case 'n': *p++ = '\n'; break;
			case 'r': *p++ = '\r'; break;
			case 't': *p++ = '\t'; break;
			case 'v': *p++ = '\v'; break;
			/* \ and up to 3 octal digits */
			case '0':
			case '1':
			case '2':
			case '3':
			case '4':
			case '5':
			case '6':
			case '7':
				val = c - '0';
				c = *s++;  /* try for 2 */
				if (c >= '0' && c <= '7') {
					val = (val << 3) | (c - '0');
					c = *s++;  /* try for 3 */
					if (c >= '0' && c <= '7')
						val = (val << 3) | (c-'0');
					else --s;
				} else --s;
				*p++ = (char)val;
				break;

			/* \x and up to 2 hex digits */
			case 'x':
				val = 'x';	/* Default if no digits */
				c = hextoint(*s++);	/* Get next char */
				if (c >= 0) {
					val = c;
					c = hextoint(*s++);
					if (c >= 0)
						val = (val << 4) + c;
					else --s;
				} else --s;
				*p++ = (char)val;
				break;
			}
		} else *p++ = (char)c;
	}
out:
	*p = '\0';
	*slen = p - origp;
	return s;
}
uint16_t MSPBSL_Connection::loadFile(std::string datalocation)
{
	uint16_t retValue = ACK;
	uint32_t i,j,block_start, block_end, block_offset=0, startadress, datasize, datapointer;
	uint8_t lastblock=0;
	std::string ignore = "\b\t\n\r\f\v "; //ignore those characters if they are between the strings. 
	std::string hexchars = "0123456789abcdefABCDEF";
	std::ifstream txt(datalocation.c_str()); 
	std::stringstream s;
	s << txt.rdbuf();
	std::string file = s.str();
	txt.close();
	uint16_t CRC_Return;

	MSPBSL_CRCEngine* Engine = new MSPBSL_CRCEngine("5/6xx");

	
	while(!lastblock)
	{
		//get start and end of current data block
		block_start=file.find("@", block_offset);	
		block_end=file.find_first_of("@q", block_start+1);
		block_offset=block_end;
		uint8_t* data;
		if(file[block_end] == 'q')
		{
			lastblock = 1;
		}

		//get start adress 
		i=file.find_first_of(ignore, block_start)-1;	//find last char of adress
		j=0;
		startadress=0;
		while(i != block_start) //manually calculate adress
		{
			startadress += hextoint(file[i]) * pow(double(16), int(j));
			i--;
			j++;
		}

		//parse data
		i=file.find_first_of(ignore, block_start);//put pointer after adress
		datapointer=0;
		datasize=0;
		data = new uint8_t[( block_end - block_start )];	//should be enough space, too much actually
		while(i < block_end)
		{
			if(hextoint(file[i]) == 0xFF)
			{
				i++;
				continue; //increase pointer if no hex character
			}

			//high nibble
			data[datapointer] = 16 * hextoint(file[i]);
			i++;
			//low nibble
			data[datapointer] += hextoint(file[i]);

			i++;
			datapointer++;
		}

		datasize = datapointer;
		retValue |= RX_DataBlockFast(data, startadress, datasize);
		{
			//CRC Check
			uint16_t retValue1 = retValue;
			retValue |= CRC_Check(&CRC_Return,startadress,datasize);
			retValue &= Engine->verify(data,datasize,CRC_Return);
			if (retValue == retValue1) printf("%s\n","CRC verification block was completed");
			//CRC Check

		}
		delete[] data;

	}//parser mainloop

	delete Engine;
	return retValue;
}
Example #19
0
static int get_name_from_EF_DatiPersonali(unsigned char *EFdata,
	char name[], int name_len)
{
	/*
	 * Bytes 0-5 contain the ASCII encoding of the following TLV
	 * strcture's total size, in base 16.
	 */

	const unsigned int EF_personaldata_maxlen = 400;
	const unsigned int tlv_length_size = 6;
	char *file = (char*)&EFdata[tlv_length_size];
	int file_size = hextoint((char*)EFdata, tlv_length_size);

	enum {
		f_issuer_code = 0,
		f_issuing_date,
		f_expiry_date,
		f_last_name,
		f_first_name,
		f_birth_date,
		f_sex,
		f_height,
		f_codice_fiscale,
		f_citizenship_code,
		f_birth_township_code,
		f_birth_country,
		f_birth_certificate,
		f_residence_township_code,
		f_residence_address,
		f_expat_notes
	};

	/* Read the fields up to f_first_name */
	struct {
		int len;
		char value[256];
	} fields[f_first_name+1];
	int i=0; /* offset inside the file */
	int f; /* field number */

	if(file_size < 0)
		return -1;

	/*
	 * This shouldn't happen, but let us be protected against wrong
	 * or malicious cards
	 */
	if(file_size > (int)EF_personaldata_maxlen - (int)tlv_length_size)
		file_size = EF_personaldata_maxlen - tlv_length_size;


	memset(fields, 0, sizeof(fields));

	for(f=0; f<f_first_name+1; f++) {
		int field_size;
		/* Don't read beyond the allocated buffer */
		if(i > file_size)
			return -1;

		field_size = hextoint((char*) &file[i], 2);
		if((field_size < 0) || (field_size+i > file_size))
			return -1;

		i += 2;

		if(field_size >= (int)sizeof(fields[f].value))
			return -1;

		fields[f].len = field_size;
		strncpy(fields[f].value, &file[i], field_size);
		fields[f].value[field_size] = '\0';
		i += field_size;
	}

	if (fields[f_first_name].len + fields[f_last_name].len + 1 >= name_len)
		return -1;

	snprintf(name, name_len, "%s %s",
		fields[f_first_name].value, fields[f_last_name].value);
	return 0;
}
Example #20
0
static size_t pull_charset_flags (charset_t from_set, charset_t to_set, charset_t cap_set, const char *src, size_t srclen, char* dest, size_t destlen, uint16_t *flags)
{
    const uint16_t option = (flags ? *flags : 0);
    size_t i_len, o_len;
    size_t j = 0;
    const char* inbuf = (const char*)src;
    char* outbuf = dest;
    atalk_iconv_t descriptor;
    atalk_iconv_t descriptor_cap;
    char escch;                 /* 150210: uninitialized OK, depends on j */

    if (srclen == (size_t)-1)
        srclen = strlen(src) + 1;

    descriptor = conv_handles[from_set][CH_UCS2];
    descriptor_cap = conv_handles[cap_set][CH_UCS2];

    if (descriptor == (atalk_iconv_t)-1 || descriptor == (atalk_iconv_t)0) {
        errno = EINVAL;
        return (size_t)-1;
    }

    i_len=srclen;
    o_len=destlen;

    if ((option & CONV_ESCAPEDOTS) && i_len >= 2 && inbuf[0] == '.') {
        if (o_len < 6) {
            errno = E2BIG;
            goto end;
        }
        ucs2_t ucs2 = ':';
        memcpy(outbuf, &ucs2, sizeof(ucs2_t));
        ucs2 = '2';
        memcpy(outbuf + sizeof(ucs2_t), &ucs2, sizeof(ucs2_t));
        ucs2 = 'e';
        memcpy(outbuf + 2 * sizeof(ucs2_t), &ucs2, sizeof(ucs2_t));
        outbuf += 6;
        o_len -= 6;
        inbuf++;
        i_len--;
        *flags |= CONV_REQESCAPE;
    }

    while (i_len > 0) {
        for (j = 0; j < i_len; ++j)
            if (inbuf[j] == ':' || inbuf[j] == '/') {
                escch = inbuf[j];
                break;
            }
        j = i_len - j;
        i_len -= j;

        if (i_len > 0 &&
            atalk_iconv(descriptor, &inbuf, &i_len, &outbuf, &o_len) == (size_t)-1) {
            if (errno == EILSEQ || errno == EINVAL) {
                errno = EILSEQ;
                if ((option & CONV_IGNORE)) {
                    *flags |= CONV_REQMANGLE;
                    return destlen - o_len;
                }
                if ((option & CONV__EILSEQ)) {
                    if (o_len < 2) {
                        errno = E2BIG;
                        goto end;
                    }
                    *((ucs2_t *)outbuf) = (ucs2_t) IGNORE_CHAR; /**inbuf */
                    inbuf++;
                    i_len--;
                    outbuf += 2;
                    o_len -= 2;
                    /* FIXME reset stat ? */
                    continue;
                }
            }
            goto end;
        }

        if (j) {
            /* we have a ':' or '/' */
            i_len = j, j = 0;

            if (escch == ':') {
                if ((option & CONV_UNESCAPEHEX)) {
                    /* treat it as a CAP hex encoded char */
                    char h[MAXPATHLEN];
                    size_t hlen = 0;

                    while (i_len >= 3 && inbuf[0] == ':' &&
                           isxdigit(inbuf[1]) && isxdigit(inbuf[2])) {
                        h[hlen++] = (hextoint(inbuf[1]) << 4) | hextoint(inbuf[2]);
                        inbuf += 3;
                        i_len -= 3;
                    }
                    if (hlen) {
                        const char *h_buf = h;
                        if (atalk_iconv(descriptor_cap, &h_buf, &hlen, &outbuf, &o_len) == (size_t)-1) {
                            i_len += hlen * 3;
                            inbuf -= hlen * 3;
                            if (errno == EILSEQ && (option & CONV_IGNORE)) {
                                *flags |= CONV_REQMANGLE;
                                return destlen - o_len;
                            }
                            goto end;
                        }
                    } else {
                        /* We have an invalid :xx sequence */
                        errno = EILSEQ;
                        if ((option & CONV_IGNORE)) {
                            *flags |= CONV_REQMANGLE;
                            return destlen - o_len;
                        }
                        goto end;
                    }
                } else if (option & CONV_ESCAPEHEX) {
                    if (o_len < 6) {
                        errno = E2BIG;
                        goto end;
                    }
                    ucs2_t ucs2 = ':';
                    memcpy(outbuf, &ucs2, sizeof(ucs2_t));
                    ucs2 = '3';
                    memcpy(outbuf + sizeof(ucs2_t), &ucs2, sizeof(ucs2_t));
                    ucs2 = 'a';
                    memcpy(outbuf + 2 * sizeof(ucs2_t), &ucs2, sizeof(ucs2_t));
                    outbuf += 6;
                    o_len -= 6;
                    inbuf++;
                    i_len--;
                } else if (to_set == CH_UTF8_MAC || to_set == CH_MAC) {
                    /* convert to a '/' */
                    ucs2_t slash = 0x002f;
                    memcpy(outbuf, &slash, sizeof(ucs2_t));
                    outbuf += 2;
                    o_len -= 2;
                    inbuf++;
                    i_len--;
                } else {
                    /* keep as ':' */
                    ucs2_t ucs2 = 0x003a;
                    memcpy(outbuf, &ucs2, sizeof(ucs2_t));
                    outbuf += 2;
                    o_len -= 2;
                    inbuf++;
                    i_len--;
                }
            } else {
                /* '/' */
                if (option & CONV_ESCAPEHEX) {
                    if (o_len < 6) {
                        errno = E2BIG;
                        goto end;
                    }
                    ucs2_t ucs2 = ':';
                    memcpy(outbuf, &ucs2, sizeof(ucs2_t));
                    ucs2 = '2';
                    memcpy(outbuf + sizeof(ucs2_t), &ucs2, sizeof(ucs2_t));
                    ucs2 = 'f';
                    memcpy(outbuf + 2 * sizeof(ucs2_t), &ucs2, sizeof(ucs2_t));
                    outbuf += 6;
                    o_len -= 6;
                    inbuf++;
                    i_len--;
                } else if ((from_set == CH_UTF8_MAC || from_set == CH_MAC)
                           && (to_set != CH_UTF8_MAC  || to_set != CH_MAC)) {
                    /* convert to ':' */
                    ucs2_t ucs2 = 0x003a;
                    memcpy(outbuf, &ucs2, sizeof(ucs2_t));
                    outbuf += 2;
                    o_len -= 2;
                    inbuf++;
                    i_len--;
                } else {
                    /* keep as '/' */
                    ucs2_t ucs2 = 0x002f;
                    memcpy(outbuf, &ucs2, sizeof(ucs2_t));
                    outbuf += 2;
                    o_len -= 2;
                    inbuf++;
                    i_len--;
                }
            }
        }
    }
end:
    return (i_len + j == 0 || (option & CONV_FORCE)) ? destlen - o_len : (size_t)-1;
}
Example #21
0
/* -------------------------------------------------------
*/
static char *
private_demangle(const struct vol *vol, char *mfilename, cnid_t did, cnid_t *osx) 
{
    char *t;
    char *u_name;
    uint32_t id, file_id;
    static char buffer[12 + MAXPATHLEN + 1];
    int len = 12 + MAXPATHLEN + 1;
    struct dir	*dir;
    size_t prefix;

    id = file_id = 0;

    t = strchr(mfilename, MANGLE_CHAR);
    if (t == NULL) {
        return mfilename;
    }
    prefix = t - mfilename;
    /* FIXME 
     * is prefix == 0 a valid mangled filename ?
    */
    /* may be a mangled filename */
    t++;
    if (*t == '0') { /* can't start with a 0 */
        return mfilename;
    }
    while(isuxdigit(*t)) {
        id = (id *16) + hextoint(*t);
        t++;
    }
    if ((*t != 0 && *t != '.') || strlen(t) > MAX_EXT_LENGTH || id < 17) {
        return mfilename;
    }

    file_id = id = htonl(id);
    if (osx) {
        *osx = id;
    }

    /* is it a dir?, there's a conflict with pre OSX 'trash #2'  */
    if ((dir = dirlookup(vol, id))) {
        if (dir->d_pdid != did) {
            /* not in the same folder, there's a race with outdate cache
             * but we have to live with it, hopefully client will recover
            */
            return mfilename;
        }
        if (!osx) {
            /* it's not from cname so mfilename and dir must be the same */
            if (strcmp(cfrombstr(dir->d_m_name), mfilename) == 0) {
                return cfrombstr(dir->d_u_name);
            }
        } else {
            return demangle_checks(vol, cfrombstr(dir->d_u_name), mfilename, prefix, t);
        }
    }
    else if (NULL != (u_name = cnid_resolve(vol->v_cdb, &id, buffer, len)) ) {
        if (id != did) {
            return mfilename;
        }
        if (!osx) {
            /* convert back to mac name and check it's the same */
            t = utompath(vol, u_name, file_id, utf8_encoding(vol->v_obj));
            if (!strcmp(t, mfilename)) {
                return u_name;
            }
        }
        else {
            return demangle_checks (vol, u_name, mfilename, prefix, t);
        }
    }

    return mfilename;
}
Example #22
0
int is_valid_command_string(char *cmd) {
  //i know this could be better, but it works and i can't be bothered.
  int i=0; command_count=0;
  bool next_zone, next_action;
  next_zone=true;
  while (next_zone) {
    if (cmd[i]=='Z'|cmd[i]=='z') { i++;
      if (hextoint(cmd[i])>=0) i++; else return i+1;
      if (hextoint(cmd[i])>=0) i++; else return i+1;
      if ((hextoint(cmd[i-2])*10)+hextoint(cmd[i-1])>27) return i-1;
      next_action=true;
      while (next_action) {
        if (cmd[i]=='B'|cmd[i]=='b'|cmd[i]=='F'|cmd[i]=='f') { i++;
          if (hextoint(cmd[i])>=0) i++; else return i+1;
          if (hextoint(cmd[i])>=0) i++; else return i+1;
          if (hextoint(cmd[i])>=0) i++; else return i+1;
          command_count++;
        } else //if B or F
        if (cmd[i]=='M'|cmd[i]=='m') { i++;
          if (hextoint(cmd[i])>=0) i++; else return i+1;
          if (hextoint(cmd[i])>=0) i++; else return i+1;
          if (hextoint(cmd[i])>=0) i++; else return i+1;
          if (hextoint(cmd[i])>=0) i++; else return i+1;
          if (hextoint(cmd[i])>=0) i++; else return i+1;
          if (hextoint(cmd[i])>=0) i++; else return i+1;
          command_count++;
        } else //if M
          if (cmd[i]=='Z'|cmd[i]=='z') next_action=false;
        else
          if (strlen(cmd)==i) return 0; else return i+1;
      } //while actions
    } else { if (i==0) return 1; else next_zone=false; } //if Z
  } //while zones
  return 0; //we made it passed the checks
}
Example #23
0
LRESULT CALLBACK HexviewProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
    static unsigned char **pBuffer = NULL ;
    unsigned char	ascii, *bin_data;
    static int  cxChar, cxCaps, cxClient, cyClient,
           iVscrollPos, iVscrollMax,
           cyChar, xCaret = 6, yCaret;
    HDC         hdc ;
    int			y, i, nResult, iVscrollInc;
    static int	iPaintBeg, iPaintEnd;
    char		szFilename[MAX_PATH], szBuffer[256];

    RECT			rect = {50, 50, 620, 300};
    PAINTSTRUCT		ps;
    TEXTMETRIC		tm;
    static HFONT	hFont;
    HDROP			hDrop;


    switch (iMsg)
    {
    //case WM_INITDIALOG:
    case WM_CREATE :
        hdc = GetDC (hwnd) ;
        hFont = CreateFont(12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, FIXED_PITCH | FF_DONTCARE, "Courier");
        SelectObject (hdc, hFont) ;
        GetTextMetrics (hdc, &tm) ;
        cxChar = tm.tmAveCharWidth ;
        cyChar = tm.tmHeight ;
        ReleaseDC (hwnd, hdc) ;

        pBuffer = CreateBuffer(64);
        return 0 ;


    case WM_SIZE :
        cxClient = LOWORD (lParam) ;
        cyClient = HIWORD (lParam) ;

        iVscrollMax = max (0, rows + 2 - cyClient / cyChar) ;
        iVscrollPos = min (iVscrollPos, iVscrollMax) ;

        SetScrollRange (hwnd, SB_VERT, 0, iVscrollMax, FALSE) ;
        SetScrollPos   (hwnd, SB_VERT, iVscrollPos, TRUE) ;

        return 0;


    case WM_VSCROLL:
        switch (LOWORD(wParam))
        {
        case SB_TOP:
            iVscrollInc = -iVscrollPos;
            break;

        case SB_BOTTOM:
            iVscrollInc = iVscrollMax = iVscrollPos;
            break;

        case SB_LINEUP:
            iVscrollInc = -1;
            break;

        case SB_LINEDOWN:
            iVscrollInc = 1;
            break;

        case SB_PAGEUP:
            iVscrollInc = min(-1, -cyClient / cyChar);
            break;

        case SB_PAGEDOWN:
            iVscrollInc = max(1, cyClient / cyChar);
            break;

        case SB_THUMBTRACK:
            iVscrollInc = HIWORD(wParam) - iVscrollPos;
            break;

        default:
            iVscrollInc = 0;
        }

        iVscrollInc = max (-iVscrollPos,
                           min (iVscrollInc, iVscrollMax - iVscrollPos));

        if (iVscrollInc != 0)
        {
            iVscrollPos += iVscrollInc;
            ScrollWindow(hwnd, 0, -cyChar * iVscrollInc, NULL, NULL);
            SetScrollPos(hwnd, SB_VERT, iVscrollPos, TRUE);

            xCaret = 6;
            yCaret = 0;
            SetCaretPos (xCaret * cxChar, 0) ;
            //UpdateWindow(hwnd);
            InvalidateRect(hwnd, NULL, TRUE);
        }

        return 0;


    case WM_SETFOCUS :
        // create and show the caret

        CreateCaret (hwnd, NULL, cxChar, cyChar) ;
        SetCaretPos (xCaret * cxChar, yCaret * cyChar) ;
        ShowCaret (hwnd) ;
        return 0 ;

    case WM_KILLFOCUS :
        // hide and destroy the caret
        HideCaret (hwnd) ;
        DestroyCaret () ;
        return 0 ;

    case WM_KEYDOWN :
        switch (wParam)
        {
        case VK_HOME :
            xCaret = 6 ;
            break ;

        case VK_PRIOR :
            yCaret = 0 ;
            break ;

        case VK_LEFT :
            xCaret = max (xCaret - 1, 6);
            if (pBuffer[yCaret][xCaret] == ' ')
                xCaret = max (xCaret - 1, 6);
            break ;

        case VK_RIGHT :
            ++xCaret;

            if (lastrow && (yCaret + iPaintBeg == rows-1))
                if (xCaret > (4 + lastrow*3))
                {
                    --xCaret;
                    break;
                }

            if (xCaret > 52)
            {
                xCaret = 6;
                ++yCaret;
                if (yCaret + iPaintBeg> rows - 1)
                {
                    --yCaret;
                    xCaret = 52;
                }
            }
            if ((xCaret % 3) == 2)
                ++xCaret;
            break ;

        case VK_UP :
            yCaret = max (yCaret - 1, 0) ;
            break ;

        case VK_DOWN :
            if (lastrow && (xCaret > (4 + lastrow*3)))
                yCaret = min(yCaret + 1, rows - iPaintBeg - 2);
            else
                yCaret = min(yCaret + 1, rows - iPaintBeg - 1);
            break ;
        }

        SetCaretPos (xCaret * cxChar, yCaret * cyChar) ;
        return 0 ;

    case WM_CHAR :
        for (i = 0 ; i < (int) LOWORD (lParam) ; i++)
        {
            if (!((wParam >= '0' && wParam <='9') || (wParam >= 'a' && wParam <= 'f')))
                return 0;


            HideCaret (hwnd) ;
            hdc = GetDC (hwnd) ;

            SelectObject (hdc, hFont);

            pBuffer[yCaret + iPaintBeg][xCaret] = wParam;
            TextOut(hdc, xCaret * cxChar, (yCaret) * cyChar, &pBuffer[yCaret + iPaintBeg][xCaret], 1);

            if (pBuffer[yCaret + iPaintBeg][xCaret-1] == ' ')
                ascii = hextoint(pBuffer[yCaret + iPaintBeg][xCaret], pBuffer[yCaret + iPaintBeg][xCaret + 1]);
            else
                ascii = hextoint(pBuffer[yCaret + iPaintBeg][xCaret - 1], pBuffer[yCaret + iPaintBeg][xCaret]);

            if (ascii > 33 && ascii < 125)
            {
                pBuffer[yCaret + iPaintBeg][(xCaret/3) + 54] = ascii;
                TextOut(hdc, ((xCaret/3) + 54)* cxChar, yCaret * cyChar, &pBuffer[yCaret + iPaintBeg][(xCaret/3) + 54], 1);
            }
            else
            {
                pBuffer[yCaret + iPaintBeg][(xCaret/3) + 54] = '.';
                TextOut(hdc, ((xCaret/3) + 54)* cxChar, yCaret * cyChar, &pBuffer[yCaret + iPaintBeg][(xCaret/3) + 54], 1);
            }

            ShowCaret (hwnd) ;
            ReleaseDC (hwnd, hdc) ;

            SendMessage(hwnd, WM_KEYDOWN, VK_RIGHT, 0);
            break ;
        }
        return 0 ;


    case WM_DROPFILES:
        hDrop = (HANDLE) wParam;
        DragQueryFile(hDrop, 0, szFilename, sizeof(szFilename));
        pBuffer = LoadPacket(szFilename);
        GetWindowRect(hwnd, &rect);
        SendMessage(hwnd, WM_SIZE, 0, MAKEWPARAM(rect.bottom - rect.top, rect.right - rect.left));
        InvalidateRect(hwnd, NULL, TRUE);
        return 0;


    case WM_PAINT :
        hdc = BeginPaint (hwnd, &ps) ;
        SelectObject (hdc, hFont) ;

        iPaintBeg = max (0, iVscrollPos + ps.rcPaint.top / cyChar );
        iPaintEnd = min (rows,
                         iVscrollPos + ps.rcPaint.bottom / cyChar);

        i =0;
        for (y = iPaintBeg; y < iPaintEnd; y++, i++)
            TextOut(hdc, 0, i * cyChar, pBuffer[y], 72);

        EndPaint (hwnd, &ps) ;
        return 0 ;

    case WM_DESTROY :
        DeleteBuffer(pBuffer);
        DeleteObject(hFont);
        return 0 ;
    }
    return DefWindowProc (hwnd, iMsg, wParam, lParam) ;
}
Example #24
0
/*
 * Convert a string containing C character escapes.  Stop at an unescaped
 * space or tab.
 * Copy the converted version to "p", returning its length in *slen.
 * Return updated scan pointer as function result.
 */
static char *
getstr(char *s, char *p, int plen, int *slen)
{
	char	*origs = s, *origp = p;
	char	*pmax = p + plen - 1;
	int	c;
	int	val;
	
	while ((c = *s++) != '\0') {
		if (isspace((u_char) c))
			break;
		if (p >= pmax) {
			warnx("getstr: string too long: %s", origs);
			break;
		}
		if (c == '\\') {
			switch ((c = *s++)) {
			case '\0':
				goto out;
			default:
				*p++ = (char) c;
				break;
			case 'n':
				*p++ = '\n';
				break;
			case 'r':
				*p++ = '\r';
				break;
			case 'b':
				*p++ = '\b';
				break;
			case 't':
				*p++ = '\t';
				break;
			case 'f':
				*p++ = '\f';
				break;
			case 'v':
				*p++ = '\v';
				break;
			/* \ and up to 3 octal digits */
			case '0':
			case '1':
			case '2':
			case '3':
			case '4':
			case '5':
			case '6':
			case '7':
				val = c - '0';
				c = *s++;  /* try for 2 */
				if (c >= '0' && c <= '7') {
					val = (val << 3) | (c - '0');
					c = *s++;  /* try for 3 */
					if (c >= '0' && c <= '7')
						val = (val << 3) | (c - '0');
					else --s;
				}
				else --s;
				*p++ = (char) val;
				break;

			/* \x and up to 2 hex digits */
			case 'x':
				val = 'x';	/* Default if no digits */
				c = hextoint(*s++);	/* Get next char */
				if (c >= 0) {
					val = c;
					c = hextoint(*s++);
					if (c >= 0) val = (val << 4) + c;
					else --s;
				}
				else --s;
				*p++ = (char) val;
				break;
			}
		}
		else *p++ = (char) c;
	}
 out:
	*p = '\0';
	*slen = p - origp;
	
	return (s);
}
Example #25
0
void test_IllegalChar(void) {
        const char *str = "5gb"; // Illegal character g
        u_long actual;

        TEST_ASSERT_FALSE(hextoint(str, &actual));
}
Example #26
0
bool afx_process(char *cmd) {
//Command begins====================================================================
  int index = 1, ref=0, zone, z1,z2,z3,r1,g1,b1,r2,g2,b2;
  if (debug) printf("Debug: Begin lightchip command sequence\n");
  while (!(cmd[ref]==0)) { //Powermode begins=======================================
    if (debug&saving) printf("Debug:   PowerBlock: %i\n",power_block);
    if (debug&power_block==0&saving) printf("Debug:   Warning: saving to EEPROM may not be available\n");
    if (debug) printf("Debug:   Reset All Lights On\n");
    afx_cmd(COMMAND_RESET,RESET_ALL_LIGHTS_ON,0,0,0,0,0,0);
    if (speed > 0) {
      if (debug) printf("Debug:   Setting speed to %i of 10\n", speed);
      afx_spd(speed);
    }
    while (!(cmd[ref+1]==0)) { //Zone begins======================================
      if (cmd[ref]=='Z'|cmd[ref]=='z') { ref++;
        zone = (10*hextoint(cmd[ref]))+hextoint(cmd[ref+1]); ref+=2;
        if (debug) {
          if (zone==0) printf("Debug:      LightZone(All)\n");
          else printf("Debug:      LightZone(%i)\n",zone);
        }
        while (!(cmd[ref+1]==0)) { //Colour begins================================
          if (cmd[ref]=='M'|cmd[ref]=='m') { ref++;
            r1 = hextoint(cmd[ref++]); g1 = hextoint(cmd[ref++]); b1 = hextoint(cmd[ref++]);
            r2 = hextoint(cmd[ref++]); g2 = hextoint(cmd[ref++]); b2 = hextoint(cmd[ref++]);
            if (saving&debug) printf("Debug:         Save Powermode(%i)\n",power_block);
            if (saving) afx_cmd(COMMAND_SAVE_NEXT,power_block,0,0,0,0,0,0);
            if (debug) printf("Debug:         Set Colour[%i]: Morph %i %i %i into %i %i %i\n",index,r1,g1,b1,r2,g2,b2);
            afx_set(COMMAND_SET_COLOUR_MORPH,command_count,zone,r1,g1,b1,r2,g2,b2,0);
            index++;
          }
          if (cmd[ref]=='B'|cmd[ref]=='b') { ref++;
            r1 = hextoint(cmd[ref++]); g1 = hextoint(cmd[ref++]); b1 = hextoint(cmd[ref++]);
            if (saving&debug) printf("Debug:         Save Powermode(%i)\n",power_block);
            if (saving) afx_cmd(COMMAND_SAVE_NEXT,power_block,0,0,0,0,0,0);
            if (debug) printf("Debug:         Set Colour[%i]: Blink %i %i %i\n",index,r1,g1,b1);
            afx_set(COMMAND_SET_COLOUR_BLINK,command_count,zone,r1,g1,b1,255,255,255,0);
            index++;
          }
          if (cmd[ref]=='F'|cmd[ref]=='f') { ref++;
            r1 = hextoint(cmd[ref++]); g1 = hextoint(cmd[ref++]); b1 = hextoint(cmd[ref++]);
            if (saving&debug) printf("Debug:         Save Powermode(%i)\n",power_block);
            if (saving) afx_cmd(COMMAND_SAVE_NEXT,power_block,0,0,0,0,0,0);
            if (debug) printf("Debug:         Set Colour[%i]: Fixed %i %i %i\n",index,r1,g1,b1);
            afx_set(COMMAND_SET_COLOUR_FIXED,command_count,zone,r1,g1,b1,0,0,0,0);
            index++;
          }
          if (cmd[ref]=='Z'|cmd[ref]=='z'|cmd[ref]=='P'|cmd[ref]=='p') break; //else ref++;
        } //Colour ends===========================================================
        if (saving&debug) printf("Debug:      Save Powermode(%i)\n",power_block);
        if (saving) afx_cmd(COMMAND_SAVE_NEXT,power_block,0,0,0,0,0,0);
        if (debug) printf("Debug:      End Loop Block\n");
        afx_cmd(COMMAND_LOOP_BLOCK_END, 0, 0, 0, 0, 0, 0,wait);
      } else break;
    } //Zone ends=================================================================
//    if (saving&debug) printf("Debug:   Save Powermode(%i)\n",power_block);
//    if (saving) afx_cmd(COMMAND_SAVE_NEXT,power_block,0,0,0,0,0,0);
  } //Powermode ends================================================================
  if (debug) printf("Debug: Execute Instruction\n");
  afx_cmd(COMMAND_TRANSMIT_EXECUTE,0,0,0,0,0,0,wait);
//Command end=======================================================================
  if (debug) printf("Debug: End lightchip command sequence\n");
}