Esempio n. 1
0
    static void append(buffer &json, const char *key, const std::string &value)
    {
        json.append("\"" , 1);
        json.append( key, strlen(key) ); //"key"
        json.append("\":", 2);

        append(json, value);
    }
Esempio n. 2
0
    static void append(buffer &json, const Seq &value)
    {
        json.append("[", 1);

        for(const auto &v : value)
        {
            json_traits<type_t>::append(json, v);
        }
        json.erase_last_comma();
        json.append("],", 2);
    }
Esempio n. 3
0
 static void append(buffer &json, const char &value,
                    const bool&, const unsigned&, unsigned&)
 {
     if( std::isgraph(value) )
     {
         char c[4] = {'"', value, '"', ','};
         json.append(c, 4);
     }
     else
         json.append("null,", 5);
 }
Esempio n. 4
0
bool xml_document::to_string(buffer& buf, bool pretty_print, bool nul_terminate) const
{
	if (_M_used == 0) {
		return nul_terminate ? buf.append((char) 0) : true;
	}

	if (!to_string(buf, pretty_print, _M_nodes, 0)) {
		return false;
	}

	return nul_terminate ? buf.append((char) 0) : true;
}
Esempio n. 5
0
 static void append(buffer &json, const std::string &value)
 {
     if(! value.empty() )    //:"value"
     {
         json.append("\"", 1);
         json.append( value.c_str(), value.length() );
         json.append("\",", 2);
     }
     else                    //:null
     {
         json.append( "null,", 5);
     }
 }
Esempio n. 6
0
 static void append(buffer &json, const char* value)
 {
     if(value != nullptr)
     {
         json.append("\"", 1);
         json.append( value, strlen(value)); //value
         json.append("\",", 2);
     }
     else
     {
         json.append( "null," , 5 ); //null
     }
 }
Esempio n. 7
0
    static void append(buffer &json, const char *key, const Seq &value)
    {
        json.append("\"", 1);
        json.append(key, strlen(key) );
        json.append("\":[", 3);

        for(const auto &v : value)
        {
            json_traits<type_t>::append(json, v);
        }

        json.erase_last_comma();
        json.append("],", 2);
    }
