예제 #1
0
/*
 * Parse the HEX string to buffer.
 * 'buf'	- the buffer to save the result.
 * 'size'	- the size of the buffer.
 * 'string'	- the HEX string.
 * On return, the byte count of the saved data in buffer.
 */
size_t read_hexstring(unsigned char * buf, size_t size, const char * string)
{
	size_t i = 0;
	int c;

	while (*string && i < size)
	{
		buf[i] = 0;

		do
		{
			/* first digit */
			c = hex_char(*string++);
			if (c < 0) break;
			buf[i] = (char)c;

			/* second digit */
			if (!(*string)) break;
			c = hex_char(*string++);
			if (c < 0) break;
			buf[i] = buf[i] * 0x10 + (char)c;

			/* If next  */
			if (!(*string)) break;
			if (hex_char(*string) < 0) string++;
		} while (0);
		i++;
	}
	return i;
}
예제 #2
0
/*
 * If the input string contains special characters that needs to be
 * escaped before the string can be used in a LDAP filter then this
 * function will return a new sanitized string. Otherwise this function
 * returns the input string (This saves us un-necessary memory allocations
 * especially when processing a batch of requests). The caller must free
 * the returned string if it isn't the input string.
 *
 * The escape mechanism for LDAP filter is described in RFC2254 basically
 * it's \hh where hh are the two hexadecimal digits representing the ASCII
 * value of the encoded character (case of hh is not significant).
 * Example: * -> \2a, ( -> \28, ) -> \29, \ -> \5c,
 *
 * outstring = sanitize_for_ldap_filter(instring);
 * if (outstring == NULL)
 *	Out of memory
 * else
 *	Use outstring
 *	if (outstring != instring)
 *		free(outstring);
 * done
 */
