Ejemplo n.º 1
0
int
utf2ascii (const char** const inbuf, size_t* const inbytesleft,
	   char** const outbuf, size_t* const outbytesleft)
{
	if (inbuf  == NULL || *inbuf  == NULL || inbytesleft  == NULL ||
	    outbuf == NULL || *outbuf == NULL || outbytesleft == NULL)
		return EFAULT; // ---------->

	if (u2c_table == NULL) {
		return no_conv (inbuf, inbytesleft, outbuf, outbytesleft);
	}
	
	const unsigned char* src = (const unsigned char*) (*inbuf);
	char* dest = *outbuf;
	while (*inbytesleft > 0 && *outbytesleft > 0) {
		unsigned char const c = (unsigned char) *src;
		uint16_t s = 0x0;
		bool s_ok = false;
		int count = 1;
		if (c <= 0x7f) {
			s = c;
			s_ok = true;
		} else if (c >= 0xc0 && c <= 0xdf &&
			   *inbytesleft > 1 &&
			   (src[1] & 0xc0) == 0x80) {
			s = ((c & 0x1f) << 6) | (src[1] & 0x3f);
			s_ok = (s > 0x7f); // detect illegal overlong sequence
			count = 2;
		} else if (c >= 0xe0 && c <= 0xef &&
			   *inbytesleft > 2 &&
			   (src[1] & 0xc0) == 0x80 && 
			   (src[2] & 0xc0) == 0x80) {
			s = ((c & 0x0f) << 12) | ((src[1] & 0x3f) << 6);
			s |= (src[2] & 0x3f);
			s_ok = (s > 0x7ff); // detect illegal overlong sequence
			count = 3;
		}
		src += count;
		(*inbytesleft) -= count;
		(*outbytesleft)--;
		if (s_ok && s < u2c_table_size) {
			*(dest++) = u2c_table[s];
		} else {
			*(dest++) = ERROR_CHAR_ASCII;
			// Skip all extraneous continuation bytes
			while ( *inbytesleft > 0 && (*src & 0xc0) == 0x80 ) {
				src++;
				(*inbytesleft)--;
			}
		}
	}
	*outbuf = dest;
	*inbuf  = (const char*) src;
	return (*inbytesleft > 0 ? E2BIG : 0);
}
Ejemplo n.º 2
0
void	in_nor(t_main *main)
{
	if (find(main, 's') == 1 && main->width > 0 && main->zeroetat == 1)
		main->print = ft_strjoin("0", main->print);
	else
	{
		if (main->zeroetat == 1 && no_conv(main) == 1 && main->width > 0)
			main->print = ft_strjoin("0", main->print);
		else if (main->zeroetat == 1 && main->width > 0 && main->accurate <= 0)
			main->print = ft_strjoin("0", main->print);
		else
			main->print = ft_strjoin(" ", main->print);
	}
}
Ejemplo n.º 3
0
int
ascii2utf (const char** const inbuf, size_t* const inbytesleft,
	   char** const outbuf, size_t* const outbytesleft)
{
	if (inbuf  == NULL || *inbuf  == NULL || inbytesleft  == NULL ||
	    outbuf == NULL || *outbuf == NULL || outbytesleft == NULL)
		return EFAULT; // ---------->

	if (c2u_table == NULL) {
		return no_conv (inbuf, inbytesleft, outbuf, outbytesleft);
	}
	
	const unsigned char* src = (const unsigned char*) (*inbuf);
	char* dest = *outbuf;
	while (*inbytesleft > 0 && *outbytesleft > 0) {
		uint16_t s = c2u_table[*src]; 
		int count;
		if (s <= 0x7f)
			count = 1;
		else if (s <= 0x7ff)
			count = 2;
		else
			count = 3;
		if (count > *outbytesleft)
			break; // ---------->
		switch (count) { // note: code falls through cases!
		case 3: dest[2] = 0x80 | (s & 0x3f); s = s >> 6; s |= 0x800;
		case 2: dest[1] = 0x80 | (s & 0x3f); s = s >> 6; s |= 0xc0;
		case 1: dest[0] = s;
		}
		dest += count;
		(*outbytesleft) -= count;
		src++;
		(*inbytesleft)--;
	}
	*outbuf = dest;
	*inbuf  = (const char*) src;
	return ((*inbytesleft) > 0 ? E2BIG : 0);
}