Ejemplo n.º 1
0
int HttpUtil::unescape_n(const char *pSrc, char *pDest, int n)
{
    register char c;
    register char * p = pDest;

    while (n-- && ((c = *pSrc++) != 0))
    {
        switch(c)
        {
        case '%':
        {
            register char x1, x2;
            x1 = *pSrc++;
            if (!isxdigit(x1))
                    return -1;
            x2 = *pSrc++;
            if (!isxdigit(x2))
                    return -1;
            *p++ = (hexdigit(x1) << 4) + hexdigit(x2);
            n -= 2;
            break;
        }
        case '/':
            //get rid of duplicate '/'s.
            if ( *pSrc == '/' )
                break;
        default:
            *p++ = c;
        }
    }
    *p = 0;
    return p - pDest;
}
Ejemplo n.º 2
0
static u_int hex2byte(netdissect_options *ndo, char *hexstring)
{
	u_int byte;

	byte = (hexdigit(ndo, hexstring[0]) << 4) + hexdigit(ndo, hexstring[1]);
	return byte;
}
Ejemplo n.º 3
0
/* Do not free result, it's a static buffer */
static const char*
udev_decode_string (const char* encoded)
{
  int len;
  const char* s;
  static char decoded[4096];

  if (encoded == NULL)
    return NULL;

  for (len = 0, s = encoded; *s && len < sizeof (decoded) - 1; ++len, ++s) {
    /* need to check for NUL terminator in advance */
    if (s[0] == '\\' && s[1] == 'x' && s[2] >= '0' && s[3] >= '0') {
      decoded[len] = (hexdigit (s[2]) << 4) | hexdigit (s[3]);
      s += 3;
    } else if (s[0] == '_' || s[0] == '-') {
      decoded[len] = ' ';
    } else {
      decoded[len] = *s;
    }
  }
  decoded[len] = '\0';

  return decoded;
}
Ejemplo n.º 4
0
Archivo: mlpx.c Proyecto: bytemine/ut
/*
 *	mux()
 *
 *	construct and add prefix, forward to main out
 */