char *
sanitize_for_ldap_filter(const char *str)
{
	const char	*p;
	char		*q, *s_str = NULL;
	int		n;

	/* Get a count of special characters */
	for (p = str, n = 0; *p; p++)
		if (*p == '*' || *p == '(' || *p == ')' ||
		    *p == '\\' || *p == '%')
			n++;
	/* If count is zero then no need to sanitize */
	if (n == 0)
		return ((char *)str);
	/* Create output buffer that will contain the sanitized value */
	s_str = calloc(1, n * 2 + strlen(str) + 1);
	if (s_str == NULL)
		return (NULL);
	for (p = str, q = s_str; *p; p++) {
		if (*p == '*' || *p == '(' || *p == ')' ||
		    *p == '\\' || *p == '%') {
			*q++ = '\\';
			*q++ = hex_char(*p >> 4);
			*q++ = hex_char(*p & 0xf);
		} else
예제 #3
0
void s_printf(char *format,...)
{
 va_list	opt;

 char out[2]=" ";

 int val;

 char *s;

 va_start(opt, format);

 while(format[0])
	{
	if(format[0]!='%') {out[0]=*format++;os_puts(out);}
	else
		{
		format++;
		switch(format[0])
			{
			case 'd':
			case 'i':
				val=va_arg(opt,int);
				int_char(val);
				
				os_puts(mem_cad);
				
				break;

			case 'u':
				val=va_arg(opt, unsigned);
				uint_char(val);
				
				os_puts(mem_cad);
				
				break;

			case 'x':
				val=va_arg(opt,int);
				hex_char((u32) val);
				os_puts(mem_cad);
				
				break;

			case 's':
				s=va_arg(opt,char *);
				os_puts(s);
				break;

			}
		 format++;
		}
	
	}
   
	va_end(opt);

	
}
예제 #4
0
파일: main.c 프로젝트: meriac/openbeacon-ng
static void print_hex(const uint8_t* data, uint16_t len)
{
	uint8_t d;

	while(len>0)
	{
		d = *data++;
		default_putchar(hex_char(d>>4));
		default_putchar(hex_char(d&0xF));
		len--;
	}
}
예제 #5
0
파일: uart.c 프로젝트: vbendeb/ci20-tools
void uart_putx(uint32_t x, unsigned digits)
{
	char buf[9];
	int idx = 7;

	buf[8] = 0;

	do {
		buf[idx--] = hex_char(x & 0xf);
		x >>= 4;
	} while (x || ((7 - idx) < digits));

	uart_puts(&buf[idx + 1]);
}
예제 #6
0
void UncompressFile::buildNodeList(HuffmanTree& ht)
{
    if ( !isPtr.is_open()) {
        isPtr.open(inFile.c_str());
        if (!isPtr) {
            cerr << "Error: Could not open: '" << inFile << "' for reading" << endl;
            return;
        }
    }

    //  char readBuf[readBufSize];
    isPtr.getline((char*)readBuf,readBufSize);

    string buf; // Have a buffer string
    vector<string> token;
    stringstream ss((char*)readBuf);
    unsigned int count;

    // Tokenize the string into a vector
    while (ss >> buf) {
        token.push_back(buf);
        count++;
    }

    // Validate that we have a even number
    if (count % 2 != 0) {
        cerr << "CountTable::extractCharMap() : Internal Error\n";
        return;
    }

    unsigned int c;
    // Convert the first token to 'hex' and the second to 'unsigned'
    for (unsigned i=0; i<token.size(); i+=2) {
        istringstream hex_char(token[i]);
        hex_char >> hex >> c;
        istringstream int_char(token[i+1]);
        int_char >> count;
        ht.buildNodeList(c, count);
    }

    if (isPtr.is_open()) isPtr.close();

} // End buildNodeList()
예제 #7
0
파일: global.c 프로젝트: klickverbot/wine
static HRESULT Global_Hex(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
{
    WCHAR buf[17], *ptr;
    DWORD n;

    TRACE("%s\n", debugstr_variant(arg));

    switch(V_VT(arg)) {
    case VT_I2:
        n = (WORD)V_I2(arg);
        break;
    case VT_I4:
        n = V_I4(arg);
        break;
    case VT_EMPTY:
        n = 0;
        break;
    case VT_NULL:
        if(res)
            V_VT(res) = VT_NULL;
        return S_OK;
    default:
        FIXME("unsupported type %s\n", debugstr_variant(arg));
        return E_NOTIMPL;
    }

    buf[16] = 0;
    ptr = buf+15;

    if(n) {
        do {
            *ptr-- = hex_char(n & 0xf);
            n >>= 4;
        }while(n);
        ptr++;
    }else {
예제 #8
0
파일: gps.c 프로젝트: st0ne/up4dar-os
int gps_get_slow_data(uint8_t * slow_data)
{
	int ret_val = 0;
	
	slow_data[0] = 0x66; // NOP data
	slow_data[1] = 0x66;
	
	if (SETTING_CHAR(C_DPRS_ENABLED) != 1)
	{
		return 0;
	}
	
	// vdisp_printc_xy(0,0,VDISP_FONT_6x8, 0, 0x30 + slow_data_state);
	
	switch (slow_data_state)
	{
		case 0:  // no data to send
			break;
			
		case 1: // send GPGGA 
			if (slow_data_ptr == 0)
			{
				slow_data_state = 4; // end
			}
			else
			{
				if (copy_slow_data(&ret_val, slow_data) != 0) // if all bytes are copied
				{
					slow_data_state = 2;
					slow_data_ptr = gprmc_data;
				}
			}
			break;
		
		case 2: // send GPRMC
			if (slow_data_ptr == 0)
			{
				slow_data_state = 4; // end
			}
			else
			{
				if (copy_slow_data(&ret_val, slow_data) != 0) // if all bytes are copied
				{
					memcpy (gps_id, settings.s.my_callsign, CALLSIGN_LENGTH);
					gps_id[8] = ',';
					memcpy (gps_id + 9, dprs_symbol[SETTING_CHAR(C_DPRS_SYMBOL)], 4);
					
					memset (gps_id + 13, ' ', 16); // spaces
					gps_id[29] = 13;
					gps_id[30] = 10;
					gps_id[31] = 0;
					
					memcpy (gps_id + 13, settings.s.dprs_msg, 13);
					
					int i = 26;
					while (i > 13)
					{
						if (gps_id[i-1] != ' ') // space
							break;
						
						i--;
					}
					
					int chksum = 0;
					
					int j;
					
					for (j=0; j < i; j++)
					{
						chksum = chksum ^ gps_id[j];
					}
					
					gps_id[i] = '*';
					gps_id[i+1] = hex_char(chksum >> 4);
					gps_id[i+2] = hex_char(chksum & 0x0F);
					
					slow_data_state = 3;
					slow_data_ptr = gps_id;
				}
			}
			break;
			
		case 3: // send gps_id
			if (slow_data_ptr == 0)
			{
				slow_data_state = 4; // end
			}
			else
			{
				if (copy_slow_data(&ret_val, slow_data) != 0) // if all bytes are copied
				{
					slow_data_state = 4;
					slow_data_ptr = 0;
				}
			}
			break;
			
		case 4:
			gps_reset_slow_data();
			break;
	}
예제 #9
0
void debug_printf(char *format,...)
{
 #if 1
 va_list	opt;

 static char out[32] ATTRIBUTE_ALIGN(32)=" ";

 int val;

 char *s;

 va_start(opt, format);

 int fd;

int message;

static int one=1;
static u32 buffer[8];
static u32 queuehandle2=-1;

if(one)
	{
	queuehandle2 = os_message_queue_create(buffer, 8);
	one=0;
	}
else
 os_message_queue_receive(queuehandle2, (void *)&message, 0);
internal=internal_debug_printf;
 
 if(internal & 2) goto salir;

 if(index_dev & 1)
	{
	
	if(internal)
		fd = FAT_Open("usb:/ffs_log.txt" ,O_WRONLY | O_APPEND);
	else
		fd=os_open("usb:/ffs_log.txt" ,O_WRONLY | O_APPEND);
	}
else
	{
	
	if(internal)
		fd = FAT_Open("sd:/ffs_log.txt" ,O_WRONLY | O_APPEND);
	else
		fd=os_open("sd:/ffs_log.txt" ,O_WRONLY | O_APPEND);
	}

	

	if(fd<0) goto salir;

 while(format[0])
	{
	if(format[0]!='%') {out[0]=*format++; printf_write(fd, out, strlen(out));}
	else
		{
		format++;
		switch(format[0])
			{
			case 'd':
			case 'i':
				val=va_arg(opt,int);
				int_char(val);
				
				printf_write(fd, mem_cad, strlen(mem_cad));
				
				break;

			case 'u':
				val=va_arg(opt, unsigned);
				uint_char(val);
				
				//printf_write(fd, mem_cad, strlen(mem_cad));
				printf_write(fd, mem_cad, strlen(mem_cad));
				
				break;

			case 'x':
				val=va_arg(opt,int);
                hex_char((u32) val);
				printf_write(fd, mem_cad, strlen(mem_cad));
				
				break;

			case 's':
				s=va_arg(opt,char *);
				printf_write(fd, s, strlen(s));
				break;

			}
		 format++;
		}
	
	}
   
	va_end(opt);
if(internal)
	FAT_Close(fd);
else
	os_close(fd);

salir:
	os_message_queue_send(queuehandle2, 0, 0);
#endif	
}