Esempio n. 8
0
static inline void make_json(const bool &pretty, const unsigned &indent, unsigned &level,
                             buffer &json, const std::string &keys, const T& val, const Types& ...values)
{
    std::string::size_type pos = keys.find(',');

    if(pretty)
    {
        json.append("\n", 1);
        json.append(' ', indent * level);
    }
    type::json_traits<T>::append(json, keys.substr(0, pos).c_str() , val, pretty, indent, level);

    make_json(pretty, indent, level, json, keys.substr(pos+1, keys.length()-1 ).c_str() ,values...);
}
Esempio n. 9
0
bool xml_document::node_to_string(buffer& buf, bool pretty_print, const struct node* node, unsigned depth) const
{
	if (pretty_print) {
		for (unsigned i = 0; i < depth; i++) {
			if (!buf.append('\t')) {
				return false;
			}
		}
	}

	switch (node->type) {
		case DOCTYPE_NODE:
			return buf.format("<!DOCTYPE %.*s>", node->len, _M_buf.data() + node->text);
		case PROCESSING_INSTRUCTION_NODE:
			return buf.format("<?%.*s?>", node->len, _M_buf.data() + node->text);
		case COMMENT_NODE:
			return buf.format("<!-- %.*s -->", node->len, _M_buf.data() + node->text);
		case ELEMENT_NODE:
			if (!buf.format("<%.*s", node->len, _M_buf.data() + node->text)) {
				return false;
			}

			if (node->attributes) {
				const char* name;
				size_t namelen;
				const char* value;
				size_t valuelen;

				for (size_t i = 0; node->attributes->get(i, name, namelen, value, valuelen); i++) {
					char quotes = (memchr(value, '"', valuelen)) ? '\'' : '"';
					if (!buf.format(" %.*s=%c%.*s%c", namelen, name, quotes, valuelen, value, quotes)) {
						return false;
					}
				}
			}

			if (node->child > 0) {
				return buf.append('>');
			} else {
				return buf.append("/>", 2);
			}

		case TEXT_NODE:
			return buf.format("%.*s", node->len, _M_buf.data() + node->text);
		default:
			return false;
	}
}
Esempio n. 10
0
bool xml_document::to_string(buffer& buf, bool pretty_print, const struct node* node, unsigned depth) const
{
	do {
		if (pretty_print) {
			// If not the first node...
			if (node != _M_nodes) {
				if (!buf.append("\r\n", 2)) {
					return false;
				}
			}
		}

		if (!node_to_string(buf, pretty_print, node, depth)) {
			return false;
		}

		if (node->child != -1) {
			if (!to_string(buf, pretty_print, &(_M_nodes[node->child]), depth + 1)) {
				return false;
			}

			if (pretty_print) {
				if (!buf.append("\r\n", 2)) {
					return false;
				}

				for (unsigned i = 0; i < depth; i++) {
					if (!buf.append('\t')) {
						return false;
					}
				}
			}

			if (!buf.format("</%.*s>", node->len, _M_buf.data() + node->text)) {
				return false;
			}
		}

		if (node->next < 0) {
			return true;
		}

		node = &(_M_nodes[node->next]);
	} while (true);
}
Esempio n. 11
0
stream::state_t
socks5_proxy::send( buffer &in_buf, buffer &out_buf )
{
	if ( m_state == stream::state_t::handshaking )
	{
		if ( !m_sent_greeting )
		{
			if ( proxy::manager::shared().authorization().size() > 0 )
			{
				out_buf.size( 4 );
			
				out_buf.put( 0, 0x05 );
				out_buf.put( 1, 0x02 );
				out_buf.put( 2, 0x00 );
				out_buf.put( 3, 0x02 );
			}
			else
			{
				out_buf.size( 3 );
				
				out_buf.put( 0, 0x05 );
				out_buf.put( 1, 0x01 );
				out_buf.put( 2, 0x00 );
			}
			
			m_sent_greeting = true;
		}
		
		m_send_queue.append( in_buf );
		
		m_state5 = waiting_for_opening_response;
		m_state = stream::state_t::connected;
	}
	else if ( m_state5 != connected )
	{
		m_send_queue.append( in_buf );
	}
	else
	{
		out_buf = std::move( in_buf );
	}
	
	return m_state;
}
Esempio n. 12
0
JSONPACK_API_BEGIN_NAMESPACE


////============================== MAKE_JSON ==============================================

static inline void make_json(const bool &pretty, const unsigned &indent, unsigned &level,
                             buffer &json, const std::string &UNUSED(key))
{
    json.erase_last_comma();

    if (pretty)
    {
        --level;
        json.append("\n", 1);
        json.append(' ', indent * level);
    }

    json.append("}", 1);
}
Esempio n. 13
0
JSONPACK_API_BEGIN_NAMESPACE


////============================== MAKE_JSON ==============================================

static inline void make_json(buffer &json, const std::string &UNUSED(key) )
{
    json.erase_last_comma();
    json.append("}\0", 2);
}
Esempio n. 14
0
 static void append(buffer &json, const std::string &value)
 {
     if(! value.empty() )    //:"value"
     {
         util::json_builder::append_string(json, value.c_str(), value.length() );
     }
     else                    //:null
     {
         json.append( "null,", 5);
     }
 }
Esempio n. 15
0
unsigned list_cases_on(vm_obj const & o, buffer<vm_obj> & data) {
    if (is_simple(o)) {
        return 0;
    } else if (is_constructor(o)) {
        data.append(csize(o), cfields(o));
        return 1;
    } else {
        lean_assert(is_external(o));
        if (auto l = dynamic_cast<vm_list<name>*>(to_external(o))) {
            return list_cases_on_core(l->m_val, data);
        } else if (auto l = dynamic_cast<vm_list<expr>*>(to_external(o))) {
            return list_cases_on_core(l->m_val, data);
        } else if (auto l = dynamic_cast<vm_list<level>*>(to_external(o))) {
            return list_cases_on_core(l->m_val, data);
        } else {
            lean_unreachable();
        }
    }
}
Esempio n. 16
0
 static void append(buffer &json, const bool &value,
                    const bool&, const unsigned&, unsigned&) //append value in array
 {
     json.append(value ? "true," : "false,", value ? 5 :6 );
 }