void mux (msg_t *m, const chn_t *ch)
{
char tc;

#ifdef DEBUG
    if (! (m->flags & MF_PLAIN)) {
	/* cannot have prefix yet */
        tesc_emerg (CHN_MSG, MF_ERR, "mux(): cannot have prefix yet!\n");
	tesc_emerg (CHN_MSG, MF_EOF, "\n");
	exit (1);
    }
    if (ch->id < 0 || ch->id > CHN_MAX) {
	/* illegal id */
        tesc_emerg (CHN_MSG, MF_ERR, "mux(): illegal id\n");
	tesc_emerg (CHN_MSG, MF_EOF, "\n");
	exit (1);
    }
#endif

    /* 'type' of prefix */
    tc = mlpx_prfxtc (m->flags);

    /* fill in prefix */
    if (! chmap[ch->id]) {
	/* no such channel */
        tesc_emerg (CHN_MSG, MF_ERR, "mux(): no such channel\n");
	tesc_emerg (CHN_MSG, MF_EOF, "\n");
	exit (1);
    }
#ifdef DEBUG
    if (PRFXLEN != 5) {
	/* huh! prefix length does not match the following part */
        tesc_emerg (CHN_MSG, MF_ERR, "mux(): internal error\n");
	tesc_emerg (CHN_MSG, MF_EOF, "\n");
	exit (1);
    }
#endif
    m->prefix[0] = tc;
    m->prefix[1] = hexdigit (ch->id / 16);
    m->prefix[2] = hexdigit (ch->id % 16);
    m->prefix[3] = tc;
    m->prefix[4] = ' ';

    m->flags &= ~MF_PLAIN;
    m->len += PRFXLEN;

    /* and off to main out */
    if (tesc_enq_wq (&ch_main_out, m)) {
	/* failed */
        tesc_emerg (CHN_MSG, MF_ERR, "mux(): test_enq_wq() failed\n");
	tesc_emerg (CHN_MSG, MF_EOF, "\n");
	exit (1);
    }

    return;
}
static Int readhex ( const Char* buf, UWord* val )
{
   Int n = 0;
   *val = 0;
   while (hexdigit(*buf) >= 0) {
      *val = (*val << 4) + hexdigit(*buf);
      n++; buf++;
   }
   return n;
}
Ejemplo n.º 6
0
char *hex16str(tt_u16 v)
{
  static char tmpbuf[5] = "    ";

  tmpbuf[3] = hexdigit(v);
  v >>= 4;
  tmpbuf[2] = hexdigit(v);
  v >>= 4;
  tmpbuf[1] = hexdigit(v);
  v >>= 4;
  tmpbuf[0] = hexdigit(v);

  return tmpbuf;
}
Ejemplo n.º 7
0
static int
read_fragment(uint8_t *frag, size_t fraglen)
{
	size_t	i = 0, nib = 0;
	char	ch;
	char	prev;

	printf("\nF> ");
	memset(frag, 0, fragmax+1);
	while (0x20 == (ch = fgetc(stdin))) printf("%c", ch);

	while (((0x20 == ch) || hexdigit(ch)) && (i < fraglen)) {
		printf("%c", ch);
		if (0x20 != ch) {
			if (nib) {
				frag[i++] = (prev<<4)+gethexdigit(ch);
				nib = 0;
			} else {
				nib++;
				prev = gethexdigit(ch);
			}
		}
		ch = fgetc(stdin);
	}

	if (i != fraglen && ((0x20 != ch) && (0x0a != ch) && (0x0d != ch))) {
		memset(frag, 0, fragmax+1);
		printf("%c\n", ch);
		return -1;
	}


	printf("%c", ch);
	return 0;
}
Ejemplo n.º 8
0
TCHAR* YMSPlugin::MakeFileName(LPCTSTR DestPath, LPCTSTR fname,int)
{
    TCHAR* fmt;
    switch(DestPath[_tcslen(DestPath)-1])
    {
        case '\\': case'/': case':':
            fmt = _T("%s%s%s"); break;
        default: fmt = _T("%s\\%s%s");
    }

    TCHAR name[MAX_PATH];
    bool bTwoDots = false;
    if(!_tcscmp(fname, _T("..")))
    {
        // make name out of parent name
        TCHAR* p = _tcsrchr(CurDir, '\\');
        if(!p) p = CurDir;
        if(!*p) p = _T("(root)");
        fname = p;
        bTwoDots = true;
    }
    bool bAllDots = true;
    TCHAR* maxName = name + MAX_PATH-1 - _tcslen(GetFileExt()) - _tcslen(DestPath)-2;
    _TUCHAR* q = (_TUCHAR*)name;
    for(_TUCHAR*p = (_TUCHAR*)fname; *p; p++)
    {
        if(_tcschr(invalid_chars, *p) || *p<' ')
	{
            *q++ = '#'; *q++ = hexdigit(*p>>4); *q++ = hexdigit(*p&15);
        }
        else
Ejemplo n.º 9
0
static int
readnum(size_t *n)
{
	char	buf[9];
	int	i = 0;
	char	ch;

	memset(buf, 0, 9);
	while (0x20 == (ch = fgetc(stdin))) printf("%c", ch);

	while (hexdigit(ch) && (i <= 8)) {
		printf("%c", ch);
		buf[i++] = ch;
		ch = fgetc(stdin);
	}

	if ((0x20 != ch) && (0x0a != ch) && (0x0d != ch)) {
		printf("%x\n", ch);
		return -1;
	}
	printf("%c", ch);

	*n = strtoul(buf, NULL, 0x10);
	return 0;
}
Ejemplo n.º 10
0
const char *GetTwoHexDigits(const char *c, unsigned char *value)
{
    int digit1=-1, digit2=-1;
    while(digit1<0)
    {
        if(*c==0) return 0;
        digit1=hexdigit(*c++);
    }
    while(digit2<0)
    {
        if(*c==0) return 0;
        digit2=hexdigit(*c++);
    }
    *value=(unsigned char)((digit1<<4)|digit2);
    return c;
}
Ejemplo n.º 11
0
int hex2int(const char* sz, int n, int* sum) 
{
    int v = 0, b;
    while (n--)
    {
        if (*sz == 0)
            return v;
		b = hexdigit(*sz++) << 4;
        if (*sz == 0)
            return v;
        b += hexdigit(*sz++);
        if (sum)
            *sum += b;
        v = (v << 8) + b;
    }
    return v;
}
Ejemplo n.º 12
0
int main(int argc, char* argv[])
{
  const char* hexstr = "0123456789ABCDEF";
  char* name = "";
  int count = 0;
  char buffer[MAX+1];
  if(argc > 1)
    name = argv[1];
#define hexdigit(c) (strchr(hexstr, (c))-hexstr)
  while(fgets(buffer, MAX, stdin) != NULL) {
    if(buffer[1] == '0' || buffer[1] == '1') {
      count += hexdigit(buffer[2])*16 + hexdigit(buffer[3]) - 3;
    }
  }
  printf("%-24s%-d\n", name, count);
  return 0;
}
Ejemplo n.º 13
0
char *hex8str(tt_u8 v)
{
  static char tmpbuf[3] = "  ";

  tmpbuf[1] = hexdigit(v);
  tmpbuf[0] = hexdigit(v >> 4);

  return tmpbuf;
}
Ejemplo n.º 14
0
struct emu_value *emu_value_new(char *buf)
{
	size_t i, len;
	struct emu_value *v;

	len = strlen(buf) / 2;

	v = malloc(sizeof(*v) + len);
	v->len = len;
	v->next = NULL;

	for (i = 0; i < len; i++)
		v->value[i] = (hexdigit(buf[2*i]) << 4) | hexdigit(buf[2*i+1]);

	free(buf);

	return v;
}
Ejemplo n.º 15
0
/**
	@brief Translate the next four characters into a UTF-8 character.
	@param parser Pointer to a Parser.
	@param unibuff Pointer to a small buffer in which to return the results.
	@return 0 if successful, or 1 if not.

	Collect the next four characters into @a unibuff, and make sure that they're all hex.
	Translate them into a nul-terminated UTF-8 byte sequence, and return the result via
	@a unibuff.

	(Note that a UTF-8 byte sequence is guaranteed not to contain a nul byte.  Hence using
	a nul as a terminator creates no ambiguity.)
*/
static int get_utf8( Parser* parser, Unibuff* unibuff ) {
	char ubuff[ 5 ];
	int i = 0;

	// Accumulate four characters into a buffer.  Make sure that
	// there are four of them, and that they're all hex.
	for( i = 0; i < 4; ++i ) {
		int c = parser_nextc( parser );
		if( !c ) {
			report_error( parser, 'u', "Incomplete Unicode sequence" );
			unibuff->buff[ 0 ] = '\0';
			return 1;
		} else if( ! isxdigit( (unsigned char) c ) ) {
			report_error( parser, c, "Non-hex byte found in Unicode sequence" );
			unibuff->buff[ 0 ] = '\0';
			return 1;
		}
		else
			ubuff[ i ] = c;
	}

	/* The following code is adapted with permission from
	 * json-c http://oss.metaparadigm.com/json-c/
	 */

	// Convert the hex sequence to a single integer
	unsigned int ucs_char =
			(hexdigit(ubuff[ 0 ]) << 12) +
			(hexdigit(ubuff[ 1 ]) <<  8) +
			(hexdigit(ubuff[ 2 ]) <<  4) +
			 hexdigit(ubuff[ 3 ]);

	unsigned char* utf_out = unibuff->buff;

	if (ucs_char < 0x80) {
		utf_out[0] = ucs_char;
		utf_out[1] = '\0';

	} else if (ucs_char < 0x800) {
		utf_out[0] = 0xc0 | (ucs_char >> 6);
		utf_out[1] = 0x80 | (ucs_char & 0x3f);
		utf_out[2] = '\0';

	} else {
static Int readdec ( const Char* buf, UInt* val )
{
   Int n = 0;
   *val = 0;
   while (hexdigit(*buf) >= 0) {
      *val = (*val * 10) + decdigit(*buf);
      n++; buf++;
   }
   return n;
}
Ejemplo n.º 17
0
void unescape3(ByteIt i, ByteIt e, String& s) {
  for (; i != e; ++i) {
    char c = *i;
    if (c != '\\')
      s.push_back(c);
    else {
      if (++i == e) throw Escape3Exception();
      c = *i;
      if (c == '\\')
        s.push_back(c);
      else {
        unsigned char high = hexdigit(c);
        if (++i == e) throw Escape3Exception();
        unsigned char low = hexdigit(*i);
        s.push_back(high * 16 + low);
      }
    }
  }
}
Ejemplo n.º 18
0
int unescape_url_n(const char *from, char *to, size_t n)
{
	register char c, x1, x2;

	while (n-- && (c = *from++) != 0) {
		if (c == '%') {
			x1 = *from++;
			if (!isxdigit(x1))
				return -1;
			x2 = *from++;
			if (!isxdigit(x2))
				return -1;
			*to++ = (hexdigit(x1) << 4) + hexdigit(x2);
		} else
			*to++ = c;
	}
	*to = 0;
	return 0;
}
Ejemplo n.º 19
0
void __declspec(dllexport) CalculateSha512Sum(HWND hwndParent, int string_size,
	TCHAR *variables, stack_t **stacktop, extra_parameters *extra)
{
	int i;
	TCHAR filename[1024];
	Sha512Context context;
	HANDLE file;
	char buffer[512];
	char comp[1024];
	DWORD bytesread;
	SHA512_HASH sha512;
	int compout;
	EXDLL_INIT();
	popstring(filename);
	popstring(comp);
	Sha512Initialise(&context);
	file = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
		OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
	if (!file)
	{
		filename[0] = '0';
		filename[1] = 0;
		pushstring(comp);
		pushstring(filename);
		return;
	}
	while (1)
	{
		ReadFile(file, buffer, 512, &bytesread, NULL);
		if (!bytesread) break;
		Sha512Update(&context, buffer, bytesread);
		if (bytesread < 512) break;
	}
	Sha512Finalise(&context, &sha512);
	CloseHandle(file);
	for (i = 0; i < (512 / 8); i++)
	{
		buffer[i * 2] = hexdigit(sha512.bytes[i] >> 4);
		buffer[(i * 2) + 1] = hexdigit(sha512.bytes[i] & 0xF);
	}
	buffer[512 / 4] = 0;
	compout = memcmp(buffer, comp, 128);
	if (compout)
	{
		filename[0] = '0';
	}
	else
	{
		filename[0] = '1';
	}
	filename[1] = 0;
	pushstring(comp);
	pushstring(filename);
}
Ejemplo n.º 20
0
static void stringrepr_inplace(char **bufp,int *szp,int *lenp,const char *str){
	int reslen=2;
	for(int i=0;str[i];i++){
		if(strchr("\"\\/\b\f\n\r\t",str[i])!=NULL)reslen+=2;
		else if(str[i]>=32&&str[i]<=126)reslen++;
		else reslen+=6;
	}
	bufextend(bufp,szp,lenp,*lenp+reslen);

	char *buf=*bufp+*lenp;
	*buf++='"';
	for(int i=0;str[i];i++){
		switch(str[i]){
			case '"': *buf++='\\'; *buf++='"'; break;
			case '\\': *buf++='\\'; *buf++='\\'; break;
			case '\b': *buf++='\\'; *buf++='b'; break;
			case '\f': *buf++='\\'; *buf++='f'; break;
			case '\n': *buf++='\\'; *buf++='n'; break;
			case '\r': *buf++='\\'; *buf++='r'; break;
			case '\t': *buf++='\\'; *buf++='t'; break;
			case '/': *buf++='\\'; *buf++='/'; break;
			default:
				if(str[i]>=32&&str[i]<=126){
					*buf++=str[i];
				} else {
					*buf++='\\';
					*buf++='u';
					*buf++='0';
					*buf++='0';
					*buf++=hexdigit((unsigned int)(unsigned char)str[i]/16);
					*buf++=hexdigit((unsigned int)(unsigned char)str[i]%16);
				}
				break;
		}
	}
	*buf++='"';
	*buf='\0';

	*lenp+=reslen;
}
Ejemplo n.º 21
0
static int sscanf6(char str[], int *a1, int *a2, int *a3, int *a4, int *a5, int *a6)
{
    int n;

    *a1 = *a2 = *a3 = *a4 = *a5 = *a6 = 0;
    while ((n=hexdigit(*str))>=0)
        (*a1 = 16*(*a1) + n, str++);
    if (*str++ != ':') return 1;
    while ((n=hexdigit(*str))>=0)
        (*a2 = 16*(*a2) + n, str++);
    if (*str++ != ':') return 2;
    while ((n=hexdigit(*str))>=0)
        (*a3 = 16*(*a3) + n, str++);
    if (*str++ != ':') return 3;
    while ((n=hexdigit(*str))>=0)
        (*a4 = 16*(*a4) + n, str++);
    if (*str++ != ':') return 4;
    while ((n=hexdigit(*str))>=0)
        (*a5 = 16*(*a5) + n, str++);
    if (*str++ != ':') return 5;
    while ((n=hexdigit(*str))>=0)
        (*a6 = 16*(*a6) + n, str++);

    return 6;
}
Ejemplo n.º 22
0
int curve25519_pubkey_hexparse_32(unsigned char *y, size_t ylen,
				  const char *x, size_t len)
{
	int seen_digits = 0, seen_colons = 0;

	if (!x || !y || ylen != 32)
		return 0;

	while (len > 0 && seen_digits != 32) {
		int digit0, digit1;

		if (x[0] == '\0')
			break;
		if (x[0] == ':') {
			seen_colons++;
			--len;
			x++;
			continue;
		}

		digit0 = hexdigit(x[0]);
		if (digit0 == -1)
			return 0;

		digit1 = hexdigit(x[1]);
		if (digit1 == -1)
			return 0;

		*y++ = digit1 + 16 * digit0;

		seen_digits++;
		--len;
		x += 2;
	}

	if (/*x[0] != '\0' ||*/ seen_digits != 32 || seen_colons != 31)
		return 0;

	return 1;
}
Ejemplo n.º 23
0
int otoi(char * oct){
	int res = 0;
	int i, digit;
	
	for(i=0; oct[i]; i++){
		res = res << 3;
		digit = hexdigit(oct[i]);
		if(digit == -1 || digit > 7) return -1;
		res |= digit;
	}
	
	return res;
}
Ejemplo n.º 24
0
static int
iri_add_encoded_charbuf(charbuf *cb, int c, int flags)
{ if ( iri_no_escape(c, flags) )
  { add_charbuf(cb, c);
  } else
  { assert(c < 128);
    add_charbuf(cb, '%');
    add_charbuf(cb, hexdigit(c>>4));
    add_charbuf(cb, hexdigit(c&0xf));
  }

  return TRUE;
}
Ejemplo n.º 25
0
void Int2HexString(unsigned int Value, char *HexString)
{
    // assumes that the string is big enough to receive the digits
    // assumes 32 bit integers
    // 32 bit hex string has 8 hex digits (index 0 to 7 inclusive)
    // need space for a terminating null also
    int i;
    for (i=7; i>=0; i--) {
        HexString[i]=hexdigit(Value & 0xf);
        Value = Value >> 4;
    }
    HexString[8]=0;
}
Ejemplo n.º 26
0
int btoi(char * bin){
	int res = 0;
	int i, digit;
	
	for(i=0; bin[i]; i++){
		res = res << 1;
		digit = hexdigit(bin[i]);
		if(digit == -1 || digit > 1) return -1;
		res |= digit;
	}
	
	return res;
}
Ejemplo n.º 27
0
int htoi(char * hex){
	int res = 0;
	int digit;
	int i;

	for(i=0; hex[i]; i++){
		res = res << 4;
		digit = hexdigit(hex[i]);
		if(digit == -1) return -1;
		res |= digit;
	}

	return res;
}
Ejemplo n.º 28
0
static int
read_hex(const char *hex, char *buf, size_t bufsize)
{
    const char *hexptr = hex;
    size_t written = 0;
    signed char upper, lower;

    while ((upper = hexdigit(hexptr[0])) >= 0) {
	if (written >= bufsize) {
	    DBG(DBG_IOCTL_TREE, "read_hex: data is larger than buffer size %zu\n", bufsize);
	    return FALSE;
	}

	lower = hexdigit(hexptr[1]);
	if (lower < 0) {
	    DBG(DBG_IOCTL_TREE, "read_hex: data has odd number of digits: '%s'\n", hex);
	    return FALSE;
	}
	buf[written++] = upper << 4 | lower;
	hexptr += 2;
    }
    return TRUE;
}
Ejemplo n.º 29
0
Archivo: ndis.c Proyecto: HarryR/sanos
void ndisapi NdisReadNetworkAddress(
    ndis_status *status,
    void **network_address,
    unsigned int *network_address_length,
    ndis_handle_t configuration_handle) {
  struct ndis_config *cfg = (struct ndis_config *) configuration_handle;
  struct ndis_configuration_parameter *param;
  wchar_t *p;
  unsigned char *q;
  int addrlen;

  NDISTRACE("NdisReadNetworkAddress");

  param = read_config(cfg, "NetworkAddress", NDIS_PARAMETER_STRING);
  if (!param) {
    *status = NDIS_STATUS_FAILURE;
    return;
  }

  p = param->parameter_data.string_data.buffer;
  q = cfg->hwaddr.addr;
  addrlen = 0;
  while (*p) {
    if (addrlen > ETHER_ADDR_LEN) break;
    if (*p == '-') *p++;
    if (*p)  {
      *q = hexdigit(*p++);
      if (*p) *q = (*q << 4) | hexdigit(*p++);
      q++;
      addrlen++;
    }
  }

 *network_address = &cfg->hwaddr;
 *network_address_length = addrlen;
 *status = 0;
}
Ejemplo n.º 30
0
void printhexnum(unsigned long num)
	{
	unsigned long temp, i = 0;
	char buf[16];

	hexdigit(0x10000000);
	hexdigit(0x1000000);
	hexdigit(0x100000);
	hexdigit(0x10000);
	hexdigit(0x1000);
	hexdigit(0x100);
	hexdigit(0x10);
	hexdigit(0x1);
	buf[i++] = 10;
	buf[i++] = 0;
	TD_Print(buf);
	}