Esempio n. 17
0
stream::state_t
socks5_proxy::recv( std::vector< buffer > &in_recv_bufs, buffer &out_send_buf, std::vector< buffer > &out_recv_bufs )
{
	switch ( m_state5 )
	{
		case waiting_for_opening_response:
		{
			std::size_t bytes_available = in_recv_bufs[ 0 ].size();
			std::size_t handshake_left	= 2 - m_recv_handshake.size();
	
			m_recv_handshake.append( in_recv_bufs[ 0 ].data(), std::min( handshake_left, bytes_available ) );
			
			if ( m_recv_handshake.size() < 2 )
			{
				in_recv_bufs[ 0 ].rotate( 0, std::min( handshake_left, bytes_available ), in_recv_bufs[ 0 ].size() );
			}
			else
			{
				if ( m_recv_handshake[ 0 ] == 0x05 )
				{
					if ( m_recv_handshake[ 1 ] == 0x00 )
					{
						out_send_buf	= make_connect_buffer();
						out_recv_bufs.clear();
						
						m_state5 = waiting_for_connect_response;
					}
					else if ( m_recv_handshake[ 1 ] == 0x02 )
					{
						out_send_buf	= make_auth_buffer();
						out_recv_bufs.clear();
	
						m_state5 = waiting_for_authentication_response;
					}
					else
					{
						m_state = stream::state_t::error;
					}
				}
				else
				{
					m_state = stream::state_t::error;
				}
					
				m_recv_handshake.clear();
			}
		}
		break;
		
		case waiting_for_authentication_response:
		{
			std::size_t bytes_available = in_recv_bufs[ 0 ].size();
			std::size_t handshake_left	= 2 - m_recv_handshake.size();
	
			m_recv_handshake.append( in_recv_bufs[ 0 ].data(), std::min( handshake_left, bytes_available ) );
			
			if ( m_recv_handshake.size() < 2 )
			{
				in_recv_bufs[ 0 ].rotate( 0, std::min( handshake_left, bytes_available ), in_recv_bufs[ 0 ].size() );
			}
			else
			{
				if ( ( m_recv_handshake[ 0 ] == 0x01 ) && ( m_recv_handshake[ 1 ] == 0x00 ) )
				{
					out_send_buf = make_connect_buffer();
					out_recv_bufs.clear();
					
					m_state5 = waiting_for_connect_response;
				}
				else
				{
					m_state = stream::state_t::error;
				}
				
				m_recv_handshake.clear();
			}
		}
		break;
				
		case waiting_for_connect_response:
		{
			std::size_t bytes_available = in_recv_bufs[ 0 ].size();
			std::size_t handshake_left	= 4 - m_recv_handshake.size();
	
			m_recv_handshake.append( in_recv_bufs[ 0 ].data(), std::min( handshake_left, bytes_available ) );
			
			if ( m_recv_handshake.size() < 4 )
			{
				in_recv_bufs[ 0 ].rotate( 0, std::min( handshake_left, bytes_available ), in_recv_bufs[ 0 ].size() );
			}
			else if ( ( m_recv_handshake[ 0 ] == 0x05 ) && ( m_recv_handshake[ 1 ] == 0x00 ) )
			{
				switch ( m_recv_handshake[ 3 ] )
				{
					case 0x01:
					{
						in_recv_bufs[ 0 ].rotate( 0, 6, in_recv_bufs[ 0 ].size() );
						
						out_send_buf = std::move( m_send_queue );
						
						m_state5 = connected;
						m_state = stream::state_t::connected;
					}
					break;
							
					case 0x03:
					{
						auto len = in_recv_bufs[ 0 ][ 0 ];
							
						in_recv_bufs[ 0 ].rotate( 0, len + 1, in_recv_bufs[ 0 ].size() );
						
						out_send_buf = std::move( m_send_queue );
							
						m_state5 = connected;
						m_state = stream::state_t::connected;
					}
					break;
								
					case 0x04:
					{
						in_recv_bufs[ 0 ].rotate( 0, 18, in_recv_bufs[ 0 ].size() );
						
						out_send_buf = std::move( m_send_queue );
						
						m_state5 = connected;
						m_state = stream::state_t::connected;
					}
					break;
						
					default:
					{
						m_state = stream::state_t::error;
					}
				}
			}
		}
		break;
		
		default:
		{
			out_recv_bufs = std::move( in_recv_bufs );
		}
	}
	
	return m_state;
}