repo_name
stringclasses 10
values | file_path
stringlengths 29
222
| content
stringlengths 24
926k
| extention
stringclasses 5
values |
---|---|---|---|
asio | data/projects/asio/include/boost/asio/detail/impl/io_uring_service.hpp | //
// detail/impl/io_uring_service.hpp
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_ASIO_DETAIL_IMPL_IO_URING_SERVICE_HPP
#define BOOST_ASIO_DETAIL_IMPL_IO_URING_SERVICE_HPP
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#if defined(BOOST_ASIO_HAS_IO_URING)
#include <boost/asio/detail/scheduler.hpp>
#include <boost/asio/detail/push_options.hpp>
namespace boost {
namespace asio {
namespace detail {
inline void io_uring_service::post_immediate_completion(
operation* op, bool is_continuation)
{
scheduler_.post_immediate_completion(op, is_continuation);
}
template <typename Time_Traits>
void io_uring_service::add_timer_queue(timer_queue<Time_Traits>& queue)
{
do_add_timer_queue(queue);
}
template <typename Time_Traits>
void io_uring_service::remove_timer_queue(timer_queue<Time_Traits>& queue)
{
do_remove_timer_queue(queue);
}
template <typename Time_Traits>
void io_uring_service::schedule_timer(timer_queue<Time_Traits>& queue,
const typename Time_Traits::time_type& time,
typename timer_queue<Time_Traits>::per_timer_data& timer, wait_op* op)
{
mutex::scoped_lock lock(mutex_);
if (shutdown_)
{
scheduler_.post_immediate_completion(op, false);
return;
}
bool earliest = queue.enqueue_timer(time, timer, op);
scheduler_.work_started();
if (earliest)
{
update_timeout();
post_submit_sqes_op(lock);
}
}
template <typename Time_Traits>
std::size_t io_uring_service::cancel_timer(timer_queue<Time_Traits>& queue,
typename timer_queue<Time_Traits>::per_timer_data& timer,
std::size_t max_cancelled)
{
mutex::scoped_lock lock(mutex_);
op_queue<operation> ops;
std::size_t n = queue.cancel_timer(timer, ops, max_cancelled);
lock.unlock();
scheduler_.post_deferred_completions(ops);
return n;
}
template <typename Time_Traits>
void io_uring_service::cancel_timer_by_key(timer_queue<Time_Traits>& queue,
typename timer_queue<Time_Traits>::per_timer_data* timer,
void* cancellation_key)
{
mutex::scoped_lock lock(mutex_);
op_queue<operation> ops;
queue.cancel_timer_by_key(timer, ops, cancellation_key);
lock.unlock();
scheduler_.post_deferred_completions(ops);
}
template <typename Time_Traits>
void io_uring_service::move_timer(timer_queue<Time_Traits>& queue,
typename timer_queue<Time_Traits>::per_timer_data& target,
typename timer_queue<Time_Traits>::per_timer_data& source)
{
mutex::scoped_lock lock(mutex_);
op_queue<operation> ops;
queue.cancel_timer(target, ops);
queue.move_timer(target, source);
lock.unlock();
scheduler_.post_deferred_completions(ops);
}
} // namespace detail
} // namespace asio
} // namespace boost
#include <boost/asio/detail/pop_options.hpp>
#endif // defined(BOOST_ASIO_HAS_IO_URING)
#endif // BOOST_ASIO_DETAIL_IMPL_IO_URING_SERVICE_HPP
| hpp |
asio | data/projects/asio/include/boost/asio/detail/impl/strand_service.hpp | //
// detail/impl/strand_service.hpp
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_ASIO_DETAIL_IMPL_STRAND_SERVICE_HPP
#define BOOST_ASIO_DETAIL_IMPL_STRAND_SERVICE_HPP
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include <boost/asio/detail/completion_handler.hpp>
#include <boost/asio/detail/fenced_block.hpp>
#include <boost/asio/detail/handler_alloc_helpers.hpp>
#include <boost/asio/detail/memory.hpp>
#include <boost/asio/detail/push_options.hpp>
namespace boost {
namespace asio {
namespace detail {
inline strand_service::strand_impl::strand_impl()
: operation(&strand_service::do_complete),
locked_(false)
{
}
template <typename Handler>
void strand_service::dispatch(strand_service::implementation_type& impl,
Handler& handler)
{
// If we are already in the strand then the handler can run immediately.
if (running_in_this_thread(impl))
{
fenced_block b(fenced_block::full);
static_cast<Handler&&>(handler)();
return;
}
// Allocate and construct an operation to wrap the handler.
typedef completion_handler<Handler, io_context::executor_type> op;
typename op::ptr p = { boost::asio::detail::addressof(handler),
op::ptr::allocate(handler), 0 };
p.p = new (p.v) op(handler, io_context_.get_executor());
BOOST_ASIO_HANDLER_CREATION((this->context(),
*p.p, "strand", impl, 0, "dispatch"));
operation* o = p.p;
p.v = p.p = 0;
do_dispatch(impl, o);
}
// Request the io_context to invoke the given handler and return immediately.
template <typename Handler>
void strand_service::post(strand_service::implementation_type& impl,
Handler& handler)
{
bool is_continuation =
boost_asio_handler_cont_helpers::is_continuation(handler);
// Allocate and construct an operation to wrap the handler.
typedef completion_handler<Handler, io_context::executor_type> op;
typename op::ptr p = { boost::asio::detail::addressof(handler),
op::ptr::allocate(handler), 0 };
p.p = new (p.v) op(handler, io_context_.get_executor());
BOOST_ASIO_HANDLER_CREATION((this->context(),
*p.p, "strand", impl, 0, "post"));
do_post(impl, p.p, is_continuation);
p.v = p.p = 0;
}
} // namespace detail
} // namespace asio
} // namespace boost
#include <boost/asio/detail/pop_options.hpp>
#endif // BOOST_ASIO_DETAIL_IMPL_STRAND_SERVICE_HPP
| hpp |
asio | data/projects/asio/include/boost/asio/detail/impl/dev_poll_reactor.hpp | //
// detail/impl/dev_poll_reactor.hpp
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_ASIO_DETAIL_IMPL_DEV_POLL_REACTOR_HPP
#define BOOST_ASIO_DETAIL_IMPL_DEV_POLL_REACTOR_HPP
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include <boost/asio/detail/config.hpp>
#if defined(BOOST_ASIO_HAS_DEV_POLL)
#include <boost/asio/detail/scheduler.hpp>
#include <boost/asio/detail/push_options.hpp>
namespace boost {
namespace asio {
namespace detail {
inline void dev_poll_reactor::post_immediate_completion(
operation* op, bool is_continuation) const
{
scheduler_.post_immediate_completion(op, is_continuation);
}
template <typename Time_Traits>
void dev_poll_reactor::add_timer_queue(timer_queue<Time_Traits>& queue)
{
do_add_timer_queue(queue);
}
template <typename Time_Traits>
void dev_poll_reactor::remove_timer_queue(timer_queue<Time_Traits>& queue)
{
do_remove_timer_queue(queue);
}
template <typename Time_Traits>
void dev_poll_reactor::schedule_timer(timer_queue<Time_Traits>& queue,
const typename Time_Traits::time_type& time,
typename timer_queue<Time_Traits>::per_timer_data& timer, wait_op* op)
{
boost::asio::detail::mutex::scoped_lock lock(mutex_);
if (shutdown_)
{
scheduler_.post_immediate_completion(op, false);
return;
}
bool earliest = queue.enqueue_timer(time, timer, op);
scheduler_.work_started();
if (earliest)
interrupter_.interrupt();
}
template <typename Time_Traits>
std::size_t dev_poll_reactor::cancel_timer(timer_queue<Time_Traits>& queue,
typename timer_queue<Time_Traits>::per_timer_data& timer,
std::size_t max_cancelled)
{
boost::asio::detail::mutex::scoped_lock lock(mutex_);
op_queue<operation> ops;
std::size_t n = queue.cancel_timer(timer, ops, max_cancelled);
lock.unlock();
scheduler_.post_deferred_completions(ops);
return n;
}
template <typename Time_Traits>
void dev_poll_reactor::cancel_timer_by_key(timer_queue<Time_Traits>& queue,
typename timer_queue<Time_Traits>::per_timer_data* timer,
void* cancellation_key)
{
boost::asio::detail::mutex::scoped_lock lock(mutex_);
op_queue<operation> ops;
queue.cancel_timer_by_key(timer, ops, cancellation_key);
lock.unlock();
scheduler_.post_deferred_completions(ops);
}
template <typename Time_Traits>
void dev_poll_reactor::move_timer(timer_queue<Time_Traits>& queue,
typename timer_queue<Time_Traits>::per_timer_data& target,
typename timer_queue<Time_Traits>::per_timer_data& source)
{
boost::asio::detail::mutex::scoped_lock lock(mutex_);
op_queue<operation> ops;
queue.cancel_timer(target, ops);
queue.move_timer(target, source);
lock.unlock();
scheduler_.post_deferred_completions(ops);
}
} // namespace detail
} // namespace asio
} // namespace boost
#include <boost/asio/detail/pop_options.hpp>
#endif // defined(BOOST_ASIO_HAS_DEV_POLL)
#endif // BOOST_ASIO_DETAIL_IMPL_DEV_POLL_REACTOR_HPP
| hpp |
asio | data/projects/asio/include/boost/asio/ip/v6_only.hpp | //
// ip/v6_only.hpp
// ~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_ASIO_IP_V6_ONLY_HPP
#define BOOST_ASIO_IP_V6_ONLY_HPP
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include <boost/asio/detail/config.hpp>
#include <boost/asio/detail/socket_option.hpp>
#include <boost/asio/detail/push_options.hpp>
namespace boost {
namespace asio {
namespace ip {
/// Socket option for determining whether an IPv6 socket supports IPv6
/// communication only.
/**
* Implements the IPPROTO_IPV6/IPV6_V6ONLY socket option.
*
* @par Examples
* Setting the option:
* @code
* boost::asio::ip::tcp::socket socket(my_context);
* ...
* boost::asio::ip::v6_only option(true);
* socket.set_option(option);
* @endcode
*
* @par
* Getting the current option value:
* @code
* boost::asio::ip::tcp::socket socket(my_context);
* ...
* boost::asio::ip::v6_only option;
* socket.get_option(option);
* bool v6_only = option.value();
* @endcode
*
* @par Concepts:
* GettableSocketOption, SettableSocketOption.
*/
#if defined(GENERATING_DOCUMENTATION)
typedef implementation_defined v6_only;
#elif defined(IPV6_V6ONLY)
typedef boost::asio::detail::socket_option::boolean<
IPPROTO_IPV6, IPV6_V6ONLY> v6_only;
#else
typedef boost::asio::detail::socket_option::boolean<
boost::asio::detail::custom_socket_option_level,
boost::asio::detail::always_fail_option> v6_only;
#endif
} // namespace ip
} // namespace asio
} // namespace boost
#include <boost/asio/detail/pop_options.hpp>
#endif // BOOST_ASIO_IP_V6_ONLY_HPP
| hpp |
asio | data/projects/asio/include/boost/asio/ip/network_v4.hpp | //
// ip/network_v4.hpp
// ~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com)
// Copyright (c) 2014 Oliver Kowalke (oliver dot kowalke at gmail dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_ASIO_IP_NETWORK_V4_HPP
#define BOOST_ASIO_IP_NETWORK_V4_HPP
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include <boost/asio/detail/config.hpp>
#include <string>
#include <boost/asio/detail/string_view.hpp>
#include <boost/system/error_code.hpp>
#include <boost/asio/ip/address_v4_range.hpp>
#include <boost/asio/detail/push_options.hpp>
namespace boost {
namespace asio {
namespace ip {
/// Represents an IPv4 network.
/**
* The boost::asio::ip::network_v4 class provides the ability to use and
* manipulate IP version 4 networks.
*
* @par Thread Safety
* @e Distinct @e objects: Safe.@n
* @e Shared @e objects: Unsafe.
*/
class network_v4
{
public:
/// Default constructor.
network_v4() noexcept
: address_(),
prefix_length_(0)
{
}
/// Construct a network based on the specified address and prefix length.
BOOST_ASIO_DECL network_v4(const address_v4& addr,
unsigned short prefix_len);
/// Construct network based on the specified address and netmask.
BOOST_ASIO_DECL network_v4(const address_v4& addr,
const address_v4& mask);
/// Copy constructor.
network_v4(const network_v4& other) noexcept
: address_(other.address_),
prefix_length_(other.prefix_length_)
{
}
/// Move constructor.
network_v4(network_v4&& other) noexcept
: address_(static_cast<address_v4&&>(other.address_)),
prefix_length_(other.prefix_length_)
{
}
/// Assign from another network.
network_v4& operator=(const network_v4& other) noexcept
{
address_ = other.address_;
prefix_length_ = other.prefix_length_;
return *this;
}
/// Move-assign from another network.
network_v4& operator=(network_v4&& other) noexcept
{
address_ = static_cast<address_v4&&>(other.address_);
prefix_length_ = other.prefix_length_;
return *this;
}
/// Obtain the address object specified when the network object was created.
address_v4 address() const noexcept
{
return address_;
}
/// Obtain the prefix length that was specified when the network object was
/// created.
unsigned short prefix_length() const noexcept
{
return prefix_length_;
}
/// Obtain the netmask that was specified when the network object was created.
BOOST_ASIO_DECL address_v4 netmask() const noexcept;
/// Obtain an address object that represents the network address.
address_v4 network() const noexcept
{
return address_v4(address_.to_uint() & netmask().to_uint());
}
/// Obtain an address object that represents the network's broadcast address.
address_v4 broadcast() const noexcept
{
return address_v4(network().to_uint() | (netmask().to_uint() ^ 0xFFFFFFFF));
}
/// Obtain an address range corresponding to the hosts in the network.
BOOST_ASIO_DECL address_v4_range hosts() const noexcept;
/// Obtain the true network address, omitting any host bits.
network_v4 canonical() const noexcept
{
return network_v4(network(), prefix_length());
}
/// Test if network is a valid host address.
bool is_host() const noexcept
{
return prefix_length_ == 32;
}
/// Test if a network is a real subnet of another network.
BOOST_ASIO_DECL bool is_subnet_of(const network_v4& other) const;
/// Get the network as an address in dotted decimal format.
BOOST_ASIO_DECL std::string to_string() const;
/// Get the network as an address in dotted decimal format.
BOOST_ASIO_DECL std::string to_string(boost::system::error_code& ec) const;
/// Compare two networks for equality.
friend bool operator==(const network_v4& a, const network_v4& b)
{
return a.address_ == b.address_ && a.prefix_length_ == b.prefix_length_;
}
/// Compare two networks for inequality.
friend bool operator!=(const network_v4& a, const network_v4& b)
{
return !(a == b);
}
private:
address_v4 address_;
unsigned short prefix_length_;
};
/// Create an IPv4 network from an address and prefix length.
/**
* @relates address_v4
*/
inline network_v4 make_network_v4(
const address_v4& addr, unsigned short prefix_len)
{
return network_v4(addr, prefix_len);
}
/// Create an IPv4 network from an address and netmask.
/**
* @relates address_v4
*/
inline network_v4 make_network_v4(
const address_v4& addr, const address_v4& mask)
{
return network_v4(addr, mask);
}
/// Create an IPv4 network from a string containing IP address and prefix
/// length.
/**
* @relates network_v4
*/
BOOST_ASIO_DECL network_v4 make_network_v4(const char* str);
/// Create an IPv4 network from a string containing IP address and prefix
/// length.
/**
* @relates network_v4
*/
BOOST_ASIO_DECL network_v4 make_network_v4(
const char* str, boost::system::error_code& ec);
/// Create an IPv4 network from a string containing IP address and prefix
/// length.
/**
* @relates network_v4
*/
BOOST_ASIO_DECL network_v4 make_network_v4(const std::string& str);
/// Create an IPv4 network from a string containing IP address and prefix
/// length.
/**
* @relates network_v4
*/
BOOST_ASIO_DECL network_v4 make_network_v4(
const std::string& str, boost::system::error_code& ec);
#if defined(BOOST_ASIO_HAS_STRING_VIEW) \
|| defined(GENERATING_DOCUMENTATION)
/// Create an IPv4 network from a string containing IP address and prefix
/// length.
/**
* @relates network_v4
*/
BOOST_ASIO_DECL network_v4 make_network_v4(string_view str);
/// Create an IPv4 network from a string containing IP address and prefix
/// length.
/**
* @relates network_v4
*/
BOOST_ASIO_DECL network_v4 make_network_v4(
string_view str, boost::system::error_code& ec);
#endif // defined(BOOST_ASIO_HAS_STRING_VIEW)
// || defined(GENERATING_DOCUMENTATION)
#if !defined(BOOST_ASIO_NO_IOSTREAM)
/// Output a network as a string.
/**
* Used to output a human-readable string for a specified network.
*
* @param os The output stream to which the string will be written.
*
* @param net The network to be written.
*
* @return The output stream.
*
* @relates boost::asio::ip::address_v4
*/
template <typename Elem, typename Traits>
std::basic_ostream<Elem, Traits>& operator<<(
std::basic_ostream<Elem, Traits>& os, const network_v4& net);
#endif // !defined(BOOST_ASIO_NO_IOSTREAM)
} // namespace ip
} // namespace asio
} // namespace boost
#include <boost/asio/detail/pop_options.hpp>
#include <boost/asio/ip/impl/network_v4.hpp>
#if defined(BOOST_ASIO_HEADER_ONLY)
# include <boost/asio/ip/impl/network_v4.ipp>
#endif // defined(BOOST_ASIO_HEADER_ONLY)
#endif // BOOST_ASIO_IP_NETWORK_V4_HPP
| hpp |
asio | data/projects/asio/include/boost/asio/ip/resolver_base.hpp | //
// ip/resolver_base.hpp
// ~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_ASIO_IP_RESOLVER_BASE_HPP
#define BOOST_ASIO_IP_RESOLVER_BASE_HPP
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include <boost/asio/detail/config.hpp>
#include <boost/asio/detail/socket_types.hpp>
#include <boost/asio/detail/push_options.hpp>
namespace boost {
namespace asio {
namespace ip {
/// The resolver_base class is used as a base for the basic_resolver class
/// templates to provide a common place to define the flag constants.
class resolver_base
{
public:
#if defined(GENERATING_DOCUMENTATION)
/// A bitmask type (C++ Std [lib.bitmask.types]).
typedef unspecified flags;
/// Determine the canonical name of the host specified in the query.
static const flags canonical_name = implementation_defined;
/// Indicate that returned endpoint is intended for use as a locally bound
/// socket endpoint.
static const flags passive = implementation_defined;
/// Host name should be treated as a numeric string defining an IPv4 or IPv6
/// address and no name resolution should be attempted.
static const flags numeric_host = implementation_defined;
/// Service name should be treated as a numeric string defining a port number
/// and no name resolution should be attempted.
static const flags numeric_service = implementation_defined;
/// If the query protocol family is specified as IPv6, return IPv4-mapped
/// IPv6 addresses on finding no IPv6 addresses.
static const flags v4_mapped = implementation_defined;
/// If used with v4_mapped, return all matching IPv6 and IPv4 addresses.
static const flags all_matching = implementation_defined;
/// Only return IPv4 addresses if a non-loopback IPv4 address is configured
/// for the system. Only return IPv6 addresses if a non-loopback IPv6 address
/// is configured for the system.
static const flags address_configured = implementation_defined;
#else
enum flags
{
canonical_name = BOOST_ASIO_OS_DEF(AI_CANONNAME),
passive = BOOST_ASIO_OS_DEF(AI_PASSIVE),
numeric_host = BOOST_ASIO_OS_DEF(AI_NUMERICHOST),
numeric_service = BOOST_ASIO_OS_DEF(AI_NUMERICSERV),
v4_mapped = BOOST_ASIO_OS_DEF(AI_V4MAPPED),
all_matching = BOOST_ASIO_OS_DEF(AI_ALL),
address_configured = BOOST_ASIO_OS_DEF(AI_ADDRCONFIG)
};
// Implement bitmask operations as shown in C++ Std [lib.bitmask.types].
friend flags operator&(flags x, flags y)
{
return static_cast<flags>(
static_cast<unsigned int>(x) & static_cast<unsigned int>(y));
}
friend flags operator|(flags x, flags y)
{
return static_cast<flags>(
static_cast<unsigned int>(x) | static_cast<unsigned int>(y));
}
friend flags operator^(flags x, flags y)
{
return static_cast<flags>(
static_cast<unsigned int>(x) ^ static_cast<unsigned int>(y));
}
friend flags operator~(flags x)
{
return static_cast<flags>(~static_cast<unsigned int>(x));
}
friend flags& operator&=(flags& x, flags y)
{
x = x & y;
return x;
}
friend flags& operator|=(flags& x, flags y)
{
x = x | y;
return x;
}
friend flags& operator^=(flags& x, flags y)
{
x = x ^ y;
return x;
}
#endif
protected:
/// Protected destructor to prevent deletion through this type.
~resolver_base()
{
}
};
} // namespace ip
} // namespace asio
} // namespace boost
#include <boost/asio/detail/pop_options.hpp>
#endif // BOOST_ASIO_IP_RESOLVER_BASE_HPP
| hpp |
asio | data/projects/asio/include/boost/asio/ip/host_name.hpp | //
// ip/host_name.hpp
// ~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_ASIO_IP_HOST_NAME_HPP
#define BOOST_ASIO_IP_HOST_NAME_HPP
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include <boost/asio/detail/config.hpp>
#include <string>
#include <boost/system/error_code.hpp>
#include <boost/asio/detail/push_options.hpp>
namespace boost {
namespace asio {
namespace ip {
/// Get the current host name.
BOOST_ASIO_DECL std::string host_name();
/// Get the current host name.
BOOST_ASIO_DECL std::string host_name(boost::system::error_code& ec);
} // namespace ip
} // namespace asio
} // namespace boost
#include <boost/asio/detail/pop_options.hpp>
#if defined(BOOST_ASIO_HEADER_ONLY)
# include <boost/asio/ip/impl/host_name.ipp>
#endif // defined(BOOST_ASIO_HEADER_ONLY)
#endif // BOOST_ASIO_IP_HOST_NAME_HPP
| hpp |
asio | data/projects/asio/include/boost/asio/ip/icmp.hpp | //
// ip/icmp.hpp
// ~~~~~~~~~~~
//
// Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_ASIO_IP_ICMP_HPP
#define BOOST_ASIO_IP_ICMP_HPP
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include <boost/asio/detail/config.hpp>
#include <boost/asio/detail/socket_types.hpp>
#include <boost/asio/basic_raw_socket.hpp>
#include <boost/asio/ip/basic_endpoint.hpp>
#include <boost/asio/ip/basic_resolver.hpp>
#include <boost/asio/ip/basic_resolver_iterator.hpp>
#include <boost/asio/ip/basic_resolver_query.hpp>
#include <boost/asio/detail/push_options.hpp>
namespace boost {
namespace asio {
namespace ip {
/// Encapsulates the flags needed for ICMP.
/**
* The boost::asio::ip::icmp class contains flags necessary for ICMP sockets.
*
* @par Thread Safety
* @e Distinct @e objects: Safe.@n
* @e Shared @e objects: Safe.
*
* @par Concepts:
* Protocol, InternetProtocol.
*/
class icmp
{
public:
/// The type of a ICMP endpoint.
typedef basic_endpoint<icmp> endpoint;
/// Construct to represent the IPv4 ICMP protocol.
static icmp v4() noexcept
{
return icmp(BOOST_ASIO_OS_DEF(IPPROTO_ICMP),
BOOST_ASIO_OS_DEF(AF_INET));
}
/// Construct to represent the IPv6 ICMP protocol.
static icmp v6() noexcept
{
return icmp(BOOST_ASIO_OS_DEF(IPPROTO_ICMPV6),
BOOST_ASIO_OS_DEF(AF_INET6));
}
/// Obtain an identifier for the type of the protocol.
int type() const noexcept
{
return BOOST_ASIO_OS_DEF(SOCK_RAW);
}
/// Obtain an identifier for the protocol.
int protocol() const noexcept
{
return protocol_;
}
/// Obtain an identifier for the protocol family.
int family() const noexcept
{
return family_;
}
/// The ICMP socket type.
typedef basic_raw_socket<icmp> socket;
/// The ICMP resolver type.
typedef basic_resolver<icmp> resolver;
/// Compare two protocols for equality.
friend bool operator==(const icmp& p1, const icmp& p2)
{
return p1.protocol_ == p2.protocol_ && p1.family_ == p2.family_;
}
/// Compare two protocols for inequality.
friend bool operator!=(const icmp& p1, const icmp& p2)
{
return p1.protocol_ != p2.protocol_ || p1.family_ != p2.family_;
}
private:
// Construct with a specific family.
explicit icmp(int protocol_id, int protocol_family) noexcept
: protocol_(protocol_id),
family_(protocol_family)
{
}
int protocol_;
int family_;
};
} // namespace ip
} // namespace asio
} // namespace boost
#include <boost/asio/detail/pop_options.hpp>
#endif // BOOST_ASIO_IP_ICMP_HPP
| hpp |
asio | data/projects/asio/include/boost/asio/ip/address_v4_iterator.hpp | //
// ip/address_v4_iterator.hpp
// ~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_ASIO_IP_ADDRESS_V4_ITERATOR_HPP
#define BOOST_ASIO_IP_ADDRESS_V4_ITERATOR_HPP
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include <boost/asio/detail/config.hpp>
#include <boost/asio/ip/address_v4.hpp>
#include <boost/asio/detail/push_options.hpp>
namespace boost {
namespace asio {
namespace ip {
template <typename> class basic_address_iterator;
/// An input iterator that can be used for traversing IPv4 addresses.
/**
* In addition to satisfying the input iterator requirements, this iterator
* also supports decrement.
*
* @par Thread Safety
* @e Distinct @e objects: Safe.@n
* @e Shared @e objects: Unsafe.
*/
template <> class basic_address_iterator<address_v4>
{
public:
/// The type of the elements pointed to by the iterator.
typedef address_v4 value_type;
/// Distance between two iterators.
typedef std::ptrdiff_t difference_type;
/// The type of a pointer to an element pointed to by the iterator.
typedef const address_v4* pointer;
/// The type of a reference to an element pointed to by the iterator.
typedef const address_v4& reference;
/// Denotes that the iterator satisfies the input iterator requirements.
typedef std::input_iterator_tag iterator_category;
/// Construct an iterator that points to the specified address.
basic_address_iterator(const address_v4& addr) noexcept
: address_(addr)
{
}
/// Copy constructor.
basic_address_iterator(const basic_address_iterator& other) noexcept
: address_(other.address_)
{
}
/// Move constructor.
basic_address_iterator(basic_address_iterator&& other) noexcept
: address_(static_cast<address_v4&&>(other.address_))
{
}
/// Assignment operator.
basic_address_iterator& operator=(
const basic_address_iterator& other) noexcept
{
address_ = other.address_;
return *this;
}
/// Move assignment operator.
basic_address_iterator& operator=(basic_address_iterator&& other) noexcept
{
address_ = static_cast<address_v4&&>(other.address_);
return *this;
}
/// Dereference the iterator.
const address_v4& operator*() const noexcept
{
return address_;
}
/// Dereference the iterator.
const address_v4* operator->() const noexcept
{
return &address_;
}
/// Pre-increment operator.
basic_address_iterator& operator++() noexcept
{
address_ = address_v4((address_.to_uint() + 1) & 0xFFFFFFFF);
return *this;
}
/// Post-increment operator.
basic_address_iterator operator++(int) noexcept
{
basic_address_iterator tmp(*this);
++*this;
return tmp;
}
/// Pre-decrement operator.
basic_address_iterator& operator--() noexcept
{
address_ = address_v4((address_.to_uint() - 1) & 0xFFFFFFFF);
return *this;
}
/// Post-decrement operator.
basic_address_iterator operator--(int)
{
basic_address_iterator tmp(*this);
--*this;
return tmp;
}
/// Compare two addresses for equality.
friend bool operator==(const basic_address_iterator& a,
const basic_address_iterator& b)
{
return a.address_ == b.address_;
}
/// Compare two addresses for inequality.
friend bool operator!=(const basic_address_iterator& a,
const basic_address_iterator& b)
{
return a.address_ != b.address_;
}
private:
address_v4 address_;
};
/// An input iterator that can be used for traversing IPv4 addresses.
typedef basic_address_iterator<address_v4> address_v4_iterator;
} // namespace ip
} // namespace asio
} // namespace boost
#include <boost/asio/detail/pop_options.hpp>
#endif // BOOST_ASIO_IP_ADDRESS_V4_ITERATOR_HPP
| hpp |
asio | data/projects/asio/include/boost/asio/ip/address.hpp | //
// ip/address.hpp
// ~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_ASIO_IP_ADDRESS_HPP
#define BOOST_ASIO_IP_ADDRESS_HPP
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include <boost/asio/detail/config.hpp>
#include <functional>
#include <string>
#include <boost/asio/detail/throw_exception.hpp>
#include <boost/asio/detail/string_view.hpp>
#include <boost/asio/detail/type_traits.hpp>
#include <boost/system/error_code.hpp>
#include <boost/asio/ip/address_v4.hpp>
#include <boost/asio/ip/address_v6.hpp>
#include <boost/asio/ip/bad_address_cast.hpp>
#if !defined(BOOST_ASIO_NO_IOSTREAM)
# include <iosfwd>
#endif // !defined(BOOST_ASIO_NO_IOSTREAM)
#include <boost/asio/detail/push_options.hpp>
namespace boost {
namespace asio {
namespace ip {
/// Implements version-independent IP addresses.
/**
* The boost::asio::ip::address class provides the ability to use either IP
* version 4 or version 6 addresses.
*
* @par Thread Safety
* @e Distinct @e objects: Safe.@n
* @e Shared @e objects: Unsafe.
*/
class address
{
public:
/// Default constructor.
BOOST_ASIO_DECL address() noexcept;
/// Construct an address from an IPv4 address.
BOOST_ASIO_DECL address(
const boost::asio::ip::address_v4& ipv4_address) noexcept;
/// Construct an address from an IPv6 address.
BOOST_ASIO_DECL address(
const boost::asio::ip::address_v6& ipv6_address) noexcept;
/// Copy constructor.
BOOST_ASIO_DECL address(const address& other) noexcept;
/// Move constructor.
BOOST_ASIO_DECL address(address&& other) noexcept;
/// Assign from another address.
BOOST_ASIO_DECL address& operator=(const address& other) noexcept;
/// Move-assign from another address.
BOOST_ASIO_DECL address& operator=(address&& other) noexcept;
/// Assign from an IPv4 address.
BOOST_ASIO_DECL address& operator=(
const boost::asio::ip::address_v4& ipv4_address) noexcept;
/// Assign from an IPv6 address.
BOOST_ASIO_DECL address& operator=(
const boost::asio::ip::address_v6& ipv6_address) noexcept;
/// Get whether the address is an IP version 4 address.
bool is_v4() const noexcept
{
return type_ == ipv4;
}
/// Get whether the address is an IP version 6 address.
bool is_v6() const noexcept
{
return type_ == ipv6;
}
/// Get the address as an IP version 4 address.
BOOST_ASIO_DECL boost::asio::ip::address_v4 to_v4() const;
/// Get the address as an IP version 6 address.
BOOST_ASIO_DECL boost::asio::ip::address_v6 to_v6() const;
/// Get the address as a string.
BOOST_ASIO_DECL std::string to_string() const;
#if !defined(BOOST_ASIO_NO_DEPRECATED)
/// (Deprecated: Use other overload.) Get the address as a string.
BOOST_ASIO_DECL std::string to_string(boost::system::error_code& ec) const;
/// (Deprecated: Use make_address().) Create an address from an IPv4 address
/// string in dotted decimal form, or from an IPv6 address in hexadecimal
/// notation.
static address from_string(const char* str);
/// (Deprecated: Use make_address().) Create an address from an IPv4 address
/// string in dotted decimal form, or from an IPv6 address in hexadecimal
/// notation.
static address from_string(const char* str, boost::system::error_code& ec);
/// (Deprecated: Use make_address().) Create an address from an IPv4 address
/// string in dotted decimal form, or from an IPv6 address in hexadecimal
/// notation.
static address from_string(const std::string& str);
/// (Deprecated: Use make_address().) Create an address from an IPv4 address
/// string in dotted decimal form, or from an IPv6 address in hexadecimal
/// notation.
static address from_string(
const std::string& str, boost::system::error_code& ec);
#endif // !defined(BOOST_ASIO_NO_DEPRECATED)
/// Determine whether the address is a loopback address.
BOOST_ASIO_DECL bool is_loopback() const noexcept;
/// Determine whether the address is unspecified.
BOOST_ASIO_DECL bool is_unspecified() const noexcept;
/// Determine whether the address is a multicast address.
BOOST_ASIO_DECL bool is_multicast() const noexcept;
/// Compare two addresses for equality.
BOOST_ASIO_DECL friend bool operator==(const address& a1,
const address& a2) noexcept;
/// Compare two addresses for inequality.
friend bool operator!=(const address& a1,
const address& a2) noexcept
{
return !(a1 == a2);
}
/// Compare addresses for ordering.
BOOST_ASIO_DECL friend bool operator<(const address& a1,
const address& a2) noexcept;
/// Compare addresses for ordering.
friend bool operator>(const address& a1,
const address& a2) noexcept
{
return a2 < a1;
}
/// Compare addresses for ordering.
friend bool operator<=(const address& a1,
const address& a2) noexcept
{
return !(a2 < a1);
}
/// Compare addresses for ordering.
friend bool operator>=(const address& a1,
const address& a2) noexcept
{
return !(a1 < a2);
}
private:
// The type of the address.
enum { ipv4, ipv6 } type_;
// The underlying IPv4 address.
boost::asio::ip::address_v4 ipv4_address_;
// The underlying IPv6 address.
boost::asio::ip::address_v6 ipv6_address_;
};
/// Create an address from an IPv4 address string in dotted decimal form,
/// or from an IPv6 address in hexadecimal notation.
/**
* @relates address
*/
BOOST_ASIO_DECL address make_address(const char* str);
/// Create an address from an IPv4 address string in dotted decimal form,
/// or from an IPv6 address in hexadecimal notation.
/**
* @relates address
*/
BOOST_ASIO_DECL address make_address(const char* str,
boost::system::error_code& ec) noexcept;
/// Create an address from an IPv4 address string in dotted decimal form,
/// or from an IPv6 address in hexadecimal notation.
/**
* @relates address
*/
BOOST_ASIO_DECL address make_address(const std::string& str);
/// Create an address from an IPv4 address string in dotted decimal form,
/// or from an IPv6 address in hexadecimal notation.
/**
* @relates address
*/
BOOST_ASIO_DECL address make_address(const std::string& str,
boost::system::error_code& ec) noexcept;
#if defined(BOOST_ASIO_HAS_STRING_VIEW) \
|| defined(GENERATING_DOCUMENTATION)
/// Create an address from an IPv4 address string in dotted decimal form,
/// or from an IPv6 address in hexadecimal notation.
/**
* @relates address
*/
BOOST_ASIO_DECL address make_address(string_view str);
/// Create an address from an IPv4 address string in dotted decimal form,
/// or from an IPv6 address in hexadecimal notation.
/**
* @relates address
*/
BOOST_ASIO_DECL address make_address(string_view str,
boost::system::error_code& ec) noexcept;
#endif // defined(BOOST_ASIO_HAS_STRING_VIEW)
// || defined(GENERATING_DOCUMENTATION)
#if !defined(BOOST_ASIO_NO_IOSTREAM)
/// Output an address as a string.
/**
* Used to output a human-readable string for a specified address.
*
* @param os The output stream to which the string will be written.
*
* @param addr The address to be written.
*
* @return The output stream.
*
* @relates boost::asio::ip::address
*/
template <typename Elem, typename Traits>
std::basic_ostream<Elem, Traits>& operator<<(
std::basic_ostream<Elem, Traits>& os, const address& addr);
#endif // !defined(BOOST_ASIO_NO_IOSTREAM)
} // namespace ip
} // namespace asio
} // namespace boost
namespace std {
template <>
struct hash<boost::asio::ip::address>
{
std::size_t operator()(const boost::asio::ip::address& addr)
const noexcept
{
return addr.is_v4()
? std::hash<boost::asio::ip::address_v4>()(addr.to_v4())
: std::hash<boost::asio::ip::address_v6>()(addr.to_v6());
}
};
} // namespace std
#include <boost/asio/detail/pop_options.hpp>
#include <boost/asio/ip/impl/address.hpp>
#if defined(BOOST_ASIO_HEADER_ONLY)
# include <boost/asio/ip/impl/address.ipp>
#endif // defined(BOOST_ASIO_HEADER_ONLY)
#endif // BOOST_ASIO_IP_ADDRESS_HPP
| hpp |
asio | data/projects/asio/include/boost/asio/ip/basic_resolver_entry.hpp | //
// ip/basic_resolver_entry.hpp
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_ASIO_IP_BASIC_RESOLVER_ENTRY_HPP
#define BOOST_ASIO_IP_BASIC_RESOLVER_ENTRY_HPP
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include <boost/asio/detail/config.hpp>
#include <string>
#include <boost/asio/detail/string_view.hpp>
#include <boost/asio/detail/push_options.hpp>
namespace boost {
namespace asio {
namespace ip {
/// An entry produced by a resolver.
/**
* The boost::asio::ip::basic_resolver_entry class template describes an entry
* as returned by a resolver.
*
* @par Thread Safety
* @e Distinct @e objects: Safe.@n
* @e Shared @e objects: Unsafe.
*/
template <typename InternetProtocol>
class basic_resolver_entry
{
public:
/// The protocol type associated with the endpoint entry.
typedef InternetProtocol protocol_type;
/// The endpoint type associated with the endpoint entry.
typedef typename InternetProtocol::endpoint endpoint_type;
/// Default constructor.
basic_resolver_entry()
{
}
/// Construct with specified endpoint, host name and service name.
basic_resolver_entry(const endpoint_type& ep,
BOOST_ASIO_STRING_VIEW_PARAM host, BOOST_ASIO_STRING_VIEW_PARAM service)
: endpoint_(ep),
host_name_(static_cast<std::string>(host)),
service_name_(static_cast<std::string>(service))
{
}
/// Get the endpoint associated with the entry.
endpoint_type endpoint() const
{
return endpoint_;
}
/// Convert to the endpoint associated with the entry.
operator endpoint_type() const
{
return endpoint_;
}
/// Get the host name associated with the entry.
std::string host_name() const
{
return host_name_;
}
/// Get the host name associated with the entry.
template <class Allocator>
std::basic_string<char, std::char_traits<char>, Allocator> host_name(
const Allocator& alloc = Allocator()) const
{
return std::basic_string<char, std::char_traits<char>, Allocator>(
host_name_.c_str(), alloc);
}
/// Get the service name associated with the entry.
std::string service_name() const
{
return service_name_;
}
/// Get the service name associated with the entry.
template <class Allocator>
std::basic_string<char, std::char_traits<char>, Allocator> service_name(
const Allocator& alloc = Allocator()) const
{
return std::basic_string<char, std::char_traits<char>, Allocator>(
service_name_.c_str(), alloc);
}
private:
endpoint_type endpoint_;
std::string host_name_;
std::string service_name_;
};
} // namespace ip
} // namespace asio
} // namespace boost
#include <boost/asio/detail/pop_options.hpp>
#endif // BOOST_ASIO_IP_BASIC_RESOLVER_ENTRY_HPP
| hpp |
asio | data/projects/asio/include/boost/asio/ip/multicast.hpp | //
// ip/multicast.hpp
// ~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_ASIO_IP_MULTICAST_HPP
#define BOOST_ASIO_IP_MULTICAST_HPP
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include <boost/asio/detail/config.hpp>
#include <cstddef>
#include <boost/asio/ip/detail/socket_option.hpp>
#include <boost/asio/detail/push_options.hpp>
namespace boost {
namespace asio {
namespace ip {
namespace multicast {
/// Socket option to join a multicast group on a specified interface.
/**
* Implements the IPPROTO_IP/IP_ADD_MEMBERSHIP socket option.
*
* @par Examples
* Setting the option to join a multicast group:
* @code
* boost::asio::ip::udp::socket socket(my_context);
* ...
* boost::asio::ip::address multicast_address =
* boost::asio::ip::address::from_string("225.0.0.1");
* boost::asio::ip::multicast::join_group option(multicast_address);
* socket.set_option(option);
* @endcode
*
* @par Concepts:
* SettableSocketOption.
*/
#if defined(GENERATING_DOCUMENTATION)
typedef implementation_defined join_group;
#else
typedef boost::asio::ip::detail::socket_option::multicast_request<
BOOST_ASIO_OS_DEF(IPPROTO_IP),
BOOST_ASIO_OS_DEF(IP_ADD_MEMBERSHIP),
BOOST_ASIO_OS_DEF(IPPROTO_IPV6),
BOOST_ASIO_OS_DEF(IPV6_JOIN_GROUP)> join_group;
#endif
/// Socket option to leave a multicast group on a specified interface.
/**
* Implements the IPPROTO_IP/IP_DROP_MEMBERSHIP socket option.
*
* @par Examples
* Setting the option to leave a multicast group:
* @code
* boost::asio::ip::udp::socket socket(my_context);
* ...
* boost::asio::ip::address multicast_address =
* boost::asio::ip::address::from_string("225.0.0.1");
* boost::asio::ip::multicast::leave_group option(multicast_address);
* socket.set_option(option);
* @endcode
*
* @par Concepts:
* SettableSocketOption.
*/
#if defined(GENERATING_DOCUMENTATION)
typedef implementation_defined leave_group;
#else
typedef boost::asio::ip::detail::socket_option::multicast_request<
BOOST_ASIO_OS_DEF(IPPROTO_IP),
BOOST_ASIO_OS_DEF(IP_DROP_MEMBERSHIP),
BOOST_ASIO_OS_DEF(IPPROTO_IPV6),
BOOST_ASIO_OS_DEF(IPV6_LEAVE_GROUP)> leave_group;
#endif
/// Socket option for local interface to use for outgoing multicast packets.
/**
* Implements the IPPROTO_IP/IP_MULTICAST_IF socket option.
*
* @par Examples
* Setting the option:
* @code
* boost::asio::ip::udp::socket socket(my_context);
* ...
* boost::asio::ip::address_v4 local_interface =
* boost::asio::ip::address_v4::from_string("1.2.3.4");
* boost::asio::ip::multicast::outbound_interface option(local_interface);
* socket.set_option(option);
* @endcode
*
* @par Concepts:
* SettableSocketOption.
*/
#if defined(GENERATING_DOCUMENTATION)
typedef implementation_defined outbound_interface;
#else
typedef boost::asio::ip::detail::socket_option::network_interface<
BOOST_ASIO_OS_DEF(IPPROTO_IP),
BOOST_ASIO_OS_DEF(IP_MULTICAST_IF),
BOOST_ASIO_OS_DEF(IPPROTO_IPV6),
BOOST_ASIO_OS_DEF(IPV6_MULTICAST_IF)> outbound_interface;
#endif
/// Socket option for time-to-live associated with outgoing multicast packets.
/**
* Implements the IPPROTO_IP/IP_MULTICAST_TTL socket option.
*
* @par Examples
* Setting the option:
* @code
* boost::asio::ip::udp::socket socket(my_context);
* ...
* boost::asio::ip::multicast::hops option(4);
* socket.set_option(option);
* @endcode
*
* @par
* Getting the current option value:
* @code
* boost::asio::ip::udp::socket socket(my_context);
* ...
* boost::asio::ip::multicast::hops option;
* socket.get_option(option);
* int ttl = option.value();
* @endcode
*
* @par Concepts:
* GettableSocketOption, SettableSocketOption.
*/
#if defined(GENERATING_DOCUMENTATION)
typedef implementation_defined hops;
#else
typedef boost::asio::ip::detail::socket_option::multicast_hops<
BOOST_ASIO_OS_DEF(IPPROTO_IP),
BOOST_ASIO_OS_DEF(IP_MULTICAST_TTL),
BOOST_ASIO_OS_DEF(IPPROTO_IPV6),
BOOST_ASIO_OS_DEF(IPV6_MULTICAST_HOPS)> hops;
#endif
/// Socket option determining whether outgoing multicast packets will be
/// received on the same socket if it is a member of the multicast group.
/**
* Implements the IPPROTO_IP/IP_MULTICAST_LOOP socket option.
*
* @par Examples
* Setting the option:
* @code
* boost::asio::ip::udp::socket socket(my_context);
* ...
* boost::asio::ip::multicast::enable_loopback option(true);
* socket.set_option(option);
* @endcode
*
* @par
* Getting the current option value:
* @code
* boost::asio::ip::udp::socket socket(my_context);
* ...
* boost::asio::ip::multicast::enable_loopback option;
* socket.get_option(option);
* bool is_set = option.value();
* @endcode
*
* @par Concepts:
* GettableSocketOption, SettableSocketOption.
*/
#if defined(GENERATING_DOCUMENTATION)
typedef implementation_defined enable_loopback;
#else
typedef boost::asio::ip::detail::socket_option::multicast_enable_loopback<
BOOST_ASIO_OS_DEF(IPPROTO_IP),
BOOST_ASIO_OS_DEF(IP_MULTICAST_LOOP),
BOOST_ASIO_OS_DEF(IPPROTO_IPV6),
BOOST_ASIO_OS_DEF(IPV6_MULTICAST_LOOP)> enable_loopback;
#endif
} // namespace multicast
} // namespace ip
} // namespace asio
} // namespace boost
#include <boost/asio/detail/pop_options.hpp>
#endif // BOOST_ASIO_IP_MULTICAST_HPP
| hpp |
asio | data/projects/asio/include/boost/asio/ip/address_v4.hpp | //
// ip/address_v4.hpp
// ~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_ASIO_IP_ADDRESS_V4_HPP
#define BOOST_ASIO_IP_ADDRESS_V4_HPP
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include <boost/asio/detail/config.hpp>
#include <functional>
#include <string>
#include <boost/asio/detail/array.hpp>
#include <boost/asio/detail/cstdint.hpp>
#include <boost/asio/detail/socket_types.hpp>
#include <boost/asio/detail/string_view.hpp>
#include <boost/asio/detail/winsock_init.hpp>
#include <boost/system/error_code.hpp>
#if !defined(BOOST_ASIO_NO_IOSTREAM)
# include <iosfwd>
#endif // !defined(BOOST_ASIO_NO_IOSTREAM)
#include <boost/asio/detail/push_options.hpp>
namespace boost {
namespace asio {
namespace ip {
/// Implements IP version 4 style addresses.
/**
* The boost::asio::ip::address_v4 class provides the ability to use and
* manipulate IP version 4 addresses.
*
* @par Thread Safety
* @e Distinct @e objects: Safe.@n
* @e Shared @e objects: Unsafe.
*/
class address_v4
{
public:
/// The type used to represent an address as an unsigned integer.
typedef uint_least32_t uint_type;
/// The type used to represent an address as an array of bytes.
/**
* @note This type is defined in terms of the C++0x template @c std::array
* when it is available. Otherwise, it uses @c boost:array.
*/
#if defined(GENERATING_DOCUMENTATION)
typedef array<unsigned char, 4> bytes_type;
#else
typedef boost::asio::detail::array<unsigned char, 4> bytes_type;
#endif
/// Default constructor.
/**
* Initialises the @c address_v4 object such that:
* @li <tt>to_bytes()</tt> yields <tt>{0, 0, 0, 0}</tt>; and
* @li <tt>to_uint() == 0</tt>.
*/
address_v4() noexcept
{
addr_.s_addr = 0;
}
/// Construct an address from raw bytes.
/**
* Initialises the @c address_v4 object such that <tt>to_bytes() ==
* bytes</tt>.
*
* @throws out_of_range Thrown if any element in @c bytes is not in the range
* <tt>0 - 0xFF</tt>. Note that no range checking is required for platforms
* where <tt>std::numeric_limits<unsigned char>::max()</tt> is <tt>0xFF</tt>.
*/
BOOST_ASIO_DECL explicit address_v4(const bytes_type& bytes);
/// Construct an address from an unsigned integer in host byte order.
/**
* Initialises the @c address_v4 object such that <tt>to_uint() == addr</tt>.
*/
BOOST_ASIO_DECL explicit address_v4(uint_type addr);
/// Copy constructor.
address_v4(const address_v4& other) noexcept
: addr_(other.addr_)
{
}
/// Move constructor.
address_v4(address_v4&& other) noexcept
: addr_(other.addr_)
{
}
/// Assign from another address.
address_v4& operator=(const address_v4& other) noexcept
{
addr_ = other.addr_;
return *this;
}
/// Move-assign from another address.
address_v4& operator=(address_v4&& other) noexcept
{
addr_ = other.addr_;
return *this;
}
/// Get the address in bytes, in network byte order.
BOOST_ASIO_DECL bytes_type to_bytes() const noexcept;
/// Get the address as an unsigned integer in host byte order.
BOOST_ASIO_DECL uint_type to_uint() const noexcept;
#if !defined(BOOST_ASIO_NO_DEPRECATED)
/// (Deprecated: Use to_uint().) Get the address as an unsigned long in host
/// byte order.
BOOST_ASIO_DECL unsigned long to_ulong() const;
#endif // !defined(BOOST_ASIO_NO_DEPRECATED)
/// Get the address as a string in dotted decimal format.
BOOST_ASIO_DECL std::string to_string() const;
#if !defined(BOOST_ASIO_NO_DEPRECATED)
/// (Deprecated: Use other overload.) Get the address as a string in dotted
/// decimal format.
BOOST_ASIO_DECL std::string to_string(boost::system::error_code& ec) const;
/// (Deprecated: Use make_address_v4().) Create an address from an IP address
/// string in dotted decimal form.
static address_v4 from_string(const char* str);
/// (Deprecated: Use make_address_v4().) Create an address from an IP address
/// string in dotted decimal form.
static address_v4 from_string(
const char* str, boost::system::error_code& ec);
/// (Deprecated: Use make_address_v4().) Create an address from an IP address
/// string in dotted decimal form.
static address_v4 from_string(const std::string& str);
/// (Deprecated: Use make_address_v4().) Create an address from an IP address
/// string in dotted decimal form.
static address_v4 from_string(
const std::string& str, boost::system::error_code& ec);
#endif // !defined(BOOST_ASIO_NO_DEPRECATED)
/// Determine whether the address is a loopback address.
/**
* This function tests whether the address is in the address block
* <tt>127.0.0.0/8</tt>, which corresponds to the address range
* <tt>127.0.0.0 - 127.255.255.255</tt>.
*
* @returns <tt>(to_uint() & 0xFF000000) == 0x7F000000</tt>.
*/
BOOST_ASIO_DECL bool is_loopback() const noexcept;
/// Determine whether the address is unspecified.
/**
* This function tests whether the address is the unspecified address
* <tt>0.0.0.0</tt>.
*
* @returns <tt>to_uint() == 0</tt>.
*/
BOOST_ASIO_DECL bool is_unspecified() const noexcept;
#if !defined(BOOST_ASIO_NO_DEPRECATED)
/// (Deprecated: Use network_v4 class.) Determine whether the address is a
/// class A address.
BOOST_ASIO_DECL bool is_class_a() const;
/// (Deprecated: Use network_v4 class.) Determine whether the address is a
/// class B address.
BOOST_ASIO_DECL bool is_class_b() const;
/// (Deprecated: Use network_v4 class.) Determine whether the address is a
/// class C address.
BOOST_ASIO_DECL bool is_class_c() const;
#endif // !defined(BOOST_ASIO_NO_DEPRECATED)
/// Determine whether the address is a multicast address.
/**
* This function tests whether the address is in the multicast address block
* <tt>224.0.0.0/4</tt>, which corresponds to the address range
* <tt>224.0.0.0 - 239.255.255.255</tt>.
*
* @returns <tt>(to_uint() & 0xF0000000) == 0xE0000000</tt>.
*/
BOOST_ASIO_DECL bool is_multicast() const noexcept;
/// Compare two addresses for equality.
friend bool operator==(const address_v4& a1,
const address_v4& a2) noexcept
{
return a1.addr_.s_addr == a2.addr_.s_addr;
}
/// Compare two addresses for inequality.
friend bool operator!=(const address_v4& a1,
const address_v4& a2) noexcept
{
return a1.addr_.s_addr != a2.addr_.s_addr;
}
/// Compare addresses for ordering.
/**
* Compares two addresses in host byte order.
*
* @returns <tt>a1.to_uint() < a2.to_uint()</tt>.
*/
friend bool operator<(const address_v4& a1,
const address_v4& a2) noexcept
{
return a1.to_uint() < a2.to_uint();
}
/// Compare addresses for ordering.
/**
* Compares two addresses in host byte order.
*
* @returns <tt>a1.to_uint() > a2.to_uint()</tt>.
*/
friend bool operator>(const address_v4& a1,
const address_v4& a2) noexcept
{
return a1.to_uint() > a2.to_uint();
}
/// Compare addresses for ordering.
/**
* Compares two addresses in host byte order.
*
* @returns <tt>a1.to_uint() <= a2.to_uint()</tt>.
*/
friend bool operator<=(const address_v4& a1,
const address_v4& a2) noexcept
{
return a1.to_uint() <= a2.to_uint();
}
/// Compare addresses for ordering.
/**
* Compares two addresses in host byte order.
*
* @returns <tt>a1.to_uint() >= a2.to_uint()</tt>.
*/
friend bool operator>=(const address_v4& a1,
const address_v4& a2) noexcept
{
return a1.to_uint() >= a2.to_uint();
}
/// Obtain an address object that represents any address.
/**
* This functions returns an address that represents the "any" address
* <tt>0.0.0.0</tt>.
*
* @returns A default-constructed @c address_v4 object.
*/
static address_v4 any() noexcept
{
return address_v4();
}
/// Obtain an address object that represents the loopback address.
/**
* This function returns an address that represents the well-known loopback
* address <tt>127.0.0.1</tt>.
*
* @returns <tt>address_v4(0x7F000001)</tt>.
*/
static address_v4 loopback() noexcept
{
return address_v4(0x7F000001);
}
/// Obtain an address object that represents the broadcast address.
/**
* This function returns an address that represents the broadcast address
* <tt>255.255.255.255</tt>.
*
* @returns <tt>address_v4(0xFFFFFFFF)</tt>.
*/
static address_v4 broadcast() noexcept
{
return address_v4(0xFFFFFFFF);
}
#if !defined(BOOST_ASIO_NO_DEPRECATED)
/// (Deprecated: Use network_v4 class.) Obtain an address object that
/// represents the broadcast address that corresponds to the specified
/// address and netmask.
BOOST_ASIO_DECL static address_v4 broadcast(
const address_v4& addr, const address_v4& mask);
/// (Deprecated: Use network_v4 class.) Obtain the netmask that corresponds
/// to the address, based on its address class.
BOOST_ASIO_DECL static address_v4 netmask(const address_v4& addr);
#endif // !defined(BOOST_ASIO_NO_DEPRECATED)
private:
// The underlying IPv4 address.
boost::asio::detail::in4_addr_type addr_;
};
/// Create an IPv4 address from raw bytes in network order.
/**
* @relates address_v4
*/
inline address_v4 make_address_v4(const address_v4::bytes_type& bytes)
{
return address_v4(bytes);
}
/// Create an IPv4 address from an unsigned integer in host byte order.
/**
* @relates address_v4
*/
inline address_v4 make_address_v4(address_v4::uint_type addr)
{
return address_v4(addr);
}
/// Create an IPv4 address from an IP address string in dotted decimal form.
/**
* @relates address_v4
*/
BOOST_ASIO_DECL address_v4 make_address_v4(const char* str);
/// Create an IPv4 address from an IP address string in dotted decimal form.
/**
* @relates address_v4
*/
BOOST_ASIO_DECL address_v4 make_address_v4(const char* str,
boost::system::error_code& ec) noexcept;
/// Create an IPv4 address from an IP address string in dotted decimal form.
/**
* @relates address_v4
*/
BOOST_ASIO_DECL address_v4 make_address_v4(const std::string& str);
/// Create an IPv4 address from an IP address string in dotted decimal form.
/**
* @relates address_v4
*/
BOOST_ASIO_DECL address_v4 make_address_v4(const std::string& str,
boost::system::error_code& ec) noexcept;
#if defined(BOOST_ASIO_HAS_STRING_VIEW) \
|| defined(GENERATING_DOCUMENTATION)
/// Create an IPv4 address from an IP address string in dotted decimal form.
/**
* @relates address_v4
*/
BOOST_ASIO_DECL address_v4 make_address_v4(string_view str);
/// Create an IPv4 address from an IP address string in dotted decimal form.
/**
* @relates address_v4
*/
BOOST_ASIO_DECL address_v4 make_address_v4(string_view str,
boost::system::error_code& ec) noexcept;
#endif // defined(BOOST_ASIO_HAS_STRING_VIEW)
// || defined(GENERATING_DOCUMENTATION)
#if !defined(BOOST_ASIO_NO_IOSTREAM)
/// Output an address as a string.
/**
* Used to output a human-readable string for a specified address.
*
* @param os The output stream to which the string will be written.
*
* @param addr The address to be written.
*
* @return The output stream.
*
* @relates boost::asio::ip::address_v4
*/
template <typename Elem, typename Traits>
std::basic_ostream<Elem, Traits>& operator<<(
std::basic_ostream<Elem, Traits>& os, const address_v4& addr);
#endif // !defined(BOOST_ASIO_NO_IOSTREAM)
} // namespace ip
} // namespace asio
} // namespace boost
namespace std {
template <>
struct hash<boost::asio::ip::address_v4>
{
std::size_t operator()(const boost::asio::ip::address_v4& addr)
const noexcept
{
return std::hash<unsigned int>()(addr.to_uint());
}
};
} // namespace std
#include <boost/asio/detail/pop_options.hpp>
#include <boost/asio/ip/impl/address_v4.hpp>
#if defined(BOOST_ASIO_HEADER_ONLY)
# include <boost/asio/ip/impl/address_v4.ipp>
#endif // defined(BOOST_ASIO_HEADER_ONLY)
#endif // BOOST_ASIO_IP_ADDRESS_V4_HPP
| hpp |
asio | data/projects/asio/include/boost/asio/ip/basic_endpoint.hpp | //
// ip/basic_endpoint.hpp
// ~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_ASIO_IP_BASIC_ENDPOINT_HPP
#define BOOST_ASIO_IP_BASIC_ENDPOINT_HPP
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include <boost/asio/detail/config.hpp>
#include <functional>
#include <boost/asio/detail/cstdint.hpp>
#include <boost/asio/ip/address.hpp>
#include <boost/asio/ip/detail/endpoint.hpp>
#if !defined(BOOST_ASIO_NO_IOSTREAM)
# include <iosfwd>
#endif // !defined(BOOST_ASIO_NO_IOSTREAM)
#include <boost/asio/detail/push_options.hpp>
namespace boost {
namespace asio {
namespace ip {
/// Type used for storing port numbers.
typedef uint_least16_t port_type;
/// Describes an endpoint for a version-independent IP socket.
/**
* The boost::asio::ip::basic_endpoint class template describes an endpoint that
* may be associated with a particular socket.
*
* @par Thread Safety
* @e Distinct @e objects: Safe.@n
* @e Shared @e objects: Unsafe.
*
* @par Concepts:
* Endpoint.
*/
template <typename InternetProtocol>
class basic_endpoint
{
public:
/// The protocol type associated with the endpoint.
typedef InternetProtocol protocol_type;
/// The type of the endpoint structure. This type is dependent on the
/// underlying implementation of the socket layer.
#if defined(GENERATING_DOCUMENTATION)
typedef implementation_defined data_type;
#else
typedef boost::asio::detail::socket_addr_type data_type;
#endif
/// Default constructor.
basic_endpoint() noexcept
: impl_()
{
}
/// Construct an endpoint using a port number, specified in the host's byte
/// order. The IP address will be the any address (i.e. INADDR_ANY or
/// in6addr_any). This constructor would typically be used for accepting new
/// connections.
/**
* @par Examples
* To initialise an IPv4 TCP endpoint for port 1234, use:
* @code
* boost::asio::ip::tcp::endpoint ep(boost::asio::ip::tcp::v4(), 1234);
* @endcode
*
* To specify an IPv6 UDP endpoint for port 9876, use:
* @code
* boost::asio::ip::udp::endpoint ep(boost::asio::ip::udp::v6(), 9876);
* @endcode
*/
basic_endpoint(const InternetProtocol& internet_protocol,
port_type port_num) noexcept
: impl_(internet_protocol.family(), port_num)
{
}
/// Construct an endpoint using a port number and an IP address. This
/// constructor may be used for accepting connections on a specific interface
/// or for making a connection to a remote endpoint.
basic_endpoint(const boost::asio::ip::address& addr,
port_type port_num) noexcept
: impl_(addr, port_num)
{
}
/// Copy constructor.
basic_endpoint(const basic_endpoint& other) noexcept
: impl_(other.impl_)
{
}
/// Move constructor.
basic_endpoint(basic_endpoint&& other) noexcept
: impl_(other.impl_)
{
}
/// Assign from another endpoint.
basic_endpoint& operator=(const basic_endpoint& other) noexcept
{
impl_ = other.impl_;
return *this;
}
/// Move-assign from another endpoint.
basic_endpoint& operator=(basic_endpoint&& other) noexcept
{
impl_ = other.impl_;
return *this;
}
/// The protocol associated with the endpoint.
protocol_type protocol() const noexcept
{
if (impl_.is_v4())
return InternetProtocol::v4();
return InternetProtocol::v6();
}
/// Get the underlying endpoint in the native type.
data_type* data() noexcept
{
return impl_.data();
}
/// Get the underlying endpoint in the native type.
const data_type* data() const noexcept
{
return impl_.data();
}
/// Get the underlying size of the endpoint in the native type.
std::size_t size() const noexcept
{
return impl_.size();
}
/// Set the underlying size of the endpoint in the native type.
void resize(std::size_t new_size)
{
impl_.resize(new_size);
}
/// Get the capacity of the endpoint in the native type.
std::size_t capacity() const noexcept
{
return impl_.capacity();
}
/// Get the port associated with the endpoint. The port number is always in
/// the host's byte order.
port_type port() const noexcept
{
return impl_.port();
}
/// Set the port associated with the endpoint. The port number is always in
/// the host's byte order.
void port(port_type port_num) noexcept
{
impl_.port(port_num);
}
/// Get the IP address associated with the endpoint.
boost::asio::ip::address address() const noexcept
{
return impl_.address();
}
/// Set the IP address associated with the endpoint.
void address(const boost::asio::ip::address& addr) noexcept
{
impl_.address(addr);
}
/// Compare two endpoints for equality.
friend bool operator==(const basic_endpoint<InternetProtocol>& e1,
const basic_endpoint<InternetProtocol>& e2) noexcept
{
return e1.impl_ == e2.impl_;
}
/// Compare two endpoints for inequality.
friend bool operator!=(const basic_endpoint<InternetProtocol>& e1,
const basic_endpoint<InternetProtocol>& e2) noexcept
{
return !(e1 == e2);
}
/// Compare endpoints for ordering.
friend bool operator<(const basic_endpoint<InternetProtocol>& e1,
const basic_endpoint<InternetProtocol>& e2) noexcept
{
return e1.impl_ < e2.impl_;
}
/// Compare endpoints for ordering.
friend bool operator>(const basic_endpoint<InternetProtocol>& e1,
const basic_endpoint<InternetProtocol>& e2) noexcept
{
return e2.impl_ < e1.impl_;
}
/// Compare endpoints for ordering.
friend bool operator<=(const basic_endpoint<InternetProtocol>& e1,
const basic_endpoint<InternetProtocol>& e2) noexcept
{
return !(e2 < e1);
}
/// Compare endpoints for ordering.
friend bool operator>=(const basic_endpoint<InternetProtocol>& e1,
const basic_endpoint<InternetProtocol>& e2) noexcept
{
return !(e1 < e2);
}
private:
// The underlying IP endpoint.
boost::asio::ip::detail::endpoint impl_;
};
#if !defined(BOOST_ASIO_NO_IOSTREAM)
/// Output an endpoint as a string.
/**
* Used to output a human-readable string for a specified endpoint.
*
* @param os The output stream to which the string will be written.
*
* @param endpoint The endpoint to be written.
*
* @return The output stream.
*
* @relates boost::asio::ip::basic_endpoint
*/
template <typename Elem, typename Traits, typename InternetProtocol>
std::basic_ostream<Elem, Traits>& operator<<(
std::basic_ostream<Elem, Traits>& os,
const basic_endpoint<InternetProtocol>& endpoint);
#endif // !defined(BOOST_ASIO_NO_IOSTREAM)
} // namespace ip
} // namespace asio
} // namespace boost
namespace std {
template <typename InternetProtocol>
struct hash<boost::asio::ip::basic_endpoint<InternetProtocol>>
{
std::size_t operator()(
const boost::asio::ip::basic_endpoint<InternetProtocol>& ep)
const noexcept
{
std::size_t hash1 = std::hash<boost::asio::ip::address>()(ep.address());
std::size_t hash2 = std::hash<unsigned short>()(ep.port());
return hash1 ^ (hash2 + 0x9e3779b9 + (hash1 << 6) + (hash1 >> 2));
}
};
} // namespace std
#include <boost/asio/detail/pop_options.hpp>
#include <boost/asio/ip/impl/basic_endpoint.hpp>
#endif // BOOST_ASIO_IP_BASIC_ENDPOINT_HPP
| hpp |
asio | data/projects/asio/include/boost/asio/ip/basic_resolver_iterator.hpp | //
// ip/basic_resolver_iterator.hpp
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_ASIO_IP_BASIC_RESOLVER_ITERATOR_HPP
#define BOOST_ASIO_IP_BASIC_RESOLVER_ITERATOR_HPP
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include <boost/asio/detail/config.hpp>
#include <cstddef>
#include <cstring>
#include <iterator>
#include <string>
#include <vector>
#include <boost/asio/detail/memory.hpp>
#include <boost/asio/detail/socket_ops.hpp>
#include <boost/asio/detail/socket_types.hpp>
#include <boost/asio/ip/basic_resolver_entry.hpp>
#if defined(BOOST_ASIO_WINDOWS_RUNTIME)
# include <boost/asio/detail/winrt_utils.hpp>
#endif // defined(BOOST_ASIO_WINDOWS_RUNTIME)
#include <boost/asio/detail/push_options.hpp>
namespace boost {
namespace asio {
namespace ip {
/// An iterator over the entries produced by a resolver.
/**
* The boost::asio::ip::basic_resolver_iterator class template is used to define
* iterators over the results returned by a resolver.
*
* The iterator's value_type, obtained when the iterator is dereferenced, is:
* @code const basic_resolver_entry<InternetProtocol> @endcode
*
* @par Thread Safety
* @e Distinct @e objects: Safe.@n
* @e Shared @e objects: Unsafe.
*/
template <typename InternetProtocol>
class basic_resolver_iterator
{
public:
/// The type used for the distance between two iterators.
typedef std::ptrdiff_t difference_type;
/// The type of the value pointed to by the iterator.
typedef basic_resolver_entry<InternetProtocol> value_type;
/// The type of the result of applying operator->() to the iterator.
typedef const basic_resolver_entry<InternetProtocol>* pointer;
/// The type of the result of applying operator*() to the iterator.
typedef const basic_resolver_entry<InternetProtocol>& reference;
/// The iterator category.
typedef std::forward_iterator_tag iterator_category;
/// Default constructor creates an end iterator.
basic_resolver_iterator()
: index_(0)
{
}
/// Copy constructor.
basic_resolver_iterator(const basic_resolver_iterator& other)
: values_(other.values_),
index_(other.index_)
{
}
/// Move constructor.
basic_resolver_iterator(basic_resolver_iterator&& other)
: values_(static_cast<values_ptr_type&&>(other.values_)),
index_(other.index_)
{
other.index_ = 0;
}
/// Assignment operator.
basic_resolver_iterator& operator=(const basic_resolver_iterator& other)
{
values_ = other.values_;
index_ = other.index_;
return *this;
}
/// Move-assignment operator.
basic_resolver_iterator& operator=(basic_resolver_iterator&& other)
{
if (this != &other)
{
values_ = static_cast<values_ptr_type&&>(other.values_);
index_ = other.index_;
other.index_ = 0;
}
return *this;
}
/// Dereference an iterator.
const basic_resolver_entry<InternetProtocol>& operator*() const
{
return dereference();
}
/// Dereference an iterator.
const basic_resolver_entry<InternetProtocol>* operator->() const
{
return &dereference();
}
/// Increment operator (prefix).
basic_resolver_iterator& operator++()
{
increment();
return *this;
}
/// Increment operator (postfix).
basic_resolver_iterator operator++(int)
{
basic_resolver_iterator tmp(*this);
++*this;
return tmp;
}
/// Test two iterators for equality.
friend bool operator==(const basic_resolver_iterator& a,
const basic_resolver_iterator& b)
{
return a.equal(b);
}
/// Test two iterators for inequality.
friend bool operator!=(const basic_resolver_iterator& a,
const basic_resolver_iterator& b)
{
return !a.equal(b);
}
protected:
void increment()
{
if (++index_ == values_->size())
{
// Reset state to match a default constructed end iterator.
values_.reset();
index_ = 0;
}
}
bool equal(const basic_resolver_iterator& other) const
{
if (!values_ && !other.values_)
return true;
if (values_ != other.values_)
return false;
return index_ == other.index_;
}
const basic_resolver_entry<InternetProtocol>& dereference() const
{
return (*values_)[index_];
}
typedef std::vector<basic_resolver_entry<InternetProtocol>> values_type;
typedef boost::asio::detail::shared_ptr<values_type> values_ptr_type;
values_ptr_type values_;
std::size_t index_;
};
} // namespace ip
} // namespace asio
} // namespace boost
#include <boost/asio/detail/pop_options.hpp>
#endif // BOOST_ASIO_IP_BASIC_RESOLVER_ITERATOR_HPP
| hpp |
asio | data/projects/asio/include/boost/asio/ip/address_v4_range.hpp | //
// ip/address_v4_range.hpp
// ~~~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_ASIO_IP_ADDRESS_V4_RANGE_HPP
#define BOOST_ASIO_IP_ADDRESS_V4_RANGE_HPP
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include <boost/asio/detail/config.hpp>
#include <boost/asio/ip/address_v4_iterator.hpp>
#include <boost/asio/detail/push_options.hpp>
namespace boost {
namespace asio {
namespace ip {
template <typename> class basic_address_range;
/// Represents a range of IPv4 addresses.
/**
* @par Thread Safety
* @e Distinct @e objects: Safe.@n
* @e Shared @e objects: Unsafe.
*/
template <> class basic_address_range<address_v4>
{
public:
/// The type of an iterator that points into the range.
typedef basic_address_iterator<address_v4> iterator;
/// Construct an empty range.
basic_address_range() noexcept
: begin_(address_v4()),
end_(address_v4())
{
}
/// Construct an range that represents the given range of addresses.
explicit basic_address_range(const iterator& first,
const iterator& last) noexcept
: begin_(first),
end_(last)
{
}
/// Copy constructor.
basic_address_range(const basic_address_range& other) noexcept
: begin_(other.begin_),
end_(other.end_)
{
}
/// Move constructor.
basic_address_range(basic_address_range&& other) noexcept
: begin_(static_cast<iterator&&>(other.begin_)),
end_(static_cast<iterator&&>(other.end_))
{
}
/// Assignment operator.
basic_address_range& operator=(const basic_address_range& other) noexcept
{
begin_ = other.begin_;
end_ = other.end_;
return *this;
}
/// Move assignment operator.
basic_address_range& operator=(basic_address_range&& other) noexcept
{
begin_ = static_cast<iterator&&>(other.begin_);
end_ = static_cast<iterator&&>(other.end_);
return *this;
}
/// Obtain an iterator that points to the start of the range.
iterator begin() const noexcept
{
return begin_;
}
/// Obtain an iterator that points to the end of the range.
iterator end() const noexcept
{
return end_;
}
/// Determine whether the range is empty.
bool empty() const noexcept
{
return size() == 0;
}
/// Return the size of the range.
std::size_t size() const noexcept
{
return end_->to_uint() - begin_->to_uint();
}
/// Find an address in the range.
iterator find(const address_v4& addr) const noexcept
{
return addr >= *begin_ && addr < *end_ ? iterator(addr) : end_;
}
private:
iterator begin_;
iterator end_;
};
/// Represents a range of IPv4 addresses.
typedef basic_address_range<address_v4> address_v4_range;
} // namespace ip
} // namespace asio
} // namespace boost
#include <boost/asio/detail/pop_options.hpp>
#endif // BOOST_ASIO_IP_ADDRESS_V4_RANGE_HPP
| hpp |
asio | data/projects/asio/include/boost/asio/ip/udp.hpp | //
// ip/udp.hpp
// ~~~~~~~~~~
//
// Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_ASIO_IP_UDP_HPP
#define BOOST_ASIO_IP_UDP_HPP
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include <boost/asio/detail/config.hpp>
#include <boost/asio/basic_datagram_socket.hpp>
#include <boost/asio/detail/socket_types.hpp>
#include <boost/asio/ip/basic_endpoint.hpp>
#include <boost/asio/ip/basic_resolver.hpp>
#include <boost/asio/ip/basic_resolver_iterator.hpp>
#include <boost/asio/ip/basic_resolver_query.hpp>
#include <boost/asio/detail/push_options.hpp>
namespace boost {
namespace asio {
namespace ip {
/// Encapsulates the flags needed for UDP.
/**
* The boost::asio::ip::udp class contains flags necessary for UDP sockets.
*
* @par Thread Safety
* @e Distinct @e objects: Safe.@n
* @e Shared @e objects: Safe.
*
* @par Concepts:
* Protocol, InternetProtocol.
*/
class udp
{
public:
/// The type of a UDP endpoint.
typedef basic_endpoint<udp> endpoint;
/// Construct to represent the IPv4 UDP protocol.
static udp v4() noexcept
{
return udp(BOOST_ASIO_OS_DEF(AF_INET));
}
/// Construct to represent the IPv6 UDP protocol.
static udp v6() noexcept
{
return udp(BOOST_ASIO_OS_DEF(AF_INET6));
}
/// Obtain an identifier for the type of the protocol.
int type() const noexcept
{
return BOOST_ASIO_OS_DEF(SOCK_DGRAM);
}
/// Obtain an identifier for the protocol.
int protocol() const noexcept
{
return BOOST_ASIO_OS_DEF(IPPROTO_UDP);
}
/// Obtain an identifier for the protocol family.
int family() const noexcept
{
return family_;
}
/// The UDP socket type.
typedef basic_datagram_socket<udp> socket;
/// The UDP resolver type.
typedef basic_resolver<udp> resolver;
/// Compare two protocols for equality.
friend bool operator==(const udp& p1, const udp& p2)
{
return p1.family_ == p2.family_;
}
/// Compare two protocols for inequality.
friend bool operator!=(const udp& p1, const udp& p2)
{
return p1.family_ != p2.family_;
}
private:
// Construct with a specific family.
explicit udp(int protocol_family) noexcept
: family_(protocol_family)
{
}
int family_;
};
} // namespace ip
} // namespace asio
} // namespace boost
#include <boost/asio/detail/pop_options.hpp>
#endif // BOOST_ASIO_IP_UDP_HPP
| hpp |
asio | data/projects/asio/include/boost/asio/ip/tcp.hpp | //
// ip/tcp.hpp
// ~~~~~~~~~~
//
// Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_ASIO_IP_TCP_HPP
#define BOOST_ASIO_IP_TCP_HPP
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include <boost/asio/detail/config.hpp>
#include <boost/asio/basic_socket_acceptor.hpp>
#include <boost/asio/basic_socket_iostream.hpp>
#include <boost/asio/basic_stream_socket.hpp>
#include <boost/asio/detail/socket_option.hpp>
#include <boost/asio/detail/socket_types.hpp>
#include <boost/asio/ip/basic_endpoint.hpp>
#include <boost/asio/ip/basic_resolver.hpp>
#include <boost/asio/ip/basic_resolver_iterator.hpp>
#include <boost/asio/ip/basic_resolver_query.hpp>
#include <boost/asio/detail/push_options.hpp>
namespace boost {
namespace asio {
namespace ip {
/// Encapsulates the flags needed for TCP.
/**
* The boost::asio::ip::tcp class contains flags necessary for TCP sockets.
*
* @par Thread Safety
* @e Distinct @e objects: Safe.@n
* @e Shared @e objects: Safe.
*
* @par Concepts:
* Protocol, InternetProtocol.
*/
class tcp
{
public:
/// The type of a TCP endpoint.
typedef basic_endpoint<tcp> endpoint;
/// Construct to represent the IPv4 TCP protocol.
static tcp v4() noexcept
{
return tcp(BOOST_ASIO_OS_DEF(AF_INET));
}
/// Construct to represent the IPv6 TCP protocol.
static tcp v6() noexcept
{
return tcp(BOOST_ASIO_OS_DEF(AF_INET6));
}
/// Obtain an identifier for the type of the protocol.
int type() const noexcept
{
return BOOST_ASIO_OS_DEF(SOCK_STREAM);
}
/// Obtain an identifier for the protocol.
int protocol() const noexcept
{
return BOOST_ASIO_OS_DEF(IPPROTO_TCP);
}
/// Obtain an identifier for the protocol family.
int family() const noexcept
{
return family_;
}
/// The TCP socket type.
typedef basic_stream_socket<tcp> socket;
/// The TCP acceptor type.
typedef basic_socket_acceptor<tcp> acceptor;
/// The TCP resolver type.
typedef basic_resolver<tcp> resolver;
#if !defined(BOOST_ASIO_NO_IOSTREAM)
/// The TCP iostream type.
typedef basic_socket_iostream<tcp> iostream;
#endif // !defined(BOOST_ASIO_NO_IOSTREAM)
/// Socket option for disabling the Nagle algorithm.
/**
* Implements the IPPROTO_TCP/TCP_NODELAY socket option.
*
* @par Examples
* Setting the option:
* @code
* boost::asio::ip::tcp::socket socket(my_context);
* ...
* boost::asio::ip::tcp::no_delay option(true);
* socket.set_option(option);
* @endcode
*
* @par
* Getting the current option value:
* @code
* boost::asio::ip::tcp::socket socket(my_context);
* ...
* boost::asio::ip::tcp::no_delay option;
* socket.get_option(option);
* bool is_set = option.value();
* @endcode
*
* @par Concepts:
* Socket_Option, Boolean_Socket_Option.
*/
#if defined(GENERATING_DOCUMENTATION)
typedef implementation_defined no_delay;
#else
typedef boost::asio::detail::socket_option::boolean<
BOOST_ASIO_OS_DEF(IPPROTO_TCP), BOOST_ASIO_OS_DEF(TCP_NODELAY)> no_delay;
#endif
/// Compare two protocols for equality.
friend bool operator==(const tcp& p1, const tcp& p2)
{
return p1.family_ == p2.family_;
}
/// Compare two protocols for inequality.
friend bool operator!=(const tcp& p1, const tcp& p2)
{
return p1.family_ != p2.family_;
}
private:
// Construct with a specific family.
explicit tcp(int protocol_family) noexcept
: family_(protocol_family)
{
}
int family_;
};
} // namespace ip
} // namespace asio
} // namespace boost
#include <boost/asio/detail/pop_options.hpp>
#endif // BOOST_ASIO_IP_TCP_HPP
| hpp |
asio | data/projects/asio/include/boost/asio/ip/address_v6_iterator.hpp | //
// ip/address_v6_iterator.hpp
// ~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com)
// Oliver Kowalke (oliver dot kowalke at gmail dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_ASIO_IP_ADDRESS_V6_ITERATOR_HPP
#define BOOST_ASIO_IP_ADDRESS_V6_ITERATOR_HPP
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include <boost/asio/detail/config.hpp>
#include <boost/asio/ip/address_v6.hpp>
#include <boost/asio/detail/push_options.hpp>
namespace boost {
namespace asio {
namespace ip {
template <typename> class basic_address_iterator;
/// An input iterator that can be used for traversing IPv6 addresses.
/**
* In addition to satisfying the input iterator requirements, this iterator
* also supports decrement.
*
* @par Thread Safety
* @e Distinct @e objects: Safe.@n
* @e Shared @e objects: Unsafe.
*/
template <> class basic_address_iterator<address_v6>
{
public:
/// The type of the elements pointed to by the iterator.
typedef address_v6 value_type;
/// Distance between two iterators.
typedef std::ptrdiff_t difference_type;
/// The type of a pointer to an element pointed to by the iterator.
typedef const address_v6* pointer;
/// The type of a reference to an element pointed to by the iterator.
typedef const address_v6& reference;
/// Denotes that the iterator satisfies the input iterator requirements.
typedef std::input_iterator_tag iterator_category;
/// Construct an iterator that points to the specified address.
basic_address_iterator(const address_v6& addr) noexcept
: address_(addr)
{
}
/// Copy constructor.
basic_address_iterator(
const basic_address_iterator& other) noexcept
: address_(other.address_)
{
}
/// Move constructor.
basic_address_iterator(basic_address_iterator&& other) noexcept
: address_(static_cast<address_v6&&>(other.address_))
{
}
/// Assignment operator.
basic_address_iterator& operator=(
const basic_address_iterator& other) noexcept
{
address_ = other.address_;
return *this;
}
/// Move assignment operator.
basic_address_iterator& operator=(basic_address_iterator&& other) noexcept
{
address_ = static_cast<address_v6&&>(other.address_);
return *this;
}
/// Dereference the iterator.
const address_v6& operator*() const noexcept
{
return address_;
}
/// Dereference the iterator.
const address_v6* operator->() const noexcept
{
return &address_;
}
/// Pre-increment operator.
basic_address_iterator& operator++() noexcept
{
for (int i = 15; i >= 0; --i)
{
if (address_.addr_.s6_addr[i] < 0xFF)
{
++address_.addr_.s6_addr[i];
break;
}
address_.addr_.s6_addr[i] = 0;
}
return *this;
}
/// Post-increment operator.
basic_address_iterator operator++(int) noexcept
{
basic_address_iterator tmp(*this);
++*this;
return tmp;
}
/// Pre-decrement operator.
basic_address_iterator& operator--() noexcept
{
for (int i = 15; i >= 0; --i)
{
if (address_.addr_.s6_addr[i] > 0)
{
--address_.addr_.s6_addr[i];
break;
}
address_.addr_.s6_addr[i] = 0xFF;
}
return *this;
}
/// Post-decrement operator.
basic_address_iterator operator--(int)
{
basic_address_iterator tmp(*this);
--*this;
return tmp;
}
/// Compare two addresses for equality.
friend bool operator==(const basic_address_iterator& a,
const basic_address_iterator& b)
{
return a.address_ == b.address_;
}
/// Compare two addresses for inequality.
friend bool operator!=(const basic_address_iterator& a,
const basic_address_iterator& b)
{
return a.address_ != b.address_;
}
private:
address_v6 address_;
};
/// An input iterator that can be used for traversing IPv6 addresses.
typedef basic_address_iterator<address_v6> address_v6_iterator;
} // namespace ip
} // namespace asio
} // namespace boost
#include <boost/asio/detail/pop_options.hpp>
#endif // BOOST_ASIO_IP_ADDRESS_V6_ITERATOR_HPP
| hpp |
asio | data/projects/asio/include/boost/asio/ip/basic_resolver_results.hpp | //
// ip/basic_resolver_results.hpp
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_ASIO_IP_BASIC_RESOLVER_RESULTS_HPP
#define BOOST_ASIO_IP_BASIC_RESOLVER_RESULTS_HPP
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include <boost/asio/detail/config.hpp>
#include <cstddef>
#include <cstring>
#include <boost/asio/detail/socket_ops.hpp>
#include <boost/asio/detail/socket_types.hpp>
#include <boost/asio/ip/basic_resolver_iterator.hpp>
#if defined(BOOST_ASIO_WINDOWS_RUNTIME)
# include <boost/asio/detail/winrt_utils.hpp>
#endif // defined(BOOST_ASIO_WINDOWS_RUNTIME)
#include <boost/asio/detail/push_options.hpp>
namespace boost {
namespace asio {
namespace ip {
/// A range of entries produced by a resolver.
/**
* The boost::asio::ip::basic_resolver_results class template is used to define
* a range over the results returned by a resolver.
*
* The iterator's value_type, obtained when a results iterator is dereferenced,
* is: @code const basic_resolver_entry<InternetProtocol> @endcode
*
* @note For backward compatibility, basic_resolver_results is derived from
* basic_resolver_iterator. This derivation is deprecated.
*
* @par Thread Safety
* @e Distinct @e objects: Safe.@n
* @e Shared @e objects: Unsafe.
*/
template <typename InternetProtocol>
class basic_resolver_results
#if !defined(BOOST_ASIO_NO_DEPRECATED)
: public basic_resolver_iterator<InternetProtocol>
#else // !defined(BOOST_ASIO_NO_DEPRECATED)
: private basic_resolver_iterator<InternetProtocol>
#endif // !defined(BOOST_ASIO_NO_DEPRECATED)
{
public:
/// The protocol type associated with the results.
typedef InternetProtocol protocol_type;
/// The endpoint type associated with the results.
typedef typename protocol_type::endpoint endpoint_type;
/// The type of a value in the results range.
typedef basic_resolver_entry<protocol_type> value_type;
/// The type of a const reference to a value in the range.
typedef const value_type& const_reference;
/// The type of a non-const reference to a value in the range.
typedef value_type& reference;
/// The type of an iterator into the range.
typedef basic_resolver_iterator<protocol_type> const_iterator;
/// The type of an iterator into the range.
typedef const_iterator iterator;
/// Type used to represent the distance between two iterators in the range.
typedef std::ptrdiff_t difference_type;
/// Type used to represent a count of the elements in the range.
typedef std::size_t size_type;
/// Default constructor creates an empty range.
basic_resolver_results()
{
}
/// Copy constructor.
basic_resolver_results(const basic_resolver_results& other)
: basic_resolver_iterator<InternetProtocol>(other)
{
}
/// Move constructor.
basic_resolver_results(basic_resolver_results&& other)
: basic_resolver_iterator<InternetProtocol>(
static_cast<basic_resolver_results&&>(other))
{
}
/// Assignment operator.
basic_resolver_results& operator=(const basic_resolver_results& other)
{
basic_resolver_iterator<InternetProtocol>::operator=(other);
return *this;
}
/// Move-assignment operator.
basic_resolver_results& operator=(basic_resolver_results&& other)
{
basic_resolver_iterator<InternetProtocol>::operator=(
static_cast<basic_resolver_results&&>(other));
return *this;
}
#if !defined(GENERATING_DOCUMENTATION)
// Create results from an addrinfo list returned by getaddrinfo.
static basic_resolver_results create(
boost::asio::detail::addrinfo_type* address_info,
const std::string& host_name, const std::string& service_name)
{
basic_resolver_results results;
if (!address_info)
return results;
std::string actual_host_name = host_name;
if (address_info->ai_canonname)
actual_host_name = address_info->ai_canonname;
results.values_.reset(new values_type);
while (address_info)
{
if (address_info->ai_family == BOOST_ASIO_OS_DEF(AF_INET)
|| address_info->ai_family == BOOST_ASIO_OS_DEF(AF_INET6))
{
using namespace std; // For memcpy.
typename InternetProtocol::endpoint endpoint;
endpoint.resize(static_cast<std::size_t>(address_info->ai_addrlen));
memcpy(endpoint.data(), address_info->ai_addr,
address_info->ai_addrlen);
results.values_->push_back(
basic_resolver_entry<InternetProtocol>(endpoint,
actual_host_name, service_name));
}
address_info = address_info->ai_next;
}
return results;
}
// Create results from an endpoint, host name and service name.
static basic_resolver_results create(const endpoint_type& endpoint,
const std::string& host_name, const std::string& service_name)
{
basic_resolver_results results;
results.values_.reset(new values_type);
results.values_->push_back(
basic_resolver_entry<InternetProtocol>(
endpoint, host_name, service_name));
return results;
}
// Create results from a sequence of endpoints, host and service name.
template <typename EndpointIterator>
static basic_resolver_results create(
EndpointIterator begin, EndpointIterator end,
const std::string& host_name, const std::string& service_name)
{
basic_resolver_results results;
if (begin != end)
{
results.values_.reset(new values_type);
for (EndpointIterator ep_iter = begin; ep_iter != end; ++ep_iter)
{
results.values_->push_back(
basic_resolver_entry<InternetProtocol>(
*ep_iter, host_name, service_name));
}
}
return results;
}
# if defined(BOOST_ASIO_WINDOWS_RUNTIME)
// Create results from a Windows Runtime list of EndpointPair objects.
static basic_resolver_results create(
Windows::Foundation::Collections::IVectorView<
Windows::Networking::EndpointPair^>^ endpoints,
const boost::asio::detail::addrinfo_type& hints,
const std::string& host_name, const std::string& service_name)
{
basic_resolver_results results;
if (endpoints->Size)
{
results.values_.reset(new values_type);
for (unsigned int i = 0; i < endpoints->Size; ++i)
{
auto pair = endpoints->GetAt(i);
if (hints.ai_family == BOOST_ASIO_OS_DEF(AF_INET)
&& pair->RemoteHostName->Type
!= Windows::Networking::HostNameType::Ipv4)
continue;
if (hints.ai_family == BOOST_ASIO_OS_DEF(AF_INET6)
&& pair->RemoteHostName->Type
!= Windows::Networking::HostNameType::Ipv6)
continue;
results.values_->push_back(
basic_resolver_entry<InternetProtocol>(
typename InternetProtocol::endpoint(
ip::make_address(
boost::asio::detail::winrt_utils::string(
pair->RemoteHostName->CanonicalName)),
boost::asio::detail::winrt_utils::integer(
pair->RemoteServiceName)),
host_name, service_name));
}
}
return results;
}
# endif // defined(BOOST_ASIO_WINDOWS_RUNTIME)
#endif // !defined(GENERATING_DOCUMENTATION)
/// Get the number of entries in the results range.
size_type size() const noexcept
{
return this->values_ ? this->values_->size() : 0;
}
/// Get the maximum number of entries permitted in a results range.
size_type max_size() const noexcept
{
return this->values_ ? this->values_->max_size() : values_type().max_size();
}
/// Determine whether the results range is empty.
bool empty() const noexcept
{
return this->values_ ? this->values_->empty() : true;
}
/// Obtain a begin iterator for the results range.
const_iterator begin() const
{
basic_resolver_results tmp(*this);
tmp.index_ = 0;
return static_cast<basic_resolver_results&&>(tmp);
}
/// Obtain an end iterator for the results range.
const_iterator end() const
{
return const_iterator();
}
/// Obtain a begin iterator for the results range.
const_iterator cbegin() const
{
return begin();
}
/// Obtain an end iterator for the results range.
const_iterator cend() const
{
return end();
}
/// Swap the results range with another.
void swap(basic_resolver_results& that) noexcept
{
if (this != &that)
{
this->values_.swap(that.values_);
std::size_t index = this->index_;
this->index_ = that.index_;
that.index_ = index;
}
}
/// Test two iterators for equality.
friend bool operator==(const basic_resolver_results& a,
const basic_resolver_results& b)
{
return a.equal(b);
}
/// Test two iterators for inequality.
friend bool operator!=(const basic_resolver_results& a,
const basic_resolver_results& b)
{
return !a.equal(b);
}
private:
typedef std::vector<basic_resolver_entry<InternetProtocol>> values_type;
};
} // namespace ip
} // namespace asio
} // namespace boost
#include <boost/asio/detail/pop_options.hpp>
#endif // BOOST_ASIO_IP_BASIC_RESOLVER_RESULTS_HPP
| hpp |
asio | data/projects/asio/include/boost/asio/ip/basic_resolver_query.hpp | //
// ip/basic_resolver_query.hpp
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_ASIO_IP_BASIC_RESOLVER_QUERY_HPP
#define BOOST_ASIO_IP_BASIC_RESOLVER_QUERY_HPP
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include <boost/asio/detail/config.hpp>
#include <string>
#include <boost/asio/detail/socket_ops.hpp>
#include <boost/asio/ip/resolver_query_base.hpp>
#include <boost/asio/detail/push_options.hpp>
namespace boost {
namespace asio {
namespace ip {
/// An query to be passed to a resolver.
/**
* The boost::asio::ip::basic_resolver_query class template describes a query
* that can be passed to a resolver.
*
* @par Thread Safety
* @e Distinct @e objects: Safe.@n
* @e Shared @e objects: Unsafe.
*/
template <typename InternetProtocol>
class basic_resolver_query
: public resolver_query_base
{
public:
/// The protocol type associated with the endpoint query.
typedef InternetProtocol protocol_type;
/// Construct with specified service name for any protocol.
/**
* This constructor is typically used to perform name resolution for local
* service binding.
*
* @param service A string identifying the requested service. This may be a
* descriptive name or a numeric string corresponding to a port number.
*
* @param resolve_flags A set of flags that determine how name resolution
* should be performed. The default flags are suitable for local service
* binding.
*
* @note On POSIX systems, service names are typically defined in the file
* <tt>/etc/services</tt>. On Windows, service names may be found in the file
* <tt>c:\\windows\\system32\\drivers\\etc\\services</tt>. Operating systems
* may use additional locations when resolving service names.
*/
basic_resolver_query(const std::string& service,
resolver_query_base::flags resolve_flags = passive | address_configured)
: hints_(),
host_name_(),
service_name_(service)
{
typename InternetProtocol::endpoint endpoint;
hints_.ai_flags = static_cast<int>(resolve_flags);
hints_.ai_family = PF_UNSPEC;
hints_.ai_socktype = endpoint.protocol().type();
hints_.ai_protocol = endpoint.protocol().protocol();
hints_.ai_addrlen = 0;
hints_.ai_canonname = 0;
hints_.ai_addr = 0;
hints_.ai_next = 0;
}
/// Construct with specified service name for a given protocol.
/**
* This constructor is typically used to perform name resolution for local
* service binding with a specific protocol version.
*
* @param protocol A protocol object, normally representing either the IPv4 or
* IPv6 version of an internet protocol.
*
* @param service A string identifying the requested service. This may be a
* descriptive name or a numeric string corresponding to a port number.
*
* @param resolve_flags A set of flags that determine how name resolution
* should be performed. The default flags are suitable for local service
* binding.
*
* @note On POSIX systems, service names are typically defined in the file
* <tt>/etc/services</tt>. On Windows, service names may be found in the file
* <tt>c:\\windows\\system32\\drivers\\etc\\services</tt>. Operating systems
* may use additional locations when resolving service names.
*/
basic_resolver_query(const protocol_type& protocol,
const std::string& service,
resolver_query_base::flags resolve_flags = passive | address_configured)
: hints_(),
host_name_(),
service_name_(service)
{
hints_.ai_flags = static_cast<int>(resolve_flags);
hints_.ai_family = protocol.family();
hints_.ai_socktype = protocol.type();
hints_.ai_protocol = protocol.protocol();
hints_.ai_addrlen = 0;
hints_.ai_canonname = 0;
hints_.ai_addr = 0;
hints_.ai_next = 0;
}
/// Construct with specified host name and service name for any protocol.
/**
* This constructor is typically used to perform name resolution for
* communication with remote hosts.
*
* @param host A string identifying a location. May be a descriptive name or
* a numeric address string. If an empty string and the passive flag has been
* specified, the resolved endpoints are suitable for local service binding.
* If an empty string and passive is not specified, the resolved endpoints
* will use the loopback address.
*
* @param service A string identifying the requested service. This may be a
* descriptive name or a numeric string corresponding to a port number. May
* be an empty string, in which case all resolved endpoints will have a port
* number of 0.
*
* @param resolve_flags A set of flags that determine how name resolution
* should be performed. The default flags are suitable for communication with
* remote hosts.
*
* @note On POSIX systems, host names may be locally defined in the file
* <tt>/etc/hosts</tt>. On Windows, host names may be defined in the file
* <tt>c:\\windows\\system32\\drivers\\etc\\hosts</tt>. Remote host name
* resolution is performed using DNS. Operating systems may use additional
* locations when resolving host names (such as NETBIOS names on Windows).
*
* On POSIX systems, service names are typically defined in the file
* <tt>/etc/services</tt>. On Windows, service names may be found in the file
* <tt>c:\\windows\\system32\\drivers\\etc\\services</tt>. Operating systems
* may use additional locations when resolving service names.
*/
basic_resolver_query(const std::string& host, const std::string& service,
resolver_query_base::flags resolve_flags = address_configured)
: hints_(),
host_name_(host),
service_name_(service)
{
typename InternetProtocol::endpoint endpoint;
hints_.ai_flags = static_cast<int>(resolve_flags);
hints_.ai_family = BOOST_ASIO_OS_DEF(AF_UNSPEC);
hints_.ai_socktype = endpoint.protocol().type();
hints_.ai_protocol = endpoint.protocol().protocol();
hints_.ai_addrlen = 0;
hints_.ai_canonname = 0;
hints_.ai_addr = 0;
hints_.ai_next = 0;
}
/// Construct with specified host name and service name for a given protocol.
/**
* This constructor is typically used to perform name resolution for
* communication with remote hosts.
*
* @param protocol A protocol object, normally representing either the IPv4 or
* IPv6 version of an internet protocol.
*
* @param host A string identifying a location. May be a descriptive name or
* a numeric address string. If an empty string and the passive flag has been
* specified, the resolved endpoints are suitable for local service binding.
* If an empty string and passive is not specified, the resolved endpoints
* will use the loopback address.
*
* @param service A string identifying the requested service. This may be a
* descriptive name or a numeric string corresponding to a port number. May
* be an empty string, in which case all resolved endpoints will have a port
* number of 0.
*
* @param resolve_flags A set of flags that determine how name resolution
* should be performed. The default flags are suitable for communication with
* remote hosts.
*
* @note On POSIX systems, host names may be locally defined in the file
* <tt>/etc/hosts</tt>. On Windows, host names may be defined in the file
* <tt>c:\\windows\\system32\\drivers\\etc\\hosts</tt>. Remote host name
* resolution is performed using DNS. Operating systems may use additional
* locations when resolving host names (such as NETBIOS names on Windows).
*
* On POSIX systems, service names are typically defined in the file
* <tt>/etc/services</tt>. On Windows, service names may be found in the file
* <tt>c:\\windows\\system32\\drivers\\etc\\services</tt>. Operating systems
* may use additional locations when resolving service names.
*/
basic_resolver_query(const protocol_type& protocol,
const std::string& host, const std::string& service,
resolver_query_base::flags resolve_flags = address_configured)
: hints_(),
host_name_(host),
service_name_(service)
{
hints_.ai_flags = static_cast<int>(resolve_flags);
hints_.ai_family = protocol.family();
hints_.ai_socktype = protocol.type();
hints_.ai_protocol = protocol.protocol();
hints_.ai_addrlen = 0;
hints_.ai_canonname = 0;
hints_.ai_addr = 0;
hints_.ai_next = 0;
}
/// Copy construct a @c basic_resolver_query from another.
basic_resolver_query(const basic_resolver_query& other)
: hints_(other.hints_),
host_name_(other.host_name_),
service_name_(other.service_name_)
{
}
/// Move construct a @c basic_resolver_query from another.
basic_resolver_query(basic_resolver_query&& other)
: hints_(other.hints_),
host_name_(static_cast<std::string&&>(other.host_name_)),
service_name_(static_cast<std::string&&>(other.service_name_))
{
}
/// Get the hints associated with the query.
const boost::asio::detail::addrinfo_type& hints() const
{
return hints_;
}
/// Get the host name associated with the query.
std::string host_name() const
{
return host_name_;
}
/// Get the service name associated with the query.
std::string service_name() const
{
return service_name_;
}
private:
boost::asio::detail::addrinfo_type hints_;
std::string host_name_;
std::string service_name_;
};
} // namespace ip
} // namespace asio
} // namespace boost
#include <boost/asio/detail/pop_options.hpp>
#endif // BOOST_ASIO_IP_BASIC_RESOLVER_QUERY_HPP
| hpp |
asio | data/projects/asio/include/boost/asio/ip/address_v6.hpp | //
// ip/address_v6.hpp
// ~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_ASIO_IP_ADDRESS_V6_HPP
#define BOOST_ASIO_IP_ADDRESS_V6_HPP
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include <boost/asio/detail/config.hpp>
#include <functional>
#include <string>
#include <boost/asio/detail/array.hpp>
#include <boost/asio/detail/cstdint.hpp>
#include <boost/asio/detail/socket_types.hpp>
#include <boost/asio/detail/string_view.hpp>
#include <boost/asio/detail/winsock_init.hpp>
#include <boost/system/error_code.hpp>
#include <boost/asio/ip/address_v4.hpp>
#if !defined(BOOST_ASIO_NO_IOSTREAM)
# include <iosfwd>
#endif // !defined(BOOST_ASIO_NO_IOSTREAM)
#include <boost/asio/detail/push_options.hpp>
namespace boost {
namespace asio {
namespace ip {
template <typename> class basic_address_iterator;
/// Type used for storing IPv6 scope IDs.
typedef uint_least32_t scope_id_type;
/// Implements IP version 6 style addresses.
/**
* The boost::asio::ip::address_v6 class provides the ability to use and
* manipulate IP version 6 addresses.
*
* @par Thread Safety
* @e Distinct @e objects: Safe.@n
* @e Shared @e objects: Unsafe.
*/
class address_v6
{
public:
/// The type used to represent an address as an array of bytes.
/**
* @note This type is defined in terms of the C++0x template @c std::array
* when it is available. Otherwise, it uses @c boost:array.
*/
#if defined(GENERATING_DOCUMENTATION)
typedef array<unsigned char, 16> bytes_type;
#else
typedef boost::asio::detail::array<unsigned char, 16> bytes_type;
#endif
/// Default constructor.
/**
* Initialises the @c address_v6 object such that:
* @li <tt>to_bytes()</tt> yields <tt>{0, 0, ..., 0}</tt>; and
* @li <tt>scope_id() == 0</tt>.
*/
BOOST_ASIO_DECL address_v6() noexcept;
/// Construct an address from raw bytes and scope ID.
/**
* Initialises the @c address_v6 object such that:
* @li <tt>to_bytes() == bytes</tt>; and
* @li <tt>this->scope_id() == scope_id</tt>.
*
* @throws out_of_range Thrown if any element in @c bytes is not in the range
* <tt>0 - 0xFF</tt>. Note that no range checking is required for platforms
* where <tt>std::numeric_limits<unsigned char>::max()</tt> is <tt>0xFF</tt>.
*/
BOOST_ASIO_DECL explicit address_v6(const bytes_type& bytes,
scope_id_type scope_id = 0);
/// Copy constructor.
BOOST_ASIO_DECL address_v6(const address_v6& other) noexcept;
/// Move constructor.
BOOST_ASIO_DECL address_v6(address_v6&& other) noexcept;
/// Assign from another address.
BOOST_ASIO_DECL address_v6& operator=(
const address_v6& other) noexcept;
/// Move-assign from another address.
BOOST_ASIO_DECL address_v6& operator=(address_v6&& other) noexcept;
/// The scope ID of the address.
/**
* Returns the scope ID associated with the IPv6 address.
*/
scope_id_type scope_id() const noexcept
{
return scope_id_;
}
/// The scope ID of the address.
/**
* Modifies the scope ID associated with the IPv6 address.
*
* @param id The new scope ID.
*/
void scope_id(scope_id_type id) noexcept
{
scope_id_ = id;
}
/// Get the address in bytes, in network byte order.
BOOST_ASIO_DECL bytes_type to_bytes() const noexcept;
/// Get the address as a string.
BOOST_ASIO_DECL std::string to_string() const;
#if !defined(BOOST_ASIO_NO_DEPRECATED)
/// (Deprecated: Use other overload.) Get the address as a string.
BOOST_ASIO_DECL std::string to_string(boost::system::error_code& ec) const;
/// (Deprecated: Use make_address_v6().) Create an IPv6 address from an IP
/// address string.
static address_v6 from_string(const char* str);
/// (Deprecated: Use make_address_v6().) Create an IPv6 address from an IP
/// address string.
static address_v6 from_string(
const char* str, boost::system::error_code& ec);
/// (Deprecated: Use make_address_v6().) Create an IPv6 address from an IP
/// address string.
static address_v6 from_string(const std::string& str);
/// (Deprecated: Use make_address_v6().) Create an IPv6 address from an IP
/// address string.
static address_v6 from_string(
const std::string& str, boost::system::error_code& ec);
/// (Deprecated: Use make_address_v4().) Converts an IPv4-mapped or
/// IPv4-compatible address to an IPv4 address.
BOOST_ASIO_DECL address_v4 to_v4() const;
#endif // !defined(BOOST_ASIO_NO_DEPRECATED)
/// Determine whether the address is a loopback address.
/**
* This function tests whether the address is the loopback address
* <tt>::1</tt>.
*/
BOOST_ASIO_DECL bool is_loopback() const noexcept;
/// Determine whether the address is unspecified.
/**
* This function tests whether the address is the loopback address
* <tt>::</tt>.
*/
BOOST_ASIO_DECL bool is_unspecified() const noexcept;
/// Determine whether the address is link local.
BOOST_ASIO_DECL bool is_link_local() const noexcept;
/// Determine whether the address is site local.
BOOST_ASIO_DECL bool is_site_local() const noexcept;
/// Determine whether the address is a mapped IPv4 address.
BOOST_ASIO_DECL bool is_v4_mapped() const noexcept;
#if !defined(BOOST_ASIO_NO_DEPRECATED)
/// (Deprecated: No replacement.) Determine whether the address is an
/// IPv4-compatible address.
BOOST_ASIO_DECL bool is_v4_compatible() const;
#endif // !defined(BOOST_ASIO_NO_DEPRECATED)
/// Determine whether the address is a multicast address.
BOOST_ASIO_DECL bool is_multicast() const noexcept;
/// Determine whether the address is a global multicast address.
BOOST_ASIO_DECL bool is_multicast_global() const noexcept;
/// Determine whether the address is a link-local multicast address.
BOOST_ASIO_DECL bool is_multicast_link_local() const noexcept;
/// Determine whether the address is a node-local multicast address.
BOOST_ASIO_DECL bool is_multicast_node_local() const noexcept;
/// Determine whether the address is a org-local multicast address.
BOOST_ASIO_DECL bool is_multicast_org_local() const noexcept;
/// Determine whether the address is a site-local multicast address.
BOOST_ASIO_DECL bool is_multicast_site_local() const noexcept;
/// Compare two addresses for equality.
BOOST_ASIO_DECL friend bool operator==(const address_v6& a1,
const address_v6& a2) noexcept;
/// Compare two addresses for inequality.
friend bool operator!=(const address_v6& a1,
const address_v6& a2) noexcept
{
return !(a1 == a2);
}
/// Compare addresses for ordering.
BOOST_ASIO_DECL friend bool operator<(const address_v6& a1,
const address_v6& a2) noexcept;
/// Compare addresses for ordering.
friend bool operator>(const address_v6& a1,
const address_v6& a2) noexcept
{
return a2 < a1;
}
/// Compare addresses for ordering.
friend bool operator<=(const address_v6& a1,
const address_v6& a2) noexcept
{
return !(a2 < a1);
}
/// Compare addresses for ordering.
friend bool operator>=(const address_v6& a1,
const address_v6& a2) noexcept
{
return !(a1 < a2);
}
/// Obtain an address object that represents any address.
/**
* This functions returns an address that represents the "any" address
* <tt>::</tt>.
*
* @returns A default-constructed @c address_v6 object.
*/
static address_v6 any() noexcept
{
return address_v6();
}
/// Obtain an address object that represents the loopback address.
/**
* This function returns an address that represents the well-known loopback
* address <tt>::1</tt>.
*/
BOOST_ASIO_DECL static address_v6 loopback() noexcept;
#if !defined(BOOST_ASIO_NO_DEPRECATED)
/// (Deprecated: Use make_address_v6().) Create an IPv4-mapped IPv6 address.
BOOST_ASIO_DECL static address_v6 v4_mapped(const address_v4& addr);
/// (Deprecated: No replacement.) Create an IPv4-compatible IPv6 address.
BOOST_ASIO_DECL static address_v6 v4_compatible(const address_v4& addr);
#endif // !defined(BOOST_ASIO_NO_DEPRECATED)
private:
friend class basic_address_iterator<address_v6>;
// The underlying IPv6 address.
boost::asio::detail::in6_addr_type addr_;
// The scope ID associated with the address.
scope_id_type scope_id_;
};
/// Create an IPv6 address from raw bytes and scope ID.
/**
* @relates address_v6
*/
inline address_v6 make_address_v6(const address_v6::bytes_type& bytes,
scope_id_type scope_id = 0)
{
return address_v6(bytes, scope_id);
}
/// Create an IPv6 address from an IP address string.
/**
* @relates address_v6
*/
BOOST_ASIO_DECL address_v6 make_address_v6(const char* str);
/// Create an IPv6 address from an IP address string.
/**
* @relates address_v6
*/
BOOST_ASIO_DECL address_v6 make_address_v6(const char* str,
boost::system::error_code& ec) noexcept;
/// Createan IPv6 address from an IP address string.
/**
* @relates address_v6
*/
BOOST_ASIO_DECL address_v6 make_address_v6(const std::string& str);
/// Create an IPv6 address from an IP address string.
/**
* @relates address_v6
*/
BOOST_ASIO_DECL address_v6 make_address_v6(const std::string& str,
boost::system::error_code& ec) noexcept;
#if defined(BOOST_ASIO_HAS_STRING_VIEW) \
|| defined(GENERATING_DOCUMENTATION)
/// Create an IPv6 address from an IP address string.
/**
* @relates address_v6
*/
BOOST_ASIO_DECL address_v6 make_address_v6(string_view str);
/// Create an IPv6 address from an IP address string.
/**
* @relates address_v6
*/
BOOST_ASIO_DECL address_v6 make_address_v6(string_view str,
boost::system::error_code& ec) noexcept;
#endif // defined(BOOST_ASIO_HAS_STRING_VIEW)
// || defined(GENERATING_DOCUMENTATION)
/// Tag type used for distinguishing overloads that deal in IPv4-mapped IPv6
/// addresses.
enum v4_mapped_t { v4_mapped };
/// Create an IPv4 address from a IPv4-mapped IPv6 address.
/**
* @relates address_v4
*/
BOOST_ASIO_DECL address_v4 make_address_v4(
v4_mapped_t, const address_v6& v6_addr);
/// Create an IPv4-mapped IPv6 address from an IPv4 address.
/**
* @relates address_v6
*/
BOOST_ASIO_DECL address_v6 make_address_v6(
v4_mapped_t, const address_v4& v4_addr);
#if !defined(BOOST_ASIO_NO_IOSTREAM)
/// Output an address as a string.
/**
* Used to output a human-readable string for a specified address.
*
* @param os The output stream to which the string will be written.
*
* @param addr The address to be written.
*
* @return The output stream.
*
* @relates boost::asio::ip::address_v6
*/
template <typename Elem, typename Traits>
std::basic_ostream<Elem, Traits>& operator<<(
std::basic_ostream<Elem, Traits>& os, const address_v6& addr);
#endif // !defined(BOOST_ASIO_NO_IOSTREAM)
} // namespace ip
} // namespace asio
} // namespace boost
namespace std {
template <>
struct hash<boost::asio::ip::address_v6>
{
std::size_t operator()(const boost::asio::ip::address_v6& addr)
const noexcept
{
const boost::asio::ip::address_v6::bytes_type bytes = addr.to_bytes();
std::size_t result = static_cast<std::size_t>(addr.scope_id());
combine_4_bytes(result, &bytes[0]);
combine_4_bytes(result, &bytes[4]);
combine_4_bytes(result, &bytes[8]);
combine_4_bytes(result, &bytes[12]);
return result;
}
private:
static void combine_4_bytes(std::size_t& seed, const unsigned char* bytes)
{
const std::size_t bytes_hash =
(static_cast<std::size_t>(bytes[0]) << 24) |
(static_cast<std::size_t>(bytes[1]) << 16) |
(static_cast<std::size_t>(bytes[2]) << 8) |
(static_cast<std::size_t>(bytes[3]));
seed ^= bytes_hash + 0x9e3779b9 + (seed << 6) + (seed >> 2);
}
};
} // namespace std
#include <boost/asio/detail/pop_options.hpp>
#include <boost/asio/ip/impl/address_v6.hpp>
#if defined(BOOST_ASIO_HEADER_ONLY)
# include <boost/asio/ip/impl/address_v6.ipp>
#endif // defined(BOOST_ASIO_HEADER_ONLY)
#endif // BOOST_ASIO_IP_ADDRESS_V6_HPP
| hpp |
asio | data/projects/asio/include/boost/asio/ip/network_v6.hpp | //
// ip/network_v6.hpp
// ~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com)
// Copyright (c) 2014 Oliver Kowalke (oliver dot kowalke at gmail dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_ASIO_IP_NETWORK_V6_HPP
#define BOOST_ASIO_IP_NETWORK_V6_HPP
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include <boost/asio/detail/config.hpp>
#include <string>
#include <boost/asio/detail/string_view.hpp>
#include <boost/system/error_code.hpp>
#include <boost/asio/ip/address_v6_range.hpp>
#include <boost/asio/detail/push_options.hpp>
namespace boost {
namespace asio {
namespace ip {
/// Represents an IPv6 network.
/**
* The boost::asio::ip::network_v6 class provides the ability to use and
* manipulate IP version 6 networks.
*
* @par Thread Safety
* @e Distinct @e objects: Safe.@n
* @e Shared @e objects: Unsafe.
*/
class network_v6
{
public:
/// Default constructor.
network_v6() noexcept
: address_(),
prefix_length_(0)
{
}
/// Construct a network based on the specified address and prefix length.
BOOST_ASIO_DECL network_v6(const address_v6& addr,
unsigned short prefix_len);
/// Copy constructor.
network_v6(const network_v6& other) noexcept
: address_(other.address_),
prefix_length_(other.prefix_length_)
{
}
/// Move constructor.
network_v6(network_v6&& other) noexcept
: address_(static_cast<address_v6&&>(other.address_)),
prefix_length_(other.prefix_length_)
{
}
/// Assign from another network.
network_v6& operator=(const network_v6& other) noexcept
{
address_ = other.address_;
prefix_length_ = other.prefix_length_;
return *this;
}
/// Move-assign from another network.
network_v6& operator=(network_v6&& other) noexcept
{
address_ = static_cast<address_v6&&>(other.address_);
prefix_length_ = other.prefix_length_;
return *this;
}
/// Obtain the address object specified when the network object was created.
address_v6 address() const noexcept
{
return address_;
}
/// Obtain the prefix length that was specified when the network object was
/// created.
unsigned short prefix_length() const noexcept
{
return prefix_length_;
}
/// Obtain an address object that represents the network address.
BOOST_ASIO_DECL address_v6 network() const noexcept;
/// Obtain an address range corresponding to the hosts in the network.
BOOST_ASIO_DECL address_v6_range hosts() const noexcept;
/// Obtain the true network address, omitting any host bits.
network_v6 canonical() const noexcept
{
return network_v6(network(), prefix_length());
}
/// Test if network is a valid host address.
bool is_host() const noexcept
{
return prefix_length_ == 128;
}
/// Test if a network is a real subnet of another network.
BOOST_ASIO_DECL bool is_subnet_of(const network_v6& other) const;
/// Get the network as an address in dotted decimal format.
BOOST_ASIO_DECL std::string to_string() const;
/// Get the network as an address in dotted decimal format.
BOOST_ASIO_DECL std::string to_string(boost::system::error_code& ec) const;
/// Compare two networks for equality.
friend bool operator==(const network_v6& a, const network_v6& b)
{
return a.address_ == b.address_ && a.prefix_length_ == b.prefix_length_;
}
/// Compare two networks for inequality.
friend bool operator!=(const network_v6& a, const network_v6& b)
{
return !(a == b);
}
private:
address_v6 address_;
unsigned short prefix_length_;
};
/// Create an IPv6 network from an address and prefix length.
/**
* @relates address_v6
*/
inline network_v6 make_network_v6(
const address_v6& addr, unsigned short prefix_len)
{
return network_v6(addr, prefix_len);
}
/// Create an IPv6 network from a string containing IP address and prefix
/// length.
/**
* @relates network_v6
*/
BOOST_ASIO_DECL network_v6 make_network_v6(const char* str);
/// Create an IPv6 network from a string containing IP address and prefix
/// length.
/**
* @relates network_v6
*/
BOOST_ASIO_DECL network_v6 make_network_v6(
const char* str, boost::system::error_code& ec);
/// Create an IPv6 network from a string containing IP address and prefix
/// length.
/**
* @relates network_v6
*/
BOOST_ASIO_DECL network_v6 make_network_v6(const std::string& str);
/// Create an IPv6 network from a string containing IP address and prefix
/// length.
/**
* @relates network_v6
*/
BOOST_ASIO_DECL network_v6 make_network_v6(
const std::string& str, boost::system::error_code& ec);
#if defined(BOOST_ASIO_HAS_STRING_VIEW) \
|| defined(GENERATING_DOCUMENTATION)
/// Create an IPv6 network from a string containing IP address and prefix
/// length.
/**
* @relates network_v6
*/
BOOST_ASIO_DECL network_v6 make_network_v6(string_view str);
/// Create an IPv6 network from a string containing IP address and prefix
/// length.
/**
* @relates network_v6
*/
BOOST_ASIO_DECL network_v6 make_network_v6(
string_view str, boost::system::error_code& ec);
#endif // defined(BOOST_ASIO_HAS_STRING_VIEW)
// || defined(GENERATING_DOCUMENTATION)
#if !defined(BOOST_ASIO_NO_IOSTREAM)
/// Output a network as a string.
/**
* Used to output a human-readable string for a specified network.
*
* @param os The output stream to which the string will be written.
*
* @param net The network to be written.
*
* @return The output stream.
*
* @relates boost::asio::ip::address_v6
*/
template <typename Elem, typename Traits>
std::basic_ostream<Elem, Traits>& operator<<(
std::basic_ostream<Elem, Traits>& os, const network_v6& net);
#endif // !defined(BOOST_ASIO_NO_IOSTREAM)
} // namespace ip
} // namespace asio
} // namespace boost
#include <boost/asio/detail/pop_options.hpp>
#include <boost/asio/ip/impl/network_v6.hpp>
#if defined(BOOST_ASIO_HEADER_ONLY)
# include <boost/asio/ip/impl/network_v6.ipp>
#endif // defined(BOOST_ASIO_HEADER_ONLY)
#endif // BOOST_ASIO_IP_NETWORK_V6_HPP
| hpp |
asio | data/projects/asio/include/boost/asio/ip/unicast.hpp | //
// ip/unicast.hpp
// ~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_ASIO_IP_UNICAST_HPP
#define BOOST_ASIO_IP_UNICAST_HPP
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include <boost/asio/detail/config.hpp>
#include <cstddef>
#include <boost/asio/ip/detail/socket_option.hpp>
#include <boost/asio/detail/push_options.hpp>
namespace boost {
namespace asio {
namespace ip {
namespace unicast {
/// Socket option for time-to-live associated with outgoing unicast packets.
/**
* Implements the IPPROTO_IP/IP_UNICAST_TTL socket option.
*
* @par Examples
* Setting the option:
* @code
* boost::asio::ip::udp::socket socket(my_context);
* ...
* boost::asio::ip::unicast::hops option(4);
* socket.set_option(option);
* @endcode
*
* @par
* Getting the current option value:
* @code
* boost::asio::ip::udp::socket socket(my_context);
* ...
* boost::asio::ip::unicast::hops option;
* socket.get_option(option);
* int ttl = option.value();
* @endcode
*
* @par Concepts:
* GettableSocketOption, SettableSocketOption.
*/
#if defined(GENERATING_DOCUMENTATION)
typedef implementation_defined hops;
#else
typedef boost::asio::ip::detail::socket_option::unicast_hops<
BOOST_ASIO_OS_DEF(IPPROTO_IP),
BOOST_ASIO_OS_DEF(IP_TTL),
BOOST_ASIO_OS_DEF(IPPROTO_IPV6),
BOOST_ASIO_OS_DEF(IPV6_UNICAST_HOPS)> hops;
#endif
} // namespace unicast
} // namespace ip
} // namespace asio
} // namespace boost
#include <boost/asio/detail/pop_options.hpp>
#endif // BOOST_ASIO_IP_UNICAST_HPP
| hpp |
asio | data/projects/asio/include/boost/asio/ip/bad_address_cast.hpp | //
// ip/bad_address_cast.hpp
// ~~~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_ASIO_IP_BAD_ADDRESS_CAST_HPP
#define BOOST_ASIO_IP_BAD_ADDRESS_CAST_HPP
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include <boost/asio/detail/config.hpp>
#include <typeinfo>
#include <boost/asio/detail/push_options.hpp>
namespace boost {
namespace asio {
namespace ip {
/// Thrown to indicate a failed address conversion.
class bad_address_cast :
#if defined(BOOST_ASIO_MSVC) && defined(_HAS_EXCEPTIONS) && !_HAS_EXCEPTIONS
public std::exception
#else
public std::bad_cast
#endif
{
public:
/// Default constructor.
bad_address_cast() {}
/// Copy constructor.
bad_address_cast(const bad_address_cast& other) noexcept
#if defined(BOOST_ASIO_MSVC) && defined(_HAS_EXCEPTIONS) && !_HAS_EXCEPTIONS
: std::exception(static_cast<const std::exception&>(other))
#else
: std::bad_cast(static_cast<const std::bad_cast&>(other))
#endif
{
}
/// Destructor.
virtual ~bad_address_cast() noexcept {}
/// Get the message associated with the exception.
virtual const char* what() const noexcept
{
return "bad address cast";
}
};
} // namespace ip
} // namespace asio
} // namespace boost
#include <boost/asio/detail/pop_options.hpp>
#endif // BOOST_ASIO_IP_ADDRESS_HPP
| hpp |
asio | data/projects/asio/include/boost/asio/ip/address_v6_range.hpp | //
// ip/address_v6_range.hpp
// ~~~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com)
// Oliver Kowalke (oliver dot kowalke at gmail dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_ASIO_IP_ADDRESS_V6_RANGE_HPP
#define BOOST_ASIO_IP_ADDRESS_V6_RANGE_HPP
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include <boost/asio/detail/config.hpp>
#include <boost/asio/ip/address_v6_iterator.hpp>
#include <boost/asio/detail/push_options.hpp>
namespace boost {
namespace asio {
namespace ip {
template <typename> class basic_address_range;
/// Represents a range of IPv6 addresses.
/**
* @par Thread Safety
* @e Distinct @e objects: Safe.@n
* @e Shared @e objects: Unsafe.
*/
template <> class basic_address_range<address_v6>
{
public:
/// The type of an iterator that points into the range.
typedef basic_address_iterator<address_v6> iterator;
/// Construct an empty range.
basic_address_range() noexcept
: begin_(address_v6()),
end_(address_v6())
{
}
/// Construct an range that represents the given range of addresses.
explicit basic_address_range(const iterator& first,
const iterator& last) noexcept
: begin_(first),
end_(last)
{
}
/// Copy constructor.
basic_address_range(const basic_address_range& other) noexcept
: begin_(other.begin_),
end_(other.end_)
{
}
/// Move constructor.
basic_address_range(basic_address_range&& other) noexcept
: begin_(static_cast<iterator&&>(other.begin_)),
end_(static_cast<iterator&&>(other.end_))
{
}
/// Assignment operator.
basic_address_range& operator=(
const basic_address_range& other) noexcept
{
begin_ = other.begin_;
end_ = other.end_;
return *this;
}
/// Move assignment operator.
basic_address_range& operator=(basic_address_range&& other) noexcept
{
begin_ = static_cast<iterator&&>(other.begin_);
end_ = static_cast<iterator&&>(other.end_);
return *this;
}
/// Obtain an iterator that points to the start of the range.
iterator begin() const noexcept
{
return begin_;
}
/// Obtain an iterator that points to the end of the range.
iterator end() const noexcept
{
return end_;
}
/// Determine whether the range is empty.
bool empty() const noexcept
{
return begin_ == end_;
}
/// Find an address in the range.
iterator find(const address_v6& addr) const noexcept
{
return addr >= *begin_ && addr < *end_ ? iterator(addr) : end_;
}
private:
iterator begin_;
iterator end_;
};
/// Represents a range of IPv6 addresses.
typedef basic_address_range<address_v6> address_v6_range;
} // namespace ip
} // namespace asio
} // namespace boost
#include <boost/asio/detail/pop_options.hpp>
#endif // BOOST_ASIO_IP_ADDRESS_V6_RANGE_HPP
| hpp |
asio | data/projects/asio/include/boost/asio/ip/resolver_query_base.hpp | //
// ip/resolver_query_base.hpp
// ~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_ASIO_IP_RESOLVER_QUERY_BASE_HPP
#define BOOST_ASIO_IP_RESOLVER_QUERY_BASE_HPP
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include <boost/asio/detail/config.hpp>
#include <boost/asio/ip/resolver_base.hpp>
#include <boost/asio/detail/push_options.hpp>
namespace boost {
namespace asio {
namespace ip {
/// The resolver_query_base class is used as a base for the
/// basic_resolver_query class templates to provide a common place to define
/// the flag constants.
class resolver_query_base : public resolver_base
{
protected:
/// Protected destructor to prevent deletion through this type.
~resolver_query_base()
{
}
};
} // namespace ip
} // namespace asio
} // namespace boost
#include <boost/asio/detail/pop_options.hpp>
#endif // BOOST_ASIO_IP_RESOLVER_QUERY_BASE_HPP
| hpp |
asio | data/projects/asio/include/boost/asio/ip/basic_resolver.hpp | //
// ip/basic_resolver.hpp
// ~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_ASIO_IP_BASIC_RESOLVER_HPP
#define BOOST_ASIO_IP_BASIC_RESOLVER_HPP
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include <boost/asio/detail/config.hpp>
#include <string>
#include <utility>
#include <boost/asio/any_io_executor.hpp>
#include <boost/asio/async_result.hpp>
#include <boost/asio/detail/handler_type_requirements.hpp>
#include <boost/asio/detail/io_object_impl.hpp>
#include <boost/asio/detail/non_const_lvalue.hpp>
#include <boost/asio/detail/string_view.hpp>
#include <boost/asio/detail/throw_error.hpp>
#include <boost/asio/error.hpp>
#include <boost/asio/execution_context.hpp>
#include <boost/asio/ip/basic_resolver_iterator.hpp>
#include <boost/asio/ip/basic_resolver_query.hpp>
#include <boost/asio/ip/basic_resolver_results.hpp>
#include <boost/asio/ip/resolver_base.hpp>
#if defined(BOOST_ASIO_WINDOWS_RUNTIME)
# include <boost/asio/detail/winrt_resolver_service.hpp>
#else
# include <boost/asio/detail/resolver_service.hpp>
#endif
#include <boost/asio/detail/push_options.hpp>
namespace boost {
namespace asio {
namespace ip {
#if !defined(BOOST_ASIO_IP_BASIC_RESOLVER_FWD_DECL)
#define BOOST_ASIO_IP_BASIC_RESOLVER_FWD_DECL
// Forward declaration with defaulted arguments.
template <typename InternetProtocol, typename Executor = any_io_executor>
class basic_resolver;
#endif // !defined(BOOST_ASIO_IP_BASIC_RESOLVER_FWD_DECL)
/// Provides endpoint resolution functionality.
/**
* The basic_resolver class template provides the ability to resolve a query
* to a list of endpoints.
*
* @par Thread Safety
* @e Distinct @e objects: Safe.@n
* @e Shared @e objects: Unsafe.
*/
template <typename InternetProtocol, typename Executor>
class basic_resolver
: public resolver_base
{
private:
class initiate_async_resolve;
public:
/// The type of the executor associated with the object.
typedef Executor executor_type;
/// Rebinds the resolver type to another executor.
template <typename Executor1>
struct rebind_executor
{
/// The resolver type when rebound to the specified executor.
typedef basic_resolver<InternetProtocol, Executor1> other;
};
/// The protocol type.
typedef InternetProtocol protocol_type;
/// The endpoint type.
typedef typename InternetProtocol::endpoint endpoint_type;
#if !defined(BOOST_ASIO_NO_DEPRECATED)
/// (Deprecated.) The query type.
typedef basic_resolver_query<InternetProtocol> query;
/// (Deprecated.) The iterator type.
typedef basic_resolver_iterator<InternetProtocol> iterator;
#endif // !defined(BOOST_ASIO_NO_DEPRECATED)
/// The results type.
typedef basic_resolver_results<InternetProtocol> results_type;
/// Construct with executor.
/**
* This constructor creates a basic_resolver.
*
* @param ex The I/O executor that the resolver will use, by default, to
* dispatch handlers for any asynchronous operations performed on the
* resolver.
*/
explicit basic_resolver(const executor_type& ex)
: impl_(0, ex)
{
}
/// Construct with execution context.
/**
* This constructor creates a basic_resolver.
*
* @param context An execution context which provides the I/O executor that
* the resolver will use, by default, to dispatch handlers for any
* asynchronous operations performed on the resolver.
*/
template <typename ExecutionContext>
explicit basic_resolver(ExecutionContext& context,
constraint_t<
is_convertible<ExecutionContext&, execution_context&>::value
> = 0)
: impl_(0, 0, context)
{
}
/// Move-construct a basic_resolver from another.
/**
* This constructor moves a resolver from one object to another.
*
* @param other The other basic_resolver object from which the move will
* occur.
*
* @note Following the move, the moved-from object is in the same state as if
* constructed using the @c basic_resolver(const executor_type&) constructor.
*/
basic_resolver(basic_resolver&& other)
: impl_(std::move(other.impl_))
{
}
// All resolvers have access to each other's implementations.
template <typename InternetProtocol1, typename Executor1>
friend class basic_resolver;
/// Move-construct a basic_resolver from another.
/**
* This constructor moves a resolver from one object to another.
*
* @param other The other basic_resolver object from which the move will
* occur.
*
* @note Following the move, the moved-from object is in the same state as if
* constructed using the @c basic_resolver(const executor_type&) constructor.
*/
template <typename Executor1>
basic_resolver(basic_resolver<InternetProtocol, Executor1>&& other,
constraint_t<
is_convertible<Executor1, Executor>::value
> = 0)
: impl_(std::move(other.impl_))
{
}
/// Move-assign a basic_resolver from another.
/**
* This assignment operator moves a resolver from one object to another.
* Cancels any outstanding asynchronous operations associated with the target
* object.
*
* @param other The other basic_resolver object from which the move will
* occur.
*
* @note Following the move, the moved-from object is in the same state as if
* constructed using the @c basic_resolver(const executor_type&) constructor.
*/
basic_resolver& operator=(basic_resolver&& other)
{
impl_ = std::move(other.impl_);
return *this;
}
/// Move-assign a basic_resolver from another.
/**
* This assignment operator moves a resolver from one object to another.
* Cancels any outstanding asynchronous operations associated with the target
* object.
*
* @param other The other basic_resolver object from which the move will
* occur.
*
* @note Following the move, the moved-from object is in the same state as if
* constructed using the @c basic_resolver(const executor_type&) constructor.
*/
template <typename Executor1>
constraint_t<
is_convertible<Executor1, Executor>::value,
basic_resolver&
> operator=(basic_resolver<InternetProtocol, Executor1>&& other)
{
basic_resolver tmp(std::move(other));
impl_ = std::move(tmp.impl_);
return *this;
}
/// Destroys the resolver.
/**
* This function destroys the resolver, cancelling any outstanding
* asynchronous wait operations associated with the resolver as if by calling
* @c cancel.
*/
~basic_resolver()
{
}
/// Get the executor associated with the object.
executor_type get_executor() noexcept
{
return impl_.get_executor();
}
/// Cancel any asynchronous operations that are waiting on the resolver.
/**
* This function forces the completion of any pending asynchronous
* operations on the host resolver. The handler for each cancelled operation
* will be invoked with the boost::asio::error::operation_aborted error code.
*/
void cancel()
{
return impl_.get_service().cancel(impl_.get_implementation());
}
#if !defined(BOOST_ASIO_NO_DEPRECATED)
/// (Deprecated: Use overload with separate host and service parameters.)
/// Perform forward resolution of a query to a list of entries.
/**
* This function is used to resolve a query into a list of endpoint entries.
*
* @param q A query object that determines what endpoints will be returned.
*
* @returns A range object representing the list of endpoint entries. A
* successful call to this function is guaranteed to return a non-empty
* range.
*
* @throws boost::system::system_error Thrown on failure.
*/
results_type resolve(const query& q)
{
boost::system::error_code ec;
results_type r = impl_.get_service().resolve(
impl_.get_implementation(), q, ec);
boost::asio::detail::throw_error(ec, "resolve");
return r;
}
/// (Deprecated: Use overload with separate host and service parameters.)
/// Perform forward resolution of a query to a list of entries.
/**
* This function is used to resolve a query into a list of endpoint entries.
*
* @param q A query object that determines what endpoints will be returned.
*
* @param ec Set to indicate what error occurred, if any.
*
* @returns A range object representing the list of endpoint entries. An
* empty range is returned if an error occurs. A successful call to this
* function is guaranteed to return a non-empty range.
*/
results_type resolve(const query& q, boost::system::error_code& ec)
{
return impl_.get_service().resolve(impl_.get_implementation(), q, ec);
}
#endif // !defined(BOOST_ASIO_NO_DEPRECATED)
/// Perform forward resolution of a query to a list of entries.
/**
* This function is used to resolve host and service names into a list of
* endpoint entries.
*
* @param host A string identifying a location. May be a descriptive name or
* a numeric address string. If an empty string and the passive flag has been
* specified, the resolved endpoints are suitable for local service binding.
* If an empty string and passive is not specified, the resolved endpoints
* will use the loopback address.
*
* @param service A string identifying the requested service. This may be a
* descriptive name or a numeric string corresponding to a port number. May
* be an empty string, in which case all resolved endpoints will have a port
* number of 0.
*
* @returns A range object representing the list of endpoint entries. A
* successful call to this function is guaranteed to return a non-empty
* range.
*
* @throws boost::system::system_error Thrown on failure.
*
* @note On POSIX systems, host names may be locally defined in the file
* <tt>/etc/hosts</tt>. On Windows, host names may be defined in the file
* <tt>c:\\windows\\system32\\drivers\\etc\\hosts</tt>. Remote host name
* resolution is performed using DNS. Operating systems may use additional
* locations when resolving host names (such as NETBIOS names on Windows).
*
* On POSIX systems, service names are typically defined in the file
* <tt>/etc/services</tt>. On Windows, service names may be found in the file
* <tt>c:\\windows\\system32\\drivers\\etc\\services</tt>. Operating systems
* may use additional locations when resolving service names.
*/
results_type resolve(BOOST_ASIO_STRING_VIEW_PARAM host,
BOOST_ASIO_STRING_VIEW_PARAM service)
{
return resolve(host, service, resolver_base::flags());
}
/// Perform forward resolution of a query to a list of entries.
/**
* This function is used to resolve host and service names into a list of
* endpoint entries.
*
* @param host A string identifying a location. May be a descriptive name or
* a numeric address string. If an empty string and the passive flag has been
* specified, the resolved endpoints are suitable for local service binding.
* If an empty string and passive is not specified, the resolved endpoints
* will use the loopback address.
*
* @param service A string identifying the requested service. This may be a
* descriptive name or a numeric string corresponding to a port number. May
* be an empty string, in which case all resolved endpoints will have a port
* number of 0.
*
* @param ec Set to indicate what error occurred, if any.
*
* @returns A range object representing the list of endpoint entries. An
* empty range is returned if an error occurs. A successful call to this
* function is guaranteed to return a non-empty range.
*
* @note On POSIX systems, host names may be locally defined in the file
* <tt>/etc/hosts</tt>. On Windows, host names may be defined in the file
* <tt>c:\\windows\\system32\\drivers\\etc\\hosts</tt>. Remote host name
* resolution is performed using DNS. Operating systems may use additional
* locations when resolving host names (such as NETBIOS names on Windows).
*
* On POSIX systems, service names are typically defined in the file
* <tt>/etc/services</tt>. On Windows, service names may be found in the file
* <tt>c:\\windows\\system32\\drivers\\etc\\services</tt>. Operating systems
* may use additional locations when resolving service names.
*/
results_type resolve(BOOST_ASIO_STRING_VIEW_PARAM host,
BOOST_ASIO_STRING_VIEW_PARAM service, boost::system::error_code& ec)
{
return resolve(host, service, resolver_base::flags(), ec);
}
/// Perform forward resolution of a query to a list of entries.
/**
* This function is used to resolve host and service names into a list of
* endpoint entries.
*
* @param host A string identifying a location. May be a descriptive name or
* a numeric address string. If an empty string and the passive flag has been
* specified, the resolved endpoints are suitable for local service binding.
* If an empty string and passive is not specified, the resolved endpoints
* will use the loopback address.
*
* @param service A string identifying the requested service. This may be a
* descriptive name or a numeric string corresponding to a port number. May
* be an empty string, in which case all resolved endpoints will have a port
* number of 0.
*
* @param resolve_flags A set of flags that determine how name resolution
* should be performed. The default flags are suitable for communication with
* remote hosts. See the @ref resolver_base documentation for the set of
* available flags.
*
* @returns A range object representing the list of endpoint entries. A
* successful call to this function is guaranteed to return a non-empty
* range.
*
* @throws boost::system::system_error Thrown on failure.
*
* @note On POSIX systems, host names may be locally defined in the file
* <tt>/etc/hosts</tt>. On Windows, host names may be defined in the file
* <tt>c:\\windows\\system32\\drivers\\etc\\hosts</tt>. Remote host name
* resolution is performed using DNS. Operating systems may use additional
* locations when resolving host names (such as NETBIOS names on Windows).
*
* On POSIX systems, service names are typically defined in the file
* <tt>/etc/services</tt>. On Windows, service names may be found in the file
* <tt>c:\\windows\\system32\\drivers\\etc\\services</tt>. Operating systems
* may use additional locations when resolving service names.
*/
results_type resolve(BOOST_ASIO_STRING_VIEW_PARAM host,
BOOST_ASIO_STRING_VIEW_PARAM service, resolver_base::flags resolve_flags)
{
boost::system::error_code ec;
basic_resolver_query<protocol_type> q(static_cast<std::string>(host),
static_cast<std::string>(service), resolve_flags);
results_type r = impl_.get_service().resolve(
impl_.get_implementation(), q, ec);
boost::asio::detail::throw_error(ec, "resolve");
return r;
}
/// Perform forward resolution of a query to a list of entries.
/**
* This function is used to resolve host and service names into a list of
* endpoint entries.
*
* @param host A string identifying a location. May be a descriptive name or
* a numeric address string. If an empty string and the passive flag has been
* specified, the resolved endpoints are suitable for local service binding.
* If an empty string and passive is not specified, the resolved endpoints
* will use the loopback address.
*
* @param service A string identifying the requested service. This may be a
* descriptive name or a numeric string corresponding to a port number. May
* be an empty string, in which case all resolved endpoints will have a port
* number of 0.
*
* @param resolve_flags A set of flags that determine how name resolution
* should be performed. The default flags are suitable for communication with
* remote hosts. See the @ref resolver_base documentation for the set of
* available flags.
*
* @param ec Set to indicate what error occurred, if any.
*
* @returns A range object representing the list of endpoint entries. An
* empty range is returned if an error occurs. A successful call to this
* function is guaranteed to return a non-empty range.
*
* @note On POSIX systems, host names may be locally defined in the file
* <tt>/etc/hosts</tt>. On Windows, host names may be defined in the file
* <tt>c:\\windows\\system32\\drivers\\etc\\hosts</tt>. Remote host name
* resolution is performed using DNS. Operating systems may use additional
* locations when resolving host names (such as NETBIOS names on Windows).
*
* On POSIX systems, service names are typically defined in the file
* <tt>/etc/services</tt>. On Windows, service names may be found in the file
* <tt>c:\\windows\\system32\\drivers\\etc\\services</tt>. Operating systems
* may use additional locations when resolving service names.
*/
results_type resolve(BOOST_ASIO_STRING_VIEW_PARAM host,
BOOST_ASIO_STRING_VIEW_PARAM service, resolver_base::flags resolve_flags,
boost::system::error_code& ec)
{
basic_resolver_query<protocol_type> q(static_cast<std::string>(host),
static_cast<std::string>(service), resolve_flags);
return impl_.get_service().resolve(impl_.get_implementation(), q, ec);
}
/// Perform forward resolution of a query to a list of entries.
/**
* This function is used to resolve host and service names into a list of
* endpoint entries.
*
* @param protocol A protocol object, normally representing either the IPv4 or
* IPv6 version of an internet protocol.
*
* @param host A string identifying a location. May be a descriptive name or
* a numeric address string. If an empty string and the passive flag has been
* specified, the resolved endpoints are suitable for local service binding.
* If an empty string and passive is not specified, the resolved endpoints
* will use the loopback address.
*
* @param service A string identifying the requested service. This may be a
* descriptive name or a numeric string corresponding to a port number. May
* be an empty string, in which case all resolved endpoints will have a port
* number of 0.
*
* @returns A range object representing the list of endpoint entries. A
* successful call to this function is guaranteed to return a non-empty
* range.
*
* @throws boost::system::system_error Thrown on failure.
*
* @note On POSIX systems, host names may be locally defined in the file
* <tt>/etc/hosts</tt>. On Windows, host names may be defined in the file
* <tt>c:\\windows\\system32\\drivers\\etc\\hosts</tt>. Remote host name
* resolution is performed using DNS. Operating systems may use additional
* locations when resolving host names (such as NETBIOS names on Windows).
*
* On POSIX systems, service names are typically defined in the file
* <tt>/etc/services</tt>. On Windows, service names may be found in the file
* <tt>c:\\windows\\system32\\drivers\\etc\\services</tt>. Operating systems
* may use additional locations when resolving service names.
*/
results_type resolve(const protocol_type& protocol,
BOOST_ASIO_STRING_VIEW_PARAM host, BOOST_ASIO_STRING_VIEW_PARAM service)
{
return resolve(protocol, host, service, resolver_base::flags());
}
/// Perform forward resolution of a query to a list of entries.
/**
* This function is used to resolve host and service names into a list of
* endpoint entries.
*
* @param protocol A protocol object, normally representing either the IPv4 or
* IPv6 version of an internet protocol.
*
* @param host A string identifying a location. May be a descriptive name or
* a numeric address string. If an empty string and the passive flag has been
* specified, the resolved endpoints are suitable for local service binding.
* If an empty string and passive is not specified, the resolved endpoints
* will use the loopback address.
*
* @param service A string identifying the requested service. This may be a
* descriptive name or a numeric string corresponding to a port number. May
* be an empty string, in which case all resolved endpoints will have a port
* number of 0.
*
* @param ec Set to indicate what error occurred, if any.
*
* @returns A range object representing the list of endpoint entries. An
* empty range is returned if an error occurs. A successful call to this
* function is guaranteed to return a non-empty range.
*
* @note On POSIX systems, host names may be locally defined in the file
* <tt>/etc/hosts</tt>. On Windows, host names may be defined in the file
* <tt>c:\\windows\\system32\\drivers\\etc\\hosts</tt>. Remote host name
* resolution is performed using DNS. Operating systems may use additional
* locations when resolving host names (such as NETBIOS names on Windows).
*
* On POSIX systems, service names are typically defined in the file
* <tt>/etc/services</tt>. On Windows, service names may be found in the file
* <tt>c:\\windows\\system32\\drivers\\etc\\services</tt>. Operating systems
* may use additional locations when resolving service names.
*/
results_type resolve(const protocol_type& protocol,
BOOST_ASIO_STRING_VIEW_PARAM host, BOOST_ASIO_STRING_VIEW_PARAM service,
boost::system::error_code& ec)
{
return resolve(protocol, host, service, resolver_base::flags(), ec);
}
/// Perform forward resolution of a query to a list of entries.
/**
* This function is used to resolve host and service names into a list of
* endpoint entries.
*
* @param protocol A protocol object, normally representing either the IPv4 or
* IPv6 version of an internet protocol.
*
* @param host A string identifying a location. May be a descriptive name or
* a numeric address string. If an empty string and the passive flag has been
* specified, the resolved endpoints are suitable for local service binding.
* If an empty string and passive is not specified, the resolved endpoints
* will use the loopback address.
*
* @param service A string identifying the requested service. This may be a
* descriptive name or a numeric string corresponding to a port number. May
* be an empty string, in which case all resolved endpoints will have a port
* number of 0.
*
* @param resolve_flags A set of flags that determine how name resolution
* should be performed. The default flags are suitable for communication with
* remote hosts. See the @ref resolver_base documentation for the set of
* available flags.
*
* @returns A range object representing the list of endpoint entries. A
* successful call to this function is guaranteed to return a non-empty
* range.
*
* @throws boost::system::system_error Thrown on failure.
*
* @note On POSIX systems, host names may be locally defined in the file
* <tt>/etc/hosts</tt>. On Windows, host names may be defined in the file
* <tt>c:\\windows\\system32\\drivers\\etc\\hosts</tt>. Remote host name
* resolution is performed using DNS. Operating systems may use additional
* locations when resolving host names (such as NETBIOS names on Windows).
*
* On POSIX systems, service names are typically defined in the file
* <tt>/etc/services</tt>. On Windows, service names may be found in the file
* <tt>c:\\windows\\system32\\drivers\\etc\\services</tt>. Operating systems
* may use additional locations when resolving service names.
*/
results_type resolve(const protocol_type& protocol,
BOOST_ASIO_STRING_VIEW_PARAM host, BOOST_ASIO_STRING_VIEW_PARAM service,
resolver_base::flags resolve_flags)
{
boost::system::error_code ec;
basic_resolver_query<protocol_type> q(
protocol, static_cast<std::string>(host),
static_cast<std::string>(service), resolve_flags);
results_type r = impl_.get_service().resolve(
impl_.get_implementation(), q, ec);
boost::asio::detail::throw_error(ec, "resolve");
return r;
}
/// Perform forward resolution of a query to a list of entries.
/**
* This function is used to resolve host and service names into a list of
* endpoint entries.
*
* @param protocol A protocol object, normally representing either the IPv4 or
* IPv6 version of an internet protocol.
*
* @param host A string identifying a location. May be a descriptive name or
* a numeric address string. If an empty string and the passive flag has been
* specified, the resolved endpoints are suitable for local service binding.
* If an empty string and passive is not specified, the resolved endpoints
* will use the loopback address.
*
* @param service A string identifying the requested service. This may be a
* descriptive name or a numeric string corresponding to a port number. May
* be an empty string, in which case all resolved endpoints will have a port
* number of 0.
*
* @param resolve_flags A set of flags that determine how name resolution
* should be performed. The default flags are suitable for communication with
* remote hosts. See the @ref resolver_base documentation for the set of
* available flags.
*
* @param ec Set to indicate what error occurred, if any.
*
* @returns A range object representing the list of endpoint entries. An
* empty range is returned if an error occurs. A successful call to this
* function is guaranteed to return a non-empty range.
*
* @note On POSIX systems, host names may be locally defined in the file
* <tt>/etc/hosts</tt>. On Windows, host names may be defined in the file
* <tt>c:\\windows\\system32\\drivers\\etc\\hosts</tt>. Remote host name
* resolution is performed using DNS. Operating systems may use additional
* locations when resolving host names (such as NETBIOS names on Windows).
*
* On POSIX systems, service names are typically defined in the file
* <tt>/etc/services</tt>. On Windows, service names may be found in the file
* <tt>c:\\windows\\system32\\drivers\\etc\\services</tt>. Operating systems
* may use additional locations when resolving service names.
*/
results_type resolve(const protocol_type& protocol,
BOOST_ASIO_STRING_VIEW_PARAM host, BOOST_ASIO_STRING_VIEW_PARAM service,
resolver_base::flags resolve_flags, boost::system::error_code& ec)
{
basic_resolver_query<protocol_type> q(
protocol, static_cast<std::string>(host),
static_cast<std::string>(service), resolve_flags);
return impl_.get_service().resolve(impl_.get_implementation(), q, ec);
}
#if !defined(BOOST_ASIO_NO_DEPRECATED)
/// (Deprecated: Use overload with separate host and service parameters.)
/// Asynchronously perform forward resolution of a query to a list of entries.
/**
* This function is used to asynchronously resolve a query into a list of
* endpoint entries. It is an initiating function for an @ref
* asynchronous_operation, and always returns immediately.
*
* @param q A query object that determines what endpoints will be returned.
*
* @param token The @ref completion_token that will be used to produce a
* completion handler, which will be called when the resolve completes.
* Potential completion tokens include @ref use_future, @ref use_awaitable,
* @ref yield_context, or a function object with the correct completion
* signature. The function signature of the completion handler must be:
* @code void handler(
* const boost::system::error_code& error, // Result of operation.
* resolver::results_type results // Resolved endpoints as a range.
* ); @endcode
* Regardless of whether the asynchronous operation completes immediately or
* not, the completion handler will not be invoked from within this function.
* On immediate completion, invocation of the handler will be performed in a
* manner equivalent to using boost::asio::post().
*
* A successful resolve operation is guaranteed to pass a non-empty range to
* the handler.
*
* @par Completion Signature
* @code void(boost::system::error_code, results_type) @endcode
*/
template <
BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
results_type)) ResolveToken = default_completion_token_t<executor_type>>
auto async_resolve(const query& q,
ResolveToken&& token = default_completion_token_t<executor_type>())
-> decltype(
boost::asio::async_initiate<ResolveToken,
void (boost::system::error_code, results_type)>(
declval<initiate_async_resolve>(), token, q))
{
return boost::asio::async_initiate<ResolveToken,
void (boost::system::error_code, results_type)>(
initiate_async_resolve(this), token, q);
}
#endif // !defined(BOOST_ASIO_NO_DEPRECATED)
/// Asynchronously perform forward resolution of a query to a list of entries.
/**
* This function is used to resolve host and service names into a list of
* endpoint entries.
*
* @param host A string identifying a location. May be a descriptive name or
* a numeric address string. If an empty string and the passive flag has been
* specified, the resolved endpoints are suitable for local service binding.
* If an empty string and passive is not specified, the resolved endpoints
* will use the loopback address.
*
* @param service A string identifying the requested service. This may be a
* descriptive name or a numeric string corresponding to a port number. May
* be an empty string, in which case all resolved endpoints will have a port
* number of 0.
*
* @param token The @ref completion_token that will be used to produce a
* completion handler, which will be called when the resolve completes.
* Potential completion tokens include @ref use_future, @ref use_awaitable,
* @ref yield_context, or a function object with the correct completion
* signature. The function signature of the completion handler must be:
* @code void handler(
* const boost::system::error_code& error, // Result of operation.
* resolver::results_type results // Resolved endpoints as a range.
* ); @endcode
* Regardless of whether the asynchronous operation completes immediately or
* not, the completion handler will not be invoked from within this function.
* On immediate completion, invocation of the handler will be performed in a
* manner equivalent to using boost::asio::post().
*
* A successful resolve operation is guaranteed to pass a non-empty range to
* the handler.
*
* @par Completion Signature
* @code void(boost::system::error_code, results_type) @endcode
*
* @note On POSIX systems, host names may be locally defined in the file
* <tt>/etc/hosts</tt>. On Windows, host names may be defined in the file
* <tt>c:\\windows\\system32\\drivers\\etc\\hosts</tt>. Remote host name
* resolution is performed using DNS. Operating systems may use additional
* locations when resolving host names (such as NETBIOS names on Windows).
*
* On POSIX systems, service names are typically defined in the file
* <tt>/etc/services</tt>. On Windows, service names may be found in the file
* <tt>c:\\windows\\system32\\drivers\\etc\\services</tt>. Operating systems
* may use additional locations when resolving service names.
*/
template <
BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
results_type)) ResolveToken = default_completion_token_t<executor_type>>
auto async_resolve(BOOST_ASIO_STRING_VIEW_PARAM host,
BOOST_ASIO_STRING_VIEW_PARAM service,
ResolveToken&& token = default_completion_token_t<executor_type>())
-> decltype(
boost::asio::async_initiate<ResolveToken,
void (boost::system::error_code, results_type)>(
declval<initiate_async_resolve>(), token,
declval<basic_resolver_query<protocol_type>&>()))
{
return async_resolve(host, service, resolver_base::flags(),
static_cast<ResolveToken&&>(token));
}
/// Asynchronously perform forward resolution of a query to a list of entries.
/**
* This function is used to resolve host and service names into a list of
* endpoint entries. It is an initiating function for an @ref
* asynchronous_operation, and always returns immediately.
*
* @param host A string identifying a location. May be a descriptive name or
* a numeric address string. If an empty string and the passive flag has been
* specified, the resolved endpoints are suitable for local service binding.
* If an empty string and passive is not specified, the resolved endpoints
* will use the loopback address.
*
* @param service A string identifying the requested service. This may be a
* descriptive name or a numeric string corresponding to a port number. May
* be an empty string, in which case all resolved endpoints will have a port
* number of 0.
*
* @param resolve_flags A set of flags that determine how name resolution
* should be performed. The default flags are suitable for communication with
* remote hosts. See the @ref resolver_base documentation for the set of
* available flags.
*
* @param token The @ref completion_token that will be used to produce a
* completion handler, which will be called when the resolve completes.
* Potential completion tokens include @ref use_future, @ref use_awaitable,
* @ref yield_context, or a function object with the correct completion
* signature. The function signature of the completion handler must be:
* @code void handler(
* const boost::system::error_code& error, // Result of operation.
* resolver::results_type results // Resolved endpoints as a range.
* ); @endcode
* Regardless of whether the asynchronous operation completes immediately or
* not, the completion handler will not be invoked from within this function.
* On immediate completion, invocation of the handler will be performed in a
* manner equivalent to using boost::asio::post().
*
* A successful resolve operation is guaranteed to pass a non-empty range to
* the handler.
*
* @par Completion Signature
* @code void(boost::system::error_code, results_type) @endcode
*
* @note On POSIX systems, host names may be locally defined in the file
* <tt>/etc/hosts</tt>. On Windows, host names may be defined in the file
* <tt>c:\\windows\\system32\\drivers\\etc\\hosts</tt>. Remote host name
* resolution is performed using DNS. Operating systems may use additional
* locations when resolving host names (such as NETBIOS names on Windows).
*
* On POSIX systems, service names are typically defined in the file
* <tt>/etc/services</tt>. On Windows, service names may be found in the file
* <tt>c:\\windows\\system32\\drivers\\etc\\services</tt>. Operating systems
* may use additional locations when resolving service names.
*/
template <
BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
results_type)) ResolveToken = default_completion_token_t<executor_type>>
auto async_resolve(BOOST_ASIO_STRING_VIEW_PARAM host,
BOOST_ASIO_STRING_VIEW_PARAM service, resolver_base::flags resolve_flags,
ResolveToken&& token = default_completion_token_t<executor_type>())
-> decltype(
boost::asio::async_initiate<ResolveToken,
void (boost::system::error_code, results_type)>(
declval<initiate_async_resolve>(), token,
declval<basic_resolver_query<protocol_type>&>()))
{
basic_resolver_query<protocol_type> q(static_cast<std::string>(host),
static_cast<std::string>(service), resolve_flags);
return boost::asio::async_initiate<ResolveToken,
void (boost::system::error_code, results_type)>(
initiate_async_resolve(this), token, q);
}
/// Asynchronously perform forward resolution of a query to a list of entries.
/**
* This function is used to resolve host and service names into a list of
* endpoint entries. It is an initiating function for an @ref
* asynchronous_operation, and always returns immediately.
*
* @param protocol A protocol object, normally representing either the IPv4 or
* IPv6 version of an internet protocol.
*
* @param host A string identifying a location. May be a descriptive name or
* a numeric address string. If an empty string and the passive flag has been
* specified, the resolved endpoints are suitable for local service binding.
* If an empty string and passive is not specified, the resolved endpoints
* will use the loopback address.
*
* @param service A string identifying the requested service. This may be a
* descriptive name or a numeric string corresponding to a port number. May
* be an empty string, in which case all resolved endpoints will have a port
* number of 0.
*
* @param token The @ref completion_token that will be used to produce a
* completion handler, which will be called when the resolve completes.
* Potential completion tokens include @ref use_future, @ref use_awaitable,
* @ref yield_context, or a function object with the correct completion
* signature. The function signature of the completion handler must be:
* @code void handler(
* const boost::system::error_code& error, // Result of operation.
* resolver::results_type results // Resolved endpoints as a range.
* ); @endcode
* Regardless of whether the asynchronous operation completes immediately or
* not, the completion handler will not be invoked from within this function.
* On immediate completion, invocation of the handler will be performed in a
* manner equivalent to using boost::asio::post().
*
* A successful resolve operation is guaranteed to pass a non-empty range to
* the handler.
*
* @par Completion Signature
* @code void(boost::system::error_code, results_type) @endcode
*
* @note On POSIX systems, host names may be locally defined in the file
* <tt>/etc/hosts</tt>. On Windows, host names may be defined in the file
* <tt>c:\\windows\\system32\\drivers\\etc\\hosts</tt>. Remote host name
* resolution is performed using DNS. Operating systems may use additional
* locations when resolving host names (such as NETBIOS names on Windows).
*
* On POSIX systems, service names are typically defined in the file
* <tt>/etc/services</tt>. On Windows, service names may be found in the file
* <tt>c:\\windows\\system32\\drivers\\etc\\services</tt>. Operating systems
* may use additional locations when resolving service names.
*/
template <
BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
results_type)) ResolveToken = default_completion_token_t<executor_type>>
auto async_resolve(const protocol_type& protocol,
BOOST_ASIO_STRING_VIEW_PARAM host, BOOST_ASIO_STRING_VIEW_PARAM service,
ResolveToken&& token = default_completion_token_t<executor_type>())
-> decltype(
boost::asio::async_initiate<ResolveToken,
void (boost::system::error_code, results_type)>(
declval<initiate_async_resolve>(), token,
declval<basic_resolver_query<protocol_type>&>()))
{
return async_resolve(protocol, host, service, resolver_base::flags(),
static_cast<ResolveToken&&>(token));
}
/// Asynchronously perform forward resolution of a query to a list of entries.
/**
* This function is used to resolve host and service names into a list of
* endpoint entries. It is an initiating function for an @ref
* asynchronous_operation, and always returns immediately.
*
* @param protocol A protocol object, normally representing either the IPv4 or
* IPv6 version of an internet protocol.
*
* @param host A string identifying a location. May be a descriptive name or
* a numeric address string. If an empty string and the passive flag has been
* specified, the resolved endpoints are suitable for local service binding.
* If an empty string and passive is not specified, the resolved endpoints
* will use the loopback address.
*
* @param service A string identifying the requested service. This may be a
* descriptive name or a numeric string corresponding to a port number. May
* be an empty string, in which case all resolved endpoints will have a port
* number of 0.
*
* @param resolve_flags A set of flags that determine how name resolution
* should be performed. The default flags are suitable for communication with
* remote hosts. See the @ref resolver_base documentation for the set of
* available flags.
*
* @param token The @ref completion_token that will be used to produce a
* completion handler, which will be called when the resolve completes.
* Potential completion tokens include @ref use_future, @ref use_awaitable,
* @ref yield_context, or a function object with the correct completion
* signature. The function signature of the completion handler must be:
* @code void handler(
* const boost::system::error_code& error, // Result of operation.
* resolver::results_type results // Resolved endpoints as a range.
* ); @endcode
* Regardless of whether the asynchronous operation completes immediately or
* not, the completion handler will not be invoked from within this function.
* On immediate completion, invocation of the handler will be performed in a
* manner equivalent to using boost::asio::post().
*
* A successful resolve operation is guaranteed to pass a non-empty range to
* the handler.
*
* @par Completion Signature
* @code void(boost::system::error_code, results_type) @endcode
*
* @note On POSIX systems, host names may be locally defined in the file
* <tt>/etc/hosts</tt>. On Windows, host names may be defined in the file
* <tt>c:\\windows\\system32\\drivers\\etc\\hosts</tt>. Remote host name
* resolution is performed using DNS. Operating systems may use additional
* locations when resolving host names (such as NETBIOS names on Windows).
*
* On POSIX systems, service names are typically defined in the file
* <tt>/etc/services</tt>. On Windows, service names may be found in the file
* <tt>c:\\windows\\system32\\drivers\\etc\\services</tt>. Operating systems
* may use additional locations when resolving service names.
*/
template <
BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
results_type)) ResolveToken = default_completion_token_t<executor_type>>
auto async_resolve(const protocol_type& protocol,
BOOST_ASIO_STRING_VIEW_PARAM host, BOOST_ASIO_STRING_VIEW_PARAM service,
resolver_base::flags resolve_flags,
ResolveToken&& token = default_completion_token_t<executor_type>())
-> decltype(
boost::asio::async_initiate<ResolveToken,
void (boost::system::error_code, results_type)>(
declval<initiate_async_resolve>(), token,
declval<basic_resolver_query<protocol_type>&>()))
{
basic_resolver_query<protocol_type> q(
protocol, static_cast<std::string>(host),
static_cast<std::string>(service), resolve_flags);
return boost::asio::async_initiate<ResolveToken,
void (boost::system::error_code, results_type)>(
initiate_async_resolve(this), token, q);
}
/// Perform reverse resolution of an endpoint to a list of entries.
/**
* This function is used to resolve an endpoint into a list of endpoint
* entries.
*
* @param e An endpoint object that determines what endpoints will be
* returned.
*
* @returns A range object representing the list of endpoint entries. A
* successful call to this function is guaranteed to return a non-empty
* range.
*
* @throws boost::system::system_error Thrown on failure.
*/
results_type resolve(const endpoint_type& e)
{
boost::system::error_code ec;
results_type i = impl_.get_service().resolve(
impl_.get_implementation(), e, ec);
boost::asio::detail::throw_error(ec, "resolve");
return i;
}
/// Perform reverse resolution of an endpoint to a list of entries.
/**
* This function is used to resolve an endpoint into a list of endpoint
* entries.
*
* @param e An endpoint object that determines what endpoints will be
* returned.
*
* @param ec Set to indicate what error occurred, if any.
*
* @returns A range object representing the list of endpoint entries. An
* empty range is returned if an error occurs. A successful call to this
* function is guaranteed to return a non-empty range.
*/
results_type resolve(const endpoint_type& e, boost::system::error_code& ec)
{
return impl_.get_service().resolve(impl_.get_implementation(), e, ec);
}
/// Asynchronously perform reverse resolution of an endpoint to a list of
/// entries.
/**
* This function is used to asynchronously resolve an endpoint into a list of
* endpoint entries. It is an initiating function for an @ref
* asynchronous_operation, and always returns immediately.
*
* @param e An endpoint object that determines what endpoints will be
* returned.
*
* @param token The @ref completion_token that will be used to produce a
* completion handler, which will be called when the resolve completes.
* Potential completion tokens include @ref use_future, @ref use_awaitable,
* @ref yield_context, or a function object with the correct completion
* signature. The function signature of the completion handler must be:
* @code void handler(
* const boost::system::error_code& error, // Result of operation.
* resolver::results_type results // Resolved endpoints as a range.
* ); @endcode
* Regardless of whether the asynchronous operation completes immediately or
* not, the completion handler will not be invoked from within this function.
* On immediate completion, invocation of the handler will be performed in a
* manner equivalent to using boost::asio::post().
*
* A successful resolve operation is guaranteed to pass a non-empty range to
* the handler.
*
* @par Completion Signature
* @code void(boost::system::error_code, results_type) @endcode
*/
template <
BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
results_type)) ResolveToken = default_completion_token_t<executor_type>>
auto async_resolve(const endpoint_type& e,
ResolveToken&& token = default_completion_token_t<executor_type>())
-> decltype(
boost::asio::async_initiate<ResolveToken,
void (boost::system::error_code, results_type)>(
declval<initiate_async_resolve>(), token, e))
{
return boost::asio::async_initiate<ResolveToken,
void (boost::system::error_code, results_type)>(
initiate_async_resolve(this), token, e);
}
private:
// Disallow copying and assignment.
basic_resolver(const basic_resolver&) = delete;
basic_resolver& operator=(const basic_resolver&) = delete;
class initiate_async_resolve
{
public:
typedef Executor executor_type;
explicit initiate_async_resolve(basic_resolver* self)
: self_(self)
{
}
executor_type get_executor() const noexcept
{
return self_->get_executor();
}
template <typename ResolveHandler, typename Query>
void operator()(ResolveHandler&& handler,
const Query& q) const
{
// If you get an error on the following line it means that your handler
// does not meet the documented type requirements for a ResolveHandler.
BOOST_ASIO_RESOLVE_HANDLER_CHECK(
ResolveHandler, handler, results_type) type_check;
boost::asio::detail::non_const_lvalue<ResolveHandler> handler2(handler);
self_->impl_.get_service().async_resolve(
self_->impl_.get_implementation(), q,
handler2.value, self_->impl_.get_executor());
}
private:
basic_resolver* self_;
};
# if defined(BOOST_ASIO_WINDOWS_RUNTIME)
boost::asio::detail::io_object_impl<
boost::asio::detail::winrt_resolver_service<InternetProtocol>,
Executor> impl_;
# else
boost::asio::detail::io_object_impl<
boost::asio::detail::resolver_service<InternetProtocol>,
Executor> impl_;
# endif
};
} // namespace ip
} // namespace asio
} // namespace boost
#include <boost/asio/detail/pop_options.hpp>
#endif // BOOST_ASIO_IP_BASIC_RESOLVER_HPP
| hpp |
asio | data/projects/asio/include/boost/asio/ip/impl/network_v4.hpp | //
// ip/impl/network_v4.hpp
// ~~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com)
// Copyright (c) 2014 Oliver Kowalke (oliver dot kowalke at gmail dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_ASIO_IP_IMPL_NETWORK_V4_HPP
#define BOOST_ASIO_IP_IMPL_NETWORK_V4_HPP
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#if !defined(BOOST_ASIO_NO_IOSTREAM)
#include <boost/asio/detail/throw_error.hpp>
#include <boost/asio/detail/push_options.hpp>
namespace boost {
namespace asio {
namespace ip {
template <typename Elem, typename Traits>
std::basic_ostream<Elem, Traits>& operator<<(
std::basic_ostream<Elem, Traits>& os, const network_v4& addr)
{
boost::system::error_code ec;
std::string s = addr.to_string(ec);
if (ec)
{
if (os.exceptions() & std::basic_ostream<Elem, Traits>::failbit)
boost::asio::detail::throw_error(ec);
else
os.setstate(std::basic_ostream<Elem, Traits>::failbit);
}
else
for (std::string::iterator i = s.begin(); i != s.end(); ++i)
os << os.widen(*i);
return os;
}
} // namespace ip
} // namespace asio
} // namespace boost
#include <boost/asio/detail/pop_options.hpp>
#endif // !defined(BOOST_ASIO_NO_IOSTREAM)
#endif // BOOST_ASIO_IP_IMPL_NETWORK_V4_HPP
| hpp |
asio | data/projects/asio/include/boost/asio/ip/impl/address.hpp | //
// ip/impl/address.hpp
// ~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_ASIO_IP_IMPL_ADDRESS_HPP
#define BOOST_ASIO_IP_IMPL_ADDRESS_HPP
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#if !defined(BOOST_ASIO_NO_IOSTREAM)
#include <boost/asio/detail/throw_error.hpp>
#include <boost/asio/detail/push_options.hpp>
namespace boost {
namespace asio {
namespace ip {
#if !defined(BOOST_ASIO_NO_DEPRECATED)
inline address address::from_string(const char* str)
{
return boost::asio::ip::make_address(str);
}
inline address address::from_string(
const char* str, boost::system::error_code& ec)
{
return boost::asio::ip::make_address(str, ec);
}
inline address address::from_string(const std::string& str)
{
return boost::asio::ip::make_address(str);
}
inline address address::from_string(
const std::string& str, boost::system::error_code& ec)
{
return boost::asio::ip::make_address(str, ec);
}
#endif // !defined(BOOST_ASIO_NO_DEPRECATED)
template <typename Elem, typename Traits>
std::basic_ostream<Elem, Traits>& operator<<(
std::basic_ostream<Elem, Traits>& os, const address& addr)
{
return os << addr.to_string().c_str();
}
} // namespace ip
} // namespace asio
} // namespace boost
#include <boost/asio/detail/pop_options.hpp>
#endif // !defined(BOOST_ASIO_NO_IOSTREAM)
#endif // BOOST_ASIO_IP_IMPL_ADDRESS_HPP
| hpp |
asio | data/projects/asio/include/boost/asio/ip/impl/address_v4.hpp | //
// ip/impl/address_v4.hpp
// ~~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_ASIO_IP_IMPL_ADDRESS_V4_HPP
#define BOOST_ASIO_IP_IMPL_ADDRESS_V4_HPP
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#if !defined(BOOST_ASIO_NO_IOSTREAM)
#include <boost/asio/detail/throw_error.hpp>
#include <boost/asio/detail/push_options.hpp>
namespace boost {
namespace asio {
namespace ip {
#if !defined(BOOST_ASIO_NO_DEPRECATED)
inline address_v4 address_v4::from_string(const char* str)
{
return boost::asio::ip::make_address_v4(str);
}
inline address_v4 address_v4::from_string(
const char* str, boost::system::error_code& ec)
{
return boost::asio::ip::make_address_v4(str, ec);
}
inline address_v4 address_v4::from_string(const std::string& str)
{
return boost::asio::ip::make_address_v4(str);
}
inline address_v4 address_v4::from_string(
const std::string& str, boost::system::error_code& ec)
{
return boost::asio::ip::make_address_v4(str, ec);
}
#endif // !defined(BOOST_ASIO_NO_DEPRECATED)
template <typename Elem, typename Traits>
std::basic_ostream<Elem, Traits>& operator<<(
std::basic_ostream<Elem, Traits>& os, const address_v4& addr)
{
return os << addr.to_string().c_str();
}
} // namespace ip
} // namespace asio
} // namespace boost
#include <boost/asio/detail/pop_options.hpp>
#endif // !defined(BOOST_ASIO_NO_IOSTREAM)
#endif // BOOST_ASIO_IP_IMPL_ADDRESS_V4_HPP
| hpp |
asio | data/projects/asio/include/boost/asio/ip/impl/basic_endpoint.hpp | //
// ip/impl/basic_endpoint.hpp
// ~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_ASIO_IP_IMPL_BASIC_ENDPOINT_HPP
#define BOOST_ASIO_IP_IMPL_BASIC_ENDPOINT_HPP
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#if !defined(BOOST_ASIO_NO_IOSTREAM)
#include <boost/asio/detail/throw_error.hpp>
#include <boost/asio/detail/push_options.hpp>
namespace boost {
namespace asio {
namespace ip {
template <typename Elem, typename Traits, typename InternetProtocol>
std::basic_ostream<Elem, Traits>& operator<<(
std::basic_ostream<Elem, Traits>& os,
const basic_endpoint<InternetProtocol>& endpoint)
{
boost::asio::ip::detail::endpoint tmp_ep(endpoint.address(), endpoint.port());
return os << tmp_ep.to_string().c_str();
}
} // namespace ip
} // namespace asio
} // namespace boost
#include <boost/asio/detail/pop_options.hpp>
#endif // !defined(BOOST_ASIO_NO_IOSTREAM)
#endif // BOOST_ASIO_IP_IMPL_BASIC_ENDPOINT_HPP
| hpp |
asio | data/projects/asio/include/boost/asio/ip/impl/address_v6.hpp | //
// ip/impl/address_v6.hpp
// ~~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_ASIO_IP_IMPL_ADDRESS_V6_HPP
#define BOOST_ASIO_IP_IMPL_ADDRESS_V6_HPP
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#if !defined(BOOST_ASIO_NO_IOSTREAM)
#include <boost/asio/detail/throw_error.hpp>
#include <boost/asio/detail/push_options.hpp>
namespace boost {
namespace asio {
namespace ip {
#if !defined(BOOST_ASIO_NO_DEPRECATED)
inline address_v6 address_v6::from_string(const char* str)
{
return boost::asio::ip::make_address_v6(str);
}
inline address_v6 address_v6::from_string(
const char* str, boost::system::error_code& ec)
{
return boost::asio::ip::make_address_v6(str, ec);
}
inline address_v6 address_v6::from_string(const std::string& str)
{
return boost::asio::ip::make_address_v6(str);
}
inline address_v6 address_v6::from_string(
const std::string& str, boost::system::error_code& ec)
{
return boost::asio::ip::make_address_v6(str, ec);
}
#endif // !defined(BOOST_ASIO_NO_DEPRECATED)
template <typename Elem, typename Traits>
std::basic_ostream<Elem, Traits>& operator<<(
std::basic_ostream<Elem, Traits>& os, const address_v6& addr)
{
return os << addr.to_string().c_str();
}
} // namespace ip
} // namespace asio
} // namespace boost
#include <boost/asio/detail/pop_options.hpp>
#endif // !defined(BOOST_ASIO_NO_IOSTREAM)
#endif // BOOST_ASIO_IP_IMPL_ADDRESS_V6_HPP
| hpp |
asio | data/projects/asio/include/boost/asio/ip/impl/network_v6.hpp | //
// ip/impl/network_v6.hpp
// ~~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_ASIO_IP_IMPL_NETWORK_V6_HPP
#define BOOST_ASIO_IP_IMPL_NETWORK_V6_HPP
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#if !defined(BOOST_ASIO_NO_IOSTREAM)
#include <boost/asio/detail/throw_error.hpp>
#include <boost/asio/detail/push_options.hpp>
namespace boost {
namespace asio {
namespace ip {
template <typename Elem, typename Traits>
std::basic_ostream<Elem, Traits>& operator<<(
std::basic_ostream<Elem, Traits>& os, const network_v6& addr)
{
boost::system::error_code ec;
std::string s = addr.to_string(ec);
if (ec)
{
if (os.exceptions() & std::basic_ostream<Elem, Traits>::failbit)
boost::asio::detail::throw_error(ec);
else
os.setstate(std::basic_ostream<Elem, Traits>::failbit);
}
else
for (std::string::iterator i = s.begin(); i != s.end(); ++i)
os << os.widen(*i);
return os;
}
} // namespace ip
} // namespace asio
} // namespace boost
#include <boost/asio/detail/pop_options.hpp>
#endif // !defined(BOOST_ASIO_NO_IOSTREAM)
#endif // BOOST_ASIO_IP_IMPL_NETWORK_V6_HPP
| hpp |
asio | data/projects/asio/include/boost/asio/ip/detail/endpoint.hpp | //
// ip/detail/endpoint.hpp
// ~~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_ASIO_IP_DETAIL_ENDPOINT_HPP
#define BOOST_ASIO_IP_DETAIL_ENDPOINT_HPP
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include <boost/asio/detail/config.hpp>
#include <string>
#include <boost/asio/detail/socket_types.hpp>
#include <boost/asio/detail/winsock_init.hpp>
#include <boost/system/error_code.hpp>
#include <boost/asio/ip/address.hpp>
#include <boost/asio/detail/push_options.hpp>
namespace boost {
namespace asio {
namespace ip {
namespace detail {
// Helper class for implementating an IP endpoint.
class endpoint
{
public:
// Default constructor.
BOOST_ASIO_DECL endpoint() noexcept;
// Construct an endpoint using a family and port number.
BOOST_ASIO_DECL endpoint(int family,
unsigned short port_num) noexcept;
// Construct an endpoint using an address and port number.
BOOST_ASIO_DECL endpoint(const boost::asio::ip::address& addr,
unsigned short port_num) noexcept;
// Copy constructor.
endpoint(const endpoint& other) noexcept
: data_(other.data_)
{
}
// Assign from another endpoint.
endpoint& operator=(const endpoint& other) noexcept
{
data_ = other.data_;
return *this;
}
// Get the underlying endpoint in the native type.
boost::asio::detail::socket_addr_type* data() noexcept
{
return &data_.base;
}
// Get the underlying endpoint in the native type.
const boost::asio::detail::socket_addr_type* data() const noexcept
{
return &data_.base;
}
// Get the underlying size of the endpoint in the native type.
std::size_t size() const noexcept
{
if (is_v4())
return sizeof(boost::asio::detail::sockaddr_in4_type);
else
return sizeof(boost::asio::detail::sockaddr_in6_type);
}
// Set the underlying size of the endpoint in the native type.
BOOST_ASIO_DECL void resize(std::size_t new_size);
// Get the capacity of the endpoint in the native type.
std::size_t capacity() const noexcept
{
return sizeof(data_);
}
// Get the port associated with the endpoint.
BOOST_ASIO_DECL unsigned short port() const noexcept;
// Set the port associated with the endpoint.
BOOST_ASIO_DECL void port(unsigned short port_num) noexcept;
// Get the IP address associated with the endpoint.
BOOST_ASIO_DECL boost::asio::ip::address address() const noexcept;
// Set the IP address associated with the endpoint.
BOOST_ASIO_DECL void address(
const boost::asio::ip::address& addr) noexcept;
// Compare two endpoints for equality.
BOOST_ASIO_DECL friend bool operator==(const endpoint& e1,
const endpoint& e2) noexcept;
// Compare endpoints for ordering.
BOOST_ASIO_DECL friend bool operator<(const endpoint& e1,
const endpoint& e2) noexcept;
// Determine whether the endpoint is IPv4.
bool is_v4() const noexcept
{
return data_.base.sa_family == BOOST_ASIO_OS_DEF(AF_INET);
}
#if !defined(BOOST_ASIO_NO_IOSTREAM)
// Convert to a string.
BOOST_ASIO_DECL std::string to_string() const;
#endif // !defined(BOOST_ASIO_NO_IOSTREAM)
private:
// The underlying IP socket address.
union data_union
{
boost::asio::detail::socket_addr_type base;
boost::asio::detail::sockaddr_in4_type v4;
boost::asio::detail::sockaddr_in6_type v6;
} data_;
};
} // namespace detail
} // namespace ip
} // namespace asio
} // namespace boost
#include <boost/asio/detail/pop_options.hpp>
#if defined(BOOST_ASIO_HEADER_ONLY)
# include <boost/asio/ip/detail/impl/endpoint.ipp>
#endif // defined(BOOST_ASIO_HEADER_ONLY)
#endif // BOOST_ASIO_IP_DETAIL_ENDPOINT_HPP
| hpp |
asio | data/projects/asio/include/boost/asio/ip/detail/socket_option.hpp | //
// detail/socket_option.hpp
// ~~~~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_ASIO_IP_DETAIL_SOCKET_OPTION_HPP
#define BOOST_ASIO_IP_DETAIL_SOCKET_OPTION_HPP
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include <boost/asio/detail/config.hpp>
#include <cstddef>
#include <cstring>
#include <stdexcept>
#include <boost/asio/detail/socket_ops.hpp>
#include <boost/asio/detail/socket_types.hpp>
#include <boost/asio/detail/throw_exception.hpp>
#include <boost/asio/ip/address.hpp>
#include <boost/asio/detail/push_options.hpp>
namespace boost {
namespace asio {
namespace ip {
namespace detail {
namespace socket_option {
// Helper template for implementing multicast enable loopback options.
template <int IPv4_Level, int IPv4_Name, int IPv6_Level, int IPv6_Name>
class multicast_enable_loopback
{
public:
#if defined(__sun) || defined(__osf__)
typedef unsigned char ipv4_value_type;
typedef unsigned char ipv6_value_type;
#elif defined(_AIX) || defined(__hpux) || defined(__QNXNTO__)
typedef unsigned char ipv4_value_type;
typedef unsigned int ipv6_value_type;
#else
typedef int ipv4_value_type;
typedef int ipv6_value_type;
#endif
// Default constructor.
multicast_enable_loopback()
: ipv4_value_(0),
ipv6_value_(0)
{
}
// Construct with a specific option value.
explicit multicast_enable_loopback(bool v)
: ipv4_value_(v ? 1 : 0),
ipv6_value_(v ? 1 : 0)
{
}
// Set the value of the boolean.
multicast_enable_loopback& operator=(bool v)
{
ipv4_value_ = v ? 1 : 0;
ipv6_value_ = v ? 1 : 0;
return *this;
}
// Get the current value of the boolean.
bool value() const
{
return !!ipv4_value_;
}
// Convert to bool.
operator bool() const
{
return !!ipv4_value_;
}
// Test for false.
bool operator!() const
{
return !ipv4_value_;
}
// Get the level of the socket option.
template <typename Protocol>
int level(const Protocol& protocol) const
{
if (protocol.family() == PF_INET6)
return IPv6_Level;
return IPv4_Level;
}
// Get the name of the socket option.
template <typename Protocol>
int name(const Protocol& protocol) const
{
if (protocol.family() == PF_INET6)
return IPv6_Name;
return IPv4_Name;
}
// Get the address of the boolean data.
template <typename Protocol>
void* data(const Protocol& protocol)
{
if (protocol.family() == PF_INET6)
return &ipv6_value_;
return &ipv4_value_;
}
// Get the address of the boolean data.
template <typename Protocol>
const void* data(const Protocol& protocol) const
{
if (protocol.family() == PF_INET6)
return &ipv6_value_;
return &ipv4_value_;
}
// Get the size of the boolean data.
template <typename Protocol>
std::size_t size(const Protocol& protocol) const
{
if (protocol.family() == PF_INET6)
return sizeof(ipv6_value_);
return sizeof(ipv4_value_);
}
// Set the size of the boolean data.
template <typename Protocol>
void resize(const Protocol& protocol, std::size_t s)
{
if (protocol.family() == PF_INET6)
{
if (s != sizeof(ipv6_value_))
{
std::length_error ex("multicast_enable_loopback socket option resize");
boost::asio::detail::throw_exception(ex);
}
ipv4_value_ = ipv6_value_ ? 1 : 0;
}
else
{
if (s != sizeof(ipv4_value_))
{
std::length_error ex("multicast_enable_loopback socket option resize");
boost::asio::detail::throw_exception(ex);
}
ipv6_value_ = ipv4_value_ ? 1 : 0;
}
}
private:
ipv4_value_type ipv4_value_;
ipv6_value_type ipv6_value_;
};
// Helper template for implementing unicast hops options.
template <int IPv4_Level, int IPv4_Name, int IPv6_Level, int IPv6_Name>
class unicast_hops
{
public:
// Default constructor.
unicast_hops()
: value_(0)
{
}
// Construct with a specific option value.
explicit unicast_hops(int v)
: value_(v)
{
}
// Set the value of the option.
unicast_hops& operator=(int v)
{
value_ = v;
return *this;
}
// Get the current value of the option.
int value() const
{
return value_;
}
// Get the level of the socket option.
template <typename Protocol>
int level(const Protocol& protocol) const
{
if (protocol.family() == PF_INET6)
return IPv6_Level;
return IPv4_Level;
}
// Get the name of the socket option.
template <typename Protocol>
int name(const Protocol& protocol) const
{
if (protocol.family() == PF_INET6)
return IPv6_Name;
return IPv4_Name;
}
// Get the address of the data.
template <typename Protocol>
int* data(const Protocol&)
{
return &value_;
}
// Get the address of the data.
template <typename Protocol>
const int* data(const Protocol&) const
{
return &value_;
}
// Get the size of the data.
template <typename Protocol>
std::size_t size(const Protocol&) const
{
return sizeof(value_);
}
// Set the size of the data.
template <typename Protocol>
void resize(const Protocol&, std::size_t s)
{
if (s != sizeof(value_))
{
std::length_error ex("unicast hops socket option resize");
boost::asio::detail::throw_exception(ex);
}
#if defined(__hpux)
if (value_ < 0)
value_ = value_ & 0xFF;
#endif
}
private:
int value_;
};
// Helper template for implementing multicast hops options.
template <int IPv4_Level, int IPv4_Name, int IPv6_Level, int IPv6_Name>
class multicast_hops
{
public:
#if defined(BOOST_ASIO_WINDOWS) && defined(UNDER_CE)
typedef int ipv4_value_type;
#else
typedef unsigned char ipv4_value_type;
#endif
typedef int ipv6_value_type;
// Default constructor.
multicast_hops()
: ipv4_value_(0),
ipv6_value_(0)
{
}
// Construct with a specific option value.
explicit multicast_hops(int v)
{
if (v < 0 || v > 255)
{
std::out_of_range ex("multicast hops value out of range");
boost::asio::detail::throw_exception(ex);
}
ipv4_value_ = (ipv4_value_type)v;
ipv6_value_ = v;
}
// Set the value of the option.
multicast_hops& operator=(int v)
{
if (v < 0 || v > 255)
{
std::out_of_range ex("multicast hops value out of range");
boost::asio::detail::throw_exception(ex);
}
ipv4_value_ = (ipv4_value_type)v;
ipv6_value_ = v;
return *this;
}
// Get the current value of the option.
int value() const
{
return ipv6_value_;
}
// Get the level of the socket option.
template <typename Protocol>
int level(const Protocol& protocol) const
{
if (protocol.family() == PF_INET6)
return IPv6_Level;
return IPv4_Level;
}
// Get the name of the socket option.
template <typename Protocol>
int name(const Protocol& protocol) const
{
if (protocol.family() == PF_INET6)
return IPv6_Name;
return IPv4_Name;
}
// Get the address of the data.
template <typename Protocol>
void* data(const Protocol& protocol)
{
if (protocol.family() == PF_INET6)
return &ipv6_value_;
return &ipv4_value_;
}
// Get the address of the data.
template <typename Protocol>
const void* data(const Protocol& protocol) const
{
if (protocol.family() == PF_INET6)
return &ipv6_value_;
return &ipv4_value_;
}
// Get the size of the data.
template <typename Protocol>
std::size_t size(const Protocol& protocol) const
{
if (protocol.family() == PF_INET6)
return sizeof(ipv6_value_);
return sizeof(ipv4_value_);
}
// Set the size of the data.
template <typename Protocol>
void resize(const Protocol& protocol, std::size_t s)
{
if (protocol.family() == PF_INET6)
{
if (s != sizeof(ipv6_value_))
{
std::length_error ex("multicast hops socket option resize");
boost::asio::detail::throw_exception(ex);
}
if (ipv6_value_ < 0)
ipv4_value_ = 0;
else if (ipv6_value_ > 255)
ipv4_value_ = 255;
else
ipv4_value_ = (ipv4_value_type)ipv6_value_;
}
else
{
if (s != sizeof(ipv4_value_))
{
std::length_error ex("multicast hops socket option resize");
boost::asio::detail::throw_exception(ex);
}
ipv6_value_ = ipv4_value_;
}
}
private:
ipv4_value_type ipv4_value_;
ipv6_value_type ipv6_value_;
};
// Helper template for implementing ip_mreq-based options.
template <int IPv4_Level, int IPv4_Name, int IPv6_Level, int IPv6_Name>
class multicast_request
{
public:
// Default constructor.
multicast_request()
: ipv4_value_(), // Zero-initialisation gives the "any" address.
ipv6_value_() // Zero-initialisation gives the "any" address.
{
}
// Construct with multicast address only.
explicit multicast_request(const address& multicast_address)
: ipv4_value_(), // Zero-initialisation gives the "any" address.
ipv6_value_() // Zero-initialisation gives the "any" address.
{
if (multicast_address.is_v6())
{
using namespace std; // For memcpy.
address_v6 ipv6_address = multicast_address.to_v6();
address_v6::bytes_type bytes = ipv6_address.to_bytes();
memcpy(ipv6_value_.ipv6mr_multiaddr.s6_addr, bytes.data(), 16);
ipv6_value_.ipv6mr_interface = ipv6_address.scope_id();
}
else
{
ipv4_value_.imr_multiaddr.s_addr =
boost::asio::detail::socket_ops::host_to_network_long(
multicast_address.to_v4().to_uint());
ipv4_value_.imr_interface.s_addr =
boost::asio::detail::socket_ops::host_to_network_long(
address_v4::any().to_uint());
}
}
// Construct with multicast address and IPv4 address specifying an interface.
explicit multicast_request(const address_v4& multicast_address,
const address_v4& network_interface = address_v4::any())
: ipv6_value_() // Zero-initialisation gives the "any" address.
{
ipv4_value_.imr_multiaddr.s_addr =
boost::asio::detail::socket_ops::host_to_network_long(
multicast_address.to_uint());
ipv4_value_.imr_interface.s_addr =
boost::asio::detail::socket_ops::host_to_network_long(
network_interface.to_uint());
}
// Construct with multicast address and IPv6 network interface index.
explicit multicast_request(
const address_v6& multicast_address,
unsigned long network_interface = 0)
: ipv4_value_() // Zero-initialisation gives the "any" address.
{
using namespace std; // For memcpy.
address_v6::bytes_type bytes = multicast_address.to_bytes();
memcpy(ipv6_value_.ipv6mr_multiaddr.s6_addr, bytes.data(), 16);
if (network_interface)
ipv6_value_.ipv6mr_interface = network_interface;
else
ipv6_value_.ipv6mr_interface = multicast_address.scope_id();
}
// Get the level of the socket option.
template <typename Protocol>
int level(const Protocol& protocol) const
{
if (protocol.family() == PF_INET6)
return IPv6_Level;
return IPv4_Level;
}
// Get the name of the socket option.
template <typename Protocol>
int name(const Protocol& protocol) const
{
if (protocol.family() == PF_INET6)
return IPv6_Name;
return IPv4_Name;
}
// Get the address of the option data.
template <typename Protocol>
const void* data(const Protocol& protocol) const
{
if (protocol.family() == PF_INET6)
return &ipv6_value_;
return &ipv4_value_;
}
// Get the size of the option data.
template <typename Protocol>
std::size_t size(const Protocol& protocol) const
{
if (protocol.family() == PF_INET6)
return sizeof(ipv6_value_);
return sizeof(ipv4_value_);
}
private:
boost::asio::detail::in4_mreq_type ipv4_value_;
boost::asio::detail::in6_mreq_type ipv6_value_;
};
// Helper template for implementing options that specify a network interface.
template <int IPv4_Level, int IPv4_Name, int IPv6_Level, int IPv6_Name>
class network_interface
{
public:
// Default constructor.
network_interface()
{
ipv4_value_.s_addr =
boost::asio::detail::socket_ops::host_to_network_long(
address_v4::any().to_uint());
ipv6_value_ = 0;
}
// Construct with IPv4 interface.
explicit network_interface(const address_v4& ipv4_interface)
{
ipv4_value_.s_addr =
boost::asio::detail::socket_ops::host_to_network_long(
ipv4_interface.to_uint());
ipv6_value_ = 0;
}
// Construct with IPv6 interface.
explicit network_interface(unsigned int ipv6_interface)
{
ipv4_value_.s_addr =
boost::asio::detail::socket_ops::host_to_network_long(
address_v4::any().to_uint());
ipv6_value_ = ipv6_interface;
}
// Get the level of the socket option.
template <typename Protocol>
int level(const Protocol& protocol) const
{
if (protocol.family() == PF_INET6)
return IPv6_Level;
return IPv4_Level;
}
// Get the name of the socket option.
template <typename Protocol>
int name(const Protocol& protocol) const
{
if (protocol.family() == PF_INET6)
return IPv6_Name;
return IPv4_Name;
}
// Get the address of the option data.
template <typename Protocol>
const void* data(const Protocol& protocol) const
{
if (protocol.family() == PF_INET6)
return &ipv6_value_;
return &ipv4_value_;
}
// Get the size of the option data.
template <typename Protocol>
std::size_t size(const Protocol& protocol) const
{
if (protocol.family() == PF_INET6)
return sizeof(ipv6_value_);
return sizeof(ipv4_value_);
}
private:
boost::asio::detail::in4_addr_type ipv4_value_;
unsigned int ipv6_value_;
};
} // namespace socket_option
} // namespace detail
} // namespace ip
} // namespace asio
} // namespace boost
#include <boost/asio/detail/pop_options.hpp>
#endif // BOOST_ASIO_IP_DETAIL_SOCKET_OPTION_HPP
| hpp |
asio | data/projects/asio/include/boost/asio/posix/basic_stream_descriptor.hpp | //
// posix/basic_stream_descriptor.hpp
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_ASIO_POSIX_BASIC_STREAM_DESCRIPTOR_HPP
#define BOOST_ASIO_POSIX_BASIC_STREAM_DESCRIPTOR_HPP
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include <boost/asio/detail/config.hpp>
#include <boost/asio/posix/basic_descriptor.hpp>
#if defined(BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR) \
|| defined(GENERATING_DOCUMENTATION)
#include <boost/asio/detail/push_options.hpp>
namespace boost {
namespace asio {
namespace posix {
/// Provides stream-oriented descriptor functionality.
/**
* The posix::basic_stream_descriptor class template provides asynchronous and
* blocking stream-oriented descriptor functionality.
*
* @par Thread Safety
* @e Distinct @e objects: Safe.@n
* @e Shared @e objects: Unsafe.
*
* Synchronous @c read_some and @c write_some operations are thread safe with
* respect to each other, if the underlying operating system calls are also
* thread safe. This means that it is permitted to perform concurrent calls to
* these synchronous operations on a single descriptor object. Other synchronous
* operations, such as @c close, are not thread safe.
*
* @par Concepts:
* AsyncReadStream, AsyncWriteStream, Stream, SyncReadStream, SyncWriteStream.
*/
template <typename Executor = any_io_executor>
class basic_stream_descriptor
: public basic_descriptor<Executor>
{
private:
class initiate_async_write_some;
class initiate_async_read_some;
public:
/// The type of the executor associated with the object.
typedef Executor executor_type;
/// Rebinds the descriptor type to another executor.
template <typename Executor1>
struct rebind_executor
{
/// The descriptor type when rebound to the specified executor.
typedef basic_stream_descriptor<Executor1> other;
};
/// The native representation of a descriptor.
typedef typename basic_descriptor<Executor>::native_handle_type
native_handle_type;
/// Construct a stream descriptor without opening it.
/**
* This constructor creates a stream descriptor without opening it. The
* descriptor needs to be opened and then connected or accepted before data
* can be sent or received on it.
*
* @param ex The I/O executor that the descriptor will use, by default, to
* dispatch handlers for any asynchronous operations performed on the
* descriptor.
*/
explicit basic_stream_descriptor(const executor_type& ex)
: basic_descriptor<Executor>(ex)
{
}
/// Construct a stream descriptor without opening it.
/**
* This constructor creates a stream descriptor without opening it. The
* descriptor needs to be opened and then connected or accepted before data
* can be sent or received on it.
*
* @param context An execution context which provides the I/O executor that
* the descriptor will use, by default, to dispatch handlers for any
* asynchronous operations performed on the descriptor.
*/
template <typename ExecutionContext>
explicit basic_stream_descriptor(ExecutionContext& context,
constraint_t<
is_convertible<ExecutionContext&, execution_context&>::value,
defaulted_constraint
> = defaulted_constraint())
: basic_descriptor<Executor>(context)
{
}
/// Construct a stream descriptor on an existing native descriptor.
/**
* This constructor creates a stream descriptor object to hold an existing
* native descriptor.
*
* @param ex The I/O executor that the descriptor will use, by default, to
* dispatch handlers for any asynchronous operations performed on the
* descriptor.
*
* @param native_descriptor The new underlying descriptor implementation.
*
* @throws boost::system::system_error Thrown on failure.
*/
basic_stream_descriptor(const executor_type& ex,
const native_handle_type& native_descriptor)
: basic_descriptor<Executor>(ex, native_descriptor)
{
}
/// Construct a stream descriptor on an existing native descriptor.
/**
* This constructor creates a stream descriptor object to hold an existing
* native descriptor.
*
* @param context An execution context which provides the I/O executor that
* the descriptor will use, by default, to dispatch handlers for any
* asynchronous operations performed on the descriptor.
*
* @param native_descriptor The new underlying descriptor implementation.
*
* @throws boost::system::system_error Thrown on failure.
*/
template <typename ExecutionContext>
basic_stream_descriptor(ExecutionContext& context,
const native_handle_type& native_descriptor,
constraint_t<
is_convertible<ExecutionContext&, execution_context&>::value
> = 0)
: basic_descriptor<Executor>(context, native_descriptor)
{
}
/// Move-construct a stream descriptor from another.
/**
* This constructor moves a stream descriptor from one object to another.
*
* @param other The other stream descriptor object from which the move
* will occur.
*
* @note Following the move, the moved-from object is in the same state as if
* constructed using the @c basic_stream_descriptor(const executor_type&)
* constructor.
*/
basic_stream_descriptor(basic_stream_descriptor&& other) noexcept
: basic_descriptor<Executor>(std::move(other))
{
}
/// Move-assign a stream descriptor from another.
/**
* This assignment operator moves a stream descriptor from one object to
* another.
*
* @param other The other stream descriptor object from which the move
* will occur.
*
* @note Following the move, the moved-from object is in the same state as if
* constructed using the @c basic_stream_descriptor(const executor_type&)
* constructor.
*/
basic_stream_descriptor& operator=(basic_stream_descriptor&& other)
{
basic_descriptor<Executor>::operator=(std::move(other));
return *this;
}
/// Move-construct a basic_stream_descriptor from a descriptor of another
/// executor type.
/**
* This constructor moves a descriptor from one object to another.
*
* @param other The other basic_stream_descriptor object from which the move
* will occur.
*
* @note Following the move, the moved-from object is in the same state as if
* constructed using the @c basic_stream_descriptor(const executor_type&)
* constructor.
*/
template <typename Executor1>
basic_stream_descriptor(basic_stream_descriptor<Executor1>&& other,
constraint_t<
is_convertible<Executor1, Executor>::value,
defaulted_constraint
> = defaulted_constraint())
: basic_descriptor<Executor>(std::move(other))
{
}
/// Move-assign a basic_stream_descriptor from a descriptor of another
/// executor type.
/**
* This assignment operator moves a descriptor from one object to another.
*
* @param other The other basic_stream_descriptor object from which the move
* will occur.
*
* @note Following the move, the moved-from object is in the same state as if
* constructed using the @c basic_stream_descriptor(const executor_type&)
* constructor.
*/
template <typename Executor1>
constraint_t<
is_convertible<Executor1, Executor>::value,
basic_stream_descriptor&
> operator=(basic_stream_descriptor<Executor1> && other)
{
basic_descriptor<Executor>::operator=(std::move(other));
return *this;
}
/// Write some data to the descriptor.
/**
* This function is used to write data to the stream descriptor. The function
* call will block until one or more bytes of the data has been written
* successfully, or until an error occurs.
*
* @param buffers One or more data buffers to be written to the descriptor.
*
* @returns The number of bytes written.
*
* @throws boost::system::system_error Thrown on failure. An error code of
* boost::asio::error::eof indicates that the connection was closed by the
* peer.
*
* @note The write_some operation may not transmit all of the data to the
* peer. Consider using the @ref write function if you need to ensure that
* all data is written before the blocking operation completes.
*
* @par Example
* To write a single data buffer use the @ref buffer function as follows:
* @code
* descriptor.write_some(boost::asio::buffer(data, size));
* @endcode
* See the @ref buffer documentation for information on writing multiple
* buffers in one go, and how to use it with arrays, boost::array or
* std::vector.
*/
template <typename ConstBufferSequence>
std::size_t write_some(const ConstBufferSequence& buffers)
{
boost::system::error_code ec;
std::size_t s = this->impl_.get_service().write_some(
this->impl_.get_implementation(), buffers, ec);
boost::asio::detail::throw_error(ec, "write_some");
return s;
}
/// Write some data to the descriptor.
/**
* This function is used to write data to the stream descriptor. The function
* call will block until one or more bytes of the data has been written
* successfully, or until an error occurs.
*
* @param buffers One or more data buffers to be written to the descriptor.
*
* @param ec Set to indicate what error occurred, if any.
*
* @returns The number of bytes written. Returns 0 if an error occurred.
*
* @note The write_some operation may not transmit all of the data to the
* peer. Consider using the @ref write function if you need to ensure that
* all data is written before the blocking operation completes.
*/
template <typename ConstBufferSequence>
std::size_t write_some(const ConstBufferSequence& buffers,
boost::system::error_code& ec)
{
return this->impl_.get_service().write_some(
this->impl_.get_implementation(), buffers, ec);
}
/// Start an asynchronous write.
/**
* This function is used to asynchronously write data to the stream
* descriptor. It is an initiating function for an @ref
* asynchronous_operation, and always returns immediately.
*
* @param buffers One or more data buffers to be written to the descriptor.
* Although the buffers object may be copied as necessary, ownership of the
* underlying memory blocks is retained by the caller, which must guarantee
* that they remain valid until the completion handler is called.
*
* @param token The @ref completion_token that will be used to produce a
* completion handler, which will be called when the write completes.
* Potential completion tokens include @ref use_future, @ref use_awaitable,
* @ref yield_context, or a function object with the correct completion
* signature. The function signature of the completion handler must be:
* @code void handler(
* const boost::system::error_code& error, // Result of operation.
* std::size_t bytes_transferred // Number of bytes written.
* ); @endcode
* Regardless of whether the asynchronous operation completes immediately or
* not, the completion handler will not be invoked from within this function.
* On immediate completion, invocation of the handler will be performed in a
* manner equivalent to using boost::asio::post().
*
* @par Completion Signature
* @code void(boost::system::error_code, std::size_t) @endcode
*
* @note The write operation may not transmit all of the data to the peer.
* Consider using the @ref async_write function if you need to ensure that all
* data is written before the asynchronous operation completes.
*
* @par Example
* To write a single data buffer use the @ref buffer function as follows:
* @code
* descriptor.async_write_some(boost::asio::buffer(data, size), handler);
* @endcode
* See the @ref buffer documentation for information on writing multiple
* buffers in one go, and how to use it with arrays, boost::array or
* std::vector.
*
* @par Per-Operation Cancellation
* This asynchronous operation supports cancellation for the following
* boost::asio::cancellation_type values:
*
* @li @c cancellation_type::terminal
*
* @li @c cancellation_type::partial
*
* @li @c cancellation_type::total
*/
template <typename ConstBufferSequence,
BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
std::size_t)) WriteToken = default_completion_token_t<executor_type>>
auto async_write_some(const ConstBufferSequence& buffers,
WriteToken&& token = default_completion_token_t<executor_type>())
-> decltype(
async_initiate<WriteToken,
void (boost::system::error_code, std::size_t)>(
initiate_async_write_some(this), token, buffers))
{
return async_initiate<WriteToken,
void (boost::system::error_code, std::size_t)>(
initiate_async_write_some(this), token, buffers);
}
/// Read some data from the descriptor.
/**
* This function is used to read data from the stream descriptor. The function
* call will block until one or more bytes of data has been read successfully,
* or until an error occurs.
*
* @param buffers One or more buffers into which the data will be read.
*
* @returns The number of bytes read.
*
* @throws boost::system::system_error Thrown on failure. An error code of
* boost::asio::error::eof indicates that the connection was closed by the
* peer.
*
* @note The read_some operation may not read all of the requested number of
* bytes. Consider using the @ref read function if you need to ensure that
* the requested amount of data is read before the blocking operation
* completes.
*
* @par Example
* To read into a single data buffer use the @ref buffer function as follows:
* @code
* descriptor.read_some(boost::asio::buffer(data, size));
* @endcode
* See the @ref buffer documentation for information on reading into multiple
* buffers in one go, and how to use it with arrays, boost::array or
* std::vector.
*/
template <typename MutableBufferSequence>
std::size_t read_some(const MutableBufferSequence& buffers)
{
boost::system::error_code ec;
std::size_t s = this->impl_.get_service().read_some(
this->impl_.get_implementation(), buffers, ec);
boost::asio::detail::throw_error(ec, "read_some");
return s;
}
/// Read some data from the descriptor.
/**
* This function is used to read data from the stream descriptor. The function
* call will block until one or more bytes of data has been read successfully,
* or until an error occurs.
*
* @param buffers One or more buffers into which the data will be read.
*
* @param ec Set to indicate what error occurred, if any.
*
* @returns The number of bytes read. Returns 0 if an error occurred.
*
* @note The read_some operation may not read all of the requested number of
* bytes. Consider using the @ref read function if you need to ensure that
* the requested amount of data is read before the blocking operation
* completes.
*/
template <typename MutableBufferSequence>
std::size_t read_some(const MutableBufferSequence& buffers,
boost::system::error_code& ec)
{
return this->impl_.get_service().read_some(
this->impl_.get_implementation(), buffers, ec);
}
/// Start an asynchronous read.
/**
* This function is used to asynchronously read data from the stream
* descriptor. It is an initiating function for an @ref
* asynchronous_operation, and always returns immediately.
*
* @param buffers One or more buffers into which the data will be read.
* Although the buffers object may be copied as necessary, ownership of the
* underlying memory blocks is retained by the caller, which must guarantee
* that they remain valid until the completion handler is called.
*
* @param token The @ref completion_token that will be used to produce a
* completion handler, which will be called when the read completes.
* Potential completion tokens include @ref use_future, @ref use_awaitable,
* @ref yield_context, or a function object with the correct completion
* signature. The function signature of the completion handler must be:
* @code void handler(
* const boost::system::error_code& error, // Result of operation.
* std::size_t bytes_transferred // Number of bytes read.
* ); @endcode
* Regardless of whether the asynchronous operation completes immediately or
* not, the completion handler will not be invoked from within this function.
* On immediate completion, invocation of the handler will be performed in a
* manner equivalent to using boost::asio::post().
*
* @par Completion Signature
* @code void(boost::system::error_code, std::size_t) @endcode
*
* @note The read operation may not read all of the requested number of bytes.
* Consider using the @ref async_read function if you need to ensure that the
* requested amount of data is read before the asynchronous operation
* completes.
*
* @par Example
* To read into a single data buffer use the @ref buffer function as follows:
* @code
* descriptor.async_read_some(boost::asio::buffer(data, size), handler);
* @endcode
* See the @ref buffer documentation for information on reading into multiple
* buffers in one go, and how to use it with arrays, boost::array or
* std::vector.
*
* @par Per-Operation Cancellation
* This asynchronous operation supports cancellation for the following
* boost::asio::cancellation_type values:
*
* @li @c cancellation_type::terminal
*
* @li @c cancellation_type::partial
*
* @li @c cancellation_type::total
*/
template <typename MutableBufferSequence,
BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
std::size_t)) ReadToken = default_completion_token_t<executor_type>>
auto async_read_some(const MutableBufferSequence& buffers,
ReadToken&& token = default_completion_token_t<executor_type>())
-> decltype(
async_initiate<ReadToken,
void (boost::system::error_code, std::size_t)>(
declval<initiate_async_read_some>(), token, buffers))
{
return async_initiate<ReadToken,
void (boost::system::error_code, std::size_t)>(
initiate_async_read_some(this), token, buffers);
}
private:
class initiate_async_write_some
{
public:
typedef Executor executor_type;
explicit initiate_async_write_some(basic_stream_descriptor* self)
: self_(self)
{
}
const executor_type& get_executor() const noexcept
{
return self_->get_executor();
}
template <typename WriteHandler, typename ConstBufferSequence>
void operator()(WriteHandler&& handler,
const ConstBufferSequence& buffers) const
{
// If you get an error on the following line it means that your handler
// does not meet the documented type requirements for a WriteHandler.
BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
detail::non_const_lvalue<WriteHandler> handler2(handler);
self_->impl_.get_service().async_write_some(
self_->impl_.get_implementation(), buffers,
handler2.value, self_->impl_.get_executor());
}
private:
basic_stream_descriptor* self_;
};
class initiate_async_read_some
{
public:
typedef Executor executor_type;
explicit initiate_async_read_some(basic_stream_descriptor* self)
: self_(self)
{
}
const executor_type& get_executor() const noexcept
{
return self_->get_executor();
}
template <typename ReadHandler, typename MutableBufferSequence>
void operator()(ReadHandler&& handler,
const MutableBufferSequence& buffers) const
{
// If you get an error on the following line it means that your handler
// does not meet the documented type requirements for a ReadHandler.
BOOST_ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
detail::non_const_lvalue<ReadHandler> handler2(handler);
self_->impl_.get_service().async_read_some(
self_->impl_.get_implementation(), buffers,
handler2.value, self_->impl_.get_executor());
}
private:
basic_stream_descriptor* self_;
};
};
} // namespace posix
} // namespace asio
} // namespace boost
#include <boost/asio/detail/pop_options.hpp>
#endif // defined(BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR)
// || defined(GENERATING_DOCUMENTATION)
#endif // BOOST_ASIO_POSIX_BASIC_STREAM_DESCRIPTOR_HPP
| hpp |
asio | data/projects/asio/include/boost/asio/posix/stream_descriptor.hpp | //
// posix/stream_descriptor.hpp
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_ASIO_POSIX_STREAM_DESCRIPTOR_HPP
#define BOOST_ASIO_POSIX_STREAM_DESCRIPTOR_HPP
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include <boost/asio/detail/config.hpp>
#if defined(BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR) \
|| defined(GENERATING_DOCUMENTATION)
#include <boost/asio/posix/basic_stream_descriptor.hpp>
namespace boost {
namespace asio {
namespace posix {
/// Typedef for the typical usage of a stream-oriented descriptor.
typedef basic_stream_descriptor<> stream_descriptor;
} // namespace posix
} // namespace asio
} // namespace boost
#endif // defined(BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR)
// || defined(GENERATING_DOCUMENTATION)
#endif // BOOST_ASIO_POSIX_STREAM_DESCRIPTOR_HPP
| hpp |
asio | data/projects/asio/include/boost/asio/posix/basic_descriptor.hpp | //
// posix/basic_descriptor.hpp
// ~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_ASIO_POSIX_BASIC_DESCRIPTOR_HPP
#define BOOST_ASIO_POSIX_BASIC_DESCRIPTOR_HPP
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include <boost/asio/detail/config.hpp>
#if defined(BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR) \
|| defined(GENERATING_DOCUMENTATION)
#include <utility>
#include <boost/asio/any_io_executor.hpp>
#include <boost/asio/async_result.hpp>
#include <boost/asio/detail/handler_type_requirements.hpp>
#include <boost/asio/detail/io_object_impl.hpp>
#include <boost/asio/detail/non_const_lvalue.hpp>
#include <boost/asio/detail/throw_error.hpp>
#include <boost/asio/error.hpp>
#include <boost/asio/execution_context.hpp>
#include <boost/asio/posix/descriptor_base.hpp>
#if defined(BOOST_ASIO_HAS_IO_URING_AS_DEFAULT)
# include <boost/asio/detail/io_uring_descriptor_service.hpp>
#else // defined(BOOST_ASIO_HAS_IO_URING_AS_DEFAULT)
# include <boost/asio/detail/reactive_descriptor_service.hpp>
#endif // defined(BOOST_ASIO_HAS_IO_URING_AS_DEFAULT)
#include <boost/asio/detail/push_options.hpp>
namespace boost {
namespace asio {
namespace posix {
/// Provides POSIX descriptor functionality.
/**
* The posix::basic_descriptor class template provides the ability to wrap a
* POSIX descriptor.
*
* @par Thread Safety
* @e Distinct @e objects: Safe.@n
* @e Shared @e objects: Unsafe.
*/
template <typename Executor = any_io_executor>
class basic_descriptor
: public descriptor_base
{
private:
class initiate_async_wait;
public:
/// The type of the executor associated with the object.
typedef Executor executor_type;
/// Rebinds the descriptor type to another executor.
template <typename Executor1>
struct rebind_executor
{
/// The descriptor type when rebound to the specified executor.
typedef basic_descriptor<Executor1> other;
};
/// The native representation of a descriptor.
#if defined(GENERATING_DOCUMENTATION)
typedef implementation_defined native_handle_type;
#elif defined(BOOST_ASIO_HAS_IO_URING_AS_DEFAULT)
typedef detail::io_uring_descriptor_service::native_handle_type
native_handle_type;
#else // defined(BOOST_ASIO_HAS_IO_URING_AS_DEFAULT)
typedef detail::reactive_descriptor_service::native_handle_type
native_handle_type;
#endif // defined(BOOST_ASIO_HAS_IO_URING_AS_DEFAULT)
/// A descriptor is always the lowest layer.
typedef basic_descriptor lowest_layer_type;
/// Construct a descriptor without opening it.
/**
* This constructor creates a descriptor without opening it.
*
* @param ex The I/O executor that the descriptor will use, by default, to
* dispatch handlers for any asynchronous operations performed on the
* descriptor.
*/
explicit basic_descriptor(const executor_type& ex)
: impl_(0, ex)
{
}
/// Construct a descriptor without opening it.
/**
* This constructor creates a descriptor without opening it.
*
* @param context An execution context which provides the I/O executor that
* the descriptor will use, by default, to dispatch handlers for any
* asynchronous operations performed on the descriptor.
*/
template <typename ExecutionContext>
explicit basic_descriptor(ExecutionContext& context,
constraint_t<
is_convertible<ExecutionContext&, execution_context&>::value,
defaulted_constraint
> = defaulted_constraint())
: impl_(0, 0, context)
{
}
/// Construct a descriptor on an existing native descriptor.
/**
* This constructor creates a descriptor object to hold an existing native
* descriptor.
*
* @param ex The I/O executor that the descriptor will use, by default, to
* dispatch handlers for any asynchronous operations performed on the
* descriptor.
*
* @param native_descriptor A native descriptor.
*
* @throws boost::system::system_error Thrown on failure.
*/
basic_descriptor(const executor_type& ex,
const native_handle_type& native_descriptor)
: impl_(0, ex)
{
boost::system::error_code ec;
impl_.get_service().assign(impl_.get_implementation(),
native_descriptor, ec);
boost::asio::detail::throw_error(ec, "assign");
}
/// Construct a descriptor on an existing native descriptor.
/**
* This constructor creates a descriptor object to hold an existing native
* descriptor.
*
* @param context An execution context which provides the I/O executor that
* the descriptor will use, by default, to dispatch handlers for any
* asynchronous operations performed on the descriptor.
*
* @param native_descriptor A native descriptor.
*
* @throws boost::system::system_error Thrown on failure.
*/
template <typename ExecutionContext>
basic_descriptor(ExecutionContext& context,
const native_handle_type& native_descriptor,
constraint_t<
is_convertible<ExecutionContext&, execution_context&>::value
> = 0)
: impl_(0, 0, context)
{
boost::system::error_code ec;
impl_.get_service().assign(impl_.get_implementation(),
native_descriptor, ec);
boost::asio::detail::throw_error(ec, "assign");
}
/// Move-construct a descriptor from another.
/**
* This constructor moves a descriptor from one object to another.
*
* @param other The other descriptor object from which the move will
* occur.
*
* @note Following the move, the moved-from object is in the same state as if
* constructed using the @c basic_descriptor(const executor_type&)
* constructor.
*/
basic_descriptor(basic_descriptor&& other) noexcept
: impl_(std::move(other.impl_))
{
}
/// Move-assign a descriptor from another.
/**
* This assignment operator moves a descriptor from one object to another.
*
* @param other The other descriptor object from which the move will
* occur.
*
* @note Following the move, the moved-from object is in the same state as if
* constructed using the @c basic_descriptor(const executor_type&)
* constructor.
*/
basic_descriptor& operator=(basic_descriptor&& other)
{
impl_ = std::move(other.impl_);
return *this;
}
// All descriptors have access to each other's implementations.
template <typename Executor1>
friend class basic_descriptor;
/// Move-construct a basic_descriptor from a descriptor of another executor
/// type.
/**
* This constructor moves a descriptor from one object to another.
*
* @param other The other basic_descriptor object from which the move will
* occur.
*
* @note Following the move, the moved-from object is in the same state as if
* constructed using the @c basic_descriptor(const executor_type&)
* constructor.
*/
template <typename Executor1>
basic_descriptor(basic_descriptor<Executor1>&& other,
constraint_t<
is_convertible<Executor1, Executor>::value,
defaulted_constraint
> = defaulted_constraint())
: impl_(std::move(other.impl_))
{
}
/// Move-assign a basic_descriptor from a descriptor of another executor type.
/**
* This assignment operator moves a descriptor from one object to another.
*
* @param other The other basic_descriptor object from which the move will
* occur.
*
* @note Following the move, the moved-from object is in the same state as if
* constructed using the @c basic_descriptor(const executor_type&)
* constructor.
*/
template <typename Executor1>
constraint_t<
is_convertible<Executor1, Executor>::value,
basic_descriptor&
> operator=(basic_descriptor<Executor1> && other)
{
basic_descriptor tmp(std::move(other));
impl_ = std::move(tmp.impl_);
return *this;
}
/// Get the executor associated with the object.
const executor_type& get_executor() noexcept
{
return impl_.get_executor();
}
/// Get a reference to the lowest layer.
/**
* This function returns a reference to the lowest layer in a stack of
* layers. Since a descriptor cannot contain any further layers, it
* simply returns a reference to itself.
*
* @return A reference to the lowest layer in the stack of layers. Ownership
* is not transferred to the caller.
*/
lowest_layer_type& lowest_layer()
{
return *this;
}
/// Get a const reference to the lowest layer.
/**
* This function returns a const reference to the lowest layer in a stack of
* layers. Since a descriptor cannot contain any further layers, it
* simply returns a reference to itself.
*
* @return A const reference to the lowest layer in the stack of layers.
* Ownership is not transferred to the caller.
*/
const lowest_layer_type& lowest_layer() const
{
return *this;
}
/// Assign an existing native descriptor to the descriptor.
/*
* This function opens the descriptor to hold an existing native descriptor.
*
* @param native_descriptor A native descriptor.
*
* @throws boost::system::system_error Thrown on failure.
*/
void assign(const native_handle_type& native_descriptor)
{
boost::system::error_code ec;
impl_.get_service().assign(impl_.get_implementation(),
native_descriptor, ec);
boost::asio::detail::throw_error(ec, "assign");
}
/// Assign an existing native descriptor to the descriptor.
/*
* This function opens the descriptor to hold an existing native descriptor.
*
* @param native_descriptor A native descriptor.
*
* @param ec Set to indicate what error occurred, if any.
*/
BOOST_ASIO_SYNC_OP_VOID assign(const native_handle_type& native_descriptor,
boost::system::error_code& ec)
{
impl_.get_service().assign(
impl_.get_implementation(), native_descriptor, ec);
BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
}
/// Determine whether the descriptor is open.
bool is_open() const
{
return impl_.get_service().is_open(impl_.get_implementation());
}
/// Close the descriptor.
/**
* This function is used to close the descriptor. Any asynchronous read or
* write operations will be cancelled immediately, and will complete with the
* boost::asio::error::operation_aborted error.
*
* @throws boost::system::system_error Thrown on failure. Note that, even if
* the function indicates an error, the underlying descriptor is closed.
*/
void close()
{
boost::system::error_code ec;
impl_.get_service().close(impl_.get_implementation(), ec);
boost::asio::detail::throw_error(ec, "close");
}
/// Close the descriptor.
/**
* This function is used to close the descriptor. Any asynchronous read or
* write operations will be cancelled immediately, and will complete with the
* boost::asio::error::operation_aborted error.
*
* @param ec Set to indicate what error occurred, if any. Note that, even if
* the function indicates an error, the underlying descriptor is closed.
*/
BOOST_ASIO_SYNC_OP_VOID close(boost::system::error_code& ec)
{
impl_.get_service().close(impl_.get_implementation(), ec);
BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
}
/// Get the native descriptor representation.
/**
* This function may be used to obtain the underlying representation of the
* descriptor. This is intended to allow access to native descriptor
* functionality that is not otherwise provided.
*/
native_handle_type native_handle()
{
return impl_.get_service().native_handle(impl_.get_implementation());
}
/// Release ownership of the native descriptor implementation.
/**
* This function may be used to obtain the underlying representation of the
* descriptor. After calling this function, @c is_open() returns false. The
* caller is responsible for closing the descriptor.
*
* All outstanding asynchronous read or write operations will finish
* immediately, and the handlers for cancelled operations will be passed the
* boost::asio::error::operation_aborted error.
*/
native_handle_type release()
{
return impl_.get_service().release(impl_.get_implementation());
}
/// Cancel all asynchronous operations associated with the descriptor.
/**
* This function causes all outstanding asynchronous read or write operations
* to finish immediately, and the handlers for cancelled operations will be
* passed the boost::asio::error::operation_aborted error.
*
* @throws boost::system::system_error Thrown on failure.
*/
void cancel()
{
boost::system::error_code ec;
impl_.get_service().cancel(impl_.get_implementation(), ec);
boost::asio::detail::throw_error(ec, "cancel");
}
/// Cancel all asynchronous operations associated with the descriptor.
/**
* This function causes all outstanding asynchronous read or write operations
* to finish immediately, and the handlers for cancelled operations will be
* passed the boost::asio::error::operation_aborted error.
*
* @param ec Set to indicate what error occurred, if any.
*/
BOOST_ASIO_SYNC_OP_VOID cancel(boost::system::error_code& ec)
{
impl_.get_service().cancel(impl_.get_implementation(), ec);
BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
}
/// Perform an IO control command on the descriptor.
/**
* This function is used to execute an IO control command on the descriptor.
*
* @param command The IO control command to be performed on the descriptor.
*
* @throws boost::system::system_error Thrown on failure.
*
* @sa IoControlCommand @n
* boost::asio::posix::descriptor_base::bytes_readable @n
* boost::asio::posix::descriptor_base::non_blocking_io
*
* @par Example
* Getting the number of bytes ready to read:
* @code
* boost::asio::posix::stream_descriptor descriptor(my_context);
* ...
* boost::asio::posix::stream_descriptor::bytes_readable command;
* descriptor.io_control(command);
* std::size_t bytes_readable = command.get();
* @endcode
*/
template <typename IoControlCommand>
void io_control(IoControlCommand& command)
{
boost::system::error_code ec;
impl_.get_service().io_control(impl_.get_implementation(), command, ec);
boost::asio::detail::throw_error(ec, "io_control");
}
/// Perform an IO control command on the descriptor.
/**
* This function is used to execute an IO control command on the descriptor.
*
* @param command The IO control command to be performed on the descriptor.
*
* @param ec Set to indicate what error occurred, if any.
*
* @sa IoControlCommand @n
* boost::asio::posix::descriptor_base::bytes_readable @n
* boost::asio::posix::descriptor_base::non_blocking_io
*
* @par Example
* Getting the number of bytes ready to read:
* @code
* boost::asio::posix::stream_descriptor descriptor(my_context);
* ...
* boost::asio::posix::stream_descriptor::bytes_readable command;
* boost::system::error_code ec;
* descriptor.io_control(command, ec);
* if (ec)
* {
* // An error occurred.
* }
* std::size_t bytes_readable = command.get();
* @endcode
*/
template <typename IoControlCommand>
BOOST_ASIO_SYNC_OP_VOID io_control(IoControlCommand& command,
boost::system::error_code& ec)
{
impl_.get_service().io_control(impl_.get_implementation(), command, ec);
BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
}
/// Gets the non-blocking mode of the descriptor.
/**
* @returns @c true if the descriptor's synchronous operations will fail with
* boost::asio::error::would_block if they are unable to perform the requested
* operation immediately. If @c false, synchronous operations will block
* until complete.
*
* @note The non-blocking mode has no effect on the behaviour of asynchronous
* operations. Asynchronous operations will never fail with the error
* boost::asio::error::would_block.
*/
bool non_blocking() const
{
return impl_.get_service().non_blocking(impl_.get_implementation());
}
/// Sets the non-blocking mode of the descriptor.
/**
* @param mode If @c true, the descriptor's synchronous operations will fail
* with boost::asio::error::would_block if they are unable to perform the
* requested operation immediately. If @c false, synchronous operations will
* block until complete.
*
* @throws boost::system::system_error Thrown on failure.
*
* @note The non-blocking mode has no effect on the behaviour of asynchronous
* operations. Asynchronous operations will never fail with the error
* boost::asio::error::would_block.
*/
void non_blocking(bool mode)
{
boost::system::error_code ec;
impl_.get_service().non_blocking(impl_.get_implementation(), mode, ec);
boost::asio::detail::throw_error(ec, "non_blocking");
}
/// Sets the non-blocking mode of the descriptor.
/**
* @param mode If @c true, the descriptor's synchronous operations will fail
* with boost::asio::error::would_block if they are unable to perform the
* requested operation immediately. If @c false, synchronous operations will
* block until complete.
*
* @param ec Set to indicate what error occurred, if any.
*
* @note The non-blocking mode has no effect on the behaviour of asynchronous
* operations. Asynchronous operations will never fail with the error
* boost::asio::error::would_block.
*/
BOOST_ASIO_SYNC_OP_VOID non_blocking(
bool mode, boost::system::error_code& ec)
{
impl_.get_service().non_blocking(impl_.get_implementation(), mode, ec);
BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
}
/// Gets the non-blocking mode of the native descriptor implementation.
/**
* This function is used to retrieve the non-blocking mode of the underlying
* native descriptor. This mode has no effect on the behaviour of the
* descriptor object's synchronous operations.
*
* @returns @c true if the underlying descriptor is in non-blocking mode and
* direct system calls may fail with boost::asio::error::would_block (or the
* equivalent system error).
*
* @note The current non-blocking mode is cached by the descriptor object.
* Consequently, the return value may be incorrect if the non-blocking mode
* was set directly on the native descriptor.
*/
bool native_non_blocking() const
{
return impl_.get_service().native_non_blocking(
impl_.get_implementation());
}
/// Sets the non-blocking mode of the native descriptor implementation.
/**
* This function is used to modify the non-blocking mode of the underlying
* native descriptor. It has no effect on the behaviour of the descriptor
* object's synchronous operations.
*
* @param mode If @c true, the underlying descriptor is put into non-blocking
* mode and direct system calls may fail with boost::asio::error::would_block
* (or the equivalent system error).
*
* @throws boost::system::system_error Thrown on failure. If the @c mode is
* @c false, but the current value of @c non_blocking() is @c true, this
* function fails with boost::asio::error::invalid_argument, as the
* combination does not make sense.
*/
void native_non_blocking(bool mode)
{
boost::system::error_code ec;
impl_.get_service().native_non_blocking(
impl_.get_implementation(), mode, ec);
boost::asio::detail::throw_error(ec, "native_non_blocking");
}
/// Sets the non-blocking mode of the native descriptor implementation.
/**
* This function is used to modify the non-blocking mode of the underlying
* native descriptor. It has no effect on the behaviour of the descriptor
* object's synchronous operations.
*
* @param mode If @c true, the underlying descriptor is put into non-blocking
* mode and direct system calls may fail with boost::asio::error::would_block
* (or the equivalent system error).
*
* @param ec Set to indicate what error occurred, if any. If the @c mode is
* @c false, but the current value of @c non_blocking() is @c true, this
* function fails with boost::asio::error::invalid_argument, as the
* combination does not make sense.
*/
BOOST_ASIO_SYNC_OP_VOID native_non_blocking(
bool mode, boost::system::error_code& ec)
{
impl_.get_service().native_non_blocking(
impl_.get_implementation(), mode, ec);
BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
}
/// Wait for the descriptor to become ready to read, ready to write, or to
/// have pending error conditions.
/**
* This function is used to perform a blocking wait for a descriptor to enter
* a ready to read, write or error condition state.
*
* @param w Specifies the desired descriptor state.
*
* @par Example
* Waiting for a descriptor to become readable.
* @code
* boost::asio::posix::stream_descriptor descriptor(my_context);
* ...
* descriptor.wait(boost::asio::posix::stream_descriptor::wait_read);
* @endcode
*/
void wait(wait_type w)
{
boost::system::error_code ec;
impl_.get_service().wait(impl_.get_implementation(), w, ec);
boost::asio::detail::throw_error(ec, "wait");
}
/// Wait for the descriptor to become ready to read, ready to write, or to
/// have pending error conditions.
/**
* This function is used to perform a blocking wait for a descriptor to enter
* a ready to read, write or error condition state.
*
* @param w Specifies the desired descriptor state.
*
* @param ec Set to indicate what error occurred, if any.
*
* @par Example
* Waiting for a descriptor to become readable.
* @code
* boost::asio::posix::stream_descriptor descriptor(my_context);
* ...
* boost::system::error_code ec;
* descriptor.wait(boost::asio::posix::stream_descriptor::wait_read, ec);
* @endcode
*/
BOOST_ASIO_SYNC_OP_VOID wait(wait_type w, boost::system::error_code& ec)
{
impl_.get_service().wait(impl_.get_implementation(), w, ec);
BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
}
/// Asynchronously wait for the descriptor to become ready to read, ready to
/// write, or to have pending error conditions.
/**
* This function is used to perform an asynchronous wait for a descriptor to
* enter a ready to read, write or error condition state. It is an initiating
* function for an @ref asynchronous_operation, and always returns
* immediately.
*
* @param w Specifies the desired descriptor state.
*
* @param token The @ref completion_token that will be used to produce a
* completion handler, which will be called when the wait completes.
* Potential completion tokens include @ref use_future, @ref use_awaitable,
* @ref yield_context, or a function object with the correct completion
* signature. The function signature of the completion handler must be:
* @code void handler(
* const boost::system::error_code& error // Result of operation.
* ); @endcode
* Regardless of whether the asynchronous operation completes immediately or
* not, the completion handler will not be invoked from within this function.
* On immediate completion, invocation of the handler will be performed in a
* manner equivalent to using boost::asio::post().
*
* @par Completion Signature
* @code void(boost::system::error_code) @endcode
*
* @par Example
* @code
* void wait_handler(const boost::system::error_code& error)
* {
* if (!error)
* {
* // Wait succeeded.
* }
* }
*
* ...
*
* boost::asio::posix::stream_descriptor descriptor(my_context);
* ...
* descriptor.async_wait(
* boost::asio::posix::stream_descriptor::wait_read,
* wait_handler);
* @endcode
*
* @par Per-Operation Cancellation
* This asynchronous operation supports cancellation for the following
* boost::asio::cancellation_type values:
*
* @li @c cancellation_type::terminal
*
* @li @c cancellation_type::partial
*
* @li @c cancellation_type::total
*/
template <
BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code))
WaitToken = default_completion_token_t<executor_type>>
auto async_wait(wait_type w,
WaitToken&& token = default_completion_token_t<executor_type>())
-> decltype(
async_initiate<WaitToken, void (boost::system::error_code)>(
declval<initiate_async_wait>(), token, w))
{
return async_initiate<WaitToken, void (boost::system::error_code)>(
initiate_async_wait(this), token, w);
}
protected:
/// Protected destructor to prevent deletion through this type.
/**
* This function destroys the descriptor, cancelling any outstanding
* asynchronous wait operations associated with the descriptor as if by
* calling @c cancel.
*/
~basic_descriptor()
{
}
#if defined(BOOST_ASIO_HAS_IO_URING_AS_DEFAULT)
detail::io_object_impl<detail::io_uring_descriptor_service, Executor> impl_;
#else // defined(BOOST_ASIO_HAS_IO_URING_AS_DEFAULT)
detail::io_object_impl<detail::reactive_descriptor_service, Executor> impl_;
#endif // defined(BOOST_ASIO_HAS_IO_URING_AS_DEFAULT)
private:
// Disallow copying and assignment.
basic_descriptor(const basic_descriptor&) = delete;
basic_descriptor& operator=(const basic_descriptor&) = delete;
class initiate_async_wait
{
public:
typedef Executor executor_type;
explicit initiate_async_wait(basic_descriptor* self)
: self_(self)
{
}
const executor_type& get_executor() const noexcept
{
return self_->get_executor();
}
template <typename WaitHandler>
void operator()(WaitHandler&& handler, wait_type w) const
{
// If you get an error on the following line it means that your handler
// does not meet the documented type requirements for a WaitHandler.
BOOST_ASIO_WAIT_HANDLER_CHECK(WaitHandler, handler) type_check;
detail::non_const_lvalue<WaitHandler> handler2(handler);
self_->impl_.get_service().async_wait(
self_->impl_.get_implementation(), w,
handler2.value, self_->impl_.get_executor());
}
private:
basic_descriptor* self_;
};
};
} // namespace posix
} // namespace asio
} // namespace boost
#include <boost/asio/detail/pop_options.hpp>
#endif // defined(BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR)
// || defined(GENERATING_DOCUMENTATION)
#endif // BOOST_ASIO_POSIX_BASIC_DESCRIPTOR_HPP
| hpp |
asio | data/projects/asio/include/boost/asio/posix/descriptor.hpp | //
// posix/descriptor.hpp
// ~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_ASIO_POSIX_DESCRIPTOR_HPP
#define BOOST_ASIO_POSIX_DESCRIPTOR_HPP
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include <boost/asio/detail/config.hpp>
#if defined(BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR) \
|| defined(GENERATING_DOCUMENTATION)
#include <boost/asio/posix/basic_descriptor.hpp>
namespace boost {
namespace asio {
namespace posix {
/// Typedef for the typical usage of basic_descriptor.
typedef basic_descriptor<> descriptor;
} // namespace posix
} // namespace asio
} // namespace boost
#endif // defined(BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR)
// || defined(GENERATING_DOCUMENTATION)
#endif // BOOST_ASIO_POSIX_DESCRIPTOR_HPP
| hpp |
asio | data/projects/asio/include/boost/asio/posix/descriptor_base.hpp | //
// posix/descriptor_base.hpp
// ~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_ASIO_POSIX_DESCRIPTOR_BASE_HPP
#define BOOST_ASIO_POSIX_DESCRIPTOR_BASE_HPP
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include <boost/asio/detail/config.hpp>
#if defined(BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR) \
|| defined(GENERATING_DOCUMENTATION)
#include <boost/asio/detail/io_control.hpp>
#include <boost/asio/detail/socket_option.hpp>
#include <boost/asio/detail/push_options.hpp>
namespace boost {
namespace asio {
namespace posix {
/// The descriptor_base class is used as a base for the descriptor class as a
/// place to define the associated IO control commands.
class descriptor_base
{
public:
/// Wait types.
/**
* For use with descriptor::wait() and descriptor::async_wait().
*/
enum wait_type
{
/// Wait for a descriptor to become ready to read.
wait_read,
/// Wait for a descriptor to become ready to write.
wait_write,
/// Wait for a descriptor to have error conditions pending.
wait_error
};
/// IO control command to get the amount of data that can be read without
/// blocking.
/**
* Implements the FIONREAD IO control command.
*
* @par Example
* @code
* boost::asio::posix::stream_descriptor descriptor(my_context);
* ...
* boost::asio::descriptor_base::bytes_readable command(true);
* descriptor.io_control(command);
* std::size_t bytes_readable = command.get();
* @endcode
*
* @par Concepts:
* IoControlCommand.
*/
#if defined(GENERATING_DOCUMENTATION)
typedef implementation_defined bytes_readable;
#else
typedef boost::asio::detail::io_control::bytes_readable bytes_readable;
#endif
protected:
/// Protected destructor to prevent deletion through this type.
~descriptor_base()
{
}
};
} // namespace posix
} // namespace asio
} // namespace boost
#include <boost/asio/detail/pop_options.hpp>
#endif // defined(BOOST_ASIO_HAS_POSIX_STREAM_DESCRIPTOR)
// || defined(GENERATING_DOCUMENTATION)
#endif // BOOST_ASIO_POSIX_DESCRIPTOR_BASE_HPP
| hpp |
tinyxml2 | data/projects/tinyxml2/tinyxml2.cpp | /*
Original code by Lee Thomason (www.grinninglizard.com)
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any
damages arising from the use of this software.
Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it and
redistribute it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must
not claim that you wrote the original software. If you use this
software in a product, an acknowledgment in the product documentation
would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
#include "tinyxml2.h"
#include <new> // yes, this one new style header, is in the Android SDK.
#if defined(ANDROID_NDK) || defined(__BORLANDC__) || defined(__QNXNTO__)
# include <stddef.h>
# include <stdarg.h>
#else
# include <cstddef>
# include <cstdarg>
#endif
#if defined(_MSC_VER) && (_MSC_VER >= 1400 ) && (!defined WINCE)
// Microsoft Visual Studio, version 2005 and higher. Not WinCE.
/*int _snprintf_s(
char *buffer,
size_t sizeOfBuffer,
size_t count,
const char *format [,
argument] ...
);*/
static inline int TIXML_SNPRINTF( char* buffer, size_t size, const char* format, ... )
{
va_list va;
va_start( va, format );
const int result = vsnprintf_s( buffer, size, _TRUNCATE, format, va );
va_end( va );
return result;
}
static inline int TIXML_VSNPRINTF( char* buffer, size_t size, const char* format, va_list va )
{
const int result = vsnprintf_s( buffer, size, _TRUNCATE, format, va );
return result;
}
#define TIXML_VSCPRINTF _vscprintf
#define TIXML_SSCANF sscanf_s
#elif defined _MSC_VER
// Microsoft Visual Studio 2003 and earlier or WinCE
#define TIXML_SNPRINTF _snprintf
#define TIXML_VSNPRINTF _vsnprintf
#define TIXML_SSCANF sscanf
#if (_MSC_VER < 1400 ) && (!defined WINCE)
// Microsoft Visual Studio 2003 and not WinCE.
#define TIXML_VSCPRINTF _vscprintf // VS2003's C runtime has this, but VC6 C runtime or WinCE SDK doesn't have.
#else
// Microsoft Visual Studio 2003 and earlier or WinCE.
static inline int TIXML_VSCPRINTF( const char* format, va_list va )
{
int len = 512;
for (;;) {
len = len*2;
char* str = new char[len]();
const int required = _vsnprintf(str, len, format, va);
delete[] str;
if ( required != -1 ) {
TIXMLASSERT( required >= 0 );
len = required;
break;
}
}
TIXMLASSERT( len >= 0 );
return len;
}
#endif
#else
// GCC version 3 and higher
//#warning( "Using sn* functions." )
#define TIXML_SNPRINTF snprintf
#define TIXML_VSNPRINTF vsnprintf
static inline int TIXML_VSCPRINTF( const char* format, va_list va )
{
int len = vsnprintf( 0, 0, format, va );
TIXMLASSERT( len >= 0 );
return len;
}
#define TIXML_SSCANF sscanf
#endif
#if defined(_WIN64)
#define TIXML_FSEEK _fseeki64
#define TIXML_FTELL _ftelli64
#elif defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__DragonFly__) || defined(__CYGWIN__)
#define TIXML_FSEEK fseeko
#define TIXML_FTELL ftello
#elif defined(__ANDROID__)
#if __ANDROID_API__ > 24
#define TIXML_FSEEK fseeko64
#define TIXML_FTELL ftello64
#else
#define TIXML_FSEEK fseeko
#define TIXML_FTELL ftello
#endif
#else
#define TIXML_FSEEK fseek
#define TIXML_FTELL ftell
#endif
static const char LINE_FEED = static_cast<char>(0x0a); // all line endings are normalized to LF
static const char LF = LINE_FEED;
static const char CARRIAGE_RETURN = static_cast<char>(0x0d); // CR gets filtered out
static const char CR = CARRIAGE_RETURN;
static const char SINGLE_QUOTE = '\'';
static const char DOUBLE_QUOTE = '\"';
// Bunch of unicode info at:
// http://www.unicode.org/faq/utf_bom.html
// ef bb bf (Microsoft "lead bytes") - designates UTF-8
static const unsigned char TIXML_UTF_LEAD_0 = 0xefU;
static const unsigned char TIXML_UTF_LEAD_1 = 0xbbU;
static const unsigned char TIXML_UTF_LEAD_2 = 0xbfU;
namespace tinyxml2
{
struct Entity {
const char* pattern;
int length;
char value;
};
static const int NUM_ENTITIES = 5;
static const Entity entities[NUM_ENTITIES] = {
{ "quot", 4, DOUBLE_QUOTE },
{ "amp", 3, '&' },
{ "apos", 4, SINGLE_QUOTE },
{ "lt", 2, '<' },
{ "gt", 2, '>' }
};
StrPair::~StrPair()
{
Reset();
}
void StrPair::TransferTo( StrPair* other )
{
if ( this == other ) {
return;
}
// This in effect implements the assignment operator by "moving"
// ownership (as in auto_ptr).
TIXMLASSERT( other != 0 );
TIXMLASSERT( other->_flags == 0 );
TIXMLASSERT( other->_start == 0 );
TIXMLASSERT( other->_end == 0 );
other->Reset();
other->_flags = _flags;
other->_start = _start;
other->_end = _end;
_flags = 0;
_start = 0;
_end = 0;
}
void StrPair::Reset()
{
if ( _flags & NEEDS_DELETE ) {
delete [] _start;
}
_flags = 0;
_start = 0;
_end = 0;
}
void StrPair::SetStr( const char* str, int flags )
{
TIXMLASSERT( str );
Reset();
size_t len = strlen( str );
TIXMLASSERT( _start == 0 );
_start = new char[ len+1 ];
memcpy( _start, str, len+1 );
_end = _start + len;
_flags = flags | NEEDS_DELETE;
}
char* StrPair::ParseText( char* p, const char* endTag, int strFlags, int* curLineNumPtr )
{
TIXMLASSERT( p );
TIXMLASSERT( endTag && *endTag );
TIXMLASSERT(curLineNumPtr);
char* start = p;
const char endChar = *endTag;
size_t length = strlen( endTag );
// Inner loop of text parsing.
while ( *p ) {
if ( *p == endChar && strncmp( p, endTag, length ) == 0 ) {
Set( start, p, strFlags );
return p + length;
} else if (*p == '\n') {
++(*curLineNumPtr);
}
++p;
TIXMLASSERT( p );
}
return 0;
}
char* StrPair::ParseName( char* p )
{
if ( !p || !(*p) ) {
return 0;
}
if ( !XMLUtil::IsNameStartChar( (unsigned char) *p ) ) {
return 0;
}
char* const start = p;
++p;
while ( *p && XMLUtil::IsNameChar( (unsigned char) *p ) ) {
++p;
}
Set( start, p, 0 );
return p;
}
void StrPair::CollapseWhitespace()
{
// Adjusting _start would cause undefined behavior on delete[]
TIXMLASSERT( ( _flags & NEEDS_DELETE ) == 0 );
// Trim leading space.
_start = XMLUtil::SkipWhiteSpace( _start, 0 );
if ( *_start ) {
const char* p = _start; // the read pointer
char* q = _start; // the write pointer
while( *p ) {
if ( XMLUtil::IsWhiteSpace( *p )) {
p = XMLUtil::SkipWhiteSpace( p, 0 );
if ( *p == 0 ) {
break; // don't write to q; this trims the trailing space.
}
*q = ' ';
++q;
}
*q = *p;
++q;
++p;
}
*q = 0;
}
}
const char* StrPair::GetStr()
{
TIXMLASSERT( _start );
TIXMLASSERT( _end );
if ( _flags & NEEDS_FLUSH ) {
*_end = 0;
_flags ^= NEEDS_FLUSH;
if ( _flags ) {
const char* p = _start; // the read pointer
char* q = _start; // the write pointer
while( p < _end ) {
if ( (_flags & NEEDS_NEWLINE_NORMALIZATION) && *p == CR ) {
// CR-LF pair becomes LF
// CR alone becomes LF
// LF-CR becomes LF
if ( *(p+1) == LF ) {
p += 2;
}
else {
++p;
}
*q = LF;
++q;
}
else if ( (_flags & NEEDS_NEWLINE_NORMALIZATION) && *p == LF ) {
if ( *(p+1) == CR ) {
p += 2;
}
else {
++p;
}
*q = LF;
++q;
}
else if ( (_flags & NEEDS_ENTITY_PROCESSING) && *p == '&' ) {
// Entities handled by tinyXML2:
// - special entities in the entity table [in/out]
// - numeric character reference [in]
// 中 or 中
if ( *(p+1) == '#' ) {
const int buflen = 10;
char buf[buflen] = { 0 };
int len = 0;
const char* adjusted = const_cast<char*>( XMLUtil::GetCharacterRef( p, buf, &len ) );
if ( adjusted == 0 ) {
*q = *p;
++p;
++q;
}
else {
TIXMLASSERT( 0 <= len && len <= buflen );
TIXMLASSERT( q + len <= adjusted );
p = adjusted;
memcpy( q, buf, len );
q += len;
}
}
else {
bool entityFound = false;
for( int i = 0; i < NUM_ENTITIES; ++i ) {
const Entity& entity = entities[i];
if ( strncmp( p + 1, entity.pattern, entity.length ) == 0
&& *( p + entity.length + 1 ) == ';' ) {
// Found an entity - convert.
*q = entity.value;
++q;
p += entity.length + 2;
entityFound = true;
break;
}
}
if ( !entityFound ) {
// fixme: treat as error?
++p;
++q;
}
}
}
else {
*q = *p;
++p;
++q;
}
}
*q = 0;
}
// The loop below has plenty going on, and this
// is a less useful mode. Break it out.
if ( _flags & NEEDS_WHITESPACE_COLLAPSING ) {
CollapseWhitespace();
}
_flags = (_flags & NEEDS_DELETE);
}
TIXMLASSERT( _start );
return _start;
}
// --------- XMLUtil ----------- //
const char* XMLUtil::writeBoolTrue = "true";
const char* XMLUtil::writeBoolFalse = "false";
void XMLUtil::SetBoolSerialization(const char* writeTrue, const char* writeFalse)
{
static const char* defTrue = "true";
static const char* defFalse = "false";
writeBoolTrue = (writeTrue) ? writeTrue : defTrue;
writeBoolFalse = (writeFalse) ? writeFalse : defFalse;
}
const char* XMLUtil::ReadBOM( const char* p, bool* bom )
{
TIXMLASSERT( p );
TIXMLASSERT( bom );
*bom = false;
const unsigned char* pu = reinterpret_cast<const unsigned char*>(p);
// Check for BOM:
if ( *(pu+0) == TIXML_UTF_LEAD_0
&& *(pu+1) == TIXML_UTF_LEAD_1
&& *(pu+2) == TIXML_UTF_LEAD_2 ) {
*bom = true;
p += 3;
}
TIXMLASSERT( p );
return p;
}
void XMLUtil::ConvertUTF32ToUTF8( unsigned long input, char* output, int* length )
{
const unsigned long BYTE_MASK = 0xBF;
const unsigned long BYTE_MARK = 0x80;
const unsigned long FIRST_BYTE_MARK[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
if (input < 0x80) {
*length = 1;
}
else if ( input < 0x800 ) {
*length = 2;
}
else if ( input < 0x10000 ) {
*length = 3;
}
else if ( input < 0x200000 ) {
*length = 4;
}
else {
*length = 0; // This code won't convert this correctly anyway.
return;
}
output += *length;
// Scary scary fall throughs are annotated with carefully designed comments
// to suppress compiler warnings such as -Wimplicit-fallthrough in gcc
switch (*length) {
case 4:
--output;
*output = static_cast<char>((input | BYTE_MARK) & BYTE_MASK);
input >>= 6;
//fall through
case 3:
--output;
*output = static_cast<char>((input | BYTE_MARK) & BYTE_MASK);
input >>= 6;
//fall through
case 2:
--output;
*output = static_cast<char>((input | BYTE_MARK) & BYTE_MASK);
input >>= 6;
//fall through
case 1:
--output;
*output = static_cast<char>(input | FIRST_BYTE_MARK[*length]);
break;
default:
TIXMLASSERT( false );
}
}
const char* XMLUtil::GetCharacterRef( const char* p, char* value, int* length )
{
// Presume an entity, and pull it out.
*length = 0;
if ( *(p+1) == '#' && *(p+2) ) {
unsigned long ucs = 0;
TIXMLASSERT( sizeof( ucs ) >= 4 );
ptrdiff_t delta = 0;
unsigned mult = 1;
static const char SEMICOLON = ';';
if ( *(p+2) == 'x' ) {
// Hexadecimal.
const char* q = p+3;
if ( !(*q) ) {
return 0;
}
q = strchr( q, SEMICOLON );
if ( !q ) {
return 0;
}
TIXMLASSERT( *q == SEMICOLON );
delta = q-p;
--q;
while ( *q != 'x' ) {
unsigned int digit = 0;
if ( *q >= '0' && *q <= '9' ) {
digit = *q - '0';
}
else if ( *q >= 'a' && *q <= 'f' ) {
digit = *q - 'a' + 10;
}
else if ( *q >= 'A' && *q <= 'F' ) {
digit = *q - 'A' + 10;
}
else {
return 0;
}
TIXMLASSERT( digit < 16 );
TIXMLASSERT( digit == 0 || mult <= UINT_MAX / digit );
const unsigned int digitScaled = mult * digit;
TIXMLASSERT( ucs <= ULONG_MAX - digitScaled );
ucs += digitScaled;
TIXMLASSERT( mult <= UINT_MAX / 16 );
mult *= 16;
--q;
}
}
else {
// Decimal.
const char* q = p+2;
if ( !(*q) ) {
return 0;
}
q = strchr( q, SEMICOLON );
if ( !q ) {
return 0;
}
TIXMLASSERT( *q == SEMICOLON );
delta = q-p;
--q;
while ( *q != '#' ) {
if ( *q >= '0' && *q <= '9' ) {
const unsigned int digit = *q - '0';
TIXMLASSERT( digit < 10 );
TIXMLASSERT( digit == 0 || mult <= UINT_MAX / digit );
const unsigned int digitScaled = mult * digit;
TIXMLASSERT( ucs <= ULONG_MAX - digitScaled );
ucs += digitScaled;
}
else {
return 0;
}
TIXMLASSERT( mult <= UINT_MAX / 10 );
mult *= 10;
--q;
}
}
// convert the UCS to UTF-8
ConvertUTF32ToUTF8( ucs, value, length );
return p + delta + 1;
}
return p+1;
}
void XMLUtil::ToStr( int v, char* buffer, int bufferSize )
{
TIXML_SNPRINTF( buffer, bufferSize, "%d", v );
}
void XMLUtil::ToStr( unsigned v, char* buffer, int bufferSize )
{
TIXML_SNPRINTF( buffer, bufferSize, "%u", v );
}
void XMLUtil::ToStr( bool v, char* buffer, int bufferSize )
{
TIXML_SNPRINTF( buffer, bufferSize, "%s", v ? writeBoolTrue : writeBoolFalse);
}
/*
ToStr() of a number is a very tricky topic.
https://github.com/leethomason/tinyxml2/issues/106
*/
void XMLUtil::ToStr( float v, char* buffer, int bufferSize )
{
TIXML_SNPRINTF( buffer, bufferSize, "%.8g", v );
}
void XMLUtil::ToStr( double v, char* buffer, int bufferSize )
{
TIXML_SNPRINTF( buffer, bufferSize, "%.17g", v );
}
void XMLUtil::ToStr( int64_t v, char* buffer, int bufferSize )
{
// horrible syntax trick to make the compiler happy about %lld
TIXML_SNPRINTF(buffer, bufferSize, "%lld", static_cast<long long>(v));
}
void XMLUtil::ToStr( uint64_t v, char* buffer, int bufferSize )
{
// horrible syntax trick to make the compiler happy about %llu
TIXML_SNPRINTF(buffer, bufferSize, "%llu", (long long)v);
}
bool XMLUtil::ToInt(const char* str, int* value)
{
if (IsPrefixHex(str)) {
unsigned v;
if (TIXML_SSCANF(str, "%x", &v) == 1) {
*value = static_cast<int>(v);
return true;
}
}
else {
if (TIXML_SSCANF(str, "%d", value) == 1) {
return true;
}
}
return false;
}
bool XMLUtil::ToUnsigned(const char* str, unsigned* value)
{
if (TIXML_SSCANF(str, IsPrefixHex(str) ? "%x" : "%u", value) == 1) {
return true;
}
return false;
}
bool XMLUtil::ToBool( const char* str, bool* value )
{
int ival = 0;
if ( ToInt( str, &ival )) {
*value = (ival==0) ? false : true;
return true;
}
static const char* TRUE_VALS[] = { "true", "True", "TRUE", 0 };
static const char* FALSE_VALS[] = { "false", "False", "FALSE", 0 };
for (int i = 0; TRUE_VALS[i]; ++i) {
if (StringEqual(str, TRUE_VALS[i])) {
*value = true;
return true;
}
}
for (int i = 0; FALSE_VALS[i]; ++i) {
if (StringEqual(str, FALSE_VALS[i])) {
*value = false;
return true;
}
}
return false;
}
bool XMLUtil::ToFloat( const char* str, float* value )
{
if ( TIXML_SSCANF( str, "%f", value ) == 1 ) {
return true;
}
return false;
}
bool XMLUtil::ToDouble( const char* str, double* value )
{
if ( TIXML_SSCANF( str, "%lf", value ) == 1 ) {
return true;
}
return false;
}
bool XMLUtil::ToInt64(const char* str, int64_t* value)
{
if (IsPrefixHex(str)) {
unsigned long long v = 0; // horrible syntax trick to make the compiler happy about %llx
if (TIXML_SSCANF(str, "%llx", &v) == 1) {
*value = static_cast<int64_t>(v);
return true;
}
}
else {
long long v = 0; // horrible syntax trick to make the compiler happy about %lld
if (TIXML_SSCANF(str, "%lld", &v) == 1) {
*value = static_cast<int64_t>(v);
return true;
}
}
return false;
}
bool XMLUtil::ToUnsigned64(const char* str, uint64_t* value) {
unsigned long long v = 0; // horrible syntax trick to make the compiler happy about %llu
if(TIXML_SSCANF(str, IsPrefixHex(str) ? "%llx" : "%llu", &v) == 1) {
*value = (uint64_t)v;
return true;
}
return false;
}
char* XMLDocument::Identify( char* p, XMLNode** node, bool first )
{
TIXMLASSERT( node );
TIXMLASSERT( p );
char* const start = p;
int const startLine = _parseCurLineNum;
p = XMLUtil::SkipWhiteSpace( p, &_parseCurLineNum );
if( !*p ) {
*node = 0;
TIXMLASSERT( p );
return p;
}
// These strings define the matching patterns:
static const char* xmlHeader = { "<?" };
static const char* commentHeader = { "<!--" };
static const char* cdataHeader = { "<![CDATA[" };
static const char* dtdHeader = { "<!" };
static const char* elementHeader = { "<" }; // and a header for everything else; check last.
static const int xmlHeaderLen = 2;
static const int commentHeaderLen = 4;
static const int cdataHeaderLen = 9;
static const int dtdHeaderLen = 2;
static const int elementHeaderLen = 1;
TIXMLASSERT( sizeof( XMLComment ) == sizeof( XMLUnknown ) ); // use same memory pool
TIXMLASSERT( sizeof( XMLComment ) == sizeof( XMLDeclaration ) ); // use same memory pool
XMLNode* returnNode = 0;
if ( XMLUtil::StringEqual( p, xmlHeader, xmlHeaderLen ) ) {
returnNode = CreateUnlinkedNode<XMLDeclaration>( _commentPool );
returnNode->_parseLineNum = _parseCurLineNum;
p += xmlHeaderLen;
}
else if ( XMLUtil::StringEqual( p, commentHeader, commentHeaderLen ) ) {
returnNode = CreateUnlinkedNode<XMLComment>( _commentPool );
returnNode->_parseLineNum = _parseCurLineNum;
p += commentHeaderLen;
}
else if ( XMLUtil::StringEqual( p, cdataHeader, cdataHeaderLen ) ) {
XMLText* text = CreateUnlinkedNode<XMLText>( _textPool );
returnNode = text;
returnNode->_parseLineNum = _parseCurLineNum;
p += cdataHeaderLen;
text->SetCData( true );
}
else if ( XMLUtil::StringEqual( p, dtdHeader, dtdHeaderLen ) ) {
returnNode = CreateUnlinkedNode<XMLUnknown>( _commentPool );
returnNode->_parseLineNum = _parseCurLineNum;
p += dtdHeaderLen;
}
else if ( XMLUtil::StringEqual( p, elementHeader, elementHeaderLen ) ) {
// Preserve whitespace pedantically before closing tag, when it's immediately after opening tag
if (WhitespaceMode() == PEDANTIC_WHITESPACE && first && p != start && *(p + elementHeaderLen) == '/') {
returnNode = CreateUnlinkedNode<XMLText>(_textPool);
returnNode->_parseLineNum = startLine;
p = start; // Back it up, all the text counts.
_parseCurLineNum = startLine;
}
else {
returnNode = CreateUnlinkedNode<XMLElement>(_elementPool);
returnNode->_parseLineNum = _parseCurLineNum;
p += elementHeaderLen;
}
}
else {
returnNode = CreateUnlinkedNode<XMLText>( _textPool );
returnNode->_parseLineNum = _parseCurLineNum; // Report line of first non-whitespace character
p = start; // Back it up, all the text counts.
_parseCurLineNum = startLine;
}
TIXMLASSERT( returnNode );
TIXMLASSERT( p );
*node = returnNode;
return p;
}
bool XMLDocument::Accept( XMLVisitor* visitor ) const
{
TIXMLASSERT( visitor );
if ( visitor->VisitEnter( *this ) ) {
for ( const XMLNode* node=FirstChild(); node; node=node->NextSibling() ) {
if ( !node->Accept( visitor ) ) {
break;
}
}
}
return visitor->VisitExit( *this );
}
// --------- XMLNode ----------- //
XMLNode::XMLNode( XMLDocument* doc ) :
_document( doc ),
_parent( 0 ),
_value(),
_parseLineNum( 0 ),
_firstChild( 0 ), _lastChild( 0 ),
_prev( 0 ), _next( 0 ),
_userData( 0 ),
_memPool( 0 )
{
}
XMLNode::~XMLNode()
{
DeleteChildren();
if ( _parent ) {
_parent->Unlink( this );
}
}
// ChildElementCount was originally suggested by msteiger on the sourceforge page for TinyXML and modified by KB1SPH for TinyXML-2.
int XMLNode::ChildElementCount(const char *value) const {
int count = 0;
const XMLElement *e = FirstChildElement(value);
while (e) {
e = e->NextSiblingElement(value);
count++;
}
return count;
}
int XMLNode::ChildElementCount() const {
int count = 0;
const XMLElement *e = FirstChildElement();
while (e) {
e = e->NextSiblingElement();
count++;
}
return count;
}
const char* XMLNode::Value() const
{
// Edge case: XMLDocuments don't have a Value. Return null.
if ( this->ToDocument() )
return 0;
return _value.GetStr();
}
void XMLNode::SetValue( const char* str, bool staticMem )
{
if ( staticMem ) {
_value.SetInternedStr( str );
}
else {
_value.SetStr( str );
}
}
XMLNode* XMLNode::DeepClone(XMLDocument* target) const
{
XMLNode* clone = this->ShallowClone(target);
if (!clone) return 0;
for (const XMLNode* child = this->FirstChild(); child; child = child->NextSibling()) {
XMLNode* childClone = child->DeepClone(target);
TIXMLASSERT(childClone);
clone->InsertEndChild(childClone);
}
return clone;
}
void XMLNode::DeleteChildren()
{
while( _firstChild ) {
TIXMLASSERT( _lastChild );
DeleteChild( _firstChild );
}
_firstChild = _lastChild = 0;
}
void XMLNode::Unlink( XMLNode* child )
{
TIXMLASSERT( child );
TIXMLASSERT( child->_document == _document );
TIXMLASSERT( child->_parent == this );
if ( child == _firstChild ) {
_firstChild = _firstChild->_next;
}
if ( child == _lastChild ) {
_lastChild = _lastChild->_prev;
}
if ( child->_prev ) {
child->_prev->_next = child->_next;
}
if ( child->_next ) {
child->_next->_prev = child->_prev;
}
child->_next = 0;
child->_prev = 0;
child->_parent = 0;
}
void XMLNode::DeleteChild( XMLNode* node )
{
TIXMLASSERT( node );
TIXMLASSERT( node->_document == _document );
TIXMLASSERT( node->_parent == this );
Unlink( node );
TIXMLASSERT(node->_prev == 0);
TIXMLASSERT(node->_next == 0);
TIXMLASSERT(node->_parent == 0);
DeleteNode( node );
}
XMLNode* XMLNode::InsertEndChild( XMLNode* addThis )
{
TIXMLASSERT( addThis );
if ( addThis->_document != _document ) {
TIXMLASSERT( false );
return 0;
}
InsertChildPreamble( addThis );
if ( _lastChild ) {
TIXMLASSERT( _firstChild );
TIXMLASSERT( _lastChild->_next == 0 );
_lastChild->_next = addThis;
addThis->_prev = _lastChild;
_lastChild = addThis;
addThis->_next = 0;
}
else {
TIXMLASSERT( _firstChild == 0 );
_firstChild = _lastChild = addThis;
addThis->_prev = 0;
addThis->_next = 0;
}
addThis->_parent = this;
return addThis;
}
XMLNode* XMLNode::InsertFirstChild( XMLNode* addThis )
{
TIXMLASSERT( addThis );
if ( addThis->_document != _document ) {
TIXMLASSERT( false );
return 0;
}
InsertChildPreamble( addThis );
if ( _firstChild ) {
TIXMLASSERT( _lastChild );
TIXMLASSERT( _firstChild->_prev == 0 );
_firstChild->_prev = addThis;
addThis->_next = _firstChild;
_firstChild = addThis;
addThis->_prev = 0;
}
else {
TIXMLASSERT( _lastChild == 0 );
_firstChild = _lastChild = addThis;
addThis->_prev = 0;
addThis->_next = 0;
}
addThis->_parent = this;
return addThis;
}
XMLNode* XMLNode::InsertAfterChild( XMLNode* afterThis, XMLNode* addThis )
{
TIXMLASSERT( addThis );
if ( addThis->_document != _document ) {
TIXMLASSERT( false );
return 0;
}
TIXMLASSERT( afterThis );
if ( afterThis->_parent != this ) {
TIXMLASSERT( false );
return 0;
}
if ( afterThis == addThis ) {
// Current state: BeforeThis -> AddThis -> OneAfterAddThis
// Now AddThis must disappear from it's location and then
// reappear between BeforeThis and OneAfterAddThis.
// So just leave it where it is.
return addThis;
}
if ( afterThis->_next == 0 ) {
// The last node or the only node.
return InsertEndChild( addThis );
}
InsertChildPreamble( addThis );
addThis->_prev = afterThis;
addThis->_next = afterThis->_next;
afterThis->_next->_prev = addThis;
afterThis->_next = addThis;
addThis->_parent = this;
return addThis;
}
const XMLElement* XMLNode::FirstChildElement( const char* name ) const
{
for( const XMLNode* node = _firstChild; node; node = node->_next ) {
const XMLElement* element = node->ToElementWithName( name );
if ( element ) {
return element;
}
}
return 0;
}
const XMLElement* XMLNode::LastChildElement( const char* name ) const
{
for( const XMLNode* node = _lastChild; node; node = node->_prev ) {
const XMLElement* element = node->ToElementWithName( name );
if ( element ) {
return element;
}
}
return 0;
}
const XMLElement* XMLNode::NextSiblingElement( const char* name ) const
{
for( const XMLNode* node = _next; node; node = node->_next ) {
const XMLElement* element = node->ToElementWithName( name );
if ( element ) {
return element;
}
}
return 0;
}
const XMLElement* XMLNode::PreviousSiblingElement( const char* name ) const
{
for( const XMLNode* node = _prev; node; node = node->_prev ) {
const XMLElement* element = node->ToElementWithName( name );
if ( element ) {
return element;
}
}
return 0;
}
char* XMLNode::ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr )
{
// This is a recursive method, but thinking about it "at the current level"
// it is a pretty simple flat list:
// <foo/>
// <!-- comment -->
//
// With a special case:
// <foo>
// </foo>
// <!-- comment -->
//
// Where the closing element (/foo) *must* be the next thing after the opening
// element, and the names must match. BUT the tricky bit is that the closing
// element will be read by the child.
//
// 'endTag' is the end tag for this node, it is returned by a call to a child.
// 'parentEnd' is the end tag for the parent, which is filled in and returned.
XMLDocument::DepthTracker tracker(_document);
if (_document->Error())
return 0;
bool first = true;
while( p && *p ) {
XMLNode* node = 0;
p = _document->Identify( p, &node, first );
TIXMLASSERT( p );
if ( node == 0 ) {
break;
}
first = false;
const int initialLineNum = node->_parseLineNum;
StrPair endTag;
p = node->ParseDeep( p, &endTag, curLineNumPtr );
if ( !p ) {
_document->DeleteNode( node );
if ( !_document->Error() ) {
_document->SetError( XML_ERROR_PARSING, initialLineNum, 0);
}
break;
}
const XMLDeclaration* const decl = node->ToDeclaration();
if ( decl ) {
// Declarations are only allowed at document level
//
// Multiple declarations are allowed but all declarations
// must occur before anything else.
//
// Optimized due to a security test case. If the first node is
// a declaration, and the last node is a declaration, then only
// declarations have so far been added.
bool wellLocated = false;
if (ToDocument()) {
if (FirstChild()) {
wellLocated =
FirstChild() &&
FirstChild()->ToDeclaration() &&
LastChild() &&
LastChild()->ToDeclaration();
}
else {
wellLocated = true;
}
}
if ( !wellLocated ) {
_document->SetError( XML_ERROR_PARSING_DECLARATION, initialLineNum, "XMLDeclaration value=%s", decl->Value());
_document->DeleteNode( node );
break;
}
}
XMLElement* ele = node->ToElement();
if ( ele ) {
// We read the end tag. Return it to the parent.
if ( ele->ClosingType() == XMLElement::CLOSING ) {
if ( parentEndTag ) {
ele->_value.TransferTo( parentEndTag );
}
node->_memPool->SetTracked(); // created and then immediately deleted.
DeleteNode( node );
return p;
}
// Handle an end tag returned to this level.
// And handle a bunch of annoying errors.
bool mismatch = false;
if ( endTag.Empty() ) {
if ( ele->ClosingType() == XMLElement::OPEN ) {
mismatch = true;
}
}
else {
if ( ele->ClosingType() != XMLElement::OPEN ) {
mismatch = true;
}
else if ( !XMLUtil::StringEqual( endTag.GetStr(), ele->Name() ) ) {
mismatch = true;
}
}
if ( mismatch ) {
_document->SetError( XML_ERROR_MISMATCHED_ELEMENT, initialLineNum, "XMLElement name=%s", ele->Name());
_document->DeleteNode( node );
break;
}
}
InsertEndChild( node );
}
return 0;
}
/*static*/ void XMLNode::DeleteNode( XMLNode* node )
{
if ( node == 0 ) {
return;
}
TIXMLASSERT(node->_document);
if (!node->ToDocument()) {
node->_document->MarkInUse(node);
}
MemPool* pool = node->_memPool;
node->~XMLNode();
pool->Free( node );
}
void XMLNode::InsertChildPreamble( XMLNode* insertThis ) const
{
TIXMLASSERT( insertThis );
TIXMLASSERT( insertThis->_document == _document );
if (insertThis->_parent) {
insertThis->_parent->Unlink( insertThis );
}
else {
insertThis->_document->MarkInUse(insertThis);
insertThis->_memPool->SetTracked();
}
}
const XMLElement* XMLNode::ToElementWithName( const char* name ) const
{
const XMLElement* element = this->ToElement();
if ( element == 0 ) {
return 0;
}
if ( name == 0 ) {
return element;
}
if ( XMLUtil::StringEqual( element->Name(), name ) ) {
return element;
}
return 0;
}
// --------- XMLText ---------- //
char* XMLText::ParseDeep( char* p, StrPair*, int* curLineNumPtr )
{
if ( this->CData() ) {
p = _value.ParseText( p, "]]>", StrPair::NEEDS_NEWLINE_NORMALIZATION, curLineNumPtr );
if ( !p ) {
_document->SetError( XML_ERROR_PARSING_CDATA, _parseLineNum, 0 );
}
return p;
}
else {
int flags = _document->ProcessEntities() ? StrPair::TEXT_ELEMENT : StrPair::TEXT_ELEMENT_LEAVE_ENTITIES;
if ( _document->WhitespaceMode() == COLLAPSE_WHITESPACE ) {
flags |= StrPair::NEEDS_WHITESPACE_COLLAPSING;
}
p = _value.ParseText( p, "<", flags, curLineNumPtr );
if ( p && *p ) {
return p-1;
}
if ( !p ) {
_document->SetError( XML_ERROR_PARSING_TEXT, _parseLineNum, 0 );
}
}
return 0;
}
XMLNode* XMLText::ShallowClone( XMLDocument* doc ) const
{
if ( !doc ) {
doc = _document;
}
XMLText* text = doc->NewText( Value() ); // fixme: this will always allocate memory. Intern?
text->SetCData( this->CData() );
return text;
}
bool XMLText::ShallowEqual( const XMLNode* compare ) const
{
TIXMLASSERT( compare );
const XMLText* text = compare->ToText();
return ( text && XMLUtil::StringEqual( text->Value(), Value() ) );
}
bool XMLText::Accept( XMLVisitor* visitor ) const
{
TIXMLASSERT( visitor );
return visitor->Visit( *this );
}
// --------- XMLComment ---------- //
XMLComment::XMLComment( XMLDocument* doc ) : XMLNode( doc )
{
}
XMLComment::~XMLComment()
{
}
char* XMLComment::ParseDeep( char* p, StrPair*, int* curLineNumPtr )
{
// Comment parses as text.
p = _value.ParseText( p, "-->", StrPair::COMMENT, curLineNumPtr );
if ( p == 0 ) {
_document->SetError( XML_ERROR_PARSING_COMMENT, _parseLineNum, 0 );
}
return p;
}
XMLNode* XMLComment::ShallowClone( XMLDocument* doc ) const
{
if ( !doc ) {
doc = _document;
}
XMLComment* comment = doc->NewComment( Value() ); // fixme: this will always allocate memory. Intern?
return comment;
}
bool XMLComment::ShallowEqual( const XMLNode* compare ) const
{
TIXMLASSERT( compare );
const XMLComment* comment = compare->ToComment();
return ( comment && XMLUtil::StringEqual( comment->Value(), Value() ));
}
bool XMLComment::Accept( XMLVisitor* visitor ) const
{
TIXMLASSERT( visitor );
return visitor->Visit( *this );
}
// --------- XMLDeclaration ---------- //
XMLDeclaration::XMLDeclaration( XMLDocument* doc ) : XMLNode( doc )
{
}
XMLDeclaration::~XMLDeclaration()
{
//printf( "~XMLDeclaration\n" );
}
char* XMLDeclaration::ParseDeep( char* p, StrPair*, int* curLineNumPtr )
{
// Declaration parses as text.
p = _value.ParseText( p, "?>", StrPair::NEEDS_NEWLINE_NORMALIZATION, curLineNumPtr );
if ( p == 0 ) {
_document->SetError( XML_ERROR_PARSING_DECLARATION, _parseLineNum, 0 );
}
return p;
}
XMLNode* XMLDeclaration::ShallowClone( XMLDocument* doc ) const
{
if ( !doc ) {
doc = _document;
}
XMLDeclaration* dec = doc->NewDeclaration( Value() ); // fixme: this will always allocate memory. Intern?
return dec;
}
bool XMLDeclaration::ShallowEqual( const XMLNode* compare ) const
{
TIXMLASSERT( compare );
const XMLDeclaration* declaration = compare->ToDeclaration();
return ( declaration && XMLUtil::StringEqual( declaration->Value(), Value() ));
}
bool XMLDeclaration::Accept( XMLVisitor* visitor ) const
{
TIXMLASSERT( visitor );
return visitor->Visit( *this );
}
// --------- XMLUnknown ---------- //
XMLUnknown::XMLUnknown( XMLDocument* doc ) : XMLNode( doc )
{
}
XMLUnknown::~XMLUnknown()
{
}
char* XMLUnknown::ParseDeep( char* p, StrPair*, int* curLineNumPtr )
{
// Unknown parses as text.
p = _value.ParseText( p, ">", StrPair::NEEDS_NEWLINE_NORMALIZATION, curLineNumPtr );
if ( !p ) {
_document->SetError( XML_ERROR_PARSING_UNKNOWN, _parseLineNum, 0 );
}
return p;
}
XMLNode* XMLUnknown::ShallowClone( XMLDocument* doc ) const
{
if ( !doc ) {
doc = _document;
}
XMLUnknown* text = doc->NewUnknown( Value() ); // fixme: this will always allocate memory. Intern?
return text;
}
bool XMLUnknown::ShallowEqual( const XMLNode* compare ) const
{
TIXMLASSERT( compare );
const XMLUnknown* unknown = compare->ToUnknown();
return ( unknown && XMLUtil::StringEqual( unknown->Value(), Value() ));
}
bool XMLUnknown::Accept( XMLVisitor* visitor ) const
{
TIXMLASSERT( visitor );
return visitor->Visit( *this );
}
// --------- XMLAttribute ---------- //
const char* XMLAttribute::Name() const
{
return _name.GetStr();
}
const char* XMLAttribute::Value() const
{
return _value.GetStr();
}
char* XMLAttribute::ParseDeep( char* p, bool processEntities, int* curLineNumPtr )
{
// Parse using the name rules: bug fix, was using ParseText before
p = _name.ParseName( p );
if ( !p || !*p ) {
return 0;
}
// Skip white space before =
p = XMLUtil::SkipWhiteSpace( p, curLineNumPtr );
if ( *p != '=' ) {
return 0;
}
++p; // move up to opening quote
p = XMLUtil::SkipWhiteSpace( p, curLineNumPtr );
if ( *p != '\"' && *p != '\'' ) {
return 0;
}
const char endTag[2] = { *p, 0 };
++p; // move past opening quote
p = _value.ParseText( p, endTag, processEntities ? StrPair::ATTRIBUTE_VALUE : StrPair::ATTRIBUTE_VALUE_LEAVE_ENTITIES, curLineNumPtr );
return p;
}
void XMLAttribute::SetName( const char* n )
{
_name.SetStr( n );
}
XMLError XMLAttribute::QueryIntValue( int* value ) const
{
if ( XMLUtil::ToInt( Value(), value )) {
return XML_SUCCESS;
}
return XML_WRONG_ATTRIBUTE_TYPE;
}
XMLError XMLAttribute::QueryUnsignedValue( unsigned int* value ) const
{
if ( XMLUtil::ToUnsigned( Value(), value )) {
return XML_SUCCESS;
}
return XML_WRONG_ATTRIBUTE_TYPE;
}
XMLError XMLAttribute::QueryInt64Value(int64_t* value) const
{
if (XMLUtil::ToInt64(Value(), value)) {
return XML_SUCCESS;
}
return XML_WRONG_ATTRIBUTE_TYPE;
}
XMLError XMLAttribute::QueryUnsigned64Value(uint64_t* value) const
{
if(XMLUtil::ToUnsigned64(Value(), value)) {
return XML_SUCCESS;
}
return XML_WRONG_ATTRIBUTE_TYPE;
}
XMLError XMLAttribute::QueryBoolValue( bool* value ) const
{
if ( XMLUtil::ToBool( Value(), value )) {
return XML_SUCCESS;
}
return XML_WRONG_ATTRIBUTE_TYPE;
}
XMLError XMLAttribute::QueryFloatValue( float* value ) const
{
if ( XMLUtil::ToFloat( Value(), value )) {
return XML_SUCCESS;
}
return XML_WRONG_ATTRIBUTE_TYPE;
}
XMLError XMLAttribute::QueryDoubleValue( double* value ) const
{
if ( XMLUtil::ToDouble( Value(), value )) {
return XML_SUCCESS;
}
return XML_WRONG_ATTRIBUTE_TYPE;
}
void XMLAttribute::SetAttribute( const char* v )
{
_value.SetStr( v );
}
void XMLAttribute::SetAttribute( int v )
{
char buf[BUF_SIZE];
XMLUtil::ToStr( v, buf, BUF_SIZE );
_value.SetStr( buf );
}
void XMLAttribute::SetAttribute( unsigned v )
{
char buf[BUF_SIZE];
XMLUtil::ToStr( v, buf, BUF_SIZE );
_value.SetStr( buf );
}
void XMLAttribute::SetAttribute(int64_t v)
{
char buf[BUF_SIZE];
XMLUtil::ToStr(v, buf, BUF_SIZE);
_value.SetStr(buf);
}
void XMLAttribute::SetAttribute(uint64_t v)
{
char buf[BUF_SIZE];
XMLUtil::ToStr(v, buf, BUF_SIZE);
_value.SetStr(buf);
}
void XMLAttribute::SetAttribute( bool v )
{
char buf[BUF_SIZE];
XMLUtil::ToStr( v, buf, BUF_SIZE );
_value.SetStr( buf );
}
void XMLAttribute::SetAttribute( double v )
{
char buf[BUF_SIZE];
XMLUtil::ToStr( v, buf, BUF_SIZE );
_value.SetStr( buf );
}
void XMLAttribute::SetAttribute( float v )
{
char buf[BUF_SIZE];
XMLUtil::ToStr( v, buf, BUF_SIZE );
_value.SetStr( buf );
}
// --------- XMLElement ---------- //
XMLElement::XMLElement( XMLDocument* doc ) : XMLNode( doc ),
_closingType( OPEN ),
_rootAttribute( 0 )
{
}
XMLElement::~XMLElement()
{
while( _rootAttribute ) {
XMLAttribute* next = _rootAttribute->_next;
DeleteAttribute( _rootAttribute );
_rootAttribute = next;
}
}
const XMLAttribute* XMLElement::FindAttribute( const char* name ) const
{
for( XMLAttribute* a = _rootAttribute; a; a = a->_next ) {
if ( XMLUtil::StringEqual( a->Name(), name ) ) {
return a;
}
}
return 0;
}
const char* XMLElement::Attribute( const char* name, const char* value ) const
{
const XMLAttribute* a = FindAttribute( name );
if ( !a ) {
return 0;
}
if ( !value || XMLUtil::StringEqual( a->Value(), value )) {
return a->Value();
}
return 0;
}
int XMLElement::IntAttribute(const char* name, int defaultValue) const
{
int i = defaultValue;
QueryIntAttribute(name, &i);
return i;
}
unsigned XMLElement::UnsignedAttribute(const char* name, unsigned defaultValue) const
{
unsigned i = defaultValue;
QueryUnsignedAttribute(name, &i);
return i;
}
int64_t XMLElement::Int64Attribute(const char* name, int64_t defaultValue) const
{
int64_t i = defaultValue;
QueryInt64Attribute(name, &i);
return i;
}
uint64_t XMLElement::Unsigned64Attribute(const char* name, uint64_t defaultValue) const
{
uint64_t i = defaultValue;
QueryUnsigned64Attribute(name, &i);
return i;
}
bool XMLElement::BoolAttribute(const char* name, bool defaultValue) const
{
bool b = defaultValue;
QueryBoolAttribute(name, &b);
return b;
}
double XMLElement::DoubleAttribute(const char* name, double defaultValue) const
{
double d = defaultValue;
QueryDoubleAttribute(name, &d);
return d;
}
float XMLElement::FloatAttribute(const char* name, float defaultValue) const
{
float f = defaultValue;
QueryFloatAttribute(name, &f);
return f;
}
const char* XMLElement::GetText() const
{
/* skip comment node */
const XMLNode* node = FirstChild();
while (node) {
if (node->ToComment()) {
node = node->NextSibling();
continue;
}
break;
}
if ( node && node->ToText() ) {
return node->Value();
}
return 0;
}
void XMLElement::SetText( const char* inText )
{
if ( FirstChild() && FirstChild()->ToText() )
FirstChild()->SetValue( inText );
else {
XMLText* theText = GetDocument()->NewText( inText );
InsertFirstChild( theText );
}
}
void XMLElement::SetText( int v )
{
char buf[BUF_SIZE];
XMLUtil::ToStr( v, buf, BUF_SIZE );
SetText( buf );
}
void XMLElement::SetText( unsigned v )
{
char buf[BUF_SIZE];
XMLUtil::ToStr( v, buf, BUF_SIZE );
SetText( buf );
}
void XMLElement::SetText(int64_t v)
{
char buf[BUF_SIZE];
XMLUtil::ToStr(v, buf, BUF_SIZE);
SetText(buf);
}
void XMLElement::SetText(uint64_t v) {
char buf[BUF_SIZE];
XMLUtil::ToStr(v, buf, BUF_SIZE);
SetText(buf);
}
void XMLElement::SetText( bool v )
{
char buf[BUF_SIZE];
XMLUtil::ToStr( v, buf, BUF_SIZE );
SetText( buf );
}
void XMLElement::SetText( float v )
{
char buf[BUF_SIZE];
XMLUtil::ToStr( v, buf, BUF_SIZE );
SetText( buf );
}
void XMLElement::SetText( double v )
{
char buf[BUF_SIZE];
XMLUtil::ToStr( v, buf, BUF_SIZE );
SetText( buf );
}
XMLError XMLElement::QueryIntText( int* ival ) const
{
if ( FirstChild() && FirstChild()->ToText() ) {
const char* t = FirstChild()->Value();
if ( XMLUtil::ToInt( t, ival ) ) {
return XML_SUCCESS;
}
return XML_CAN_NOT_CONVERT_TEXT;
}
return XML_NO_TEXT_NODE;
}
XMLError XMLElement::QueryUnsignedText( unsigned* uval ) const
{
if ( FirstChild() && FirstChild()->ToText() ) {
const char* t = FirstChild()->Value();
if ( XMLUtil::ToUnsigned( t, uval ) ) {
return XML_SUCCESS;
}
return XML_CAN_NOT_CONVERT_TEXT;
}
return XML_NO_TEXT_NODE;
}
XMLError XMLElement::QueryInt64Text(int64_t* ival) const
{
if (FirstChild() && FirstChild()->ToText()) {
const char* t = FirstChild()->Value();
if (XMLUtil::ToInt64(t, ival)) {
return XML_SUCCESS;
}
return XML_CAN_NOT_CONVERT_TEXT;
}
return XML_NO_TEXT_NODE;
}
XMLError XMLElement::QueryUnsigned64Text(uint64_t* uval) const
{
if(FirstChild() && FirstChild()->ToText()) {
const char* t = FirstChild()->Value();
if(XMLUtil::ToUnsigned64(t, uval)) {
return XML_SUCCESS;
}
return XML_CAN_NOT_CONVERT_TEXT;
}
return XML_NO_TEXT_NODE;
}
XMLError XMLElement::QueryBoolText( bool* bval ) const
{
if ( FirstChild() && FirstChild()->ToText() ) {
const char* t = FirstChild()->Value();
if ( XMLUtil::ToBool( t, bval ) ) {
return XML_SUCCESS;
}
return XML_CAN_NOT_CONVERT_TEXT;
}
return XML_NO_TEXT_NODE;
}
XMLError XMLElement::QueryDoubleText( double* dval ) const
{
if ( FirstChild() && FirstChild()->ToText() ) {
const char* t = FirstChild()->Value();
if ( XMLUtil::ToDouble( t, dval ) ) {
return XML_SUCCESS;
}
return XML_CAN_NOT_CONVERT_TEXT;
}
return XML_NO_TEXT_NODE;
}
XMLError XMLElement::QueryFloatText( float* fval ) const
{
if ( FirstChild() && FirstChild()->ToText() ) {
const char* t = FirstChild()->Value();
if ( XMLUtil::ToFloat( t, fval ) ) {
return XML_SUCCESS;
}
return XML_CAN_NOT_CONVERT_TEXT;
}
return XML_NO_TEXT_NODE;
}
int XMLElement::IntText(int defaultValue) const
{
int i = defaultValue;
QueryIntText(&i);
return i;
}
unsigned XMLElement::UnsignedText(unsigned defaultValue) const
{
unsigned i = defaultValue;
QueryUnsignedText(&i);
return i;
}
int64_t XMLElement::Int64Text(int64_t defaultValue) const
{
int64_t i = defaultValue;
QueryInt64Text(&i);
return i;
}
uint64_t XMLElement::Unsigned64Text(uint64_t defaultValue) const
{
uint64_t i = defaultValue;
QueryUnsigned64Text(&i);
return i;
}
bool XMLElement::BoolText(bool defaultValue) const
{
bool b = defaultValue;
QueryBoolText(&b);
return b;
}
double XMLElement::DoubleText(double defaultValue) const
{
double d = defaultValue;
QueryDoubleText(&d);
return d;
}
float XMLElement::FloatText(float defaultValue) const
{
float f = defaultValue;
QueryFloatText(&f);
return f;
}
XMLAttribute* XMLElement::FindOrCreateAttribute( const char* name )
{
XMLAttribute* last = 0;
XMLAttribute* attrib = 0;
for( attrib = _rootAttribute;
attrib;
last = attrib, attrib = attrib->_next ) {
if ( XMLUtil::StringEqual( attrib->Name(), name ) ) {
break;
}
}
if ( !attrib ) {
attrib = CreateAttribute();
TIXMLASSERT( attrib );
if ( last ) {
TIXMLASSERT( last->_next == 0 );
last->_next = attrib;
}
else {
TIXMLASSERT( _rootAttribute == 0 );
_rootAttribute = attrib;
}
attrib->SetName( name );
}
return attrib;
}
void XMLElement::DeleteAttribute( const char* name )
{
XMLAttribute* prev = 0;
for( XMLAttribute* a=_rootAttribute; a; a=a->_next ) {
if ( XMLUtil::StringEqual( name, a->Name() ) ) {
if ( prev ) {
prev->_next = a->_next;
}
else {
_rootAttribute = a->_next;
}
DeleteAttribute( a );
break;
}
prev = a;
}
}
char* XMLElement::ParseAttributes( char* p, int* curLineNumPtr )
{
XMLAttribute* prevAttribute = 0;
// Read the attributes.
while( p ) {
p = XMLUtil::SkipWhiteSpace( p, curLineNumPtr );
if ( !(*p) ) {
_document->SetError( XML_ERROR_PARSING_ELEMENT, _parseLineNum, "XMLElement name=%s", Name() );
return 0;
}
// attribute.
if (XMLUtil::IsNameStartChar( (unsigned char) *p ) ) {
XMLAttribute* attrib = CreateAttribute();
TIXMLASSERT( attrib );
attrib->_parseLineNum = _document->_parseCurLineNum;
const int attrLineNum = attrib->_parseLineNum;
p = attrib->ParseDeep( p, _document->ProcessEntities(), curLineNumPtr );
if ( !p || Attribute( attrib->Name() ) ) {
DeleteAttribute( attrib );
_document->SetError( XML_ERROR_PARSING_ATTRIBUTE, attrLineNum, "XMLElement name=%s", Name() );
return 0;
}
// There is a minor bug here: if the attribute in the source xml
// document is duplicated, it will not be detected and the
// attribute will be doubly added. However, tracking the 'prevAttribute'
// avoids re-scanning the attribute list. Preferring performance for
// now, may reconsider in the future.
if ( prevAttribute ) {
TIXMLASSERT( prevAttribute->_next == 0 );
prevAttribute->_next = attrib;
}
else {
TIXMLASSERT( _rootAttribute == 0 );
_rootAttribute = attrib;
}
prevAttribute = attrib;
}
// end of the tag
else if ( *p == '>' ) {
++p;
break;
}
// end of the tag
else if ( *p == '/' && *(p+1) == '>' ) {
_closingType = CLOSED;
return p+2; // done; sealed element.
}
else {
_document->SetError( XML_ERROR_PARSING_ELEMENT, _parseLineNum, 0 );
return 0;
}
}
return p;
}
void XMLElement::DeleteAttribute( XMLAttribute* attribute )
{
if ( attribute == 0 ) {
return;
}
MemPool* pool = attribute->_memPool;
attribute->~XMLAttribute();
pool->Free( attribute );
}
XMLAttribute* XMLElement::CreateAttribute()
{
TIXMLASSERT( sizeof( XMLAttribute ) == _document->_attributePool.ItemSize() );
XMLAttribute* attrib = new (_document->_attributePool.Alloc() ) XMLAttribute();
TIXMLASSERT( attrib );
attrib->_memPool = &_document->_attributePool;
attrib->_memPool->SetTracked();
return attrib;
}
XMLElement* XMLElement::InsertNewChildElement(const char* name)
{
XMLElement* node = _document->NewElement(name);
return InsertEndChild(node) ? node : 0;
}
XMLComment* XMLElement::InsertNewComment(const char* comment)
{
XMLComment* node = _document->NewComment(comment);
return InsertEndChild(node) ? node : 0;
}
XMLText* XMLElement::InsertNewText(const char* text)
{
XMLText* node = _document->NewText(text);
return InsertEndChild(node) ? node : 0;
}
XMLDeclaration* XMLElement::InsertNewDeclaration(const char* text)
{
XMLDeclaration* node = _document->NewDeclaration(text);
return InsertEndChild(node) ? node : 0;
}
XMLUnknown* XMLElement::InsertNewUnknown(const char* text)
{
XMLUnknown* node = _document->NewUnknown(text);
return InsertEndChild(node) ? node : 0;
}
//
// <ele></ele>
// <ele>foo<b>bar</b></ele>
//
char* XMLElement::ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr )
{
// Read the element name.
p = XMLUtil::SkipWhiteSpace( p, curLineNumPtr );
// The closing element is the </element> form. It is
// parsed just like a regular element then deleted from
// the DOM.
if ( *p == '/' ) {
_closingType = CLOSING;
++p;
}
p = _value.ParseName( p );
if ( _value.Empty() ) {
return 0;
}
p = ParseAttributes( p, curLineNumPtr );
if ( !p || !*p || _closingType != OPEN ) {
return p;
}
p = XMLNode::ParseDeep( p, parentEndTag, curLineNumPtr );
return p;
}
XMLNode* XMLElement::ShallowClone( XMLDocument* doc ) const
{
if ( !doc ) {
doc = _document;
}
XMLElement* element = doc->NewElement( Value() ); // fixme: this will always allocate memory. Intern?
for( const XMLAttribute* a=FirstAttribute(); a; a=a->Next() ) {
element->SetAttribute( a->Name(), a->Value() ); // fixme: this will always allocate memory. Intern?
}
return element;
}
bool XMLElement::ShallowEqual( const XMLNode* compare ) const
{
TIXMLASSERT( compare );
const XMLElement* other = compare->ToElement();
if ( other && XMLUtil::StringEqual( other->Name(), Name() )) {
const XMLAttribute* a=FirstAttribute();
const XMLAttribute* b=other->FirstAttribute();
while ( a && b ) {
if ( !XMLUtil::StringEqual( a->Value(), b->Value() ) ) {
return false;
}
a = a->Next();
b = b->Next();
}
if ( a || b ) {
// different count
return false;
}
return true;
}
return false;
}
bool XMLElement::Accept( XMLVisitor* visitor ) const
{
TIXMLASSERT( visitor );
if ( visitor->VisitEnter( *this, _rootAttribute ) ) {
for ( const XMLNode* node=FirstChild(); node; node=node->NextSibling() ) {
if ( !node->Accept( visitor ) ) {
break;
}
}
}
return visitor->VisitExit( *this );
}
// --------- XMLDocument ----------- //
// Warning: List must match 'enum XMLError'
const char* XMLDocument::_errorNames[XML_ERROR_COUNT] = {
"XML_SUCCESS",
"XML_NO_ATTRIBUTE",
"XML_WRONG_ATTRIBUTE_TYPE",
"XML_ERROR_FILE_NOT_FOUND",
"XML_ERROR_FILE_COULD_NOT_BE_OPENED",
"XML_ERROR_FILE_READ_ERROR",
"XML_ERROR_PARSING_ELEMENT",
"XML_ERROR_PARSING_ATTRIBUTE",
"XML_ERROR_PARSING_TEXT",
"XML_ERROR_PARSING_CDATA",
"XML_ERROR_PARSING_COMMENT",
"XML_ERROR_PARSING_DECLARATION",
"XML_ERROR_PARSING_UNKNOWN",
"XML_ERROR_EMPTY_DOCUMENT",
"XML_ERROR_MISMATCHED_ELEMENT",
"XML_ERROR_PARSING",
"XML_CAN_NOT_CONVERT_TEXT",
"XML_NO_TEXT_NODE",
"XML_ELEMENT_DEPTH_EXCEEDED"
};
XMLDocument::XMLDocument( bool processEntities, Whitespace whitespaceMode ) :
XMLNode( 0 ),
_writeBOM( false ),
_processEntities( processEntities ),
_errorID(XML_SUCCESS),
_whitespaceMode( whitespaceMode ),
_errorStr(),
_errorLineNum( 0 ),
_charBuffer( 0 ),
_parseCurLineNum( 0 ),
_parsingDepth(0),
_unlinked(),
_elementPool(),
_attributePool(),
_textPool(),
_commentPool()
{
// avoid VC++ C4355 warning about 'this' in initializer list (C4355 is off by default in VS2012+)
_document = this;
}
XMLDocument::~XMLDocument()
{
Clear();
}
void XMLDocument::MarkInUse(const XMLNode* const node)
{
TIXMLASSERT(node);
TIXMLASSERT(node->_parent == 0);
for (int i = 0; i < _unlinked.Size(); ++i) {
if (node == _unlinked[i]) {
_unlinked.SwapRemove(i);
break;
}
}
}
void XMLDocument::Clear()
{
DeleteChildren();
while( _unlinked.Size()) {
DeleteNode(_unlinked[0]); // Will remove from _unlinked as part of delete.
}
#ifdef TINYXML2_DEBUG
const bool hadError = Error();
#endif
ClearError();
delete [] _charBuffer;
_charBuffer = 0;
_parsingDepth = 0;
#if 0
_textPool.Trace( "text" );
_elementPool.Trace( "element" );
_commentPool.Trace( "comment" );
_attributePool.Trace( "attribute" );
#endif
#ifdef TINYXML2_DEBUG
if ( !hadError ) {
TIXMLASSERT( _elementPool.CurrentAllocs() == _elementPool.Untracked() );
TIXMLASSERT( _attributePool.CurrentAllocs() == _attributePool.Untracked() );
TIXMLASSERT( _textPool.CurrentAllocs() == _textPool.Untracked() );
TIXMLASSERT( _commentPool.CurrentAllocs() == _commentPool.Untracked() );
}
#endif
}
void XMLDocument::DeepCopy(XMLDocument* target) const
{
TIXMLASSERT(target);
if (target == this) {
return; // technically success - a no-op.
}
target->Clear();
for (const XMLNode* node = this->FirstChild(); node; node = node->NextSibling()) {
target->InsertEndChild(node->DeepClone(target));
}
}
XMLElement* XMLDocument::NewElement( const char* name )
{
XMLElement* ele = CreateUnlinkedNode<XMLElement>( _elementPool );
ele->SetName( name );
return ele;
}
XMLComment* XMLDocument::NewComment( const char* str )
{
XMLComment* comment = CreateUnlinkedNode<XMLComment>( _commentPool );
comment->SetValue( str );
return comment;
}
XMLText* XMLDocument::NewText( const char* str )
{
XMLText* text = CreateUnlinkedNode<XMLText>( _textPool );
text->SetValue( str );
return text;
}
XMLDeclaration* XMLDocument::NewDeclaration( const char* str )
{
XMLDeclaration* dec = CreateUnlinkedNode<XMLDeclaration>( _commentPool );
dec->SetValue( str ? str : "xml version=\"1.0\" encoding=\"UTF-8\"" );
return dec;
}
XMLUnknown* XMLDocument::NewUnknown( const char* str )
{
XMLUnknown* unk = CreateUnlinkedNode<XMLUnknown>( _commentPool );
unk->SetValue( str );
return unk;
}
static FILE* callfopen( const char* filepath, const char* mode )
{
TIXMLASSERT( filepath );
TIXMLASSERT( mode );
#if defined(_MSC_VER) && (_MSC_VER >= 1400 ) && (!defined WINCE)
FILE* fp = 0;
const errno_t err = fopen_s( &fp, filepath, mode );
if ( err ) {
return 0;
}
#else
FILE* fp = fopen( filepath, mode );
#endif
return fp;
}
void XMLDocument::DeleteNode( XMLNode* node ) {
TIXMLASSERT( node );
TIXMLASSERT(node->_document == this );
if (node->_parent) {
node->_parent->DeleteChild( node );
}
else {
// Isn't in the tree.
// Use the parent delete.
// Also, we need to mark it tracked: we 'know'
// it was never used.
node->_memPool->SetTracked();
// Call the static XMLNode version:
XMLNode::DeleteNode(node);
}
}
XMLError XMLDocument::LoadFile( const char* filename )
{
if ( !filename ) {
TIXMLASSERT( false );
SetError( XML_ERROR_FILE_COULD_NOT_BE_OPENED, 0, "filename=<null>" );
return _errorID;
}
Clear();
FILE* fp = callfopen( filename, "rb" );
if ( !fp ) {
SetError( XML_ERROR_FILE_NOT_FOUND, 0, "filename=%s", filename );
return _errorID;
}
LoadFile( fp );
fclose( fp );
return _errorID;
}
XMLError XMLDocument::LoadFile( FILE* fp )
{
Clear();
TIXML_FSEEK( fp, 0, SEEK_SET );
if ( fgetc( fp ) == EOF && ferror( fp ) != 0 ) {
SetError( XML_ERROR_FILE_READ_ERROR, 0, 0 );
return _errorID;
}
TIXML_FSEEK( fp, 0, SEEK_END );
unsigned long long filelength;
{
const long long fileLengthSigned = TIXML_FTELL( fp );
TIXML_FSEEK( fp, 0, SEEK_SET );
if ( fileLengthSigned == -1L ) {
SetError( XML_ERROR_FILE_READ_ERROR, 0, 0 );
return _errorID;
}
TIXMLASSERT( fileLengthSigned >= 0 );
filelength = static_cast<unsigned long long>(fileLengthSigned);
}
const size_t maxSizeT = static_cast<size_t>(-1);
// We'll do the comparison as an unsigned long long, because that's guaranteed to be at
// least 8 bytes, even on a 32-bit platform.
if ( filelength >= static_cast<unsigned long long>(maxSizeT) ) {
// Cannot handle files which won't fit in buffer together with null terminator
SetError( XML_ERROR_FILE_READ_ERROR, 0, 0 );
return _errorID;
}
if ( filelength == 0 ) {
SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0 );
return _errorID;
}
const size_t size = static_cast<size_t>(filelength);
TIXMLASSERT( _charBuffer == 0 );
_charBuffer = new char[size+1];
const size_t read = fread( _charBuffer, 1, size, fp );
if ( read != size ) {
SetError( XML_ERROR_FILE_READ_ERROR, 0, 0 );
return _errorID;
}
_charBuffer[size] = 0;
Parse();
return _errorID;
}
XMLError XMLDocument::SaveFile( const char* filename, bool compact )
{
if ( !filename ) {
TIXMLASSERT( false );
SetError( XML_ERROR_FILE_COULD_NOT_BE_OPENED, 0, "filename=<null>" );
return _errorID;
}
FILE* fp = callfopen( filename, "w" );
if ( !fp ) {
SetError( XML_ERROR_FILE_COULD_NOT_BE_OPENED, 0, "filename=%s", filename );
return _errorID;
}
SaveFile(fp, compact);
fclose( fp );
return _errorID;
}
XMLError XMLDocument::SaveFile( FILE* fp, bool compact )
{
// Clear any error from the last save, otherwise it will get reported
// for *this* call.
ClearError();
XMLPrinter stream( fp, compact );
Print( &stream );
return _errorID;
}
XMLError XMLDocument::Parse( const char* xml, size_t nBytes )
{
Clear();
if ( nBytes == 0 || !xml || !*xml ) {
SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0 );
return _errorID;
}
if ( nBytes == static_cast<size_t>(-1) ) {
nBytes = strlen( xml );
}
TIXMLASSERT( _charBuffer == 0 );
_charBuffer = new char[ nBytes+1 ];
memcpy( _charBuffer, xml, nBytes );
_charBuffer[nBytes] = 0;
Parse();
if ( Error() ) {
// clean up now essentially dangling memory.
// and the parse fail can put objects in the
// pools that are dead and inaccessible.
DeleteChildren();
_elementPool.Clear();
_attributePool.Clear();
_textPool.Clear();
_commentPool.Clear();
}
return _errorID;
}
void XMLDocument::Print( XMLPrinter* streamer ) const
{
if ( streamer ) {
Accept( streamer );
}
else {
XMLPrinter stdoutStreamer( stdout );
Accept( &stdoutStreamer );
}
}
void XMLDocument::ClearError() {
_errorID = XML_SUCCESS;
_errorLineNum = 0;
_errorStr.Reset();
}
void XMLDocument::SetError( XMLError error, int lineNum, const char* format, ... )
{
TIXMLASSERT( error >= 0 && error < XML_ERROR_COUNT );
_errorID = error;
_errorLineNum = lineNum;
_errorStr.Reset();
const size_t BUFFER_SIZE = 1000;
char* buffer = new char[BUFFER_SIZE];
TIXMLASSERT(sizeof(error) <= sizeof(int));
TIXML_SNPRINTF(buffer, BUFFER_SIZE, "Error=%s ErrorID=%d (0x%x) Line number=%d", ErrorIDToName(error), int(error), int(error), lineNum);
if (format) {
size_t len = strlen(buffer);
TIXML_SNPRINTF(buffer + len, BUFFER_SIZE - len, ": ");
len = strlen(buffer);
va_list va;
va_start(va, format);
TIXML_VSNPRINTF(buffer + len, BUFFER_SIZE - len, format, va);
va_end(va);
}
_errorStr.SetStr(buffer);
delete[] buffer;
}
/*static*/ const char* XMLDocument::ErrorIDToName(XMLError errorID)
{
TIXMLASSERT( errorID >= 0 && errorID < XML_ERROR_COUNT );
const char* errorName = _errorNames[errorID];
TIXMLASSERT( errorName && errorName[0] );
return errorName;
}
const char* XMLDocument::ErrorStr() const
{
return _errorStr.Empty() ? "" : _errorStr.GetStr();
}
void XMLDocument::PrintError() const
{
printf("%s\n", ErrorStr());
}
const char* XMLDocument::ErrorName() const
{
return ErrorIDToName(_errorID);
}
void XMLDocument::Parse()
{
TIXMLASSERT( NoChildren() ); // Clear() must have been called previously
TIXMLASSERT( _charBuffer );
_parseCurLineNum = 1;
_parseLineNum = 1;
char* p = _charBuffer;
p = XMLUtil::SkipWhiteSpace( p, &_parseCurLineNum );
p = const_cast<char*>( XMLUtil::ReadBOM( p, &_writeBOM ) );
if ( !*p ) {
SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0 );
return;
}
ParseDeep(p, 0, &_parseCurLineNum );
}
void XMLDocument::PushDepth()
{
_parsingDepth++;
if (_parsingDepth == TINYXML2_MAX_ELEMENT_DEPTH) {
SetError(XML_ELEMENT_DEPTH_EXCEEDED, _parseCurLineNum, "Element nesting is too deep." );
}
}
void XMLDocument::PopDepth()
{
TIXMLASSERT(_parsingDepth > 0);
--_parsingDepth;
}
XMLPrinter::XMLPrinter( FILE* file, bool compact, int depth ) :
_elementJustOpened( false ),
_stack(),
_firstElement( true ),
_fp( file ),
_depth( depth ),
_textDepth( -1 ),
_processEntities( true ),
_compactMode( compact ),
_buffer()
{
for( int i=0; i<ENTITY_RANGE; ++i ) {
_entityFlag[i] = false;
_restrictedEntityFlag[i] = false;
}
for( int i=0; i<NUM_ENTITIES; ++i ) {
const char entityValue = entities[i].value;
const unsigned char flagIndex = static_cast<unsigned char>(entityValue);
TIXMLASSERT( flagIndex < ENTITY_RANGE );
_entityFlag[flagIndex] = true;
}
_restrictedEntityFlag[static_cast<unsigned char>('&')] = true;
_restrictedEntityFlag[static_cast<unsigned char>('<')] = true;
_restrictedEntityFlag[static_cast<unsigned char>('>')] = true; // not required, but consistency is nice
_buffer.Push( 0 );
}
void XMLPrinter::Print( const char* format, ... )
{
va_list va;
va_start( va, format );
if ( _fp ) {
vfprintf( _fp, format, va );
}
else {
const int len = TIXML_VSCPRINTF( format, va );
// Close out and re-start the va-args
va_end( va );
TIXMLASSERT( len >= 0 );
va_start( va, format );
TIXMLASSERT( _buffer.Size() > 0 && _buffer[_buffer.Size() - 1] == 0 );
char* p = _buffer.PushArr( len ) - 1; // back up over the null terminator.
TIXML_VSNPRINTF( p, len+1, format, va );
}
va_end( va );
}
void XMLPrinter::Write( const char* data, size_t size )
{
if ( _fp ) {
fwrite ( data , sizeof(char), size, _fp);
}
else {
char* p = _buffer.PushArr( static_cast<int>(size) ) - 1; // back up over the null terminator.
memcpy( p, data, size );
p[size] = 0;
}
}
void XMLPrinter::Putc( char ch )
{
if ( _fp ) {
fputc ( ch, _fp);
}
else {
char* p = _buffer.PushArr( sizeof(char) ) - 1; // back up over the null terminator.
p[0] = ch;
p[1] = 0;
}
}
void XMLPrinter::PrintSpace( int depth )
{
for( int i=0; i<depth; ++i ) {
Write( " " );
}
}
void XMLPrinter::PrintString( const char* p, bool restricted )
{
// Look for runs of bytes between entities to print.
const char* q = p;
if ( _processEntities ) {
const bool* flag = restricted ? _restrictedEntityFlag : _entityFlag;
while ( *q ) {
TIXMLASSERT( p <= q );
// Remember, char is sometimes signed. (How many times has that bitten me?)
if ( *q > 0 && *q < ENTITY_RANGE ) {
// Check for entities. If one is found, flush
// the stream up until the entity, write the
// entity, and keep looking.
if ( flag[static_cast<unsigned char>(*q)] ) {
while ( p < q ) {
const size_t delta = q - p;
const int toPrint = ( INT_MAX < delta ) ? INT_MAX : static_cast<int>(delta);
Write( p, toPrint );
p += toPrint;
}
bool entityPatternPrinted = false;
for( int i=0; i<NUM_ENTITIES; ++i ) {
if ( entities[i].value == *q ) {
Putc( '&' );
Write( entities[i].pattern, entities[i].length );
Putc( ';' );
entityPatternPrinted = true;
break;
}
}
if ( !entityPatternPrinted ) {
// TIXMLASSERT( entityPatternPrinted ) causes gcc -Wunused-but-set-variable in release
TIXMLASSERT( false );
}
++p;
}
}
++q;
TIXMLASSERT( p <= q );
}
// Flush the remaining string. This will be the entire
// string if an entity wasn't found.
if ( p < q ) {
const size_t delta = q - p;
const int toPrint = ( INT_MAX < delta ) ? INT_MAX : static_cast<int>(delta);
Write( p, toPrint );
}
}
else {
Write( p );
}
}
void XMLPrinter::PushHeader( bool writeBOM, bool writeDec )
{
if ( writeBOM ) {
static const unsigned char bom[] = { TIXML_UTF_LEAD_0, TIXML_UTF_LEAD_1, TIXML_UTF_LEAD_2, 0 };
Write( reinterpret_cast< const char* >( bom ) );
}
if ( writeDec ) {
PushDeclaration( "xml version=\"1.0\"" );
}
}
void XMLPrinter::PrepareForNewNode( bool compactMode )
{
SealElementIfJustOpened();
if ( compactMode ) {
return;
}
if ( _firstElement ) {
PrintSpace (_depth);
} else if ( _textDepth < 0) {
Putc( '\n' );
PrintSpace( _depth );
}
_firstElement = false;
}
void XMLPrinter::OpenElement( const char* name, bool compactMode )
{
PrepareForNewNode( compactMode );
_stack.Push( name );
Write ( "<" );
Write ( name );
_elementJustOpened = true;
++_depth;
}
void XMLPrinter::PushAttribute( const char* name, const char* value )
{
TIXMLASSERT( _elementJustOpened );
Putc ( ' ' );
Write( name );
Write( "=\"" );
PrintString( value, false );
Putc ( '\"' );
}
void XMLPrinter::PushAttribute( const char* name, int v )
{
char buf[BUF_SIZE];
XMLUtil::ToStr( v, buf, BUF_SIZE );
PushAttribute( name, buf );
}
void XMLPrinter::PushAttribute( const char* name, unsigned v )
{
char buf[BUF_SIZE];
XMLUtil::ToStr( v, buf, BUF_SIZE );
PushAttribute( name, buf );
}
void XMLPrinter::PushAttribute(const char* name, int64_t v)
{
char buf[BUF_SIZE];
XMLUtil::ToStr(v, buf, BUF_SIZE);
PushAttribute(name, buf);
}
void XMLPrinter::PushAttribute(const char* name, uint64_t v)
{
char buf[BUF_SIZE];
XMLUtil::ToStr(v, buf, BUF_SIZE);
PushAttribute(name, buf);
}
void XMLPrinter::PushAttribute( const char* name, bool v )
{
char buf[BUF_SIZE];
XMLUtil::ToStr( v, buf, BUF_SIZE );
PushAttribute( name, buf );
}
void XMLPrinter::PushAttribute( const char* name, double v )
{
char buf[BUF_SIZE];
XMLUtil::ToStr( v, buf, BUF_SIZE );
PushAttribute( name, buf );
}
void XMLPrinter::CloseElement( bool compactMode )
{
--_depth;
const char* name = _stack.Pop();
if ( _elementJustOpened ) {
Write( "/>" );
}
else {
if ( _textDepth < 0 && !compactMode) {
Putc( '\n' );
PrintSpace( _depth );
}
Write ( "</" );
Write ( name );
Write ( ">" );
}
if ( _textDepth == _depth ) {
_textDepth = -1;
}
if ( _depth == 0 && !compactMode) {
Putc( '\n' );
}
_elementJustOpened = false;
}
void XMLPrinter::SealElementIfJustOpened()
{
if ( !_elementJustOpened ) {
return;
}
_elementJustOpened = false;
Putc( '>' );
}
void XMLPrinter::PushText( const char* text, bool cdata )
{
_textDepth = _depth-1;
SealElementIfJustOpened();
if ( cdata ) {
Write( "<![CDATA[" );
Write( text );
Write( "]]>" );
}
else {
PrintString( text, true );
}
}
void XMLPrinter::PushText( int64_t value )
{
char buf[BUF_SIZE];
XMLUtil::ToStr( value, buf, BUF_SIZE );
PushText( buf, false );
}
void XMLPrinter::PushText( uint64_t value )
{
char buf[BUF_SIZE];
XMLUtil::ToStr(value, buf, BUF_SIZE);
PushText(buf, false);
}
void XMLPrinter::PushText( int value )
{
char buf[BUF_SIZE];
XMLUtil::ToStr( value, buf, BUF_SIZE );
PushText( buf, false );
}
void XMLPrinter::PushText( unsigned value )
{
char buf[BUF_SIZE];
XMLUtil::ToStr( value, buf, BUF_SIZE );
PushText( buf, false );
}
void XMLPrinter::PushText( bool value )
{
char buf[BUF_SIZE];
XMLUtil::ToStr( value, buf, BUF_SIZE );
PushText( buf, false );
}
void XMLPrinter::PushText( float value )
{
char buf[BUF_SIZE];
XMLUtil::ToStr( value, buf, BUF_SIZE );
PushText( buf, false );
}
void XMLPrinter::PushText( double value )
{
char buf[BUF_SIZE];
XMLUtil::ToStr( value, buf, BUF_SIZE );
PushText( buf, false );
}
void XMLPrinter::PushComment( const char* comment )
{
PrepareForNewNode( _compactMode );
Write( "<!--" );
Write( comment );
Write( "-->" );
}
void XMLPrinter::PushDeclaration( const char* value )
{
PrepareForNewNode( _compactMode );
Write( "<?" );
Write( value );
Write( "?>" );
}
void XMLPrinter::PushUnknown( const char* value )
{
PrepareForNewNode( _compactMode );
Write( "<!" );
Write( value );
Putc( '>' );
}
bool XMLPrinter::VisitEnter( const XMLDocument& doc )
{
_processEntities = doc.ProcessEntities();
if ( doc.HasBOM() ) {
PushHeader( true, false );
}
return true;
}
bool XMLPrinter::VisitEnter( const XMLElement& element, const XMLAttribute* attribute )
{
const XMLElement* parentElem = 0;
if ( element.Parent() ) {
parentElem = element.Parent()->ToElement();
}
const bool compactMode = parentElem ? CompactMode( *parentElem ) : _compactMode;
OpenElement( element.Name(), compactMode );
while ( attribute ) {
PushAttribute( attribute->Name(), attribute->Value() );
attribute = attribute->Next();
}
return true;
}
bool XMLPrinter::VisitExit( const XMLElement& element )
{
CloseElement( CompactMode(element) );
return true;
}
bool XMLPrinter::Visit( const XMLText& text )
{
PushText( text.Value(), text.CData() );
return true;
}
bool XMLPrinter::Visit( const XMLComment& comment )
{
PushComment( comment.Value() );
return true;
}
bool XMLPrinter::Visit( const XMLDeclaration& declaration )
{
PushDeclaration( declaration.Value() );
return true;
}
bool XMLPrinter::Visit( const XMLUnknown& unknown )
{
PushUnknown( unknown.Value() );
return true;
}
} // namespace tinyxml2
| cpp |
tinyxml2 | data/projects/tinyxml2/tinyxml2.h | /*
Original code by Lee Thomason (www.grinninglizard.com)
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any
damages arising from the use of this software.
Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it and
redistribute it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must
not claim that you wrote the original software. If you use this
software in a product, an acknowledgment in the product documentation
would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
#ifndef TINYXML2_INCLUDED
#define TINYXML2_INCLUDED
#if defined(ANDROID_NDK) || defined(__BORLANDC__) || defined(__QNXNTO__)
# include <ctype.h>
# include <limits.h>
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# if defined(__PS3__)
# include <stddef.h>
# endif
#else
# include <cctype>
# include <climits>
# include <cstdio>
# include <cstdlib>
# include <cstring>
#endif
#include <stdint.h>
/*
gcc:
g++ -Wall -DTINYXML2_DEBUG tinyxml2.cpp xmltest.cpp -o gccxmltest.exe
Formatting, Artistic Style:
AStyle.exe --style=1tbs --indent-switches --break-closing-brackets --indent-preprocessor tinyxml2.cpp tinyxml2.h
*/
#if defined( _DEBUG ) || defined (__DEBUG__)
# ifndef TINYXML2_DEBUG
# define TINYXML2_DEBUG
# endif
#endif
#ifdef _MSC_VER
# pragma warning(push)
# pragma warning(disable: 4251)
#endif
#ifdef _MSC_VER
# ifdef TINYXML2_EXPORT
# define TINYXML2_LIB __declspec(dllexport)
# elif defined(TINYXML2_IMPORT)
# define TINYXML2_LIB __declspec(dllimport)
# else
# define TINYXML2_LIB
# endif
#elif __GNUC__ >= 4
# define TINYXML2_LIB __attribute__((visibility("default")))
#else
# define TINYXML2_LIB
#endif
#if !defined(TIXMLASSERT)
#if defined(TINYXML2_DEBUG)
# if defined(_MSC_VER)
# // "(void)0," is for suppressing C4127 warning in "assert(false)", "assert(true)" and the like
# define TIXMLASSERT( x ) do { if ( !((void)0,(x))) { __debugbreak(); } } while(false)
# elif defined (ANDROID_NDK)
# include <android/log.h>
# define TIXMLASSERT( x ) do { if ( !(x)) { __android_log_assert( "assert", "grinliz", "ASSERT in '%s' at %d.", __FILE__, __LINE__ ); } } while(false)
# else
# include <assert.h>
# define TIXMLASSERT assert
# endif
#else
# define TIXMLASSERT( x ) do {} while(false)
#endif
#endif
/* Versioning, past 1.0.14:
http://semver.org/
*/
static const int TIXML2_MAJOR_VERSION = 10;
static const int TIXML2_MINOR_VERSION = 0;
static const int TIXML2_PATCH_VERSION = 0;
#define TINYXML2_MAJOR_VERSION 10
#define TINYXML2_MINOR_VERSION 0
#define TINYXML2_PATCH_VERSION 0
// A fixed element depth limit is problematic. There needs to be a
// limit to avoid a stack overflow. However, that limit varies per
// system, and the capacity of the stack. On the other hand, it's a trivial
// attack that can result from ill, malicious, or even correctly formed XML,
// so there needs to be a limit in place.
static const int TINYXML2_MAX_ELEMENT_DEPTH = 500;
namespace tinyxml2
{
class XMLDocument;
class XMLElement;
class XMLAttribute;
class XMLComment;
class XMLText;
class XMLDeclaration;
class XMLUnknown;
class XMLPrinter;
/*
A class that wraps strings. Normally stores the start and end
pointers into the XML file itself, and will apply normalization
and entity translation if actually read. Can also store (and memory
manage) a traditional char[]
Isn't clear why TINYXML2_LIB is needed; but seems to fix #719
*/
class TINYXML2_LIB StrPair
{
public:
enum Mode {
NEEDS_ENTITY_PROCESSING = 0x01,
NEEDS_NEWLINE_NORMALIZATION = 0x02,
NEEDS_WHITESPACE_COLLAPSING = 0x04,
TEXT_ELEMENT = NEEDS_ENTITY_PROCESSING | NEEDS_NEWLINE_NORMALIZATION,
TEXT_ELEMENT_LEAVE_ENTITIES = NEEDS_NEWLINE_NORMALIZATION,
ATTRIBUTE_NAME = 0,
ATTRIBUTE_VALUE = NEEDS_ENTITY_PROCESSING | NEEDS_NEWLINE_NORMALIZATION,
ATTRIBUTE_VALUE_LEAVE_ENTITIES = NEEDS_NEWLINE_NORMALIZATION,
COMMENT = NEEDS_NEWLINE_NORMALIZATION
};
StrPair() : _flags( 0 ), _start( 0 ), _end( 0 ) {}
~StrPair();
void Set( char* start, char* end, int flags ) {
TIXMLASSERT( start );
TIXMLASSERT( end );
Reset();
_start = start;
_end = end;
_flags = flags | NEEDS_FLUSH;
}
const char* GetStr();
bool Empty() const {
return _start == _end;
}
void SetInternedStr( const char* str ) {
Reset();
_start = const_cast<char*>(str);
}
void SetStr( const char* str, int flags=0 );
char* ParseText( char* in, const char* endTag, int strFlags, int* curLineNumPtr );
char* ParseName( char* in );
void TransferTo( StrPair* other );
void Reset();
private:
void CollapseWhitespace();
enum {
NEEDS_FLUSH = 0x100,
NEEDS_DELETE = 0x200
};
int _flags;
char* _start;
char* _end;
StrPair( const StrPair& other ); // not supported
void operator=( const StrPair& other ); // not supported, use TransferTo()
};
/*
A dynamic array of Plain Old Data. Doesn't support constructors, etc.
Has a small initial memory pool, so that low or no usage will not
cause a call to new/delete
*/
template <class T, int INITIAL_SIZE>
class DynArray
{
public:
DynArray() :
_mem( _pool ),
_allocated( INITIAL_SIZE ),
_size( 0 )
{
}
~DynArray() {
if ( _mem != _pool ) {
delete [] _mem;
}
}
void Clear() {
_size = 0;
}
void Push( T t ) {
TIXMLASSERT( _size < INT_MAX );
EnsureCapacity( _size+1 );
_mem[_size] = t;
++_size;
}
T* PushArr( int count ) {
TIXMLASSERT( count >= 0 );
TIXMLASSERT( _size <= INT_MAX - count );
EnsureCapacity( _size+count );
T* ret = &_mem[_size];
_size += count;
return ret;
}
T Pop() {
TIXMLASSERT( _size > 0 );
--_size;
return _mem[_size];
}
void PopArr( int count ) {
TIXMLASSERT( _size >= count );
_size -= count;
}
bool Empty() const {
return _size == 0;
}
T& operator[](int i) {
TIXMLASSERT( i>= 0 && i < _size );
return _mem[i];
}
const T& operator[](int i) const {
TIXMLASSERT( i>= 0 && i < _size );
return _mem[i];
}
const T& PeekTop() const {
TIXMLASSERT( _size > 0 );
return _mem[ _size - 1];
}
int Size() const {
TIXMLASSERT( _size >= 0 );
return _size;
}
int Capacity() const {
TIXMLASSERT( _allocated >= INITIAL_SIZE );
return _allocated;
}
void SwapRemove(int i) {
TIXMLASSERT(i >= 0 && i < _size);
TIXMLASSERT(_size > 0);
_mem[i] = _mem[_size - 1];
--_size;
}
const T* Mem() const {
TIXMLASSERT( _mem );
return _mem;
}
T* Mem() {
TIXMLASSERT( _mem );
return _mem;
}
private:
DynArray( const DynArray& ); // not supported
void operator=( const DynArray& ); // not supported
void EnsureCapacity( int cap ) {
TIXMLASSERT( cap > 0 );
if ( cap > _allocated ) {
TIXMLASSERT( cap <= INT_MAX / 2 );
const int newAllocated = cap * 2;
T* newMem = new T[static_cast<unsigned int>(newAllocated)];
TIXMLASSERT( newAllocated >= _size );
memcpy( newMem, _mem, sizeof(T)*static_cast<size_t>(_size) ); // warning: not using constructors, only works for PODs
if ( _mem != _pool ) {
delete [] _mem;
}
_mem = newMem;
_allocated = newAllocated;
}
}
T* _mem;
T _pool[static_cast<size_t>(INITIAL_SIZE)];
int _allocated; // objects allocated
int _size; // number objects in use
};
/*
Parent virtual class of a pool for fast allocation
and deallocation of objects.
*/
class MemPool
{
public:
MemPool() {}
virtual ~MemPool() {}
virtual int ItemSize() const = 0;
virtual void* Alloc() = 0;
virtual void Free( void* ) = 0;
virtual void SetTracked() = 0;
};
/*
Template child class to create pools of the correct type.
*/
template< int ITEM_SIZE >
class MemPoolT : public MemPool
{
public:
MemPoolT() : _blockPtrs(), _root(0), _currentAllocs(0), _nAllocs(0), _maxAllocs(0), _nUntracked(0) {}
~MemPoolT() {
MemPoolT< ITEM_SIZE >::Clear();
}
void Clear() {
// Delete the blocks.
while( !_blockPtrs.Empty()) {
Block* lastBlock = _blockPtrs.Pop();
delete lastBlock;
}
_root = 0;
_currentAllocs = 0;
_nAllocs = 0;
_maxAllocs = 0;
_nUntracked = 0;
}
virtual int ItemSize() const override{
return ITEM_SIZE;
}
int CurrentAllocs() const {
return _currentAllocs;
}
virtual void* Alloc() override{
if ( !_root ) {
// Need a new block.
Block* block = new Block;
_blockPtrs.Push( block );
Item* blockItems = block->items;
for( int i = 0; i < ITEMS_PER_BLOCK - 1; ++i ) {
blockItems[i].next = &(blockItems[i + 1]);
}
blockItems[ITEMS_PER_BLOCK - 1].next = 0;
_root = blockItems;
}
Item* const result = _root;
TIXMLASSERT( result != 0 );
_root = _root->next;
++_currentAllocs;
if ( _currentAllocs > _maxAllocs ) {
_maxAllocs = _currentAllocs;
}
++_nAllocs;
++_nUntracked;
return result;
}
virtual void Free( void* mem ) override {
if ( !mem ) {
return;
}
--_currentAllocs;
Item* item = static_cast<Item*>( mem );
#ifdef TINYXML2_DEBUG
memset( item, 0xfe, sizeof( *item ) );
#endif
item->next = _root;
_root = item;
}
void Trace( const char* name ) {
printf( "Mempool %s watermark=%d [%dk] current=%d size=%d nAlloc=%d blocks=%d\n",
name, _maxAllocs, _maxAllocs * ITEM_SIZE / 1024, _currentAllocs,
ITEM_SIZE, _nAllocs, _blockPtrs.Size() );
}
void SetTracked() override {
--_nUntracked;
}
int Untracked() const {
return _nUntracked;
}
// This number is perf sensitive. 4k seems like a good tradeoff on my machine.
// The test file is large, 170k.
// Release: VS2010 gcc(no opt)
// 1k: 4000
// 2k: 4000
// 4k: 3900 21000
// 16k: 5200
// 32k: 4300
// 64k: 4000 21000
// Declared public because some compilers do not accept to use ITEMS_PER_BLOCK
// in private part if ITEMS_PER_BLOCK is private
enum { ITEMS_PER_BLOCK = (4 * 1024) / ITEM_SIZE };
private:
MemPoolT( const MemPoolT& ); // not supported
void operator=( const MemPoolT& ); // not supported
union Item {
Item* next;
char itemData[static_cast<size_t>(ITEM_SIZE)];
};
struct Block {
Item items[ITEMS_PER_BLOCK];
};
DynArray< Block*, 10 > _blockPtrs;
Item* _root;
int _currentAllocs;
int _nAllocs;
int _maxAllocs;
int _nUntracked;
};
/**
Implements the interface to the "Visitor pattern" (see the Accept() method.)
If you call the Accept() method, it requires being passed a XMLVisitor
class to handle callbacks. For nodes that contain other nodes (Document, Element)
you will get called with a VisitEnter/VisitExit pair. Nodes that are always leafs
are simply called with Visit().
If you return 'true' from a Visit method, recursive parsing will continue. If you return
false, <b>no children of this node or its siblings</b> will be visited.
All flavors of Visit methods have a default implementation that returns 'true' (continue
visiting). You need to only override methods that are interesting to you.
Generally Accept() is called on the XMLDocument, although all nodes support visiting.
You should never change the document from a callback.
@sa XMLNode::Accept()
*/
class TINYXML2_LIB XMLVisitor
{
public:
virtual ~XMLVisitor() {}
/// Visit a document.
virtual bool VisitEnter( const XMLDocument& /*doc*/ ) {
return true;
}
/// Visit a document.
virtual bool VisitExit( const XMLDocument& /*doc*/ ) {
return true;
}
/// Visit an element.
virtual bool VisitEnter( const XMLElement& /*element*/, const XMLAttribute* /*firstAttribute*/ ) {
return true;
}
/// Visit an element.
virtual bool VisitExit( const XMLElement& /*element*/ ) {
return true;
}
/// Visit a declaration.
virtual bool Visit( const XMLDeclaration& /*declaration*/ ) {
return true;
}
/// Visit a text node.
virtual bool Visit( const XMLText& /*text*/ ) {
return true;
}
/// Visit a comment node.
virtual bool Visit( const XMLComment& /*comment*/ ) {
return true;
}
/// Visit an unknown node.
virtual bool Visit( const XMLUnknown& /*unknown*/ ) {
return true;
}
};
// WARNING: must match XMLDocument::_errorNames[]
enum XMLError {
XML_SUCCESS = 0,
XML_NO_ATTRIBUTE,
XML_WRONG_ATTRIBUTE_TYPE,
XML_ERROR_FILE_NOT_FOUND,
XML_ERROR_FILE_COULD_NOT_BE_OPENED,
XML_ERROR_FILE_READ_ERROR,
XML_ERROR_PARSING_ELEMENT,
XML_ERROR_PARSING_ATTRIBUTE,
XML_ERROR_PARSING_TEXT,
XML_ERROR_PARSING_CDATA,
XML_ERROR_PARSING_COMMENT,
XML_ERROR_PARSING_DECLARATION,
XML_ERROR_PARSING_UNKNOWN,
XML_ERROR_EMPTY_DOCUMENT,
XML_ERROR_MISMATCHED_ELEMENT,
XML_ERROR_PARSING,
XML_CAN_NOT_CONVERT_TEXT,
XML_NO_TEXT_NODE,
XML_ELEMENT_DEPTH_EXCEEDED,
XML_ERROR_COUNT
};
/*
Utility functionality.
*/
class TINYXML2_LIB XMLUtil
{
public:
static const char* SkipWhiteSpace( const char* p, int* curLineNumPtr ) {
TIXMLASSERT( p );
while( IsWhiteSpace(*p) ) {
if (curLineNumPtr && *p == '\n') {
++(*curLineNumPtr);
}
++p;
}
TIXMLASSERT( p );
return p;
}
static char* SkipWhiteSpace( char* const p, int* curLineNumPtr ) {
return const_cast<char*>( SkipWhiteSpace( const_cast<const char*>(p), curLineNumPtr ) );
}
// Anything in the high order range of UTF-8 is assumed to not be whitespace. This isn't
// correct, but simple, and usually works.
static bool IsWhiteSpace( char p ) {
return !IsUTF8Continuation(p) && isspace( static_cast<unsigned char>(p) );
}
inline static bool IsNameStartChar( unsigned char ch ) {
if ( ch >= 128 ) {
// This is a heuristic guess in attempt to not implement Unicode-aware isalpha()
return true;
}
if ( isalpha( ch ) ) {
return true;
}
return ch == ':' || ch == '_';
}
inline static bool IsNameChar( unsigned char ch ) {
return IsNameStartChar( ch )
|| isdigit( ch )
|| ch == '.'
|| ch == '-';
}
inline static bool IsPrefixHex( const char* p) {
p = SkipWhiteSpace(p, 0);
return p && *p == '0' && ( *(p + 1) == 'x' || *(p + 1) == 'X');
}
inline static bool StringEqual( const char* p, const char* q, int nChar=INT_MAX ) {
if ( p == q ) {
return true;
}
TIXMLASSERT( p );
TIXMLASSERT( q );
TIXMLASSERT( nChar >= 0 );
return strncmp( p, q, static_cast<size_t>(nChar) ) == 0;
}
inline static bool IsUTF8Continuation( const char p ) {
return ( p & 0x80 ) != 0;
}
static const char* ReadBOM( const char* p, bool* hasBOM );
// p is the starting location,
// the UTF-8 value of the entity will be placed in value, and length filled in.
static const char* GetCharacterRef( const char* p, char* value, int* length );
static void ConvertUTF32ToUTF8( unsigned long input, char* output, int* length );
// converts primitive types to strings
static void ToStr( int v, char* buffer, int bufferSize );
static void ToStr( unsigned v, char* buffer, int bufferSize );
static void ToStr( bool v, char* buffer, int bufferSize );
static void ToStr( float v, char* buffer, int bufferSize );
static void ToStr( double v, char* buffer, int bufferSize );
static void ToStr(int64_t v, char* buffer, int bufferSize);
static void ToStr(uint64_t v, char* buffer, int bufferSize);
// converts strings to primitive types
static bool ToInt( const char* str, int* value );
static bool ToUnsigned( const char* str, unsigned* value );
static bool ToBool( const char* str, bool* value );
static bool ToFloat( const char* str, float* value );
static bool ToDouble( const char* str, double* value );
static bool ToInt64(const char* str, int64_t* value);
static bool ToUnsigned64(const char* str, uint64_t* value);
// Changes what is serialized for a boolean value.
// Default to "true" and "false". Shouldn't be changed
// unless you have a special testing or compatibility need.
// Be careful: static, global, & not thread safe.
// Be sure to set static const memory as parameters.
static void SetBoolSerialization(const char* writeTrue, const char* writeFalse);
private:
static const char* writeBoolTrue;
static const char* writeBoolFalse;
};
/** XMLNode is a base class for every object that is in the
XML Document Object Model (DOM), except XMLAttributes.
Nodes have siblings, a parent, and children which can
be navigated. A node is always in a XMLDocument.
The type of a XMLNode can be queried, and it can
be cast to its more defined type.
A XMLDocument allocates memory for all its Nodes.
When the XMLDocument gets deleted, all its Nodes
will also be deleted.
@verbatim
A Document can contain: Element (container or leaf)
Comment (leaf)
Unknown (leaf)
Declaration( leaf )
An Element can contain: Element (container or leaf)
Text (leaf)
Attributes (not on tree)
Comment (leaf)
Unknown (leaf)
@endverbatim
*/
class TINYXML2_LIB XMLNode
{
friend class XMLDocument;
friend class XMLElement;
public:
/// Get the XMLDocument that owns this XMLNode.
const XMLDocument* GetDocument() const {
TIXMLASSERT( _document );
return _document;
}
/// Get the XMLDocument that owns this XMLNode.
XMLDocument* GetDocument() {
TIXMLASSERT( _document );
return _document;
}
/// Safely cast to an Element, or null.
virtual XMLElement* ToElement() {
return 0;
}
/// Safely cast to Text, or null.
virtual XMLText* ToText() {
return 0;
}
/// Safely cast to a Comment, or null.
virtual XMLComment* ToComment() {
return 0;
}
/// Safely cast to a Document, or null.
virtual XMLDocument* ToDocument() {
return 0;
}
/// Safely cast to a Declaration, or null.
virtual XMLDeclaration* ToDeclaration() {
return 0;
}
/// Safely cast to an Unknown, or null.
virtual XMLUnknown* ToUnknown() {
return 0;
}
virtual const XMLElement* ToElement() const {
return 0;
}
virtual const XMLText* ToText() const {
return 0;
}
virtual const XMLComment* ToComment() const {
return 0;
}
virtual const XMLDocument* ToDocument() const {
return 0;
}
virtual const XMLDeclaration* ToDeclaration() const {
return 0;
}
virtual const XMLUnknown* ToUnknown() const {
return 0;
}
// ChildElementCount was originally suggested by msteiger on the sourceforge page for TinyXML and modified by KB1SPH for TinyXML-2.
int ChildElementCount(const char *value) const;
int ChildElementCount() const;
/** The meaning of 'value' changes for the specific type.
@verbatim
Document: empty (NULL is returned, not an empty string)
Element: name of the element
Comment: the comment text
Unknown: the tag contents
Text: the text string
@endverbatim
*/
const char* Value() const;
/** Set the Value of an XML node.
@sa Value()
*/
void SetValue( const char* val, bool staticMem=false );
/// Gets the line number the node is in, if the document was parsed from a file.
int GetLineNum() const { return _parseLineNum; }
/// Get the parent of this node on the DOM.
const XMLNode* Parent() const {
return _parent;
}
XMLNode* Parent() {
return _parent;
}
/// Returns true if this node has no children.
bool NoChildren() const {
return !_firstChild;
}
/// Get the first child node, or null if none exists.
const XMLNode* FirstChild() const {
return _firstChild;
}
XMLNode* FirstChild() {
return _firstChild;
}
/** Get the first child element, or optionally the first child
element with the specified name.
*/
const XMLElement* FirstChildElement( const char* name = 0 ) const;
XMLElement* FirstChildElement( const char* name = 0 ) {
return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->FirstChildElement( name ));
}
/// Get the last child node, or null if none exists.
const XMLNode* LastChild() const {
return _lastChild;
}
XMLNode* LastChild() {
return _lastChild;
}
/** Get the last child element or optionally the last child
element with the specified name.
*/
const XMLElement* LastChildElement( const char* name = 0 ) const;
XMLElement* LastChildElement( const char* name = 0 ) {
return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->LastChildElement(name) );
}
/// Get the previous (left) sibling node of this node.
const XMLNode* PreviousSibling() const {
return _prev;
}
XMLNode* PreviousSibling() {
return _prev;
}
/// Get the previous (left) sibling element of this node, with an optionally supplied name.
const XMLElement* PreviousSiblingElement( const char* name = 0 ) const ;
XMLElement* PreviousSiblingElement( const char* name = 0 ) {
return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->PreviousSiblingElement( name ) );
}
/// Get the next (right) sibling node of this node.
const XMLNode* NextSibling() const {
return _next;
}
XMLNode* NextSibling() {
return _next;
}
/// Get the next (right) sibling element of this node, with an optionally supplied name.
const XMLElement* NextSiblingElement( const char* name = 0 ) const;
XMLElement* NextSiblingElement( const char* name = 0 ) {
return const_cast<XMLElement*>(const_cast<const XMLNode*>(this)->NextSiblingElement( name ) );
}
/**
Add a child node as the last (right) child.
If the child node is already part of the document,
it is moved from its old location to the new location.
Returns the addThis argument or 0 if the node does not
belong to the same document.
*/
XMLNode* InsertEndChild( XMLNode* addThis );
XMLNode* LinkEndChild( XMLNode* addThis ) {
return InsertEndChild( addThis );
}
/**
Add a child node as the first (left) child.
If the child node is already part of the document,
it is moved from its old location to the new location.
Returns the addThis argument or 0 if the node does not
belong to the same document.
*/
XMLNode* InsertFirstChild( XMLNode* addThis );
/**
Add a node after the specified child node.
If the child node is already part of the document,
it is moved from its old location to the new location.
Returns the addThis argument or 0 if the afterThis node
is not a child of this node, or if the node does not
belong to the same document.
*/
XMLNode* InsertAfterChild( XMLNode* afterThis, XMLNode* addThis );
/**
Delete all the children of this node.
*/
void DeleteChildren();
/**
Delete a child of this node.
*/
void DeleteChild( XMLNode* node );
/**
Make a copy of this node, but not its children.
You may pass in a Document pointer that will be
the owner of the new Node. If the 'document' is
null, then the node returned will be allocated
from the current Document. (this->GetDocument())
Note: if called on a XMLDocument, this will return null.
*/
virtual XMLNode* ShallowClone( XMLDocument* document ) const = 0;
/**
Make a copy of this node and all its children.
If the 'target' is null, then the nodes will
be allocated in the current document. If 'target'
is specified, the memory will be allocated is the
specified XMLDocument.
NOTE: This is probably not the correct tool to
copy a document, since XMLDocuments can have multiple
top level XMLNodes. You probably want to use
XMLDocument::DeepCopy()
*/
XMLNode* DeepClone( XMLDocument* target ) const;
/**
Test if 2 nodes are the same, but don't test children.
The 2 nodes do not need to be in the same Document.
Note: if called on a XMLDocument, this will return false.
*/
virtual bool ShallowEqual( const XMLNode* compare ) const = 0;
/** Accept a hierarchical visit of the nodes in the TinyXML-2 DOM. Every node in the
XML tree will be conditionally visited and the host will be called back
via the XMLVisitor interface.
This is essentially a SAX interface for TinyXML-2. (Note however it doesn't re-parse
the XML for the callbacks, so the performance of TinyXML-2 is unchanged by using this
interface versus any other.)
The interface has been based on ideas from:
- http://www.saxproject.org/
- http://c2.com/cgi/wiki?HierarchicalVisitorPattern
Which are both good references for "visiting".
An example of using Accept():
@verbatim
XMLPrinter printer;
tinyxmlDoc.Accept( &printer );
const char* xmlcstr = printer.CStr();
@endverbatim
*/
virtual bool Accept( XMLVisitor* visitor ) const = 0;
/**
Set user data into the XMLNode. TinyXML-2 in
no way processes or interprets user data.
It is initially 0.
*/
void SetUserData(void* userData) { _userData = userData; }
/**
Get user data set into the XMLNode. TinyXML-2 in
no way processes or interprets user data.
It is initially 0.
*/
void* GetUserData() const { return _userData; }
protected:
explicit XMLNode( XMLDocument* );
virtual ~XMLNode();
virtual char* ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr);
XMLDocument* _document;
XMLNode* _parent;
mutable StrPair _value;
int _parseLineNum;
XMLNode* _firstChild;
XMLNode* _lastChild;
XMLNode* _prev;
XMLNode* _next;
void* _userData;
private:
MemPool* _memPool;
void Unlink( XMLNode* child );
static void DeleteNode( XMLNode* node );
void InsertChildPreamble( XMLNode* insertThis ) const;
const XMLElement* ToElementWithName( const char* name ) const;
XMLNode( const XMLNode& ); // not supported
XMLNode& operator=( const XMLNode& ); // not supported
};
/** XML text.
Note that a text node can have child element nodes, for example:
@verbatim
<root>This is <b>bold</b></root>
@endverbatim
A text node can have 2 ways to output the next. "normal" output
and CDATA. It will default to the mode it was parsed from the XML file and
you generally want to leave it alone, but you can change the output mode with
SetCData() and query it with CData().
*/
class TINYXML2_LIB XMLText : public XMLNode
{
friend class XMLDocument;
public:
virtual bool Accept( XMLVisitor* visitor ) const override;
virtual XMLText* ToText() override {
return this;
}
virtual const XMLText* ToText() const override {
return this;
}
/// Declare whether this should be CDATA or standard text.
void SetCData( bool isCData ) {
_isCData = isCData;
}
/// Returns true if this is a CDATA text element.
bool CData() const {
return _isCData;
}
virtual XMLNode* ShallowClone( XMLDocument* document ) const override;
virtual bool ShallowEqual( const XMLNode* compare ) const override;
protected:
explicit XMLText( XMLDocument* doc ) : XMLNode( doc ), _isCData( false ) {}
virtual ~XMLText() {}
char* ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr ) override;
private:
bool _isCData;
XMLText( const XMLText& ); // not supported
XMLText& operator=( const XMLText& ); // not supported
};
/** An XML Comment. */
class TINYXML2_LIB XMLComment : public XMLNode
{
friend class XMLDocument;
public:
virtual XMLComment* ToComment() override {
return this;
}
virtual const XMLComment* ToComment() const override {
return this;
}
virtual bool Accept( XMLVisitor* visitor ) const override;
virtual XMLNode* ShallowClone( XMLDocument* document ) const override;
virtual bool ShallowEqual( const XMLNode* compare ) const override;
protected:
explicit XMLComment( XMLDocument* doc );
virtual ~XMLComment();
char* ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr) override;
private:
XMLComment( const XMLComment& ); // not supported
XMLComment& operator=( const XMLComment& ); // not supported
};
/** In correct XML the declaration is the first entry in the file.
@verbatim
<?xml version="1.0" standalone="yes"?>
@endverbatim
TinyXML-2 will happily read or write files without a declaration,
however.
The text of the declaration isn't interpreted. It is parsed
and written as a string.
*/
class TINYXML2_LIB XMLDeclaration : public XMLNode
{
friend class XMLDocument;
public:
virtual XMLDeclaration* ToDeclaration() override {
return this;
}
virtual const XMLDeclaration* ToDeclaration() const override {
return this;
}
virtual bool Accept( XMLVisitor* visitor ) const override;
virtual XMLNode* ShallowClone( XMLDocument* document ) const override;
virtual bool ShallowEqual( const XMLNode* compare ) const override;
protected:
explicit XMLDeclaration( XMLDocument* doc );
virtual ~XMLDeclaration();
char* ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr ) override;
private:
XMLDeclaration( const XMLDeclaration& ); // not supported
XMLDeclaration& operator=( const XMLDeclaration& ); // not supported
};
/** Any tag that TinyXML-2 doesn't recognize is saved as an
unknown. It is a tag of text, but should not be modified.
It will be written back to the XML, unchanged, when the file
is saved.
DTD tags get thrown into XMLUnknowns.
*/
class TINYXML2_LIB XMLUnknown : public XMLNode
{
friend class XMLDocument;
public:
virtual XMLUnknown* ToUnknown() override {
return this;
}
virtual const XMLUnknown* ToUnknown() const override {
return this;
}
virtual bool Accept( XMLVisitor* visitor ) const override;
virtual XMLNode* ShallowClone( XMLDocument* document ) const override;
virtual bool ShallowEqual( const XMLNode* compare ) const override;
protected:
explicit XMLUnknown( XMLDocument* doc );
virtual ~XMLUnknown();
char* ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr ) override;
private:
XMLUnknown( const XMLUnknown& ); // not supported
XMLUnknown& operator=( const XMLUnknown& ); // not supported
};
/** An attribute is a name-value pair. Elements have an arbitrary
number of attributes, each with a unique name.
@note The attributes are not XMLNodes. You may only query the
Next() attribute in a list.
*/
class TINYXML2_LIB XMLAttribute
{
friend class XMLElement;
public:
/// The name of the attribute.
const char* Name() const;
/// The value of the attribute.
const char* Value() const;
/// Gets the line number the attribute is in, if the document was parsed from a file.
int GetLineNum() const { return _parseLineNum; }
/// The next attribute in the list.
const XMLAttribute* Next() const {
return _next;
}
/** IntValue interprets the attribute as an integer, and returns the value.
If the value isn't an integer, 0 will be returned. There is no error checking;
use QueryIntValue() if you need error checking.
*/
int IntValue() const {
int i = 0;
QueryIntValue(&i);
return i;
}
int64_t Int64Value() const {
int64_t i = 0;
QueryInt64Value(&i);
return i;
}
uint64_t Unsigned64Value() const {
uint64_t i = 0;
QueryUnsigned64Value(&i);
return i;
}
/// Query as an unsigned integer. See IntValue()
unsigned UnsignedValue() const {
unsigned i=0;
QueryUnsignedValue( &i );
return i;
}
/// Query as a boolean. See IntValue()
bool BoolValue() const {
bool b=false;
QueryBoolValue( &b );
return b;
}
/// Query as a double. See IntValue()
double DoubleValue() const {
double d=0;
QueryDoubleValue( &d );
return d;
}
/// Query as a float. See IntValue()
float FloatValue() const {
float f=0;
QueryFloatValue( &f );
return f;
}
/** QueryIntValue interprets the attribute as an integer, and returns the value
in the provided parameter. The function will return XML_SUCCESS on success,
and XML_WRONG_ATTRIBUTE_TYPE if the conversion is not successful.
*/
XMLError QueryIntValue( int* value ) const;
/// See QueryIntValue
XMLError QueryUnsignedValue( unsigned int* value ) const;
/// See QueryIntValue
XMLError QueryInt64Value(int64_t* value) const;
/// See QueryIntValue
XMLError QueryUnsigned64Value(uint64_t* value) const;
/// See QueryIntValue
XMLError QueryBoolValue( bool* value ) const;
/// See QueryIntValue
XMLError QueryDoubleValue( double* value ) const;
/// See QueryIntValue
XMLError QueryFloatValue( float* value ) const;
/// Set the attribute to a string value.
void SetAttribute( const char* value );
/// Set the attribute to value.
void SetAttribute( int value );
/// Set the attribute to value.
void SetAttribute( unsigned value );
/// Set the attribute to value.
void SetAttribute(int64_t value);
/// Set the attribute to value.
void SetAttribute(uint64_t value);
/// Set the attribute to value.
void SetAttribute( bool value );
/// Set the attribute to value.
void SetAttribute( double value );
/// Set the attribute to value.
void SetAttribute( float value );
private:
enum { BUF_SIZE = 200 };
XMLAttribute() : _name(), _value(),_parseLineNum( 0 ), _next( 0 ), _memPool( 0 ) {}
virtual ~XMLAttribute() {}
XMLAttribute( const XMLAttribute& ); // not supported
void operator=( const XMLAttribute& ); // not supported
void SetName( const char* name );
char* ParseDeep( char* p, bool processEntities, int* curLineNumPtr );
mutable StrPair _name;
mutable StrPair _value;
int _parseLineNum;
XMLAttribute* _next;
MemPool* _memPool;
};
/** The element is a container class. It has a value, the element name,
and can contain other elements, text, comments, and unknowns.
Elements also contain an arbitrary number of attributes.
*/
class TINYXML2_LIB XMLElement : public XMLNode
{
friend class XMLDocument;
public:
/// Get the name of an element (which is the Value() of the node.)
const char* Name() const {
return Value();
}
/// Set the name of the element.
void SetName( const char* str, bool staticMem=false ) {
SetValue( str, staticMem );
}
virtual XMLElement* ToElement() override {
return this;
}
virtual const XMLElement* ToElement() const override {
return this;
}
virtual bool Accept( XMLVisitor* visitor ) const override;
/** Given an attribute name, Attribute() returns the value
for the attribute of that name, or null if none
exists. For example:
@verbatim
const char* value = ele->Attribute( "foo" );
@endverbatim
The 'value' parameter is normally null. However, if specified,
the attribute will only be returned if the 'name' and 'value'
match. This allow you to write code:
@verbatim
if ( ele->Attribute( "foo", "bar" ) ) callFooIsBar();
@endverbatim
rather than:
@verbatim
if ( ele->Attribute( "foo" ) ) {
if ( strcmp( ele->Attribute( "foo" ), "bar" ) == 0 ) callFooIsBar();
}
@endverbatim
*/
const char* Attribute( const char* name, const char* value=0 ) const;
/** Given an attribute name, IntAttribute() returns the value
of the attribute interpreted as an integer. The default
value will be returned if the attribute isn't present,
or if there is an error. (For a method with error
checking, see QueryIntAttribute()).
*/
int IntAttribute(const char* name, int defaultValue = 0) const;
/// See IntAttribute()
unsigned UnsignedAttribute(const char* name, unsigned defaultValue = 0) const;
/// See IntAttribute()
int64_t Int64Attribute(const char* name, int64_t defaultValue = 0) const;
/// See IntAttribute()
uint64_t Unsigned64Attribute(const char* name, uint64_t defaultValue = 0) const;
/// See IntAttribute()
bool BoolAttribute(const char* name, bool defaultValue = false) const;
/// See IntAttribute()
double DoubleAttribute(const char* name, double defaultValue = 0) const;
/// See IntAttribute()
float FloatAttribute(const char* name, float defaultValue = 0) const;
/** Given an attribute name, QueryIntAttribute() returns
XML_SUCCESS, XML_WRONG_ATTRIBUTE_TYPE if the conversion
can't be performed, or XML_NO_ATTRIBUTE if the attribute
doesn't exist. If successful, the result of the conversion
will be written to 'value'. If not successful, nothing will
be written to 'value'. This allows you to provide default
value:
@verbatim
int value = 10;
QueryIntAttribute( "foo", &value ); // if "foo" isn't found, value will still be 10
@endverbatim
*/
XMLError QueryIntAttribute( const char* name, int* value ) const {
const XMLAttribute* a = FindAttribute( name );
if ( !a ) {
return XML_NO_ATTRIBUTE;
}
return a->QueryIntValue( value );
}
/// See QueryIntAttribute()
XMLError QueryUnsignedAttribute( const char* name, unsigned int* value ) const {
const XMLAttribute* a = FindAttribute( name );
if ( !a ) {
return XML_NO_ATTRIBUTE;
}
return a->QueryUnsignedValue( value );
}
/// See QueryIntAttribute()
XMLError QueryInt64Attribute(const char* name, int64_t* value) const {
const XMLAttribute* a = FindAttribute(name);
if (!a) {
return XML_NO_ATTRIBUTE;
}
return a->QueryInt64Value(value);
}
/// See QueryIntAttribute()
XMLError QueryUnsigned64Attribute(const char* name, uint64_t* value) const {
const XMLAttribute* a = FindAttribute(name);
if(!a) {
return XML_NO_ATTRIBUTE;
}
return a->QueryUnsigned64Value(value);
}
/// See QueryIntAttribute()
XMLError QueryBoolAttribute( const char* name, bool* value ) const {
const XMLAttribute* a = FindAttribute( name );
if ( !a ) {
return XML_NO_ATTRIBUTE;
}
return a->QueryBoolValue( value );
}
/// See QueryIntAttribute()
XMLError QueryDoubleAttribute( const char* name, double* value ) const {
const XMLAttribute* a = FindAttribute( name );
if ( !a ) {
return XML_NO_ATTRIBUTE;
}
return a->QueryDoubleValue( value );
}
/// See QueryIntAttribute()
XMLError QueryFloatAttribute( const char* name, float* value ) const {
const XMLAttribute* a = FindAttribute( name );
if ( !a ) {
return XML_NO_ATTRIBUTE;
}
return a->QueryFloatValue( value );
}
/// See QueryIntAttribute()
XMLError QueryStringAttribute(const char* name, const char** value) const {
const XMLAttribute* a = FindAttribute(name);
if (!a) {
return XML_NO_ATTRIBUTE;
}
*value = a->Value();
return XML_SUCCESS;
}
/** Given an attribute name, QueryAttribute() returns
XML_SUCCESS, XML_WRONG_ATTRIBUTE_TYPE if the conversion
can't be performed, or XML_NO_ATTRIBUTE if the attribute
doesn't exist. It is overloaded for the primitive types,
and is a generally more convenient replacement of
QueryIntAttribute() and related functions.
If successful, the result of the conversion
will be written to 'value'. If not successful, nothing will
be written to 'value'. This allows you to provide default
value:
@verbatim
int value = 10;
QueryAttribute( "foo", &value ); // if "foo" isn't found, value will still be 10
@endverbatim
*/
XMLError QueryAttribute( const char* name, int* value ) const {
return QueryIntAttribute( name, value );
}
XMLError QueryAttribute( const char* name, unsigned int* value ) const {
return QueryUnsignedAttribute( name, value );
}
XMLError QueryAttribute(const char* name, int64_t* value) const {
return QueryInt64Attribute(name, value);
}
XMLError QueryAttribute(const char* name, uint64_t* value) const {
return QueryUnsigned64Attribute(name, value);
}
XMLError QueryAttribute( const char* name, bool* value ) const {
return QueryBoolAttribute( name, value );
}
XMLError QueryAttribute( const char* name, double* value ) const {
return QueryDoubleAttribute( name, value );
}
XMLError QueryAttribute( const char* name, float* value ) const {
return QueryFloatAttribute( name, value );
}
XMLError QueryAttribute(const char* name, const char** value) const {
return QueryStringAttribute(name, value);
}
/// Sets the named attribute to value.
void SetAttribute( const char* name, const char* value ) {
XMLAttribute* a = FindOrCreateAttribute( name );
a->SetAttribute( value );
}
/// Sets the named attribute to value.
void SetAttribute( const char* name, int value ) {
XMLAttribute* a = FindOrCreateAttribute( name );
a->SetAttribute( value );
}
/// Sets the named attribute to value.
void SetAttribute( const char* name, unsigned value ) {
XMLAttribute* a = FindOrCreateAttribute( name );
a->SetAttribute( value );
}
/// Sets the named attribute to value.
void SetAttribute(const char* name, int64_t value) {
XMLAttribute* a = FindOrCreateAttribute(name);
a->SetAttribute(value);
}
/// Sets the named attribute to value.
void SetAttribute(const char* name, uint64_t value) {
XMLAttribute* a = FindOrCreateAttribute(name);
a->SetAttribute(value);
}
/// Sets the named attribute to value.
void SetAttribute( const char* name, bool value ) {
XMLAttribute* a = FindOrCreateAttribute( name );
a->SetAttribute( value );
}
/// Sets the named attribute to value.
void SetAttribute( const char* name, double value ) {
XMLAttribute* a = FindOrCreateAttribute( name );
a->SetAttribute( value );
}
/// Sets the named attribute to value.
void SetAttribute( const char* name, float value ) {
XMLAttribute* a = FindOrCreateAttribute( name );
a->SetAttribute( value );
}
/**
Delete an attribute.
*/
void DeleteAttribute( const char* name );
/// Return the first attribute in the list.
const XMLAttribute* FirstAttribute() const {
return _rootAttribute;
}
/// Query a specific attribute in the list.
const XMLAttribute* FindAttribute( const char* name ) const;
/** Convenience function for easy access to the text inside an element. Although easy
and concise, GetText() is limited compared to getting the XMLText child
and accessing it directly.
If the first child of 'this' is a XMLText, the GetText()
returns the character string of the Text node, else null is returned.
This is a convenient method for getting the text of simple contained text:
@verbatim
<foo>This is text</foo>
const char* str = fooElement->GetText();
@endverbatim
'str' will be a pointer to "This is text".
Note that this function can be misleading. If the element foo was created from
this XML:
@verbatim
<foo><b>This is text</b></foo>
@endverbatim
then the value of str would be null. The first child node isn't a text node, it is
another element. From this XML:
@verbatim
<foo>This is <b>text</b></foo>
@endverbatim
GetText() will return "This is ".
*/
const char* GetText() const;
/** Convenience function for easy access to the text inside an element. Although easy
and concise, SetText() is limited compared to creating an XMLText child
and mutating it directly.
If the first child of 'this' is a XMLText, SetText() sets its value to
the given string, otherwise it will create a first child that is an XMLText.
This is a convenient method for setting the text of simple contained text:
@verbatim
<foo>This is text</foo>
fooElement->SetText( "Hullaballoo!" );
<foo>Hullaballoo!</foo>
@endverbatim
Note that this function can be misleading. If the element foo was created from
this XML:
@verbatim
<foo><b>This is text</b></foo>
@endverbatim
then it will not change "This is text", but rather prefix it with a text element:
@verbatim
<foo>Hullaballoo!<b>This is text</b></foo>
@endverbatim
For this XML:
@verbatim
<foo />
@endverbatim
SetText() will generate
@verbatim
<foo>Hullaballoo!</foo>
@endverbatim
*/
void SetText( const char* inText );
/// Convenience method for setting text inside an element. See SetText() for important limitations.
void SetText( int value );
/// Convenience method for setting text inside an element. See SetText() for important limitations.
void SetText( unsigned value );
/// Convenience method for setting text inside an element. See SetText() for important limitations.
void SetText(int64_t value);
/// Convenience method for setting text inside an element. See SetText() for important limitations.
void SetText(uint64_t value);
/// Convenience method for setting text inside an element. See SetText() for important limitations.
void SetText( bool value );
/// Convenience method for setting text inside an element. See SetText() for important limitations.
void SetText( double value );
/// Convenience method for setting text inside an element. See SetText() for important limitations.
void SetText( float value );
/**
Convenience method to query the value of a child text node. This is probably best
shown by example. Given you have a document is this form:
@verbatim
<point>
<x>1</x>
<y>1.4</y>
</point>
@endverbatim
The QueryIntText() and similar functions provide a safe and easier way to get to the
"value" of x and y.
@verbatim
int x = 0;
float y = 0; // types of x and y are contrived for example
const XMLElement* xElement = pointElement->FirstChildElement( "x" );
const XMLElement* yElement = pointElement->FirstChildElement( "y" );
xElement->QueryIntText( &x );
yElement->QueryFloatText( &y );
@endverbatim
@returns XML_SUCCESS (0) on success, XML_CAN_NOT_CONVERT_TEXT if the text cannot be converted
to the requested type, and XML_NO_TEXT_NODE if there is no child text to query.
*/
XMLError QueryIntText( int* ival ) const;
/// See QueryIntText()
XMLError QueryUnsignedText( unsigned* uval ) const;
/// See QueryIntText()
XMLError QueryInt64Text(int64_t* uval) const;
/// See QueryIntText()
XMLError QueryUnsigned64Text(uint64_t* uval) const;
/// See QueryIntText()
XMLError QueryBoolText( bool* bval ) const;
/// See QueryIntText()
XMLError QueryDoubleText( double* dval ) const;
/// See QueryIntText()
XMLError QueryFloatText( float* fval ) const;
int IntText(int defaultValue = 0) const;
/// See QueryIntText()
unsigned UnsignedText(unsigned defaultValue = 0) const;
/// See QueryIntText()
int64_t Int64Text(int64_t defaultValue = 0) const;
/// See QueryIntText()
uint64_t Unsigned64Text(uint64_t defaultValue = 0) const;
/// See QueryIntText()
bool BoolText(bool defaultValue = false) const;
/// See QueryIntText()
double DoubleText(double defaultValue = 0) const;
/// See QueryIntText()
float FloatText(float defaultValue = 0) const;
/**
Convenience method to create a new XMLElement and add it as last (right)
child of this node. Returns the created and inserted element.
*/
XMLElement* InsertNewChildElement(const char* name);
/// See InsertNewChildElement()
XMLComment* InsertNewComment(const char* comment);
/// See InsertNewChildElement()
XMLText* InsertNewText(const char* text);
/// See InsertNewChildElement()
XMLDeclaration* InsertNewDeclaration(const char* text);
/// See InsertNewChildElement()
XMLUnknown* InsertNewUnknown(const char* text);
// internal:
enum ElementClosingType {
OPEN, // <foo>
CLOSED, // <foo/>
CLOSING // </foo>
};
ElementClosingType ClosingType() const {
return _closingType;
}
virtual XMLNode* ShallowClone( XMLDocument* document ) const override;
virtual bool ShallowEqual( const XMLNode* compare ) const override;
protected:
char* ParseDeep( char* p, StrPair* parentEndTag, int* curLineNumPtr ) override;
private:
XMLElement( XMLDocument* doc );
virtual ~XMLElement();
XMLElement( const XMLElement& ); // not supported
void operator=( const XMLElement& ); // not supported
XMLAttribute* FindOrCreateAttribute( const char* name );
char* ParseAttributes( char* p, int* curLineNumPtr );
static void DeleteAttribute( XMLAttribute* attribute );
XMLAttribute* CreateAttribute();
enum { BUF_SIZE = 200 };
ElementClosingType _closingType;
// The attribute list is ordered; there is no 'lastAttribute'
// because the list needs to be scanned for dupes before adding
// a new attribute.
XMLAttribute* _rootAttribute;
};
enum Whitespace {
PRESERVE_WHITESPACE,
COLLAPSE_WHITESPACE,
PEDANTIC_WHITESPACE
};
/** A Document binds together all the functionality.
It can be saved, loaded, and printed to the screen.
All Nodes are connected and allocated to a Document.
If the Document is deleted, all its Nodes are also deleted.
*/
class TINYXML2_LIB XMLDocument : public XMLNode
{
friend class XMLElement;
// Gives access to SetError and Push/PopDepth, but over-access for everything else.
// Wishing C++ had "internal" scope.
friend class XMLNode;
friend class XMLText;
friend class XMLComment;
friend class XMLDeclaration;
friend class XMLUnknown;
public:
/// constructor
XMLDocument( bool processEntities = true, Whitespace whitespaceMode = PRESERVE_WHITESPACE );
~XMLDocument();
virtual XMLDocument* ToDocument() override {
TIXMLASSERT( this == _document );
return this;
}
virtual const XMLDocument* ToDocument() const override {
TIXMLASSERT( this == _document );
return this;
}
/**
Parse an XML file from a character string.
Returns XML_SUCCESS (0) on success, or
an errorID.
You may optionally pass in the 'nBytes', which is
the number of bytes which will be parsed. If not
specified, TinyXML-2 will assume 'xml' points to a
null terminated string.
*/
XMLError Parse( const char* xml, size_t nBytes=static_cast<size_t>(-1) );
/**
Load an XML file from disk.
Returns XML_SUCCESS (0) on success, or
an errorID.
*/
XMLError LoadFile( const char* filename );
/**
Load an XML file from disk. You are responsible
for providing and closing the FILE*.
NOTE: The file should be opened as binary ("rb")
not text in order for TinyXML-2 to correctly
do newline normalization.
Returns XML_SUCCESS (0) on success, or
an errorID.
*/
XMLError LoadFile( FILE* );
/**
Save the XML file to disk.
Returns XML_SUCCESS (0) on success, or
an errorID.
*/
XMLError SaveFile( const char* filename, bool compact = false );
/**
Save the XML file to disk. You are responsible
for providing and closing the FILE*.
Returns XML_SUCCESS (0) on success, or
an errorID.
*/
XMLError SaveFile( FILE* fp, bool compact = false );
bool ProcessEntities() const {
return _processEntities;
}
Whitespace WhitespaceMode() const {
return _whitespaceMode;
}
/**
Returns true if this document has a leading Byte Order Mark of UTF8.
*/
bool HasBOM() const {
return _writeBOM;
}
/** Sets whether to write the BOM when writing the file.
*/
void SetBOM( bool useBOM ) {
_writeBOM = useBOM;
}
/** Return the root element of DOM. Equivalent to FirstChildElement().
To get the first node, use FirstChild().
*/
XMLElement* RootElement() {
return FirstChildElement();
}
const XMLElement* RootElement() const {
return FirstChildElement();
}
/** Print the Document. If the Printer is not provided, it will
print to stdout. If you provide Printer, this can print to a file:
@verbatim
XMLPrinter printer( fp );
doc.Print( &printer );
@endverbatim
Or you can use a printer to print to memory:
@verbatim
XMLPrinter printer;
doc.Print( &printer );
// printer.CStr() has a const char* to the XML
@endverbatim
*/
void Print( XMLPrinter* streamer=0 ) const;
virtual bool Accept( XMLVisitor* visitor ) const override;
/**
Create a new Element associated with
this Document. The memory for the Element
is managed by the Document.
*/
XMLElement* NewElement( const char* name );
/**
Create a new Comment associated with
this Document. The memory for the Comment
is managed by the Document.
*/
XMLComment* NewComment( const char* comment );
/**
Create a new Text associated with
this Document. The memory for the Text
is managed by the Document.
*/
XMLText* NewText( const char* text );
/**
Create a new Declaration associated with
this Document. The memory for the object
is managed by the Document.
If the 'text' param is null, the standard
declaration is used.:
@verbatim
<?xml version="1.0" encoding="UTF-8"?>
@endverbatim
*/
XMLDeclaration* NewDeclaration( const char* text=0 );
/**
Create a new Unknown associated with
this Document. The memory for the object
is managed by the Document.
*/
XMLUnknown* NewUnknown( const char* text );
/**
Delete a node associated with this document.
It will be unlinked from the DOM.
*/
void DeleteNode( XMLNode* node );
/// Clears the error flags.
void ClearError();
/// Return true if there was an error parsing the document.
bool Error() const {
return _errorID != XML_SUCCESS;
}
/// Return the errorID.
XMLError ErrorID() const {
return _errorID;
}
const char* ErrorName() const;
static const char* ErrorIDToName(XMLError errorID);
/** Returns a "long form" error description. A hopefully helpful
diagnostic with location, line number, and/or additional info.
*/
const char* ErrorStr() const;
/// A (trivial) utility function that prints the ErrorStr() to stdout.
void PrintError() const;
/// Return the line where the error occurred, or zero if unknown.
int ErrorLineNum() const
{
return _errorLineNum;
}
/// Clear the document, resetting it to the initial state.
void Clear();
/**
Copies this document to a target document.
The target will be completely cleared before the copy.
If you want to copy a sub-tree, see XMLNode::DeepClone().
NOTE: that the 'target' must be non-null.
*/
void DeepCopy(XMLDocument* target) const;
// internal
char* Identify( char* p, XMLNode** node, bool first );
// internal
void MarkInUse(const XMLNode* const);
virtual XMLNode* ShallowClone( XMLDocument* /*document*/ ) const override{
return 0;
}
virtual bool ShallowEqual( const XMLNode* /*compare*/ ) const override{
return false;
}
private:
XMLDocument( const XMLDocument& ); // not supported
void operator=( const XMLDocument& ); // not supported
bool _writeBOM;
bool _processEntities;
XMLError _errorID;
Whitespace _whitespaceMode;
mutable StrPair _errorStr;
int _errorLineNum;
char* _charBuffer;
int _parseCurLineNum;
int _parsingDepth;
// Memory tracking does add some overhead.
// However, the code assumes that you don't
// have a bunch of unlinked nodes around.
// Therefore it takes less memory to track
// in the document vs. a linked list in the XMLNode,
// and the performance is the same.
DynArray<XMLNode*, 10> _unlinked;
MemPoolT< sizeof(XMLElement) > _elementPool;
MemPoolT< sizeof(XMLAttribute) > _attributePool;
MemPoolT< sizeof(XMLText) > _textPool;
MemPoolT< sizeof(XMLComment) > _commentPool;
static const char* _errorNames[XML_ERROR_COUNT];
void Parse();
void SetError( XMLError error, int lineNum, const char* format, ... );
// Something of an obvious security hole, once it was discovered.
// Either an ill-formed XML or an excessively deep one can overflow
// the stack. Track stack depth, and error out if needed.
class DepthTracker {
public:
explicit DepthTracker(XMLDocument * document) {
this->_document = document;
document->PushDepth();
}
~DepthTracker() {
_document->PopDepth();
}
private:
XMLDocument * _document;
};
void PushDepth();
void PopDepth();
template<class NodeType, int PoolElementSize>
NodeType* CreateUnlinkedNode( MemPoolT<PoolElementSize>& pool );
};
template<class NodeType, int PoolElementSize>
inline NodeType* XMLDocument::CreateUnlinkedNode( MemPoolT<PoolElementSize>& pool )
{
TIXMLASSERT( sizeof( NodeType ) == PoolElementSize );
TIXMLASSERT( sizeof( NodeType ) == pool.ItemSize() );
NodeType* returnNode = new (pool.Alloc()) NodeType( this );
TIXMLASSERT( returnNode );
returnNode->_memPool = &pool;
_unlinked.Push(returnNode);
return returnNode;
}
/**
A XMLHandle is a class that wraps a node pointer with null checks; this is
an incredibly useful thing. Note that XMLHandle is not part of the TinyXML-2
DOM structure. It is a separate utility class.
Take an example:
@verbatim
<Document>
<Element attributeA = "valueA">
<Child attributeB = "value1" />
<Child attributeB = "value2" />
</Element>
</Document>
@endverbatim
Assuming you want the value of "attributeB" in the 2nd "Child" element, it's very
easy to write a *lot* of code that looks like:
@verbatim
XMLElement* root = document.FirstChildElement( "Document" );
if ( root )
{
XMLElement* element = root->FirstChildElement( "Element" );
if ( element )
{
XMLElement* child = element->FirstChildElement( "Child" );
if ( child )
{
XMLElement* child2 = child->NextSiblingElement( "Child" );
if ( child2 )
{
// Finally do something useful.
@endverbatim
And that doesn't even cover "else" cases. XMLHandle addresses the verbosity
of such code. A XMLHandle checks for null pointers so it is perfectly safe
and correct to use:
@verbatim
XMLHandle docHandle( &document );
XMLElement* child2 = docHandle.FirstChildElement( "Document" ).FirstChildElement( "Element" ).FirstChildElement().NextSiblingElement();
if ( child2 )
{
// do something useful
@endverbatim
Which is MUCH more concise and useful.
It is also safe to copy handles - internally they are nothing more than node pointers.
@verbatim
XMLHandle handleCopy = handle;
@endverbatim
See also XMLConstHandle, which is the same as XMLHandle, but operates on const objects.
*/
class TINYXML2_LIB XMLHandle
{
public:
/// Create a handle from any node (at any depth of the tree.) This can be a null pointer.
explicit XMLHandle( XMLNode* node ) : _node( node ) {
}
/// Create a handle from a node.
explicit XMLHandle( XMLNode& node ) : _node( &node ) {
}
/// Copy constructor
XMLHandle( const XMLHandle& ref ) : _node( ref._node ) {
}
/// Assignment
XMLHandle& operator=( const XMLHandle& ref ) {
_node = ref._node;
return *this;
}
/// Get the first child of this handle.
XMLHandle FirstChild() {
return XMLHandle( _node ? _node->FirstChild() : 0 );
}
/// Get the first child element of this handle.
XMLHandle FirstChildElement( const char* name = 0 ) {
return XMLHandle( _node ? _node->FirstChildElement( name ) : 0 );
}
/// Get the last child of this handle.
XMLHandle LastChild() {
return XMLHandle( _node ? _node->LastChild() : 0 );
}
/// Get the last child element of this handle.
XMLHandle LastChildElement( const char* name = 0 ) {
return XMLHandle( _node ? _node->LastChildElement( name ) : 0 );
}
/// Get the previous sibling of this handle.
XMLHandle PreviousSibling() {
return XMLHandle( _node ? _node->PreviousSibling() : 0 );
}
/// Get the previous sibling element of this handle.
XMLHandle PreviousSiblingElement( const char* name = 0 ) {
return XMLHandle( _node ? _node->PreviousSiblingElement( name ) : 0 );
}
/// Get the next sibling of this handle.
XMLHandle NextSibling() {
return XMLHandle( _node ? _node->NextSibling() : 0 );
}
/// Get the next sibling element of this handle.
XMLHandle NextSiblingElement( const char* name = 0 ) {
return XMLHandle( _node ? _node->NextSiblingElement( name ) : 0 );
}
/// Safe cast to XMLNode. This can return null.
XMLNode* ToNode() {
return _node;
}
/// Safe cast to XMLElement. This can return null.
XMLElement* ToElement() {
return ( _node ? _node->ToElement() : 0 );
}
/// Safe cast to XMLText. This can return null.
XMLText* ToText() {
return ( _node ? _node->ToText() : 0 );
}
/// Safe cast to XMLUnknown. This can return null.
XMLUnknown* ToUnknown() {
return ( _node ? _node->ToUnknown() : 0 );
}
/// Safe cast to XMLDeclaration. This can return null.
XMLDeclaration* ToDeclaration() {
return ( _node ? _node->ToDeclaration() : 0 );
}
private:
XMLNode* _node;
};
/**
A variant of the XMLHandle class for working with const XMLNodes and Documents. It is the
same in all regards, except for the 'const' qualifiers. See XMLHandle for API.
*/
class TINYXML2_LIB XMLConstHandle
{
public:
explicit XMLConstHandle( const XMLNode* node ) : _node( node ) {
}
explicit XMLConstHandle( const XMLNode& node ) : _node( &node ) {
}
XMLConstHandle( const XMLConstHandle& ref ) : _node( ref._node ) {
}
XMLConstHandle& operator=( const XMLConstHandle& ref ) {
_node = ref._node;
return *this;
}
const XMLConstHandle FirstChild() const {
return XMLConstHandle( _node ? _node->FirstChild() : 0 );
}
const XMLConstHandle FirstChildElement( const char* name = 0 ) const {
return XMLConstHandle( _node ? _node->FirstChildElement( name ) : 0 );
}
const XMLConstHandle LastChild() const {
return XMLConstHandle( _node ? _node->LastChild() : 0 );
}
const XMLConstHandle LastChildElement( const char* name = 0 ) const {
return XMLConstHandle( _node ? _node->LastChildElement( name ) : 0 );
}
const XMLConstHandle PreviousSibling() const {
return XMLConstHandle( _node ? _node->PreviousSibling() : 0 );
}
const XMLConstHandle PreviousSiblingElement( const char* name = 0 ) const {
return XMLConstHandle( _node ? _node->PreviousSiblingElement( name ) : 0 );
}
const XMLConstHandle NextSibling() const {
return XMLConstHandle( _node ? _node->NextSibling() : 0 );
}
const XMLConstHandle NextSiblingElement( const char* name = 0 ) const {
return XMLConstHandle( _node ? _node->NextSiblingElement( name ) : 0 );
}
const XMLNode* ToNode() const {
return _node;
}
const XMLElement* ToElement() const {
return ( _node ? _node->ToElement() : 0 );
}
const XMLText* ToText() const {
return ( _node ? _node->ToText() : 0 );
}
const XMLUnknown* ToUnknown() const {
return ( _node ? _node->ToUnknown() : 0 );
}
const XMLDeclaration* ToDeclaration() const {
return ( _node ? _node->ToDeclaration() : 0 );
}
private:
const XMLNode* _node;
};
/**
Printing functionality. The XMLPrinter gives you more
options than the XMLDocument::Print() method.
It can:
-# Print to memory.
-# Print to a file you provide.
-# Print XML without a XMLDocument.
Print to Memory
@verbatim
XMLPrinter printer;
doc.Print( &printer );
SomeFunction( printer.CStr() );
@endverbatim
Print to a File
You provide the file pointer.
@verbatim
XMLPrinter printer( fp );
doc.Print( &printer );
@endverbatim
Print without a XMLDocument
When loading, an XML parser is very useful. However, sometimes
when saving, it just gets in the way. The code is often set up
for streaming, and constructing the DOM is just overhead.
The Printer supports the streaming case. The following code
prints out a trivially simple XML file without ever creating
an XML document.
@verbatim
XMLPrinter printer( fp );
printer.OpenElement( "foo" );
printer.PushAttribute( "foo", "bar" );
printer.CloseElement();
@endverbatim
*/
class TINYXML2_LIB XMLPrinter : public XMLVisitor
{
public:
/** Construct the printer. If the FILE* is specified,
this will print to the FILE. Else it will print
to memory, and the result is available in CStr().
If 'compact' is set to true, then output is created
with only required whitespace and newlines.
*/
XMLPrinter( FILE* file=0, bool compact = false, int depth = 0 );
virtual ~XMLPrinter() {}
/** If streaming, write the BOM and declaration. */
void PushHeader( bool writeBOM, bool writeDeclaration );
/** If streaming, start writing an element.
The element must be closed with CloseElement()
*/
void OpenElement( const char* name, bool compactMode=false );
/// If streaming, add an attribute to an open element.
void PushAttribute( const char* name, const char* value );
void PushAttribute( const char* name, int value );
void PushAttribute( const char* name, unsigned value );
void PushAttribute( const char* name, int64_t value );
void PushAttribute( const char* name, uint64_t value );
void PushAttribute( const char* name, bool value );
void PushAttribute( const char* name, double value );
/// If streaming, close the Element.
virtual void CloseElement( bool compactMode=false );
/// Add a text node.
void PushText( const char* text, bool cdata=false );
/// Add a text node from an integer.
void PushText( int value );
/// Add a text node from an unsigned.
void PushText( unsigned value );
/// Add a text node from a signed 64bit integer.
void PushText( int64_t value );
/// Add a text node from an unsigned 64bit integer.
void PushText( uint64_t value );
/// Add a text node from a bool.
void PushText( bool value );
/// Add a text node from a float.
void PushText( float value );
/// Add a text node from a double.
void PushText( double value );
/// Add a comment
void PushComment( const char* comment );
void PushDeclaration( const char* value );
void PushUnknown( const char* value );
virtual bool VisitEnter( const XMLDocument& /*doc*/ ) override;
virtual bool VisitExit( const XMLDocument& /*doc*/ ) override {
return true;
}
virtual bool VisitEnter( const XMLElement& element, const XMLAttribute* attribute ) override;
virtual bool VisitExit( const XMLElement& element ) override;
virtual bool Visit( const XMLText& text ) override;
virtual bool Visit( const XMLComment& comment ) override;
virtual bool Visit( const XMLDeclaration& declaration ) override;
virtual bool Visit( const XMLUnknown& unknown ) override;
/**
If in print to memory mode, return a pointer to
the XML file in memory.
*/
const char* CStr() const {
return _buffer.Mem();
}
/**
If in print to memory mode, return the size
of the XML file in memory. (Note the size returned
includes the terminating null.)
*/
int CStrSize() const {
return _buffer.Size();
}
/**
If in print to memory mode, reset the buffer to the
beginning.
*/
void ClearBuffer( bool resetToFirstElement = true ) {
_buffer.Clear();
_buffer.Push(0);
_firstElement = resetToFirstElement;
}
protected:
virtual bool CompactMode( const XMLElement& ) { return _compactMode; }
/** Prints out the space before an element. You may override to change
the space and tabs used. A PrintSpace() override should call Print().
*/
virtual void PrintSpace( int depth );
virtual void Print( const char* format, ... );
virtual void Write( const char* data, size_t size );
virtual void Putc( char ch );
inline void Write(const char* data) { Write(data, strlen(data)); }
void SealElementIfJustOpened();
bool _elementJustOpened;
DynArray< const char*, 10 > _stack;
private:
/**
Prepares to write a new node. This includes sealing an element that was
just opened, and writing any whitespace necessary if not in compact mode.
*/
void PrepareForNewNode( bool compactMode );
void PrintString( const char*, bool restrictedEntitySet ); // prints out, after detecting entities.
bool _firstElement;
FILE* _fp;
int _depth;
int _textDepth;
bool _processEntities;
bool _compactMode;
enum {
ENTITY_RANGE = 64,
BUF_SIZE = 200
};
bool _entityFlag[ENTITY_RANGE];
bool _restrictedEntityFlag[ENTITY_RANGE];
DynArray< char, 20 > _buffer;
// Prohibit cloning, intentionally not implemented
XMLPrinter( const XMLPrinter& );
XMLPrinter& operator=( const XMLPrinter& );
};
} // tinyxml2
#if defined(_MSC_VER)
# pragma warning(pop)
#endif
#endif // TINYXML2_INCLUDED
| h |
tinyxml2 | data/projects/tinyxml2/xmltest.cpp | #if defined( _MSC_VER )
#if !defined( _CRT_SECURE_NO_WARNINGS )
#define _CRT_SECURE_NO_WARNINGS // This test file is not intended to be secure.
#endif
#endif
#include "tinyxml2.h"
#include <cerrno>
#include <cstdlib>
#include <cstring>
#include <ctime>
#if defined( _MSC_VER ) || defined (WIN32)
#include <crtdbg.h>
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
_CrtMemState startMemState;
_CrtMemState endMemState;
#else
#include <sys/stat.h>
#include <sys/types.h>
#endif
using namespace tinyxml2;
using namespace std;
int gPass = 0;
int gFail = 0;
bool XMLTest (const char* testString, const char* expected, const char* found, bool echo=true, bool extraNL=false )
{
bool pass;
if ( !expected && !found )
pass = true;
else if ( !expected || !found )
pass = false;
else
pass = !strcmp( expected, found );
if ( pass )
printf ("[pass]");
else
printf ("[fail]");
if ( !echo ) {
printf (" %s\n", testString);
}
else {
if ( extraNL ) {
printf( " %s\n", testString );
printf( "%s\n", expected );
printf( "%s\n", found );
}
else {
printf (" %s [%s][%s]\n", testString, expected, found);
}
}
if ( pass )
++gPass;
else
++gFail;
return pass;
}
bool XMLTest(const char* testString, XMLError expected, XMLError found, bool echo = true, bool extraNL = false)
{
return XMLTest(testString, XMLDocument::ErrorIDToName(expected), XMLDocument::ErrorIDToName(found), echo, extraNL);
}
bool XMLTest(const char* testString, bool expected, bool found, bool echo = true, bool extraNL = false)
{
return XMLTest(testString, expected ? "true" : "false", found ? "true" : "false", echo, extraNL);
}
template< class T > bool XMLTest( const char* testString, T expected, T found, bool echo=true )
{
bool pass = ( expected == found );
if ( pass )
printf ("[pass]");
else
printf ("[fail]");
if ( !echo )
printf (" %s\n", testString);
else {
char expectedAsString[64];
XMLUtil::ToStr(expected, expectedAsString, sizeof(expectedAsString));
char foundAsString[64];
XMLUtil::ToStr(found, foundAsString, sizeof(foundAsString));
printf (" %s [%s][%s]\n", testString, expectedAsString, foundAsString );
}
if ( pass )
++gPass;
else
++gFail;
return pass;
}
void NullLineEndings( char* p )
{
while( p && *p ) {
if ( *p == '\n' || *p == '\r' ) {
*p = 0;
return;
}
++p;
}
}
int example_1()
{
XMLDocument doc;
doc.LoadFile( "resources/dream.xml" );
return doc.ErrorID();
}
/** @page Example_1 Load an XML File
* @dontinclude ./xmltest.cpp
* Basic XML file loading.
* The basic syntax to load an XML file from
* disk and check for an error. (ErrorID()
* will return 0 for no error.)
* @skip example_1()
* @until }
*/
int example_2()
{
static const char* xml = "<element/>";
XMLDocument doc;
doc.Parse( xml );
return doc.ErrorID();
}
/** @page Example_2 Parse an XML from char buffer
* @dontinclude ./xmltest.cpp
* Basic XML string parsing.
* The basic syntax to parse an XML for
* a char* and check for an error. (ErrorID()
* will return 0 for no error.)
* @skip example_2()
* @until }
*/
int example_3()
{
static const char* xml =
"<?xml version=\"1.0\"?>"
"<!DOCTYPE PLAY SYSTEM \"play.dtd\">"
"<PLAY>"
"<TITLE>A Midsummer Night's Dream</TITLE>"
"</PLAY>";
XMLDocument doc;
doc.Parse( xml );
XMLElement* titleElement = doc.FirstChildElement( "PLAY" )->FirstChildElement( "TITLE" );
const char* title = titleElement->GetText();
printf( "Name of play (1): %s\n", title );
XMLText* textNode = titleElement->FirstChild()->ToText();
title = textNode->Value();
printf( "Name of play (2): %s\n", title );
return doc.ErrorID();
}
/** @page Example_3 Get information out of XML
@dontinclude ./xmltest.cpp
In this example, we navigate a simple XML
file, and read some interesting text. Note
that this example doesn't use error
checking; working code should check for null
pointers when walking an XML tree, or use
XMLHandle.
(The XML is an excerpt from "dream.xml").
@skip example_3()
@until </PLAY>";
The structure of the XML file is:
<ul>
<li>(declaration)</li>
<li>(dtd stuff)</li>
<li>Element "PLAY"</li>
<ul>
<li>Element "TITLE"</li>
<ul>
<li>Text "A Midsummer Night's Dream"</li>
</ul>
</ul>
</ul>
For this example, we want to print out the
title of the play. The text of the title (what
we want) is child of the "TITLE" element which
is a child of the "PLAY" element.
We want to skip the declaration and dtd, so the
method FirstChildElement() is a good choice. The
FirstChildElement() of the Document is the "PLAY"
Element, the FirstChildElement() of the "PLAY" Element
is the "TITLE" Element.
@until ( "TITLE" );
We can then use the convenience function GetText()
to get the title of the play.
@until title );
Text is just another Node in the XML DOM. And in
fact you should be a little cautious with it, as
text nodes can contain elements.
@verbatim
Consider: A Midsummer Night's <b>Dream</b>
@endverbatim
It is more correct to actually query the Text Node
if in doubt:
@until title );
Noting that here we use FirstChild() since we are
looking for XMLText, not an element, and ToText()
is a cast from a Node to a XMLText.
*/
bool example_4()
{
static const char* xml =
"<information>"
" <attributeApproach v='2' />"
" <textApproach>"
" <v>2</v>"
" </textApproach>"
"</information>";
XMLDocument doc;
doc.Parse( xml );
int v0 = 0;
int v1 = 0;
XMLElement* attributeApproachElement = doc.FirstChildElement()->FirstChildElement( "attributeApproach" );
attributeApproachElement->QueryIntAttribute( "v", &v0 );
XMLElement* textApproachElement = doc.FirstChildElement()->FirstChildElement( "textApproach" );
textApproachElement->FirstChildElement( "v" )->QueryIntText( &v1 );
printf( "Both values are the same: %d and %d\n", v0, v1 );
return !doc.Error() && ( v0 == v1 );
}
/** @page Example_4 Read attributes and text information.
@dontinclude ./xmltest.cpp
There are fundamentally 2 ways of writing a key-value
pair into an XML file. (Something that's always annoyed
me about XML.) Either by using attributes, or by writing
the key name into an element and the value into
the text node wrapped by the element. Both approaches
are illustrated in this example, which shows two ways
to encode the value "2" into the key "v":
@skip example_4()
@until "</information>";
TinyXML-2 has accessors for both approaches.
When using an attribute, you navigate to the XMLElement
with that attribute and use the QueryIntAttribute()
group of methods. (Also QueryFloatAttribute(), etc.)
@skip XMLElement* attributeApproachElement
@until &v0 );
When using the text approach, you need to navigate
down one more step to the XMLElement that contains
the text. Note the extra FirstChildElement( "v" )
in the code below. The value of the text can then
be safely queried with the QueryIntText() group
of methods. (Also QueryFloatText(), etc.)
@skip XMLElement* textApproachElement
@until &v1 );
*/
int main( int argc, const char ** argv )
{
#if defined( _MSC_VER ) && defined( TINYXML2_DEBUG )
_CrtMemCheckpoint( &startMemState );
// Enable MS Visual C++ debug heap memory leaks dump on exit
_CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG) | _CRTDBG_LEAK_CHECK_DF);
{
int leaksOnStart = _CrtDumpMemoryLeaks();
XMLTest( "No leaks on start?", FALSE, leaksOnStart );
}
#endif
{
TIXMLASSERT( true );
}
if ( argc > 1 ) {
XMLDocument* doc = new XMLDocument();
clock_t startTime = clock();
doc->LoadFile( argv[1] );
clock_t loadTime = clock();
int errorID = doc->ErrorID();
delete doc; doc = 0;
clock_t deleteTime = clock();
printf( "Test file '%s' loaded. ErrorID=%d\n", argv[1], errorID );
if ( !errorID ) {
printf( "Load time=%u\n", (unsigned)(loadTime - startTime) );
printf( "Delete time=%u\n", (unsigned)(deleteTime - loadTime) );
printf( "Total time=%u\n", (unsigned)(deleteTime - startTime) );
}
exit(0);
}
FILE* fp = fopen( "resources/dream.xml", "r" );
if ( !fp ) {
printf( "Error opening test file 'dream.xml'.\n"
"Is your working directory the same as where \n"
"the xmltest.cpp and dream.xml file are?\n\n"
#if defined( _MSC_VER )
"In windows Visual Studio you may need to set\n"
"Properties->Debugging->Working Directory to '..'\n"
#endif
);
exit( 1 );
}
fclose( fp );
XMLTest( "Example_1", 0, example_1() );
XMLTest( "Example_2", 0, example_2() );
XMLTest( "Example_3", 0, example_3() );
XMLTest( "Example_4", true, example_4() );
/* ------ Example 2: Lookup information. ---- */
{
static const char* test[] = { "<element />",
"<element></element>",
"<element><subelement/></element>",
"<element><subelement></subelement></element>",
"<element><subelement><subsub/></subelement></element>",
"<!--comment beside elements--><element><subelement></subelement></element>",
"<!--comment beside elements, this time with spaces--> \n <element> <subelement> \n </subelement> </element>",
"<element attrib1='foo' attrib2=\"bar\" ></element>",
"<element attrib1='foo' attrib2=\"bar\" ><subelement attrib3='yeehaa' /></element>",
"<element>Text inside element.</element>",
"<element><b></b></element>",
"<element>Text inside and <b>bolded</b> in the element.</element>",
"<outer><element>Text inside and <b>bolded</b> in the element.</element></outer>",
"<element>This & That.</element>",
"<element attrib='This<That' />",
0
};
for( int i=0; test[i]; ++i ) {
XMLDocument doc;
doc.Parse( test[i] );
XMLTest( "Element test", false, doc.Error() );
doc.Print();
printf( "----------------------------------------------\n" );
}
}
#if 1
{
static const char* test = "<!--hello world\n"
" line 2\r"
" line 3\r\n"
" line 4\n\r"
" line 5\r-->";
XMLDocument doc;
doc.Parse( test );
XMLTest( "Hello world declaration", false, doc.Error() );
doc.Print();
}
{
// This test is pre-test for the next one
// (where Element1 is inserted "after itself".
// This code didn't use to crash.
XMLDocument doc;
XMLElement* element1 = doc.NewElement("Element1");
XMLElement* element2 = doc.NewElement("Element2");
doc.InsertEndChild(element1);
doc.InsertEndChild(element2);
doc.InsertAfterChild(element2, element2);
doc.InsertAfterChild(element2, element2);
}
{
XMLDocument doc;
XMLElement* element1 = doc.NewElement("Element1");
XMLElement* element2 = doc.NewElement("Element2");
doc.InsertEndChild(element1);
doc.InsertEndChild(element2);
// This insertion "after itself"
// used to cause invalid memory access and crash
doc.InsertAfterChild(element1, element1);
doc.InsertAfterChild(element1, element1);
doc.InsertAfterChild(element2, element2);
doc.InsertAfterChild(element2, element2);
}
{
static const char* test = "<element>Text before.</element>";
XMLDocument doc;
doc.Parse( test );
XMLTest( "Element text before", false, doc.Error() );
XMLElement* root = doc.FirstChildElement();
XMLElement* newElement = doc.NewElement( "Subelement" );
root->InsertEndChild( newElement );
doc.Print();
}
{
XMLDocument* doc = new XMLDocument();
static const char* test = "<element><sub/></element>";
doc->Parse( test );
XMLTest( "Element with sub element", false, doc->Error() );
delete doc;
}
{
// Test: Programmatic DOM nodes insertion return values
XMLDocument doc;
XMLNode* first = doc.NewElement( "firstElement" );
XMLTest( "New element", true, first != 0 );
XMLNode* firstAfterInsertion = doc.InsertFirstChild( first );
XMLTest( "New element inserted first", true, firstAfterInsertion == first );
XMLNode* last = doc.NewElement( "lastElement" );
XMLTest( "New element", true, last != 0 );
XMLNode* lastAfterInsertion = doc.InsertEndChild( last );
XMLTest( "New element inserted last", true, lastAfterInsertion == last );
XMLNode* middle = doc.NewElement( "middleElement" );
XMLTest( "New element", true, middle != 0 );
XMLNode* middleAfterInsertion = doc.InsertAfterChild( first, middle );
XMLTest( "New element inserted middle", true, middleAfterInsertion == middle );
}
{
// Test: Programmatic DOM
// Build:
// <element>
// <!--comment-->
// <sub attrib="0" />
// <sub attrib="1" />
// <sub attrib="2" >& Text!</sub>
// <element>
XMLDocument* doc = new XMLDocument();
XMLNode* element = doc->InsertEndChild( doc->NewElement( "element" ) );
XMLElement* sub[3] = { doc->NewElement( "sub" ), doc->NewElement( "sub" ), doc->NewElement( "sub" ) };
for( int i=0; i<3; ++i ) {
sub[i]->SetAttribute( "attrib", i );
}
element->InsertEndChild( sub[2] );
const int dummyInitialValue = 1000;
int dummyValue = dummyInitialValue;
XMLNode* comment = element->InsertFirstChild( doc->NewComment( "comment" ) );
comment->SetUserData(&dummyValue);
element->InsertAfterChild( comment, sub[0] );
element->InsertAfterChild( sub[0], sub[1] );
sub[2]->InsertFirstChild( doc->NewText( "& Text!" ));
doc->Print();
XMLTest( "Programmatic DOM", "comment", doc->FirstChildElement( "element" )->FirstChild()->Value() );
XMLTest( "Programmatic DOM", "0", doc->FirstChildElement( "element" )->FirstChildElement()->Attribute( "attrib" ) );
XMLTest( "Programmatic DOM", 2, doc->FirstChildElement()->LastChildElement( "sub" )->IntAttribute( "attrib" ) );
XMLTest( "Programmatic DOM", "& Text!",
doc->FirstChildElement()->LastChildElement( "sub" )->FirstChild()->ToText()->Value() );
XMLTest("User data - pointer", true, &dummyValue == comment->GetUserData(), false);
XMLTest("User data - value behind pointer", dummyInitialValue, dummyValue, false);
// And now deletion:
element->DeleteChild( sub[2] );
doc->DeleteNode( comment );
element->FirstChildElement()->SetAttribute( "attrib", true );
element->LastChildElement()->DeleteAttribute( "attrib" );
XMLTest( "Programmatic DOM", true, doc->FirstChildElement()->FirstChildElement()->BoolAttribute( "attrib" ) );
const int defaultIntValue = 10;
const int replacementIntValue = 20;
int value1 = defaultIntValue;
int value2 = doc->FirstChildElement()->LastChildElement()->IntAttribute( "attrib", replacementIntValue );
XMLError result = doc->FirstChildElement()->LastChildElement()->QueryIntAttribute( "attrib", &value1 );
XMLTest( "Programmatic DOM", XML_NO_ATTRIBUTE, result );
XMLTest( "Programmatic DOM", defaultIntValue, value1 );
XMLTest( "Programmatic DOM", replacementIntValue, value2 );
doc->Print();
{
XMLPrinter streamer;
doc->Print( &streamer );
printf( "%s", streamer.CStr() );
}
{
XMLPrinter streamer( 0, true );
doc->Print( &streamer );
XMLTest( "Compact mode", "<element><sub attrib=\"true\"/><sub/></element>", streamer.CStr(), false );
}
doc->SaveFile( "./resources/out/pretty.xml" );
XMLTest( "Save pretty.xml", false, doc->Error() );
doc->SaveFile( "./resources/out/compact.xml", true );
XMLTest( "Save compact.xml", false, doc->Error() );
delete doc;
}
{
// Test: Dream
// XML1 : 1,187,569 bytes in 31,209 allocations
// XML2 : 469,073 bytes in 323 allocations
//int newStart = gNew;
XMLDocument doc;
doc.LoadFile( "resources/dream.xml" );
XMLTest( "Load dream.xml", false, doc.Error() );
doc.SaveFile( "resources/out/dreamout.xml" );
XMLTest( "Save dreamout.xml", false, doc.Error() );
doc.PrintError();
XMLTest( "Dream", "xml version=\"1.0\"",
doc.FirstChild()->ToDeclaration()->Value() );
XMLTest( "Dream", true, doc.FirstChild()->NextSibling()->ToUnknown() != 0 );
XMLTest( "Dream", "DOCTYPE PLAY SYSTEM \"play.dtd\"",
doc.FirstChild()->NextSibling()->ToUnknown()->Value() );
XMLTest( "Dream", "And Robin shall restore amends.",
doc.LastChild()->LastChild()->LastChild()->LastChild()->LastChildElement()->GetText() );
XMLTest( "Dream", "And Robin shall restore amends.",
doc.LastChild()->LastChild()->LastChild()->LastChild()->LastChildElement()->GetText() );
XMLDocument doc2;
doc2.LoadFile( "resources/out/dreamout.xml" );
XMLTest( "Load dreamout.xml", false, doc2.Error() );
XMLTest( "Dream-out", "xml version=\"1.0\"",
doc2.FirstChild()->ToDeclaration()->Value() );
XMLTest( "Dream-out", true, doc2.FirstChild()->NextSibling()->ToUnknown() != 0 );
XMLTest( "Dream-out", "DOCTYPE PLAY SYSTEM \"play.dtd\"",
doc2.FirstChild()->NextSibling()->ToUnknown()->Value() );
XMLTest( "Dream-out", "And Robin shall restore amends.",
doc2.LastChild()->LastChild()->LastChild()->LastChild()->LastChildElement()->GetText() );
//gNewTotal = gNew - newStart;
}
{
const char* error = "<?xml version=\"1.0\" standalone=\"no\" ?>\n"
"<passages count=\"006\" formatversion=\"20020620\">\n"
" <wrong error>\n"
"</passages>";
XMLDocument doc;
doc.Parse( error );
XMLTest( "Bad XML", XML_ERROR_PARSING_ATTRIBUTE, doc.ErrorID() );
const char* errorStr = doc.ErrorStr();
XMLTest("Formatted error string",
"Error=XML_ERROR_PARSING_ATTRIBUTE ErrorID=7 (0x7) Line number=3: XMLElement name=wrong",
errorStr);
}
{
const char* str = "<doc attr0='1' attr1='2.0' attr2='foo' />";
XMLDocument doc;
doc.Parse( str );
XMLTest( "Top level attributes", false, doc.Error() );
XMLElement* ele = doc.FirstChildElement();
int iVal;
XMLError result;
double dVal;
result = ele->QueryDoubleAttribute( "attr0", &dVal );
XMLTest( "Query attribute: int as double", XML_SUCCESS, result);
XMLTest( "Query attribute: int as double", 1, (int)dVal );
XMLTest( "Query attribute: int as double", 1, (int)ele->DoubleAttribute("attr0"));
result = ele->QueryDoubleAttribute( "attr1", &dVal );
XMLTest( "Query attribute: double as double", XML_SUCCESS, result);
XMLTest( "Query attribute: double as double", 2.0, dVal );
XMLTest( "Query attribute: double as double", 2.0, ele->DoubleAttribute("attr1") );
result = ele->QueryIntAttribute( "attr1", &iVal );
XMLTest( "Query attribute: double as int", XML_SUCCESS, result);
XMLTest( "Query attribute: double as int", 2, iVal );
result = ele->QueryIntAttribute( "attr2", &iVal );
XMLTest( "Query attribute: not a number", XML_WRONG_ATTRIBUTE_TYPE, result );
XMLTest( "Query attribute: not a number", 4.0, ele->DoubleAttribute("attr2", 4.0) );
result = ele->QueryIntAttribute( "bar", &iVal );
XMLTest( "Query attribute: does not exist", XML_NO_ATTRIBUTE, result );
XMLTest( "Query attribute: does not exist", true, ele->BoolAttribute("bar", true) );
}
{
const char* str = "<doc/>";
XMLDocument doc;
doc.Parse( str );
XMLTest( "Empty top element", false, doc.Error() );
XMLElement* ele = doc.FirstChildElement();
int iVal, iVal2;
double dVal, dVal2;
ele->SetAttribute( "str", "strValue" );
ele->SetAttribute( "int", 1 );
ele->SetAttribute( "double", -1.0 );
const char* answer = 0;
ele->QueryAttribute("str", &answer);
XMLTest("Query char attribute", "strValue", answer);
const char* cStr = ele->Attribute( "str" );
{
XMLError queryResult = ele->QueryIntAttribute( "int", &iVal );
XMLTest( "Query int attribute", XML_SUCCESS, queryResult);
}
{
XMLError queryResult = ele->QueryDoubleAttribute( "double", &dVal );
XMLTest( "Query double attribute", XML_SUCCESS, queryResult);
}
{
XMLError queryResult = ele->QueryAttribute( "int", &iVal2 );
XMLTest( "Query int attribute generic", (int)XML_SUCCESS, queryResult);
}
{
XMLError queryResult = ele->QueryAttribute( "double", &dVal2 );
XMLTest( "Query double attribute generic", (int)XML_SUCCESS, queryResult);
}
XMLTest( "Attribute match test", "strValue", ele->Attribute( "str", "strValue" ) );
XMLTest( "Attribute round trip. c-string.", "strValue", cStr );
XMLTest( "Attribute round trip. int.", 1, iVal );
XMLTest( "Attribute round trip. double.", -1, (int)dVal );
XMLTest( "Alternate query", true, iVal == iVal2 );
XMLTest( "Alternate query", true, dVal == dVal2 );
XMLTest( "Alternate query", true, iVal == ele->IntAttribute("int") );
XMLTest( "Alternate query", true, dVal == ele->DoubleAttribute("double") );
}
{
XMLDocument doc;
doc.LoadFile( "resources/utf8test.xml" );
XMLTest( "Load utf8test.xml", false, doc.Error() );
// Get the attribute "value" from the "Russian" element and check it.
XMLElement* element = doc.FirstChildElement( "document" )->FirstChildElement( "Russian" );
const unsigned char correctValue[] = { 0xd1U, 0x86U, 0xd0U, 0xb5U, 0xd0U, 0xbdU, 0xd0U, 0xbdU,
0xd0U, 0xbeU, 0xd1U, 0x81U, 0xd1U, 0x82U, 0xd1U, 0x8cU, 0 };
XMLTest( "UTF-8: Russian value.", (const char*)correctValue, element->Attribute( "value" ) );
const unsigned char russianElementName[] = { 0xd0U, 0xa0U, 0xd1U, 0x83U,
0xd1U, 0x81U, 0xd1U, 0x81U,
0xd0U, 0xbaU, 0xd0U, 0xb8U,
0xd0U, 0xb9U, 0 };
const char russianText[] = "<\xD0\xB8\xD0\xBC\xD0\xB5\xD0\xB5\xD1\x82>";
XMLText* text = doc.FirstChildElement( "document" )->FirstChildElement( (const char*) russianElementName )->FirstChild()->ToText();
XMLTest( "UTF-8: Browsing russian element name.",
russianText,
text->Value() );
// Now try for a round trip.
doc.SaveFile( "resources/out/utf8testout.xml" );
XMLTest( "UTF-8: Save testout.xml", false, doc.Error() );
// Check the round trip.
bool roundTripOkay = false;
FILE* saved = fopen( "resources/out/utf8testout.xml", "r" );
XMLTest( "UTF-8: Open utf8testout.xml", true, saved != 0 );
FILE* verify = fopen( "resources/utf8testverify.xml", "r" );
XMLTest( "UTF-8: Open utf8testverify.xml", true, verify != 0 );
if ( saved && verify )
{
roundTripOkay = true;
char verifyBuf[256];
while ( fgets( verifyBuf, 256, verify ) )
{
char savedBuf[256];
fgets( savedBuf, 256, saved );
NullLineEndings( verifyBuf );
NullLineEndings( savedBuf );
if ( strcmp( verifyBuf, savedBuf ) )
{
printf( "verify:%s<\n", verifyBuf );
printf( "saved :%s<\n", savedBuf );
roundTripOkay = false;
break;
}
}
}
if ( saved )
fclose( saved );
if ( verify )
fclose( verify );
XMLTest( "UTF-8: Verified multi-language round trip.", true, roundTripOkay );
}
// --------GetText()-----------
{
const char* str = "<foo>This is text</foo>";
XMLDocument doc;
doc.Parse( str );
XMLTest( "Double whitespace", false, doc.Error() );
const XMLElement* element = doc.RootElement();
XMLTest( "GetText() normal use.", "This is text", element->GetText() );
str = "<foo><b>This is text</b></foo>";
doc.Parse( str );
XMLTest( "Bold text simulation", false, doc.Error() );
element = doc.RootElement();
XMLTest( "GetText() contained element.", element->GetText() == 0, true );
}
// --------SetText()-----------
{
const char* str = "<foo></foo>";
XMLDocument doc;
doc.Parse( str );
XMLTest( "Empty closed element", false, doc.Error() );
XMLElement* element = doc.RootElement();
element->SetText("darkness.");
XMLTest( "SetText() normal use (open/close).", "darkness.", element->GetText() );
element->SetText("blue flame.");
XMLTest( "SetText() replace.", "blue flame.", element->GetText() );
str = "<foo/>";
doc.Parse( str );
XMLTest( "Empty self-closed element", false, doc.Error() );
element = doc.RootElement();
element->SetText("The driver");
XMLTest( "SetText() normal use. (self-closing)", "The driver", element->GetText() );
element->SetText("<b>horses</b>");
XMLTest( "SetText() replace with tag-like text.", "<b>horses</b>", element->GetText() );
//doc.Print();
str = "<foo><bar>Text in nested element</bar></foo>";
doc.Parse( str );
XMLTest( "Text in nested element", false, doc.Error() );
element = doc.RootElement();
element->SetText("wolves");
XMLTest( "SetText() prefix to nested non-text children.", "wolves", element->GetText() );
str = "<foo/>";
doc.Parse( str );
XMLTest( "Empty self-closed element round 2", false, doc.Error() );
element = doc.RootElement();
element->SetText( "str" );
XMLTest( "SetText types", "str", element->GetText() );
element->SetText( 1 );
XMLTest( "SetText types", "1", element->GetText() );
element->SetText( 1U );
XMLTest( "SetText types", "1", element->GetText() );
element->SetText( true );
XMLTest( "SetText types", "true", element->GetText() );
element->SetText( 1.5f );
XMLTest( "SetText types", "1.5", element->GetText() );
element->SetText( 1.5 );
XMLTest( "SetText types", "1.5", element->GetText() );
}
// ---------- Attributes ---------
{
static const int64_t BIG = -123456789012345678;
static const uint64_t BIG_POS = 123456789012345678;
XMLDocument doc;
XMLElement* element = doc.NewElement("element");
doc.InsertFirstChild(element);
{
element->SetAttribute("attrib", int(-100));
{
int v = 0;
XMLError queryResult = element->QueryIntAttribute("attrib", &v);
XMLTest("Attribute: int", XML_SUCCESS, queryResult, true);
XMLTest("Attribute: int", -100, v, true);
}
{
int v = 0;
XMLError queryResult = element->QueryAttribute("attrib", &v);
XMLTest("Attribute: int", (int)XML_SUCCESS, queryResult, true);
XMLTest("Attribute: int", -100, v, true);
}
XMLTest("Attribute: int", -100, element->IntAttribute("attrib"), true);
}
{
element->SetAttribute("attrib", unsigned(100));
{
unsigned v = 0;
XMLError queryResult = element->QueryUnsignedAttribute("attrib", &v);
XMLTest("Attribute: unsigned", XML_SUCCESS, queryResult, true);
XMLTest("Attribute: unsigned", unsigned(100), v, true);
}
{
unsigned v = 0;
XMLError queryResult = element->QueryAttribute("attrib", &v);
XMLTest("Attribute: unsigned", (int)XML_SUCCESS, queryResult, true);
XMLTest("Attribute: unsigned", unsigned(100), v, true);
}
{
const char* v = "failed";
XMLError queryResult = element->QueryStringAttribute("not-attrib", &v);
XMLTest("Attribute: string default", false, queryResult == XML_SUCCESS);
queryResult = element->QueryStringAttribute("attrib", &v);
XMLTest("Attribute: string", XML_SUCCESS, queryResult, true);
XMLTest("Attribute: string", "100", v);
}
XMLTest("Attribute: unsigned", unsigned(100), element->UnsignedAttribute("attrib"), true);
}
{
element->SetAttribute("attrib", BIG);
{
int64_t v = 0;
XMLError queryResult = element->QueryInt64Attribute("attrib", &v);
XMLTest("Attribute: int64_t", XML_SUCCESS, queryResult, true);
XMLTest("Attribute: int64_t", BIG, v, true);
}
{
int64_t v = 0;
XMLError queryResult = element->QueryAttribute("attrib", &v);
XMLTest("Attribute: int64_t", (int)XML_SUCCESS, queryResult, true);
XMLTest("Attribute: int64_t", BIG, v, true);
}
XMLTest("Attribute: int64_t", BIG, element->Int64Attribute("attrib"), true);
}
{
element->SetAttribute("attrib", BIG_POS);
{
uint64_t v = 0;
XMLError queryResult = element->QueryUnsigned64Attribute("attrib", &v);
XMLTest("Attribute: uint64_t", XML_SUCCESS, queryResult, true);
XMLTest("Attribute: uint64_t", BIG_POS, v, true);
}
{
uint64_t v = 0;
XMLError queryResult = element->QueryAttribute("attrib", &v);
XMLTest("Attribute: uint64_t", (int)XML_SUCCESS, queryResult, true);
XMLTest("Attribute: uint64_t", BIG_POS, v, true);
}
XMLTest("Attribute: uint64_t", BIG_POS, element->Unsigned64Attribute("attrib"), true);
}
{
element->SetAttribute("attrib", true);
{
bool v = false;
XMLError queryResult = element->QueryBoolAttribute("attrib", &v);
XMLTest("Attribute: bool", XML_SUCCESS, queryResult, true);
XMLTest("Attribute: bool", true, v, true);
}
{
bool v = false;
XMLError queryResult = element->QueryAttribute("attrib", &v);
XMLTest("Attribute: bool", (int)XML_SUCCESS, queryResult, true);
XMLTest("Attribute: bool", true, v, true);
}
XMLTest("Attribute: bool", true, element->BoolAttribute("attrib"), true);
}
{
element->SetAttribute("attrib", true);
const char* result = element->Attribute("attrib");
XMLTest("Bool true is 'true'", "true", result);
XMLUtil::SetBoolSerialization("1", "0");
element->SetAttribute("attrib", true);
result = element->Attribute("attrib");
XMLTest("Bool true is '1'", "1", result);
XMLUtil::SetBoolSerialization(0, 0);
}
{
element->SetAttribute("attrib", 100.0);
{
double v = 0;
XMLError queryResult = element->QueryDoubleAttribute("attrib", &v);
XMLTest("Attribute: double", XML_SUCCESS, queryResult, true);
XMLTest("Attribute: double", 100.0, v, true);
}
{
double v = 0;
XMLError queryResult = element->QueryAttribute("attrib", &v);
XMLTest("Attribute: bool", (int)XML_SUCCESS, queryResult, true);
XMLTest("Attribute: double", 100.0, v, true);
}
XMLTest("Attribute: double", 100.0, element->DoubleAttribute("attrib"), true);
}
{
element->SetAttribute("attrib", 100.0f);
{
float v = 0;
XMLError queryResult = element->QueryFloatAttribute("attrib", &v);
XMLTest("Attribute: float", XML_SUCCESS, queryResult, true);
XMLTest("Attribute: float", 100.0f, v, true);
}
{
float v = 0;
XMLError queryResult = element->QueryAttribute("attrib", &v);
XMLTest("Attribute: float", (int)XML_SUCCESS, queryResult, true);
XMLTest("Attribute: float", 100.0f, v, true);
}
XMLTest("Attribute: float", 100.0f, element->FloatAttribute("attrib"), true);
}
{
element->SetText(BIG);
int64_t v = 0;
XMLError queryResult = element->QueryInt64Text(&v);
XMLTest("Element: int64_t", XML_SUCCESS, queryResult, true);
XMLTest("Element: int64_t", BIG, v, true);
}
{
element->SetText(BIG_POS);
uint64_t v = 0;
XMLError queryResult = element->QueryUnsigned64Text(&v);
XMLTest("Element: uint64_t", XML_SUCCESS, queryResult, true);
XMLTest("Element: uint64_t", BIG_POS, v, true);
}
}
// ---------- XMLPrinter stream mode ------
{
{
FILE* printerfp = fopen("resources/out/printer.xml", "w");
XMLTest("Open printer.xml", true, printerfp != 0);
XMLPrinter printer(printerfp);
printer.OpenElement("foo");
printer.PushAttribute("attrib-text", "text");
printer.PushAttribute("attrib-int", int(1));
printer.PushAttribute("attrib-unsigned", unsigned(2));
printer.PushAttribute("attrib-int64", int64_t(3));
printer.PushAttribute("attrib-uint64", uint64_t(37));
printer.PushAttribute("attrib-bool", true);
printer.PushAttribute("attrib-double", 4.0);
printer.CloseElement();
fclose(printerfp);
}
{
XMLDocument doc;
doc.LoadFile("resources/out/printer.xml");
XMLTest("XMLPrinter Stream mode: load", XML_SUCCESS, doc.ErrorID(), true);
const XMLDocument& cdoc = doc;
const XMLAttribute* attrib = cdoc.FirstChildElement("foo")->FindAttribute("attrib-text");
XMLTest("attrib-text", "text", attrib->Value(), true);
attrib = cdoc.FirstChildElement("foo")->FindAttribute("attrib-int");
XMLTest("attrib-int", int(1), attrib->IntValue(), true);
attrib = cdoc.FirstChildElement("foo")->FindAttribute("attrib-unsigned");
XMLTest("attrib-unsigned", unsigned(2), attrib->UnsignedValue(), true);
attrib = cdoc.FirstChildElement("foo")->FindAttribute("attrib-int64");
XMLTest("attrib-int64", int64_t(3), attrib->Int64Value(), true);
attrib = cdoc.FirstChildElement("foo")->FindAttribute("attrib-uint64");
XMLTest("attrib-uint64", uint64_t(37), attrib->Unsigned64Value(), true);
attrib = cdoc.FirstChildElement("foo")->FindAttribute("attrib-bool");
XMLTest("attrib-bool", true, attrib->BoolValue(), true);
attrib = cdoc.FirstChildElement("foo")->FindAttribute("attrib-double");
XMLTest("attrib-double", 4.0, attrib->DoubleValue(), true);
}
// Add API_testcatse :PushDeclaration();PushText();PushComment()
{
FILE* fp1 = fopen("resources/out/printer_1.xml", "w");
XMLPrinter printer(fp1);
printer.PushDeclaration("version = '1.0' enconding = 'utf-8'");
printer.OpenElement("foo");
printer.PushAttribute("attrib-text", "text");
printer.OpenElement("text");
printer.PushText("Tinyxml2");
printer.CloseElement();
printer.OpenElement("int");
printer.PushText(int(11));
printer.CloseElement();
printer.OpenElement("unsigned");
printer.PushText(unsigned(12));
printer.CloseElement();
printer.OpenElement("int64_t");
printer.PushText(int64_t(13));
printer.CloseElement();
printer.OpenElement("uint64_t");
printer.PushText(uint64_t(14));
printer.CloseElement();
printer.OpenElement("bool");
printer.PushText(true);
printer.CloseElement();
printer.OpenElement("float");
printer.PushText("1.56");
printer.CloseElement();
printer.OpenElement("double");
printer.PushText("12.12");
printer.CloseElement();
printer.OpenElement("comment");
printer.PushComment("this is Tinyxml2");
printer.CloseElement();
printer.CloseElement();
fclose(fp1);
}
{
XMLDocument doc;
doc.LoadFile("resources/out/printer_1.xml");
XMLTest("XMLPrinter Stream mode: load", XML_SUCCESS, doc.ErrorID(), true);
const XMLDocument& cdoc = doc;
const XMLElement* root = cdoc.FirstChildElement("foo");
const char* text_value;
text_value = root->FirstChildElement("text")->GetText();
XMLTest("PushText( const char* text, bool cdata=false ) test", "Tinyxml2", text_value);
int int_value;
int_value = root->FirstChildElement("int")->IntText();
XMLTest("PushText( int value ) test", 11, int_value);
unsigned unsigned_value;
unsigned_value = root->FirstChildElement("unsigned")->UnsignedText();
XMLTest("PushText( unsigned value ) test", (unsigned)12, unsigned_value);
int64_t int64_t_value;
int64_t_value = root->FirstChildElement("int64_t")->Int64Text();
XMLTest("PushText( int64_t value ) test", (int64_t) 13, int64_t_value);
uint64_t uint64_t_value;
uint64_t_value = root->FirstChildElement("uint64_t")->Unsigned64Text();
XMLTest("PushText( uint64_t value ) test", (uint64_t) 14, uint64_t_value);
float float_value;
float_value = root->FirstChildElement("float")->FloatText();
XMLTest("PushText( float value ) test", 1.56f, float_value);
double double_value;
double_value = root->FirstChildElement("double")->DoubleText();
XMLTest("PushText( double value ) test", 12.12, double_value);
bool bool_value;
bool_value = root->FirstChildElement("bool")->BoolText();
XMLTest("PushText( bool value ) test", true, bool_value);
const XMLComment* comment = root->FirstChildElement("comment")->FirstChild()->ToComment();
const char* comment_value = comment->Value();
XMLTest("PushComment() test", "this is Tinyxml2", comment_value);
const XMLDeclaration* declaration = cdoc.FirstChild()->ToDeclaration();
const char* declaration_value = declaration->Value();
XMLTest("PushDeclaration() test", "version = '1.0' enconding = 'utf-8'", declaration_value);
}
}
// ---------- CDATA ---------------
{
const char* str = "<xmlElement>"
"<![CDATA["
"I am > the rules!\n"
"...since I make symbolic puns"
"]]>"
"</xmlElement>";
XMLDocument doc;
doc.Parse( str );
XMLTest( "CDATA symbolic puns round 1", false, doc.Error() );
doc.Print();
XMLTest( "CDATA parse.", "I am > the rules!\n...since I make symbolic puns",
doc.FirstChildElement()->FirstChild()->Value(),
false );
}
// ----------- CDATA -------------
{
const char* str = "<xmlElement>"
"<![CDATA["
"<b>I am > the rules!</b>\n"
"...since I make symbolic puns"
"]]>"
"</xmlElement>";
XMLDocument doc;
doc.Parse( str );
XMLTest( "CDATA symbolic puns round 2", false, doc.Error() );
doc.Print();
XMLTest( "CDATA parse. [ tixml1:1480107 ]",
"<b>I am > the rules!</b>\n...since I make symbolic puns",
doc.FirstChildElement()->FirstChild()->Value(),
false );
}
// InsertAfterChild causes crash.
{
// InsertBeforeChild and InsertAfterChild causes crash.
XMLDocument doc;
XMLElement* parent = doc.NewElement( "Parent" );
doc.InsertFirstChild( parent );
XMLElement* childText0 = doc.NewElement( "childText0" );
XMLElement* childText1 = doc.NewElement( "childText1" );
XMLNode* childNode0 = parent->InsertEndChild( childText0 );
XMLTest( "InsertEndChild() return", true, childNode0 == childText0 );
XMLNode* childNode1 = parent->InsertAfterChild( childNode0, childText1 );
XMLTest( "InsertAfterChild() return", true, childNode1 == childText1 );
XMLTest( "Test InsertAfterChild on empty node. ", true, ( childNode1 == parent->LastChild() ) );
}
{
// Entities not being written correctly.
// From Lynn Allen
const char* passages =
"<?xml version=\"1.0\" standalone=\"no\" ?>"
"<passages count=\"006\" formatversion=\"20020620\">"
"<psg context=\"Line 5 has "quotation marks" and 'apostrophe marks'."
" It also has <, >, and &, as well as a fake copyright ©.\"> </psg>"
"</passages>";
XMLDocument doc;
doc.Parse( passages );
XMLTest( "Entity transformation parse round 1", false, doc.Error() );
XMLElement* psg = doc.RootElement()->FirstChildElement();
const char* context = psg->Attribute( "context" );
const char* expected = "Line 5 has \"quotation marks\" and 'apostrophe marks'. It also has <, >, and &, as well as a fake copyright \xC2\xA9.";
XMLTest( "Entity transformation: read. ", expected, context, true );
const char* textFilePath = "resources/out/textfile.txt";
FILE* textfile = fopen( textFilePath, "w" );
XMLTest( "Entity transformation: open text file for writing", true, textfile != 0, true );
if ( textfile )
{
XMLPrinter streamer( textfile );
bool acceptResult = psg->Accept( &streamer );
fclose( textfile );
XMLTest( "Entity transformation: Accept", true, acceptResult );
}
textfile = fopen( textFilePath, "r" );
XMLTest( "Entity transformation: open text file for reading", true, textfile != 0, true );
if ( textfile )
{
char buf[ 1024 ];
fgets( buf, 1024, textfile );
XMLTest( "Entity transformation: write. ",
"<psg context=\"Line 5 has "quotation marks" and 'apostrophe marks'."
" It also has <, >, and &, as well as a fake copyright \xC2\xA9.\"/>\n",
buf, false );
fclose( textfile );
}
}
{
// Suppress entities.
const char* passages =
"<?xml version=\"1.0\" standalone=\"no\" ?>"
"<passages count=\"006\" formatversion=\"20020620\">"
"<psg context=\"Line 5 has "quotation marks" and 'apostrophe marks'.\">Crazy &ttk;</psg>"
"</passages>";
XMLDocument doc( false );
doc.Parse( passages );
XMLTest( "Entity transformation parse round 2", false, doc.Error() );
XMLTest( "No entity parsing.",
"Line 5 has "quotation marks" and 'apostrophe marks'.",
doc.FirstChildElement()->FirstChildElement()->Attribute( "context" ) );
XMLTest( "No entity parsing.", "Crazy &ttk;",
doc.FirstChildElement()->FirstChildElement()->FirstChild()->Value() );
doc.Print();
}
{
const char* test = "<?xml version='1.0'?><a.elem xmi.version='2.0'/>";
XMLDocument doc;
doc.Parse( test );
XMLTest( "dot in names", false, doc.Error() );
XMLTest( "dot in names", "a.elem", doc.FirstChildElement()->Name() );
XMLTest( "dot in names", "2.0", doc.FirstChildElement()->Attribute( "xmi.version" ) );
}
{
const char* test = "<element><Name>1.1 Start easy ignore fin thickness
</Name></element>";
XMLDocument doc;
doc.Parse( test );
XMLTest( "fin thickness", false, doc.Error() );
XMLText* text = doc.FirstChildElement()->FirstChildElement()->FirstChild()->ToText();
XMLTest( "Entity with one digit.",
"1.1 Start easy ignore fin thickness\n", text->Value(),
false );
}
{
// DOCTYPE not preserved (950171)
//
const char* doctype =
"<?xml version=\"1.0\" ?>"
"<!DOCTYPE PLAY SYSTEM 'play.dtd'>"
"<!ELEMENT title (#PCDATA)>"
"<!ELEMENT books (title,authors)>"
"<element />";
XMLDocument doc;
doc.Parse( doctype );
XMLTest( "PLAY SYSTEM parse", false, doc.Error() );
doc.SaveFile( "resources/out/test7.xml" );
XMLTest( "PLAY SYSTEM save", false, doc.Error() );
doc.DeleteChild( doc.RootElement() );
doc.LoadFile( "resources/out/test7.xml" );
XMLTest( "PLAY SYSTEM load", false, doc.Error() );
doc.Print();
const XMLUnknown* decl = doc.FirstChild()->NextSibling()->ToUnknown();
XMLTest( "Correct value of unknown.", "DOCTYPE PLAY SYSTEM 'play.dtd'", decl->Value() );
}
{
// Comments do not stream out correctly.
const char* doctype =
"<!-- Somewhat<evil> -->";
XMLDocument doc;
doc.Parse( doctype );
XMLTest( "Comment somewhat evil", false, doc.Error() );
XMLComment* comment = doc.FirstChild()->ToComment();
XMLTest( "Comment formatting.", " Somewhat<evil> ", comment->Value() );
}
{
// Double attributes
const char* doctype = "<element attr='red' attr='blue' />";
XMLDocument doc;
doc.Parse( doctype );
XMLTest( "Parsing repeated attributes.", XML_ERROR_PARSING_ATTRIBUTE, doc.ErrorID() ); // is an error to tinyxml (didn't use to be, but caused issues)
doc.PrintError();
}
{
// Embedded null in stream.
const char* doctype = "<element att\0r='red' attr='blue' />";
XMLDocument doc;
doc.Parse( doctype );
XMLTest( "Embedded null throws error.", true, doc.Error() );
}
{
// Empty documents should return TIXML_XML_ERROR_PARSING_EMPTY, bug 1070717
const char* str = "";
XMLDocument doc;
doc.Parse( str );
XMLTest( "Empty document error", XML_ERROR_EMPTY_DOCUMENT, doc.ErrorID() );
// But be sure there is an error string!
const char* errorStr = doc.ErrorStr();
XMLTest("Error string should be set",
"Error=XML_ERROR_EMPTY_DOCUMENT ErrorID=13 (0xd) Line number=0",
errorStr);
}
{
// Documents with all whitespaces should return TIXML_XML_ERROR_PARSING_EMPTY, bug 1070717
const char* str = " ";
XMLDocument doc;
doc.Parse( str );
XMLTest( "All whitespaces document error", XML_ERROR_EMPTY_DOCUMENT, doc.ErrorID() );
}
{
// Low entities
XMLDocument doc;
doc.Parse( "<test></test>" );
XMLTest( "Hex values", false, doc.Error() );
const char result[] = { 0x0e, 0 };
XMLTest( "Low entities.", result, doc.FirstChildElement()->GetText() );
doc.Print();
}
{
// Attribute values with trailing quotes not handled correctly
XMLDocument doc;
doc.Parse( "<foo attribute=bar\" />" );
XMLTest( "Throw error with bad end quotes.", true, doc.Error() );
}
{
// [ 1663758 ] Failure to report error on bad XML
XMLDocument xml;
xml.Parse("<x>");
XMLTest("Missing end tag at end of input", true, xml.Error());
xml.Parse("<x> ");
XMLTest("Missing end tag with trailing whitespace", true, xml.Error());
xml.Parse("<x></y>");
XMLTest("Mismatched tags", XML_ERROR_MISMATCHED_ELEMENT, xml.ErrorID() );
}
{
// [ 1475201 ] TinyXML parses entities in comments
XMLDocument xml;
xml.Parse("<!-- declarations for <head> & <body> -->"
"<!-- far & away -->" );
XMLTest( "Declarations for head and body", false, xml.Error() );
XMLNode* e0 = xml.FirstChild();
XMLNode* e1 = e0->NextSibling();
XMLComment* c0 = e0->ToComment();
XMLComment* c1 = e1->ToComment();
XMLTest( "Comments ignore entities.", " declarations for <head> & <body> ", c0->Value(), true );
XMLTest( "Comments ignore entities.", " far & away ", c1->Value(), true );
}
{
XMLDocument xml;
xml.Parse( "<Parent>"
"<child1 att=''/>"
"<!-- With this comment, child2 will not be parsed! -->"
"<child2 att=''/>"
"</Parent>" );
XMLTest( "Comments iteration", false, xml.Error() );
xml.Print();
int count = 0;
for( XMLNode* ele = xml.FirstChildElement( "Parent" )->FirstChild();
ele;
ele = ele->NextSibling() )
{
++count;
}
XMLTest( "Comments iterate correctly.", 3, count );
}
{
// trying to repro [1874301]. If it doesn't go into an infinite loop, all is well.
unsigned char buf[] = "<?xml version=\"1.0\" encoding=\"utf-8\"?><feed><![CDATA[Test XMLblablablalblbl";
buf[60] = 239;
buf[61] = 0;
XMLDocument doc;
doc.Parse( (const char*)buf);
XMLTest( "Broken CDATA", true, doc.Error() );
}
{
// bug 1827248 Error while parsing a little bit malformed file
// Actually not malformed - should work.
XMLDocument xml;
xml.Parse( "<attributelist> </attributelist >" );
XMLTest( "Handle end tag whitespace", false, xml.Error() );
}
{
// This one must not result in an infinite loop
XMLDocument xml;
xml.Parse( "<infinite>loop" );
XMLTest( "No closing element", true, xml.Error() );
XMLTest( "Infinite loop test.", true, true );
}
#endif
{
const char* pub = "<?xml version='1.0'?> <element><sub/></element> <!--comment--> <!DOCTYPE>";
XMLDocument doc;
doc.Parse( pub );
XMLTest( "Trailing DOCTYPE", false, doc.Error() );
XMLDocument clone;
for( const XMLNode* node=doc.FirstChild(); node; node=node->NextSibling() ) {
XMLNode* copy = node->ShallowClone( &clone );
clone.InsertEndChild( copy );
}
clone.Print();
int count=0;
const XMLNode* a=clone.FirstChild();
const XMLNode* b=doc.FirstChild();
for( ; a && b; a=a->NextSibling(), b=b->NextSibling() ) {
++count;
XMLTest( "Clone and Equal", true, a->ShallowEqual( b ));
}
XMLTest( "Clone and Equal", 4, count );
}
{
// Deep Cloning of root element.
XMLDocument doc2;
XMLPrinter printer1;
{
// Make sure doc1 is deleted before we test doc2
const char* xml =
"<root>"
" <child1 foo='bar'/>"
" <!-- comment thing -->"
" <child2 val='1'>Text</child2>"
"</root>";
XMLDocument doc;
doc.Parse(xml);
XMLTest( "Parse before deep cloning root element", false, doc.Error() );
doc.Print(&printer1);
XMLNode* root = doc.RootElement()->DeepClone(&doc2);
doc2.InsertFirstChild(root);
}
XMLPrinter printer2;
doc2.Print(&printer2);
XMLTest("Deep clone of element.", printer1.CStr(), printer2.CStr(), true);
}
{
// Deep Cloning of sub element.
XMLDocument doc2;
XMLPrinter printer1;
{
// Make sure doc1 is deleted before we test doc2
const char* xml =
"<?xml version ='1.0'?>"
"<root>"
" <child1 foo='bar'/>"
" <!-- comment thing -->"
" <child2 val='1'>Text</child2>"
"</root>";
XMLDocument doc;
doc.Parse(xml);
XMLTest( "Parse before deep cloning sub element", false, doc.Error() );
const XMLElement* subElement = doc.FirstChildElement("root")->FirstChildElement("child2");
bool acceptResult = subElement->Accept(&printer1);
XMLTest( "Accept before deep cloning", true, acceptResult );
XMLNode* clonedSubElement = subElement->DeepClone(&doc2);
doc2.InsertFirstChild(clonedSubElement);
}
XMLPrinter printer2;
doc2.Print(&printer2);
XMLTest("Deep clone of sub-element.", printer1.CStr(), printer2.CStr(), true);
}
{
// Deep cloning of document.
XMLDocument doc2;
XMLPrinter printer1;
{
// Make sure doc1 is deleted before we test doc2
const char* xml =
"<?xml version ='1.0'?>"
"<!-- Top level comment. -->"
"<root>"
" <child1 foo='bar'/>"
" <!-- comment thing -->"
" <child2 val='1'>Text</child2>"
"</root>";
XMLDocument doc;
doc.Parse(xml);
XMLTest( "Parse before deep cloning document", false, doc.Error() );
doc.Print(&printer1);
doc.DeepCopy(&doc2);
}
XMLPrinter printer2;
doc2.Print(&printer2);
XMLTest("DeepCopy of document.", printer1.CStr(), printer2.CStr(), true);
}
{
// This shouldn't crash.
XMLDocument doc;
if(XML_SUCCESS != doc.LoadFile( "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" ))
{
doc.PrintError();
}
XMLTest( "Error in snprinf handling.", true, doc.Error() );
}
{
// Attribute ordering.
static const char* xml = "<element attrib1=\"1\" attrib2=\"2\" attrib3=\"3\" />";
XMLDocument doc;
doc.Parse( xml );
XMLTest( "Parse for attribute ordering", false, doc.Error() );
XMLElement* ele = doc.FirstChildElement();
const XMLAttribute* a = ele->FirstAttribute();
XMLTest( "Attribute order", "1", a->Value() );
a = a->Next();
XMLTest( "Attribute order", "2", a->Value() );
a = a->Next();
XMLTest( "Attribute order", "3", a->Value() );
XMLTest( "Attribute order", "attrib3", a->Name() );
ele->DeleteAttribute( "attrib2" );
a = ele->FirstAttribute();
XMLTest( "Attribute order", "1", a->Value() );
a = a->Next();
XMLTest( "Attribute order", "3", a->Value() );
ele->DeleteAttribute( "attrib1" );
ele->DeleteAttribute( "attrib3" );
XMLTest( "Attribute order (empty)", true, ele->FirstAttribute() == 0 );
}
{
// Make sure an attribute with a space in it succeeds.
static const char* xml0 = "<element attribute1= \"Test Attribute\"/>";
static const char* xml1 = "<element attribute1 =\"Test Attribute\"/>";
static const char* xml2 = "<element attribute1 = \"Test Attribute\"/>";
XMLDocument doc0;
doc0.Parse( xml0 );
XMLTest( "Parse attribute with space 1", false, doc0.Error() );
XMLDocument doc1;
doc1.Parse( xml1 );
XMLTest( "Parse attribute with space 2", false, doc1.Error() );
XMLDocument doc2;
doc2.Parse( xml2 );
XMLTest( "Parse attribute with space 3", false, doc2.Error() );
XMLElement* ele = 0;
ele = doc0.FirstChildElement();
XMLTest( "Attribute with space #1", "Test Attribute", ele->Attribute( "attribute1" ) );
ele = doc1.FirstChildElement();
XMLTest( "Attribute with space #2", "Test Attribute", ele->Attribute( "attribute1" ) );
ele = doc2.FirstChildElement();
XMLTest( "Attribute with space #3", "Test Attribute", ele->Attribute( "attribute1" ) );
}
{
// Make sure we don't go into an infinite loop.
static const char* xml = "<doc><element attribute='attribute'/><element attribute='attribute'/></doc>";
XMLDocument doc;
doc.Parse( xml );
XMLTest( "Parse two elements with attribute", false, doc.Error() );
XMLElement* ele0 = doc.FirstChildElement()->FirstChildElement();
XMLElement* ele1 = ele0->NextSiblingElement();
bool equal = ele0->ShallowEqual( ele1 );
XMLTest( "Infinite loop in shallow equal.", true, equal );
}
// -------- Handles ------------
{
static const char* xml = "<element attrib='bar'><sub>Text</sub></element>";
XMLDocument doc;
doc.Parse( xml );
XMLTest( "Handle, parse element with attribute and nested element", false, doc.Error() );
{
XMLElement* ele = XMLHandle( doc ).FirstChildElement( "element" ).FirstChild().ToElement();
XMLTest( "Handle, non-const, element is found", true, ele != 0 );
XMLTest( "Handle, non-const, element name matches", "sub", ele->Value() );
}
{
XMLHandle docH( doc );
XMLElement* ele = docH.FirstChildElement( "noSuchElement" ).FirstChildElement( "element" ).ToElement();
XMLTest( "Handle, non-const, element not found", true, ele == 0 );
}
{
const XMLElement* ele = XMLConstHandle( doc ).FirstChildElement( "element" ).FirstChild().ToElement();
XMLTest( "Handle, const, element is found", true, ele != 0 );
XMLTest( "Handle, const, element name matches", "sub", ele->Value() );
}
{
XMLConstHandle docH( doc );
const XMLElement* ele = docH.FirstChildElement( "noSuchElement" ).FirstChildElement( "element" ).ToElement();
XMLTest( "Handle, const, element not found", true, ele == 0 );
}
}
{
// Default Declaration & BOM
XMLDocument doc;
doc.InsertEndChild( doc.NewDeclaration() );
doc.SetBOM( true );
XMLPrinter printer;
doc.Print( &printer );
static const char* result = "\xef\xbb\xbf<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
XMLTest( "BOM and default declaration", result, printer.CStr(), false );
XMLTest( "CStrSize", 42, printer.CStrSize(), false );
}
{
const char* xml = "<ipxml ws='1'><info bla=' /></ipxml>";
XMLDocument doc;
doc.Parse( xml );
XMLTest( "Ill formed XML", true, doc.Error() );
}
{
//API:IntText(),UnsignedText(),Int64Text(),DoubleText(),BoolText() and FloatText() test
const char* xml = "<point> <IntText>-24</IntText> <UnsignedText>42</UnsignedText> \
<Int64Text>38</Int64Text> <BoolText>true</BoolText> <DoubleText>2.35</DoubleText> </point>";
XMLDocument doc;
doc.Parse(xml);
const XMLElement* pointElement = doc.RootElement();
int test1 = pointElement->FirstChildElement("IntText")->IntText();
XMLTest("IntText() test", -24, test1);
unsigned test2 = pointElement->FirstChildElement("UnsignedText")->UnsignedText();
XMLTest("UnsignedText() test", static_cast<unsigned>(42), test2);
int64_t test3 = pointElement->FirstChildElement("Int64Text")->Int64Text();
XMLTest("Int64Text() test", static_cast<int64_t>(38), test3);
double test4 = pointElement->FirstChildElement("DoubleText")->DoubleText();
XMLTest("DoubleText() test", 2.35, test4);
float test5 = pointElement->FirstChildElement("DoubleText")->FloatText();
XMLTest("FloatText()) test", 2.35f, test5);
bool test6 = pointElement->FirstChildElement("BoolText")->BoolText();
XMLTest("FloatText()) test", true, test6);
}
{
// hex value test
const char* xml = "<point> <IntText> 0x2020</IntText> <UnsignedText>0X2020</UnsignedText> \
<Int64Text> 0x1234</Int64Text></point>";
XMLDocument doc;
doc.Parse(xml);
const XMLElement* pointElement = doc.RootElement();
int test1 = pointElement->FirstChildElement("IntText")->IntText();
XMLTest("IntText() hex value test", 0x2020, test1);
unsigned test2 = pointElement->FirstChildElement("UnsignedText")->UnsignedText();
XMLTest("UnsignedText() hex value test", static_cast<unsigned>(0x2020), test2);
int64_t test3 = pointElement->FirstChildElement("Int64Text")->Int64Text();
XMLTest("Int64Text() hex value test", static_cast<int64_t>(0x1234), test3);
}
{
//API:ShallowEqual() test
const char* xml = "<playlist id = 'playlist'>"
"<property name = 'track_name'>voice</property>"
"</playlist>";
XMLDocument doc;
doc.Parse( xml );
const XMLNode* PlaylistNode = doc.RootElement();
const XMLNode* PropertyNode = PlaylistNode->FirstChildElement();
bool result;
result = PlaylistNode->ShallowEqual(PropertyNode);
XMLTest("ShallowEqual() test",false,result);
result = PlaylistNode->ShallowEqual(PlaylistNode);
XMLTest("ShallowEqual() test",true,result);
}
{
//API: previousSiblingElement() and NextSiblingElement() test
const char* xml = "<playlist id = 'playlist'>"
"<property name = 'track_name'>voice</property>"
"<entry out = '946' producer = '2_playlist1' in = '0'/>"
"<blank length = '1'/>"
"</playlist>";
XMLDocument doc;
doc.Parse( xml );
XMLElement* ElementPlaylist = doc.FirstChildElement("playlist");
XMLTest("previousSiblingElement() test",true,ElementPlaylist != 0);
const XMLElement* pre = ElementPlaylist->PreviousSiblingElement();
XMLTest("previousSiblingElement() test",true,pre == 0);
const XMLElement* ElementBlank = ElementPlaylist->FirstChildElement("entry")->NextSiblingElement("blank");
XMLTest("NextSiblingElement() test",true,ElementBlank != 0);
const XMLElement* next = ElementBlank->NextSiblingElement();
XMLTest("NextSiblingElement() test",true,next == 0);
const XMLElement* ElementEntry = ElementBlank->PreviousSiblingElement("entry");
XMLTest("PreviousSiblingElement test",true,ElementEntry != 0);
}
// QueryXYZText
{
const char* xml = "<point> <x>1.2</x> <y>1</y> <z>38</z> <valid>true</valid> </point>";
XMLDocument doc;
doc.Parse( xml );
XMLTest( "Parse points", false, doc.Error() );
const XMLElement* pointElement = doc.RootElement();
{
int intValue = 0;
XMLError queryResult = pointElement->FirstChildElement( "y" )->QueryIntText( &intValue );
XMLTest( "QueryIntText result", XML_SUCCESS, queryResult, false );
XMLTest( "QueryIntText", 1, intValue, false );
}
{
unsigned unsignedValue = 0;
XMLError queryResult = pointElement->FirstChildElement( "y" )->QueryUnsignedText( &unsignedValue );
XMLTest( "QueryUnsignedText result", XML_SUCCESS, queryResult, false );
XMLTest( "QueryUnsignedText", (unsigned)1, unsignedValue, false );
}
{
float floatValue = 0;
XMLError queryResult = pointElement->FirstChildElement( "x" )->QueryFloatText( &floatValue );
XMLTest( "QueryFloatText result", XML_SUCCESS, queryResult, false );
XMLTest( "QueryFloatText", 1.2f, floatValue, false );
}
{
double doubleValue = 0;
XMLError queryResult = pointElement->FirstChildElement( "x" )->QueryDoubleText( &doubleValue );
XMLTest( "QueryDoubleText result", XML_SUCCESS, queryResult, false );
XMLTest( "QueryDoubleText", 1.2, doubleValue, false );
}
{
bool boolValue = false;
XMLError queryResult = pointElement->FirstChildElement( "valid" )->QueryBoolText( &boolValue );
XMLTest( "QueryBoolText result", XML_SUCCESS, queryResult, false );
XMLTest( "QueryBoolText", true, boolValue, false );
}
}
{
const char* xml = "<element><_sub/><:sub/><sub:sub/><sub-sub/></element>";
XMLDocument doc;
doc.Parse( xml );
XMLTest( "Non-alpha element lead letter parses.", false, doc.Error() );
}
{
const char* xml = "<element _attr1=\"foo\" :attr2=\"bar\"></element>";
XMLDocument doc;
doc.Parse( xml );
XMLTest("Non-alpha attribute lead character parses.", false, doc.Error());
}
{
const char* xml = "<3lement></3lement>";
XMLDocument doc;
doc.Parse( xml );
XMLTest("Element names with lead digit fail to parse.", true, doc.Error());
}
{
const char* xml = "<element/>WOA THIS ISN'T GOING TO PARSE";
XMLDocument doc;
doc.Parse( xml, 10 );
XMLTest( "Set length of incoming data", false, doc.Error() );
}
{
XMLDocument doc;
XMLTest( "Document is initially empty", true, doc.NoChildren() );
doc.Clear();
XMLTest( "Empty is empty after Clear()", true, doc.NoChildren() );
doc.LoadFile( "resources/dream.xml" );
XMLTest( "Load dream.xml", false, doc.Error() );
XMLTest( "Document has something to Clear()", false, doc.NoChildren() );
doc.Clear();
XMLTest( "Document Clear()'s", true, doc.NoChildren() );
}
{
XMLDocument doc;
XMLTest( "No error initially", false, doc.Error() );
XMLError error = doc.Parse( "This is not XML" );
XMLTest( "Error after invalid XML", true, doc.Error() );
XMLTest( "Error after invalid XML", error, doc.ErrorID() );
doc.Clear();
XMLTest( "No error after Clear()", false, doc.Error() );
}
// ----------- Whitespace ------------
{
const char* xml = "<element>"
"<a> This \nis ' text ' </a>"
"<b> This is ' text ' \n</b>"
"<c>This is ' \n\n text '</c>"
"</element>";
XMLDocument doc( true, COLLAPSE_WHITESPACE );
doc.Parse( xml );
XMLTest( "Parse with whitespace collapsing and &apos", false, doc.Error() );
const XMLElement* element = doc.FirstChildElement();
for( const XMLElement* parent = element->FirstChildElement();
parent;
parent = parent->NextSiblingElement() )
{
XMLTest( "Whitespace collapse", "This is ' text '", parent->GetText() );
}
}
#if 0
{
// Passes if assert doesn't fire.
XMLDocument xmlDoc;
xmlDoc.NewDeclaration();
xmlDoc.NewComment("Configuration file");
XMLElement *root = xmlDoc.NewElement("settings");
root->SetAttribute("version", 2);
}
#endif
{
const char* xml = "<element> </element>";
XMLDocument doc( true, COLLAPSE_WHITESPACE );
doc.Parse( xml );
XMLTest( "Parse with all whitespaces", false, doc.Error() );
XMLTest( "Whitespace all space", true, 0 == doc.FirstChildElement()->FirstChild() );
}
// ----------- Preserve Whitespace ------------
{
const char* xml = "<element>This is ' \n\n text '</element>";
XMLDocument doc(true, PRESERVE_WHITESPACE);
doc.Parse(xml);
XMLTest("Parse with whitespace preserved", false, doc.Error());
XMLTest("Whitespace preserved", "This is ' \n\n text '", doc.FirstChildElement()->GetText());
}
{
const char* xml = "<element> This \nis ' text ' </element>";
XMLDocument doc(true, PRESERVE_WHITESPACE);
doc.Parse(xml);
XMLTest("Parse with whitespace preserved", false, doc.Error());
XMLTest("Whitespace preserved", " This \nis ' text ' ", doc.FirstChildElement()->GetText());
}
{
const char* xml = "<element> \n This is ' text ' \n</element>";
XMLDocument doc(true, PRESERVE_WHITESPACE);
doc.Parse(xml);
XMLTest("Parse with whitespace preserved", false, doc.Error());
XMLTest("Whitespace preserved", " \n This is ' text ' \n", doc.FirstChildElement()->GetText());
}
// Following cases are for text that is all whitespace which are not preserved intentionally
{
const char* xml = "<element> </element>";
XMLDocument doc(true, PRESERVE_WHITESPACE);
doc.Parse(xml);
XMLTest("Parse with whitespace preserved", false, doc.Error());
XMLTest("Whitespace preserved", true, 0 == doc.FirstChildElement()->GetText());
}
{
const char* xml = "<element> </element>";
XMLDocument doc(true, PRESERVE_WHITESPACE);
doc.Parse(xml);
XMLTest("Parse with whitespace preserved", false, doc.Error());
XMLTest("Whitespace preserved", true, 0 == doc.FirstChildElement()->GetText());
}
{
const char* xml = "<element>\n\n</element>";
XMLDocument doc(true, PRESERVE_WHITESPACE);
doc.Parse(xml);
XMLTest("Parse with whitespace preserved", false, doc.Error());
XMLTest("Whitespace preserved", true, 0 == doc.FirstChildElement()->GetText());
}
{
const char* xml = "<element> \n</element>";
XMLDocument doc(true, PRESERVE_WHITESPACE);
doc.Parse(xml);
XMLTest("Parse with whitespace preserved", false, doc.Error());
XMLTest("Whitespace preserved", true, 0 == doc.FirstChildElement()->GetText());
}
{
const char* xml = "<element> \n \n </element>";
XMLDocument doc(true, PRESERVE_WHITESPACE);
doc.Parse(xml);
XMLTest("Parse with whitespace preserved", false, doc.Error());
XMLTest("Whitespace preserved", true, 0 == doc.FirstChildElement()->GetText());
}
// ----------- Pedantic Whitespace ------------
{
const char* xml = "<element>This is ' \n\n text '</element>";
XMLDocument doc(true, PEDANTIC_WHITESPACE);
doc.Parse(xml);
XMLTest("Parse with pedantic whitespace", false, doc.Error());
XMLTest("Pedantic whitespace", "This is ' \n\n text '", doc.FirstChildElement()->GetText());
}
{
const char* xml = "<element> This \nis ' text ' </element>";
XMLDocument doc(true, PEDANTIC_WHITESPACE);
doc.Parse(xml);
XMLTest("Parse with pedantic whitespace", false, doc.Error());
XMLTest("Pedantic whitespace", " This \nis ' text ' ", doc.FirstChildElement()->GetText());
}
{
const char* xml = "<element> \n This is ' text ' \n</element>";
XMLDocument doc(true, PEDANTIC_WHITESPACE);
doc.Parse(xml);
XMLTest("Parse with pedantic whitespace", false, doc.Error());
XMLTest("Pedantic whitespace", " \n This is ' text ' \n", doc.FirstChildElement()->GetText());
}
// Following cases are for text that is all whitespace which is preserved with pedantic mode
{
const char* xml = "<element> </element>";
XMLDocument doc(true, PEDANTIC_WHITESPACE);
doc.Parse(xml);
XMLTest("Parse with pedantic whitespace", false, doc.Error());
XMLTest("Pedantic whitespace", " ", doc.FirstChildElement()->GetText());
}
{
const char* xml = "<element> </element>";
XMLDocument doc(true, PEDANTIC_WHITESPACE);
doc.Parse(xml);
XMLTest("Parse with pedantic whitespace", false, doc.Error());
XMLTest("Pedantic whitespace", " ", doc.FirstChildElement()->GetText());
}
{
const char* xml = "<element>\n\n</element>\n";
XMLDocument doc(true, PEDANTIC_WHITESPACE);
doc.Parse(xml);
XMLTest("Parse with pedantic whitespace", false, doc.Error());
XMLTest("Pedantic whitespace", "\n\n", doc.FirstChildElement()->GetText());
}
{
const char* xml = "<element> \n</element> \n ";
XMLDocument doc(true, PEDANTIC_WHITESPACE);
doc.Parse(xml);
XMLTest("Parse with pedantic whitespace", false, doc.Error());
XMLTest("Pedantic whitespace", " \n", doc.FirstChildElement()->GetText());
}
{
const char* xml = "<element> \n \n </element> ";
XMLDocument doc(true, PEDANTIC_WHITESPACE);
doc.Parse(xml);
XMLTest("Parse with pedantic whitespace", false, doc.Error());
XMLTest("Pedantic whitespace", " \n \n ", doc.FirstChildElement()->GetText());
}
// Following cases are for checking nested elements are still parsed with pedantic whitespace
{
const char* xml = "<element>\n\t<a> This is nested text </a>\n</element> ";
XMLDocument doc(true, PEDANTIC_WHITESPACE);
doc.Parse(xml);
XMLTest("Parse nested elements with pedantic whitespace", false, doc.Error());
XMLTest("Pedantic whitespace", " This is nested text ", doc.RootElement()->FirstChildElement()->GetText());
}
{
const char* xml = "<element> <b> </b> </element>\n";
XMLDocument doc(true, PEDANTIC_WHITESPACE);
doc.Parse(xml);
XMLTest("Parse nested elements with pedantic whitespace", false, doc.Error());
XMLTest("Pedantic whitespace", " ", doc.RootElement()->FirstChildElement()->GetText());
}
{
const char* xml = "<element> <c attribute=\"test\"/> </element>\n ";
XMLDocument doc(true, PEDANTIC_WHITESPACE);
doc.Parse(xml);
XMLTest("Parse nested elements with pedantic whitespace", false, doc.Error());
XMLTest("Pedantic whitespace", true, 0 == doc.RootElement()->FirstChildElement()->GetText());
}
// Check sample xml can be parsed with pedantic mode
{
XMLDocument doc(true, PEDANTIC_WHITESPACE);
doc.LoadFile("resources/dream.xml");
XMLTest("Load dream.xml with pedantic whitespace mode", false, doc.Error());
XMLTest("Dream", "xml version=\"1.0\"",
doc.FirstChild()->ToDeclaration()->Value());
XMLTest("Dream", true, doc.FirstChild()->NextSibling()->ToUnknown() != 0);
XMLTest("Dream", "DOCTYPE PLAY SYSTEM \"play.dtd\"",
doc.FirstChild()->NextSibling()->ToUnknown()->Value());
XMLTest("Dream", "And Robin shall restore amends.",
doc.LastChild()->LastChild()->LastChild()->LastChild()->LastChildElement()->GetText());
}
{
// An assert should not fire.
const char* xml = "<element/>";
XMLDocument doc;
doc.Parse( xml );
XMLTest( "Parse with self-closed element", false, doc.Error() );
XMLElement* ele = doc.NewElement( "unused" ); // This will get cleaned up with the 'doc' going out of scope.
XMLTest( "Tracking unused elements", true, ele != 0, false );
}
{
const char* xml = "<parent><child>abc</child></parent>";
XMLDocument doc;
doc.Parse( xml );
XMLTest( "Parse for printing of sub-element", false, doc.Error() );
XMLElement* ele = doc.FirstChildElement( "parent")->FirstChildElement( "child");
XMLPrinter printer;
bool acceptResult = ele->Accept( &printer );
XMLTest( "Accept of sub-element", true, acceptResult );
XMLTest( "Printing of sub-element", "<child>abc</child>\n", printer.CStr(), false );
}
{
XMLDocument doc;
XMLError error = doc.LoadFile( "resources/empty.xml" );
XMLTest( "Loading an empty file", XML_ERROR_EMPTY_DOCUMENT, error );
XMLTest( "Loading an empty file and ErrorName as string", "XML_ERROR_EMPTY_DOCUMENT", doc.ErrorName() );
doc.PrintError();
}
{
// BOM preservation
static const char* xml_bom_preservation = "\xef\xbb\xbf<element/>\n";
{
XMLDocument doc;
XMLTest( "BOM preservation (parse)", XML_SUCCESS, doc.Parse( xml_bom_preservation ), false );
XMLPrinter printer;
doc.Print( &printer );
XMLTest( "BOM preservation (compare)", xml_bom_preservation, printer.CStr(), false, true );
doc.SaveFile( "resources/out/bomtest.xml" );
XMLTest( "Save bomtest.xml", false, doc.Error() );
}
{
XMLDocument doc;
doc.LoadFile( "resources/out/bomtest.xml" );
XMLTest( "Load bomtest.xml", false, doc.Error() );
XMLTest( "BOM preservation (load)", true, doc.HasBOM(), false );
XMLPrinter printer;
doc.Print( &printer );
XMLTest( "BOM preservation (compare)", xml_bom_preservation, printer.CStr(), false, true );
}
}
{
// Insertion with Removal
const char* xml = "<?xml version=\"1.0\" ?>"
"<root>"
"<one>"
"<subtree>"
"<elem>element 1</elem>text<!-- comment -->"
"</subtree>"
"</one>"
"<two/>"
"</root>";
const char* xmlInsideTwo = "<?xml version=\"1.0\" ?>"
"<root>"
"<one/>"
"<two>"
"<subtree>"
"<elem>element 1</elem>text<!-- comment -->"
"</subtree>"
"</two>"
"</root>";
const char* xmlAfterOne = "<?xml version=\"1.0\" ?>"
"<root>"
"<one/>"
"<subtree>"
"<elem>element 1</elem>text<!-- comment -->"
"</subtree>"
"<two/>"
"</root>";
const char* xmlAfterTwo = "<?xml version=\"1.0\" ?>"
"<root>"
"<one/>"
"<two/>"
"<subtree>"
"<elem>element 1</elem>text<!-- comment -->"
"</subtree>"
"</root>";
XMLDocument doc;
doc.Parse(xml);
XMLTest( "Insertion with removal parse round 1", false, doc.Error() );
XMLElement* subtree = doc.RootElement()->FirstChildElement("one")->FirstChildElement("subtree");
XMLElement* two = doc.RootElement()->FirstChildElement("two");
two->InsertFirstChild(subtree);
XMLPrinter printer1(0, true);
bool acceptResult = doc.Accept(&printer1);
XMLTest("Move node from within <one> to <two> - Accept()", true, acceptResult);
XMLTest("Move node from within <one> to <two>", xmlInsideTwo, printer1.CStr());
doc.Parse(xml);
XMLTest( "Insertion with removal parse round 2", false, doc.Error() );
subtree = doc.RootElement()->FirstChildElement("one")->FirstChildElement("subtree");
two = doc.RootElement()->FirstChildElement("two");
doc.RootElement()->InsertAfterChild(two, subtree);
XMLPrinter printer2(0, true);
acceptResult = doc.Accept(&printer2);
XMLTest("Move node from within <one> after <two> - Accept()", true, acceptResult);
XMLTest("Move node from within <one> after <two>", xmlAfterTwo, printer2.CStr(), false);
doc.Parse(xml);
XMLTest( "Insertion with removal parse round 3", false, doc.Error() );
XMLNode* one = doc.RootElement()->FirstChildElement("one");
subtree = one->FirstChildElement("subtree");
doc.RootElement()->InsertAfterChild(one, subtree);
XMLPrinter printer3(0, true);
acceptResult = doc.Accept(&printer3);
XMLTest("Move node from within <one> after <one> - Accept()", true, acceptResult);
XMLTest("Move node from within <one> after <one>", xmlAfterOne, printer3.CStr(), false);
doc.Parse(xml);
XMLTest( "Insertion with removal parse round 4", false, doc.Error() );
subtree = doc.RootElement()->FirstChildElement("one")->FirstChildElement("subtree");
two = doc.RootElement()->FirstChildElement("two");
XMLTest("<two> is the last child at root level", true, two == doc.RootElement()->LastChildElement());
doc.RootElement()->InsertEndChild(subtree);
XMLPrinter printer4(0, true);
acceptResult = doc.Accept(&printer4);
XMLTest("Move node from within <one> after <two> - Accept()", true, acceptResult);
XMLTest("Move node from within <one> after <two>", xmlAfterTwo, printer4.CStr(), false);
}
{
const char* xml = "<svg width = \"128\" height = \"128\">"
" <text> </text>"
"</svg>";
XMLDocument doc;
doc.Parse(xml);
XMLTest( "Parse svg with text", false, doc.Error() );
doc.Print();
}
{
// Test that it doesn't crash.
const char* xml = "<?xml version=\"1.0\"?><root><sample><field0><1</field0><field1>2</field1></sample></root>";
XMLDocument doc;
doc.Parse(xml);
XMLTest( "Parse root-sample-field0", true, doc.Error() );
doc.PrintError();
}
#if 1
// the question being explored is what kind of print to use:
// https://github.com/leethomason/tinyxml2/issues/63
{
//const char* xml = "<element attrA='123456789.123456789' attrB='1.001e9' attrC='1.0e-10' attrD='1001000000.000000' attrE='0.1234567890123456789'/>";
const char* xml = "<element/>";
XMLDocument doc;
doc.Parse( xml );
XMLTest( "Parse self-closed empty element", false, doc.Error() );
doc.FirstChildElement()->SetAttribute( "attrA-f64", 123456789.123456789 );
doc.FirstChildElement()->SetAttribute( "attrB-f64", 1.001e9 );
doc.FirstChildElement()->SetAttribute( "attrC-f64", 1.0e9 );
doc.FirstChildElement()->SetAttribute( "attrC-f64", 1.0e20 );
doc.FirstChildElement()->SetAttribute( "attrD-f64", 1.0e-10 );
doc.FirstChildElement()->SetAttribute( "attrD-f64", 0.123456789 );
doc.FirstChildElement()->SetAttribute( "attrA-f32", 123456789.123456789f );
doc.FirstChildElement()->SetAttribute( "attrB-f32", 1.001e9f );
doc.FirstChildElement()->SetAttribute( "attrC-f32", 1.0e9f );
doc.FirstChildElement()->SetAttribute( "attrC-f32", 1.0e20f );
doc.FirstChildElement()->SetAttribute( "attrD-f32", 1.0e-10f );
doc.FirstChildElement()->SetAttribute( "attrD-f32", 0.123456789f );
doc.Print();
/* The result of this test is platform, compiler, and library version dependent. :("
XMLPrinter printer;
doc.Print( &printer );
XMLTest( "Float and double formatting.",
"<element attrA-f64=\"123456789.12345679\" attrB-f64=\"1001000000\" attrC-f64=\"1e+20\" attrD-f64=\"0.123456789\" attrA-f32=\"1.2345679e+08\" attrB-f32=\"1.001e+09\" attrC-f32=\"1e+20\" attrD-f32=\"0.12345679\"/>\n",
printer.CStr(),
true );
*/
}
#endif
{
// Issue #184
// If it doesn't assert, it passes. Caused by objects
// getting created during parsing which are then
// inaccessible in the memory pools.
const char* xmlText = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><test>";
{
XMLDocument doc;
doc.Parse(xmlText);
XMLTest( "Parse hex no closing tag round 1", true, doc.Error() );
}
{
XMLDocument doc;
doc.Parse(xmlText);
XMLTest( "Parse hex no closing tag round 2", true, doc.Error() );
doc.Clear();
}
}
{
// If this doesn't assert in TINYXML2_DEBUG, all is well.
tinyxml2::XMLDocument doc;
tinyxml2::XMLElement *pRoot = doc.NewElement("Root");
doc.DeleteNode(pRoot);
}
{
XMLDocument doc;
XMLElement* root = doc.NewElement( "Root" );
XMLTest( "Node document before insertion", true, &doc == root->GetDocument() );
doc.InsertEndChild( root );
XMLTest( "Node document after insertion", true, &doc == root->GetDocument() );
}
{
// If this doesn't assert in TINYXML2_DEBUG, all is well.
XMLDocument doc;
XMLElement* unlinkedRoot = doc.NewElement( "Root" );
XMLElement* linkedRoot = doc.NewElement( "Root" );
doc.InsertFirstChild( linkedRoot );
unlinkedRoot->GetDocument()->DeleteNode( linkedRoot );
unlinkedRoot->GetDocument()->DeleteNode( unlinkedRoot );
}
{
// Should not assert in TINYXML2_DEBUG
XMLPrinter printer;
}
{
// Issue 291. Should not crash
const char* xml = "�</a>";
XMLDocument doc;
doc.Parse( xml );
XMLTest( "Parse hex with closing tag", false, doc.Error() );
XMLPrinter printer;
doc.Print( &printer );
}
{
// Issue 299. Can print elements that are not linked in.
// Will crash if issue not fixed.
XMLDocument doc;
XMLElement* newElement = doc.NewElement( "printme" );
XMLPrinter printer;
bool acceptResult = newElement->Accept( &printer );
XMLTest( "printme - Accept()", true, acceptResult );
// Delete the node to avoid possible memory leak report in debug output
doc.DeleteNode( newElement );
}
{
// Issue 302. Clear errors from LoadFile/SaveFile
XMLDocument doc;
XMLTest( "Issue 302. Should be no error initially", "XML_SUCCESS", doc.ErrorName() );
doc.SaveFile( "./no/such/path/pretty.xml" );
XMLTest( "Issue 302. Fail to save", "XML_ERROR_FILE_COULD_NOT_BE_OPENED", doc.ErrorName() );
doc.SaveFile( "./resources/out/compact.xml", true );
XMLTest( "Issue 302. Subsequent success in saving", "XML_SUCCESS", doc.ErrorName() );
}
{
// If a document fails to load then subsequent
// successful loads should clear the error
XMLDocument doc;
XMLTest( "Should be no error initially", false, doc.Error() );
doc.LoadFile( "resources/no-such-file.xml" );
XMLTest( "No such file - should fail", true, doc.Error() );
doc.LoadFile("resources/dream.xml");
XMLTest("Error should be cleared", false, doc.Error());
doc.LoadFile( "resources/xmltest-5330.xml" );
XMLTest( "parse errors occur - should fail", true, doc.Error() );
doc.LoadFile( "resources/dream.xml" );
XMLTest( "Error should be cleared", false, doc.Error() );
}
{
// Check that declarations are allowed only at beginning of document
const char* xml0 = "<?xml version=\"1.0\" ?>"
" <!-- xml version=\"1.1\" -->"
"<first />";
const char* xml1 = "<?xml version=\"1.0\" ?>"
"<?xml-stylesheet type=\"text/xsl\" href=\"Anything.xsl\"?>"
"<first />";
const char* xml2 = "<first />"
"<?xml version=\"1.0\" ?>";
const char* xml3 = "<first></first>"
"<?xml version=\"1.0\" ?>";
const char* xml4 = "<first><?xml version=\"1.0\" ?></first>";
XMLDocument doc;
doc.Parse(xml0);
XMLTest("Test that the code changes do not affect normal parsing", false, doc.Error() );
doc.Parse(xml1);
XMLTest("Test that the second declaration is allowed", false, doc.Error() );
doc.Parse(xml2);
XMLTest("Test that declaration after self-closed child is not allowed", XML_ERROR_PARSING_DECLARATION, doc.ErrorID() );
doc.Parse(xml3);
XMLTest("Test that declaration after a child is not allowed", XML_ERROR_PARSING_DECLARATION, doc.ErrorID() );
doc.Parse(xml4);
XMLTest("Test that declaration inside a child is not allowed", XML_ERROR_PARSING_DECLARATION, doc.ErrorID() );
}
{
// No matter - before or after successfully parsing a text -
// calling XMLDocument::Value() used to cause an assert in debug.
// Null must be returned.
const char* validXml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>"
"<first />"
"<second />";
XMLDocument* doc = new XMLDocument();
XMLTest( "XMLDocument::Value() returns null?", NULL, doc->Value() );
doc->Parse( validXml );
XMLTest( "Parse to test XMLDocument::Value()", false, doc->Error());
XMLTest( "XMLDocument::Value() returns null?", NULL, doc->Value() );
delete doc;
}
{
XMLDocument doc;
for( int i = 0; i < XML_ERROR_COUNT; i++ ) {
const XMLError error = static_cast<XMLError>(i);
const char* name = XMLDocument::ErrorIDToName(error);
XMLTest( "ErrorName() not null after ClearError()", true, name != 0 );
if( name == 0 ) {
// passing null pointer into strlen() is undefined behavior, so
// compiler is allowed to optimise away the null test above if it's
// as reachable as the strlen() call
continue;
}
XMLTest( "ErrorName() not empty after ClearError()", true, strlen(name) > 0 );
}
}
{
const char* html("<!DOCTYPE html><html><body><p>test</p><p><br/></p></body></html>");
XMLDocument doc(false);
doc.Parse(html);
XMLPrinter printer(0, true);
doc.Print(&printer);
XMLTest(html, html, printer.CStr());
}
{
// Evil memory leaks.
// If an XMLElement (etc) is allocated via NewElement() (etc.)
// and NOT added to the XMLDocument, what happens?
//
// Previously (buggy):
// The memory would be free'd when the XMLDocument is
// destructed. But the XMLElement destructor wasn't called, so
// memory allocated for the XMLElement text would not be free'd.
// In practice this meant strings allocated for the XMLElement
// text would be leaked. An edge case, but annoying.
// Now:
// The XMLElement destructor is called. But the unlinked nodes
// have to be tracked using a list. This has a minor performance
// impact that can become significant if you have a lot of
// unlinked nodes. (But why would you do that?)
// The only way to see this bug was in a Visual C++ runtime debug heap
// leak tracker. This is compiled in by default on Windows Debug and
// enabled with _CRTDBG_LEAK_CHECK_DF parameter passed to _CrtSetDbgFlag().
{
XMLDocument doc;
doc.NewElement("LEAK 1");
}
{
XMLDocument doc;
XMLElement* ele = doc.NewElement("LEAK 2");
doc.DeleteNode(ele);
}
}
{
// Bad bad crash. Parsing error results in stack overflow, if uncaught.
const char* TESTS[] = {
"./resources/xmltest-5330.xml",
"./resources/xmltest-4636783552757760.xml",
"./resources/xmltest-5720541257269248.xml",
0
};
for (int i=0; TESTS[i]; ++i) {
XMLDocument doc;
doc.LoadFile(TESTS[i]);
XMLTest("Stack overflow prevented.", XML_ELEMENT_DEPTH_EXCEEDED, doc.ErrorID());
}
}
{
const char* TESTS[] = {
"./resources/xmltest-5662204197076992.xml", // Security-level performance issue.
0
};
for (int i = 0; TESTS[i]; ++i) {
XMLDocument doc;
doc.LoadFile(TESTS[i]);
// Need only not crash / lock up.
XMLTest("Fuzz attack prevented.", true, true);
}
}
{
// Crashing reported via email.
const char* xml =
"<playlist id='playlist1'>"
"<property name='track_name'>voice</property>"
"<property name='audio_track'>1</property>"
"<entry out = '604' producer = '4_playlist1' in = '0' />"
"<blank length = '1' />"
"<entry out = '1625' producer = '3_playlist' in = '0' />"
"<blank length = '2' />"
"<entry out = '946' producer = '2_playlist1' in = '0' />"
"<blank length = '1' />"
"<entry out = '128' producer = '1_playlist1' in = '0' />"
"</playlist>";
// It's not a good idea to delete elements as you walk the
// list. I'm not sure this technically should work; but it's
// an interesting test case.
XMLDocument doc;
XMLError err = doc.Parse(xml);
XMLTest("Crash bug parsing", XML_SUCCESS, err );
XMLElement* playlist = doc.FirstChildElement("playlist");
XMLTest("Crash bug parsing", true, playlist != 0);
{
const char* elementName = "entry";
XMLElement* entry = playlist->FirstChildElement(elementName);
XMLTest("Crash bug parsing", true, entry != 0);
while (entry) {
XMLElement* todelete = entry;
entry = entry->NextSiblingElement(elementName);
playlist->DeleteChild(todelete);
}
entry = playlist->FirstChildElement(elementName);
XMLTest("Crash bug parsing", true, entry == 0);
}
{
const char* elementName = "blank";
XMLElement* blank = playlist->FirstChildElement(elementName);
XMLTest("Crash bug parsing", true, blank != 0);
while (blank) {
XMLElement* todelete = blank;
blank = blank->NextSiblingElement(elementName);
playlist->DeleteChild(todelete);
}
XMLTest("Crash bug parsing", true, blank == 0);
}
tinyxml2::XMLPrinter printer;
const bool acceptResult = playlist->Accept(&printer);
XMLTest("Crash bug parsing - Accept()", true, acceptResult);
printf("%s\n", printer.CStr());
// No test; it only need to not crash.
// Still, wrap it up with a sanity check
int nProperty = 0;
for (const XMLElement* p = playlist->FirstChildElement("property"); p; p = p->NextSiblingElement("property")) {
nProperty++;
}
XMLTest("Crash bug parsing", 2, nProperty);
}
// ----------- Line Number Tracking --------------
{
struct TestUtil: XMLVisitor
{
TestUtil() : str() {}
void TestParseError(const char *testString, const char *docStr, XMLError expected_error, int expectedLine)
{
XMLDocument doc;
const XMLError parseError = doc.Parse(docStr);
XMLTest(testString, parseError, doc.ErrorID());
XMLTest(testString, true, doc.Error());
XMLTest(testString, expected_error, parseError);
XMLTest(testString, expectedLine, doc.ErrorLineNum());
};
void TestStringLines(const char *testString, const char *docStr, const char *expectedLines)
{
XMLDocument doc;
doc.Parse(docStr);
XMLTest(testString, false, doc.Error());
TestDocLines(testString, doc, expectedLines);
}
void TestFileLines(const char *testString, const char *file_name, const char *expectedLines)
{
XMLDocument doc;
doc.LoadFile(file_name);
XMLTest(testString, false, doc.Error());
TestDocLines(testString, doc, expectedLines);
}
private:
DynArray<char, 10> str;
void Push(char type, int lineNum)
{
str.Push(type);
str.Push(char('0' + (lineNum / 10)));
str.Push(char('0' + (lineNum % 10)));
}
bool VisitEnter(const XMLDocument& doc)
{
Push('D', doc.GetLineNum());
return true;
}
bool VisitEnter(const XMLElement& element, const XMLAttribute* firstAttribute)
{
Push('E', element.GetLineNum());
for (const XMLAttribute *attr = firstAttribute; attr != 0; attr = attr->Next())
Push('A', attr->GetLineNum());
return true;
}
bool Visit(const XMLDeclaration& declaration)
{
Push('L', declaration.GetLineNum());
return true;
}
bool Visit(const XMLText& text)
{
Push('T', text.GetLineNum());
return true;
}
bool Visit(const XMLComment& comment)
{
Push('C', comment.GetLineNum());
return true;
}
bool Visit(const XMLUnknown& unknown)
{
Push('U', unknown.GetLineNum());
return true;
}
void TestDocLines(const char *testString, XMLDocument &doc, const char *expectedLines)
{
str.Clear();
const bool acceptResult = doc.Accept(this);
XMLTest(testString, true, acceptResult);
str.Push(0);
XMLTest(testString, expectedLines, str.Mem());
}
} tester;
tester.TestParseError("ErrorLine-Parsing", "\n<root>\n foo \n<unclosed/>", XML_ERROR_PARSING, 2);
tester.TestParseError("ErrorLine-Declaration", "<root>\n<?xml version=\"1.0\"?>", XML_ERROR_PARSING_DECLARATION, 2);
tester.TestParseError("ErrorLine-Mismatch", "\n<root>\n</mismatch>", XML_ERROR_MISMATCHED_ELEMENT, 2);
tester.TestParseError("ErrorLine-CData", "\n<root><![CDATA[ \n foo bar \n", XML_ERROR_PARSING_CDATA, 2);
tester.TestParseError("ErrorLine-Text", "\n<root>\n foo bar \n", XML_ERROR_PARSING_TEXT, 3);
tester.TestParseError("ErrorLine-Comment", "\n<root>\n<!-- >\n", XML_ERROR_PARSING_COMMENT, 3);
tester.TestParseError("ErrorLine-Declaration", "\n<root>\n<? >\n", XML_ERROR_PARSING_DECLARATION, 3);
tester.TestParseError("ErrorLine-Unknown", "\n<root>\n<! \n", XML_ERROR_PARSING_UNKNOWN, 3);
tester.TestParseError("ErrorLine-Element", "\n<root>\n<unclosed \n", XML_ERROR_PARSING_ELEMENT, 3);
tester.TestParseError("ErrorLine-Attribute", "\n<root>\n<unclosed \n att\n", XML_ERROR_PARSING_ATTRIBUTE, 4);
tester.TestParseError("ErrorLine-ElementClose", "\n<root>\n<unclosed \n/unexpected", XML_ERROR_PARSING_ELEMENT, 3);
tester.TestStringLines(
"LineNumbers-String",
"<?xml version=\"1.0\"?>\n" // 1 Doc, DecL
"<root a='b' \n" // 2 Element Attribute
"c='d'> d <blah/> \n" // 3 Attribute Text Element
"newline in text \n" // 4 Text
"and second <zxcv/><![CDATA[\n" // 5 Element Text
" cdata test ]]><!-- comment -->\n" // 6 Comment
"<! unknown></root>", // 7 Unknown
"D01L01E02A02A03T03E03T04E05T05C06U07");
tester.TestStringLines(
"LineNumbers-CRLF",
"\r\n" // 1 Doc (arguably should be line 2)
"<?xml version=\"1.0\"?>\n" // 2 DecL
"<root>\r\n" // 3 Element
"\n" // 4
"text contining new line \n" // 5 Text
" and also containing crlf \r\n" // 6
"<sub><![CDATA[\n" // 7 Element Text
"cdata containing new line \n" // 8
" and also containing cflr\r\n" // 9
"]]></sub><sub2/></root>", // 10 Element
"D01L02E03T05E07T07E10");
tester.TestFileLines(
"LineNumbers-File",
"resources/utf8test.xml",
"D01L01E02E03A03A03T03E04A04A04T04E05A05A05T05E06A06A06T06E07A07A07T07E08A08A08T08E09T09E10T10");
}
{
const char* xml = "<Hello>Text</Error>";
XMLDocument doc;
doc.Parse(xml);
XMLTest("Test mismatched elements.", true, doc.Error());
XMLTest("Test mismatched elements.", XML_ERROR_MISMATCHED_ELEMENT, doc.ErrorID());
// For now just make sure calls work & doesn't crash.
// May solidify the error output in the future.
printf("%s\n", doc.ErrorStr());
doc.PrintError();
}
// ----------- Performance tracking --------------
{
#if defined( _MSC_VER )
__int64 start, end, freq;
QueryPerformanceFrequency((LARGE_INTEGER*)&freq);
#endif
FILE* perfFP = fopen("resources/dream.xml", "r");
XMLTest("Open dream.xml", true, perfFP != 0);
fseek(perfFP, 0, SEEK_END);
long size = ftell(perfFP);
fseek(perfFP, 0, SEEK_SET);
char* mem = new char[size + 1];
memset(mem, 0xfe, size);
size_t bytesRead = fread(mem, 1, size, perfFP);
XMLTest("Read dream.xml", true, uint32_t(size) >= uint32_t(bytesRead));
fclose(perfFP);
mem[size] = 0;
#if defined( _MSC_VER )
QueryPerformanceCounter((LARGE_INTEGER*)&start);
#else
clock_t cstart = clock();
#endif
bool parseDreamXmlFailed = false;
static const int COUNT = 10;
for (int i = 0; i < COUNT; ++i) {
XMLDocument doc;
doc.Parse(mem);
parseDreamXmlFailed = parseDreamXmlFailed || doc.Error();
}
#if defined( _MSC_VER )
QueryPerformanceCounter((LARGE_INTEGER*)&end);
#else
clock_t cend = clock();
#endif
XMLTest( "Parse dream.xml", false, parseDreamXmlFailed );
delete[] mem;
static const char* note =
#ifdef TINYXML2_DEBUG
"DEBUG";
#else
"Release";
#endif
#if defined( _MSC_VER )
const double duration = 1000.0 * (double)(end - start) / ((double)freq * (double)COUNT);
#else
const double duration = (double)(cend - cstart) / (double)COUNT;
#endif
printf("\nParsing dream.xml (%s): %.3f milli-seconds\n", note, duration);
}
#if defined( _MSC_VER ) && defined( TINYXML2_DEBUG )
{
_CrtMemCheckpoint( &endMemState );
_CrtMemState diffMemState;
_CrtMemDifference( &diffMemState, &startMemState, &endMemState );
_CrtMemDumpStatistics( &diffMemState );
{
int leaksBeforeExit = _CrtDumpMemoryLeaks();
XMLTest( "No leaks before exit?", FALSE, leaksBeforeExit );
}
}
#endif
printf ("\nPass %d, Fail %d\n", gPass, gFail);
return gFail;
}
| cpp |
tinyxml2 | data/projects/tinyxml2/contrib/html5-printer.cpp | // g++ -Wall -O2 contrib/html5-printer.cpp -o html5-printer -ltinyxml2
// This program demonstrates how to use "tinyxml2" to generate conformant HTML5
// by deriving from the "tinyxml2::XMLPrinter" class.
// http://dev.w3.org/html5/markup/syntax.html
// In HTML5, there are 16 so-called "void" elements. "void elements" NEVER have
// inner content (but they MAY have attributes), and are assumed to be self-closing.
// An example of a self-closig HTML5 element is "<br/>" (line break)
// All other elements are called "non-void" and MUST never self-close.
// Examples: "<div class='lolcats'></div>".
// tinyxml2::XMLPrinter will emit _ALL_ XML elements with no inner content as
// self-closing. This behavior produces space-effeceint XML, but incorrect HTML5.
// Author: Dennis Jenkins, dennis (dot) jenkins (dot) 75 (at) gmail (dot) com.
// License: Same as tinyxml2 (zlib, see below)
// This example is a small contribution to the world! Enjoy it!
/*
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any
damages arising from the use of this software.
Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it and
redistribute it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must
not claim that you wrote the original software. If you use this
software in a product, an acknowledgment in the product documentation
would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
#include "../tinyxml2.h"
#include <iostream>
#if defined (_MSC_VER)
#define strcasecmp stricmp
#endif
using namespace tinyxml2;
// Contrived input containing a mix of void and non-void HTML5 elements.
// When printed via XMLPrinter, some non-void elements will self-close (not valid HTML5).
static const char input[] =
"<html><body><p style='a'></p><br/>©<col a='1' b='2'/><div a='1'></div></body></html>";
// XMLPrinterHTML5 is small enough, just put the entire implementation inline.
class XMLPrinterHTML5 : public XMLPrinter
{
public:
XMLPrinterHTML5 (FILE* file=0, bool compact = false, int depth = 0) :
XMLPrinter (file, compact, depth)
{}
protected:
virtual void CloseElement () {
if (_elementJustOpened && !isVoidElement (_stack.PeekTop())) {
SealElementIfJustOpened();
}
XMLPrinter::CloseElement();
}
virtual bool isVoidElement (const char *name) {
// Complete list of all HTML5 "void elements",
// http://dev.w3.org/html5/markup/syntax.html
static const char *list[] = {
"area", "base", "br", "col", "command", "embed", "hr", "img",
"input", "keygen", "link", "meta", "param", "source", "track", "wbr",
NULL
};
// I could use 'bsearch', but I don't have MSVC to test on (it would work with gcc/libc).
for (const char **p = list; *p; ++p) {
if (!strcasecmp (name, *p)) {
return true;
}
}
return false;
}
};
int main (void) {
XMLDocument doc (false);
doc.Parse (input);
std::cout << "INPUT:\n" << input << "\n\n";
XMLPrinter prn (NULL, true);
doc.Print (&prn);
std::cout << "XMLPrinter (not valid HTML5):\n" << prn.CStr() << "\n\n";
XMLPrinterHTML5 html5 (NULL, true);
doc.Print (&html5);
std::cout << "XMLPrinterHTML5:\n" << html5.CStr() << "\n";
return 0;
}
| cpp |
inifile-cpp | data/projects/inifile-cpp/examples/save_ini_file.cpp | /* save_ini_file.cpp
*
* Author: Fabian Meyer
* Created On: 14 Nov 2020
*/
#include <inicpp.h>
#include <iostream>
int main(int argc, char **argv)
{
if(argc != 2)
{
std::cerr << "usage: save_ini_file [FILE_PATh]" << std::endl;
return 1;
}
std::string path = argv[1];
ini::IniFile inif;
inif["Foo"]["hello"] = "world";
inif["Foo"]["float"] = 1.02f;
inif["Foo"]["int"] = 123;
inif["Another"]["char"] = 'q';
inif["Another"]["bool"] = true;
inif.save(path);
std::cout << "Saved ini file." << std::endl;
return 0;
} | cpp |
inifile-cpp | data/projects/inifile-cpp/examples/load_ini_file.cpp | /* load_ini_file.cpp
*
* Author: Fabian Meyer
* Created On: 14 Nov 2020
*/
#include <inicpp.h>
#include <iostream>
int main(int argc, char **argv)
{
if(argc != 2)
{
std::cerr << "usage: load_ini_file [FILE_PATh]" << std::endl;
return 1;
}
std::string path = argv[1];
// load the file
ini::IniFile inif;
inif.load(path);
// show the parsed contents of the ini file
std::cout << "Parsed ini contents" << std::endl;
std::cout << "Has " << inif.size() << " sections" << std::endl;
for(const auto §ionPair : inif)
{
const std::string §ionName = sectionPair.first;
const ini::IniSection §ion = sectionPair.second;
std::cout << "Section '" << sectionName << "' has " << section.size() << " fields" << std::endl;
for(const auto &fieldPair : sectionPair.second)
{
const std::string &fieldName = fieldPair.first;
const ini::IniField &field = fieldPair.second;
std::cout << " Field '" << fieldName << "' Value '" << field.as<std::string>() << "'" << std::endl;
}
}
return 0;
} | cpp |
inifile-cpp | data/projects/inifile-cpp/examples/decode_ini_file.cpp | /* decode_ini_file.cpp
*
* Author: Fabian Meyer
* Created On: 14 Nov 2020
*/
#include <inicpp.h>
#include <iostream>
int main()
{
// create some ini content
std::string content = "[Foo]\nhello=world\nnum=123\n[Test]\nstatus=pass\n[Nothing]";
ini::IniFile inif;
inif.decode(content);
// show the parsed contents of the ini file
std::cout << "Parsed ini contents" << std::endl;
std::cout << "Has " << inif.size() << " sections" << std::endl;
for(const auto §ionPair : inif)
{
const std::string §ionName = sectionPair.first;
const ini::IniSection §ion = sectionPair.second;
std::cout << "Section '" << sectionName << "' has " << section.size() << " fields" << std::endl;
for(const auto &fieldPair : sectionPair.second)
{
const std::string &fieldName = fieldPair.first;
const ini::IniField &field = fieldPair.second;
std::cout << " Field '" << fieldName << "' Value '" << field.as<std::string>() << "'" << std::endl;
}
}
return 0;
} | cpp |
inifile-cpp | data/projects/inifile-cpp/examples/encode_ini_file.cpp | /* encode_ini_file.cpp
*
* Author: Fabian Meyer
* Created On: 14 Nov 2020
*/
#include <inicpp.h>
#include <iostream>
int main()
{
ini::IniFile inif;
inif["Foo"]["hello"] = "world";
inif["Foo"]["float"] = 1.02f;
inif["Foo"]["int"] = 123;
inif["Another"]["char"] = 'q';
inif["Another"]["bool"] = true;
std::string content = inif.encode();
std::cout << "Encoded ini file" << std::endl;
std::cout << content << std::endl;
return 0;
} | cpp |
inifile-cpp | data/projects/inifile-cpp/examples/custom_type_conversion.cpp |
/* decode_ini_file.cpp
*
* Author: Fabian Meyer
* Created On: 14 Nov 2020
*/
#include <inicpp.h>
#include <iostream>
// the conversion functor must live in the "ini" namespace
namespace ini
{
/** Conversion functor to parse std::vectors from an ini field-
* The generic template can be passed down to the vector. */
template<typename T>
struct Convert<std::vector<T>>
{
/** Decodes a std::vector from a string. */
void decode(const std::string &value, std::vector<T> &result)
{
result.clear();
// variable to store the decoded value of each element
T decoded;
// maintain a start and end pos within the string
size_t startPos = 0;
size_t endPos = 0;
size_t cnt;
while(endPos != std::string::npos)
{
if(endPos != 0)
startPos = endPos + 1;
// search for the next comma as separator
endPos = value.find(',', startPos);
// if no comma was found use the rest of the string
// as input
if(endPos == std::string::npos)
cnt = value.size() - startPos;
else
cnt = endPos - startPos;
std::string tmp = value.substr(startPos, cnt);
// use the conversion functor for the type contained in
// the vector, so the vector can use any type that
// is compatible with inifile-cpp
Convert<T> conv;
conv.decode(tmp, decoded);
result.push_back(decoded);
}
}
/** Encodes a std::vector to a string. */
void encode(const std::vector<T> &value, std::string &result)
{
// variable to store the encoded element value
std::string encoded;
// string stream to build the result stream
std::stringstream ss;
for(size_t i = 0; i < value.size(); ++i)
{
// use the conversion functor for the type contained in
// the vector, so the vector can use any type that
// is compatible with inifile-cp
Convert<T> conv;
conv.encode(value[i], encoded);
ss << encoded;
// if this is not the last element add a comma as separator
if(i != value.size() - 1)
ss << ',';
}
// store the created string in the result
result = ss.str();
}
};
}
int main()
{
// create some ini content that we can parse
std::string content = "[Foo]\nintList=1,2,3,4,5,6,7,8\ndoubleList=3.4,1.2,2.2,4.7";
// decode the ini contents
ini::IniFile inputIni;
inputIni.decode(content);
// print the results
std::cout << "Parsed ini file" << std::endl;
std::cout << "===============" << std::endl;
// parse the int list
std::vector<int> intList = inputIni["Foo"]["intList"].as<std::vector<int>>();
std::cout << "int list:" << std::endl;
for(size_t i = 0; i < intList.size(); ++i)
std::cout << " " << intList[i] << std::endl;
// parse the double list
std::vector<double> doubleList = inputIni["Foo"]["doubleList"].as<std::vector<double>>();
std::cout << "double list:" << std::endl;
for(size_t i = 0; i < doubleList.size(); ++i)
std::cout << " " << doubleList[i] << std::endl;
std::cout << std::endl;
// create another ini file for encoding
ini::IniFile outputIni;
outputIni["Bar"]["floatList"] =std::vector<float>{1.0f, 9.3f, 3.256f};
outputIni["Bar"]["boolList"] =std::vector<bool>{true, false, false, true};
std::cout << "Encoded ini file" << std::endl;
std::cout << "================" << std::endl;
std::cout << outputIni.encode() << std::endl;
return 0;
} | cpp |
inifile-cpp | data/projects/inifile-cpp/test/main.cpp | /*
* main.cpp
*
* Created on: 26 Dec 2015
* Author: Fabian Meyer
* License: MIT
*/
#define CATCH_CONFIG_MAIN
#include <catch2/catch.hpp>
| cpp |
inifile-cpp | data/projects/inifile-cpp/test/test_inifile.cpp | /*
* test_inifile.cpp
*
* Created on: 26 Dec 2015
* Author: Fabian Meyer
* License: MIT
*/
#include "inicpp.h"
#include <catch2/catch.hpp>
#include <cstring>
#include <sstream>
TEST_CASE("parse ini file", "IniFile")
{
std::istringstream ss(("[Foo]\nbar=hello world\n[Test]"));
ini::IniFile inif(ss);
REQUIRE(inif.size() == 2);
REQUIRE(inif["Foo"]["bar"].as<std::string>() == "hello world");
REQUIRE(inif["Test"].size() == 0);
}
TEST_CASE("parse empty file", "IniFile")
{
std::istringstream ss("");
ini::IniFile inif(ss);
REQUIRE(inif.size() == 0);
}
TEST_CASE("parse comment only file", "IniFile")
{
std::istringstream ss("# this is a comment");
ini::IniFile inif(ss);
REQUIRE(inif.size() == 0);
}
TEST_CASE("parse empty section", "IniFile")
{
std::istringstream ss("[Foo]");
ini::IniFile inif(ss);
REQUIRE(inif.size() == 1);
REQUIRE(inif["Foo"].size() == 0);
}
TEST_CASE("parse empty field", "IniFile")
{
std::istringstream ss("[Foo]\nbar=");
ini::IniFile inif(ss);
REQUIRE(inif.size() == 1);
REQUIRE(inif["Foo"].size() == 1);
REQUIRE(inif["Foo"]["bar"].as<std::string>() == "");
}
TEST_CASE("parse section with duplicate field", "IniFile")
{
std::istringstream ss("[Foo]\nbar=hello\nbar=world");
ini::IniFile inif(ss);
REQUIRE(inif.size() == 1);
REQUIRE(inif["Foo"].size() == 1);
REQUIRE(inif["Foo"]["bar"].as<std::string>() == "world");
}
TEST_CASE("parse section with duplicate field and overwriteDuplicateFields_ set to true", "IniFile")
{
ini::IniFile inif;
inif.allowOverwriteDuplicateFields(true);
inif.decode("[Foo]\nbar=hello\nbar=world");
REQUIRE(inif.size() == 1);
REQUIRE(inif["Foo"].size() == 1);
REQUIRE(inif["Foo"]["bar"].as<std::string>() == "world");
}
TEST_CASE("parse field as bool", "IniFile")
{
std::istringstream ss("[Foo]\nbar1=true\nbar2=false\nbar3=tRuE");
ini::IniFile inif(ss);
REQUIRE(inif.size() == 1);
REQUIRE(inif["Foo"].size() == 3);
REQUIRE(inif["Foo"]["bar1"].as<bool>());
REQUIRE_FALSE(inif["Foo"]["bar2"].as<bool>());
REQUIRE(inif["Foo"]["bar3"].as<bool>());
}
TEST_CASE("parse field as char", "IniFile")
{
std::istringstream ss("[Foo]\nbar1=c\nbar2=q");
ini::IniFile inif(ss);
REQUIRE(inif.size() == 1);
REQUIRE(inif["Foo"].size() == 2);
REQUIRE(inif["Foo"]["bar1"].as<char>() == 'c');
REQUIRE(inif["Foo"]["bar2"].as<char>() == 'q');
}
TEST_CASE("parse field as unsigned char", "IniFile")
{
std::istringstream ss("[Foo]\nbar1=c\nbar2=q");
ini::IniFile inif(ss);
REQUIRE(inif.size() == 1);
REQUIRE(inif["Foo"].size() == 2);
REQUIRE(inif["Foo"]["bar1"].as<unsigned char>() == 'c');
REQUIRE(inif["Foo"]["bar2"].as<unsigned char>() == 'q');
}
TEST_CASE("parse field as short", "IniFile")
{
std::istringstream ss("[Foo]\nbar1=1\nbar2=-2");
ini::IniFile inif(ss);
REQUIRE(inif.size() == 1);
REQUIRE(inif["Foo"].size() == 2);
REQUIRE(inif["Foo"]["bar1"].as<short>() == 1);
REQUIRE(inif["Foo"]["bar2"].as<short>() == -2);
}
TEST_CASE("parse field as unsigned short", "IniFile")
{
std::istringstream ss("[Foo]\nbar1=1\nbar2=13");
ini::IniFile inif(ss);
REQUIRE(inif.size() == 1);
REQUIRE(inif["Foo"].size() == 2);
REQUIRE(inif["Foo"]["bar1"].as<unsigned short>() == 1);
REQUIRE(inif["Foo"]["bar2"].as<unsigned short>() == 13);
}
TEST_CASE("parse field as int", "IniFile")
{
std::istringstream ss("[Foo]\nbar1=1\nbar2=-2");
ini::IniFile inif(ss);
REQUIRE(inif.size() == 1);
REQUIRE(inif["Foo"].size() == 2);
REQUIRE(inif["Foo"]["bar1"].as<int>() == 1);
REQUIRE(inif["Foo"]["bar2"].as<int>() == -2);
}
TEST_CASE("parse field as unsigned int", "IniFile")
{
std::istringstream ss("[Foo]\nbar1=1\nbar2=13");
ini::IniFile inif(ss);
REQUIRE(inif.size() == 1);
REQUIRE(inif["Foo"].size() == 2);
REQUIRE(inif["Foo"]["bar1"].as<unsigned int>() == 1);
REQUIRE(inif["Foo"]["bar2"].as<unsigned int>() == 13);
}
TEST_CASE("parse field as long", "IniFile")
{
std::istringstream ss("[Foo]\nbar1=1\nbar2=-2");
ini::IniFile inif(ss);
REQUIRE(inif.size() == 1);
REQUIRE(inif["Foo"].size() == 2);
REQUIRE(inif["Foo"]["bar1"].as<long>() == 1);
REQUIRE(inif["Foo"]["bar2"].as<long>() == -2);
}
TEST_CASE("parse field as unsigned long", "IniFile")
{
std::istringstream ss("[Foo]\nbar1=1\nbar2=13");
ini::IniFile inif(ss);
REQUIRE(inif.size() == 1);
REQUIRE(inif["Foo"].size() == 2);
REQUIRE(inif["Foo"]["bar1"].as<unsigned long>() == 1);
REQUIRE(inif["Foo"]["bar2"].as<unsigned long>() == 13);
}
TEST_CASE("parse field as double", "IniFile")
{
std::istringstream ss("[Foo]\nbar1=1.2\nbar2=1\nbar3=-2.4");
ini::IniFile inif(ss);
REQUIRE(inif.size() == 1);
REQUIRE(inif["Foo"].size() == 3);
REQUIRE(inif["Foo"]["bar1"].as<double>() == Approx(1.2).margin(1e-3));
REQUIRE(inif["Foo"]["bar2"].as<double>() == Approx(1.0).margin(1e-3));
REQUIRE(inif["Foo"]["bar3"].as<double>() == Approx(-2.4).margin(1e-3));
}
TEST_CASE("parse field as float", "IniFile")
{
std::istringstream ss("[Foo]\nbar1=1.2\nbar2=1\nbar3=-2.4");
ini::IniFile inif(ss);
REQUIRE(inif.size() == 1);
REQUIRE(inif["Foo"].size() == 3);
REQUIRE(inif["Foo"]["bar1"].as<float>() == Approx(1.2f).margin(1e-3f));
REQUIRE(inif["Foo"]["bar2"].as<float>() == Approx(1.0f).margin(1e-3f));
REQUIRE(inif["Foo"]["bar3"].as<float>() == Approx(-2.4f).margin(1e-3f));
}
TEST_CASE("parse field as std::string", "IniFile")
{
std::istringstream ss("[Foo]\nbar1=hello\nbar2=world");
ini::IniFile inif(ss);
REQUIRE(inif.size() == 1);
REQUIRE(inif["Foo"].size() == 2);
REQUIRE(inif["Foo"]["bar1"].as<std::string>() == "hello");
REQUIRE(inif["Foo"]["bar2"].as<std::string>() == "world");
}
TEST_CASE("parse field as const char*", "IniFile")
{
std::istringstream ss("[Foo]\nbar1=hello\nbar2=world");
ini::IniFile inif(ss);
REQUIRE(inif.size() == 1);
REQUIRE(inif["Foo"].size() == 2);
REQUIRE(std::strcmp(inif["Foo"]["bar1"].as<const char*>(), "hello") == 0);
REQUIRE(std::strcmp(inif["Foo"]["bar2"].as<const char*>(), "world") == 0);
}
TEST_CASE("parse field with custom field sep", "IniFile")
{
std::istringstream ss("[Foo]\nbar1:true\nbar2:false\nbar3:tRuE");
ini::IniFile inif;
inif.setFieldSep(':');
inif.decode(ss);
REQUIRE(inif.size() == 1);
REQUIRE(inif["Foo"].size() == 3);
REQUIRE(inif["Foo"]["bar1"].as<bool>());
REQUIRE_FALSE(inif["Foo"]["bar2"].as<bool>());
REQUIRE(inif["Foo"]["bar3"].as<bool>());
}
TEST_CASE("parse with comment", "IniFile")
{
std::istringstream ss("[Foo]\n# this is a test\nbar=bla");
ini::IniFile inif(ss);
REQUIRE(inif.size() == 1);
REQUIRE(inif["Foo"].size() == 1);
REQUIRE(inif["Foo"]["bar"].as<std::string>() == "bla");
}
TEST_CASE("parse with custom comment char prefix", "IniFile")
{
std::istringstream ss("[Foo]\n$ this is a test\nbar=bla");
ini::IniFile inif;
inif.setFieldSep('=');
inif.setCommentChar('$');
inif.decode(ss);
REQUIRE(inif.size() == 1);
REQUIRE(inif["Foo"].size() == 1);
REQUIRE(inif["Foo"]["bar"].as<std::string>() == "bla");
}
TEST_CASE("parse with multi char comment prefix", "IniFile")
{
std::istringstream ss("[Foo]\nREM this is a test\nbar=bla");
ini::IniFile inif(ss, '=', {"REM"});
REQUIRE(inif.size() == 1);
REQUIRE(inif["Foo"].size() == 1);
REQUIRE(inif["Foo"]["bar"].as<std::string>() == "bla");
}
TEST_CASE("parse with multiple multi char comment prefixes", "IniFile")
{
std::istringstream ss("[Foo]\n"
"REM this is a comment\n"
"#Also a comment\n"
"//Even this is a comment\n"
"bar=bla");
ini::IniFile inif(ss, '=', {"REM", "#", "//"});
REQUIRE(inif.size() == 1);
REQUIRE(inif["Foo"].size() == 1);
REQUIRE(inif["Foo"]["bar"].as<std::string>() == "bla");
}
TEST_CASE("comment prefixes can be set after construction", "IniFile")
{
std::istringstream ss("[Foo]\n"
"REM this is a comment\n"
"#Also a comment\n"
"//Even this is a comment\n"
"bar=bla");
ini::IniFile inif;
inif.setCommentPrefixes({"REM", "#", "//"});
inif.decode(ss);
REQUIRE(inif.size() == 1);
REQUIRE(inif["Foo"].size() == 1);
REQUIRE(inif["Foo"]["bar"].as<std::string>() == "bla");
}
TEST_CASE("comments are allowed after escaped comments", "IniFile")
{
std::istringstream ss("[Foo]\n"
"hello=world \\## this is a comment\n"
"more=of this \\# \\#\n");
ini::IniFile inif(ss);
REQUIRE(inif["Foo"]["hello"].as<std::string>() == "world #");
REQUIRE(inif["Foo"]["more"].as<std::string>() == "of this # #");
}
TEST_CASE(
"escape char right before a comment prefix escapes all the comment prefix",
"IniFile")
{
std::istringstream ss("[Foo]\n"
"weird1=note \\### this is not a comment\n"
"weird2=but \\#### this is a comment");
ini::IniFile inif(ss, '=', {"##"});
REQUIRE(inif["Foo"]["weird1"].as<std::string>() ==
"note ### this is not a comment");
REQUIRE(inif["Foo"]["weird2"].as<std::string>() == "but ##");
}
TEST_CASE("escape comment when writing", "IniFile")
{
ini::IniFile inif('=', {"#"});
inif["Fo#o"] = ini::IniSection();
inif["Fo#o"]["he#llo"] = "world";
inif["Fo#o"]["world"] = "he#llo";
std::string str = inif.encode();
REQUIRE(str ==
"[Fo\\#o]\n"
"he\\#llo=world\n"
"world=he\\#llo\n");
}
TEST_CASE("decode what we encoded", "IniFile")
{
std::string content = "[Fo\\#o]\n"
"he\\REMllo=worl\\REMd\n"
"world=he\\//llo\n";
ini::IniFile inif('=', {"#", "REM", "//"});
// decode the string
inif.decode(content);
REQUIRE(inif.size() == 1);
REQUIRE(inif.find("Fo#o") != inif.end());
REQUIRE(inif["Fo#o"].size() == 2);
REQUIRE(inif["Fo#o"].find("heREMllo") != inif["Fo#o"].end());
REQUIRE(inif["Fo#o"].find("world") != inif["Fo#o"].end());
REQUIRE(inif["Fo#o"]["heREMllo"].as<std::string>() == "worlREMd");
REQUIRE(inif["Fo#o"]["world"].as<std::string>() == "he//llo");
auto actual = inif.encode();
REQUIRE(content == actual);
inif.decode(actual);
REQUIRE(inif.size() == 1);
REQUIRE(inif.find("Fo#o") != inif.end());
REQUIRE(inif["Fo#o"].size() == 2);
REQUIRE(inif["Fo#o"].find("heREMllo") != inif["Fo#o"].end());
REQUIRE(inif["Fo#o"].find("world") != inif["Fo#o"].end());
REQUIRE(inif["Fo#o"]["heREMllo"].as<std::string>() == "worlREMd");
REQUIRE(inif["Fo#o"]["world"].as<std::string>() == "he//llo");
auto actual2 = inif.encode();
REQUIRE(content == actual2);
}
TEST_CASE("save with bool fields", "IniFile")
{
ini::IniFile inif;
inif["Foo"]["bar1"] = true;
inif["Foo"]["bar2"] = false;
std::string result = inif.encode();
REQUIRE(result == "[Foo]\nbar1=true\nbar2=false\n");
}
TEST_CASE("save with char fields", "IniFile")
{
ini::IniFile inif;
inif["Foo"]["bar1"] = static_cast<char>('c');
inif["Foo"]["bar2"] = static_cast<char>('q');
std::string result = inif.encode();
REQUIRE(result == "[Foo]\nbar1=c\nbar2=q\n");
}
TEST_CASE("save with unsigned char fields", "IniFile")
{
ini::IniFile inif;
inif["Foo"]["bar1"] = static_cast<unsigned char>('c');
inif["Foo"]["bar2"] = static_cast<unsigned char>('q');
std::string result = inif.encode();
REQUIRE(result == "[Foo]\nbar1=c\nbar2=q\n");
}
TEST_CASE("save with short fields", "IniFile")
{
ini::IniFile inif;
inif["Foo"]["bar1"] = static_cast<short>(1);
inif["Foo"]["bar2"] = static_cast<short>(-2);
std::string result = inif.encode();
REQUIRE(result == "[Foo]\nbar1=1\nbar2=-2\n");
}
TEST_CASE("save with unsigned short fields", "IniFile")
{
ini::IniFile inif;
inif["Foo"]["bar1"] = static_cast<unsigned short>(1);
inif["Foo"]["bar2"] = static_cast<unsigned short>(13);
std::string result = inif.encode();
REQUIRE(result == "[Foo]\nbar1=1\nbar2=13\n");
}
TEST_CASE("save with int fields", "IniFile")
{
ini::IniFile inif;
inif["Foo"]["bar1"] = static_cast<int>(1);
inif["Foo"]["bar2"] = static_cast<int>(-2);
std::string result = inif.encode();
REQUIRE(result == "[Foo]\nbar1=1\nbar2=-2\n");
}
TEST_CASE("save with unsigned int fields", "IniFile")
{
ini::IniFile inif;
inif["Foo"]["bar1"] = static_cast<unsigned int>(1);
inif["Foo"]["bar2"] = static_cast<unsigned int>(13);
std::string result = inif.encode();
REQUIRE(result == "[Foo]\nbar1=1\nbar2=13\n");
}
TEST_CASE("save with long fields", "IniFile")
{
ini::IniFile inif;
inif["Foo"]["bar1"] = static_cast<long>(1);
inif["Foo"]["bar2"] = static_cast<long>(-2);
std::string result = inif.encode();
REQUIRE(result == "[Foo]\nbar1=1\nbar2=-2\n");
}
TEST_CASE("save with unsigned long fields", "IniFile")
{
ini::IniFile inif;
inif["Foo"]["bar1"] = static_cast<unsigned long>(1);
inif["Foo"]["bar2"] = static_cast<unsigned long>(13);
std::string result = inif.encode();
REQUIRE(result == "[Foo]\nbar1=1\nbar2=13\n");
}
TEST_CASE("save with double fields", "IniFile")
{
ini::IniFile inif;
inif["Foo"]["bar1"] = static_cast<double>(1.2);
inif["Foo"]["bar2"] = static_cast<double>(-2.4);
std::string result = inif.encode();
REQUIRE(result == "[Foo]\nbar1=1.2\nbar2=-2.4\n");
}
TEST_CASE("save with float fields", "IniFile")
{
ini::IniFile inif;
inif["Foo"]["bar1"] = static_cast<float>(1.2f);
inif["Foo"]["bar2"] = static_cast<float>(-2.4f);
std::string result = inif.encode();
REQUIRE(result == "[Foo]\nbar1=1.2\nbar2=-2.4\n");
}
TEST_CASE("save with std::string fields", "IniFile")
{
ini::IniFile inif;
inif["Foo"]["bar1"] = static_cast<std::string>("hello");
inif["Foo"]["bar2"] = static_cast<std::string>("world");
std::string result = inif.encode();
REQUIRE(result == "[Foo]\nbar1=hello\nbar2=world\n");
}
TEST_CASE("save with const char* fields", "IniFile")
{
ini::IniFile inif;
inif["Foo"]["bar1"] = static_cast<const char*>("hello");
inif["Foo"]["bar2"] = static_cast<const char*>("world");
std::string result = inif.encode();
REQUIRE(result == "[Foo]\nbar1=hello\nbar2=world\n");
}
TEST_CASE("save with char* fields", "IniFile")
{
ini::IniFile inif;
char bar1[6] = "hello";
char bar2[6] = "world";
inif["Foo"]["bar1"] = static_cast<char*>(bar1);
inif["Foo"]["bar2"] = static_cast<char*>(bar2);
std::string result = inif.encode();
REQUIRE(result == "[Foo]\nbar1=hello\nbar2=world\n");
}
TEST_CASE("save with string literal fields", "IniFile")
{
ini::IniFile inif;
inif["Foo"]["bar1"] = "hello";
inif["Foo"]["bar2"] = "world";
std::string result = inif.encode();
REQUIRE(result == "[Foo]\nbar1=hello\nbar2=world\n");
}
TEST_CASE("save with custom field sep", "IniFile")
{
ini::IniFile inif(':', '#');
inif["Foo"]["bar1"] = true;
inif["Foo"]["bar2"] = false;
std::string result = inif.encode();
REQUIRE(result == "[Foo]\nbar1:true\nbar2:false\n");
}
TEST_CASE("inline comments in sections are discarded", "IniFile")
{
std::istringstream ss("[Foo] # This is an inline comment\nbar=Hello world!");
ini::IniFile inif(ss);
REQUIRE(inif.find("Foo") != inif.end());
}
TEST_CASE("inline comments in fields are discarded", "IniFile")
{
std::istringstream ss("[Foo]\n"
"bar=Hello #world!");
ini::IniFile inif(ss);
REQUIRE(inif["Foo"]["bar"].as<std::string>() == "Hello");
}
TEST_CASE("inline comments can be escaped", "IniFile")
{
std::istringstream ss("[Foo]\n"
"bar=Hello \\#world!");
ini::IniFile inif(ss);
REQUIRE(inif["Foo"]["bar"].as<std::string>() == "Hello #world!");
}
TEST_CASE("escape characters are kept if not before a comment prefix", "IniFile")
{
std::istringstream ss("[Foo]\n"
"bar=Hello \\world!");
ini::IniFile inif(ss);
REQUIRE(inif["Foo"]["bar"].as<std::string>() == "Hello \\world!");
}
TEST_CASE("multi-line values are read correctly with space indents", "IniFile")
{
std::istringstream ss("[Foo]\n"
"bar=Hello\n"
" world!");
ini::IniFile inif;
inif.setMultiLineValues(true);
inif.decode(ss);
REQUIRE(inif["Foo"]["bar"].as<std::string>() == "Hello\nworld!");
}
TEST_CASE("multi-line values are read correctly with tab indents", "IniFile")
{
std::istringstream ss("[Foo]\n"
"bar=Hello\n"
"\tworld!");
ini::IniFile inif;
inif.setMultiLineValues(true);
inif.decode(ss);
REQUIRE(inif["Foo"]["bar"].as<std::string>() == "Hello\nworld!");
}
TEST_CASE("multi-line values discard end-of-line comments", "IniFile")
{
std::istringstream ss("[Foo]\n"
"bar=Hello ; everyone\n"
" world! ; comment");
ini::IniFile inif;
inif.setMultiLineValues(true);
inif.decode(ss);
REQUIRE(inif["Foo"]["bar"].as<std::string>() == "Hello\nworld!");
}
TEST_CASE("multi-line values discard interspersed comment lines", "IniFile")
{
std::istringstream ss("[Foo]\n"
"bar=Hello\n"
"; everyone\n"
" world!");
ini::IniFile inif;
inif.setMultiLineValues(true);
inif.decode(ss);
REQUIRE(inif["Foo"]["bar"].as<std::string>() == "Hello\nworld!");
}
TEST_CASE("multi-line values should not be parsed when disabled", "IniFile")
{
std::istringstream ss("[Foo]\n"
" bar=Hello\n"
" baz=world!");
ini::IniFile inif;
inif.setMultiLineValues(false);
inif.decode(ss);
REQUIRE(inif["Foo"]["bar"].as<std::string>() == "Hello");
REQUIRE(inif["Foo"]["baz"].as<std::string>() == "world!");
}
TEST_CASE("multi-line values should be parsed when enabled, even when the continuation contains =", "IniFile")
{
std::istringstream ss("[Foo]\n"
" bar=Hello\n"
" baz=world!");
ini::IniFile inif;
inif.setMultiLineValues(true);
inif.decode(ss);
REQUIRE(inif["Foo"]["bar"].as<std::string>() == "Hello\nbaz=world!");
REQUIRE(inif["Foo"]["baz"].as<std::string>() == "");
}
TEST_CASE("when multi-line values are enabled, write newlines as multi-line value continuations", "IniFile")
{
ini::IniFile inif;
inif.setMultiLineValues(true);
inif["Foo"] = ini::IniSection();
inif["Foo"]["bar"] = "Hello\nworld!";
std::string str = inif.encode();
REQUIRE(str ==
"[Foo]\n"
"bar=Hello\n"
"\tworld!\n");
}
TEST_CASE("stringInsensitiveLess operator() returns true if and only if first parameter is less than the second ignoring sensitivity", "StringInsensitiveLessFunctor")
{
ini::StringInsensitiveLess cc;
REQUIRE(cc("a", "b"));
REQUIRE(cc("a", "B"));
REQUIRE(cc("aaa", "aaB"));
}
TEST_CASE("stringInsensitiveLess operator() returns false when words differs only in case", "StringInsensitiveLessFunctor")
{
ini::StringInsensitiveLess cc;
REQUIRE(cc("AA", "aa") == false);
}
TEST_CASE("stringInsensitiveLess operator() has a case insensitive strict weak ordering policy", "StringInsensitiveLessFunctor")
{
ini::StringInsensitiveLess cc;
REQUIRE(cc("B", "a") == false);
}
TEST_CASE("default inifile parser is case sensitive", "IniFile")
{
std::istringstream ss("[FOO]\nbar=bla");
ini::IniFile inif(ss);
REQUIRE(inif.find("foo") == inif.end());
REQUIRE(inif["FOO"].find("BAR") == inif["FOO"].end());
}
TEST_CASE("case insensitive inifile ignores case of section", "IniFile")
{
std::istringstream ss("[FOO]\nbar=bla");
ini::IniFileCaseInsensitive inif(ss);
REQUIRE(inif.find("foo") != inif.end());
REQUIRE(inif.find("FOO") != inif.end());
}
TEST_CASE("case insensitive inifile ignores case of field", "IniFile")
{
std::istringstream ss("[FOO]\nbar=bla");
ini::IniFileCaseInsensitive inif(ss);
REQUIRE(inif["FOO"].find("BAR") != inif["FOO"].end());
}
TEST_CASE(".as<>() works with IniFileCaseInsensitive", "IniFile")
{
std::istringstream ss("[FOO]\nbar=bla");
ini::IniFileCaseInsensitive inif(ss);
REQUIRE(inif["FOO"]["bar"].as<std::string>() == "bla");
}
TEST_CASE("trim() works with empty strings", "TrimFunction")
{
std::string example1 = "";
std::string example2 = " \t\n ";
ini::trim(example1);
ini::trim(example2);
REQUIRE(example1.size() == 0);
REQUIRE(example2.size() == 0);
}
TEST_CASE("trim() works with already trimmed strings", "TrimFunction")
{
std::string example1 = "example_text";
std::string example2 = "example \t\n text";
ini::trim(example1);
ini::trim(example2);
REQUIRE(example1 == "example_text");
REQUIRE(example2 == "example \t\n text");
}
TEST_CASE("trim() works with untrimmed strings", "TrimFunction")
{
std::string example1 = "example text ";
std::string example2 = " example text";
std::string example3 = " example text ";
std::string example4 = " \t\n example \t\n text \t\n ";
ini::trim(example1);
ini::trim(example2);
ini::trim(example3);
ini::trim(example4);
REQUIRE(example1 == "example text");
REQUIRE(example2 == "example text");
REQUIRE(example3 == "example text");
REQUIRE(example4 == "example \t\n text");
}
/***************************************************
* Failing Tests
***************************************************/
TEST_CASE("fail to load unclosed section", "IniFile")
{
ini::IniFile inif;
REQUIRE_THROWS(inif.decode("[Foo\nbar=bla"));
}
TEST_CASE("fail to load field without equal", "IniFile")
{
ini::IniFile inif;
REQUIRE_THROWS(inif.decode("[Foo]\nbar"));
}
TEST_CASE("fail to parse a multi-line field without indentation (when enabled)", "IniFile")
{
ini::IniFile inif;
inif.setMultiLineValues(true);
REQUIRE_THROWS(inif.decode("[Foo]\nbar=Hello\nworld!"));
}
TEST_CASE("fail to parse a multi-line field without indentation (when disabled)", "IniFile")
{
ini::IniFile inif;
inif.setMultiLineValues(false);
REQUIRE_THROWS(inif.decode("[Foo]\nbar=Hello\nworld!"));
}
TEST_CASE("fail to continue multi-line field without start (when enabled)", "IniFile")
{
ini::IniFile inif;
inif.setMultiLineValues(true);
REQUIRE_THROWS(inif.decode("[Foo]\n world!\nbar=Hello"));
}
TEST_CASE("fail to continue multi-line field without start (when disabled)", "IniFile")
{
ini::IniFile inif;
inif.setMultiLineValues(false);
REQUIRE_THROWS(inif.decode("[Foo]\n world!\nbar=Hello"));
}
TEST_CASE("fail to parse as bool", "IniFile")
{
std::istringstream ss("[Foo]\nbar=bla");
ini::IniFile inif(ss);
REQUIRE(inif.size() == 1);
REQUIRE(inif["Foo"].size() == 1);
REQUIRE_THROWS(inif["Foo"]["bar"].as<bool>());
}
TEST_CASE("fail to parse as int", "IniFile")
{
std::istringstream ss("[Foo]\nbar=bla");
ini::IniFile inif(ss);
REQUIRE(inif.size() == 1);
REQUIRE(inif["Foo"].size() == 1);
REQUIRE_THROWS(inif["Foo"]["bar"].as<int>());
}
TEST_CASE("fail to parse as double", "IniFile")
{
std::istringstream ss("[Foo]\nbar=bla");
ini::IniFile inif(ss);
REQUIRE(inif.size() == 1);
REQUIRE(inif["Foo"].size() == 1);
REQUIRE_THROWS(inif["Foo"]["bar"].as<double>());
}
TEST_CASE("fail to parse field without section", "IniFile")
{
ini::IniFile inif;
REQUIRE_THROWS(inif.decode("bar=bla"));
}
TEST_CASE("spaces are not taken into account in field names", "IniFile")
{
std::istringstream ss(("[Foo]\n \t bar \t =hello world"));
ini::IniFile inif(ss);
REQUIRE(inif["Foo"].find("bar") != inif["Foo"].end());
REQUIRE(inif["Foo"]["bar"].as<std::string>() == "hello world");
}
TEST_CASE("spaces are not taken into account in field values", "IniFile")
{
std::istringstream ss(("[Foo]\nbar= \t hello world \t "));
ini::IniFile inif(ss);
REQUIRE(inif["Foo"]["bar"].as<std::string>() == "hello world");
}
TEST_CASE("spaces are not taken into account in sections", "IniFile")
{
std::istringstream ss(" \t [Foo] \t \nbar=bla");
ini::IniFile inif(ss);
REQUIRE(inif.find("Foo") != inif.end());
}
TEST_CASE("parse section with duplicate field and overwriteDuplicateFields_ set to false", "IniFile")
{
ini::IniFile inif;
inif.allowOverwriteDuplicateFields(false);
REQUIRE_THROWS(inif.decode("[Foo]\nbar=hello\nbar=world"));
} | cpp |
inifile-cpp | data/projects/inifile-cpp/include/inicpp.h | /*
* inicpp.h
*
* Created on: 26 Dec 2015
* Author: Fabian Meyer
* License: MIT
*/
#ifndef INICPP_H_
#define INICPP_H_
#include <algorithm>
#include <fstream>
#include <istream>
#include <map>
#include <assert.h>
#include <sstream>
#include <stdexcept>
#include <vector>
namespace ini
{
/************************************************
* Helper Functions
************************************************/
/** Returns a string of whitespace characters. */
constexpr const char *whitespaces()
{
return " \t\n\r\f\v";
}
/** Returns a string of indentation characters. */
constexpr const char *indents()
{
return " \t";
}
/** Trims a string in place.
* @param str string to be trimmed in place */
inline void trim(std::string &str)
{
// first erasing from end should be slighty more efficient
// because erasing from start potentially moves all chars
// multiple indices towards the front.
auto lastpos = str.find_last_not_of(whitespaces());
if(lastpos == std::string::npos)
{
str.clear();
return;
}
str.erase(lastpos + 1);
str.erase(0, str.find_first_not_of(whitespaces()));
}
/************************************************
* Conversion Functors
************************************************/
inline bool strToLong(const std::string &value, long &result)
{
char *endptr;
// check if decimal
result = std::strtol(value.c_str(), &endptr, 10);
if(*endptr == '\0')
return true;
// check if octal
result = std::strtol(value.c_str(), &endptr, 8);
if(*endptr == '\0')
return true;
// check if hex
result = std::strtol(value.c_str(), &endptr, 16);
if(*endptr == '\0')
return true;
return false;
}
inline bool strToULong(const std::string &value, unsigned long &result)
{
char *endptr;
// check if decimal
result = std::strtoul(value.c_str(), &endptr, 10);
if(*endptr == '\0')
return true;
// check if octal
result = std::strtoul(value.c_str(), &endptr, 8);
if(*endptr == '\0')
return true;
// check if hex
result = std::strtoul(value.c_str(), &endptr, 16);
if(*endptr == '\0')
return true;
return false;
}
template<typename T>
struct Convert
{};
template<>
struct Convert<bool>
{
void decode(const std::string &value, bool &result)
{
std::string str(value);
std::transform(str.begin(), str.end(), str.begin(), [](const char c){
return static_cast<char>(::toupper(c));
});
if(str == "TRUE")
result = true;
else if(str == "FALSE")
result = false;
else
throw std::invalid_argument("field is not a bool");
}
void encode(const bool value, std::string &result)
{
result = value ? "true" : "false";
}
};
template<>
struct Convert<char>
{
void decode(const std::string &value, char &result)
{
assert(value.size() > 0);
result = value[0];
}
void encode(const char value, std::string &result)
{
result = value;
}
};
template<>
struct Convert<unsigned char>
{
void decode(const std::string &value, unsigned char &result)
{
assert(value.size() > 0);
result = value[0];
}
void encode(const unsigned char value, std::string &result)
{
result = value;
}
};
template<>
struct Convert<short>
{
void decode(const std::string &value, short &result)
{
long tmp;
if(!strToLong(value, tmp))
throw std::invalid_argument("field is not a short");
result = static_cast<short>(tmp);
}
void encode(const short value, std::string &result)
{
std::stringstream ss;
ss << value;
result = ss.str();
}
};
template<>
struct Convert<unsigned short>
{
void decode(const std::string &value, unsigned short &result)
{
unsigned long tmp;
if(!strToULong(value, tmp))
throw std::invalid_argument("field is not an unsigned short");
result = static_cast<unsigned short>(tmp);
}
void encode(const unsigned short value, std::string &result)
{
std::stringstream ss;
ss << value;
result = ss.str();
}
};
template<>
struct Convert<int>
{
void decode(const std::string &value, int &result)
{
long tmp;
if(!strToLong(value, tmp))
throw std::invalid_argument("field is not an int");
result = static_cast<int>(tmp);
}
void encode(const int value, std::string &result)
{
std::stringstream ss;
ss << value;
result = ss.str();
}
};
template<>
struct Convert<unsigned int>
{
void decode(const std::string &value, unsigned int &result)
{
unsigned long tmp;
if(!strToULong(value, tmp))
throw std::invalid_argument("field is not an unsigned int");
result = static_cast<unsigned int>(tmp);
}
void encode(const unsigned int value, std::string &result)
{
std::stringstream ss;
ss << value;
result = ss.str();
}
};
template<>
struct Convert<long>
{
void decode(const std::string &value, long &result)
{
if(!strToLong(value, result))
throw std::invalid_argument("field is not a long");
}
void encode(const long value, std::string &result)
{
std::stringstream ss;
ss << value;
result = ss.str();
}
};
template<>
struct Convert<unsigned long>
{
void decode(const std::string &value, unsigned long &result)
{
if(!strToULong(value, result))
throw std::invalid_argument("field is not an unsigned long");
}
void encode(const unsigned long value, std::string &result)
{
std::stringstream ss;
ss << value;
result = ss.str();
}
};
template<>
struct Convert<double>
{
void decode(const std::string &value, double &result)
{
result = std::stod(value);
}
void encode(const double value, std::string &result)
{
std::stringstream ss;
ss << value;
result = ss.str();
}
};
template<>
struct Convert<float>
{
void decode(const std::string &value, float &result)
{
result = std::stof(value);
}
void encode(const float value, std::string &result)
{
std::stringstream ss;
ss << value;
result = ss.str();
}
};
template<>
struct Convert<std::string>
{
void decode(const std::string &value, std::string &result)
{
result = value;
}
void encode(const std::string &value, std::string &result)
{
result = value;
}
};
template<>
struct Convert<const char*>
{
void encode(const char* const &value, std::string &result)
{
result = value;
}
void decode(const std::string &value, const char* &result)
{
result = value.c_str();
}
};
template<>
struct Convert<char*>
{
void encode(const char* const &value, std::string &result)
{
result = value;
}
};
template<size_t n>
struct Convert<char[n]>
{
void encode(const char *value, std::string &result)
{
result = value;
}
};
class IniField
{
private:
std::string value_;
public:
IniField() : value_()
{}
IniField(const std::string &value) : value_(value)
{}
IniField(const IniField &field) : value_(field.value_)
{}
~IniField()
{}
template<typename T>
T as() const
{
Convert<T> conv;
T result;
conv.decode(value_, result);
return result;
}
template<typename T>
IniField &operator=(const T &value)
{
Convert<T> conv;
conv.encode(value, value_);
return *this;
}
IniField &operator=(const IniField &field)
{
value_ = field.value_;
return *this;
}
};
struct StringInsensitiveLess
{
bool operator()(std::string lhs, std::string rhs) const
{
std::transform(lhs.begin(), lhs.end(), lhs.begin(), [](const char c){
return static_cast<char>(::tolower(c));
});
std::transform(rhs.begin(), rhs.end(), rhs.begin(), [](const char c){
return static_cast<char>(::tolower(c));
});
return lhs < rhs;
}
};
template <typename Comparator>
class IniSectionBase : public std::map<std::string, IniField, Comparator>
{
public:
IniSectionBase()
{}
~IniSectionBase()
{}
};
using IniSection = IniSectionBase<std::less<std::string>>;
using IniSectionCaseInsensitive = IniSectionBase<StringInsensitiveLess>;
template <typename Comparator>
class IniFileBase : public std::map<std::string, IniSectionBase<Comparator>, Comparator>
{
private:
char fieldSep_ = '=';
char esc_ = '\\';
std::vector<std::string> commentPrefixes_ = { "#" , ";" };
bool multiLineValues_ = false;
bool overwriteDuplicateFields_ = true;
void eraseComment(const std::string &commentPrefix,
std::string &str,
std::string::size_type startpos = 0)
{
size_t prefixpos = str.find(commentPrefix, startpos);
if(std::string::npos == prefixpos)
return;
// Found a comment prefix, is it escaped?
if(0 != prefixpos && str[prefixpos - 1] == esc_)
{
// The comment prefix is escaped, so just delete the escape char
// and keep erasing after the comment prefix
str.erase(prefixpos - 1, 1);
eraseComment(
commentPrefix, str, prefixpos - 1 + commentPrefix.size());
}
else
{
str.erase(prefixpos);
}
}
void eraseComments(std::string &str)
{
for(const std::string &commentPrefix : commentPrefixes_)
eraseComment(commentPrefix, str);
}
/** Tries to find a suitable comment prefix for the string data at the given
* position. Returns commentPrefixes_.end() if not match was found. */
std::vector<std::string>::const_iterator findCommentPrefix(const std::string &str,
const std::size_t startpos) const
{
// if startpos is invalid simply return "not found"
if(startpos >= str.size())
return commentPrefixes_.end();
for(size_t i = 0; i < commentPrefixes_.size(); ++i)
{
const std::string &prefix = commentPrefixes_[i];
// if this comment prefix is longer than the string view itself
// then skip
if(prefix.size() + startpos > str.size())
continue;
bool match = true;
for(size_t j = 0; j < prefix.size() && match; ++j)
match = str[startpos + j] == prefix[j];
if(match)
return commentPrefixes_.begin() + i;
}
return commentPrefixes_.end();
}
void writeEscaped(std::ostream &os, const std::string &str) const
{
for(size_t i = 0; i < str.length(); ++i)
{
auto prefixpos = findCommentPrefix(str, i);
// if no suitable prefix was found at this position
// then simply write the current character
if(prefixpos != commentPrefixes_.end())
{
const std::string &prefix = *prefixpos;
os.put(esc_);
os.write(prefix.c_str(), prefix.size());
i += prefix.size() - 1;
}
else if (multiLineValues_ && str[i] == '\n')
os.write("\n\t", 2);
else
os.put(str[i]);
}
}
public:
IniFileBase() = default;
IniFileBase(const char fieldSep, const char comment)
: fieldSep_(fieldSep), commentPrefixes_(1, std::string(1, comment))
{}
IniFileBase(const std::string &filename)
{
load(filename);
}
IniFileBase(std::istream &is)
{
decode(is);
}
IniFileBase(const char fieldSep,
const std::vector<std::string> &commentPrefixes)
: fieldSep_(fieldSep), commentPrefixes_(commentPrefixes)
{}
IniFileBase(const std::string &filename,
const char fieldSep,
const std::vector<std::string> &commentPrefixes)
: fieldSep_(fieldSep), commentPrefixes_(commentPrefixes)
{
load(filename);
}
IniFileBase(std::istream &is,
const char fieldSep,
const std::vector<std::string> &commentPrefixes)
: fieldSep_(fieldSep), commentPrefixes_(commentPrefixes)
{
decode(is);
}
~IniFileBase()
{}
/** Sets the separator charactor for fields in the INI file.
* @param sep separator character to be used. */
void setFieldSep(const char sep)
{
fieldSep_ = sep;
}
/** Sets the character that should be interpreted as the start of comments.
* Default is '#'.
* Note: If the inifile contains the comment character as data it must be prefixed with
* the configured escape character.
* @param comment comment character to be used. */
void setCommentChar(const char comment)
{
commentPrefixes_ = {std::string(1, comment)};
}
/** Sets the list of strings that should be interpreted as the start of comments.
* Default is [ "#" ].
* Note: If the inifile contains any comment string as data it must be prefixed with
* the configured escape character.
* @param commentPrefixes vector of comment prefix strings to be used. */
void setCommentPrefixes(const std::vector<std::string> &commentPrefixes)
{
commentPrefixes_ = commentPrefixes;
}
/** Sets the character that should be used to escape comment prefixes.
* Default is '\'.
* @param esc escape character to be used. */
void setEscapeChar(const char esc)
{
esc_ = esc;
}
/** Sets whether or not to parse multi-line field values.
* Default is false.
* @param enable enable or disable? */
void setMultiLineValues(bool enable)
{
multiLineValues_ = enable;
}
/** Sets whether or not overwriting duplicate fields is allowed.
* If overwriting duplicate fields is not allowed,
* an exception is thrown when a duplicate field is found inside a section.
* Default is true.
* @param allowed Is overwriting duplicate fields allowed or not? */
void allowOverwriteDuplicateFields(bool allowed)
{
overwriteDuplicateFields_ = allowed;
}
/** Tries to decode a ini file from the given input stream.
* @param is input stream from which data should be read. */
void decode(std::istream &is)
{
this->clear();
int lineNo = 0;
IniSectionBase<Comparator> *currentSection = nullptr;
std::string mutliLineValueFieldName = "";
std::string line;
// iterate file line by line
while(!is.eof() && !is.fail())
{
std::getline(is, line, '\n');
eraseComments(line);
bool hasIndent = line.find_first_not_of(indents()) != 0;
trim(line);
++lineNo;
// skip if line is empty
if(line.size() == 0)
continue;
if(line[0] == '[')
{
// line is a section
// check if the section is also closed on same line
std::size_t pos = line.find("]");
if(pos == std::string::npos)
{
std::stringstream ss;
ss << "l." << lineNo
<< ": ini parsing failed, section not closed";
throw std::logic_error(ss.str());
}
// check if the section name is empty
if(pos == 1)
{
std::stringstream ss;
ss << "l." << lineNo
<< ": ini parsing failed, section is empty";
throw std::logic_error(ss.str());
}
// retrieve section name
std::string secName = line.substr(1, pos - 1);
currentSection = &((*this)[secName]);
// clear multiline value field name
// a new section means there is no value to continue
mutliLineValueFieldName = "";
}
else
{
// line is a field definition
// check if section was already opened
if(currentSection == nullptr)
{
std::stringstream ss;
ss << "l." << lineNo
<< ": ini parsing failed, field has no section"
" or ini file in use by another application";
throw std::logic_error(ss.str());
}
// find key value separator
std::size_t pos = line.find(fieldSep_);
if (multiLineValues_ && hasIndent && mutliLineValueFieldName != "")
{
// extend a multi-line value
IniField previous_value = (*currentSection)[mutliLineValueFieldName];
std::string value = previous_value.as<std::string>() + "\n" + line;
(*currentSection)[mutliLineValueFieldName] = value;
}
else if(pos == std::string::npos)
{
std::stringstream ss;
ss << "l." << lineNo
<< ": ini parsing failed, no '"
<< fieldSep_
<< "' found";
if (multiLineValues_)
ss << ", and not a multi-line value continuation";
throw std::logic_error(ss.str());
}
else
{
// retrieve field name and value
std::string name = line.substr(0, pos);
trim(name);
if (!overwriteDuplicateFields_ && currentSection->count(name) != 0)
{
std::stringstream ss;
ss << "l." << lineNo
<< ": ini parsing failed, duplicate field found";
throw std::logic_error(ss.str());
}
std::string value = line.substr(pos + 1, std::string::npos);
trim(value);
(*currentSection)[name] = value;
// store last field name for potential multi-line values
mutliLineValueFieldName = name;
}
}
}
}
/** Tries to decode a ini file from the given input string.
* @param content string to be decoded. */
void decode(const std::string &content)
{
std::istringstream ss(content);
decode(ss);
}
/** Tries to load and decode a ini file from the file at the given path.
* @param fileName path to the file that should be loaded. */
void load(const std::string &fileName)
{
std::ifstream is(fileName.c_str());
decode(is);
}
/** Encodes this inifile object and writes the output to the given stream.
* @param os target stream. */
void encode(std::ostream &os) const
{
// iterate through all sections in this file
for(const auto &filePair : *this)
{
os.put('[');
writeEscaped(os, filePair.first);
os.put(']');
os.put('\n');
// iterate through all fields in the section
for(const auto &secPair : filePair.second)
{
writeEscaped(os, secPair.first);
os.put(fieldSep_);
writeEscaped(os, secPair.second.template as<std::string>());
os.put('\n');
}
}
}
/** Encodes this inifile object as string and returns the result.
* @return encoded infile string. */
std::string encode() const
{
std::ostringstream ss;
encode(ss);
return ss.str();
}
/** Saves this inifile object to the file at the given path.
* @param fileName path to the file where the data should be stored. */
void save(const std::string &fileName) const
{
std::ofstream os(fileName.c_str());
encode(os);
}
};
using IniFile = IniFileBase<std::less<std::string>>;
using IniSection = IniSectionBase<std::less<std::string>>;
using IniFileCaseInsensitive = IniFileBase<StringInsensitiveLess>;
using IniSectionCaseInsensitive = IniSectionBase<StringInsensitiveLess>;
}
#endif
| h |
oneAPI-samples | data/projects/oneAPI-samples/RenderingToolkit/GettingStarted/03_openvkl_gsg/gpu/src/vklTutorialGPU.cpp | // Copyright 2022 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
#include <openvkl/openvkl.h>
#include <openvkl/device/openvkl.h>
#include <iomanip>
#include <iostream>
// setup specialization constant for feature flags
static_assert(std::is_trivially_copyable<VKLFeatureFlags>::value);
constexpr static sycl::specialization_id<VKLFeatureFlags> samplerSpecId{
VKL_FEATURE_FLAGS_DEFAULT};
void demoGpuAPI(sycl::queue &syclQueue, VKLDevice device, VKLVolume volume) {
std::cout << "demo of GPU API" << std::endl;
std::cout << std::fixed << std::setprecision(6);
VKLSampler sampler = vklNewSampler(volume);
vklCommit(sampler);
// feature flags improve performance on GPU, as well as JIT times
const VKLFeatureFlags requiredFeatures = vklGetFeatureFlags(sampler);
// bounding box
vkl_box3f bbox = vklGetBoundingBox(volume);
std::cout << "\tbounding box" << std::endl;
std::cout << "\t\tlower = " << bbox.lower.x << " " << bbox.lower.y << " "
<< bbox.lower.z << std::endl;
std::cout << "\t\tupper = " << bbox.upper.x << " " << bbox.upper.y << " "
<< bbox.upper.z << std::endl
<< std::endl;
// number of attributes
unsigned int numAttributes = vklGetNumAttributes(volume);
std::cout << "\tnum attributes = " << numAttributes << std::endl;
// value range for all attributes
for (unsigned int i = 0; i < numAttributes; i++) {
vkl_range1f valueRange = vklGetValueRange(volume, i);
std::cout << "\tvalue range (attribute " << i << ") = (" << valueRange.lower
<< " " << valueRange.upper << ")" << std::endl;
}
std::cout << std::endl << "\tsampling" << std::endl;
// coordinate for sampling / gradients
vkl_vec3f coord = {1.f, 2.f, 3.f};
std::cout << "\n\tcoord = " << coord.x << " " << coord.y << " " << coord.z
<< std::endl
<< std::endl;
// sample, gradient (first attribute)
const unsigned int attributeIndex = 0;
const float time = 0.f;
// USM shared allocations, required when we want to pass results back from GPU
float *sample = sycl::malloc_shared<float>(1, syclQueue);
vkl_vec3f *grad = sycl::malloc_shared<vkl_vec3f>(1, syclQueue);
syclQueue
.submit([=](sycl::handler &cgh) {
cgh.set_specialization_constant<samplerSpecId>(requiredFeatures);
cgh.single_task([=](sycl::kernel_handler kh) {
const VKLFeatureFlags featureFlags =
kh.get_specialization_constant<samplerSpecId>();
*sample = vklComputeSample(&sampler, &coord, attributeIndex, time,
featureFlags);
*grad = vklComputeGradient(&sampler, &coord, attributeIndex, time,
featureFlags);
});
})
.wait();
std::cout << "\tsampling and gradient computation (first attribute)"
<< std::endl;
std::cout << "\t\tsample = " << *sample << std::endl;
std::cout << "\t\tgrad = " << grad->x << " " << grad->y << " " << grad->z
<< std::endl
<< std::endl;
sycl::free(sample, syclQueue);
sycl::free(grad, syclQueue);
// sample (multiple attributes)
const unsigned int M = 3;
const unsigned int attributeIndices[] = {0, 1, 2};
float *samples = sycl::malloc_shared<float>(M, syclQueue);
syclQueue
.submit([=](sycl::handler &cgh) {
cgh.set_specialization_constant<samplerSpecId>(requiredFeatures);
cgh.single_task([=](sycl::kernel_handler kh) {
const VKLFeatureFlags featureFlags =
kh.get_specialization_constant<samplerSpecId>();
vklComputeSampleM(&sampler, &coord, samples, M, attributeIndices,
time, featureFlags);
});
})
.wait();
std::cout << "\tsampling (multiple attributes)" << std::endl;
std::cout << "\t\tsamples = " << samples[0] << " " << samples[1] << " "
<< samples[2] << std::endl;
sycl::free(samples, syclQueue);
// interval iterator context setup
std::cout << std::endl << "\tinterval iteration" << std::endl << std::endl;
std::vector<vkl_range1f> ranges{{10, 20}, {50, 75}};
VKLData rangesData =
vklNewData(device, ranges.size(), VKL_BOX1F, ranges.data());
VKLIntervalIteratorContext intervalContext =
vklNewIntervalIteratorContext(sampler);
vklSetInt(intervalContext, "attributeIndex", 0);
vklSetData(intervalContext, "valueRanges", rangesData);
vklRelease(rangesData);
vklCommit(intervalContext);
// ray definition for iterators
vkl_vec3f rayOrigin{0.f, 1.f, 1.f};
vkl_vec3f rayDirection{1.f, 0.f, 0.f};
vkl_range1f rayTRange{0.f, 200.f};
std::cout << "\trayOrigin = " << rayOrigin.x << " " << rayOrigin.y << " "
<< rayOrigin.z << std::endl;
std::cout << "\trayDirection = " << rayDirection.x << " " << rayDirection.y
<< " " << rayDirection.z << std::endl;
std::cout << "\trayTRange = " << rayTRange.lower << " " << rayTRange.upper
<< std::endl
<< std::endl;
// interval iteration
char *iteratorBuffer = sycl::malloc_device<char>(
vklGetIntervalIteratorSize(&intervalContext), syclQueue);
int *numIntervals = sycl::malloc_shared<int>(1, syclQueue);
*numIntervals = 0;
const size_t maxNumIntervals = 999;
VKLInterval *intervalsBuffer =
sycl::malloc_shared<VKLInterval>(maxNumIntervals, syclQueue);
memset(intervalsBuffer, 0, maxNumIntervals * sizeof(VKLInterval));
std::cout << "\tinterval iterator for value ranges";
for (const auto &r : ranges) {
std::cout << " {" << r.lower << " " << r.upper << "}";
}
std::cout << std::endl << std::endl;
syclQueue
.submit([=](sycl::handler &cgh) {
cgh.set_specialization_constant<samplerSpecId>(requiredFeatures);
cgh.single_task([=](sycl::kernel_handler kh) {
const VKLFeatureFlags featureFlags =
kh.get_specialization_constant<samplerSpecId>();
VKLIntervalIterator intervalIterator = vklInitIntervalIterator(
&intervalContext, &rayOrigin, &rayDirection, &rayTRange, time,
(void *)iteratorBuffer, featureFlags);
for (;;) {
VKLInterval interval;
int result =
vklIterateInterval(intervalIterator, &interval, featureFlags);
if (!result) {
break;
}
intervalsBuffer[*numIntervals] = interval;
*numIntervals = *numIntervals + 1;
if (*numIntervals >= maxNumIntervals) break;
}
});
})
.wait();
for (int i = 0; i < *numIntervals; ++i) {
std::cout << "\t\ttRange (" << intervalsBuffer[i].tRange.lower << " "
<< intervalsBuffer[i].tRange.upper << ")" << std::endl;
std::cout << "\t\tvalueRange (" << intervalsBuffer[i].valueRange.lower
<< " " << intervalsBuffer[i].valueRange.upper << ")" << std::endl;
std::cout << "\t\tnominalDeltaT " << intervalsBuffer[i].nominalDeltaT
<< std::endl
<< std::endl;
}
sycl::free(iteratorBuffer, syclQueue);
sycl::free(numIntervals, syclQueue);
sycl::free(intervalsBuffer, syclQueue);
vklRelease(intervalContext);
// hit iteration
std::cout << std::endl << "\thit iteration" << std::endl << std::endl;
// hit iterator context setup
float values[2] = {32.f, 96.f};
int num_values = 2;
VKLData valuesData = vklNewData(device, num_values, VKL_FLOAT, values);
VKLHitIteratorContext hitContext = vklNewHitIteratorContext(sampler);
vklSetInt(hitContext, "attributeIndex", 0);
vklSetData(hitContext, "values", valuesData);
vklRelease(valuesData);
vklCommit(hitContext);
// ray definition for iterators
// see rayOrigin, Direction and TRange above
char *hitIteratorBuffer =
sycl::malloc_device<char>(vklGetHitIteratorSize(&hitContext), syclQueue);
int *numHits = sycl::malloc_shared<int>(1, syclQueue);
*numHits = 0;
const size_t maxNumHits = 999;
VKLHit *hitBuffer = sycl::malloc_shared<VKLHit>(maxNumHits, syclQueue);
memset(hitBuffer, 0, maxNumHits * sizeof(VKLHit));
std::cout << "\thit iterator for values";
for (const auto &r : values) {
std::cout << " " << r << " ";
}
std::cout << std::endl << std::endl;
syclQueue
.submit([=](sycl::handler &cgh) {
cgh.set_specialization_constant<samplerSpecId>(requiredFeatures);
cgh.single_task([=](sycl::kernel_handler kh) {
const VKLFeatureFlags featureFlags =
kh.get_specialization_constant<samplerSpecId>();
VKLHitIterator hitIterator = vklInitHitIterator(
&hitContext, &rayOrigin, &rayDirection, &rayTRange, time,
(void *)hitIteratorBuffer, featureFlags);
for (;;) {
VKLHit hit;
int result = vklIterateHit(hitIterator, &hit, featureFlags);
if (!result) {
break;
}
hitBuffer[*numHits] = hit;
*numHits = *numHits + 1;
if (*numHits >= maxNumHits) break;
}
});
})
.wait();
for (int i = 0; i < *numHits; ++i) {
std::cout << "\t\tt " << hitBuffer[i].t << std::endl;
std::cout << "\t\tsample " << hitBuffer[i].sample << std::endl;
std::cout << "\t\tepsilon " << hitBuffer[i].epsilon << std::endl
<< std::endl;
}
sycl::free(hitIteratorBuffer, syclQueue);
sycl::free(numHits, syclQueue);
sycl::free(hitBuffer, syclQueue);
vklRelease(hitContext);
vklRelease(sampler);
}
int main() {
auto IntelGPUDeviceSelector = [](const sycl::device &device) {
using namespace sycl::info;
const std::string deviceName = device.get_info<device::name>();
bool match = device.is_gpu() &&
device.get_info<sycl::info::device::vendor_id>() == 0x8086 &&
device.get_backend() == sycl::backend::ext_oneapi_level_zero;
return match ? 1 : -1;
};
sycl::queue syclQueue(IntelGPUDeviceSelector);
sycl::context syclContext = syclQueue.get_context();
std::cout << "Target SYCL device: "
<< syclQueue.get_device().get_info<sycl::info::device::name>()
<< std::endl
<< std::endl;
vklInit();
VKLDevice device = vklNewDevice("gpu");
vklDeviceSetVoidPtr(device, "syclContext", static_cast<void *>(&syclContext));
vklCommitDevice(device);
const int dimensions[] = {128, 128, 128};
const int numVoxels = dimensions[0] * dimensions[1] * dimensions[2];
const int numAttributes = 3;
VKLVolume volume = vklNewVolume(device, "structuredRegular");
vklSetVec3i(volume, "dimensions", dimensions[0], dimensions[1],
dimensions[2]);
vklSetVec3f(volume, "gridOrigin", 0, 0, 0);
vklSetVec3f(volume, "gridSpacing", 1, 1, 1);
std::vector<float> voxels(numVoxels);
// volume attribute 0: x-grad
for (int k = 0; k < dimensions[2]; k++)
for (int j = 0; j < dimensions[1]; j++)
for (int i = 0; i < dimensions[0]; i++)
voxels[k * dimensions[0] * dimensions[1] + j * dimensions[2] + i] =
(float)i;
VKLData data0 = vklNewData(device, numVoxels, VKL_FLOAT, voxels.data());
// volume attribute 1: y-grad
for (int k = 0; k < dimensions[2]; k++)
for (int j = 0; j < dimensions[1]; j++)
for (int i = 0; i < dimensions[0]; i++)
voxels[k * dimensions[0] * dimensions[1] + j * dimensions[2] + i] =
(float)j;
VKLData data1 = vklNewData(device, numVoxels, VKL_FLOAT, voxels.data());
// volume attribute 2: z-grad
for (int k = 0; k < dimensions[2]; k++)
for (int j = 0; j < dimensions[1]; j++)
for (int i = 0; i < dimensions[0]; i++)
voxels[k * dimensions[0] * dimensions[1] + j * dimensions[2] + i] =
(float)k;
VKLData data2 = vklNewData(device, numVoxels, VKL_FLOAT, voxels.data());
VKLData attributes[] = {data0, data1, data2};
VKLData attributesData =
vklNewData(device, numAttributes, VKL_DATA, attributes);
vklRelease(data0);
vklRelease(data1);
vklRelease(data2);
vklSetData(volume, "data", attributesData);
vklRelease(attributesData);
vklCommit(volume);
demoGpuAPI(syclQueue, device, volume);
vklRelease(volume);
vklReleaseDevice(device);
std::cout << "complete." << std::endl;
return 0;
}
| cpp |
oneAPI-samples | data/projects/oneAPI-samples/RenderingToolkit/GettingStarted/03_openvkl_gsg/cpu/src/vklTutorialCPU.c | // Copyright 2019 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
#include <openvkl/openvkl.h>
#include <openvkl/device/openvkl.h>
#include <stdio.h>
#if defined(_MSC_VER)
#include <malloc.h> // _malloca
#include <windows.h> // Sleep
#endif
void demoScalarAPI(VKLDevice device, VKLVolume volume) {
printf("demo of 1-wide API\n");
VKLSampler sampler = vklNewSampler(volume);
vklCommit(sampler);
// bounding box
vkl_box3f bbox = vklGetBoundingBox(volume);
printf("\tbounding box\n");
printf("\t\tlower = %f %f %f\n", bbox.lower.x, bbox.lower.y, bbox.lower.z);
printf("\t\tupper = %f %f %f\n\n", bbox.upper.x, bbox.upper.y, bbox.upper.z);
// number of attributes
unsigned int numAttributes = vklGetNumAttributes(volume);
printf("\tnum attributes = %d\n\n", numAttributes);
// value range for all attributes
for (unsigned int i = 0; i < numAttributes; i++) {
vkl_range1f valueRange = vklGetValueRange(volume, i);
printf("\tvalue range (attribute %u) = (%f %f)\n", i, valueRange.lower,
valueRange.upper);
}
// coordinate for sampling / gradients
vkl_vec3f coord = {1.f, 2.f, 3.f};
printf("\n\tcoord = %f %f %f\n\n", coord.x, coord.y, coord.z);
// sample, gradient (first attribute)
unsigned int attributeIndex = 0;
float time = 0.f;
float sample = vklComputeSample(&sampler, &coord, attributeIndex, time);
vkl_vec3f grad = vklComputeGradient(&sampler, &coord, attributeIndex, time);
printf("\tsampling and gradient computation (first attribute)\n");
printf("\t\tsample = %f\n", sample);
printf("\t\tgrad = %f %f %f\n\n", grad.x, grad.y, grad.z);
// sample (multiple attributes)
unsigned int M = 3;
unsigned int attributeIndices[] = {0, 1, 2};
float samples[3];
vklComputeSampleM(&sampler, &coord, samples, M, attributeIndices, time);
printf("\tsampling (multiple attributes)\n");
printf("\t\tsamples = %f %f %f\n\n", samples[0], samples[1], samples[2]);
// interval iterator context setup
vkl_range1f ranges[2] = {{10, 20}, {50, 75}};
int num_ranges = 2;
VKLData rangesData =
vklNewData(device, num_ranges, VKL_BOX1F, ranges, VKL_DATA_DEFAULT, 0);
VKLIntervalIteratorContext intervalContext =
vklNewIntervalIteratorContext(sampler);
vklSetInt(intervalContext, "attributeIndex", attributeIndex);
vklSetData(intervalContext, "valueRanges", rangesData);
vklRelease(rangesData);
vklCommit(intervalContext);
// hit iterator context setup
float values[2] = {32, 96};
int num_values = 2;
VKLData valuesData =
vklNewData(device, num_values, VKL_FLOAT, values, VKL_DATA_DEFAULT, 0);
VKLHitIteratorContext hitContext = vklNewHitIteratorContext(sampler);
vklSetInt(hitContext, "attributeIndex", attributeIndex);
vklSetData(hitContext, "values", valuesData);
vklRelease(valuesData);
vklCommit(hitContext);
// ray definition for iterators
vkl_vec3f rayOrigin = {0, 1, 1};
vkl_vec3f rayDirection = {1, 0, 0};
vkl_range1f rayTRange = {0, 200};
printf("\trayOrigin = %f %f %f\n", rayOrigin.x, rayOrigin.y, rayOrigin.z);
printf("\trayDirection = %f %f %f\n", rayDirection.x, rayDirection.y,
rayDirection.z);
printf("\trayTRange = %f %f\n", rayTRange.lower, rayTRange.upper);
// interval iteration. This is scoped
{
// Note: buffer will cease to exist at the end of this scope.
#if defined(_MSC_VER)
// MSVC does not support variable length arrays, but provides a
// safer version of alloca.
char *buffer = _malloca(vklGetIntervalIteratorSize(&intervalContext));
#else
char buffer[vklGetIntervalIteratorSize(&intervalContext)];
#endif
VKLIntervalIterator intervalIterator = vklInitIntervalIterator(
&intervalContext, &rayOrigin, &rayDirection, &rayTRange, time, buffer);
printf("\n\tinterval iterator for value ranges {%f %f} {%f %f}\n",
ranges[0].lower, ranges[0].upper, ranges[1].lower, ranges[1].upper);
for (;;) {
VKLInterval interval;
int result = vklIterateInterval(intervalIterator, &interval);
if (!result) break;
printf(
"\t\ttRange (%f %f)\n\t\tvalueRange (%f %f)\n\t\tnominalDeltaT "
"%f\n\n",
interval.tRange.lower, interval.tRange.upper,
interval.valueRange.lower, interval.valueRange.upper,
interval.nominalDeltaT);
}
#if defined(_MSC_VER)
_freea(buffer);
#endif
}
// hit iteration
{
#if defined(_MSC_VER)
// MSVC does not support variable length arrays, but provides a
// safer version of alloca.
char *buffer = _malloca(vklGetHitIteratorSize(&hitContext));
#else
char buffer[vklGetHitIteratorSize(&hitContext)];
#endif
VKLHitIterator hitIterator = vklInitHitIterator(
&hitContext, &rayOrigin, &rayDirection, &rayTRange, time, buffer);
printf("\thit iterator for values %f %f\n", values[0], values[1]);
for (;;) {
VKLHit hit;
int result = vklIterateHit(hitIterator, &hit);
if (!result) break;
printf("\t\tt %f\n\t\tsample %f\n\t\tepsilon %f\n\n", hit.t, hit.sample,
hit.epsilon);
}
#if defined(_MSC_VER)
_freea(buffer);
#endif
}
vklRelease(hitContext);
vklRelease(intervalContext);
vklRelease(sampler);
}
void demoVectorAPI(VKLVolume volume) {
printf("demo of 4-wide API (8- and 16- follow the same pattern)\n");
VKLSampler sampler = vklNewSampler(volume);
vklCommit(sampler);
// structure-of-array layout
vkl_vvec3f4 coord4;
int valid[4];
for (int i = 0; i < 4; i++) {
coord4.x[i] = i * 3 + 0;
coord4.y[i] = i * 3 + 1;
coord4.z[i] = i * 3 + 2;
valid[i] = -1; // valid mask: 0 = not valid, -1 = valid
}
for (int i = 0; i < 4; i++) {
printf("\tcoord[%d] = %f %f %f\n", i, coord4.x[i], coord4.y[i],
coord4.z[i]);
}
// sample, gradient (first attribute)
unsigned int attributeIndex = 0;
float time4[4] = {0.f};
float sample4[4];
vkl_vvec3f4 grad4;
vklComputeSample4(valid, &sampler, &coord4, sample4, attributeIndex, time4);
vklComputeGradient4(valid, &sampler, &coord4, &grad4, attributeIndex, time4);
printf("\n\tsampling and gradient computation (first attribute)\n");
for (int i = 0; i < 4; i++) {
printf("\t\tsample[%d] = %f\n", i, sample4[i]);
printf("\t\tgrad[%d] = %f %f %f\n", i, grad4.x[i], grad4.y[i],
grad4.z[i]);
}
// sample (multiple attributes)
unsigned int M = 3;
unsigned int attributeIndices[] = {0, 1, 2};
float samples[3 * 4];
vklComputeSampleM4(valid, &sampler, &coord4, samples, M, attributeIndices,
time4);
printf("\n\tsampling (multiple attributes)\n");
printf("\t\tsamples = ");
for (unsigned int j = 0; j < M; j++) {
printf("%f %f %f %f\n", samples[j * 4 + 0], samples[j * 4 + 1],
samples[j * 4 + 2], samples[j * 4 + 3]);
printf("\t\t ");
}
printf("\n");
vklRelease(sampler);
}
void demoStreamAPI(VKLVolume volume) {
printf("demo of stream API\n");
VKLSampler sampler = vklNewSampler(volume);
vklCommit(sampler);
// array-of-structure layout; arbitrary stream lengths are supported
vkl_vec3f coord[5];
for (int i = 0; i < 5; i++) {
coord[i].x = i * 3 + 0;
coord[i].y = i * 3 + 1;
coord[i].z = i * 3 + 2;
}
for (int i = 0; i < 5; i++) {
printf("\tcoord[%d] = %f %f %f\n", i, coord[i].x, coord[i].y, coord[i].z);
}
// sample, gradient (first attribute)
printf("\n\tsampling and gradient computation (first attribute)\n");
unsigned int attributeIndex = 0;
float time[5] = {0.f};
float sample[5];
vkl_vec3f grad[5];
vklComputeSampleN(&sampler, 5, coord, sample, attributeIndex, time);
vklComputeGradientN(&sampler, 5, coord, grad, attributeIndex, time);
for (int i = 0; i < 5; i++) {
printf("\t\tsample[%d] = %f\n", i, sample[i]);
printf("\t\tgrad[%d] = %f %f %f\n", i, grad[i].x, grad[i].y, grad[i].z);
}
// sample (multiple attributes)
unsigned int M = 3;
unsigned int attributeIndices[] = {0, 1, 2};
float samples[3 * 5];
vklComputeSampleMN(&sampler, 5, coord, samples, M, attributeIndices, time);
printf("\n\tsampling (multiple attributes)\n");
printf("\t\tsamples = ");
for (int i = 0; i < 5; i++) {
for (unsigned int j = 0; j < M; j++) {
printf("%f ", samples[i * M + j]);
}
printf("\n\t\t ");
}
printf("\n");
vklRelease(sampler);
}
int main() {
vklInit();
VKLDevice device = vklNewDevice("cpu");
vklCommitDevice(device);
const int dimensions[] = {128, 128, 128};
const int numVoxels = dimensions[0] * dimensions[1] * dimensions[2];
const int numAttributes = 3;
VKLVolume volume = vklNewVolume(device, "structuredRegular");
vklSetVec3i(volume, "dimensions", dimensions[0], dimensions[1],
dimensions[2]);
vklSetVec3f(volume, "gridOrigin", 0, 0, 0);
vklSetVec3f(volume, "gridSpacing", 1, 1, 1);
float *voxels = malloc(numVoxels * sizeof(float));
if (!voxels) {
printf("failed to allocate voxel memory!\n");
return 1;
}
// volume attribute 0: x-grad
for (int k = 0; k < dimensions[2]; k++)
for (int j = 0; j < dimensions[1]; j++)
for (int i = 0; i < dimensions[0]; i++)
voxels[k * dimensions[0] * dimensions[1] + j * dimensions[2] + i] =
(float)i;
VKLData data0 =
vklNewData(device, numVoxels, VKL_FLOAT, voxels, VKL_DATA_DEFAULT, 0);
// volume attribute 1: y-grad
for (int k = 0; k < dimensions[2]; k++)
for (int j = 0; j < dimensions[1]; j++)
for (int i = 0; i < dimensions[0]; i++)
voxels[k * dimensions[0] * dimensions[1] + j * dimensions[2] + i] =
(float)j;
VKLData data1 =
vklNewData(device, numVoxels, VKL_FLOAT, voxels, VKL_DATA_DEFAULT, 0);
// volume attribute 2: z-grad
for (int k = 0; k < dimensions[2]; k++)
for (int j = 0; j < dimensions[1]; j++)
for (int i = 0; i < dimensions[0]; i++)
voxels[k * dimensions[0] * dimensions[1] + j * dimensions[2] + i] =
(float)k;
VKLData data2 =
vklNewData(device, numVoxels, VKL_FLOAT, voxels, VKL_DATA_DEFAULT, 0);
VKLData attributes[] = {data0, data1, data2};
VKLData attributesData = vklNewData(device, numAttributes, VKL_DATA,
attributes, VKL_DATA_DEFAULT, 0);
vklRelease(data0);
vklRelease(data1);
vklRelease(data2);
vklSetData(volume, "data", attributesData);
vklRelease(attributesData);
vklCommit(volume);
demoScalarAPI(device, volume);
demoVectorAPI(volume);
demoStreamAPI(volume);
vklRelease(volume);
vklReleaseDevice(device);
free(voxels);
printf("complete.\n");
#if defined(_MSC_VER)
// On Windows, sleep for a few seconds so the terminal window doesn't close
// immediately.
Sleep(3000);
#endif
return 0;
}
| c |
oneAPI-samples | data/projects/oneAPI-samples/RenderingToolkit/GettingStarted/05_ispc_gsg/src/simple.cpp | /*
Copyright (c) 2010-2022, Intel Corporation
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of Intel Corporation nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdlib.h>
// Include the header file that the ispc compiler generates
#include "simple_ispc.h"
using namespace ispc;
int main() {
float vin[16], vout[16];
// Initialize input buffer
for (int i = 0; i < 16; ++i) vin[i] = (float)i;
// Call simple() function from simple.ispc file
simple(vin, vout, 16);
// Print results
for (int i = 0; i < 16; ++i)
printf("%d: simple(%f) = %f\n", i, vin[i], vout[i]);
return 0;
}
| cpp |
oneAPI-samples | data/projects/oneAPI-samples/RenderingToolkit/GettingStarted/04_oidn_gsg/src/common/platform.cpp | // Copyright 2009 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
#include "platform.h"
OIDN_NAMESPACE_BEGIN
// -----------------------------------------------------------------------------------------------
// Common functions
// -----------------------------------------------------------------------------------------------
void* alignedMalloc(size_t size, size_t alignment)
{
if (size == 0)
return nullptr;
assert((alignment & (alignment-1)) == 0);
#if defined(OIDN_ARCH_X64)
void* ptr = _mm_malloc(size, alignment);
#else
void* ptr;
if (posix_memalign(&ptr, max(alignment, sizeof(void*)), size) != 0)
ptr = nullptr;
#endif
if (ptr == nullptr)
throw std::bad_alloc();
return ptr;
}
void alignedFree(void* ptr)
{
if (ptr)
#if defined(OIDN_ARCH_X64)
_mm_free(ptr);
#else
free(ptr);
#endif
}
// -----------------------------------------------------------------------------------------------
// System information
// -----------------------------------------------------------------------------------------------
std::string getOSName()
{
std::string name;
#if defined(__linux__)
name = "Linux";
#elif defined(__FreeBSD__)
name = "FreeBSD";
#elif defined(__CYGWIN__)
name = "Cygwin";
#elif defined(_WIN32)
name = "Windows";
#elif defined(__APPLE__)
name = "macOS";
#elif defined(__unix__)
name = "Unix";
#else
return "Unknown";
#endif
#if defined(__x86_64__) || defined(_M_X64) || defined(__ia64__) || defined(__aarch64__)
name += " (64-bit)";
#else
name += " (32-bit)";
#endif
return name;
}
std::string getCompilerName()
{
#if defined(__INTEL_COMPILER)
int major = __INTEL_COMPILER / 100 % 100;
int minor = __INTEL_COMPILER % 100 / 10;
std::string version = "Intel Compiler ";
version += toString(major);
version += "." + toString(minor);
#if defined(__INTEL_COMPILER_UPDATE)
version += "." + toString(__INTEL_COMPILER_UPDATE);
#endif
return version;
#elif defined(__clang__)
return "Clang " __clang_version__;
#elif defined(__GNUC__)
return "GCC " __VERSION__;
#elif defined(_MSC_VER)
std::string version = toString(_MSC_FULL_VER);
version.insert(4, ".");
version.insert(9, ".");
version.insert(2, ".");
return "Visual C++ Compiler " + version;
#else
return "Unknown";
#endif
}
std::string getBuildName()
{
#if defined(NDEBUG)
return "Release";
#else
return "Debug";
#endif
}
OIDN_NAMESPACE_END
| cpp |
oneAPI-samples | data/projects/oneAPI-samples/RenderingToolkit/GettingStarted/04_oidn_gsg/src/common/common.cpp | // Copyright 2018 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
#include "common.h"
OIDN_NAMESPACE_BEGIN
size_t getDataTypeSize(DataType dataType)
{
switch (dataType)
{
case DataType::Float32: return sizeof(float);
case DataType::Float16: return sizeof(int16_t);
case DataType::UInt8: return 1;
default:
throw std::invalid_argument("invalid data type");
}
}
DataType getFormatDataType(Format format)
{
switch (format)
{
case Format::Float:
case Format::Float2:
case Format::Float3:
case Format::Float4:
return DataType::Float32;
case Format::Half:
case Format::Half2:
case Format::Half3:
case Format::Half4:
return DataType::Float16;
default:
throw std::invalid_argument("invalid format");
}
}
OIDN_NAMESPACE_END | cpp |
oneAPI-samples | data/projects/oneAPI-samples/RenderingToolkit/GettingStarted/04_oidn_gsg/src/common/half.cpp | // Copyright 2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
#include "half.h"
OIDN_NAMESPACE_BEGIN
// https://gist.github.com/rygorous/2156668
namespace
{
typedef unsigned int uint;
union FP32
{
uint u;
float f;
struct
{
uint Mantissa : 23;
uint Exponent : 8;
uint Sign : 1;
};
};
union FP16
{
unsigned short u;
struct
{
uint Mantissa : 10;
uint Exponent : 5;
uint Sign : 1;
};
};
// Original ISPC reference version; this always rounds ties up.
FP16 float_to_half(FP32 f)
{
FP16 o = { 0 };
// Based on ISPC reference code (with minor modifications)
if (f.Exponent == 0) // Signed zero/denormal (which will underflow)
o.Exponent = 0;
else if (f.Exponent == 255) // Inf or NaN (all exponent bits set)
{
o.Exponent = 31;
o.Mantissa = f.Mantissa ? 0x200 : 0; // NaN->qNaN and Inf->Inf
}
else // Normalized number
{
// Exponent unbias the single, then bias the halfp
int newexp = f.Exponent - 127 + 15;
if (newexp >= 31) // Overflow, return signed infinity
o.Exponent = 31;
else if (newexp <= 0) // Underflow
{
if ((14 - newexp) <= 24) // Mantissa might be non-zero
{
uint mant = f.Mantissa | 0x800000; // Hidden 1 bit
o.Mantissa = mant >> (14 - newexp);
if ((mant >> (13 - newexp)) & 1) // Check for rounding
o.u++; // Round, might overflow into exp bit, but this is OK
}
}
else
{
o.Exponent = newexp;
o.Mantissa = f.Mantissa >> 13;
if (f.Mantissa & 0x1000) // Check for rounding
o.u++; // Round, might overflow to inf, this is OK
}
}
o.Sign = f.Sign;
return o;
}
FP32 half_to_float(FP16 h)
{
static const FP32 magic = { 113 << 23 };
static const uint shifted_exp = 0x7c00 << 13; // exponent mask after shift
FP32 o;
o.u = (h.u & 0x7fff) << 13; // exponent/mantissa bits
uint exp = shifted_exp & o.u; // just the exponent
o.u += (127 - 15) << 23; // exponent adjust
// handle exponent special cases
if (exp == shifted_exp) // Inf/NaN?
o.u += (128 - 16) << 23; // extra exp adjust
else if (exp == 0) // Zero/Denormal?
{
o.u += 1 << 23; // extra exp adjust
o.f -= magic.f; // renormalize
}
o.u |= (h.u & 0x8000) << 16; // sign bit
return o;
}
}
float half_to_float(int16_t x)
{
FP16 fp16;
fp16.u = (unsigned short)x;
return half_to_float(fp16).f;
}
int16_t float_to_half(float x)
{
FP32 fp32;
fp32.f = x;
return (int16_t)float_to_half(fp32).u;
}
OIDN_NAMESPACE_END | cpp |
oneAPI-samples | data/projects/oneAPI-samples/RenderingToolkit/GettingStarted/04_oidn_gsg/src/common/common.h | // Copyright 2018 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include "oidn_utils.h" // must be included before platform.h
#include "platform.h"
OIDN_NAMESPACE_BEGIN
// Synchronization mode for operations
enum class SyncMode
{
Sync, // synchronous
Async // asynchronous
};
// Data types sorted by precision in ascending order
enum class DataType
{
UInt8,
Float16,
Float32,
};
template<typename T>
struct DataTypeOf;
template<> struct DataTypeOf<float> { static constexpr DataType value = DataType::Float32; };
template<> struct DataTypeOf<half> { static constexpr DataType value = DataType::Float16; };
template<> struct DataTypeOf<uint8_t> { static constexpr DataType value = DataType::UInt8; };
// Returns the size of a data type in bytes
size_t getDataTypeSize(DataType dataType);
// Returns the data type of a format
DataType getFormatDataType(Format format);
OIDN_NAMESPACE_END | h |
oneAPI-samples | data/projects/oneAPI-samples/RenderingToolkit/GettingStarted/04_oidn_gsg/src/common/timer.h | // Copyright 2018 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include "common/platform.h"
#include <chrono>
OIDN_NAMESPACE_BEGIN
class Timer
{
public:
Timer()
{
reset();
}
void reset()
{
start = clock::now();
}
double query() const
{
auto end = clock::now();
return std::chrono::duration_cast<std::chrono::duration<double>>(end - start).count();
}
private:
using clock = std::chrono::steady_clock;
std::chrono::time_point<clock> start;
};
OIDN_NAMESPACE_END
| h |
oneAPI-samples | data/projects/oneAPI-samples/RenderingToolkit/GettingStarted/04_oidn_gsg/src/common/half.h | // Copyright 2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include "include/OpenImageDenoise/config.h"
#include <cstdint>
OIDN_NAMESPACE_BEGIN
float half_to_float(int16_t x);
int16_t float_to_half(float x);
// Minimal half data type
class half
{
public:
half() = default;
half(const half& h) : x(h.x) {}
half(float f) : x(float_to_half(f)) {}
half& operator =(const half& h) { x = h.x; return *this; }
half& operator =(float f) { x = float_to_half(f); return *this; }
operator float() const { return half_to_float(x); }
private:
int16_t x;
};
OIDN_NAMESPACE_END | h |
oneAPI-samples | data/projects/oneAPI-samples/RenderingToolkit/GettingStarted/04_oidn_gsg/src/common/oidn_utils.h | // Copyright 2018 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include "include/OpenImageDenoise/oidn.hpp"
#include <iostream>
#include <iomanip>
OIDN_NAMESPACE_BEGIN
// Returns the size of a format in bytes
size_t getFormatSize(Format format);
std::ostream& operator <<(std::ostream& sm, Format format);
std::ostream& operator <<(std::ostream& sm, DeviceType deviceType);
std::istream& operator >>(std::istream& sm, DeviceType& deviceType);
std::ostream& operator <<(std::ostream& sm, Quality quality);
std::ostream& operator <<(std::ostream& sm, const UUID& uuid);
std::ostream& operator <<(std::ostream& sm, const LUID& luid);
OIDN_NAMESPACE_END | h |
oneAPI-samples | data/projects/oneAPI-samples/RenderingToolkit/GettingStarted/04_oidn_gsg/src/common/platform.h | // Copyright 2009 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include "include/OpenImageDenoise/config.h"
// -------------------------------------------------------------------------------------------------
// Macros
// -------------------------------------------------------------------------------------------------
#if defined(__x86_64__) || defined(_M_X64)
#define OIDN_ARCH_X64
#elif defined(__aarch64__)
#define OIDN_ARCH_ARM64
#endif
#if defined(SYCL_LANGUAGE_VERSION)
#define OIDN_COMPILE_SYCL
#endif
#if defined(__SYCL_DEVICE_ONLY__)
#define OIDN_COMPILE_SYCL_DEVICE
#endif
#if defined(__CUDACC__)
#define OIDN_COMPILE_CUDA
#endif
#if defined(__CUDA_ARCH__)
#define OIDN_COMPILE_CUDA_DEVICE
#endif
#if defined(__HIPCC__)
#define OIDN_COMPILE_HIP
#endif
#if defined(__HIP_DEVICE_COMPILE__)
#define OIDN_COMPILE_HIP_DEVICE
#endif
#if defined(OIDN_COMPILE_SYCL_DEVICE) || defined(OIDN_COMPILE_CUDA_DEVICE) || defined(OIDN_COMPILE_HIP_DEVICE)
#define OIDN_COMPILE_DEVICE
#endif
#if defined(_WIN32)
// Windows
#define OIDN_INLINE __forceinline
#define OIDN_NOINLINE __declspec(noinline)
#else
// Unix
#define OIDN_INLINE inline __attribute__((always_inline))
#define OIDN_NOINLINE __attribute__((noinline))
#endif
#ifndef UNUSED
#define UNUSED(x) ((void)x)
#endif
#ifndef MAYBE_UNUSED
#define MAYBE_UNUSED(x) UNUSED(x)
#endif
#if defined(OIDN_COMPILE_CUDA) || defined(OIDN_COMPILE_HIP)
#define OIDN_DEVICE __device__
#define OIDN_DEVICE_INLINE __device__ OIDN_INLINE
#define OIDN_HOST_DEVICE __host__ __device__
#define OIDN_HOST_DEVICE_INLINE __host__ __device__ OIDN_INLINE
#define OIDN_SHARED __shared__
#else
#define OIDN_DEVICE
#define OIDN_DEVICE_INLINE OIDN_INLINE
#define OIDN_HOST_DEVICE
#define OIDN_HOST_DEVICE_INLINE OIDN_INLINE
#define OIDN_SHARED
#endif
// Helper string macros
#define OIDN_TO_STRING2(a) #a
#define OIDN_TO_STRING(a) OIDN_TO_STRING2(a)
#define OIDN_CONCAT2(a, b) a##b
#define OIDN_CONCAT(a, b) OIDN_CONCAT2(a, b)
// -------------------------------------------------------------------------------------------------
// Includes
// -------------------------------------------------------------------------------------------------
#if defined(_WIN32)
#if !defined(WIN32_LEAN_AND_MEAN)
#define WIN32_LEAN_AND_MEAN
#endif
#if !defined(NOMINMAX)
#define NOMINMAX
#endif
#include <Windows.h>
#elif defined(__APPLE__)
#include <sys/sysctl.h>
#endif
#if defined(OIDN_ARCH_X64)
#include <xmmintrin.h>
#include <pmmintrin.h>
#endif
#include <cstdint>
#include <cstddef>
#include <cstdlib>
#include <climits>
#include <cstring>
#include <limits>
#include <atomic>
#include <algorithm>
#include <memory>
#include <array>
#include <type_traits>
#include <cmath>
#include <cfloat>
#include <string>
#include <sstream>
#include <iostream>
#include <cassert>
#if defined(OIDN_COMPILE_SYCL)
#include <sycl/sycl.hpp>
#include <sycl/ext/intel/esimd.hpp>
#endif
#if defined(OIDN_COMPILE_CUDA)
#include <cuda_fp16.h>
#elif defined(OIDN_COMPILE_HIP)
#include <hip/hip_runtime.h>
#include <hip/hip_fp16.h>
#elif !defined(OIDN_COMPILE_SYCL)
#include "half.h"
#endif
OIDN_NAMESPACE_BEGIN
#if defined(OIDN_COMPILE_SYCL)
namespace syclx = sycl::ext::intel::experimental;
namespace esimd = sycl::ext::intel::esimd;
namespace esimdx = sycl::ext::intel::experimental::esimd;
using sycl::half;
#endif
template<bool B, class T = void>
using enable_if_t = typename std::enable_if<B, T>::type;
// -----------------------------------------------------------------------------------------------
// Error handling and debugging
// -----------------------------------------------------------------------------------------------
struct Verbose
{
int verbose;
Verbose(int v = 0) : verbose(v) {}
OIDN_INLINE bool isVerbose(int v = 1) const { return v <= verbose; }
};
#define OIDN_WARNING(message) { if (isVerbose()) std::cerr << "Warning: " << message << std::endl; }
#define OIDN_FATAL(message) throw std::runtime_error(message);
// -----------------------------------------------------------------------------------------------
// Common functions
// -----------------------------------------------------------------------------------------------
template<typename T>
OIDN_HOST_DEVICE_INLINE constexpr T min(T a, T b) { return (b < a) ? b : a; }
template<typename T>
OIDN_HOST_DEVICE_INLINE constexpr T max(T a, T b) { return (a < b) ? b : a; }
template<typename T>
OIDN_HOST_DEVICE_INLINE constexpr T clamp(T x, T minVal, T maxVal)
{
return min(max(x, minVal), maxVal);
}
// Returns ceil(a / b) for non-negative integers
template<typename Int, typename IntB>
OIDN_HOST_DEVICE_INLINE constexpr Int ceil_div(Int a, IntB b)
{
//assert(a >= 0);
//assert(b > 0);
return (a + b - 1) / b;
}
// Returns a rounded up to multiple of b
template<typename Int, typename IntB>
OIDN_HOST_DEVICE_INLINE constexpr Int round_up(Int a, IntB b)
{
return ceil_div(a, b) * b;
}
// -----------------------------------------------------------------------------------------------
// Memory allocation
// -----------------------------------------------------------------------------------------------
constexpr size_t memoryAlignment = 128;
void* alignedMalloc(size_t size, size_t alignment = memoryAlignment);
void alignedFree(void* ptr);
// -----------------------------------------------------------------------------------------------
// String functions
// -----------------------------------------------------------------------------------------------
template<typename T>
inline std::string toString(const T& a)
{
std::stringstream sm;
sm << a;
return sm.str();
}
template<typename T>
inline T fromString(const std::string& str)
{
std::stringstream sm(str);
T a{};
sm >> a;
return a;
}
template<>
inline std::string fromString(const std::string& str)
{
return str;
}
inline std::string toLower(const std::string& str)
{
std::string result = str;
std::transform(str.begin(), str.end(), result.begin(), ::tolower);
return result;
}
#if defined(__APPLE__)
template<typename T>
inline bool getSysctl(const char* name, T& value)
{
int64_t result = 0;
size_t size = sizeof(result);
if (sysctlbyname(name, &result, &size, nullptr, 0) != 0)
return false;
value = T(result);
return true;
}
#endif
inline bool isEnvVar(const std::string& name)
{
auto* str = getenv(name.c_str());
return (str != nullptr);
}
template<typename T>
inline bool getEnvVar(const std::string& name, T& value)
{
auto* str = getenv(name.c_str());
bool found = (str != nullptr);
if (found)
value = fromString<T>(str);
return found;
}
template<typename T>
inline T getEnvVarOrDefault(const std::string& name, const T& defaultValue)
{
T value = defaultValue;
getEnvVar(name, value);
return value;
}
template<typename T>
inline bool setEnvVar(const std::string& name, const T& value, bool overwrite)
{
const std::string valueStr = toString(value);
#if defined(_WIN32)
if (overwrite || !isEnvVar(name))
return _putenv_s(name.c_str(), valueStr.c_str()) == 0;
else
return true;
#else
return setenv(name.c_str(), valueStr.c_str(), overwrite) == 0;
#endif
}
// -----------------------------------------------------------------------------------------------
// System information
// -----------------------------------------------------------------------------------------------
std::string getOSName();
std::string getCompilerName();
std::string getBuildName();
OIDN_NAMESPACE_END
| h |
oneAPI-samples | data/projects/oneAPI-samples/RenderingToolkit/GettingStarted/04_oidn_gsg/src/common/oidn_utils.cpp | // Copyright 2018 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
#include "oidn_utils.h"
#include "platform.h"
OIDN_NAMESPACE_BEGIN
size_t getFormatSize(Format format)
{
switch (format)
{
case Format::Undefined: return 0;
case Format::Float: return sizeof(float);
case Format::Float2: return sizeof(float)*2;
case Format::Float3: return sizeof(float)*3;
case Format::Float4: return sizeof(float)*4;
case Format::Half: return sizeof(int16_t);
case Format::Half2: return sizeof(int16_t)*2;
case Format::Half3: return sizeof(int16_t)*3;
case Format::Half4: return sizeof(int16_t)*4;
default:
throw std::invalid_argument("invalid format");
}
}
std::ostream& operator <<(std::ostream& sm, Format format)
{
switch (format)
{
case Format::Float: sm << "f"; break;
case Format::Float2: sm << "f2"; break;
case Format::Float3: sm << "f3"; break;
case Format::Float4: sm << "f4"; break;
case Format::Half: sm << "h"; break;
case Format::Half2: sm << "h2"; break;
case Format::Half3: sm << "h3"; break;
case Format::Half4: sm << "h4"; break;
default: sm << "?"; break;
}
return sm;
}
std::ostream& operator <<(std::ostream& sm, DeviceType deviceType)
{
switch (deviceType)
{
case DeviceType::Default: sm << "default"; break;
case DeviceType::CPU: sm << "CPU"; break;
case DeviceType::SYCL: sm << "SYCL"; break;
case DeviceType::CUDA: sm << "CUDA"; break;
case DeviceType::HIP: sm << "HIP"; break;
default:
throw std::invalid_argument("invalid device type");
}
return sm;
}
std::istream& operator >>(std::istream& sm, DeviceType& deviceType)
{
std::string str;
sm >> str;
str = toLower(str);
if (str == "default")
deviceType = DeviceType::Default;
else if (str == "cpu")
deviceType = DeviceType::CPU;
else if (str == "sycl")
deviceType = DeviceType::SYCL;
else if (str == "cuda")
deviceType = DeviceType::CUDA;
else if (str == "hip")
deviceType = DeviceType::HIP;
else
throw std::invalid_argument("invalid device type");
return sm;
}
std::ostream& operator <<(std::ostream& sm, Quality quality)
{
switch (quality)
{
case Quality::Default: sm << "default"; break;
case Quality::High: sm << "high"; break;
case Quality::Balanced: sm << "balanced"; break;
default:
throw std::invalid_argument("invalid quality mode");
}
return sm;
}
std::ostream& operator <<(std::ostream& sm, const UUID& uuid)
{
auto flags = sm.flags();
for (size_t i = 0; i < sizeof(uuid.bytes); ++i)
sm << std::hex << std::setw(2) << std::setfill('0') << int(uuid.bytes[i]);
sm.flags(flags);
return sm;
}
std::ostream& operator <<(std::ostream& sm, const LUID& luid)
{
auto flags = sm.flags();
for (size_t i = 0; i < sizeof(luid.bytes); ++i)
sm << std::hex << std::setw(2) << std::setfill('0') << int(luid.bytes[i]);
sm.flags(flags);
return sm;
}
OIDN_NAMESPACE_END | cpp |
oneAPI-samples | data/projects/oneAPI-samples/RenderingToolkit/GettingStarted/04_oidn_gsg/src/apps/oidnDenoise.cpp | // Copyright 2018 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
#include "common/common.h"
#include "common/timer.h"
#include "utils/arg_parser.h"
#include "utils/image_io.h"
#include "utils/device_info.h"
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cassert>
#include <limits>
#include <cmath>
#include <signal.h>
#ifdef VTUNE
#include <ittnotify.h>
#endif
OIDN_NAMESPACE_USING
void printUsage()
{
std::cout << "Intel(R) Open Image Denoise" << std::endl;
std::cout << "usage: oidnDenoise [-d/--device [0-9]+|default|cpu|sycl|cuda|hip]" << std::endl
<< " [-f/--filter RT|RTLightmap]" << std::endl
<< " [--hdr color.pfm] [--ldr color.pfm] [--srgb] [--dir directional.pfm]" << std::endl
<< " [--alb albedo.pfm] [--nrm normal.pfm] [--clean_aux]" << std::endl
<< " [--is/--input_scale value]" << std::endl
<< " [-o/--output output.pfm] [-r/--ref reference_output.pfm]" << std::endl
<< " [-t/--type float|half]" << std::endl
<< " [-q/--quality default|h|high|b|balanced]" << std::endl
<< " [-w/--weights weights.tza]" << std::endl
<< " [--threads n] [--affinity 0|1] [--maxmem MB] [--inplace]" << std::endl
<< " [-n times_to_run] [-v/--verbose 0-3]" << std::endl
<< " [--ld|--list_devices] [-h/--help]" << std::endl;
}
void errorCallback(void* userPtr, Error error, const char* message)
{
throw std::runtime_error(message);
}
volatile bool isCancelled = false;
void signalHandler(int signal)
{
isCancelled = true;
}
bool progressCallback(void* userPtr, double n)
{
if (isCancelled)
{
std::cout << std::endl;
return false;
}
std::cout << "\rDenoising " << int(n * 100.) << "%" << std::flush;
return true;
}
std::vector<char> loadFile(const std::string& filename)
{
std::ifstream file(filename, std::ios::binary);
if (file.fail())
throw std::runtime_error("cannot open file: '" + filename + "'");
file.seekg(0, file.end);
const size_t size = file.tellg();
file.seekg(0, file.beg);
std::vector<char> buffer(size);
file.read(buffer.data(), size);
if (file.fail())
throw std::runtime_error("error reading from file: '" + filename + "'");
return buffer;
}
int main(int argc, char* argv[])
{
DeviceType deviceType = DeviceType::Default;
PhysicalDeviceRef physicalDevice;
std::string filterType = "RT";
std::string colorFilename, albedoFilename, normalFilename;
std::string outputFilename, refFilename;
std::string weightsFilename;
Quality quality = Quality::Default;
bool hdr = false;
bool srgb = false;
bool directional = false;
float inputScale = std::numeric_limits<float>::quiet_NaN();
bool cleanAux = false;
Format dataType = Format::Undefined;
int numRuns = 1;
int numThreads = -1;
int setAffinity = -1;
int maxMemoryMB = -1;
bool inplace = false;
int verbose = -1;
// Parse the arguments
if (argc == 1)
{
printUsage();
return 1;
}
try
{
ArgParser args(argc, argv);
while (args.hasNext())
{
std::string opt = args.getNextOpt();
if (opt == "d" || opt == "dev" || opt == "device")
{
std::string value = args.getNext();
if (isdigit(value[0]))
physicalDevice = fromString<int>(value);
else
deviceType = fromString<DeviceType>(value);
}
else if (opt == "f" || opt == "filter")
filterType = args.getNextValue();
else if (opt == "hdr")
{
colorFilename = args.getNextValue();
hdr = true;
}
else if (opt == "ldr")
{
colorFilename = args.getNextValue();
hdr = false;
}
else if (opt == "srgb")
srgb = true;
else if (opt == "dir")
{
colorFilename = args.getNextValue();
directional = true;
}
else if (opt == "alb" || opt == "albedo")
albedoFilename = args.getNextValue();
else if (opt == "nrm" || opt == "normal")
normalFilename = args.getNextValue();
else if (opt == "o" || opt == "out" || opt == "output")
outputFilename = args.getNextValue();
else if (opt == "r" || opt == "ref" || opt == "reference")
refFilename = args.getNextValue();
else if (opt == "is" || opt == "input_scale" || opt == "input-scale" || opt == "inputScale" || opt == "inputscale")
inputScale = args.getNextValue<float>();
else if (opt == "clean_aux" || opt == "clean-aux" || opt == "cleanAux" || opt == "cleanaux")
cleanAux = true;
else if (opt == "t" || opt == "type")
{
const auto val = toLower(args.getNextValue());
if (val == "f" || val == "float" || val == "fp32")
dataType = Format::Float;
else if (val == "h" || val == "half" || val == "fp16")
dataType = Format::Half;
else
throw std::runtime_error("invalid data type");
}
else if (opt == "q" || opt == "quality")
{
const auto val = toLower(args.getNextValue());
if (val == "default")
quality = Quality::Default;
else if (val == "h" || val == "high")
quality = Quality::High;
else if (val == "b" || val == "balanced")
quality = Quality::Balanced;
else
throw std::runtime_error("invalid filter quality mode");
}
else if (opt == "w" || opt == "weights")
weightsFilename = args.getNextValue();
else if (opt == "n")
numRuns = std::max(args.getNextValue<int>(), 1);
else if (opt == "threads")
numThreads = args.getNextValue<int>();
else if (opt == "affinity")
setAffinity = args.getNextValue<int>();
else if (opt == "maxmem" || opt == "maxMemoryMB")
maxMemoryMB = args.getNextValue<int>();
else if (opt == "inplace")
inplace = true;
else if (opt == "v" || opt == "verbose")
verbose = args.getNextValue<int>();
else if (opt == "ld" || opt == "list_devices" || opt == "list-devices" || opt == "listDevices" || opt == "listdevices")
return printPhysicalDevices();
else if (opt == "h" || opt == "help")
{
printUsage();
return 1;
}
else
throw std::invalid_argument("invalid argument '" + opt + "'");
}
#if defined(OIDN_ARCH_X64)
// Set MXCSR flags
if (!refFilename.empty())
{
// In reference mode we have to disable the FTZ and DAZ flags to get accurate results
_MM_SET_FLUSH_ZERO_MODE(_MM_FLUSH_ZERO_OFF);
_MM_SET_DENORMALS_ZERO_MODE(_MM_DENORMALS_ZERO_OFF);
}
else
{
// Enable the FTZ and DAZ flags to maximize performance
_MM_SET_FLUSH_ZERO_MODE(_MM_FLUSH_ZERO_ON);
_MM_SET_DENORMALS_ZERO_MODE(_MM_DENORMALS_ZERO_ON);
}
#endif
// Initialize the denoising device
std::cout << "Initializing device" << std::endl;
Timer timer;
DeviceRef device;
if (physicalDevice)
device = physicalDevice.newDevice();
else
device = newDevice(deviceType);
const char* errorMessage;
if (device.getError(errorMessage) != Error::None)
throw std::runtime_error(errorMessage);
device.setErrorFunction(errorCallback);
if (numThreads > 0)
device.set("numThreads", numThreads);
if (setAffinity >= 0)
device.set("setAffinity", bool(setAffinity));
if (verbose >= 0)
device.set("verbose", verbose);
device.commit();
const double deviceInitTime = timer.query();
deviceType = device.get<DeviceType>("type");
const int versionMajor = device.get<int>("versionMajor");
const int versionMinor = device.get<int>("versionMinor");
const int versionPatch = device.get<int>("versionPatch");
std::cout << " device=" << deviceType
<< ", version=" << versionMajor << "." << versionMinor << "." << versionPatch
<< ", msec=" << (1000. * deviceInitTime) << std::endl;
// Load the input image
std::shared_ptr<ImageBuffer> input, ref;
std::shared_ptr<ImageBuffer> color, albedo, normal;
std::cout << "Loading input" << std::endl;
if (!albedoFilename.empty())
input = albedo = loadImage(device, albedoFilename, 3, false, dataType);
if (!normalFilename.empty())
input = normal = loadImage(device, normalFilename, 3, dataType);
if (!colorFilename.empty())
input = color = loadImage(device, colorFilename, 3, srgb, dataType);
if (!input)
throw std::runtime_error("no input image specified");
if (!refFilename.empty())
{
ref = loadImage(device, refFilename, 3, srgb, dataType);
if (ref->getDims() != input->getDims())
throw std::runtime_error("invalid reference output image");
}
const int width = input->getW();
const int height = input->getH();
std::cout << "Resolution: " << width << "x" << height << std::endl;
// Initialize the output image
std::shared_ptr<ImageBuffer> output;
if (inplace)
output = input;
else
output = std::make_shared<ImageBuffer>(device, width, height, 3, input->getDataType());
std::shared_ptr<ImageBuffer> inputCopy;
if (inplace && numRuns > 1)
inputCopy = input->clone();
// Load the filter weights if specified
std::vector<char> weights;
if (!weightsFilename.empty())
{
std::cout << "Loading filter weights" << std::endl;
weights = loadFile(weightsFilename);
}
// Initialize the denoising filter
std::cout << "Initializing filter" << std::endl;
timer.reset();
FilterRef filter = device.newFilter(filterType.c_str());
if (color)
filter.setImage("color", color->getBuffer(), color->getFormat(), color->getW(), color->getH());
if (albedo)
filter.setImage("albedo", albedo->getBuffer(), albedo->getFormat(), albedo->getW(), albedo->getH());
if (normal)
filter.setImage("normal", normal->getBuffer(), normal->getFormat(), normal->getW(), normal->getH());
filter.setImage("output", output->getBuffer(), output->getFormat(), output->getW(), output->getH());
if (filterType == "RT")
{
if (hdr)
filter.set("hdr", true);
if (srgb)
filter.set("srgb", true);
}
else if (filterType == "RTLightmap")
{
if (directional)
filter.set("directional", true);
}
if (std::isfinite(inputScale))
filter.set("inputScale", inputScale);
if (cleanAux)
filter.set("cleanAux", cleanAux);
if (quality != Quality::Default)
filter.set("quality", quality);
if (maxMemoryMB >= 0)
filter.set("maxMemoryMB", maxMemoryMB);
if (!weights.empty())
filter.setData("weights", weights.data(), weights.size());
const bool showProgress = verbose <= 1;
if (showProgress)
{
filter.setProgressMonitorFunction(progressCallback);
signal(SIGINT, signalHandler);
}
filter.commit();
const double filterInitTime = timer.query();
std::cout << " filter=" << filterType
<< ", msec=" << (1000. * filterInitTime) << std::endl;
// Denoise the image
uint32_t prevHash = 0;
for (int run = 0; run < numRuns; ++run)
{
if (inplace && run > 0)
memcpy(input->getData(), inputCopy->getData(), inputCopy->getByteSize());
if (!showProgress)
std::cout << "Denoising" << std::endl;
timer.reset();
filter.execute();
const double denoiseTime = timer.query();
if (showProgress)
std::cout << std::endl;
std::cout << " msec=" << (1000. * denoiseTime);
if (numRuns > 1 || verbose >= 2)
{
// Compute a hash of the output
const size_t numBytes = output->getByteSize();
const uint8_t* outputBytes = static_cast<const uint8_t*>(output->getData());
uint32_t hash = 0x811c9dc5;
for (size_t i = 0; i < numBytes; ++i)
{
hash ^= outputBytes[i];
hash *= 0x1000193;
}
std::cout << ", hash=" << std::hex << std::setfill('0') << std::setw(8) << hash << std::dec << std::endl;
if (run > 0 && hash != prevHash)
throw std::runtime_error("output hash mismatch (non-deterministic output)");
prevHash = hash;
}
else
std::cout << std::endl;
if (run == 0 && ref)
{
// Verify the output values
std::cout << "Verifying output" << std::endl;
const double errorThreshold = (input == normal || directional) ? 0.05 : 0.003;
size_t numErrors;
double avgError;
std::tie(numErrors, avgError) = compareImage(*output, *ref, errorThreshold);
std::cout << " values=" << output->getSize()
<< ", errors=" << numErrors << ", avgerror=" << avgError << std::endl;
if (numErrors > 0)
{
// Save debug images
std::cout << "Saving debug images" << std::endl;
saveImage("denoise_in.pfm", *input, srgb);
saveImage("denoise_out.pfm", *output, srgb);
saveImage("denoise_ref.pfm", *ref, srgb);
throw std::runtime_error("output does not match the reference");
}
}
}
if (showProgress)
{
filter.setProgressMonitorFunction(nullptr);
signal(SIGINT, SIG_DFL);
}
if (!outputFilename.empty())
{
// Save output image
std::cout << "Saving output" << std::endl;
saveImage(outputFilename, *output, srgb);
}
}
catch (const std::exception& e)
{
std::cerr << "Error: " << e.what() << std::endl;
return 1;
}
return 0;
}
| cpp |
oneAPI-samples | data/projects/oneAPI-samples/RenderingToolkit/GettingStarted/04_oidn_gsg/src/apps/utils/image_buffer.h | // Copyright 2018 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include "common/common.h"
#include <memory>
#include <string>
#include <vector>
#include <array>
#include <tuple>
OIDN_NAMESPACE_BEGIN
class ImageBuffer
{
public:
ImageBuffer();
ImageBuffer(const DeviceRef& device, int width, int height, int numChannels,
Format dataType = Format::Float,
Storage storage = Storage::Undefined,
bool forceHostCopy = false);
~ImageBuffer();
OIDN_INLINE int getW() const { return width; }
OIDN_INLINE int getH() const { return height; }
OIDN_INLINE int getC() const { return numChannels; }
std::array<int, 3> getDims() const { return {width, height, numChannels}; }
OIDN_INLINE Format getDataType() const { return dataType; }
Format getFormat() const
{
if (dataType == Format::Undefined)
return Format::Undefined;
return Format(int(dataType) + numChannels - 1);
}
OIDN_INLINE size_t getSize() const { return numValues; }
OIDN_INLINE size_t getByteSize() const { return byteSize; }
OIDN_INLINE const void* getData() const { return devPtr; }
OIDN_INLINE void* getData() { return devPtr; }
OIDN_INLINE const BufferRef& getBuffer() const { return buffer; }
OIDN_INLINE operator bool() const { return devPtr != nullptr; }
// Data with device storage must be explicitly copied between host and device
void toHost();
void toHostAsync();
void toDevice();
void toDeviceAsync();
template<typename T = float>
T get(size_t i) const;
OIDN_INLINE void set(size_t i, float x)
{
switch (dataType)
{
case Format::Float:
reinterpret_cast<float*>(hostPtr)[i] = x;
break;
case Format::Half:
reinterpret_cast<half*>(hostPtr)[i] = half(x);
break;
default:
assert(0);
}
}
OIDN_INLINE void set(size_t i, half x)
{
switch (dataType)
{
case Format::Float:
reinterpret_cast<float*>(hostPtr)[i] = float(x);
break;
case Format::Half:
reinterpret_cast<half*>(hostPtr)[i] = x;
break;
default:
assert(0);
}
}
// Returns a copy of the image buffer
std::shared_ptr<ImageBuffer> clone() const;
private:
// Disable copying
ImageBuffer(const ImageBuffer&) = delete;
ImageBuffer& operator =(const ImageBuffer&) = delete;
DeviceRef device;
BufferRef buffer;
char* devPtr;
char* hostPtr;
size_t byteSize;
size_t numValues;
int width;
int height;
int numChannels;
Format dataType;
};
template<>
OIDN_INLINE float ImageBuffer::get(size_t i) const
{
switch (dataType)
{
case Format::Float:
return reinterpret_cast<float*>(hostPtr)[i];
case Format::Half:
return float(reinterpret_cast<half*>(hostPtr)[i]);
default:
assert(0);
return 0;
}
}
template<>
OIDN_INLINE half ImageBuffer::get(size_t i) const
{
switch (dataType)
{
case Format::Float:
return half(reinterpret_cast<float*>(hostPtr)[i]);
case Format::Half:
return reinterpret_cast<half*>(hostPtr)[i];
default:
assert(0);
return 0;
}
}
// Compares an image to a reference image and returns the number of errors
// and the average error value
std::tuple<size_t, double> compareImage(const ImageBuffer& image,
const ImageBuffer& ref,
double errorThreshold = 0.005);
OIDN_NAMESPACE_END
| h |
oneAPI-samples | data/projects/oneAPI-samples/RenderingToolkit/GettingStarted/04_oidn_gsg/src/apps/utils/image_io.cpp | // Copyright 2018 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
#include "image_io.h"
#include <fstream>
#if defined(OIDN_USE_OPENIMAGEIO)
#include <OpenImageIO/imageio.h>
#endif
OIDN_NAMESPACE_BEGIN
namespace
{
inline float srgbForward(float y)
{
return (y <= 0.0031308f) ? (12.92f * y) : (1.055f * std::pow(y, 1.f/2.4f) - 0.055f);
}
inline float srgbInverse(float x)
{
return (x <= 0.04045f) ? (x / 12.92f) : std::pow((x + 0.055f) / 1.055f, 2.4f);
}
void srgbForward(ImageBuffer& image)
{
for (size_t i = 0; i < image.getSize(); ++i)
image.set(i, srgbForward(image.get(i)));
}
void srgbInverse(ImageBuffer& image)
{
for (size_t i = 0; i < image.getSize(); ++i)
image.set(i, srgbInverse(image.get(i)));
}
std::string getExtension(const std::string& filename)
{
const size_t pos = filename.find_last_of('.');
if (pos == std::string::npos)
return ""; // no extension
else
{
std::string ext = filename.substr(pos + 1);
for (auto& c : ext) c = tolower(c);
return ext;
}
}
std::shared_ptr<ImageBuffer> loadImagePFM(const DeviceRef& device,
const std::string& filename,
int numChannels,
Format dataType)
{
// Open the file
std::ifstream file(filename, std::ios::binary);
if (file.fail())
throw std::runtime_error("cannot open image file: '" + filename + "'");
// Read the header
std::string id;
file >> id;
int C;
if (id == "PF")
C = 3;
else if (id == "Pf")
C = 1;
else
throw std::runtime_error("invalid PFM image");
if (numChannels == 0)
numChannels = C;
else if (C < numChannels)
throw std::runtime_error("not enough image channels");
if (dataType == Format::Undefined)
dataType = Format::Float;
int H, W;
file >> W >> H;
float scale;
file >> scale;
file.get(); // skip newline
if (file.fail())
throw std::runtime_error("invalid PFM image");
if (scale >= 0.f)
throw std::runtime_error("big-endian PFM images are not supported");
scale = fabs(scale);
// Read the pixels
auto image = std::make_shared<ImageBuffer>(device, W, H, numChannels, dataType);
for (int h = 0; h < H; ++h)
{
for (int w = 0; w < W; ++w)
{
for (int c = 0; c < C; ++c)
{
float x;
file.read((char*)&x, sizeof(float));
if (c < numChannels)
image->set((size_t(H-1-h)*W + w) * numChannels + c, x * scale);
}
}
}
if (file.fail())
throw std::runtime_error("invalid PFM image");
return image;
}
void saveImagePFM(const std::string& filename, const ImageBuffer& image)
{
const int H = image.getH();
const int W = image.getW();
const int C = image.getC();
std::string id;
if (C == 3)
id = "PF";
else if (C == 1)
id = "Pf";
else
throw std::runtime_error("unsupported number of channels");
// Open the file
std::ofstream file(filename, std::ios::binary);
if (file.fail())
throw std::runtime_error("cannot open image file: '" + filename + "'");
// Write the header
file << id << std::endl;
file << W << " " << H << std::endl;
file << "-1.0" << std::endl;
// Write the pixels
for (int h = 0; h < H; ++h)
{
for (int w = 0; w < W; ++w)
{
for (int c = 0; c < C; ++c)
{
const float x = image.get((size_t(H-1-h)*W + w) * C + c);
file.write((char*)&x, sizeof(x));
}
}
}
}
std::shared_ptr<ImageBuffer> loadImagePHM(const DeviceRef& device,
const std::string& filename,
int numChannels,
Format dataType)
{
// Open the file
std::ifstream file(filename, std::ios::binary);
if (file.fail())
throw std::runtime_error("cannot open image file: '" + filename + "'");
// Read the header
std::string id;
file >> id;
int C;
if (id == "PH")
C = 3;
else if (id == "Ph")
C = 1;
else
throw std::runtime_error("invalid PHM image");
if (numChannels == 0)
numChannels = C;
else if (C < numChannels)
throw std::runtime_error("not enough image channels");
if (dataType == Format::Undefined)
dataType = Format::Half;
int H, W;
file >> W >> H;
float scale;
file >> scale;
file.get(); // skip newline
if (file.fail())
throw std::runtime_error("invalid PHM image");
if (scale >= 0.f)
throw std::runtime_error("big-endian PHM images are not supported");
scale = fabs(scale);
// Read the pixels
auto image = std::make_shared<ImageBuffer>(device, W, H, numChannels, dataType);
for (int h = 0; h < H; ++h)
{
for (int w = 0; w < W; ++w)
{
for (int c = 0; c < C; ++c)
{
half x;
file.read((char*)&x, sizeof(x));
if (c < numChannels)
{
if (scale == 1.f)
{
image->set((size_t(H-1-h)*W + w) * numChannels + c, x);
}
else
{
const float xs = float(x) * scale;
image->set((size_t(H-1-h)*W + w) * numChannels + c, xs);
}
}
}
}
}
if (file.fail())
throw std::runtime_error("invalid PHM image");
return image;
}
void saveImagePHM(const std::string& filename, const ImageBuffer& image)
{
const int H = image.getH();
const int W = image.getW();
const int C = image.getC();
std::string id;
if (C == 3)
id = "PH";
else if (C == 1)
id = "Ph";
else
throw std::runtime_error("unsupported number of channels");
// Open the file
std::ofstream file(filename, std::ios::binary);
if (file.fail())
throw std::runtime_error("cannot open image file: '" + filename + "'");
// Write the header
file << id << std::endl;
file << W << " " << H << std::endl;
file << "-1.0" << std::endl;
// Write the pixels
for (int h = 0; h < H; ++h)
{
for (int w = 0; w < W; ++w)
{
for (int c = 0; c < C; ++c)
{
const half x = image.get<half>((size_t(H-1-h)*W + w) * C + c);
file.write((char*)&x, sizeof(x));
}
}
}
}
void saveImagePPM(const std::string& filename, const ImageBuffer& image)
{
if (image.getC() != 3)
throw std::invalid_argument("image must have 3 channels");
const int H = image.getH();
const int W = image.getW();
const int C = image.getC();
// Open the file
std::ofstream file(filename, std::ios::binary);
if (file.fail())
throw std::runtime_error("cannot open image file: '" + filename + "'");
// Write the header
file << "P6" << std::endl;
file << W << " " << H << std::endl;
file << "255" << std::endl;
// Write the pixels
for (int i = 0; i < W*H; ++i)
{
for (int c = 0; c < 3; ++c)
{
const float x = image.get(i*C+c);
const int ch = std::min(std::max(int(x * 255.f), 0), 255);
file.put(char(ch));
}
}
}
#ifdef OIDN_USE_OPENIMAGEIO
std::shared_ptr<ImageBuffer> loadImageOIIO(const DeviceRef& device,
const std::string& filename,
int numChannels,
Format dataType)
{
auto in = OIIO::ImageInput::open(filename);
if (!in)
throw std::runtime_error("cannot open image file: '" + filename + "'");
const OIIO::ImageSpec& spec = in->spec();
if (numChannels == 0)
numChannels = spec.nchannels;
else if (spec.nchannels < numChannels)
throw std::runtime_error("not enough image channels");
if (dataType == Format::Undefined)
dataType = (spec.channelformat(0) == OIIO::TypeDesc::HALF) ? Format::Half : Format::Float;
auto image = std::make_shared<ImageBuffer>(device, spec.width, spec.height, numChannels, dataType);
bool success = in->read_image(0, 0, 0, numChannels, dataType == Format::Half ? OIIO::TypeDesc::HALF : OIIO::TypeDesc::FLOAT, image->getData());
in->close();
#if OIIO_VERSION < 10903
OIIO::ImageInput::destroy(in);
#endif
if (!success)
throw std::runtime_error("failed to read image data");
return image;
}
void saveImageOIIO(const std::string& filename, const ImageBuffer& image)
{
auto out = OIIO::ImageOutput::create(filename);
if (!out)
throw std::runtime_error("cannot save unsupported image file format: '" + filename + "'");
OIIO::TypeDesc format;
switch (image.getDataType())
{
case Format::Float:
format = OIIO::TypeDesc::FLOAT;
break;
case Format::Half:
format = OIIO::TypeDesc::HALF;
break;
default:
throw std::runtime_error("unsupported image data type");
}
OIIO::ImageSpec spec(image.getW(),
image.getH(),
image.getC(),
format);
if (!out->open(filename, spec))
throw std::runtime_error("cannot create image file: '" + filename + "'");
bool success = out->write_image(format, image.getData());
out->close();
#if OIIO_VERSION < 10903
OIIO::ImageOutput::destroy(out);
#endif
if (!success)
throw std::runtime_error("failed to write image data");
}
#endif
} // namespace
std::shared_ptr<ImageBuffer> loadImage(const DeviceRef& device,
const std::string& filename,
int numChannels,
Format dataType)
{
const std::string ext = getExtension(filename);
std::shared_ptr<ImageBuffer> image;
if (ext == "pfm")
image = loadImagePFM(device, filename, numChannels, dataType);
else if (ext == "phm")
image = loadImagePHM(device, filename, numChannels, dataType);
else
#if OIDN_USE_OPENIMAGEIO
image = loadImageOIIO(device, filename, numChannels, dataType);
#else
throw std::runtime_error("cannot load unsupported image file format: '" + filename + "'");
#endif
return image;
}
void saveImage(const std::string& filename, const ImageBuffer& image)
{
const std::string ext = getExtension(filename);
if (ext == "pfm")
saveImagePFM(filename, image);
else if (ext == "phm")
saveImagePHM(filename, image);
else if (ext == "ppm")
saveImagePPM(filename, image);
else
#if OIDN_USE_OPENIMAGEIO
saveImageOIIO(filename, image);
#else
throw std::runtime_error("cannot write unsupported image file format: '" + filename + "'");
#endif
}
bool isSrgbImage(const std::string& filename)
{
const std::string ext = getExtension(filename);
return ext != "pfm" && ext != "phm" && ext != "exr" && ext != "hdr";
}
std::shared_ptr<ImageBuffer> loadImage(const DeviceRef& device,
const std::string& filename,
int numChannels,
bool srgb,
Format dataType)
{
auto image = loadImage(device, filename, numChannels, dataType);
if (!srgb && isSrgbImage(filename))
srgbInverse(*image);
return image;
}
void saveImage(const std::string& filename, const ImageBuffer& image, bool srgb)
{
if (!srgb && isSrgbImage(filename))
{
std::shared_ptr<ImageBuffer> newImage = image.clone();
srgbForward(*newImage);
saveImage(filename, *newImage);
}
else
{
saveImage(filename, image);
}
}
OIDN_NAMESPACE_END | cpp |
oneAPI-samples | data/projects/oneAPI-samples/RenderingToolkit/GettingStarted/04_oidn_gsg/src/apps/utils/image_io.h | // Copyright 2018 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include "image_buffer.h"
OIDN_NAMESPACE_BEGIN
// Loads an image with optionally specified number of channels and data type
std::shared_ptr<ImageBuffer> loadImage(const DeviceRef& device,
const std::string& filename,
int numChannels = 0,
Format dataType = Format::Undefined);
// Loads an image with/without sRGB to linear conversion
std::shared_ptr<ImageBuffer> loadImage(const DeviceRef& device,
const std::string& filename,
int numChannels,
bool srgb,
Format dataType = Format::Undefined);
// Saves an image
void saveImage(const std::string& filename, const ImageBuffer& image);
// Saves an image with/without linear to sRGB conversion
void saveImage(const std::string& filename, const ImageBuffer& image, bool srgb);
OIDN_NAMESPACE_END | h |
oneAPI-samples | data/projects/oneAPI-samples/RenderingToolkit/GettingStarted/04_oidn_gsg/src/apps/utils/random.h | // Copyright 2020 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include "common/platform.h"
OIDN_NAMESPACE_BEGIN
// Simple and very fast LCG random number generator
class Random
{
private:
uint32_t state;
public:
OIDN_INLINE Random(uint32_t seed = 1) : state(seed) {}
OIDN_INLINE void reset(uint32_t seed = 1)
{
state = (seed * 8191) ^ 140167;
}
OIDN_INLINE void next()
{
const uint32_t multiplier = 1664525;
const uint32_t increment = 1013904223;
state = multiplier * state + increment;
}
OIDN_INLINE uint32_t getUInt()
{
next();
return state;
}
OIDN_INLINE int getInt()
{
next();
return state;
}
OIDN_INLINE float getFloat()
{
next();
return float(state) * 2.3283064365386962890625e-10f; // x / 2^32
}
};
OIDN_NAMESPACE_END | h |
oneAPI-samples | data/projects/oneAPI-samples/RenderingToolkit/GettingStarted/04_oidn_gsg/src/apps/utils/image_buffer.cpp | // Copyright 2018 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
#include "image_io.h"
OIDN_NAMESPACE_BEGIN
ImageBuffer::ImageBuffer()
: devPtr(nullptr),
hostPtr(nullptr),
byteSize(0),
numValues(0),
width(0),
height(0),
numChannels(0),
dataType(Format::Undefined) {}
ImageBuffer::ImageBuffer(const DeviceRef& device, int width, int height, int numChannels, Format dataType,
Storage storage, bool forceHostCopy)
: device(device),
numValues(size_t(width) * height * numChannels),
width(width),
height(height),
numChannels(numChannels),
dataType(dataType)
{
size_t valueByteSize = 0;
switch (dataType)
{
case Format::Float:
valueByteSize = sizeof(float);
break;
case Format::Half:
valueByteSize = sizeof(int16_t);
break;
default:
assert(0);
}
byteSize = std::max(numValues * valueByteSize, size_t(1)); // avoid zero-sized buffer
buffer = device.newBuffer(byteSize, storage);
storage = buffer.getStorage(); // get actual storage mode
devPtr = static_cast<char*>(buffer.getData());
hostPtr = (storage != Storage::Device && !forceHostCopy) ? devPtr : static_cast<char*>(malloc(byteSize));
}
ImageBuffer::~ImageBuffer()
{
if (hostPtr != devPtr)
free(hostPtr);
}
void ImageBuffer::toHost()
{
if (hostPtr != devPtr)
buffer.read(0, byteSize, hostPtr);
}
void ImageBuffer::toHostAsync()
{
if (hostPtr != devPtr)
buffer.readAsync(0, byteSize, hostPtr);
}
void ImageBuffer::toDevice()
{
if (hostPtr != devPtr)
buffer.write(0, byteSize, hostPtr);
}
void ImageBuffer::toDeviceAsync()
{
if (hostPtr != devPtr)
buffer.writeAsync(0, byteSize, hostPtr);
}
std::shared_ptr<ImageBuffer> ImageBuffer::clone() const
{
auto result = std::make_shared<ImageBuffer>(device, width, height, numChannels, dataType);
result->buffer.write(0, getByteSize(), devPtr);
return result;
}
std::tuple<size_t, double> compareImage(const ImageBuffer& image,
const ImageBuffer& ref,
double errorThreshold)
{
assert(ref.getDims() == image.getDims());
size_t numErrors = 0;
double avgError = 0; // SMAPE
for (size_t i = 0; i < image.getSize(); ++i)
{
const double actual = image.get(i);
const double expect = ref.get(i);
const double absError = std::abs(expect - actual);
const double relError = absError / (std::abs(expect) + std::abs(actual) + 0.01);
// Detect severe outliers
if (absError > 0.02 && relError > 0.05)
{
if (numErrors < 5)
std::cerr << " error i=" << i << ", expect=" << expect << ", actual=" << actual << std::endl;
++numErrors;
}
avgError += relError;
}
avgError /= image.getSize();
if (avgError > errorThreshold)
numErrors = image.getSize();
return std::make_tuple(numErrors, avgError);
}
OIDN_NAMESPACE_END
| cpp |
oneAPI-samples | data/projects/oneAPI-samples/RenderingToolkit/GettingStarted/04_oidn_gsg/src/apps/utils/arg_parser.h | // Copyright 2018 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include "common/platform.h"
OIDN_NAMESPACE_BEGIN
// Command-line argument parser
class ArgParser
{
public:
ArgParser(int argc, char* argv[]);
bool hasNext() const;
std::string getNext();
std::string getNextOpt();
template<typename T = std::string>
T getNextValue()
{
return fromString<T>(getNextValue());
}
private:
int argc;
char** argv;
int pos;
};
template<>
std::string ArgParser::getNextValue();
OIDN_NAMESPACE_END
| h |
oneAPI-samples | data/projects/oneAPI-samples/RenderingToolkit/GettingStarted/04_oidn_gsg/src/apps/utils/arg_parser.cpp | // Copyright 2018 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
#include <stdexcept>
#include "arg_parser.h"
OIDN_NAMESPACE_BEGIN
ArgParser::ArgParser(int argc, char* argv[])
: argc(argc), argv(argv),
pos(1) {}
bool ArgParser::hasNext() const
{
return pos < argc;
}
std::string ArgParser::getNext()
{
if (pos < argc)
return argv[pos++];
else
throw std::invalid_argument("argument expected");
}
std::string ArgParser::getNextOpt()
{
std::string str = getNext();
size_t pos = str.find_first_not_of("-");
if (pos == 0 || pos == std::string::npos)
throw std::invalid_argument("option expected");
return str.substr(pos);
}
template<>
std::string ArgParser::getNextValue()
{
std::string str = getNext();
if (!str.empty() && str[0] == '-')
throw std::invalid_argument("value expected");
return str;
}
OIDN_NAMESPACE_END
| cpp |
oneAPI-samples | data/projects/oneAPI-samples/RenderingToolkit/GettingStarted/04_oidn_gsg/src/apps/utils/device_info.h | // Copyright 2023 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include "common/common.h"
OIDN_NAMESPACE_BEGIN
inline int printPhysicalDevices()
{
const int numDevices = getNumPhysicalDevices();
if (numDevices == 0)
{
std::cout << "No supported devices found" << std::endl;
return 1;
}
for (int i = 0; i < numDevices; ++i)
{
PhysicalDeviceRef physicalDevice(i);
std::cout << "Device " << i << std::endl;
std::cout << " Name: " << physicalDevice.get<std::string>("name") << std::endl;
std::cout << " Type: " << physicalDevice.get<DeviceType>("type") << std::endl;
if (physicalDevice.get<bool>("uuidSupported"))
std::cout << " UUID: " << physicalDevice.get<OIDN_NAMESPACE::UUID>("uuid") << std::endl;
if (physicalDevice.get<bool>("luidSupported"))
{
std::cout << " LUID: " << physicalDevice.get<OIDN_NAMESPACE::LUID>("luid") << std::endl;
std::cout << " Node: " << physicalDevice.get<uint32_t>("nodeMask") << std::endl;
}
if (physicalDevice.get<bool>("pciAddressSupported"))
{
auto flags = std::cout.flags();
std::cout << " PCI : "
<< std::hex << std::setfill('0')
<< std::setw(4) << physicalDevice.get<int>("pciDomain") << ":"
<< std::setw(2) << physicalDevice.get<int>("pciBus") << ":"
<< std::setw(2) << physicalDevice.get<int>("pciDevice") << "."
<< std::setw(1) << physicalDevice.get<int>("pciFunction")
<< std::endl;
std::cout.flags(flags);
}
if (i < numDevices-1)
std::cout << std::endl;
}
return 0;
}
OIDN_NAMESPACE_END
| h |
oneAPI-samples | data/projects/oneAPI-samples/RenderingToolkit/GettingStarted/02_embree_gsg/gpu/src/minimal_sycl.cpp | // Copyright 2009-2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
/*
* To use the Embree DPC++ API you have to include sycl.hpp before the
* embree API headers.
*/
#include <sycl/sycl.hpp>
#include <embree4/rtcore.h>
#include <cstdio>
#include <limits>
/*
* A minimal tutorial.
*
* It demonstrates how to intersect a ray with a single triangle. It is
* meant to get you started as quickly as possible, and does not output
* an image.
*/
/*
* This is only required to make the tutorial compile even when
* a custom namespace is set.
*/
#if defined(RTC_NAMESPACE_USE)
RTC_NAMESPACE_USE
#endif
const sycl::specialization_id<RTCFeatureFlags> feature_mask;
const RTCFeatureFlags required_features = RTC_FEATURE_FLAG_TRIANGLE;
struct Result {
unsigned geomID;
unsigned primID;
float tfar;
};
/*
* This function allocated USM memory that is writeable by the device.
*/
template <typename T>
T* alignedSYCLMallocDeviceReadWrite(const sycl::queue& queue, size_t count,
size_t align) {
if (count == 0) return nullptr;
assert((align & (align - 1)) == 0);
T* ptr = (T*)sycl::aligned_alloc(align, count * sizeof(T), queue,
sycl::usm::alloc::shared);
if (count != 0 && ptr == nullptr) throw std::bad_alloc();
return ptr;
}
/*
* This function allocated USM memory that is only readable by the
* device. Using this mode many small allocations are possible by the
* application.
*/
template <typename T>
T* alignedSYCLMallocDeviceReadOnly(const sycl::queue& queue, size_t count,
size_t align) {
if (count == 0) return nullptr;
assert((align & (align - 1)) == 0);
T* ptr = (T*)sycl::aligned_alloc_shared(
align, count * sizeof(T), queue,
sycl::ext::oneapi::property::usm::device_read_only());
if (count != 0 && ptr == nullptr) throw std::bad_alloc();
return ptr;
}
void alignedSYCLFree(const sycl::queue& queue, void* ptr) {
if (ptr) sycl::free(ptr, queue);
}
/*
* We will register this error handler with the device in initializeDevice(),
* so that we are automatically informed on errors.
* This is extremely helpful for finding bugs in your code, prevents you
* from having to add explicit error checking to each Embree API call.
*/
void errorFunction(void* userPtr, enum RTCError error, const char* str) {
printf("error %d: %s\n", error, str);
}
/*
* Embree has a notion of devices, which are entities that can run
* raytracing kernels.
* We initialize our device here, and then register the error handler so that
* we don't miss any errors.
*
* rtcNewDevice() takes a configuration string as an argument. See the API docs
* for more information.
*
* Note that RTCDevice is reference-counted.
*/
RTCDevice initializeDevice(sycl::context& sycl_context) {
RTCDevice emb_device = rtcNewSYCLDevice(sycl_context, "");
if (!emb_device)
printf("error %d: cannot create embree device\n", rtcGetDeviceError(NULL));
rtcSetDeviceErrorFunction(emb_device, errorFunction, NULL);
return emb_device;
}
/*
* Create a scene, which is a collection of geometry objects. Scenes are
* what the intersect / occluded functions work on. You can think of a
* scene as an acceleration structure, e.g. a bounding-volume hierarchy.
*
* Scenes, like devices, are reference-counted.
*/
RTCScene initializeScene(RTCDevice device, const sycl::queue& queue) {
RTCScene scene = rtcNewScene(device);
/*
* Create a triangle mesh geometry, and initialize a single triangle.
* You can look up geometry types in the API documentation to
* find out which type expects which buffers.
*
* We create buffers directly on the device, but you can also use
* shared buffers. For shared buffers, special care must be taken
* to ensure proper alignment and padding. This is described in
* more detail in the API documentation.
*/
RTCGeometry geom = rtcNewGeometry(device, RTC_GEOMETRY_TYPE_TRIANGLE);
float* vertices = alignedSYCLMallocDeviceReadOnly<float>(queue, 3 * 3, 16);
rtcSetSharedGeometryBuffer(geom, RTC_BUFFER_TYPE_VERTEX, 0, RTC_FORMAT_FLOAT3,
vertices, 0, 3 * sizeof(float), 3);
unsigned* indices = alignedSYCLMallocDeviceReadOnly<unsigned>(queue, 3, 16);
rtcSetSharedGeometryBuffer(geom, RTC_BUFFER_TYPE_INDEX, 0, RTC_FORMAT_UINT3,
indices, 0, 3 * sizeof(unsigned), 1);
if (vertices && indices) {
vertices[0] = 0.f;
vertices[1] = 0.f;
vertices[2] = 0.f;
vertices[3] = 1.f;
vertices[4] = 0.f;
vertices[5] = 0.f;
vertices[6] = 0.f;
vertices[7] = 1.f;
vertices[8] = 0.f;
indices[0] = 0;
indices[1] = 1;
indices[2] = 2;
}
/*
* You must commit geometry objects when you are done setting them up,
* or you will not get any intersections.
*/
rtcCommitGeometry(geom);
/*
* In rtcAttachGeometry(...), the scene takes ownership of the geom
* by increasing its reference count. This means that we don't have
* to hold on to the geom handle, and may release it. The geom object
* will be released automatically when the scene is destroyed.
*
* rtcAttachGeometry() returns a geometry ID. We could use this to
* identify intersected objects later on.
*/
rtcAttachGeometry(scene, geom);
rtcReleaseGeometry(geom);
/*
* Like geometry objects, scenes must be committed. This lets
* Embree know that it may start building an acceleration structure.
*/
rtcCommitScene(scene);
return scene;
}
/*
* Cast a single ray with origin (ox, oy, oz) and direction
* (dx, dy, dz).
*/
void castRay(sycl::queue& queue, const RTCScene scene, float ox, float oy,
float oz, float dx, float dy, float dz, Result* result) {
queue.submit([=](sycl::handler& cgh) {
cgh.set_specialization_constant<feature_mask>(required_features);
cgh.parallel_for(sycl::range<1>(1),
[=](sycl::item<1> item, sycl::kernel_handler kh) {
/*
* The intersect arguments can be used to pass a feature
* mask, which improves performance and JIT compile times
* on the GPU
*/
RTCIntersectArguments args;
rtcInitIntersectArguments(&args);
const RTCFeatureFlags features =
kh.get_specialization_constant<feature_mask>();
args.feature_mask = features;
/*
* The ray hit structure holds both the ray and the hit.
* The user must initialize it properly -- see API
* documentation for rtcIntersect1() for details.
*/
struct RTCRayHit rayhit;
rayhit.ray.org_x = ox;
rayhit.ray.org_y = oy;
rayhit.ray.org_z = oz;
rayhit.ray.dir_x = dx;
rayhit.ray.dir_y = dy;
rayhit.ray.dir_z = dz;
rayhit.ray.tnear = 0;
rayhit.ray.tfar = std::numeric_limits<float>::infinity();
rayhit.ray.mask = -1;
rayhit.ray.flags = 0;
rayhit.hit.geomID = RTC_INVALID_GEOMETRY_ID;
rayhit.hit.instID[0] = RTC_INVALID_GEOMETRY_ID;
/*
* There are multiple variants of rtcIntersect. This one
* intersects a single ray with the scene.
*/
rtcIntersect1(scene, &rayhit, &args);
/*
* write hit result to output buffer
*/
result->geomID = rayhit.hit.geomID;
result->primID = rayhit.hit.primID;
result->tfar = rayhit.ray.tfar;
});
});
queue.wait_and_throw();
printf("%f, %f, %f: ", ox, oy, oz);
if (result->geomID != RTC_INVALID_GEOMETRY_ID) {
/* Note how geomID and primID identify the geometry we just hit.
* We could use them here to interpolate geometry information,
* compute shading, etc.
* Since there is only a single triangle in this scene, we will
* get geomID=0 / primID=0 for all hits.
* There is also instID, used for instancing. See
* the instancing tutorials for more information */
printf("Found intersection on geometry %d, primitive %d at tfar=%f\n",
result->geomID, result->primID, result->tfar);
} else
printf("Did not find any intersection.\n");
}
/*
* Enable persistent JIT compilation caching. These environment
* variables must be set before the SYCL device creation.
*/
void enablePersistentJITCache() {
#if defined(_WIN32)
_putenv_s("SYCL_CACHE_PERSISTENT", "1");
_putenv_s("SYCL_CACHE_DIR", "cache");
#else
setenv("SYCL_CACHE_PERSISTENT", "1", 1);
setenv("SYCL_CACHE_DIR", "cache", 1);
#endif
}
/* -------------------------------------------------------------------------- */
int main() {
enablePersistentJITCache();
/* This will select the first GPU supported by Embree */
sycl::device sycl_device(rtcSYCLDeviceSelector);
sycl::queue sycl_queue(sycl_device);
sycl::context sycl_context(sycl_device);
RTCDevice emb_device = initializeDevice(sycl_context);
RTCScene emb_scene = initializeScene(emb_device, sycl_queue);
Result* result = alignedSYCLMallocDeviceReadWrite<Result>(sycl_queue, 1, 16);
/* This will hit the triangle at t=1. */
castRay(sycl_queue, emb_scene, 0.33f, 0.33f, -1, 0, 0, 1, result);
/* This will not hit anything. */
castRay(sycl_queue, emb_scene, 1.00f, 1.00f, -1, 0, 0, 1, result);
alignedSYCLFree(sycl_queue, result);
/* Though not strictly necessary in this example, you should
* always make sure to release resources allocated through Embree. */
rtcReleaseScene(emb_scene);
rtcReleaseDevice(emb_device);
return 0;
}
| cpp |
oneAPI-samples | data/projects/oneAPI-samples/RenderingToolkit/GettingStarted/02_embree_gsg/cpu/src/minimal.cpp | // Copyright 2009-2023 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
#include <embree4/rtcore.h>
#include <math.h>
#include <stdio.h>
#if defined(_WIN32)
#include <conio.h>
#include <windows.h>
#endif
/*
* A minimal tutorial.
*
* It demonstrates how to intersect a ray with a single triangle. It is
* meant to get you started as quickly as possible, and does not output
* an image.
*
* For more complex examples, see the other tutorials.
*
* Compile this file using
*
* gcc -std=c99 \
* -I<PATH>/<TO>/<EMBREE>/include \
* -o minimal \
* minimal.c \
* -L<PATH>/<TO>/<EMBREE>/lib \
* -lembree4
*
* You should be able to compile this using a C or C++ compiler.
*/
/*
* This is only required to make the tutorial compile even when
* a custom namespace is set.
*/
#if defined(RTC_NAMESPACE_USE)
RTC_NAMESPACE_USE
#endif
/*
* We will register this error handler with the device in initializeDevice(),
* so that we are automatically informed on errors.
* This is extremely helpful for finding bugs in your code, prevents you
* from having to add explicit error checking to each Embree API call.
*/
void errorFunction(void *userPtr, enum RTCError error, const char *str) {
printf("error %d: %s\n", error, str);
}
/*
* Embree has a notion of devices, which are entities that can run
* raytracing kernels.
* We initialize our device here, and then register the error handler so that
* we don't miss any errors.
*
* rtcNewDevice() takes a configuration string as an argument. See the API docs
* for more information.
*
* Note that RTCDevice is reference-counted.
*/
RTCDevice initializeDevice() {
RTCDevice device = rtcNewDevice(NULL);
if (!device)
printf("error %d: cannot create device\n", rtcGetDeviceError(NULL));
rtcSetDeviceErrorFunction(device, errorFunction, NULL);
return device;
}
/*
* Create a scene, which is a collection of geometry objects. Scenes are
* what the intersect / occluded functions work on. You can think of a
* scene as an acceleration structure, e.g. a bounding-volume hierarchy.
*
* Scenes, like devices, are reference-counted.
*/
RTCScene initializeScene(RTCDevice device) {
RTCScene scene = rtcNewScene(device);
/*
* Create a triangle mesh geometry, and initialize a single triangle.
* You can look up geometry types in the API documentation to
* find out which type expects which buffers.
*
* We create buffers directly on the device, but you can also use
* shared buffers. For shared buffers, special care must be taken
* to ensure proper alignment and padding. This is described in
* more detail in the API documentation.
*/
RTCGeometry geom = rtcNewGeometry(device, RTC_GEOMETRY_TYPE_TRIANGLE);
float *vertices = (float *)rtcSetNewGeometryBuffer(
geom, RTC_BUFFER_TYPE_VERTEX, 0, RTC_FORMAT_FLOAT3, 3 * sizeof(float), 3);
unsigned *indices = (unsigned *)rtcSetNewGeometryBuffer(
geom, RTC_BUFFER_TYPE_INDEX, 0, RTC_FORMAT_UINT3, 3 * sizeof(unsigned),
1);
if (vertices && indices) {
vertices[0] = 0.f;
vertices[1] = 0.f;
vertices[2] = 0.f;
vertices[3] = 1.f;
vertices[4] = 0.f;
vertices[5] = 0.f;
vertices[6] = 0.f;
vertices[7] = 1.f;
vertices[8] = 0.f;
indices[0] = 0;
indices[1] = 1;
indices[2] = 2;
}
/*
* You must commit geometry objects when you are done setting them up,
* or you will not get any intersections.
*/
rtcCommitGeometry(geom);
/*
* In rtcAttachGeometry(...), the scene takes ownership of the geom
* by increasing its reference count. This means that we don't have
* to hold on to the geom handle, and may release it. The geom object
* will be released automatically when the scene is destroyed.
*
* rtcAttachGeometry() returns a geometry ID. We could use this to
* identify intersected objects later on.
*/
rtcAttachGeometry(scene, geom);
rtcReleaseGeometry(geom);
/*
* Like geometry objects, scenes must be committed. This lets
* Embree know that it may start building an acceleration structure.
*/
rtcCommitScene(scene);
return scene;
}
/*
* Cast a single ray with origin (ox, oy, oz) and direction
* (dx, dy, dz).
*/
void castRay(RTCScene scene, float ox, float oy, float oz, float dx, float dy,
float dz) {
/*
* The intersect arguments can be used to set intersection
* filters or flags, and it also contains the instance ID stack
* used in multi-level instancing. RTCIntersectArguments from Embree 4
* replaces RTCIntersectContext from Embree 3.
*/
struct RTCIntersectArguments args;
rtcInitIntersectArguments(&args);
args.feature_mask = RTC_FEATURE_FLAG_TRIANGLE;
/*
* The ray hit structure holds both the ray and the hit.
* The user must initialize it properly -- see API documentation
* for rtcIntersect1() for details.
*/
struct RTCRayHit rayhit;
rayhit.ray.org_x = ox;
rayhit.ray.org_y = oy;
rayhit.ray.org_z = oz;
rayhit.ray.dir_x = dx;
rayhit.ray.dir_y = dy;
rayhit.ray.dir_z = dz;
rayhit.ray.tnear = 0;
rayhit.ray.tfar = INFINITY;
rayhit.ray.mask = -1;
rayhit.ray.flags = 0;
rayhit.hit.geomID = RTC_INVALID_GEOMETRY_ID;
rayhit.hit.instID[0] = RTC_INVALID_GEOMETRY_ID;
/*
* There are multiple variants of rtcIntersect. This one
* intersects a single ray with the scene. The rtcIntersect1 function
* signature was updated with Embree 4.0.0.
*/
rtcIntersect1(scene, &rayhit, &args);
printf("%f, %f, %f: ", ox, oy, oz);
if (rayhit.hit.geomID != RTC_INVALID_GEOMETRY_ID) {
/* Note how geomID and primID identify the geometry we just hit.
* We could use them here to interpolate geometry information,
* compute shading, etc.
* Since there is only a single triangle in this scene, we will
* get geomID=0 / primID=0 for all hits.
* There is also instID, used for instancing. See
* the instancing tutorials for more information */
printf("Found intersection on geometry %d, primitive %d at tfar=%f\n",
rayhit.hit.geomID, rayhit.hit.primID, rayhit.ray.tfar);
} else
printf("Did not find any intersection.\n");
}
void waitForKeyPressedUnderWindows() {
#if defined(_WIN32)
HANDLE hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi;
if (!GetConsoleScreenBufferInfo(hStdOutput, &csbi)) {
printf("GetConsoleScreenBufferInfo failed: %d\n", GetLastError());
return;
}
/* do not pause when running on a shell */
if (csbi.dwCursorPosition.X != 0 || csbi.dwCursorPosition.Y != 0) return;
/* only pause if running in separate console window. */
printf("\n\tPress any key to exit...\n");
int ch = getch();
#endif
}
/* -------------------------------------------------------------------------- */
int main() {
/* Initialization. All of this may fail, but we will be notified by
* our errorFunction. */
RTCDevice device = initializeDevice();
RTCScene scene = initializeScene(device);
/* This will hit the triangle at t=1. */
castRay(scene, 0, 0, -1, 0, 0, 1);
/* This will not hit anything. */
castRay(scene, 1, 1, -1, 0, 0, 1);
/* Though not strictly necessary in this example, you should
* always make sure to release resources allocated through Embree. */
rtcReleaseScene(scene);
rtcReleaseDevice(device);
/* wait for user input under Windows when opened in separate window */
waitForKeyPressedUnderWindows();
return 0;
}
| cpp |
oneAPI-samples | data/projects/oneAPI-samples/RenderingToolkit/GettingStarted/01_ospray_gsg/src/ospTutorial.cpp | // Copyright 2009-2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
/* This is a small example tutorial how to use OSPRay in an application.
*
* On Linux build it in the build_directory with
* g++ ../apps/ospTutorial/ospTutorial.cpp -I ../ospray/include \
* -I ../../rkcommon -L . -lospray -Wl,-rpath,. -o ospTutorial
* On Windows build it in the build_directory\$Configuration with
* cl ..\..\apps\ospTutorial\ospTutorial.cpp /EHsc -I ..\..\ospray\include ^
* -I ..\.. -I ..\..\..\rkcommon ospray.lib
* Above commands assume that rkcommon is present in a directory right "next
* to" the OSPRay directory. If this is not the case, then adjust the include
* path (alter "-I <path/to/rkcommon>" appropriately).
*/
#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#ifdef _WIN32
#define NOMINMAX
#include <conio.h>
#include <malloc.h>
#include <windows.h>
#else
#include <alloca.h>
#endif
#include <vector>
#include "ospray/ospray_cpp.h"
#include "ospray/ospray_cpp/ext/rkcommon.h"
#include "rkcommon/utility/SaveImage.h"
using namespace rkcommon::math;
int main(int argc, const char **argv) {
// image size
vec2i imgSize;
imgSize.x = 1024; // width
imgSize.y = 768; // height
// camera
vec3f cam_pos{0.f, 0.f, 0.f};
vec3f cam_up{0.f, 1.f, 0.f};
vec3f cam_view{0.1f, 0.f, 1.f};
// triangle mesh data
std::vector<vec3f> vertex = {
vec3f(-1.0f, -1.0f, 3.0f), vec3f(-1.0f, 1.0f, 3.0f),
vec3f(1.0f, -1.0f, 3.0f), vec3f(0.1f, 0.1f, 0.3f)};
std::vector<vec4f> color = {
vec4f(0.9f, 0.5f, 0.5f, 1.0f), vec4f(0.8f, 0.8f, 0.8f, 1.0f),
vec4f(0.8f, 0.8f, 0.8f, 1.0f), vec4f(0.5f, 0.9f, 0.5f, 1.0f)};
std::vector<vec3ui> index = {vec3ui(0, 1, 2), vec3ui(1, 2, 3)};
#ifdef _WIN32
bool waitForKey = false;
CONSOLE_SCREEN_BUFFER_INFO csbi;
if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi)) {
// detect standalone console: cursor at (0,0)?
waitForKey = csbi.dwCursorPosition.X == 0 && csbi.dwCursorPosition.Y == 0;
}
#endif
// initialize OSPRay; OSPRay parses (and removes) its commandline parameters,
// e.g. "--osp:debug"
OSPError init_error = ospInit(&argc, argv);
if (init_error != OSP_NO_ERROR) return init_error;
// use scoped lifetimes of wrappers to release everything before ospShutdown()
{
// create and setup camera
ospray::cpp::Camera camera("perspective");
camera.setParam("aspect", imgSize.x / (float)imgSize.y);
camera.setParam("position", cam_pos);
camera.setParam("direction", cam_view);
camera.setParam("up", cam_up);
camera.commit(); // commit each object to indicate modifications are done
// create and setup model and mesh
ospray::cpp::Geometry mesh("mesh");
mesh.setParam("vertex.position", ospray::cpp::CopiedData(vertex));
mesh.setParam("vertex.color", ospray::cpp::CopiedData(color));
mesh.setParam("index", ospray::cpp::CopiedData(index));
mesh.commit();
// put the mesh into a model
ospray::cpp::GeometricModel model(mesh);
model.commit();
// put the model into a group (collection of models)
ospray::cpp::Group group;
group.setParam("geometry", ospray::cpp::CopiedData(model));
group.commit();
// put the group into an instance (give the group a world transform)
ospray::cpp::Instance instance(group);
instance.commit();
// put the instance in the world
ospray::cpp::World world;
world.setParam("instance", ospray::cpp::CopiedData(instance));
// create and setup light for Ambient Occlusion
ospray::cpp::Light light("ambient");
light.commit();
world.setParam("light", ospray::cpp::CopiedData(light));
world.commit();
// create renderer, choose Scientific Visualization renderer
ospray::cpp::Renderer renderer("scivis");
// complete setup of renderer
renderer.setParam("aoSamples", 1);
renderer.setParam("backgroundColor", 1.0f); // white, transparent
renderer.commit();
// create and setup framebuffer
ospray::cpp::FrameBuffer framebuffer(imgSize.x, imgSize.y, OSP_FB_SRGBA,
OSP_FB_COLOR | OSP_FB_ACCUM);
framebuffer.clear();
// render one frame
framebuffer.renderFrame(renderer, camera, world);
// access framebuffer and write its content as PPM file
uint32_t *fb = (uint32_t *)framebuffer.map(OSP_FB_COLOR);
rkcommon::utility::writePPM("firstFrameCpp.ppm", imgSize.x, imgSize.y, fb);
framebuffer.unmap(fb);
std::cout << "rendering initial frame to firstFrameCpp.ppm" << std::endl;
// render 10 more frames, which are accumulated to result in a better
// converged image
for (int frames = 0; frames < 10; frames++)
framebuffer.renderFrame(renderer, camera, world);
fb = (uint32_t *)framebuffer.map(OSP_FB_COLOR);
rkcommon::utility::writePPM("accumulatedFrameCpp.ppm", imgSize.x, imgSize.y,
fb);
framebuffer.unmap(fb);
std::cout << "rendering 10 accumulated frames to accumulatedFrameCpp.ppm"
<< std::endl;
ospray::cpp::PickResult res =
framebuffer.pick(renderer, camera, world, 0.5f, 0.5f);
if (res.hasHit) {
std::cout << "picked geometry [instance: " << res.instance.handle()
<< ", model: " << res.model.handle()
<< ", primitive: " << res.primID << "]" << std::endl;
}
}
ospShutdown();
#ifdef _WIN32
if (waitForKey) {
printf("\n\tpress any key to exit");
_getch();
}
#endif
return 0;
}
| cpp |
oneAPI-samples | data/projects/oneAPI-samples/RenderingToolkit/Tutorial/IntroToRayTracingWithEmbree/gpu/src/rkRayTracerGPU.cpp | #ifdef _MSC_VER
#ifndef NOMINMAX
/* use intended min and max instead of the MSVS macros */
#define NOMINMAX
#endif
#endif
/* Added for GPU */
#include <sycl/sycl.hpp>
#include <embree4/rtcore.h>
#include "MathBindings.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include <stb/stb_image_write.h>
using Vec3fa = MathBindings::Vec3fa;
using LinearSpace3 = MathBindings::LinearSpace3fa;
using AffineSpace3fa = MathBindings::AffineSpace3fa;
using MathBindings::clamp;
using MathBindings::cross;
using MathBindings::deg2rad;
using MathBindings::dot;
using MathBindings::normalize;
using std::max;
using std::min;
#define TILE_SIZE_X 8
#define TILE_SIZE_Y 8
typedef AffineSpace3fa Camera;
/* The allocator helps us use USM pointers for the vector container */
/* Acheived: Dynamic element creation (push_back), USM pointer access in the
* device kernel, Device Reads memory thus no transfer back occurs upon kernel
* completion */
/* Dynamic RO designation is set up when we create an allocator object later */
/* This allocator object is useful for std::vector and serves as self contained
* provisioning of functionality similar to the
* alignedSYCLUSMMallocDeviceReadOnly(const sycl::queue& queue, size_t count,
* size_t align) function defined globally */
typedef sycl::usm_allocator<unsigned int, sycl::usm::alloc::shared>
USMDRO_UI_ALLOC;
const sycl::specialization_id<RTCFeatureFlags> feature_mask;
const RTCFeatureFlags required_features = RTC_FEATURE_FLAG_TRIANGLE;
/*
* This function allocated USM memory that is only readable by the
* device. Using this mode many small allocations are possible by the
* application.
*/
template <typename T>
T* alignedSYCLUSMMallocDeviceReadOnly(const sycl::queue& queue, size_t count,
size_t align) {
if (count == 0) return nullptr;
assert((align & (align - 1)) == 0);
T* ptr = (T*)sycl::aligned_alloc_shared(
align, count * sizeof(T), queue,
sycl::ext::oneapi::property::usm::device_read_only());
if (count != 0 && ptr == nullptr) throw std::bad_alloc();
return ptr;
}
/*
* This function allocates USM memory that can be accessible back on the host
* (ex: output image buffer)
*/
template <typename T>
T* alignedSYCLUSMMalloc(const sycl::queue& queue, size_t count, size_t align) {
if (count == 0) return nullptr;
assert((align & (align - 1)) == 0);
void* ptr = nullptr;
ptr = sycl::aligned_alloc_shared(align, count * sizeof(T), queue);
if (count != 0 && ptr == nullptr) throw std::bad_alloc();
return static_cast<T*>(ptr);
}
void alignedSYCLFree(const sycl::queue& queue, void* ptr) {
if (ptr) sycl::free(ptr, queue);
}
inline sycl::nd_range<2> make_nd_range(unsigned int size0, unsigned int size1) {
const sycl::range<2> wg_size = sycl::range<2>(4, 4);
/* align iteration space to work group size */
size0 = ((size0 + wg_size[0] - 1) / wg_size[0]) * wg_size[0];
size1 = ((size1 + wg_size[1] - 1) / wg_size[1]) * wg_size[1];
return sycl::nd_range(sycl::range(size0, size1), wg_size);
}
void error_handler(void* userPtr, const RTCError code, const char* str) {
if (code == RTC_ERROR_NONE) return;
printf("fail: Embree Error: ");
switch (code) {
case RTC_ERROR_UNKNOWN:
printf("RTC_ERROR_UNKNOWN");
break;
case RTC_ERROR_INVALID_ARGUMENT:
printf("RTC_ERROR_INVALID_ARGUMENT");
break;
case RTC_ERROR_INVALID_OPERATION:
printf("RTC_ERROR_INVALID_OPERATION");
break;
case RTC_ERROR_OUT_OF_MEMORY:
printf("RTC_ERROR_OUT_OF_MEMORY");
break;
case RTC_ERROR_UNSUPPORTED_CPU:
printf("RTC_ERROR_UNSUPPORTED_CPU");
break;
case RTC_ERROR_CANCELLED:
printf("RTC_ERROR_CANCELLED");
break;
default:
printf("invalid error code");
break;
}
if (str) {
printf(" (");
while (*str) putchar(*str++);
printf(")\n");
}
exit(1);
}
/* from tutorial_device.h */
/* vertex and triangle layout */
struct Vertex {
float x, y, z, r;
};
struct Triangle {
int v0, v1, v2;
};
Camera positionCamera(Vec3fa from, Vec3fa to, Vec3fa up, float fov,
size_t width, size_t height) {
/* There are many ways to set up a camera projection. This one is consolidated
* from the camera code in the Embree/tutorial/common/tutorial/camera.h object
*/
Camera camMatrix;
Vec3fa Z = normalize(Vec3fa(to - from));
Vec3fa U = normalize(cross(up, Z));
Vec3fa V = normalize(cross(Z, U));
camMatrix.l.vx = U;
camMatrix.l.vy = V;
camMatrix.l.vz = Z;
camMatrix.p = from;
/* negate for a right handed camera*/
camMatrix.l.vx = -camMatrix.l.vx;
const float fovScale = 1.0f / tanf(deg2rad(0.5f * fov));
camMatrix.l.vz = -0.5f * width * camMatrix.l.vx +
0.5f * height * camMatrix.l.vy +
0.5f * height * fovScale * camMatrix.l.vz;
camMatrix.l.vy = -camMatrix.l.vy;
return camMatrix;
}
/* adds a cube to the scene */
unsigned int addCube(const sycl::queue& queue, const RTCDevice& emb_device,
const RTCScene& scene, Vec3fa** pp_cube_face_colors,
Vec3fa** pp_cube_vertex_colors) {
/*
* Create a triangle mesh geometry for the cube, and initialize 12 triangles.
* You can look up geometry types in the API documentation to
* find out which type expects which buffers.
*
* We create buffers directly on the device, but you can also use
* shared buffers. For shared buffers, special care must be taken
* to ensure proper alignment and padding. This is described in
* more detail in the API documentation.
*/
RTCGeometry mesh = rtcNewGeometry(emb_device, RTC_GEOMETRY_TYPE_TRIANGLE);
/* create face and vertex color arrays */
*pp_cube_face_colors =
alignedSYCLUSMMallocDeviceReadOnly<Vec3fa>(queue, 12, 16);
/* For GPU, we change the vertex color buffer allocation from aligned malloc
* to a SYCL allocator */
*pp_cube_vertex_colors =
alignedSYCLUSMMallocDeviceReadOnly<Vec3fa>(queue, 8, 16);
/* set vertices and vertex colors */
Vertex* vertices = alignedSYCLUSMMallocDeviceReadOnly<Vertex>(queue, 8, 16);
/* For GPU, change rtcSetNewGeometryBuffer to rtcSetSharedGeometryBuffer to
* work with the SYCL allocator */
rtcSetSharedGeometryBuffer(mesh, RTC_BUFFER_TYPE_VERTEX, 0, RTC_FORMAT_FLOAT3,
vertices, 0, sizeof(Vertex), 8);
(*pp_cube_vertex_colors)[0] = Vec3fa(0, 0, 0);
vertices[0].x = -1;
vertices[0].y = -1;
vertices[0].z = -1;
(*pp_cube_vertex_colors)[1] = Vec3fa(0, 0, 1);
vertices[1].x = -1;
vertices[1].y = -1;
vertices[1].z = +1;
(*pp_cube_vertex_colors)[2] = Vec3fa(0, 1, 0);
vertices[2].x = -1;
vertices[2].y = +1;
vertices[2].z = -1;
(*pp_cube_vertex_colors)[3] = Vec3fa(0, 1, 1);
vertices[3].x = -1;
vertices[3].y = +1;
vertices[3].z = +1;
(*pp_cube_vertex_colors)[4] = Vec3fa(1, 0, 0);
vertices[4].x = +1;
vertices[4].y = -1;
vertices[4].z = -1;
(*pp_cube_vertex_colors)[5] = Vec3fa(1, 0, 1);
vertices[5].x = +1;
vertices[5].y = -1;
vertices[5].z = +1;
(*pp_cube_vertex_colors)[6] = Vec3fa(1, 1, 0);
vertices[6].x = +1;
vertices[6].y = +1;
vertices[6].z = -1;
(*pp_cube_vertex_colors)[7] = Vec3fa(1, 1, 1);
vertices[7].x = +1;
vertices[7].y = +1;
vertices[7].z = +1;
/* set triangles and face colors */
int tri = 0;
Triangle* triangles =
alignedSYCLUSMMallocDeviceReadOnly<Triangle>(queue, 12, 16);
/* For GPU, change rtcSetNewGeometryBuffer to rtcSetSharedGeometryBuffer to
* work with the SYCL allocator */
rtcSetSharedGeometryBuffer(mesh, RTC_BUFFER_TYPE_INDEX, 0, RTC_FORMAT_UINT3,
triangles, 0, sizeof(Triangle), 12);
// left side
(*pp_cube_face_colors)[tri] = Vec3fa(1, 0, 0);
triangles[tri].v0 = 0;
triangles[tri].v1 = 1;
triangles[tri].v2 = 2;
tri++;
(*pp_cube_face_colors)[tri] = Vec3fa(1, 0, 0);
triangles[tri].v0 = 1;
triangles[tri].v1 = 3;
triangles[tri].v2 = 2;
tri++;
// right side
(*pp_cube_face_colors)[tri] = Vec3fa(0, 1, 0);
triangles[tri].v0 = 4;
triangles[tri].v1 = 6;
triangles[tri].v2 = 5;
tri++;
(*pp_cube_face_colors)[tri] = Vec3fa(0, 1, 0);
triangles[tri].v0 = 5;
triangles[tri].v1 = 6;
triangles[tri].v2 = 7;
tri++;
// bottom side
(*pp_cube_face_colors)[tri] = Vec3fa(0.5f);
triangles[tri].v0 = 0;
triangles[tri].v1 = 4;
triangles[tri].v2 = 1;
tri++;
(*pp_cube_face_colors)[tri] = Vec3fa(0.5f);
triangles[tri].v0 = 1;
triangles[tri].v1 = 4;
triangles[tri].v2 = 5;
tri++;
// top side
(*pp_cube_face_colors)[tri] = Vec3fa(1.0f);
triangles[tri].v0 = 2;
triangles[tri].v1 = 3;
triangles[tri].v2 = 6;
tri++;
(*pp_cube_face_colors)[tri] = Vec3fa(1.0f);
triangles[tri].v0 = 3;
triangles[tri].v1 = 7;
triangles[tri].v2 = 6;
tri++;
// front side
(*pp_cube_face_colors)[tri] = Vec3fa(0, 0, 1);
triangles[tri].v0 = 0;
triangles[tri].v1 = 2;
triangles[tri].v2 = 4;
tri++;
(*pp_cube_face_colors)[tri] = Vec3fa(0, 0, 1);
triangles[tri].v0 = 2;
triangles[tri].v1 = 6;
triangles[tri].v2 = 4;
tri++;
// back side
(*pp_cube_face_colors)[tri] = Vec3fa(1, 1, 0);
triangles[tri].v0 = 1;
triangles[tri].v1 = 5;
triangles[tri].v2 = 3;
tri++;
(*pp_cube_face_colors)[tri] = Vec3fa(1, 1, 0);
triangles[tri].v0 = 3;
triangles[tri].v1 = 5;
triangles[tri].v2 = 7;
tri++;
rtcSetGeometryVertexAttributeCount(mesh, 1);
rtcSetSharedGeometryBuffer(mesh, RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE, 0,
RTC_FORMAT_FLOAT3, *pp_cube_vertex_colors, 0,
sizeof(Vec3fa), 8);
/*
* You must commit geometry objects when you are done setting them up,
* or you will not get any intersections.
*/
rtcCommitGeometry(mesh);
/*
* In rtcAttachGeometry(...), the scene takes ownership of the geom
* by increasing its reference count. This means that we don't have
* to hold on to the geom handle, and may release it. The geom object
* will be released automatically when the scene is destroyed.
*
* rtcAttachGeometry() returns a geometry ID. We could use this to
* identify intersected objects later on.
*/
unsigned int geomID = rtcAttachGeometry(scene, mesh);
rtcReleaseGeometry(mesh);
return geomID;
}
/* adds a ground plane to the scene */
unsigned int addGroundPlane(const sycl::queue& queue,
const RTCDevice& emb_device,
const RTCScene& emb_scene,
Vec3fa** pp_ground_face_colors,
Vec3fa** pp_ground_vertex_colors) {
/* create a triangulated plane with 2 triangles and 4 vertices */
RTCGeometry mesh = rtcNewGeometry(emb_device, RTC_GEOMETRY_TYPE_TRIANGLE);
/* create face and vertex color arrays */
*pp_ground_face_colors =
alignedSYCLUSMMallocDeviceReadOnly<Vec3fa>(queue, 2, 16);
/* For GPU, we change the vertex color buffer allocation from aligned malloc
* to a SYCL allocator */
*pp_ground_vertex_colors =
alignedSYCLUSMMallocDeviceReadOnly<Vec3fa>(queue, 4, 16);
/* set vertices and vertex colors */
Vertex* vertices = alignedSYCLUSMMallocDeviceReadOnly<Vertex>(queue, 4, 16);
/* For GPU, change rtcSetNewGeometryBuffer to rtcSetSharedGeometryBuffer to
* work with the SYCL allocator */
rtcSetSharedGeometryBuffer(mesh, RTC_BUFFER_TYPE_VERTEX, 0, RTC_FORMAT_FLOAT3,
vertices, 0, sizeof(Vertex), 4);
(*pp_ground_vertex_colors)[0] = Vec3fa(1, 0, 0);
vertices[0].x = -10;
vertices[0].y = -2;
vertices[0].z = -10;
(*pp_ground_vertex_colors)[1] = Vec3fa(1, 0, 1);
vertices[1].x = -10;
vertices[1].y = -2;
vertices[1].z = +10;
(*pp_ground_vertex_colors)[2] = Vec3fa(1, 1, 0);
vertices[2].x = +10;
vertices[2].y = -2;
vertices[2].z = -10;
(*pp_ground_vertex_colors)[3] = Vec3fa(1, 1, 1);
vertices[3].x = +10;
vertices[3].y = -2;
vertices[3].z = +10;
/* set triangles */
Triangle* triangles =
alignedSYCLUSMMallocDeviceReadOnly<Triangle>(queue, 2, 16);
/* For GPU, change rtcSetNewGeometryBuffer to rtcSetSharedGeometryBuffer to
* work with the SYCL allocator */
rtcSetSharedGeometryBuffer(mesh, RTC_BUFFER_TYPE_INDEX, 0, RTC_FORMAT_UINT3,
triangles, 0, sizeof(Triangle), 2);
(*pp_ground_face_colors)[0] = Vec3fa(1, 0, 0);
triangles[0].v0 = 0;
triangles[0].v1 = 1;
triangles[0].v2 = 2;
(*pp_ground_face_colors)[1] = Vec3fa(1, 0, 0);
triangles[1].v0 = 1;
triangles[1].v1 = 3;
triangles[1].v2 = 2;
rtcSetGeometryVertexAttributeCount(mesh, 1);
rtcSetSharedGeometryBuffer(mesh, RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE, 0,
RTC_FORMAT_FLOAT3, *pp_ground_vertex_colors, 0,
sizeof(Vec3fa), 4);
/*
* You must commit geometry objects when you are done setting them up,
* or you will not get any intersections.
*/
rtcCommitGeometry(mesh);
/*
* In rtcAttachGeometry(...), the scene takes ownership of the geom
* by increasing its reference count. This means that we don't have
* to hold on to the geom handle, and may release it. The geom object
* will be released automatically when the scene is destroyed.
*
* rtcAttachGeometry() returns a geometry ID. We could use this to
* identify intersected objects later on.
*/
unsigned int geomID = rtcAttachGeometry(emb_scene, mesh);
rtcReleaseGeometry(mesh);
return geomID;
}
/* task that renders a single screen pixel */
void renderPixelStandard(
int x, int y, unsigned char* pixels, const unsigned int width,
const unsigned int height, const unsigned int channels, const float time,
Camera* p_camera, const RTCFeatureFlags features, const RTCScene& scene,
Vec3fa* p_cube_face_colors, Vec3fa* p_ground_face_colors,
unsigned int* p_usm_geomIDs) {
RTCIntersectArguments iargs;
rtcInitIntersectArguments(&iargs);
iargs.feature_mask = features;
const Vec3fa dir =
normalize(x * p_camera->l.vx + y * p_camera->l.vy + p_camera->l.vz);
const Vec3fa org = Vec3fa(p_camera->p.x, p_camera->p.y, p_camera->p.z);
/* initialize ray */
RTCRayHit rhPrimary;
rhPrimary.ray.dir_x = dir.x;
rhPrimary.ray.dir_y = dir.y;
rhPrimary.ray.dir_z = dir.z;
rhPrimary.ray.org_x = org.x;
rhPrimary.ray.org_y = org.y;
rhPrimary.ray.org_z = org.z;
rhPrimary.ray.tnear = 0.0f;
rhPrimary.ray.time = time;
rhPrimary.ray.tfar = std::numeric_limits<float>::infinity();
rhPrimary.hit.geomID = RTC_INVALID_GEOMETRY_ID;
rhPrimary.hit.primID = RTC_INVALID_GEOMETRY_ID;
rhPrimary.ray.mask = -1;
/* intersect ray with scene */
rtcIntersect1(scene, &rhPrimary, &iargs);
/* shade pixels */
Vec3fa color = Vec3fa(0.0f);
if (rhPrimary.hit.geomID != RTC_INVALID_GEOMETRY_ID) {
Vec3fa diffuse;
if (rhPrimary.hit.geomID == p_usm_geomIDs[0])
diffuse = p_cube_face_colors[rhPrimary.hit.primID];
else if (rhPrimary.hit.geomID == p_usm_geomIDs[1])
diffuse = p_ground_face_colors[rhPrimary.hit.primID];
color = color + diffuse * 0.5f;
Vec3fa lightDir = normalize(Vec3fa(-1, -1, -1));
/* initialize shadow ray */
RTCRay rShadow;
Vec3fa sOrg = org + rhPrimary.ray.tfar * dir;
rShadow.dir_x = -lightDir.x;
rShadow.dir_y = -lightDir.y;
rShadow.dir_z = -lightDir.z;
rShadow.org_x = sOrg.x;
rShadow.org_y = sOrg.y;
rShadow.org_z = sOrg.z;
rShadow.tnear = 0.001f;
rShadow.time = 0.0f;
rShadow.tfar = std::numeric_limits<float>::infinity();
rShadow.mask = -1;
RTCOccludedArguments oargs;
rtcInitOccludedArguments(&oargs);
oargs.flags = RTC_RAY_QUERY_FLAG_INCOHERENT;
oargs.feature_mask = RTC_FEATURE_FLAG_TRIANGLE;
/* trace shadow ray */
rtcOccluded1(scene, &rShadow, &oargs);
/* add light contribution */
if (rShadow.tfar >= 0.0f) {
Vec3fa Ng =
Vec3fa(rhPrimary.hit.Ng_x, rhPrimary.hit.Ng_y, rhPrimary.hit.Ng_z);
color =
color + diffuse * clamp(-dot(lightDir, normalize(Ng)), 0.0f, 1.0f);
}
}
/* write color to framebuffer */
unsigned char r = (unsigned char)(255.0f * clamp(color.x, 0.0f, 1.0f));
unsigned char g = (unsigned char)(255.0f * clamp(color.y, 0.0f, 1.0f));
unsigned char b = (unsigned char)(255.0f * clamp(color.z, 0.0f, 1.0f));
pixels[y * width * channels + x * channels] = r;
pixels[y * width * channels + x * channels + 1] = g;
pixels[y * width * channels + x * channels + 2] = b;
}
/* called by the C++ code to render */
void renderFrameStandard(sycl::queue* queue, unsigned char* pixels,
const unsigned int width, const unsigned int height,
const unsigned int channels, const float time,
Camera* p_camera, const RTCScene& scene,
Vec3fa* p_cube_face_colors,
Vec3fa* p_ground_face_colors,
unsigned int* p_usm_geomIDs) {
sycl::event event = queue->submit([=](sycl::handler& cgh) {
cgh.set_specialization_constant<feature_mask>(required_features);
const sycl::nd_range<2> nd_range = make_nd_range(width, height);
cgh.parallel_for(
nd_range, [=](sycl::nd_item<2> item, sycl::kernel_handler kh) {
const unsigned int x = item.get_global_id(0);
if (x >= width) return;
const unsigned int y = item.get_global_id(1);
if (y >= height) return;
const RTCFeatureFlags features =
kh.get_specialization_constant<feature_mask>();
renderPixelStandard(x, y, pixels, width, height, channels, time,
p_camera, features, scene, p_cube_face_colors,
p_ground_face_colors, p_usm_geomIDs);
});
});
event.wait_and_throw();
}
/* called by the C++ code for cleanup */
void device_cleanup(const sycl::queue& queue, RTCScene* p_scene,
Vec3fa** pp_cube_face_colors,
Vec3fa** pp_cube_vertex_colors,
Vec3fa** pp_ground_face_colors,
Vec3fa** pp_ground_vertex_colors) {
rtcReleaseScene(*p_scene);
*p_scene = nullptr;
if (*pp_cube_face_colors) alignedSYCLFree(queue, *pp_cube_face_colors);
*pp_cube_face_colors = nullptr;
if (*pp_cube_vertex_colors) alignedSYCLFree(queue, *pp_cube_vertex_colors);
*pp_cube_vertex_colors = nullptr;
if (*pp_ground_face_colors) alignedSYCLFree(queue, *pp_ground_face_colors);
*pp_ground_face_colors = nullptr;
if (*pp_ground_vertex_colors)
alignedSYCLFree(queue, *pp_ground_vertex_colors);
*pp_ground_vertex_colors = nullptr;
}
/*
* Create a scene, which is a collection of geometry objects. Scenes are
* what the intersect / occluded functions work on. You can think of a
* scene as an acceleration structure, e.g. a bounding-volume hierarchy.
*
* Scenes, like devices, are reference-counted.
*/
RTCScene initializeScene(
const sycl::queue& queue, const RTCDevice& device,
Vec3fa** pp_cube_face_colors, Vec3fa** pp_cube_vertex_colors,
Vec3fa** pp_ground_face_colors, Vec3fa** pp_ground_vertex_colors,
std::vector<unsigned int, USMDRO_UI_ALLOC>* p_geomIDs) {
RTCScene scene = rtcNewScene(device);
/* add cube */
p_geomIDs->push_back(addCube(queue, device, scene, pp_cube_face_colors,
pp_cube_vertex_colors));
/* add ground plane */
p_geomIDs->push_back(addGroundPlane(
queue, device, scene, pp_ground_face_colors, pp_ground_vertex_colors));
/*
* Like geometry objects, scenes must be committed. This lets
* Embree know that it may start building an acceleration structure.
*/
rtcCommitScene(scene);
return scene;
}
RTCDevice initializeDevice(sycl::context& sycl_context,
sycl::device& sycl_device) {
RTCDevice device = rtcNewSYCLDevice(sycl_context, "");
if (!device)
printf("fail: error %d: cannot create device\n", rtcGetDeviceError(NULL));
rtcSetDeviceErrorFunction(device, error_handler, NULL);
return device;
}
void enablePersistentJITCache() {
#if defined(_WIN32)
_putenv_s("SYCL_CACHE_PERSISTENT", "1");
_putenv_s("SYCL_CACHE_DIR", "cache");
#else
setenv("SYCL_CACHE_PERSISTENT", "1", 1);
setenv("SYCL_CACHE_DIR", "cache", 1);
#endif
}
int main() {
enablePersistentJITCache();
Vec3fa* p_cube_face_colors = nullptr;
Vec3fa* p_cube_vertex_colors = nullptr;
Vec3fa* p_ground_face_colors = nullptr;
Vec3fa* p_ground_vertex_colors = nullptr;
/* The allocator helps us use USM pointers for the vector container */
/* Acheived:
* 1. Dynamic element creation (push_back) into USM memory
* 2. USM pointer access in the device kernel
* 3. Device knows this is "read only" memory, thus there is no transfer back upon kernel
* completion.*/
std::vector<unsigned int, USMDRO_UI_ALLOC>* p_geomIDs = nullptr;
RTCDevice emb_device = nullptr;
RTCScene emb_scene = nullptr;
unsigned char* p_pixels;
Camera* p_camera = nullptr;
/* This will select the first GPU supported by Embree */
sycl::device sycl_device(rtcSYCLDeviceSelector);
sycl::queue sycl_queue(sycl_device);
sycl::context sycl_context(sycl_device);
/* Creation of the USM allocator object uses arguments of the queue, and the needed
* OneAPI usm property */
USMDRO_UI_ALLOC qalloc(sycl_queue,
sycl::ext::oneapi::property::usm::device_read_only());
/* The std::vector object is created using the allocator */
p_geomIDs = new std::vector<unsigned int, USMDRO_UI_ALLOC>(qalloc);
emb_device = initializeDevice(sycl_context, sycl_device);
emb_scene = initializeScene(sycl_queue, emb_device, &p_cube_face_colors,
&p_cube_vertex_colors, &p_ground_face_colors,
&p_ground_vertex_colors, p_geomIDs);
/* std::vector says memory is linear, thus an address to element 0 is not a
* usm address */
/* The USM address is used in the kernel */
unsigned int* p_usm_geomIDs = &((*p_geomIDs)[0]);
/* create an image buffer initialize it with all zeroes */
const unsigned int width = 320;
const unsigned int height = 200;
const unsigned int channels = 3;
p_pixels = alignedSYCLUSMMalloc<unsigned char>(sycl_queue,
width * height * channels, 64);
std::memset(p_pixels, 0, sizeof(unsigned char) * width * height * channels);
p_camera = alignedSYCLUSMMallocDeviceReadOnly<Camera>(sycl_queue, 1, 64);
*p_camera = positionCamera(Vec3fa(1.5f, 1.5f, -1.5f), Vec3fa(0, 0, 0),
Vec3fa(0, 1, 0), 90.0f, width, height);
renderFrameStandard(&sycl_queue, p_pixels, width, height, channels, 0.0,
p_camera, emb_scene, p_cube_face_colors,
p_ground_face_colors, p_usm_geomIDs);
stbi_write_png("rkRayTracerGPU.png", width, height, channels, p_pixels,
width * channels);
if (p_pixels) {
alignedSYCLFree(sycl_queue, p_pixels);
p_pixels = nullptr;
}
if (p_geomIDs) {
delete p_geomIDs;
p_geomIDs = nullptr;
}
if (p_camera) {
alignedSYCLFree(sycl_queue, p_camera);
p_camera = nullptr;
}
device_cleanup(sycl_queue, &emb_scene, &p_cube_face_colors,
&p_cube_vertex_colors, &p_ground_face_colors,
&p_ground_vertex_colors);
rtcReleaseDevice(emb_device);
printf("success\n");
return 0;
}
| cpp |
oneAPI-samples | data/projects/oneAPI-samples/RenderingToolkit/Tutorial/IntroToRayTracingWithEmbree/gpu/src/MathBindings.h | #pragma once
#ifndef FILEMATHBINDINGSSEEN
#define FILEMATHBINDINGSSEEN
#include "sycl/sycl.hpp"
/* These MathBindings are a quick wrapper for abstraction of geometric algebra operations and vector types. This wrapper functions on both host and target SYCL device.
* In previous introduction programs, rkcommon library was used in the CPU only environment. This manual definition of grometric algebra types allows for targeting CPU or GPU devices and can be a stand-in for your own library.
* A standard SYCL vector type sycl::float2 and sycl::float4 and x,y,[z] union aliasing is used to be adaptable and more optimizable for application kernels.
*
*/
/* We don't use much from Vec4ff, just in accumulation buffers. So 4 dimensional vector overloads are not needed for samples.*/
using Vec4ff = sycl::float4;
namespace MathBindings {
struct Vec3fa {
union {
sycl::float4 data;
struct {
float x, y, z, pad;
};
};
Vec3fa() {}
Vec3fa(float in_x, float in_y, float in_z) {
this->x = in_x;
this->y = in_y;
this->z = in_z;
}
Vec3fa(const float& in) { this->x = this->y = this->z = in; }
Vec3fa(const sycl::float4& in) { this->data = in; }
// Do not use w... We use Vec3fa for alignment.
inline Vec3fa operator+(const Vec3fa& second) {
Vec3fa out(this->data + second.data);
return out;
}
inline Vec3fa operator-(const Vec3fa& second) {
Vec3fa out(this->data - second.data);
return out;
}
inline Vec3fa operator/(const Vec3fa& second) {
return Vec3fa(this->x / second.x, this->y / second.y, this->z / second.z);
}
inline Vec3fa operator/(float f) {
return Vec3fa(this->x / f, this->y / f, this->z / f);
}
friend inline Vec3fa operator-(const Vec3fa& in) {
return Vec3fa(-in.x, -in.y, -in.z);
}
friend inline Vec3fa operator*(const Vec3fa& first, const Vec3fa& second) {
return Vec3fa(first.x * second.x, first.y * second.y, first.z * second.z);
}
friend inline const Vec3fa operator-(const Vec3fa& first,
const Vec3fa& second) {
return Vec3fa(first.data - second.data);
}
};
inline const Vec3fa operator+(const Vec3fa& first, const Vec3fa& second) {
return Vec3fa(first.data + second.data);
}
inline Vec3fa operator*(const float& f, const Vec3fa& in) {
return Vec3fa(in.x * f, in.y * f, in.z * f);
}
inline Vec3fa operator*(const Vec3fa& in, const float& f) {
return Vec3fa(in.x * f, in.y * f, in.z * f);
}
inline float dot(const Vec3fa& first, const Vec3fa& second) {
return first.data[0] * second.data[0] + first.data[1] * second.data[1] +
first.data[2] * second.data[2];
}
inline float dot(const sycl::float3& first,
const sycl::float3& second) {
return first[0] * second[0] + first[1] * second[1] + first[2] * second[2];
}
inline Vec3fa normalize(const Vec3fa& in) {
return in * sycl::rsqrt(dot(in, in));
}
inline Vec3fa cross(const Vec3fa& first, const Vec3fa& second) {
return Vec3fa(first.y * second.z - first.z * second.y,
first.z * second.x - first.x * second.z,
first.x * second.y - first.y * second.x);
}
struct LinearSpace3fa {
/*! default matrix constructor */
inline LinearSpace3fa() = default;
inline LinearSpace3fa(const LinearSpace3fa& other) {
vx = other.vx;
vy = other.vy;
vz = other.vz;
}
inline LinearSpace3fa& operator=(const LinearSpace3fa& other) {
vx = other.vx;
vy = other.vy;
vz = other.vz;
return *this;
}
inline LinearSpace3fa(Vec3fa a, Vec3fa b, Vec3fa c) {
vx = a;
vy = b;
vz = c;
}
Vec3fa vx, vy, vz;
};
struct AffineSpace3fa {
inline AffineSpace3fa() = default;
LinearSpace3fa l;
Vec3fa p;
};
inline LinearSpace3fa operator*(const float& a, const LinearSpace3fa& b) {
return LinearSpace3fa(a * b.vx, a * b.vy, a * b.vz);
}
inline Vec3fa operator*(const LinearSpace3fa& a, const Vec3fa& b) {
return b.x * a.vx + b.y * a.vy + b.z * a.vz;
}
/* Wrapping sycl::float2 here. This makes a pathtracer kernel with 2D random sampling on a
* hemisphere a little bit more adaptable.*/
struct Vec2f {
union {
sycl::float2 data;
struct {
float x, y;
};
};
Vec2f() {}
Vec2f(float in_x, float in_y) {
this->x = x;
this->y = y;
}
Vec2f(const Vec2f& in) {
this->x = in.x;
this->y = in.y;
}
Vec2f(const float& in) { this->x = this->y = in; }
inline Vec2f operator+(const Vec2f& second) {
Vec2f out;
out.data = this->data + second.data;
return out;
}
inline Vec2f operator-(const Vec2f& second) {
Vec2f out;
out.data = this->data - second.data;
return out;
}
inline Vec2f operator/(const Vec2f& second) {
return Vec2f(this->x / second.x, this->y / second.y);
}
inline Vec2f operator/(float f) { return Vec2f(this->x / f, this->y / f); }
friend inline Vec2f operator-(const Vec2f& in) { return Vec2f(-in.x, -in.y); }
friend inline Vec2f operator*(const Vec2f& first, const Vec2f& second) {
return Vec2f(first.x * second.x, first.y * second.y);
}
};
inline Vec2f operator*(const float& f, const Vec2f& in) {
return Vec2f(in.x * f, in.y * f);
}
inline Vec2f operator*(const Vec2f& in, const float& f) {
return Vec2f(in.x * f, in.y * f);
}
inline float dot(const Vec2f& first, const Vec2f& second) {
return first.data[0] * second.data[0] + first.data[1] * second.data[1];
}
inline float dot(const sycl::float2& first,
const sycl::float2& second) {
return first[0] * second[0] + first[1] * second[1] + first[2] * second[2];
}
inline Vec2f normalize(const Vec2f& in) {
return in * sycl::rsqrt(dot(in, in));
}
inline float deg2rad(const float& x) {
return x * float(1.745329251994329576923690768489e-2);
}
inline float clamp(const float& x) { return sycl::clamp(x, 0.f, 1.f); }
inline float clamp(const float& x, const float& y, const float& z) {
return sycl::clamp(x, y, z);
}
} // namespace MathBindings
#endif // FILEMATHBINDINGSSEEN
| h |
oneAPI-samples | data/projects/oneAPI-samples/RenderingToolkit/Tutorial/IntroToRayTracingWithEmbree/cpu/src/rkRayTracer.cpp | #ifdef _MSC_VER
#ifndef NOMINMAX
/* use the c++ library min and max instead of the MSVS macros */
/* rkcommon will define this macro upon CMake import */
#define NOMINMAX
#endif
#endif
#include <embree4/rtcore.h>
#include <rkcommon/math/vec.h>
#include <rkcommon/memory/malloc.h>
#include <tbb/parallel_for.h>
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include <stb/stb_image_write.h>
using Vec3fa = rkcommon::math::vec_t<float, 3, 1>;
using rkcommon::math::cross;
using rkcommon::math::deg2rad;
using rkcommon::math::normalize;
using std::max;
using std::min;
#ifdef _WIN32
#define alignedMalloc(a, b) _aligned_malloc(a, b)
#define alignedFree(a) _aligned_free(a)
#else
#include <mm_malloc.h>
#define alignedMalloc(a, b) _mm_malloc(a, b)
#define alignedFree(a) _mm_free(a)
#endif
#define TILE_SIZE_X 8
#define TILE_SIZE_Y 8
struct LinearSpace3 {
Vec3fa vx, vy, vz;
};
typedef struct Affine3fa {
LinearSpace3 l;
Vec3fa p;
} ISPCCamera;
Vec3fa* g_cube_face_colors = nullptr;
Vec3fa* g_cube_vertex_colors = nullptr;
Vec3fa* g_ground_face_colors = nullptr;
Vec3fa* g_ground_vertex_colors = nullptr;
std::vector<unsigned int> geomIDs;
RTCDevice g_device = nullptr;
RTCScene g_scene = nullptr;
unsigned char* g_pixels;
ISPCCamera g_camera;
void error_handler(void* userPtr, const RTCError code, const char* str) {
if (code == RTC_ERROR_NONE) return;
printf("fail: Embree Error: ");
switch (code) {
case RTC_ERROR_UNKNOWN:
printf("RTC_ERROR_UNKNOWN");
break;
case RTC_ERROR_INVALID_ARGUMENT:
printf("RTC_ERROR_INVALID_ARGUMENT");
break;
case RTC_ERROR_INVALID_OPERATION:
printf("RTC_ERROR_INVALID_OPERATION");
break;
case RTC_ERROR_OUT_OF_MEMORY:
printf("RTC_ERROR_OUT_OF_MEMORY");
break;
case RTC_ERROR_UNSUPPORTED_CPU:
printf("RTC_ERROR_UNSUPPORTED_CPU");
break;
case RTC_ERROR_CANCELLED:
printf("RTC_ERROR_CANCELLED");
break;
default:
printf("invalid error code");
break;
}
if (str) {
printf(" (");
while (*str) putchar(*str++);
printf(")\n");
}
exit(1);
}
/* from tutorial_device.h */
/* vertex and triangle layout */
struct Vertex {
float x, y, z, r;
};
struct Triangle {
int v0, v1, v2;
};
ISPCCamera positionCamera(Vec3fa from, Vec3fa to, Vec3fa up, float fov,
size_t width, size_t height) {
/* There are many ways to set up a camera projection. This one is consolidated
* from the camera code in the Embree/tutorial/common/tutorial/camera.h object
*/
ISPCCamera camMatrix;
Vec3fa Z =
rkcommon::math::normalize(rkcommon::math::vec_t<float, 3, 1>(to - from));
Vec3fa U = rkcommon::math::normalize(
rkcommon::math::cross(rkcommon::math::vec_t<float, 3, 1>(up),
rkcommon::math::vec_t<float, 3, 1>(Z)));
Vec3fa V = rkcommon::math::normalize(
rkcommon::math::cross(rkcommon::math::vec_t<float, 3, 1>(Z),
rkcommon::math::vec_t<float, 3, 1>(U)));
camMatrix.l.vx = U;
camMatrix.l.vy = V;
camMatrix.l.vz = Z;
camMatrix.p = from;
/* negate for a right handed camera*/
camMatrix.l.vx = -camMatrix.l.vx;
const float fovScale = 1.0f / tanf(rkcommon::math::deg2rad(0.5f * fov));
camMatrix.l.vz = -0.5f * width * camMatrix.l.vx +
0.5f * height * camMatrix.l.vy +
0.5f * height * fovScale * camMatrix.l.vz;
camMatrix.l.vy = -camMatrix.l.vy;
return camMatrix;
}
/* adds a cube to the scene */
unsigned int addCube(RTCScene _scene) {
/* create a triangulated cube with 12 triangles and 8 vertices */
RTCGeometry mesh = rtcNewGeometry(g_device, RTC_GEOMETRY_TYPE_TRIANGLE);
/* create face and vertex color arrays */
g_cube_face_colors = (Vec3fa*)alignedMalloc(sizeof(Vec3fa) * 12, 16);
g_cube_vertex_colors = (Vec3fa*)alignedMalloc(sizeof(Vec3fa) * 8, 16);
/* set vertices and vertex colors */
Vertex* vertices = (Vertex*)rtcSetNewGeometryBuffer(
mesh, RTC_BUFFER_TYPE_VERTEX, 0, RTC_FORMAT_FLOAT3, sizeof(Vertex), 8);
g_cube_vertex_colors[0] = Vec3fa(0, 0, 0);
vertices[0].x = -1;
vertices[0].y = -1;
vertices[0].z = -1;
g_cube_vertex_colors[1] = Vec3fa(0, 0, 1);
vertices[1].x = -1;
vertices[1].y = -1;
vertices[1].z = +1;
g_cube_vertex_colors[2] = Vec3fa(0, 1, 0);
vertices[2].x = -1;
vertices[2].y = +1;
vertices[2].z = -1;
g_cube_vertex_colors[3] = Vec3fa(0, 1, 1);
vertices[3].x = -1;
vertices[3].y = +1;
vertices[3].z = +1;
g_cube_vertex_colors[4] = Vec3fa(1, 0, 0);
vertices[4].x = +1;
vertices[4].y = -1;
vertices[4].z = -1;
g_cube_vertex_colors[5] = Vec3fa(1, 0, 1);
vertices[5].x = +1;
vertices[5].y = -1;
vertices[5].z = +1;
g_cube_vertex_colors[6] = Vec3fa(1, 1, 0);
vertices[6].x = +1;
vertices[6].y = +1;
vertices[6].z = -1;
g_cube_vertex_colors[7] = Vec3fa(1, 1, 1);
vertices[7].x = +1;
vertices[7].y = +1;
vertices[7].z = +1;
/* set triangles and face colors */
int tri = 0;
Triangle* triangles = (Triangle*)rtcSetNewGeometryBuffer(
mesh, RTC_BUFFER_TYPE_INDEX, 0, RTC_FORMAT_UINT3, sizeof(Triangle), 12);
// left side
g_cube_face_colors[tri] = Vec3fa(1, 0, 0);
triangles[tri].v0 = 0;
triangles[tri].v1 = 1;
triangles[tri].v2 = 2;
tri++;
g_cube_face_colors[tri] = Vec3fa(1, 0, 0);
triangles[tri].v0 = 1;
triangles[tri].v1 = 3;
triangles[tri].v2 = 2;
tri++;
// right side
g_cube_face_colors[tri] = Vec3fa(0, 1, 0);
triangles[tri].v0 = 4;
triangles[tri].v1 = 6;
triangles[tri].v2 = 5;
tri++;
g_cube_face_colors[tri] = Vec3fa(0, 1, 0);
triangles[tri].v0 = 5;
triangles[tri].v1 = 6;
triangles[tri].v2 = 7;
tri++;
// bottom side
g_cube_face_colors[tri] = Vec3fa(0.5f);
triangles[tri].v0 = 0;
triangles[tri].v1 = 4;
triangles[tri].v2 = 1;
tri++;
g_cube_face_colors[tri] = Vec3fa(0.5f);
triangles[tri].v0 = 1;
triangles[tri].v1 = 4;
triangles[tri].v2 = 5;
tri++;
// top side
g_cube_face_colors[tri] = Vec3fa(1.0f);
triangles[tri].v0 = 2;
triangles[tri].v1 = 3;
triangles[tri].v2 = 6;
tri++;
g_cube_face_colors[tri] = Vec3fa(1.0f);
triangles[tri].v0 = 3;
triangles[tri].v1 = 7;
triangles[tri].v2 = 6;
tri++;
// front side
g_cube_face_colors[tri] = Vec3fa(0, 0, 1);
triangles[tri].v0 = 0;
triangles[tri].v1 = 2;
triangles[tri].v2 = 4;
tri++;
g_cube_face_colors[tri] = Vec3fa(0, 0, 1);
triangles[tri].v0 = 2;
triangles[tri].v1 = 6;
triangles[tri].v2 = 4;
tri++;
// back side
g_cube_face_colors[tri] = Vec3fa(1, 1, 0);
triangles[tri].v0 = 1;
triangles[tri].v1 = 5;
triangles[tri].v2 = 3;
tri++;
g_cube_face_colors[tri] = Vec3fa(1, 1, 0);
triangles[tri].v0 = 3;
triangles[tri].v1 = 5;
triangles[tri].v2 = 7;
tri++;
rtcSetGeometryVertexAttributeCount(mesh, 1);
rtcSetSharedGeometryBuffer(mesh, RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE, 0,
RTC_FORMAT_FLOAT3, g_cube_vertex_colors, 0,
sizeof(Vec3fa), 8);
rtcCommitGeometry(mesh);
unsigned int geomID = rtcAttachGeometry(_scene, mesh);
rtcReleaseGeometry(mesh);
return geomID;
}
/* adds a ground plane to the scene */
unsigned int addGroundPlane(RTCScene _scene) {
/* create a triangulated plane with 2 triangles and 4 vertices */
RTCGeometry mesh = rtcNewGeometry(g_device, RTC_GEOMETRY_TYPE_TRIANGLE);
/* create face and vertex color arrays */
g_ground_face_colors = (Vec3fa*)alignedMalloc(sizeof(Vec3fa) * 2, 16);
g_ground_vertex_colors = (Vec3fa*)alignedMalloc(sizeof(Vec3fa) * 4, 16);
/* set vertices */
Vertex* vertices = (Vertex*)rtcSetNewGeometryBuffer(
mesh, RTC_BUFFER_TYPE_VERTEX, 0, RTC_FORMAT_FLOAT3, sizeof(Vertex), 4);
g_ground_vertex_colors[0] = Vec3fa(1, 0, 0);
vertices[0].x = -10;
vertices[0].y = -2;
vertices[0].z = -10;
g_ground_vertex_colors[1] = Vec3fa(1, 0, 1);
vertices[1].x = -10;
vertices[1].y = -2;
vertices[1].z = +10;
g_ground_vertex_colors[2] = Vec3fa(1, 1, 0);
vertices[2].x = +10;
vertices[2].y = -2;
vertices[2].z = -10;
g_ground_vertex_colors[3] = Vec3fa(1, 1, 1);
vertices[3].x = +10;
vertices[3].y = -2;
vertices[3].z = +10;
/* set triangles */
Triangle* triangles = (Triangle*)rtcSetNewGeometryBuffer(
mesh, RTC_BUFFER_TYPE_INDEX, 0, RTC_FORMAT_UINT3, sizeof(Triangle), 2);
g_ground_face_colors[0] = Vec3fa(1, 0, 0);
triangles[0].v0 = 0;
triangles[0].v1 = 1;
triangles[0].v2 = 2;
g_ground_face_colors[1] = Vec3fa(1, 0, 0);
triangles[1].v0 = 1;
triangles[1].v1 = 3;
triangles[1].v2 = 2;
rtcSetGeometryVertexAttributeCount(mesh, 1);
rtcSetSharedGeometryBuffer(mesh, RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE, 0,
RTC_FORMAT_FLOAT3, g_ground_vertex_colors, 0,
sizeof(Vec3fa), 4);
rtcCommitGeometry(mesh);
unsigned int geomID = rtcAttachGeometry(_scene, mesh);
rtcReleaseGeometry(mesh);
return geomID;
}
/* task that renders a single screen pixel */
void renderPixelStandard(int x, int y, unsigned char* pixels,
const unsigned int width, const unsigned int height,
const unsigned int channels, const float time,
const ISPCCamera& camera) {
/* RTCIntersectArguments is new for Embree 4 */
RTCIntersectArguments iargs;
rtcInitIntersectArguments(&iargs);
iargs.feature_mask = RTC_FEATURE_FLAG_TRIANGLE;
iargs.flags = RTC_RAY_QUERY_FLAG_COHERENT;
const Vec3fa dir = rkcommon::math::normalize(x * camera.l.vx +
y * camera.l.vy + camera.l.vz);
const Vec3fa org = Vec3fa(camera.p.x, camera.p.y, camera.p.z);
/* initialize ray */
RTCRayHit rhPrimary;
rhPrimary.ray.dir_x = dir.x;
rhPrimary.ray.dir_y = dir.y;
rhPrimary.ray.dir_z = dir.z;
rhPrimary.ray.org_x = org.x;
rhPrimary.ray.org_y = org.y;
rhPrimary.ray.org_z = org.z;
rhPrimary.ray.tnear = 0.0f;
rhPrimary.ray.time = time;
rhPrimary.ray.tfar = std::numeric_limits<float>::infinity();
rhPrimary.hit.geomID = RTC_INVALID_GEOMETRY_ID;
rhPrimary.hit.primID = RTC_INVALID_GEOMETRY_ID;
rhPrimary.ray.mask = -1;
/* intersect ray with scene */
rtcIntersect1(g_scene, &rhPrimary, &iargs);
/* shade pixels */
Vec3fa color = Vec3fa(0.0f);
if (rhPrimary.hit.geomID != RTC_INVALID_GEOMETRY_ID) {
Vec3fa diffuse;
if (rhPrimary.hit.geomID == geomIDs[0])
diffuse = g_cube_face_colors[rhPrimary.hit.primID];
else if (rhPrimary.hit.geomID == geomIDs[1])
diffuse = g_ground_face_colors[rhPrimary.hit.primID];
color = color + diffuse * 0.5f;
Vec3fa lightDir = normalize(Vec3fa(-1, -1, -1));
/* initialize shadow ray */
RTCRay rShadow;
Vec3fa sOrg = org + rhPrimary.ray.tfar * dir;
rShadow.dir_x = -lightDir.x;
rShadow.dir_y = -lightDir.y;
rShadow.dir_z = -lightDir.z;
rShadow.org_x = sOrg.x;
rShadow.org_y = sOrg.y;
rShadow.org_z = sOrg.z;
rShadow.tnear = 0.001f;
rShadow.time = 0.0f;
rShadow.tfar = std::numeric_limits<float>::infinity();
rShadow.mask = -1;
/* New for Embree 4 */
RTCOccludedArguments oargs;
rtcInitOccludedArguments(&oargs);
oargs.flags = RTC_RAY_QUERY_FLAG_INCOHERENT;
oargs.feature_mask = RTC_FEATURE_FLAG_TRIANGLE;
/* trace shadow ray */
rtcOccluded1(g_scene, &rShadow, &oargs);
/* add light contribution */
if (rShadow.tfar >= 0.0f) {
Vec3fa Ng =
Vec3fa(rhPrimary.hit.Ng_x, rhPrimary.hit.Ng_y, rhPrimary.hit.Ng_z);
color =
color + diffuse * rkcommon::math::clamp(
-rkcommon::math::dot(lightDir, normalize(Ng)),
0.0f, 1.0f);
}
}
/* write color to framebuffer */
unsigned char r =
(unsigned char)(255.0f * rkcommon::math::clamp(color.x, 0.0f, 1.0f));
unsigned char g =
(unsigned char)(255.0f * rkcommon::math::clamp(color.y, 0.0f, 1.0f));
unsigned char b =
(unsigned char)(255.0f * rkcommon::math::clamp(color.z, 0.0f, 1.0f));
pixels[y * width * channels + x * channels] = r;
pixels[y * width * channels + x * channels + 1] = g;
pixels[y * width * channels + x * channels + 2] = b;
}
/* task that renders a single screen tile */
void renderTileTask(int taskIndex, int threadIndex, unsigned char* pixels,
const unsigned int width, const unsigned int height,
const unsigned int channels, const float time,
const ISPCCamera& camera, const int numTilesX,
const int numTilesY) {
const unsigned int tileY = taskIndex / numTilesX;
const unsigned int tileX = taskIndex - tileY * numTilesX;
const unsigned int x0 = tileX * TILE_SIZE_X;
const unsigned int x1 = min(x0 + TILE_SIZE_X, width);
const unsigned int y0 = tileY * TILE_SIZE_Y;
const unsigned int y1 = min(y0 + TILE_SIZE_Y, height);
for (unsigned int y = y0; y < y1; y++)
for (unsigned int x = x0; x < x1; x++) {
renderPixelStandard(x, y, pixels, width, height, channels, time, camera);
}
}
/* called by the C++ code to render */
void renderFrameStandard(unsigned char* pixels, const unsigned int width,
const unsigned int height, const unsigned int channels,
const float time, const ISPCCamera& camera) {
const int numTilesX = (width + TILE_SIZE_X - 1) / TILE_SIZE_X;
const int numTilesY = (height + TILE_SIZE_Y - 1) / TILE_SIZE_Y;
tbb::task_group_context tgContext;
tbb::parallel_for(
tbb::blocked_range<size_t>(0, numTilesX * numTilesY, 1),
[&](const tbb::blocked_range<size_t>& r) {
const int threadIndex = tbb::this_task_arena::current_thread_index();
for (size_t i = r.begin(); i < r.end(); i++)
renderTileTask((int)i, threadIndex, pixels, width, height, channels,
time, camera, numTilesX, numTilesY);
},
tgContext);
if (tgContext.is_group_execution_cancelled())
throw std::runtime_error("fail: oneTBB task cancelled");
}
/* called by the C++ code for cleanup */
void device_cleanup() {
rtcReleaseScene(g_scene);
g_scene = nullptr;
if (g_cube_face_colors) alignedFree(g_cube_face_colors);
g_cube_face_colors = nullptr;
if (g_cube_vertex_colors) alignedFree(g_cube_vertex_colors);
g_cube_vertex_colors = nullptr;
if (g_ground_face_colors) alignedFree(g_ground_face_colors);
g_ground_face_colors = nullptr;
if (g_ground_vertex_colors) alignedFree(g_ground_vertex_colors);
g_ground_vertex_colors = nullptr;
}
void device_init(char* cfg) {
/* create scene */
g_scene = nullptr;
g_scene = rtcNewScene(g_device);
/* add cube */
geomIDs.push_back(addCube(g_scene));
/* add ground plane */
geomIDs.push_back(addGroundPlane(g_scene));
/* commit changes to scene */
rtcCommitScene(g_scene);
}
int main() {
/* create device */
g_device = rtcNewDevice(nullptr);
error_handler(nullptr, rtcGetDeviceError(g_device),
"fail: Embree Error Unable to create embree device");
/* set error handler */
rtcSetDeviceErrorFunction(g_device, error_handler, nullptr);
device_init(nullptr);
/* create an image buffer initialize it with all zeroes */
const unsigned int width = 320;
const unsigned int height = 200;
const unsigned int channels = 3;
g_pixels = (unsigned char*)new unsigned char[width * height * channels];
std::memset(g_pixels, 0, sizeof(unsigned char) * width * height * channels);
g_camera = positionCamera(Vec3fa(1.5f, 1.5f, -1.5f), Vec3fa(0, 0, 0),
Vec3fa(0, 1, 0), 90.0f, width, height);
renderFrameStandard(g_pixels, width, height, channels, 0.0, g_camera);
stbi_write_png("rkRayTracer.png", width, height, channels,
g_pixels, width * channels);
delete[] g_pixels;
g_pixels = nullptr;
device_cleanup();
rtcReleaseDevice(g_device);
printf("success\n");
return 0;
}
| cpp |
oneAPI-samples | data/projects/oneAPI-samples/RenderingToolkit/Tutorial/PathTracingWithEmbree/cpu/src/RandomSampler.h | #pragma once
#ifndef FILE_RANDOMSAMPLERH_SEEN
#define FILE_RANDOMSAMPLERH_SEEN
class RandomSampler {
public:
RandomSampler();
RandomSampler(unsigned int id);
RandomSampler(unsigned int pixelId, unsigned int sampleId);
RandomSampler(unsigned int x, unsigned int y, int sampleId);
/* Seperate constructor from setting the seed with same inputs */
inline void seed(unsigned int id);
inline void seed(unsigned int pixelId, unsigned int sampleId);
inline void seed(unsigned int x, unsigned int y, int sampleId);
float get_float();
int get_int();
private:
unsigned int MurmurHash3_mix(unsigned int has, unsigned int k);
unsigned int MurmurHash3_finalize(unsigned int hash);
unsigned int LCG_next(unsigned int value);
unsigned int m_s;
};
RandomSampler::RandomSampler() {
/* Use a seed function rather than this default */
seed(0);
}
RandomSampler::RandomSampler(unsigned int id) {
seed(id);
}
RandomSampler::RandomSampler(unsigned int pixelId, unsigned int sampleId) {
seed(pixelId, sampleId);
}
RandomSampler::RandomSampler(unsigned int x, unsigned int y, int sampleId) {
seed(x | (y << 16), sampleId);
}
void RandomSampler::seed(unsigned int id) {
unsigned int hash = 0;
hash = MurmurHash3_mix(hash, id);
hash = MurmurHash3_finalize(hash);
m_s = hash;
}
void RandomSampler::seed(unsigned int pixelId, unsigned int sampleId) {
unsigned int hash = 0;
hash = MurmurHash3_mix(hash, pixelId);
hash = MurmurHash3_mix(hash, sampleId);
hash = MurmurHash3_finalize(hash);
m_s = hash;
}
void RandomSampler::seed(unsigned int x, unsigned int y, int sampleId) {
seed(x | (y << 16), sampleId);
}
unsigned int RandomSampler::LCG_next(unsigned int value)
{
const unsigned int m = 1664525;
const unsigned int n = 1013904223;
return value * m + n;
}
int RandomSampler::get_int() {
m_s = LCG_next(m_s); return m_s >> 1;
}
float RandomSampler::get_float() {
return (float)get_int() * 4.656612873077392578125e-10f;
}
unsigned int RandomSampler::MurmurHash3_mix(unsigned int hash, unsigned int k) {
const unsigned int c1 = 0xcc9e2d51;
const unsigned int c2 = 0x1b873593;
const unsigned int r1 = 15;
const unsigned int r2 = 13;
const unsigned int m = 5;
const unsigned int n = 0xe6546b64;
k *= c1;
k = (k << r1) | (k >> (32 - r1));
k *= c2;
hash ^= k;
hash = ((hash << r2) | (hash >> (32 - r2))) * m + n;
return hash;
}
unsigned int RandomSampler::MurmurHash3_finalize(unsigned int hash) {
hash ^= hash >> 16;
hash *= 0x85ebca6b;
hash ^= hash >> 13;
hash *= 0xc2b2ae35;
hash ^= hash >> 16;
return hash;
}
#endif //FILE_RANDOMSAMPLERH_SEEN | h |
oneAPI-samples | data/projects/oneAPI-samples/RenderingToolkit/Tutorial/PathTracingWithEmbree/cpu/src/Renderer.h | #pragma once
#ifndef FILE_RENDERER_SEEN
#define FILE_RENDERER_SEEN
#include <embree4/rtcore.h>
#include <tbb/parallel_for.h>
#include "PathTracer.h"
#include "SceneGraph.h"
#include "definitions.h"
struct Renderer {
public:
Renderer(unsigned int width, unsigned int height, unsigned int channels,
unsigned int samples_per_pixel, unsigned int accumulation_limit,
unsigned int max_path_length, SceneSelector SELECTED_SCENE);
~Renderer();
static void handle_error(void* userPtr, const RTCError code,
const char* str);
void init_device(const char* cfg = nullptr);
void init_scene(char* cfg, unsigned int width, unsigned int height);
/* called by the C++ code to render */
void render_accumulation();
/* task that renders a single screen tile */
void render_tile_task(
int taskIndex, int threadIndex, const int numTilesX, const int numTilesY,
RandomSampler& randomSampler);
Vec3fa render_pixel_samples(
int x, int y, RandomSampler& randomSampler);
unsigned char* get_pixels();
unsigned char* m_pixels = nullptr;
private:
std::shared_ptr<PathTracer> m_pt;
/* We might want to use this function outside of our path tracer at somepoint
*/
inline Vec3fa face_forward(const Vec3fa& dir, const Vec3fa& _Ng);
/* create an image buffer initialize it with all zeroes */
const unsigned int m_width;
const unsigned int m_height;
const unsigned int m_channels;
RTCDevice m_device = nullptr;
std::shared_ptr<SceneGraph> m_sg;
/* Additions for pathtracer */
std::vector<std::shared_ptr<Vec3ff>> m_accu;
unsigned int m_accu_count = 0;
unsigned int m_max_path_length;
unsigned int m_spp;
SceneSelector m_sceneSelector;
std::vector<Light> m_lights;
unsigned long long m_accu_limit;
/* "Time" set to 0.0f for all rays as there is no motion blur, nor frame
* interpolation, nor animation */
const float m_time = 0.0f;
};
Renderer::Renderer(unsigned int width, unsigned int height,
unsigned int channels, unsigned int samples_per_pixel,
unsigned int accumulation_limit,
unsigned int max_path_length, SceneSelector SELECTED_SCENE)
: m_width(width),
m_height(height),
m_channels(channels),
m_spp(samples_per_pixel),
m_accu_limit(accumulation_limit),
m_max_path_length(max_path_length),
m_sceneSelector(SELECTED_SCENE) {
m_accu_count = 0;
m_pixels = (unsigned char*)new unsigned char[m_width * m_height * m_channels];
std::memset(m_pixels, 0,
sizeof(unsigned char) * m_width * m_height * m_channels);
/* accumulation buffer used for convenience here, but is critical in
* interactive/future applications */
m_accu.resize(m_width * m_height);
for (auto i = 0; i < m_width * m_height; i++)
m_accu[i] = std::make_shared<Vec3ff>(0.0f);
init_device(nullptr);
init_scene(nullptr, m_width, m_height);
// m_pt = std::make_shared<PathTracer>(max_path_length);
// For Multiple Importance sampling we need per pixel storage for light PDFs
m_pt = std::make_shared<PathTracer>(max_path_length, m_width, m_height,
m_sg->getNumLights());
}
void Renderer::handle_error(void* userPtr, const RTCError code,
const char* str) {
if (code == RTC_ERROR_NONE) return;
std::cout << "fail: Embree Error: ";
switch (code) {
case RTC_ERROR_UNKNOWN:
std::cout << "RTC_ERROR_UNKNOWN";
break;
case RTC_ERROR_INVALID_ARGUMENT:
std::cout << "RTC_ERROR_INVALID_ARGUMENT";
break;
case RTC_ERROR_INVALID_OPERATION:
std::cout << "RTC_ERROR_INVALID_OPERATION";
break;
case RTC_ERROR_OUT_OF_MEMORY:
std::cout << "RTC_ERROR_OUT_OF_MEMORY";
break;
case RTC_ERROR_UNSUPPORTED_CPU:
std::cout << "RTC_ERROR_UNSUPPORTED_CPU";
break;
case RTC_ERROR_CANCELLED:
std::cout << "RTC_ERROR_CANCELLED";
break;
default:
std::cout << "invalid error code";
break;
}
if (str) {
std::cout << " (" << str << ")\n";
}
exit(1);
}
void Renderer::init_device(const char* cfg) {
/* create device */
m_device = rtcNewDevice(nullptr);
handle_error(nullptr, rtcGetDeviceError(m_device),
"fail: Embree Error Unable to create embree device");
/* set error handler */
rtcSetDeviceErrorFunction(m_device, handle_error, nullptr);
}
void Renderer::init_scene(char* cfg, unsigned int width, unsigned int height) {
m_sg = std::make_shared<SceneGraph>(m_device, m_sceneSelector, m_width,
m_height);
}
/* called by the C++ code to render */
void Renderer::render_accumulation() {
const int numTilesX = (m_width + TILE_SIZE_X - 1) / TILE_SIZE_X;
const int numTilesY = (m_height + TILE_SIZE_Y - 1) / TILE_SIZE_Y;
tbb::task_group_context tgContext;
tbb::parallel_for(
tbb::blocked_range<size_t>(0, numTilesX * numTilesY, 1),
[&](const tbb::blocked_range<size_t>& r) {
const int threadIndex = tbb::this_task_arena::current_thread_index();
RandomSampler randomSampler;
for (size_t i = r.begin(); i < r.end(); i++) {
render_tile_task((int)i, threadIndex, numTilesX, numTilesY, randomSampler);
}
},
tgContext);
if (tgContext.is_group_execution_cancelled())
throw std::runtime_error("fail: oneTBB task cancelled");
m_accu_count++;
}
/* task that renders a single screen tile */
void Renderer::render_tile_task(
int taskIndex, int threadIndex, const int numTilesX, const int numTilesY,
RandomSampler& randomSampler) {
const unsigned int tileY = taskIndex / numTilesX;
const unsigned int tileX = taskIndex - tileY * numTilesX;
const unsigned int x0 = tileX * TILE_SIZE_X;
const unsigned int x1 = min(x0 + TILE_SIZE_X, m_width);
const unsigned int y0 = tileY * TILE_SIZE_Y;
const unsigned int y1 = min(y0 + TILE_SIZE_Y, m_height);
for (unsigned int y = y0; y < y1; y++)
for (unsigned int x = x0; x < x1; x++) {
Vec3fa Lsample = render_pixel_samples(x, y, randomSampler);
/* In case you run into issues with visibility try manual debug */
//#define MY_DEBUG
#ifdef MY_DEBUG
if (max(max(color.x, color.y), color.z) > 0.0f)
std::cout << "Hit pixel at :" << x << " , " << y << ": " << color.x
<< " " << color.y << " " << color.z << "\n";
#endif
/* write color to accumulation buffer */
Vec3ff accu_color =
*m_accu[y * m_width + x] + Vec3ff(Lsample.x, Lsample.y, Lsample.z, 1.0f);
*m_accu[y * m_width + x] = accu_color;
float f = rcp(max(0.001f, accu_color.w));
/* write color from accumulation buffer to framebuffer */
unsigned char r =
(unsigned char)(255.0f * clamp(accu_color.x * f, 0.0f, 1.0f));
unsigned char g =
(unsigned char)(255.0f * clamp(accu_color.y * f, 0.0f, 1.0f));
unsigned char b =
(unsigned char)(255.0f * clamp(accu_color.z * f, 0.0f, 1.0f));
m_pixels[y * m_width * m_channels + x * m_channels] = r;
m_pixels[y * m_width * m_channels + x * m_channels + 1] = g;
m_pixels[y * m_width * m_channels + x * m_channels + 2] = b;
}
}
/* task that renders a single screen pixel */
Vec3fa Renderer::render_pixel_samples(
int x, int y, RandomSampler& randomSampler) {
Vec3fa L = Vec3fa(0.0f);
for (int i = 0; i < m_spp; i++) {
/* Generate a new seed for a sample.
Make sure to use a portable random number generator when necessary. This application uses a hand coded LCG generator from the Embree repository.
Be careful when considering a cryptographic random number input for one of the C++11 and later random number generators in the <random> library.
std::hash behavior is implementation defined, therefore using it to seed your random number generator may or may not give a good distribution.
*/
randomSampler.seed(x, y, m_accu_count * m_spp + i);
/* calculate pixel color, slightly offset the ray cast orientation randomly
* so each sample occurs at a slightly different location within a pixel */
/* Note: random offsets for samples within a pixel provide natural
* anti-aliasing (smoothing) near object edges */
float fx = x + randomSampler.get_float();
float fy = y + randomSampler.get_float();
L = L + m_pt->render_path(fx, fy, randomSampler, m_sg, y * m_width + x);
/* If you are not seeing anything, try some printf debug */
//#define MY_DEBUG
#ifdef MY_DEBUG
if (max(max(L.x, L.y), L.z) > 0.0f)
std::cout << "Hit pixel at " << fx << " , " << fy << ": " << L.x << " "
<< L.y << " " << L.z << "\n";
#endif
}
L = L / (float)m_spp;
return L;
}
unsigned char* Renderer::get_pixels() { return m_pixels; }
Renderer::~Renderer() {
if (m_pixels) {
delete m_pixels;
m_pixels = nullptr;
}
}
#endif /* FILE_RENDERER_SEEN */
| h |
oneAPI-samples | data/projects/oneAPI-samples/RenderingToolkit/Tutorial/PathTracingWithEmbree/cpu/src/Pool.h | #pragma once
#ifndef FILE_POOL_SEEN
#define FILE_POOL_SEEN
#include <embree4/rtcore.h>
#include <vector>
#include "Geometry.h"
#include "Lights.h"
#include "Materials.h"
#include "definitions.h"
class Pool : public Geometry {
public:
Pool(const RTCScene& scene, const RTCDevice& device,
std::map<unsigned int, MatAndPrimColorTable>& mapGeomToPrim,
std::map<unsigned int, size_t>& mapGeomToLightIdx,
std::vector<std::shared_ptr<Light>>& lights, AffineSpace3fa& camera,
unsigned int width, unsigned int height);
~Pool();
private:
void add_geometry(
const RTCScene& scene, const RTCDevice& device,
std::map<unsigned int, MatAndPrimColorTable>& mapGeomToPrim);
unsigned int addPool(
const RTCScene& scene, const RTCDevice& device,
std::map<unsigned int, MatAndPrimColorTable>& mapGeomToPrim);
unsigned int addWater(
const RTCScene& scene, const RTCDevice& device,
std::map<unsigned int, MatAndPrimColorTable>& mapGeomToPrim);
void setup_camera_and_lights(
const RTCScene& scene, const RTCDevice& device,
std::map<unsigned int, MatAndPrimColorTable>& mapGeomToPrim,
std::map<unsigned int, size_t>& mapGeomToLightIdx,
std::vector<std::shared_ptr<Light>>& lights, AffineSpace3fa& camera,
unsigned int width, unsigned int height);
void clean_geometry();
void cleanPool();
void cleanWater();
static const std::vector<Vertex> m_poolBoxVertices;
static const std::vector<Quad> m_poolBoxIndices;
static const std::vector<Vec3fa> m_poolBoxColors;
static const std::vector<MaterialType> m_poolBoxMats;
std::vector<MaterialType> m_waterMats;
/* Added for pathtracer */
Vec3fa* m_pool_face_colors = nullptr;
Vec3fa* m_pool_vertex_colors = nullptr;
Vec3fa* m_water_face_colors = nullptr;
Vec3fa* m_water_vertex_colors = nullptr;
};
// mesh data
const std::vector<Vertex> Pool::m_poolBoxVertices = {
// South Wall
{1.00f, -1.00f, -10.00f, 0.0f},
{-1.00f, -1.00f, -10.00f, 0.0f},
{-1.00f, -1.00f, 1.00f, 0.0f},
{1.00f, -1.00f, 1.00f, 0.0f},
// North Wall
{1.00f, 1.00f, -10.00f, 0.0f},
{1.00f, 1.00f, 1.00f, 0.0f},
{-1.00f, 1.00f, 1.00f, 0.0f},
{-1.00f, 1.00f, -10.00f, 0.0f},
// Foundation
{1.00f, -1.00f, 1.00f, 0.0f},
{-1.00f, -1.00f, 1.00f, 0.0f},
{-1.00f, 1.00f, 1.00f, 0.0f},
{1.00f, 1.00f, 1.00f, 0.0f},
// East Wall
{-1.00f, -1.00f, 1.00f, 0.0f},
{-1.00f, -1.00f, -10.00f, 0.0f},
{-1.00f, 1.00f, -10.00f, 0.0f},
{-1.00f, 1.00f, 1.00f, 0.0f},
// West Wall
{1.00f, -1.00f, -10.00f, 0.0f},
{1.00f, -1.00f, 1.00f, 0.0f},
{1.00f, 1.00f, 1.00f, 0.0f},
{1.00f, 1.00f, -10.00f, 0.0f},
// Unseen wall
{1.00f, -1.00f, -10.00f, 0.0f},
{-1.00f, -1.00f, -10.00f, 0.0f},
{-1.00f, 1.00f, -10.00f, 0.0f},
{1.00f, 1.00f, -10.00f, 0.0f}
};
const std::vector<Quad> Pool::m_poolBoxIndices = {
{0, 1, 2, 3}, // Floor
{4, 5, 6, 7}, // Ceiling
{8, 9, 10, 11}, // Backwall
{12, 13, 14, 15}, // RightWall
{16, 17, 18, 19}, // LeftWall
{20, 21, 22, 23} // Unseen wall
};
const std::vector<Vec3fa> Pool::m_poolBoxColors = {
// South Wall
{0.725f, 0.710f, 0.68f},
{0.725f, 0.710f, 0.68f},
{0.725f, 0.710f, 0.68f},
{0.725f, 0.710f, 0.68f},
// North Wall
{0.725f, 0.710f, 0.68f},
{0.725f, 0.710f, 0.68f},
{0.725f, 0.710f, 0.68f},
{0.725f, 0.710f, 0.68f},
// Foundation "Egyptian Blue"
{0.078f, 0.204f, 0.643f},
{0.078f, 0.204f, 0.643f},
{0.078f, 0.204f, 0.643f},
{0.078f, 0.204f, 0.643f},
// East Wall
{0.725f, 0.710f, 0.68f},
{0.725f, 0.710f, 0.68f},
{0.725f, 0.710f, 0.68f},
{0.725f, 0.710f, 0.68f},
// West Wall
{0.725f, 0.710f, 0.68f},
{0.725f, 0.710f, 0.68f},
{0.725f, 0.710f, 0.68f},
{0.725f, 0.710f, 0.68f},
// West Wall
{0.725f, 0.710f, 0.68f},
{0.725f, 0.710f, 0.68f},
{0.725f, 0.710f, 0.68f},
{0.725f, 0.710f, 0.68f},
};
const std::vector<MaterialType> Pool::m_poolBoxMats = {
// Floor
MaterialType::MATERIAL_MATTE,
// Ceiling
MaterialType::MATERIAL_MATTE,
// Backwall
MaterialType::MATERIAL_MATTE,
// RightWall
MaterialType::MATERIAL_MATTE,
// LeftWall
MaterialType::MATERIAL_MATTE,
// Unseen Wall
MaterialType::MATERIAL_MATTE
};
inline float square(const float& in) { return in * in; }
Pool::Pool(const RTCScene& scene, const RTCDevice& device,
std::map<unsigned int, MatAndPrimColorTable>& mapGeomToPrim,
std::map<unsigned int, size_t>& mapGeomToLightIdx,
std::vector<std::shared_ptr<Light>>& lights, AffineSpace3fa& camera,
unsigned int width, unsigned int height) {
add_geometry(scene, device, mapGeomToPrim);
setup_camera_and_lights(scene, device, mapGeomToPrim, mapGeomToLightIdx,
lights, camera, width, height);
}
void Pool::add_geometry(
const RTCScene& scene, const RTCDevice& device,
std::map<unsigned int, MatAndPrimColorTable>& mapGeomToPrim) {
addPool(scene, device, mapGeomToPrim);
addWater(scene, device, mapGeomToPrim);
}
unsigned int Pool::addPool(
const RTCScene& scene, const RTCDevice& device,
std::map<unsigned int, MatAndPrimColorTable>& mapGeomToPrim) {
/* create a mesh for all the quads in the pool Box scene */
RTCGeometry mesh = rtcNewGeometry(device, RTC_GEOMETRY_TYPE_QUAD);
m_pool_face_colors =
(Vec3fa*)alignedMalloc(sizeof(Vec3fa) * m_poolBoxIndices.size(), 16);
m_pool_vertex_colors =
(Vec3fa*)alignedMalloc(sizeof(Vec3fa) * m_poolBoxVertices.size(), 16);
Vertex* vertices = (Vertex*)rtcSetNewGeometryBuffer(
mesh, RTC_BUFFER_TYPE_VERTEX, 0, RTC_FORMAT_FLOAT3, sizeof(Vertex),
m_poolBoxVertices.size());
for (auto i = 0; i < m_poolBoxVertices.size(); i++) {
vertices[i] = m_poolBoxVertices[i];
m_pool_vertex_colors[i] = m_poolBoxColors[i];
}
/* set quads */
Quad* quads = (Quad*)rtcSetNewGeometryBuffer(mesh, RTC_BUFFER_TYPE_INDEX, 0,
RTC_FORMAT_UINT4, sizeof(Quad),
m_poolBoxIndices.size());
for (auto i = 0; i < m_poolBoxIndices.size(); i++) {
quads[i] = m_poolBoxIndices[i];
m_pool_face_colors[i] = m_poolBoxColors[i * 4];
}
rtcSetGeometryVertexAttributeCount(mesh, 1);
rtcSetSharedGeometryBuffer(mesh, RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE, 0,
RTC_FORMAT_FLOAT3, m_pool_vertex_colors, 0,
sizeof(Vec3fa), m_poolBoxVertices.size());
rtcCommitGeometry(mesh);
unsigned int geomID = rtcAttachGeometry(scene, mesh);
rtcReleaseGeometry(mesh);
MatAndPrimColorTable mpTable;
mpTable.materialTable = m_poolBoxMats;
mpTable.primColorTable = m_pool_face_colors;
mapGeomToPrim.insert(std::make_pair(geomID, mpTable));
return geomID;
}
unsigned int Pool::addWater(
const RTCScene& scene, const RTCDevice& device,
std::map<unsigned int, MatAndPrimColorTable>& mapGeomToPrim) {
/* create a triangulated water with 12 triangles and 8 vertices */
RTCGeometry mesh = rtcNewGeometry(device, RTC_GEOMETRY_TYPE_TRIANGLE);
size_t latticeWidth = 200;
int numTriangles = 2 * (latticeWidth - 1) * (latticeWidth - 1);
/* create face and vertex color arrays */
m_water_face_colors =
(Vec3fa*)alignedMalloc(sizeof(Vec3fa) * numTriangles, 16);
m_water_vertex_colors =
(Vec3fa*)alignedMalloc(sizeof(Vec3fa) * latticeWidth * latticeWidth, 16);
/* set vertices and vertex colors */
Vertex* vertices = (Vertex*)rtcSetNewGeometryBuffer(
mesh, RTC_BUFFER_TYPE_VERTEX, 0, RTC_FORMAT_FLOAT3, sizeof(Vertex),
latticeWidth * latticeWidth);
// Bubbles color
Vec3fa waterColor = Vec3fa(0.906f, 0.9961f, 1.0f);
// Top of the water will be z = FarPlaneZ - waterdepth... modulated up or down
// by some sin waves
float waterDepth = 1.4f;
float horizScale = 0.03f;
// float vertScale = 0.03f;
int horizPeriods = 4;
// int vertPeriods = 6;
for (int j = 0; j < latticeWidth; j++) {
for (int i = 0; i < latticeWidth; i++) {
m_water_vertex_colors[j * latticeWidth + i] = waterColor;
// Assigning vertices from bottom right to top left
vertices[j * latticeWidth + i].x =
1.0f - (float(i) / float(latticeWidth - 1)) * 2.0f;
vertices[j * latticeWidth + i].y =
1.0f - (float(j) / float(latticeWidth - 1)) * 2.0f;
vertices[j * latticeWidth + i].z =
1.0f - waterDepth +
horizScale * waterDepth *
sinf((horizPeriods *
(square(float(i) / (latticeWidth - 1)) +
square(float(j) / (latticeWidth - 1))) *
2.f * float(M_PI)));
//+ vertScale * waterDepth * sinf(vertPeriods*(float(j) /
// float(latticeWidth - 1)) * 2.f * float(M_PI));
}
}
/* set triangles and face colors */
int tri = 0;
Triangle* triangles = (Triangle*)rtcSetNewGeometryBuffer(
mesh, RTC_BUFFER_TYPE_INDEX, 0, RTC_FORMAT_UINT3, sizeof(Triangle),
numTriangles);
for (int j = 0; j < latticeWidth - 1; j++) {
for (int i = 0; i < latticeWidth - 1; i++) {
m_water_face_colors[tri] = waterColor;
m_waterMats.push_back(MaterialType::MATERIAL_WATER);
triangles[tri].v0 = j * latticeWidth + i;
triangles[tri].v1 = j * latticeWidth + (i + 1);
triangles[tri].v2 = (j + 1) * latticeWidth + i;
tri++;
m_water_face_colors[tri] = waterColor;
m_waterMats.push_back(MaterialType::MATERIAL_WATER);
triangles[tri].v0 = j * latticeWidth + (i + 1);
triangles[tri].v1 = (j + 1) * latticeWidth + i;
triangles[tri].v2 = (j + 1) * latticeWidth + (i + 1);
tri++;
}
}
rtcSetGeometryVertexAttributeCount(mesh, 1);
rtcSetSharedGeometryBuffer(mesh, RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE, 0,
RTC_FORMAT_FLOAT3, m_water_vertex_colors, 0,
sizeof(Vec3fa), latticeWidth * latticeWidth);
rtcCommitGeometry(mesh);
unsigned int geomID = rtcAttachGeometry(scene, mesh);
rtcReleaseGeometry(mesh);
MatAndPrimColorTable mpTable;
mpTable.materialTable = m_waterMats;
mpTable.primColorTable = m_water_face_colors;
mapGeomToPrim.insert(std::make_pair(geomID, mpTable));
return geomID;
}
void Pool::setup_camera_and_lights(
const RTCScene& scene, const RTCDevice& device,
std::map<unsigned int, MatAndPrimColorTable>& mapGeomToPrim,
std::map<unsigned int, size_t>& mapGeomToLightIdx,
std::vector<std::shared_ptr<Light>>& lights, AffineSpace3fa& camera,
unsigned int width, unsigned int height) {
/* A camera position that connects the field of vision angle of the camera to
* the bounds of the pool box */
float fov = 30.0f;
float fovrad = fov * M_PI / 180.0f;
float half_fovrad = fovrad * 0.5f;
// Careful to not move the camera outside the box! :-D
camera = positionCamera(Vec3fa(0.0, 0.0, -1.0f - 1.f / tanf(half_fovrad)),
Vec3fa(0, 0, 0), Vec3fa(0, 1, 0), fov, width, height);
/* The magnitude of the light can be tricky. Lights such as the point light
* fall off at the inverse square of the distance. When designing a sandbox
* renderer, you may need to scale your light up or down to see your scene. */
Vec3fa spotPow = 20.f * Vec3fa(0.9922, 0.9843, 0.8275);
/* A somewhat central position for the point light within the box. This is
* similar to the position for the interactive pathtracer program shipped with
* Intel Embree */
/* Here we have a light as a disc geometry */
Vec3fa spotPos(0.f, 0.f, -5.0f);
Vec3fa spotDir(0.f, 0.f, 1.f);
float spotCosAngleMax = cosf(80.f * M_PI / 180.f);
float spotCosAngleScale = 50.f;
float spotRadius = 0.4f;
lights.push_back(std::make_shared<SpotLight>(spotPos, spotDir, spotPow,
spotCosAngleMax,
spotCosAngleScale, spotRadius));
/* Add geometry if you want it! */
if (spotRadius > 0.f) {
std::shared_ptr<SpotLight> pSpotLight =
std::dynamic_pointer_cast<SpotLight>(lights.back());
unsigned int geomID =
pSpotLight->add_geometry(scene, device, mapGeomToPrim);
mapGeomToLightIdx.insert(std::make_pair(geomID, lights.size() - 1));
}
}
void Pool::cleanPool() {
if (m_pool_face_colors) alignedFree(m_pool_face_colors);
m_pool_face_colors = nullptr;
if (m_pool_vertex_colors) alignedFree(m_pool_vertex_colors);
m_pool_vertex_colors = nullptr;
}
void Pool::cleanWater() {
if (m_water_face_colors) alignedFree(m_water_face_colors);
m_water_face_colors = nullptr;
if (m_water_vertex_colors) alignedFree(m_water_vertex_colors);
m_water_vertex_colors = nullptr;
}
void Pool::clean_geometry() {
cleanPool();
cleanWater();
}
Pool::~Pool() { clean_geometry(); }
#endif /* !FILE_POOL_SEEN */
| h |
oneAPI-samples | data/projects/oneAPI-samples/RenderingToolkit/Tutorial/PathTracingWithEmbree/cpu/src/Geometry.h | #pragma once
#ifndef FILE_GEOMETRYH_SEEN
#define FILE_GEOMETRYH_SEEN
#include <embree4/rtcore.h>
#include <map>
#include <vector>
#include "Lights.h"
#include "Materials.h"
#include "definitions.h"
/* We'll use this 'geometries' container to automatically clean up the data
* arrays created that are used to create embree geometries */
class Geometry {
public:
Geometry(){};
virtual ~Geometry(){/* Empty */};
// Derived Geometrys will use a custom constructor for Geometries to give
// Geometry specific parameters needed
private:
virtual void add_geometry(
const RTCScene& scene, const RTCDevice& device,
std::map<unsigned int, MatAndPrimColorTable>& mapGeomToPrim) = 0;
virtual void setup_camera_and_lights(
const RTCScene& scene, const RTCDevice& device,
std::map<unsigned int, MatAndPrimColorTable>& mapGeomToPrim,
std::map<unsigned int, size_t>& mapGeomToLightIdx,
std::vector<std::shared_ptr<Light>>& lights, AffineSpace3fa& camera,
unsigned int width, unsigned int height) = 0;
virtual void clean_geometry() = 0;
};
#endif /* FILE_GEOMETRYH_SEEN */ | h |
oneAPI-samples | data/projects/oneAPI-samples/RenderingToolkit/Tutorial/PathTracingWithEmbree/cpu/src/definitions.h | #pragma once
#ifndef FILE_DEFINITIONS_SEEN
#define FILE_DEFINITIONS_SEEN
/* The definitions.h file is home to 3D math utility types and 3D math utilty
* functions. Embree repository tutorial sources often place these into an
* optics.h or sampling.g */
#include <embree4/rtcore.h>
#include <rkcommon/math/vec.h>
#include <utility>
/* Added for pathtracer */
#include <rkcommon/math/AffineSpace.h>
#include <rkcommon/math/LinearSpace.h>
using Vec3fa = rkcommon::math::vec_t<float, 3, 1>;
using rkcommon::math::AffineSpace3fa;
using rkcommon::math::LinearSpace3fa;
#ifdef _WIN32
#define alignedMalloc(a, b) _aligned_malloc(a, b)
#define alignedFree(a) _aligned_free(a)
#else
#include <mm_malloc.h>
#define alignedMalloc(a, b) _mm_malloc(a, b)
#define alignedFree(a) _mm_free(a)
#endif
/* Here we define the tile size in use for oneTBB tasks */
#define TILE_SIZE_X 8
#define TILE_SIZE_Y 8
using Vec3fa = rkcommon::math::vec_t<float, 3, 1>;
using rkcommon::math::cross;
using rkcommon::math::deg2rad;
using rkcommon::math::normalize;
using std::max;
using std::min;
/* Additions for pathtracer */
using Vec3ff = rkcommon::math::vec4f;
using rkcommon::math::rcp;
using Vec2f = rkcommon::math::vec2f;
using rkcommon::math::clamp;
using rkcommon::math::dot;
using rkcommon::math::rsqrt;
struct PosInf {
__forceinline operator float() const {
return std::numeric_limits<float>::infinity();
}
};
PosInf inf;
/* originally from tutorial_device.h */
/* vertex, quad, and triangle layout */
struct Vertex {
float x, y, z, r;
};
struct Quad {
int v0, v1, v2, v3;
};
struct Triangle {
int v0, v1, v2;
};
/* Added for pathtracer */
struct Normal {
float x, y, z;
};
/* Added for pathtracer */
struct DifferentialGeometry {
unsigned int instIDs[RTC_MAX_INSTANCE_LEVEL_COUNT];
unsigned int geomID;
unsigned int primID;
float u, v;
Vec3fa P;
Vec3fa Ng;
/* This sample program does not interpolate normals for normal specific
* shading. Ns is set to Ng, the regular normal. */
Vec3fa Ns;
/* This sample does not use textures. Tx is a place holder. */
Vec3fa Tx;
/* This sample does not use textures. Ty is a place holder. */
Vec3fa Ty;
float eps;
};
/* Added for pathtracer */
struct Sample3f {
Vec3fa v;
float pdf;
};
inline float cosineSampleHemispherePDF(const Vec3fa& dir) {
return dir.z / float(M_PI);
}
/* Added for pathtracer. The frame function creates a transform from a normal.
*/
LinearSpace3fa frame(const Vec3fa& N) {
const Vec3fa dx0(0, N.z, -N.y);
const Vec3fa dx1(-N.z, 0, N.x);
const Vec3fa dx = normalize((dot(dx0, dx0) > dot(dx1, dx1)) ? dx0 : dx1);
const Vec3fa dy = normalize(cross(N, dx));
return LinearSpace3fa(dx, dy, N);
}
inline Vec3fa cartesian(const float phi, const float sinTheta,
const float cosTheta) {
const float sinPhi = sinf(phi);
const float cosPhi = cosf(phi);
// sincosf(phi, &sinPhi, &cosPhi);
return Vec3fa(cosPhi * sinTheta, sinPhi * sinTheta, cosTheta);
}
inline Vec3fa cartesian(const float phi, const float cosTheta) {
return cartesian(phi, sqrt(max(0.f, 1.f - (cosTheta * cosTheta))), cosTheta);
}
/// cosine-weighted sampling of hemisphere oriented along the +z-axis
////////////////////////////////////////////////////////////////////////////////
inline Vec3fa cosineSampleHemisphere(const Vec2f s) {
const float phi = float(2.f * M_PI) * s.x;
const float cosTheta = sqrt(s.y);
const float sinTheta = sqrt(1.0f - s.y);
return cartesian(phi, sinTheta, cosTheta);
}
/*! Cosine weighted hemisphere sampling. Up direction is provided as argument.
*/
inline Vec3fa cosineSampleHemisphere(const float u, const float v,
const Vec3fa& N) {
/* Determine cartesian coordinate for new Vec3fa */
const float phi = float(2.0f * M_PI) * u;
const float cosTheta = sqrt(v);
const float sinTheta = sqrt(1.0f - v);
const float sinPhi = sinf(phi);
const float cosPhi = cosf(phi);
Vec3fa localDir = Vec3fa(cosPhi * sinTheta, sinPhi * sinTheta, cosTheta);
/* Gives the new Vec3fa transformed about the input Vec3fa */
return frame(N) * localDir;
}
inline Vec3fa cosinePDFHemisphere(const float s) {
return sqrt(s) / float(M_PI);
}
/// sampling of cone of directions oriented along the +z-axis
////////////////////////////////////////////////////////////////////////////////
inline Vec3fa uniformSampleCone(const float cosAngle, const Vec2f& s) {
const float phi = float(2.f * M_PI) * s.x;
const float cosTheta = 1.0f - s.y * (1.0f - cosAngle);
return cartesian(phi, cosTheta);
}
inline float uniformSampleConePDF(const float cosAngle) {
return rcp(float(2.f * M_PI) * (1.0f - cosAngle));
}
/// sampling of disk
////////////////////////////////////////////////////////////////////////////////
inline Vec3fa uniformSampleDisk(const float radius, const Vec2f& s) {
const float r = sqrtf(s.x) * radius;
const float phi = float(2.0f * float(M_PI)) * s.y;
const float sinPhi = sinf(phi);
const float cosPhi = cosf(phi);
// sincosf(phi, &sinPhi, &cosPhi);
return Vec3fa(r * cosPhi, r * sinPhi, 0.f);
}
inline float uniformSampleDiskPDF(const float radius) {
return rcp(float(M_PI) * (radius * radius));
}
/* Added for pathtracer */
inline Vec3fa face_forward(const Vec3fa& dir, const Vec3fa& _Ng) {
const Vec3fa Ng = _Ng;
return dot(dir, Ng) < 0.0f ? Ng : -Ng;
}
/* Added for pathtracer: Initializes a rayhit data structure. Used for embree to
* perform ray to primitive intersect */
inline void init_RayHit(RTCRayHit& rayhit, const Vec3fa& org, const Vec3fa& dir,
float tnear, float tfar, float time) {
rayhit.ray.dir_x = dir.x;
rayhit.ray.dir_y = dir.y;
rayhit.ray.dir_z = dir.z;
rayhit.ray.org_x = org.x;
rayhit.ray.org_y = org.y;
rayhit.ray.org_z = org.z;
rayhit.ray.tnear = tnear;
rayhit.ray.time = time;
rayhit.ray.tfar = tfar;
rayhit.hit.geomID = RTC_INVALID_GEOMETRY_ID;
rayhit.hit.primID = RTC_INVALID_GEOMETRY_ID;
rayhit.ray.mask = -1;
}
AffineSpace3fa positionCamera(Vec3fa from, Vec3fa to, Vec3fa up, float fov,
size_t width, size_t height) {
/* There are many ways to set up a camera projection. This one is consolidated
* from the camera code in the Embree/tutorial/common/tutorial/camera.h object
*/
AffineSpace3fa camMatrix;
Vec3fa Z = normalize(Vec3fa(to - from));
Vec3fa U = normalize(cross(up, Z));
Vec3fa V = normalize(cross(Z, U));
camMatrix.l.vx = U;
camMatrix.l.vy = V;
camMatrix.l.vz = Z;
camMatrix.p = from;
/* negate for a right handed camera*/
camMatrix.l.vx = -camMatrix.l.vx;
const float fovScale = 1.0f / tanf(deg2rad(0.5f * fov));
camMatrix.l.vz = -0.5f * width * camMatrix.l.vx +
0.5f * height * camMatrix.l.vy +
0.5f * height * fovScale * camMatrix.l.vz;
camMatrix.l.vy = -camMatrix.l.vy;
return camMatrix;
}
#endif /* !FILE_DEFINITIONS_SEEN */ | h |
oneAPI-samples | data/projects/oneAPI-samples/RenderingToolkit/Tutorial/PathTracingWithEmbree/cpu/src/DefaultCubeAndPlane.h | #pragma once
#ifndef FILE_DEFAULTCUBEANDPLANE_SEEN
#define FILE_DEFAULTCUBEANDPLANE_SEEN
#include <embree4/rtcore.h>
#include <vector>
#include "Lights.h"
#include "Materials.h"
#include "definitions.h"
class CubeAndPlane : public Geometry {
public:
CubeAndPlane(const RTCScene& scene, const RTCDevice& device,
std::map<unsigned int, MatAndPrimColorTable>& mapGeomToPrim,
std::map<unsigned int, size_t>& mapGeomToLightIdx,
std::vector<std::shared_ptr<Light>>& lights,
AffineSpace3fa& camera, unsigned int width, unsigned int height);
~CubeAndPlane();
private:
void add_geometry(
const RTCScene& scene, const RTCDevice& device,
std::map<unsigned int, MatAndPrimColorTable>& mapGeomToPrim);
void setup_camera_and_lights(
const RTCScene& scene, const RTCDevice& device,
std::map<unsigned int, MatAndPrimColorTable>& mapGeomToPrim,
std::map<unsigned int, size_t>& mapGeomToLightIdx,
std::vector<std::shared_ptr<Light>>& lights, AffineSpace3fa& camera,
unsigned int width, unsigned int height);
unsigned int addCube(
const RTCScene& scene, const RTCDevice& device,
std::map<unsigned int, MatAndPrimColorTable>& mapGeomToPrim);
unsigned int addGroundPlane(
const RTCScene& scene, const RTCDevice& device,
std::map<unsigned int, MatAndPrimColorTable>& mapGeomToPrim);
void clean_geometry();
Vec3fa* m_cube_face_colors = nullptr;
Vec3fa* m_cube_vertex_colors = nullptr;
Vec3fa* m_ground_face_colors = nullptr;
Vec3fa* m_ground_vertex_colors = nullptr;
static const std::vector<MaterialType> m_cubeMats;
static const std::vector<MaterialType> m_groundMats;
};
CubeAndPlane::CubeAndPlane(
const RTCScene& scene, const RTCDevice& device,
std::map<unsigned int, MatAndPrimColorTable>& mapGeomToPrim,
std::map<unsigned int, size_t>& mapGeomToLightIdx,
std::vector<std::shared_ptr<Light>>& lights, AffineSpace3fa& camera,
unsigned int width, unsigned int height) {
add_geometry(scene, device, mapGeomToPrim);
setup_camera_and_lights(scene, device, mapGeomToPrim, mapGeomToLightIdx,
lights, camera, width, height);
};
void CubeAndPlane::add_geometry(
const RTCScene& scene, const RTCDevice& device,
std::map<unsigned int, MatAndPrimColorTable>& mapGeomToPrim) {
addCube(scene, device, mapGeomToPrim);
addGroundPlane(scene, device, mapGeomToPrim);
}
const std::vector<MaterialType> CubeAndPlane::m_cubeMats = {
/* Two tris per face and six faces */
MaterialType::MATERIAL_MATTE,
MaterialType::MATERIAL_MATTE,
MaterialType::MATERIAL_MATTE,
MaterialType::MATERIAL_MATTE,
MaterialType::MATERIAL_MATTE,
MaterialType::MATERIAL_MATTE,
MaterialType::MATERIAL_MATTE,
MaterialType::MATERIAL_MATTE,
MaterialType::MATERIAL_MATTE,
MaterialType::MATERIAL_MATTE,
MaterialType::MATERIAL_MATTE,
MaterialType::MATERIAL_MATTE
};
const std::vector<MaterialType> CubeAndPlane::m_groundMats = {
MaterialType::MATERIAL_MATTE, MaterialType::MATERIAL_MATTE};
/* adds a cube to the scene */
unsigned int CubeAndPlane::addCube(
const RTCScene& scene, const RTCDevice& device,
std::map<unsigned int, MatAndPrimColorTable>& mapGeomToPrim) {
/* create a triangulated cube with 12 triangles and 8 vertices */
RTCGeometry mesh = rtcNewGeometry(device, RTC_GEOMETRY_TYPE_TRIANGLE);
/* create face and vertex color arrays */
m_cube_face_colors = (Vec3fa*)alignedMalloc(sizeof(Vec3fa) * 12, 16);
m_cube_vertex_colors = (Vec3fa*)alignedMalloc(sizeof(Vec3fa) * 8, 16);
/* set vertices and vertex colors */
Vertex* vertices = (Vertex*)rtcSetNewGeometryBuffer(
mesh, RTC_BUFFER_TYPE_VERTEX, 0, RTC_FORMAT_FLOAT3, sizeof(Vertex), 8);
m_cube_vertex_colors[0] = Vec3fa(0, 0, 0);
vertices[0].x = -1;
vertices[0].y = -1;
vertices[0].z = -1;
m_cube_vertex_colors[1] = Vec3fa(0, 0, 1);
vertices[1].x = -1;
vertices[1].y = -1;
vertices[1].z = +1;
m_cube_vertex_colors[2] = Vec3fa(0, 1, 0);
vertices[2].x = -1;
vertices[2].y = +1;
vertices[2].z = -1;
m_cube_vertex_colors[3] = Vec3fa(0, 1, 1);
vertices[3].x = -1;
vertices[3].y = +1;
vertices[3].z = +1;
m_cube_vertex_colors[4] = Vec3fa(1, 0, 0);
vertices[4].x = +1;
vertices[4].y = -1;
vertices[4].z = -1;
m_cube_vertex_colors[5] = Vec3fa(1, 0, 1);
vertices[5].x = +1;
vertices[5].y = -1;
vertices[5].z = +1;
m_cube_vertex_colors[6] = Vec3fa(1, 1, 0);
vertices[6].x = +1;
vertices[6].y = +1;
vertices[6].z = -1;
m_cube_vertex_colors[7] = Vec3fa(1, 1, 1);
vertices[7].x = +1;
vertices[7].y = +1;
vertices[7].z = +1;
/* set triangles and face colors */
int tri = 0;
Triangle* triangles = (Triangle*)rtcSetNewGeometryBuffer(
mesh, RTC_BUFFER_TYPE_INDEX, 0, RTC_FORMAT_UINT3, sizeof(Triangle), 12);
// left side
m_cube_face_colors[tri] = Vec3fa(1, 0, 0);
triangles[tri].v0 = 0;
triangles[tri].v1 = 1;
triangles[tri].v2 = 2;
tri++;
m_cube_face_colors[tri] = Vec3fa(1, 0, 0);
triangles[tri].v0 = 1;
triangles[tri].v1 = 3;
triangles[tri].v2 = 2;
tri++;
// right side
m_cube_face_colors[tri] = Vec3fa(0, 1, 0);
triangles[tri].v0 = 4;
triangles[tri].v1 = 6;
triangles[tri].v2 = 5;
tri++;
m_cube_face_colors[tri] = Vec3fa(0, 1, 0);
triangles[tri].v0 = 5;
triangles[tri].v1 = 6;
triangles[tri].v2 = 7;
tri++;
// bottom side
m_cube_face_colors[tri] = Vec3fa(1.0f);
triangles[tri].v0 = 0;
triangles[tri].v1 = 4;
triangles[tri].v2 = 1;
tri++;
m_cube_face_colors[tri] = Vec3fa(1.0f);
triangles[tri].v0 = 1;
triangles[tri].v1 = 4;
triangles[tri].v2 = 5;
tri++;
// top side
m_cube_face_colors[tri] = Vec3fa(1.0f);
triangles[tri].v0 = 2;
triangles[tri].v1 = 3;
triangles[tri].v2 = 6;
tri++;
m_cube_face_colors[tri] = Vec3fa(1.0f);
triangles[tri].v0 = 3;
triangles[tri].v1 = 7;
triangles[tri].v2 = 6;
tri++;
// front side
m_cube_face_colors[tri] = Vec3fa(0, 0, 1);
triangles[tri].v0 = 0;
triangles[tri].v1 = 2;
triangles[tri].v2 = 4;
tri++;
m_cube_face_colors[tri] = Vec3fa(0, 0, 1);
triangles[tri].v0 = 2;
triangles[tri].v1 = 6;
triangles[tri].v2 = 4;
tri++;
// back side
m_cube_face_colors[tri] = Vec3fa(1, 1, 0);
triangles[tri].v0 = 1;
triangles[tri].v1 = 5;
triangles[tri].v2 = 3;
tri++;
m_cube_face_colors[tri] = Vec3fa(1, 1, 0);
triangles[tri].v0 = 3;
triangles[tri].v1 = 5;
triangles[tri].v2 = 7;
tri++;
rtcSetGeometryVertexAttributeCount(mesh, 1);
rtcSetSharedGeometryBuffer(mesh, RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE, 0,
RTC_FORMAT_FLOAT3, m_cube_vertex_colors, 0,
sizeof(Vec3fa), 8);
rtcCommitGeometry(mesh);
unsigned int geomID = rtcAttachGeometry(scene, mesh);
rtcReleaseGeometry(mesh);
MatAndPrimColorTable mpTable;
mpTable.materialTable = m_cubeMats;
mpTable.primColorTable = m_cube_face_colors;
mapGeomToPrim.insert(std::make_pair(geomID, mpTable));
return geomID;
}
/* adds a ground plane to the scene */
unsigned int CubeAndPlane::addGroundPlane(
const RTCScene& scene, const RTCDevice& device,
std::map<unsigned int, MatAndPrimColorTable>& mapGeomToPrim) {
/* create a triangulated plane with 2 triangles and 4 vertices */
RTCGeometry mesh = rtcNewGeometry(device, RTC_GEOMETRY_TYPE_TRIANGLE);
/* create face and vertex color arrays */
m_ground_face_colors = (Vec3fa*)alignedMalloc(sizeof(Vec3fa) * 2, 16);
m_ground_vertex_colors = (Vec3fa*)alignedMalloc(sizeof(Vec3fa) * 4, 16);
/* Moving the plane up to the bottom of the cube shows more global
illumination color bleed Try y = -1 to see it!
*/
/* The color of the ground plane is changed to white to see global
* illumination effects */
/* set vertices */
Vertex* vertices = (Vertex*)rtcSetNewGeometryBuffer(
mesh, RTC_BUFFER_TYPE_VERTEX, 0, RTC_FORMAT_FLOAT3, sizeof(Vertex), 4);
m_ground_vertex_colors[0] = Vec3fa(1, 0, 0);
vertices[0].x = -10;
vertices[0].y = -2;
vertices[0].z = -10;
m_ground_vertex_colors[1] = Vec3fa(1, 0, 1);
vertices[1].x = -10;
vertices[1].y = -2;
vertices[1].z = +10;
m_ground_vertex_colors[2] = Vec3fa(1, 1, 0);
vertices[2].x = +10;
vertices[2].y = -2;
vertices[2].z = -10;
m_ground_vertex_colors[3] = Vec3fa(1, 1, 1);
vertices[3].x = +10;
vertices[3].y = -2;
vertices[3].z = +10;
/* set triangles */
Triangle* triangles = (Triangle*)rtcSetNewGeometryBuffer(
mesh, RTC_BUFFER_TYPE_INDEX, 0, RTC_FORMAT_UINT3, sizeof(Triangle), 2);
m_ground_face_colors[0] = Vec3fa(1, 0, 0);
triangles[0].v0 = 0;
triangles[0].v1 = 1;
triangles[0].v2 = 2;
m_ground_face_colors[1] = Vec3fa(1, 0, 0);
triangles[1].v0 = 1;
triangles[1].v1 = 3;
triangles[1].v2 = 2;
rtcSetGeometryVertexAttributeCount(mesh, 1);
rtcSetSharedGeometryBuffer(mesh, RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE, 0,
RTC_FORMAT_FLOAT3, m_ground_vertex_colors, 0,
sizeof(Vec3fa), 4);
rtcCommitGeometry(mesh);
unsigned int geomID = rtcAttachGeometry(scene, mesh);
rtcReleaseGeometry(mesh);
MatAndPrimColorTable mpTable;
mpTable.materialTable = m_groundMats;
mpTable.primColorTable = m_ground_face_colors;
mapGeomToPrim.insert(std::make_pair(geomID, mpTable));
return geomID;
}
void CubeAndPlane::clean_geometry() {
if (m_cube_face_colors) alignedFree(m_cube_face_colors);
m_cube_face_colors = nullptr;
if (m_cube_vertex_colors) alignedFree(m_cube_vertex_colors);
m_cube_vertex_colors = nullptr;
if (m_ground_face_colors) alignedFree(m_ground_face_colors);
m_ground_face_colors = nullptr;
if (m_ground_vertex_colors) alignedFree(m_ground_vertex_colors);
m_ground_vertex_colors = nullptr;
}
CubeAndPlane::~CubeAndPlane() { clean_geometry(); }
void CubeAndPlane::setup_camera_and_lights(
const RTCScene& scene, const RTCDevice& device,
std::map<unsigned int, MatAndPrimColorTable>& mapGeomToPrim,
std::map<unsigned int, size_t>& mapGeomToLightIdx,
std::vector<std::shared_ptr<Light>>& lights, AffineSpace3fa& camera,
unsigned int width, unsigned int height) {
Vec3fa defaultLightIntensity = {1.0f, 1.0f, 1.0f};
camera = positionCamera(Vec3fa(1.5f, 1.5f, -1.5f), Vec3fa(0, 0, 0),
Vec3fa(0, 1, 0), 90.0f, width, height);
/* Zoomed out camera position below: */
// camera = positionCamera(Vec3fa(3.f, 3.f, -3.f), Vec3fa(0, 0, 0),
// Vec3fa(0, 1, 0), 90.0f, width, height);
/* We've implemented a directional light... Try it with this direction :
Vec3fa defaultLightDirection = normalize(Vec3fa(-1.0f, -1.0f, -1.0f));
This will give results even more similar to the original triangle geometry
sample. You may want to change the power as well.
*/
/* Picking the magnitude of the light can be tricky. Lights such as the point
* light fall off at the inverse square of the distance. When building a
* renderer, you may need to scale your light up or down to see your scene. */
Vec3fa pow = 800.f * Vec3fa(1.f, 1.f, 1.f);
/* The point light that mimicks the direction of the directional light */
Vec3fa pos = Vec3fa(10.0f, 10.0f, 10.0f);
float radius = 0.f;
lights.push_back(std::make_shared<PointLight>(pos, pow, radius));
/* If radius is greater than 0 lets try to build a geometry */
if (radius > 0.f) {
std::shared_ptr<PointLight> pPointLight =
std::dynamic_pointer_cast<PointLight>(lights.back());
unsigned int geomID =
pPointLight->add_geometry(scene, device, mapGeomToPrim);
mapGeomToLightIdx.insert(std::make_pair(geomID, lights.size() - 1));
}
}
#endif /* !FILE_DEFAULTCUBEANDPLANE_SEEN */
| h |
oneAPI-samples | data/projects/oneAPI-samples/RenderingToolkit/Tutorial/PathTracingWithEmbree/cpu/src/Lights.h | #pragma once
#ifndef FILE_LIGHTSSEEN
#define FILE_LIGHTSSEEN
/* Added for pathtracer */
#include "Materials.h"
#include "definitions.h"
// for very small cones treat as singular light, because float precision is not
// good enough
#define COS_ANGLE_MAX 0.99999988f
enum class LightType { INFINITE_DIRECTIONAL_LIGHT, POINT_LIGHT, SPOT_LIGHT };
/* Added for pathtracer */
struct Light_SampleRes {
Vec3fa weight; //!< radiance that arrives at the given point divided by pdf
Vec3fa dir; //!< direction towards the light source
float dist; //!< largest valid t_far value for a shadow ray
float pdf; //!< probability density that this sample was taken
};
struct Light_EvalRes {
Vec3fa value; //!< radiance that arrives at the given point (not weighted by
//!< pdf)
float dist;
float
pdf; //!< probability density that the direction would have been sampled
};
class Light {
public:
Light(){};
virtual Light_SampleRes sample(const DifferentialGeometry& dg,
const Vec2f& randomLightSample) = 0;
virtual Light_EvalRes eval(const Vec3fa& org, const Vec3fa&);
LightType m_type;
};
Light_EvalRes Light::eval(const Vec3fa& dg, const Vec3fa& dir) {
Light_EvalRes res;
res.value = Vec3fa(0.f);
res.dist = inf;
res.pdf = 0.f;
return res;
}
class PointLight : public Light {
public:
PointLight(Vec3fa pos, Vec3fa pow, float r)
: m_position(pos), m_power(pow), m_radius(r) {
m_type = LightType::POINT_LIGHT;
};
~PointLight(){};
Light_SampleRes sample(const DifferentialGeometry& dg, const Vec2f& s);
Light_EvalRes eval(const Vec3fa& org, const Vec3fa& dir);
unsigned int add_geometry(
RTCScene scene, RTCDevice device,
std::map<unsigned int, MatAndPrimColorTable>& mapGeomToPrim);
inline void clean_geometry();
Vec3fa m_position;
Vec3fa m_power;
float m_radius;
};
Light_SampleRes PointLight::sample(const DifferentialGeometry& dg,
const Vec2f& s) {
Light_SampleRes res;
// extant light vector from the hit point
const Vec3fa dir = m_position - dg.P;
const float dist2 = dot(dir, dir);
const float invdist = rsqrt(dist2);
// normalized light vector
res.dir = dir * invdist;
res.dist = dist2 * invdist;
res.pdf = inf; // per default we always take this res
// convert from power to radiance by attenuating by distance^2
res.weight = m_power * (invdist * invdist);
const float sinTheta = m_radius * invdist;
if ((m_radius > 0.f) && (sinTheta > 0.005f)) {
// res surface of sphere as seen by hit point -> cone of directions
// for very small cones treat as point light, because float precision is not
// good enough
if (sinTheta < 1.f) {
const float cosTheta = sqrt(1.f - sinTheta * sinTheta);
const Vec3fa localDir = uniformSampleCone(cosTheta, s);
res.dir = frame(res.dir) * localDir;
res.pdf = uniformSampleConePDF(cosTheta);
const float c = localDir.z;
res.dist =
c * res.dist - sqrt((m_radius * m_radius) - (1.f - c * c) * dist2);
// TODO scale radiance by actual distance
} else { // inside sphere
const Vec3fa localDir = cosineSampleHemisphere(s);
res.dir = frame(dg.Ns) * localDir;
res.pdf = cosineSampleHemispherePDF(localDir);
// TODO:
res.weight = m_power * rcp(m_radius * m_radius);
res.dist = m_radius;
}
}
return res;
}
Light_EvalRes PointLight::eval(const Vec3fa& org, const Vec3fa& dir) {
Light_EvalRes res;
res.value = Vec3fa(0.f);
res.dist = inf;
res.pdf = 0.f;
if (m_radius > 0.f) {
const Vec3fa A = m_position - org;
const float a = dot(dir, dir);
const float b = 2.f * dot(dir, A);
const float centerDist2 = dot(A, A);
const float c = centerDist2 - (m_radius * m_radius);
const float radical = (b * b) - 4.f * a * c;
if (radical > 0.f) {
const float t_near = (b - sqrt(radical)) / (2.f * a);
const float t_far = (b + sqrt(radical)) / (2.f * a);
if (t_far > 0.0f) {
// TODO: handle interior case
res.dist = t_near;
const float sinTheta2 = (m_radius * m_radius) * rcp(centerDist2);
const float cosTheta = sqrt(1.f - sinTheta2);
res.pdf = uniformSampleConePDF(cosTheta);
const float invdist = rcp(t_near);
res.value = m_power * res.pdf * (invdist * invdist);
}
}
}
return res;
}
unsigned int PointLight::add_geometry(
RTCScene scene, RTCDevice device,
std::map<unsigned int, MatAndPrimColorTable>& mapGeomToPrim) {
RTCGeometry mesh = rtcNewGeometry(device, RTC_GEOMETRY_TYPE_SPHERE_POINT);
Vertex* vertices = (Vertex*)rtcSetNewGeometryBuffer(
mesh, RTC_BUFFER_TYPE_VERTEX, 0, RTC_FORMAT_FLOAT4, sizeof(Vertex), 1);
// Sphere primitive defined as singular Vec4 point for embree
Vertex p = {m_position.x, m_position.y, m_position.z, m_radius};
vertices[0] = p;
rtcCommitGeometry(mesh);
unsigned int geomID = rtcAttachGeometry(scene, mesh);
rtcReleaseGeometry(mesh);
MatAndPrimColorTable mpTable;
mpTable.materialTable = {MaterialType::MATERIAL_EMITTER};
// We don't want to store 'albedo' colors for the point light, we will use
// sample/eval functions and members of the light object
mpTable.primColorTable = nullptr;
mapGeomToPrim.insert(std::make_pair(geomID, mpTable));
return geomID;
}
/* Only a place holder. Nothing is here because we do not attach any attributes
* for the sphere. */
inline void PointLight::clean_geometry() {}
class DirectionalLight : public Light {
public:
DirectionalLight(const Vec3fa& _direction, const Vec3fa& _radiance,
float _cosAngle);
~DirectionalLight(){};
Light_SampleRes sample(const DifferentialGeometry& dg, const Vec2f& s);
Light_EvalRes eval(const Vec3fa& org, const Vec3fa& dir);
LinearSpace3fa m_coordFrame; //!< coordinate frame, with vz == direction
//!< *towards* the light source
Vec3fa m_radiance; //!< RGB color and intensity of light
float m_cosAngle; //!< Angular limit of the cone light in an easier to use
//!< form: cosine of the half angle in radians
float m_pdf; //!< Probability to sample a direction to the light
};
//! Set the parameters of an ispc-side DirectionalLight object
DirectionalLight::DirectionalLight(const Vec3fa& _direction,
const Vec3fa& _radiance, float _cosAngle) {
m_coordFrame = frame(_direction);
m_radiance = _radiance;
m_cosAngle = _cosAngle;
m_pdf = _cosAngle < COS_ANGLE_MAX ? uniformSampleConePDF(_cosAngle) : inf;
m_type = LightType::INFINITE_DIRECTIONAL_LIGHT;
}
Light_SampleRes DirectionalLight::sample(const DifferentialGeometry& dg,
const Vec2f& s) {
Light_SampleRes res;
res.dir = m_coordFrame.vz;
res.dist = inf;
res.pdf = m_pdf;
if (m_cosAngle < COS_ANGLE_MAX)
res.dir = m_coordFrame * uniformSampleCone(m_cosAngle, s);
res.weight = m_radiance; // *pdf/pdf cancel
return res;
}
Light_EvalRes DirectionalLight::eval(const Vec3fa&, const Vec3fa& dir) {
Light_EvalRes res;
res.dist = inf;
if (m_cosAngle < COS_ANGLE_MAX && dot(m_coordFrame.vz, dir) > m_cosAngle) {
res.value = m_radiance * m_pdf;
res.pdf = m_pdf;
} else {
res.value = Vec3fa(0.f);
res.pdf = 0.f;
}
return res;
}
class SpotLight : public Light {
public:
SpotLight(const Vec3fa& _position, const Vec3fa& _direction,
const Vec3fa& _power, float _cosAngleMax, float _cosAngleScale,
float _radius)
: m_position(_position),
m_direction(_direction),
m_power(_power),
m_cosAngleMax(_cosAngleMax),
m_cosAngleScale(_cosAngleScale),
m_radius(_radius)
{
m_coordFrame = frame(_direction);
m_diskPdf = uniformSampleDiskPDF(_radius);
};
~SpotLight(){};
Light_SampleRes sample(const DifferentialGeometry& dg, const Vec2f& s);
Light_EvalRes eval(const Vec3fa& org, const Vec3fa& dir);
unsigned int add_geometry(
RTCScene scene, RTCDevice device,
std::map<unsigned int, MatAndPrimColorTable>& mapGeomToPrim);
Vec3fa m_position; //!< Position of the SpotLight
LinearSpace3fa m_coordFrame; //!< coordinate frame, with vz == direction that
//!< the SpotLight is emitting
Vec3fa m_power; //!< RGB color and intensity of the SpotLight
float m_cosAngleMax; //!< Angular limit of the spot in an easier to use form:
//!< cosine of the half angle in radians
float m_cosAngleScale; //!< 1/(cos(border of the penumbra area) -
//!< cosAngleMax); positive
float m_radius; //!< defines the size of the (extended) SpotLight
float m_diskPdf; //!< pdf of disk with radius
Vec3fa m_direction; // store the disk direction
};
Light_SampleRes SpotLight::sample(const DifferentialGeometry& dg,
const Vec2f& s) {
Light_SampleRes res;
// extant light vector from the hit point
res.dir = m_position - dg.P;
if (m_radius > 0.f)
res.dir = m_coordFrame * uniformSampleDisk(m_radius, s) + res.dir;
const float dist2 = dot(res.dir, res.dir);
const float invdist = rsqrt(dist2);
// normalized light vector
res.dir = res.dir * invdist;
res.dist = dist2 * invdist;
// cosine of the negated light direction and light vector.
const float cosAngle = -dot(m_coordFrame.vz, res.dir);
const float angularAttenuation =
clamp((cosAngle - m_cosAngleMax) * m_cosAngleScale);
if (m_radius > 0.f)
res.pdf = m_diskPdf * dist2 * abs(cosAngle);
else
res.pdf = inf; // we always take this res
// convert from power to radiance by attenuating by distance^2; attenuate by
// angle
res.weight = m_power * ((invdist * invdist) * angularAttenuation);
return res;
}
Light_EvalRes SpotLight::eval(const Vec3fa& org, const Vec3fa& dir) {
Light_EvalRes res;
res.value = Vec3fa(0.f);
res.dist = inf;
res.pdf = 0.f;
if (m_radius > 0.f) {
// intersect disk
const float cosAngle = -dot(dir, m_coordFrame.vz);
if (cosAngle > m_cosAngleMax) { // inside illuminated cone?
const Vec3fa vp = org - m_position;
const float dp = dot(vp, m_coordFrame.vz);
if (dp > 0.f) { // in front of light?
const float t = dp * rcp(cosAngle);
const Vec3fa vd = vp + t * dir;
if (dot(vd, vd) < (m_radius * m_radius)) { // inside disk?
const float angularAttenuation =
min((cosAngle - m_cosAngleMax) * m_cosAngleScale, 1.f);
const float pdf = m_diskPdf * cosAngle;
res.value =
m_power * (angularAttenuation * pdf); // *sqr(t)/sqr(t) cancels
res.dist = t;
res.pdf = pdf * (t * t);
}
}
}
}
return res;
}
unsigned int SpotLight::add_geometry(
RTCScene scene, RTCDevice device,
std::map<unsigned int, MatAndPrimColorTable>& mapGeomToPrim) {
RTCGeometry mesh =
rtcNewGeometry(device, RTC_GEOMETRY_TYPE_ORIENTED_DISC_POINT);
Vertex* vertices = (Vertex*)rtcSetNewGeometryBuffer(
mesh, RTC_BUFFER_TYPE_VERTEX, 0, RTC_FORMAT_FLOAT4, sizeof(Vertex), 1);
// Sphere primitive defined as singular Vec4 point for embree
Vertex p = {m_position.x, m_position.y, m_position.z, m_radius};
vertices[0] = p;
Normal* normals = (Normal*)rtcSetNewGeometryBuffer(
mesh, RTC_BUFFER_TYPE_NORMAL, 0, RTC_FORMAT_FLOAT3, sizeof(Normal), 1);
Normal n = {m_direction.x, m_direction.y, m_direction.z};
normals[0] = n;
rtcCommitGeometry(mesh);
unsigned int geomID = rtcAttachGeometry(scene, mesh);
rtcReleaseGeometry(mesh);
MatAndPrimColorTable mpTable;
mpTable.materialTable = {MaterialType::MATERIAL_EMITTER};
// We don't want to store albedo colors for the point light, we will use
// sample/eval functions and members of the light object
mpTable.primColorTable = nullptr;
mapGeomToPrim.insert(std::make_pair(geomID, mpTable));
return geomID;
}
#endif /* FILE_LIGHTSSEEN */
| h |
oneAPI-samples | data/projects/oneAPI-samples/RenderingToolkit/Tutorial/PathTracingWithEmbree/cpu/src/rkPathTracer.cpp | #ifdef _MSC_VER
#ifndef NOMINMAX
/* use the c++ library min and max instead of the MSVS macros */
/* rkcommon will define this macro upon CMake import */
#define NOMINMAX
#endif
#endif
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include <stb/stb_image_write.h>
/* Additions for pathtracer */
#include <chrono>
#include <map>
#include <memory>
#include <string>
#include <vector>
#include "Renderer.h"
#include "definitions.h"
using std::string;
int write_image_first_accumulation(SceneSelector sceneSelector,
unsigned int width, unsigned int height,
unsigned int channels, unsigned int spp,
unsigned int accu_limit,
unsigned int max_path_length,
unsigned char* pixels) {
int ret = -1;
/* Label construction for output files */
string strscene;
switch (sceneSelector) {
case SceneSelector::SHOW_CORNELL_BOX:
strscene = "-cornell";
break;
case SceneSelector::SHOW_CUBE_AND_PLANE:
strscene = "-cubeandplane";
break;
case SceneSelector::SHOW_POOL:
strscene = "-pool";
break;
default:
strscene = "-default";
break;
};
string suffix = string("-spp") + std::to_string(spp) + string("-accu") +
std::to_string(accu_limit) + string("-plength") +
std::to_string(max_path_length) + string("-") +
std::to_string(width) + string("x") + std::to_string(height) +
string(".png");
string prefix = "pathtracer-single";
string singleFilename = prefix + strscene + suffix;
/* Write a single accumulation image (useful for comparison) */
if ((ret = stbi_write_png(singleFilename.c_str(), width, height, channels,
pixels, width * channels)))
std::cout << "Output image: '" << singleFilename
<< "'... written to disk\n";
return ret;
}
int write_image_all_accumulations(SceneSelector sceneSelector,
unsigned int width, unsigned int height,
unsigned int channels, unsigned int spp,
unsigned int accu_limit,
unsigned int max_path_length,
unsigned char* pixels) {
int ret = -1;
/* Label construction for output files */
string strscene;
switch (sceneSelector) {
case SceneSelector::SHOW_CORNELL_BOX:
strscene = "-cornell";
break;
case SceneSelector::SHOW_CUBE_AND_PLANE:
strscene = "-cubeandplane";
break;
case SceneSelector::SHOW_POOL:
strscene = "-pool";
break;
default:
strscene = "-default";
break;
};
string suffix = string("-spp") + std::to_string(spp) + string("-accu") +
std::to_string(accu_limit) + string("-plength") +
std::to_string(max_path_length) + string("-") +
std::to_string(width) + string("x") + std::to_string(height) +
string(".png");
/* Label construction for the accumulated output */
string prefix = "pathtracer-accu";
string accumFilename = prefix + strscene + suffix;
/* Write the accumulated image output */
if ((ret = stbi_write_png(accumFilename.c_str(), width, height, channels,
pixels, width * channels)))
std::cout << "Output image: '" << accumFilename << "'... written to disk\n";
return ret;
}
int main() {
/* create an image buffer initialize it with all zeroes */
const unsigned int width = 512;
const unsigned int height = 512;
const unsigned int channels = 3;
/* Control the total number of accumulations, the total number of samples per
* pixel per accumulation, and the maximum path length of any given traced
* path.*/
const unsigned long long accu_limit = 500;
const unsigned int spp = 1;
const unsigned int max_path_length = 8;
std::unique_ptr<Renderer> r;
// SceneSelector sceneSelector = SceneSelector::SHOW_POOL;
SceneSelector sceneSelector = SceneSelector::SHOW_CORNELL_BOX;
// SceneSelector sceneSelector = SceneSelector::SHOW_CUBE_AND_PLANE;
r = std::make_unique<Renderer>(width, height, channels, spp, accu_limit,
max_path_length, sceneSelector);
/* Use a basic timer to capure compute time per accumulation */
auto start = std::chrono::high_resolution_clock::now();
r->render_accumulation();
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> accum_time = end - start;
std::cout << "Accumulation 1 of " << accu_limit << ": " << accum_time.count()
<< "s\n";
write_image_first_accumulation(sceneSelector, width, height, channels, spp,
accu_limit, max_path_length, r->get_pixels());
/* Render all remaining accumulations (in addition to the first) */
for (unsigned long long i = 1; i < accu_limit; i++) {
start = std::chrono::high_resolution_clock::now();
r->render_accumulation();
end = std::chrono::high_resolution_clock::now();
accum_time = end - start;
std::cout << "Accumulation " << i + 1 << " of " << accu_limit << ": "
<< accum_time.count() << "s" << std::endl;
}
write_image_all_accumulations(sceneSelector, width, height, channels, spp,
accu_limit, max_path_length, r->get_pixels());
std::cout << "success\n";
return 0;
}
| cpp |
oneAPI-samples | data/projects/oneAPI-samples/RenderingToolkit/Tutorial/PathTracingWithEmbree/cpu/src/Sphere.h | #pragma once
#ifndef FILE_SPHERE_SEEN
#define FILE_SPHERE_SEEN
#include <embree4/rtcore.h>
#include <vector>
#include "Geometry.h"
#include "Materials.h"
#include "definitions.h"
class Sphere : public Geometry {
public:
Sphere(RTCScene scene, RTCDevice device,
std::map<unsigned int, MatAndPrimColorTable>& mapGeomToPrim,
MaterialType sphereMat, const Vec3fa& pos,
const Vec3fa& color, float radius,
std::map<unsigned int, size_t>& mapGeomToLightIdx,
std::vector<std::shared_ptr<Light>>& lights, AffineSpace3fa& camera,
unsigned int width, unsigned int height);
~Sphere();
unsigned int add_geometry(
RTCScene scene, RTCDevice device,
std::map<unsigned int, MatAndPrimColorTable>& mapGeomToPrim,
MaterialType sphereMat, const Vec3fa& pos, const Vec3fa& color,
float radius);
/* Place holder ... Could put a default sphere in here but we will leave it
* blank for now */
void add_geometry(
const RTCScene& scene, const RTCDevice& device,
std::map<unsigned int, MatAndPrimColorTable>& mapGeomToPrim) {}
/* Place holder ... Could put a default light or camera in here but we will
* leave it blank for now */
void setup_camera_and_lights(
const RTCScene& scene, const RTCDevice& device,
std::map<unsigned int, MatAndPrimColorTable>& mapGeomToPrim,
std::map<unsigned int, size_t>& mapGeomToLightIdx,
std::vector<std::shared_ptr<Light>>& lights, AffineSpace3fa& camera,
unsigned int width, unsigned int height) {}
/* Place holder */
void clean_geometry();
// Just one material for our sphere primitive
MaterialType m_sphereMat;
Vec3fa m_sphere_face_colors;
Vec3fa m_pos;
Vec3fa m_radius;
};
Sphere::Sphere(RTCScene scene, RTCDevice device,
std::map<unsigned int, MatAndPrimColorTable>& mapGeomToPrim,
MaterialType sphereMat, const Vec3fa& pos,
const Vec3fa& color, float radius,
std::map<unsigned int, size_t>& mapGeomToLightIdx,
std::vector<std::shared_ptr<Light>>& lights,
AffineSpace3fa& camera, unsigned int width,
unsigned int height) {
/* The unused variables are in case you would like to try lights with this
* sphere. See the setup_camera_and_lights(..) functions from the other
* Geometry objects */
m_sphereMat = sphereMat;
m_sphere_face_colors = color;
m_pos = pos;
m_radius = radius;
add_geometry(scene, device, mapGeomToPrim, sphereMat, pos, color, radius);
}
unsigned int Sphere::add_geometry(
RTCScene scene, RTCDevice device,
std::map<unsigned int, MatAndPrimColorTable>& mapGeomToPrim,
MaterialType sphereMat, const Vec3fa& pos, const Vec3fa& color,
float radius) {
RTCGeometry mesh = rtcNewGeometry(device, RTC_GEOMETRY_TYPE_SPHERE_POINT);
Vertex* vertices = (Vertex*)rtcSetNewGeometryBuffer(
mesh, RTC_BUFFER_TYPE_VERTEX, 0, RTC_FORMAT_FLOAT4, sizeof(Vertex), 1);
// Sphere primitive defined as singular Vec4 point for embree
Vertex p = {pos.x, pos.y, pos.z, radius};
vertices[0] = p;
rtcCommitGeometry(mesh);
unsigned int geomID = rtcAttachGeometry(scene, mesh);
rtcReleaseGeometry(mesh);
MatAndPrimColorTable mpTable;
mpTable.materialTable = std::vector<MaterialType>({m_sphereMat});
mpTable.primColorTable = &m_sphere_face_colors;
mapGeomToPrim.insert(std::make_pair(geomID, mpTable));
return geomID;
}
Sphere::~Sphere() { clean_geometry(); }
/* Only a place holder. Nothing is here because we do not attach any attributes
* for the sphere. */
void Sphere::clean_geometry() {}
#endif /* FILE_SPHERE_SEEN */
| h |
oneAPI-samples | data/projects/oneAPI-samples/RenderingToolkit/Tutorial/PathTracingWithEmbree/cpu/src/CornellBox.h | #pragma once
#ifndef FILE_CORNELLBOX_SEEN
#define FILE_CORNELLBOX_SEEN
#include <embree4/rtcore.h>
#include <vector>
#include "Geometry.h"
#include "Lights.h"
#include "Materials.h"
#include "definitions.h"
class CornellBoxGeometry : public Geometry {
public:
CornellBoxGeometry(
const RTCScene& scene, const RTCDevice& device,
std::map<unsigned int, MatAndPrimColorTable>& mapGeomToPrim,
std::map<unsigned int, size_t>& mapGeomToLightIdx,
std::vector<std::shared_ptr<Light>>& lights, AffineSpace3fa& camera,
unsigned int width, unsigned int height);
~CornellBoxGeometry();
private:
void add_geometry(
const RTCScene& scene, const RTCDevice& device,
std::map<unsigned int, MatAndPrimColorTable>& mapGeomToPrim);
void setup_camera_and_lights(
const RTCScene& scene, const RTCDevice& device,
std::map<unsigned int, MatAndPrimColorTable>& mapGeomToPrim,
std::map<unsigned int, size_t>& mapGeomToLightIdx,
std::vector<std::shared_ptr<Light>>& lights, AffineSpace3fa& camera,
unsigned int width, unsigned int height);
void clean_geometry();
/* Added for pathtracer */
Vec3fa* m_cornell_face_colors = nullptr;
Vec3fa* m_cornell_vertex_colors = nullptr;
// mesh data
static const std::vector<Vertex> m_cornellBoxVertices;
static const std::vector<Vec3fa> m_cornellBoxColors;
static const std::vector<MaterialType> m_cornellBoxMats;
static const std::vector<Quad> m_cornellBoxIndices;
};
CornellBoxGeometry::CornellBoxGeometry(
const RTCScene& scene, const RTCDevice& device,
std::map<unsigned int, MatAndPrimColorTable>& mapGeomToPrim,
std::map<unsigned int, size_t>& mapGeomToLightIdx,
std::vector<std::shared_ptr<Light>>& lights, AffineSpace3fa& camera,
unsigned int width, unsigned int height) {
add_geometry(scene, device, mapGeomToPrim);
setup_camera_and_lights(scene, device, mapGeomToPrim, mapGeomToLightIdx,
lights, camera, width, height);
}
void CornellBoxGeometry::add_geometry(
const RTCScene& scene, const RTCDevice& device,
std::map<unsigned int, MatAndPrimColorTable>& mapGeomToPrim) {
/* create a mesh for all the quads in the Cornell Box scene */
RTCGeometry mesh = rtcNewGeometry(device, RTC_GEOMETRY_TYPE_QUAD);
m_cornell_face_colors =
(Vec3fa*)alignedMalloc(sizeof(Vec3fa) * m_cornellBoxIndices.size(), 16);
m_cornell_vertex_colors =
(Vec3fa*)alignedMalloc(sizeof(Vec3fa) * m_cornellBoxVertices.size(), 16);
Vertex* vertices = (Vertex*)rtcSetNewGeometryBuffer(
mesh, RTC_BUFFER_TYPE_VERTEX, 0, RTC_FORMAT_FLOAT3, sizeof(Vertex),
m_cornellBoxVertices.size());
for (auto i = 0; i < m_cornellBoxVertices.size(); i++) {
vertices[i] = m_cornellBoxVertices[i];
m_cornell_vertex_colors[i] = m_cornellBoxColors[i];
}
/* set quads */
Quad* quads = (Quad*)rtcSetNewGeometryBuffer(mesh, RTC_BUFFER_TYPE_INDEX, 0,
RTC_FORMAT_UINT4, sizeof(Quad),
m_cornellBoxIndices.size());
for (auto i = 0; i < m_cornellBoxIndices.size(); i++) {
quads[i] = m_cornellBoxIndices[i];
m_cornell_face_colors[i] = m_cornellBoxColors[i * 4];
}
rtcSetGeometryVertexAttributeCount(mesh, 1);
rtcSetSharedGeometryBuffer(mesh, RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE, 0,
RTC_FORMAT_FLOAT3, *m_cornell_vertex_colors, 0,
sizeof(Vec3fa), m_cornellBoxVertices.size());
rtcCommitGeometry(mesh);
unsigned int geomID = rtcAttachGeometry(scene, mesh);
rtcReleaseGeometry(mesh);
MatAndPrimColorTable mpTable;
mpTable.materialTable = m_cornellBoxMats;
mpTable.primColorTable = m_cornell_face_colors;
mapGeomToPrim.insert(std::make_pair(geomID, mpTable));
}
void CornellBoxGeometry::clean_geometry() {
if (m_cornell_face_colors) alignedFree(m_cornell_face_colors);
m_cornell_face_colors = nullptr;
if (m_cornell_vertex_colors) alignedFree(m_cornell_vertex_colors);
m_cornell_vertex_colors = nullptr;
}
void CornellBoxGeometry::setup_camera_and_lights(
const RTCScene& scene, const RTCDevice& device,
std::map<unsigned int, MatAndPrimColorTable>& mapGeomToPrim,
std::map<unsigned int, size_t>& mapGeomToLightIdx,
std::vector<std::shared_ptr<Light>>& lights, AffineSpace3fa& camera,
unsigned int width, unsigned int height) {
/* A default camera view as specified from Cornell box presets given input
* from Intel OSPRay*/
// camera = positionCamera(Vec3fa(0.0, 0.0, -2.0f), Vec3fa(0, 0, 0),
// Vec3fa(0, 1, 0), 90.0f, width, height);
/* A camera position that connects the field of vision angle of the camera to
* the bounds of the cornell box */
float fov = 30.0f;
float fovrad = fov * M_PI / 180.0f;
float half_fovrad = fovrad * 0.5f;
camera = positionCamera(Vec3fa(0.0, 0.0, -1.0f - 1.f / tanf(half_fovrad)),
Vec3fa(0, 0, 0), Vec3fa(0, 1, 0), fov, width, height);
/* The magnitude of the light can be tricky. Lights such as the point light
* fall off at the inverse square of the distance. When designing a sandbox
* renderer, you may need to scale your light up or down to see your scene. */
// Vec3fa pow = 3.f * Vec3fa(0.78f, 0.551f, 0.183f);
/* An interesting position for an overhead light in the Cornell Box scene.
* Notice increased noise when lights are near objects */
// Vec3fa pos = Vec3fa(0.0f, 0.95f, 0.0f);
/* A somewhat central position for the point light within the box. This is
* similar to the position for the interactive pathtracer program shipped with
* Intel Embree */
// Vec3fa pos =
// Vec3fa(2.f * 213.0f / 556.0f - 1.f, 2.f * 300.f / 558.8f - 1.f,
// 2.f * 227.f / 559.2f - 1.f);
/* Below is a setup for a delta point light or a spherical geometric light */
/* Delta is 0 radius */
// float radius = 0.f;
// float radius = 0.075f;
// lights.push_back(std::make_shared<PointLight>(pos, pow, radius));
// Place holder to toggle light geometries
// if (radius > 0.f && true) {
// std::shared_ptr<PointLight> newPointLight =
// std::dynamic_pointer_cast<PointLight>(lights.back());
// unsigned int geomID = newPointLight->add_geometry(scene, device);
// mapGeomToLightIdx.insert(std::make_pair(geomID, lights.size() - 1));
//}
/* Here we have a light as a disc geometry */
Vec3fa spotPos(0.f, 0.95f, 0.0f);
Vec3fa spotDir(0.f, -1.f, 0.f);
Vec3fa spotPow = 10.f * Vec3fa(0.78f, 0.551f, 0.183f);
float spotCosAngleMax = cosf(80.f * M_PI / 180.f);
float spotCosAngleScale = 50.f;
float spotRadius = 0.4f;
lights.push_back(std::make_shared<SpotLight>(spotPos, spotDir, spotPow,
spotCosAngleMax,
spotCosAngleScale, spotRadius));
/* Add geometry if you want it! */
if (spotRadius > 0.f) {
std::shared_ptr<SpotLight> pSpotLight =
std::dynamic_pointer_cast<SpotLight>(lights.back());
unsigned int geomID =
pSpotLight->add_geometry(scene, device, mapGeomToPrim);
mapGeomToLightIdx.insert(std::make_pair(geomID, lights.size() - 1));
}
}
CornellBoxGeometry::~CornellBoxGeometry() { clean_geometry(); }
const std::vector<Vertex> CornellBoxGeometry::m_cornellBoxVertices = {
// Floor
{1.00f, -1.00f, -1.00f, 0.0f},
{-1.00f, -1.00f, -1.00f, 0.0f},
{-1.00f, -1.00f, 1.00f, 0.0f},
{1.00f, -1.00f, 1.00f, 0.0f},
// Ceiling
{1.00f, 1.00f, -1.00f, 0.0f},
{1.00f, 1.00f, 1.00f, 0.0f},
{-1.00f, 1.00f, 1.00f, 0.0f},
{-1.00f, 1.00f, -1.00f, 0.0f},
// Backwall
{1.00f, -1.00f, 1.00f, 0.0f},
{-1.00f, -1.00f, 1.00f, 0.0f},
{-1.00f, 1.00f, 1.00f, 0.0f},
{1.00f, 1.00f, 1.00f, 0.0f},
// RightWall
{-1.00f, -1.00f, 1.00f, 0.0f},
{-1.00f, -1.00f, -1.00f, 0.0f},
{-1.00f, 1.00f, -1.00f, 0.0f},
{-1.00f, 1.00f, 1.00f, 0.0f},
// LeftWall
{1.00f, -1.00f, -1.00f, 0.0f},
{1.00f, -1.00f, 1.00f, 0.0f},
{1.00f, 1.00f, 1.00f, 0.0f},
{1.00f, 1.00f, -1.00f, 0.0f},
// ShortBox Top Face
{-0.53f, -0.40f, -0.75f, 0.0f},
{-0.70f, -0.40f, -0.17f, 0.0f},
{-0.13f, -0.40f, -0.00f, 0.0f},
{0.05f, -0.40f, -0.57f, 0.0f},
// ShortBox Left Face
{0.05f, -1.00f, -0.57f, 0.0f},
{0.05f, -0.40f, -0.57f, 0.0f},
{-0.13f, -0.40f, -0.00f, 0.0f},
{-0.13f, -1.00f, -0.00f, 0.0f},
// ShortBox Front Face
{-0.53f, -1.00f, -0.75f, 0.0f},
{-0.53f, -0.40f, -0.75f, 0.0f},
{0.05f, -0.40f, -0.57f, 0.0f},
{0.05f, -1.00f, -0.57f, 0.0f},
// ShortBox Right Face
{-0.70f, -1.00f, -0.17f, 0.0f},
{-0.70f, -0.40f, -0.17f, 0.0f},
{-0.53f, -0.40f, -0.75f, 0.0f},
{-0.53f, -1.00f, -0.75f, 0.0f},
// ShortBox Back Face
{-0.13f, -1.00f, -0.00f, 0.0f},
{-0.13f, -0.40f, -0.00f, 0.0f},
{-0.70f, -0.40f, -0.17f, 0.0f},
{-0.70f, -1.00f, -0.17f, 0.0f},
// ShortBox Bottom Face
{-0.53f, -1.00f, -0.75f, 0.0f},
{-0.70f, -1.00f, -0.17f, 0.0f},
{-0.13f, -1.00f, -0.00f, 0.0f},
{0.05f, -1.00f, -0.57f, 0.0f},
// TallBox Top Face
{0.53f, 0.20f, -0.09f, 0.0f},
{-0.04f, 0.20f, 0.09f, 0.0f},
{0.14f, 0.20f, 0.67f, 0.0f},
{0.71f, 0.20f, 0.49f, 0.0f},
// TallBox Left Face
{0.53f, -1.00f, -0.09f, 0.0f},
{0.53f, 0.20f, -0.09f, 0.0f},
{0.71f, 0.20f, 0.49f, 0.0f},
{0.71f, -1.00f, 0.49f, 0.0f},
// TallBox Front Face
{0.71f, -1.00f, 0.49f, 0.0f},
{0.71f, 0.20f, 0.49f, 0.0f},
{0.14f, 0.20f, 0.67f, 0.0f},
{0.14f, -1.00f, 0.67f, 0.0f},
// TallBox Right Face
{0.14f, -1.00f, 0.67f, 0.0f},
{0.14f, 0.20f, 0.67f, 0.0f},
{-0.04f, 0.20f, 0.09f, 0.0f},
{-0.04f, -1.00f, 0.09f, 0.0f},
// TallBox Back Face
{-0.04f, -1.00f, 0.09f, 0.0f},
{-0.04f, 0.20f, 0.09f, 0.0f},
{0.53f, 0.20f, -0.09f, 0.0f},
{0.53f, -1.00f, -0.09f, 0.0f},
// TallBox Bottom Face
{0.53f, -1.00f, -0.09f, 0.0f},
{-0.04f, -1.00f, 0.09f, 0.0f},
{0.14f, -1.00f, 0.67f, 0.0f},
{0.71f, -1.00f, 0.49f, 0.0f}};
const std::vector<Quad> CornellBoxGeometry::m_cornellBoxIndices = {
{0, 1, 2, 3}, // Floor
{4, 5, 6, 7}, // Ceiling
{8, 9, 10, 11}, // Backwall
{12, 13, 14, 15}, // RightWall
{16, 17, 18, 19}, // LeftWall
{20, 21, 22, 23}, // ShortBox Top Face
{24, 25, 26, 27}, // ShortBox Left Face
{28, 29, 30, 31}, // ShortBox Front Face
{32, 33, 34, 35}, // ShortBox Right Face
{36, 37, 38, 39}, // ShortBox Back Face
{40, 41, 42, 43}, // ShortBox Bottom Face
{44, 45, 46, 47}, // TallBox Top Face
{48, 49, 50, 51}, // TallBox Left Face
{52, 53, 54, 55}, // TallBox Front Face
{56, 57, 58, 59}, // TallBox Right Face
{60, 61, 62, 63}, // TallBox Back Face
{64, 65, 66, 67} // TallBox Bottom Face
};
const std::vector<Vec3fa> CornellBoxGeometry::m_cornellBoxColors = {
// Floor
{0.725f, 0.710f, 0.68f},
{0.725f, 0.710f, 0.68f},
{0.725f, 0.710f, 0.68f},
{0.725f, 0.710f, 0.68f},
// Ceiling
{0.725f, 0.710f, 0.68f},
{0.725f, 0.710f, 0.68f},
{0.725f, 0.710f, 0.68f},
{0.725f, 0.710f, 0.68f},
// Backwall
{0.725f, 0.710f, 0.68f},
{0.725f, 0.710f, 0.68f},
{0.725f, 0.710f, 0.68f},
{0.725f, 0.710f, 0.68f},
// RightWall
{0.140f, 0.450f, 0.091f},
{0.140f, 0.450f, 0.091f},
{0.140f, 0.450f, 0.091f},
{0.140f, 0.450f, 0.091f},
// LeftWall
{0.630f, 0.065f, 0.05f},
{0.630f, 0.065f, 0.05f},
{0.630f, 0.065f, 0.05f},
{0.630f, 0.065f, 0.05f},
// ShortBox Top Face
{0.725f, 0.710f, 0.68f},
{0.725f, 0.710f, 0.68f},
{0.725f, 0.710f, 0.68f},
{0.725f, 0.710f, 0.68f},
// ShortBox Left Face
{0.725f, 0.710f, 0.68f},
{0.725f, 0.710f, 0.68f},
{0.725f, 0.710f, 0.68f},
{0.725f, 0.710f, 0.68f},
// ShortBox Front Face
{0.725f, 0.710f, 0.68f},
{0.725f, 0.710f, 0.68f},
{0.725f, 0.710f, 0.68f},
{0.725f, 0.710f, 0.68f},
// ShortBox Right Face
{0.725f, 0.710f, 0.68f},
{0.725f, 0.710f, 0.68f},
{0.725f, 0.710f, 0.68f},
{0.725f, 0.710f, 0.68f},
// ShortBox Back Face
{0.725f, 0.710f, 0.68f},
{0.725f, 0.710f, 0.68f},
{0.725f, 0.710f, 0.68f},
{0.725f, 0.710f, 0.68f},
// ShortBox Bottom Face
{0.725f, 0.710f, 0.68f},
{0.725f, 0.710f, 0.68f},
{0.725f, 0.710f, 0.68f},
{0.725f, 0.710f, 0.68f},
/* 0.8f intensity of reflectance gives a decent proxy for a great real life
mirror */
// TallBox Top Face
{0.8f, 0.8f, 0.8f},
{0.8f, 0.8f, 0.8f},
{0.8f, 0.8f, 0.8f},
{0.8f, 0.8f, 0.8f},
// TallBox Left Face
{0.8f, 0.8f, 0.8f},
{0.8f, 0.8f, 0.8f},
{0.8f, 0.8f, 0.8f},
{0.8f, 0.8f, 0.8f},
// TallBox Front Face
{0.8f, 0.8f, 0.8f},
{0.8f, 0.8f, 0.8f},
{0.8f, 0.8f, 0.8f},
{0.8f, 0.8f, 0.8f},
// TallBox Right Face
{0.8f, 0.8f, 0.8f},
{0.8f, 0.8f, 0.8f},
{0.8f, 0.8f, 0.8f},
{0.8f, 0.8f, 0.8f},
// TallBox Back Face
{0.8f, 0.8f, 0.8f},
{0.8f, 0.8f, 0.8f},
{0.8f, 0.8f, 0.8f},
{0.8f, 0.8f, 0.8f},
// TallBox Bottom Face
{0.8f, 0.8f, 0.8f},
{0.8f, 0.8f, 0.8f},
{0.8f, 0.8f, 0.8f},
{0.8f, 0.8f, 0.8f}
/* Original colors of TallBox */
// TallBox Top Face
//{0.725f, 0.710f, 0.68f},
//{0.725f, 0.710f, 0.68f},
//{0.725f, 0.710f, 0.68f},
//{0.725f, 0.710f, 0.68f},
//// TallBox Left Face
//{0.725f, 0.710f, 0.68f},
//{0.725f, 0.710f, 0.68f},
//{0.725f, 0.710f, 0.68f},
//{0.725f, 0.710f, 0.68f},
//// TallBox Front Face
//{0.725f, 0.710f, 0.68f},
//{0.725f, 0.710f, 0.68f},
//{0.725f, 0.710f, 0.68f},
//{0.725f, 0.710f, 0.68f},
//// TallBox Right Face
//{0.725f, 0.710f, 0.68f},
//{0.725f, 0.710f, 0.68f},
//{0.725f, 0.710f, 0.68f},
//{0.725f, 0.710f, 0.68f},
//// TallBox Back Face
//{0.725f, 0.710f, 0.68f},
//{0.725f, 0.710f, 0.68f},
//{0.725f, 0.710f, 0.68f},
//{0.725f, 0.710f, 0.68f},
//// TallBox Bottom Face
//{0.725f, 0.710f, 0.68f},
//{0.725f, 0.710f, 0.68f},
//{0.725f, 0.710f, 0.68f},
//{0.725f, 0.710f, 0.68f}
};
const std::vector<MaterialType>
CornellBoxGeometry::m_cornellBoxMats = {
// Floor
MaterialType::MATERIAL_MATTE,
/* Swap in thr below material to make the ceiling a matte material*/
// Ceiling
MaterialType::MATERIAL_MATTE,
/* Swap in the below material to make the ceiling a mirror */
/*
//Ceiling
MaterialType::MATERIAL_MIRROR,
*/
// Backwall
MaterialType::MATERIAL_MATTE,
// RightWall
MaterialType::MATERIAL_MATTE,
// LeftWall
MaterialType::MATERIAL_MATTE,
/* Small box configuration for a matte material. Swap this section in
for the glass (thin dielectric) material as desired */
// ShortBox Top Face
MaterialType::MATERIAL_MATTE,
// ShortBox Left Face
MaterialType::MATERIAL_MATTE,
// ShortBox Front Face
MaterialType::MATERIAL_MATTE,
// ShortBox Right Face
MaterialType::MATERIAL_MATTE,
// ShortBox Back Face
MaterialType::MATERIAL_MATTE,
// ShortBox Bottom Face
MaterialType::MATERIAL_MATTE,
/* Small box configuration for glass material. Swap this section in for
the matte above. */
/*
// ShortBox Top Face
MaterialType::MATERIAL_GLASS,
// ShortBox Left Face
MaterialType::MATERIAL_GLASS,
// ShortBox Front Face
MaterialType::MATERIAL_GLASS,
// ShortBox Right Face
MaterialType::MATERIAL_GLASS,
// ShortBox Back Face
MaterialType::MATERIAL_GLASS,
// ShortBox Bottom Face
MaterialType::MATERIAL_GLASS,
*/
/* Tall Box configuration for a matte material. Swap this section in for
the mirror tall box (below) as desired*/
// TallBox Top Face
//MaterialType::MATERIAL_MATTE,
//// TallBox Left Face
//MaterialType::MATERIAL_MATTE,
//// TallBox Front Face
//MaterialType::MATERIAL_MATTE,
//// TallBox Right Face
//MaterialType::MATERIAL_MATTE,
//// TallBox Back Face
//MaterialType::MATERIAL_MATTE,
//// TallBox Bottom Face
//MaterialType::MATERIAL_MATTE
/* Tall box configuration for a mirror material. Swap this section in to
see behind the short cube */
// TallBox Top Face
MaterialType::MATERIAL_MIRROR,
// TallBox Left Face
MaterialType::MATERIAL_MIRROR,
// TallBox Front Face
MaterialType::MATERIAL_MIRROR,
// TallBox Right Face
MaterialType::MATERIAL_MIRROR,
// TallBox Back Face
MaterialType::MATERIAL_MIRROR,
// TallBox Bottom Face
MaterialType::MATERIAL_MIRROR
};
#endif /* !FILE_CORNELLBOX_SEEN */
| h |
oneAPI-samples | data/projects/oneAPI-samples/RenderingToolkit/Tutorial/PathTracingWithEmbree/cpu/src/SceneGraph.h | #pragma once
#include <embree4/rtcore.h>
#include "CornellBox.h"
#include "DefaultCubeAndPlane.h"
#include "Geometry.h"
#include "Lights.h"
#include "Pool.h"
#include "RandomSampler.h"
#include "Sphere.h"
/* Added for geometry selection in pathtracer */
enum class SceneSelector { SHOW_CUBE_AND_PLANE, SHOW_CORNELL_BOX, SHOW_POOL };
/* The most basic scene graph possible for exploratory code... please consider
* the scene graph from ospray studio or embree tutorials themselves as better
* production references */
struct SceneGraph {
public:
SceneGraph(RTCDevice device, SceneSelector SELECT_SCENE, unsigned int width,
unsigned int height);
void init_embree_scene(const RTCDevice device, SceneSelector SELECT_SCENE,
const unsigned int width, const unsigned int height);
bool intersect_path_and_scene(Vec3fa& org, Vec3fa& dir, RTCRayHit& rayhit,
DifferentialGeometry& dg, bool bCoherent);
void cast_shadow_rays(DifferentialGeometry& dg, Vec3fa& albedo,
MaterialType materialType, const Vec3fa& Lw,
const Vec3fa& wo, const Medium& medium, float time,
Vec3fa& L, RandomSampler& randomSampler,
bool bCoherent);
float cast_shadow_ray(const Vec3fa& org, const Vec3fa& dir, float tnear,
float tfar, float _time, bool bCoherent);
Vec3fa get_camera_origin();
Vec3fa get_direction_from_pixel(float x, float y);
unsigned int getNumLights();
std::shared_ptr<Light> get_light_from_geomID(unsigned int geomID);
~SceneGraph();
std::vector<std::shared_ptr<Light>> m_lights;
/* Added for path tracer: for holding material properties for each geometry id
*/
std::map<unsigned int, MatAndPrimColorTable> m_mapGeomToPrim;
private:
RTCScene m_scene;
SceneSelector m_sceneSelector;
rkcommon::math::AffineSpace3fa m_camera;
std::map<unsigned int, size_t> m_mapGeomToLightIdx;
void scene_cleanup();
/* We'll use this 'geometries' container to automatically clean up the data
* arrays created that are used to create embree geometries */
std::vector<std::unique_ptr<Geometry>> geometries;
};
SceneGraph::SceneGraph(const RTCDevice device, SceneSelector SELECT_SCENE,
const unsigned int width, const unsigned int height) {
init_embree_scene(device, SELECT_SCENE, width, height);
}
void SceneGraph::init_embree_scene(const RTCDevice device,
SceneSelector SELECT_SCENE,
const unsigned int width,
const unsigned int height) {
m_sceneSelector = SELECT_SCENE;
/* create scene */
m_scene = nullptr;
m_scene = rtcNewScene(device);
switch (m_sceneSelector) {
case SceneSelector::SHOW_CUBE_AND_PLANE:
/* add cube, add ground plane, and light */
geometries.push_back(std::make_unique<CubeAndPlane>(
m_scene, device, m_mapGeomToPrim, m_mapGeomToLightIdx, m_lights,
m_camera, width, height));
/* The sphere can be used in the cube and plane scene with a corresponding
* position for that scene */
/*
* geometries.push_back(std::make_unique<Sphere>(m_scene, device,
m_mapGeomToPrim, MaterialType::MATERIAL_MIRROR, Vec3fa(2.5f, 0.f, 2.5f),
Vec3fa(0.8f, 0.8f, 0.8f), 1.0f, m_mapGeomToLightIdx, m_lights, m_camera,
width, height));
*/
break;
case SceneSelector::SHOW_POOL:
geometries.push_back(std::make_unique<Pool>(
m_scene, device, m_mapGeomToPrim, m_mapGeomToLightIdx, m_lights,
m_camera, width, height));
break;
case SceneSelector::SHOW_CORNELL_BOX:
default:
/* add cornell box */
geometries.push_back(std::make_unique<CornellBoxGeometry>(
m_scene, device, m_mapGeomToPrim, m_mapGeomToLightIdx, m_lights,
m_camera, width, height));
/* If you would like to add an Intel Embree sphere see below for an
* example... Remember to look for materials properties
* set in the Sphere source */
Vec3fa pos = {0.6f, -0.8f, -0.6f};
Vec3fa color = {1.f, 1.f, 1.f};
float radius = 0.2f;
geometries.push_back(std::make_unique<Sphere>(
m_scene, device, m_mapGeomToPrim, MaterialType::MATERIAL_GLASS, pos,
color, radius, m_mapGeomToLightIdx, m_lights, m_camera, width,
height));
break;
}
/* commit changes to scene */
rtcCommitScene(m_scene);
}
bool SceneGraph::intersect_path_and_scene(Vec3fa& org, Vec3fa& dir,
RTCRayHit& rayhit,
DifferentialGeometry& dg,
bool bCoherent) {
/* New with Embree 4... RTCIntersectArguments to set ray coherency */
/* Only primary rays are set as coherent in this example program */
RTCIntersectArguments iargs;
rtcInitIntersectArguments(&iargs);
iargs.flags =
(bCoherent) ? RTC_RAY_QUERY_FLAG_COHERENT : RTC_RAY_QUERY_FLAG_INCOHERENT;
/* intersect ray with scene */
rtcIntersect1(m_scene, &rayhit, &iargs);
/* if nothing hit the path is terminated, this could be an environment light
* lookup instead */
if (rayhit.hit.geomID == RTC_INVALID_GEOMETRY_ID) return false;
Vec3fa Ng = Vec3fa(rayhit.hit.Ng_x, rayhit.hit.Ng_y, rayhit.hit.Ng_z);
Vec3fa Ns = normalize(Ng);
/* compute differential geometry */
for (int i = 0; i < RTC_MAX_INSTANCE_LEVEL_COUNT; i++)
dg.instIDs[i] = rayhit.hit.instID[i];
dg.geomID = rayhit.hit.geomID;
dg.primID = rayhit.hit.primID;
dg.u = rayhit.hit.u;
dg.v = rayhit.hit.v;
dg.P = org + rayhit.ray.tfar * dir;
dg.Ng = Ng;
dg.Ns = Ns;
/* Reference epsilon value to move away from the plane, avoid artifacts */
dg.eps =
32.0f * 1.19209e-07f *
max(max(abs(dg.P.x), abs(dg.P.y)), max(abs(dg.P.z), rayhit.ray.tfar));
dg.Ng = face_forward(dir, normalize(dg.Ng));
dg.Ns = face_forward(dir, normalize(dg.Ns));
return true;
}
void SceneGraph::cast_shadow_rays(DifferentialGeometry& dg, Vec3fa& albedo,
MaterialType materialType, const Vec3fa& Lw,
const Vec3fa& wo, const Medium& medium,
float time, Vec3fa& L,
RandomSampler& randomSampler,
bool bCoherent) {
Vec3fa ret;
RTCOccludedArguments oargs;
rtcInitOccludedArguments(&oargs);
/* In this program Occluded rays are never coherent so this is hard coded. */
oargs.flags = RTC_RAY_QUERY_FLAG_INCOHERENT;
/* Otherwise, the assignment commented below would apply */
/* oargs.flags = (bCoherent) ? RTC_RAY_QUERY_FLAG_COHERENT :
* RTC_RAY_QUERY_FLAG_INCOHERENT; */
for (std::shared_ptr<Light> light : m_lights) {
Vec2f randomLightSample(randomSampler.get_float(),
randomSampler.get_float());
Light_SampleRes ls = light->sample(dg, randomLightSample);
/* If the sample probability density evaluation is 0 then no need to
* consider this shadow ray */
if (ls.pdf <= 0.0f) continue;
RTCRayHit shadow;
init_RayHit(shadow, dg.P, ls.dir, dg.eps, ls.dist, time);
rtcOccluded1(m_scene, &shadow.ray, &oargs);
if (shadow.ray.tfar >= 0.0f) {
L = L + Lw * ls.weight *
Material_eval(albedo, materialType, Lw, wo, dg, ls.dir,
medium, randomLightSample);
}
}
}
float SceneGraph::cast_shadow_ray(const Vec3fa& org, const Vec3fa& dir,
float tnear, float tfar, float _time,
bool bCoherent) {
RTCRayHit shadow;
init_RayHit(shadow, org, dir, tnear, tfar, _time);
RTCOccludedArguments oargs;
rtcInitOccludedArguments(&oargs);
/* In this program Occluded rays are assumed coherent so this is hard coded. */
oargs.flags = RTC_RAY_QUERY_FLAG_INCOHERENT;
/* Otherwise, the assignment commented below would apply */
/* oargs.flags = (bCoherent) ? RTC_RAY_QUERY_FLAG_COHERENT :
* RTC_RAY_QUERY_FLAG_INCOHERENT; */
rtcOccluded1(m_scene, &shadow.ray, &oargs);
return shadow.ray.tfar;
}
Vec3fa SceneGraph::get_camera_origin() {
return Vec3fa(m_camera.p.x, m_camera.p.y, m_camera.p.z);
}
Vec3fa SceneGraph::get_direction_from_pixel(float x, float y) {
return normalize(x * m_camera.l.vx + y * m_camera.l.vy + m_camera.l.vz);
}
unsigned int SceneGraph::getNumLights() { return m_lights.size(); }
std::shared_ptr<Light> SceneGraph::get_light_from_geomID(unsigned int geomID) {
size_t idx = m_mapGeomToLightIdx[geomID];
return m_lights[idx];
}
/* called by the C++ code for cleanup */
void SceneGraph::scene_cleanup() {
rtcReleaseScene(m_scene);
m_scene = nullptr;
}
SceneGraph::~SceneGraph() { scene_cleanup(); }
| h |
oneAPI-samples | data/projects/oneAPI-samples/RenderingToolkit/Tutorial/PathTracingWithEmbree/cpu/src/PathTracer.h | #pragma once
#ifndef FILE_PATHTRACERSEEN
#define FILE_PATHTRACERSEEN
#include <random>
#include "Lights.h"
#include "RandomSampler.h"
#include "SceneGraph.h"
#include "definitions.h"
struct PathTracer {
public:
PathTracer(unsigned int max_path_length);
PathTracer(unsigned int max_path_length, unsigned int width,
unsigned int height, unsigned int numLights);
~PathTracer();
/* task that renders a single path pixel */
Vec3fa render_path(float x, float y, RandomSampler& randomSampler,
std::shared_ptr<SceneGraph> sg, unsigned int pxID);
private:
unsigned int m_max_path_length;
/* "Time" set to 0.0f for all rays as there is no motion blur, nor frame
* interpolation, nor animation */
const float m_time = 0.0f;
unsigned int m_numLights;
};
PathTracer::PathTracer(unsigned int max_path_length)
: m_max_path_length(max_path_length) {}
PathTracer::PathTracer(unsigned int max_path_length, unsigned int width,
unsigned int height, unsigned int numLights)
: m_max_path_length(max_path_length), m_numLights(numLights) {}
/* task that renders a single screen pixel */
Vec3fa PathTracer::render_path(float x, float y, RandomSampler& randomSampler,
std::shared_ptr<SceneGraph> sg,
unsigned int pxID) {
Vec3fa dir = sg->get_direction_from_pixel(x, y);
Vec3fa org = sg->get_camera_origin();
/* initialize ray */
RTCRayHit rayhit;
init_RayHit(rayhit, org, dir, 0.0f, std::numeric_limits<float>::infinity(),
m_time);
Vec3fa L = Vec3fa(0.0f);
Vec3fa Lw = Vec3fa(1.0f);
Medium medium, nextMedium;
medium.eta = nextMedium.eta = 1.f;
DifferentialGeometry dg;
bool bCoherent = true;
/* iterative path tracer loop */
for (int i = 0; i < m_max_path_length; i++) {
/* terminate if contribution too low */
if (max(Lw.x, max(Lw.y, Lw.z)) < 0.01f) break;
/* New for Embree 4: Use coherent ray designation on pramary ray cast with
* RTCIntersectArguments::flags by passing bCoherent*/
if (!sg->intersect_path_and_scene(org, dir, rayhit, dg, bCoherent)) break;
const Vec3fa wo = -dir;
/* Next is material discovery.
* Note: the full pathtracer program includes lookup of materials from a
* scenegraph object. This could include texture lookup transformations that
* are based on vertex-to-texture assignments. This could include a required
* tranformation of normals for geometries that have been instanced. Below
* is a simple option for materials.
*/
/* default albedo is a pink color for debug */
Vec3fa albedo = Vec3fa(0.9f, 0.7f, 0.7f);
MaterialType materialType = MaterialType::MATERIAL_MATTE;
materialType =
sg->m_mapGeomToPrim[rayhit.hit.geomID].materialTable[rayhit.hit.primID];
if (materialType == MaterialType::MATERIAL_EMITTER) {
std::shared_ptr<Light> light =
sg->get_light_from_geomID(rayhit.hit.geomID);
Light_EvalRes le = light->eval(org, dir);
L = L + Lw * le.value;
// If we encounter a light emitter we will terminate adding more light
// from the path.
// Alternatively, we could move the intersected ray through the light
// source and continue
break;
} else {
albedo = sg->m_mapGeomToPrim[rayhit.hit.geomID]
.primColorTable[rayhit.hit.primID];
/* An albedo as well as a material type is used */
}
/* weight scaling based on material sample. used for attenuation amongst
* path segments */
Vec3fa c = Vec3fa(1.0f);
Vec3fa wi1;
Vec2f randomMatSample(randomSampler.get_float(), randomSampler.get_float());
/* Occlusion and Intersect test arguments have changed with Embree 4.
* Occlusion query flags are set before the shadow ray lookup In this
* example program, Intersect query flags will be set to
* RTC_RAY_QUERY_INCOHERENT for all non-primary rays. */
bCoherent = false;
/* For the occlusion test, search for each light in the scene from the hit
* point. Aggregate the radiance if hit point is not occluded */
if (Material_direct_illumination(materialType)) {
/* Cast shadow ray(s) from the hit point */
sg->cast_shadow_rays(dg, albedo, materialType, Lw, wo, medium, m_time, L,
randomSampler, bCoherent);
}
/* Sample, Eval, and PDF computation are split and internally perform some
* redundant calculation */
wi1 = Material_sample(materialType, Lw, wo, dg, medium, nextMedium,
randomMatSample);
c = c * Material_eval(albedo, materialType, Lw, wo, dg, wi1, medium);
float nextPDF = Material_pdf(materialType, Lw, wo, dg, medium, wi1);
if (nextPDF <= 1E-4f) break;
Lw = Lw * c / nextPDF;
/* setup secondary ray */
medium = nextMedium;
float sign = dot(wi1, dg.Ng) < 0.0f ? -1.0f : 1.0f;
dg.P = dg.P + sign * dg.eps * dg.Ng;
org = dg.P;
dir = normalize(wi1);
init_RayHit(rayhit, org, dir, dg.eps, inf, m_time);
}
return L;
}
PathTracer::~PathTracer() {}
#endif /* FILE_PATHTRACERSEEN */
| h |
oneAPI-samples | data/projects/oneAPI-samples/RenderingToolkit/Tutorial/PathTracingWithEmbree/cpu/src/Materials.h | #pragma once
#ifndef FILE_MATERIALSSEEN
#define FILE_MATERIALSSEEN
#include "definitions.h"
/* Added for pathtracer */
enum class MaterialType {
MATERIAL_MATTE,
MATERIAL_MIRROR,
MATERIAL_GLASS,
MATERIAL_WATER,
MATERIAL_EMITTER
};
/* Added for pathtracer */
struct Medium {
/* Just using medium eta value not transmission in this code */
// Vec3fa transmission;
float eta;
};
/* Added for path tracer: creating a lookup structure for intersected geometries
*/
struct MatAndPrimColorTable {
std::vector<MaterialType> materialTable;
Vec3fa* primColorTable;
};
inline Vec3fa refract(const Vec3fa& V, const Vec3fa& N, const float eta,
const float cosi, float& cost, float& refractionPDF) {
const float k = 1.0f - eta * eta * (1.0f - cosi * cosi);
if (k < 0.0f) {
cost = 0.0f;
refractionPDF = 0.f;
return Vec3fa(0.f);
}
cost = sqrt(k);
refractionPDF = eta * eta;
return eta * (cosi * N - V) - cost * N;
}
/*! Reflects a viewing vector V at a normal N. */
inline Vec3fa reflect(const Vec3fa& V, const Vec3fa& N) {
return 2.0f * dot(V, N) * N - V;
}
inline float fresnelDielectric(const float cosi, const float cost,
const float eta) {
const float Rper = (eta * cosi - cost) * rcp(eta * cosi + cost);
const float Rpar = (cosi - eta * cost) * rcp(cosi + eta * cost);
return 0.5f * (Rpar * Rpar + Rper * Rper);
}
inline float fresnelDielectric(const float cosi, const float eta) {
const float k = 1.0f - eta * eta * (1.0f - cosi * cosi);
if (k < 0.0f) return 1.0f;
const float cost = sqrt(k);
return fresnelDielectric(cosi, cost, eta);
}
/* Monte Carlo ray tracing "_eval" functions:
evaluates X for a given set of random variables (direction)
*/
inline Vec3fa Dielectric_eval(const Vec3fa& albedo, const Vec3fa& Lw,
const Vec3fa& wo, const DifferentialGeometry& dg,
const Vec3fa& wi_v, const Medium& medium,
const float dielEta, const float s) {
float eta = 0.0f;
Medium mediumOutside;
mediumOutside.eta = 1.0f;
Medium mediumInside;
mediumInside.eta = dielEta;
Medium mediumFront, mediumBack;
if (medium.eta == mediumInside.eta) {
eta = mediumInside.eta / mediumOutside.eta;
mediumFront = mediumInside;
mediumBack = mediumOutside;
} else {
eta = mediumOutside.eta / mediumInside.eta;
mediumFront = mediumOutside;
mediumBack = mediumInside;
}
float cosThetaO = clamp(dot(wo, dg.Ns));
float cosThetaI;
/* refraction computation */
Vec3fa refractionDir;
float refractionPDF;
refractionDir = refract(wo, dg.Ns, eta, cosThetaO, cosThetaI, refractionPDF);
/* reflection computation */
Vec3fa reflectionDir;
reflectionDir = reflect(wo, dg.Ns);
float reflectionPDF = 1.0f;
float R = fresnelDielectric(cosThetaO, cosThetaI, eta);
Vec3fa cs = Vec3fa(R);
Vec3fa ct = Vec3fa(1.0f - R);
const Vec3fa m0 = Lw * cs / reflectionPDF;
const Vec3fa m1 = Lw * ct / refractionPDF;
const float C0 = reflectionPDF == 0.0f ? 0.0f : max(max(m0.x, m0.y), m0.z);
const float C1 = refractionPDF == 0.0f ? 0.0f : max(max(m1.x, m1.y), m1.z);
const float C = C0 + C1;
if (C == 0.0f) {
return Vec3fa(0.f, 0.f, 0.f);
}
/* Compare weights for the reflection and the refraction. Pick a direction
* given s is a random between 0 and 1 */
const float CP0 = C0 / C;
if (s < CP0)
return albedo * cs;
else
return albedo * ct;
}
inline Vec3fa Dielectric_eval(const Vec3fa& albedo, const Vec3fa& Lw,
const Vec3fa& wo, const DifferentialGeometry& dg,
const Vec3fa& wi_v, const Medium& medium,
const float dielEta) {
float eta = 0.0f;
Medium mediumOutside;
mediumOutside.eta = 1.0f;
Medium mediumInside;
mediumInside.eta = dielEta;
Medium mediumFront, mediumBack;
if (medium.eta == mediumInside.eta) {
eta = mediumInside.eta / mediumOutside.eta;
mediumFront = mediumInside;
mediumBack = mediumOutside;
} else {
eta = mediumOutside.eta / mediumInside.eta;
mediumFront = mediumOutside;
mediumBack = mediumInside;
}
float cosThetaO = clamp(dot(wo, dg.Ns));
float cosThetaI;
/* refraction computation */
Vec3fa refractionDir;
float refractionPDF;
refractionDir = refract(wo, dg.Ns, eta, cosThetaO, cosThetaI, refractionPDF);
/* reflection computation */
Vec3fa reflectionDir;
reflectionDir = reflect(wo, dg.Ns);
float reflectionPDF = 1.0f;
float R = fresnelDielectric(cosThetaO, cosThetaI, eta);
Vec3fa cs = Vec3fa(R);
Vec3fa ct = Vec3fa(1.0f - R);
const Vec3fa m0 = Lw * cs / reflectionPDF;
const Vec3fa m1 = Lw * ct / refractionPDF;
const float C0 = reflectionPDF == 0.0f ? 0.0f : max(max(m0.x, m0.y), m0.z);
const float C1 = refractionPDF == 0.0f ? 0.0f : max(max(m1.x, m1.y), m1.z);
const float C = C0 + C1;
if (C == 0.0f) {
return Vec3fa(0.f, 0.f, 0.f);
}
/* Compare weights for the reflection and the refraction. Pick a direction
* given s is a random between 0 and 1 */
const float CP0 = C0 / C;
if (dot(wi_v, dg.Ns) >= 0.f)
return albedo * cs;
else
return albedo * ct;
}
inline Vec3fa Lambertian_eval(const Vec3fa& albedo, const Vec3fa& wo,
const DifferentialGeometry& dg,
const Vec3fa& wi_v) {
/* The diffuse material. Reflectance (albedo) times the cosign fall off of the
* vector about the normal. */
return albedo * (1.f / (float)(float(M_PI))) * clamp(dot(wi_v, dg.Ns));
}
inline Vec3fa Mirror_eval(const Vec3fa& albedo, const Vec3fa& wo,
const DifferentialGeometry& dg, Vec3fa wi_v) {
return albedo;
}
Vec3fa Material_eval(Vec3fa albedo, MaterialType materialType, const Vec3fa& Lw,
const Vec3fa& wo, const DifferentialGeometry& dg,
const Vec3fa& wi, const Medium& medium, const Vec2f& s) {
Vec3fa c = Vec3fa(0.0f);
switch (materialType) {
case MaterialType::MATERIAL_MATTE:
return Lambertian_eval(albedo, wo, dg, wi);
break;
case MaterialType::MATERIAL_MIRROR:
return Mirror_eval(albedo, wo, dg, wi);
break;
case MaterialType::MATERIAL_GLASS:
return Dielectric_eval(albedo, Lw, wo, dg, wi, medium, 1.5f, s.x);
/* Try thin dielectric!? */
break;
case MaterialType::MATERIAL_WATER:
return Dielectric_eval(albedo, Lw, wo, dg, wi, medium, 1.3f, s.x);
break;
/* Return our debug color if something goes awry */
default:
break;
}
return c;
}
Vec3fa Material_eval(Vec3fa albedo, MaterialType materialType, const Vec3fa& Lw,
const Vec3fa& wo, const DifferentialGeometry& dg,
const Vec3fa& wi, const Medium& medium) {
Vec3fa c = Vec3fa(0.0f);
switch (materialType) {
case MaterialType::MATERIAL_MATTE:
return Lambertian_eval(albedo, wo, dg, wi);
break;
case MaterialType::MATERIAL_MIRROR:
return Mirror_eval(albedo, wo, dg, wi);
break;
case MaterialType::MATERIAL_GLASS:
return Dielectric_eval(albedo, Lw, wo, dg, wi, medium, 1.5f);
/* Try thin dielectric!? */
break;
case MaterialType::MATERIAL_WATER:
return Dielectric_eval(albedo, Lw, wo, dg, wi, medium, 1.3f);
break;
/* Return our debug color if something goes awry */
default:
break;
}
return c;
}
/* Material Sampling Functions */
Vec3fa Dielectric_sample(const Vec3fa& Lw, const Vec3fa& wo,
const DifferentialGeometry& dg, const Medium& medium,
float dielEta, Medium& nextMedium, const float s) {
float eta = 0.0f;
Medium mediumOutside;
mediumOutside.eta = 1.0f;
Medium mediumInside;
mediumInside.eta = dielEta;
Medium mediumFront, mediumBack;
if (medium.eta == mediumInside.eta) {
eta = mediumInside.eta / mediumOutside.eta;
mediumFront = mediumInside;
mediumBack = mediumOutside;
} else {
eta = mediumOutside.eta / mediumInside.eta;
mediumFront = mediumOutside;
mediumBack = mediumInside;
}
float cosThetaO = clamp(dot(wo, dg.Ns));
float cosThetaI;
/* refraction computation */
Vec3fa refractionDir;
float refractionPDF;
refractionDir = refract(wo, dg.Ns, eta, cosThetaO, cosThetaI, refractionPDF);
/* reflection computation */
Vec3fa reflectionDir;
reflectionDir = reflect(wo, dg.Ns);
float reflectionPDF = 1.0f;
float R = fresnelDielectric(cosThetaO, cosThetaI, eta);
Vec3fa cs = Vec3fa(R);
Vec3fa ct = Vec3fa(1.0f - R);
const Vec3fa m0 = Lw * cs / reflectionPDF;
const Vec3fa m1 = Lw * ct / refractionPDF;
const float C0 = reflectionPDF == 0.0f ? 0.0f : max(max(m0.x, m0.y), m0.z);
const float C1 = refractionPDF == 0.0f ? 0.0f : max(max(m1.x, m1.y), m1.z);
const float C = C0 + C1;
if (C == 0.0f) {
return Vec3fa(0, 0, 0);
}
/* Compare weights for the reflection and the refraction. Pick a direction
* given s is a random between 0 and 1 */
const float CP0 = C0 / C;
const float CP1 = C1 / C;
if (s < CP0) {
nextMedium = mediumFront;
return reflectionDir;
} else {
nextMedium = mediumBack;
return refractionDir;
}
}
Vec3fa Lambertian_sample(const Vec3fa& wo, const DifferentialGeometry& dg,
const Vec2f& randomMatSample) {
return cosineSampleHemisphere(randomMatSample.x, randomMatSample.y, dg.Ns);
}
Vec3fa Mirror_sample(const Vec3fa& Lw, const Vec3fa& wo,
const DifferentialGeometry& dg) {
/* Compute a reflection vector 2 * N.L * N - L */
return 2.0f * dot(wo, dg.Ns) * dg.Ns - wo;
}
Vec3fa Material_sample(MaterialType materialType, const Vec3fa& Lw,
const Vec3fa& wo, const DifferentialGeometry& dg,
const Medium& medium, Medium& nextMedium,
const Vec2f& randomMatSample) {
Vec3fa dir = Vec3fa(0.0f);
switch (materialType) {
case MaterialType::MATERIAL_MATTE:
return Lambertian_sample(wo, dg, randomMatSample);
break;
case MaterialType::MATERIAL_MIRROR:
return Mirror_sample(Lw, wo, dg);
break;
case MaterialType::MATERIAL_GLASS:
return Dielectric_sample(Lw, wo, dg, medium, 1.5f, nextMedium,
randomMatSample.x);
break;
case MaterialType::MATERIAL_WATER:
return Dielectric_sample(Lw, wo, dg, medium, 1.3f, nextMedium,
randomMatSample.x);
break;
default:
break;
}
return dir;
}
/* Compute PDF */
/* Important to use the same random sample as is used for the
* Lambertian_sample(..) if looking just for the pdf */
float Lambertian_pdf(const float& s) {
float cosTheta = sqrt(s);
return cosTheta / float(M_PI);
}
float Lambertian_pdf(const DifferentialGeometry& dg, const Vec3fa& wi1) {
return dot(wi1, dg.Ns) / float(M_PI);
}
float Mirror_pdf() { return 1.0f; }
float Dielectric_pdf(const Vec3fa& Lw, const Vec3fa& wo,
const DifferentialGeometry& dg, const Medium& medium,
const float dielEta, const float s) {
float pdf = 0.f;
float eta = 0.0f;
Medium mediumOutside;
mediumOutside.eta = 1.0f;
Medium mediumInside;
mediumInside.eta = dielEta;
if (medium.eta == mediumInside.eta) {
eta = mediumInside.eta / mediumOutside.eta;
} else {
eta = mediumOutside.eta / mediumInside.eta;
}
float cosThetaO = clamp(dot(wo, dg.Ns));
float cosThetaI;
/* refraction computation */
float refractPDF;
const float k = 1.0f - eta * eta * (1.0f - cosThetaO * cosThetaO);
if (k < 0.0f) {
cosThetaI = 0.0f;
refractPDF = 0.0f;
} else {
cosThetaI = sqrt(k);
refractPDF = eta * eta;
}
/* reflection computation */
float reflectPDF;
reflectPDF = 1.0f;
float R = fresnelDielectric(cosThetaO, cosThetaI, eta);
Vec3fa cs = Vec3fa(R);
Vec3fa ct = Vec3fa(1.0f - R);
const Vec3fa m0 = Lw * cs / reflectPDF;
const Vec3fa m1 = Lw * ct / refractPDF;
const float C0 = reflectPDF == 0.0f ? 0.0f : max(max(m0.x, m0.y), m0.z);
const float C1 = refractPDF == 0.0f ? 0.0f : max(max(m1.x, m1.y), m1.z);
const float C = C0 + C1;
if (C == 0.0f) {
return 0.0f;
}
/* Compare weights for the reflection and the refraction. Pick a pdf
* given s.x is a random between 0 and 1 */
const float CP0 = C0 / C;
const float CP1 = C1 / C;
if (s < CP0) {
pdf = reflectPDF * CP0;
} else {
pdf = refractPDF * CP1;
}
return pdf;
}
float Dielectric_pdf(const Vec3fa& Lw, const Vec3fa& wo,
const DifferentialGeometry& dg, const Medium& medium,
const float dielEta, const Vec3fa& wi1) {
float pdf = 0.f;
float eta = 0.0f;
Medium mediumOutside;
mediumOutside.eta = 1.0f;
Medium mediumInside;
mediumInside.eta = dielEta;
if (medium.eta == mediumInside.eta) {
eta = mediumInside.eta / mediumOutside.eta;
} else {
eta = mediumOutside.eta / mediumInside.eta;
}
float cosThetaO = clamp(dot(wo, dg.Ns));
float cosThetaI;
/* refraction computation */
float refractPDF;
const float k = 1.0f - eta * eta * (1.0f - cosThetaO * cosThetaO);
if (k < 0.0f) {
cosThetaI = 0.0f;
refractPDF = 0.0f;
} else {
cosThetaI = sqrt(k);
refractPDF = eta * eta;
}
/* reflection computation */
float reflectPDF;
reflectPDF = 1.0f;
float R = fresnelDielectric(cosThetaO, cosThetaI, eta);
Vec3fa cs = Vec3fa(R);
Vec3fa ct = Vec3fa(1.0f - R);
const Vec3fa m0 = Lw * cs / reflectPDF;
const Vec3fa m1 = Lw * ct / refractPDF;
const float C0 = reflectPDF == 0.0f ? 0.0f : max(max(m0.x, m0.y), m0.z);
const float C1 = refractPDF == 0.0f ? 0.0f : max(max(m1.x, m1.y), m1.z);
const float C = C0 + C1;
if (C == 0.0f) {
return 0.0f;
}
/* Compare weights for the reflection and the refraction. Pick a pdf
* given s.x is a random between 0 and 1 */
const float CP0 = C0 / C;
const float CP1 = C1 / C;
if (dot(wi1, dg.Ns) >= 0.f) {
pdf = reflectPDF * CP0;
} else {
pdf = refractPDF * CP1;
}
return pdf;
}
/* Determine Probability Density Function for Materials */
float Material_pdf(MaterialType materialType, const Vec3fa& Lw,
const Vec3fa& wo, const DifferentialGeometry& dg,
const Medium& medium, const Vec2f& randomSample) {
switch (materialType) {
case MaterialType::MATERIAL_MATTE:
return Lambertian_pdf(randomSample.y);
break;
case MaterialType::MATERIAL_MIRROR:
return Mirror_pdf();
break;
case MaterialType::MATERIAL_GLASS:
return Dielectric_pdf(Lw, wo, dg, medium, 1.5f, randomSample.x);
break;
case MaterialType::MATERIAL_WATER:
return Dielectric_pdf(Lw, wo, dg, medium, 1.3f, randomSample.x);
break;
default:
break;
}
return 0.f;
}
float Material_pdf(MaterialType materialType, const Vec3fa& Lw,
const Vec3fa& wo, const DifferentialGeometry& dg,
const Medium& medium, const Vec3fa& wi1) {
switch (materialType) {
case MaterialType::MATERIAL_MATTE:
return Lambertian_pdf(dg, wi1);
break;
case MaterialType::MATERIAL_MIRROR:
return Mirror_pdf();
break;
case MaterialType::MATERIAL_GLASS:
return Dielectric_pdf(Lw, wo, dg, medium, 1.5f, wi1);
break;
case MaterialType::MATERIAL_WATER:
return Dielectric_pdf(Lw, wo, dg, medium, 1.3f, wi1);
break;
default:
break;
}
return 0.f;
}
inline bool Material_direct_illumination(MaterialType materialType) {
switch (materialType) {
case MaterialType::MATERIAL_MATTE:
return true;
break;
case MaterialType::MATERIAL_MIRROR:
return false;
break;
case MaterialType::MATERIAL_GLASS:
case MaterialType::MATERIAL_WATER:
return false;
break;
default:
break;
}
return false;
}
#endif /* FILE_MATERIALSSEEN */
| h |
oneAPI-samples | data/projects/oneAPI-samples/common/dpc_common.hpp | // Copyright (C) 2020 Intel Corporation
// SPDX-License-Identifier: MIT
#ifndef _DP_HPP
#define _DP_HPP
#include <stdlib.h>
#include <exception>
#include <sycl/sycl.hpp>
namespace dpc_common {
// This exception handler will catch async exceptions
static auto exception_handler = [](sycl::exception_list eList) {
for (std::exception_ptr const &e : eList) {
try {
std::rethrow_exception(e);
} catch (std::exception const &e) {
#if _DEBUG
std::cout << "Failure" << std::endl;
#endif
std::terminate();
}
}
};
// The TimeInterval is a simple RAII class.
// Construct the timer at the point you want to start timing.
// Use the Elapsed() method to return time since construction.
class TimeInterval {
public:
TimeInterval() : start_(std::chrono::steady_clock::now()) {}
double Elapsed() {
auto now = std::chrono::steady_clock::now();
return std::chrono::duration_cast<Duration>(now - start_).count();
}
private:
using Duration = std::chrono::duration<double>;
std::chrono::steady_clock::time_point start_;
};
}; // namespace dpc_common
#endif
| hpp |
oneAPI-samples | data/projects/oneAPI-samples/common/stb/stb_image.h | /* stb_image - v2.25 - public domain image loader - http://nothings.org/stb
no warranty implied; use at your own risk
Do this:
#define STB_IMAGE_IMPLEMENTATION
before you include this file in *one* C or C++ file to create the implementation.
// i.e. it should look like this:
#include ...
#include ...
#include ...
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
You can #define STBI_ASSERT(x) before the #include to avoid using assert.h.
And #define STBI_MALLOC, STBI_REALLOC, and STBI_FREE to avoid using malloc,realloc,free
QUICK NOTES:
Primarily of interest to game developers and other people who can
avoid problematic images and only need the trivial interface
JPEG baseline & progressive (12 bpc/arithmetic not supported, same as stock IJG lib)
PNG 1/2/4/8/16-bit-per-channel
TGA (not sure what subset, if a subset)
BMP non-1bpp, non-RLE
PSD (composited view only, no extra channels, 8/16 bit-per-channel)
GIF (*comp always reports as 4-channel)
HDR (radiance rgbE format)
PIC (Softimage PIC)
PNM (PPM and PGM binary only)
Animated GIF still needs a proper API, but here's one way to do it:
http://gist.github.com/urraka/685d9a6340b26b830d49
- decode from memory or through FILE (define STBI_NO_STDIO to remove code)
- decode from arbitrary I/O callbacks
- SIMD acceleration on x86/x64 (SSE2) and ARM (NEON)
Full documentation under "DOCUMENTATION" below.
LICENSE
See end of file for license information.
RECENT REVISION HISTORY:
2.25 (2020-02-02) fix warnings
2.24 (2020-02-02) fix warnings; thread-local failure_reason and flip_vertically
2.23 (2019-08-11) fix clang static analysis warning
2.22 (2019-03-04) gif fixes, fix warnings
2.21 (2019-02-25) fix typo in comment
2.20 (2019-02-07) support utf8 filenames in Windows; fix warnings and platform ifdefs
2.19 (2018-02-11) fix warning
2.18 (2018-01-30) fix warnings
2.17 (2018-01-29) bugfix, 1-bit BMP, 16-bitness query, fix warnings
2.16 (2017-07-23) all functions have 16-bit variants; optimizations; bugfixes
2.15 (2017-03-18) fix png-1,2,4; all Imagenet JPGs; no runtime SSE detection on GCC
2.14 (2017-03-03) remove deprecated STBI_JPEG_OLD; fixes for Imagenet JPGs
2.13 (2016-12-04) experimental 16-bit API, only for PNG so far; fixes
2.12 (2016-04-02) fix typo in 2.11 PSD fix that caused crashes
2.11 (2016-04-02) 16-bit PNGS; enable SSE2 in non-gcc x64
RGB-format JPEG; remove white matting in PSD;
allocate large structures on the stack;
correct channel count for PNG & BMP
2.10 (2016-01-22) avoid warning introduced in 2.09
2.09 (2016-01-16) 16-bit TGA; comments in PNM files; STBI_REALLOC_SIZED
See end of file for full revision history.
============================ Contributors =========================
Image formats Extensions, features
Sean Barrett (jpeg, png, bmp) Jetro Lauha (stbi_info)
Nicolas Schulz (hdr, psd) Martin "SpartanJ" Golini (stbi_info)
Jonathan Dummer (tga) James "moose2000" Brown (iPhone PNG)
Jean-Marc Lienher (gif) Ben "Disch" Wenger (io callbacks)
Tom Seddon (pic) Omar Cornut (1/2/4-bit PNG)
Thatcher Ulrich (psd) Nicolas Guillemot (vertical flip)
Ken Miller (pgm, ppm) Richard Mitton (16-bit PSD)
github:urraka (animated gif) Junggon Kim (PNM comments)
Christopher Forseth (animated gif) Daniel Gibson (16-bit TGA)
socks-the-fox (16-bit PNG)
Jeremy Sawicki (handle all ImageNet JPGs)
Optimizations & bugfixes Mikhail Morozov (1-bit BMP)
Fabian "ryg" Giesen Anael Seghezzi (is-16-bit query)
Arseny Kapoulkine
John-Mark Allen
Carmelo J Fdez-Aguera
Bug & warning fixes
Marc LeBlanc David Woo Guillaume George Martins Mozeiko
Christpher Lloyd Jerry Jansson Joseph Thomson Phil Jordan
Dave Moore Roy Eltham Hayaki Saito Nathan Reed
Won Chun Luke Graham Johan Duparc Nick Verigakis
the Horde3D community Thomas Ruf Ronny Chevalier github:rlyeh
Janez Zemva John Bartholomew Michal Cichon github:romigrou
Jonathan Blow Ken Hamada Tero Hanninen github:svdijk
Laurent Gomila Cort Stratton Sergio Gonzalez github:snagar
Aruelien Pocheville Thibault Reuille Cass Everitt github:Zelex
Ryamond Barbiero Paul Du Bois Engin Manap github:grim210
Aldo Culquicondor Philipp Wiesemann Dale Weiler github:sammyhw
Oriol Ferrer Mesia Josh Tobin Matthew Gregan github:phprus
Julian Raschke Gregory Mullen Baldur Karlsson github:poppolopoppo
Christian Floisand Kevin Schmidt JR Smith github:darealshinji
Brad Weinberger Matvey Cherevko github:Michaelangel007
Blazej Dariusz Roszkowski Alexander Veselov
*/
#ifndef STBI_INCLUDE_STB_IMAGE_H
#define STBI_INCLUDE_STB_IMAGE_H
// DOCUMENTATION
//
// Limitations:
// - no 12-bit-per-channel JPEG
// - no JPEGs with arithmetic coding
// - GIF always returns *comp=4
//
// Basic usage (see HDR discussion below for HDR usage):
// int x,y,n;
// unsigned char *data = stbi_load(filename, &x, &y, &n, 0);
// // ... process data if not NULL ...
// // ... x = width, y = height, n = # 8-bit components per pixel ...
// // ... replace '0' with '1'..'4' to force that many components per pixel
// // ... but 'n' will always be the number that it would have been if you said 0
// stbi_image_free(data)
//
// Standard parameters:
// int *x -- outputs image width in pixels
// int *y -- outputs image height in pixels
// int *channels_in_file -- outputs # of image components in image file
// int desired_channels -- if non-zero, # of image components requested in result
//
// The return value from an image loader is an 'unsigned char *' which points
// to the pixel data, or NULL on an allocation failure or if the image is
// corrupt or invalid. The pixel data consists of *y scanlines of *x pixels,
// with each pixel consisting of N interleaved 8-bit components; the first
// pixel pointed to is top-left-most in the image. There is no padding between
// image scanlines or between pixels, regardless of format. The number of
// components N is 'desired_channels' if desired_channels is non-zero, or
// *channels_in_file otherwise. If desired_channels is non-zero,
// *channels_in_file has the number of components that _would_ have been
// output otherwise. E.g. if you set desired_channels to 4, you will always
// get RGBA output, but you can check *channels_in_file to see if it's trivially
// opaque because e.g. there were only 3 channels in the source image.
//
// An output image with N components has the following components interleaved
// in this order in each pixel:
//
// N=#comp components
// 1 grey
// 2 grey, alpha
// 3 red, green, blue
// 4 red, green, blue, alpha
//
// If image loading fails for any reason, the return value will be NULL,
// and *x, *y, *channels_in_file will be unchanged. The function
// stbi_failure_reason() can be queried for an extremely brief, end-user
// unfriendly explanation of why the load failed. Define STBI_NO_FAILURE_STRINGS
// to avoid compiling these strings at all, and STBI_FAILURE_USERMSG to get slightly
// more user-friendly ones.
//
// Paletted PNG, BMP, GIF, and PIC images are automatically depalettized.
//
// ===========================================================================
//
// UNICODE:
//
// If compiling for Windows and you wish to use Unicode filenames, compile
// with
// #define STBI_WINDOWS_UTF8
// and pass utf8-encoded filenames. Call stbi_convert_wchar_to_utf8 to convert
// Windows wchar_t filenames to utf8.
//
// ===========================================================================
//
// Philosophy
//
// stb libraries are designed with the following priorities:
//
// 1. easy to use
// 2. easy to maintain
// 3. good performance
//
// Sometimes I let "good performance" creep up in priority over "easy to maintain",
// and for best performance I may provide less-easy-to-use APIs that give higher
// performance, in addition to the easy-to-use ones. Nevertheless, it's important
// to keep in mind that from the standpoint of you, a client of this library,
// all you care about is #1 and #3, and stb libraries DO NOT emphasize #3 above all.
//
// Some secondary priorities arise directly from the first two, some of which
// provide more explicit reasons why performance can't be emphasized.
//
// - Portable ("ease of use")
// - Small source code footprint ("easy to maintain")
// - No dependencies ("ease of use")
//
// ===========================================================================
//
// I/O callbacks
//
// I/O callbacks allow you to read from arbitrary sources, like packaged
// files or some other source. Data read from callbacks are processed
// through a small internal buffer (currently 128 bytes) to try to reduce
// overhead.
//
// The three functions you must define are "read" (reads some bytes of data),
// "skip" (skips some bytes of data), "eof" (reports if the stream is at the end).
//
// ===========================================================================
//
// SIMD support
//
// The JPEG decoder will try to automatically use SIMD kernels on x86 when
// supported by the compiler. For ARM Neon support, you must explicitly
// request it.
//
// (The old do-it-yourself SIMD API is no longer supported in the current
// code.)
//
// On x86, SSE2 will automatically be used when available based on a run-time
// test; if not, the generic C versions are used as a fall-back. On ARM targets,
// the typical path is to have separate builds for NEON and non-NEON devices
// (at least this is true for iOS and Android). Therefore, the NEON support is
// toggled by a build flag: define STBI_NEON to get NEON loops.
//
// If for some reason you do not want to use any of SIMD code, or if
// you have issues compiling it, you can disable it entirely by
// defining STBI_NO_SIMD.
//
// ===========================================================================
//
// HDR image support (disable by defining STBI_NO_HDR)
//
// stb_image supports loading HDR images in general, and currently the Radiance
// .HDR file format specifically. You can still load any file through the existing
// interface; if you attempt to load an HDR file, it will be automatically remapped
// to LDR, assuming gamma 2.2 and an arbitrary scale factor defaulting to 1;
// both of these constants can be reconfigured through this interface:
//
// stbi_hdr_to_ldr_gamma(2.2f);
// stbi_hdr_to_ldr_scale(1.0f);
//
// (note, do not use _inverse_ constants; stbi_image will invert them
// appropriately).
//
// Additionally, there is a new, parallel interface for loading files as
// (linear) floats to preserve the full dynamic range:
//
// float *data = stbi_loadf(filename, &x, &y, &n, 0);
//
// If you load LDR images through this interface, those images will
// be promoted to floating point values, run through the inverse of
// constants corresponding to the above:
//
// stbi_ldr_to_hdr_scale(1.0f);
// stbi_ldr_to_hdr_gamma(2.2f);
//
// Finally, given a filename (or an open file or memory block--see header
// file for details) containing image data, you can query for the "most
// appropriate" interface to use (that is, whether the image is HDR or
// not), using:
//
// stbi_is_hdr(char *filename);
//
// ===========================================================================
//
// iPhone PNG support:
//
// By default we convert iphone-formatted PNGs back to RGB, even though
// they are internally encoded differently. You can disable this conversion
// by calling stbi_convert_iphone_png_to_rgb(0), in which case
// you will always just get the native iphone "format" through (which
// is BGR stored in RGB).
//
// Call stbi_set_unpremultiply_on_load(1) as well to force a divide per
// pixel to remove any premultiplied alpha *only* if the image file explicitly
// says there's premultiplied data (currently only happens in iPhone images,
// and only if iPhone convert-to-rgb processing is on).
//
// ===========================================================================
//
// ADDITIONAL CONFIGURATION
//
// - You can suppress implementation of any of the decoders to reduce
// your code footprint by #defining one or more of the following
// symbols before creating the implementation.
//
// STBI_NO_JPEG
// STBI_NO_PNG
// STBI_NO_BMP
// STBI_NO_PSD
// STBI_NO_TGA
// STBI_NO_GIF
// STBI_NO_HDR
// STBI_NO_PIC
// STBI_NO_PNM (.ppm and .pgm)
//
// - You can request *only* certain decoders and suppress all other ones
// (this will be more forward-compatible, as addition of new decoders
// doesn't require you to disable them explicitly):
//
// STBI_ONLY_JPEG
// STBI_ONLY_PNG
// STBI_ONLY_BMP
// STBI_ONLY_PSD
// STBI_ONLY_TGA
// STBI_ONLY_GIF
// STBI_ONLY_HDR
// STBI_ONLY_PIC
// STBI_ONLY_PNM (.ppm and .pgm)
//
// - If you use STBI_NO_PNG (or _ONLY_ without PNG), and you still
// want the zlib decoder to be available, #define STBI_SUPPORT_ZLIB
//
#ifndef STBI_NO_STDIO
#include <stdio.h>
#endif // STBI_NO_STDIO
#define STBI_VERSION 1
enum
{
STBI_default = 0, // only used for desired_channels
STBI_grey = 1,
STBI_grey_alpha = 2,
STBI_rgb = 3,
STBI_rgb_alpha = 4
};
#include <stdlib.h>
typedef unsigned char stbi_uc;
typedef unsigned short stbi_us;
#ifdef __cplusplus
extern "C" {
#endif
#ifndef STBIDEF
#ifdef STB_IMAGE_STATIC
#define STBIDEF static
#else
#define STBIDEF extern
#endif
#endif
//////////////////////////////////////////////////////////////////////////////
//
// PRIMARY API - works on images of any type
//
//
// load image by filename, open file, or memory buffer
//
typedef struct
{
int (*read) (void *user,char *data,int size); // fill 'data' with 'size' bytes. return number of bytes actually read
void (*skip) (void *user,int n); // skip the next 'n' bytes, or 'unget' the last -n bytes if negative
int (*eof) (void *user); // returns nonzero if we are at end of file/data
} stbi_io_callbacks;
////////////////////////////////////
//
// 8-bits-per-channel interface
//
STBIDEF stbi_uc *stbi_load_from_memory (stbi_uc const *buffer, int len , int *x, int *y, int *channels_in_file, int desired_channels);
STBIDEF stbi_uc *stbi_load_from_callbacks(stbi_io_callbacks const *clbk , void *user, int *x, int *y, int *channels_in_file, int desired_channels);
#ifndef STBI_NO_STDIO
STBIDEF stbi_uc *stbi_load (char const *filename, int *x, int *y, int *channels_in_file, int desired_channels);
STBIDEF stbi_uc *stbi_load_from_file (FILE *f, int *x, int *y, int *channels_in_file, int desired_channels);
// for stbi_load_from_file, file pointer is left pointing immediately after image
#endif
#ifndef STBI_NO_GIF
STBIDEF stbi_uc *stbi_load_gif_from_memory(stbi_uc const *buffer, int len, int **delays, int *x, int *y, int *z, int *comp, int req_comp);
#endif
#ifdef STBI_WINDOWS_UTF8
STBIDEF int stbi_convert_wchar_to_utf8(char *buffer, size_t bufferlen, const wchar_t* input);
#endif
////////////////////////////////////
//
// 16-bits-per-channel interface
//
STBIDEF stbi_us *stbi_load_16_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *channels_in_file, int desired_channels);
STBIDEF stbi_us *stbi_load_16_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *channels_in_file, int desired_channels);
#ifndef STBI_NO_STDIO
STBIDEF stbi_us *stbi_load_16 (char const *filename, int *x, int *y, int *channels_in_file, int desired_channels);
STBIDEF stbi_us *stbi_load_from_file_16(FILE *f, int *x, int *y, int *channels_in_file, int desired_channels);
#endif
////////////////////////////////////
//
// float-per-channel interface
//
#ifndef STBI_NO_LINEAR
STBIDEF float *stbi_loadf_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *channels_in_file, int desired_channels);
STBIDEF float *stbi_loadf_from_callbacks (stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *channels_in_file, int desired_channels);
#ifndef STBI_NO_STDIO
STBIDEF float *stbi_loadf (char const *filename, int *x, int *y, int *channels_in_file, int desired_channels);
STBIDEF float *stbi_loadf_from_file (FILE *f, int *x, int *y, int *channels_in_file, int desired_channels);
#endif
#endif
#ifndef STBI_NO_HDR
STBIDEF void stbi_hdr_to_ldr_gamma(float gamma);
STBIDEF void stbi_hdr_to_ldr_scale(float scale);
#endif // STBI_NO_HDR
#ifndef STBI_NO_LINEAR
STBIDEF void stbi_ldr_to_hdr_gamma(float gamma);
STBIDEF void stbi_ldr_to_hdr_scale(float scale);
#endif // STBI_NO_LINEAR
// stbi_is_hdr is always defined, but always returns false if STBI_NO_HDR
STBIDEF int stbi_is_hdr_from_callbacks(stbi_io_callbacks const *clbk, void *user);
STBIDEF int stbi_is_hdr_from_memory(stbi_uc const *buffer, int len);
#ifndef STBI_NO_STDIO
STBIDEF int stbi_is_hdr (char const *filename);
STBIDEF int stbi_is_hdr_from_file(FILE *f);
#endif // STBI_NO_STDIO
// get a VERY brief reason for failure
// on most compilers (and ALL modern mainstream compilers) this is threadsafe
STBIDEF const char *stbi_failure_reason (void);
// free the loaded image -- this is just free()
STBIDEF void stbi_image_free (void *retval_from_stbi_load);
// get image dimensions & components without fully decoding
STBIDEF int stbi_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp);
STBIDEF int stbi_info_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp);
STBIDEF int stbi_is_16_bit_from_memory(stbi_uc const *buffer, int len);
STBIDEF int stbi_is_16_bit_from_callbacks(stbi_io_callbacks const *clbk, void *user);
#ifndef STBI_NO_STDIO
STBIDEF int stbi_info (char const *filename, int *x, int *y, int *comp);
STBIDEF int stbi_info_from_file (FILE *f, int *x, int *y, int *comp);
STBIDEF int stbi_is_16_bit (char const *filename);
STBIDEF int stbi_is_16_bit_from_file(FILE *f);
#endif
// for image formats that explicitly notate that they have premultiplied alpha,
// we just return the colors as stored in the file. set this flag to force
// unpremultiplication. results are undefined if the unpremultiply overflow.
STBIDEF void stbi_set_unpremultiply_on_load(int flag_true_if_should_unpremultiply);
// indicate whether we should process iphone images back to canonical format,
// or just pass them through "as-is"
STBIDEF void stbi_convert_iphone_png_to_rgb(int flag_true_if_should_convert);
// flip the image vertically, so the first pixel in the output array is the bottom left
STBIDEF void stbi_set_flip_vertically_on_load(int flag_true_if_should_flip);
// as above, but only applies to images loaded on the thread that calls the function
// this function is only available if your compiler supports thread-local variables;
// calling it will fail to link if your compiler doesn't
STBIDEF void stbi_set_flip_vertically_on_load_thread(int flag_true_if_should_flip);
// ZLIB client - used by PNG, available for other purposes
STBIDEF char *stbi_zlib_decode_malloc_guesssize(const char *buffer, int len, int initial_size, int *outlen);
STBIDEF char *stbi_zlib_decode_malloc_guesssize_headerflag(const char *buffer, int len, int initial_size, int *outlen, int parse_header);
STBIDEF char *stbi_zlib_decode_malloc(const char *buffer, int len, int *outlen);
STBIDEF int stbi_zlib_decode_buffer(char *obuffer, int olen, const char *ibuffer, int ilen);
STBIDEF char *stbi_zlib_decode_noheader_malloc(const char *buffer, int len, int *outlen);
STBIDEF int stbi_zlib_decode_noheader_buffer(char *obuffer, int olen, const char *ibuffer, int ilen);
#ifdef __cplusplus
}
#endif
//
//
//// end header file /////////////////////////////////////////////////////
#endif // STBI_INCLUDE_STB_IMAGE_H
#ifdef STB_IMAGE_IMPLEMENTATION
#if defined(STBI_ONLY_JPEG) || defined(STBI_ONLY_PNG) || defined(STBI_ONLY_BMP) \
|| defined(STBI_ONLY_TGA) || defined(STBI_ONLY_GIF) || defined(STBI_ONLY_PSD) \
|| defined(STBI_ONLY_HDR) || defined(STBI_ONLY_PIC) || defined(STBI_ONLY_PNM) \
|| defined(STBI_ONLY_ZLIB)
#ifndef STBI_ONLY_JPEG
#define STBI_NO_JPEG
#endif
#ifndef STBI_ONLY_PNG
#define STBI_NO_PNG
#endif
#ifndef STBI_ONLY_BMP
#define STBI_NO_BMP
#endif
#ifndef STBI_ONLY_PSD
#define STBI_NO_PSD
#endif
#ifndef STBI_ONLY_TGA
#define STBI_NO_TGA
#endif
#ifndef STBI_ONLY_GIF
#define STBI_NO_GIF
#endif
#ifndef STBI_ONLY_HDR
#define STBI_NO_HDR
#endif
#ifndef STBI_ONLY_PIC
#define STBI_NO_PIC
#endif
#ifndef STBI_ONLY_PNM
#define STBI_NO_PNM
#endif
#endif
#if defined(STBI_NO_PNG) && !defined(STBI_SUPPORT_ZLIB) && !defined(STBI_NO_ZLIB)
#define STBI_NO_ZLIB
#endif
#include <stdarg.h>
#include <stddef.h> // ptrdiff_t on osx
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#if !defined(STBI_NO_LINEAR) || !defined(STBI_NO_HDR)
#include <math.h> // ldexp, pow
#endif
#ifndef STBI_NO_STDIO
#include <stdio.h>
#endif
#ifndef STBI_ASSERT
#include <assert.h>
#define STBI_ASSERT(x) assert(x)
#endif
#ifdef __cplusplus
#define STBI_EXTERN extern "C"
#else
#define STBI_EXTERN extern
#endif
#ifndef _MSC_VER
#ifdef __cplusplus
#define stbi_inline inline
#else
#define stbi_inline
#endif
#else
#define stbi_inline __forceinline
#endif
#ifndef STBI_NO_THREAD_LOCALS
#if defined(__cplusplus) && __cplusplus >= 201103L
#define STBI_THREAD_LOCAL thread_local
#elif defined (__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
#define STBI_THREAD_LOCAL _Thread_local
#elif defined(__GNUC__)
#define STBI_THREAD_LOCAL __thread
#elif defined(_MSC_VER)
#define STBI_THREAD_LOCAL __declspec(thread)
#endif
#endif
#ifdef _MSC_VER
typedef unsigned short stbi__uint16;
typedef signed short stbi__int16;
typedef unsigned int stbi__uint32;
typedef signed int stbi__int32;
#else
#include <stdint.h>
typedef uint16_t stbi__uint16;
typedef int16_t stbi__int16;
typedef uint32_t stbi__uint32;
typedef int32_t stbi__int32;
#endif
// should produce compiler error if size is wrong
typedef unsigned char validate_uint32[sizeof(stbi__uint32)==4 ? 1 : -1];
#ifdef _MSC_VER
#define STBI_NOTUSED(v) (void)(v)
#else
#define STBI_NOTUSED(v) (void)sizeof(v)
#endif
#ifdef _MSC_VER
#define STBI_HAS_LROTL
#endif
#ifdef STBI_HAS_LROTL
#define stbi_lrot(x,y) _lrotl(x,y)
#else
#define stbi_lrot(x,y) (((x) << (y)) | ((x) >> (32 - (y))))
#endif
#if defined(STBI_MALLOC) && defined(STBI_FREE) && (defined(STBI_REALLOC) || defined(STBI_REALLOC_SIZED))
// ok
#elif !defined(STBI_MALLOC) && !defined(STBI_FREE) && !defined(STBI_REALLOC) && !defined(STBI_REALLOC_SIZED)
// ok
#else
#error "Must define all or none of STBI_MALLOC, STBI_FREE, and STBI_REALLOC (or STBI_REALLOC_SIZED)."
#endif
#ifndef STBI_MALLOC
#define STBI_MALLOC(sz) malloc(sz)
#define STBI_REALLOC(p,newsz) realloc(p,newsz)
#define STBI_FREE(p) free(p)
#endif
#ifndef STBI_REALLOC_SIZED
#define STBI_REALLOC_SIZED(p,oldsz,newsz) STBI_REALLOC(p,newsz)
#endif
// x86/x64 detection
#if defined(__x86_64__) || defined(_M_X64)
#define STBI__X64_TARGET
#elif defined(__i386) || defined(_M_IX86)
#define STBI__X86_TARGET
#endif
#if defined(__GNUC__) && defined(STBI__X86_TARGET) && !defined(__SSE2__) && !defined(STBI_NO_SIMD)
// gcc doesn't support sse2 intrinsics unless you compile with -msse2,
// which in turn means it gets to use SSE2 everywhere. This is unfortunate,
// but previous attempts to provide the SSE2 functions with runtime
// detection caused numerous issues. The way architecture extensions are
// exposed in GCC/Clang is, sadly, not really suited for one-file libs.
// New behavior: if compiled with -msse2, we use SSE2 without any
// detection; if not, we don't use it at all.
#define STBI_NO_SIMD
#endif
#if defined(__MINGW32__) && defined(STBI__X86_TARGET) && !defined(STBI_MINGW_ENABLE_SSE2) && !defined(STBI_NO_SIMD)
// Note that __MINGW32__ doesn't actually mean 32-bit, so we have to avoid STBI__X64_TARGET
//
// 32-bit MinGW wants ESP to be 16-byte aligned, but this is not in the
// Windows ABI and VC++ as well as Windows DLLs don't maintain that invariant.
// As a result, enabling SSE2 on 32-bit MinGW is dangerous when not
// simultaneously enabling "-mstackrealign".
//
// See https://github.com/nothings/stb/issues/81 for more information.
//
// So default to no SSE2 on 32-bit MinGW. If you've read this far and added
// -mstackrealign to your build settings, feel free to #define STBI_MINGW_ENABLE_SSE2.
#define STBI_NO_SIMD
#endif
#if !defined(STBI_NO_SIMD) && (defined(STBI__X86_TARGET) || defined(STBI__X64_TARGET))
#define STBI_SSE2
#include <emmintrin.h>
#ifdef _MSC_VER
#if _MSC_VER >= 1400 // not VC6
#include <intrin.h> // __cpuid
static int stbi__cpuid3(void)
{
int info[4];
__cpuid(info,1);
return info[3];
}
#else
static int stbi__cpuid3(void)
{
int res;
__asm {
mov eax,1
cpuid
mov res,edx
}
return res;
}
#endif
#define STBI_SIMD_ALIGN(type, name) __declspec(align(16)) type name
#if !defined(STBI_NO_JPEG) && defined(STBI_SSE2)
static int stbi__sse2_available(void)
{
int info3 = stbi__cpuid3();
return ((info3 >> 26) & 1) != 0;
}
#endif
#else // assume GCC-style if not VC++
#define STBI_SIMD_ALIGN(type, name) type name __attribute__((aligned(16)))
#if !defined(STBI_NO_JPEG) && defined(STBI_SSE2)
static int stbi__sse2_available(void)
{
// If we're even attempting to compile this on GCC/Clang, that means
// -msse2 is on, which means the compiler is allowed to use SSE2
// instructions at will, and so are we.
return 1;
}
#endif
#endif
#endif
// ARM NEON
#if defined(STBI_NO_SIMD) && defined(STBI_NEON)
#undef STBI_NEON
#endif
#ifdef STBI_NEON
#include <arm_neon.h>
// assume GCC or Clang on ARM targets
#define STBI_SIMD_ALIGN(type, name) type name __attribute__((aligned(16)))
#endif
#ifndef STBI_SIMD_ALIGN
#define STBI_SIMD_ALIGN(type, name) type name
#endif
///////////////////////////////////////////////
//
// stbi__context struct and start_xxx functions
// stbi__context structure is our basic context used by all images, so it
// contains all the IO context, plus some basic image information
typedef struct
{
stbi__uint32 img_x, img_y;
int img_n, img_out_n;
stbi_io_callbacks io;
void *io_user_data;
int read_from_callbacks;
int buflen;
stbi_uc buffer_start[128];
stbi_uc *img_buffer, *img_buffer_end;
stbi_uc *img_buffer_original, *img_buffer_original_end;
} stbi__context;
static void stbi__refill_buffer(stbi__context *s);
// initialize a memory-decode context
static void stbi__start_mem(stbi__context *s, stbi_uc const *buffer, int len)
{
s->io.read = NULL;
s->read_from_callbacks = 0;
s->img_buffer = s->img_buffer_original = (stbi_uc *) buffer;
s->img_buffer_end = s->img_buffer_original_end = (stbi_uc *) buffer+len;
}
// initialize a callback-based context
static void stbi__start_callbacks(stbi__context *s, stbi_io_callbacks *c, void *user)
{
s->io = *c;
s->io_user_data = user;
s->buflen = sizeof(s->buffer_start);
s->read_from_callbacks = 1;
s->img_buffer_original = s->buffer_start;
stbi__refill_buffer(s);
s->img_buffer_original_end = s->img_buffer_end;
}
#ifndef STBI_NO_STDIO
static int stbi__stdio_read(void *user, char *data, int size)
{
return (int) fread(data,1,size,(FILE*) user);
}
static void stbi__stdio_skip(void *user, int n)
{
fseek((FILE*) user, n, SEEK_CUR);
}
static int stbi__stdio_eof(void *user)
{
return feof((FILE*) user);
}
static stbi_io_callbacks stbi__stdio_callbacks =
{
stbi__stdio_read,
stbi__stdio_skip,
stbi__stdio_eof,
};
static void stbi__start_file(stbi__context *s, FILE *f)
{
stbi__start_callbacks(s, &stbi__stdio_callbacks, (void *) f);
}
//static void stop_file(stbi__context *s) { }
#endif // !STBI_NO_STDIO
static void stbi__rewind(stbi__context *s)
{
// conceptually rewind SHOULD rewind to the beginning of the stream,
// but we just rewind to the beginning of the initial buffer, because
// we only use it after doing 'test', which only ever looks at at most 92 bytes
s->img_buffer = s->img_buffer_original;
s->img_buffer_end = s->img_buffer_original_end;
}
enum
{
STBI_ORDER_RGB,
STBI_ORDER_BGR
};
typedef struct
{
int bits_per_channel;
int num_channels;
int channel_order;
} stbi__result_info;
#ifndef STBI_NO_JPEG
static int stbi__jpeg_test(stbi__context *s);
static void *stbi__jpeg_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri);
static int stbi__jpeg_info(stbi__context *s, int *x, int *y, int *comp);
#endif
#ifndef STBI_NO_PNG
static int stbi__png_test(stbi__context *s);
static void *stbi__png_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri);
static int stbi__png_info(stbi__context *s, int *x, int *y, int *comp);
static int stbi__png_is16(stbi__context *s);
#endif
#ifndef STBI_NO_BMP
static int stbi__bmp_test(stbi__context *s);
static void *stbi__bmp_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri);
static int stbi__bmp_info(stbi__context *s, int *x, int *y, int *comp);
#endif
#ifndef STBI_NO_TGA
static int stbi__tga_test(stbi__context *s);
static void *stbi__tga_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri);
static int stbi__tga_info(stbi__context *s, int *x, int *y, int *comp);
#endif
#ifndef STBI_NO_PSD
static int stbi__psd_test(stbi__context *s);
static void *stbi__psd_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri, int bpc);
static int stbi__psd_info(stbi__context *s, int *x, int *y, int *comp);
static int stbi__psd_is16(stbi__context *s);
#endif
#ifndef STBI_NO_HDR
static int stbi__hdr_test(stbi__context *s);
static float *stbi__hdr_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri);
static int stbi__hdr_info(stbi__context *s, int *x, int *y, int *comp);
#endif
#ifndef STBI_NO_PIC
static int stbi__pic_test(stbi__context *s);
static void *stbi__pic_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri);
static int stbi__pic_info(stbi__context *s, int *x, int *y, int *comp);
#endif
#ifndef STBI_NO_GIF
static int stbi__gif_test(stbi__context *s);
static void *stbi__gif_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri);
static void *stbi__load_gif_main(stbi__context *s, int **delays, int *x, int *y, int *z, int *comp, int req_comp);
static int stbi__gif_info(stbi__context *s, int *x, int *y, int *comp);
#endif
#ifndef STBI_NO_PNM
static int stbi__pnm_test(stbi__context *s);
static void *stbi__pnm_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri);
static int stbi__pnm_info(stbi__context *s, int *x, int *y, int *comp);
#endif
static
#ifdef STBI_THREAD_LOCAL
STBI_THREAD_LOCAL
#endif
const char *stbi__g_failure_reason;
STBIDEF const char *stbi_failure_reason(void)
{
return stbi__g_failure_reason;
}
#ifndef STBI_NO_FAILURE_STRINGS
static int stbi__err(const char *str)
{
stbi__g_failure_reason = str;
return 0;
}
#endif
static void *stbi__malloc(size_t size)
{
return STBI_MALLOC(size);
}
// stb_image uses ints pervasively, including for offset calculations.
// therefore the largest decoded image size we can support with the
// current code, even on 64-bit targets, is INT_MAX. this is not a
// significant limitation for the intended use case.
//
// we do, however, need to make sure our size calculations don't
// overflow. hence a few helper functions for size calculations that
// multiply integers together, making sure that they're non-negative
// and no overflow occurs.
// return 1 if the sum is valid, 0 on overflow.
// negative terms are considered invalid.
static int stbi__addsizes_valid(int a, int b)
{
if (b < 0) return 0;
// now 0 <= b <= INT_MAX, hence also
// 0 <= INT_MAX - b <= INTMAX.
// And "a + b <= INT_MAX" (which might overflow) is the
// same as a <= INT_MAX - b (no overflow)
return a <= INT_MAX - b;
}
// returns 1 if the product is valid, 0 on overflow.
// negative factors are considered invalid.
static int stbi__mul2sizes_valid(int a, int b)
{
if (a < 0 || b < 0) return 0;
if (b == 0) return 1; // mul-by-0 is always safe
// portable way to check for no overflows in a*b
return a <= INT_MAX/b;
}
#if !defined(STBI_NO_JPEG) || !defined(STBI_NO_PNG) || !defined(STBI_NO_TGA) || !defined(STBI_NO_HDR)
// returns 1 if "a*b + add" has no negative terms/factors and doesn't overflow
static int stbi__mad2sizes_valid(int a, int b, int add)
{
return stbi__mul2sizes_valid(a, b) && stbi__addsizes_valid(a*b, add);
}
#endif
// returns 1 if "a*b*c + add" has no negative terms/factors and doesn't overflow
static int stbi__mad3sizes_valid(int a, int b, int c, int add)
{
return stbi__mul2sizes_valid(a, b) && stbi__mul2sizes_valid(a*b, c) &&
stbi__addsizes_valid(a*b*c, add);
}
// returns 1 if "a*b*c*d + add" has no negative terms/factors and doesn't overflow
#if !defined(STBI_NO_LINEAR) || !defined(STBI_NO_HDR)
static int stbi__mad4sizes_valid(int a, int b, int c, int d, int add)
{
return stbi__mul2sizes_valid(a, b) && stbi__mul2sizes_valid(a*b, c) &&
stbi__mul2sizes_valid(a*b*c, d) && stbi__addsizes_valid(a*b*c*d, add);
}
#endif
#if !defined(STBI_NO_JPEG) || !defined(STBI_NO_PNG) || !defined(STBI_NO_TGA) || !defined(STBI_NO_HDR)
// mallocs with size overflow checking
static void *stbi__malloc_mad2(int a, int b, int add)
{
if (!stbi__mad2sizes_valid(a, b, add)) return NULL;
return stbi__malloc(a*b + add);
}
#endif
static void *stbi__malloc_mad3(int a, int b, int c, int add)
{
if (!stbi__mad3sizes_valid(a, b, c, add)) return NULL;
return stbi__malloc(a*b*c + add);
}
#if !defined(STBI_NO_LINEAR) || !defined(STBI_NO_HDR)
static void *stbi__malloc_mad4(int a, int b, int c, int d, int add)
{
if (!stbi__mad4sizes_valid(a, b, c, d, add)) return NULL;
return stbi__malloc(a*b*c*d + add);
}
#endif
// stbi__err - error
// stbi__errpf - error returning pointer to float
// stbi__errpuc - error returning pointer to unsigned char
#ifdef STBI_NO_FAILURE_STRINGS
#define stbi__err(x,y) 0
#elif defined(STBI_FAILURE_USERMSG)
#define stbi__err(x,y) stbi__err(y)
#else
#define stbi__err(x,y) stbi__err(x)
#endif
#define stbi__errpf(x,y) ((float *)(size_t) (stbi__err(x,y)?NULL:NULL))
#define stbi__errpuc(x,y) ((unsigned char *)(size_t) (stbi__err(x,y)?NULL:NULL))
STBIDEF void stbi_image_free(void *retval_from_stbi_load)
{
STBI_FREE(retval_from_stbi_load);
}
#ifndef STBI_NO_LINEAR
static float *stbi__ldr_to_hdr(stbi_uc *data, int x, int y, int comp);
#endif
#ifndef STBI_NO_HDR
static stbi_uc *stbi__hdr_to_ldr(float *data, int x, int y, int comp);
#endif
static int stbi__vertically_flip_on_load_global = 0;
STBIDEF void stbi_set_flip_vertically_on_load(int flag_true_if_should_flip)
{
stbi__vertically_flip_on_load_global = flag_true_if_should_flip;
}
#ifndef STBI_THREAD_LOCAL
#define stbi__vertically_flip_on_load stbi__vertically_flip_on_load_global
#else
static STBI_THREAD_LOCAL int stbi__vertically_flip_on_load_local, stbi__vertically_flip_on_load_set;
STBIDEF void stbi_set_flip_vertically_on_load_thread(int flag_true_if_should_flip)
{
stbi__vertically_flip_on_load_local = flag_true_if_should_flip;
stbi__vertically_flip_on_load_set = 1;
}
#define stbi__vertically_flip_on_load (stbi__vertically_flip_on_load_set \
? stbi__vertically_flip_on_load_local \
: stbi__vertically_flip_on_load_global)
#endif // STBI_THREAD_LOCAL
static void *stbi__load_main(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri, int bpc)
{
memset(ri, 0, sizeof(*ri)); // make sure it's initialized if we add new fields
ri->bits_per_channel = 8; // default is 8 so most paths don't have to be changed
ri->channel_order = STBI_ORDER_RGB; // all current input & output are this, but this is here so we can add BGR order
ri->num_channels = 0;
#ifndef STBI_NO_JPEG
if (stbi__jpeg_test(s)) return stbi__jpeg_load(s,x,y,comp,req_comp, ri);
#endif
#ifndef STBI_NO_PNG
if (stbi__png_test(s)) return stbi__png_load(s,x,y,comp,req_comp, ri);
#endif
#ifndef STBI_NO_BMP
if (stbi__bmp_test(s)) return stbi__bmp_load(s,x,y,comp,req_comp, ri);
#endif
#ifndef STBI_NO_GIF
if (stbi__gif_test(s)) return stbi__gif_load(s,x,y,comp,req_comp, ri);
#endif
#ifndef STBI_NO_PSD
if (stbi__psd_test(s)) return stbi__psd_load(s,x,y,comp,req_comp, ri, bpc);
#else
STBI_NOTUSED(bpc);
#endif
#ifndef STBI_NO_PIC
if (stbi__pic_test(s)) return stbi__pic_load(s,x,y,comp,req_comp, ri);
#endif
#ifndef STBI_NO_PNM
if (stbi__pnm_test(s)) return stbi__pnm_load(s,x,y,comp,req_comp, ri);
#endif
#ifndef STBI_NO_HDR
if (stbi__hdr_test(s)) {
float *hdr = stbi__hdr_load(s, x,y,comp,req_comp, ri);
return stbi__hdr_to_ldr(hdr, *x, *y, req_comp ? req_comp : *comp);
}
#endif
#ifndef STBI_NO_TGA
// test tga last because it's a crappy test!
if (stbi__tga_test(s))
return stbi__tga_load(s,x,y,comp,req_comp, ri);
#endif
return stbi__errpuc("unknown image type", "Image not of any known type, or corrupt");
}
static stbi_uc *stbi__convert_16_to_8(stbi__uint16 *orig, int w, int h, int channels)
{
int i;
int img_len = w * h * channels;
stbi_uc *reduced;
reduced = (stbi_uc *) stbi__malloc(img_len);
if (reduced == NULL) return stbi__errpuc("outofmem", "Out of memory");
for (i = 0; i < img_len; ++i)
reduced[i] = (stbi_uc)((orig[i] >> 8) & 0xFF); // top half of each byte is sufficient approx of 16->8 bit scaling
STBI_FREE(orig);
return reduced;
}
static stbi__uint16 *stbi__convert_8_to_16(stbi_uc *orig, int w, int h, int channels)
{
int i;
int img_len = w * h * channels;
stbi__uint16 *enlarged;
enlarged = (stbi__uint16 *) stbi__malloc(img_len*2);
if (enlarged == NULL) return (stbi__uint16 *) stbi__errpuc("outofmem", "Out of memory");
for (i = 0; i < img_len; ++i)
enlarged[i] = (stbi__uint16)((orig[i] << 8) + orig[i]); // replicate to high and low byte, maps 0->0, 255->0xffff
STBI_FREE(orig);
return enlarged;
}
static void stbi__vertical_flip(void *image, int w, int h, int bytes_per_pixel)
{
int row;
size_t bytes_per_row = (size_t)w * bytes_per_pixel;
stbi_uc temp[2048];
stbi_uc *bytes = (stbi_uc *)image;
for (row = 0; row < (h>>1); row++) {
stbi_uc *row0 = bytes + row*bytes_per_row;
stbi_uc *row1 = bytes + (h - row - 1)*bytes_per_row;
// swap row0 with row1
size_t bytes_left = bytes_per_row;
while (bytes_left) {
size_t bytes_copy = (bytes_left < sizeof(temp)) ? bytes_left : sizeof(temp);
memcpy(temp, row0, bytes_copy);
memcpy(row0, row1, bytes_copy);
memcpy(row1, temp, bytes_copy);
row0 += bytes_copy;
row1 += bytes_copy;
bytes_left -= bytes_copy;
}
}
}
#ifndef STBI_NO_GIF
static void stbi__vertical_flip_slices(void *image, int w, int h, int z, int bytes_per_pixel)
{
int slice;
int slice_size = w * h * bytes_per_pixel;
stbi_uc *bytes = (stbi_uc *)image;
for (slice = 0; slice < z; ++slice) {
stbi__vertical_flip(bytes, w, h, bytes_per_pixel);
bytes += slice_size;
}
}
#endif
static unsigned char *stbi__load_and_postprocess_8bit(stbi__context *s, int *x, int *y, int *comp, int req_comp)
{
stbi__result_info ri;
void *result = stbi__load_main(s, x, y, comp, req_comp, &ri, 8);
if (result == NULL)
return NULL;
if (ri.bits_per_channel != 8) {
STBI_ASSERT(ri.bits_per_channel == 16);
result = stbi__convert_16_to_8((stbi__uint16 *) result, *x, *y, req_comp == 0 ? *comp : req_comp);
ri.bits_per_channel = 8;
}
// @TODO: move stbi__convert_format to here
if (stbi__vertically_flip_on_load) {
int channels = req_comp ? req_comp : *comp;
stbi__vertical_flip(result, *x, *y, channels * sizeof(stbi_uc));
}
return (unsigned char *) result;
}
static stbi__uint16 *stbi__load_and_postprocess_16bit(stbi__context *s, int *x, int *y, int *comp, int req_comp)
{
stbi__result_info ri;
void *result = stbi__load_main(s, x, y, comp, req_comp, &ri, 16);
if (result == NULL)
return NULL;
if (ri.bits_per_channel != 16) {
STBI_ASSERT(ri.bits_per_channel == 8);
result = stbi__convert_8_to_16((stbi_uc *) result, *x, *y, req_comp == 0 ? *comp : req_comp);
ri.bits_per_channel = 16;
}
// @TODO: move stbi__convert_format16 to here
// @TODO: special case RGB-to-Y (and RGBA-to-YA) for 8-bit-to-16-bit case to keep more precision
if (stbi__vertically_flip_on_load) {
int channels = req_comp ? req_comp : *comp;
stbi__vertical_flip(result, *x, *y, channels * sizeof(stbi__uint16));
}
return (stbi__uint16 *) result;
}
#if !defined(STBI_NO_HDR) && !defined(STBI_NO_LINEAR)
static void stbi__float_postprocess(float *result, int *x, int *y, int *comp, int req_comp)
{
if (stbi__vertically_flip_on_load && result != NULL) {
int channels = req_comp ? req_comp : *comp;
stbi__vertical_flip(result, *x, *y, channels * sizeof(float));
}
}
#endif
#ifndef STBI_NO_STDIO
#if defined(_MSC_VER) && defined(STBI_WINDOWS_UTF8)
STBI_EXTERN __declspec(dllimport) int __stdcall MultiByteToWideChar(unsigned int cp, unsigned long flags, const char *str, int cbmb, wchar_t *widestr, int cchwide);
STBI_EXTERN __declspec(dllimport) int __stdcall WideCharToMultiByte(unsigned int cp, unsigned long flags, const wchar_t *widestr, int cchwide, char *str, int cbmb, const char *defchar, int *used_default);
#endif
#if defined(_MSC_VER) && defined(STBI_WINDOWS_UTF8)
STBIDEF int stbi_convert_wchar_to_utf8(char *buffer, size_t bufferlen, const wchar_t* input)
{
return WideCharToMultiByte(65001 /* UTF8 */, 0, input, -1, buffer, (int) bufferlen, NULL, NULL);
}
#endif
static FILE *stbi__fopen(char const *filename, char const *mode)
{
FILE *f;
#if defined(_MSC_VER) && defined(STBI_WINDOWS_UTF8)
wchar_t wMode[64];
wchar_t wFilename[1024];
if (0 == MultiByteToWideChar(65001 /* UTF8 */, 0, filename, -1, wFilename, sizeof(wFilename)))
return 0;
if (0 == MultiByteToWideChar(65001 /* UTF8 */, 0, mode, -1, wMode, sizeof(wMode)))
return 0;
#if _MSC_VER >= 1400
if (0 != _wfopen_s(&f, wFilename, wMode))
f = 0;
#else
f = _wfopen(wFilename, wMode);
#endif
#elif defined(_MSC_VER) && _MSC_VER >= 1400
if (0 != fopen_s(&f, filename, mode))
f=0;
#else
f = fopen(filename, mode);
#endif
return f;
}
STBIDEF stbi_uc *stbi_load(char const *filename, int *x, int *y, int *comp, int req_comp)
{
FILE *f = stbi__fopen(filename, "rb");
unsigned char *result;
if (!f) return stbi__errpuc("can't fopen", "Unable to open file");
result = stbi_load_from_file(f,x,y,comp,req_comp);
fclose(f);
return result;
}
STBIDEF stbi_uc *stbi_load_from_file(FILE *f, int *x, int *y, int *comp, int req_comp)
{
unsigned char *result;
stbi__context s;
stbi__start_file(&s,f);
result = stbi__load_and_postprocess_8bit(&s,x,y,comp,req_comp);
if (result) {
// need to 'unget' all the characters in the IO buffer
fseek(f, - (int) (s.img_buffer_end - s.img_buffer), SEEK_CUR);
}
return result;
}
STBIDEF stbi__uint16 *stbi_load_from_file_16(FILE *f, int *x, int *y, int *comp, int req_comp)
{
stbi__uint16 *result;
stbi__context s;
stbi__start_file(&s,f);
result = stbi__load_and_postprocess_16bit(&s,x,y,comp,req_comp);
if (result) {
// need to 'unget' all the characters in the IO buffer
fseek(f, - (int) (s.img_buffer_end - s.img_buffer), SEEK_CUR);
}
return result;
}
STBIDEF stbi_us *stbi_load_16(char const *filename, int *x, int *y, int *comp, int req_comp)
{
FILE *f = stbi__fopen(filename, "rb");
stbi__uint16 *result;
if (!f) return (stbi_us *) stbi__errpuc("can't fopen", "Unable to open file");
result = stbi_load_from_file_16(f,x,y,comp,req_comp);
fclose(f);
return result;
}
#endif //!STBI_NO_STDIO
STBIDEF stbi_us *stbi_load_16_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *channels_in_file, int desired_channels)
{
stbi__context s;
stbi__start_mem(&s,buffer,len);
return stbi__load_and_postprocess_16bit(&s,x,y,channels_in_file,desired_channels);
}
STBIDEF stbi_us *stbi_load_16_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *channels_in_file, int desired_channels)
{
stbi__context s;
stbi__start_callbacks(&s, (stbi_io_callbacks *)clbk, user);
return stbi__load_and_postprocess_16bit(&s,x,y,channels_in_file,desired_channels);
}
STBIDEF stbi_uc *stbi_load_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp)
{
stbi__context s;
stbi__start_mem(&s,buffer,len);
return stbi__load_and_postprocess_8bit(&s,x,y,comp,req_comp);
}
STBIDEF stbi_uc *stbi_load_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp, int req_comp)
{
stbi__context s;
stbi__start_callbacks(&s, (stbi_io_callbacks *) clbk, user);
return stbi__load_and_postprocess_8bit(&s,x,y,comp,req_comp);
}
#ifndef STBI_NO_GIF
STBIDEF stbi_uc *stbi_load_gif_from_memory(stbi_uc const *buffer, int len, int **delays, int *x, int *y, int *z, int *comp, int req_comp)
{
unsigned char *result;
stbi__context s;
stbi__start_mem(&s,buffer,len);
result = (unsigned char*) stbi__load_gif_main(&s, delays, x, y, z, comp, req_comp);
if (stbi__vertically_flip_on_load) {
stbi__vertical_flip_slices( result, *x, *y, *z, *comp );
}
return result;
}
#endif
#ifndef STBI_NO_LINEAR
static float *stbi__loadf_main(stbi__context *s, int *x, int *y, int *comp, int req_comp)
{
unsigned char *data;
#ifndef STBI_NO_HDR
if (stbi__hdr_test(s)) {
stbi__result_info ri;
float *hdr_data = stbi__hdr_load(s,x,y,comp,req_comp, &ri);
if (hdr_data)
stbi__float_postprocess(hdr_data,x,y,comp,req_comp);
return hdr_data;
}
#endif
data = stbi__load_and_postprocess_8bit(s, x, y, comp, req_comp);
if (data)
return stbi__ldr_to_hdr(data, *x, *y, req_comp ? req_comp : *comp);
return stbi__errpf("unknown image type", "Image not of any known type, or corrupt");
}
STBIDEF float *stbi_loadf_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp)
{
stbi__context s;
stbi__start_mem(&s,buffer,len);
return stbi__loadf_main(&s,x,y,comp,req_comp);
}
STBIDEF float *stbi_loadf_from_callbacks(stbi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp, int req_comp)
{
stbi__context s;
stbi__start_callbacks(&s, (stbi_io_callbacks *) clbk, user);
return stbi__loadf_main(&s,x,y,comp,req_comp);
}
#ifndef STBI_NO_STDIO
STBIDEF float *stbi_loadf(char const *filename, int *x, int *y, int *comp, int req_comp)
{
float *result;
FILE *f = stbi__fopen(filename, "rb");
if (!f) return stbi__errpf("can't fopen", "Unable to open file");
result = stbi_loadf_from_file(f,x,y,comp,req_comp);
fclose(f);
return result;
}
STBIDEF float *stbi_loadf_from_file(FILE *f, int *x, int *y, int *comp, int req_comp)
{
stbi__context s;
stbi__start_file(&s,f);
return stbi__loadf_main(&s,x,y,comp,req_comp);
}
#endif // !STBI_NO_STDIO
#endif // !STBI_NO_LINEAR
// these is-hdr-or-not is defined independent of whether STBI_NO_LINEAR is
// defined, for API simplicity; if STBI_NO_LINEAR is defined, it always
// reports false!
STBIDEF int stbi_is_hdr_from_memory(stbi_uc const *buffer, int len)
{
#ifndef STBI_NO_HDR
stbi__context s;
stbi__start_mem(&s,buffer,len);
return stbi__hdr_test(&s);
#else
STBI_NOTUSED(buffer);
STBI_NOTUSED(len);
return 0;
#endif
}
#ifndef STBI_NO_STDIO
STBIDEF int stbi_is_hdr (char const *filename)
{
FILE *f = stbi__fopen(filename, "rb");
int result=0;
if (f) {
result = stbi_is_hdr_from_file(f);
fclose(f);
}
return result;
}
STBIDEF int stbi_is_hdr_from_file(FILE *f)
{
#ifndef STBI_NO_HDR
long pos = ftell(f);
int res;
stbi__context s;
stbi__start_file(&s,f);
res = stbi__hdr_test(&s);
fseek(f, pos, SEEK_SET);
return res;
#else
STBI_NOTUSED(f);
return 0;
#endif
}
#endif // !STBI_NO_STDIO
STBIDEF int stbi_is_hdr_from_callbacks(stbi_io_callbacks const *clbk, void *user)
{
#ifndef STBI_NO_HDR
stbi__context s;
stbi__start_callbacks(&s, (stbi_io_callbacks *) clbk, user);
return stbi__hdr_test(&s);
#else
STBI_NOTUSED(clbk);
STBI_NOTUSED(user);
return 0;
#endif
}
#ifndef STBI_NO_LINEAR
static float stbi__l2h_gamma=2.2f, stbi__l2h_scale=1.0f;
STBIDEF void stbi_ldr_to_hdr_gamma(float gamma) { stbi__l2h_gamma = gamma; }
STBIDEF void stbi_ldr_to_hdr_scale(float scale) { stbi__l2h_scale = scale; }
#endif
static float stbi__h2l_gamma_i=1.0f/2.2f, stbi__h2l_scale_i=1.0f;
STBIDEF void stbi_hdr_to_ldr_gamma(float gamma) { stbi__h2l_gamma_i = 1/gamma; }
STBIDEF void stbi_hdr_to_ldr_scale(float scale) { stbi__h2l_scale_i = 1/scale; }
//////////////////////////////////////////////////////////////////////////////
//
// Common code used by all image loaders
//
enum
{
STBI__SCAN_load=0,
STBI__SCAN_type,
STBI__SCAN_header
};
static void stbi__refill_buffer(stbi__context *s)
{
int n = (s->io.read)(s->io_user_data,(char*)s->buffer_start,s->buflen);
if (n == 0) {
// at end of file, treat same as if from memory, but need to handle case
// where s->img_buffer isn't pointing to safe memory, e.g. 0-byte file
s->read_from_callbacks = 0;
s->img_buffer = s->buffer_start;
s->img_buffer_end = s->buffer_start+1;
*s->img_buffer = 0;
} else {
s->img_buffer = s->buffer_start;
s->img_buffer_end = s->buffer_start + n;
}
}
stbi_inline static stbi_uc stbi__get8(stbi__context *s)
{
if (s->img_buffer < s->img_buffer_end)
return *s->img_buffer++;
if (s->read_from_callbacks) {
stbi__refill_buffer(s);
return *s->img_buffer++;
}
return 0;
}
#if defined(STBI_NO_JPEG) && defined(STBI_NO_HDR) && defined(STBI_NO_PIC) && defined(STBI_NO_PNM)
// nothing
#else
stbi_inline static int stbi__at_eof(stbi__context *s)
{
if (s->io.read) {
if (!(s->io.eof)(s->io_user_data)) return 0;
// if feof() is true, check if buffer = end
// special case: we've only got the special 0 character at the end
if (s->read_from_callbacks == 0) return 1;
}
return s->img_buffer >= s->img_buffer_end;
}
#endif
#if defined(STBI_NO_JPEG) && defined(STBI_NO_PNG) && defined(STBI_NO_BMP) && defined(STBI_NO_PSD) && defined(STBI_NO_TGA) && defined(STBI_NO_GIF) && defined(STBI_NO_PIC)
// nothing
#else
static void stbi__skip(stbi__context *s, int n)
{
if (n < 0) {
s->img_buffer = s->img_buffer_end;
return;
}
if (s->io.read) {
int blen = (int) (s->img_buffer_end - s->img_buffer);
if (blen < n) {
s->img_buffer = s->img_buffer_end;
(s->io.skip)(s->io_user_data, n - blen);
return;
}
}
s->img_buffer += n;
}
#endif
#if defined(STBI_NO_PNG) && defined(STBI_NO_TGA) && defined(STBI_NO_HDR) && defined(STBI_NO_PNM)
// nothing
#else
static int stbi__getn(stbi__context *s, stbi_uc *buffer, int n)
{
if (s->io.read) {
int blen = (int) (s->img_buffer_end - s->img_buffer);
if (blen < n) {
int res, count;
memcpy(buffer, s->img_buffer, blen);
count = (s->io.read)(s->io_user_data, (char*) buffer + blen, n - blen);
res = (count == (n-blen));
s->img_buffer = s->img_buffer_end;
return res;
}
}
if (s->img_buffer+n <= s->img_buffer_end) {
memcpy(buffer, s->img_buffer, n);
s->img_buffer += n;
return 1;
} else
return 0;
}
#endif
#if defined(STBI_NO_JPEG) && defined(STBI_NO_PNG) && defined(STBI_NO_PSD) && defined(STBI_NO_PIC)
// nothing
#else
static int stbi__get16be(stbi__context *s)
{
int z = stbi__get8(s);
return (z << 8) + stbi__get8(s);
}
#endif
#if defined(STBI_NO_PNG) && defined(STBI_NO_PSD) && defined(STBI_NO_PIC)
// nothing
#else
static stbi__uint32 stbi__get32be(stbi__context *s)
{
stbi__uint32 z = stbi__get16be(s);
return (z << 16) + stbi__get16be(s);
}
#endif
#if defined(STBI_NO_BMP) && defined(STBI_NO_TGA) && defined(STBI_NO_GIF)
// nothing
#else
static int stbi__get16le(stbi__context *s)
{
int z = stbi__get8(s);
return z + (stbi__get8(s) << 8);
}
#endif
#ifndef STBI_NO_BMP
static stbi__uint32 stbi__get32le(stbi__context *s)
{
stbi__uint32 z = stbi__get16le(s);
return z + (stbi__get16le(s) << 16);
}
#endif
#define STBI__BYTECAST(x) ((stbi_uc) ((x) & 255)) // truncate int to byte without warnings
#if defined(STBI_NO_JPEG) && defined(STBI_NO_PNG) && defined(STBI_NO_BMP) && defined(STBI_NO_PSD) && defined(STBI_NO_TGA) && defined(STBI_NO_GIF) && defined(STBI_NO_PIC) && defined(STBI_NO_PNM)
// nothing
#else
//////////////////////////////////////////////////////////////////////////////
//
// generic converter from built-in img_n to req_comp
// individual types do this automatically as much as possible (e.g. jpeg
// does all cases internally since it needs to colorspace convert anyway,
// and it never has alpha, so very few cases ). png can automatically
// interleave an alpha=255 channel, but falls back to this for other cases
//
// assume data buffer is malloced, so malloc a new one and free that one
// only failure mode is malloc failing
static stbi_uc stbi__compute_y(int r, int g, int b)
{
return (stbi_uc) (((r*77) + (g*150) + (29*b)) >> 8);
}
#endif
#if defined(STBI_NO_PNG) && defined(STBI_NO_BMP) && defined(STBI_NO_PSD) && defined(STBI_NO_TGA) && defined(STBI_NO_GIF) && defined(STBI_NO_PIC) && defined(STBI_NO_PNM)
// nothing
#else
static unsigned char *stbi__convert_format(unsigned char *data, int img_n, int req_comp, unsigned int x, unsigned int y)
{
int i,j;
unsigned char *good;
if (req_comp == img_n) return data;
STBI_ASSERT(req_comp >= 1 && req_comp <= 4);
good = (unsigned char *) stbi__malloc_mad3(req_comp, x, y, 0);
if (good == NULL) {
STBI_FREE(data);
return stbi__errpuc("outofmem", "Out of memory");
}
for (j=0; j < (int) y; ++j) {
unsigned char *src = data + j * x * img_n ;
unsigned char *dest = good + j * x * req_comp;
#define STBI__COMBO(a,b) ((a)*8+(b))
#define STBI__CASE(a,b) case STBI__COMBO(a,b): for(i=x-1; i >= 0; --i, src += a, dest += b)
// convert source image with img_n components to one with req_comp components;
// avoid switch per pixel, so use switch per scanline and massive macros
switch (STBI__COMBO(img_n, req_comp)) {
STBI__CASE(1,2) { dest[0]=src[0]; dest[1]=255; } break;
STBI__CASE(1,3) { dest[0]=dest[1]=dest[2]=src[0]; } break;
STBI__CASE(1,4) { dest[0]=dest[1]=dest[2]=src[0]; dest[3]=255; } break;
STBI__CASE(2,1) { dest[0]=src[0]; } break;
STBI__CASE(2,3) { dest[0]=dest[1]=dest[2]=src[0]; } break;
STBI__CASE(2,4) { dest[0]=dest[1]=dest[2]=src[0]; dest[3]=src[1]; } break;
STBI__CASE(3,4) { dest[0]=src[0];dest[1]=src[1];dest[2]=src[2];dest[3]=255; } break;
STBI__CASE(3,1) { dest[0]=stbi__compute_y(src[0],src[1],src[2]); } break;
STBI__CASE(3,2) { dest[0]=stbi__compute_y(src[0],src[1],src[2]); dest[1] = 255; } break;
STBI__CASE(4,1) { dest[0]=stbi__compute_y(src[0],src[1],src[2]); } break;
STBI__CASE(4,2) { dest[0]=stbi__compute_y(src[0],src[1],src[2]); dest[1] = src[3]; } break;
STBI__CASE(4,3) { dest[0]=src[0];dest[1]=src[1];dest[2]=src[2]; } break;
default: STBI_ASSERT(0);
}
#undef STBI__CASE
}
STBI_FREE(data);
return good;
}
#endif
#if defined(STBI_NO_PNG) && defined(STBI_NO_PSD)
// nothing
#else
static stbi__uint16 stbi__compute_y_16(int r, int g, int b)
{
return (stbi__uint16) (((r*77) + (g*150) + (29*b)) >> 8);
}
#endif
#if defined(STBI_NO_PNG) && defined(STBI_NO_PSD)
// nothing
#else
static stbi__uint16 *stbi__convert_format16(stbi__uint16 *data, int img_n, int req_comp, unsigned int x, unsigned int y)
{
int i,j;
stbi__uint16 *good;
if (req_comp == img_n) return data;
STBI_ASSERT(req_comp >= 1 && req_comp <= 4);
good = (stbi__uint16 *) stbi__malloc(req_comp * x * y * 2);
if (good == NULL) {
STBI_FREE(data);
return (stbi__uint16 *) stbi__errpuc("outofmem", "Out of memory");
}
for (j=0; j < (int) y; ++j) {
stbi__uint16 *src = data + j * x * img_n ;
stbi__uint16 *dest = good + j * x * req_comp;
#define STBI__COMBO(a,b) ((a)*8+(b))
#define STBI__CASE(a,b) case STBI__COMBO(a,b): for(i=x-1; i >= 0; --i, src += a, dest += b)
// convert source image with img_n components to one with req_comp components;
// avoid switch per pixel, so use switch per scanline and massive macros
switch (STBI__COMBO(img_n, req_comp)) {
STBI__CASE(1,2) { dest[0]=src[0]; dest[1]=0xffff; } break;
STBI__CASE(1,3) { dest[0]=dest[1]=dest[2]=src[0]; } break;
STBI__CASE(1,4) { dest[0]=dest[1]=dest[2]=src[0]; dest[3]=0xffff; } break;
STBI__CASE(2,1) { dest[0]=src[0]; } break;
STBI__CASE(2,3) { dest[0]=dest[1]=dest[2]=src[0]; } break;
STBI__CASE(2,4) { dest[0]=dest[1]=dest[2]=src[0]; dest[3]=src[1]; } break;
STBI__CASE(3,4) { dest[0]=src[0];dest[1]=src[1];dest[2]=src[2];dest[3]=0xffff; } break;
STBI__CASE(3,1) { dest[0]=stbi__compute_y_16(src[0],src[1],src[2]); } break;
STBI__CASE(3,2) { dest[0]=stbi__compute_y_16(src[0],src[1],src[2]); dest[1] = 0xffff; } break;
STBI__CASE(4,1) { dest[0]=stbi__compute_y_16(src[0],src[1],src[2]); } break;
STBI__CASE(4,2) { dest[0]=stbi__compute_y_16(src[0],src[1],src[2]); dest[1] = src[3]; } break;
STBI__CASE(4,3) { dest[0]=src[0];dest[1]=src[1];dest[2]=src[2]; } break;
default: STBI_ASSERT(0);
}
#undef STBI__CASE
}
STBI_FREE(data);
return good;
}
#endif
#ifndef STBI_NO_LINEAR
static float *stbi__ldr_to_hdr(stbi_uc *data, int x, int y, int comp)
{
int i,k,n;
float *output;
if (!data) return NULL;
output = (float *) stbi__malloc_mad4(x, y, comp, sizeof(float), 0);
if (output == NULL) { STBI_FREE(data); return stbi__errpf("outofmem", "Out of memory"); }
// compute number of non-alpha components
if (comp & 1) n = comp; else n = comp-1;
for (i=0; i < x*y; ++i) {
for (k=0; k < n; ++k) {
output[i*comp + k] = (float) (pow(data[i*comp+k]/255.0f, stbi__l2h_gamma) * stbi__l2h_scale);
}
}
if (n < comp) {
for (i=0; i < x*y; ++i) {
output[i*comp + n] = data[i*comp + n]/255.0f;
}
}
STBI_FREE(data);
return output;
}
#endif
#ifndef STBI_NO_HDR
#define stbi__float2int(x) ((int) (x))
static stbi_uc *stbi__hdr_to_ldr(float *data, int x, int y, int comp)
{
int i,k,n;
stbi_uc *output;
if (!data) return NULL;
output = (stbi_uc *) stbi__malloc_mad3(x, y, comp, 0);
if (output == NULL) { STBI_FREE(data); return stbi__errpuc("outofmem", "Out of memory"); }
// compute number of non-alpha components
if (comp & 1) n = comp; else n = comp-1;
for (i=0; i < x*y; ++i) {
for (k=0; k < n; ++k) {
float z = (float) pow(data[i*comp+k]*stbi__h2l_scale_i, stbi__h2l_gamma_i) * 255 + 0.5f;
if (z < 0) z = 0;
if (z > 255) z = 255;
output[i*comp + k] = (stbi_uc) stbi__float2int(z);
}
if (k < comp) {
float z = data[i*comp+k] * 255 + 0.5f;
if (z < 0) z = 0;
if (z > 255) z = 255;
output[i*comp + k] = (stbi_uc) stbi__float2int(z);
}
}
STBI_FREE(data);
return output;
}
#endif
//////////////////////////////////////////////////////////////////////////////
//
// "baseline" JPEG/JFIF decoder
//
// simple implementation
// - doesn't support delayed output of y-dimension
// - simple interface (only one output format: 8-bit interleaved RGB)
// - doesn't try to recover corrupt jpegs
// - doesn't allow partial loading, loading multiple at once
// - still fast on x86 (copying globals into locals doesn't help x86)
// - allocates lots of intermediate memory (full size of all components)
// - non-interleaved case requires this anyway
// - allows good upsampling (see next)
// high-quality
// - upsampled channels are bilinearly interpolated, even across blocks
// - quality integer IDCT derived from IJG's 'slow'
// performance
// - fast huffman; reasonable integer IDCT
// - some SIMD kernels for common paths on targets with SSE2/NEON
// - uses a lot of intermediate memory, could cache poorly
#ifndef STBI_NO_JPEG
// huffman decoding acceleration
#define FAST_BITS 9 // larger handles more cases; smaller stomps less cache
typedef struct
{
stbi_uc fast[1 << FAST_BITS];
// weirdly, repacking this into AoS is a 10% speed loss, instead of a win
stbi__uint16 code[256];
stbi_uc values[256];
stbi_uc size[257];
unsigned int maxcode[18];
int delta[17]; // old 'firstsymbol' - old 'firstcode'
} stbi__huffman;
typedef struct
{
stbi__context *s;
stbi__huffman huff_dc[4];
stbi__huffman huff_ac[4];
stbi__uint16 dequant[4][64];
stbi__int16 fast_ac[4][1 << FAST_BITS];
// sizes for components, interleaved MCUs
int img_h_max, img_v_max;
int img_mcu_x, img_mcu_y;
int img_mcu_w, img_mcu_h;
// definition of jpeg image component
struct
{
int id;
int h,v;
int tq;
int hd,ha;
int dc_pred;
int x,y,w2,h2;
stbi_uc *data;
void *raw_data, *raw_coeff;
stbi_uc *linebuf;
short *coeff; // progressive only
int coeff_w, coeff_h; // number of 8x8 coefficient blocks
} img_comp[4];
stbi__uint32 code_buffer; // jpeg entropy-coded buffer
int code_bits; // number of valid bits
unsigned char marker; // marker seen while filling entropy buffer
int nomore; // flag if we saw a marker so must stop
int progressive;
int spec_start;
int spec_end;
int succ_high;
int succ_low;
int eob_run;
int jfif;
int app14_color_transform; // Adobe APP14 tag
int rgb;
int scan_n, order[4];
int restart_interval, todo;
// kernels
void (*idct_block_kernel)(stbi_uc *out, int out_stride, short data[64]);
void (*YCbCr_to_RGB_kernel)(stbi_uc *out, const stbi_uc *y, const stbi_uc *pcb, const stbi_uc *pcr, int count, int step);
stbi_uc *(*resample_row_hv_2_kernel)(stbi_uc *out, stbi_uc *in_near, stbi_uc *in_far, int w, int hs);
} stbi__jpeg;
static int stbi__build_huffman(stbi__huffman *h, int *count)
{
int i,j,k=0;
unsigned int code;
// build size list for each symbol (from JPEG spec)
for (i=0; i < 16; ++i)
for (j=0; j < count[i]; ++j)
h->size[k++] = (stbi_uc) (i+1);
h->size[k] = 0;
// compute actual symbols (from jpeg spec)
code = 0;
k = 0;
for(j=1; j <= 16; ++j) {
// compute delta to add to code to compute symbol id
h->delta[j] = k - code;
if (h->size[k] == j) {
while (h->size[k] == j)
h->code[k++] = (stbi__uint16) (code++);
if (code-1 >= (1u << j)) return stbi__err("bad code lengths","Corrupt JPEG");
}
// compute largest code + 1 for this size, preshifted as needed later
h->maxcode[j] = code << (16-j);
code <<= 1;
}
h->maxcode[j] = 0xffffffff;
// build non-spec acceleration table; 255 is flag for not-accelerated
memset(h->fast, 255, 1 << FAST_BITS);
for (i=0; i < k; ++i) {
int s = h->size[i];
if (s <= FAST_BITS) {
int c = h->code[i] << (FAST_BITS-s);
int m = 1 << (FAST_BITS-s);
for (j=0; j < m; ++j) {
h->fast[c+j] = (stbi_uc) i;
}
}
}
return 1;
}
// build a table that decodes both magnitude and value of small ACs in
// one go.
static void stbi__build_fast_ac(stbi__int16 *fast_ac, stbi__huffman *h)
{
int i;
for (i=0; i < (1 << FAST_BITS); ++i) {
stbi_uc fast = h->fast[i];
fast_ac[i] = 0;
if (fast < 255) {
int rs = h->values[fast];
int run = (rs >> 4) & 15;
int magbits = rs & 15;
int len = h->size[fast];
if (magbits && len + magbits <= FAST_BITS) {
// magnitude code followed by receive_extend code
int k = ((i << len) & ((1 << FAST_BITS) - 1)) >> (FAST_BITS - magbits);
int m = 1 << (magbits - 1);
if (k < m) k += (~0U << magbits) + 1;
// if the result is small enough, we can fit it in fast_ac table
if (k >= -128 && k <= 127)
fast_ac[i] = (stbi__int16) ((k * 256) + (run * 16) + (len + magbits));
}
}
}
}
static void stbi__grow_buffer_unsafe(stbi__jpeg *j)
{
do {
unsigned int b = j->nomore ? 0 : stbi__get8(j->s);
if (b == 0xff) {
int c = stbi__get8(j->s);
while (c == 0xff) c = stbi__get8(j->s); // consume fill bytes
if (c != 0) {
j->marker = (unsigned char) c;
j->nomore = 1;
return;
}
}
j->code_buffer |= b << (24 - j->code_bits);
j->code_bits += 8;
} while (j->code_bits <= 24);
}
// (1 << n) - 1
static const stbi__uint32 stbi__bmask[17]={0,1,3,7,15,31,63,127,255,511,1023,2047,4095,8191,16383,32767,65535};
// decode a jpeg huffman value from the bitstream
stbi_inline static int stbi__jpeg_huff_decode(stbi__jpeg *j, stbi__huffman *h)
{
unsigned int temp;
int c,k;
if (j->code_bits < 16) stbi__grow_buffer_unsafe(j);
// look at the top FAST_BITS and determine what symbol ID it is,
// if the code is <= FAST_BITS
c = (j->code_buffer >> (32 - FAST_BITS)) & ((1 << FAST_BITS)-1);
k = h->fast[c];
if (k < 255) {
int s = h->size[k];
if (s > j->code_bits)
return -1;
j->code_buffer <<= s;
j->code_bits -= s;
return h->values[k];
}
// naive test is to shift the code_buffer down so k bits are
// valid, then test against maxcode. To speed this up, we've
// preshifted maxcode left so that it has (16-k) 0s at the
// end; in other words, regardless of the number of bits, it
// wants to be compared against something shifted to have 16;
// that way we don't need to shift inside the loop.
temp = j->code_buffer >> 16;
for (k=FAST_BITS+1 ; ; ++k)
if (temp < h->maxcode[k])
break;
if (k == 17) {
// error! code not found
j->code_bits -= 16;
return -1;
}
if (k > j->code_bits)
return -1;
// convert the huffman code to the symbol id
c = ((j->code_buffer >> (32 - k)) & stbi__bmask[k]) + h->delta[k];
STBI_ASSERT((((j->code_buffer) >> (32 - h->size[c])) & stbi__bmask[h->size[c]]) == h->code[c]);
// convert the id to a symbol
j->code_bits -= k;
j->code_buffer <<= k;
return h->values[c];
}
// bias[n] = (-1<<n) + 1
static const int stbi__jbias[16] = {0,-1,-3,-7,-15,-31,-63,-127,-255,-511,-1023,-2047,-4095,-8191,-16383,-32767};
// combined JPEG 'receive' and JPEG 'extend', since baseline
// always extends everything it receives.
stbi_inline static int stbi__extend_receive(stbi__jpeg *j, int n)
{
unsigned int k;
int sgn;
if (j->code_bits < n) stbi__grow_buffer_unsafe(j);
sgn = (stbi__int32)j->code_buffer >> 31; // sign bit is always in MSB
k = stbi_lrot(j->code_buffer, n);
STBI_ASSERT(n >= 0 && n < (int) (sizeof(stbi__bmask)/sizeof(*stbi__bmask)));
j->code_buffer = k & ~stbi__bmask[n];
k &= stbi__bmask[n];
j->code_bits -= n;
return k + (stbi__jbias[n] & ~sgn);
}
// get some unsigned bits
stbi_inline static int stbi__jpeg_get_bits(stbi__jpeg *j, int n)
{
unsigned int k;
if (j->code_bits < n) stbi__grow_buffer_unsafe(j);
k = stbi_lrot(j->code_buffer, n);
j->code_buffer = k & ~stbi__bmask[n];
k &= stbi__bmask[n];
j->code_bits -= n;
return k;
}
stbi_inline static int stbi__jpeg_get_bit(stbi__jpeg *j)
{
unsigned int k;
if (j->code_bits < 1) stbi__grow_buffer_unsafe(j);
k = j->code_buffer;
j->code_buffer <<= 1;
--j->code_bits;
return k & 0x80000000;
}
// given a value that's at position X in the zigzag stream,
// where does it appear in the 8x8 matrix coded as row-major?
static const stbi_uc stbi__jpeg_dezigzag[64+15] =
{
0, 1, 8, 16, 9, 2, 3, 10,
17, 24, 32, 25, 18, 11, 4, 5,
12, 19, 26, 33, 40, 48, 41, 34,
27, 20, 13, 6, 7, 14, 21, 28,
35, 42, 49, 56, 57, 50, 43, 36,
29, 22, 15, 23, 30, 37, 44, 51,
58, 59, 52, 45, 38, 31, 39, 46,
53, 60, 61, 54, 47, 55, 62, 63,
// let corrupt input sample past end
63, 63, 63, 63, 63, 63, 63, 63,
63, 63, 63, 63, 63, 63, 63
};
// decode one 64-entry block--
static int stbi__jpeg_decode_block(stbi__jpeg *j, short data[64], stbi__huffman *hdc, stbi__huffman *hac, stbi__int16 *fac, int b, stbi__uint16 *dequant)
{
int diff,dc,k;
int t;
if (j->code_bits < 16) stbi__grow_buffer_unsafe(j);
t = stbi__jpeg_huff_decode(j, hdc);
if (t < 0) return stbi__err("bad huffman code","Corrupt JPEG");
// 0 all the ac values now so we can do it 32-bits at a time
memset(data,0,64*sizeof(data[0]));
diff = t ? stbi__extend_receive(j, t) : 0;
dc = j->img_comp[b].dc_pred + diff;
j->img_comp[b].dc_pred = dc;
data[0] = (short) (dc * dequant[0]);
// decode AC components, see JPEG spec
k = 1;
do {
unsigned int zig;
int c,r,s;
if (j->code_bits < 16) stbi__grow_buffer_unsafe(j);
c = (j->code_buffer >> (32 - FAST_BITS)) & ((1 << FAST_BITS)-1);
r = fac[c];
if (r) { // fast-AC path
k += (r >> 4) & 15; // run
s = r & 15; // combined length
j->code_buffer <<= s;
j->code_bits -= s;
// decode into unzigzag'd location
zig = stbi__jpeg_dezigzag[k++];
data[zig] = (short) ((r >> 8) * dequant[zig]);
} else {
int rs = stbi__jpeg_huff_decode(j, hac);
if (rs < 0) return stbi__err("bad huffman code","Corrupt JPEG");
s = rs & 15;
r = rs >> 4;
if (s == 0) {
if (rs != 0xf0) break; // end block
k += 16;
} else {
k += r;
// decode into unzigzag'd location
zig = stbi__jpeg_dezigzag[k++];
data[zig] = (short) (stbi__extend_receive(j,s) * dequant[zig]);
}
}
} while (k < 64);
return 1;
}
static int stbi__jpeg_decode_block_prog_dc(stbi__jpeg *j, short data[64], stbi__huffman *hdc, int b)
{
int diff,dc;
int t;
if (j->spec_end != 0) return stbi__err("can't merge dc and ac", "Corrupt JPEG");
if (j->code_bits < 16) stbi__grow_buffer_unsafe(j);
if (j->succ_high == 0) {
// first scan for DC coefficient, must be first
memset(data,0,64*sizeof(data[0])); // 0 all the ac values now
t = stbi__jpeg_huff_decode(j, hdc);
diff = t ? stbi__extend_receive(j, t) : 0;
dc = j->img_comp[b].dc_pred + diff;
j->img_comp[b].dc_pred = dc;
data[0] = (short) (dc << j->succ_low);
} else {
// refinement scan for DC coefficient
if (stbi__jpeg_get_bit(j))
data[0] += (short) (1 << j->succ_low);
}
return 1;
}
// @OPTIMIZE: store non-zigzagged during the decode passes,
// and only de-zigzag when dequantizing
static int stbi__jpeg_decode_block_prog_ac(stbi__jpeg *j, short data[64], stbi__huffman *hac, stbi__int16 *fac)
{
int k;
if (j->spec_start == 0) return stbi__err("can't merge dc and ac", "Corrupt JPEG");
if (j->succ_high == 0) {
int shift = j->succ_low;
if (j->eob_run) {
--j->eob_run;
return 1;
}
k = j->spec_start;
do {
unsigned int zig;
int c,r,s;
if (j->code_bits < 16) stbi__grow_buffer_unsafe(j);
c = (j->code_buffer >> (32 - FAST_BITS)) & ((1 << FAST_BITS)-1);
r = fac[c];
if (r) { // fast-AC path
k += (r >> 4) & 15; // run
s = r & 15; // combined length
j->code_buffer <<= s;
j->code_bits -= s;
zig = stbi__jpeg_dezigzag[k++];
data[zig] = (short) ((r >> 8) << shift);
} else {
int rs = stbi__jpeg_huff_decode(j, hac);
if (rs < 0) return stbi__err("bad huffman code","Corrupt JPEG");
s = rs & 15;
r = rs >> 4;
if (s == 0) {
if (r < 15) {
j->eob_run = (1 << r);
if (r)
j->eob_run += stbi__jpeg_get_bits(j, r);
--j->eob_run;
break;
}
k += 16;
} else {
k += r;
zig = stbi__jpeg_dezigzag[k++];
data[zig] = (short) (stbi__extend_receive(j,s) << shift);
}
}
} while (k <= j->spec_end);
} else {
// refinement scan for these AC coefficients
short bit = (short) (1 << j->succ_low);
if (j->eob_run) {
--j->eob_run;
for (k = j->spec_start; k <= j->spec_end; ++k) {
short *p = &data[stbi__jpeg_dezigzag[k]];
if (*p != 0)
if (stbi__jpeg_get_bit(j))
if ((*p & bit)==0) {
if (*p > 0)
*p += bit;
else
*p -= bit;
}
}
} else {
k = j->spec_start;
do {
int r,s;
int rs = stbi__jpeg_huff_decode(j, hac); // @OPTIMIZE see if we can use the fast path here, advance-by-r is so slow, eh
if (rs < 0) return stbi__err("bad huffman code","Corrupt JPEG");
s = rs & 15;
r = rs >> 4;
if (s == 0) {
if (r < 15) {
j->eob_run = (1 << r) - 1;
if (r)
j->eob_run += stbi__jpeg_get_bits(j, r);
r = 64; // force end of block
} else {
// r=15 s=0 should write 16 0s, so we just do
// a run of 15 0s and then write s (which is 0),
// so we don't have to do anything special here
}
} else {
if (s != 1) return stbi__err("bad huffman code", "Corrupt JPEG");
// sign bit
if (stbi__jpeg_get_bit(j))
s = bit;
else
s = -bit;
}
// advance by r
while (k <= j->spec_end) {
short *p = &data[stbi__jpeg_dezigzag[k++]];
if (*p != 0) {
if (stbi__jpeg_get_bit(j))
if ((*p & bit)==0) {
if (*p > 0)
*p += bit;
else
*p -= bit;
}
} else {
if (r == 0) {
*p = (short) s;
break;
}
--r;
}
}
} while (k <= j->spec_end);
}
}
return 1;
}
// take a -128..127 value and stbi__clamp it and convert to 0..255
stbi_inline static stbi_uc stbi__clamp(int x)
{
// trick to use a single test to catch both cases
if ((unsigned int) x > 255) {
if (x < 0) return 0;
if (x > 255) return 255;
}
return (stbi_uc) x;
}
#define stbi__f2f(x) ((int) (((x) * 4096 + 0.5)))
#define stbi__fsh(x) ((x) * 4096)
// derived from jidctint -- DCT_ISLOW
#define STBI__IDCT_1D(s0,s1,s2,s3,s4,s5,s6,s7) \
int t0,t1,t2,t3,p1,p2,p3,p4,p5,x0,x1,x2,x3; \
p2 = s2; \
p3 = s6; \
p1 = (p2+p3) * stbi__f2f(0.5411961f); \
t2 = p1 + p3*stbi__f2f(-1.847759065f); \
t3 = p1 + p2*stbi__f2f( 0.765366865f); \
p2 = s0; \
p3 = s4; \
t0 = stbi__fsh(p2+p3); \
t1 = stbi__fsh(p2-p3); \
x0 = t0+t3; \
x3 = t0-t3; \
x1 = t1+t2; \
x2 = t1-t2; \
t0 = s7; \
t1 = s5; \
t2 = s3; \
t3 = s1; \
p3 = t0+t2; \
p4 = t1+t3; \
p1 = t0+t3; \
p2 = t1+t2; \
p5 = (p3+p4)*stbi__f2f( 1.175875602f); \
t0 = t0*stbi__f2f( 0.298631336f); \
t1 = t1*stbi__f2f( 2.053119869f); \
t2 = t2*stbi__f2f( 3.072711026f); \
t3 = t3*stbi__f2f( 1.501321110f); \
p1 = p5 + p1*stbi__f2f(-0.899976223f); \
p2 = p5 + p2*stbi__f2f(-2.562915447f); \
p3 = p3*stbi__f2f(-1.961570560f); \
p4 = p4*stbi__f2f(-0.390180644f); \
t3 += p1+p4; \
t2 += p2+p3; \
t1 += p2+p4; \
t0 += p1+p3;
static void stbi__idct_block(stbi_uc *out, int out_stride, short data[64])
{
int i,val[64],*v=val;
stbi_uc *o;
short *d = data;
// columns
for (i=0; i < 8; ++i,++d, ++v) {
// if all zeroes, shortcut -- this avoids dequantizing 0s and IDCTing
if (d[ 8]==0 && d[16]==0 && d[24]==0 && d[32]==0
&& d[40]==0 && d[48]==0 && d[56]==0) {
// no shortcut 0 seconds
// (1|2|3|4|5|6|7)==0 0 seconds
// all separate -0.047 seconds
// 1 && 2|3 && 4|5 && 6|7: -0.047 seconds
int dcterm = d[0]*4;
v[0] = v[8] = v[16] = v[24] = v[32] = v[40] = v[48] = v[56] = dcterm;
} else {
STBI__IDCT_1D(d[ 0],d[ 8],d[16],d[24],d[32],d[40],d[48],d[56])
// constants scaled things up by 1<<12; let's bring them back
// down, but keep 2 extra bits of precision
x0 += 512; x1 += 512; x2 += 512; x3 += 512;
v[ 0] = (x0+t3) >> 10;
v[56] = (x0-t3) >> 10;
v[ 8] = (x1+t2) >> 10;
v[48] = (x1-t2) >> 10;
v[16] = (x2+t1) >> 10;
v[40] = (x2-t1) >> 10;
v[24] = (x3+t0) >> 10;
v[32] = (x3-t0) >> 10;
}
}
for (i=0, v=val, o=out; i < 8; ++i,v+=8,o+=out_stride) {
// no fast case since the first 1D IDCT spread components out
STBI__IDCT_1D(v[0],v[1],v[2],v[3],v[4],v[5],v[6],v[7])
// constants scaled things up by 1<<12, plus we had 1<<2 from first
// loop, plus horizontal and vertical each scale by sqrt(8) so together
// we've got an extra 1<<3, so 1<<17 total we need to remove.
// so we want to round that, which means adding 0.5 * 1<<17,
// aka 65536. Also, we'll end up with -128 to 127 that we want
// to encode as 0..255 by adding 128, so we'll add that before the shift
x0 += 65536 + (128<<17);
x1 += 65536 + (128<<17);
x2 += 65536 + (128<<17);
x3 += 65536 + (128<<17);
// tried computing the shifts into temps, or'ing the temps to see
// if any were out of range, but that was slower
o[0] = stbi__clamp((x0+t3) >> 17);
o[7] = stbi__clamp((x0-t3) >> 17);
o[1] = stbi__clamp((x1+t2) >> 17);
o[6] = stbi__clamp((x1-t2) >> 17);
o[2] = stbi__clamp((x2+t1) >> 17);
o[5] = stbi__clamp((x2-t1) >> 17);
o[3] = stbi__clamp((x3+t0) >> 17);
o[4] = stbi__clamp((x3-t0) >> 17);
}
}
#ifdef STBI_SSE2
// sse2 integer IDCT. not the fastest possible implementation but it
// produces bit-identical results to the generic C version so it's
// fully "transparent".
static void stbi__idct_simd(stbi_uc *out, int out_stride, short data[64])
{
// This is constructed to match our regular (generic) integer IDCT exactly.
__m128i row0, row1, row2, row3, row4, row5, row6, row7;
__m128i tmp;
// dot product constant: even elems=x, odd elems=y
#define dct_const(x,y) _mm_setr_epi16((x),(y),(x),(y),(x),(y),(x),(y))
// out(0) = c0[even]*x + c0[odd]*y (c0, x, y 16-bit, out 32-bit)
// out(1) = c1[even]*x + c1[odd]*y
#define dct_rot(out0,out1, x,y,c0,c1) \
__m128i c0##lo = _mm_unpacklo_epi16((x),(y)); \
__m128i c0##hi = _mm_unpackhi_epi16((x),(y)); \
__m128i out0##_l = _mm_madd_epi16(c0##lo, c0); \
__m128i out0##_h = _mm_madd_epi16(c0##hi, c0); \
__m128i out1##_l = _mm_madd_epi16(c0##lo, c1); \
__m128i out1##_h = _mm_madd_epi16(c0##hi, c1)
// out = in << 12 (in 16-bit, out 32-bit)
#define dct_widen(out, in) \
__m128i out##_l = _mm_srai_epi32(_mm_unpacklo_epi16(_mm_setzero_si128(), (in)), 4); \
__m128i out##_h = _mm_srai_epi32(_mm_unpackhi_epi16(_mm_setzero_si128(), (in)), 4)
// wide add
#define dct_wadd(out, a, b) \
__m128i out##_l = _mm_add_epi32(a##_l, b##_l); \
__m128i out##_h = _mm_add_epi32(a##_h, b##_h)
// wide sub
#define dct_wsub(out, a, b) \
__m128i out##_l = _mm_sub_epi32(a##_l, b##_l); \
__m128i out##_h = _mm_sub_epi32(a##_h, b##_h)
// butterfly a/b, add bias, then shift by "s" and pack
#define dct_bfly32o(out0, out1, a,b,bias,s) \
{ \
__m128i abiased_l = _mm_add_epi32(a##_l, bias); \
__m128i abiased_h = _mm_add_epi32(a##_h, bias); \
dct_wadd(sum, abiased, b); \
dct_wsub(dif, abiased, b); \
out0 = _mm_packs_epi32(_mm_srai_epi32(sum_l, s), _mm_srai_epi32(sum_h, s)); \
out1 = _mm_packs_epi32(_mm_srai_epi32(dif_l, s), _mm_srai_epi32(dif_h, s)); \
}
// 8-bit interleave step (for transposes)
#define dct_interleave8(a, b) \
tmp = a; \
a = _mm_unpacklo_epi8(a, b); \
b = _mm_unpackhi_epi8(tmp, b)
// 16-bit interleave step (for transposes)
#define dct_interleave16(a, b) \
tmp = a; \
a = _mm_unpacklo_epi16(a, b); \
b = _mm_unpackhi_epi16(tmp, b)
#define dct_pass(bias,shift) \
{ \
/* even part */ \
dct_rot(t2e,t3e, row2,row6, rot0_0,rot0_1); \
__m128i sum04 = _mm_add_epi16(row0, row4); \
__m128i dif04 = _mm_sub_epi16(row0, row4); \
dct_widen(t0e, sum04); \
dct_widen(t1e, dif04); \
dct_wadd(x0, t0e, t3e); \
dct_wsub(x3, t0e, t3e); \
dct_wadd(x1, t1e, t2e); \
dct_wsub(x2, t1e, t2e); \
/* odd part */ \
dct_rot(y0o,y2o, row7,row3, rot2_0,rot2_1); \
dct_rot(y1o,y3o, row5,row1, rot3_0,rot3_1); \
__m128i sum17 = _mm_add_epi16(row1, row7); \
__m128i sum35 = _mm_add_epi16(row3, row5); \
dct_rot(y4o,y5o, sum17,sum35, rot1_0,rot1_1); \
dct_wadd(x4, y0o, y4o); \
dct_wadd(x5, y1o, y5o); \
dct_wadd(x6, y2o, y5o); \
dct_wadd(x7, y3o, y4o); \
dct_bfly32o(row0,row7, x0,x7,bias,shift); \
dct_bfly32o(row1,row6, x1,x6,bias,shift); \
dct_bfly32o(row2,row5, x2,x5,bias,shift); \
dct_bfly32o(row3,row4, x3,x4,bias,shift); \
}
__m128i rot0_0 = dct_const(stbi__f2f(0.5411961f), stbi__f2f(0.5411961f) + stbi__f2f(-1.847759065f));
__m128i rot0_1 = dct_const(stbi__f2f(0.5411961f) + stbi__f2f( 0.765366865f), stbi__f2f(0.5411961f));
__m128i rot1_0 = dct_const(stbi__f2f(1.175875602f) + stbi__f2f(-0.899976223f), stbi__f2f(1.175875602f));
__m128i rot1_1 = dct_const(stbi__f2f(1.175875602f), stbi__f2f(1.175875602f) + stbi__f2f(-2.562915447f));
__m128i rot2_0 = dct_const(stbi__f2f(-1.961570560f) + stbi__f2f( 0.298631336f), stbi__f2f(-1.961570560f));
__m128i rot2_1 = dct_const(stbi__f2f(-1.961570560f), stbi__f2f(-1.961570560f) + stbi__f2f( 3.072711026f));
__m128i rot3_0 = dct_const(stbi__f2f(-0.390180644f) + stbi__f2f( 2.053119869f), stbi__f2f(-0.390180644f));
__m128i rot3_1 = dct_const(stbi__f2f(-0.390180644f), stbi__f2f(-0.390180644f) + stbi__f2f( 1.501321110f));
// rounding biases in column/row passes, see stbi__idct_block for explanation.
__m128i bias_0 = _mm_set1_epi32(512);
__m128i bias_1 = _mm_set1_epi32(65536 + (128<<17));
// load
row0 = _mm_load_si128((const __m128i *) (data + 0*8));
row1 = _mm_load_si128((const __m128i *) (data + 1*8));
row2 = _mm_load_si128((const __m128i *) (data + 2*8));
row3 = _mm_load_si128((const __m128i *) (data + 3*8));
row4 = _mm_load_si128((const __m128i *) (data + 4*8));
row5 = _mm_load_si128((const __m128i *) (data + 5*8));
row6 = _mm_load_si128((const __m128i *) (data + 6*8));
row7 = _mm_load_si128((const __m128i *) (data + 7*8));
// column pass
dct_pass(bias_0, 10);
{
// 16bit 8x8 transpose pass 1
dct_interleave16(row0, row4);
dct_interleave16(row1, row5);
dct_interleave16(row2, row6);
dct_interleave16(row3, row7);
// transpose pass 2
dct_interleave16(row0, row2);
dct_interleave16(row1, row3);
dct_interleave16(row4, row6);
dct_interleave16(row5, row7);
// transpose pass 3
dct_interleave16(row0, row1);
dct_interleave16(row2, row3);
dct_interleave16(row4, row5);
dct_interleave16(row6, row7);
}
// row pass
dct_pass(bias_1, 17);
{
// pack
__m128i p0 = _mm_packus_epi16(row0, row1); // a0a1a2a3...a7b0b1b2b3...b7
__m128i p1 = _mm_packus_epi16(row2, row3);
__m128i p2 = _mm_packus_epi16(row4, row5);
__m128i p3 = _mm_packus_epi16(row6, row7);
// 8bit 8x8 transpose pass 1
dct_interleave8(p0, p2); // a0e0a1e1...
dct_interleave8(p1, p3); // c0g0c1g1...
// transpose pass 2
dct_interleave8(p0, p1); // a0c0e0g0...
dct_interleave8(p2, p3); // b0d0f0h0...
// transpose pass 3
dct_interleave8(p0, p2); // a0b0c0d0...
dct_interleave8(p1, p3); // a4b4c4d4...
// store
_mm_storel_epi64((__m128i *) out, p0); out += out_stride;
_mm_storel_epi64((__m128i *) out, _mm_shuffle_epi32(p0, 0x4e)); out += out_stride;
_mm_storel_epi64((__m128i *) out, p2); out += out_stride;
_mm_storel_epi64((__m128i *) out, _mm_shuffle_epi32(p2, 0x4e)); out += out_stride;
_mm_storel_epi64((__m128i *) out, p1); out += out_stride;
_mm_storel_epi64((__m128i *) out, _mm_shuffle_epi32(p1, 0x4e)); out += out_stride;
_mm_storel_epi64((__m128i *) out, p3); out += out_stride;
_mm_storel_epi64((__m128i *) out, _mm_shuffle_epi32(p3, 0x4e));
}
#undef dct_const
#undef dct_rot
#undef dct_widen
#undef dct_wadd
#undef dct_wsub
#undef dct_bfly32o
#undef dct_interleave8
#undef dct_interleave16
#undef dct_pass
}
#endif // STBI_SSE2
#ifdef STBI_NEON
// NEON integer IDCT. should produce bit-identical
// results to the generic C version.
static void stbi__idct_simd(stbi_uc *out, int out_stride, short data[64])
{
int16x8_t row0, row1, row2, row3, row4, row5, row6, row7;
int16x4_t rot0_0 = vdup_n_s16(stbi__f2f(0.5411961f));
int16x4_t rot0_1 = vdup_n_s16(stbi__f2f(-1.847759065f));
int16x4_t rot0_2 = vdup_n_s16(stbi__f2f( 0.765366865f));
int16x4_t rot1_0 = vdup_n_s16(stbi__f2f( 1.175875602f));
int16x4_t rot1_1 = vdup_n_s16(stbi__f2f(-0.899976223f));
int16x4_t rot1_2 = vdup_n_s16(stbi__f2f(-2.562915447f));
int16x4_t rot2_0 = vdup_n_s16(stbi__f2f(-1.961570560f));
int16x4_t rot2_1 = vdup_n_s16(stbi__f2f(-0.390180644f));
int16x4_t rot3_0 = vdup_n_s16(stbi__f2f( 0.298631336f));
int16x4_t rot3_1 = vdup_n_s16(stbi__f2f( 2.053119869f));
int16x4_t rot3_2 = vdup_n_s16(stbi__f2f( 3.072711026f));
int16x4_t rot3_3 = vdup_n_s16(stbi__f2f( 1.501321110f));
#define dct_long_mul(out, inq, coeff) \
int32x4_t out##_l = vmull_s16(vget_low_s16(inq), coeff); \
int32x4_t out##_h = vmull_s16(vget_high_s16(inq), coeff)
#define dct_long_mac(out, acc, inq, coeff) \
int32x4_t out##_l = vmlal_s16(acc##_l, vget_low_s16(inq), coeff); \
int32x4_t out##_h = vmlal_s16(acc##_h, vget_high_s16(inq), coeff)
#define dct_widen(out, inq) \
int32x4_t out##_l = vshll_n_s16(vget_low_s16(inq), 12); \
int32x4_t out##_h = vshll_n_s16(vget_high_s16(inq), 12)
// wide add
#define dct_wadd(out, a, b) \
int32x4_t out##_l = vaddq_s32(a##_l, b##_l); \
int32x4_t out##_h = vaddq_s32(a##_h, b##_h)
// wide sub
#define dct_wsub(out, a, b) \
int32x4_t out##_l = vsubq_s32(a##_l, b##_l); \
int32x4_t out##_h = vsubq_s32(a##_h, b##_h)
// butterfly a/b, then shift using "shiftop" by "s" and pack
#define dct_bfly32o(out0,out1, a,b,shiftop,s) \
{ \
dct_wadd(sum, a, b); \
dct_wsub(dif, a, b); \
out0 = vcombine_s16(shiftop(sum_l, s), shiftop(sum_h, s)); \
out1 = vcombine_s16(shiftop(dif_l, s), shiftop(dif_h, s)); \
}
#define dct_pass(shiftop, shift) \
{ \
/* even part */ \
int16x8_t sum26 = vaddq_s16(row2, row6); \
dct_long_mul(p1e, sum26, rot0_0); \
dct_long_mac(t2e, p1e, row6, rot0_1); \
dct_long_mac(t3e, p1e, row2, rot0_2); \
int16x8_t sum04 = vaddq_s16(row0, row4); \
int16x8_t dif04 = vsubq_s16(row0, row4); \
dct_widen(t0e, sum04); \
dct_widen(t1e, dif04); \
dct_wadd(x0, t0e, t3e); \
dct_wsub(x3, t0e, t3e); \
dct_wadd(x1, t1e, t2e); \
dct_wsub(x2, t1e, t2e); \
/* odd part */ \
int16x8_t sum15 = vaddq_s16(row1, row5); \
int16x8_t sum17 = vaddq_s16(row1, row7); \
int16x8_t sum35 = vaddq_s16(row3, row5); \
int16x8_t sum37 = vaddq_s16(row3, row7); \
int16x8_t sumodd = vaddq_s16(sum17, sum35); \
dct_long_mul(p5o, sumodd, rot1_0); \
dct_long_mac(p1o, p5o, sum17, rot1_1); \
dct_long_mac(p2o, p5o, sum35, rot1_2); \
dct_long_mul(p3o, sum37, rot2_0); \
dct_long_mul(p4o, sum15, rot2_1); \
dct_wadd(sump13o, p1o, p3o); \
dct_wadd(sump24o, p2o, p4o); \
dct_wadd(sump23o, p2o, p3o); \
dct_wadd(sump14o, p1o, p4o); \
dct_long_mac(x4, sump13o, row7, rot3_0); \
dct_long_mac(x5, sump24o, row5, rot3_1); \
dct_long_mac(x6, sump23o, row3, rot3_2); \
dct_long_mac(x7, sump14o, row1, rot3_3); \
dct_bfly32o(row0,row7, x0,x7,shiftop,shift); \
dct_bfly32o(row1,row6, x1,x6,shiftop,shift); \
dct_bfly32o(row2,row5, x2,x5,shiftop,shift); \
dct_bfly32o(row3,row4, x3,x4,shiftop,shift); \
}
// load
row0 = vld1q_s16(data + 0*8);
row1 = vld1q_s16(data + 1*8);
row2 = vld1q_s16(data + 2*8);
row3 = vld1q_s16(data + 3*8);
row4 = vld1q_s16(data + 4*8);
row5 = vld1q_s16(data + 5*8);
row6 = vld1q_s16(data + 6*8);
row7 = vld1q_s16(data + 7*8);
// add DC bias
row0 = vaddq_s16(row0, vsetq_lane_s16(1024, vdupq_n_s16(0), 0));
// column pass
dct_pass(vrshrn_n_s32, 10);
// 16bit 8x8 transpose
{
// these three map to a single VTRN.16, VTRN.32, and VSWP, respectively.
// whether compilers actually get this is another story, sadly.
#define dct_trn16(x, y) { int16x8x2_t t = vtrnq_s16(x, y); x = t.val[0]; y = t.val[1]; }
#define dct_trn32(x, y) { int32x4x2_t t = vtrnq_s32(vreinterpretq_s32_s16(x), vreinterpretq_s32_s16(y)); x = vreinterpretq_s16_s32(t.val[0]); y = vreinterpretq_s16_s32(t.val[1]); }
#define dct_trn64(x, y) { int16x8_t x0 = x; int16x8_t y0 = y; x = vcombine_s16(vget_low_s16(x0), vget_low_s16(y0)); y = vcombine_s16(vget_high_s16(x0), vget_high_s16(y0)); }
// pass 1
dct_trn16(row0, row1); // a0b0a2b2a4b4a6b6
dct_trn16(row2, row3);
dct_trn16(row4, row5);
dct_trn16(row6, row7);
// pass 2
dct_trn32(row0, row2); // a0b0c0d0a4b4c4d4
dct_trn32(row1, row3);
dct_trn32(row4, row6);
dct_trn32(row5, row7);
// pass 3
dct_trn64(row0, row4); // a0b0c0d0e0f0g0h0
dct_trn64(row1, row5);
dct_trn64(row2, row6);
dct_trn64(row3, row7);
#undef dct_trn16
#undef dct_trn32
#undef dct_trn64
}
// row pass
// vrshrn_n_s32 only supports shifts up to 16, we need
// 17. so do a non-rounding shift of 16 first then follow
// up with a rounding shift by 1.
dct_pass(vshrn_n_s32, 16);
{
// pack and round
uint8x8_t p0 = vqrshrun_n_s16(row0, 1);
uint8x8_t p1 = vqrshrun_n_s16(row1, 1);
uint8x8_t p2 = vqrshrun_n_s16(row2, 1);
uint8x8_t p3 = vqrshrun_n_s16(row3, 1);
uint8x8_t p4 = vqrshrun_n_s16(row4, 1);
uint8x8_t p5 = vqrshrun_n_s16(row5, 1);
uint8x8_t p6 = vqrshrun_n_s16(row6, 1);
uint8x8_t p7 = vqrshrun_n_s16(row7, 1);
// again, these can translate into one instruction, but often don't.
#define dct_trn8_8(x, y) { uint8x8x2_t t = vtrn_u8(x, y); x = t.val[0]; y = t.val[1]; }
#define dct_trn8_16(x, y) { uint16x4x2_t t = vtrn_u16(vreinterpret_u16_u8(x), vreinterpret_u16_u8(y)); x = vreinterpret_u8_u16(t.val[0]); y = vreinterpret_u8_u16(t.val[1]); }
#define dct_trn8_32(x, y) { uint32x2x2_t t = vtrn_u32(vreinterpret_u32_u8(x), vreinterpret_u32_u8(y)); x = vreinterpret_u8_u32(t.val[0]); y = vreinterpret_u8_u32(t.val[1]); }
// sadly can't use interleaved stores here since we only write
// 8 bytes to each scan line!
// 8x8 8-bit transpose pass 1
dct_trn8_8(p0, p1);
dct_trn8_8(p2, p3);
dct_trn8_8(p4, p5);
dct_trn8_8(p6, p7);
// pass 2
dct_trn8_16(p0, p2);
dct_trn8_16(p1, p3);
dct_trn8_16(p4, p6);
dct_trn8_16(p5, p7);
// pass 3
dct_trn8_32(p0, p4);
dct_trn8_32(p1, p5);
dct_trn8_32(p2, p6);
dct_trn8_32(p3, p7);
// store
vst1_u8(out, p0); out += out_stride;
vst1_u8(out, p1); out += out_stride;
vst1_u8(out, p2); out += out_stride;
vst1_u8(out, p3); out += out_stride;
vst1_u8(out, p4); out += out_stride;
vst1_u8(out, p5); out += out_stride;
vst1_u8(out, p6); out += out_stride;
vst1_u8(out, p7);
#undef dct_trn8_8
#undef dct_trn8_16
#undef dct_trn8_32
}
#undef dct_long_mul
#undef dct_long_mac
#undef dct_widen
#undef dct_wadd
#undef dct_wsub
#undef dct_bfly32o
#undef dct_pass
}
#endif // STBI_NEON
#define STBI__MARKER_none 0xff
// if there's a pending marker from the entropy stream, return that
// otherwise, fetch from the stream and get a marker. if there's no
// marker, return 0xff, which is never a valid marker value
static stbi_uc stbi__get_marker(stbi__jpeg *j)
{
stbi_uc x;
if (j->marker != STBI__MARKER_none) { x = j->marker; j->marker = STBI__MARKER_none; return x; }
x = stbi__get8(j->s);
if (x != 0xff) return STBI__MARKER_none;
while (x == 0xff)
x = stbi__get8(j->s); // consume repeated 0xff fill bytes
return x;
}
// in each scan, we'll have scan_n components, and the order
// of the components is specified by order[]
#define STBI__RESTART(x) ((x) >= 0xd0 && (x) <= 0xd7)
// after a restart interval, stbi__jpeg_reset the entropy decoder and
// the dc prediction
static void stbi__jpeg_reset(stbi__jpeg *j)
{
j->code_bits = 0;
j->code_buffer = 0;
j->nomore = 0;
j->img_comp[0].dc_pred = j->img_comp[1].dc_pred = j->img_comp[2].dc_pred = j->img_comp[3].dc_pred = 0;
j->marker = STBI__MARKER_none;
j->todo = j->restart_interval ? j->restart_interval : 0x7fffffff;
j->eob_run = 0;
// no more than 1<<31 MCUs if no restart_interal? that's plenty safe,
// since we don't even allow 1<<30 pixels
}
static int stbi__parse_entropy_coded_data(stbi__jpeg *z)
{
stbi__jpeg_reset(z);
if (!z->progressive) {
if (z->scan_n == 1) {
int i,j;
STBI_SIMD_ALIGN(short, data[64]);
int n = z->order[0];
// non-interleaved data, we just need to process one block at a time,
// in trivial scanline order
// number of blocks to do just depends on how many actual "pixels" this
// component has, independent of interleaved MCU blocking and such
int w = (z->img_comp[n].x+7) >> 3;
int h = (z->img_comp[n].y+7) >> 3;
for (j=0; j < h; ++j) {
for (i=0; i < w; ++i) {
int ha = z->img_comp[n].ha;
if (!stbi__jpeg_decode_block(z, data, z->huff_dc+z->img_comp[n].hd, z->huff_ac+ha, z->fast_ac[ha], n, z->dequant[z->img_comp[n].tq])) return 0;
z->idct_block_kernel(z->img_comp[n].data+z->img_comp[n].w2*j*8+i*8, z->img_comp[n].w2, data);
// every data block is an MCU, so countdown the restart interval
if (--z->todo <= 0) {
if (z->code_bits < 24) stbi__grow_buffer_unsafe(z);
// if it's NOT a restart, then just bail, so we get corrupt data
// rather than no data
if (!STBI__RESTART(z->marker)) return 1;
stbi__jpeg_reset(z);
}
}
}
return 1;
} else { // interleaved
int i,j,k,x,y;
STBI_SIMD_ALIGN(short, data[64]);
for (j=0; j < z->img_mcu_y; ++j) {
for (i=0; i < z->img_mcu_x; ++i) {
// scan an interleaved mcu... process scan_n components in order
for (k=0; k < z->scan_n; ++k) {
int n = z->order[k];
// scan out an mcu's worth of this component; that's just determined
// by the basic H and V specified for the component
for (y=0; y < z->img_comp[n].v; ++y) {
for (x=0; x < z->img_comp[n].h; ++x) {
int x2 = (i*z->img_comp[n].h + x)*8;
int y2 = (j*z->img_comp[n].v + y)*8;
int ha = z->img_comp[n].ha;
if (!stbi__jpeg_decode_block(z, data, z->huff_dc+z->img_comp[n].hd, z->huff_ac+ha, z->fast_ac[ha], n, z->dequant[z->img_comp[n].tq])) return 0;
z->idct_block_kernel(z->img_comp[n].data+z->img_comp[n].w2*y2+x2, z->img_comp[n].w2, data);
}
}
}
// after all interleaved components, that's an interleaved MCU,
// so now count down the restart interval
if (--z->todo <= 0) {
if (z->code_bits < 24) stbi__grow_buffer_unsafe(z);
if (!STBI__RESTART(z->marker)) return 1;
stbi__jpeg_reset(z);
}
}
}
return 1;
}
} else {
if (z->scan_n == 1) {
int i,j;
int n = z->order[0];
// non-interleaved data, we just need to process one block at a time,
// in trivial scanline order
// number of blocks to do just depends on how many actual "pixels" this
// component has, independent of interleaved MCU blocking and such
int w = (z->img_comp[n].x+7) >> 3;
int h = (z->img_comp[n].y+7) >> 3;
for (j=0; j < h; ++j) {
for (i=0; i < w; ++i) {
short *data = z->img_comp[n].coeff + 64 * (i + j * z->img_comp[n].coeff_w);
if (z->spec_start == 0) {
if (!stbi__jpeg_decode_block_prog_dc(z, data, &z->huff_dc[z->img_comp[n].hd], n))
return 0;
} else {
int ha = z->img_comp[n].ha;
if (!stbi__jpeg_decode_block_prog_ac(z, data, &z->huff_ac[ha], z->fast_ac[ha]))
return 0;
}
// every data block is an MCU, so countdown the restart interval
if (--z->todo <= 0) {
if (z->code_bits < 24) stbi__grow_buffer_unsafe(z);
if (!STBI__RESTART(z->marker)) return 1;
stbi__jpeg_reset(z);
}
}
}
return 1;
} else { // interleaved
int i,j,k,x,y;
for (j=0; j < z->img_mcu_y; ++j) {
for (i=0; i < z->img_mcu_x; ++i) {
// scan an interleaved mcu... process scan_n components in order
for (k=0; k < z->scan_n; ++k) {
int n = z->order[k];
// scan out an mcu's worth of this component; that's just determined
// by the basic H and V specified for the component
for (y=0; y < z->img_comp[n].v; ++y) {
for (x=0; x < z->img_comp[n].h; ++x) {
int x2 = (i*z->img_comp[n].h + x);
int y2 = (j*z->img_comp[n].v + y);
short *data = z->img_comp[n].coeff + 64 * (x2 + y2 * z->img_comp[n].coeff_w);
if (!stbi__jpeg_decode_block_prog_dc(z, data, &z->huff_dc[z->img_comp[n].hd], n))
return 0;
}
}
}
// after all interleaved components, that's an interleaved MCU,
// so now count down the restart interval
if (--z->todo <= 0) {
if (z->code_bits < 24) stbi__grow_buffer_unsafe(z);
if (!STBI__RESTART(z->marker)) return 1;
stbi__jpeg_reset(z);
}
}
}
return 1;
}
}
}
static void stbi__jpeg_dequantize(short *data, stbi__uint16 *dequant)
{
int i;
for (i=0; i < 64; ++i)
data[i] *= dequant[i];
}
static void stbi__jpeg_finish(stbi__jpeg *z)
{
if (z->progressive) {
// dequantize and idct the data
int i,j,n;
for (n=0; n < z->s->img_n; ++n) {
int w = (z->img_comp[n].x+7) >> 3;
int h = (z->img_comp[n].y+7) >> 3;
for (j=0; j < h; ++j) {
for (i=0; i < w; ++i) {
short *data = z->img_comp[n].coeff + 64 * (i + j * z->img_comp[n].coeff_w);
stbi__jpeg_dequantize(data, z->dequant[z->img_comp[n].tq]);
z->idct_block_kernel(z->img_comp[n].data+z->img_comp[n].w2*j*8+i*8, z->img_comp[n].w2, data);
}
}
}
}
}
static int stbi__process_marker(stbi__jpeg *z, int m)
{
int L;
switch (m) {
case STBI__MARKER_none: // no marker found
return stbi__err("expected marker","Corrupt JPEG");
case 0xDD: // DRI - specify restart interval
if (stbi__get16be(z->s) != 4) return stbi__err("bad DRI len","Corrupt JPEG");
z->restart_interval = stbi__get16be(z->s);
return 1;
case 0xDB: // DQT - define quantization table
L = stbi__get16be(z->s)-2;
while (L > 0) {
int q = stbi__get8(z->s);
int p = q >> 4, sixteen = (p != 0);
int t = q & 15,i;
if (p != 0 && p != 1) return stbi__err("bad DQT type","Corrupt JPEG");
if (t > 3) return stbi__err("bad DQT table","Corrupt JPEG");
for (i=0; i < 64; ++i)
z->dequant[t][stbi__jpeg_dezigzag[i]] = (stbi__uint16)(sixteen ? stbi__get16be(z->s) : stbi__get8(z->s));
L -= (sixteen ? 129 : 65);
}
return L==0;
case 0xC4: // DHT - define huffman table
L = stbi__get16be(z->s)-2;
while (L > 0) {
stbi_uc *v;
int sizes[16],i,n=0;
int q = stbi__get8(z->s);
int tc = q >> 4;
int th = q & 15;
if (tc > 1 || th > 3) return stbi__err("bad DHT header","Corrupt JPEG");
for (i=0; i < 16; ++i) {
sizes[i] = stbi__get8(z->s);
n += sizes[i];
}
L -= 17;
if (tc == 0) {
if (!stbi__build_huffman(z->huff_dc+th, sizes)) return 0;
v = z->huff_dc[th].values;
} else {
if (!stbi__build_huffman(z->huff_ac+th, sizes)) return 0;
v = z->huff_ac[th].values;
}
for (i=0; i < n; ++i)
v[i] = stbi__get8(z->s);
if (tc != 0)
stbi__build_fast_ac(z->fast_ac[th], z->huff_ac + th);
L -= n;
}
return L==0;
}
// check for comment block or APP blocks
if ((m >= 0xE0 && m <= 0xEF) || m == 0xFE) {
L = stbi__get16be(z->s);
if (L < 2) {
if (m == 0xFE)
return stbi__err("bad COM len","Corrupt JPEG");
else
return stbi__err("bad APP len","Corrupt JPEG");
}
L -= 2;
if (m == 0xE0 && L >= 5) { // JFIF APP0 segment
static const unsigned char tag[5] = {'J','F','I','F','\0'};
int ok = 1;
int i;
for (i=0; i < 5; ++i)
if (stbi__get8(z->s) != tag[i])
ok = 0;
L -= 5;
if (ok)
z->jfif = 1;
} else if (m == 0xEE && L >= 12) { // Adobe APP14 segment
static const unsigned char tag[6] = {'A','d','o','b','e','\0'};
int ok = 1;
int i;
for (i=0; i < 6; ++i)
if (stbi__get8(z->s) != tag[i])
ok = 0;
L -= 6;
if (ok) {
stbi__get8(z->s); // version
stbi__get16be(z->s); // flags0
stbi__get16be(z->s); // flags1
z->app14_color_transform = stbi__get8(z->s); // color transform
L -= 6;
}
}
stbi__skip(z->s, L);
return 1;
}
return stbi__err("unknown marker","Corrupt JPEG");
}
// after we see SOS
static int stbi__process_scan_header(stbi__jpeg *z)
{
int i;
int Ls = stbi__get16be(z->s);
z->scan_n = stbi__get8(z->s);
if (z->scan_n < 1 || z->scan_n > 4 || z->scan_n > (int) z->s->img_n) return stbi__err("bad SOS component count","Corrupt JPEG");
if (Ls != 6+2*z->scan_n) return stbi__err("bad SOS len","Corrupt JPEG");
for (i=0; i < z->scan_n; ++i) {
int id = stbi__get8(z->s), which;
int q = stbi__get8(z->s);
for (which = 0; which < z->s->img_n; ++which)
if (z->img_comp[which].id == id)
break;
if (which == z->s->img_n) return 0; // no match
z->img_comp[which].hd = q >> 4; if (z->img_comp[which].hd > 3) return stbi__err("bad DC huff","Corrupt JPEG");
z->img_comp[which].ha = q & 15; if (z->img_comp[which].ha > 3) return stbi__err("bad AC huff","Corrupt JPEG");
z->order[i] = which;
}
{
int aa;
z->spec_start = stbi__get8(z->s);
z->spec_end = stbi__get8(z->s); // should be 63, but might be 0
aa = stbi__get8(z->s);
z->succ_high = (aa >> 4);
z->succ_low = (aa & 15);
if (z->progressive) {
if (z->spec_start > 63 || z->spec_end > 63 || z->spec_start > z->spec_end || z->succ_high > 13 || z->succ_low > 13)
return stbi__err("bad SOS", "Corrupt JPEG");
} else {
if (z->spec_start != 0) return stbi__err("bad SOS","Corrupt JPEG");
if (z->succ_high != 0 || z->succ_low != 0) return stbi__err("bad SOS","Corrupt JPEG");
z->spec_end = 63;
}
}
return 1;
}
static int stbi__free_jpeg_components(stbi__jpeg *z, int ncomp, int why)
{
int i;
for (i=0; i < ncomp; ++i) {
if (z->img_comp[i].raw_data) {
STBI_FREE(z->img_comp[i].raw_data);
z->img_comp[i].raw_data = NULL;
z->img_comp[i].data = NULL;
}
if (z->img_comp[i].raw_coeff) {
STBI_FREE(z->img_comp[i].raw_coeff);
z->img_comp[i].raw_coeff = 0;
z->img_comp[i].coeff = 0;
}
if (z->img_comp[i].linebuf) {
STBI_FREE(z->img_comp[i].linebuf);
z->img_comp[i].linebuf = NULL;
}
}
return why;
}
static int stbi__process_frame_header(stbi__jpeg *z, int scan)
{
stbi__context *s = z->s;
int Lf,p,i,q, h_max=1,v_max=1,c;
Lf = stbi__get16be(s); if (Lf < 11) return stbi__err("bad SOF len","Corrupt JPEG"); // JPEG
p = stbi__get8(s); if (p != 8) return stbi__err("only 8-bit","JPEG format not supported: 8-bit only"); // JPEG baseline
s->img_y = stbi__get16be(s); if (s->img_y == 0) return stbi__err("no header height", "JPEG format not supported: delayed height"); // Legal, but we don't handle it--but neither does IJG
s->img_x = stbi__get16be(s); if (s->img_x == 0) return stbi__err("0 width","Corrupt JPEG"); // JPEG requires
c = stbi__get8(s);
if (c != 3 && c != 1 && c != 4) return stbi__err("bad component count","Corrupt JPEG");
s->img_n = c;
for (i=0; i < c; ++i) {
z->img_comp[i].data = NULL;
z->img_comp[i].linebuf = NULL;
}
if (Lf != 8+3*s->img_n) return stbi__err("bad SOF len","Corrupt JPEG");
z->rgb = 0;
for (i=0; i < s->img_n; ++i) {
static const unsigned char rgb[3] = { 'R', 'G', 'B' };
z->img_comp[i].id = stbi__get8(s);
if (s->img_n == 3 && z->img_comp[i].id == rgb[i])
++z->rgb;
q = stbi__get8(s);
z->img_comp[i].h = (q >> 4); if (!z->img_comp[i].h || z->img_comp[i].h > 4) return stbi__err("bad H","Corrupt JPEG");
z->img_comp[i].v = q & 15; if (!z->img_comp[i].v || z->img_comp[i].v > 4) return stbi__err("bad V","Corrupt JPEG");
z->img_comp[i].tq = stbi__get8(s); if (z->img_comp[i].tq > 3) return stbi__err("bad TQ","Corrupt JPEG");
}
if (scan != STBI__SCAN_load) return 1;
if (!stbi__mad3sizes_valid(s->img_x, s->img_y, s->img_n, 0)) return stbi__err("too large", "Image too large to decode");
for (i=0; i < s->img_n; ++i) {
if (z->img_comp[i].h > h_max) h_max = z->img_comp[i].h;
if (z->img_comp[i].v > v_max) v_max = z->img_comp[i].v;
}
// compute interleaved mcu info
z->img_h_max = h_max;
z->img_v_max = v_max;
z->img_mcu_w = h_max * 8;
z->img_mcu_h = v_max * 8;
// these sizes can't be more than 17 bits
z->img_mcu_x = (s->img_x + z->img_mcu_w-1) / z->img_mcu_w;
z->img_mcu_y = (s->img_y + z->img_mcu_h-1) / z->img_mcu_h;
for (i=0; i < s->img_n; ++i) {
// number of effective pixels (e.g. for non-interleaved MCU)
z->img_comp[i].x = (s->img_x * z->img_comp[i].h + h_max-1) / h_max;
z->img_comp[i].y = (s->img_y * z->img_comp[i].v + v_max-1) / v_max;
// to simplify generation, we'll allocate enough memory to decode
// the bogus oversized data from using interleaved MCUs and their
// big blocks (e.g. a 16x16 iMCU on an image of width 33); we won't
// discard the extra data until colorspace conversion
//
// img_mcu_x, img_mcu_y: <=17 bits; comp[i].h and .v are <=4 (checked earlier)
// so these muls can't overflow with 32-bit ints (which we require)
z->img_comp[i].w2 = z->img_mcu_x * z->img_comp[i].h * 8;
z->img_comp[i].h2 = z->img_mcu_y * z->img_comp[i].v * 8;
z->img_comp[i].coeff = 0;
z->img_comp[i].raw_coeff = 0;
z->img_comp[i].linebuf = NULL;
z->img_comp[i].raw_data = stbi__malloc_mad2(z->img_comp[i].w2, z->img_comp[i].h2, 15);
if (z->img_comp[i].raw_data == NULL)
return stbi__free_jpeg_components(z, i+1, stbi__err("outofmem", "Out of memory"));
// align blocks for idct using mmx/sse
z->img_comp[i].data = (stbi_uc*) (((size_t) z->img_comp[i].raw_data + 15) & ~15);
if (z->progressive) {
// w2, h2 are multiples of 8 (see above)
z->img_comp[i].coeff_w = z->img_comp[i].w2 / 8;
z->img_comp[i].coeff_h = z->img_comp[i].h2 / 8;
z->img_comp[i].raw_coeff = stbi__malloc_mad3(z->img_comp[i].w2, z->img_comp[i].h2, sizeof(short), 15);
if (z->img_comp[i].raw_coeff == NULL)
return stbi__free_jpeg_components(z, i+1, stbi__err("outofmem", "Out of memory"));
z->img_comp[i].coeff = (short*) (((size_t) z->img_comp[i].raw_coeff + 15) & ~15);
}
}
return 1;
}
// use comparisons since in some cases we handle more than one case (e.g. SOF)
#define stbi__DNL(x) ((x) == 0xdc)
#define stbi__SOI(x) ((x) == 0xd8)
#define stbi__EOI(x) ((x) == 0xd9)
#define stbi__SOF(x) ((x) == 0xc0 || (x) == 0xc1 || (x) == 0xc2)
#define stbi__SOS(x) ((x) == 0xda)
#define stbi__SOF_progressive(x) ((x) == 0xc2)
static int stbi__decode_jpeg_header(stbi__jpeg *z, int scan)
{
int m;
z->jfif = 0;
z->app14_color_transform = -1; // valid values are 0,1,2
z->marker = STBI__MARKER_none; // initialize cached marker to empty
m = stbi__get_marker(z);
if (!stbi__SOI(m)) return stbi__err("no SOI","Corrupt JPEG");
if (scan == STBI__SCAN_type) return 1;
m = stbi__get_marker(z);
while (!stbi__SOF(m)) {
if (!stbi__process_marker(z,m)) return 0;
m = stbi__get_marker(z);
while (m == STBI__MARKER_none) {
// some files have extra padding after their blocks, so ok, we'll scan
if (stbi__at_eof(z->s)) return stbi__err("no SOF", "Corrupt JPEG");
m = stbi__get_marker(z);
}
}
z->progressive = stbi__SOF_progressive(m);
if (!stbi__process_frame_header(z, scan)) return 0;
return 1;
}
// decode image to YCbCr format
static int stbi__decode_jpeg_image(stbi__jpeg *j)
{
int m;
for (m = 0; m < 4; m++) {
j->img_comp[m].raw_data = NULL;
j->img_comp[m].raw_coeff = NULL;
}
j->restart_interval = 0;
if (!stbi__decode_jpeg_header(j, STBI__SCAN_load)) return 0;
m = stbi__get_marker(j);
while (!stbi__EOI(m)) {
if (stbi__SOS(m)) {
if (!stbi__process_scan_header(j)) return 0;
if (!stbi__parse_entropy_coded_data(j)) return 0;
if (j->marker == STBI__MARKER_none ) {
// handle 0s at the end of image data from IP Kamera 9060
while (!stbi__at_eof(j->s)) {
int x = stbi__get8(j->s);
if (x == 255) {
j->marker = stbi__get8(j->s);
break;
}
}
// if we reach eof without hitting a marker, stbi__get_marker() below will fail and we'll eventually return 0
}
} else if (stbi__DNL(m)) {
int Ld = stbi__get16be(j->s);
stbi__uint32 NL = stbi__get16be(j->s);
if (Ld != 4) return stbi__err("bad DNL len", "Corrupt JPEG");
if (NL != j->s->img_y) return stbi__err("bad DNL height", "Corrupt JPEG");
} else {
if (!stbi__process_marker(j, m)) return 0;
}
m = stbi__get_marker(j);
}
if (j->progressive)
stbi__jpeg_finish(j);
return 1;
}
// static jfif-centered resampling (across block boundaries)
typedef stbi_uc *(*resample_row_func)(stbi_uc *out, stbi_uc *in0, stbi_uc *in1,
int w, int hs);
#define stbi__div4(x) ((stbi_uc) ((x) >> 2))
static stbi_uc *resample_row_1(stbi_uc *out, stbi_uc *in_near, stbi_uc *in_far, int w, int hs)
{
STBI_NOTUSED(out);
STBI_NOTUSED(in_far);
STBI_NOTUSED(w);
STBI_NOTUSED(hs);
return in_near;
}
static stbi_uc* stbi__resample_row_v_2(stbi_uc *out, stbi_uc *in_near, stbi_uc *in_far, int w, int hs)
{
// need to generate two samples vertically for every one in input
int i;
STBI_NOTUSED(hs);
for (i=0; i < w; ++i)
out[i] = stbi__div4(3*in_near[i] + in_far[i] + 2);
return out;
}
static stbi_uc* stbi__resample_row_h_2(stbi_uc *out, stbi_uc *in_near, stbi_uc *in_far, int w, int hs)
{
// need to generate two samples horizontally for every one in input
int i;
stbi_uc *input = in_near;
if (w == 1) {
// if only one sample, can't do any interpolation
out[0] = out[1] = input[0];
return out;
}
out[0] = input[0];
out[1] = stbi__div4(input[0]*3 + input[1] + 2);
for (i=1; i < w-1; ++i) {
int n = 3*input[i]+2;
out[i*2+0] = stbi__div4(n+input[i-1]);
out[i*2+1] = stbi__div4(n+input[i+1]);
}
out[i*2+0] = stbi__div4(input[w-2]*3 + input[w-1] + 2);
out[i*2+1] = input[w-1];
STBI_NOTUSED(in_far);
STBI_NOTUSED(hs);
return out;
}
#define stbi__div16(x) ((stbi_uc) ((x) >> 4))
static stbi_uc *stbi__resample_row_hv_2(stbi_uc *out, stbi_uc *in_near, stbi_uc *in_far, int w, int hs)
{
// need to generate 2x2 samples for every one in input
int i,t0,t1;
if (w == 1) {
out[0] = out[1] = stbi__div4(3*in_near[0] + in_far[0] + 2);
return out;
}
t1 = 3*in_near[0] + in_far[0];
out[0] = stbi__div4(t1+2);
for (i=1; i < w; ++i) {
t0 = t1;
t1 = 3*in_near[i]+in_far[i];
out[i*2-1] = stbi__div16(3*t0 + t1 + 8);
out[i*2 ] = stbi__div16(3*t1 + t0 + 8);
}
out[w*2-1] = stbi__div4(t1+2);
STBI_NOTUSED(hs);
return out;
}
#if defined(STBI_SSE2) || defined(STBI_NEON)
static stbi_uc *stbi__resample_row_hv_2_simd(stbi_uc *out, stbi_uc *in_near, stbi_uc *in_far, int w, int hs)
{
// need to generate 2x2 samples for every one in input
int i=0,t0,t1;
if (w == 1) {
out[0] = out[1] = stbi__div4(3*in_near[0] + in_far[0] + 2);
return out;
}
t1 = 3*in_near[0] + in_far[0];
// process groups of 8 pixels for as long as we can.
// note we can't handle the last pixel in a row in this loop
// because we need to handle the filter boundary conditions.
for (; i < ((w-1) & ~7); i += 8) {
#if defined(STBI_SSE2)
// load and perform the vertical filtering pass
// this uses 3*x + y = 4*x + (y - x)
__m128i zero = _mm_setzero_si128();
__m128i farb = _mm_loadl_epi64((__m128i *) (in_far + i));
__m128i nearb = _mm_loadl_epi64((__m128i *) (in_near + i));
__m128i farw = _mm_unpacklo_epi8(farb, zero);
__m128i nearw = _mm_unpacklo_epi8(nearb, zero);
__m128i diff = _mm_sub_epi16(farw, nearw);
__m128i nears = _mm_slli_epi16(nearw, 2);
__m128i curr = _mm_add_epi16(nears, diff); // current row
// horizontal filter works the same based on shifted vers of current
// row. "prev" is current row shifted right by 1 pixel; we need to
// insert the previous pixel value (from t1).
// "next" is current row shifted left by 1 pixel, with first pixel
// of next block of 8 pixels added in.
__m128i prv0 = _mm_slli_si128(curr, 2);
__m128i nxt0 = _mm_srli_si128(curr, 2);
__m128i prev = _mm_insert_epi16(prv0, t1, 0);
__m128i next = _mm_insert_epi16(nxt0, 3*in_near[i+8] + in_far[i+8], 7);
// horizontal filter, polyphase implementation since it's convenient:
// even pixels = 3*cur + prev = cur*4 + (prev - cur)
// odd pixels = 3*cur + next = cur*4 + (next - cur)
// note the shared term.
__m128i bias = _mm_set1_epi16(8);
__m128i curs = _mm_slli_epi16(curr, 2);
__m128i prvd = _mm_sub_epi16(prev, curr);
__m128i nxtd = _mm_sub_epi16(next, curr);
__m128i curb = _mm_add_epi16(curs, bias);
__m128i even = _mm_add_epi16(prvd, curb);
__m128i odd = _mm_add_epi16(nxtd, curb);
// interleave even and odd pixels, then undo scaling.
__m128i int0 = _mm_unpacklo_epi16(even, odd);
__m128i int1 = _mm_unpackhi_epi16(even, odd);
__m128i de0 = _mm_srli_epi16(int0, 4);
__m128i de1 = _mm_srli_epi16(int1, 4);
// pack and write output
__m128i outv = _mm_packus_epi16(de0, de1);
_mm_storeu_si128((__m128i *) (out + i*2), outv);
#elif defined(STBI_NEON)
// load and perform the vertical filtering pass
// this uses 3*x + y = 4*x + (y - x)
uint8x8_t farb = vld1_u8(in_far + i);
uint8x8_t nearb = vld1_u8(in_near + i);
int16x8_t diff = vreinterpretq_s16_u16(vsubl_u8(farb, nearb));
int16x8_t nears = vreinterpretq_s16_u16(vshll_n_u8(nearb, 2));
int16x8_t curr = vaddq_s16(nears, diff); // current row
// horizontal filter works the same based on shifted vers of current
// row. "prev" is current row shifted right by 1 pixel; we need to
// insert the previous pixel value (from t1).
// "next" is current row shifted left by 1 pixel, with first pixel
// of next block of 8 pixels added in.
int16x8_t prv0 = vextq_s16(curr, curr, 7);
int16x8_t nxt0 = vextq_s16(curr, curr, 1);
int16x8_t prev = vsetq_lane_s16(t1, prv0, 0);
int16x8_t next = vsetq_lane_s16(3*in_near[i+8] + in_far[i+8], nxt0, 7);
// horizontal filter, polyphase implementation since it's convenient:
// even pixels = 3*cur + prev = cur*4 + (prev - cur)
// odd pixels = 3*cur + next = cur*4 + (next - cur)
// note the shared term.
int16x8_t curs = vshlq_n_s16(curr, 2);
int16x8_t prvd = vsubq_s16(prev, curr);
int16x8_t nxtd = vsubq_s16(next, curr);
int16x8_t even = vaddq_s16(curs, prvd);
int16x8_t odd = vaddq_s16(curs, nxtd);
// undo scaling and round, then store with even/odd phases interleaved
uint8x8x2_t o;
o.val[0] = vqrshrun_n_s16(even, 4);
o.val[1] = vqrshrun_n_s16(odd, 4);
vst2_u8(out + i*2, o);
#endif
// "previous" value for next iter
t1 = 3*in_near[i+7] + in_far[i+7];
}
t0 = t1;
t1 = 3*in_near[i] + in_far[i];
out[i*2] = stbi__div16(3*t1 + t0 + 8);
for (++i; i < w; ++i) {
t0 = t1;
t1 = 3*in_near[i]+in_far[i];
out[i*2-1] = stbi__div16(3*t0 + t1 + 8);
out[i*2 ] = stbi__div16(3*t1 + t0 + 8);
}
out[w*2-1] = stbi__div4(t1+2);
STBI_NOTUSED(hs);
return out;
}
#endif
static stbi_uc *stbi__resample_row_generic(stbi_uc *out, stbi_uc *in_near, stbi_uc *in_far, int w, int hs)
{
// resample with nearest-neighbor
int i,j;
STBI_NOTUSED(in_far);
for (i=0; i < w; ++i)
for (j=0; j < hs; ++j)
out[i*hs+j] = in_near[i];
return out;
}
// this is a reduced-precision calculation of YCbCr-to-RGB introduced
// to make sure the code produces the same results in both SIMD and scalar
#define stbi__float2fixed(x) (((int) ((x) * 4096.0f + 0.5f)) << 8)
static void stbi__YCbCr_to_RGB_row(stbi_uc *out, const stbi_uc *y, const stbi_uc *pcb, const stbi_uc *pcr, int count, int step)
{
int i;
for (i=0; i < count; ++i) {
int y_fixed = (y[i] << 20) + (1<<19); // rounding
int r,g,b;
int cr = pcr[i] - 128;
int cb = pcb[i] - 128;
r = y_fixed + cr* stbi__float2fixed(1.40200f);
g = y_fixed + (cr*-stbi__float2fixed(0.71414f)) + ((cb*-stbi__float2fixed(0.34414f)) & 0xffff0000);
b = y_fixed + cb* stbi__float2fixed(1.77200f);
r >>= 20;
g >>= 20;
b >>= 20;
if ((unsigned) r > 255) { if (r < 0) r = 0; else r = 255; }
if ((unsigned) g > 255) { if (g < 0) g = 0; else g = 255; }
if ((unsigned) b > 255) { if (b < 0) b = 0; else b = 255; }
out[0] = (stbi_uc)r;
out[1] = (stbi_uc)g;
out[2] = (stbi_uc)b;
out[3] = 255;
out += step;
}
}
#if defined(STBI_SSE2) || defined(STBI_NEON)
static void stbi__YCbCr_to_RGB_simd(stbi_uc *out, stbi_uc const *y, stbi_uc const *pcb, stbi_uc const *pcr, int count, int step)
{
int i = 0;
#ifdef STBI_SSE2
// step == 3 is pretty ugly on the final interleave, and i'm not convinced
// it's useful in practice (you wouldn't use it for textures, for example).
// so just accelerate step == 4 case.
if (step == 4) {
// this is a fairly straightforward implementation and not super-optimized.
__m128i signflip = _mm_set1_epi8(-0x80);
__m128i cr_const0 = _mm_set1_epi16( (short) ( 1.40200f*4096.0f+0.5f));
__m128i cr_const1 = _mm_set1_epi16( - (short) ( 0.71414f*4096.0f+0.5f));
__m128i cb_const0 = _mm_set1_epi16( - (short) ( 0.34414f*4096.0f+0.5f));
__m128i cb_const1 = _mm_set1_epi16( (short) ( 1.77200f*4096.0f+0.5f));
__m128i y_bias = _mm_set1_epi8((char) (unsigned char) 128);
__m128i xw = _mm_set1_epi16(255); // alpha channel
for (; i+7 < count; i += 8) {
// load
__m128i y_bytes = _mm_loadl_epi64((__m128i *) (y+i));
__m128i cr_bytes = _mm_loadl_epi64((__m128i *) (pcr+i));
__m128i cb_bytes = _mm_loadl_epi64((__m128i *) (pcb+i));
__m128i cr_biased = _mm_xor_si128(cr_bytes, signflip); // -128
__m128i cb_biased = _mm_xor_si128(cb_bytes, signflip); // -128
// unpack to short (and left-shift cr, cb by 8)
__m128i yw = _mm_unpacklo_epi8(y_bias, y_bytes);
__m128i crw = _mm_unpacklo_epi8(_mm_setzero_si128(), cr_biased);
__m128i cbw = _mm_unpacklo_epi8(_mm_setzero_si128(), cb_biased);
// color transform
__m128i yws = _mm_srli_epi16(yw, 4);
__m128i cr0 = _mm_mulhi_epi16(cr_const0, crw);
__m128i cb0 = _mm_mulhi_epi16(cb_const0, cbw);
__m128i cb1 = _mm_mulhi_epi16(cbw, cb_const1);
__m128i cr1 = _mm_mulhi_epi16(crw, cr_const1);
__m128i rws = _mm_add_epi16(cr0, yws);
__m128i gwt = _mm_add_epi16(cb0, yws);
__m128i bws = _mm_add_epi16(yws, cb1);
__m128i gws = _mm_add_epi16(gwt, cr1);
// descale
__m128i rw = _mm_srai_epi16(rws, 4);
__m128i bw = _mm_srai_epi16(bws, 4);
__m128i gw = _mm_srai_epi16(gws, 4);
// back to byte, set up for transpose
__m128i brb = _mm_packus_epi16(rw, bw);
__m128i gxb = _mm_packus_epi16(gw, xw);
// transpose to interleave channels
__m128i t0 = _mm_unpacklo_epi8(brb, gxb);
__m128i t1 = _mm_unpackhi_epi8(brb, gxb);
__m128i o0 = _mm_unpacklo_epi16(t0, t1);
__m128i o1 = _mm_unpackhi_epi16(t0, t1);
// store
_mm_storeu_si128((__m128i *) (out + 0), o0);
_mm_storeu_si128((__m128i *) (out + 16), o1);
out += 32;
}
}
#endif
#ifdef STBI_NEON
// in this version, step=3 support would be easy to add. but is there demand?
if (step == 4) {
// this is a fairly straightforward implementation and not super-optimized.
uint8x8_t signflip = vdup_n_u8(0x80);
int16x8_t cr_const0 = vdupq_n_s16( (short) ( 1.40200f*4096.0f+0.5f));
int16x8_t cr_const1 = vdupq_n_s16( - (short) ( 0.71414f*4096.0f+0.5f));
int16x8_t cb_const0 = vdupq_n_s16( - (short) ( 0.34414f*4096.0f+0.5f));
int16x8_t cb_const1 = vdupq_n_s16( (short) ( 1.77200f*4096.0f+0.5f));
for (; i+7 < count; i += 8) {
// load
uint8x8_t y_bytes = vld1_u8(y + i);
uint8x8_t cr_bytes = vld1_u8(pcr + i);
uint8x8_t cb_bytes = vld1_u8(pcb + i);
int8x8_t cr_biased = vreinterpret_s8_u8(vsub_u8(cr_bytes, signflip));
int8x8_t cb_biased = vreinterpret_s8_u8(vsub_u8(cb_bytes, signflip));
// expand to s16
int16x8_t yws = vreinterpretq_s16_u16(vshll_n_u8(y_bytes, 4));
int16x8_t crw = vshll_n_s8(cr_biased, 7);
int16x8_t cbw = vshll_n_s8(cb_biased, 7);
// color transform
int16x8_t cr0 = vqdmulhq_s16(crw, cr_const0);
int16x8_t cb0 = vqdmulhq_s16(cbw, cb_const0);
int16x8_t cr1 = vqdmulhq_s16(crw, cr_const1);
int16x8_t cb1 = vqdmulhq_s16(cbw, cb_const1);
int16x8_t rws = vaddq_s16(yws, cr0);
int16x8_t gws = vaddq_s16(vaddq_s16(yws, cb0), cr1);
int16x8_t bws = vaddq_s16(yws, cb1);
// undo scaling, round, convert to byte
uint8x8x4_t o;
o.val[0] = vqrshrun_n_s16(rws, 4);
o.val[1] = vqrshrun_n_s16(gws, 4);
o.val[2] = vqrshrun_n_s16(bws, 4);
o.val[3] = vdup_n_u8(255);
// store, interleaving r/g/b/a
vst4_u8(out, o);
out += 8*4;
}
}
#endif
for (; i < count; ++i) {
int y_fixed = (y[i] << 20) + (1<<19); // rounding
int r,g,b;
int cr = pcr[i] - 128;
int cb = pcb[i] - 128;
r = y_fixed + cr* stbi__float2fixed(1.40200f);
g = y_fixed + cr*-stbi__float2fixed(0.71414f) + ((cb*-stbi__float2fixed(0.34414f)) & 0xffff0000);
b = y_fixed + cb* stbi__float2fixed(1.77200f);
r >>= 20;
g >>= 20;
b >>= 20;
if ((unsigned) r > 255) { if (r < 0) r = 0; else r = 255; }
if ((unsigned) g > 255) { if (g < 0) g = 0; else g = 255; }
if ((unsigned) b > 255) { if (b < 0) b = 0; else b = 255; }
out[0] = (stbi_uc)r;
out[1] = (stbi_uc)g;
out[2] = (stbi_uc)b;
out[3] = 255;
out += step;
}
}
#endif
// set up the kernels
static void stbi__setup_jpeg(stbi__jpeg *j)
{
j->idct_block_kernel = stbi__idct_block;
j->YCbCr_to_RGB_kernel = stbi__YCbCr_to_RGB_row;
j->resample_row_hv_2_kernel = stbi__resample_row_hv_2;
#ifdef STBI_SSE2
if (stbi__sse2_available()) {
j->idct_block_kernel = stbi__idct_simd;
j->YCbCr_to_RGB_kernel = stbi__YCbCr_to_RGB_simd;
j->resample_row_hv_2_kernel = stbi__resample_row_hv_2_simd;
}
#endif
#ifdef STBI_NEON
j->idct_block_kernel = stbi__idct_simd;
j->YCbCr_to_RGB_kernel = stbi__YCbCr_to_RGB_simd;
j->resample_row_hv_2_kernel = stbi__resample_row_hv_2_simd;
#endif
}
// clean up the temporary component buffers
static void stbi__cleanup_jpeg(stbi__jpeg *j)
{
stbi__free_jpeg_components(j, j->s->img_n, 0);
}
typedef struct
{
resample_row_func resample;
stbi_uc *line0,*line1;
int hs,vs; // expansion factor in each axis
int w_lores; // horizontal pixels pre-expansion
int ystep; // how far through vertical expansion we are
int ypos; // which pre-expansion row we're on
} stbi__resample;
// fast 0..255 * 0..255 => 0..255 rounded multiplication
static stbi_uc stbi__blinn_8x8(stbi_uc x, stbi_uc y)
{
unsigned int t = x*y + 128;
return (stbi_uc) ((t + (t >>8)) >> 8);
}
static stbi_uc *load_jpeg_image(stbi__jpeg *z, int *out_x, int *out_y, int *comp, int req_comp)
{
int n, decode_n, is_rgb;
z->s->img_n = 0; // make stbi__cleanup_jpeg safe
// validate req_comp
if (req_comp < 0 || req_comp > 4) return stbi__errpuc("bad req_comp", "Internal error");
// load a jpeg image from whichever source, but leave in YCbCr format
if (!stbi__decode_jpeg_image(z)) { stbi__cleanup_jpeg(z); return NULL; }
// determine actual number of components to generate
n = req_comp ? req_comp : z->s->img_n >= 3 ? 3 : 1;
is_rgb = z->s->img_n == 3 && (z->rgb == 3 || (z->app14_color_transform == 0 && !z->jfif));
if (z->s->img_n == 3 && n < 3 && !is_rgb)
decode_n = 1;
else
decode_n = z->s->img_n;
// resample and color-convert
{
int k;
unsigned int i,j;
stbi_uc *output;
stbi_uc *coutput[4] = { NULL, NULL, NULL, NULL };
stbi__resample res_comp[4];
for (k=0; k < decode_n; ++k) {
stbi__resample *r = &res_comp[k];
// allocate line buffer big enough for upsampling off the edges
// with upsample factor of 4
z->img_comp[k].linebuf = (stbi_uc *) stbi__malloc(z->s->img_x + 3);
if (!z->img_comp[k].linebuf) { stbi__cleanup_jpeg(z); return stbi__errpuc("outofmem", "Out of memory"); }
r->hs = z->img_h_max / z->img_comp[k].h;
r->vs = z->img_v_max / z->img_comp[k].v;
r->ystep = r->vs >> 1;
r->w_lores = (z->s->img_x + r->hs-1) / r->hs;
r->ypos = 0;
r->line0 = r->line1 = z->img_comp[k].data;
if (r->hs == 1 && r->vs == 1) r->resample = resample_row_1;
else if (r->hs == 1 && r->vs == 2) r->resample = stbi__resample_row_v_2;
else if (r->hs == 2 && r->vs == 1) r->resample = stbi__resample_row_h_2;
else if (r->hs == 2 && r->vs == 2) r->resample = z->resample_row_hv_2_kernel;
else r->resample = stbi__resample_row_generic;
}
// can't error after this so, this is safe
output = (stbi_uc *) stbi__malloc_mad3(n, z->s->img_x, z->s->img_y, 1);
if (!output) { stbi__cleanup_jpeg(z); return stbi__errpuc("outofmem", "Out of memory"); }
// now go ahead and resample
for (j=0; j < z->s->img_y; ++j) {
stbi_uc *out = output + n * z->s->img_x * j;
for (k=0; k < decode_n; ++k) {
stbi__resample *r = &res_comp[k];
int y_bot = r->ystep >= (r->vs >> 1);
coutput[k] = r->resample(z->img_comp[k].linebuf,
y_bot ? r->line1 : r->line0,
y_bot ? r->line0 : r->line1,
r->w_lores, r->hs);
if (++r->ystep >= r->vs) {
r->ystep = 0;
r->line0 = r->line1;
if (++r->ypos < z->img_comp[k].y)
r->line1 += z->img_comp[k].w2;
}
}
if (n >= 3) {
stbi_uc *y = coutput[0];
if (z->s->img_n == 3) {
if (is_rgb) {
for (i=0; i < z->s->img_x; ++i) {
out[0] = y[i];
out[1] = coutput[1][i];
out[2] = coutput[2][i];
out[3] = 255;
out += n;
}
} else {
z->YCbCr_to_RGB_kernel(out, y, coutput[1], coutput[2], z->s->img_x, n);
}
} else if (z->s->img_n == 4) {
if (z->app14_color_transform == 0) { // CMYK
for (i=0; i < z->s->img_x; ++i) {
stbi_uc m = coutput[3][i];
out[0] = stbi__blinn_8x8(coutput[0][i], m);
out[1] = stbi__blinn_8x8(coutput[1][i], m);
out[2] = stbi__blinn_8x8(coutput[2][i], m);
out[3] = 255;
out += n;
}
} else if (z->app14_color_transform == 2) { // YCCK
z->YCbCr_to_RGB_kernel(out, y, coutput[1], coutput[2], z->s->img_x, n);
for (i=0; i < z->s->img_x; ++i) {
stbi_uc m = coutput[3][i];
out[0] = stbi__blinn_8x8(255 - out[0], m);
out[1] = stbi__blinn_8x8(255 - out[1], m);
out[2] = stbi__blinn_8x8(255 - out[2], m);
out += n;
}
} else { // YCbCr + alpha? Ignore the fourth channel for now
z->YCbCr_to_RGB_kernel(out, y, coutput[1], coutput[2], z->s->img_x, n);
}
} else
for (i=0; i < z->s->img_x; ++i) {
out[0] = out[1] = out[2] = y[i];
out[3] = 255; // not used if n==3
out += n;
}
} else {
if (is_rgb) {
if (n == 1)
for (i=0; i < z->s->img_x; ++i)
*out++ = stbi__compute_y(coutput[0][i], coutput[1][i], coutput[2][i]);
else {
for (i=0; i < z->s->img_x; ++i, out += 2) {
out[0] = stbi__compute_y(coutput[0][i], coutput[1][i], coutput[2][i]);
out[1] = 255;
}
}
} else if (z->s->img_n == 4 && z->app14_color_transform == 0) {
for (i=0; i < z->s->img_x; ++i) {
stbi_uc m = coutput[3][i];
stbi_uc r = stbi__blinn_8x8(coutput[0][i], m);
stbi_uc g = stbi__blinn_8x8(coutput[1][i], m);
stbi_uc b = stbi__blinn_8x8(coutput[2][i], m);
out[0] = stbi__compute_y(r, g, b);
out[1] = 255;
out += n;
}
} else if (z->s->img_n == 4 && z->app14_color_transform == 2) {
for (i=0; i < z->s->img_x; ++i) {
out[0] = stbi__blinn_8x8(255 - coutput[0][i], coutput[3][i]);
out[1] = 255;
out += n;
}
} else {
stbi_uc *y = coutput[0];
if (n == 1)
for (i=0; i < z->s->img_x; ++i) out[i] = y[i];
else
for (i=0; i < z->s->img_x; ++i) { *out++ = y[i]; *out++ = 255; }
}
}
}
stbi__cleanup_jpeg(z);
*out_x = z->s->img_x;
*out_y = z->s->img_y;
if (comp) *comp = z->s->img_n >= 3 ? 3 : 1; // report original components, not output
return output;
}
}
static void *stbi__jpeg_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri)
{
unsigned char* result;
stbi__jpeg* j = (stbi__jpeg*) stbi__malloc(sizeof(stbi__jpeg));
STBI_NOTUSED(ri);
j->s = s;
stbi__setup_jpeg(j);
result = load_jpeg_image(j, x,y,comp,req_comp);
STBI_FREE(j);
return result;
}
static int stbi__jpeg_test(stbi__context *s)
{
int r;
stbi__jpeg* j = (stbi__jpeg*)stbi__malloc(sizeof(stbi__jpeg));
j->s = s;
stbi__setup_jpeg(j);
r = stbi__decode_jpeg_header(j, STBI__SCAN_type);
stbi__rewind(s);
STBI_FREE(j);
return r;
}
static int stbi__jpeg_info_raw(stbi__jpeg *j, int *x, int *y, int *comp)
{
if (!stbi__decode_jpeg_header(j, STBI__SCAN_header)) {
stbi__rewind( j->s );
return 0;
}
if (x) *x = j->s->img_x;
if (y) *y = j->s->img_y;
if (comp) *comp = j->s->img_n >= 3 ? 3 : 1;
return 1;
}
static int stbi__jpeg_info(stbi__context *s, int *x, int *y, int *comp)
{
int result;
stbi__jpeg* j = (stbi__jpeg*) (stbi__malloc(sizeof(stbi__jpeg)));
j->s = s;
result = stbi__jpeg_info_raw(j, x, y, comp);
STBI_FREE(j);
return result;
}
#endif
// public domain zlib decode v0.2 Sean Barrett 2006-11-18
// simple implementation
// - all input must be provided in an upfront buffer
// - all output is written to a single output buffer (can malloc/realloc)
// performance
// - fast huffman
#ifndef STBI_NO_ZLIB
// fast-way is faster to check than jpeg huffman, but slow way is slower
#define STBI__ZFAST_BITS 9 // accelerate all cases in default tables
#define STBI__ZFAST_MASK ((1 << STBI__ZFAST_BITS) - 1)
// zlib-style huffman encoding
// (jpegs packs from left, zlib from right, so can't share code)
typedef struct
{
stbi__uint16 fast[1 << STBI__ZFAST_BITS];
stbi__uint16 firstcode[16];
int maxcode[17];
stbi__uint16 firstsymbol[16];
stbi_uc size[288];
stbi__uint16 value[288];
} stbi__zhuffman;
stbi_inline static int stbi__bitreverse16(int n)
{
n = ((n & 0xAAAA) >> 1) | ((n & 0x5555) << 1);
n = ((n & 0xCCCC) >> 2) | ((n & 0x3333) << 2);
n = ((n & 0xF0F0) >> 4) | ((n & 0x0F0F) << 4);
n = ((n & 0xFF00) >> 8) | ((n & 0x00FF) << 8);
return n;
}
stbi_inline static int stbi__bit_reverse(int v, int bits)
{
STBI_ASSERT(bits <= 16);
// to bit reverse n bits, reverse 16 and shift
// e.g. 11 bits, bit reverse and shift away 5
return stbi__bitreverse16(v) >> (16-bits);
}
static int stbi__zbuild_huffman(stbi__zhuffman *z, const stbi_uc *sizelist, int num)
{
int i,k=0;
int code, next_code[16], sizes[17];
// DEFLATE spec for generating codes
memset(sizes, 0, sizeof(sizes));
memset(z->fast, 0, sizeof(z->fast));
for (i=0; i < num; ++i)
++sizes[sizelist[i]];
sizes[0] = 0;
for (i=1; i < 16; ++i)
if (sizes[i] > (1 << i))
return stbi__err("bad sizes", "Corrupt PNG");
code = 0;
for (i=1; i < 16; ++i) {
next_code[i] = code;
z->firstcode[i] = (stbi__uint16) code;
z->firstsymbol[i] = (stbi__uint16) k;
code = (code + sizes[i]);
if (sizes[i])
if (code-1 >= (1 << i)) return stbi__err("bad codelengths","Corrupt PNG");
z->maxcode[i] = code << (16-i); // preshift for inner loop
code <<= 1;
k += sizes[i];
}
z->maxcode[16] = 0x10000; // sentinel
for (i=0; i < num; ++i) {
int s = sizelist[i];
if (s) {
int c = next_code[s] - z->firstcode[s] + z->firstsymbol[s];
stbi__uint16 fastv = (stbi__uint16) ((s << 9) | i);
z->size [c] = (stbi_uc ) s;
z->value[c] = (stbi__uint16) i;
if (s <= STBI__ZFAST_BITS) {
int j = stbi__bit_reverse(next_code[s],s);
while (j < (1 << STBI__ZFAST_BITS)) {
z->fast[j] = fastv;
j += (1 << s);
}
}
++next_code[s];
}
}
return 1;
}
// zlib-from-memory implementation for PNG reading
// because PNG allows splitting the zlib stream arbitrarily,
// and it's annoying structurally to have PNG call ZLIB call PNG,
// we require PNG read all the IDATs and combine them into a single
// memory buffer
typedef struct
{
stbi_uc *zbuffer, *zbuffer_end;
int num_bits;
stbi__uint32 code_buffer;
char *zout;
char *zout_start;
char *zout_end;
int z_expandable;
stbi__zhuffman z_length, z_distance;
} stbi__zbuf;
stbi_inline static stbi_uc stbi__zget8(stbi__zbuf *z)
{
if (z->zbuffer >= z->zbuffer_end) return 0;
return *z->zbuffer++;
}
static void stbi__fill_bits(stbi__zbuf *z)
{
do {
STBI_ASSERT(z->code_buffer < (1U << z->num_bits));
z->code_buffer |= (unsigned int) stbi__zget8(z) << z->num_bits;
z->num_bits += 8;
} while (z->num_bits <= 24);
}
stbi_inline static unsigned int stbi__zreceive(stbi__zbuf *z, int n)
{
unsigned int k;
if (z->num_bits < n) stbi__fill_bits(z);
k = z->code_buffer & ((1 << n) - 1);
z->code_buffer >>= n;
z->num_bits -= n;
return k;
}
static int stbi__zhuffman_decode_slowpath(stbi__zbuf *a, stbi__zhuffman *z)
{
int b,s,k;
// not resolved by fast table, so compute it the slow way
// use jpeg approach, which requires MSbits at top
k = stbi__bit_reverse(a->code_buffer, 16);
for (s=STBI__ZFAST_BITS+1; ; ++s)
if (k < z->maxcode[s])
break;
if (s == 16) return -1; // invalid code!
// code size is s, so:
b = (k >> (16-s)) - z->firstcode[s] + z->firstsymbol[s];
STBI_ASSERT(z->size[b] == s);
a->code_buffer >>= s;
a->num_bits -= s;
return z->value[b];
}
stbi_inline static int stbi__zhuffman_decode(stbi__zbuf *a, stbi__zhuffman *z)
{
int b,s;
if (a->num_bits < 16) stbi__fill_bits(a);
b = z->fast[a->code_buffer & STBI__ZFAST_MASK];
if (b) {
s = b >> 9;
a->code_buffer >>= s;
a->num_bits -= s;
return b & 511;
}
return stbi__zhuffman_decode_slowpath(a, z);
}
static int stbi__zexpand(stbi__zbuf *z, char *zout, int n) // need to make room for n bytes
{
char *q;
int cur, limit, old_limit;
z->zout = zout;
if (!z->z_expandable) return stbi__err("output buffer limit","Corrupt PNG");
cur = (int) (z->zout - z->zout_start);
limit = old_limit = (int) (z->zout_end - z->zout_start);
while (cur + n > limit)
limit *= 2;
q = (char *) STBI_REALLOC_SIZED(z->zout_start, old_limit, limit);
STBI_NOTUSED(old_limit);
if (q == NULL) return stbi__err("outofmem", "Out of memory");
z->zout_start = q;
z->zout = q + cur;
z->zout_end = q + limit;
return 1;
}
static const int stbi__zlength_base[31] = {
3,4,5,6,7,8,9,10,11,13,
15,17,19,23,27,31,35,43,51,59,
67,83,99,115,131,163,195,227,258,0,0 };
static const int stbi__zlength_extra[31]=
{ 0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0,0,0 };
static const int stbi__zdist_base[32] = { 1,2,3,4,5,7,9,13,17,25,33,49,65,97,129,193,
257,385,513,769,1025,1537,2049,3073,4097,6145,8193,12289,16385,24577,0,0};
static const int stbi__zdist_extra[32] =
{ 0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13};
static int stbi__parse_huffman_block(stbi__zbuf *a)
{
char *zout = a->zout;
for(;;) {
int z = stbi__zhuffman_decode(a, &a->z_length);
if (z < 256) {
if (z < 0) return stbi__err("bad huffman code","Corrupt PNG"); // error in huffman codes
if (zout >= a->zout_end) {
if (!stbi__zexpand(a, zout, 1)) return 0;
zout = a->zout;
}
*zout++ = (char) z;
} else {
stbi_uc *p;
int len,dist;
if (z == 256) {
a->zout = zout;
return 1;
}
z -= 257;
len = stbi__zlength_base[z];
if (stbi__zlength_extra[z]) len += stbi__zreceive(a, stbi__zlength_extra[z]);
z = stbi__zhuffman_decode(a, &a->z_distance);
if (z < 0) return stbi__err("bad huffman code","Corrupt PNG");
dist = stbi__zdist_base[z];
if (stbi__zdist_extra[z]) dist += stbi__zreceive(a, stbi__zdist_extra[z]);
if (zout - a->zout_start < dist) return stbi__err("bad dist","Corrupt PNG");
if (zout + len > a->zout_end) {
if (!stbi__zexpand(a, zout, len)) return 0;
zout = a->zout;
}
p = (stbi_uc *) (zout - dist);
if (dist == 1) { // run of one byte; common in images.
stbi_uc v = *p;
if (len) { do *zout++ = v; while (--len); }
} else {
if (len) { do *zout++ = *p++; while (--len); }
}
}
}
}
static int stbi__compute_huffman_codes(stbi__zbuf *a)
{
static const stbi_uc length_dezigzag[19] = { 16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15 };
stbi__zhuffman z_codelength;
stbi_uc lencodes[286+32+137];//padding for maximum single op
stbi_uc codelength_sizes[19];
int i,n;
int hlit = stbi__zreceive(a,5) + 257;
int hdist = stbi__zreceive(a,5) + 1;
int hclen = stbi__zreceive(a,4) + 4;
int ntot = hlit + hdist;
memset(codelength_sizes, 0, sizeof(codelength_sizes));
for (i=0; i < hclen; ++i) {
int s = stbi__zreceive(a,3);
codelength_sizes[length_dezigzag[i]] = (stbi_uc) s;
}
if (!stbi__zbuild_huffman(&z_codelength, codelength_sizes, 19)) return 0;
n = 0;
while (n < ntot) {
int c = stbi__zhuffman_decode(a, &z_codelength);
if (c < 0 || c >= 19) return stbi__err("bad codelengths", "Corrupt PNG");
if (c < 16)
lencodes[n++] = (stbi_uc) c;
else {
stbi_uc fill = 0;
if (c == 16) {
c = stbi__zreceive(a,2)+3;
if (n == 0) return stbi__err("bad codelengths", "Corrupt PNG");
fill = lencodes[n-1];
} else if (c == 17)
c = stbi__zreceive(a,3)+3;
else {
STBI_ASSERT(c == 18);
c = stbi__zreceive(a,7)+11;
}
if (ntot - n < c) return stbi__err("bad codelengths", "Corrupt PNG");
memset(lencodes+n, fill, c);
n += c;
}
}
if (n != ntot) return stbi__err("bad codelengths","Corrupt PNG");
if (!stbi__zbuild_huffman(&a->z_length, lencodes, hlit)) return 0;
if (!stbi__zbuild_huffman(&a->z_distance, lencodes+hlit, hdist)) return 0;
return 1;
}
static int stbi__parse_uncompressed_block(stbi__zbuf *a)
{
stbi_uc header[4];
int len,nlen,k;
if (a->num_bits & 7)
stbi__zreceive(a, a->num_bits & 7); // discard
// drain the bit-packed data into header
k = 0;
while (a->num_bits > 0) {
header[k++] = (stbi_uc) (a->code_buffer & 255); // suppress MSVC run-time check
a->code_buffer >>= 8;
a->num_bits -= 8;
}
STBI_ASSERT(a->num_bits == 0);
// now fill header the normal way
while (k < 4)
header[k++] = stbi__zget8(a);
len = header[1] * 256 + header[0];
nlen = header[3] * 256 + header[2];
if (nlen != (len ^ 0xffff)) return stbi__err("zlib corrupt","Corrupt PNG");
if (a->zbuffer + len > a->zbuffer_end) return stbi__err("read past buffer","Corrupt PNG");
if (a->zout + len > a->zout_end)
if (!stbi__zexpand(a, a->zout, len)) return 0;
memcpy(a->zout, a->zbuffer, len);
a->zbuffer += len;
a->zout += len;
return 1;
}
static int stbi__parse_zlib_header(stbi__zbuf *a)
{
int cmf = stbi__zget8(a);
int cm = cmf & 15;
/* int cinfo = cmf >> 4; */
int flg = stbi__zget8(a);
if ((cmf*256+flg) % 31 != 0) return stbi__err("bad zlib header","Corrupt PNG"); // zlib spec
if (flg & 32) return stbi__err("no preset dict","Corrupt PNG"); // preset dictionary not allowed in png
if (cm != 8) return stbi__err("bad compression","Corrupt PNG"); // DEFLATE required for png
// window = 1 << (8 + cinfo)... but who cares, we fully buffer output
return 1;
}
static const stbi_uc stbi__zdefault_length[288] =
{
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,
9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9, 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,
9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9, 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,
9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9, 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, 7,7,7,7,7,7,7,7,8,8,8,8,8,8,8,8
};
static const stbi_uc stbi__zdefault_distance[32] =
{
5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5
};
/*
Init algorithm:
{
int i; // use <= to match clearly with spec
for (i=0; i <= 143; ++i) stbi__zdefault_length[i] = 8;
for ( ; i <= 255; ++i) stbi__zdefault_length[i] = 9;
for ( ; i <= 279; ++i) stbi__zdefault_length[i] = 7;
for ( ; i <= 287; ++i) stbi__zdefault_length[i] = 8;
for (i=0; i <= 31; ++i) stbi__zdefault_distance[i] = 5;
}
*/
static int stbi__parse_zlib(stbi__zbuf *a, int parse_header)
{
int final, type;
if (parse_header)
if (!stbi__parse_zlib_header(a)) return 0;
a->num_bits = 0;
a->code_buffer = 0;
do {
final = stbi__zreceive(a,1);
type = stbi__zreceive(a,2);
if (type == 0) {
if (!stbi__parse_uncompressed_block(a)) return 0;
} else if (type == 3) {
return 0;
} else {
if (type == 1) {
// use fixed code lengths
if (!stbi__zbuild_huffman(&a->z_length , stbi__zdefault_length , 288)) return 0;
if (!stbi__zbuild_huffman(&a->z_distance, stbi__zdefault_distance, 32)) return 0;
} else {
if (!stbi__compute_huffman_codes(a)) return 0;
}
if (!stbi__parse_huffman_block(a)) return 0;
}
} while (!final);
return 1;
}
static int stbi__do_zlib(stbi__zbuf *a, char *obuf, int olen, int exp, int parse_header)
{
a->zout_start = obuf;
a->zout = obuf;
a->zout_end = obuf + olen;
a->z_expandable = exp;
return stbi__parse_zlib(a, parse_header);
}
STBIDEF char *stbi_zlib_decode_malloc_guesssize(const char *buffer, int len, int initial_size, int *outlen)
{
stbi__zbuf a;
char *p = (char *) stbi__malloc(initial_size);
if (p == NULL) return NULL;
a.zbuffer = (stbi_uc *) buffer;
a.zbuffer_end = (stbi_uc *) buffer + len;
if (stbi__do_zlib(&a, p, initial_size, 1, 1)) {
if (outlen) *outlen = (int) (a.zout - a.zout_start);
return a.zout_start;
} else {
STBI_FREE(a.zout_start);
return NULL;
}
}
STBIDEF char *stbi_zlib_decode_malloc(char const *buffer, int len, int *outlen)
{
return stbi_zlib_decode_malloc_guesssize(buffer, len, 16384, outlen);
}
STBIDEF char *stbi_zlib_decode_malloc_guesssize_headerflag(const char *buffer, int len, int initial_size, int *outlen, int parse_header)
{
stbi__zbuf a;
char *p = (char *) stbi__malloc(initial_size);
if (p == NULL) return NULL;
a.zbuffer = (stbi_uc *) buffer;
a.zbuffer_end = (stbi_uc *) buffer + len;
if (stbi__do_zlib(&a, p, initial_size, 1, parse_header)) {
if (outlen) *outlen = (int) (a.zout - a.zout_start);
return a.zout_start;
} else {
STBI_FREE(a.zout_start);
return NULL;
}
}
STBIDEF int stbi_zlib_decode_buffer(char *obuffer, int olen, char const *ibuffer, int ilen)
{
stbi__zbuf a;
a.zbuffer = (stbi_uc *) ibuffer;
a.zbuffer_end = (stbi_uc *) ibuffer + ilen;
if (stbi__do_zlib(&a, obuffer, olen, 0, 1))
return (int) (a.zout - a.zout_start);
else
return -1;
}
STBIDEF char *stbi_zlib_decode_noheader_malloc(char const *buffer, int len, int *outlen)
{
stbi__zbuf a;
char *p = (char *) stbi__malloc(16384);
if (p == NULL) return NULL;
a.zbuffer = (stbi_uc *) buffer;
a.zbuffer_end = (stbi_uc *) buffer+len;
if (stbi__do_zlib(&a, p, 16384, 1, 0)) {
if (outlen) *outlen = (int) (a.zout - a.zout_start);
return a.zout_start;
} else {
STBI_FREE(a.zout_start);
return NULL;
}
}
STBIDEF int stbi_zlib_decode_noheader_buffer(char *obuffer, int olen, const char *ibuffer, int ilen)
{
stbi__zbuf a;
a.zbuffer = (stbi_uc *) ibuffer;
a.zbuffer_end = (stbi_uc *) ibuffer + ilen;
if (stbi__do_zlib(&a, obuffer, olen, 0, 0))
return (int) (a.zout - a.zout_start);
else
return -1;
}
#endif
// public domain "baseline" PNG decoder v0.10 Sean Barrett 2006-11-18
// simple implementation
// - only 8-bit samples
// - no CRC checking
// - allocates lots of intermediate memory
// - avoids problem of streaming data between subsystems
// - avoids explicit window management
// performance
// - uses stb_zlib, a PD zlib implementation with fast huffman decoding
#ifndef STBI_NO_PNG
typedef struct
{
stbi__uint32 length;
stbi__uint32 type;
} stbi__pngchunk;
static stbi__pngchunk stbi__get_chunk_header(stbi__context *s)
{
stbi__pngchunk c;
c.length = stbi__get32be(s);
c.type = stbi__get32be(s);
return c;
}
static int stbi__check_png_header(stbi__context *s)
{
static const stbi_uc png_sig[8] = { 137,80,78,71,13,10,26,10 };
int i;
for (i=0; i < 8; ++i)
if (stbi__get8(s) != png_sig[i]) return stbi__err("bad png sig","Not a PNG");
return 1;
}
typedef struct
{
stbi__context *s;
stbi_uc *idata, *expanded, *out;
int depth;
} stbi__png;
enum {
STBI__F_none=0,
STBI__F_sub=1,
STBI__F_up=2,
STBI__F_avg=3,
STBI__F_paeth=4,
// synthetic filters used for first scanline to avoid needing a dummy row of 0s
STBI__F_avg_first,
STBI__F_paeth_first
};
static stbi_uc first_row_filter[5] =
{
STBI__F_none,
STBI__F_sub,
STBI__F_none,
STBI__F_avg_first,
STBI__F_paeth_first
};
static int stbi__paeth(int a, int b, int c)
{
int p = a + b - c;
int pa = abs(p-a);
int pb = abs(p-b);
int pc = abs(p-c);
if (pa <= pb && pa <= pc) return a;
if (pb <= pc) return b;
return c;
}
static const stbi_uc stbi__depth_scale_table[9] = { 0, 0xff, 0x55, 0, 0x11, 0,0,0, 0x01 };
// create the png data from post-deflated data
static int stbi__create_png_image_raw(stbi__png *a, stbi_uc *raw, stbi__uint32 raw_len, int out_n, stbi__uint32 x, stbi__uint32 y, int depth, int color)
{
int bytes = (depth == 16? 2 : 1);
stbi__context *s = a->s;
stbi__uint32 i,j,stride = x*out_n*bytes;
stbi__uint32 img_len, img_width_bytes;
int k;
int img_n = s->img_n; // copy it into a local for later
int output_bytes = out_n*bytes;
int filter_bytes = img_n*bytes;
int width = x;
STBI_ASSERT(out_n == s->img_n || out_n == s->img_n+1);
a->out = (stbi_uc *) stbi__malloc_mad3(x, y, output_bytes, 0); // extra bytes to write off the end into
if (!a->out) return stbi__err("outofmem", "Out of memory");
if (!stbi__mad3sizes_valid(img_n, x, depth, 7)) return stbi__err("too large", "Corrupt PNG");
img_width_bytes = (((img_n * x * depth) + 7) >> 3);
img_len = (img_width_bytes + 1) * y;
// we used to check for exact match between raw_len and img_len on non-interlaced PNGs,
// but issue #276 reported a PNG in the wild that had extra data at the end (all zeros),
// so just check for raw_len < img_len always.
if (raw_len < img_len) return stbi__err("not enough pixels","Corrupt PNG");
for (j=0; j < y; ++j) {
stbi_uc *cur = a->out + stride*j;
stbi_uc *prior;
int filter = *raw++;
if (filter > 4)
return stbi__err("invalid filter","Corrupt PNG");
if (depth < 8) {
STBI_ASSERT(img_width_bytes <= x);
cur += x*out_n - img_width_bytes; // store output to the rightmost img_len bytes, so we can decode in place
filter_bytes = 1;
width = img_width_bytes;
}
prior = cur - stride; // bugfix: need to compute this after 'cur +=' computation above
// if first row, use special filter that doesn't sample previous row
if (j == 0) filter = first_row_filter[filter];
// handle first byte explicitly
for (k=0; k < filter_bytes; ++k) {
switch (filter) {
case STBI__F_none : cur[k] = raw[k]; break;
case STBI__F_sub : cur[k] = raw[k]; break;
case STBI__F_up : cur[k] = STBI__BYTECAST(raw[k] + prior[k]); break;
case STBI__F_avg : cur[k] = STBI__BYTECAST(raw[k] + (prior[k]>>1)); break;
case STBI__F_paeth : cur[k] = STBI__BYTECAST(raw[k] + stbi__paeth(0,prior[k],0)); break;
case STBI__F_avg_first : cur[k] = raw[k]; break;
case STBI__F_paeth_first: cur[k] = raw[k]; break;
}
}
if (depth == 8) {
if (img_n != out_n)
cur[img_n] = 255; // first pixel
raw += img_n;
cur += out_n;
prior += out_n;
} else if (depth == 16) {
if (img_n != out_n) {
cur[filter_bytes] = 255; // first pixel top byte
cur[filter_bytes+1] = 255; // first pixel bottom byte
}
raw += filter_bytes;
cur += output_bytes;
prior += output_bytes;
} else {
raw += 1;
cur += 1;
prior += 1;
}
// this is a little gross, so that we don't switch per-pixel or per-component
if (depth < 8 || img_n == out_n) {
int nk = (width - 1)*filter_bytes;
#define STBI__CASE(f) \
case f: \
for (k=0; k < nk; ++k)
switch (filter) {
// "none" filter turns into a memcpy here; make that explicit.
case STBI__F_none: memcpy(cur, raw, nk); break;
STBI__CASE(STBI__F_sub) { cur[k] = STBI__BYTECAST(raw[k] + cur[k-filter_bytes]); } break;
STBI__CASE(STBI__F_up) { cur[k] = STBI__BYTECAST(raw[k] + prior[k]); } break;
STBI__CASE(STBI__F_avg) { cur[k] = STBI__BYTECAST(raw[k] + ((prior[k] + cur[k-filter_bytes])>>1)); } break;
STBI__CASE(STBI__F_paeth) { cur[k] = STBI__BYTECAST(raw[k] + stbi__paeth(cur[k-filter_bytes],prior[k],prior[k-filter_bytes])); } break;
STBI__CASE(STBI__F_avg_first) { cur[k] = STBI__BYTECAST(raw[k] + (cur[k-filter_bytes] >> 1)); } break;
STBI__CASE(STBI__F_paeth_first) { cur[k] = STBI__BYTECAST(raw[k] + stbi__paeth(cur[k-filter_bytes],0,0)); } break;
}
#undef STBI__CASE
raw += nk;
} else {
STBI_ASSERT(img_n+1 == out_n);
#define STBI__CASE(f) \
case f: \
for (i=x-1; i >= 1; --i, cur[filter_bytes]=255,raw+=filter_bytes,cur+=output_bytes,prior+=output_bytes) \
for (k=0; k < filter_bytes; ++k)
switch (filter) {
STBI__CASE(STBI__F_none) { cur[k] = raw[k]; } break;
STBI__CASE(STBI__F_sub) { cur[k] = STBI__BYTECAST(raw[k] + cur[k- output_bytes]); } break;
STBI__CASE(STBI__F_up) { cur[k] = STBI__BYTECAST(raw[k] + prior[k]); } break;
STBI__CASE(STBI__F_avg) { cur[k] = STBI__BYTECAST(raw[k] + ((prior[k] + cur[k- output_bytes])>>1)); } break;
STBI__CASE(STBI__F_paeth) { cur[k] = STBI__BYTECAST(raw[k] + stbi__paeth(cur[k- output_bytes],prior[k],prior[k- output_bytes])); } break;
STBI__CASE(STBI__F_avg_first) { cur[k] = STBI__BYTECAST(raw[k] + (cur[k- output_bytes] >> 1)); } break;
STBI__CASE(STBI__F_paeth_first) { cur[k] = STBI__BYTECAST(raw[k] + stbi__paeth(cur[k- output_bytes],0,0)); } break;
}
#undef STBI__CASE
// the loop above sets the high byte of the pixels' alpha, but for
// 16 bit png files we also need the low byte set. we'll do that here.
if (depth == 16) {
cur = a->out + stride*j; // start at the beginning of the row again
for (i=0; i < x; ++i,cur+=output_bytes) {
cur[filter_bytes+1] = 255;
}
}
}
}
// we make a separate pass to expand bits to pixels; for performance,
// this could run two scanlines behind the above code, so it won't
// intefere with filtering but will still be in the cache.
if (depth < 8) {
for (j=0; j < y; ++j) {
stbi_uc *cur = a->out + stride*j;
stbi_uc *in = a->out + stride*j + x*out_n - img_width_bytes;
// unpack 1/2/4-bit into a 8-bit buffer. allows us to keep the common 8-bit path optimal at minimal cost for 1/2/4-bit
// png guarante byte alignment, if width is not multiple of 8/4/2 we'll decode dummy trailing data that will be skipped in the later loop
stbi_uc scale = (color == 0) ? stbi__depth_scale_table[depth] : 1; // scale grayscale values to 0..255 range
// note that the final byte might overshoot and write more data than desired.
// we can allocate enough data that this never writes out of memory, but it
// could also overwrite the next scanline. can it overwrite non-empty data
// on the next scanline? yes, consider 1-pixel-wide scanlines with 1-bit-per-pixel.
// so we need to explicitly clamp the final ones
if (depth == 4) {
for (k=x*img_n; k >= 2; k-=2, ++in) {
*cur++ = scale * ((*in >> 4) );
*cur++ = scale * ((*in ) & 0x0f);
}
if (k > 0) *cur++ = scale * ((*in >> 4) );
} else if (depth == 2) {
for (k=x*img_n; k >= 4; k-=4, ++in) {
*cur++ = scale * ((*in >> 6) );
*cur++ = scale * ((*in >> 4) & 0x03);
*cur++ = scale * ((*in >> 2) & 0x03);
*cur++ = scale * ((*in ) & 0x03);
}
if (k > 0) *cur++ = scale * ((*in >> 6) );
if (k > 1) *cur++ = scale * ((*in >> 4) & 0x03);
if (k > 2) *cur++ = scale * ((*in >> 2) & 0x03);
} else if (depth == 1) {
for (k=x*img_n; k >= 8; k-=8, ++in) {
*cur++ = scale * ((*in >> 7) );
*cur++ = scale * ((*in >> 6) & 0x01);
*cur++ = scale * ((*in >> 5) & 0x01);
*cur++ = scale * ((*in >> 4) & 0x01);
*cur++ = scale * ((*in >> 3) & 0x01);
*cur++ = scale * ((*in >> 2) & 0x01);
*cur++ = scale * ((*in >> 1) & 0x01);
*cur++ = scale * ((*in ) & 0x01);
}
if (k > 0) *cur++ = scale * ((*in >> 7) );
if (k > 1) *cur++ = scale * ((*in >> 6) & 0x01);
if (k > 2) *cur++ = scale * ((*in >> 5) & 0x01);
if (k > 3) *cur++ = scale * ((*in >> 4) & 0x01);
if (k > 4) *cur++ = scale * ((*in >> 3) & 0x01);
if (k > 5) *cur++ = scale * ((*in >> 2) & 0x01);
if (k > 6) *cur++ = scale * ((*in >> 1) & 0x01);
}
if (img_n != out_n) {
int q;
// insert alpha = 255
cur = a->out + stride*j;
if (img_n == 1) {
for (q=x-1; q >= 0; --q) {
cur[q*2+1] = 255;
cur[q*2+0] = cur[q];
}
} else {
STBI_ASSERT(img_n == 3);
for (q=x-1; q >= 0; --q) {
cur[q*4+3] = 255;
cur[q*4+2] = cur[q*3+2];
cur[q*4+1] = cur[q*3+1];
cur[q*4+0] = cur[q*3+0];
}
}
}
}
} else if (depth == 16) {
// force the image data from big-endian to platform-native.
// this is done in a separate pass due to the decoding relying
// on the data being untouched, but could probably be done
// per-line during decode if care is taken.
stbi_uc *cur = a->out;
stbi__uint16 *cur16 = (stbi__uint16*)cur;
for(i=0; i < x*y*out_n; ++i,cur16++,cur+=2) {
*cur16 = (cur[0] << 8) | cur[1];
}
}
return 1;
}
static int stbi__create_png_image(stbi__png *a, stbi_uc *image_data, stbi__uint32 image_data_len, int out_n, int depth, int color, int interlaced)
{
int bytes = (depth == 16 ? 2 : 1);
int out_bytes = out_n * bytes;
stbi_uc *final;
int p;
if (!interlaced)
return stbi__create_png_image_raw(a, image_data, image_data_len, out_n, a->s->img_x, a->s->img_y, depth, color);
// de-interlacing
final = (stbi_uc *) stbi__malloc_mad3(a->s->img_x, a->s->img_y, out_bytes, 0);
for (p=0; p < 7; ++p) {
int xorig[] = { 0,4,0,2,0,1,0 };
int yorig[] = { 0,0,4,0,2,0,1 };
int xspc[] = { 8,8,4,4,2,2,1 };
int yspc[] = { 8,8,8,4,4,2,2 };
int i,j,x,y;
// pass1_x[4] = 0, pass1_x[5] = 1, pass1_x[12] = 1
x = (a->s->img_x - xorig[p] + xspc[p]-1) / xspc[p];
y = (a->s->img_y - yorig[p] + yspc[p]-1) / yspc[p];
if (x && y) {
stbi__uint32 img_len = ((((a->s->img_n * x * depth) + 7) >> 3) + 1) * y;
if (!stbi__create_png_image_raw(a, image_data, image_data_len, out_n, x, y, depth, color)) {
STBI_FREE(final);
return 0;
}
for (j=0; j < y; ++j) {
for (i=0; i < x; ++i) {
int out_y = j*yspc[p]+yorig[p];
int out_x = i*xspc[p]+xorig[p];
memcpy(final + out_y*a->s->img_x*out_bytes + out_x*out_bytes,
a->out + (j*x+i)*out_bytes, out_bytes);
}
}
STBI_FREE(a->out);
image_data += img_len;
image_data_len -= img_len;
}
}
a->out = final;
return 1;
}
static int stbi__compute_transparency(stbi__png *z, stbi_uc tc[3], int out_n)
{
stbi__context *s = z->s;
stbi__uint32 i, pixel_count = s->img_x * s->img_y;
stbi_uc *p = z->out;
// compute color-based transparency, assuming we've
// already got 255 as the alpha value in the output
STBI_ASSERT(out_n == 2 || out_n == 4);
if (out_n == 2) {
for (i=0; i < pixel_count; ++i) {
p[1] = (p[0] == tc[0] ? 0 : 255);
p += 2;
}
} else {
for (i=0; i < pixel_count; ++i) {
if (p[0] == tc[0] && p[1] == tc[1] && p[2] == tc[2])
p[3] = 0;
p += 4;
}
}
return 1;
}
static int stbi__compute_transparency16(stbi__png *z, stbi__uint16 tc[3], int out_n)
{
stbi__context *s = z->s;
stbi__uint32 i, pixel_count = s->img_x * s->img_y;
stbi__uint16 *p = (stbi__uint16*) z->out;
// compute color-based transparency, assuming we've
// already got 65535 as the alpha value in the output
STBI_ASSERT(out_n == 2 || out_n == 4);
if (out_n == 2) {
for (i = 0; i < pixel_count; ++i) {
p[1] = (p[0] == tc[0] ? 0 : 65535);
p += 2;
}
} else {
for (i = 0; i < pixel_count; ++i) {
if (p[0] == tc[0] && p[1] == tc[1] && p[2] == tc[2])
p[3] = 0;
p += 4;
}
}
return 1;
}
static int stbi__expand_png_palette(stbi__png *a, stbi_uc *palette, int len, int pal_img_n)
{
stbi__uint32 i, pixel_count = a->s->img_x * a->s->img_y;
stbi_uc *p, *temp_out, *orig = a->out;
p = (stbi_uc *) stbi__malloc_mad2(pixel_count, pal_img_n, 0);
if (p == NULL) return stbi__err("outofmem", "Out of memory");
// between here and free(out) below, exitting would leak
temp_out = p;
if (pal_img_n == 3) {
for (i=0; i < pixel_count; ++i) {
int n = orig[i]*4;
p[0] = palette[n ];
p[1] = palette[n+1];
p[2] = palette[n+2];
p += 3;
}
} else {
for (i=0; i < pixel_count; ++i) {
int n = orig[i]*4;
p[0] = palette[n ];
p[1] = palette[n+1];
p[2] = palette[n+2];
p[3] = palette[n+3];
p += 4;
}
}
STBI_FREE(a->out);
a->out = temp_out;
STBI_NOTUSED(len);
return 1;
}
static int stbi__unpremultiply_on_load = 0;
static int stbi__de_iphone_flag = 0;
STBIDEF void stbi_set_unpremultiply_on_load(int flag_true_if_should_unpremultiply)
{
stbi__unpremultiply_on_load = flag_true_if_should_unpremultiply;
}
STBIDEF void stbi_convert_iphone_png_to_rgb(int flag_true_if_should_convert)
{
stbi__de_iphone_flag = flag_true_if_should_convert;
}
static void stbi__de_iphone(stbi__png *z)
{
stbi__context *s = z->s;
stbi__uint32 i, pixel_count = s->img_x * s->img_y;
stbi_uc *p = z->out;
if (s->img_out_n == 3) { // convert bgr to rgb
for (i=0; i < pixel_count; ++i) {
stbi_uc t = p[0];
p[0] = p[2];
p[2] = t;
p += 3;
}
} else {
STBI_ASSERT(s->img_out_n == 4);
if (stbi__unpremultiply_on_load) {
// convert bgr to rgb and unpremultiply
for (i=0; i < pixel_count; ++i) {
stbi_uc a = p[3];
stbi_uc t = p[0];
if (a) {
stbi_uc half = a / 2;
p[0] = (p[2] * 255 + half) / a;
p[1] = (p[1] * 255 + half) / a;
p[2] = ( t * 255 + half) / a;
} else {
p[0] = p[2];
p[2] = t;
}
p += 4;
}
} else {
// convert bgr to rgb
for (i=0; i < pixel_count; ++i) {
stbi_uc t = p[0];
p[0] = p[2];
p[2] = t;
p += 4;
}
}
}
}
#define STBI__PNG_TYPE(a,b,c,d) (((unsigned) (a) << 24) + ((unsigned) (b) << 16) + ((unsigned) (c) << 8) + (unsigned) (d))
static int stbi__parse_png_file(stbi__png *z, int scan, int req_comp)
{
stbi_uc palette[1024], pal_img_n=0;
stbi_uc has_trans=0, tc[3]={0};
stbi__uint16 tc16[3];
stbi__uint32 ioff=0, idata_limit=0, i, pal_len=0;
int first=1,k,interlace=0, color=0, is_iphone=0;
stbi__context *s = z->s;
z->expanded = NULL;
z->idata = NULL;
z->out = NULL;
if (!stbi__check_png_header(s)) return 0;
if (scan == STBI__SCAN_type) return 1;
for (;;) {
stbi__pngchunk c = stbi__get_chunk_header(s);
switch (c.type) {
case STBI__PNG_TYPE('C','g','B','I'):
is_iphone = 1;
stbi__skip(s, c.length);
break;
case STBI__PNG_TYPE('I','H','D','R'): {
int comp,filter;
if (!first) return stbi__err("multiple IHDR","Corrupt PNG");
first = 0;
if (c.length != 13) return stbi__err("bad IHDR len","Corrupt PNG");
s->img_x = stbi__get32be(s); if (s->img_x > (1 << 24)) return stbi__err("too large","Very large image (corrupt?)");
s->img_y = stbi__get32be(s); if (s->img_y > (1 << 24)) return stbi__err("too large","Very large image (corrupt?)");
z->depth = stbi__get8(s); if (z->depth != 1 && z->depth != 2 && z->depth != 4 && z->depth != 8 && z->depth != 16) return stbi__err("1/2/4/8/16-bit only","PNG not supported: 1/2/4/8/16-bit only");
color = stbi__get8(s); if (color > 6) return stbi__err("bad ctype","Corrupt PNG");
if (color == 3 && z->depth == 16) return stbi__err("bad ctype","Corrupt PNG");
if (color == 3) pal_img_n = 3; else if (color & 1) return stbi__err("bad ctype","Corrupt PNG");
comp = stbi__get8(s); if (comp) return stbi__err("bad comp method","Corrupt PNG");
filter= stbi__get8(s); if (filter) return stbi__err("bad filter method","Corrupt PNG");
interlace = stbi__get8(s); if (interlace>1) return stbi__err("bad interlace method","Corrupt PNG");
if (!s->img_x || !s->img_y) return stbi__err("0-pixel image","Corrupt PNG");
if (!pal_img_n) {
s->img_n = (color & 2 ? 3 : 1) + (color & 4 ? 1 : 0);
if ((1 << 30) / s->img_x / s->img_n < s->img_y) return stbi__err("too large", "Image too large to decode");
if (scan == STBI__SCAN_header) return 1;
} else {
// if paletted, then pal_n is our final components, and
// img_n is # components to decompress/filter.
s->img_n = 1;
if ((1 << 30) / s->img_x / 4 < s->img_y) return stbi__err("too large","Corrupt PNG");
// if SCAN_header, have to scan to see if we have a tRNS
}
break;
}
case STBI__PNG_TYPE('P','L','T','E'): {
if (first) return stbi__err("first not IHDR", "Corrupt PNG");
if (c.length > 256*3) return stbi__err("invalid PLTE","Corrupt PNG");
pal_len = c.length / 3;
if (pal_len * 3 != c.length) return stbi__err("invalid PLTE","Corrupt PNG");
for (i=0; i < pal_len; ++i) {
palette[i*4+0] = stbi__get8(s);
palette[i*4+1] = stbi__get8(s);
palette[i*4+2] = stbi__get8(s);
palette[i*4+3] = 255;
}
break;
}
case STBI__PNG_TYPE('t','R','N','S'): {
if (first) return stbi__err("first not IHDR", "Corrupt PNG");
if (z->idata) return stbi__err("tRNS after IDAT","Corrupt PNG");
if (pal_img_n) {
if (scan == STBI__SCAN_header) { s->img_n = 4; return 1; }
if (pal_len == 0) return stbi__err("tRNS before PLTE","Corrupt PNG");
if (c.length > pal_len) return stbi__err("bad tRNS len","Corrupt PNG");
pal_img_n = 4;
for (i=0; i < c.length; ++i)
palette[i*4+3] = stbi__get8(s);
} else {
if (!(s->img_n & 1)) return stbi__err("tRNS with alpha","Corrupt PNG");
if (c.length != (stbi__uint32) s->img_n*2) return stbi__err("bad tRNS len","Corrupt PNG");
has_trans = 1;
if (z->depth == 16) {
for (k = 0; k < s->img_n; ++k) tc16[k] = (stbi__uint16)stbi__get16be(s); // copy the values as-is
} else {
for (k = 0; k < s->img_n; ++k) tc[k] = (stbi_uc)(stbi__get16be(s) & 255) * stbi__depth_scale_table[z->depth]; // non 8-bit images will be larger
}
}
break;
}
case STBI__PNG_TYPE('I','D','A','T'): {
if (first) return stbi__err("first not IHDR", "Corrupt PNG");
if (pal_img_n && !pal_len) return stbi__err("no PLTE","Corrupt PNG");
if (scan == STBI__SCAN_header) { s->img_n = pal_img_n; return 1; }
if ((int)(ioff + c.length) < (int)ioff) return 0;
if (ioff + c.length > idata_limit) {
stbi__uint32 idata_limit_old = idata_limit;
stbi_uc *p;
if (idata_limit == 0) idata_limit = c.length > 4096 ? c.length : 4096;
while (ioff + c.length > idata_limit)
idata_limit *= 2;
STBI_NOTUSED(idata_limit_old);
p = (stbi_uc *) STBI_REALLOC_SIZED(z->idata, idata_limit_old, idata_limit); if (p == NULL) return stbi__err("outofmem", "Out of memory");
z->idata = p;
}
if (!stbi__getn(s, z->idata+ioff,c.length)) return stbi__err("outofdata","Corrupt PNG");
ioff += c.length;
break;
}
case STBI__PNG_TYPE('I','E','N','D'): {
stbi__uint32 raw_len, bpl;
if (first) return stbi__err("first not IHDR", "Corrupt PNG");
if (scan != STBI__SCAN_load) return 1;
if (z->idata == NULL) return stbi__err("no IDAT","Corrupt PNG");
// initial guess for decoded data size to avoid unnecessary reallocs
bpl = (s->img_x * z->depth + 7) / 8; // bytes per line, per component
raw_len = bpl * s->img_y * s->img_n /* pixels */ + s->img_y /* filter mode per row */;
z->expanded = (stbi_uc *) stbi_zlib_decode_malloc_guesssize_headerflag((char *) z->idata, ioff, raw_len, (int *) &raw_len, !is_iphone);
if (z->expanded == NULL) return 0; // zlib should set error
STBI_FREE(z->idata); z->idata = NULL;
if ((req_comp == s->img_n+1 && req_comp != 3 && !pal_img_n) || has_trans)
s->img_out_n = s->img_n+1;
else
s->img_out_n = s->img_n;
if (!stbi__create_png_image(z, z->expanded, raw_len, s->img_out_n, z->depth, color, interlace)) return 0;
if (has_trans) {
if (z->depth == 16) {
if (!stbi__compute_transparency16(z, tc16, s->img_out_n)) return 0;
} else {
if (!stbi__compute_transparency(z, tc, s->img_out_n)) return 0;
}
}
if (is_iphone && stbi__de_iphone_flag && s->img_out_n > 2)
stbi__de_iphone(z);
if (pal_img_n) {
// pal_img_n == 3 or 4
s->img_n = pal_img_n; // record the actual colors we had
s->img_out_n = pal_img_n;
if (req_comp >= 3) s->img_out_n = req_comp;
if (!stbi__expand_png_palette(z, palette, pal_len, s->img_out_n))
return 0;
} else if (has_trans) {
// non-paletted image with tRNS -> source image has (constant) alpha
++s->img_n;
}
STBI_FREE(z->expanded); z->expanded = NULL;
// end of PNG chunk, read and skip CRC
stbi__get32be(s);
return 1;
}
default:
// if critical, fail
if (first) return stbi__err("first not IHDR", "Corrupt PNG");
if ((c.type & (1 << 29)) == 0) {
#ifndef STBI_NO_FAILURE_STRINGS
// not threadsafe
static char invalid_chunk[] = "XXXX PNG chunk not known";
invalid_chunk[0] = STBI__BYTECAST(c.type >> 24);
invalid_chunk[1] = STBI__BYTECAST(c.type >> 16);
invalid_chunk[2] = STBI__BYTECAST(c.type >> 8);
invalid_chunk[3] = STBI__BYTECAST(c.type >> 0);
#endif
return stbi__err(invalid_chunk, "PNG not supported: unknown PNG chunk type");
}
stbi__skip(s, c.length);
break;
}
// end of PNG chunk, read and skip CRC
stbi__get32be(s);
}
}
static void *stbi__do_png(stbi__png *p, int *x, int *y, int *n, int req_comp, stbi__result_info *ri)
{
void *result=NULL;
if (req_comp < 0 || req_comp > 4) return stbi__errpuc("bad req_comp", "Internal error");
if (stbi__parse_png_file(p, STBI__SCAN_load, req_comp)) {
if (p->depth < 8)
ri->bits_per_channel = 8;
else
ri->bits_per_channel = p->depth;
result = p->out;
p->out = NULL;
if (req_comp && req_comp != p->s->img_out_n) {
if (ri->bits_per_channel == 8)
result = stbi__convert_format((unsigned char *) result, p->s->img_out_n, req_comp, p->s->img_x, p->s->img_y);
else
result = stbi__convert_format16((stbi__uint16 *) result, p->s->img_out_n, req_comp, p->s->img_x, p->s->img_y);
p->s->img_out_n = req_comp;
if (result == NULL) return result;
}
*x = p->s->img_x;
*y = p->s->img_y;
if (n) *n = p->s->img_n;
}
STBI_FREE(p->out); p->out = NULL;
STBI_FREE(p->expanded); p->expanded = NULL;
STBI_FREE(p->idata); p->idata = NULL;
return result;
}
static void *stbi__png_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri)
{
stbi__png p;
p.s = s;
return stbi__do_png(&p, x,y,comp,req_comp, ri);
}
static int stbi__png_test(stbi__context *s)
{
int r;
r = stbi__check_png_header(s);
stbi__rewind(s);
return r;
}
static int stbi__png_info_raw(stbi__png *p, int *x, int *y, int *comp)
{
if (!stbi__parse_png_file(p, STBI__SCAN_header, 0)) {
stbi__rewind( p->s );
return 0;
}
if (x) *x = p->s->img_x;
if (y) *y = p->s->img_y;
if (comp) *comp = p->s->img_n;
return 1;
}
static int stbi__png_info(stbi__context *s, int *x, int *y, int *comp)
{
stbi__png p;
p.s = s;
return stbi__png_info_raw(&p, x, y, comp);
}
static int stbi__png_is16(stbi__context *s)
{
stbi__png p;
p.s = s;
if (!stbi__png_info_raw(&p, NULL, NULL, NULL))
return 0;
if (p.depth != 16) {
stbi__rewind(p.s);
return 0;
}
return 1;
}
#endif
// Microsoft/Windows BMP image
#ifndef STBI_NO_BMP
static int stbi__bmp_test_raw(stbi__context *s)
{
int r;
int sz;
if (stbi__get8(s) != 'B') return 0;
if (stbi__get8(s) != 'M') return 0;
stbi__get32le(s); // discard filesize
stbi__get16le(s); // discard reserved
stbi__get16le(s); // discard reserved
stbi__get32le(s); // discard data offset
sz = stbi__get32le(s);
r = (sz == 12 || sz == 40 || sz == 56 || sz == 108 || sz == 124);
return r;
}
static int stbi__bmp_test(stbi__context *s)
{
int r = stbi__bmp_test_raw(s);
stbi__rewind(s);
return r;
}
// returns 0..31 for the highest set bit
static int stbi__high_bit(unsigned int z)
{
int n=0;
if (z == 0) return -1;
if (z >= 0x10000) { n += 16; z >>= 16; }
if (z >= 0x00100) { n += 8; z >>= 8; }
if (z >= 0x00010) { n += 4; z >>= 4; }
if (z >= 0x00004) { n += 2; z >>= 2; }
if (z >= 0x00002) { n += 1;/* >>= 1;*/ }
return n;
}
static int stbi__bitcount(unsigned int a)
{
a = (a & 0x55555555) + ((a >> 1) & 0x55555555); // max 2
a = (a & 0x33333333) + ((a >> 2) & 0x33333333); // max 4
a = (a + (a >> 4)) & 0x0f0f0f0f; // max 8 per 4, now 8 bits
a = (a + (a >> 8)); // max 16 per 8 bits
a = (a + (a >> 16)); // max 32 per 8 bits
return a & 0xff;
}
// extract an arbitrarily-aligned N-bit value (N=bits)
// from v, and then make it 8-bits long and fractionally
// extend it to full full range.
static int stbi__shiftsigned(unsigned int v, int shift, int bits)
{
static unsigned int mul_table[9] = {
0,
0xff/*0b11111111*/, 0x55/*0b01010101*/, 0x49/*0b01001001*/, 0x11/*0b00010001*/,
0x21/*0b00100001*/, 0x41/*0b01000001*/, 0x81/*0b10000001*/, 0x01/*0b00000001*/,
};
static unsigned int shift_table[9] = {
0, 0,0,1,0,2,4,6,0,
};
if (shift < 0)
v <<= -shift;
else
v >>= shift;
STBI_ASSERT(v < 256);
v >>= (8-bits);
STBI_ASSERT(bits >= 0 && bits <= 8);
return (int) ((unsigned) v * mul_table[bits]) >> shift_table[bits];
}
typedef struct
{
int bpp, offset, hsz;
unsigned int mr,mg,mb,ma, all_a;
int extra_read;
} stbi__bmp_data;
static void *stbi__bmp_parse_header(stbi__context *s, stbi__bmp_data *info)
{
int hsz;
if (stbi__get8(s) != 'B' || stbi__get8(s) != 'M') return stbi__errpuc("not BMP", "Corrupt BMP");
stbi__get32le(s); // discard filesize
stbi__get16le(s); // discard reserved
stbi__get16le(s); // discard reserved
info->offset = stbi__get32le(s);
info->hsz = hsz = stbi__get32le(s);
info->mr = info->mg = info->mb = info->ma = 0;
info->extra_read = 14;
if (hsz != 12 && hsz != 40 && hsz != 56 && hsz != 108 && hsz != 124) return stbi__errpuc("unknown BMP", "BMP type not supported: unknown");
if (hsz == 12) {
s->img_x = stbi__get16le(s);
s->img_y = stbi__get16le(s);
} else {
s->img_x = stbi__get32le(s);
s->img_y = stbi__get32le(s);
}
if (stbi__get16le(s) != 1) return stbi__errpuc("bad BMP", "bad BMP");
info->bpp = stbi__get16le(s);
if (hsz != 12) {
int compress = stbi__get32le(s);
if (compress == 1 || compress == 2) return stbi__errpuc("BMP RLE", "BMP type not supported: RLE");
stbi__get32le(s); // discard sizeof
stbi__get32le(s); // discard hres
stbi__get32le(s); // discard vres
stbi__get32le(s); // discard colorsused
stbi__get32le(s); // discard max important
if (hsz == 40 || hsz == 56) {
if (hsz == 56) {
stbi__get32le(s);
stbi__get32le(s);
stbi__get32le(s);
stbi__get32le(s);
}
if (info->bpp == 16 || info->bpp == 32) {
if (compress == 0) {
if (info->bpp == 32) {
info->mr = 0xffu << 16;
info->mg = 0xffu << 8;
info->mb = 0xffu << 0;
info->ma = 0xffu << 24;
info->all_a = 0; // if all_a is 0 at end, then we loaded alpha channel but it was all 0
} else {
info->mr = 31u << 10;
info->mg = 31u << 5;
info->mb = 31u << 0;
}
} else if (compress == 3) {
info->mr = stbi__get32le(s);
info->mg = stbi__get32le(s);
info->mb = stbi__get32le(s);
info->extra_read += 12;
// not documented, but generated by photoshop and handled by mspaint
if (info->mr == info->mg && info->mg == info->mb) {
// ?!?!?
return stbi__errpuc("bad BMP", "bad BMP");
}
} else
return stbi__errpuc("bad BMP", "bad BMP");
}
} else {
int i;
if (hsz != 108 && hsz != 124)
return stbi__errpuc("bad BMP", "bad BMP");
info->mr = stbi__get32le(s);
info->mg = stbi__get32le(s);
info->mb = stbi__get32le(s);
info->ma = stbi__get32le(s);
stbi__get32le(s); // discard color space
for (i=0; i < 12; ++i)
stbi__get32le(s); // discard color space parameters
if (hsz == 124) {
stbi__get32le(s); // discard rendering intent
stbi__get32le(s); // discard offset of profile data
stbi__get32le(s); // discard size of profile data
stbi__get32le(s); // discard reserved
}
}
}
return (void *) 1;
}
static void *stbi__bmp_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri)
{
stbi_uc *out;
unsigned int mr=0,mg=0,mb=0,ma=0, all_a;
stbi_uc pal[256][4];
int psize=0,i,j,width;
int flip_vertically, pad, target;
stbi__bmp_data info;
STBI_NOTUSED(ri);
info.all_a = 255;
if (stbi__bmp_parse_header(s, &info) == NULL)
return NULL; // error code already set
flip_vertically = ((int) s->img_y) > 0;
s->img_y = abs((int) s->img_y);
mr = info.mr;
mg = info.mg;
mb = info.mb;
ma = info.ma;
all_a = info.all_a;
if (info.hsz == 12) {
if (info.bpp < 24)
psize = (info.offset - info.extra_read - 24) / 3;
} else {
if (info.bpp < 16)
psize = (info.offset - info.extra_read - info.hsz) >> 2;
}
if (psize == 0) {
STBI_ASSERT(info.offset == (s->img_buffer - s->buffer_start));
}
if (info.bpp == 24 && ma == 0xff000000)
s->img_n = 3;
else
s->img_n = ma ? 4 : 3;
if (req_comp && req_comp >= 3) // we can directly decode 3 or 4
target = req_comp;
else
target = s->img_n; // if they want monochrome, we'll post-convert
// sanity-check size
if (!stbi__mad3sizes_valid(target, s->img_x, s->img_y, 0))
return stbi__errpuc("too large", "Corrupt BMP");
out = (stbi_uc *) stbi__malloc_mad3(target, s->img_x, s->img_y, 0);
if (!out) return stbi__errpuc("outofmem", "Out of memory");
if (info.bpp < 16) {
int z=0;
if (psize == 0 || psize > 256) { STBI_FREE(out); return stbi__errpuc("invalid", "Corrupt BMP"); }
for (i=0; i < psize; ++i) {
pal[i][2] = stbi__get8(s);
pal[i][1] = stbi__get8(s);
pal[i][0] = stbi__get8(s);
if (info.hsz != 12) stbi__get8(s);
pal[i][3] = 255;
}
stbi__skip(s, info.offset - info.extra_read - info.hsz - psize * (info.hsz == 12 ? 3 : 4));
if (info.bpp == 1) width = (s->img_x + 7) >> 3;
else if (info.bpp == 4) width = (s->img_x + 1) >> 1;
else if (info.bpp == 8) width = s->img_x;
else { STBI_FREE(out); return stbi__errpuc("bad bpp", "Corrupt BMP"); }
pad = (-width)&3;
if (info.bpp == 1) {
for (j=0; j < (int) s->img_y; ++j) {
int bit_offset = 7, v = stbi__get8(s);
for (i=0; i < (int) s->img_x; ++i) {
int color = (v>>bit_offset)&0x1;
out[z++] = pal[color][0];
out[z++] = pal[color][1];
out[z++] = pal[color][2];
if (target == 4) out[z++] = 255;
if (i+1 == (int) s->img_x) break;
if((--bit_offset) < 0) {
bit_offset = 7;
v = stbi__get8(s);
}
}
stbi__skip(s, pad);
}
} else {
for (j=0; j < (int) s->img_y; ++j) {
for (i=0; i < (int) s->img_x; i += 2) {
int v=stbi__get8(s),v2=0;
if (info.bpp == 4) {
v2 = v & 15;
v >>= 4;
}
out[z++] = pal[v][0];
out[z++] = pal[v][1];
out[z++] = pal[v][2];
if (target == 4) out[z++] = 255;
if (i+1 == (int) s->img_x) break;
v = (info.bpp == 8) ? stbi__get8(s) : v2;
out[z++] = pal[v][0];
out[z++] = pal[v][1];
out[z++] = pal[v][2];
if (target == 4) out[z++] = 255;
}
stbi__skip(s, pad);
}
}
} else {
int rshift=0,gshift=0,bshift=0,ashift=0,rcount=0,gcount=0,bcount=0,acount=0;
int z = 0;
int easy=0;
stbi__skip(s, info.offset - info.extra_read - info.hsz);
if (info.bpp == 24) width = 3 * s->img_x;
else if (info.bpp == 16) width = 2*s->img_x;
else /* bpp = 32 and pad = 0 */ width=0;
pad = (-width) & 3;
if (info.bpp == 24) {
easy = 1;
} else if (info.bpp == 32) {
if (mb == 0xff && mg == 0xff00 && mr == 0x00ff0000 && ma == 0xff000000)
easy = 2;
}
if (!easy) {
if (!mr || !mg || !mb) { STBI_FREE(out); return stbi__errpuc("bad masks", "Corrupt BMP"); }
// right shift amt to put high bit in position #7
rshift = stbi__high_bit(mr)-7; rcount = stbi__bitcount(mr);
gshift = stbi__high_bit(mg)-7; gcount = stbi__bitcount(mg);
bshift = stbi__high_bit(mb)-7; bcount = stbi__bitcount(mb);
ashift = stbi__high_bit(ma)-7; acount = stbi__bitcount(ma);
}
for (j=0; j < (int) s->img_y; ++j) {
if (easy) {
for (i=0; i < (int) s->img_x; ++i) {
unsigned char a;
out[z+2] = stbi__get8(s);
out[z+1] = stbi__get8(s);
out[z+0] = stbi__get8(s);
z += 3;
a = (easy == 2 ? stbi__get8(s) : 255);
all_a |= a;
if (target == 4) out[z++] = a;
}
} else {
int bpp = info.bpp;
for (i=0; i < (int) s->img_x; ++i) {
stbi__uint32 v = (bpp == 16 ? (stbi__uint32) stbi__get16le(s) : stbi__get32le(s));
unsigned int a;
out[z++] = STBI__BYTECAST(stbi__shiftsigned(v & mr, rshift, rcount));
out[z++] = STBI__BYTECAST(stbi__shiftsigned(v & mg, gshift, gcount));
out[z++] = STBI__BYTECAST(stbi__shiftsigned(v & mb, bshift, bcount));
a = (ma ? stbi__shiftsigned(v & ma, ashift, acount) : 255);
all_a |= a;
if (target == 4) out[z++] = STBI__BYTECAST(a);
}
}
stbi__skip(s, pad);
}
}
// if alpha channel is all 0s, replace with all 255s
if (target == 4 && all_a == 0)
for (i=4*s->img_x*s->img_y-1; i >= 0; i -= 4)
out[i] = 255;
if (flip_vertically) {
stbi_uc t;
for (j=0; j < (int) s->img_y>>1; ++j) {
stbi_uc *p1 = out + j *s->img_x*target;
stbi_uc *p2 = out + (s->img_y-1-j)*s->img_x*target;
for (i=0; i < (int) s->img_x*target; ++i) {
t = p1[i]; p1[i] = p2[i]; p2[i] = t;
}
}
}
if (req_comp && req_comp != target) {
out = stbi__convert_format(out, target, req_comp, s->img_x, s->img_y);
if (out == NULL) return out; // stbi__convert_format frees input on failure
}
*x = s->img_x;
*y = s->img_y;
if (comp) *comp = s->img_n;
return out;
}
#endif
// Targa Truevision - TGA
// by Jonathan Dummer
#ifndef STBI_NO_TGA
// returns STBI_rgb or whatever, 0 on error
static int stbi__tga_get_comp(int bits_per_pixel, int is_grey, int* is_rgb16)
{
// only RGB or RGBA (incl. 16bit) or grey allowed
if (is_rgb16) *is_rgb16 = 0;
switch(bits_per_pixel) {
case 8: return STBI_grey;
case 16: if(is_grey) return STBI_grey_alpha;
// fallthrough
case 15: if(is_rgb16) *is_rgb16 = 1;
return STBI_rgb;
case 24: // fallthrough
case 32: return bits_per_pixel/8;
default: return 0;
}
}
static int stbi__tga_info(stbi__context *s, int *x, int *y, int *comp)
{
int tga_w, tga_h, tga_comp, tga_image_type, tga_bits_per_pixel, tga_colormap_bpp;
int sz, tga_colormap_type;
stbi__get8(s); // discard Offset
tga_colormap_type = stbi__get8(s); // colormap type
if( tga_colormap_type > 1 ) {
stbi__rewind(s);
return 0; // only RGB or indexed allowed
}
tga_image_type = stbi__get8(s); // image type
if ( tga_colormap_type == 1 ) { // colormapped (paletted) image
if (tga_image_type != 1 && tga_image_type != 9) {
stbi__rewind(s);
return 0;
}
stbi__skip(s,4); // skip index of first colormap entry and number of entries
sz = stbi__get8(s); // check bits per palette color entry
if ( (sz != 8) && (sz != 15) && (sz != 16) && (sz != 24) && (sz != 32) ) {
stbi__rewind(s);
return 0;
}
stbi__skip(s,4); // skip image x and y origin
tga_colormap_bpp = sz;
} else { // "normal" image w/o colormap - only RGB or grey allowed, +/- RLE
if ( (tga_image_type != 2) && (tga_image_type != 3) && (tga_image_type != 10) && (tga_image_type != 11) ) {
stbi__rewind(s);
return 0; // only RGB or grey allowed, +/- RLE
}
stbi__skip(s,9); // skip colormap specification and image x/y origin
tga_colormap_bpp = 0;
}
tga_w = stbi__get16le(s);
if( tga_w < 1 ) {
stbi__rewind(s);
return 0; // test width
}
tga_h = stbi__get16le(s);
if( tga_h < 1 ) {
stbi__rewind(s);
return 0; // test height
}
tga_bits_per_pixel = stbi__get8(s); // bits per pixel
stbi__get8(s); // ignore alpha bits
if (tga_colormap_bpp != 0) {
if((tga_bits_per_pixel != 8) && (tga_bits_per_pixel != 16)) {
// when using a colormap, tga_bits_per_pixel is the size of the indexes
// I don't think anything but 8 or 16bit indexes makes sense
stbi__rewind(s);
return 0;
}
tga_comp = stbi__tga_get_comp(tga_colormap_bpp, 0, NULL);
} else {
tga_comp = stbi__tga_get_comp(tga_bits_per_pixel, (tga_image_type == 3) || (tga_image_type == 11), NULL);
}
if(!tga_comp) {
stbi__rewind(s);
return 0;
}
if (x) *x = tga_w;
if (y) *y = tga_h;
if (comp) *comp = tga_comp;
return 1; // seems to have passed everything
}
static int stbi__tga_test(stbi__context *s)
{
int res = 0;
int sz, tga_color_type;
stbi__get8(s); // discard Offset
tga_color_type = stbi__get8(s); // color type
if ( tga_color_type > 1 ) goto errorEnd; // only RGB or indexed allowed
sz = stbi__get8(s); // image type
if ( tga_color_type == 1 ) { // colormapped (paletted) image
if (sz != 1 && sz != 9) goto errorEnd; // colortype 1 demands image type 1 or 9
stbi__skip(s,4); // skip index of first colormap entry and number of entries
sz = stbi__get8(s); // check bits per palette color entry
if ( (sz != 8) && (sz != 15) && (sz != 16) && (sz != 24) && (sz != 32) ) goto errorEnd;
stbi__skip(s,4); // skip image x and y origin
} else { // "normal" image w/o colormap
if ( (sz != 2) && (sz != 3) && (sz != 10) && (sz != 11) ) goto errorEnd; // only RGB or grey allowed, +/- RLE
stbi__skip(s,9); // skip colormap specification and image x/y origin
}
if ( stbi__get16le(s) < 1 ) goto errorEnd; // test width
if ( stbi__get16le(s) < 1 ) goto errorEnd; // test height
sz = stbi__get8(s); // bits per pixel
if ( (tga_color_type == 1) && (sz != 8) && (sz != 16) ) goto errorEnd; // for colormapped images, bpp is size of an index
if ( (sz != 8) && (sz != 15) && (sz != 16) && (sz != 24) && (sz != 32) ) goto errorEnd;
res = 1; // if we got this far, everything's good and we can return 1 instead of 0
errorEnd:
stbi__rewind(s);
return res;
}
// read 16bit value and convert to 24bit RGB
static void stbi__tga_read_rgb16(stbi__context *s, stbi_uc* out)
{
stbi__uint16 px = (stbi__uint16)stbi__get16le(s);
stbi__uint16 fiveBitMask = 31;
// we have 3 channels with 5bits each
int r = (px >> 10) & fiveBitMask;
int g = (px >> 5) & fiveBitMask;
int b = px & fiveBitMask;
// Note that this saves the data in RGB(A) order, so it doesn't need to be swapped later
out[0] = (stbi_uc)((r * 255)/31);
out[1] = (stbi_uc)((g * 255)/31);
out[2] = (stbi_uc)((b * 255)/31);
// some people claim that the most significant bit might be used for alpha
// (possibly if an alpha-bit is set in the "image descriptor byte")
// but that only made 16bit test images completely translucent..
// so let's treat all 15 and 16bit TGAs as RGB with no alpha.
}
static void *stbi__tga_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri)
{
// read in the TGA header stuff
int tga_offset = stbi__get8(s);
int tga_indexed = stbi__get8(s);
int tga_image_type = stbi__get8(s);
int tga_is_RLE = 0;
int tga_palette_start = stbi__get16le(s);
int tga_palette_len = stbi__get16le(s);
int tga_palette_bits = stbi__get8(s);
int tga_x_origin = stbi__get16le(s);
int tga_y_origin = stbi__get16le(s);
int tga_width = stbi__get16le(s);
int tga_height = stbi__get16le(s);
int tga_bits_per_pixel = stbi__get8(s);
int tga_comp, tga_rgb16=0;
int tga_inverted = stbi__get8(s);
// int tga_alpha_bits = tga_inverted & 15; // the 4 lowest bits - unused (useless?)
// image data
unsigned char *tga_data;
unsigned char *tga_palette = NULL;
int i, j;
unsigned char raw_data[4] = {0};
int RLE_count = 0;
int RLE_repeating = 0;
int read_next_pixel = 1;
STBI_NOTUSED(ri);
STBI_NOTUSED(tga_x_origin); // @TODO
STBI_NOTUSED(tga_y_origin); // @TODO
// do a tiny bit of precessing
if ( tga_image_type >= 8 )
{
tga_image_type -= 8;
tga_is_RLE = 1;
}
tga_inverted = 1 - ((tga_inverted >> 5) & 1);
// If I'm paletted, then I'll use the number of bits from the palette
if ( tga_indexed ) tga_comp = stbi__tga_get_comp(tga_palette_bits, 0, &tga_rgb16);
else tga_comp = stbi__tga_get_comp(tga_bits_per_pixel, (tga_image_type == 3), &tga_rgb16);
if(!tga_comp) // shouldn't really happen, stbi__tga_test() should have ensured basic consistency
return stbi__errpuc("bad format", "Can't find out TGA pixelformat");
// tga info
*x = tga_width;
*y = tga_height;
if (comp) *comp = tga_comp;
if (!stbi__mad3sizes_valid(tga_width, tga_height, tga_comp, 0))
return stbi__errpuc("too large", "Corrupt TGA");
tga_data = (unsigned char*)stbi__malloc_mad3(tga_width, tga_height, tga_comp, 0);
if (!tga_data) return stbi__errpuc("outofmem", "Out of memory");
// skip to the data's starting position (offset usually = 0)
stbi__skip(s, tga_offset );
if ( !tga_indexed && !tga_is_RLE && !tga_rgb16 ) {
for (i=0; i < tga_height; ++i) {
int row = tga_inverted ? tga_height -i - 1 : i;
stbi_uc *tga_row = tga_data + row*tga_width*tga_comp;
stbi__getn(s, tga_row, tga_width * tga_comp);
}
} else {
// do I need to load a palette?
if ( tga_indexed)
{
// any data to skip? (offset usually = 0)
stbi__skip(s, tga_palette_start );
// load the palette
tga_palette = (unsigned char*)stbi__malloc_mad2(tga_palette_len, tga_comp, 0);
if (!tga_palette) {
STBI_FREE(tga_data);
return stbi__errpuc("outofmem", "Out of memory");
}
if (tga_rgb16) {
stbi_uc *pal_entry = tga_palette;
STBI_ASSERT(tga_comp == STBI_rgb);
for (i=0; i < tga_palette_len; ++i) {
stbi__tga_read_rgb16(s, pal_entry);
pal_entry += tga_comp;
}
} else if (!stbi__getn(s, tga_palette, tga_palette_len * tga_comp)) {
STBI_FREE(tga_data);
STBI_FREE(tga_palette);
return stbi__errpuc("bad palette", "Corrupt TGA");
}
}
// load the data
for (i=0; i < tga_width * tga_height; ++i)
{
// if I'm in RLE mode, do I need to get a RLE stbi__pngchunk?
if ( tga_is_RLE )
{
if ( RLE_count == 0 )
{
// yep, get the next byte as a RLE command
int RLE_cmd = stbi__get8(s);
RLE_count = 1 + (RLE_cmd & 127);
RLE_repeating = RLE_cmd >> 7;
read_next_pixel = 1;
} else if ( !RLE_repeating )
{
read_next_pixel = 1;
}
} else
{
read_next_pixel = 1;
}
// OK, if I need to read a pixel, do it now
if ( read_next_pixel )
{
// load however much data we did have
if ( tga_indexed )
{
// read in index, then perform the lookup
int pal_idx = (tga_bits_per_pixel == 8) ? stbi__get8(s) : stbi__get16le(s);
if ( pal_idx >= tga_palette_len ) {
// invalid index
pal_idx = 0;
}
pal_idx *= tga_comp;
for (j = 0; j < tga_comp; ++j) {
raw_data[j] = tga_palette[pal_idx+j];
}
} else if(tga_rgb16) {
STBI_ASSERT(tga_comp == STBI_rgb);
stbi__tga_read_rgb16(s, raw_data);
} else {
// read in the data raw
for (j = 0; j < tga_comp; ++j) {
raw_data[j] = stbi__get8(s);
}
}
// clear the reading flag for the next pixel
read_next_pixel = 0;
} // end of reading a pixel
// copy data
for (j = 0; j < tga_comp; ++j)
tga_data[i*tga_comp+j] = raw_data[j];
// in case we're in RLE mode, keep counting down
--RLE_count;
}
// do I need to invert the image?
if ( tga_inverted )
{
for (j = 0; j*2 < tga_height; ++j)
{
int index1 = j * tga_width * tga_comp;
int index2 = (tga_height - 1 - j) * tga_width * tga_comp;
for (i = tga_width * tga_comp; i > 0; --i)
{
unsigned char temp = tga_data[index1];
tga_data[index1] = tga_data[index2];
tga_data[index2] = temp;
++index1;
++index2;
}
}
}
// clear my palette, if I had one
if ( tga_palette != NULL )
{
STBI_FREE( tga_palette );
}
}
// swap RGB - if the source data was RGB16, it already is in the right order
if (tga_comp >= 3 && !tga_rgb16)
{
unsigned char* tga_pixel = tga_data;
for (i=0; i < tga_width * tga_height; ++i)
{
unsigned char temp = tga_pixel[0];
tga_pixel[0] = tga_pixel[2];
tga_pixel[2] = temp;
tga_pixel += tga_comp;
}
}
// convert to target component count
if (req_comp && req_comp != tga_comp)
tga_data = stbi__convert_format(tga_data, tga_comp, req_comp, tga_width, tga_height);
// the things I do to get rid of an error message, and yet keep
// Microsoft's C compilers happy... [8^(
tga_palette_start = tga_palette_len = tga_palette_bits =
tga_x_origin = tga_y_origin = 0;
STBI_NOTUSED(tga_palette_start);
// OK, done
return tga_data;
}
#endif
// *************************************************************************************************
// Photoshop PSD loader -- PD by Thatcher Ulrich, integration by Nicolas Schulz, tweaked by STB
#ifndef STBI_NO_PSD
static int stbi__psd_test(stbi__context *s)
{
int r = (stbi__get32be(s) == 0x38425053);
stbi__rewind(s);
return r;
}
static int stbi__psd_decode_rle(stbi__context *s, stbi_uc *p, int pixelCount)
{
int count, nleft, len;
count = 0;
while ((nleft = pixelCount - count) > 0) {
len = stbi__get8(s);
if (len == 128) {
// No-op.
} else if (len < 128) {
// Copy next len+1 bytes literally.
len++;
if (len > nleft) return 0; // corrupt data
count += len;
while (len) {
*p = stbi__get8(s);
p += 4;
len--;
}
} else if (len > 128) {
stbi_uc val;
// Next -len+1 bytes in the dest are replicated from next source byte.
// (Interpret len as a negative 8-bit int.)
len = 257 - len;
if (len > nleft) return 0; // corrupt data
val = stbi__get8(s);
count += len;
while (len) {
*p = val;
p += 4;
len--;
}
}
}
return 1;
}
static void *stbi__psd_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri, int bpc)
{
int pixelCount;
int channelCount, compression;
int channel, i;
int bitdepth;
int w,h;
stbi_uc *out;
STBI_NOTUSED(ri);
// Check identifier
if (stbi__get32be(s) != 0x38425053) // "8BPS"
return stbi__errpuc("not PSD", "Corrupt PSD image");
// Check file type version.
if (stbi__get16be(s) != 1)
return stbi__errpuc("wrong version", "Unsupported version of PSD image");
// Skip 6 reserved bytes.
stbi__skip(s, 6 );
// Read the number of channels (R, G, B, A, etc).
channelCount = stbi__get16be(s);
if (channelCount < 0 || channelCount > 16)
return stbi__errpuc("wrong channel count", "Unsupported number of channels in PSD image");
// Read the rows and columns of the image.
h = stbi__get32be(s);
w = stbi__get32be(s);
// Make sure the depth is 8 bits.
bitdepth = stbi__get16be(s);
if (bitdepth != 8 && bitdepth != 16)
return stbi__errpuc("unsupported bit depth", "PSD bit depth is not 8 or 16 bit");
// Make sure the color mode is RGB.
// Valid options are:
// 0: Bitmap
// 1: Grayscale
// 2: Indexed color
// 3: RGB color
// 4: CMYK color
// 7: Multichannel
// 8: Duotone
// 9: Lab color
if (stbi__get16be(s) != 3)
return stbi__errpuc("wrong color format", "PSD is not in RGB color format");
// Skip the Mode Data. (It's the palette for indexed color; other info for other modes.)
stbi__skip(s,stbi__get32be(s) );
// Skip the image resources. (resolution, pen tool paths, etc)
stbi__skip(s, stbi__get32be(s) );
// Skip the reserved data.
stbi__skip(s, stbi__get32be(s) );
// Find out if the data is compressed.
// Known values:
// 0: no compression
// 1: RLE compressed
compression = stbi__get16be(s);
if (compression > 1)
return stbi__errpuc("bad compression", "PSD has an unknown compression format");
// Check size
if (!stbi__mad3sizes_valid(4, w, h, 0))
return stbi__errpuc("too large", "Corrupt PSD");
// Create the destination image.
if (!compression && bitdepth == 16 && bpc == 16) {
out = (stbi_uc *) stbi__malloc_mad3(8, w, h, 0);
ri->bits_per_channel = 16;
} else
out = (stbi_uc *) stbi__malloc(4 * w*h);
if (!out) return stbi__errpuc("outofmem", "Out of memory");
pixelCount = w*h;
// Initialize the data to zero.
//memset( out, 0, pixelCount * 4 );
// Finally, the image data.
if (compression) {
// RLE as used by .PSD and .TIFF
// Loop until you get the number of unpacked bytes you are expecting:
// Read the next source byte into n.
// If n is between 0 and 127 inclusive, copy the next n+1 bytes literally.
// Else if n is between -127 and -1 inclusive, copy the next byte -n+1 times.
// Else if n is 128, noop.
// Endloop
// The RLE-compressed data is preceded by a 2-byte data count for each row in the data,
// which we're going to just skip.
stbi__skip(s, h * channelCount * 2 );
// Read the RLE data by channel.
for (channel = 0; channel < 4; channel++) {
stbi_uc *p;
p = out+channel;
if (channel >= channelCount) {
// Fill this channel with default data.
for (i = 0; i < pixelCount; i++, p += 4)
*p = (channel == 3 ? 255 : 0);
} else {
// Read the RLE data.
if (!stbi__psd_decode_rle(s, p, pixelCount)) {
STBI_FREE(out);
return stbi__errpuc("corrupt", "bad RLE data");
}
}
}
} else {
// We're at the raw image data. It's each channel in order (Red, Green, Blue, Alpha, ...)
// where each channel consists of an 8-bit (or 16-bit) value for each pixel in the image.
// Read the data by channel.
for (channel = 0; channel < 4; channel++) {
if (channel >= channelCount) {
// Fill this channel with default data.
if (bitdepth == 16 && bpc == 16) {
stbi__uint16 *q = ((stbi__uint16 *) out) + channel;
stbi__uint16 val = channel == 3 ? 65535 : 0;
for (i = 0; i < pixelCount; i++, q += 4)
*q = val;
} else {
stbi_uc *p = out+channel;
stbi_uc val = channel == 3 ? 255 : 0;
for (i = 0; i < pixelCount; i++, p += 4)
*p = val;
}
} else {
if (ri->bits_per_channel == 16) { // output bpc
stbi__uint16 *q = ((stbi__uint16 *) out) + channel;
for (i = 0; i < pixelCount; i++, q += 4)
*q = (stbi__uint16) stbi__get16be(s);
} else {
stbi_uc *p = out+channel;
if (bitdepth == 16) { // input bpc
for (i = 0; i < pixelCount; i++, p += 4)
*p = (stbi_uc) (stbi__get16be(s) >> 8);
} else {
for (i = 0; i < pixelCount; i++, p += 4)
*p = stbi__get8(s);
}
}
}
}
}
// remove weird white matte from PSD
if (channelCount >= 4) {
if (ri->bits_per_channel == 16) {
for (i=0; i < w*h; ++i) {
stbi__uint16 *pixel = (stbi__uint16 *) out + 4*i;
if (pixel[3] != 0 && pixel[3] != 65535) {
float a = pixel[3] / 65535.0f;
float ra = 1.0f / a;
float inv_a = 65535.0f * (1 - ra);
pixel[0] = (stbi__uint16) (pixel[0]*ra + inv_a);
pixel[1] = (stbi__uint16) (pixel[1]*ra + inv_a);
pixel[2] = (stbi__uint16) (pixel[2]*ra + inv_a);
}
}
} else {
for (i=0; i < w*h; ++i) {
unsigned char *pixel = out + 4*i;
if (pixel[3] != 0 && pixel[3] != 255) {
float a = pixel[3] / 255.0f;
float ra = 1.0f / a;
float inv_a = 255.0f * (1 - ra);
pixel[0] = (unsigned char) (pixel[0]*ra + inv_a);
pixel[1] = (unsigned char) (pixel[1]*ra + inv_a);
pixel[2] = (unsigned char) (pixel[2]*ra + inv_a);
}
}
}
}
// convert to desired output format
if (req_comp && req_comp != 4) {
if (ri->bits_per_channel == 16)
out = (stbi_uc *) stbi__convert_format16((stbi__uint16 *) out, 4, req_comp, w, h);
else
out = stbi__convert_format(out, 4, req_comp, w, h);
if (out == NULL) return out; // stbi__convert_format frees input on failure
}
if (comp) *comp = 4;
*y = h;
*x = w;
return out;
}
#endif
// *************************************************************************************************
// Softimage PIC loader
// by Tom Seddon
//
// See http://softimage.wiki.softimage.com/index.php/INFO:_PIC_file_format
// See http://ozviz.wasp.uwa.edu.au/~pbourke/dataformats/softimagepic/
#ifndef STBI_NO_PIC
static int stbi__pic_is4(stbi__context *s,const char *str)
{
int i;
for (i=0; i<4; ++i)
if (stbi__get8(s) != (stbi_uc)str[i])
return 0;
return 1;
}
static int stbi__pic_test_core(stbi__context *s)
{
int i;
if (!stbi__pic_is4(s,"\x53\x80\xF6\x34"))
return 0;
for(i=0;i<84;++i)
stbi__get8(s);
if (!stbi__pic_is4(s,"PICT"))
return 0;
return 1;
}
typedef struct
{
stbi_uc size,type,channel;
} stbi__pic_packet;
static stbi_uc *stbi__readval(stbi__context *s, int channel, stbi_uc *dest)
{
int mask=0x80, i;
for (i=0; i<4; ++i, mask>>=1) {
if (channel & mask) {
if (stbi__at_eof(s)) return stbi__errpuc("bad file","PIC file too short");
dest[i]=stbi__get8(s);
}
}
return dest;
}
static void stbi__copyval(int channel,stbi_uc *dest,const stbi_uc *src)
{
int mask=0x80,i;
for (i=0;i<4; ++i, mask>>=1)
if (channel&mask)
dest[i]=src[i];
}
static stbi_uc *stbi__pic_load_core(stbi__context *s,int width,int height,int *comp, stbi_uc *result)
{
int act_comp=0,num_packets=0,y,chained;
stbi__pic_packet packets[10];
// this will (should...) cater for even some bizarre stuff like having data
// for the same channel in multiple packets.
do {
stbi__pic_packet *packet;
if (num_packets==sizeof(packets)/sizeof(packets[0]))
return stbi__errpuc("bad format","too many packets");
packet = &packets[num_packets++];
chained = stbi__get8(s);
packet->size = stbi__get8(s);
packet->type = stbi__get8(s);
packet->channel = stbi__get8(s);
act_comp |= packet->channel;
if (stbi__at_eof(s)) return stbi__errpuc("bad file","file too short (reading packets)");
if (packet->size != 8) return stbi__errpuc("bad format","packet isn't 8bpp");
} while (chained);
*comp = (act_comp & 0x10 ? 4 : 3); // has alpha channel?
for(y=0; y<height; ++y) {
int packet_idx;
for(packet_idx=0; packet_idx < num_packets; ++packet_idx) {
stbi__pic_packet *packet = &packets[packet_idx];
stbi_uc *dest = result+y*width*4;
switch (packet->type) {
default:
return stbi__errpuc("bad format","packet has bad compression type");
case 0: {//uncompressed
int x;
for(x=0;x<width;++x, dest+=4)
if (!stbi__readval(s,packet->channel,dest))
return 0;
break;
}
case 1://Pure RLE
{
int left=width, i;
while (left>0) {
stbi_uc count,value[4];
count=stbi__get8(s);
if (stbi__at_eof(s)) return stbi__errpuc("bad file","file too short (pure read count)");
if (count > left)
count = (stbi_uc) left;
if (!stbi__readval(s,packet->channel,value)) return 0;
for(i=0; i<count; ++i,dest+=4)
stbi__copyval(packet->channel,dest,value);
left -= count;
}
}
break;
case 2: {//Mixed RLE
int left=width;
while (left>0) {
int count = stbi__get8(s), i;
if (stbi__at_eof(s)) return stbi__errpuc("bad file","file too short (mixed read count)");
if (count >= 128) { // Repeated
stbi_uc value[4];
if (count==128)
count = stbi__get16be(s);
else
count -= 127;
if (count > left)
return stbi__errpuc("bad file","scanline overrun");
if (!stbi__readval(s,packet->channel,value))
return 0;
for(i=0;i<count;++i, dest += 4)
stbi__copyval(packet->channel,dest,value);
} else { // Raw
++count;
if (count>left) return stbi__errpuc("bad file","scanline overrun");
for(i=0;i<count;++i, dest+=4)
if (!stbi__readval(s,packet->channel,dest))
return 0;
}
left-=count;
}
break;
}
}
}
}
return result;
}
static void *stbi__pic_load(stbi__context *s,int *px,int *py,int *comp,int req_comp, stbi__result_info *ri)
{
stbi_uc *result;
int i, x,y, internal_comp;
STBI_NOTUSED(ri);
if (!comp) comp = &internal_comp;
for (i=0; i<92; ++i)
stbi__get8(s);
x = stbi__get16be(s);
y = stbi__get16be(s);
if (stbi__at_eof(s)) return stbi__errpuc("bad file","file too short (pic header)");
if (!stbi__mad3sizes_valid(x, y, 4, 0)) return stbi__errpuc("too large", "PIC image too large to decode");
stbi__get32be(s); //skip `ratio'
stbi__get16be(s); //skip `fields'
stbi__get16be(s); //skip `pad'
// intermediate buffer is RGBA
result = (stbi_uc *) stbi__malloc_mad3(x, y, 4, 0);
memset(result, 0xff, x*y*4);
if (!stbi__pic_load_core(s,x,y,comp, result)) {
STBI_FREE(result);
result=0;
}
*px = x;
*py = y;
if (req_comp == 0) req_comp = *comp;
result=stbi__convert_format(result,4,req_comp,x,y);
return result;
}
static int stbi__pic_test(stbi__context *s)
{
int r = stbi__pic_test_core(s);
stbi__rewind(s);
return r;
}
#endif
// *************************************************************************************************
// GIF loader -- public domain by Jean-Marc Lienher -- simplified/shrunk by stb
#ifndef STBI_NO_GIF
typedef struct
{
stbi__int16 prefix;
stbi_uc first;
stbi_uc suffix;
} stbi__gif_lzw;
typedef struct
{
int w,h;
stbi_uc *out; // output buffer (always 4 components)
stbi_uc *background; // The current "background" as far as a gif is concerned
stbi_uc *history;
int flags, bgindex, ratio, transparent, eflags;
stbi_uc pal[256][4];
stbi_uc lpal[256][4];
stbi__gif_lzw codes[8192];
stbi_uc *color_table;
int parse, step;
int lflags;
int start_x, start_y;
int max_x, max_y;
int cur_x, cur_y;
int line_size;
int delay;
} stbi__gif;
static int stbi__gif_test_raw(stbi__context *s)
{
int sz;
if (stbi__get8(s) != 'G' || stbi__get8(s) != 'I' || stbi__get8(s) != 'F' || stbi__get8(s) != '8') return 0;
sz = stbi__get8(s);
if (sz != '9' && sz != '7') return 0;
if (stbi__get8(s) != 'a') return 0;
return 1;
}
static int stbi__gif_test(stbi__context *s)
{
int r = stbi__gif_test_raw(s);
stbi__rewind(s);
return r;
}
static void stbi__gif_parse_colortable(stbi__context *s, stbi_uc pal[256][4], int num_entries, int transp)
{
int i;
for (i=0; i < num_entries; ++i) {
pal[i][2] = stbi__get8(s);
pal[i][1] = stbi__get8(s);
pal[i][0] = stbi__get8(s);
pal[i][3] = transp == i ? 0 : 255;
}
}
static int stbi__gif_header(stbi__context *s, stbi__gif *g, int *comp, int is_info)
{
stbi_uc version;
if (stbi__get8(s) != 'G' || stbi__get8(s) != 'I' || stbi__get8(s) != 'F' || stbi__get8(s) != '8')
return stbi__err("not GIF", "Corrupt GIF");
version = stbi__get8(s);
if (version != '7' && version != '9') return stbi__err("not GIF", "Corrupt GIF");
if (stbi__get8(s) != 'a') return stbi__err("not GIF", "Corrupt GIF");
stbi__g_failure_reason = "";
g->w = stbi__get16le(s);
g->h = stbi__get16le(s);
g->flags = stbi__get8(s);
g->bgindex = stbi__get8(s);
g->ratio = stbi__get8(s);
g->transparent = -1;
if (comp != 0) *comp = 4; // can't actually tell whether it's 3 or 4 until we parse the comments
if (is_info) return 1;
if (g->flags & 0x80)
stbi__gif_parse_colortable(s,g->pal, 2 << (g->flags & 7), -1);
return 1;
}
static int stbi__gif_info_raw(stbi__context *s, int *x, int *y, int *comp)
{
stbi__gif* g = (stbi__gif*) stbi__malloc(sizeof(stbi__gif));
if (!stbi__gif_header(s, g, comp, 1)) {
STBI_FREE(g);
stbi__rewind( s );
return 0;
}
if (x) *x = g->w;
if (y) *y = g->h;
STBI_FREE(g);
return 1;
}
static void stbi__out_gif_code(stbi__gif *g, stbi__uint16 code)
{
stbi_uc *p, *c;
int idx;
// recurse to decode the prefixes, since the linked-list is backwards,
// and working backwards through an interleaved image would be nasty
if (g->codes[code].prefix >= 0)
stbi__out_gif_code(g, g->codes[code].prefix);
if (g->cur_y >= g->max_y) return;
idx = g->cur_x + g->cur_y;
p = &g->out[idx];
g->history[idx / 4] = 1;
c = &g->color_table[g->codes[code].suffix * 4];
if (c[3] > 128) { // don't render transparent pixels;
p[0] = c[2];
p[1] = c[1];
p[2] = c[0];
p[3] = c[3];
}
g->cur_x += 4;
if (g->cur_x >= g->max_x) {
g->cur_x = g->start_x;
g->cur_y += g->step;
while (g->cur_y >= g->max_y && g->parse > 0) {
g->step = (1 << g->parse) * g->line_size;
g->cur_y = g->start_y + (g->step >> 1);
--g->parse;
}
}
}
static stbi_uc *stbi__process_gif_raster(stbi__context *s, stbi__gif *g)
{
stbi_uc lzw_cs;
stbi__int32 len, init_code;
stbi__uint32 first;
stbi__int32 codesize, codemask, avail, oldcode, bits, valid_bits, clear;
stbi__gif_lzw *p;
lzw_cs = stbi__get8(s);
if (lzw_cs > 12) return NULL;
clear = 1 << lzw_cs;
first = 1;
codesize = lzw_cs + 1;
codemask = (1 << codesize) - 1;
bits = 0;
valid_bits = 0;
for (init_code = 0; init_code < clear; init_code++) {
g->codes[init_code].prefix = -1;
g->codes[init_code].first = (stbi_uc) init_code;
g->codes[init_code].suffix = (stbi_uc) init_code;
}
// support no starting clear code
avail = clear+2;
oldcode = -1;
len = 0;
for(;;) {
if (valid_bits < codesize) {
if (len == 0) {
len = stbi__get8(s); // start new block
if (len == 0)
return g->out;
}
--len;
bits |= (stbi__int32) stbi__get8(s) << valid_bits;
valid_bits += 8;
} else {
stbi__int32 code = bits & codemask;
bits >>= codesize;
valid_bits -= codesize;
// @OPTIMIZE: is there some way we can accelerate the non-clear path?
if (code == clear) { // clear code
codesize = lzw_cs + 1;
codemask = (1 << codesize) - 1;
avail = clear + 2;
oldcode = -1;
first = 0;
} else if (code == clear + 1) { // end of stream code
stbi__skip(s, len);
while ((len = stbi__get8(s)) > 0)
stbi__skip(s,len);
return g->out;
} else if (code <= avail) {
if (first) {
return stbi__errpuc("no clear code", "Corrupt GIF");
}
if (oldcode >= 0) {
p = &g->codes[avail++];
if (avail > 8192) {
return stbi__errpuc("too many codes", "Corrupt GIF");
}
p->prefix = (stbi__int16) oldcode;
p->first = g->codes[oldcode].first;
p->suffix = (code == avail) ? p->first : g->codes[code].first;
} else if (code == avail)
return stbi__errpuc("illegal code in raster", "Corrupt GIF");
stbi__out_gif_code(g, (stbi__uint16) code);
if ((avail & codemask) == 0 && avail <= 0x0FFF) {
codesize++;
codemask = (1 << codesize) - 1;
}
oldcode = code;
} else {
return stbi__errpuc("illegal code in raster", "Corrupt GIF");
}
}
}
}
// this function is designed to support animated gifs, although stb_image doesn't support it
// two back is the image from two frames ago, used for a very specific disposal format
static stbi_uc *stbi__gif_load_next(stbi__context *s, stbi__gif *g, int *comp, int req_comp, stbi_uc *two_back)
{
int dispose;
int first_frame;
int pi;
int pcount;
STBI_NOTUSED(req_comp);
// on first frame, any non-written pixels get the background colour (non-transparent)
first_frame = 0;
if (g->out == 0) {
if (!stbi__gif_header(s, g, comp,0)) return 0; // stbi__g_failure_reason set by stbi__gif_header
if (!stbi__mad3sizes_valid(4, g->w, g->h, 0))
return stbi__errpuc("too large", "GIF image is too large");
pcount = g->w * g->h;
g->out = (stbi_uc *) stbi__malloc(4 * pcount);
g->background = (stbi_uc *) stbi__malloc(4 * pcount);
g->history = (stbi_uc *) stbi__malloc(pcount);
if (!g->out || !g->background || !g->history)
return stbi__errpuc("outofmem", "Out of memory");
// image is treated as "transparent" at the start - ie, nothing overwrites the current background;
// background colour is only used for pixels that are not rendered first frame, after that "background"
// color refers to the color that was there the previous frame.
memset(g->out, 0x00, 4 * pcount);
memset(g->background, 0x00, 4 * pcount); // state of the background (starts transparent)
memset(g->history, 0x00, pcount); // pixels that were affected previous frame
first_frame = 1;
} else {
// second frame - how do we dispoase of the previous one?
dispose = (g->eflags & 0x1C) >> 2;
pcount = g->w * g->h;
if ((dispose == 3) && (two_back == 0)) {
dispose = 2; // if I don't have an image to revert back to, default to the old background
}
if (dispose == 3) { // use previous graphic
for (pi = 0; pi < pcount; ++pi) {
if (g->history[pi]) {
memcpy( &g->out[pi * 4], &two_back[pi * 4], 4 );
}
}
} else if (dispose == 2) {
// restore what was changed last frame to background before that frame;
for (pi = 0; pi < pcount; ++pi) {
if (g->history[pi]) {
memcpy( &g->out[pi * 4], &g->background[pi * 4], 4 );
}
}
} else {
// This is a non-disposal case eithe way, so just
// leave the pixels as is, and they will become the new background
// 1: do not dispose
// 0: not specified.
}
// background is what out is after the undoing of the previou frame;
memcpy( g->background, g->out, 4 * g->w * g->h );
}
// clear my history;
memset( g->history, 0x00, g->w * g->h ); // pixels that were affected previous frame
for (;;) {
int tag = stbi__get8(s);
switch (tag) {
case 0x2C: /* Image Descriptor */
{
stbi__int32 x, y, w, h;
stbi_uc *o;
x = stbi__get16le(s);
y = stbi__get16le(s);
w = stbi__get16le(s);
h = stbi__get16le(s);
if (((x + w) > (g->w)) || ((y + h) > (g->h)))
return stbi__errpuc("bad Image Descriptor", "Corrupt GIF");
g->line_size = g->w * 4;
g->start_x = x * 4;
g->start_y = y * g->line_size;
g->max_x = g->start_x + w * 4;
g->max_y = g->start_y + h * g->line_size;
g->cur_x = g->start_x;
g->cur_y = g->start_y;
// if the width of the specified rectangle is 0, that means
// we may not see *any* pixels or the image is malformed;
// to make sure this is caught, move the current y down to
// max_y (which is what out_gif_code checks).
if (w == 0)
g->cur_y = g->max_y;
g->lflags = stbi__get8(s);
if (g->lflags & 0x40) {
g->step = 8 * g->line_size; // first interlaced spacing
g->parse = 3;
} else {
g->step = g->line_size;
g->parse = 0;
}
if (g->lflags & 0x80) {
stbi__gif_parse_colortable(s,g->lpal, 2 << (g->lflags & 7), g->eflags & 0x01 ? g->transparent : -1);
g->color_table = (stbi_uc *) g->lpal;
} else if (g->flags & 0x80) {
g->color_table = (stbi_uc *) g->pal;
} else
return stbi__errpuc("missing color table", "Corrupt GIF");
o = stbi__process_gif_raster(s, g);
if (!o) return NULL;
// if this was the first frame,
pcount = g->w * g->h;
if (first_frame && (g->bgindex > 0)) {
// if first frame, any pixel not drawn to gets the background color
for (pi = 0; pi < pcount; ++pi) {
if (g->history[pi] == 0) {
g->pal[g->bgindex][3] = 255; // just in case it was made transparent, undo that; It will be reset next frame if need be;
memcpy( &g->out[pi * 4], &g->pal[g->bgindex], 4 );
}
}
}
return o;
}
case 0x21: // Comment Extension.
{
int len;
int ext = stbi__get8(s);
if (ext == 0xF9) { // Graphic Control Extension.
len = stbi__get8(s);
if (len == 4) {
g->eflags = stbi__get8(s);
g->delay = 10 * stbi__get16le(s); // delay - 1/100th of a second, saving as 1/1000ths.
// unset old transparent
if (g->transparent >= 0) {
g->pal[g->transparent][3] = 255;
}
if (g->eflags & 0x01) {
g->transparent = stbi__get8(s);
if (g->transparent >= 0) {
g->pal[g->transparent][3] = 0;
}
} else {
// don't need transparent
stbi__skip(s, 1);
g->transparent = -1;
}
} else {
stbi__skip(s, len);
break;
}
}
while ((len = stbi__get8(s)) != 0) {
stbi__skip(s, len);
}
break;
}
case 0x3B: // gif stream termination code
return (stbi_uc *) s; // using '1' causes warning on some compilers
default:
return stbi__errpuc("unknown code", "Corrupt GIF");
}
}
}
static void *stbi__load_gif_main(stbi__context *s, int **delays, int *x, int *y, int *z, int *comp, int req_comp)
{
if (stbi__gif_test(s)) {
int layers = 0;
stbi_uc *u = 0;
stbi_uc *out = 0;
stbi_uc *two_back = 0;
stbi__gif g;
int stride;
memset(&g, 0, sizeof(g));
if (delays) {
*delays = 0;
}
do {
u = stbi__gif_load_next(s, &g, comp, req_comp, two_back);
if (u == (stbi_uc *) s) u = 0; // end of animated gif marker
if (u) {
*x = g.w;
*y = g.h;
++layers;
stride = g.w * g.h * 4;
if (out) {
void *tmp = (stbi_uc*) STBI_REALLOC( out, layers * stride );
if (NULL == tmp) {
STBI_FREE(g.out);
STBI_FREE(g.history);
STBI_FREE(g.background);
return stbi__errpuc("outofmem", "Out of memory");
}
else
out = (stbi_uc*) tmp;
if (delays) {
*delays = (int*) STBI_REALLOC( *delays, sizeof(int) * layers );
}
} else {
out = (stbi_uc*)stbi__malloc( layers * stride );
if (delays) {
*delays = (int*) stbi__malloc( layers * sizeof(int) );
}
}
memcpy( out + ((layers - 1) * stride), u, stride );
if (layers >= 2) {
two_back = out - 2 * stride;
}
if (delays) {
(*delays)[layers - 1U] = g.delay;
}
}
} while (u != 0);
// free temp buffer;
STBI_FREE(g.out);
STBI_FREE(g.history);
STBI_FREE(g.background);
// do the final conversion after loading everything;
if (req_comp && req_comp != 4)
out = stbi__convert_format(out, 4, req_comp, layers * g.w, g.h);
*z = layers;
return out;
} else {
return stbi__errpuc("not GIF", "Image was not as a gif type.");
}
}
static void *stbi__gif_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri)
{
stbi_uc *u = 0;
stbi__gif g;
memset(&g, 0, sizeof(g));
STBI_NOTUSED(ri);
u = stbi__gif_load_next(s, &g, comp, req_comp, 0);
if (u == (stbi_uc *) s) u = 0; // end of animated gif marker
if (u) {
*x = g.w;
*y = g.h;
// moved conversion to after successful load so that the same
// can be done for multiple frames.
if (req_comp && req_comp != 4)
u = stbi__convert_format(u, 4, req_comp, g.w, g.h);
} else if (g.out) {
// if there was an error and we allocated an image buffer, free it!
STBI_FREE(g.out);
}
// free buffers needed for multiple frame loading;
STBI_FREE(g.history);
STBI_FREE(g.background);
return u;
}
static int stbi__gif_info(stbi__context *s, int *x, int *y, int *comp)
{
return stbi__gif_info_raw(s,x,y,comp);
}
#endif
// *************************************************************************************************
// Radiance RGBE HDR loader
// originally by Nicolas Schulz
#ifndef STBI_NO_HDR
static int stbi__hdr_test_core(stbi__context *s, const char *signature)
{
int i;
for (i=0; signature[i]; ++i)
if (stbi__get8(s) != signature[i])
return 0;
stbi__rewind(s);
return 1;
}
static int stbi__hdr_test(stbi__context* s)
{
int r = stbi__hdr_test_core(s, "#?RADIANCE\n");
stbi__rewind(s);
if(!r) {
r = stbi__hdr_test_core(s, "#?RGBE\n");
stbi__rewind(s);
}
return r;
}
#define STBI__HDR_BUFLEN 1024
static char *stbi__hdr_gettoken(stbi__context *z, char *buffer)
{
int len=0;
char c = '\0';
c = (char) stbi__get8(z);
while (!stbi__at_eof(z) && c != '\n') {
buffer[len++] = c;
if (len == STBI__HDR_BUFLEN-1) {
// flush to end of line
while (!stbi__at_eof(z) && stbi__get8(z) != '\n')
;
break;
}
c = (char) stbi__get8(z);
}
buffer[len] = 0;
return buffer;
}
static void stbi__hdr_convert(float *output, stbi_uc *input, int req_comp)
{
if ( input[3] != 0 ) {
float f1;
// Exponent
f1 = (float) ldexp(1.0f, input[3] - (int)(128 + 8));
if (req_comp <= 2)
output[0] = (input[0] + input[1] + input[2]) * f1 / 3;
else {
output[0] = input[0] * f1;
output[1] = input[1] * f1;
output[2] = input[2] * f1;
}
if (req_comp == 2) output[1] = 1;
if (req_comp == 4) output[3] = 1;
} else {
switch (req_comp) {
case 4: output[3] = 1; /* fallthrough */
case 3: output[0] = output[1] = output[2] = 0;
break;
case 2: output[1] = 1; /* fallthrough */
case 1: output[0] = 0;
break;
}
}
}
static float *stbi__hdr_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri)
{
char buffer[STBI__HDR_BUFLEN];
char *token;
int valid = 0;
int width, height;
stbi_uc *scanline;
float *hdr_data;
int len;
unsigned char count, value;
int i, j, k, c1,c2, z;
const char *headerToken;
STBI_NOTUSED(ri);
// Check identifier
headerToken = stbi__hdr_gettoken(s,buffer);
if (strcmp(headerToken, "#?RADIANCE") != 0 && strcmp(headerToken, "#?RGBE") != 0)
return stbi__errpf("not HDR", "Corrupt HDR image");
// Parse header
for(;;) {
token = stbi__hdr_gettoken(s,buffer);
if (token[0] == 0) break;
if (strcmp(token, "FORMAT=32-bit_rle_rgbe") == 0) valid = 1;
}
if (!valid) return stbi__errpf("unsupported format", "Unsupported HDR format");
// Parse width and height
// can't use sscanf() if we're not using stdio!
token = stbi__hdr_gettoken(s,buffer);
if (strncmp(token, "-Y ", 3)) return stbi__errpf("unsupported data layout", "Unsupported HDR format");
token += 3;
height = (int) strtol(token, &token, 10);
while (*token == ' ') ++token;
if (strncmp(token, "+X ", 3)) return stbi__errpf("unsupported data layout", "Unsupported HDR format");
token += 3;
width = (int) strtol(token, NULL, 10);
*x = width;
*y = height;
if (comp) *comp = 3;
if (req_comp == 0) req_comp = 3;
if (!stbi__mad4sizes_valid(width, height, req_comp, sizeof(float), 0))
return stbi__errpf("too large", "HDR image is too large");
// Read data
hdr_data = (float *) stbi__malloc_mad4(width, height, req_comp, sizeof(float), 0);
if (!hdr_data)
return stbi__errpf("outofmem", "Out of memory");
// Load image data
// image data is stored as some number of sca
if ( width < 8 || width >= 32768) {
// Read flat data
for (j=0; j < height; ++j) {
for (i=0; i < width; ++i) {
stbi_uc rgbe[4];
main_decode_loop:
stbi__getn(s, rgbe, 4);
stbi__hdr_convert(hdr_data + j * width * req_comp + i * req_comp, rgbe, req_comp);
}
}
} else {
// Read RLE-encoded data
scanline = NULL;
for (j = 0; j < height; ++j) {
c1 = stbi__get8(s);
c2 = stbi__get8(s);
len = stbi__get8(s);
if (c1 != 2 || c2 != 2 || (len & 0x80)) {
// not run-length encoded, so we have to actually use THIS data as a decoded
// pixel (note this can't be a valid pixel--one of RGB must be >= 128)
stbi_uc rgbe[4];
rgbe[0] = (stbi_uc) c1;
rgbe[1] = (stbi_uc) c2;
rgbe[2] = (stbi_uc) len;
rgbe[3] = (stbi_uc) stbi__get8(s);
stbi__hdr_convert(hdr_data, rgbe, req_comp);
i = 1;
j = 0;
STBI_FREE(scanline);
goto main_decode_loop; // yes, this makes no sense
}
len <<= 8;
len |= stbi__get8(s);
if (len != width) { STBI_FREE(hdr_data); STBI_FREE(scanline); return stbi__errpf("invalid decoded scanline length", "corrupt HDR"); }
if (scanline == NULL) {
scanline = (stbi_uc *) stbi__malloc_mad2(width, 4, 0);
if (!scanline) {
STBI_FREE(hdr_data);
return stbi__errpf("outofmem", "Out of memory");
}
}
for (k = 0; k < 4; ++k) {
int nleft;
i = 0;
while ((nleft = width - i) > 0) {
count = stbi__get8(s);
if (count > 128) {
// Run
value = stbi__get8(s);
count -= 128;
if (count > nleft) { STBI_FREE(hdr_data); STBI_FREE(scanline); return stbi__errpf("corrupt", "bad RLE data in HDR"); }
for (z = 0; z < count; ++z)
scanline[i++ * 4 + k] = value;
} else {
// Dump
if (count > nleft) { STBI_FREE(hdr_data); STBI_FREE(scanline); return stbi__errpf("corrupt", "bad RLE data in HDR"); }
for (z = 0; z < count; ++z)
scanline[i++ * 4 + k] = stbi__get8(s);
}
}
}
for (i=0; i < width; ++i)
stbi__hdr_convert(hdr_data+(j*width + i)*req_comp, scanline + i*4, req_comp);
}
if (scanline)
STBI_FREE(scanline);
}
return hdr_data;
}
static int stbi__hdr_info(stbi__context *s, int *x, int *y, int *comp)
{
char buffer[STBI__HDR_BUFLEN];
char *token;
int valid = 0;
int dummy;
if (!x) x = &dummy;
if (!y) y = &dummy;
if (!comp) comp = &dummy;
if (stbi__hdr_test(s) == 0) {
stbi__rewind( s );
return 0;
}
for(;;) {
token = stbi__hdr_gettoken(s,buffer);
if (token[0] == 0) break;
if (strcmp(token, "FORMAT=32-bit_rle_rgbe") == 0) valid = 1;
}
if (!valid) {
stbi__rewind( s );
return 0;
}
token = stbi__hdr_gettoken(s,buffer);
if (strncmp(token, "-Y ", 3)) {
stbi__rewind( s );
return 0;
}
token += 3;
*y = (int) strtol(token, &token, 10);
while (*token == ' ') ++token;
if (strncmp(token, "+X ", 3)) {
stbi__rewind( s );
return 0;
}
token += 3;
*x = (int) strtol(token, NULL, 10);
*comp = 3;
return 1;
}
#endif // STBI_NO_HDR
#ifndef STBI_NO_BMP
static int stbi__bmp_info(stbi__context *s, int *x, int *y, int *comp)
{
void *p;
stbi__bmp_data info;
info.all_a = 255;
p = stbi__bmp_parse_header(s, &info);
stbi__rewind( s );
if (p == NULL)
return 0;
if (x) *x = s->img_x;
if (y) *y = s->img_y;
if (comp) {
if (info.bpp == 24 && info.ma == 0xff000000)
*comp = 3;
else
*comp = info.ma ? 4 : 3;
}
return 1;
}
#endif
#ifndef STBI_NO_PSD
static int stbi__psd_info(stbi__context *s, int *x, int *y, int *comp)
{
int channelCount, dummy, depth;
if (!x) x = &dummy;
if (!y) y = &dummy;
if (!comp) comp = &dummy;
if (stbi__get32be(s) != 0x38425053) {
stbi__rewind( s );
return 0;
}
if (stbi__get16be(s) != 1) {
stbi__rewind( s );
return 0;
}
stbi__skip(s, 6);
channelCount = stbi__get16be(s);
if (channelCount < 0 || channelCount > 16) {
stbi__rewind( s );
return 0;
}
*y = stbi__get32be(s);
*x = stbi__get32be(s);
depth = stbi__get16be(s);
if (depth != 8 && depth != 16) {
stbi__rewind( s );
return 0;
}
if (stbi__get16be(s) != 3) {
stbi__rewind( s );
return 0;
}
*comp = 4;
return 1;
}
static int stbi__psd_is16(stbi__context *s)
{
int channelCount, depth;
if (stbi__get32be(s) != 0x38425053) {
stbi__rewind( s );
return 0;
}
if (stbi__get16be(s) != 1) {
stbi__rewind( s );
return 0;
}
stbi__skip(s, 6);
channelCount = stbi__get16be(s);
if (channelCount < 0 || channelCount > 16) {
stbi__rewind( s );
return 0;
}
(void) stbi__get32be(s);
(void) stbi__get32be(s);
depth = stbi__get16be(s);
if (depth != 16) {
stbi__rewind( s );
return 0;
}
return 1;
}
#endif
#ifndef STBI_NO_PIC
static int stbi__pic_info(stbi__context *s, int *x, int *y, int *comp)
{
int act_comp=0,num_packets=0,chained,dummy;
stbi__pic_packet packets[10];
if (!x) x = &dummy;
if (!y) y = &dummy;
if (!comp) comp = &dummy;
if (!stbi__pic_is4(s,"\x53\x80\xF6\x34")) {
stbi__rewind(s);
return 0;
}
stbi__skip(s, 88);
*x = stbi__get16be(s);
*y = stbi__get16be(s);
if (stbi__at_eof(s)) {
stbi__rewind( s);
return 0;
}
if ( (*x) != 0 && (1 << 28) / (*x) < (*y)) {
stbi__rewind( s );
return 0;
}
stbi__skip(s, 8);
do {
stbi__pic_packet *packet;
if (num_packets==sizeof(packets)/sizeof(packets[0]))
return 0;
packet = &packets[num_packets++];
chained = stbi__get8(s);
packet->size = stbi__get8(s);
packet->type = stbi__get8(s);
packet->channel = stbi__get8(s);
act_comp |= packet->channel;
if (stbi__at_eof(s)) {
stbi__rewind( s );
return 0;
}
if (packet->size != 8) {
stbi__rewind( s );
return 0;
}
} while (chained);
*comp = (act_comp & 0x10 ? 4 : 3);
return 1;
}
#endif
// *************************************************************************************************
// Portable Gray Map and Portable Pixel Map loader
// by Ken Miller
//
// PGM: http://netpbm.sourceforge.net/doc/pgm.html
// PPM: http://netpbm.sourceforge.net/doc/ppm.html
//
// Known limitations:
// Does not support comments in the header section
// Does not support ASCII image data (formats P2 and P3)
// Does not support 16-bit-per-channel
#ifndef STBI_NO_PNM
static int stbi__pnm_test(stbi__context *s)
{
char p, t;
p = (char) stbi__get8(s);
t = (char) stbi__get8(s);
if (p != 'P' || (t != '5' && t != '6')) {
stbi__rewind( s );
return 0;
}
return 1;
}
static void *stbi__pnm_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri)
{
stbi_uc *out;
STBI_NOTUSED(ri);
if (!stbi__pnm_info(s, (int *)&s->img_x, (int *)&s->img_y, (int *)&s->img_n))
return 0;
*x = s->img_x;
*y = s->img_y;
if (comp) *comp = s->img_n;
if (!stbi__mad3sizes_valid(s->img_n, s->img_x, s->img_y, 0))
return stbi__errpuc("too large", "PNM too large");
out = (stbi_uc *) stbi__malloc_mad3(s->img_n, s->img_x, s->img_y, 0);
if (!out) return stbi__errpuc("outofmem", "Out of memory");
stbi__getn(s, out, s->img_n * s->img_x * s->img_y);
if (req_comp && req_comp != s->img_n) {
out = stbi__convert_format(out, s->img_n, req_comp, s->img_x, s->img_y);
if (out == NULL) return out; // stbi__convert_format frees input on failure
}
return out;
}
static int stbi__pnm_isspace(char c)
{
return c == ' ' || c == '\t' || c == '\n' || c == '\v' || c == '\f' || c == '\r';
}
static void stbi__pnm_skip_whitespace(stbi__context *s, char *c)
{
for (;;) {
while (!stbi__at_eof(s) && stbi__pnm_isspace(*c))
*c = (char) stbi__get8(s);
if (stbi__at_eof(s) || *c != '#')
break;
while (!stbi__at_eof(s) && *c != '\n' && *c != '\r' )
*c = (char) stbi__get8(s);
}
}
static int stbi__pnm_isdigit(char c)
{
return c >= '0' && c <= '9';
}
static int stbi__pnm_getinteger(stbi__context *s, char *c)
{
int value = 0;
while (!stbi__at_eof(s) && stbi__pnm_isdigit(*c)) {
value = value*10 + (*c - '0');
*c = (char) stbi__get8(s);
}
return value;
}
static int stbi__pnm_info(stbi__context *s, int *x, int *y, int *comp)
{
int maxv, dummy;
char c, p, t;
if (!x) x = &dummy;
if (!y) y = &dummy;
if (!comp) comp = &dummy;
stbi__rewind(s);
// Get identifier
p = (char) stbi__get8(s);
t = (char) stbi__get8(s);
if (p != 'P' || (t != '5' && t != '6')) {
stbi__rewind(s);
return 0;
}
*comp = (t == '6') ? 3 : 1; // '5' is 1-component .pgm; '6' is 3-component .ppm
c = (char) stbi__get8(s);
stbi__pnm_skip_whitespace(s, &c);
*x = stbi__pnm_getinteger(s, &c); // read width
stbi__pnm_skip_whitespace(s, &c);
*y = stbi__pnm_getinteger(s, &c); // read height
stbi__pnm_skip_whitespace(s, &c);
maxv = stbi__pnm_getinteger(s, &c); // read max value
if (maxv > 255)
return stbi__err("max value > 255", "PPM image not 8-bit");
else
return 1;
}
#endif
static int stbi__info_main(stbi__context *s, int *x, int *y, int *comp)
{
#ifndef STBI_NO_JPEG
if (stbi__jpeg_info(s, x, y, comp)) return 1;
#endif
#ifndef STBI_NO_PNG
if (stbi__png_info(s, x, y, comp)) return 1;
#endif
#ifndef STBI_NO_GIF
if (stbi__gif_info(s, x, y, comp)) return 1;
#endif
#ifndef STBI_NO_BMP
if (stbi__bmp_info(s, x, y, comp)) return 1;
#endif
#ifndef STBI_NO_PSD
if (stbi__psd_info(s, x, y, comp)) return 1;
#endif
#ifndef STBI_NO_PIC
if (stbi__pic_info(s, x, y, comp)) return 1;
#endif
#ifndef STBI_NO_PNM
if (stbi__pnm_info(s, x, y, comp)) return 1;
#endif
#ifndef STBI_NO_HDR
if (stbi__hdr_info(s, x, y, comp)) return 1;
#endif
// test tga last because it's a crappy test!
#ifndef STBI_NO_TGA
if (stbi__tga_info(s, x, y, comp))
return 1;
#endif
return stbi__err("unknown image type", "Image not of any known type, or corrupt");
}
static int stbi__is_16_main(stbi__context *s)
{
#ifndef STBI_NO_PNG
if (stbi__png_is16(s)) return 1;
#endif
#ifndef STBI_NO_PSD
if (stbi__psd_is16(s)) return 1;
#endif
return 0;
}
#ifndef STBI_NO_STDIO
STBIDEF int stbi_info(char const *filename, int *x, int *y, int *comp)
{
FILE *f = stbi__fopen(filename, "rb");
int result;
if (!f) return stbi__err("can't fopen", "Unable to open file");
result = stbi_info_from_file(f, x, y, comp);
fclose(f);
return result;
}
STBIDEF int stbi_info_from_file(FILE *f, int *x, int *y, int *comp)
{
int r;
stbi__context s;
long pos = ftell(f);
stbi__start_file(&s, f);
r = stbi__info_main(&s,x,y,comp);
fseek(f,pos,SEEK_SET);
return r;
}
STBIDEF int stbi_is_16_bit(char const *filename)
{
FILE *f = stbi__fopen(filename, "rb");
int result;
if (!f) return stbi__err("can't fopen", "Unable to open file");
result = stbi_is_16_bit_from_file(f);
fclose(f);
return result;
}
STBIDEF int stbi_is_16_bit_from_file(FILE *f)
{
int r;
stbi__context s;
long pos = ftell(f);
stbi__start_file(&s, f);
r = stbi__is_16_main(&s);
fseek(f,pos,SEEK_SET);
return r;
}
#endif // !STBI_NO_STDIO
STBIDEF int stbi_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp)
{
stbi__context s;
stbi__start_mem(&s,buffer,len);
return stbi__info_main(&s,x,y,comp);
}
STBIDEF int stbi_info_from_callbacks(stbi_io_callbacks const *c, void *user, int *x, int *y, int *comp)
{
stbi__context s;
stbi__start_callbacks(&s, (stbi_io_callbacks *) c, user);
return stbi__info_main(&s,x,y,comp);
}
STBIDEF int stbi_is_16_bit_from_memory(stbi_uc const *buffer, int len)
{
stbi__context s;
stbi__start_mem(&s,buffer,len);
return stbi__is_16_main(&s);
}
STBIDEF int stbi_is_16_bit_from_callbacks(stbi_io_callbacks const *c, void *user)
{
stbi__context s;
stbi__start_callbacks(&s, (stbi_io_callbacks *) c, user);
return stbi__is_16_main(&s);
}
#endif // STB_IMAGE_IMPLEMENTATION
/*
revision history:
2.20 (2019-02-07) support utf8 filenames in Windows; fix warnings and platform ifdefs
2.19 (2018-02-11) fix warning
2.18 (2018-01-30) fix warnings
2.17 (2018-01-29) change sbti__shiftsigned to avoid clang -O2 bug
1-bit BMP
*_is_16_bit api
avoid warnings
2.16 (2017-07-23) all functions have 16-bit variants;
STBI_NO_STDIO works again;
compilation fixes;
fix rounding in unpremultiply;
optimize vertical flip;
disable raw_len validation;
documentation fixes
2.15 (2017-03-18) fix png-1,2,4 bug; now all Imagenet JPGs decode;
warning fixes; disable run-time SSE detection on gcc;
uniform handling of optional "return" values;
thread-safe initialization of zlib tables
2.14 (2017-03-03) remove deprecated STBI_JPEG_OLD; fixes for Imagenet JPGs
2.13 (2016-11-29) add 16-bit API, only supported for PNG right now
2.12 (2016-04-02) fix typo in 2.11 PSD fix that caused crashes
2.11 (2016-04-02) allocate large structures on the stack
remove white matting for transparent PSD
fix reported channel count for PNG & BMP
re-enable SSE2 in non-gcc 64-bit
support RGB-formatted JPEG
read 16-bit PNGs (only as 8-bit)
2.10 (2016-01-22) avoid warning introduced in 2.09 by STBI_REALLOC_SIZED
2.09 (2016-01-16) allow comments in PNM files
16-bit-per-pixel TGA (not bit-per-component)
info() for TGA could break due to .hdr handling
info() for BMP to shares code instead of sloppy parse
can use STBI_REALLOC_SIZED if allocator doesn't support realloc
code cleanup
2.08 (2015-09-13) fix to 2.07 cleanup, reading RGB PSD as RGBA
2.07 (2015-09-13) fix compiler warnings
partial animated GIF support
limited 16-bpc PSD support
#ifdef unused functions
bug with < 92 byte PIC,PNM,HDR,TGA
2.06 (2015-04-19) fix bug where PSD returns wrong '*comp' value
2.05 (2015-04-19) fix bug in progressive JPEG handling, fix warning
2.04 (2015-04-15) try to re-enable SIMD on MinGW 64-bit
2.03 (2015-04-12) extra corruption checking (mmozeiko)
stbi_set_flip_vertically_on_load (nguillemot)
fix NEON support; fix mingw support
2.02 (2015-01-19) fix incorrect assert, fix warning
2.01 (2015-01-17) fix various warnings; suppress SIMD on gcc 32-bit without -msse2
2.00b (2014-12-25) fix STBI_MALLOC in progressive JPEG
2.00 (2014-12-25) optimize JPG, including x86 SSE2 & NEON SIMD (ryg)
progressive JPEG (stb)
PGM/PPM support (Ken Miller)
STBI_MALLOC,STBI_REALLOC,STBI_FREE
GIF bugfix -- seemingly never worked
STBI_NO_*, STBI_ONLY_*
1.48 (2014-12-14) fix incorrectly-named assert()
1.47 (2014-12-14) 1/2/4-bit PNG support, both direct and paletted (Omar Cornut & stb)
optimize PNG (ryg)
fix bug in interlaced PNG with user-specified channel count (stb)
1.46 (2014-08-26)
fix broken tRNS chunk (colorkey-style transparency) in non-paletted PNG
1.45 (2014-08-16)
fix MSVC-ARM internal compiler error by wrapping malloc
1.44 (2014-08-07)
various warning fixes from Ronny Chevalier
1.43 (2014-07-15)
fix MSVC-only compiler problem in code changed in 1.42
1.42 (2014-07-09)
don't define _CRT_SECURE_NO_WARNINGS (affects user code)
fixes to stbi__cleanup_jpeg path
added STBI_ASSERT to avoid requiring assert.h
1.41 (2014-06-25)
fix search&replace from 1.36 that messed up comments/error messages
1.40 (2014-06-22)
fix gcc struct-initialization warning
1.39 (2014-06-15)
fix to TGA optimization when req_comp != number of components in TGA;
fix to GIF loading because BMP wasn't rewinding (whoops, no GIFs in my test suite)
add support for BMP version 5 (more ignored fields)
1.38 (2014-06-06)
suppress MSVC warnings on integer casts truncating values
fix accidental rename of 'skip' field of I/O
1.37 (2014-06-04)
remove duplicate typedef
1.36 (2014-06-03)
convert to header file single-file library
if de-iphone isn't set, load iphone images color-swapped instead of returning NULL
1.35 (2014-05-27)
various warnings
fix broken STBI_SIMD path
fix bug where stbi_load_from_file no longer left file pointer in correct place
fix broken non-easy path for 32-bit BMP (possibly never used)
TGA optimization by Arseny Kapoulkine
1.34 (unknown)
use STBI_NOTUSED in stbi__resample_row_generic(), fix one more leak in tga failure case
1.33 (2011-07-14)
make stbi_is_hdr work in STBI_NO_HDR (as specified), minor compiler-friendly improvements
1.32 (2011-07-13)
support for "info" function for all supported filetypes (SpartanJ)
1.31 (2011-06-20)
a few more leak fixes, bug in PNG handling (SpartanJ)
1.30 (2011-06-11)
added ability to load files via callbacks to accomidate custom input streams (Ben Wenger)
removed deprecated format-specific test/load functions
removed support for installable file formats (stbi_loader) -- would have been broken for IO callbacks anyway
error cases in bmp and tga give messages and don't leak (Raymond Barbiero, grisha)
fix inefficiency in decoding 32-bit BMP (David Woo)
1.29 (2010-08-16)
various warning fixes from Aurelien Pocheville
1.28 (2010-08-01)
fix bug in GIF palette transparency (SpartanJ)
1.27 (2010-08-01)
cast-to-stbi_uc to fix warnings
1.26 (2010-07-24)
fix bug in file buffering for PNG reported by SpartanJ
1.25 (2010-07-17)
refix trans_data warning (Won Chun)
1.24 (2010-07-12)
perf improvements reading from files on platforms with lock-heavy fgetc()
minor perf improvements for jpeg
deprecated type-specific functions so we'll get feedback if they're needed
attempt to fix trans_data warning (Won Chun)
1.23 fixed bug in iPhone support
1.22 (2010-07-10)
removed image *writing* support
stbi_info support from Jetro Lauha
GIF support from Jean-Marc Lienher
iPhone PNG-extensions from James Brown
warning-fixes from Nicolas Schulz and Janez Zemva (i.stbi__err. Janez (U+017D)emva)
1.21 fix use of 'stbi_uc' in header (reported by jon blow)
1.20 added support for Softimage PIC, by Tom Seddon
1.19 bug in interlaced PNG corruption check (found by ryg)
1.18 (2008-08-02)
fix a threading bug (local mutable static)
1.17 support interlaced PNG
1.16 major bugfix - stbi__convert_format converted one too many pixels
1.15 initialize some fields for thread safety
1.14 fix threadsafe conversion bug
header-file-only version (#define STBI_HEADER_FILE_ONLY before including)
1.13 threadsafe
1.12 const qualifiers in the API
1.11 Support installable IDCT, colorspace conversion routines
1.10 Fixes for 64-bit (don't use "unsigned long")
optimized upsampling by Fabian "ryg" Giesen
1.09 Fix format-conversion for PSD code (bad global variables!)
1.08 Thatcher Ulrich's PSD code integrated by Nicolas Schulz
1.07 attempt to fix C++ warning/errors again
1.06 attempt to fix C++ warning/errors again
1.05 fix TGA loading to return correct *comp and use good luminance calc
1.04 default float alpha is 1, not 255; use 'void *' for stbi_image_free
1.03 bugfixes to STBI_NO_STDIO, STBI_NO_HDR
1.02 support for (subset of) HDR files, float interface for preferred access to them
1.01 fix bug: possible bug in handling right-side up bmps... not sure
fix bug: the stbi__bmp_load() and stbi__tga_load() functions didn't work at all
1.00 interface to zlib that skips zlib header
0.99 correct handling of alpha in palette
0.98 TGA loader by lonesock; dynamically add loaders (untested)
0.97 jpeg errors on too large a file; also catch another malloc failure
0.96 fix detection of invalid v value - particleman@mollyrocket forum
0.95 during header scan, seek to markers in case of padding
0.94 STBI_NO_STDIO to disable stdio usage; rename all #defines the same
0.93 handle jpegtran output; verbose errors
0.92 read 4,8,16,24,32-bit BMP files of several formats
0.91 output 24-bit Windows 3.0 BMP files
0.90 fix a few more warnings; bump version number to approach 1.0
0.61 bugfixes due to Marc LeBlanc, Christopher Lloyd
0.60 fix compiling as c++
0.59 fix warnings: merge Dave Moore's -Wall fixes
0.58 fix bug: zlib uncompressed mode len/nlen was wrong endian
0.57 fix bug: jpg last huffman symbol before marker was >9 bits but less than 16 available
0.56 fix bug: zlib uncompressed mode len vs. nlen
0.55 fix bug: restart_interval not initialized to 0
0.54 allow NULL for 'int *comp'
0.53 fix bug in png 3->4; speedup png decoding
0.52 png handles req_comp=3,4 directly; minor cleanup; jpeg comments
0.51 obey req_comp requests, 1-component jpegs return as 1-component,
on 'test' only check type, not whether we support this variant
0.50 (2006-11-19)
first released version
*/
/*
------------------------------------------------------------------------------
This software is available under 2 licenses -- choose whichever you prefer.
------------------------------------------------------------------------------
ALTERNATIVE A - MIT License
Copyright (c) 2017 Sean Barrett
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
------------------------------------------------------------------------------
ALTERNATIVE B - Public Domain (www.unlicense.org)
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
software, either in source code form or as a compiled binary, for any purpose,
commercial or non-commercial, and by any means.
In jurisdictions that recognize copyright laws, the author or authors of this
software dedicate any and all copyright interest in the software to the public
domain. We make this dedication for the benefit of the public at large and to
the detriment of our heirs and successors. We intend this dedication to be an
overt act of relinquishment in perpetuity of all present and future rights to
this software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
------------------------------------------------------------------------------
*/
| h |
oneAPI-samples | data/projects/oneAPI-samples/common/stb/stb_image_write.h | /* stb_image_write - v1.14 - public domain - http://nothings.org/stb
writes out PNG/BMP/TGA/JPEG/HDR images to C stdio - Sean Barrett 2010-2015
no warranty implied; use at your own risk
Before #including,
#define STB_IMAGE_WRITE_IMPLEMENTATION
in the file that you want to have the implementation.
Will probably not work correctly with strict-aliasing optimizations.
ABOUT:
This header file is a library for writing images to C stdio or a callback.
The PNG output is not optimal; it is 20-50% larger than the file
written by a decent optimizing implementation; though providing a custom
zlib compress function (see STBIW_ZLIB_COMPRESS) can mitigate that.
This library is designed for source code compactness and simplicity,
not optimal image file size or run-time performance.
BUILDING:
You can #define STBIW_ASSERT(x) before the #include to avoid using assert.h.
You can #define STBIW_MALLOC(), STBIW_REALLOC(), and STBIW_FREE() to replace
malloc,realloc,free.
You can #define STBIW_MEMMOVE() to replace memmove()
You can #define STBIW_ZLIB_COMPRESS to use a custom zlib-style compress function
for PNG compression (instead of the builtin one), it must have the following signature:
unsigned char * my_compress(unsigned char *data, int data_len, int *out_len, int quality);
The returned data will be freed with STBIW_FREE() (free() by default),
so it must be heap allocated with STBIW_MALLOC() (malloc() by default),
UNICODE:
If compiling for Windows and you wish to use Unicode filenames, compile
with
#define STBIW_WINDOWS_UTF8
and pass utf8-encoded filenames. Call stbiw_convert_wchar_to_utf8 to convert
Windows wchar_t filenames to utf8.
USAGE:
There are five functions, one for each image file format:
int stbi_write_png(char const *filename, int w, int h, int comp, const void *data, int stride_in_bytes);
int stbi_write_bmp(char const *filename, int w, int h, int comp, const void *data);
int stbi_write_tga(char const *filename, int w, int h, int comp, const void *data);
int stbi_write_jpg(char const *filename, int w, int h, int comp, const void *data, int quality);
int stbi_write_hdr(char const *filename, int w, int h, int comp, const float *data);
void stbi_flip_vertically_on_write(int flag); // flag is non-zero to flip data vertically
There are also five equivalent functions that use an arbitrary write function. You are
expected to open/close your file-equivalent before and after calling these:
int stbi_write_png_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data, int stride_in_bytes);
int stbi_write_bmp_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data);
int stbi_write_tga_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data);
int stbi_write_hdr_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const float *data);
int stbi_write_jpg_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data, int quality);
where the callback is:
void stbi_write_func(void *context, void *data, int size);
You can configure it with these global variables:
int stbi_write_tga_with_rle; // defaults to true; set to 0 to disable RLE
int stbi_write_png_compression_level; // defaults to 8; set to higher for more compression
int stbi_write_force_png_filter; // defaults to -1; set to 0..5 to force a filter mode
You can define STBI_WRITE_NO_STDIO to disable the file variant of these
functions, so the library will not use stdio.h at all. However, this will
also disable HDR writing, because it requires stdio for formatted output.
Each function returns 0 on failure and non-0 on success.
The functions create an image file defined by the parameters. The image
is a rectangle of pixels stored from left-to-right, top-to-bottom.
Each pixel contains 'comp' channels of data stored interleaved with 8-bits
per channel, in the following order: 1=Y, 2=YA, 3=RGB, 4=RGBA. (Y is
monochrome color.) The rectangle is 'w' pixels wide and 'h' pixels tall.
The *data pointer points to the first byte of the top-left-most pixel.
For PNG, "stride_in_bytes" is the distance in bytes from the first byte of
a row of pixels to the first byte of the next row of pixels.
PNG creates output files with the same number of components as the input.
The BMP format expands Y to RGB in the file format and does not
output alpha.
PNG supports writing rectangles of data even when the bytes storing rows of
data are not consecutive in memory (e.g. sub-rectangles of a larger image),
by supplying the stride between the beginning of adjacent rows. The other
formats do not. (Thus you cannot write a native-format BMP through the BMP
writer, both because it is in BGR order and because it may have padding
at the end of the line.)
PNG allows you to set the deflate compression level by setting the global
variable 'stbi_write_png_compression_level' (it defaults to 8).
HDR expects linear float data. Since the format is always 32-bit rgb(e)
data, alpha (if provided) is discarded, and for monochrome data it is
replicated across all three channels.
TGA supports RLE or non-RLE compressed data. To use non-RLE-compressed
data, set the global variable 'stbi_write_tga_with_rle' to 0.
JPEG does ignore alpha channels in input data; quality is between 1 and 100.
Higher quality looks better but results in a bigger image.
JPEG baseline (no JPEG progressive).
CREDITS:
Sean Barrett - PNG/BMP/TGA
Baldur Karlsson - HDR
Jean-Sebastien Guay - TGA monochrome
Tim Kelsey - misc enhancements
Alan Hickman - TGA RLE
Emmanuel Julien - initial file IO callback implementation
Jon Olick - original jo_jpeg.cpp code
Daniel Gibson - integrate JPEG, allow external zlib
Aarni Koskela - allow choosing PNG filter
bugfixes:
github:Chribba
Guillaume Chereau
github:jry2
github:romigrou
Sergio Gonzalez
Jonas Karlsson
Filip Wasil
Thatcher Ulrich
github:poppolopoppo
Patrick Boettcher
github:xeekworx
Cap Petschulat
Simon Rodriguez
Ivan Tikhonov
github:ignotion
Adam Schackart
LICENSE
See end of file for license information.
*/
#ifndef INCLUDE_STB_IMAGE_WRITE_H
#define INCLUDE_STB_IMAGE_WRITE_H
#include <stdlib.h>
// if STB_IMAGE_WRITE_STATIC causes problems, try defining STBIWDEF to 'inline' or 'static inline'
#ifndef STBIWDEF
#ifdef STB_IMAGE_WRITE_STATIC
#define STBIWDEF static
#else
#ifdef __cplusplus
#define STBIWDEF extern "C"
#else
#define STBIWDEF extern
#endif
#endif
#endif
#ifndef STB_IMAGE_WRITE_STATIC // C++ forbids static forward declarations
extern int stbi_write_tga_with_rle;
extern int stbi_write_png_compression_level;
extern int stbi_write_force_png_filter;
#endif
#ifndef STBI_WRITE_NO_STDIO
STBIWDEF int stbi_write_png(char const *filename, int w, int h, int comp, const void *data, int stride_in_bytes);
STBIWDEF int stbi_write_bmp(char const *filename, int w, int h, int comp, const void *data);
STBIWDEF int stbi_write_tga(char const *filename, int w, int h, int comp, const void *data);
STBIWDEF int stbi_write_hdr(char const *filename, int w, int h, int comp, const float *data);
STBIWDEF int stbi_write_jpg(char const *filename, int x, int y, int comp, const void *data, int quality);
#ifdef STBI_WINDOWS_UTF8
STBIWDEF int stbiw_convert_wchar_to_utf8(char *buffer, size_t bufferlen, const wchar_t* input);
#endif
#endif
typedef void stbi_write_func(void *context, void *data, int size);
STBIWDEF int stbi_write_png_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data, int stride_in_bytes);
STBIWDEF int stbi_write_bmp_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data);
STBIWDEF int stbi_write_tga_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data);
STBIWDEF int stbi_write_hdr_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const float *data);
STBIWDEF int stbi_write_jpg_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data, int quality);
STBIWDEF void stbi_flip_vertically_on_write(int flip_boolean);
#endif//INCLUDE_STB_IMAGE_WRITE_H
#ifdef STB_IMAGE_WRITE_IMPLEMENTATION
#ifdef _WIN32
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#endif
#ifndef _CRT_NONSTDC_NO_DEPRECATE
#define _CRT_NONSTDC_NO_DEPRECATE
#endif
#endif
#ifndef STBI_WRITE_NO_STDIO
#include <stdio.h>
#endif // STBI_WRITE_NO_STDIO
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#if defined(STBIW_MALLOC) && defined(STBIW_FREE) && (defined(STBIW_REALLOC) || defined(STBIW_REALLOC_SIZED))
// ok
#elif !defined(STBIW_MALLOC) && !defined(STBIW_FREE) && !defined(STBIW_REALLOC) && !defined(STBIW_REALLOC_SIZED)
// ok
#else
#error "Must define all or none of STBIW_MALLOC, STBIW_FREE, and STBIW_REALLOC (or STBIW_REALLOC_SIZED)."
#endif
#ifndef STBIW_MALLOC
#define STBIW_MALLOC(sz) malloc(sz)
#define STBIW_REALLOC(p,newsz) realloc(p,newsz)
#define STBIW_FREE(p) free(p)
#endif
#ifndef STBIW_REALLOC_SIZED
#define STBIW_REALLOC_SIZED(p,oldsz,newsz) STBIW_REALLOC(p,newsz)
#endif
#ifndef STBIW_MEMMOVE
#define STBIW_MEMMOVE(a,b,sz) memmove(a,b,sz)
#endif
#ifndef STBIW_ASSERT
#include <assert.h>
#define STBIW_ASSERT(x) assert(x)
#endif
#define STBIW_UCHAR(x) (unsigned char) ((x) & 0xff)
#ifdef STB_IMAGE_WRITE_STATIC
static int stbi_write_png_compression_level = 8;
static int stbi_write_tga_with_rle = 1;
static int stbi_write_force_png_filter = -1;
#else
int stbi_write_png_compression_level = 8;
int stbi_write_tga_with_rle = 1;
int stbi_write_force_png_filter = -1;
#endif
static int stbi__flip_vertically_on_write = 0;
STBIWDEF void stbi_flip_vertically_on_write(int flag)
{
stbi__flip_vertically_on_write = flag;
}
typedef struct
{
stbi_write_func *func;
void *context;
} stbi__write_context;
// initialize a callback-based context
static void stbi__start_write_callbacks(stbi__write_context *s, stbi_write_func *c, void *context)
{
s->func = c;
s->context = context;
}
#ifndef STBI_WRITE_NO_STDIO
static void stbi__stdio_write(void *context, void *data, int size)
{
fwrite(data,1,size,(FILE*) context);
}
#if defined(_MSC_VER) && defined(STBI_WINDOWS_UTF8)
#ifdef __cplusplus
#define STBIW_EXTERN extern "C"
#else
#define STBIW_EXTERN extern
#endif
STBIW_EXTERN __declspec(dllimport) int __stdcall MultiByteToWideChar(unsigned int cp, unsigned long flags, const char *str, int cbmb, wchar_t *widestr, int cchwide);
STBIW_EXTERN __declspec(dllimport) int __stdcall WideCharToMultiByte(unsigned int cp, unsigned long flags, const wchar_t *widestr, int cchwide, char *str, int cbmb, const char *defchar, int *used_default);
STBIWDEF int stbiw_convert_wchar_to_utf8(char *buffer, size_t bufferlen, const wchar_t* input)
{
return WideCharToMultiByte(65001 /* UTF8 */, 0, input, -1, buffer, (int) bufferlen, NULL, NULL);
}
#endif
static FILE *stbiw__fopen(char const *filename, char const *mode)
{
FILE *f;
#if defined(_MSC_VER) && defined(STBI_WINDOWS_UTF8)
wchar_t wMode[64];
wchar_t wFilename[1024];
if (0 == MultiByteToWideChar(65001 /* UTF8 */, 0, filename, -1, wFilename, sizeof(wFilename)))
return 0;
if (0 == MultiByteToWideChar(65001 /* UTF8 */, 0, mode, -1, wMode, sizeof(wMode)))
return 0;
#if _MSC_VER >= 1400
if (0 != _wfopen_s(&f, wFilename, wMode))
f = 0;
#else
f = _wfopen(wFilename, wMode);
#endif
#elif defined(_MSC_VER) && _MSC_VER >= 1400
if (0 != fopen_s(&f, filename, mode))
f=0;
#else
f = fopen(filename, mode);
#endif
return f;
}
static int stbi__start_write_file(stbi__write_context *s, const char *filename)
{
FILE *f = stbiw__fopen(filename, "wb");
stbi__start_write_callbacks(s, stbi__stdio_write, (void *) f);
return f != NULL;
}
static void stbi__end_write_file(stbi__write_context *s)
{
fclose((FILE *)s->context);
}
#endif // !STBI_WRITE_NO_STDIO
typedef unsigned int stbiw_uint32;
typedef int stb_image_write_test[sizeof(stbiw_uint32)==4 ? 1 : -1];
static void stbiw__writefv(stbi__write_context *s, const char *fmt, va_list v)
{
while (*fmt) {
switch (*fmt++) {
case ' ': break;
case '1': { unsigned char x = STBIW_UCHAR(va_arg(v, int));
s->func(s->context,&x,1);
break; }
case '2': { int x = va_arg(v,int);
unsigned char b[2];
b[0] = STBIW_UCHAR(x);
b[1] = STBIW_UCHAR(x>>8);
s->func(s->context,b,2);
break; }
case '4': { stbiw_uint32 x = va_arg(v,int);
unsigned char b[4];
b[0]=STBIW_UCHAR(x);
b[1]=STBIW_UCHAR(x>>8);
b[2]=STBIW_UCHAR(x>>16);
b[3]=STBIW_UCHAR(x>>24);
s->func(s->context,b,4);
break; }
default:
STBIW_ASSERT(0);
return;
}
}
}
static void stbiw__writef(stbi__write_context *s, const char *fmt, ...)
{
va_list v;
va_start(v, fmt);
stbiw__writefv(s, fmt, v);
va_end(v);
}
static void stbiw__putc(stbi__write_context *s, unsigned char c)
{
s->func(s->context, &c, 1);
}
static void stbiw__write3(stbi__write_context *s, unsigned char a, unsigned char b, unsigned char c)
{
unsigned char arr[3];
arr[0] = a; arr[1] = b; arr[2] = c;
s->func(s->context, arr, 3);
}
static void stbiw__write_pixel(stbi__write_context *s, int rgb_dir, int comp, int write_alpha, int expand_mono, unsigned char *d)
{
unsigned char bg[3] = { 255, 0, 255}, px[3];
int k;
if (write_alpha < 0)
s->func(s->context, &d[comp - 1], 1);
switch (comp) {
case 2: // 2 pixels = mono + alpha, alpha is written separately, so same as 1-channel case
case 1:
if (expand_mono)
stbiw__write3(s, d[0], d[0], d[0]); // monochrome bmp
else
s->func(s->context, d, 1); // monochrome TGA
break;
case 4:
if (!write_alpha) {
// composite against pink background
for (k = 0; k < 3; ++k)
px[k] = bg[k] + ((d[k] - bg[k]) * d[3]) / 255;
stbiw__write3(s, px[1 - rgb_dir], px[1], px[1 + rgb_dir]);
break;
}
/* FALLTHROUGH */
case 3:
stbiw__write3(s, d[1 - rgb_dir], d[1], d[1 + rgb_dir]);
break;
}
if (write_alpha > 0)
s->func(s->context, &d[comp - 1], 1);
}
static void stbiw__write_pixels(stbi__write_context *s, int rgb_dir, int vdir, int x, int y, int comp, void *data, int write_alpha, int scanline_pad, int expand_mono)
{
stbiw_uint32 zero = 0;
int i,j, j_end;
if (y <= 0)
return;
if (stbi__flip_vertically_on_write)
vdir *= -1;
if (vdir < 0) {
j_end = -1; j = y-1;
} else {
j_end = y; j = 0;
}
for (; j != j_end; j += vdir) {
for (i=0; i < x; ++i) {
unsigned char *d = (unsigned char *) data + (j*x+i)*comp;
stbiw__write_pixel(s, rgb_dir, comp, write_alpha, expand_mono, d);
}
s->func(s->context, &zero, scanline_pad);
}
}
static int stbiw__outfile(stbi__write_context *s, int rgb_dir, int vdir, int x, int y, int comp, int expand_mono, void *data, int alpha, int pad, const char *fmt, ...)
{
if (y < 0 || x < 0) {
return 0;
} else {
va_list v;
va_start(v, fmt);
stbiw__writefv(s, fmt, v);
va_end(v);
stbiw__write_pixels(s,rgb_dir,vdir,x,y,comp,data,alpha,pad, expand_mono);
return 1;
}
}
static int stbi_write_bmp_core(stbi__write_context *s, int x, int y, int comp, const void *data)
{
int pad = (-x*3) & 3;
return stbiw__outfile(s,-1,-1,x,y,comp,1,(void *) data,0,pad,
"11 4 22 4" "4 44 22 444444",
'B', 'M', 14+40+(x*3+pad)*y, 0,0, 14+40, // file header
40, x,y, 1,24, 0,0,0,0,0,0); // bitmap header
}
STBIWDEF int stbi_write_bmp_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data)
{
stbi__write_context s;
stbi__start_write_callbacks(&s, func, context);
return stbi_write_bmp_core(&s, x, y, comp, data);
}
#ifndef STBI_WRITE_NO_STDIO
STBIWDEF int stbi_write_bmp(char const *filename, int x, int y, int comp, const void *data)
{
stbi__write_context s;
if (stbi__start_write_file(&s,filename)) {
int r = stbi_write_bmp_core(&s, x, y, comp, data);
stbi__end_write_file(&s);
return r;
} else
return 0;
}
#endif //!STBI_WRITE_NO_STDIO
static int stbi_write_tga_core(stbi__write_context *s, int x, int y, int comp, void *data)
{
int has_alpha = (comp == 2 || comp == 4);
int colorbytes = has_alpha ? comp-1 : comp;
int format = colorbytes < 2 ? 3 : 2; // 3 color channels (RGB/RGBA) = 2, 1 color channel (Y/YA) = 3
if (y < 0 || x < 0)
return 0;
if (!stbi_write_tga_with_rle) {
return stbiw__outfile(s, -1, -1, x, y, comp, 0, (void *) data, has_alpha, 0,
"111 221 2222 11", 0, 0, format, 0, 0, 0, 0, 0, x, y, (colorbytes + has_alpha) * 8, has_alpha * 8);
} else {
int i,j,k;
int jend, jdir;
stbiw__writef(s, "111 221 2222 11", 0,0,format+8, 0,0,0, 0,0,x,y, (colorbytes + has_alpha) * 8, has_alpha * 8);
if (stbi__flip_vertically_on_write) {
j = 0;
jend = y;
jdir = 1;
} else {
j = y-1;
jend = -1;
jdir = -1;
}
for (; j != jend; j += jdir) {
unsigned char *row = (unsigned char *) data + j * x * comp;
int len;
for (i = 0; i < x; i += len) {
unsigned char *begin = row + i * comp;
int diff = 1;
len = 1;
if (i < x - 1) {
++len;
diff = memcmp(begin, row + (i + 1) * comp, comp);
if (diff) {
const unsigned char *prev = begin;
for (k = i + 2; k < x && len < 128; ++k) {
if (memcmp(prev, row + k * comp, comp)) {
prev += comp;
++len;
} else {
--len;
break;
}
}
} else {
for (k = i + 2; k < x && len < 128; ++k) {
if (!memcmp(begin, row + k * comp, comp)) {
++len;
} else {
break;
}
}
}
}
if (diff) {
unsigned char header = STBIW_UCHAR(len - 1);
s->func(s->context, &header, 1);
for (k = 0; k < len; ++k) {
stbiw__write_pixel(s, -1, comp, has_alpha, 0, begin + k * comp);
}
} else {
unsigned char header = STBIW_UCHAR(len - 129);
s->func(s->context, &header, 1);
stbiw__write_pixel(s, -1, comp, has_alpha, 0, begin);
}
}
}
}
return 1;
}
STBIWDEF int stbi_write_tga_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data)
{
stbi__write_context s;
stbi__start_write_callbacks(&s, func, context);
return stbi_write_tga_core(&s, x, y, comp, (void *) data);
}
#ifndef STBI_WRITE_NO_STDIO
STBIWDEF int stbi_write_tga(char const *filename, int x, int y, int comp, const void *data)
{
stbi__write_context s;
if (stbi__start_write_file(&s,filename)) {
int r = stbi_write_tga_core(&s, x, y, comp, (void *) data);
stbi__end_write_file(&s);
return r;
} else
return 0;
}
#endif
// *************************************************************************************************
// Radiance RGBE HDR writer
// by Baldur Karlsson
#define stbiw__max(a, b) ((a) > (b) ? (a) : (b))
static void stbiw__linear_to_rgbe(unsigned char *rgbe, float *linear)
{
int exponent;
float maxcomp = stbiw__max(linear[0], stbiw__max(linear[1], linear[2]));
if (maxcomp < 1e-32f) {
rgbe[0] = rgbe[1] = rgbe[2] = rgbe[3] = 0;
} else {
float normalize = (float) frexp(maxcomp, &exponent) * 256.0f/maxcomp;
rgbe[0] = (unsigned char)(linear[0] * normalize);
rgbe[1] = (unsigned char)(linear[1] * normalize);
rgbe[2] = (unsigned char)(linear[2] * normalize);
rgbe[3] = (unsigned char)(exponent + 128);
}
}
static void stbiw__write_run_data(stbi__write_context *s, int length, unsigned char databyte)
{
unsigned char lengthbyte = STBIW_UCHAR(length+128);
STBIW_ASSERT(length+128 <= 255);
s->func(s->context, &lengthbyte, 1);
s->func(s->context, &databyte, 1);
}
static void stbiw__write_dump_data(stbi__write_context *s, int length, unsigned char *data)
{
unsigned char lengthbyte = STBIW_UCHAR(length);
STBIW_ASSERT(length <= 128); // inconsistent with spec but consistent with official code
s->func(s->context, &lengthbyte, 1);
s->func(s->context, data, length);
}
static void stbiw__write_hdr_scanline(stbi__write_context *s, int width, int ncomp, unsigned char *scratch, float *scanline)
{
unsigned char scanlineheader[4] = { 2, 2, 0, 0 };
unsigned char rgbe[4];
float linear[3];
int x;
scanlineheader[2] = (width&0xff00)>>8;
scanlineheader[3] = (width&0x00ff);
/* skip RLE for images too small or large */
if (width < 8 || width >= 32768) {
for (x=0; x < width; x++) {
switch (ncomp) {
case 4: /* fallthrough */
case 3: linear[2] = scanline[x*ncomp + 2];
linear[1] = scanline[x*ncomp + 1];
linear[0] = scanline[x*ncomp + 0];
break;
default:
linear[0] = linear[1] = linear[2] = scanline[x*ncomp + 0];
break;
}
stbiw__linear_to_rgbe(rgbe, linear);
s->func(s->context, rgbe, 4);
}
} else {
int c,r;
/* encode into scratch buffer */
for (x=0; x < width; x++) {
switch(ncomp) {
case 4: /* fallthrough */
case 3: linear[2] = scanline[x*ncomp + 2];
linear[1] = scanline[x*ncomp + 1];
linear[0] = scanline[x*ncomp + 0];
break;
default:
linear[0] = linear[1] = linear[2] = scanline[x*ncomp + 0];
break;
}
stbiw__linear_to_rgbe(rgbe, linear);
scratch[x + width*0] = rgbe[0];
scratch[x + width*1] = rgbe[1];
scratch[x + width*2] = rgbe[2];
scratch[x + width*3] = rgbe[3];
}
s->func(s->context, scanlineheader, 4);
/* RLE each component separately */
for (c=0; c < 4; c++) {
unsigned char *comp = &scratch[width*c];
x = 0;
while (x < width) {
// find first run
r = x;
while (r+2 < width) {
if (comp[r] == comp[r+1] && comp[r] == comp[r+2])
break;
++r;
}
if (r+2 >= width)
r = width;
// dump up to first run
while (x < r) {
int len = r-x;
if (len > 128) len = 128;
stbiw__write_dump_data(s, len, &comp[x]);
x += len;
}
// if there's a run, output it
if (r+2 < width) { // same test as what we break out of in search loop, so only true if we break'd
// find next byte after run
while (r < width && comp[r] == comp[x])
++r;
// output run up to r
while (x < r) {
int len = r-x;
if (len > 127) len = 127;
stbiw__write_run_data(s, len, comp[x]);
x += len;
}
}
}
}
}
}
static int stbi_write_hdr_core(stbi__write_context *s, int x, int y, int comp, float *data)
{
if (y <= 0 || x <= 0 || data == NULL)
return 0;
else {
// Each component is stored separately. Allocate scratch space for full output scanline.
unsigned char *scratch = (unsigned char *) STBIW_MALLOC(x*4);
int i, len;
char buffer[128];
char header[] = "#?RADIANCE\n# Written by stb_image_write.h\nFORMAT=32-bit_rle_rgbe\n";
s->func(s->context, header, sizeof(header)-1);
#ifdef __STDC_WANT_SECURE_LIB__
len = sprintf_s(buffer, sizeof(buffer), "EXPOSURE= 1.0000000000000\n\n-Y %d +X %d\n", y, x);
#else
len = sprintf(buffer, "EXPOSURE= 1.0000000000000\n\n-Y %d +X %d\n", y, x);
#endif
s->func(s->context, buffer, len);
for(i=0; i < y; i++)
stbiw__write_hdr_scanline(s, x, comp, scratch, data + comp*x*(stbi__flip_vertically_on_write ? y-1-i : i));
STBIW_FREE(scratch);
return 1;
}
}
STBIWDEF int stbi_write_hdr_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const float *data)
{
stbi__write_context s;
stbi__start_write_callbacks(&s, func, context);
return stbi_write_hdr_core(&s, x, y, comp, (float *) data);
}
#ifndef STBI_WRITE_NO_STDIO
STBIWDEF int stbi_write_hdr(char const *filename, int x, int y, int comp, const float *data)
{
stbi__write_context s;
if (stbi__start_write_file(&s,filename)) {
int r = stbi_write_hdr_core(&s, x, y, comp, (float *) data);
stbi__end_write_file(&s);
return r;
} else
return 0;
}
#endif // STBI_WRITE_NO_STDIO
//////////////////////////////////////////////////////////////////////////////
//
// PNG writer
//
#ifndef STBIW_ZLIB_COMPRESS
// stretchy buffer; stbiw__sbpush() == vector<>::push_back() -- stbiw__sbcount() == vector<>::size()
#define stbiw__sbraw(a) ((int *) (void *) (a) - 2)
#define stbiw__sbm(a) stbiw__sbraw(a)[0]
#define stbiw__sbn(a) stbiw__sbraw(a)[1]
#define stbiw__sbneedgrow(a,n) ((a)==0 || stbiw__sbn(a)+n >= stbiw__sbm(a))
#define stbiw__sbmaybegrow(a,n) (stbiw__sbneedgrow(a,(n)) ? stbiw__sbgrow(a,n) : 0)
#define stbiw__sbgrow(a,n) stbiw__sbgrowf((void **) &(a), (n), sizeof(*(a)))
#define stbiw__sbpush(a, v) (stbiw__sbmaybegrow(a,1), (a)[stbiw__sbn(a)++] = (v))
#define stbiw__sbcount(a) ((a) ? stbiw__sbn(a) : 0)
#define stbiw__sbfree(a) ((a) ? STBIW_FREE(stbiw__sbraw(a)),0 : 0)
static void *stbiw__sbgrowf(void **arr, int increment, int itemsize)
{
int m = *arr ? 2*stbiw__sbm(*arr)+increment : increment+1;
void *p = STBIW_REALLOC_SIZED(*arr ? stbiw__sbraw(*arr) : 0, *arr ? (stbiw__sbm(*arr)*itemsize + sizeof(int)*2) : 0, itemsize * m + sizeof(int)*2);
STBIW_ASSERT(p);
if (p) {
if (!*arr) ((int *) p)[1] = 0;
*arr = (void *) ((int *) p + 2);
stbiw__sbm(*arr) = m;
}
return *arr;
}
static unsigned char *stbiw__zlib_flushf(unsigned char *data, unsigned int *bitbuffer, int *bitcount)
{
while (*bitcount >= 8) {
stbiw__sbpush(data, STBIW_UCHAR(*bitbuffer));
*bitbuffer >>= 8;
*bitcount -= 8;
}
return data;
}
static int stbiw__zlib_bitrev(int code, int codebits)
{
int res=0;
while (codebits--) {
res = (res << 1) | (code & 1);
code >>= 1;
}
return res;
}
static unsigned int stbiw__zlib_countm(unsigned char *a, unsigned char *b, int limit)
{
int i;
for (i=0; i < limit && i < 258; ++i)
if (a[i] != b[i]) break;
return i;
}
static unsigned int stbiw__zhash(unsigned char *data)
{
stbiw_uint32 hash = data[0] + (data[1] << 8) + (data[2] << 16);
hash ^= hash << 3;
hash += hash >> 5;
hash ^= hash << 4;
hash += hash >> 17;
hash ^= hash << 25;
hash += hash >> 6;
return hash;
}
#define stbiw__zlib_flush() (out = stbiw__zlib_flushf(out, &bitbuf, &bitcount))
#define stbiw__zlib_add(code,codebits) \
(bitbuf |= (code) << bitcount, bitcount += (codebits), stbiw__zlib_flush())
#define stbiw__zlib_huffa(b,c) stbiw__zlib_add(stbiw__zlib_bitrev(b,c),c)
// default huffman tables
#define stbiw__zlib_huff1(n) stbiw__zlib_huffa(0x30 + (n), 8)
#define stbiw__zlib_huff2(n) stbiw__zlib_huffa(0x190 + (n)-144, 9)
#define stbiw__zlib_huff3(n) stbiw__zlib_huffa(0 + (n)-256,7)
#define stbiw__zlib_huff4(n) stbiw__zlib_huffa(0xc0 + (n)-280,8)
#define stbiw__zlib_huff(n) ((n) <= 143 ? stbiw__zlib_huff1(n) : (n) <= 255 ? stbiw__zlib_huff2(n) : (n) <= 279 ? stbiw__zlib_huff3(n) : stbiw__zlib_huff4(n))
#define stbiw__zlib_huffb(n) ((n) <= 143 ? stbiw__zlib_huff1(n) : stbiw__zlib_huff2(n))
#define stbiw__ZHASH 16384
#endif // STBIW_ZLIB_COMPRESS
STBIWDEF unsigned char * stbi_zlib_compress(unsigned char *data, int data_len, int *out_len, int quality)
{
#ifdef STBIW_ZLIB_COMPRESS
// user provided a zlib compress implementation, use that
return STBIW_ZLIB_COMPRESS(data, data_len, out_len, quality);
#else // use builtin
static unsigned short lengthc[] = { 3,4,5,6,7,8,9,10,11,13,15,17,19,23,27,31,35,43,51,59,67,83,99,115,131,163,195,227,258, 259 };
static unsigned char lengtheb[]= { 0,0,0,0,0,0,0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0 };
static unsigned short distc[] = { 1,2,3,4,5,7,9,13,17,25,33,49,65,97,129,193,257,385,513,769,1025,1537,2049,3073,4097,6145,8193,12289,16385,24577, 32768 };
static unsigned char disteb[] = { 0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13 };
unsigned int bitbuf=0;
int i,j, bitcount=0;
unsigned char *out = NULL;
unsigned char ***hash_table = (unsigned char***) STBIW_MALLOC(stbiw__ZHASH * sizeof(unsigned char**));
if (hash_table == NULL)
return NULL;
if (quality < 5) quality = 5;
stbiw__sbpush(out, 0x78); // DEFLATE 32K window
stbiw__sbpush(out, 0x5e); // FLEVEL = 1
stbiw__zlib_add(1,1); // BFINAL = 1
stbiw__zlib_add(1,2); // BTYPE = 1 -- fixed huffman
for (i=0; i < stbiw__ZHASH; ++i)
hash_table[i] = NULL;
i=0;
while (i < data_len-3) {
// hash next 3 bytes of data to be compressed
int h = stbiw__zhash(data+i)&(stbiw__ZHASH-1), best=3;
unsigned char *bestloc = 0;
unsigned char **hlist = hash_table[h];
int n = stbiw__sbcount(hlist);
for (j=0; j < n; ++j) {
if (hlist[j]-data > i-32768) { // if entry lies within window
int d = stbiw__zlib_countm(hlist[j], data+i, data_len-i);
if (d >= best) { best=d; bestloc=hlist[j]; }
}
}
// when hash table entry is too long, delete half the entries
if (hash_table[h] && stbiw__sbn(hash_table[h]) == 2*quality) {
STBIW_MEMMOVE(hash_table[h], hash_table[h]+quality, sizeof(hash_table[h][0])*quality);
stbiw__sbn(hash_table[h]) = quality;
}
stbiw__sbpush(hash_table[h],data+i);
if (bestloc) {
// "lazy matching" - check match at *next* byte, and if it's better, do cur byte as literal
h = stbiw__zhash(data+i+1)&(stbiw__ZHASH-1);
hlist = hash_table[h];
n = stbiw__sbcount(hlist);
for (j=0; j < n; ++j) {
if (hlist[j]-data > i-32767) {
int e = stbiw__zlib_countm(hlist[j], data+i+1, data_len-i-1);
if (e > best) { // if next match is better, bail on current match
bestloc = NULL;
break;
}
}
}
}
if (bestloc) {
int d = (int) (data+i - bestloc); // distance back
STBIW_ASSERT(d <= 32767 && best <= 258);
for (j=0; best > lengthc[j+1]-1; ++j);
stbiw__zlib_huff(j+257);
if (lengtheb[j]) stbiw__zlib_add(best - lengthc[j], lengtheb[j]);
for (j=0; d > distc[j+1]-1; ++j);
stbiw__zlib_add(stbiw__zlib_bitrev(j,5),5);
if (disteb[j]) stbiw__zlib_add(d - distc[j], disteb[j]);
i += best;
} else {
stbiw__zlib_huffb(data[i]);
++i;
}
}
// write out final bytes
for (;i < data_len; ++i)
stbiw__zlib_huffb(data[i]);
stbiw__zlib_huff(256); // end of block
// pad with 0 bits to byte boundary
while (bitcount)
stbiw__zlib_add(0,1);
for (i=0; i < stbiw__ZHASH; ++i)
(void) stbiw__sbfree(hash_table[i]);
STBIW_FREE(hash_table);
{
// compute adler32 on input
unsigned int s1=1, s2=0;
int blocklen = (int) (data_len % 5552);
j=0;
while (j < data_len) {
for (i=0; i < blocklen; ++i) { s1 += data[j+i]; s2 += s1; }
s1 %= 65521; s2 %= 65521;
j += blocklen;
blocklen = 5552;
}
stbiw__sbpush(out, STBIW_UCHAR(s2 >> 8));
stbiw__sbpush(out, STBIW_UCHAR(s2));
stbiw__sbpush(out, STBIW_UCHAR(s1 >> 8));
stbiw__sbpush(out, STBIW_UCHAR(s1));
}
*out_len = stbiw__sbn(out);
// make returned pointer freeable
STBIW_MEMMOVE(stbiw__sbraw(out), out, *out_len);
return (unsigned char *) stbiw__sbraw(out);
#endif // STBIW_ZLIB_COMPRESS
}
static unsigned int stbiw__crc32(unsigned char *buffer, int len)
{
#ifdef STBIW_CRC32
return STBIW_CRC32(buffer, len);
#else
static unsigned int crc_table[256] =
{
0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA, 0x076DC419, 0x706AF48F, 0xE963A535, 0x9E6495A3,
0x0eDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988, 0x09B64C2B, 0x7EB17CBD, 0xE7B82D07, 0x90BF1D91,
0x1DB71064, 0x6AB020F2, 0xF3B97148, 0x84BE41DE, 0x1ADAD47D, 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7,
0x136C9856, 0x646BA8C0, 0xFD62F97A, 0x8A65C9EC, 0x14015C4F, 0x63066CD9, 0xFA0F3D63, 0x8D080DF5,
0x3B6E20C8, 0x4C69105E, 0xD56041E4, 0xA2677172, 0x3C03E4D1, 0x4B04D447, 0xD20D85FD, 0xA50AB56B,
0x35B5A8FA, 0x42B2986C, 0xDBBBC9D6, 0xACBCF940, 0x32D86CE3, 0x45DF5C75, 0xDCD60DCF, 0xABD13D59,
0x26D930AC, 0x51DE003A, 0xC8D75180, 0xBFD06116, 0x21B4F4B5, 0x56B3C423, 0xCFBA9599, 0xB8BDA50F,
0x2802B89E, 0x5F058808, 0xC60CD9B2, 0xB10BE924, 0x2F6F7C87, 0x58684C11, 0xC1611DAB, 0xB6662D3D,
0x76DC4190, 0x01DB7106, 0x98D220BC, 0xEFD5102A, 0x71B18589, 0x06B6B51F, 0x9FBFE4A5, 0xE8B8D433,
0x7807C9A2, 0x0F00F934, 0x9609A88E, 0xE10E9818, 0x7F6A0DBB, 0x086D3D2D, 0x91646C97, 0xE6635C01,
0x6B6B51F4, 0x1C6C6162, 0x856530D8, 0xF262004E, 0x6C0695ED, 0x1B01A57B, 0x8208F4C1, 0xF50FC457,
0x65B0D9C6, 0x12B7E950, 0x8BBEB8EA, 0xFCB9887C, 0x62DD1DDF, 0x15DA2D49, 0x8CD37CF3, 0xFBD44C65,
0x4DB26158, 0x3AB551CE, 0xA3BC0074, 0xD4BB30E2, 0x4ADFA541, 0x3DD895D7, 0xA4D1C46D, 0xD3D6F4FB,
0x4369E96A, 0x346ED9FC, 0xAD678846, 0xDA60B8D0, 0x44042D73, 0x33031DE5, 0xAA0A4C5F, 0xDD0D7CC9,
0x5005713C, 0x270241AA, 0xBE0B1010, 0xC90C2086, 0x5768B525, 0x206F85B3, 0xB966D409, 0xCE61E49F,
0x5EDEF90E, 0x29D9C998, 0xB0D09822, 0xC7D7A8B4, 0x59B33D17, 0x2EB40D81, 0xB7BD5C3B, 0xC0BA6CAD,
0xEDB88320, 0x9ABFB3B6, 0x03B6E20C, 0x74B1D29A, 0xEAD54739, 0x9DD277AF, 0x04DB2615, 0x73DC1683,
0xE3630B12, 0x94643B84, 0x0D6D6A3E, 0x7A6A5AA8, 0xE40ECF0B, 0x9309FF9D, 0x0A00AE27, 0x7D079EB1,
0xF00F9344, 0x8708A3D2, 0x1E01F268, 0x6906C2FE, 0xF762575D, 0x806567CB, 0x196C3671, 0x6E6B06E7,
0xFED41B76, 0x89D32BE0, 0x10DA7A5A, 0x67DD4ACC, 0xF9B9DF6F, 0x8EBEEFF9, 0x17B7BE43, 0x60B08ED5,
0xD6D6A3E8, 0xA1D1937E, 0x38D8C2C4, 0x4FDFF252, 0xD1BB67F1, 0xA6BC5767, 0x3FB506DD, 0x48B2364B,
0xD80D2BDA, 0xAF0A1B4C, 0x36034AF6, 0x41047A60, 0xDF60EFC3, 0xA867DF55, 0x316E8EEF, 0x4669BE79,
0xCB61B38C, 0xBC66831A, 0x256FD2A0, 0x5268E236, 0xCC0C7795, 0xBB0B4703, 0x220216B9, 0x5505262F,
0xC5BA3BBE, 0xB2BD0B28, 0x2BB45A92, 0x5CB36A04, 0xC2D7FFA7, 0xB5D0CF31, 0x2CD99E8B, 0x5BDEAE1D,
0x9B64C2B0, 0xEC63F226, 0x756AA39C, 0x026D930A, 0x9C0906A9, 0xEB0E363F, 0x72076785, 0x05005713,
0x95BF4A82, 0xE2B87A14, 0x7BB12BAE, 0x0CB61B38, 0x92D28E9B, 0xE5D5BE0D, 0x7CDCEFB7, 0x0BDBDF21,
0x86D3D2D4, 0xF1D4E242, 0x68DDB3F8, 0x1FDA836E, 0x81BE16CD, 0xF6B9265B, 0x6FB077E1, 0x18B74777,
0x88085AE6, 0xFF0F6A70, 0x66063BCA, 0x11010B5C, 0x8F659EFF, 0xF862AE69, 0x616BFFD3, 0x166CCF45,
0xA00AE278, 0xD70DD2EE, 0x4E048354, 0x3903B3C2, 0xA7672661, 0xD06016F7, 0x4969474D, 0x3E6E77DB,
0xAED16A4A, 0xD9D65ADC, 0x40DF0B66, 0x37D83BF0, 0xA9BCAE53, 0xDEBB9EC5, 0x47B2CF7F, 0x30B5FFE9,
0xBDBDF21C, 0xCABAC28A, 0x53B39330, 0x24B4A3A6, 0xBAD03605, 0xCDD70693, 0x54DE5729, 0x23D967BF,
0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94, 0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D
};
unsigned int crc = ~0u;
int i;
for (i=0; i < len; ++i)
crc = (crc >> 8) ^ crc_table[buffer[i] ^ (crc & 0xff)];
return ~crc;
#endif
}
#define stbiw__wpng4(o,a,b,c,d) ((o)[0]=STBIW_UCHAR(a),(o)[1]=STBIW_UCHAR(b),(o)[2]=STBIW_UCHAR(c),(o)[3]=STBIW_UCHAR(d),(o)+=4)
#define stbiw__wp32(data,v) stbiw__wpng4(data, (v)>>24,(v)>>16,(v)>>8,(v));
#define stbiw__wptag(data,s) stbiw__wpng4(data, s[0],s[1],s[2],s[3])
static void stbiw__wpcrc(unsigned char **data, int len)
{
unsigned int crc = stbiw__crc32(*data - len - 4, len+4);
stbiw__wp32(*data, crc);
}
static unsigned char stbiw__paeth(int a, int b, int c)
{
int p = a + b - c, pa = abs(p-a), pb = abs(p-b), pc = abs(p-c);
if (pa <= pb && pa <= pc) return STBIW_UCHAR(a);
if (pb <= pc) return STBIW_UCHAR(b);
return STBIW_UCHAR(c);
}
// @OPTIMIZE: provide an option that always forces left-predict or paeth predict
static void stbiw__encode_png_line(unsigned char *pixels, int stride_bytes, int width, int height, int y, int n, int filter_type, signed char *line_buffer)
{
static int mapping[] = { 0,1,2,3,4 };
static int firstmap[] = { 0,1,0,5,6 };
int *mymap = (y != 0) ? mapping : firstmap;
int i;
int type = mymap[filter_type];
unsigned char *z = pixels + stride_bytes * (stbi__flip_vertically_on_write ? height-1-y : y);
int signed_stride = stbi__flip_vertically_on_write ? -stride_bytes : stride_bytes;
if (type==0) {
memcpy(line_buffer, z, width*n);
return;
}
// first loop isn't optimized since it's just one pixel
for (i = 0; i < n; ++i) {
switch (type) {
case 1: line_buffer[i] = z[i]; break;
case 2: line_buffer[i] = z[i] - z[i-signed_stride]; break;
case 3: line_buffer[i] = z[i] - (z[i-signed_stride]>>1); break;
case 4: line_buffer[i] = (signed char) (z[i] - stbiw__paeth(0,z[i-signed_stride],0)); break;
case 5: line_buffer[i] = z[i]; break;
case 6: line_buffer[i] = z[i]; break;
}
}
switch (type) {
case 1: for (i=n; i < width*n; ++i) line_buffer[i] = z[i] - z[i-n]; break;
case 2: for (i=n; i < width*n; ++i) line_buffer[i] = z[i] - z[i-signed_stride]; break;
case 3: for (i=n; i < width*n; ++i) line_buffer[i] = z[i] - ((z[i-n] + z[i-signed_stride])>>1); break;
case 4: for (i=n; i < width*n; ++i) line_buffer[i] = z[i] - stbiw__paeth(z[i-n], z[i-signed_stride], z[i-signed_stride-n]); break;
case 5: for (i=n; i < width*n; ++i) line_buffer[i] = z[i] - (z[i-n]>>1); break;
case 6: for (i=n; i < width*n; ++i) line_buffer[i] = z[i] - stbiw__paeth(z[i-n], 0,0); break;
}
}
STBIWDEF unsigned char *stbi_write_png_to_mem(const unsigned char *pixels, int stride_bytes, int x, int y, int n, int *out_len)
{
int force_filter = stbi_write_force_png_filter;
int ctype[5] = { -1, 0, 4, 2, 6 };
unsigned char sig[8] = { 137,80,78,71,13,10,26,10 };
unsigned char *out,*o, *filt, *zlib;
signed char *line_buffer;
int j,zlen;
if (stride_bytes == 0)
stride_bytes = x * n;
if (force_filter >= 5) {
force_filter = -1;
}
filt = (unsigned char *) STBIW_MALLOC((x*n+1) * y); if (!filt) return 0;
line_buffer = (signed char *) STBIW_MALLOC(x * n); if (!line_buffer) { STBIW_FREE(filt); return 0; }
for (j=0; j < y; ++j) {
int filter_type;
if (force_filter > -1) {
filter_type = force_filter;
stbiw__encode_png_line((unsigned char*)(pixels), stride_bytes, x, y, j, n, force_filter, line_buffer);
} else { // Estimate the best filter by running through all of them:
int best_filter = 0, best_filter_val = 0x7fffffff, est, i;
for (filter_type = 0; filter_type < 5; filter_type++) {
stbiw__encode_png_line((unsigned char*)(pixels), stride_bytes, x, y, j, n, filter_type, line_buffer);
// Estimate the entropy of the line using this filter; the less, the better.
est = 0;
for (i = 0; i < x*n; ++i) {
est += abs((signed char) line_buffer[i]);
}
if (est < best_filter_val) {
best_filter_val = est;
best_filter = filter_type;
}
}
if (filter_type != best_filter) { // If the last iteration already got us the best filter, don't redo it
stbiw__encode_png_line((unsigned char*)(pixels), stride_bytes, x, y, j, n, best_filter, line_buffer);
filter_type = best_filter;
}
}
// when we get here, filter_type contains the filter type, and line_buffer contains the data
filt[j*(x*n+1)] = (unsigned char) filter_type;
STBIW_MEMMOVE(filt+j*(x*n+1)+1, line_buffer, x*n);
}
STBIW_FREE(line_buffer);
zlib = stbi_zlib_compress(filt, y*( x*n+1), &zlen, stbi_write_png_compression_level);
STBIW_FREE(filt);
if (!zlib) return 0;
// each tag requires 12 bytes of overhead
out = (unsigned char *) STBIW_MALLOC(8 + 12+13 + 12+zlen + 12);
if (!out) return 0;
*out_len = 8 + 12+13 + 12+zlen + 12;
o=out;
STBIW_MEMMOVE(o,sig,8); o+= 8;
stbiw__wp32(o, 13); // header length
stbiw__wptag(o, "IHDR");
stbiw__wp32(o, x);
stbiw__wp32(o, y);
*o++ = 8;
*o++ = STBIW_UCHAR(ctype[n]);
*o++ = 0;
*o++ = 0;
*o++ = 0;
stbiw__wpcrc(&o,13);
stbiw__wp32(o, zlen);
stbiw__wptag(o, "IDAT");
STBIW_MEMMOVE(o, zlib, zlen);
o += zlen;
STBIW_FREE(zlib);
stbiw__wpcrc(&o, zlen);
stbiw__wp32(o,0);
stbiw__wptag(o, "IEND");
stbiw__wpcrc(&o,0);
STBIW_ASSERT(o == out + *out_len);
return out;
}
#ifndef STBI_WRITE_NO_STDIO
STBIWDEF int stbi_write_png(char const *filename, int x, int y, int comp, const void *data, int stride_bytes)
{
FILE *f;
int len;
unsigned char *png = stbi_write_png_to_mem((const unsigned char *) data, stride_bytes, x, y, comp, &len);
if (png == NULL) return 0;
f = stbiw__fopen(filename, "wb");
if (!f) { STBIW_FREE(png); return 0; }
fwrite(png, 1, len, f);
fclose(f);
STBIW_FREE(png);
return 1;
}
#endif
STBIWDEF int stbi_write_png_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data, int stride_bytes)
{
int len;
unsigned char *png = stbi_write_png_to_mem((const unsigned char *) data, stride_bytes, x, y, comp, &len);
if (png == NULL) return 0;
func(context, png, len);
STBIW_FREE(png);
return 1;
}
/* ***************************************************************************
*
* JPEG writer
*
* This is based on Jon Olick's jo_jpeg.cpp:
* public domain Simple, Minimalistic JPEG writer - http://www.jonolick.com/code.html
*/
static const unsigned char stbiw__jpg_ZigZag[] = { 0,1,5,6,14,15,27,28,2,4,7,13,16,26,29,42,3,8,12,17,25,30,41,43,9,11,18,
24,31,40,44,53,10,19,23,32,39,45,52,54,20,22,33,38,46,51,55,60,21,34,37,47,50,56,59,61,35,36,48,49,57,58,62,63 };
static void stbiw__jpg_writeBits(stbi__write_context *s, int *bitBufP, int *bitCntP, const unsigned short *bs) {
int bitBuf = *bitBufP, bitCnt = *bitCntP;
bitCnt += bs[1];
bitBuf |= bs[0] << (24 - bitCnt);
while(bitCnt >= 8) {
unsigned char c = (bitBuf >> 16) & 255;
stbiw__putc(s, c);
if(c == 255) {
stbiw__putc(s, 0);
}
bitBuf <<= 8;
bitCnt -= 8;
}
*bitBufP = bitBuf;
*bitCntP = bitCnt;
}
static void stbiw__jpg_DCT(float *d0p, float *d1p, float *d2p, float *d3p, float *d4p, float *d5p, float *d6p, float *d7p) {
float d0 = *d0p, d1 = *d1p, d2 = *d2p, d3 = *d3p, d4 = *d4p, d5 = *d5p, d6 = *d6p, d7 = *d7p;
float z1, z2, z3, z4, z5, z11, z13;
float tmp0 = d0 + d7;
float tmp7 = d0 - d7;
float tmp1 = d1 + d6;
float tmp6 = d1 - d6;
float tmp2 = d2 + d5;
float tmp5 = d2 - d5;
float tmp3 = d3 + d4;
float tmp4 = d3 - d4;
// Even part
float tmp10 = tmp0 + tmp3; // phase 2
float tmp13 = tmp0 - tmp3;
float tmp11 = tmp1 + tmp2;
float tmp12 = tmp1 - tmp2;
d0 = tmp10 + tmp11; // phase 3
d4 = tmp10 - tmp11;
z1 = (tmp12 + tmp13) * 0.707106781f; // c4
d2 = tmp13 + z1; // phase 5
d6 = tmp13 - z1;
// Odd part
tmp10 = tmp4 + tmp5; // phase 2
tmp11 = tmp5 + tmp6;
tmp12 = tmp6 + tmp7;
// The rotator is modified from fig 4-8 to avoid extra negations.
z5 = (tmp10 - tmp12) * 0.382683433f; // c6
z2 = tmp10 * 0.541196100f + z5; // c2-c6
z4 = tmp12 * 1.306562965f + z5; // c2+c6
z3 = tmp11 * 0.707106781f; // c4
z11 = tmp7 + z3; // phase 5
z13 = tmp7 - z3;
*d5p = z13 + z2; // phase 6
*d3p = z13 - z2;
*d1p = z11 + z4;
*d7p = z11 - z4;
*d0p = d0; *d2p = d2; *d4p = d4; *d6p = d6;
}
static void stbiw__jpg_calcBits(int val, unsigned short bits[2]) {
int tmp1 = val < 0 ? -val : val;
val = val < 0 ? val-1 : val;
bits[1] = 1;
while(tmp1 >>= 1) {
++bits[1];
}
bits[0] = val & ((1<<bits[1])-1);
}
static int stbiw__jpg_processDU(stbi__write_context *s, int *bitBuf, int *bitCnt, float *CDU, int du_stride, float *fdtbl, int DC, const unsigned short HTDC[256][2], const unsigned short HTAC[256][2]) {
const unsigned short EOB[2] = { HTAC[0x00][0], HTAC[0x00][1] };
const unsigned short M16zeroes[2] = { HTAC[0xF0][0], HTAC[0xF0][1] };
int dataOff, i, j, n, diff, end0pos, x, y;
int DU[64];
// DCT rows
for(dataOff=0, n=du_stride*8; dataOff<n; dataOff+=du_stride) {
stbiw__jpg_DCT(&CDU[dataOff], &CDU[dataOff+1], &CDU[dataOff+2], &CDU[dataOff+3], &CDU[dataOff+4], &CDU[dataOff+5], &CDU[dataOff+6], &CDU[dataOff+7]);
}
// DCT columns
for(dataOff=0; dataOff<8; ++dataOff) {
stbiw__jpg_DCT(&CDU[dataOff], &CDU[dataOff+du_stride], &CDU[dataOff+du_stride*2], &CDU[dataOff+du_stride*3], &CDU[dataOff+du_stride*4],
&CDU[dataOff+du_stride*5], &CDU[dataOff+du_stride*6], &CDU[dataOff+du_stride*7]);
}
// Quantize/descale/zigzag the coefficients
for(y = 0, j=0; y < 8; ++y) {
for(x = 0; x < 8; ++x,++j) {
float v;
i = y*du_stride+x;
v = CDU[i]*fdtbl[j];
// DU[stbiw__jpg_ZigZag[j]] = (int)(v < 0 ? ceilf(v - 0.5f) : floorf(v + 0.5f));
// ceilf() and floorf() are C99, not C89, but I /think/ they're not needed here anyway?
DU[stbiw__jpg_ZigZag[j]] = (int)(v < 0 ? v - 0.5f : v + 0.5f);
}
}
// Encode DC
diff = DU[0] - DC;
if (diff == 0) {
stbiw__jpg_writeBits(s, bitBuf, bitCnt, HTDC[0]);
} else {
unsigned short bits[2];
stbiw__jpg_calcBits(diff, bits);
stbiw__jpg_writeBits(s, bitBuf, bitCnt, HTDC[bits[1]]);
stbiw__jpg_writeBits(s, bitBuf, bitCnt, bits);
}
// Encode ACs
end0pos = 63;
for(; (end0pos>0)&&(DU[end0pos]==0); --end0pos) {
}
// end0pos = first element in reverse order !=0
if(end0pos == 0) {
stbiw__jpg_writeBits(s, bitBuf, bitCnt, EOB);
return DU[0];
}
for(i = 1; i <= end0pos; ++i) {
int startpos = i;
int nrzeroes;
unsigned short bits[2];
for (; DU[i]==0 && i<=end0pos; ++i) {
}
nrzeroes = i-startpos;
if ( nrzeroes >= 16 ) {
int lng = nrzeroes>>4;
int nrmarker;
for (nrmarker=1; nrmarker <= lng; ++nrmarker)
stbiw__jpg_writeBits(s, bitBuf, bitCnt, M16zeroes);
nrzeroes &= 15;
}
stbiw__jpg_calcBits(DU[i], bits);
stbiw__jpg_writeBits(s, bitBuf, bitCnt, HTAC[(nrzeroes<<4)+bits[1]]);
stbiw__jpg_writeBits(s, bitBuf, bitCnt, bits);
}
if(end0pos != 63) {
stbiw__jpg_writeBits(s, bitBuf, bitCnt, EOB);
}
return DU[0];
}
static int stbi_write_jpg_core(stbi__write_context *s, int width, int height, int comp, const void* data, int quality) {
// Constants that don't pollute global namespace
static const unsigned char std_dc_luminance_nrcodes[] = {0,0,1,5,1,1,1,1,1,1,0,0,0,0,0,0,0};
static const unsigned char std_dc_luminance_values[] = {0,1,2,3,4,5,6,7,8,9,10,11};
static const unsigned char std_ac_luminance_nrcodes[] = {0,0,2,1,3,3,2,4,3,5,5,4,4,0,0,1,0x7d};
static const unsigned char std_ac_luminance_values[] = {
0x01,0x02,0x03,0x00,0x04,0x11,0x05,0x12,0x21,0x31,0x41,0x06,0x13,0x51,0x61,0x07,0x22,0x71,0x14,0x32,0x81,0x91,0xa1,0x08,
0x23,0x42,0xb1,0xc1,0x15,0x52,0xd1,0xf0,0x24,0x33,0x62,0x72,0x82,0x09,0x0a,0x16,0x17,0x18,0x19,0x1a,0x25,0x26,0x27,0x28,
0x29,0x2a,0x34,0x35,0x36,0x37,0x38,0x39,0x3a,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4a,0x53,0x54,0x55,0x56,0x57,0x58,0x59,
0x5a,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6a,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7a,0x83,0x84,0x85,0x86,0x87,0x88,0x89,
0x8a,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9a,0xa2,0xa3,0xa4,0xa5,0xa6,0xa7,0xa8,0xa9,0xaa,0xb2,0xb3,0xb4,0xb5,0xb6,
0xb7,0xb8,0xb9,0xba,0xc2,0xc3,0xc4,0xc5,0xc6,0xc7,0xc8,0xc9,0xca,0xd2,0xd3,0xd4,0xd5,0xd6,0xd7,0xd8,0xd9,0xda,0xe1,0xe2,
0xe3,0xe4,0xe5,0xe6,0xe7,0xe8,0xe9,0xea,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,0xf9,0xfa
};
static const unsigned char std_dc_chrominance_nrcodes[] = {0,0,3,1,1,1,1,1,1,1,1,1,0,0,0,0,0};
static const unsigned char std_dc_chrominance_values[] = {0,1,2,3,4,5,6,7,8,9,10,11};
static const unsigned char std_ac_chrominance_nrcodes[] = {0,0,2,1,2,4,4,3,4,7,5,4,4,0,1,2,0x77};
static const unsigned char std_ac_chrominance_values[] = {
0x00,0x01,0x02,0x03,0x11,0x04,0x05,0x21,0x31,0x06,0x12,0x41,0x51,0x07,0x61,0x71,0x13,0x22,0x32,0x81,0x08,0x14,0x42,0x91,
0xa1,0xb1,0xc1,0x09,0x23,0x33,0x52,0xf0,0x15,0x62,0x72,0xd1,0x0a,0x16,0x24,0x34,0xe1,0x25,0xf1,0x17,0x18,0x19,0x1a,0x26,
0x27,0x28,0x29,0x2a,0x35,0x36,0x37,0x38,0x39,0x3a,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4a,0x53,0x54,0x55,0x56,0x57,0x58,
0x59,0x5a,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6a,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7a,0x82,0x83,0x84,0x85,0x86,0x87,
0x88,0x89,0x8a,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9a,0xa2,0xa3,0xa4,0xa5,0xa6,0xa7,0xa8,0xa9,0xaa,0xb2,0xb3,0xb4,
0xb5,0xb6,0xb7,0xb8,0xb9,0xba,0xc2,0xc3,0xc4,0xc5,0xc6,0xc7,0xc8,0xc9,0xca,0xd2,0xd3,0xd4,0xd5,0xd6,0xd7,0xd8,0xd9,0xda,
0xe2,0xe3,0xe4,0xe5,0xe6,0xe7,0xe8,0xe9,0xea,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,0xf9,0xfa
};
// Huffman tables
static const unsigned short YDC_HT[256][2] = { {0,2},{2,3},{3,3},{4,3},{5,3},{6,3},{14,4},{30,5},{62,6},{126,7},{254,8},{510,9}};
static const unsigned short UVDC_HT[256][2] = { {0,2},{1,2},{2,2},{6,3},{14,4},{30,5},{62,6},{126,7},{254,8},{510,9},{1022,10},{2046,11}};
static const unsigned short YAC_HT[256][2] = {
{10,4},{0,2},{1,2},{4,3},{11,4},{26,5},{120,7},{248,8},{1014,10},{65410,16},{65411,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
{12,4},{27,5},{121,7},{502,9},{2038,11},{65412,16},{65413,16},{65414,16},{65415,16},{65416,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
{28,5},{249,8},{1015,10},{4084,12},{65417,16},{65418,16},{65419,16},{65420,16},{65421,16},{65422,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
{58,6},{503,9},{4085,12},{65423,16},{65424,16},{65425,16},{65426,16},{65427,16},{65428,16},{65429,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
{59,6},{1016,10},{65430,16},{65431,16},{65432,16},{65433,16},{65434,16},{65435,16},{65436,16},{65437,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
{122,7},{2039,11},{65438,16},{65439,16},{65440,16},{65441,16},{65442,16},{65443,16},{65444,16},{65445,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
{123,7},{4086,12},{65446,16},{65447,16},{65448,16},{65449,16},{65450,16},{65451,16},{65452,16},{65453,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
{250,8},{4087,12},{65454,16},{65455,16},{65456,16},{65457,16},{65458,16},{65459,16},{65460,16},{65461,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
{504,9},{32704,15},{65462,16},{65463,16},{65464,16},{65465,16},{65466,16},{65467,16},{65468,16},{65469,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
{505,9},{65470,16},{65471,16},{65472,16},{65473,16},{65474,16},{65475,16},{65476,16},{65477,16},{65478,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
{506,9},{65479,16},{65480,16},{65481,16},{65482,16},{65483,16},{65484,16},{65485,16},{65486,16},{65487,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
{1017,10},{65488,16},{65489,16},{65490,16},{65491,16},{65492,16},{65493,16},{65494,16},{65495,16},{65496,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
{1018,10},{65497,16},{65498,16},{65499,16},{65500,16},{65501,16},{65502,16},{65503,16},{65504,16},{65505,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
{2040,11},{65506,16},{65507,16},{65508,16},{65509,16},{65510,16},{65511,16},{65512,16},{65513,16},{65514,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
{65515,16},{65516,16},{65517,16},{65518,16},{65519,16},{65520,16},{65521,16},{65522,16},{65523,16},{65524,16},{0,0},{0,0},{0,0},{0,0},{0,0},
{2041,11},{65525,16},{65526,16},{65527,16},{65528,16},{65529,16},{65530,16},{65531,16},{65532,16},{65533,16},{65534,16},{0,0},{0,0},{0,0},{0,0},{0,0}
};
static const unsigned short UVAC_HT[256][2] = {
{0,2},{1,2},{4,3},{10,4},{24,5},{25,5},{56,6},{120,7},{500,9},{1014,10},{4084,12},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
{11,4},{57,6},{246,8},{501,9},{2038,11},{4085,12},{65416,16},{65417,16},{65418,16},{65419,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
{26,5},{247,8},{1015,10},{4086,12},{32706,15},{65420,16},{65421,16},{65422,16},{65423,16},{65424,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
{27,5},{248,8},{1016,10},{4087,12},{65425,16},{65426,16},{65427,16},{65428,16},{65429,16},{65430,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
{58,6},{502,9},{65431,16},{65432,16},{65433,16},{65434,16},{65435,16},{65436,16},{65437,16},{65438,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
{59,6},{1017,10},{65439,16},{65440,16},{65441,16},{65442,16},{65443,16},{65444,16},{65445,16},{65446,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
{121,7},{2039,11},{65447,16},{65448,16},{65449,16},{65450,16},{65451,16},{65452,16},{65453,16},{65454,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
{122,7},{2040,11},{65455,16},{65456,16},{65457,16},{65458,16},{65459,16},{65460,16},{65461,16},{65462,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
{249,8},{65463,16},{65464,16},{65465,16},{65466,16},{65467,16},{65468,16},{65469,16},{65470,16},{65471,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
{503,9},{65472,16},{65473,16},{65474,16},{65475,16},{65476,16},{65477,16},{65478,16},{65479,16},{65480,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
{504,9},{65481,16},{65482,16},{65483,16},{65484,16},{65485,16},{65486,16},{65487,16},{65488,16},{65489,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
{505,9},{65490,16},{65491,16},{65492,16},{65493,16},{65494,16},{65495,16},{65496,16},{65497,16},{65498,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
{506,9},{65499,16},{65500,16},{65501,16},{65502,16},{65503,16},{65504,16},{65505,16},{65506,16},{65507,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
{2041,11},{65508,16},{65509,16},{65510,16},{65511,16},{65512,16},{65513,16},{65514,16},{65515,16},{65516,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
{16352,14},{65517,16},{65518,16},{65519,16},{65520,16},{65521,16},{65522,16},{65523,16},{65524,16},{65525,16},{0,0},{0,0},{0,0},{0,0},{0,0},
{1018,10},{32707,15},{65526,16},{65527,16},{65528,16},{65529,16},{65530,16},{65531,16},{65532,16},{65533,16},{65534,16},{0,0},{0,0},{0,0},{0,0},{0,0}
};
static const int YQT[] = {16,11,10,16,24,40,51,61,12,12,14,19,26,58,60,55,14,13,16,24,40,57,69,56,14,17,22,29,51,87,80,62,18,22,
37,56,68,109,103,77,24,35,55,64,81,104,113,92,49,64,78,87,103,121,120,101,72,92,95,98,112,100,103,99};
static const int UVQT[] = {17,18,24,47,99,99,99,99,18,21,26,66,99,99,99,99,24,26,56,99,99,99,99,99,47,66,99,99,99,99,99,99,
99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99};
static const float aasf[] = { 1.0f * 2.828427125f, 1.387039845f * 2.828427125f, 1.306562965f * 2.828427125f, 1.175875602f * 2.828427125f,
1.0f * 2.828427125f, 0.785694958f * 2.828427125f, 0.541196100f * 2.828427125f, 0.275899379f * 2.828427125f };
int row, col, i, k, subsample;
float fdtbl_Y[64], fdtbl_UV[64];
unsigned char YTable[64], UVTable[64];
if(!data || !width || !height || comp > 4 || comp < 1) {
return 0;
}
quality = quality ? quality : 90;
subsample = quality <= 90 ? 1 : 0;
quality = quality < 1 ? 1 : quality > 100 ? 100 : quality;
quality = quality < 50 ? 5000 / quality : 200 - quality * 2;
for(i = 0; i < 64; ++i) {
int uvti, yti = (YQT[i]*quality+50)/100;
YTable[stbiw__jpg_ZigZag[i]] = (unsigned char) (yti < 1 ? 1 : yti > 255 ? 255 : yti);
uvti = (UVQT[i]*quality+50)/100;
UVTable[stbiw__jpg_ZigZag[i]] = (unsigned char) (uvti < 1 ? 1 : uvti > 255 ? 255 : uvti);
}
for(row = 0, k = 0; row < 8; ++row) {
for(col = 0; col < 8; ++col, ++k) {
fdtbl_Y[k] = 1 / (YTable [stbiw__jpg_ZigZag[k]] * aasf[row] * aasf[col]);
fdtbl_UV[k] = 1 / (UVTable[stbiw__jpg_ZigZag[k]] * aasf[row] * aasf[col]);
}
}
// Write Headers
{
static const unsigned char head0[] = { 0xFF,0xD8,0xFF,0xE0,0,0x10,'J','F','I','F',0,1,1,0,0,1,0,1,0,0,0xFF,0xDB,0,0x84,0 };
static const unsigned char head2[] = { 0xFF,0xDA,0,0xC,3,1,0,2,0x11,3,0x11,0,0x3F,0 };
const unsigned char head1[] = { 0xFF,0xC0,0,0x11,8,(unsigned char)(height>>8),STBIW_UCHAR(height),(unsigned char)(width>>8),STBIW_UCHAR(width),
3,1,(unsigned char)(subsample?0x22:0x11),0,2,0x11,1,3,0x11,1,0xFF,0xC4,0x01,0xA2,0 };
s->func(s->context, (void*)head0, sizeof(head0));
s->func(s->context, (void*)YTable, sizeof(YTable));
stbiw__putc(s, 1);
s->func(s->context, UVTable, sizeof(UVTable));
s->func(s->context, (void*)head1, sizeof(head1));
s->func(s->context, (void*)(std_dc_luminance_nrcodes+1), sizeof(std_dc_luminance_nrcodes)-1);
s->func(s->context, (void*)std_dc_luminance_values, sizeof(std_dc_luminance_values));
stbiw__putc(s, 0x10); // HTYACinfo
s->func(s->context, (void*)(std_ac_luminance_nrcodes+1), sizeof(std_ac_luminance_nrcodes)-1);
s->func(s->context, (void*)std_ac_luminance_values, sizeof(std_ac_luminance_values));
stbiw__putc(s, 1); // HTUDCinfo
s->func(s->context, (void*)(std_dc_chrominance_nrcodes+1), sizeof(std_dc_chrominance_nrcodes)-1);
s->func(s->context, (void*)std_dc_chrominance_values, sizeof(std_dc_chrominance_values));
stbiw__putc(s, 0x11); // HTUACinfo
s->func(s->context, (void*)(std_ac_chrominance_nrcodes+1), sizeof(std_ac_chrominance_nrcodes)-1);
s->func(s->context, (void*)std_ac_chrominance_values, sizeof(std_ac_chrominance_values));
s->func(s->context, (void*)head2, sizeof(head2));
}
// Encode 8x8 macroblocks
{
static const unsigned short fillBits[] = {0x7F, 7};
int DCY=0, DCU=0, DCV=0;
int bitBuf=0, bitCnt=0;
// comp == 2 is grey+alpha (alpha is ignored)
int ofsG = comp > 2 ? 1 : 0, ofsB = comp > 2 ? 2 : 0;
const unsigned char *dataR = (const unsigned char *)data;
const unsigned char *dataG = dataR + ofsG;
const unsigned char *dataB = dataR + ofsB;
int x, y, pos;
if(subsample) {
for(y = 0; y < height; y += 16) {
for(x = 0; x < width; x += 16) {
float Y[256], U[256], V[256];
for(row = y, pos = 0; row < y+16; ++row) {
// row >= height => use last input row
int clamped_row = (row < height) ? row : height - 1;
int base_p = (stbi__flip_vertically_on_write ? (height-1-clamped_row) : clamped_row)*width*comp;
for(col = x; col < x+16; ++col, ++pos) {
// if col >= width => use pixel from last input column
int p = base_p + ((col < width) ? col : (width-1))*comp;
float r = dataR[p], g = dataG[p], b = dataB[p];
Y[pos]= +0.29900f*r + 0.58700f*g + 0.11400f*b - 128;
U[pos]= -0.16874f*r - 0.33126f*g + 0.50000f*b;
V[pos]= +0.50000f*r - 0.41869f*g - 0.08131f*b;
}
}
DCY = stbiw__jpg_processDU(s, &bitBuf, &bitCnt, Y+0, 16, fdtbl_Y, DCY, YDC_HT, YAC_HT);
DCY = stbiw__jpg_processDU(s, &bitBuf, &bitCnt, Y+8, 16, fdtbl_Y, DCY, YDC_HT, YAC_HT);
DCY = stbiw__jpg_processDU(s, &bitBuf, &bitCnt, Y+128, 16, fdtbl_Y, DCY, YDC_HT, YAC_HT);
DCY = stbiw__jpg_processDU(s, &bitBuf, &bitCnt, Y+136, 16, fdtbl_Y, DCY, YDC_HT, YAC_HT);
// subsample U,V
{
float subU[64], subV[64];
int yy, xx;
for(yy = 0, pos = 0; yy < 8; ++yy) {
for(xx = 0; xx < 8; ++xx, ++pos) {
int j = yy*32+xx*2;
subU[pos] = (U[j+0] + U[j+1] + U[j+16] + U[j+17]) * 0.25f;
subV[pos] = (V[j+0] + V[j+1] + V[j+16] + V[j+17]) * 0.25f;
}
}
DCU = stbiw__jpg_processDU(s, &bitBuf, &bitCnt, subU, 8, fdtbl_UV, DCU, UVDC_HT, UVAC_HT);
DCV = stbiw__jpg_processDU(s, &bitBuf, &bitCnt, subV, 8, fdtbl_UV, DCV, UVDC_HT, UVAC_HT);
}
}
}
} else {
for(y = 0; y < height; y += 8) {
for(x = 0; x < width; x += 8) {
float Y[64], U[64], V[64];
for(row = y, pos = 0; row < y+8; ++row) {
// row >= height => use last input row
int clamped_row = (row < height) ? row : height - 1;
int base_p = (stbi__flip_vertically_on_write ? (height-1-clamped_row) : clamped_row)*width*comp;
for(col = x; col < x+8; ++col, ++pos) {
// if col >= width => use pixel from last input column
int p = base_p + ((col < width) ? col : (width-1))*comp;
float r = dataR[p], g = dataG[p], b = dataB[p];
Y[pos]= +0.29900f*r + 0.58700f*g + 0.11400f*b - 128;
U[pos]= -0.16874f*r - 0.33126f*g + 0.50000f*b;
V[pos]= +0.50000f*r - 0.41869f*g - 0.08131f*b;
}
}
DCY = stbiw__jpg_processDU(s, &bitBuf, &bitCnt, Y, 8, fdtbl_Y, DCY, YDC_HT, YAC_HT);
DCU = stbiw__jpg_processDU(s, &bitBuf, &bitCnt, U, 8, fdtbl_UV, DCU, UVDC_HT, UVAC_HT);
DCV = stbiw__jpg_processDU(s, &bitBuf, &bitCnt, V, 8, fdtbl_UV, DCV, UVDC_HT, UVAC_HT);
}
}
}
// Do the bit alignment of the EOI marker
stbiw__jpg_writeBits(s, &bitBuf, &bitCnt, fillBits);
}
// EOI
stbiw__putc(s, 0xFF);
stbiw__putc(s, 0xD9);
return 1;
}
STBIWDEF int stbi_write_jpg_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data, int quality)
{
stbi__write_context s;
stbi__start_write_callbacks(&s, func, context);
return stbi_write_jpg_core(&s, x, y, comp, (void *) data, quality);
}
#ifndef STBI_WRITE_NO_STDIO
STBIWDEF int stbi_write_jpg(char const *filename, int x, int y, int comp, const void *data, int quality)
{
stbi__write_context s;
if (stbi__start_write_file(&s,filename)) {
int r = stbi_write_jpg_core(&s, x, y, comp, data, quality);
stbi__end_write_file(&s);
return r;
} else
return 0;
}
#endif
#endif // STB_IMAGE_WRITE_IMPLEMENTATION
/* Revision history
1.14 (2020-02-02) updated JPEG writer to downsample chroma channels
1.13
1.12
1.11 (2019-08-11)
1.10 (2019-02-07)
support utf8 filenames in Windows; fix warnings and platform ifdefs
1.09 (2018-02-11)
fix typo in zlib quality API, improve STB_I_W_STATIC in C++
1.08 (2018-01-29)
add stbi__flip_vertically_on_write, external zlib, zlib quality, choose PNG filter
1.07 (2017-07-24)
doc fix
1.06 (2017-07-23)
writing JPEG (using Jon Olick's code)
1.05 ???
1.04 (2017-03-03)
monochrome BMP expansion
1.03 ???
1.02 (2016-04-02)
avoid allocating large structures on the stack
1.01 (2016-01-16)
STBIW_REALLOC_SIZED: support allocators with no realloc support
avoid race-condition in crc initialization
minor compile issues
1.00 (2015-09-14)
installable file IO function
0.99 (2015-09-13)
warning fixes; TGA rle support
0.98 (2015-04-08)
added STBIW_MALLOC, STBIW_ASSERT etc
0.97 (2015-01-18)
fixed HDR asserts, rewrote HDR rle logic
0.96 (2015-01-17)
add HDR output
fix monochrome BMP
0.95 (2014-08-17)
add monochrome TGA output
0.94 (2014-05-31)
rename private functions to avoid conflicts with stb_image.h
0.93 (2014-05-27)
warning fixes
0.92 (2010-08-01)
casts to unsigned char to fix warnings
0.91 (2010-07-17)
first public release
0.90 first internal release
*/
/*
------------------------------------------------------------------------------
This software is available under 2 licenses -- choose whichever you prefer.
------------------------------------------------------------------------------
ALTERNATIVE A - MIT License
Copyright (c) 2017 Sean Barrett
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
------------------------------------------------------------------------------
ALTERNATIVE B - Public Domain (www.unlicense.org)
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
software, either in source code form or as a compiled binary, for any purpose,
commercial or non-commercial, and by any means.
In jurisdictions that recognize copyright laws, the author or authors of this
software dedicate any and all copyright interest in the software to the public
domain. We make this dedication for the benefit of the public at large and to
the detriment of our heirs and successors. We intend this dedication to be an
overt act of relinquishment in perpetuity of all present and future rights to
this software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
------------------------------------------------------------------------------
*/
| h |
oneAPI-samples | data/projects/oneAPI-samples/common/stb/stb.h | /* stb.h - v2.35 - Sean's Tool Box -- public domain -- http://nothings.org/stb.h
no warranty is offered or implied; use this code at your own risk
This is a single header file with a bunch of useful utilities
for getting stuff done in C/C++.
Documentation: http://nothings.org/stb/stb_h.html
Unit tests: http://nothings.org/stb/stb.c
============================================================================
You MUST
#define STB_DEFINE
in EXACTLY _one_ C or C++ file that includes this header, BEFORE the
include, like this:
#define STB_DEFINE
#include "stb.h"
All other files should just #include "stb.h" without the #define.
============================================================================
Version History
2.36 various fixes
2.35 fix clang-cl issues with swprintf
2.34 fix warnings
2.33 more fixes to random numbers
2.32 stb_intcmprev, stb_uidict, fix random numbers on Linux
2.31 stb_ucharcmp
2.30 MinGW fix
2.29 attempt to fix use of swprintf()
2.28 various new functionality
2.27 test _WIN32 not WIN32 in STB_THREADS
2.26 various warning & bugfixes
2.25 various warning & bugfixes
2.24 various warning & bugfixes
2.23 fix 2.22
2.22 64-bit fixes from '!='; fix stb_sdict_copy() to have preferred name
2.21 utf-8 decoder rejects "overlong" encodings; attempted 64-bit improvements
2.20 fix to hash "copy" function--reported by someone with handle "!="
2.19 ???
2.18 stb_readdir_subdirs_mask
2.17 stb_cfg_dir
2.16 fix stb_bgio_, add stb_bgio_stat(); begin a streaming wrapper
2.15 upgraded hash table template to allow:
- aggregate keys (explicit comparison func for EMPTY and DEL keys)
- "static" implementations (so they can be culled if unused)
2.14 stb_mprintf
2.13 reduce identifiable strings in STB_NO_STB_STRINGS
2.12 fix STB_ONLY -- lots of uint32s, TRUE/FALSE things had crept in
2.11 fix bug in stb_dirtree_get() which caused "c://path" sorts of stuff
2.10 STB_F(), STB_I() inline constants (also KI,KU,KF,KD)
2.09 stb_box_face_vertex_axis_side
2.08 bugfix stb_trimwhite()
2.07 colored printing in windows (why are we in 1985?)
2.06 comparison functions are now functions-that-return-functions and
accept a struct-offset as a parameter (not thread-safe)
2.05 compile and pass tests under Linux (but no threads); thread cleanup
2.04 stb_cubic_bezier_1d, smoothstep, avoid dependency on registry
2.03 ?
2.02 remove integrated documentation
2.01 integrate various fixes; stb_force_uniprocessor
2.00 revised stb_dupe to use multiple hashes
1.99 stb_charcmp
1.98 stb_arr_deleten, stb_arr_insertn
1.97 fix stb_newell_normal()
1.96 stb_hash_number()
1.95 hack stb__rec_max; clean up recursion code to use new functions
1.94 stb_dirtree; rename stb_extra to stb_ptrmap
1.93 stb_sem_new() API cleanup (no blockflag-starts blocked; use 'extra')
1.92 stb_threadqueue--multi reader/writer queue, fixed size or resizeable
1.91 stb_bgio_* for reading disk asynchronously
1.90 stb_mutex uses CRITICAL_REGION; new stb_sync primitive for thread
joining; workqueue supports stb_sync instead of stb_semaphore
1.89 support ';' in constant-string wildcards; stb_mutex wrapper (can
implement with EnterCriticalRegion eventually)
1.88 portable threading API (only for win32 so far); worker thread queue
1.87 fix wildcard handling in stb_readdir_recursive
1.86 support ';' in wildcards
1.85 make stb_regex work with non-constant strings;
beginnings of stb_introspect()
1.84 (forgot to make notes)
1.83 whoops, stb_keep_if_different wasn't deleting the temp file
1.82 bring back stb_compress from stb_file.h for cmirror
1.81 various bugfixes, STB_FASTMALLOC_INIT inits FASTMALLOC in release
1.80 stb_readdir returns utf8; write own utf8-utf16 because lib was wrong
1.79 stb_write
1.78 calloc() support for malloc wrapper, STB_FASTMALLOC
1.77 STB_FASTMALLOC
1.76 STB_STUA - Lua-like language; (stb_image, stb_csample, stb_bilinear)
1.75 alloc/free array of blocks; stb_hheap bug; a few stb_ps_ funcs;
hash*getkey, hash*copy; stb_bitset; stb_strnicmp; bugfix stb_bst
1.74 stb_replaceinplace; use stdlib C function to convert utf8 to UTF-16
1.73 fix performance bug & leak in stb_ischar (C++ port lost a 'static')
1.72 remove stb_block, stb_block_manager, stb_decompress (to stb_file.h)
1.71 stb_trimwhite, stb_tokens_nested, etc.
1.70 back out 1.69 because it might problemize mixed builds; stb_filec()
1.69 (stb_file returns 'char *' in C++)
1.68 add a special 'tree root' data type for stb_bst; stb_arr_end
1.67 full C++ port. (stb_block_manager)
1.66 stb_newell_normal
1.65 stb_lex_item_wild -- allow wildcard items which MUST match entirely
1.64 stb_data
1.63 stb_log_name
1.62 stb_define_sort; C++ cleanup
1.61 stb_hash_fast -- Paul Hsieh's hash function (beats Bob Jenkins'?)
1.60 stb_delete_directory_recursive
1.59 stb_readdir_recursive
1.58 stb_bst variant with parent pointer for O(1) iteration, not O(log N)
1.57 replace LCG random with Mersenne Twister (found a public domain one)
1.56 stb_perfect_hash, stb_ischar, stb_regex
1.55 new stb_bst API allows multiple BSTs per node (e.g. secondary keys)
1.54 bugfix: stb_define_hash, stb_wildmatch, regexp
1.53 stb_define_hash; recoded stb_extra, stb_sdict use it
1.52 stb_rand_define, stb_bst, stb_reverse
1.51 fix 'stb_arr_setlen(NULL, 0)'
1.50 stb_wordwrap
1.49 minor improvements to enable the scripting language
1.48 better approach for stb_arr using stb_malloc; more invasive, clearer
1.47 stb_lex (lexes stb.h at 1.5ML/s on 3Ghz P4; 60/70% of optimal/flex)
1.46 stb_wrapper_*, STB_MALLOC_WRAPPER
1.45 lightly tested DFA acceleration of regexp searching
1.44 wildcard matching & searching; regexp matching & searching
1.43 stb_temp
1.42 allow stb_arr to use stb_malloc/realloc; note this is global
1.41 make it compile in C++; (disable stb_arr in C++)
1.40 stb_dupe tweak; stb_swap; stb_substr
1.39 stb_dupe; improve stb_file_max to be less stupid
1.38 stb_sha1_file: generate sha1 for file, even > 4GB
1.37 stb_file_max; partial support for utf8 filenames in Windows
1.36 remove STB__NO_PREFIX - poor interaction with IDE, not worth it
streamline stb_arr to make it separately publishable
1.35 bugfixes for stb_sdict, stb_malloc(0), stristr
1.34 (streaming interfaces for stb_compress)
1.33 stb_alloc; bug in stb_getopt; remove stb_overflow
1.32 (stb_compress returns, smaller&faster; encode window & 64-bit len)
1.31 stb_prefix_count
1.30 (STB__NO_PREFIX - remove stb_ prefixes for personal projects)
1.29 stb_fput_varlen64, etc.
1.28 stb_sha1
1.27 ?
1.26 stb_extra
1.25 ?
1.24 stb_copyfile
1.23 stb_readdir
1.22 ?
1.21 ?
1.20 ?
1.19 ?
1.18 ?
1.17 ?
1.16 ?
1.15 stb_fixpath, stb_splitpath, stb_strchr2
1.14 stb_arr
1.13 ?stb, stb_log, stb_fatal
1.12 ?stb_hash2
1.11 miniML
1.10 stb_crc32, stb_adler32
1.09 stb_sdict
1.08 stb_bitreverse, stb_ispow2, stb_big32
stb_fopen, stb_fput_varlen, stb_fput_ranged
stb_fcmp, stb_feq
1.07 (stb_encompress)
1.06 stb_compress
1.05 stb_tokens, (stb_hheap)
1.04 stb_rand
1.03 ?(s-strings)
1.02 ?stb_filelen, stb_tokens
1.01 stb_tolower
1.00 stb_hash, stb_intcmp
stb_file, stb_stringfile, stb_fgets
stb_prefix, stb_strlower, stb_strtok
stb_image
(stb_array), (stb_arena)
Parenthesized items have since been removed.
LICENSE
See end of file for license information.
CREDITS
Written by Sean Barrett.
Fixes:
Philipp Wiesemann
Robert Nix
r-lyeh
blackpawn
github:Mojofreem
Ryan Whitworth
Vincent Isambart
Mike Sartain
Eugene Opalev
Tim Sjostrand
github:infatum
Dave Butler (Croepha)
Ethan Lee (flibitijibibo)
Brian Collins
*/
#include <stdarg.h>
#ifndef STB__INCLUDE_STB_H
#define STB__INCLUDE_STB_H
#define STB_VERSION 1
#ifdef STB_INTROSPECT
#define STB_DEFINE
#endif
#ifdef STB_DEFINE_THREADS
#ifndef STB_DEFINE
#define STB_DEFINE
#endif
#ifndef STB_THREADS
#define STB_THREADS
#endif
#endif
#if defined(_WIN32) && !defined(__MINGW32__)
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#endif
#ifndef _CRT_NONSTDC_NO_DEPRECATE
#define _CRT_NONSTDC_NO_DEPRECATE
#endif
#ifndef _CRT_NON_CONFORMING_SWPRINTFS
#define _CRT_NON_CONFORMING_SWPRINTFS
#endif
#if !defined(_MSC_VER) || _MSC_VER > 1700
#include <intrin.h> // _BitScanReverse
#endif
#endif
#include <stdlib.h> // stdlib could have min/max
#include <stdio.h> // need FILE
#include <string.h> // stb_define_hash needs memcpy/memset
#include <time.h> // stb_dirtree
#ifdef __MINGW32__
#include <fcntl.h> // O_RDWR
#endif
#ifdef STB_PERSONAL
typedef int Bool;
#define False 0
#define True 1
#endif
#ifdef STB_MALLOC_WRAPPER_PAGED
#define STB_MALLOC_WRAPPER_DEBUG
#endif
#ifdef STB_MALLOC_WRAPPER_DEBUG
#define STB_MALLOC_WRAPPER
#endif
#ifdef STB_MALLOC_WRAPPER_FASTMALLOC
#define STB_FASTMALLOC
#define STB_MALLOC_WRAPPER
#endif
#ifdef STB_FASTMALLOC
#ifndef _WIN32
#undef STB_FASTMALLOC
#endif
#endif
#ifdef STB_DEFINE
#include <assert.h>
#include <stdarg.h>
#include <stddef.h>
#include <ctype.h>
#include <math.h>
#ifndef _WIN32
#include <unistd.h>
#else
#include <io.h> // _mktemp
#include <direct.h> // _rmdir
#endif
#include <sys/types.h> // stat()/_stat()
#include <sys/stat.h> // stat()/_stat()
#endif
#define stb_min(a,b) ((a) < (b) ? (a) : (b))
#define stb_max(a,b) ((a) > (b) ? (a) : (b))
#ifndef STB_ONLY
#if !defined(__cplusplus) && !defined(min) && !defined(max)
#define min(x,y) stb_min(x,y)
#define max(x,y) stb_max(x,y)
#endif
#ifndef M_PI
#define M_PI 3.14159265358979323846f
#endif
#ifndef TRUE
#define TRUE 1
#define FALSE 0
#endif
#ifndef deg2rad
#define deg2rad(a) ((a)*(M_PI/180))
#endif
#ifndef rad2deg
#define rad2deg(a) ((a)*(180/M_PI))
#endif
#ifndef swap
#ifndef __cplusplus
#define swap(TYPE,a,b) \
do { TYPE stb__t; stb__t = (a); (a) = (b); (b) = stb__t; } while (0)
#endif
#endif
typedef unsigned char uint8 ;
typedef signed char int8 ;
typedef unsigned short uint16;
typedef signed short int16;
#if defined(STB_USE_LONG_FOR_32_BIT_INT) || defined(STB_LONG32)
typedef unsigned long uint32;
typedef signed long int32;
#else
typedef unsigned int uint32;
typedef signed int int32;
#endif
typedef unsigned char uchar ;
typedef unsigned short ushort;
typedef unsigned int uint ;
typedef unsigned long ulong ;
// produce compile errors if the sizes aren't right
typedef char stb__testsize16[sizeof(int16)==2];
typedef char stb__testsize32[sizeof(int32)==4];
#endif
#ifndef STB_TRUE
#define STB_TRUE 1
#define STB_FALSE 0
#endif
// if we're STB_ONLY, can't rely on uint32 or even uint, so all the
// variables we'll use herein need typenames prefixed with 'stb':
typedef unsigned char stb_uchar;
typedef unsigned char stb_uint8;
typedef unsigned int stb_uint;
typedef unsigned short stb_uint16;
typedef short stb_int16;
typedef signed char stb_int8;
#if defined(STB_USE_LONG_FOR_32_BIT_INT) || defined(STB_LONG32)
typedef unsigned long stb_uint32;
typedef long stb_int32;
#else
typedef unsigned int stb_uint32;
typedef int stb_int32;
#endif
typedef char stb__testsize2_16[sizeof(stb_uint16)==2 ? 1 : -1];
typedef char stb__testsize2_32[sizeof(stb_uint32)==4 ? 1 : -1];
#ifdef _MSC_VER
typedef unsigned __int64 stb_uint64;
typedef __int64 stb_int64;
#define STB_IMM_UINT64(literalui64) (literalui64##ui64)
#define STB_IMM_INT64(literali64) (literali64##i64)
#else
// ??
typedef unsigned long long stb_uint64;
typedef long long stb_int64;
#define STB_IMM_UINT64(literalui64) (literalui64##ULL)
#define STB_IMM_INT64(literali64) (literali64##LL)
#endif
typedef char stb__testsize2_64[sizeof(stb_uint64)==8 ? 1 : -1];
// add platform-specific ways of checking for sizeof(char*) == 8,
// and make those define STB_PTR64
#if defined(_WIN64) || defined(__x86_64__) || defined(__ia64__) || defined(__LP64__)
#define STB_PTR64
#endif
#ifdef STB_PTR64
typedef char stb__testsize2_ptr[sizeof(char *) == 8];
typedef stb_uint64 stb_uinta;
typedef stb_int64 stb_inta;
#else
typedef char stb__testsize2_ptr[sizeof(char *) == 4];
typedef stb_uint32 stb_uinta;
typedef stb_int32 stb_inta;
#endif
typedef char stb__testsize2_uinta[sizeof(stb_uinta)==sizeof(char*) ? 1 : -1];
// if so, we should define an int type that is the pointer size. until then,
// we'll have to make do with this (which is not the same at all!)
typedef union
{
unsigned int i;
void * p;
} stb_uintptr;
#ifdef __cplusplus
#define STB_EXTERN extern "C"
#else
#define STB_EXTERN extern
#endif
// check for well-known debug defines
#if defined(DEBUG) || defined(_DEBUG) || defined(DBG)
#ifndef NDEBUG
#define STB_DEBUG
#endif
#endif
#ifdef STB_DEBUG
#include <assert.h>
#endif
//////////////////////////////////////////////////////////////////////////////
//
// C library function platform handling
//
#ifdef STB_DEFINE
#if defined(_WIN32) && defined(__STDC_WANT_SECURE_LIB__)
static FILE * stb_p_fopen(const char *filename, const char *mode)
{
FILE *f;
if (0 == fopen_s(&f, filename, mode))
return f;
else
return NULL;
}
static FILE * stb_p_wfopen(const wchar_t *filename, const wchar_t *mode)
{
FILE *f;
if (0 == _wfopen_s(&f, filename, mode))
return f;
else
return NULL;
}
static char *stb_p_strcpy_s(char *a, size_t size, const char *b)
{
strcpy_s(a,size,b);
return a;
}
static char *stb_p_strncpy_s(char *a, size_t size, const char *b, size_t count)
{
strncpy_s(a,size,b,count);
return a;
}
#define stb_p_mktemp(s) (_mktemp_s(s, strlen(s)+1) == 0)
#define stb_p_sprintf sprintf_s
#define stb_p_size(x) ,(x)
#else
#define stb_p_fopen fopen
#define stb_p_wfopen _wfopen
#define stb_p_strcpy_s(a,s,b) strcpy(a,b)
#define stb_p_strncpy_s(a,s,b,c) strncpy(a,b,c)
#define stb_p_mktemp(s) (mktemp(s) != NULL)
#define stb_p_sprintf sprintf
#define stb_p_size(x)
#endif
#if defined(_WIN32)
#define stb_p_vsnprintf _vsnprintf
#else
#define stb_p_vsnprintf vsnprintf
#endif
#endif // STB_DEFINE
#if defined(_WIN32) && (_MSC_VER >= 1300)
#define stb_p_stricmp _stricmp
#define stb_p_strnicmp _strnicmp
#define stb_p_strdup _strdup
#else
#define stb_p_strdup strdup
#define stb_p_stricmp stricmp
#define stb_p_strnicmp strnicmp
#endif
STB_EXTERN void stb_wrapper_malloc(void *newp, size_t sz, char *file, int line);
STB_EXTERN void stb_wrapper_free(void *oldp, char *file, int line);
STB_EXTERN void stb_wrapper_realloc(void *oldp, void *newp, size_t sz, char *file, int line);
STB_EXTERN void stb_wrapper_calloc(size_t num, size_t sz, char *file, int line);
STB_EXTERN void stb_wrapper_listall(void (*func)(void *ptr, size_t sz, char *file, int line));
STB_EXTERN void stb_wrapper_dump(char *filename);
STB_EXTERN size_t stb_wrapper_allocsize(void *oldp);
STB_EXTERN void stb_wrapper_check(void *oldp);
#ifdef STB_DEFINE
// this is a special function used inside malloc wrapper
// to do allocations that aren't tracked (to avoid
// reentrancy). Of course if someone _else_ wraps realloc,
// this breaks, but if they're doing that AND the malloc
// wrapper they need to explicitly check for reentrancy.
//
// only define realloc_raw() and we do realloc(NULL,sz)
// for malloc() and realloc(p,0) for free().
static void * stb__realloc_raw(void *p, int sz)
{
if (p == NULL) return malloc(sz);
if (sz == 0) { free(p); return NULL; }
return realloc(p,sz);
}
#endif
#ifdef _WIN32
STB_EXTERN void * stb_smalloc(size_t sz);
STB_EXTERN void stb_sfree(void *p);
STB_EXTERN void * stb_srealloc(void *p, size_t sz);
STB_EXTERN void * stb_scalloc(size_t n, size_t sz);
STB_EXTERN char * stb_sstrdup(char *s);
#endif
#ifdef STB_FASTMALLOC
#define malloc stb_smalloc
#define free stb_sfree
#define realloc stb_srealloc
#define strdup stb_sstrdup
#define calloc stb_scalloc
#endif
#ifndef STB_MALLOC_ALLCHECK
#define stb__check(p) 1
#else
#ifndef STB_MALLOC_WRAPPER
#error STB_MALLOC_ALLCHECK requires STB_MALLOC_WRAPPER
#else
#define stb__check(p) stb_mcheck(p)
#endif
#endif
#ifdef STB_MALLOC_WRAPPER
STB_EXTERN void * stb__malloc(size_t, char *, int);
STB_EXTERN void * stb__realloc(void *, size_t, char *, int);
STB_EXTERN void * stb__calloc(size_t n, size_t s, char *, int);
STB_EXTERN void stb__free(void *, char *file, int);
STB_EXTERN char * stb__strdup(char *s, char *file, int);
STB_EXTERN void stb_malloc_checkall(void);
STB_EXTERN void stb_malloc_check_counter(int init_delay, int rep_delay);
#ifndef STB_MALLOC_WRAPPER_DEBUG
#define stb_mcheck(p) 1
#else
STB_EXTERN int stb_mcheck(void *);
#endif
#ifdef STB_DEFINE
#ifdef STB_MALLOC_WRAPPER_DEBUG
#define STB__PAD 32
#define STB__BIAS 16
#define STB__SIG 0x51b01234
#define STB__FIXSIZE(sz) (((sz+3) & ~3) + STB__PAD)
#define STB__ptr(x,y) ((char *) (x) + (y))
#else
#define STB__ptr(x,y) (x)
#define STB__FIXSIZE(sz) (sz)
#endif
#ifdef STB_MALLOC_WRAPPER_DEBUG
int stb_mcheck(void *p)
{
unsigned int sz;
if (p == NULL) return 1;
p = ((char *) p) - STB__BIAS;
sz = * (unsigned int *) p;
assert(* (unsigned int *) STB__ptr(p,4) == STB__SIG);
assert(* (unsigned int *) STB__ptr(p,8) == STB__SIG);
assert(* (unsigned int *) STB__ptr(p,12) == STB__SIG);
assert(* (unsigned int *) STB__ptr(p,sz-4) == STB__SIG+1);
assert(* (unsigned int *) STB__ptr(p,sz-8) == STB__SIG+1);
assert(* (unsigned int *) STB__ptr(p,sz-12) == STB__SIG+1);
assert(* (unsigned int *) STB__ptr(p,sz-16) == STB__SIG+1);
stb_wrapper_check(STB__ptr(p, STB__BIAS));
return 1;
}
static void stb__check2(void *p, size_t sz, char *file, int line)
{
stb_mcheck(p);
}
void stb_malloc_checkall(void)
{
stb_wrapper_listall(stb__check2);
}
#else
void stb_malloc_checkall(void) { }
#endif
static int stb__malloc_wait=(1 << 30), stb__malloc_next_wait = (1 << 30), stb__malloc_iter;
void stb_malloc_check_counter(int init_delay, int rep_delay)
{
stb__malloc_wait = init_delay;
stb__malloc_next_wait = rep_delay;
}
void stb_mcheck_all(void)
{
#ifdef STB_MALLOC_WRAPPER_DEBUG
++stb__malloc_iter;
if (--stb__malloc_wait <= 0) {
stb_malloc_checkall();
stb__malloc_wait = stb__malloc_next_wait;
}
#endif
}
#ifdef STB_MALLOC_WRAPPER_PAGED
#define STB__WINDOWS_PAGE (1 << 12)
#ifndef _WINDOWS_
STB_EXTERN __declspec(dllimport) void * __stdcall VirtualAlloc(void *p, unsigned long size, unsigned long type, unsigned long protect);
STB_EXTERN __declspec(dllimport) int __stdcall VirtualFree(void *p, unsigned long size, unsigned long freetype);
#endif
#endif
static void *stb__malloc_final(size_t sz)
{
#ifdef STB_MALLOC_WRAPPER_PAGED
size_t aligned = (sz + STB__WINDOWS_PAGE - 1) & ~(STB__WINDOWS_PAGE-1);
char *p = VirtualAlloc(NULL, aligned + STB__WINDOWS_PAGE, 0x2000, 0x04); // RESERVE, READWRITE
if (p == NULL) return p;
VirtualAlloc(p, aligned, 0x1000, 0x04); // COMMIT, READWRITE
return p;
#else
return malloc(sz);
#endif
}
static void stb__free_final(void *p)
{
#ifdef STB_MALLOC_WRAPPER_PAGED
VirtualFree(p, 0, 0x8000); // RELEASE
#else
free(p);
#endif
}
int stb__malloc_failure;
#ifdef STB_MALLOC_WRAPPER_PAGED
static void *stb__realloc_final(void *p, size_t sz, size_t old_sz)
{
void *q = stb__malloc_final(sz);
if (q == NULL)
return ++stb__malloc_failure, q;
// @TODO: deal with p being smaller!
memcpy(q, p, sz < old_sz ? sz : old_sz);
stb__free_final(p);
return q;
}
#endif
void stb__free(void *p, char *file, int line)
{
stb_mcheck_all();
if (!p) return;
#ifdef STB_MALLOC_WRAPPER_DEBUG
stb_mcheck(p);
#endif
stb_wrapper_free(p,file,line);
#ifdef STB_MALLOC_WRAPPER_DEBUG
p = STB__ptr(p,-STB__BIAS);
* (unsigned int *) STB__ptr(p,0) = 0xdeadbeef;
* (unsigned int *) STB__ptr(p,4) = 0xdeadbeef;
* (unsigned int *) STB__ptr(p,8) = 0xdeadbeef;
* (unsigned int *) STB__ptr(p,12) = 0xdeadbeef;
#endif
stb__free_final(p);
}
void * stb__malloc(size_t sz, char *file, int line)
{
void *p;
stb_mcheck_all();
if (sz == 0) return NULL;
p = stb__malloc_final(STB__FIXSIZE(sz));
if (p == NULL) p = stb__malloc_final(STB__FIXSIZE(sz));
if (p == NULL) p = stb__malloc_final(STB__FIXSIZE(sz));
if (p == NULL) {
++stb__malloc_failure;
#ifdef STB_MALLOC_WRAPPER_DEBUG
stb_malloc_checkall();
#endif
return p;
}
#ifdef STB_MALLOC_WRAPPER_DEBUG
* (int *) STB__ptr(p,0) = STB__FIXSIZE(sz);
* (unsigned int *) STB__ptr(p,4) = STB__SIG;
* (unsigned int *) STB__ptr(p,8) = STB__SIG;
* (unsigned int *) STB__ptr(p,12) = STB__SIG;
* (unsigned int *) STB__ptr(p,STB__FIXSIZE(sz)-4) = STB__SIG+1;
* (unsigned int *) STB__ptr(p,STB__FIXSIZE(sz)-8) = STB__SIG+1;
* (unsigned int *) STB__ptr(p,STB__FIXSIZE(sz)-12) = STB__SIG+1;
* (unsigned int *) STB__ptr(p,STB__FIXSIZE(sz)-16) = STB__SIG+1;
p = STB__ptr(p, STB__BIAS);
#endif
stb_wrapper_malloc(p,sz,file,line);
return p;
}
void * stb__realloc(void *p, size_t sz, char *file, int line)
{
void *q;
stb_mcheck_all();
if (p == NULL) return stb__malloc(sz,file,line);
if (sz == 0 ) { stb__free(p,file,line); return NULL; }
#ifdef STB_MALLOC_WRAPPER_DEBUG
stb_mcheck(p);
p = STB__ptr(p,-STB__BIAS);
#endif
#ifdef STB_MALLOC_WRAPPER_PAGED
{
size_t n = stb_wrapper_allocsize(STB__ptr(p,STB__BIAS));
if (!n)
stb_wrapper_check(STB__ptr(p,STB__BIAS));
q = stb__realloc_final(p, STB__FIXSIZE(sz), STB__FIXSIZE(n));
}
#else
q = realloc(p, STB__FIXSIZE(sz));
#endif
if (q == NULL)
return ++stb__malloc_failure, q;
#ifdef STB_MALLOC_WRAPPER_DEBUG
* (int *) STB__ptr(q,0) = STB__FIXSIZE(sz);
* (unsigned int *) STB__ptr(q,4) = STB__SIG;
* (unsigned int *) STB__ptr(q,8) = STB__SIG;
* (unsigned int *) STB__ptr(q,12) = STB__SIG;
* (unsigned int *) STB__ptr(q,STB__FIXSIZE(sz)-4) = STB__SIG+1;
* (unsigned int *) STB__ptr(q,STB__FIXSIZE(sz)-8) = STB__SIG+1;
* (unsigned int *) STB__ptr(q,STB__FIXSIZE(sz)-12) = STB__SIG+1;
* (unsigned int *) STB__ptr(q,STB__FIXSIZE(sz)-16) = STB__SIG+1;
q = STB__ptr(q, STB__BIAS);
p = STB__ptr(p, STB__BIAS);
#endif
stb_wrapper_realloc(p,q,sz,file,line);
return q;
}
STB_EXTERN int stb_log2_ceil(size_t);
static void *stb__calloc(size_t n, size_t sz, char *file, int line)
{
void *q;
stb_mcheck_all();
if (n == 0 || sz == 0) return NULL;
if (stb_log2_ceil(n) + stb_log2_ceil(sz) >= 32) return NULL;
q = stb__malloc(n*sz, file, line);
if (q) memset(q, 0, n*sz);
return q;
}
char * stb__strdup(char *s, char *file, int line)
{
char *p;
stb_mcheck_all();
p = stb__malloc(strlen(s)+1, file, line);
if (!p) return p;
stb_p_strcpy_s(p, strlen(s)+1, s);
return p;
}
#endif // STB_DEFINE
#ifdef STB_FASTMALLOC
#undef malloc
#undef realloc
#undef free
#undef strdup
#undef calloc
#endif
// include everything that might define these, BEFORE making macros
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#define malloc(s) stb__malloc ( s, __FILE__, __LINE__)
#define realloc(p,s) stb__realloc(p,s, __FILE__, __LINE__)
#define calloc(n,s) stb__calloc (n,s, __FILE__, __LINE__)
#define free(p) stb__free (p, __FILE__, __LINE__)
#define strdup(p) stb__strdup (p, __FILE__, __LINE__)
#endif
//////////////////////////////////////////////////////////////////////////////
//
// Windows pretty display
//
STB_EXTERN void stbprint(const char *fmt, ...);
STB_EXTERN char *stb_sprintf(const char *fmt, ...);
STB_EXTERN char *stb_mprintf(const char *fmt, ...);
STB_EXTERN int stb_snprintf(char *s, size_t n, const char *fmt, ...);
STB_EXTERN int stb_vsnprintf(char *s, size_t n, const char *fmt, va_list v);
#ifdef STB_DEFINE
int stb_vsnprintf(char *s, size_t n, const char *fmt, va_list v)
{
int res;
#ifdef _WIN32
#ifdef __STDC_WANT_SECURE_LIB__
res = _vsnprintf_s(s, n, _TRUNCATE, fmt, v);
#else
res = stb_p_vsnprintf(s,n,fmt,v);
#endif
#else
res = vsnprintf(s,n,fmt,v);
#endif
if (n) s[n-1] = 0;
// Unix returns length output would require, Windows returns negative when truncated.
return (res >= (int) n || res < 0) ? -1 : res;
}
int stb_snprintf(char *s, size_t n, const char *fmt, ...)
{
int res;
va_list v;
va_start(v,fmt);
res = stb_vsnprintf(s, n, fmt, v);
va_end(v);
return res;
}
char *stb_sprintf(const char *fmt, ...)
{
static char buffer[1024];
va_list v;
va_start(v,fmt);
stb_vsnprintf(buffer,1024,fmt,v);
va_end(v);
return buffer;
}
char *stb_mprintf(const char *fmt, ...)
{
static char buffer[1024];
va_list v;
va_start(v,fmt);
stb_vsnprintf(buffer,1024,fmt,v);
va_end(v);
return stb_p_strdup(buffer);
}
#ifdef _WIN32
#ifndef _WINDOWS_
STB_EXTERN __declspec(dllimport) int __stdcall WriteConsoleA(void *, const void *, unsigned int, unsigned int *, void *);
STB_EXTERN __declspec(dllimport) void * __stdcall GetStdHandle(unsigned int);
STB_EXTERN __declspec(dllimport) int __stdcall SetConsoleTextAttribute(void *, unsigned short);
#endif
static void stb__print_one(void *handle, char *s, ptrdiff_t len)
{
if (len)
if (0==WriteConsoleA(handle, s, (unsigned) len, NULL,NULL))
// if it fails, maybe redirected, so output normally...
// but it's supriously reporting failure now on Win7 and later
{}//fwrite(s, 1, (unsigned) len, stdout);
}
static void stb__print(char *s)
{
void *handle = GetStdHandle((unsigned int) -11); // STD_OUTPUT_HANDLE
int pad=0; // number of padding characters to add
char *t = s;
while (*s) {
int lpad;
while (*s && *s != '{') {
if (pad) {
if (*s == '\r' || *s == '\n')
pad = 0;
else if (s[0] == ' ' && s[1] == ' ') {
stb__print_one(handle, t, s-t);
t = s;
while (pad) {
stb__print_one(handle, t, 1);
--pad;
}
}
}
++s;
}
if (!*s) break;
stb__print_one(handle, t, s-t);
if (s[1] == '{') {
++s;
continue;
}
if (s[1] == '#') {
t = s+3;
if (isxdigit(s[2]))
if (isdigit(s[2]))
SetConsoleTextAttribute(handle, s[2] - '0');
else
SetConsoleTextAttribute(handle, tolower(s[2]) - 'a' + 10);
else {
SetConsoleTextAttribute(handle, 0x0f);
t=s+2;
}
} else if (s[1] == '!') {
SetConsoleTextAttribute(handle, 0x0c);
t = s+2;
} else if (s[1] == '@') {
SetConsoleTextAttribute(handle, 0x09);
t = s+2;
} else if (s[1] == '$') {
SetConsoleTextAttribute(handle, 0x0a);
t = s+2;
} else {
SetConsoleTextAttribute(handle, 0x08); // 0,7,8,15 => shades of grey
t = s+1;
}
lpad = (int) (t-s);
s = t;
while (*s && *s != '}') ++s;
if (!*s) break;
stb__print_one(handle, t, s-t);
if (s[1] == '}') {
t = s+2;
} else {
pad += 1+lpad;
t = s+1;
}
s=t;
SetConsoleTextAttribute(handle, 0x07);
}
stb__print_one(handle, t, s-t);
SetConsoleTextAttribute(handle, 0x07);
}
void stbprint(const char *fmt, ...)
{
int res;
char buffer[1024];
char *tbuf = buffer;
va_list v;
va_start(v,fmt);
res = stb_vsnprintf(buffer, sizeof(buffer), fmt, v);
va_end(v);
if (res < 0) {
tbuf = (char *) malloc(16384);
va_start(v,fmt);
res = stb_vsnprintf(tbuf,16384, fmt, v);
va_end(v);
tbuf[16383] = 0;
}
stb__print(tbuf);
if (tbuf != buffer)
free(tbuf);
}
#else // _WIN32
void stbprint(const char *fmt, ...)
{
va_list v;
va_start(v,fmt);
vprintf(fmt,v);
va_end(v);
}
#endif // _WIN32
#endif // STB_DEFINE
//////////////////////////////////////////////////////////////////////////////
//
// Windows UTF8 filename handling
//
// Windows stupidly treats 8-bit filenames as some dopey code page,
// rather than utf-8. If we want to use utf8 filenames, we have to
// convert them to WCHAR explicitly and call WCHAR versions of the
// file functions. So, ok, we do.
#ifdef _WIN32
#define stb__fopen(x,y) stb_p_wfopen((const wchar_t *)stb__from_utf8(x), (const wchar_t *)stb__from_utf8_alt(y))
#define stb__windows(x,y) x
#else
#define stb__fopen(x,y) stb_p_fopen(x,y)
#define stb__windows(x,y) y
#endif
typedef unsigned short stb__wchar;
STB_EXTERN stb__wchar * stb_from_utf8(stb__wchar *buffer, const char *str, int n);
STB_EXTERN char * stb_to_utf8 (char *buffer, const stb__wchar *str, int n);
STB_EXTERN stb__wchar *stb__from_utf8(const char *str);
STB_EXTERN stb__wchar *stb__from_utf8_alt(const char *str);
STB_EXTERN char *stb__to_utf8(const stb__wchar *str);
#ifdef STB_DEFINE
stb__wchar * stb_from_utf8(stb__wchar *buffer, const char *ostr, int n)
{
unsigned char *str = (unsigned char *) ostr;
stb_uint32 c;
int i=0;
--n;
while (*str) {
if (i >= n)
return NULL;
if (!(*str & 0x80))
buffer[i++] = *str++;
else if ((*str & 0xe0) == 0xc0) {
if (*str < 0xc2) return NULL;
c = (*str++ & 0x1f) << 6;
if ((*str & 0xc0) != 0x80) return NULL;
buffer[i++] = c + (*str++ & 0x3f);
} else if ((*str & 0xf0) == 0xe0) {
if (*str == 0xe0 && (str[1] < 0xa0 || str[1] > 0xbf)) return NULL;
if (*str == 0xed && str[1] > 0x9f) return NULL; // str[1] < 0x80 is checked below
c = (*str++ & 0x0f) << 12;
if ((*str & 0xc0) != 0x80) return NULL;
c += (*str++ & 0x3f) << 6;
if ((*str & 0xc0) != 0x80) return NULL;
buffer[i++] = c + (*str++ & 0x3f);
} else if ((*str & 0xf8) == 0xf0) {
if (*str > 0xf4) return NULL;
if (*str == 0xf0 && (str[1] < 0x90 || str[1] > 0xbf)) return NULL;
if (*str == 0xf4 && str[1] > 0x8f) return NULL; // str[1] < 0x80 is checked below
c = (*str++ & 0x07) << 18;
if ((*str & 0xc0) != 0x80) return NULL;
c += (*str++ & 0x3f) << 12;
if ((*str & 0xc0) != 0x80) return NULL;
c += (*str++ & 0x3f) << 6;
if ((*str & 0xc0) != 0x80) return NULL;
c += (*str++ & 0x3f);
// utf-8 encodings of values used in surrogate pairs are invalid
if ((c & 0xFFFFF800) == 0xD800) return NULL;
if (c >= 0x10000) {
c -= 0x10000;
if (i + 2 > n) return NULL;
buffer[i++] = 0xD800 | (0x3ff & (c >> 10));
buffer[i++] = 0xDC00 | (0x3ff & (c ));
}
} else
return NULL;
}
buffer[i] = 0;
return buffer;
}
char * stb_to_utf8(char *buffer, const stb__wchar *str, int n)
{
int i=0;
--n;
while (*str) {
if (*str < 0x80) {
if (i+1 > n) return NULL;
buffer[i++] = (char) *str++;
} else if (*str < 0x800) {
if (i+2 > n) return NULL;
buffer[i++] = 0xc0 + (*str >> 6);
buffer[i++] = 0x80 + (*str & 0x3f);
str += 1;
} else if (*str >= 0xd800 && *str < 0xdc00) {
stb_uint32 c;
if (i+4 > n) return NULL;
c = ((str[0] - 0xd800) << 10) + ((str[1]) - 0xdc00) + 0x10000;
buffer[i++] = 0xf0 + (c >> 18);
buffer[i++] = 0x80 + ((c >> 12) & 0x3f);
buffer[i++] = 0x80 + ((c >> 6) & 0x3f);
buffer[i++] = 0x80 + ((c ) & 0x3f);
str += 2;
} else if (*str >= 0xdc00 && *str < 0xe000) {
return NULL;
} else {
if (i+3 > n) return NULL;
buffer[i++] = 0xe0 + (*str >> 12);
buffer[i++] = 0x80 + ((*str >> 6) & 0x3f);
buffer[i++] = 0x80 + ((*str ) & 0x3f);
str += 1;
}
}
buffer[i] = 0;
return buffer;
}
stb__wchar *stb__from_utf8(const char *str)
{
static stb__wchar buffer[4096];
return stb_from_utf8(buffer, str, 4096);
}
stb__wchar *stb__from_utf8_alt(const char *str)
{
static stb__wchar buffer[4096];
return stb_from_utf8(buffer, str, 4096);
}
char *stb__to_utf8(const stb__wchar *str)
{
static char buffer[4096];
return stb_to_utf8(buffer, str, 4096);
}
#endif
//////////////////////////////////////////////////////////////////////////////
//
// Miscellany
//
STB_EXTERN void stb_fatal(const char *fmt, ...);
STB_EXTERN void stb_(char *fmt, ...);
STB_EXTERN void stb_append_to_file(char *file, char *fmt, ...);
STB_EXTERN void stb_log(int active);
STB_EXTERN void stb_log_fileline(int active);
STB_EXTERN void stb_log_name(char *filename);
STB_EXTERN void stb_swap(void *p, void *q, size_t sz);
STB_EXTERN void *stb_copy(void *p, size_t sz);
STB_EXTERN void stb_pointer_array_free(void *p, int len);
STB_EXTERN void **stb_array_block_alloc(int count, int blocksize);
#define stb_arrcount(x) (sizeof(x)/sizeof((x)[0]))
STB_EXTERN int stb__record_fileline(const char *f, int n);
#ifdef STB_DEFINE
static char *stb__file;
static int stb__line;
int stb__record_fileline(const char *f, int n)
{
stb__file = (char*) f;
stb__line = n;
return 0;
}
void stb_fatal(const char *s, ...)
{
va_list a;
if (stb__file)
fprintf(stderr, "[%s:%d] ", stb__file, stb__line);
va_start(a,s);
fputs("Fatal error: ", stderr);
vfprintf(stderr, s, a);
va_end(a);
fputs("\n", stderr);
#ifdef STB_DEBUG
#ifdef _MSC_VER
#ifndef STB_PTR64
__asm int 3; // trap to debugger!
#else
__debugbreak();
#endif
#else
__builtin_trap();
#endif
#endif
exit(1);
}
static int stb__log_active=1, stb__log_fileline=1;
void stb_log(int active)
{
stb__log_active = active;
}
void stb_log_fileline(int active)
{
stb__log_fileline = active;
}
#ifdef STB_NO_STB_STRINGS
const char *stb__log_filename = "temp.log";
#else
const char *stb__log_filename = "stb.log";
#endif
void stb_log_name(char *s)
{
stb__log_filename = s;
}
void stb_(char *s, ...)
{
if (stb__log_active) {
FILE *f = stb_p_fopen(stb__log_filename, "a");
if (f) {
va_list a;
if (stb__log_fileline && stb__file)
fprintf(f, "[%s:%4d] ", stb__file, stb__line);
va_start(a,s);
vfprintf(f, s, a);
va_end(a);
fputs("\n", f);
fclose(f);
}
}
}
void stb_append_to_file(char *filename, char *s, ...)
{
FILE *f = stb_p_fopen(filename, "a");
if (f) {
va_list a;
va_start(a,s);
vfprintf(f, s, a);
va_end(a);
fputs("\n", f);
fclose(f);
}
}
typedef struct { char d[4]; } stb__4;
typedef struct { char d[8]; } stb__8;
// optimize the small cases, though you shouldn't be calling this for those!
void stb_swap(void *p, void *q, size_t sz)
{
char buffer[256];
if (p == q) return;
if (sz == 4) {
stb__4 temp = * ( stb__4 *) p;
* (stb__4 *) p = * ( stb__4 *) q;
* (stb__4 *) q = temp;
return;
} else if (sz == 8) {
stb__8 temp = * ( stb__8 *) p;
* (stb__8 *) p = * ( stb__8 *) q;
* (stb__8 *) q = temp;
return;
}
while (sz > sizeof(buffer)) {
stb_swap(p, q, sizeof(buffer));
p = (char *) p + sizeof(buffer);
q = (char *) q + sizeof(buffer);
sz -= sizeof(buffer);
}
memcpy(buffer, p , sz);
memcpy(p , q , sz);
memcpy(q , buffer, sz);
}
void *stb_copy(void *p, size_t sz)
{
void *q = malloc(sz);
memcpy(q, p, sz);
return q;
}
void stb_pointer_array_free(void *q, int len)
{
void **p = (void **) q;
int i;
for (i=0; i < len; ++i)
free(p[i]);
}
void **stb_array_block_alloc(int count, int blocksize)
{
int i;
char *p = (char *) malloc(sizeof(void *) * count + count * blocksize);
void **q;
if (p == NULL) return NULL;
q = (void **) p;
p += sizeof(void *) * count;
for (i=0; i < count; ++i)
q[i] = p + i * blocksize;
return q;
}
#endif
#ifdef STB_DEBUG
// tricky hack to allow recording FILE,LINE even in varargs functions
#define STB__RECORD_FILE(x) (stb__record_fileline(__FILE__, __LINE__),(x))
#define stb_log STB__RECORD_FILE(stb_log)
#define stb_ STB__RECORD_FILE(stb_)
#ifndef STB_FATAL_CLEAN
#define stb_fatal STB__RECORD_FILE(stb_fatal)
#endif
#define STB__DEBUG(x) x
#else
#define STB__DEBUG(x)
#endif
//////////////////////////////////////////////////////////////////////////////
//
// stb_temp
//
#define stb_temp(block, sz) stb__temp(block, sizeof(block), (sz))
STB_EXTERN void * stb__temp(void *b, int b_sz, int want_sz);
STB_EXTERN void stb_tempfree(void *block, void *ptr);
#ifdef STB_DEFINE
void * stb__temp(void *b, int b_sz, int want_sz)
{
if (b_sz >= want_sz)
return b;
else
return malloc(want_sz);
}
void stb_tempfree(void *b, void *p)
{
if (p != b)
free(p);
}
#endif
//////////////////////////////////////////////////////////////////////////////
//
// math/sampling operations
//
#define stb_lerp(t,a,b) ( (a) + (t) * (float) ((b)-(a)) )
#define stb_unlerp(t,a,b) ( ((t) - (a)) / (float) ((b) - (a)) )
#define stb_clamp(x,xmin,xmax) ((x) < (xmin) ? (xmin) : (x) > (xmax) ? (xmax) : (x))
STB_EXTERN void stb_newell_normal(float *normal, int num_vert, float **vert, int normalize);
STB_EXTERN int stb_box_face_vertex_axis_side(int face_number, int vertex_number, int axis);
STB_EXTERN void stb_linear_controller(float *curpos, float target_pos, float acc, float deacc, float dt);
STB_EXTERN int stb_float_eq(float x, float y, float delta, int max_ulps);
STB_EXTERN int stb_is_prime(unsigned int m);
STB_EXTERN unsigned int stb_power_of_two_nearest_prime(int n);
STB_EXTERN float stb_smoothstep(float t);
STB_EXTERN float stb_cubic_bezier_1d(float t, float p0, float p1, float p2, float p3);
STB_EXTERN double stb_linear_remap(double x, double a, double b,
double c, double d);
#ifdef STB_DEFINE
float stb_smoothstep(float t)
{
return (3 - 2*t)*(t*t);
}
float stb_cubic_bezier_1d(float t, float p0, float p1, float p2, float p3)
{
float it = 1-t;
return it*it*it*p0 + 3*it*it*t*p1 + 3*it*t*t*p2 + t*t*t*p3;
}
void stb_newell_normal(float *normal, int num_vert, float **vert, int normalize)
{
int i,j;
float p;
normal[0] = normal[1] = normal[2] = 0;
for (i=num_vert-1,j=0; j < num_vert; i=j++) {
float *u = vert[i];
float *v = vert[j];
normal[0] += (u[1] - v[1]) * (u[2] + v[2]);
normal[1] += (u[2] - v[2]) * (u[0] + v[0]);
normal[2] += (u[0] - v[0]) * (u[1] + v[1]);
}
if (normalize) {
p = normal[0]*normal[0] + normal[1]*normal[1] + normal[2]*normal[2];
p = (float) (1.0 / sqrt(p));
normal[0] *= p;
normal[1] *= p;
normal[2] *= p;
}
}
int stb_box_face_vertex_axis_side(int face_number, int vertex_number, int axis)
{
static int box_vertices[6][4][3] =
{
{ { 1,1,1 }, { 1,0,1 }, { 1,0,0 }, { 1,1,0 } },
{ { 0,0,0 }, { 0,0,1 }, { 0,1,1 }, { 0,1,0 } },
{ { 0,0,0 }, { 0,1,0 }, { 1,1,0 }, { 1,0,0 } },
{ { 0,0,0 }, { 1,0,0 }, { 1,0,1 }, { 0,0,1 } },
{ { 1,1,1 }, { 0,1,1 }, { 0,0,1 }, { 1,0,1 } },
{ { 1,1,1 }, { 1,1,0 }, { 0,1,0 }, { 0,1,1 } },
};
assert(face_number >= 0 && face_number < 6);
assert(vertex_number >= 0 && vertex_number < 4);
assert(axis >= 0 && axis < 3);
return box_vertices[face_number][vertex_number][axis];
}
void stb_linear_controller(float *curpos, float target_pos, float acc, float deacc, float dt)
{
float sign = 1, p, cp = *curpos;
if (cp == target_pos) return;
if (target_pos < cp) {
target_pos = -target_pos;
cp = -cp;
sign = -1;
}
// first decelerate
if (cp < 0) {
p = cp + deacc * dt;
if (p > 0) {
p = 0;
dt = dt - cp / deacc;
if (dt < 0) dt = 0;
} else {
dt = 0;
}
cp = p;
}
// now accelerate
p = cp + acc*dt;
if (p > target_pos) p = target_pos;
*curpos = p * sign;
// @TODO: testing
}
float stb_quadratic_controller(float target_pos, float curpos, float maxvel, float maxacc, float dt, float *curvel)
{
return 0; // @TODO
}
int stb_float_eq(float x, float y, float delta, int max_ulps)
{
if (fabs(x-y) <= delta) return 1;
if (abs(*(int *)&x - *(int *)&y) <= max_ulps) return 1;
return 0;
}
int stb_is_prime(unsigned int m)
{
unsigned int i,j;
if (m < 2) return 0;
if (m == 2) return 1;
if (!(m & 1)) return 0;
if (m % 3 == 0) return (m == 3);
for (i=5; (j=i*i), j <= m && j > i; i += 6) {
if (m % i == 0) return 0;
if (m % (i+2) == 0) return 0;
}
return 1;
}
unsigned int stb_power_of_two_nearest_prime(int n)
{
static signed char tab[32] = { 0,0,0,0,1,0,-1,0,1,-1,-1,3,-1,0,-1,2,1,
0,2,0,-1,-4,-1,5,-1,18,-2,15,2,-1,2,0 };
if (!tab[0]) {
int i;
for (i=0; i < 32; ++i)
tab[i] = (1 << i) + 2*tab[i] - 1;
tab[1] = 2;
tab[0] = 1;
}
if (n >= 32) return 0xfffffffb;
return tab[n];
}
double stb_linear_remap(double x, double x_min, double x_max,
double out_min, double out_max)
{
return stb_lerp(stb_unlerp(x,x_min,x_max),out_min,out_max);
}
#endif
// create a macro so it's faster, but you can get at the function pointer
#define stb_linear_remap(t,a,b,c,d) stb_lerp(stb_unlerp(t,a,b),c,d)
//////////////////////////////////////////////////////////////////////////////
//
// bit operations
//
#define stb_big32(c) (((c)[0]<<24) + (c)[1]*65536 + (c)[2]*256 + (c)[3])
#define stb_little32(c) (((c)[3]<<24) + (c)[2]*65536 + (c)[1]*256 + (c)[0])
#define stb_big16(c) ((c)[0]*256 + (c)[1])
#define stb_little16(c) ((c)[1]*256 + (c)[0])
STB_EXTERN int stb_bitcount(unsigned int a);
STB_EXTERN unsigned int stb_bitreverse8(unsigned char n);
STB_EXTERN unsigned int stb_bitreverse(unsigned int n);
STB_EXTERN int stb_is_pow2(size_t);
STB_EXTERN int stb_log2_ceil(size_t);
STB_EXTERN int stb_log2_floor(size_t);
STB_EXTERN int stb_lowbit8(unsigned int n);
STB_EXTERN int stb_highbit8(unsigned int n);
#ifdef STB_DEFINE
int stb_bitcount(unsigned int a)
{
a = (a & 0x55555555) + ((a >> 1) & 0x55555555); // max 2
a = (a & 0x33333333) + ((a >> 2) & 0x33333333); // max 4
a = (a + (a >> 4)) & 0x0f0f0f0f; // max 8 per 4, now 8 bits
a = (a + (a >> 8)); // max 16 per 8 bits
a = (a + (a >> 16)); // max 32 per 8 bits
return a & 0xff;
}
unsigned int stb_bitreverse8(unsigned char n)
{
n = ((n & 0xAA) >> 1) + ((n & 0x55) << 1);
n = ((n & 0xCC) >> 2) + ((n & 0x33) << 2);
return (unsigned char) ((n >> 4) + (n << 4));
}
unsigned int stb_bitreverse(unsigned int n)
{
n = ((n & 0xAAAAAAAA) >> 1) | ((n & 0x55555555) << 1);
n = ((n & 0xCCCCCCCC) >> 2) | ((n & 0x33333333) << 2);
n = ((n & 0xF0F0F0F0) >> 4) | ((n & 0x0F0F0F0F) << 4);
n = ((n & 0xFF00FF00) >> 8) | ((n & 0x00FF00FF) << 8);
return (n >> 16) | (n << 16);
}
int stb_is_pow2(size_t n)
{
return (n & (n-1)) == 0;
}
// tricky use of 4-bit table to identify 5 bit positions (note the '-1')
// 3-bit table would require another tree level; 5-bit table wouldn't save one
#if defined(_WIN32) && !defined(__MINGW32__)
#pragma warning(push)
#pragma warning(disable: 4035) // disable warning about no return value
int stb_log2_floor(size_t n)
{
#if _MSC_VER > 1700
unsigned long i;
#ifdef STB_PTR64
_BitScanReverse64(&i, n);
#else
_BitScanReverse(&i, n);
#endif
return i != 0 ? i : -1;
#else
__asm {
bsr eax,n
jnz done
mov eax,-1
}
done:;
#endif
}
#pragma warning(pop)
#else
int stb_log2_floor(size_t n)
{
static signed char log2_4[16] = { -1,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3 };
#ifdef STB_PTR64
if (n >= ((size_t) 1u << 32))
return stb_log2_floor(n >> 32);
#endif
// 2 compares if n < 16, 3 compares otherwise
if (n < (1U << 14))
if (n < (1U << 4)) return 0 + log2_4[n ];
else if (n < (1U << 9)) return 5 + log2_4[n >> 5];
else return 10 + log2_4[n >> 10];
else if (n < (1U << 24))
if (n < (1U << 19)) return 15 + log2_4[n >> 15];
else return 20 + log2_4[n >> 20];
else if (n < (1U << 29)) return 25 + log2_4[n >> 25];
else return 30 + log2_4[n >> 30];
}
#endif
// define ceil from floor
int stb_log2_ceil(size_t n)
{
if (stb_is_pow2(n)) return stb_log2_floor(n);
else return 1 + stb_log2_floor(n);
}
int stb_highbit8(unsigned int n)
{
return stb_log2_ceil(n&255);
}
int stb_lowbit8(unsigned int n)
{
static signed char lowbit4[16] = { -1,0,1,0, 2,0,1,0, 3,0,1,0, 2,0,1,0 };
int k = lowbit4[n & 15];
if (k >= 0) return k;
k = lowbit4[(n >> 4) & 15];
if (k >= 0) return k+4;
return k;
}
#endif
//////////////////////////////////////////////////////////////////////////////
//
// qsort Compare Routines
//
#ifdef _WIN32
#define stb_stricmp(a,b) stb_p_stricmp(a,b)
#define stb_strnicmp(a,b,n) stb_p_strnicmp(a,b,n)
#else
#define stb_stricmp(a,b) strcasecmp(a,b)
#define stb_strnicmp(a,b,n) strncasecmp(a,b,n)
#endif
STB_EXTERN int (*stb_intcmp(int offset))(const void *a, const void *b);
STB_EXTERN int (*stb_intcmprev(int offset))(const void *a, const void *b);
STB_EXTERN int (*stb_qsort_strcmp(int offset))(const void *a, const void *b);
STB_EXTERN int (*stb_qsort_stricmp(int offset))(const void *a, const void *b);
STB_EXTERN int (*stb_floatcmp(int offset))(const void *a, const void *b);
STB_EXTERN int (*stb_doublecmp(int offset))(const void *a, const void *b);
STB_EXTERN int (*stb_charcmp(int offset))(const void *a, const void *b);
#ifdef STB_DEFINE
static int stb__intcmpoffset, stb__ucharcmpoffset, stb__strcmpoffset;
static int stb__floatcmpoffset, stb__doublecmpoffset;
static int stb__memcmpoffset, stb__memcmpsize;
int stb__intcmp(const void *a, const void *b)
{
const int p = *(const int *) ((const char *) a + stb__intcmpoffset);
const int q = *(const int *) ((const char *) b + stb__intcmpoffset);
return p < q ? -1 : p > q;
}
int stb__intcmprev(const void *a, const void *b)
{
const int p = *(const int *) ((const char *) a + stb__intcmpoffset);
const int q = *(const int *) ((const char *) b + stb__intcmpoffset);
return q < p ? -1 : q > p;
}
int stb__ucharcmp(const void *a, const void *b)
{
const int p = *(const unsigned char *) ((const char *) a + stb__ucharcmpoffset);
const int q = *(const unsigned char *) ((const char *) b + stb__ucharcmpoffset);
return p < q ? -1 : p > q;
}
int stb__floatcmp(const void *a, const void *b)
{
const float p = *(const float *) ((const char *) a + stb__floatcmpoffset);
const float q = *(const float *) ((const char *) b + stb__floatcmpoffset);
return p < q ? -1 : p > q;
}
int stb__doublecmp(const void *a, const void *b)
{
const double p = *(const double *) ((const char *) a + stb__doublecmpoffset);
const double q = *(const double *) ((const char *) b + stb__doublecmpoffset);
return p < q ? -1 : p > q;
}
int stb__qsort_strcmp(const void *a, const void *b)
{
const char *p = *(const char **) ((const char *) a + stb__strcmpoffset);
const char *q = *(const char **) ((const char *) b + stb__strcmpoffset);
return strcmp(p,q);
}
int stb__qsort_stricmp(const void *a, const void *b)
{
const char *p = *(const char **) ((const char *) a + stb__strcmpoffset);
const char *q = *(const char **) ((const char *) b + stb__strcmpoffset);
return stb_stricmp(p,q);
}
int stb__memcmp(const void *a, const void *b)
{
return memcmp((char *) a + stb__memcmpoffset, (char *) b + stb__memcmpoffset, stb__memcmpsize);
}
int (*stb_intcmp(int offset))(const void *, const void *)
{
stb__intcmpoffset = offset;
return &stb__intcmp;
}
int (*stb_intcmprev(int offset))(const void *, const void *)
{
stb__intcmpoffset = offset;
return &stb__intcmprev;
}
int (*stb_ucharcmp(int offset))(const void *, const void *)
{
stb__ucharcmpoffset = offset;
return &stb__ucharcmp;
}
int (*stb_qsort_strcmp(int offset))(const void *, const void *)
{
stb__strcmpoffset = offset;
return &stb__qsort_strcmp;
}
int (*stb_qsort_stricmp(int offset))(const void *, const void *)
{
stb__strcmpoffset = offset;
return &stb__qsort_stricmp;
}
int (*stb_floatcmp(int offset))(const void *, const void *)
{
stb__floatcmpoffset = offset;
return &stb__floatcmp;
}
int (*stb_doublecmp(int offset))(const void *, const void *)
{
stb__doublecmpoffset = offset;
return &stb__doublecmp;
}
int (*stb_memcmp(int offset, int size))(const void *, const void *)
{
stb__memcmpoffset = offset;
stb__memcmpsize = size;
return &stb__memcmp;
}
#endif
//////////////////////////////////////////////////////////////////////////////
//
// Binary Search Toolkit
//
typedef struct
{
int minval, maxval, guess;
int mode, step;
} stb_search;
STB_EXTERN int stb_search_binary(stb_search *s, int minv, int maxv, int find_smallest);
STB_EXTERN int stb_search_open(stb_search *s, int minv, int find_smallest);
STB_EXTERN int stb_probe(stb_search *s, int compare, int *result); // return 0 when done
#ifdef STB_DEFINE
enum
{
STB_probe_binary_smallest,
STB_probe_binary_largest,
STB_probe_open_smallest,
STB_probe_open_largest,
};
static int stb_probe_guess(stb_search *s, int *result)
{
switch(s->mode) {
case STB_probe_binary_largest:
if (s->minval == s->maxval) {
*result = s->minval;
return 0;
}
assert(s->minval < s->maxval);
// if a < b, then a < p <= b
s->guess = s->minval + (((unsigned) s->maxval - s->minval + 1) >> 1);
break;
case STB_probe_binary_smallest:
if (s->minval == s->maxval) {
*result = s->minval;
return 0;
}
assert(s->minval < s->maxval);
// if a < b, then a <= p < b
s->guess = s->minval + (((unsigned) s->maxval - s->minval) >> 1);
break;
case STB_probe_open_smallest:
case STB_probe_open_largest:
s->guess = s->maxval; // guess the current maxval
break;
}
*result = s->guess;
return 1;
}
int stb_probe(stb_search *s, int compare, int *result)
{
switch(s->mode) {
case STB_probe_open_smallest:
case STB_probe_open_largest: {
if (compare <= 0) {
// then it lies within minval & maxval
if (s->mode == STB_probe_open_smallest)
s->mode = STB_probe_binary_smallest;
else
s->mode = STB_probe_binary_largest;
} else {
// otherwise, we need to probe larger
s->minval = s->maxval + 1;
s->maxval = s->minval + s->step;
s->step += s->step;
}
break;
}
case STB_probe_binary_smallest: {
// if compare < 0, then s->minval <= a < p
// if compare = 0, then s->minval <= a <= p
// if compare > 0, then p < a <= s->maxval
if (compare <= 0)
s->maxval = s->guess;
else
s->minval = s->guess+1;
break;
}
case STB_probe_binary_largest: {
// if compare < 0, then s->minval <= a < p
// if compare = 0, then p <= a <= s->maxval
// if compare > 0, then p < a <= s->maxval
if (compare < 0)
s->maxval = s->guess-1;
else
s->minval = s->guess;
break;
}
}
return stb_probe_guess(s, result);
}
int stb_search_binary(stb_search *s, int minv, int maxv, int find_smallest)
{
int r;
if (maxv < minv) return minv-1;
s->minval = minv;
s->maxval = maxv;
s->mode = find_smallest ? STB_probe_binary_smallest : STB_probe_binary_largest;
stb_probe_guess(s, &r);
return r;
}
int stb_search_open(stb_search *s, int minv, int find_smallest)
{
int r;
s->step = 4;
s->minval = minv;
s->maxval = minv+s->step;
s->mode = find_smallest ? STB_probe_open_smallest : STB_probe_open_largest;
stb_probe_guess(s, &r);
return r;
}
#endif
//////////////////////////////////////////////////////////////////////////////
//
// String Processing
//
#define stb_prefixi(s,t) (0==stb_strnicmp((s),(t),strlen(t)))
enum stb_splitpath_flag
{
STB_PATH = 1,
STB_FILE = 2,
STB_EXT = 4,
STB_PATH_FILE = STB_PATH + STB_FILE,
STB_FILE_EXT = STB_FILE + STB_EXT,
STB_EXT_NO_PERIOD = 8,
};
STB_EXTERN char * stb_skipwhite(char *s);
STB_EXTERN char * stb_trimwhite(char *s);
STB_EXTERN char * stb_skipnewline(char *s);
STB_EXTERN char * stb_strncpy(char *s, char *t, int n);
STB_EXTERN char * stb_substr(char *t, int n);
STB_EXTERN char * stb_duplower(char *s);
STB_EXTERN void stb_tolower (char *s);
STB_EXTERN char * stb_strchr2 (char *s, char p1, char p2);
STB_EXTERN char * stb_strrchr2(char *s, char p1, char p2);
STB_EXTERN char * stb_strtok(char *output, char *src, char *delimit);
STB_EXTERN char * stb_strtok_keep(char *output, char *src, char *delimit);
STB_EXTERN char * stb_strtok_invert(char *output, char *src, char *allowed);
STB_EXTERN char * stb_dupreplace(char *s, char *find, char *replace);
STB_EXTERN void stb_replaceinplace(char *s, char *find, char *replace);
STB_EXTERN char * stb_splitpath(char *output, char *src, int flag);
STB_EXTERN char * stb_splitpathdup(char *src, int flag);
STB_EXTERN char * stb_replacedir(char *output, char *src, char *dir);
STB_EXTERN char * stb_replaceext(char *output, char *src, char *ext);
STB_EXTERN void stb_fixpath(char *path);
STB_EXTERN char * stb_shorten_path_readable(char *path, int max_len);
STB_EXTERN int stb_suffix (char *s, char *t);
STB_EXTERN int stb_suffixi(char *s, char *t);
STB_EXTERN int stb_prefix (char *s, char *t);
STB_EXTERN char * stb_strichr(char *s, char t);
STB_EXTERN char * stb_stristr(char *s, char *t);
STB_EXTERN int stb_prefix_count(char *s, char *t);
STB_EXTERN const char * stb_plural(int n); // "s" or ""
STB_EXTERN size_t stb_strscpy(char *d, const char *s, size_t n);
STB_EXTERN char **stb_tokens(char *src, char *delimit, int *count);
STB_EXTERN char **stb_tokens_nested(char *src, char *delimit, int *count, char *nest_in, char *nest_out);
STB_EXTERN char **stb_tokens_nested_empty(char *src, char *delimit, int *count, char *nest_in, char *nest_out);
STB_EXTERN char **stb_tokens_allowempty(char *src, char *delimit, int *count);
STB_EXTERN char **stb_tokens_stripwhite(char *src, char *delimit, int *count);
STB_EXTERN char **stb_tokens_withdelim(char *src, char *delimit, int *count);
STB_EXTERN char **stb_tokens_quoted(char *src, char *delimit, int *count);
// with 'quoted', allow delimiters to appear inside quotation marks, and don't
// strip whitespace inside them (and we delete the quotation marks unless they
// appear back to back, in which case they're considered escaped)
#ifdef STB_DEFINE
size_t stb_strscpy(char *d, const char *s, size_t n)
{
size_t len = strlen(s);
if (len >= n) {
if (n) d[0] = 0;
return 0;
}
stb_p_strcpy_s(d,n+1,s);
return len + 1;
}
const char *stb_plural(int n)
{
return n == 1 ? "" : "s";
}
int stb_prefix(char *s, char *t)
{
while (*t)
if (*s++ != *t++)
return STB_FALSE;
return STB_TRUE;
}
int stb_prefix_count(char *s, char *t)
{
int c=0;
while (*t) {
if (*s++ != *t++)
break;
++c;
}
return c;
}
int stb_suffix(char *s, char *t)
{
size_t n = strlen(s);
size_t m = strlen(t);
if (m <= n)
return 0 == strcmp(s+n-m, t);
else
return 0;
}
int stb_suffixi(char *s, char *t)
{
size_t n = strlen(s);
size_t m = strlen(t);
if (m <= n)
return 0 == stb_stricmp(s+n-m, t);
else
return 0;
}
// originally I was using this table so that I could create known sentinel
// values--e.g. change whitetable[0] to be true if I was scanning for whitespace,
// and false if I was scanning for nonwhite. I don't appear to be using that
// functionality anymore (I do for tokentable, though), so just replace it
// with isspace()
char *stb_skipwhite(char *s)
{
while (isspace((unsigned char) *s)) ++s;
return s;
}
char *stb_skipnewline(char *s)
{
if (s[0] == '\r' || s[0] == '\n') {
if (s[0]+s[1] == '\r' + '\n') ++s;
++s;
}
return s;
}
char *stb_trimwhite(char *s)
{
int i,n;
s = stb_skipwhite(s);
n = (int) strlen(s);
for (i=n-1; i >= 0; --i)
if (!isspace(s[i]))
break;
s[i+1] = 0;
return s;
}
char *stb_strncpy(char *s, char *t, int n)
{
stb_p_strncpy_s(s,n+1,t,n);
s[n] = 0;
return s;
}
char *stb_substr(char *t, int n)
{
char *a;
int z = (int) strlen(t);
if (z < n) n = z;
a = (char *) malloc(n+1);
stb_p_strncpy_s(a,n+1,t,n);
a[n] = 0;
return a;
}
char *stb_duplower(char *s)
{
char *p = stb_p_strdup(s), *q = p;
while (*q) {
*q = tolower(*q);
++q;
}
return p;
}
void stb_tolower(char *s)
{
while (*s) {
*s = tolower(*s);
++s;
}
}
char *stb_strchr2(char *s, char x, char y)
{
for(; *s; ++s)
if (*s == x || *s == y)
return s;
return NULL;
}
char *stb_strrchr2(char *s, char x, char y)
{
char *r = NULL;
for(; *s; ++s)
if (*s == x || *s == y)
r = s;
return r;
}
char *stb_strichr(char *s, char t)
{
if (tolower(t) == toupper(t))
return strchr(s,t);
return stb_strchr2(s, (char) tolower(t), (char) toupper(t));
}
char *stb_stristr(char *s, char *t)
{
size_t n = strlen(t);
char *z;
if (n==0) return s;
while ((z = stb_strichr(s, *t)) != NULL) {
if (0==stb_strnicmp(z, t, n))
return z;
s = z+1;
}
return NULL;
}
static char *stb_strtok_raw(char *output, char *src, char *delimit, int keep, int invert)
{
if (invert) {
while (*src && strchr(delimit, *src) != NULL) {
*output++ = *src++;
}
} else {
while (*src && strchr(delimit, *src) == NULL) {
*output++ = *src++;
}
}
*output = 0;
if (keep)
return src;
else
return *src ? src+1 : src;
}
char *stb_strtok(char *output, char *src, char *delimit)
{
return stb_strtok_raw(output, src, delimit, 0, 0);
}
char *stb_strtok_keep(char *output, char *src, char *delimit)
{
return stb_strtok_raw(output, src, delimit, 1, 0);
}
char *stb_strtok_invert(char *output, char *src, char *delimit)
{
return stb_strtok_raw(output, src, delimit, 1,1);
}
static char **stb_tokens_raw(char *src_, char *delimit, int *count,
int stripwhite, int allow_empty, char *start, char *end)
{
int nested = 0;
unsigned char *src = (unsigned char *) src_;
static char stb_tokentable[256]; // rely on static initializion to 0
static char stable[256],etable[256];
char *out;
char **result;
int num=0;
unsigned char *s;
s = (unsigned char *) delimit; while (*s) stb_tokentable[*s++] = 1;
if (start) {
s = (unsigned char *) start; while (*s) stable[*s++] = 1;
s = (unsigned char *) end; if (s) while (*s) stable[*s++] = 1;
s = (unsigned char *) end; if (s) while (*s) etable[*s++] = 1;
}
stable[0] = 1;
// two passes through: the first time, counting how many
s = (unsigned char *) src;
while (*s) {
// state: just found delimiter
// skip further delimiters
if (!allow_empty) {
stb_tokentable[0] = 0;
while (stb_tokentable[*s])
++s;
if (!*s) break;
}
++num;
// skip further non-delimiters
stb_tokentable[0] = 1;
if (stripwhite == 2) { // quoted strings
while (!stb_tokentable[*s]) {
if (*s != '"')
++s;
else {
++s;
if (*s == '"')
++s; // "" -> ", not start a string
else {
// begin a string
while (*s) {
if (s[0] == '"') {
if (s[1] == '"') s += 2; // "" -> "
else { ++s; break; } // terminating "
} else
++s;
}
}
}
}
} else
while (nested || !stb_tokentable[*s]) {
if (stable[*s]) {
if (!*s) break;
if (end ? etable[*s] : nested)
--nested;
else
++nested;
}
++s;
}
if (allow_empty) {
if (*s) ++s;
}
}
// now num has the actual count... malloc our output structure
// need space for all the strings: strings won't be any longer than
// original input, since for every '\0' there's at least one delimiter
result = (char **) malloc(sizeof(*result) * (num+1) + (s-src+1));
if (result == NULL) return result;
out = (char *) (result + (num+1));
// second pass: copy out the data
s = (unsigned char *) src;
num = 0;
nested = 0;
while (*s) {
char *last_nonwhite;
// state: just found delimiter
// skip further delimiters
if (!allow_empty) {
stb_tokentable[0] = 0;
if (stripwhite)
while (stb_tokentable[*s] || isspace(*s))
++s;
else
while (stb_tokentable[*s])
++s;
} else if (stripwhite) {
while (isspace(*s)) ++s;
}
if (!*s) break;
// we're past any leading delimiters and whitespace
result[num] = out;
++num;
// copy non-delimiters
stb_tokentable[0] = 1;
last_nonwhite = out-1;
if (stripwhite == 2) {
while (!stb_tokentable[*s]) {
if (*s != '"') {
if (!isspace(*s)) last_nonwhite = out;
*out++ = *s++;
} else {
++s;
if (*s == '"') {
if (!isspace(*s)) last_nonwhite = out;
*out++ = *s++; // "" -> ", not start string
} else {
// begin a quoted string
while (*s) {
if (s[0] == '"') {
if (s[1] == '"') { *out++ = *s; s += 2; }
else { ++s; break; } // terminating "
} else
*out++ = *s++;
}
last_nonwhite = out-1; // all in quotes counts as non-white
}
}
}
} else {
while (nested || !stb_tokentable[*s]) {
if (!isspace(*s)) last_nonwhite = out;
if (stable[*s]) {
if (!*s) break;
if (end ? etable[*s] : nested)
--nested;
else
++nested;
}
*out++ = *s++;
}
}
if (stripwhite) // rewind to last non-whitespace char
out = last_nonwhite+1;
*out++ = '\0';
if (*s) ++s; // skip delimiter
}
s = (unsigned char *) delimit; while (*s) stb_tokentable[*s++] = 0;
if (start) {
s = (unsigned char *) start; while (*s) stable[*s++] = 1;
s = (unsigned char *) end; if (s) while (*s) stable[*s++] = 1;
s = (unsigned char *) end; if (s) while (*s) etable[*s++] = 1;
}
if (count != NULL) *count = num;
result[num] = 0;
return result;
}
char **stb_tokens(char *src, char *delimit, int *count)
{
return stb_tokens_raw(src,delimit,count,0,0,0,0);
}
char **stb_tokens_nested(char *src, char *delimit, int *count, char *nest_in, char *nest_out)
{
return stb_tokens_raw(src,delimit,count,0,0,nest_in,nest_out);
}
char **stb_tokens_nested_empty(char *src, char *delimit, int *count, char *nest_in, char *nest_out)
{
return stb_tokens_raw(src,delimit,count,0,1,nest_in,nest_out);
}
char **stb_tokens_allowempty(char *src, char *delimit, int *count)
{
return stb_tokens_raw(src,delimit,count,0,1,0,0);
}
char **stb_tokens_stripwhite(char *src, char *delimit, int *count)
{
return stb_tokens_raw(src,delimit,count,1,1,0,0);
}
char **stb_tokens_quoted(char *src, char *delimit, int *count)
{
return stb_tokens_raw(src,delimit,count,2,1,0,0);
}
char *stb_dupreplace(char *src, char *find, char *replace)
{
size_t len_find = strlen(find);
size_t len_replace = strlen(replace);
int count = 0;
char *s,*p,*q;
s = strstr(src, find);
if (s == NULL) return stb_p_strdup(src);
do {
++count;
s = strstr(s + len_find, find);
} while (s != NULL);
p = (char *) malloc(strlen(src) + count * (len_replace - len_find) + 1);
if (p == NULL) return p;
q = p;
s = src;
for (;;) {
char *t = strstr(s, find);
if (t == NULL) {
stb_p_strcpy_s(q,strlen(src)+count*(len_replace-len_find)+1,s);
assert(strlen(p) == strlen(src) + count*(len_replace-len_find));
return p;
}
memcpy(q, s, t-s);
q += t-s;
memcpy(q, replace, len_replace);
q += len_replace;
s = t + len_find;
}
}
void stb_replaceinplace(char *src, char *find, char *replace)
{
size_t len_find = strlen(find);
size_t len_replace = strlen(replace);
int delta;
char *s,*p,*q;
delta = (int) (len_replace - len_find);
assert(delta <= 0);
if (delta > 0) return;
p = strstr(src, find);
if (p == NULL) return;
s = q = p;
while (*s) {
memcpy(q, replace, len_replace);
p += len_find;
q += len_replace;
s = strstr(p, find);
if (s == NULL) s = p + strlen(p);
memmove(q, p, s-p);
q += s-p;
p = s;
}
*q = 0;
}
void stb_fixpath(char *path)
{
for(; *path; ++path)
if (*path == '\\')
*path = '/';
}
void stb__add_section(char *buffer, char *data, ptrdiff_t curlen, ptrdiff_t newlen)
{
if (newlen < curlen) {
ptrdiff_t z1 = newlen >> 1, z2 = newlen-z1;
memcpy(buffer, data, z1-1);
buffer[z1-1] = '.';
buffer[z1-0] = '.';
memcpy(buffer+z1+1, data+curlen-z2+1, z2-1);
} else
memcpy(buffer, data, curlen);
}
char * stb_shorten_path_readable(char *path, int len)
{
static char buffer[1024];
ptrdiff_t n = strlen(path),n1,n2,r1,r2;
char *s;
if (n <= len) return path;
if (len > 1024) return path;
s = stb_strrchr2(path, '/', '\\');
if (s) {
n1 = s - path + 1;
n2 = n - n1;
++s;
} else {
n1 = 0;
n2 = n;
s = path;
}
// now we need to reduce r1 and r2 so that they fit in len
if (n1 < len>>1) {
r1 = n1;
r2 = len - r1;
} else if (n2 < len >> 1) {
r2 = n2;
r1 = len - r2;
} else {
r1 = n1 * len / n;
r2 = n2 * len / n;
if (r1 < len>>2) r1 = len>>2, r2 = len-r1;
if (r2 < len>>2) r2 = len>>2, r1 = len-r2;
}
assert(r1 <= n1 && r2 <= n2);
if (n1)
stb__add_section(buffer, path, n1, r1);
stb__add_section(buffer+r1, s, n2, r2);
buffer[len] = 0;
return buffer;
}
static char *stb__splitpath_raw(char *buffer, char *path, int flag)
{
ptrdiff_t len=0,x,y, n = (int) strlen(path), f1,f2;
char *s = stb_strrchr2(path, '/', '\\');
char *t = strrchr(path, '.');
if (s && t && t < s) t = NULL;
if (s) ++s;
if (flag == STB_EXT_NO_PERIOD)
flag |= STB_EXT;
if (!(flag & (STB_PATH | STB_FILE | STB_EXT))) return NULL;
f1 = s == NULL ? 0 : s-path; // start of filename
f2 = t == NULL ? n : t-path; // just past end of filename
if (flag & STB_PATH) {
x = 0; if (f1 == 0 && flag == STB_PATH) len=2;
} else if (flag & STB_FILE) {
x = f1;
} else {
x = f2;
if (flag & STB_EXT_NO_PERIOD)
if (path[x] == '.')
++x;
}
if (flag & STB_EXT)
y = n;
else if (flag & STB_FILE)
y = f2;
else
y = f1;
if (buffer == NULL) {
buffer = (char *) malloc(y-x + len + 1);
if (!buffer) return NULL;
}
if (len) { stb_p_strcpy_s(buffer, 3, "./"); return buffer; }
stb_strncpy(buffer, path+(int)x, (int)(y-x));
return buffer;
}
char *stb_splitpath(char *output, char *src, int flag)
{
return stb__splitpath_raw(output, src, flag);
}
char *stb_splitpathdup(char *src, int flag)
{
return stb__splitpath_raw(NULL, src, flag);
}
char *stb_replacedir(char *output, char *src, char *dir)
{
char buffer[4096];
stb_splitpath(buffer, src, STB_FILE | STB_EXT);
if (dir)
stb_p_sprintf(output stb_p_size(9999), "%s/%s", dir, buffer);
else
stb_p_strcpy_s(output, sizeof(buffer), buffer); // @UNSAFE
return output;
}
char *stb_replaceext(char *output, char *src, char *ext)
{
char buffer[4096];
stb_splitpath(buffer, src, STB_PATH | STB_FILE);
if (ext)
stb_p_sprintf(output stb_p_size(9999), "%s.%s", buffer, ext[0] == '.' ? ext+1 : ext);
else
stb_p_strcpy_s(output, sizeof(buffer), buffer); // @UNSAFE
return output;
}
#endif
//////////////////////////////////////////////////////////////////////////////
//
// stb_alloc - hierarchical allocator
//
// inspired by http://swapped.cc/halloc
//
//
// When you alloc a given block through stb_alloc, you have these choices:
//
// 1. does it have a parent?
// 2. can it have children?
// 3. can it be freed directly?
// 4. is it transferrable?
// 5. what is its alignment?
//
// Here are interesting combinations of those:
//
// children free transfer alignment
// arena Y Y N n/a
// no-overhead, chunked N N N normal
// string pool alloc N N N 1
// parent-ptr, chunked Y N N normal
// low-overhead, unchunked N Y Y normal
// general purpose alloc Y Y Y normal
//
// Unchunked allocations will probably return 16-aligned pointers. If
// we 16-align the results, we have room for 4 pointers. For smaller
// allocations that allow finer alignment, we can reduce the pointers.
//
// The strategy is that given a pointer, assuming it has a header (only
// the no-overhead allocations have no header), we can determine the
// type of the header fields, and the number of them, by stepping backwards
// through memory and looking at the tags in the bottom bits.
//
// Implementation strategy:
// chunked allocations come from the middle of chunks, and can't
// be freed. thefore they do not need to be on a sibling chain.
// they may need child pointers if they have children.
//
// chunked, with-children
// void *parent;
//
// unchunked, no-children -- reduced storage
// void *next_sibling;
// void *prev_sibling_nextp;
//
// unchunked, general
// void *first_child;
// void *next_sibling;
// void *prev_sibling_nextp;
// void *chunks;
//
// so, if we code each of these fields with different bit patterns
// (actually same one for next/prev/child), then we can identify which
// each one is from the last field.
STB_EXTERN void stb_free(void *p);
STB_EXTERN void *stb_malloc_global(size_t size);
STB_EXTERN void *stb_malloc(void *context, size_t size);
STB_EXTERN void *stb_malloc_nofree(void *context, size_t size);
STB_EXTERN void *stb_malloc_leaf(void *context, size_t size);
STB_EXTERN void *stb_malloc_raw(void *context, size_t size);
STB_EXTERN void *stb_realloc(void *ptr, size_t newsize);
STB_EXTERN void stb_reassign(void *new_context, void *ptr);
STB_EXTERN void stb_malloc_validate(void *p, void *parent);
extern int stb_alloc_chunk_size ;
extern int stb_alloc_count_free ;
extern int stb_alloc_count_alloc;
extern int stb_alloc_alignment ;
#ifdef STB_DEFINE
int stb_alloc_chunk_size = 65536;
int stb_alloc_count_free = 0;
int stb_alloc_count_alloc = 0;
int stb_alloc_alignment = -16;
typedef struct stb__chunk
{
struct stb__chunk *next;
int data_left;
int alloc;
} stb__chunk;
typedef struct
{
void * next;
void ** prevn;
} stb__nochildren;
typedef struct
{
void ** prevn;
void * child;
void * next;
stb__chunk *chunks;
} stb__alloc;
typedef struct
{
stb__alloc *parent;
} stb__chunked;
#define STB__PARENT 1
#define STB__CHUNKS 2
typedef enum
{
STB__nochildren = 0,
STB__chunked = STB__PARENT,
STB__alloc = STB__CHUNKS,
STB__chunk_raw = 4,
} stb__alloc_type;
// these functions set the bottom bits of a pointer efficiently
#define STB__DECODE(x,v) ((void *) ((char *) (x) - (v)))
#define STB__ENCODE(x,v) ((void *) ((char *) (x) + (v)))
#define stb__parent(z) (stb__alloc *) STB__DECODE((z)->parent, STB__PARENT)
#define stb__chunks(z) (stb__chunk *) STB__DECODE((z)->chunks, STB__CHUNKS)
#define stb__setparent(z,p) (z)->parent = (stb__alloc *) STB__ENCODE((p), STB__PARENT)
#define stb__setchunks(z,c) (z)->chunks = (stb__chunk *) STB__ENCODE((c), STB__CHUNKS)
static stb__alloc stb__alloc_global =
{
NULL,
NULL,
NULL,
(stb__chunk *) STB__ENCODE(NULL, STB__CHUNKS)
};
static stb__alloc_type stb__identify(void *p)
{
void **q = (void **) p;
return (stb__alloc_type) ((stb_uinta) q[-1] & 3);
}
static void *** stb__prevn(void *p)
{
if (stb__identify(p) == STB__alloc) {
stb__alloc *s = (stb__alloc *) p - 1;
return &s->prevn;
} else {
stb__nochildren *s = (stb__nochildren *) p - 1;
return &s->prevn;
}
}
void stb_free(void *p)
{
if (p == NULL) return;
// count frees so that unit tests can see what's happening
++stb_alloc_count_free;
switch(stb__identify(p)) {
case STB__chunked:
// freeing a chunked-block with children does nothing;
// they only get freed when the parent does
// surely this is wrong, and it should free them immediately?
// otherwise how are they getting put on the right chain?
return;
case STB__nochildren: {
stb__nochildren *s = (stb__nochildren *) p - 1;
// unlink from sibling chain
*(s->prevn) = s->next;
if (s->next)
*stb__prevn(s->next) = s->prevn;
free(s);
return;
}
case STB__alloc: {
stb__alloc *s = (stb__alloc *) p - 1;
stb__chunk *c, *n;
void *q;
// unlink from sibling chain, if any
*(s->prevn) = s->next;
if (s->next)
*stb__prevn(s->next) = s->prevn;
// first free chunks
c = (stb__chunk *) stb__chunks(s);
while (c != NULL) {
n = c->next;
stb_alloc_count_free += c->alloc;
free(c);
c = n;
}
// validating
stb__setchunks(s,NULL);
s->prevn = NULL;
s->next = NULL;
// now free children
while ((q = s->child) != NULL) {
stb_free(q);
}
// now free self
free(s);
return;
}
default:
assert(0); /* NOTREACHED */
}
}
void stb_malloc_validate(void *p, void *parent)
{
if (p == NULL) return;
switch(stb__identify(p)) {
case STB__chunked:
return;
case STB__nochildren: {
stb__nochildren *n = (stb__nochildren *) p - 1;
if (n->prevn)
assert(*n->prevn == p);
if (n->next) {
assert(*stb__prevn(n->next) == &n->next);
stb_malloc_validate(n, parent);
}
return;
}
case STB__alloc: {
stb__alloc *s = (stb__alloc *) p - 1;
if (s->prevn)
assert(*s->prevn == p);
if (s->child) {
assert(*stb__prevn(s->child) == &s->child);
stb_malloc_validate(s->child, p);
}
if (s->next) {
assert(*stb__prevn(s->next) == &s->next);
stb_malloc_validate(s->next, parent);
}
return;
}
default:
assert(0); /* NOTREACHED */
}
}
static void * stb__try_chunk(stb__chunk *c, int size, int align, int pre_align)
{
char *memblock = (char *) (c+1), *q;
stb_inta iq;
int start_offset;
// we going to allocate at the end of the chunk, not the start. confusing,
// but it means we don't need both a 'limit' and a 'cur', just a 'cur'.
// the block ends at: p + c->data_left
// then we move back by size
start_offset = c->data_left - size;
// now we need to check the alignment of that
q = memblock + start_offset;
iq = (stb_inta) q;
assert(sizeof(q) == sizeof(iq));
// suppose align = 2
// then we need to retreat iq far enough that (iq & (2-1)) == 0
// to get (iq & (align-1)) = 0 requires subtracting (iq & (align-1))
start_offset -= iq & (align-1);
assert(((stb_uinta) (memblock+start_offset) & (align-1)) == 0);
// now, if that + pre_align works, go for it!
start_offset -= pre_align;
if (start_offset >= 0) {
c->data_left = start_offset;
return memblock + start_offset;
}
return NULL;
}
static void stb__sort_chunks(stb__alloc *src)
{
// of the first two chunks, put the chunk with more data left in it first
stb__chunk *c = stb__chunks(src), *d;
if (c == NULL) return;
d = c->next;
if (d == NULL) return;
if (c->data_left > d->data_left) return;
c->next = d->next;
d->next = c;
stb__setchunks(src, d);
}
static void * stb__alloc_chunk(stb__alloc *src, int size, int align, int pre_align)
{
void *p;
stb__chunk *c = stb__chunks(src);
if (c && size <= stb_alloc_chunk_size) {
p = stb__try_chunk(c, size, align, pre_align);
if (p) { ++c->alloc; return p; }
// try a second chunk to reduce wastage
if (c->next) {
p = stb__try_chunk(c->next, size, align, pre_align);
if (p) { ++c->alloc; return p; }
// put the bigger chunk first, since the second will get buried
// the upshot of this is that, until it gets allocated from, chunk #2
// is always the largest remaining chunk. (could formalize
// this with a heap!)
stb__sort_chunks(src);
c = stb__chunks(src);
}
}
// allocate a new chunk
{
stb__chunk *n;
int chunk_size = stb_alloc_chunk_size;
// we're going to allocate a new chunk to put this in
if (size > chunk_size)
chunk_size = size;
assert(sizeof(*n) + pre_align <= 16);
// loop trying to allocate a large enough chunk
// the loop is because the alignment may cause problems if it's big...
// and we don't know what our chunk alignment is going to be
while (1) {
n = (stb__chunk *) malloc(16 + chunk_size);
if (n == NULL) return NULL;
n->data_left = chunk_size - sizeof(*n);
p = stb__try_chunk(n, size, align, pre_align);
if (p != NULL) {
n->next = c;
stb__setchunks(src, n);
// if we just used up the whole block immediately,
// move the following chunk up
n->alloc = 1;
if (size == chunk_size)
stb__sort_chunks(src);
return p;
}
free(n);
chunk_size += 16+align;
}
}
}
static stb__alloc * stb__get_context(void *context)
{
if (context == NULL) {
return &stb__alloc_global;
} else {
int u = stb__identify(context);
// if context is chunked, grab parent
if (u == STB__chunked) {
stb__chunked *s = (stb__chunked *) context - 1;
return stb__parent(s);
} else {
return (stb__alloc *) context - 1;
}
}
}
static void stb__insert_alloc(stb__alloc *src, stb__alloc *s)
{
s->prevn = &src->child;
s->next = src->child;
src->child = s+1;
if (s->next)
*stb__prevn(s->next) = &s->next;
}
static void stb__insert_nochild(stb__alloc *src, stb__nochildren *s)
{
s->prevn = &src->child;
s->next = src->child;
src->child = s+1;
if (s->next)
*stb__prevn(s->next) = &s->next;
}
static void * malloc_base(void *context, size_t size, stb__alloc_type t, int align)
{
void *p;
stb__alloc *src = stb__get_context(context);
if (align <= 0) {
// compute worst-case C packed alignment
// e.g. a 24-byte struct is 8-aligned
int align_proposed = 1 << stb_lowbit8((unsigned int) size);
if (align_proposed < 0)
align_proposed = 4;
if (align_proposed == 0) {
if (size == 0)
align_proposed = 1;
else
align_proposed = 256;
}
// a negative alignment means 'don't align any larger
// than this'; so -16 means we align 1,2,4,8, or 16
if (align < 0) {
if (align_proposed > -align)
align_proposed = -align;
}
align = align_proposed;
}
assert(stb_is_pow2(align));
// don't cause misalignment when allocating nochildren
if (t == STB__nochildren && align > 8)
t = STB__alloc;
switch (t) {
case STB__alloc: {
stb__alloc *s = (stb__alloc *) malloc(size + sizeof(*s));
if (s == NULL) return NULL;
p = s+1;
s->child = NULL;
stb__insert_alloc(src, s);
stb__setchunks(s,NULL);
break;
}
case STB__nochildren: {
stb__nochildren *s = (stb__nochildren *) malloc(size + sizeof(*s));
if (s == NULL) return NULL;
p = s+1;
stb__insert_nochild(src, s);
break;
}
case STB__chunk_raw: {
p = stb__alloc_chunk(src, (int) size, align, 0);
if (p == NULL) return NULL;
break;
}
case STB__chunked: {
stb__chunked *s;
if (align < sizeof(stb_uintptr)) align = sizeof(stb_uintptr);
s = (stb__chunked *) stb__alloc_chunk(src, (int) size, align, sizeof(*s));
if (s == NULL) return NULL;
stb__setparent(s, src);
p = s+1;
break;
}
default: p = NULL; assert(0); /* NOTREACHED */
}
++stb_alloc_count_alloc;
return p;
}
void *stb_malloc_global(size_t size)
{
return malloc_base(NULL, size, STB__alloc, stb_alloc_alignment);
}
void *stb_malloc(void *context, size_t size)
{
return malloc_base(context, size, STB__alloc, stb_alloc_alignment);
}
void *stb_malloc_nofree(void *context, size_t size)
{
return malloc_base(context, size, STB__chunked, stb_alloc_alignment);
}
void *stb_malloc_leaf(void *context, size_t size)
{
return malloc_base(context, size, STB__nochildren, stb_alloc_alignment);
}
void *stb_malloc_raw(void *context, size_t size)
{
return malloc_base(context, size, STB__chunk_raw, stb_alloc_alignment);
}
char *stb_malloc_string(void *context, size_t size)
{
return (char *) malloc_base(context, size, STB__chunk_raw, 1);
}
void *stb_realloc(void *ptr, size_t newsize)
{
stb__alloc_type t;
if (ptr == NULL) return stb_malloc(NULL, newsize);
if (newsize == 0) { stb_free(ptr); return NULL; }
t = stb__identify(ptr);
assert(t == STB__alloc || t == STB__nochildren);
if (t == STB__alloc) {
stb__alloc *s = (stb__alloc *) ptr - 1;
s = (stb__alloc *) realloc(s, newsize + sizeof(*s));
if (s == NULL) return NULL;
ptr = s+1;
// update pointers
(*s->prevn) = ptr;
if (s->next)
*stb__prevn(s->next) = &s->next;
if (s->child)
*stb__prevn(s->child) = &s->child;
return ptr;
} else {
stb__nochildren *s = (stb__nochildren *) ptr - 1;
s = (stb__nochildren *) realloc(ptr, newsize + sizeof(s));
if (s == NULL) return NULL;
// update pointers
(*s->prevn) = s+1;
if (s->next)
*stb__prevn(s->next) = &s->next;
return s+1;
}
}
void *stb_realloc_c(void *context, void *ptr, size_t newsize)
{
if (ptr == NULL) return stb_malloc(context, newsize);
if (newsize == 0) { stb_free(ptr); return NULL; }
// @TODO: verify you haven't changed contexts
return stb_realloc(ptr, newsize);
}
void stb_reassign(void *new_context, void *ptr)
{
stb__alloc *src = stb__get_context(new_context);
stb__alloc_type t = stb__identify(ptr);
assert(t == STB__alloc || t == STB__nochildren);
if (t == STB__alloc) {
stb__alloc *s = (stb__alloc *) ptr - 1;
// unlink from old
*(s->prevn) = s->next;
if (s->next)
*stb__prevn(s->next) = s->prevn;
stb__insert_alloc(src, s);
} else {
stb__nochildren *s = (stb__nochildren *) ptr - 1;
// unlink from old
*(s->prevn) = s->next;
if (s->next)
*stb__prevn(s->next) = s->prevn;
stb__insert_nochild(src, s);
}
}
#endif
//////////////////////////////////////////////////////////////////////////////
//
// stb_arr
//
// An stb_arr is directly useable as a pointer (use the actual type in your
// definition), but when it resizes, it returns a new pointer and you can't
// use the old one, so you have to be careful to copy-in-out as necessary.
//
// Use a NULL pointer as a 0-length array.
//
// float *my_array = NULL, *temp;
//
// // add elements on the end one at a time
// stb_arr_push(my_array, 0.0f);
// stb_arr_push(my_array, 1.0f);
// stb_arr_push(my_array, 2.0f);
//
// assert(my_array[1] == 2.0f);
//
// // add an uninitialized element at the end, then assign it
// *stb_arr_add(my_array) = 3.0f;
//
// // add three uninitialized elements at the end
// temp = stb_arr_addn(my_array,3);
// temp[0] = 4.0f;
// temp[1] = 5.0f;
// temp[2] = 6.0f;
//
// assert(my_array[5] == 5.0f);
//
// // remove the last one
// stb_arr_pop(my_array);
//
// assert(stb_arr_len(my_array) == 6);
#ifdef STB_MALLOC_WRAPPER
#define STB__PARAMS , char *file, int line
#define STB__ARGS , file, line
#else
#define STB__PARAMS
#define STB__ARGS
#endif
// calling this function allocates an empty stb_arr attached to p
// (whereas NULL isn't attached to anything)
STB_EXTERN void stb_arr_malloc(void **target, void *context);
// call this function with a non-NULL value to have all successive
// stbs that are created be attached to the associated parent. Note
// that once a given stb_arr is non-empty, it stays attached to its
// current parent, even if you call this function again.
// it turns the previous value, so you can restore it
STB_EXTERN void* stb_arr_malloc_parent(void *p);
// simple functions written on top of other functions
#define stb_arr_empty(a) ( stb_arr_len(a) == 0 )
#define stb_arr_add(a) ( stb_arr_addn((a),1) )
#define stb_arr_push(a,v) ( *stb_arr_add(a)=(v) )
typedef struct
{
int len, limit;
int stb_malloc;
unsigned int signature;
} stb__arr;
#define stb_arr_signature 0x51bada7b // ends with 0123 in decimal
// access the header block stored before the data
#define stb_arrhead(a) /*lint --e(826)*/ (((stb__arr *) (a)) - 1)
#define stb_arrhead2(a) /*lint --e(826)*/ (((stb__arr *) (a)) - 1)
#ifdef STB_DEBUG
#define stb_arr_check(a) assert(!a || stb_arrhead(a)->signature == stb_arr_signature)
#define stb_arr_check2(a) assert(!a || stb_arrhead2(a)->signature == stb_arr_signature)
#else
#define stb_arr_check(a) ((void) 0)
#define stb_arr_check2(a) ((void) 0)
#endif
// ARRAY LENGTH
// get the array length; special case if pointer is NULL
#define stb_arr_len(a) (a ? stb_arrhead(a)->len : 0)
#define stb_arr_len2(a) ((stb__arr *) (a) ? stb_arrhead2(a)->len : 0)
#define stb_arr_lastn(a) (stb_arr_len(a)-1)
// check whether a given index is valid -- tests 0 <= i < stb_arr_len(a)
#define stb_arr_valid(a,i) (a ? (int) (i) < stb_arrhead(a)->len : 0)
// change the array length so is is exactly N entries long, creating
// uninitialized entries as needed
#define stb_arr_setlen(a,n) \
(stb__arr_setlen((void **) &(a), sizeof(a[0]), (n)))
// change the array length so that N is a valid index (that is, so
// it is at least N entries long), creating uninitialized entries as needed
#define stb_arr_makevalid(a,n) \
(stb_arr_len(a) < (n)+1 ? stb_arr_setlen(a,(n)+1),(a) : (a))
// remove the last element of the array, returning it
#define stb_arr_pop(a) ((stb_arr_check(a), (a))[--stb_arrhead(a)->len])
// access the last element in the array
#define stb_arr_last(a) ((stb_arr_check(a), (a))[stb_arr_len(a)-1])
// is iterator at end of list?
#define stb_arr_end(a,i) ((i) >= &(a)[stb_arr_len(a)])
// (internal) change the allocated length of the array
#define stb_arr__grow(a,n) (stb_arr_check(a), stb_arrhead(a)->len += (n))
// add N new uninitialized elements to the end of the array
#define stb_arr__addn(a,n) /*lint --e(826)*/ \
((stb_arr_len(a)+(n) > stb_arrcurmax(a)) \
? (stb__arr_addlen((void **) &(a),sizeof(*a),(n)),0) \
: ((stb_arr__grow(a,n), 0)))
// add N new uninitialized elements to the end of the array, and return
// a pointer to the first new one
#define stb_arr_addn(a,n) (stb_arr__addn((a),n),(a)+stb_arr_len(a)-(n))
// add N new uninitialized elements starting at index 'i'
#define stb_arr_insertn(a,i,n) (stb__arr_insertn((void **) &(a), sizeof(*a), (i), (n)))
// insert an element at i
#define stb_arr_insert(a,i,v) (stb__arr_insertn((void **) &(a), sizeof(*a), (i), (1)), ((a)[i] = v))
// delete N elements from the middle starting at index 'i'
#define stb_arr_deleten(a,i,n) (stb__arr_deleten((void **) &(a), sizeof(*a), (i), (n)))
// delete the i'th element
#define stb_arr_delete(a,i) stb_arr_deleten(a,i,1)
// delete the i'th element, swapping down from the end
#define stb_arr_fastdelete(a,i) \
(stb_swap(&a[i], &a[stb_arrhead(a)->len-1], sizeof(*a)), stb_arr_pop(a))
// ARRAY STORAGE
// get the array maximum storage; special case if NULL
#define stb_arrcurmax(a) (a ? stb_arrhead(a)->limit : 0)
#define stb_arrcurmax2(a) (a ? stb_arrhead2(a)->limit : 0)
// set the maxlength of the array to n in anticipation of further growth
#define stb_arr_setsize(a,n) (stb_arr_check(a), stb__arr_setsize((void **) &(a),sizeof((a)[0]),n))
// make sure maxlength is large enough for at least N new allocations
#define stb_arr_atleast(a,n) (stb_arr_len(a)+(n) > stb_arrcurmax(a) \
? stb_arr_setsize((a), (n)) : 0)
// make a copy of a given array (copies contents via 'memcpy'!)
#define stb_arr_copy(a) stb__arr_copy(a, sizeof((a)[0]))
// compute the storage needed to store all the elements of the array
#define stb_arr_storage(a) (stb_arr_len(a) * sizeof((a)[0]))
#define stb_arr_for(v,arr) for((v)=(arr); (v) < (arr)+stb_arr_len(arr); ++(v))
// IMPLEMENTATION
STB_EXTERN void stb_arr_free_(void **p);
STB_EXTERN void *stb__arr_copy_(void *p, int elem_size);
STB_EXTERN void stb__arr_setsize_(void **p, int size, int limit STB__PARAMS);
STB_EXTERN void stb__arr_setlen_(void **p, int size, int newlen STB__PARAMS);
STB_EXTERN void stb__arr_addlen_(void **p, int size, int addlen STB__PARAMS);
STB_EXTERN void stb__arr_deleten_(void **p, int size, int loc, int n STB__PARAMS);
STB_EXTERN void stb__arr_insertn_(void **p, int size, int loc, int n STB__PARAMS);
#define stb_arr_free(p) stb_arr_free_((void **) &(p))
#define stb__arr_copy stb__arr_copy_
#ifndef STB_MALLOC_WRAPPER
#define stb__arr_setsize stb__arr_setsize_
#define stb__arr_setlen stb__arr_setlen_
#define stb__arr_addlen stb__arr_addlen_
#define stb__arr_deleten stb__arr_deleten_
#define stb__arr_insertn stb__arr_insertn_
#else
#define stb__arr_addlen(p,s,n) stb__arr_addlen_(p,s,n,__FILE__,__LINE__)
#define stb__arr_setlen(p,s,n) stb__arr_setlen_(p,s,n,__FILE__,__LINE__)
#define stb__arr_setsize(p,s,n) stb__arr_setsize_(p,s,n,__FILE__,__LINE__)
#define stb__arr_deleten(p,s,i,n) stb__arr_deleten_(p,s,i,n,__FILE__,__LINE__)
#define stb__arr_insertn(p,s,i,n) stb__arr_insertn_(p,s,i,n,__FILE__,__LINE__)
#endif
#ifdef STB_DEFINE
static void *stb__arr_context;
void *stb_arr_malloc_parent(void *p)
{
void *q = stb__arr_context;
stb__arr_context = p;
return q;
}
void stb_arr_malloc(void **target, void *context)
{
stb__arr *q = (stb__arr *) stb_malloc(context, sizeof(*q));
q->len = q->limit = 0;
q->stb_malloc = 1;
q->signature = stb_arr_signature;
*target = (void *) (q+1);
}
static void * stb__arr_malloc(int size)
{
if (stb__arr_context)
return stb_malloc(stb__arr_context, size);
return malloc(size);
}
void * stb__arr_copy_(void *p, int elem_size)
{
stb__arr *q;
if (p == NULL) return p;
q = (stb__arr *) stb__arr_malloc(sizeof(*q) + elem_size * stb_arrhead2(p)->limit);
stb_arr_check2(p);
memcpy(q, stb_arrhead2(p), sizeof(*q) + elem_size * stb_arrhead2(p)->len);
q->stb_malloc = !!stb__arr_context;
return q+1;
}
void stb_arr_free_(void **pp)
{
void *p = *pp;
stb_arr_check2(p);
if (p) {
stb__arr *q = stb_arrhead2(p);
if (q->stb_malloc)
stb_free(q);
else
free(q);
}
*pp = NULL;
}
static void stb__arrsize_(void **pp, int size, int limit, int len STB__PARAMS)
{
void *p = *pp;
stb__arr *a;
stb_arr_check2(p);
if (p == NULL) {
if (len == 0 && size == 0) return;
a = (stb__arr *) stb__arr_malloc(sizeof(*a) + size*limit);
a->limit = limit;
a->len = len;
a->stb_malloc = !!stb__arr_context;
a->signature = stb_arr_signature;
} else {
a = stb_arrhead2(p);
a->len = len;
if (a->limit < limit) {
void *p;
if (a->limit >= 4 && limit < a->limit * 2)
limit = a->limit * 2;
if (a->stb_malloc)
p = stb_realloc(a, sizeof(*a) + limit*size);
else
#ifdef STB_MALLOC_WRAPPER
p = stb__realloc(a, sizeof(*a) + limit*size, file, line);
#else
p = realloc(a, sizeof(*a) + limit*size);
#endif
if (p) {
a = (stb__arr *) p;
a->limit = limit;
} else {
// throw an error!
}
}
}
a->len = stb_min(a->len, a->limit);
*pp = a+1;
}
void stb__arr_setsize_(void **pp, int size, int limit STB__PARAMS)
{
void *p = *pp;
stb_arr_check2(p);
stb__arrsize_(pp, size, limit, stb_arr_len2(p) STB__ARGS);
}
void stb__arr_setlen_(void **pp, int size, int newlen STB__PARAMS)
{
void *p = *pp;
stb_arr_check2(p);
if (stb_arrcurmax2(p) < newlen || p == NULL) {
stb__arrsize_(pp, size, newlen, newlen STB__ARGS);
} else {
stb_arrhead2(p)->len = newlen;
}
}
void stb__arr_addlen_(void **p, int size, int addlen STB__PARAMS)
{
stb__arr_setlen_(p, size, stb_arr_len2(*p) + addlen STB__ARGS);
}
void stb__arr_insertn_(void **pp, int size, int i, int n STB__PARAMS)
{
void *p = *pp;
if (n) {
int z;
if (p == NULL) {
stb__arr_addlen_(pp, size, n STB__ARGS);
return;
}
z = stb_arr_len2(p);
stb__arr_addlen_(&p, size, n STB__ARGS);
memmove((char *) p + (i+n)*size, (char *) p + i*size, size * (z-i));
}
*pp = p;
}
void stb__arr_deleten_(void **pp, int size, int i, int n STB__PARAMS)
{
void *p = *pp;
if (n) {
memmove((char *) p + i*size, (char *) p + (i+n)*size, size * (stb_arr_len2(p)-(i+n)));
stb_arrhead2(p)->len -= n;
}
*pp = p;
}
#endif
//////////////////////////////////////////////////////////////////////////////
//
// Hashing
//
// typical use for this is to make a power-of-two hash table.
//
// let N = size of table (2^n)
// let H = stb_hash(str)
// let S = stb_rehash(H) | 1
//
// then hash probe sequence P(i) for i=0..N-1
// P(i) = (H + S*i) & (N-1)
//
// the idea is that H has 32 bits of hash information, but the
// table has only, say, 2^20 entries so only uses 20 of the bits.
// then by rehashing the original H we get 2^12 different probe
// sequences for a given initial probe location. (So it's optimal
// for 64K tables and its optimality decreases past that.)
//
// ok, so I've added something that generates _two separate_
// 32-bit hashes simultaneously which should scale better to
// very large tables.
STB_EXTERN unsigned int stb_hash(char *str);
STB_EXTERN unsigned int stb_hashptr(void *p);
STB_EXTERN unsigned int stb_hashlen(char *str, int len);
STB_EXTERN unsigned int stb_rehash_improved(unsigned int v);
STB_EXTERN unsigned int stb_hash_fast(void *p, int len);
STB_EXTERN unsigned int stb_hash2(char *str, unsigned int *hash2_ptr);
STB_EXTERN unsigned int stb_hash_number(unsigned int hash);
#define stb_rehash(x) ((x) + ((x) >> 6) + ((x) >> 19))
#ifdef STB_DEFINE
unsigned int stb_hash(char *str)
{
unsigned int hash = 0;
while (*str)
hash = (hash << 7) + (hash >> 25) + *str++;
return hash + (hash >> 16);
}
unsigned int stb_hashlen(char *str, int len)
{
unsigned int hash = 0;
while (len-- > 0 && *str)
hash = (hash << 7) + (hash >> 25) + *str++;
return hash + (hash >> 16);
}
unsigned int stb_hashptr(void *p)
{
unsigned int x = (unsigned int)(size_t) p;
// typically lacking in low bits and high bits
x = stb_rehash(x);
x += x << 16;
// pearson's shuffle
x ^= x << 3;
x += x >> 5;
x ^= x << 2;
x += x >> 15;
x ^= x << 10;
return stb_rehash(x);
}
unsigned int stb_rehash_improved(unsigned int v)
{
return stb_hashptr((void *)(size_t) v);
}
unsigned int stb_hash2(char *str, unsigned int *hash2_ptr)
{
unsigned int hash1 = 0x3141592c;
unsigned int hash2 = 0x77f044ed;
while (*str) {
hash1 = (hash1 << 7) + (hash1 >> 25) + *str;
hash2 = (hash2 << 11) + (hash2 >> 21) + *str;
++str;
}
*hash2_ptr = hash2 + (hash1 >> 16);
return hash1 + (hash2 >> 16);
}
// Paul Hsieh hash
#define stb__get16(p) ((p)[0] | ((p)[1] << 8))
unsigned int stb_hash_fast(void *p, int len)
{
unsigned char *q = (unsigned char *) p;
unsigned int hash = len;
if (len <= 0 || q == NULL) return 0;
/* Main loop */
for (;len > 3; len -= 4) {
unsigned int val;
hash += stb__get16(q);
val = (stb__get16(q+2) << 11);
hash = (hash << 16) ^ hash ^ val;
q += 4;
hash += hash >> 11;
}
/* Handle end cases */
switch (len) {
case 3: hash += stb__get16(q);
hash ^= hash << 16;
hash ^= q[2] << 18;
hash += hash >> 11;
break;
case 2: hash += stb__get16(q);
hash ^= hash << 11;
hash += hash >> 17;
break;
case 1: hash += q[0];
hash ^= hash << 10;
hash += hash >> 1;
break;
case 0: break;
}
/* Force "avalanching" of final 127 bits */
hash ^= hash << 3;
hash += hash >> 5;
hash ^= hash << 4;
hash += hash >> 17;
hash ^= hash << 25;
hash += hash >> 6;
return hash;
}
unsigned int stb_hash_number(unsigned int hash)
{
hash ^= hash << 3;
hash += hash >> 5;
hash ^= hash << 4;
hash += hash >> 17;
hash ^= hash << 25;
hash += hash >> 6;
return hash;
}
#endif
#ifdef STB_PERFECT_HASH
//////////////////////////////////////////////////////////////////////////////
//
// Perfect hashing for ints/pointers
//
// This is mainly useful for making faster pointer-indexed tables
// that don't change frequently. E.g. for stb_ischar().
//
typedef struct
{
stb_uint32 addend;
stb_uint multiplicand;
stb_uint b_mask;
stb_uint8 small_bmap[16];
stb_uint16 *large_bmap;
stb_uint table_mask;
stb_uint32 *table;
} stb_perfect;
STB_EXTERN int stb_perfect_create(stb_perfect *,unsigned int*,int n);
STB_EXTERN void stb_perfect_destroy(stb_perfect *);
STB_EXTERN int stb_perfect_hash(stb_perfect *, unsigned int x);
extern int stb_perfect_hash_max_failures;
#ifdef STB_DEFINE
int stb_perfect_hash_max_failures;
int stb_perfect_hash(stb_perfect *p, unsigned int x)
{
stb_uint m = x * p->multiplicand;
stb_uint y = x >> 16;
stb_uint bv = (m >> 24) + y;
stb_uint av = (m + y) >> 12;
if (p->table == NULL) return -1; // uninitialized table fails
bv &= p->b_mask;
av &= p->table_mask;
if (p->large_bmap)
av ^= p->large_bmap[bv];
else
av ^= p->small_bmap[bv];
return p->table[av] == x ? av : -1;
}
static void stb__perfect_prehash(stb_perfect *p, stb_uint x, stb_uint16 *a, stb_uint16 *b)
{
stb_uint m = x * p->multiplicand;
stb_uint y = x >> 16;
stb_uint bv = (m >> 24) + y;
stb_uint av = (m + y) >> 12;
bv &= p->b_mask;
av &= p->table_mask;
*b = bv;
*a = av;
}
static unsigned long stb__perfect_rand(void)
{
static unsigned long stb__rand;
stb__rand = stb__rand * 2147001325 + 715136305;
return 0x31415926 ^ ((stb__rand >> 16) + (stb__rand << 16));
}
typedef struct {
unsigned short count;
unsigned short b;
unsigned short map;
unsigned short *entries;
} stb__slot;
static int stb__slot_compare(const void *p, const void *q)
{
stb__slot *a = (stb__slot *) p;
stb__slot *b = (stb__slot *) q;
return a->count > b->count ? -1 : a->count < b->count; // sort large to small
}
int stb_perfect_create(stb_perfect *p, unsigned int *v, int n)
{
unsigned int buffer1[64], buffer2[64], buffer3[64], buffer4[64], buffer5[32];
unsigned short *as = (unsigned short *) stb_temp(buffer1, sizeof(*v)*n);
unsigned short *bs = (unsigned short *) stb_temp(buffer2, sizeof(*v)*n);
unsigned short *entries = (unsigned short *) stb_temp(buffer4, sizeof(*entries) * n);
int size = 1 << stb_log2_ceil(n), bsize=8;
int failure = 0,i,j,k;
assert(n <= 32768);
p->large_bmap = NULL;
for(;;) {
stb__slot *bcount = (stb__slot *) stb_temp(buffer3, sizeof(*bcount) * bsize);
unsigned short *bloc = (unsigned short *) stb_temp(buffer5, sizeof(*bloc) * bsize);
unsigned short *e;
int bad=0;
p->addend = stb__perfect_rand();
p->multiplicand = stb__perfect_rand() | 1;
p->table_mask = size-1;
p->b_mask = bsize-1;
p->table = (stb_uint32 *) malloc(size * sizeof(*p->table));
for (i=0; i < bsize; ++i) {
bcount[i].b = i;
bcount[i].count = 0;
bcount[i].map = 0;
}
for (i=0; i < n; ++i) {
stb__perfect_prehash(p, v[i], as+i, bs+i);
++bcount[bs[i]].count;
}
qsort(bcount, bsize, sizeof(*bcount), stb__slot_compare);
e = entries; // now setup up their entries index
for (i=0; i < bsize; ++i) {
bcount[i].entries = e;
e += bcount[i].count;
bcount[i].count = 0;
bloc[bcount[i].b] = i;
}
// now fill them out
for (i=0; i < n; ++i) {
int b = bs[i];
int w = bloc[b];
bcount[w].entries[bcount[w].count++] = i;
}
stb_tempfree(buffer5,bloc);
// verify
for (i=0; i < bsize; ++i)
for (j=0; j < bcount[i].count; ++j)
assert(bs[bcount[i].entries[j]] == bcount[i].b);
memset(p->table, 0, size*sizeof(*p->table));
// check if any b has duplicate a
for (i=0; i < bsize; ++i) {
if (bcount[i].count > 1) {
for (j=0; j < bcount[i].count; ++j) {
if (p->table[as[bcount[i].entries[j]]])
bad = 1;
p->table[as[bcount[i].entries[j]]] = 1;
}
for (j=0; j < bcount[i].count; ++j) {
p->table[as[bcount[i].entries[j]]] = 0;
}
if (bad) break;
}
}
if (!bad) {
// go through the bs and populate the table, first fit
for (i=0; i < bsize; ++i) {
if (bcount[i].count) {
// go through the candidate table[b] values
for (j=0; j < size; ++j) {
// go through the a values and see if they fit
for (k=0; k < bcount[i].count; ++k) {
int a = as[bcount[i].entries[k]];
if (p->table[(a^j)&p->table_mask]) {
break; // fails
}
}
// if succeeded, accept
if (k == bcount[i].count) {
bcount[i].map = j;
for (k=0; k < bcount[i].count; ++k) {
int a = as[bcount[i].entries[k]];
p->table[(a^j)&p->table_mask] = 1;
}
break;
}
}
if (j == size)
break; // no match for i'th entry, so break out in failure
}
}
if (i == bsize) {
// success... fill out map
if (bsize <= 16 && size <= 256) {
p->large_bmap = NULL;
for (i=0; i < bsize; ++i)
p->small_bmap[bcount[i].b] = (stb_uint8) bcount[i].map;
} else {
p->large_bmap = (unsigned short *) malloc(sizeof(*p->large_bmap) * bsize);
for (i=0; i < bsize; ++i)
p->large_bmap[bcount[i].b] = bcount[i].map;
}
// initialize table to v[0], so empty slots will fail
for (i=0; i < size; ++i)
p->table[i] = v[0];
for (i=0; i < n; ++i)
if (p->large_bmap)
p->table[as[i] ^ p->large_bmap[bs[i]]] = v[i];
else
p->table[as[i] ^ p->small_bmap[bs[i]]] = v[i];
// and now validate that none of them collided
for (i=0; i < n; ++i)
assert(stb_perfect_hash(p, v[i]) >= 0);
stb_tempfree(buffer3, bcount);
break;
}
}
free(p->table);
p->table = NULL;
stb_tempfree(buffer3, bcount);
++failure;
if (failure >= 4 && bsize < size) bsize *= 2;
if (failure >= 8 && (failure & 3) == 0 && size < 4*n) {
size *= 2;
bsize *= 2;
}
if (failure == 6) {
// make sure the input data is unique, so we don't infinite loop
unsigned int *data = (unsigned int *) stb_temp(buffer3, n * sizeof(*data));
memcpy(data, v, sizeof(*data) * n);
qsort(data, n, sizeof(*data), stb_intcmp(0));
for (i=1; i < n; ++i) {
if (data[i] == data[i-1])
size = 0; // size is return value, so 0 it
}
stb_tempfree(buffer3, data);
if (!size) break;
}
}
if (failure > stb_perfect_hash_max_failures)
stb_perfect_hash_max_failures = failure;
stb_tempfree(buffer1, as);
stb_tempfree(buffer2, bs);
stb_tempfree(buffer4, entries);
return size;
}
void stb_perfect_destroy(stb_perfect *p)
{
if (p->large_bmap) free(p->large_bmap);
if (p->table ) free(p->table);
p->large_bmap = NULL;
p->table = NULL;
p->b_mask = 0;
p->table_mask = 0;
}
#endif
//////////////////////////////////////////////////////////////////////////////
//
// Perfect hash clients
STB_EXTERN int stb_ischar(char s, char *set);
#ifdef STB_DEFINE
int stb_ischar(char c, char *set)
{
static unsigned char bit[8] = { 1,2,4,8,16,32,64,128 };
static stb_perfect p;
static unsigned char (*tables)[256];
static char ** sets = NULL;
int z = stb_perfect_hash(&p, (int)(size_t) set);
if (z < 0) {
int i,k,n,j,f;
// special code that means free all existing data
if (set == NULL) {
stb_arr_free(sets);
free(tables);
tables = NULL;
stb_perfect_destroy(&p);
return 0;
}
stb_arr_push(sets, set);
stb_perfect_destroy(&p);
n = stb_perfect_create(&p, (unsigned int *) (char **) sets, stb_arr_len(sets));
assert(n != 0);
k = (n+7) >> 3;
tables = (unsigned char (*)[256]) realloc(tables, sizeof(*tables) * k);
memset(tables, 0, sizeof(*tables) * k);
for (i=0; i < stb_arr_len(sets); ++i) {
k = stb_perfect_hash(&p, (int)(size_t) sets[i]);
assert(k >= 0);
n = k >> 3;
f = bit[k&7];
for (j=0; !j || sets[i][j]; ++j) {
tables[n][(unsigned char) sets[i][j]] |= f;
}
}
z = stb_perfect_hash(&p, (int)(size_t) set);
}
return tables[z >> 3][(unsigned char) c] & bit[z & 7];
}
#endif
#endif
//////////////////////////////////////////////////////////////////////////////
//
// Instantiated data structures
//
// This is an attempt to implement a templated data structure.
//
// Hash table: call stb_define_hash(TYPE,N,KEY,K1,K2,HASH,VALUE)
// TYPE -- will define a structure type containing the hash table
// N -- the name, will prefix functions named:
// N create
// N destroy
// N get
// N set, N add, N update,
// N remove
// KEY -- the type of the key. 'x == y' must be valid
// K1,K2 -- keys never used by the app, used as flags in the hashtable
// HASH -- a piece of code ending with 'return' that hashes key 'k'
// VALUE -- the type of the value. 'x = y' must be valid
//
// Note that stb_define_hash_base can be used to define more sophisticated
// hash tables, e.g. those that make copies of the key or use special
// comparisons (e.g. strcmp).
#define STB_(prefix,name) stb__##prefix##name
#define STB__(prefix,name) prefix##name
#define STB__use(x) x
#define STB__skip(x)
#define stb_declare_hash(PREFIX,TYPE,N,KEY,VALUE) \
typedef struct stb__st_##TYPE TYPE;\
PREFIX int STB__(N, init)(TYPE *h, int count);\
PREFIX int STB__(N, memory_usage)(TYPE *h);\
PREFIX TYPE * STB__(N, create)(void);\
PREFIX TYPE * STB__(N, copy)(TYPE *h);\
PREFIX void STB__(N, destroy)(TYPE *h);\
PREFIX int STB__(N,get_flag)(TYPE *a, KEY k, VALUE *v);\
PREFIX VALUE STB__(N,get)(TYPE *a, KEY k);\
PREFIX int STB__(N, set)(TYPE *a, KEY k, VALUE v);\
PREFIX int STB__(N, add)(TYPE *a, KEY k, VALUE v);\
PREFIX int STB__(N, update)(TYPE*a,KEY k,VALUE v);\
PREFIX int STB__(N, remove)(TYPE *a, KEY k, VALUE *v);
#define STB_nocopy(x) (x)
#define STB_nodelete(x) 0
#define STB_nofields
#define STB_nonullvalue(x)
#define STB_nullvalue(x) x
#define STB_safecompare(x) x
#define STB_nosafe(x)
#define STB_noprefix
#ifdef __GNUC__
#define STB__nogcc(x)
#else
#define STB__nogcc(x) x
#endif
#define stb_define_hash_base(PREFIX,TYPE,FIELDS,N,NC,LOAD_FACTOR, \
KEY,EMPTY,DEL,COPY,DISPOSE,SAFE, \
VCOMPARE,CCOMPARE,HASH, \
VALUE,HASVNULL,VNULL) \
\
typedef struct \
{ \
KEY k; \
VALUE v; \
} STB_(N,_hashpair); \
\
STB__nogcc( typedef struct stb__st_##TYPE TYPE; ) \
struct stb__st_##TYPE { \
FIELDS \
STB_(N,_hashpair) *table; \
unsigned int mask; \
int count, limit; \
int deleted; \
\
int delete_threshhold; \
int grow_threshhold; \
int shrink_threshhold; \
unsigned char alloced, has_empty, has_del; \
VALUE ev; VALUE dv; \
}; \
\
static unsigned int STB_(N, hash)(KEY k) \
{ \
HASH \
} \
\
PREFIX int STB__(N, init)(TYPE *h, int count) \
{ \
int i; \
if (count < 4) count = 4; \
h->limit = count; \
h->count = 0; \
h->mask = count-1; \
h->deleted = 0; \
h->grow_threshhold = (int) (count * LOAD_FACTOR); \
h->has_empty = h->has_del = 0; \
h->alloced = 0; \
if (count <= 64) \
h->shrink_threshhold = 0; \
else \
h->shrink_threshhold = (int) (count * (LOAD_FACTOR/2.25)); \
h->delete_threshhold = (int) (count * (1-LOAD_FACTOR)/2); \
h->table = (STB_(N,_hashpair)*) malloc(sizeof(h->table[0]) * count); \
if (h->table == NULL) return 0; \
/* ideally this gets turned into a memset32 automatically */ \
for (i=0; i < count; ++i) \
h->table[i].k = EMPTY; \
return 1; \
} \
\
PREFIX int STB__(N, memory_usage)(TYPE *h) \
{ \
return sizeof(*h) + h->limit * sizeof(h->table[0]); \
} \
\
PREFIX TYPE * STB__(N, create)(void) \
{ \
TYPE *h = (TYPE *) malloc(sizeof(*h)); \
if (h) { \
if (STB__(N, init)(h, 16)) \
h->alloced = 1; \
else { free(h); h=NULL; } \
} \
return h; \
} \
\
PREFIX void STB__(N, destroy)(TYPE *a) \
{ \
int i; \
for (i=0; i < a->limit; ++i) \
if (!CCOMPARE(a->table[i].k,EMPTY) && !CCOMPARE(a->table[i].k, DEL)) \
DISPOSE(a->table[i].k); \
free(a->table); \
if (a->alloced) \
free(a); \
} \
\
static void STB_(N, rehash)(TYPE *a, int count); \
\
PREFIX int STB__(N,get_flag)(TYPE *a, KEY k, VALUE *v) \
{ \
unsigned int h = STB_(N, hash)(k); \
unsigned int n = h & a->mask, s; \
if (CCOMPARE(k,EMPTY)){ if (a->has_empty) *v = a->ev; return a->has_empty;}\
if (CCOMPARE(k,DEL)) { if (a->has_del ) *v = a->dv; return a->has_del; }\
if (CCOMPARE(a->table[n].k,EMPTY)) return 0; \
SAFE(if (!CCOMPARE(a->table[n].k,DEL))) \
if (VCOMPARE(a->table[n].k,k)) { *v = a->table[n].v; return 1; } \
s = stb_rehash(h) | 1; \
for(;;) { \
n = (n + s) & a->mask; \
if (CCOMPARE(a->table[n].k,EMPTY)) return 0; \
SAFE(if (CCOMPARE(a->table[n].k,DEL)) continue;) \
if (VCOMPARE(a->table[n].k,k)) \
{ *v = a->table[n].v; return 1; } \
} \
} \
\
HASVNULL( \
PREFIX VALUE STB__(N,get)(TYPE *a, KEY k) \
{ \
VALUE v; \
if (STB__(N,get_flag)(a,k,&v)) return v; \
else return VNULL; \
} \
) \
\
PREFIX int STB__(N,getkey)(TYPE *a, KEY k, KEY *kout) \
{ \
unsigned int h = STB_(N, hash)(k); \
unsigned int n = h & a->mask, s; \
if (CCOMPARE(k,EMPTY)||CCOMPARE(k,DEL)) return 0; \
if (CCOMPARE(a->table[n].k,EMPTY)) return 0; \
SAFE(if (!CCOMPARE(a->table[n].k,DEL))) \
if (VCOMPARE(a->table[n].k,k)) { *kout = a->table[n].k; return 1; } \
s = stb_rehash(h) | 1; \
for(;;) { \
n = (n + s) & a->mask; \
if (CCOMPARE(a->table[n].k,EMPTY)) return 0; \
SAFE(if (CCOMPARE(a->table[n].k,DEL)) continue;) \
if (VCOMPARE(a->table[n].k,k)) \
{ *kout = a->table[n].k; return 1; } \
} \
} \
\
static int STB_(N,addset)(TYPE *a, KEY k, VALUE v, \
int allow_new, int allow_old, int copy) \
{ \
unsigned int h = STB_(N, hash)(k); \
unsigned int n = h & a->mask; \
int b = -1; \
if (CCOMPARE(k,EMPTY)) { \
if (a->has_empty ? allow_old : allow_new) { \
n=a->has_empty; a->ev = v; a->has_empty = 1; return !n; \
} else return 0; \
} \
if (CCOMPARE(k,DEL)) { \
if (a->has_del ? allow_old : allow_new) { \
n=a->has_del; a->dv = v; a->has_del = 1; return !n; \
} else return 0; \
} \
if (!CCOMPARE(a->table[n].k, EMPTY)) { \
unsigned int s; \
if (CCOMPARE(a->table[n].k, DEL)) \
b = n; \
else if (VCOMPARE(a->table[n].k,k)) { \
if (allow_old) \
a->table[n].v = v; \
return !allow_new; \
} \
s = stb_rehash(h) | 1; \
for(;;) { \
n = (n + s) & a->mask; \
if (CCOMPARE(a->table[n].k, EMPTY)) break; \
if (CCOMPARE(a->table[n].k, DEL)) { \
if (b < 0) b = n; \
} else if (VCOMPARE(a->table[n].k,k)) { \
if (allow_old) \
a->table[n].v = v; \
return !allow_new; \
} \
} \
} \
if (!allow_new) return 0; \
if (b < 0) b = n; else --a->deleted; \
a->table[b].k = copy ? COPY(k) : k; \
a->table[b].v = v; \
++a->count; \
if (a->count > a->grow_threshhold) \
STB_(N,rehash)(a, a->limit*2); \
return 1; \
} \
\
PREFIX int STB__(N, set)(TYPE *a, KEY k, VALUE v){return STB_(N,addset)(a,k,v,1,1,1);}\
PREFIX int STB__(N, add)(TYPE *a, KEY k, VALUE v){return STB_(N,addset)(a,k,v,1,0,1);}\
PREFIX int STB__(N, update)(TYPE*a,KEY k,VALUE v){return STB_(N,addset)(a,k,v,0,1,1);}\
\
PREFIX int STB__(N, remove)(TYPE *a, KEY k, VALUE *v) \
{ \
unsigned int h = STB_(N, hash)(k); \
unsigned int n = h & a->mask, s; \
if (CCOMPARE(k,EMPTY)) { if (a->has_empty) { if(v)*v = a->ev; a->has_empty=0; return 1; } return 0; } \
if (CCOMPARE(k,DEL)) { if (a->has_del ) { if(v)*v = a->dv; a->has_del =0; return 1; } return 0; } \
if (CCOMPARE(a->table[n].k,EMPTY)) return 0; \
if (SAFE(CCOMPARE(a->table[n].k,DEL) || ) !VCOMPARE(a->table[n].k,k)) { \
s = stb_rehash(h) | 1; \
for(;;) { \
n = (n + s) & a->mask; \
if (CCOMPARE(a->table[n].k,EMPTY)) return 0; \
SAFE(if (CCOMPARE(a->table[n].k, DEL)) continue;) \
if (VCOMPARE(a->table[n].k,k)) break; \
} \
} \
DISPOSE(a->table[n].k); \
a->table[n].k = DEL; \
--a->count; \
++a->deleted; \
if (v != NULL) \
*v = a->table[n].v; \
if (a->count < a->shrink_threshhold) \
STB_(N, rehash)(a, a->limit >> 1); \
else if (a->deleted > a->delete_threshhold) \
STB_(N, rehash)(a, a->limit); \
return 1; \
} \
\
PREFIX TYPE * STB__(NC, copy)(TYPE *a) \
{ \
int i; \
TYPE *h = (TYPE *) malloc(sizeof(*h)); \
if (!h) return NULL; \
if (!STB__(N, init)(h, a->limit)) { free(h); return NULL; } \
h->count = a->count; \
h->deleted = a->deleted; \
h->alloced = 1; \
h->ev = a->ev; h->dv = a->dv; \
h->has_empty = a->has_empty; h->has_del = a->has_del; \
memcpy(h->table, a->table, h->limit * sizeof(h->table[0])); \
for (i=0; i < a->limit; ++i) \
if (!CCOMPARE(h->table[i].k,EMPTY) && !CCOMPARE(h->table[i].k,DEL)) \
h->table[i].k = COPY(h->table[i].k); \
return h; \
} \
\
static void STB_(N, rehash)(TYPE *a, int count) \
{ \
int i; \
TYPE b; \
STB__(N, init)(&b, count); \
for (i=0; i < a->limit; ++i) \
if (!CCOMPARE(a->table[i].k,EMPTY) && !CCOMPARE(a->table[i].k,DEL)) \
STB_(N,addset)(&b, a->table[i].k, a->table[i].v,1,1,0); \
free(a->table); \
a->table = b.table; \
a->mask = b.mask; \
a->count = b.count; \
a->limit = b.limit; \
a->deleted = b.deleted; \
a->delete_threshhold = b.delete_threshhold; \
a->grow_threshhold = b.grow_threshhold; \
a->shrink_threshhold = b.shrink_threshhold; \
}
#define STB_equal(a,b) ((a) == (b))
#define stb_define_hash(TYPE,N,KEY,EMPTY,DEL,HASH,VALUE) \
stb_define_hash_base(STB_noprefix, TYPE,STB_nofields,N,NC,0.85f, \
KEY,EMPTY,DEL,STB_nocopy,STB_nodelete,STB_nosafe, \
STB_equal,STB_equal,HASH, \
VALUE,STB_nonullvalue,0)
#define stb_define_hash_vnull(TYPE,N,KEY,EMPTY,DEL,HASH,VALUE,VNULL) \
stb_define_hash_base(STB_noprefix, TYPE,STB_nofields,N,NC,0.85f, \
KEY,EMPTY,DEL,STB_nocopy,STB_nodelete,STB_nosafe, \
STB_equal,STB_equal,HASH, \
VALUE,STB_nullvalue,VNULL)
//////////////////////////////////////////////////////////////////////////////
//
// stb_ptrmap
//
// An stb_ptrmap data structure is an O(1) hash table between pointers. One
// application is to let you store "extra" data associated with pointers,
// which is why it was originally called stb_extra.
stb_declare_hash(STB_EXTERN, stb_ptrmap, stb_ptrmap_, void *, void *)
stb_declare_hash(STB_EXTERN, stb_idict, stb_idict_, stb_int32, stb_int32)
stb_declare_hash(STB_EXTERN, stb_uidict, stbi_uidict_, stb_uint32, stb_uint32)
STB_EXTERN void stb_ptrmap_delete(stb_ptrmap *e, void (*free_func)(void *));
STB_EXTERN stb_ptrmap *stb_ptrmap_new(void);
STB_EXTERN stb_idict * stb_idict_new_size(int size);
STB_EXTERN void stb_idict_remove_all(stb_idict *e);
STB_EXTERN void stb_uidict_reset(stb_uidict *e);
#ifdef STB_DEFINE
#define STB_EMPTY ((void *) 2)
#define STB_EDEL ((void *) 6)
stb_define_hash_base(STB_noprefix,stb_ptrmap, STB_nofields, stb_ptrmap_,stb_ptrmap_,0.85f,
void *,STB_EMPTY,STB_EDEL,STB_nocopy,STB_nodelete,STB_nosafe,
STB_equal,STB_equal,return stb_hashptr(k);,
void *,STB_nullvalue,NULL)
stb_ptrmap *stb_ptrmap_new(void)
{
return stb_ptrmap_create();
}
void stb_ptrmap_delete(stb_ptrmap *e, void (*free_func)(void *))
{
int i;
if (free_func)
for (i=0; i < e->limit; ++i)
if (e->table[i].k != STB_EMPTY && e->table[i].k != STB_EDEL) {
if (free_func == free)
free(e->table[i].v); // allow STB_MALLOC_WRAPPER to operate
else
free_func(e->table[i].v);
}
stb_ptrmap_destroy(e);
}
// extra fields needed for stua_dict
#define STB_IEMPTY ((int) 1)
#define STB_IDEL ((int) 3)
stb_define_hash_base(STB_noprefix, stb_idict, short type; short gc; STB_nofields, stb_idict_,stb_idict_,0.95f,
stb_int32,STB_IEMPTY,STB_IDEL,STB_nocopy,STB_nodelete,STB_nosafe,
STB_equal,STB_equal,
return stb_rehash_improved(k);,stb_int32,STB_nonullvalue,0)
stb_idict * stb_idict_new_size(int size)
{
stb_idict *e = (stb_idict *) malloc(sizeof(*e));
if (e) {
if (!stb_is_pow2(size))
size = 1 << stb_log2_ceil(size);
stb_idict_init(e, size);
e->alloced = 1;
}
return e;
}
void stb_idict_remove_all(stb_idict *e)
{
int n;
for (n=0; n < e->limit; ++n)
e->table[n].k = STB_IEMPTY;
e->has_empty = e->has_del = 0;
e->count = 0;
e->deleted = 0;
}
stb_define_hash_base(STB_noprefix, stb_uidict, STB_nofields, stb_uidict_,stb_uidict_,0.85f,
stb_int32,0xffffffff,0xfffffffe,STB_nocopy,STB_nodelete,STB_nosafe,
STB_equal,STB_equal,
return stb_rehash_improved(k);,stb_uint32,STB_nonullvalue,0)
void stb_uidict_reset(stb_uidict *e)
{
int n;
for (n=0; n < e->limit; ++n)
e->table[n].k = 0xffffffff;
e->has_empty = e->has_del = 0;
e->count = 0;
e->deleted = 0;
}
#endif
//////////////////////////////////////////////////////////////////////////////
//
// stb_sparse_ptr_matrix
//
// An stb_ptrmap data structure is an O(1) hash table storing an arbitrary
// block of data for a given pair of pointers.
//
// If create=0, returns
typedef struct stb__st_stb_spmatrix stb_spmatrix;
STB_EXTERN stb_spmatrix * stb_sparse_ptr_matrix_new(int val_size);
STB_EXTERN void stb_sparse_ptr_matrix_free(stb_spmatrix *z);
STB_EXTERN void * stb_sparse_ptr_matrix_get(stb_spmatrix *z, void *a, void *b, int create);
#ifdef STB_DEFINE
typedef struct
{
void *a;
void *b;
} stb__ptrpair;
static stb__ptrpair stb__ptrpair_empty = { (void *) 1, (void *) 1 };
static stb__ptrpair stb__ptrpair_del = { (void *) 2, (void *) 2 };
#define STB__equal_ptrpair(x,y) ((x).a == (y).a && (x).b == (y).b)
stb_define_hash_base(STB_noprefix, stb_spmatrix, int val_size; void *arena;, stb__spmatrix_,stb__spmatrix_, 0.85,
stb__ptrpair, stb__ptrpair_empty, stb__ptrpair_del,
STB_nocopy, STB_nodelete, STB_nosafe,
STB__equal_ptrpair, STB__equal_ptrpair, return stb_rehash(stb_hashptr(k.a))+stb_hashptr(k.b);,
void *, STB_nullvalue, 0)
stb_spmatrix *stb_sparse_ptr_matrix_new(int val_size)
{
stb_spmatrix *m = stb__spmatrix_create();
if (m) m->val_size = val_size;
if (m) m->arena = stb_malloc_global(1);
return m;
}
void stb_sparse_ptr_matrix_free(stb_spmatrix *z)
{
if (z->arena) stb_free(z->arena);
stb__spmatrix_destroy(z);
}
void *stb_sparse_ptr_matrix_get(stb_spmatrix *z, void *a, void *b, int create)
{
stb__ptrpair t = { a,b };
void *data = stb__spmatrix_get(z, t);
if (!data && create) {
data = stb_malloc_raw(z->arena, z->val_size);
if (!data) return NULL;
memset(data, 0, z->val_size);
stb__spmatrix_add(z, t, data);
}
return data;
}
#endif
//////////////////////////////////////////////////////////////////////////////
//
// SDICT: Hash Table for Strings (symbol table)
//
// if "use_arena=1", then strings will be copied
// into blocks and never freed until the sdict is freed;
// otherwise they're malloc()ed and free()d on the fly.
// (specify use_arena=1 if you never stb_sdict_remove)
stb_declare_hash(STB_EXTERN, stb_sdict, stb_sdict_, char *, void *)
STB_EXTERN stb_sdict * stb_sdict_new(int use_arena);
STB_EXTERN stb_sdict * stb_sdict_copy(stb_sdict*);
STB_EXTERN void stb_sdict_delete(stb_sdict *);
STB_EXTERN void * stb_sdict_change(stb_sdict *, char *str, void *p);
STB_EXTERN int stb_sdict_count(stb_sdict *d);
STB_EXTERN int stb_sdict_internal_limit(stb_sdict *d);
STB_EXTERN char * stb_sdict_internal_key(stb_sdict *d, int n);
STB_EXTERN void * stb_sdict_internal_value(stb_sdict *d, int n);
#define stb_sdict_for(d,i,q,z) \
for(i=0; i < stb_sdict_internal_limit(d) ? (q=stb_sdict_internal_key(d,i),z=stb_sdict_internal_value(d,i),1) : 0; ++i) \
if (q==NULL||q==(void *) 1);else // reversed makes macro friendly
#ifdef STB_DEFINE
// if in same translation unit, for speed, don't call accessors
#undef stb_sdict_for
#define stb_sdict_for(d,i,q,z) \
for(i=0; i < (d)->limit ? (q=(d)->table[i].k,z=(d)->table[i].v,1) : 0; ++i) \
if (q==NULL||q==(void *) 1);else // reversed makes macro friendly
#define STB_DEL ((void *) 1)
#define STB_SDEL ((char *) 1)
#define stb_sdict__copy(x) \
stb_p_strcpy_s(a->arena ? stb_malloc_string(a->arena, strlen(x)+1) \
: (char *) malloc(strlen(x)+1), strlen(x)+1, x)
#define stb_sdict__dispose(x) if (!a->arena) free(x)
stb_define_hash_base(STB_noprefix, stb_sdict, void*arena;, stb_sdict_,stb_sdictinternal_, 0.85f,
char *, NULL, STB_SDEL, stb_sdict__copy, stb_sdict__dispose,
STB_safecompare, !strcmp, STB_equal, return stb_hash(k);,
void *, STB_nullvalue, NULL)
int stb_sdict_count(stb_sdict *a)
{
return a->count;
}
int stb_sdict_internal_limit(stb_sdict *a)
{
return a->limit;
}
char* stb_sdict_internal_key(stb_sdict *a, int n)
{
return a->table[n].k;
}
void* stb_sdict_internal_value(stb_sdict *a, int n)
{
return a->table[n].v;
}
stb_sdict * stb_sdict_new(int use_arena)
{
stb_sdict *d = stb_sdict_create();
if (d == NULL) return NULL;
d->arena = use_arena ? stb_malloc_global(1) : NULL;
return d;
}
stb_sdict* stb_sdict_copy(stb_sdict *old)
{
stb_sdict *n;
void *old_arena = old->arena;
void *new_arena = old_arena ? stb_malloc_global(1) : NULL;
old->arena = new_arena;
n = stb_sdictinternal_copy(old);
old->arena = old_arena;
if (n)
n->arena = new_arena;
else if (new_arena)
stb_free(new_arena);
return n;
}
void stb_sdict_delete(stb_sdict *d)
{
if (d->arena)
stb_free(d->arena);
stb_sdict_destroy(d);
}
void * stb_sdict_change(stb_sdict *d, char *str, void *p)
{
void *q = stb_sdict_get(d, str);
stb_sdict_set(d, str, p);
return q;
}
#endif
//////////////////////////////////////////////////////////////////////////////
//
// Instantiated data structures
//
// This is an attempt to implement a templated data structure.
// What you do is define a struct foo, and then include several
// pointer fields to struct foo in your struct. Then you call
// the instantiator, which creates the functions that implement
// the data structure. This requires massive undebuggable #defines,
// so we limit the cases where we do this.
//
// AA tree is an encoding of a 2-3 tree whereas RB trees encode a 2-3-4 tree;
// much simpler code due to fewer cases.
#define stb__bst_parent(x) x
#define stb__bst_noparent(x)
#define stb_bst_fields(N) \
*STB_(N,left), *STB_(N,right); \
unsigned char STB_(N,level)
#define stb_bst_fields_parent(N) \
*STB_(N,left), *STB_(N,right), *STB_(N,parent); \
unsigned char STB_(N,level)
#define STB__level(N,x) ((x) ? (x)->STB_(N,level) : 0)
#define stb_bst_base(TYPE, N, TREE, M, compare, PAR) \
\
static int STB_(N,_compare)(TYPE *p, TYPE *q) \
{ \
compare \
} \
\
static void STB_(N,setleft)(TYPE *q, TYPE *v) \
{ \
q->STB_(N,left) = v; \
PAR(if (v) v->STB_(N,parent) = q;) \
} \
\
static void STB_(N,setright)(TYPE *q, TYPE *v) \
{ \
q->STB_(N,right) = v; \
PAR(if (v) v->STB_(N,parent) = q;) \
} \
\
static TYPE *STB_(N,skew)(TYPE *q) \
{ \
if (q == NULL) return q; \
if (q->STB_(N,left) \
&& q->STB_(N,left)->STB_(N,level) == q->STB_(N,level)) { \
TYPE *p = q->STB_(N,left); \
STB_(N,setleft)(q, p->STB_(N,right)); \
STB_(N,setright)(p, q); \
return p; \
} \
return q; \
} \
\
static TYPE *STB_(N,split)(TYPE *p) \
{ \
TYPE *q = p->STB_(N,right); \
if (q && q->STB_(N,right) \
&& q->STB_(N,right)->STB_(N,level) == p->STB_(N,level)) { \
STB_(N,setright)(p, q->STB_(N,left)); \
STB_(N,setleft)(q,p); \
++q->STB_(N,level); \
return q; \
} \
return p; \
} \
\
TYPE *STB__(N,insert)(TYPE *tree, TYPE *item) \
{ \
int c; \
if (tree == NULL) { \
item->STB_(N,left) = NULL; \
item->STB_(N,right) = NULL; \
item->STB_(N,level) = 1; \
PAR(item->STB_(N,parent) = NULL;) \
return item; \
} \
c = STB_(N,_compare)(item,tree); \
if (c == 0) { \
if (item != tree) { \
STB_(N,setleft)(item, tree->STB_(N,left)); \
STB_(N,setright)(item, tree->STB_(N,right)); \
item->STB_(N,level) = tree->STB_(N,level); \
PAR(item->STB_(N,parent) = NULL;) \
} \
return item; \
} \
if (c < 0) \
STB_(N,setleft )(tree, STB__(N,insert)(tree->STB_(N,left), item)); \
else \
STB_(N,setright)(tree, STB__(N,insert)(tree->STB_(N,right), item)); \
tree = STB_(N,skew)(tree); \
tree = STB_(N,split)(tree); \
PAR(tree->STB_(N,parent) = NULL;) \
return tree; \
} \
\
TYPE *STB__(N,remove)(TYPE *tree, TYPE *item) \
{ \
static TYPE *delnode, *leaf, *restore; \
if (tree == NULL) return NULL; \
leaf = tree; \
if (STB_(N,_compare)(item, tree) < 0) { \
STB_(N,setleft)(tree, STB__(N,remove)(tree->STB_(N,left), item)); \
} else { \
TYPE *r; \
delnode = tree; \
r = STB__(N,remove)(tree->STB_(N,right), item); \
/* maybe move 'leaf' up to this location */ \
if (restore == tree) { tree = leaf; leaf = restore = NULL; } \
STB_(N,setright)(tree,r); \
assert(tree->STB_(N,right) != tree); \
} \
if (tree == leaf) { \
if (delnode == item) { \
tree = tree->STB_(N,right); \
assert(leaf->STB_(N,left) == NULL); \
/* move leaf (the right sibling) up to delnode */ \
STB_(N,setleft )(leaf, item->STB_(N,left )); \
STB_(N,setright)(leaf, item->STB_(N,right)); \
leaf->STB_(N,level) = item->STB_(N,level); \
if (leaf != item) \
restore = delnode; \
} \
delnode = NULL; \
} else { \
if (STB__level(N,tree->STB_(N,left) ) < tree->STB_(N,level)-1 || \
STB__level(N,tree->STB_(N,right)) < tree->STB_(N,level)-1) { \
--tree->STB_(N,level); \
if (STB__level(N,tree->STB_(N,right)) > tree->STB_(N,level)) \
tree->STB_(N,right)->STB_(N,level) = tree->STB_(N,level); \
tree = STB_(N,skew)(tree); \
STB_(N,setright)(tree, STB_(N,skew)(tree->STB_(N,right))); \
if (tree->STB_(N,right)) \
STB_(N,setright)(tree->STB_(N,right), \
STB_(N,skew)(tree->STB_(N,right)->STB_(N,right))); \
tree = STB_(N,split)(tree); \
if (tree->STB_(N,right)) \
STB_(N,setright)(tree, STB_(N,split)(tree->STB_(N,right))); \
} \
} \
PAR(if (tree) tree->STB_(N,parent) = NULL;) \
return tree; \
} \
\
TYPE *STB__(N,last)(TYPE *tree) \
{ \
if (tree) \
while (tree->STB_(N,right)) tree = tree->STB_(N,right); \
return tree; \
} \
\
TYPE *STB__(N,first)(TYPE *tree) \
{ \
if (tree) \
while (tree->STB_(N,left)) tree = tree->STB_(N,left); \
return tree; \
} \
\
TYPE *STB__(N,next)(TYPE *tree, TYPE *item) \
{ \
TYPE *next = NULL; \
if (item->STB_(N,right)) \
return STB__(N,first)(item->STB_(N,right)); \
PAR( \
while(item->STB_(N,parent)) { \
TYPE *up = item->STB_(N,parent); \
if (up->STB_(N,left) == item) return up; \
item = up; \
} \
return NULL; \
) \
while (tree != item) { \
if (STB_(N,_compare)(item, tree) < 0) { \
next = tree; \
tree = tree->STB_(N,left); \
} else { \
tree = tree->STB_(N,right); \
} \
} \
return next; \
} \
\
TYPE *STB__(N,prev)(TYPE *tree, TYPE *item) \
{ \
TYPE *next = NULL; \
if (item->STB_(N,left)) \
return STB__(N,last)(item->STB_(N,left)); \
PAR( \
while(item->STB_(N,parent)) { \
TYPE *up = item->STB_(N,parent); \
if (up->STB_(N,right) == item) return up; \
item = up; \
} \
return NULL; \
) \
while (tree != item) { \
if (STB_(N,_compare)(item, tree) < 0) { \
tree = tree->STB_(N,left); \
} else { \
next = tree; \
tree = tree->STB_(N,right); \
} \
} \
return next; \
} \
\
STB__DEBUG( \
void STB__(N,_validate)(TYPE *tree, int root) \
{ \
if (tree == NULL) return; \
PAR(if(root) assert(tree->STB_(N,parent) == NULL);) \
assert(STB__level(N,tree->STB_(N,left) ) == tree->STB_(N,level)-1); \
assert(STB__level(N,tree->STB_(N,right)) <= tree->STB_(N,level)); \
assert(STB__level(N,tree->STB_(N,right)) >= tree->STB_(N,level)-1); \
if (tree->STB_(N,right)) { \
assert(STB__level(N,tree->STB_(N,right)->STB_(N,right)) \
!= tree->STB_(N,level)); \
PAR(assert(tree->STB_(N,right)->STB_(N,parent) == tree);) \
} \
PAR(if(tree->STB_(N,left)) assert(tree->STB_(N,left)->STB_(N,parent) == tree);) \
STB__(N,_validate)(tree->STB_(N,left) ,0); \
STB__(N,_validate)(tree->STB_(N,right),0); \
} \
) \
\
typedef struct \
{ \
TYPE *root; \
} TREE; \
\
void STB__(M,Insert)(TREE *tree, TYPE *item) \
{ tree->root = STB__(N,insert)(tree->root, item); } \
void STB__(M,Remove)(TREE *tree, TYPE *item) \
{ tree->root = STB__(N,remove)(tree->root, item); } \
TYPE *STB__(M,Next)(TREE *tree, TYPE *item) \
{ return STB__(N,next)(tree->root, item); } \
TYPE *STB__(M,Prev)(TREE *tree, TYPE *item) \
{ return STB__(N,prev)(tree->root, item); } \
TYPE *STB__(M,First)(TREE *tree) { return STB__(N,first)(tree->root); } \
TYPE *STB__(M,Last) (TREE *tree) { return STB__(N,last) (tree->root); } \
void STB__(M,Init)(TREE *tree) { tree->root = NULL; }
#define stb_bst_find(N,tree,fcompare) \
{ \
int c; \
while (tree != NULL) { \
fcompare \
if (c == 0) return tree; \
if (c < 0) tree = tree->STB_(N,left); \
else tree = tree->STB_(N,right); \
} \
return NULL; \
}
#define stb_bst_raw(TYPE,N,TREE,M,vfield,VTYPE,compare,PAR) \
stb_bst_base(TYPE,N,TREE,M, \
VTYPE a = p->vfield; VTYPE b = q->vfield; return (compare);, PAR ) \
\
TYPE *STB__(N,find)(TYPE *tree, VTYPE a) \
stb_bst_find(N,tree,VTYPE b = tree->vfield; c = (compare);) \
TYPE *STB__(M,Find)(TREE *tree, VTYPE a) \
{ return STB__(N,find)(tree->root, a); }
#define stb_bst(TYPE,N,TREE,M,vfield,VTYPE,compare) \
stb_bst_raw(TYPE,N,TREE,M,vfield,VTYPE,compare,stb__bst_noparent)
#define stb_bst_parent(TYPE,N,TREE,M,vfield,VTYPE,compare) \
stb_bst_raw(TYPE,N,TREE,M,vfield,VTYPE,compare,stb__bst_parent)
//////////////////////////////////////////////////////////////////////////////
//
// Pointer Nulling
//
// This lets you automatically NULL dangling pointers to "registered"
// objects. Note that you have to make sure you call the appropriate
// functions when you free or realloc blocks of memory that contain
// pointers or pointer targets. stb.h can automatically do this for
// stb_arr, or for all frees/reallocs if it's wrapping them.
//
#ifdef STB_NPTR
STB_EXTERN void stb_nptr_set(void *address_of_pointer, void *value_to_write);
STB_EXTERN void stb_nptr_didset(void *address_of_pointer);
STB_EXTERN void stb_nptr_didfree(void *address_being_freed, int len);
STB_EXTERN void stb_nptr_free(void *address_being_freed, int len);
STB_EXTERN void stb_nptr_didrealloc(void *new_address, void *old_address, int len);
STB_EXTERN void stb_nptr_recache(void); // recache all known pointers
// do this after pointer sets outside your control, slow
#ifdef STB_DEFINE
// for fast updating on free/realloc, we need to be able to find
// all the objects (pointers and targets) within a given block;
// this precludes hashing
// we use a three-level hierarchy of memory to minimize storage:
// level 1: 65536 pointers to stb__memory_node (always uses 256 KB)
// level 2: each stb__memory_node represents a 64K block of memory
// with 256 stb__memory_leafs (worst case 64MB)
// level 3: each stb__memory_leaf represents 256 bytes of memory
// using a list of target locations and a list of pointers
// (which are hopefully fairly short normally!)
// this approach won't work in 64-bit, which has a much larger address
// space. need to redesign
#define STB__NPTR_ROOT_LOG2 16
#define STB__NPTR_ROOT_NUM (1 << STB__NPTR_ROOT_LOG2)
#define STB__NPTR_ROOT_SHIFT (32 - STB__NPTR_ROOT_LOG2)
#define STB__NPTR_NODE_LOG2 5
#define STB__NPTR_NODE_NUM (1 << STB__NPTR_NODE_LOG2)
#define STB__NPTR_NODE_MASK (STB__NPTR_NODE_NUM-1)
#define STB__NPTR_NODE_SHIFT (STB__NPTR_ROOT_SHIFT - STB__NPTR_NODE_LOG2)
#define STB__NPTR_NODE_OFFSET(x) (((x) >> STB__NPTR_NODE_SHIFT) & STB__NPTR_NODE_MASK)
typedef struct stb__st_nptr
{
void *ptr; // address of actual pointer
struct stb__st_nptr *next; // next pointer with same target
struct stb__st_nptr **prev; // prev pointer with same target, address of 'next' field (or first)
struct stb__st_nptr *next_in_block;
} stb__nptr;
typedef struct stb__st_nptr_target
{
void *ptr; // address of target
stb__nptr *first; // address of first nptr pointing to this
struct stb__st_nptr_target *next_in_block;
} stb__nptr_target;
typedef struct
{
stb__nptr *pointers;
stb__nptr_target *targets;
} stb__memory_leaf;
typedef struct
{
stb__memory_leaf *children[STB__NPTR_NODE_NUM];
} stb__memory_node;
stb__memory_node *stb__memtab_root[STB__NPTR_ROOT_NUM];
static stb__memory_leaf *stb__nptr_find_leaf(void *mem)
{
stb_uint32 address = (stb_uint32) mem;
stb__memory_node *z = stb__memtab_root[address >> STB__NPTR_ROOT_SHIFT];
if (z)
return z->children[STB__NPTR_NODE_OFFSET(address)];
else
return NULL;
}
static void * stb__nptr_alloc(int size)
{
return stb__realloc_raw(0,size);
}
static void stb__nptr_free(void *p)
{
stb__realloc_raw(p,0);
}
static stb__memory_leaf *stb__nptr_make_leaf(void *mem)
{
stb_uint32 address = (stb_uint32) mem;
stb__memory_node *z = stb__memtab_root[address >> STB__NPTR_ROOT_SHIFT];
stb__memory_leaf *f;
if (!z) {
int i;
z = (stb__memory_node *) stb__nptr_alloc(sizeof(*stb__memtab_root[0]));
stb__memtab_root[address >> STB__NPTR_ROOT_SHIFT] = z;
for (i=0; i < 256; ++i)
z->children[i] = 0;
}
f = (stb__memory_leaf *) stb__nptr_alloc(sizeof(*f));
z->children[STB__NPTR_NODE_OFFSET(address)] = f;
f->pointers = NULL;
f->targets = NULL;
return f;
}
static stb__nptr_target *stb__nptr_find_target(void *target, int force)
{
stb__memory_leaf *p = stb__nptr_find_leaf(target);
if (p) {
stb__nptr_target *t = p->targets;
while (t) {
if (t->ptr == target)
return t;
t = t->next_in_block;
}
}
if (force) {
stb__nptr_target *t = (stb__nptr_target*) stb__nptr_alloc(sizeof(*t));
if (!p) p = stb__nptr_make_leaf(target);
t->ptr = target;
t->first = NULL;
t->next_in_block = p->targets;
p->targets = t;
return t;
} else
return NULL;
}
static stb__nptr *stb__nptr_find_pointer(void *ptr, int force)
{
stb__memory_leaf *p = stb__nptr_find_leaf(ptr);
if (p) {
stb__nptr *t = p->pointers;
while (t) {
if (t->ptr == ptr)
return t;
t = t->next_in_block;
}
}
if (force) {
stb__nptr *t = (stb__nptr *) stb__nptr_alloc(sizeof(*t));
if (!p) p = stb__nptr_make_leaf(ptr);
t->ptr = ptr;
t->next = NULL;
t->prev = NULL;
t->next_in_block = p->pointers;
p->pointers = t;
return t;
} else
return NULL;
}
void stb_nptr_set(void *address_of_pointer, void *value_to_write)
{
if (*(void **)address_of_pointer != value_to_write) {
*(void **) address_of_pointer = value_to_write;
stb_nptr_didset(address_of_pointer);
}
}
void stb_nptr_didset(void *address_of_pointer)
{
// first unlink from old chain
void *new_address;
stb__nptr *p = stb__nptr_find_pointer(address_of_pointer, 1); // force building if doesn't exist
if (p->prev) { // if p->prev is NULL, we just built it, or it was NULL
*(p->prev) = p->next;
if (p->next) p->next->prev = p->prev;
}
// now add to new chain
new_address = *(void **)address_of_pointer;
if (new_address != NULL) {
stb__nptr_target *t = stb__nptr_find_target(new_address, 1);
p->next = t->first;
if (p->next) p->next->prev = &p->next;
p->prev = &t->first;
t->first = p;
} else {
p->prev = NULL;
p->next = NULL;
}
}
void stb__nptr_block(void *address, int len, void (*function)(stb__memory_leaf *f, int datum, void *start, void *end), int datum)
{
void *end_address = (void *) ((char *) address + len - 1);
stb__memory_node *n;
stb_uint32 start = (stb_uint32) address;
stb_uint32 end = start + len - 1;
int b0 = start >> STB__NPTR_ROOT_SHIFT;
int b1 = end >> STB__NPTR_ROOT_SHIFT;
int b=b0,i,e0,e1;
e0 = STB__NPTR_NODE_OFFSET(start);
if (datum <= 0) {
// first block
n = stb__memtab_root[b0];
if (n) {
if (b0 != b1)
e1 = STB__NPTR_NODE_NUM-1;
else
e1 = STB__NPTR_NODE_OFFSET(end);
for (i=e0; i <= e1; ++i)
if (n->children[i])
function(n->children[i], datum, address, end_address);
}
if (b1 > b0) {
// blocks other than the first and last block
for (b=b0+1; b < b1; ++b) {
n = stb__memtab_root[b];
if (n)
for (i=0; i <= STB__NPTR_NODE_NUM-1; ++i)
if (n->children[i])
function(n->children[i], datum, address, end_address);
}
// last block
n = stb__memtab_root[b1];
if (n) {
e1 = STB__NPTR_NODE_OFFSET(end);
for (i=0; i <= e1; ++i)
if (n->children[i])
function(n->children[i], datum, address, end_address);
}
}
} else {
if (b1 > b0) {
// last block
n = stb__memtab_root[b1];
if (n) {
e1 = STB__NPTR_NODE_OFFSET(end);
for (i=e1; i >= 0; --i)
if (n->children[i])
function(n->children[i], datum, address, end_address);
}
// blocks other than the first and last block
for (b=b1-1; b > b0; --b) {
n = stb__memtab_root[b];
if (n)
for (i=STB__NPTR_NODE_NUM-1; i >= 0; --i)
if (n->children[i])
function(n->children[i], datum, address, end_address);
}
}
// first block
n = stb__memtab_root[b0];
if (n) {
if (b0 != b1)
e1 = STB__NPTR_NODE_NUM-1;
else
e1 = STB__NPTR_NODE_OFFSET(end);
for (i=e1; i >= e0; --i)
if (n->children[i])
function(n->children[i], datum, address, end_address);
}
}
}
static void stb__nptr_delete_pointers(stb__memory_leaf *f, int offset, void *start, void *end)
{
stb__nptr **p = &f->pointers;
while (*p) {
stb__nptr *n = *p;
if (n->ptr >= start && n->ptr <= end) {
// unlink
if (n->prev) {
*(n->prev) = n->next;
if (n->next) n->next->prev = n->prev;
}
*p = n->next_in_block;
stb__nptr_free(n);
} else
p = &(n->next_in_block);
}
}
static void stb__nptr_delete_targets(stb__memory_leaf *f, int offset, void *start, void *end)
{
stb__nptr_target **p = &f->targets;
while (*p) {
stb__nptr_target *n = *p;
if (n->ptr >= start && n->ptr <= end) {
// null pointers
stb__nptr *z = n->first;
while (z) {
stb__nptr *y = z->next;
z->prev = NULL;
z->next = NULL;
*(void **) z->ptr = NULL;
z = y;
}
// unlink this target
*p = n->next_in_block;
stb__nptr_free(n);
} else
p = &(n->next_in_block);
}
}
void stb_nptr_didfree(void *address_being_freed, int len)
{
// step one: delete all pointers in this block
stb__nptr_block(address_being_freed, len, stb__nptr_delete_pointers, 0);
// step two: NULL all pointers to this block; do this second to avoid NULLing deleted pointers
stb__nptr_block(address_being_freed, len, stb__nptr_delete_targets, 0);
}
void stb_nptr_free(void *address_being_freed, int len)
{
free(address_being_freed);
stb_nptr_didfree(address_being_freed, len);
}
static void stb__nptr_move_targets(stb__memory_leaf *f, int offset, void *start, void *end)
{
stb__nptr_target **t = &f->targets;
while (*t) {
stb__nptr_target *n = *t;
if (n->ptr >= start && n->ptr <= end) {
stb__nptr *z;
stb__memory_leaf *f;
// unlink n
*t = n->next_in_block;
// update n to new address
n->ptr = (void *) ((char *) n->ptr + offset);
f = stb__nptr_find_leaf(n->ptr);
if (!f) f = stb__nptr_make_leaf(n->ptr);
n->next_in_block = f->targets;
f->targets = n;
// now go through all pointers and make them point here
z = n->first;
while (z) {
*(void**) z->ptr = n->ptr;
z = z->next;
}
} else
t = &(n->next_in_block);
}
}
static void stb__nptr_move_pointers(stb__memory_leaf *f, int offset, void *start, void *end)
{
stb__nptr **p = &f->pointers;
while (*p) {
stb__nptr *n = *p;
if (n->ptr >= start && n->ptr <= end) {
// unlink
*p = n->next_in_block;
n->ptr = (void *) ((int) n->ptr + offset);
// move to new block
f = stb__nptr_find_leaf(n->ptr);
if (!f) f = stb__nptr_make_leaf(n->ptr);
n->next_in_block = f->pointers;
f->pointers = n;
} else
p = &(n->next_in_block);
}
}
void stb_nptr_realloc(void *new_address, void *old_address, int len)
{
if (new_address == old_address) return;
// have to move the pointers first, because moving the targets
// requires writing to the pointers-to-the-targets, and if some of those moved too,
// we need to make sure we don't write to the old memory
// step one: move all pointers within the block
stb__nptr_block(old_address, len, stb__nptr_move_pointers, (char *) new_address - (char *) old_address);
// step two: move all targets within the block
stb__nptr_block(old_address, len, stb__nptr_move_targets, (char *) new_address - (char *) old_address);
}
void stb_nptr_move(void *new_address, void *old_address)
{
stb_nptr_realloc(new_address, old_address, 1);
}
void stb_nptr_recache(void)
{
int i,j;
for (i=0; i < STB__NPTR_ROOT_NUM; ++i)
if (stb__memtab_root[i])
for (j=0; j < STB__NPTR_NODE_NUM; ++j)
if (stb__memtab_root[i]->children[j]) {
stb__nptr *p = stb__memtab_root[i]->children[j]->pointers;
while (p) {
stb_nptr_didset(p->ptr);
p = p->next_in_block;
}
}
}
#endif // STB_DEFINE
#endif // STB_NPTR
//////////////////////////////////////////////////////////////////////////////
//
// File Processing
//
#ifdef _WIN32
#define stb_rename(x,y) _wrename((const wchar_t *)stb__from_utf8(x), (const wchar_t *)stb__from_utf8_alt(y))
#else
#define stb_rename rename
#endif
STB_EXTERN void stb_fput_varlen64(FILE *f, stb_uint64 v);
STB_EXTERN stb_uint64 stb_fget_varlen64(FILE *f);
STB_EXTERN int stb_size_varlen64(stb_uint64 v);
#define stb_filec (char *) stb_file
#define stb_fileu (unsigned char *) stb_file
STB_EXTERN void * stb_file(char *filename, size_t *length);
STB_EXTERN void * stb_file_max(char *filename, size_t *length);
STB_EXTERN size_t stb_filelen(FILE *f);
STB_EXTERN int stb_filewrite(char *filename, void *data, size_t length);
STB_EXTERN int stb_filewritestr(char *filename, char *data);
STB_EXTERN char ** stb_stringfile(char *filename, int *len);
STB_EXTERN char ** stb_stringfile_trimmed(char *name, int *len, char comm);
STB_EXTERN char * stb_fgets(char *buffer, int buflen, FILE *f);
STB_EXTERN char * stb_fgets_malloc(FILE *f);
STB_EXTERN int stb_fexists(char *filename);
STB_EXTERN int stb_fcmp(char *s1, char *s2);
STB_EXTERN int stb_feq(char *s1, char *s2);
STB_EXTERN time_t stb_ftimestamp(char *filename);
STB_EXTERN int stb_fullpath(char *abs, int abs_size, char *rel);
STB_EXTERN FILE * stb_fopen(char *filename, const char *mode);
STB_EXTERN int stb_fclose(FILE *f, int keep);
enum
{
stb_keep_no = 0,
stb_keep_yes = 1,
stb_keep_if_different = 2,
};
STB_EXTERN int stb_copyfile(char *src, char *dest);
STB_EXTERN void stb_fput_varlen64(FILE *f, stb_uint64 v);
STB_EXTERN stb_uint64 stb_fget_varlen64(FILE *f);
STB_EXTERN int stb_size_varlen64(stb_uint64 v);
STB_EXTERN void stb_fwrite32(FILE *f, stb_uint32 datum);
STB_EXTERN void stb_fput_varlen (FILE *f, int v);
STB_EXTERN void stb_fput_varlenu(FILE *f, unsigned int v);
STB_EXTERN int stb_fget_varlen (FILE *f);
STB_EXTERN stb_uint stb_fget_varlenu(FILE *f);
STB_EXTERN void stb_fput_ranged (FILE *f, int v, int b, stb_uint n);
STB_EXTERN int stb_fget_ranged (FILE *f, int b, stb_uint n);
STB_EXTERN int stb_size_varlen (int v);
STB_EXTERN int stb_size_varlenu(unsigned int v);
STB_EXTERN int stb_size_ranged (int b, stb_uint n);
STB_EXTERN int stb_fread(void *data, size_t len, size_t count, void *f);
STB_EXTERN int stb_fwrite(void *data, size_t len, size_t count, void *f);
#if 0
typedef struct
{
FILE *base_file;
char *buffer;
int buffer_size;
int buffer_off;
int buffer_left;
} STBF;
STB_EXTERN STBF *stb_tfopen(char *filename, char *mode);
STB_EXTERN int stb_tfread(void *data, size_t len, size_t count, STBF *f);
STB_EXTERN int stb_tfwrite(void *data, size_t len, size_t count, STBF *f);
#endif
#ifdef STB_DEFINE
#if 0
STBF *stb_tfopen(char *filename, char *mode)
{
STBF *z;
FILE *f = stb_p_fopen(filename, mode);
if (!f) return NULL;
z = (STBF *) malloc(sizeof(*z));
if (!z) { fclose(f); return NULL; }
z->base_file = f;
if (!strcmp(mode, "rb") || !strcmp(mode, "wb")) {
z->buffer_size = 4096;
z->buffer_off = z->buffer_size;
z->buffer_left = 0;
z->buffer = malloc(z->buffer_size);
if (!z->buffer) { free(z); fclose(f); return NULL; }
} else {
z->buffer = 0;
z->buffer_size = 0;
z->buffer_left = 0;
}
return z;
}
int stb_tfread(void *data, size_t len, size_t count, STBF *f)
{
int total = len*count, done=0;
if (!total) return 0;
if (total <= z->buffer_left) {
memcpy(data, z->buffer + z->buffer_off, total);
z->buffer_off += total;
z->buffer_left -= total;
return count;
} else {
char *out = (char *) data;
// consume all buffered data
memcpy(data, z->buffer + z->buffer_off, z->buffer_left);
done = z->buffer_left;
out += z->buffer_left;
z->buffer_left=0;
if (total-done > (z->buffer_size >> 1)) {
done += fread(out
}
}
}
#endif
void stb_fwrite32(FILE *f, stb_uint32 x)
{
fwrite(&x, 4, 1, f);
}
#if defined(_WIN32)
#define stb__stat _stat
#else
#define stb__stat stat
#endif
int stb_fexists(char *filename)
{
struct stb__stat buf;
return stb__windows(
_wstat((const wchar_t *)stb__from_utf8(filename), &buf),
stat(filename,&buf)
) == 0;
}
time_t stb_ftimestamp(char *filename)
{
struct stb__stat buf;
if (stb__windows(
_wstat((const wchar_t *)stb__from_utf8(filename), &buf),
stat(filename,&buf)
) == 0)
{
return buf.st_mtime;
} else {
return 0;
}
}
size_t stb_filelen(FILE *f)
{
long len, pos;
pos = ftell(f);
fseek(f, 0, SEEK_END);
len = ftell(f);
fseek(f, pos, SEEK_SET);
return (size_t) len;
}
void *stb_file(char *filename, size_t *length)
{
FILE *f = stb__fopen(filename, "rb");
char *buffer;
size_t len, len2;
if (!f) return NULL;
len = stb_filelen(f);
buffer = (char *) malloc(len+2); // nul + extra
len2 = fread(buffer, 1, len, f);
if (len2 == len) {
if (length) *length = len;
buffer[len] = 0;
} else {
free(buffer);
buffer = NULL;
}
fclose(f);
return buffer;
}
int stb_filewrite(char *filename, void *data, size_t length)
{
FILE *f = stb_fopen(filename, "wb");
if (f) {
unsigned char *data_ptr = (unsigned char *) data;
size_t remaining = length;
while (remaining > 0) {
size_t len2 = remaining > 65536 ? 65536 : remaining;
size_t len3 = fwrite(data_ptr, 1, len2, f);
if (len2 != len3) {
fprintf(stderr, "Failed while writing %s\n", filename);
break;
}
remaining -= len2;
data_ptr += len2;
}
stb_fclose(f, stb_keep_if_different);
}
return f != NULL;
}
int stb_filewritestr(char *filename, char *data)
{
return stb_filewrite(filename, data, strlen(data));
}
void * stb_file_max(char *filename, size_t *length)
{
FILE *f = stb__fopen(filename, "rb");
char *buffer;
size_t len, maxlen;
if (!f) return NULL;
maxlen = *length;
buffer = (char *) malloc(maxlen+1);
len = fread(buffer, 1, maxlen, f);
buffer[len] = 0;
fclose(f);
*length = len;
return buffer;
}
char ** stb_stringfile(char *filename, int *plen)
{
FILE *f = stb__fopen(filename, "rb");
char *buffer, **list=NULL, *s;
size_t len, count, i;
if (!f) return NULL;
len = stb_filelen(f);
buffer = (char *) malloc(len+1);
len = fread(buffer, 1, len, f);
buffer[len] = 0;
fclose(f);
// two passes through: first time count lines, second time set them
for (i=0; i < 2; ++i) {
s = buffer;
if (i == 1)
list[0] = s;
count = 1;
while (*s) {
if (*s == '\n' || *s == '\r') {
// detect if both cr & lf are together
int crlf = (s[0] + s[1]) == ('\n' + '\r');
if (i == 1) *s = 0;
if (crlf) ++s;
if (s[1]) { // it's not over yet
if (i == 1) list[count] = s+1;
++count;
}
}
++s;
}
if (i == 0) {
list = (char **) malloc(sizeof(*list) * (count+1) + len+1);
if (!list) return NULL;
list[count] = 0;
// recopy the file so there's just a single allocation to free
memcpy(&list[count+1], buffer, len+1);
free(buffer);
buffer = (char *) &list[count+1];
if (plen) *plen = (int) count;
}
}
return list;
}
char ** stb_stringfile_trimmed(char *name, int *len, char comment)
{
int i,n,o=0;
char **s = stb_stringfile(name, &n);
if (s == NULL) return NULL;
for (i=0; i < n; ++i) {
char *p = stb_skipwhite(s[i]);
if (*p && *p != comment)
s[o++] = p;
}
s[o] = NULL;
if (len) *len = o;
return s;
}
char * stb_fgets(char *buffer, int buflen, FILE *f)
{
char *p;
buffer[0] = 0;
p = fgets(buffer, buflen, f);
if (p) {
int n = (int) (strlen(p)-1);
if (n >= 0)
if (p[n] == '\n')
p[n] = 0;
}
return p;
}
char * stb_fgets_malloc(FILE *f)
{
// avoid reallocing for small strings
char quick_buffer[800];
quick_buffer[sizeof(quick_buffer)-2] = 0;
if (!fgets(quick_buffer, sizeof(quick_buffer), f))
return NULL;
if (quick_buffer[sizeof(quick_buffer)-2] == 0) {
size_t n = strlen(quick_buffer);
if (n > 0 && quick_buffer[n-1] == '\n')
quick_buffer[n-1] = 0;
return stb_p_strdup(quick_buffer);
} else {
char *p;
char *a = stb_p_strdup(quick_buffer);
size_t len = sizeof(quick_buffer)-1;
while (!feof(f)) {
if (a[len-1] == '\n') break;
a = (char *) realloc(a, len*2);
p = &a[len];
p[len-2] = 0;
if (!fgets(p, (int) len, f))
break;
if (p[len-2] == 0) {
len += strlen(p);
break;
}
len = len + (len-1);
}
if (a[len-1] == '\n')
a[len-1] = 0;
return a;
}
}
int stb_fullpath(char *abs, int abs_size, char *rel)
{
#ifdef _WIN32
return _fullpath(abs, rel, abs_size) != NULL;
#else
if (rel[0] == '/' || rel[0] == '~') {
if ((int) strlen(rel) >= abs_size)
return 0;
stb_p_strcpy_s(abs,65536,rel);
return STB_TRUE;
} else {
int n;
getcwd(abs, abs_size);
n = strlen(abs);
if (n+(int) strlen(rel)+2 <= abs_size) {
abs[n] = '/';
stb_p_strcpy_s(abs+n+1, 65536,rel);
return STB_TRUE;
} else {
return STB_FALSE;
}
}
#endif
}
static int stb_fcmp_core(FILE *f, FILE *g)
{
char buf1[1024],buf2[1024];
int n1,n2, res=0;
while (1) {
n1 = (int) fread(buf1, 1, sizeof(buf1), f);
n2 = (int) fread(buf2, 1, sizeof(buf2), g);
res = memcmp(buf1,buf2,stb_min(n1,n2));
if (res)
break;
if (n1 != n2) {
res = n1 < n2 ? -1 : 1;
break;
}
if (n1 == 0)
break;
}
fclose(f);
fclose(g);
return res;
}
int stb_fcmp(char *s1, char *s2)
{
FILE *f = stb__fopen(s1, "rb");
FILE *g = stb__fopen(s2, "rb");
if (f == NULL || g == NULL) {
if (f) fclose(f);
if (g) {
fclose(g);
return STB_TRUE;
}
return f != NULL;
}
return stb_fcmp_core(f,g);
}
int stb_feq(char *s1, char *s2)
{
FILE *f = stb__fopen(s1, "rb");
FILE *g = stb__fopen(s2, "rb");
if (f == NULL || g == NULL) {
if (f) fclose(f);
if (g) fclose(g);
return f == g;
}
// feq is faster because it shortcuts if they're different length
if (stb_filelen(f) != stb_filelen(g)) {
fclose(f);
fclose(g);
return 0;
}
return !stb_fcmp_core(f,g);
}
static stb_ptrmap *stb__files;
typedef struct
{
char *temp_name;
char *name;
int errors;
} stb__file_data;
static FILE *stb__open_temp_file(char *temp_name, char *src_name, const char *mode)
{
size_t p;
#ifdef _MSC_VER
int j;
#endif
FILE *f;
// try to generate a temporary file in the same directory
p = strlen(src_name)-1;
while (p > 0 && src_name[p] != '/' && src_name[p] != '\\'
&& src_name[p] != ':' && src_name[p] != '~')
--p;
++p;
memcpy(temp_name, src_name, p);
#ifdef _MSC_VER
// try multiple times to make a temp file... just in
// case some other process makes the name first
for (j=0; j < 32; ++j) {
stb_p_strcpy_s(temp_name+p, 65536, "stmpXXXXXX");
if (!stb_p_mktemp(temp_name))
return 0;
f = stb_p_fopen(temp_name, mode);
if (f != NULL)
break;
}
#else
{
stb_p_strcpy_s(temp_name+p, 65536, "stmpXXXXXX");
#ifdef __MINGW32__
int fd = open(stb_p_mktemp(temp_name), O_RDWR);
#else
int fd = mkstemp(temp_name);
#endif
if (fd == -1) return NULL;
f = fdopen(fd, mode);
if (f == NULL) {
unlink(temp_name);
close(fd);
return NULL;
}
}
#endif
return f;
}
FILE * stb_fopen(char *filename, const char *mode)
{
FILE *f;
char name_full[4096];
char temp_full[sizeof(name_full) + 12];
// @TODO: if the file doesn't exist, we can also use the fastpath here
if (mode[0] != 'w' && !strchr(mode, '+'))
return stb__fopen(filename, mode);
// save away the full path to the file so if the program
// changes the cwd everything still works right! unix has
// better ways to do this, but we have to work in windows
name_full[0] = '\0'; // stb_fullpath reads name_full[0]
if (stb_fullpath(name_full, sizeof(name_full), filename)==0)
return 0;
f = stb__open_temp_file(temp_full, name_full, mode);
if (f != NULL) {
stb__file_data *d = (stb__file_data *) malloc(sizeof(*d));
if (!d) { assert(0); /* NOTREACHED */fclose(f); return NULL; }
if (stb__files == NULL) stb__files = stb_ptrmap_create();
d->temp_name = stb_p_strdup(temp_full);
d->name = stb_p_strdup(name_full);
d->errors = 0;
stb_ptrmap_add(stb__files, f, d);
return f;
}
return NULL;
}
int stb_fclose(FILE *f, int keep)
{
stb__file_data *d;
int ok = STB_FALSE;
if (f == NULL) return 0;
if (ferror(f))
keep = stb_keep_no;
fclose(f);
if (stb__files && stb_ptrmap_remove(stb__files, f, (void **) &d)) {
if (stb__files->count == 0) {
stb_ptrmap_destroy(stb__files);
stb__files = NULL;
}
} else
return STB_TRUE; // not special
if (keep == stb_keep_if_different) {
// check if the files are identical
if (stb_feq(d->name, d->temp_name)) {
keep = stb_keep_no;
ok = STB_TRUE; // report success if no change
}
}
if (keep == stb_keep_no) {
remove(d->temp_name);
} else {
if (!stb_fexists(d->name)) {
// old file doesn't exist, so just move the new file over it
stb_rename(d->temp_name, d->name);
} else {
// don't delete the old file yet in case there are troubles! First rename it!
char preserved_old_file[4096];
// generate a temp filename in the same directory (also creates it, which we don't need)
FILE *dummy = stb__open_temp_file(preserved_old_file, d->name, "wb");
if (dummy != NULL) {
// we don't actually want the open file
fclose(dummy);
// discard what we just created
remove(preserved_old_file); // if this fails, there's nothing we can do, and following logic handles it as best as possible anyway
// move the existing file to the preserved name
if (0 != stb_rename(d->name, preserved_old_file)) { // 0 on success
// failed, state is:
// filename -> old file
// tempname -> new file
// keep tempname around so we don't lose data
} else {
// state is:
// preserved -> old file
// tempname -> new file
// move the new file to the old name
if (0 == stb_rename(d->temp_name, d->name)) {
// state is:
// preserved -> old file
// filename -> new file
ok = STB_TRUE;
// 'filename -> new file' has always been the goal, so clean up
remove(preserved_old_file); // nothing to be done if it fails
} else {
// couldn't rename, so try renaming preserved file back
// state is:
// preserved -> old file
// tempname -> new file
stb_rename(preserved_old_file, d->name);
// if the rename failed, there's nothing more we can do
}
}
} else {
// we couldn't get a temp filename. do this the naive way; the worst case failure here
// leaves the filename pointing to nothing and the new file as a tempfile
remove(d->name);
stb_rename(d->temp_name, d->name);
}
}
}
free(d->temp_name);
free(d->name);
free(d);
return ok;
}
int stb_copyfile(char *src, char *dest)
{
char raw_buffer[1024];
char *buffer;
int buf_size = 65536;
FILE *f, *g;
// if file already exists at destination, do nothing
if (stb_feq(src, dest)) return STB_TRUE;
// open file
f = stb__fopen(src, "rb");
if (f == NULL) return STB_FALSE;
// open file for writing
g = stb__fopen(dest, "wb");
if (g == NULL) {
fclose(f);
return STB_FALSE;
}
buffer = (char *) malloc(buf_size);
if (buffer == NULL) {
buffer = raw_buffer;
buf_size = sizeof(raw_buffer);
}
while (!feof(f)) {
size_t n = fread(buffer, 1, buf_size, f);
if (n != 0)
fwrite(buffer, 1, n, g);
}
fclose(f);
if (buffer != raw_buffer)
free(buffer);
fclose(g);
return STB_TRUE;
}
// varlen:
// v' = (v >> 31) + (v < 0 ? ~v : v)<<1; // small abs(v) => small v'
// output v as big endian v'+k for v' <= k:
// 1 byte : v' <= 0x00000080 ( -64 <= v < 64) 7 bits
// 2 bytes: v' <= 0x00004000 (-8192 <= v < 8192) 14 bits
// 3 bytes: v' <= 0x00200000 21 bits
// 4 bytes: v' <= 0x10000000 28 bits
// the number of most significant 1-bits in the first byte
// equals the number of bytes after the first
#define stb__varlen_xform(v) (v<0 ? (~v << 1)+1 : (v << 1))
int stb_size_varlen(int v) { return stb_size_varlenu(stb__varlen_xform(v)); }
int stb_size_varlenu(unsigned int v)
{
if (v < 0x00000080) return 1;
if (v < 0x00004000) return 2;
if (v < 0x00200000) return 3;
if (v < 0x10000000) return 4;
return 5;
}
void stb_fput_varlen(FILE *f, int v) { stb_fput_varlenu(f, stb__varlen_xform(v)); }
void stb_fput_varlenu(FILE *f, unsigned int z)
{
if (z >= 0x10000000) fputc(0xF0,f);
if (z >= 0x00200000) fputc((z < 0x10000000 ? 0xE0 : 0)+(z>>24),f);
if (z >= 0x00004000) fputc((z < 0x00200000 ? 0xC0 : 0)+(z>>16),f);
if (z >= 0x00000080) fputc((z < 0x00004000 ? 0x80 : 0)+(z>> 8),f);
fputc(z,f);
}
#define stb_fgetc(f) ((unsigned char) fgetc(f))
int stb_fget_varlen(FILE *f)
{
unsigned int z = stb_fget_varlenu(f);
return (z & 1) ? ~(z>>1) : (z>>1);
}
unsigned int stb_fget_varlenu(FILE *f)
{
unsigned int z;
unsigned char d;
d = stb_fgetc(f);
if (d >= 0x80) {
if (d >= 0xc0) {
if (d >= 0xe0) {
if (d == 0xf0) z = stb_fgetc(f) << 24;
else z = (d - 0xe0) << 24;
z += stb_fgetc(f) << 16;
}
else
z = (d - 0xc0) << 16;
z += stb_fgetc(f) << 8;
} else
z = (d - 0x80) << 8;
z += stb_fgetc(f);
} else
z = d;
return z;
}
stb_uint64 stb_fget_varlen64(FILE *f)
{
stb_uint64 z;
unsigned char d;
d = stb_fgetc(f);
if (d >= 0x80) {
if (d >= 0xc0) {
if (d >= 0xe0) {
if (d >= 0xf0) {
if (d >= 0xf8) {
if (d >= 0xfc) {
if (d >= 0xfe) {
if (d >= 0xff)
z = (stb_uint64) stb_fgetc(f) << 56;
else
z = (stb_uint64) (d - 0xfe) << 56;
z |= (stb_uint64) stb_fgetc(f) << 48;
} else z = (stb_uint64) (d - 0xfc) << 48;
z |= (stb_uint64) stb_fgetc(f) << 40;
} else z = (stb_uint64) (d - 0xf8) << 40;
z |= (stb_uint64) stb_fgetc(f) << 32;
} else z = (stb_uint64) (d - 0xf0) << 32;
z |= (stb_uint) stb_fgetc(f) << 24;
} else z = (stb_uint) (d - 0xe0) << 24;
z |= (stb_uint) stb_fgetc(f) << 16;
} else z = (stb_uint) (d - 0xc0) << 16;
z |= (stb_uint) stb_fgetc(f) << 8;
} else z = (stb_uint) (d - 0x80) << 8;
z |= stb_fgetc(f);
} else
z = d;
return (z & 1) ? ~(z >> 1) : (z >> 1);
}
int stb_size_varlen64(stb_uint64 v)
{
if (v < 0x00000080) return 1;
if (v < 0x00004000) return 2;
if (v < 0x00200000) return 3;
if (v < 0x10000000) return 4;
if (v < STB_IMM_UINT64(0x0000000800000000)) return 5;
if (v < STB_IMM_UINT64(0x0000040000000000)) return 6;
if (v < STB_IMM_UINT64(0x0002000000000000)) return 7;
if (v < STB_IMM_UINT64(0x0100000000000000)) return 8;
return 9;
}
void stb_fput_varlen64(FILE *f, stb_uint64 v)
{
stb_uint64 z = stb__varlen_xform(v);
int first=1;
if (z >= STB_IMM_UINT64(0x100000000000000)) {
fputc(0xff,f);
first=0;
}
if (z >= STB_IMM_UINT64(0x02000000000000)) fputc((first ? 0xFE : 0)+(char)(z>>56),f), first=0;
if (z >= STB_IMM_UINT64(0x00040000000000)) fputc((first ? 0xFC : 0)+(char)(z>>48),f), first=0;
if (z >= STB_IMM_UINT64(0x00000800000000)) fputc((first ? 0xF8 : 0)+(char)(z>>40),f), first=0;
if (z >= STB_IMM_UINT64(0x00000010000000)) fputc((first ? 0xF0 : 0)+(char)(z>>32),f), first=0;
if (z >= STB_IMM_UINT64(0x00000000200000)) fputc((first ? 0xE0 : 0)+(char)(z>>24),f), first=0;
if (z >= STB_IMM_UINT64(0x00000000004000)) fputc((first ? 0xC0 : 0)+(char)(z>>16),f), first=0;
if (z >= STB_IMM_UINT64(0x00000000000080)) fputc((first ? 0x80 : 0)+(char)(z>> 8),f), first=0;
fputc((char)z,f);
}
void stb_fput_ranged(FILE *f, int v, int b, stb_uint n)
{
v -= b;
if (n <= (1 << 31))
assert((stb_uint) v < n);
if (n > (1 << 24)) fputc(v >> 24, f);
if (n > (1 << 16)) fputc(v >> 16, f);
if (n > (1 << 8)) fputc(v >> 8, f);
fputc(v,f);
}
int stb_fget_ranged(FILE *f, int b, stb_uint n)
{
unsigned int v=0;
if (n > (1 << 24)) v += stb_fgetc(f) << 24;
if (n > (1 << 16)) v += stb_fgetc(f) << 16;
if (n > (1 << 8)) v += stb_fgetc(f) << 8;
v += stb_fgetc(f);
return b+v;
}
int stb_size_ranged(int b, stb_uint n)
{
if (n > (1 << 24)) return 4;
if (n > (1 << 16)) return 3;
if (n > (1 << 8)) return 2;
return 1;
}
void stb_fput_string(FILE *f, char *s)
{
size_t len = strlen(s);
stb_fput_varlenu(f, (unsigned int) len);
fwrite(s, 1, len, f);
}
// inverse of the above algorithm
char *stb_fget_string(FILE *f, void *p)
{
char *s;
int len = stb_fget_varlenu(f);
if (len > 4096) return NULL;
s = p ? stb_malloc_string(p, len+1) : (char *) malloc(len+1);
fread(s, 1, len, f);
s[len] = 0;
return s;
}
char *stb_strdup(char *str, void *pool)
{
size_t len = strlen(str);
char *p = stb_malloc_string(pool, len+1);
stb_p_strcpy_s(p, len+1, str);
return p;
}
// strip the trailing '/' or '\\' from a directory so we can refer to it
// as a file for _stat()
char *stb_strip_final_slash(char *t)
{
if (t[0]) {
char *z = t + strlen(t) - 1;
// *z is the last character
if (*z == '\\' || *z == '/')
if (z != t+2 || t[1] != ':') // but don't strip it if it's e.g. "c:/"
*z = 0;
if (*z == '\\')
*z = '/'; // canonicalize to make sure it matches db
}
return t;
}
char *stb_strip_final_slash_regardless(char *t)
{
if (t[0]) {
char *z = t + strlen(t) - 1;
// *z is the last character
if (*z == '\\' || *z == '/')
*z = 0;
if (*z == '\\')
*z = '/'; // canonicalize to make sure it matches db
}
return t;
}
#endif
//////////////////////////////////////////////////////////////////////////////
//
// Options parsing
//
STB_EXTERN char **stb_getopt_param(int *argc, char **argv, char *param);
STB_EXTERN char **stb_getopt(int *argc, char **argv);
STB_EXTERN void stb_getopt_free(char **opts);
#ifdef STB_DEFINE
void stb_getopt_free(char **opts)
{
int i;
char ** o2 = opts;
for (i=0; i < stb_arr_len(o2); ++i)
free(o2[i]);
stb_arr_free(o2);
}
char **stb_getopt(int *argc, char **argv)
{
return stb_getopt_param(argc, argv, (char*) "");
}
char **stb_getopt_param(int *argc, char **argv, char *param)
{
char ** opts=NULL;
int i,j=1;
for (i=1; i < *argc; ++i) {
if (argv[i][0] != '-') {
argv[j++] = argv[i];
} else {
if (argv[i][1] == 0) { // plain - == don't parse further options
++i;
while (i < *argc)
argv[j++] = argv[i++];
break;
} else if (argv[i][1] == '-') {
// copy argument through including initial '-' for clarity
stb_arr_push(opts, stb_p_strdup(argv[i]));
} else {
int k;
char *q = argv[i]; // traverse options list
for (k=1; q[k]; ++k) {
char *s;
if (strchr(param, q[k])) { // does it take a parameter?
char *t = &q[k+1], z = q[k];
size_t len=0;
if (*t == 0) {
if (i == *argc-1) { // takes a parameter, but none found
*argc = 0;
stb_getopt_free(opts);
return NULL;
}
t = argv[++i];
} else
k += (int) strlen(t);
len = strlen(t);
s = (char *) malloc(len+2);
if (!s) return NULL;
s[0] = z;
stb_p_strcpy_s(s+1, len+2, t);
} else {
// no parameter
s = (char *) malloc(2);
if (!s) return NULL;
s[0] = q[k];
s[1] = 0;
}
stb_arr_push(opts, s);
}
}
}
}
stb_arr_push(opts, NULL);
*argc = j;
return opts;
}
#endif
//////////////////////////////////////////////////////////////////////////////
//
// Portable directory reading
//
STB_EXTERN char **stb_readdir_files (char *dir);
STB_EXTERN char **stb_readdir_files_mask(char *dir, char *wild);
STB_EXTERN char **stb_readdir_subdirs(char *dir);
STB_EXTERN char **stb_readdir_subdirs_mask(char *dir, char *wild);
STB_EXTERN void stb_readdir_free (char **files);
STB_EXTERN char **stb_readdir_recursive(char *dir, char *filespec);
STB_EXTERN void stb_delete_directory_recursive(char *dir);
#ifdef STB_DEFINE
#ifdef _MSC_VER
#include <io.h>
#else
#include <unistd.h>
#include <dirent.h>
#endif
void stb_readdir_free(char **files)
{
char **f2 = files;
int i;
for (i=0; i < stb_arr_len(f2); ++i)
free(f2[i]);
stb_arr_free(f2);
}
static int isdotdirname(char *name)
{
if (name[0] == '.')
return (name[1] == '.') ? !name[2] : !name[1];
return 0;
}
STB_EXTERN int stb_wildmatchi(char *expr, char *candidate);
static char **readdir_raw(char *dir, int return_subdirs, char *mask)
{
char **results = NULL;
char buffer[4096], with_slash[4096];
size_t n;
#ifdef _MSC_VER
stb__wchar *ws;
struct _wfinddata_t data;
#ifdef _WIN64
const intptr_t none = -1;
intptr_t z;
#else
const long none = -1;
long z;
#endif
#else // !_MSC_VER
const DIR *none = NULL;
DIR *z;
#endif
n = stb_strscpy(buffer,dir,sizeof(buffer));
if (!n || n >= sizeof(buffer))
return NULL;
stb_fixpath(buffer);
n--;
if (n > 0 && (buffer[n-1] != '/')) {
buffer[n++] = '/';
}
buffer[n] = 0;
if (!stb_strscpy(with_slash,buffer,sizeof(with_slash)))
return NULL;
#ifdef _MSC_VER
if (!stb_strscpy(buffer+n,"*.*",sizeof(buffer)-n))
return NULL;
ws = stb__from_utf8(buffer);
z = _wfindfirst((const wchar_t *)ws, &data);
#else
z = opendir(dir);
#endif
if (z != none) {
int nonempty = STB_TRUE;
#ifndef _MSC_VER
struct dirent *data = readdir(z);
nonempty = (data != NULL);
#endif
if (nonempty) {
do {
int is_subdir;
#ifdef _MSC_VER
char *name = stb__to_utf8((stb__wchar *)data.name);
if (name == NULL) {
fprintf(stderr, "%s to convert '%S' to %s!\n", "Unable", data.name, "utf8");
continue;
}
is_subdir = !!(data.attrib & _A_SUBDIR);
#else
char *name = data->d_name;
if (!stb_strscpy(buffer+n,name,sizeof(buffer)-n))
break;
// Could follow DT_LNK, but would need to check for recursive links.
is_subdir = !!(data->d_type & DT_DIR);
#endif
if (is_subdir == return_subdirs) {
if (!is_subdir || !isdotdirname(name)) {
if (!mask || stb_wildmatchi(mask, name)) {
char buffer[4096],*p=buffer;
if ( stb_snprintf(buffer, sizeof(buffer), "%s%s", with_slash, name) < 0 )
break;
if (buffer[0] == '.' && buffer[1] == '/')
p = buffer+2;
stb_arr_push(results, stb_p_strdup(p));
}
}
}
}
#ifdef _MSC_VER
while (0 == _wfindnext(z, &data));
#else
while ((data = readdir(z)) != NULL);
#endif
}
#ifdef _MSC_VER
_findclose(z);
#else
closedir(z);
#endif
}
return results;
}
char **stb_readdir_files (char *dir) { return readdir_raw(dir, 0, NULL); }
char **stb_readdir_subdirs(char *dir) { return readdir_raw(dir, 1, NULL); }
char **stb_readdir_files_mask(char *dir, char *wild) { return readdir_raw(dir, 0, wild); }
char **stb_readdir_subdirs_mask(char *dir, char *wild) { return readdir_raw(dir, 1, wild); }
int stb__rec_max=0x7fffffff;
static char **stb_readdir_rec(char **sofar, char *dir, char *filespec)
{
char **files;
char ** dirs;
char **p;
if (stb_arr_len(sofar) >= stb__rec_max) return sofar;
files = stb_readdir_files_mask(dir, filespec);
stb_arr_for(p, files) {
stb_arr_push(sofar, stb_p_strdup(*p));
if (stb_arr_len(sofar) >= stb__rec_max) break;
}
stb_readdir_free(files);
if (stb_arr_len(sofar) >= stb__rec_max) return sofar;
dirs = stb_readdir_subdirs(dir);
stb_arr_for(p, dirs)
sofar = stb_readdir_rec(sofar, *p, filespec);
stb_readdir_free(dirs);
return sofar;
}
char **stb_readdir_recursive(char *dir, char *filespec)
{
return stb_readdir_rec(NULL, dir, filespec);
}
void stb_delete_directory_recursive(char *dir)
{
char **list = stb_readdir_subdirs(dir);
int i;
for (i=0; i < stb_arr_len(list); ++i)
stb_delete_directory_recursive(list[i]);
stb_arr_free(list);
list = stb_readdir_files(dir);
for (i=0; i < stb_arr_len(list); ++i)
if (!remove(list[i])) {
// on windows, try again after making it writeable; don't ALWAYS
// do this first since that would be slow in the normal case
#ifdef _MSC_VER
_chmod(list[i], _S_IWRITE);
remove(list[i]);
#endif
}
stb_arr_free(list);
stb__windows(_rmdir,rmdir)(dir);
}
#endif
//////////////////////////////////////////////////////////////////////////////
//
// construct trees from filenames; useful for cmirror summaries
typedef struct stb_dirtree2 stb_dirtree2;
struct stb_dirtree2
{
stb_dirtree2 **subdirs;
// make convenient for stb_summarize_tree
int num_subdir;
float weight;
// actual data
char *fullpath;
char *relpath;
char **files;
};
STB_EXTERN stb_dirtree2 *stb_dirtree2_from_files_relative(char *src, char **filelist, int count);
STB_EXTERN stb_dirtree2 *stb_dirtree2_from_files(char **filelist, int count);
STB_EXTERN int stb_dir_is_prefix(char *dir, int dirlen, char *file);
#ifdef STB_DEFINE
int stb_dir_is_prefix(char *dir, int dirlen, char *file)
{
if (dirlen == 0) return STB_TRUE;
if (stb_strnicmp(dir, file, dirlen)) return STB_FALSE;
if (file[dirlen] == '/' || file[dirlen] == '\\') return STB_TRUE;
return STB_FALSE;
}
stb_dirtree2 *stb_dirtree2_from_files_relative(char *src, char **filelist, int count)
{
char buffer1[1024];
int i;
int dlen = (int) strlen(src), elen;
stb_dirtree2 *d;
char ** descendents = NULL;
char ** files = NULL;
char *s;
if (!count) return NULL;
// first find all the ones that belong here... note this is will take O(NM) with N files and M subdirs
for (i=0; i < count; ++i) {
if (stb_dir_is_prefix(src, dlen, filelist[i])) {
stb_arr_push(descendents, filelist[i]);
}
}
if (descendents == NULL)
return NULL;
elen = dlen;
// skip a leading slash
if (elen == 0 && (descendents[0][0] == '/' || descendents[0][0] == '\\'))
++elen;
else if (elen)
++elen;
// now extract all the ones that have their root here
for (i=0; i < stb_arr_len(descendents);) {
if (!stb_strchr2(descendents[i]+elen, '/', '\\')) {
stb_arr_push(files, descendents[i]);
descendents[i] = descendents[stb_arr_len(descendents)-1];
stb_arr_pop(descendents);
} else
++i;
}
// now create a record
d = (stb_dirtree2 *) malloc(sizeof(*d));
d->files = files;
d->subdirs = NULL;
d->fullpath = stb_p_strdup(src);
s = stb_strrchr2(d->fullpath, '/', '\\');
if (s)
++s;
else
s = d->fullpath;
d->relpath = s;
// now create the children
qsort(descendents, stb_arr_len(descendents), sizeof(char *), stb_qsort_stricmp(0));
buffer1[0] = 0;
for (i=0; i < stb_arr_len(descendents); ++i) {
char buffer2[1024];
char *s = descendents[i] + elen, *t;
t = stb_strchr2(s, '/', '\\');
assert(t);
stb_strncpy(buffer2, descendents[i], (int) (t-descendents[i]+1));
if (stb_stricmp(buffer1, buffer2)) {
stb_dirtree2 *t = stb_dirtree2_from_files_relative(buffer2, descendents, stb_arr_len(descendents));
assert(t != NULL);
stb_p_strcpy_s(buffer1, sizeof(buffer1), buffer2);
stb_arr_push(d->subdirs, t);
}
}
d->num_subdir = stb_arr_len(d->subdirs);
d->weight = 0;
return d;
}
stb_dirtree2 *stb_dirtree2_from_files(char **filelist, int count)
{
return stb_dirtree2_from_files_relative((char*) "", filelist, count);
}
#endif
//////////////////////////////////////////////////////////////////////////////
//
// Checksums: CRC-32, ADLER32, SHA-1
//
// CRC-32 and ADLER32 allow streaming blocks
// SHA-1 requires either a complete buffer, max size 2^32 - 73
// or it can checksum directly from a file, max 2^61
#define STB_ADLER32_SEED 1
#define STB_CRC32_SEED 0 // note that we logical NOT this in the code
STB_EXTERN stb_uint
stb_adler32(stb_uint adler32, stb_uchar *buffer, stb_uint buflen);
STB_EXTERN stb_uint
stb_crc32_block(stb_uint crc32, stb_uchar *buffer, stb_uint len);
STB_EXTERN stb_uint stb_crc32(unsigned char *buffer, stb_uint len);
STB_EXTERN void stb_sha1(
unsigned char output[20], unsigned char *buffer, unsigned int len);
STB_EXTERN int stb_sha1_file(unsigned char output[20], char *file);
STB_EXTERN void stb_sha1_readable(char display[27], unsigned char sha[20]);
#ifdef STB_DEFINE
stb_uint stb_crc32_block(stb_uint crc, unsigned char *buffer, stb_uint len)
{
static stb_uint crc_table[256];
stb_uint i,j,s;
crc = ~crc;
if (crc_table[1] == 0)
for(i=0; i < 256; i++) {
for (s=i, j=0; j < 8; ++j)
s = (s >> 1) ^ (s & 1 ? 0xedb88320 : 0);
crc_table[i] = s;
}
for (i=0; i < len; ++i)
crc = (crc >> 8) ^ crc_table[buffer[i] ^ (crc & 0xff)];
return ~crc;
}
stb_uint stb_crc32(unsigned char *buffer, stb_uint len)
{
return stb_crc32_block(0, buffer, len);
}
stb_uint stb_adler32(stb_uint adler32, stb_uchar *buffer, stb_uint buflen)
{
const unsigned long ADLER_MOD = 65521;
unsigned long s1 = adler32 & 0xffff, s2 = adler32 >> 16;
unsigned long blocklen, i;
blocklen = buflen % 5552;
while (buflen) {
for (i=0; i + 7 < blocklen; i += 8) {
s1 += buffer[0], s2 += s1;
s1 += buffer[1], s2 += s1;
s1 += buffer[2], s2 += s1;
s1 += buffer[3], s2 += s1;
s1 += buffer[4], s2 += s1;
s1 += buffer[5], s2 += s1;
s1 += buffer[6], s2 += s1;
s1 += buffer[7], s2 += s1;
buffer += 8;
}
for (; i < blocklen; ++i)
s1 += *buffer++, s2 += s1;
s1 %= ADLER_MOD, s2 %= ADLER_MOD;
buflen -= blocklen;
blocklen = 5552;
}
return (s2 << 16) + s1;
}
static void stb__sha1(stb_uchar *chunk, stb_uint h[5])
{
int i;
stb_uint a,b,c,d,e;
stb_uint w[80];
for (i=0; i < 16; ++i)
w[i] = stb_big32(&chunk[i*4]);
for (i=16; i < 80; ++i) {
stb_uint t;
t = w[i-3] ^ w[i-8] ^ w[i-14] ^ w[i-16];
w[i] = (t + t) | (t >> 31);
}
a = h[0];
b = h[1];
c = h[2];
d = h[3];
e = h[4];
#define STB__SHA1(k,f) \
{ \
stb_uint temp = (a << 5) + (a >> 27) + (f) + e + (k) + w[i]; \
e = d; \
d = c; \
c = (b << 30) + (b >> 2); \
b = a; \
a = temp; \
}
i=0;
for (; i < 20; ++i) STB__SHA1(0x5a827999, d ^ (b & (c ^ d)) );
for (; i < 40; ++i) STB__SHA1(0x6ed9eba1, b ^ c ^ d );
for (; i < 60; ++i) STB__SHA1(0x8f1bbcdc, (b & c) + (d & (b ^ c)) );
for (; i < 80; ++i) STB__SHA1(0xca62c1d6, b ^ c ^ d );
#undef STB__SHA1
h[0] += a;
h[1] += b;
h[2] += c;
h[3] += d;
h[4] += e;
}
void stb_sha1(stb_uchar output[20], stb_uchar *buffer, stb_uint len)
{
unsigned char final_block[128];
stb_uint end_start, final_len, j;
int i;
stb_uint h[5];
h[0] = 0x67452301;
h[1] = 0xefcdab89;
h[2] = 0x98badcfe;
h[3] = 0x10325476;
h[4] = 0xc3d2e1f0;
// we need to write padding to the last one or two
// blocks, so build those first into 'final_block'
// we have to write one special byte, plus the 8-byte length
// compute the block where the data runs out
end_start = len & ~63;
// compute the earliest we can encode the length
if (((len+9) & ~63) == end_start) {
// it all fits in one block, so fill a second-to-last block
end_start -= 64;
}
final_len = end_start + 128;
// now we need to copy the data in
assert(end_start + 128 >= len+9);
assert(end_start < len || len < 64-9);
j = 0;
if (end_start > len)
j = (stb_uint) - (int) end_start;
for (; end_start + j < len; ++j)
final_block[j] = buffer[end_start + j];
final_block[j++] = 0x80;
while (j < 128-5) // 5 byte length, so write 4 extra padding bytes
final_block[j++] = 0;
// big-endian size
final_block[j++] = len >> 29;
final_block[j++] = len >> 21;
final_block[j++] = len >> 13;
final_block[j++] = len >> 5;
final_block[j++] = len << 3;
assert(j == 128 && end_start + j == final_len);
for (j=0; j < final_len; j += 64) { // 512-bit chunks
if (j+64 >= end_start+64)
stb__sha1(&final_block[j - end_start], h);
else
stb__sha1(&buffer[j], h);
}
for (i=0; i < 5; ++i) {
output[i*4 + 0] = h[i] >> 24;
output[i*4 + 1] = h[i] >> 16;
output[i*4 + 2] = h[i] >> 8;
output[i*4 + 3] = h[i] >> 0;
}
}
#ifdef _MSC_VER
int stb_sha1_file(stb_uchar output[20], char *file)
{
int i;
stb_uint64 length=0;
unsigned char buffer[128];
FILE *f = stb__fopen(file, "rb");
stb_uint h[5];
if (f == NULL) return 0; // file not found
h[0] = 0x67452301;
h[1] = 0xefcdab89;
h[2] = 0x98badcfe;
h[3] = 0x10325476;
h[4] = 0xc3d2e1f0;
for(;;) {
size_t n = fread(buffer, 1, 64, f);
if (n == 64) {
stb__sha1(buffer, h);
length += n;
} else {
int block = 64;
length += n;
buffer[n++] = 0x80;
// if there isn't enough room for the length, double the block
if (n + 8 > 64)
block = 128;
// pad to end
memset(buffer+n, 0, block-8-n);
i = block - 8;
buffer[i++] = (stb_uchar) (length >> 53);
buffer[i++] = (stb_uchar) (length >> 45);
buffer[i++] = (stb_uchar) (length >> 37);
buffer[i++] = (stb_uchar) (length >> 29);
buffer[i++] = (stb_uchar) (length >> 21);
buffer[i++] = (stb_uchar) (length >> 13);
buffer[i++] = (stb_uchar) (length >> 5);
buffer[i++] = (stb_uchar) (length << 3);
assert(i == block);
stb__sha1(buffer, h);
if (block == 128)
stb__sha1(buffer+64, h);
else
assert(block == 64);
break;
}
}
fclose(f);
for (i=0; i < 5; ++i) {
output[i*4 + 0] = h[i] >> 24;
output[i*4 + 1] = h[i] >> 16;
output[i*4 + 2] = h[i] >> 8;
output[i*4 + 3] = h[i] >> 0;
}
return 1;
}
#endif // _MSC_VER
// client can truncate this wherever they like
void stb_sha1_readable(char display[27], unsigned char sha[20])
{
char encoding[65] = "0123456789abcdefghijklmnopqrstuv"
"wxyzABCDEFGHIJKLMNOPQRSTUVWXYZ%$";
int num_bits = 0, acc=0;
int i=0,o=0;
while (o < 26) {
int v;
// expand the accumulator
if (num_bits < 6) {
assert(i != 20);
acc += sha[i++] << num_bits;
num_bits += 8;
}
v = acc & ((1 << 6) - 1);
display[o++] = encoding[v];
acc >>= 6;
num_bits -= 6;
}
assert(num_bits == 20*8 - 26*6);
display[o++] = encoding[acc];
}
#endif // STB_DEFINE
///////////////////////////////////////////////////////////
//
// simplified WINDOWS registry interface... hopefully
// we'll never actually use this?
#if defined(_WIN32)
STB_EXTERN void * stb_reg_open(const char *mode, const char *where); // mode: "rHKLM" or "rHKCU" or "w.."
STB_EXTERN void stb_reg_close(void *reg);
STB_EXTERN int stb_reg_read(void *zreg, const char *str, void *data, unsigned long len);
STB_EXTERN int stb_reg_read_string(void *zreg, const char *str, char *data, int len);
STB_EXTERN void stb_reg_write(void *zreg, const char *str, const void *data, unsigned long len);
STB_EXTERN void stb_reg_write_string(void *zreg, const char *str, const char *data);
#if defined(STB_DEFINE) && !defined(STB_NO_REGISTRY)
#define STB_HAS_REGISTRY
#ifndef _WINDOWS_
#define HKEY void *
STB_EXTERN __declspec(dllimport) long __stdcall RegCloseKey ( HKEY hKey );
STB_EXTERN __declspec(dllimport) long __stdcall RegCreateKeyExA ( HKEY hKey, const char * lpSubKey,
int Reserved, char * lpClass, int dwOptions,
int samDesired, void *lpSecurityAttributes, HKEY * phkResult, int * lpdwDisposition );
STB_EXTERN __declspec(dllimport) long __stdcall RegDeleteKeyA ( HKEY hKey, const char * lpSubKey );
STB_EXTERN __declspec(dllimport) long __stdcall RegQueryValueExA ( HKEY hKey, const char * lpValueName,
int * lpReserved, unsigned long * lpType, unsigned char * lpData, unsigned long * lpcbData );
STB_EXTERN __declspec(dllimport) long __stdcall RegSetValueExA ( HKEY hKey, const char * lpValueName,
int Reserved, int dwType, const unsigned char* lpData, int cbData );
STB_EXTERN __declspec(dllimport) long __stdcall RegOpenKeyExA ( HKEY hKey, const char * lpSubKey,
int ulOptions, int samDesired, HKEY * phkResult );
#endif // _WINDOWS_
#define STB__REG_OPTION_NON_VOLATILE 0
#define STB__REG_KEY_ALL_ACCESS 0x000f003f
#define STB__REG_KEY_READ 0x00020019
#ifdef _M_AMD64
#define STB__HKEY_CURRENT_USER 0x80000001ull
#define STB__HKEY_LOCAL_MACHINE 0x80000002ull
#else
#define STB__HKEY_CURRENT_USER 0x80000001
#define STB__HKEY_LOCAL_MACHINE 0x80000002
#endif
void *stb_reg_open(const char *mode, const char *where)
{
long res;
HKEY base;
HKEY zreg;
if (!stb_stricmp(mode+1, "cu") || !stb_stricmp(mode+1, "hkcu"))
base = (HKEY) STB__HKEY_CURRENT_USER;
else if (!stb_stricmp(mode+1, "lm") || !stb_stricmp(mode+1, "hklm"))
base = (HKEY) STB__HKEY_LOCAL_MACHINE;
else
return NULL;
if (mode[0] == 'r')
res = RegOpenKeyExA(base, where, 0, STB__REG_KEY_READ, &zreg);
else if (mode[0] == 'w')
res = RegCreateKeyExA(base, where, 0, NULL, STB__REG_OPTION_NON_VOLATILE, STB__REG_KEY_ALL_ACCESS, NULL, &zreg, NULL);
else
return NULL;
return res ? NULL : zreg;
}
void stb_reg_close(void *reg)
{
RegCloseKey((HKEY) reg);
}
#define STB__REG_SZ 1
#define STB__REG_BINARY 3
#define STB__REG_DWORD 4
int stb_reg_read(void *zreg, const char *str, void *data, unsigned long len)
{
unsigned long type;
unsigned long alen = len;
if (0 == RegQueryValueExA((HKEY) zreg, str, 0, &type, (unsigned char *) data, &len))
if (type == STB__REG_BINARY || type == STB__REG_SZ || type == STB__REG_DWORD) {
if (len < alen)
*((char *) data + len) = 0;
return 1;
}
return 0;
}
void stb_reg_write(void *zreg, const char *str, const void *data, unsigned long len)
{
if (zreg)
RegSetValueExA((HKEY) zreg, str, 0, STB__REG_BINARY, (const unsigned char *) data, len);
}
int stb_reg_read_string(void *zreg, const char *str, char *data, int len)
{
if (!stb_reg_read(zreg, str, data, len)) return 0;
data[len-1] = 0; // force a 0 at the end of the string no matter what
return 1;
}
void stb_reg_write_string(void *zreg, const char *str, const char *data)
{
if (zreg)
RegSetValueExA((HKEY) zreg, str, 0, STB__REG_SZ, (const unsigned char *) data, (int) strlen(data)+1);
}
#endif // STB_DEFINE
#endif // _WIN32
//////////////////////////////////////////////////////////////////////////////
//
// stb_cfg - This is like the registry, but the config info
// is all stored in plain old files where we can
// backup and restore them easily. The LOCATION of
// the config files is gotten from... the registry!
#ifndef STB_NO_STB_STRINGS
typedef struct stb_cfg_st stb_cfg;
STB_EXTERN stb_cfg * stb_cfg_open(char *config, const char *mode); // mode = "r", "w"
STB_EXTERN void stb_cfg_close(stb_cfg *cfg);
STB_EXTERN int stb_cfg_read(stb_cfg *cfg, char *key, void *value, int len);
STB_EXTERN void stb_cfg_write(stb_cfg *cfg, char *key, void *value, int len);
STB_EXTERN int stb_cfg_read_string(stb_cfg *cfg, char *key, char *value, int len);
STB_EXTERN void stb_cfg_write_string(stb_cfg *cfg, char *key, char *value);
STB_EXTERN int stb_cfg_delete(stb_cfg *cfg, char *key);
STB_EXTERN void stb_cfg_set_directory(char *dir);
#ifdef STB_DEFINE
typedef struct
{
char *key;
void *value;
int value_len;
} stb__cfg_item;
struct stb_cfg_st
{
stb__cfg_item *data;
char *loaded_file; // this needs to be freed
FILE *f; // write the data to this file on close
};
static const char *stb__cfg_sig = "sTbCoNfIg!\0\0";
static char stb__cfg_dir[512];
STB_EXTERN void stb_cfg_set_directory(char *dir)
{
stb_p_strcpy_s(stb__cfg_dir, sizeof(stb__cfg_dir), dir);
}
STB_EXTERN stb_cfg * stb_cfg_open(char *config, const char *mode)
{
size_t len;
stb_cfg *z;
char file[512];
if (mode[0] != 'r' && mode[0] != 'w') return NULL;
if (!stb__cfg_dir[0]) {
#ifdef _WIN32
stb_p_strcpy_s(stb__cfg_dir, sizeof(stb__cfg_dir), "c:/stb");
#else
strcpy(stb__cfg_dir, "~/.stbconfig");
#endif
#ifdef STB_HAS_REGISTRY
{
void *reg = stb_reg_open("rHKLM", "Software\\SilverSpaceship\\stb");
if (reg) {
stb_reg_read_string(reg, "config_dir", stb__cfg_dir, sizeof(stb__cfg_dir));
stb_reg_close(reg);
}
}
#endif
}
stb_p_sprintf(file stb_p_size(sizeof(file)), "%s/%s.cfg", stb__cfg_dir, config);
z = (stb_cfg *) stb_malloc(0, sizeof(*z));
z->data = NULL;
z->loaded_file = stb_filec(file, &len);
if (z->loaded_file) {
char *s = z->loaded_file;
if (!memcmp(s, stb__cfg_sig, 12)) {
char *s = z->loaded_file + 12;
while (s < z->loaded_file + len) {
stb__cfg_item a;
int n = *(stb_int16 *) s;
a.key = s+2;
s = s+2 + n;
a.value_len = *(int *) s;
s += 4;
a.value = s;
s += a.value_len;
stb_arr_push(z->data, a);
}
assert(s == z->loaded_file + len);
}
}
if (mode[0] == 'w')
z->f = stb_p_fopen(file, "wb");
else
z->f = NULL;
return z;
}
void stb_cfg_close(stb_cfg *z)
{
if (z->f) {
int i;
// write the file out
fwrite(stb__cfg_sig, 12, 1, z->f);
for (i=0; i < stb_arr_len(z->data); ++i) {
stb_int16 n = (stb_int16) strlen(z->data[i].key)+1;
fwrite(&n, 2, 1, z->f);
fwrite(z->data[i].key, n, 1, z->f);
fwrite(&z->data[i].value_len, 4, 1, z->f);
fwrite(z->data[i].value, z->data[i].value_len, 1, z->f);
}
fclose(z->f);
}
stb_arr_free(z->data);
stb_free(z);
}
int stb_cfg_read(stb_cfg *z, char *key, void *value, int len)
{
int i;
for (i=0; i < stb_arr_len(z->data); ++i) {
if (!stb_stricmp(z->data[i].key, key)) {
int n = stb_min(len, z->data[i].value_len);
memcpy(value, z->data[i].value, n);
if (n < len)
*((char *) value + n) = 0;
return 1;
}
}
return 0;
}
void stb_cfg_write(stb_cfg *z, char *key, void *value, int len)
{
int i;
for (i=0; i < stb_arr_len(z->data); ++i)
if (!stb_stricmp(z->data[i].key, key))
break;
if (i == stb_arr_len(z->data)) {
stb__cfg_item p;
p.key = stb_strdup(key, z);
p.value = NULL;
p.value_len = 0;
stb_arr_push(z->data, p);
}
z->data[i].value = stb_malloc(z, len);
z->data[i].value_len = len;
memcpy(z->data[i].value, value, len);
}
int stb_cfg_delete(stb_cfg *z, char *key)
{
int i;
for (i=0; i < stb_arr_len(z->data); ++i)
if (!stb_stricmp(z->data[i].key, key)) {
stb_arr_fastdelete(z->data, i);
return 1;
}
return 0;
}
int stb_cfg_read_string(stb_cfg *z, char *key, char *value, int len)
{
if (!stb_cfg_read(z, key, value, len)) return 0;
value[len-1] = 0;
return 1;
}
void stb_cfg_write_string(stb_cfg *z, char *key, char *value)
{
stb_cfg_write(z, key, value, (int) strlen(value)+1);
}
#endif
//////////////////////////////////////////////////////////////////////////////
//
// stb_dirtree - load a description of a directory tree
// uses a cache and stat()s the directories for changes
// MUCH faster on NTFS, _wrong_ on FAT32, so should
// ignore the db on FAT32
#ifdef _WIN32
typedef struct
{
char * path; // full path from passed-in root
time_t last_modified;
int num_files;
int flag;
} stb_dirtree_dir;
typedef struct
{
char *name; // name relative to path
int dir; // index into dirs[] array
stb_int64 size; // size, max 4GB
time_t last_modified;
int flag;
} stb_dirtree_file;
typedef struct
{
stb_dirtree_dir *dirs;
stb_dirtree_file *files;
// internal use
void * string_pool; // used to free data en masse
} stb_dirtree;
extern void stb_dirtree_free ( stb_dirtree *d );
extern stb_dirtree *stb_dirtree_get ( char *dir);
extern stb_dirtree *stb_dirtree_get_dir ( char *dir, char *cache_dir);
extern stb_dirtree *stb_dirtree_get_with_file ( char *dir, char *cache_file);
// get a list of all the files recursively underneath 'dir'
//
// cache_file is used to store a copy of the directory tree to speed up
// later calls. It must be unique to 'dir' and the current working
// directory! Otherwise who knows what will happen (a good solution
// is to put it _in_ dir, but this API doesn't force that).
//
// Also, it might be possible to break this if you have two different processes
// do a call to stb_dirtree_get() with the same cache file at about the same
// time, but I _think_ it might just work.
// i needed to build an identical data structure representing the state of
// a mirrored copy WITHOUT bothering to rescan it (i.e. we're mirroring to
// it WITHOUT scanning it, e.g. it's over the net), so this requires access
// to all of the innards.
extern void stb_dirtree_db_add_dir(stb_dirtree *active, char *path, time_t last);
extern void stb_dirtree_db_add_file(stb_dirtree *active, char *name, int dir, stb_int64 size, time_t last);
extern void stb_dirtree_db_read(stb_dirtree *target, char *filename, char *dir);
extern void stb_dirtree_db_write(stb_dirtree *target, char *filename, char *dir);
#ifdef STB_DEFINE
static void stb__dirtree_add_dir(char *path, time_t last, stb_dirtree *active)
{
stb_dirtree_dir d;
d.last_modified = last;
d.num_files = 0;
d.path = stb_strdup(path, active->string_pool);
stb_arr_push(active->dirs, d);
}
static void stb__dirtree_add_file(char *name, int dir, stb_int64 size, time_t last, stb_dirtree *active)
{
stb_dirtree_file f;
f.dir = dir;
f.size = size;
f.last_modified = last;
f.name = stb_strdup(name, active->string_pool);
++active->dirs[dir].num_files;
stb_arr_push(active->files, f);
}
// version 02 supports > 4GB files
static char stb__signature[12] = { 's', 'T', 'b', 'D', 'i', 'R', 't', 'R', 'e', 'E', '0', '2' };
static void stb__dirtree_save_db(char *filename, stb_dirtree *data, char *root)
{
int i, num_dirs_final=0, num_files_final;
char *info = root ? root : (char*)"";
int *remap;
FILE *f = stb_p_fopen(filename, "wb");
if (!f) return;
fwrite(stb__signature, sizeof(stb__signature), 1, f);
fwrite(info, strlen(info)+1, 1, f);
// need to be slightly tricky and not write out NULLed directories, nor the root
// build remapping table of all dirs we'll be writing out
remap = (int *) malloc(sizeof(remap[0]) * stb_arr_len(data->dirs));
for (i=0; i < stb_arr_len(data->dirs); ++i) {
if (data->dirs[i].path == NULL || (root && 0==stb_stricmp(data->dirs[i].path, root))) {
remap[i] = -1;
} else {
remap[i] = num_dirs_final++;
}
}
fwrite(&num_dirs_final, 4, 1, f);
for (i=0; i < stb_arr_len(data->dirs); ++i) {
if (remap[i] >= 0) {
fwrite(&data->dirs[i].last_modified, 4, 1, f);
stb_fput_string(f, data->dirs[i].path);
}
}
num_files_final = 0;
for (i=0; i < stb_arr_len(data->files); ++i)
if (remap[data->files[i].dir] >= 0 && data->files[i].name)
++num_files_final;
fwrite(&num_files_final, 4, 1, f);
for (i=0; i < stb_arr_len(data->files); ++i) {
if (remap[data->files[i].dir] >= 0 && data->files[i].name) {
stb_fput_ranged(f, remap[data->files[i].dir], 0, num_dirs_final);
stb_fput_varlen64(f, data->files[i].size);
fwrite(&data->files[i].last_modified, 4, 1, f);
stb_fput_string(f, data->files[i].name);
}
}
fclose(f);
}
// note: stomps any existing data, rather than appending
static void stb__dirtree_load_db(char *filename, stb_dirtree *data, char *dir)
{
char sig[2048];
int i,n;
FILE *f = stb_p_fopen(filename, "rb");
if (!f) return;
data->string_pool = stb_malloc(0,1);
fread(sig, sizeof(stb__signature), 1, f);
if (memcmp(stb__signature, sig, sizeof(stb__signature))) { fclose(f); return; }
if (!fread(sig, strlen(dir)+1, 1, f)) { fclose(f); return; }
if (stb_stricmp(sig,dir)) { fclose(f); return; }
// we can just read them straight in, because they're guaranteed to be valid
fread(&n, 4, 1, f);
stb_arr_setlen(data->dirs, n);
for(i=0; i < stb_arr_len(data->dirs); ++i) {
fread(&data->dirs[i].last_modified, 4, 1, f);
data->dirs[i].path = stb_fget_string(f, data->string_pool);
if (data->dirs[i].path == NULL) goto bail;
}
fread(&n, 4, 1, f);
stb_arr_setlen(data->files, n);
for (i=0; i < stb_arr_len(data->files); ++i) {
data->files[i].dir = stb_fget_ranged(f, 0, stb_arr_len(data->dirs));
data->files[i].size = stb_fget_varlen64(f);
fread(&data->files[i].last_modified, 4, 1, f);
data->files[i].name = stb_fget_string(f, data->string_pool);
if (data->files[i].name == NULL) goto bail;
}
if (0) {
bail:
stb_arr_free(data->dirs);
stb_arr_free(data->files);
}
fclose(f);
}
FILE *hlog;
static int stb__dircount, stb__dircount_mask, stb__showfile;
static void stb__dirtree_scandir(char *path, time_t last_time, stb_dirtree *active)
{
// this is dumb depth first; theoretically it might be faster
// to fully traverse each directory before visiting its children,
// but it's complicated and didn't seem like a gain in the test app
int n;
struct _wfinddatai64_t c_file;
long hFile;
stb__wchar full_path[1024];
int has_slash;
if (stb__showfile) printf("<");
has_slash = (path[0] && path[strlen(path)-1] == '/');
// @TODO: do this concatenation without using swprintf to avoid this mess:
#if (defined(_MSC_VER) && _MSC_VER < 1400) // || (defined(__clang__))
// confusingly, Windows Kits\10 needs to go down this path?!?
// except now it doesn't, I don't know what changed
if (has_slash)
swprintf(full_path, L"%s*", stb__from_utf8(path));
else
swprintf(full_path, L"%s/*", stb__from_utf8(path));
#else
if (has_slash)
swprintf((wchar_t *) full_path, (size_t) 1024, L"%s*", (wchar_t *) stb__from_utf8(path));
else
swprintf((wchar_t *) full_path, (size_t) 1024, L"%s/*", (wchar_t *) stb__from_utf8(path));
#endif
// it's possible this directory is already present: that means it was in the
// cache, but its parent wasn't... in that case, we're done with it
if (stb__showfile) printf("C[%d]", stb_arr_len(active->dirs));
for (n=0; n < stb_arr_len(active->dirs); ++n)
if (0 == stb_stricmp(active->dirs[n].path, path)) {
if (stb__showfile) printf("D");
return;
}
if (stb__showfile) printf("E");
// otherwise, we need to add it
stb__dirtree_add_dir(path, last_time, active);
n = stb_arr_lastn(active->dirs);
if (stb__showfile) printf("[");
if( (hFile = (long) _wfindfirsti64( (wchar_t *) full_path, &c_file )) != -1L ) {
do {
if (stb__showfile) printf(")");
if (c_file.attrib & _A_SUBDIR) {
// ignore subdirectories starting with '.', e.g. "." and ".."
if (c_file.name[0] != '.') {
char *new_path = (char *) full_path;
char *temp = stb__to_utf8((stb__wchar *) c_file.name);
if (has_slash)
stb_p_sprintf(new_path stb_p_size(sizeof(full_path)), "%s%s", path, temp);
else
stb_p_sprintf(new_path stb_p_size(sizeof(full_path)), "%s/%s", path, temp);
if (stb__dircount_mask) {
++stb__dircount;
if (!(stb__dircount & stb__dircount_mask)) {
char dummy_path[128], *pad;
stb_strncpy(dummy_path, new_path, sizeof(dummy_path)-1);
if (strlen(dummy_path) > 96) {
stb_p_strcpy_s(dummy_path+96/2-1,128, "...");
stb_p_strcpy_s(dummy_path+96/2+2,128, new_path + strlen(new_path)-96/2+2);
}
pad = dummy_path + strlen(dummy_path);
while (pad < dummy_path+98)
*pad++ = ' ';
*pad = 0;
printf("%s\r", dummy_path);
#if 0
if (hlog == 0) {
hlog = stb_p_fopen("c:/x/temp.log", "w");
fprintf(hlog, "%s\n", dummy_path);
}
#endif
}
}
stb__dirtree_scandir(new_path, c_file.time_write, active);
}
} else {
char *temp = stb__to_utf8((stb__wchar *) c_file.name);
stb__dirtree_add_file(temp, n, c_file.size, c_file.time_write, active);
}
if (stb__showfile) printf("(");
} while( _wfindnexti64( hFile, &c_file ) == 0 );
if (stb__showfile) printf("]");
_findclose( hFile );
}
if (stb__showfile) printf(">\n");
}
// scan the database and see if it's all valid
static int stb__dirtree_update_db(stb_dirtree *db, stb_dirtree *active)
{
int changes_detected = STB_FALSE;
int i;
int *remap;
int *rescan=NULL;
remap = (int *) malloc(sizeof(remap[0]) * stb_arr_len(db->dirs));
memset(remap, 0, sizeof(remap[0]) * stb_arr_len(db->dirs));
rescan = NULL;
for (i=0; i < stb_arr_len(db->dirs); ++i) {
struct _stat info;
if (stb__dircount_mask) {
++stb__dircount;
if (!(stb__dircount & stb__dircount_mask)) {
printf(".");
}
}
if (0 == _stat(db->dirs[i].path, &info)) {
if (info.st_mode & _S_IFDIR) {
// it's still a directory, as expected
int n = abs((int) (info.st_mtime - db->dirs[i].last_modified));
if (n > 1 && n != 3600) { // the 3600 is a hack because sometimes this jumps for no apparent reason, even when no time zone or DST issues are at play
// it's changed! force a rescan
// we don't want to scan it until we've stat()d its
// subdirs, though, so we queue it
if (stb__showfile) printf("Changed: %s - %08x:%08x\n", db->dirs[i].path, (unsigned int) db->dirs[i].last_modified, (unsigned int) info.st_mtime);
stb_arr_push(rescan, i);
// update the last_mod time
db->dirs[i].last_modified = info.st_mtime;
// ignore existing files in this dir
remap[i] = -1;
changes_detected = STB_TRUE;
} else {
// it hasn't changed, just copy it through unchanged
stb__dirtree_add_dir(db->dirs[i].path, db->dirs[i].last_modified, active);
remap[i] = stb_arr_lastn(active->dirs);
}
} else {
// this path used to refer to a directory, but now it's a file!
// assume that the parent directory is going to be forced to rescan anyway
goto delete_entry;
}
} else {
delete_entry:
// directory no longer exists, so don't copy it
// we don't free it because it's in the string pool now
db->dirs[i].path = NULL;
remap[i] = -1;
changes_detected = STB_TRUE;
}
}
// at this point, we have:
//
// <rescan> holds a list of directory indices that need to be scanned due to being out of date
// <remap> holds the directory index in <active> for each dir in <db>, if it exists; -1 if not
// directories in <rescan> are not in <active> yet
// so we can go ahead and remap all the known files right now
for (i=0; i < stb_arr_len(db->files); ++i) {
int dir = db->files[i].dir;
if (remap[dir] >= 0) {
stb__dirtree_add_file(db->files[i].name, remap[dir], db->files[i].size, db->files[i].last_modified, active);
}
}
// at this point we're done with db->files, and done with remap
free(remap);
// now scan those directories using the standard scan
for (i=0; i < stb_arr_len(rescan); ++i) {
int z = rescan[i];
stb__dirtree_scandir(db->dirs[z].path, db->dirs[z].last_modified, active);
}
stb_arr_free(rescan);
return changes_detected;
}
static void stb__dirtree_free_raw(stb_dirtree *d)
{
stb_free(d->string_pool);
stb_arr_free(d->dirs);
stb_arr_free(d->files);
}
stb_dirtree *stb_dirtree_get_with_file(char *dir, char *cache_file)
{
stb_dirtree *output = (stb_dirtree *) malloc(sizeof(*output));
stb_dirtree db,active;
int prev_dir_count, cache_mismatch;
char *stripped_dir; // store the directory name without a trailing '/' or '\\'
// load the database of last-known state on disk
db.string_pool = NULL;
db.files = NULL;
db.dirs = NULL;
stripped_dir = stb_strip_final_slash(stb_p_strdup(dir));
if (cache_file != NULL)
stb__dirtree_load_db(cache_file, &db, stripped_dir);
else if (stb__showfile)
printf("No cache file\n");
active.files = NULL;
active.dirs = NULL;
active.string_pool = stb_malloc(0,1); // @TODO: share string pools between both?
// check all the directories in the database; make note if
// anything we scanned had changed, and rescan those things
cache_mismatch = stb__dirtree_update_db(&db, &active);
// check the root tree
prev_dir_count = stb_arr_len(active.dirs); // record how many directories we've seen
stb__dirtree_scandir(stripped_dir, 0, &active); // no last_modified time available for root
if (stb__dircount_mask)
printf(" \r");
// done with the DB; write it back out if any changes, i.e. either
// 1. any inconsistency found between cached information and actual disk
// or 2. if scanning the root found any new directories--which we detect because
// more than one directory got added to the active db during that scan
if (cache_mismatch || stb_arr_len(active.dirs) > prev_dir_count+1)
stb__dirtree_save_db(cache_file, &active, stripped_dir);
free(stripped_dir);
stb__dirtree_free_raw(&db);
*output = active;
return output;
}
stb_dirtree *stb_dirtree_get_dir(char *dir, char *cache_dir)
{
int i;
stb_uint8 sha[20];
char dir_lower[1024];
char cache_file[1024],*s;
if (cache_dir == NULL)
return stb_dirtree_get_with_file(dir, NULL);
stb_p_strcpy_s(dir_lower, sizeof(dir_lower), dir);
stb_tolower(dir_lower);
stb_sha1(sha, (unsigned char *) dir_lower, (unsigned int) strlen(dir_lower));
stb_p_strcpy_s(cache_file, sizeof(cache_file), cache_dir);
s = cache_file + strlen(cache_file);
if (s[-1] != '/' && s[-1] != '\\') *s++ = '/';
stb_p_strcpy_s(s, sizeof(cache_file), "dirtree_");
s += strlen(s);
for (i=0; i < 8; ++i) {
char *hex = (char*)"0123456789abcdef";
stb_uint z = sha[i];
*s++ = hex[z >> 4];
*s++ = hex[z & 15];
}
stb_p_strcpy_s(s, sizeof(cache_file), ".bin");
return stb_dirtree_get_with_file(dir, cache_file);
}
stb_dirtree *stb_dirtree_get(char *dir)
{
char cache_dir[256];
stb_p_strcpy_s(cache_dir, sizeof(cache_dir), "c:/bindata");
#ifdef STB_HAS_REGISTRY
{
void *reg = stb_reg_open("rHKLM", "Software\\SilverSpaceship\\stb");
if (reg) {
stb_reg_read(reg, "dirtree", cache_dir, sizeof(cache_dir));
stb_reg_close(reg);
}
}
#endif
return stb_dirtree_get_dir(dir, cache_dir);
}
void stb_dirtree_free(stb_dirtree *d)
{
stb__dirtree_free_raw(d);
free(d);
}
void stb_dirtree_db_add_dir(stb_dirtree *active, char *path, time_t last)
{
stb__dirtree_add_dir(path, last, active);
}
void stb_dirtree_db_add_file(stb_dirtree *active, char *name, int dir, stb_int64 size, time_t last)
{
stb__dirtree_add_file(name, dir, size, last, active);
}
void stb_dirtree_db_read(stb_dirtree *target, char *filename, char *dir)
{
char *s = stb_strip_final_slash(stb_p_strdup(dir));
target->dirs = 0;
target->files = 0;
target->string_pool = 0;
stb__dirtree_load_db(filename, target, s);
free(s);
}
void stb_dirtree_db_write(stb_dirtree *target, char *filename, char *dir)
{
stb__dirtree_save_db(filename, target, 0); // don't strip out any directories
}
#endif // STB_DEFINE
#endif // _WIN32
#endif // STB_NO_STB_STRINGS
//////////////////////////////////////////////////////////////////////////////
//
// STB_MALLOC_WRAPPER
//
// you can use the wrapper functions with your own malloc wrapper,
// or define STB_MALLOC_WRAPPER project-wide to have
// malloc/free/realloc/strdup all get vectored to it
// this has too many very specific error messages you could google for and find in stb.h,
// so don't use it if they don't want any stb.h-identifiable strings
#if defined(STB_DEFINE) && !defined(STB_NO_STB_STRINGS)
typedef struct
{
void *p;
char *file;
int line;
size_t size;
} stb_malloc_record;
#ifndef STB_MALLOC_HISTORY_COUNT
#define STB_MALLOC_HISTORY_COUNT 50 // 800 bytes
#endif
stb_malloc_record *stb__allocations;
static int stb__alloc_size, stb__alloc_limit, stb__alloc_mask;
int stb__alloc_count;
stb_malloc_record stb__alloc_history[STB_MALLOC_HISTORY_COUNT];
int stb__history_pos;
static int stb__hashfind(void *p)
{
stb_uint32 h = stb_hashptr(p);
int s,n = h & stb__alloc_mask;
if (stb__allocations[n].p == p)
return n;
s = stb_rehash(h)|1;
for(;;) {
if (stb__allocations[n].p == NULL)
return -1;
n = (n+s) & stb__alloc_mask;
if (stb__allocations[n].p == p)
return n;
}
}
size_t stb_wrapper_allocsize(void *p)
{
int n = stb__hashfind(p);
if (n < 0) return 0;
return stb__allocations[n].size;
}
static int stb__historyfind(void *p)
{
int n = stb__history_pos;
int i;
for (i=0; i < STB_MALLOC_HISTORY_COUNT; ++i) {
if (--n < 0) n = STB_MALLOC_HISTORY_COUNT-1;
if (stb__alloc_history[n].p == p)
return n;
}
return -1;
}
static void stb__add_alloc(void *p, size_t sz, char *file, int line);
static void stb__grow_alloc(void)
{
int i,old_num = stb__alloc_size;
stb_malloc_record *old = stb__allocations;
if (stb__alloc_size == 0)
stb__alloc_size = 64;
else
stb__alloc_size *= 2;
stb__allocations = (stb_malloc_record *) stb__realloc_raw(NULL, stb__alloc_size * sizeof(stb__allocations[0]));
if (stb__allocations == NULL)
stb_fatal("Internal error: couldn't grow malloc wrapper table");
memset(stb__allocations, 0, stb__alloc_size * sizeof(stb__allocations[0]));
stb__alloc_limit = (stb__alloc_size*3)>>2;
stb__alloc_mask = stb__alloc_size-1;
stb__alloc_count = 0;
for (i=0; i < old_num; ++i)
if (old[i].p > STB_DEL) {
stb__add_alloc(old[i].p, old[i].size, old[i].file, old[i].line);
assert(stb__hashfind(old[i].p) >= 0);
}
for (i=0; i < old_num; ++i)
if (old[i].p > STB_DEL)
assert(stb__hashfind(old[i].p) >= 0);
stb__realloc_raw(old, 0);
}
static void stb__add_alloc(void *p, size_t sz, char *file, int line)
{
stb_uint32 h;
int n;
if (stb__alloc_count >= stb__alloc_limit)
stb__grow_alloc();
h = stb_hashptr(p);
n = h & stb__alloc_mask;
if (stb__allocations[n].p > STB_DEL) {
int s = stb_rehash(h)|1;
do {
n = (n+s) & stb__alloc_mask;
} while (stb__allocations[n].p > STB_DEL);
}
assert(stb__allocations[n].p == NULL || stb__allocations[n].p == STB_DEL);
stb__allocations[n].p = p;
stb__allocations[n].size = sz;
stb__allocations[n].line = line;
stb__allocations[n].file = file;
++stb__alloc_count;
}
static void stb__remove_alloc(int n, char *file, int line)
{
stb__alloc_history[stb__history_pos] = stb__allocations[n];
stb__alloc_history[stb__history_pos].file = file;
stb__alloc_history[stb__history_pos].line = line;
if (++stb__history_pos == STB_MALLOC_HISTORY_COUNT)
stb__history_pos = 0;
stb__allocations[n].p = STB_DEL;
--stb__alloc_count;
}
void stb_wrapper_malloc(void *p, size_t sz, char *file, int line)
{
if (!p) return;
stb__add_alloc(p,sz,file,line);
}
void stb_wrapper_free(void *p, char *file, int line)
{
int n;
if (p == NULL) return;
n = stb__hashfind(p);
if (n >= 0)
stb__remove_alloc(n, file, line);
else {
// tried to free something we hadn't allocated!
n = stb__historyfind(p);
assert(0); /* NOTREACHED */
if (n >= 0)
stb_fatal("Attempted to free %d-byte block %p at %s:%d previously freed/realloced at %s:%d",
stb__alloc_history[n].size, p,
file, line,
stb__alloc_history[n].file, stb__alloc_history[n].line);
else
stb_fatal("Attempted to free unknown block %p at %s:%d", p, file,line);
}
}
void stb_wrapper_check(void *p)
{
int n;
if (p == NULL) return;
n = stb__hashfind(p);
if (n >= 0) return;
for (n=0; n < stb__alloc_size; ++n)
if (stb__allocations[n].p == p)
stb_fatal("Internal error: pointer %p was allocated, but hash search failed", p);
// tried to free something that wasn't allocated!
n = stb__historyfind(p);
if (n >= 0)
stb_fatal("Checked %d-byte block %p previously freed/realloced at %s:%d",
stb__alloc_history[n].size, p,
stb__alloc_history[n].file, stb__alloc_history[n].line);
stb_fatal("Checked unknown block %p");
}
void stb_wrapper_realloc(void *p, void *q, size_t sz, char *file, int line)
{
int n;
if (p == NULL) { stb_wrapper_malloc(q, sz, file, line); return; }
if (q == NULL) return; // nothing happened
n = stb__hashfind(p);
if (n == -1) {
// tried to free something we hadn't allocated!
// this is weird, though, because we got past the realloc!
n = stb__historyfind(p);
assert(0); /* NOTREACHED */
if (n >= 0)
stb_fatal("Attempted to realloc %d-byte block %p at %s:%d previously freed/realloced at %s:%d",
stb__alloc_history[n].size, p,
file, line,
stb__alloc_history[n].file, stb__alloc_history[n].line);
else
stb_fatal("Attempted to realloc unknown block %p at %s:%d", p, file,line);
} else {
if (q == p) {
stb__allocations[n].size = sz;
stb__allocations[n].file = file;
stb__allocations[n].line = line;
} else {
stb__remove_alloc(n, file, line);
stb__add_alloc(q,sz,file,line);
}
}
}
void stb_wrapper_listall(void (*func)(void *ptr, size_t sz, char *file, int line))
{
int i;
for (i=0; i < stb__alloc_size; ++i)
if (stb__allocations[i].p > STB_DEL)
func(stb__allocations[i].p , stb__allocations[i].size,
stb__allocations[i].file, stb__allocations[i].line);
}
void stb_wrapper_dump(char *filename)
{
int i;
FILE *f = stb_p_fopen(filename, "w");
if (!f) return;
for (i=0; i < stb__alloc_size; ++i)
if (stb__allocations[i].p > STB_DEL)
fprintf(f, "%p %7d - %4d %s\n",
stb__allocations[i].p , (int) stb__allocations[i].size,
stb__allocations[i].line, stb__allocations[i].file);
}
#endif // STB_DEFINE
//////////////////////////////////////////////////////////////////////////////
//
// stb_pointer_set
//
//
// For data structures that support querying by key, data structure
// classes always hand-wave away the issue of what to do if two entries
// have the same key: basically, store a linked list of all the nodes
// which have the same key (a LISP-style list).
//
// The thing is, it's not that trivial. If you have an O(log n)
// lookup data structure, but then n/4 items have the same value,
// you don't want to spend O(n) time scanning that list when
// deleting an item if you already have a pointer to the item.
// (You have to spend O(n) time enumerating all the items with
// a given key, sure, and you can't accelerate deleting a particular
// item if you only have the key, not a pointer to the item.)
//
// I'm going to call this data structure, whatever it turns out to
// be, a "pointer set", because we don't store any associated data for
// items in this data structure, we just answer the question of
// whether an item is in it or not (it's effectively one bit per pointer).
// Technically they don't have to be pointers; you could cast ints
// to (void *) if you want, but you can't store 0 or 1 because of the
// hash table.
//
// Since the fastest data structure we might want to add support for
// identical-keys to is a hash table with O(1)-ish lookup time,
// that means that the conceptual "linked list of all items with
// the same indexed value" that we build needs to have the same
// performance; that way when we index a table we think is arbitrary
// ints, but in fact half of them are 0, we don't get screwed.
//
// Therefore, it needs to be a hash table, at least when it gets
// large. On the other hand, when the data has totally arbitrary ints
// or floats, there won't be many collisions, and we'll have tons of
// 1-item bitmaps. That will be grossly inefficient as hash tables;
// trade-off; the hash table is reasonably efficient per-item when
// it's large, but not when it's small. So we need to do something
// Judy-like and use different strategies depending on the size.
//
// Like Judy, we'll use the bottom bit to encode the strategy:
//
// bottom bits:
// 00 - direct pointer
// 01 - 4-item bucket (16 bytes, no length, NULLs)
// 10 - N-item array
// 11 - hash table
typedef struct stb_ps stb_ps;
STB_EXTERN int stb_ps_find (stb_ps *ps, void *value);
STB_EXTERN stb_ps * stb_ps_add (stb_ps *ps, void *value);
STB_EXTERN stb_ps * stb_ps_remove(stb_ps *ps, void *value);
STB_EXTERN stb_ps * stb_ps_remove_any(stb_ps *ps, void **value);
STB_EXTERN void stb_ps_delete(stb_ps *ps);
STB_EXTERN int stb_ps_count (stb_ps *ps);
STB_EXTERN stb_ps * stb_ps_copy (stb_ps *ps);
STB_EXTERN int stb_ps_subset(stb_ps *bigger, stb_ps *smaller);
STB_EXTERN int stb_ps_eq (stb_ps *p0, stb_ps *p1);
STB_EXTERN void ** stb_ps_getlist (stb_ps *ps, int *count);
STB_EXTERN int stb_ps_writelist(stb_ps *ps, void **list, int size );
// enum and fastlist don't allocate storage, but you must consume the
// list before there's any chance the data structure gets screwed up;
STB_EXTERN int stb_ps_enum (stb_ps *ps, void *data,
int (*func)(void *value, void*data) );
STB_EXTERN void ** stb_ps_fastlist(stb_ps *ps, int *count);
// result:
// returns a list, *count is the length of that list,
// but some entries of the list may be invalid;
// test with 'stb_ps_fastlist_valid(x)'
#define stb_ps_fastlist_valid(x) ((stb_uinta) (x) > 1)
#ifdef STB_DEFINE
enum
{
STB_ps_direct = 0,
STB_ps_bucket = 1,
STB_ps_array = 2,
STB_ps_hash = 3,
};
#define STB_BUCKET_SIZE 4
typedef struct
{
void *p[STB_BUCKET_SIZE];
} stb_ps_bucket;
#define GetBucket(p) ((stb_ps_bucket *) ((char *) (p) - STB_ps_bucket))
#define EncodeBucket(p) ((stb_ps *) ((char *) (p) + STB_ps_bucket))
static void stb_bucket_free(stb_ps_bucket *b)
{
free(b);
}
static stb_ps_bucket *stb_bucket_create2(void *v0, void *v1)
{
stb_ps_bucket *b = (stb_ps_bucket*) malloc(sizeof(*b));
b->p[0] = v0;
b->p[1] = v1;
b->p[2] = NULL;
b->p[3] = NULL;
return b;
}
static stb_ps_bucket * stb_bucket_create3(void **v)
{
stb_ps_bucket *b = (stb_ps_bucket*) malloc(sizeof(*b));
b->p[0] = v[0];
b->p[1] = v[1];
b->p[2] = v[2];
b->p[3] = NULL;
return b;
}
// could use stb_arr, but this will save us memory
typedef struct
{
int count;
void *p[1];
} stb_ps_array;
#define GetArray(p) ((stb_ps_array *) ((char *) (p) - STB_ps_array))
#define EncodeArray(p) ((stb_ps *) ((char *) (p) + STB_ps_array))
static int stb_ps_array_max = 13;
typedef struct
{
int size, mask;
int count, count_deletes;
int grow_threshhold;
int shrink_threshhold;
int rehash_threshhold;
int any_offset;
void *table[1];
} stb_ps_hash;
#define GetHash(p) ((stb_ps_hash *) ((char *) (p) - STB_ps_hash))
#define EncodeHash(p) ((stb_ps *) ((char *) (p) + STB_ps_hash))
#define stb_ps_empty(v) (((stb_uint32) v) <= 1)
static stb_ps_hash *stb_ps_makehash(int size, int old_size, void **old_data)
{
int i;
stb_ps_hash *h = (stb_ps_hash *) malloc(sizeof(*h) + (size-1) * sizeof(h->table[0]));
assert(stb_is_pow2(size));
h->size = size;
h->mask = size-1;
h->shrink_threshhold = (int) (0.3f * size);
h-> grow_threshhold = (int) (0.8f * size);
h->rehash_threshhold = (int) (0.9f * size);
h->count = 0;
h->count_deletes = 0;
h->any_offset = 0;
memset(h->table, 0, size * sizeof(h->table[0]));
for (i=0; i < old_size; ++i)
if (!stb_ps_empty((size_t)old_data[i]))
stb_ps_add(EncodeHash(h), old_data[i]);
return h;
}
void stb_ps_delete(stb_ps *ps)
{
switch (3 & (int)(size_t) ps) {
case STB_ps_direct: break;
case STB_ps_bucket: stb_bucket_free(GetBucket(ps)); break;
case STB_ps_array : free(GetArray(ps)); break;
case STB_ps_hash : free(GetHash(ps)); break;
}
}
stb_ps *stb_ps_copy(stb_ps *ps)
{
int i;
// not a switch: order based on expected performance/power-law distribution
switch (3 & (int)(size_t) ps) {
case STB_ps_direct: return ps;
case STB_ps_bucket: {
stb_ps_bucket *n = (stb_ps_bucket *) malloc(sizeof(*n));
*n = *GetBucket(ps);
return EncodeBucket(n);
}
case STB_ps_array: {
stb_ps_array *a = GetArray(ps);
stb_ps_array *n = (stb_ps_array *) malloc(sizeof(*n) + stb_ps_array_max * sizeof(n->p[0]));
n->count = a->count;
for (i=0; i < a->count; ++i)
n->p[i] = a->p[i];
return EncodeArray(n);
}
case STB_ps_hash: {
stb_ps_hash *h = GetHash(ps);
stb_ps_hash *n = stb_ps_makehash(h->size, h->size, h->table);
return EncodeHash(n);
}
}
assert(0); /* NOTREACHED */
return NULL;
}
int stb_ps_find(stb_ps *ps, void *value)
{
int i, code = 3 & (int)(size_t) ps;
assert((3 & (int)(size_t) value) == STB_ps_direct);
assert(stb_ps_fastlist_valid(value));
// not a switch: order based on expected performance/power-law distribution
if (code == STB_ps_direct)
return value == ps;
if (code == STB_ps_bucket) {
stb_ps_bucket *b = GetBucket(ps);
assert(STB_BUCKET_SIZE == 4);
if (b->p[0] == value || b->p[1] == value ||
b->p[2] == value || b->p[3] == value)
return STB_TRUE;
return STB_FALSE;
}
if (code == STB_ps_array) {
stb_ps_array *a = GetArray(ps);
for (i=0; i < a->count; ++i)
if (a->p[i] == value)
return STB_TRUE;
return STB_FALSE;
} else {
stb_ps_hash *h = GetHash(ps);
stb_uint32 hash = stb_hashptr(value);
stb_uint32 s, n = hash & h->mask;
void **t = h->table;
if (t[n] == value) return STB_TRUE;
if (t[n] == NULL) return STB_FALSE;
s = stb_rehash(hash) | 1;
do {
n = (n + s) & h->mask;
if (t[n] == value) return STB_TRUE;
} while (t[n] != NULL);
return STB_FALSE;
}
}
stb_ps * stb_ps_add (stb_ps *ps, void *value)
{
#ifdef STB_DEBUG
assert(!stb_ps_find(ps,value));
#endif
if (value == NULL) return ps; // ignore NULL adds to avoid bad breakage
assert((3 & (int)(size_t) value) == STB_ps_direct);
assert(stb_ps_fastlist_valid(value));
assert(value != STB_DEL); // STB_DEL is less likely
switch (3 & (int)(size_t) ps) {
case STB_ps_direct:
if (ps == NULL) return (stb_ps *) value;
return EncodeBucket(stb_bucket_create2(ps,value));
case STB_ps_bucket: {
stb_ps_bucket *b = GetBucket(ps);
stb_ps_array *a;
assert(STB_BUCKET_SIZE == 4);
if (b->p[0] == NULL) { b->p[0] = value; return ps; }
if (b->p[1] == NULL) { b->p[1] = value; return ps; }
if (b->p[2] == NULL) { b->p[2] = value; return ps; }
if (b->p[3] == NULL) { b->p[3] = value; return ps; }
a = (stb_ps_array *) malloc(sizeof(*a) + 7 * sizeof(a->p[0])); // 8 slots, must be 2^k
memcpy(a->p, b, sizeof(*b));
a->p[4] = value;
a->count = 5;
stb_bucket_free(b);
return EncodeArray(a);
}
case STB_ps_array: {
stb_ps_array *a = GetArray(ps);
if (a->count == stb_ps_array_max) {
// promote from array to hash
stb_ps_hash *h = stb_ps_makehash(2 << stb_log2_ceil(a->count), a->count, a->p);
free(a);
return stb_ps_add(EncodeHash(h), value);
}
// do we need to resize the array? the array doubles in size when it
// crosses a power-of-two
if ((a->count & (a->count-1))==0) {
int newsize = a->count*2;
// clamp newsize to max if:
// 1. it's larger than max
// 2. newsize*1.5 is larger than max (to avoid extra resizing)
if (newsize + a->count > stb_ps_array_max)
newsize = stb_ps_array_max;
a = (stb_ps_array *) realloc(a, sizeof(*a) + (newsize-1) * sizeof(a->p[0]));
}
a->p[a->count++] = value;
return EncodeArray(a);
}
case STB_ps_hash: {
stb_ps_hash *h = GetHash(ps);
stb_uint32 hash = stb_hashptr(value);
stb_uint32 n = hash & h->mask;
void **t = h->table;
// find first NULL or STB_DEL entry
if (!stb_ps_empty((size_t)t[n])) {
stb_uint32 s = stb_rehash(hash) | 1;
do {
n = (n + s) & h->mask;
} while (!stb_ps_empty((size_t)t[n]));
}
if (t[n] == STB_DEL)
-- h->count_deletes;
t[n] = value;
++ h->count;
if (h->count == h->grow_threshhold) {
stb_ps_hash *h2 = stb_ps_makehash(h->size*2, h->size, t);
free(h);
return EncodeHash(h2);
}
if (h->count + h->count_deletes == h->rehash_threshhold) {
stb_ps_hash *h2 = stb_ps_makehash(h->size, h->size, t);
free(h);
return EncodeHash(h2);
}
return ps;
}
}
return NULL; /* NOTREACHED */
}
stb_ps *stb_ps_remove(stb_ps *ps, void *value)
{
#ifdef STB_DEBUG
assert(stb_ps_find(ps, value));
#endif
assert((3 & (int)(size_t) value) == STB_ps_direct);
if (value == NULL) return ps; // ignore NULL removes to avoid bad breakage
switch (3 & (int)(size_t) ps) {
case STB_ps_direct:
return ps == value ? NULL : ps;
case STB_ps_bucket: {
stb_ps_bucket *b = GetBucket(ps);
int count=0;
assert(STB_BUCKET_SIZE == 4);
if (b->p[0] == value) b->p[0] = NULL; else count += (b->p[0] != NULL);
if (b->p[1] == value) b->p[1] = NULL; else count += (b->p[1] != NULL);
if (b->p[2] == value) b->p[2] = NULL; else count += (b->p[2] != NULL);
if (b->p[3] == value) b->p[3] = NULL; else count += (b->p[3] != NULL);
if (count == 1) { // shrink bucket at size 1
value = b->p[0];
if (value == NULL) value = b->p[1];
if (value == NULL) value = b->p[2];
if (value == NULL) value = b->p[3];
assert(value != NULL);
stb_bucket_free(b);
return (stb_ps *) value; // return STB_ps_direct of value
}
return ps;
}
case STB_ps_array: {
stb_ps_array *a = GetArray(ps);
int i;
for (i=0; i < a->count; ++i) {
if (a->p[i] == value) {
a->p[i] = a->p[--a->count];
if (a->count == 3) { // shrink to bucket!
stb_ps_bucket *b = stb_bucket_create3(a->p);
free(a);
return EncodeBucket(b);
}
return ps;
}
}
return ps;
}
case STB_ps_hash: {
stb_ps_hash *h = GetHash(ps);
stb_uint32 hash = stb_hashptr(value);
stb_uint32 s, n = hash & h->mask;
void **t = h->table;
if (t[n] != value) {
s = stb_rehash(hash) | 1;
do {
n = (n + s) & h->mask;
} while (t[n] != value);
}
t[n] = STB_DEL;
-- h->count;
++ h->count_deletes;
// should we shrink down to an array?
if (h->count < stb_ps_array_max) {
int n = 1 << stb_log2_floor(stb_ps_array_max);
if (h->count < n) {
stb_ps_array *a = (stb_ps_array *) malloc(sizeof(*a) + (n-1) * sizeof(a->p[0]));
int i,j=0;
for (i=0; i < h->size; ++i)
if (!stb_ps_empty((size_t)t[i]))
a->p[j++] = t[i];
assert(j == h->count);
a->count = j;
free(h);
return EncodeArray(a);
}
}
if (h->count == h->shrink_threshhold) {
stb_ps_hash *h2 = stb_ps_makehash(h->size >> 1, h->size, t);
free(h);
return EncodeHash(h2);
}
return ps;
}
}
return ps; /* NOTREACHED */
}
stb_ps *stb_ps_remove_any(stb_ps *ps, void **value)
{
assert(ps != NULL);
switch (3 & (int)(size_t) ps) {
case STB_ps_direct:
*value = ps;
return NULL;
case STB_ps_bucket: {
stb_ps_bucket *b = GetBucket(ps);
int count=0, slast=0, last=0;
assert(STB_BUCKET_SIZE == 4);
if (b->p[0]) { ++count; last = 0; }
if (b->p[1]) { ++count; slast = last; last = 1; }
if (b->p[2]) { ++count; slast = last; last = 2; }
if (b->p[3]) { ++count; slast = last; last = 3; }
*value = b->p[last];
b->p[last] = 0;
if (count == 2) {
void *leftover = b->p[slast]; // second to last
stb_bucket_free(b);
return (stb_ps *) leftover;
}
return ps;
}
case STB_ps_array: {
stb_ps_array *a = GetArray(ps);
*value = a->p[a->count-1];
if (a->count == 4)
return stb_ps_remove(ps, *value);
--a->count;
return ps;
}
case STB_ps_hash: {
stb_ps_hash *h = GetHash(ps);
void **t = h->table;
stb_uint32 n = h->any_offset;
while (stb_ps_empty((size_t)t[n]))
n = (n + 1) & h->mask;
*value = t[n];
h->any_offset = (n+1) & h->mask;
// check if we need to skip down to the previous type
if (h->count-1 < stb_ps_array_max || h->count-1 == h->shrink_threshhold)
return stb_ps_remove(ps, *value);
t[n] = STB_DEL;
-- h->count;
++ h->count_deletes;
return ps;
}
}
return ps; /* NOTREACHED */
}
void ** stb_ps_getlist(stb_ps *ps, int *count)
{
int i,n=0;
void **p = NULL;
switch (3 & (int)(size_t) ps) {
case STB_ps_direct:
if (ps == NULL) { *count = 0; return NULL; }
p = (void **) malloc(sizeof(*p) * 1);
p[0] = ps;
*count = 1;
return p;
case STB_ps_bucket: {
stb_ps_bucket *b = GetBucket(ps);
p = (void **) malloc(sizeof(*p) * STB_BUCKET_SIZE);
for (i=0; i < STB_BUCKET_SIZE; ++i)
if (b->p[i] != NULL)
p[n++] = b->p[i];
break;
}
case STB_ps_array: {
stb_ps_array *a = GetArray(ps);
p = (void **) malloc(sizeof(*p) * a->count);
memcpy(p, a->p, sizeof(*p) * a->count);
*count = a->count;
return p;
}
case STB_ps_hash: {
stb_ps_hash *h = GetHash(ps);
p = (void **) malloc(sizeof(*p) * h->count);
for (i=0; i < h->size; ++i)
if (!stb_ps_empty((size_t)h->table[i]))
p[n++] = h->table[i];
break;
}
}
*count = n;
return p;
}
int stb_ps_writelist(stb_ps *ps, void **list, int size )
{
int i,n=0;
switch (3 & (int)(size_t) ps) {
case STB_ps_direct:
if (ps == NULL || size <= 0) return 0;
list[0] = ps;
return 1;
case STB_ps_bucket: {
stb_ps_bucket *b = GetBucket(ps);
for (i=0; i < STB_BUCKET_SIZE; ++i)
if (b->p[i] != NULL && n < size)
list[n++] = b->p[i];
return n;
}
case STB_ps_array: {
stb_ps_array *a = GetArray(ps);
n = stb_min(size, a->count);
memcpy(list, a->p, sizeof(*list) * n);
return n;
}
case STB_ps_hash: {
stb_ps_hash *h = GetHash(ps);
if (size <= 0) return 0;
for (i=0; i < h->count; ++i) {
if (!stb_ps_empty((size_t)h->table[i])) {
list[n++] = h->table[i];
if (n == size) break;
}
}
return n;
}
}
return 0; /* NOTREACHED */
}
int stb_ps_enum(stb_ps *ps, void *data, int (*func)(void *value, void *data))
{
int i;
switch (3 & (int)(size_t) ps) {
case STB_ps_direct:
if (ps == NULL) return STB_TRUE;
return func(ps, data);
case STB_ps_bucket: {
stb_ps_bucket *b = GetBucket(ps);
for (i=0; i < STB_BUCKET_SIZE; ++i)
if (b->p[i] != NULL)
if (!func(b->p[i], data))
return STB_FALSE;
return STB_TRUE;
}
case STB_ps_array: {
stb_ps_array *a = GetArray(ps);
for (i=0; i < a->count; ++i)
if (!func(a->p[i], data))
return STB_FALSE;
return STB_TRUE;
}
case STB_ps_hash: {
stb_ps_hash *h = GetHash(ps);
for (i=0; i < h->count; ++i)
if (!stb_ps_empty((size_t)h->table[i]))
if (!func(h->table[i], data))
return STB_FALSE;
return STB_TRUE;
}
}
return STB_TRUE; /* NOTREACHED */
}
int stb_ps_count (stb_ps *ps)
{
switch (3 & (int)(size_t) ps) {
case STB_ps_direct:
return ps != NULL;
case STB_ps_bucket: {
stb_ps_bucket *b = GetBucket(ps);
return (b->p[0] != NULL) + (b->p[1] != NULL) +
(b->p[2] != NULL) + (b->p[3] != NULL);
}
case STB_ps_array: {
stb_ps_array *a = GetArray(ps);
return a->count;
}
case STB_ps_hash: {
stb_ps_hash *h = GetHash(ps);
return h->count;
}
}
return 0;
}
void ** stb_ps_fastlist(stb_ps *ps, int *count)
{
static void *storage;
switch (3 & (int)(size_t) ps) {
case STB_ps_direct:
if (ps == NULL) { *count = 0; return NULL; }
storage = ps;
*count = 1;
return &storage;
case STB_ps_bucket: {
stb_ps_bucket *b = GetBucket(ps);
*count = STB_BUCKET_SIZE;
return b->p;
}
case STB_ps_array: {
stb_ps_array *a = GetArray(ps);
*count = a->count;
return a->p;
}
case STB_ps_hash: {
stb_ps_hash *h = GetHash(ps);
*count = h->size;
return h->table;
}
}
return NULL; /* NOTREACHED */
}
int stb_ps_subset(stb_ps *bigger, stb_ps *smaller)
{
int i, listlen;
void **list = stb_ps_fastlist(smaller, &listlen);
for(i=0; i < listlen; ++i)
if (stb_ps_fastlist_valid(list[i]))
if (!stb_ps_find(bigger, list[i]))
return 0;
return 1;
}
int stb_ps_eq(stb_ps *p0, stb_ps *p1)
{
if (stb_ps_count(p0) != stb_ps_count(p1))
return 0;
return stb_ps_subset(p0, p1);
}
#undef GetBucket
#undef GetArray
#undef GetHash
#undef EncodeBucket
#undef EncodeArray
#undef EncodeHash
#endif
//////////////////////////////////////////////////////////////////////////////
//
// Random Numbers via Meresenne Twister or LCG
//
STB_EXTERN unsigned int stb_srandLCG(unsigned int seed);
STB_EXTERN unsigned int stb_randLCG(void);
STB_EXTERN double stb_frandLCG(void);
STB_EXTERN void stb_srand(unsigned int seed);
STB_EXTERN unsigned int stb_rand(void);
STB_EXTERN double stb_frand(void);
STB_EXTERN void stb_shuffle(void *p, size_t n, size_t sz,
unsigned int seed);
STB_EXTERN void stb_reverse(void *p, size_t n, size_t sz);
STB_EXTERN unsigned int stb_randLCG_explicit(unsigned int seed);
#define stb_rand_define(x,y) \
\
unsigned int x(void) \
{ \
static unsigned int stb__rand = y; \
stb__rand = stb__rand * 2147001325 + 715136305; /* BCPL */ \
return 0x31415926 ^ ((stb__rand >> 16) + (stb__rand << 16)); \
}
#ifdef STB_DEFINE
unsigned int stb_randLCG_explicit(unsigned int seed)
{
return seed * 2147001325 + 715136305;
}
static unsigned int stb__rand_seed=0;
unsigned int stb_srandLCG(unsigned int seed)
{
unsigned int previous = stb__rand_seed;
stb__rand_seed = seed;
return previous;
}
unsigned int stb_randLCG(void)
{
stb__rand_seed = stb__rand_seed * 2147001325 + 715136305; // BCPL generator
// shuffle non-random bits to the middle, and xor to decorrelate with seed
return 0x31415926 ^ ((stb__rand_seed >> 16) + (stb__rand_seed << 16));
}
double stb_frandLCG(void)
{
return stb_randLCG() / ((double) (1 << 16) * (1 << 16));
}
void stb_shuffle(void *p, size_t n, size_t sz, unsigned int seed)
{
char *a;
unsigned int old_seed;
int i;
if (seed)
old_seed = stb_srandLCG(seed);
a = (char *) p + (n-1) * sz;
for (i=(int) n; i > 1; --i) {
int j = stb_randLCG() % i;
stb_swap(a, (char *) p + j * sz, sz);
a -= sz;
}
if (seed)
stb_srandLCG(old_seed);
}
void stb_reverse(void *p, size_t n, size_t sz)
{
size_t i,j = n-1;
for (i=0; i < j; ++i,--j) {
stb_swap((char *) p + i * sz, (char *) p + j * sz, sz);
}
}
// public domain Mersenne Twister by Michael Brundage
#define STB__MT_LEN 624
int stb__mt_index = STB__MT_LEN*sizeof(int)+1;
unsigned int stb__mt_buffer[STB__MT_LEN];
void stb_srand(unsigned int seed)
{
int i;
unsigned int old = stb_srandLCG(seed);
for (i = 0; i < STB__MT_LEN; i++)
stb__mt_buffer[i] = stb_randLCG();
stb_srandLCG(old);
stb__mt_index = STB__MT_LEN*sizeof(unsigned int);
}
#define STB__MT_IA 397
#define STB__MT_IB (STB__MT_LEN - STB__MT_IA)
#define STB__UPPER_MASK 0x80000000
#define STB__LOWER_MASK 0x7FFFFFFF
#define STB__MATRIX_A 0x9908B0DF
#define STB__TWIST(b,i,j) ((b)[i] & STB__UPPER_MASK) | ((b)[j] & STB__LOWER_MASK)
#define STB__MAGIC(s) (((s)&1)*STB__MATRIX_A)
unsigned int stb_rand()
{
unsigned int * b = stb__mt_buffer;
int idx = stb__mt_index;
unsigned int s,r;
int i;
if (idx >= STB__MT_LEN*sizeof(unsigned int)) {
if (idx > STB__MT_LEN*sizeof(unsigned int))
stb_srand(0);
idx = 0;
i = 0;
for (; i < STB__MT_IB; i++) {
s = STB__TWIST(b, i, i+1);
b[i] = b[i + STB__MT_IA] ^ (s >> 1) ^ STB__MAGIC(s);
}
for (; i < STB__MT_LEN-1; i++) {
s = STB__TWIST(b, i, i+1);
b[i] = b[i - STB__MT_IB] ^ (s >> 1) ^ STB__MAGIC(s);
}
s = STB__TWIST(b, STB__MT_LEN-1, 0);
b[STB__MT_LEN-1] = b[STB__MT_IA-1] ^ (s >> 1) ^ STB__MAGIC(s);
}
stb__mt_index = idx + sizeof(unsigned int);
r = *(unsigned int *)((unsigned char *)b + idx);
r ^= (r >> 11);
r ^= (r << 7) & 0x9D2C5680;
r ^= (r << 15) & 0xEFC60000;
r ^= (r >> 18);
return r;
}
double stb_frand(void)
{
return stb_rand() / ((double) (1 << 16) * (1 << 16));
}
#endif
//////////////////////////////////////////////////////////////////////////////
//
// stb_dupe
//
// stb_dupe is a duplicate-finding system for very, very large data
// structures--large enough that sorting is too slow, but not so large
// that we can't keep all the data in memory. using it works as follows:
//
// 1. create an stb_dupe:
// provide a hash function
// provide an equality function
// provide an estimate for the size
// optionally provide a comparison function
//
// 2. traverse your data, 'adding' pointers to the stb_dupe
//
// 3. finish and ask for duplicates
//
// the stb_dupe will discard its intermediate data and build
// a collection of sorted lists of duplicates, with non-duplicate
// entries omitted entirely
//
//
// Implementation strategy:
//
// while collecting the N items, we keep a hash table of approximate
// size sqrt(N). (if you tell use the N up front, the hash table is
// just that size exactly)
//
// each entry in the hash table is just an stb__arr of pointers (no need
// to use stb_ps, because we don't need to delete from these)
//
// for step 3, for each entry in the hash table, we apply stb_dupe to it
// recursively. once the size gets small enough (or doesn't decrease
// significantly), we switch to either using qsort() on the comparison
// function, or else we just do the icky N^2 gather
typedef struct stb_dupe stb_dupe;
typedef int (*stb_compare_func)(void *a, void *b);
typedef int (*stb_hash_func)(void *a, unsigned int seed);
STB_EXTERN void stb_dupe_free(stb_dupe *sd);
STB_EXTERN stb_dupe *stb_dupe_create(stb_hash_func hash,
stb_compare_func eq, int size, stb_compare_func ineq);
STB_EXTERN void stb_dupe_add(stb_dupe *sd, void *item);
STB_EXTERN void stb_dupe_finish(stb_dupe *sd);
STB_EXTERN int stb_dupe_numsets(stb_dupe *sd);
STB_EXTERN void **stb_dupe_set(stb_dupe *sd, int num);
STB_EXTERN int stb_dupe_set_count(stb_dupe *sd, int num);
struct stb_dupe
{
void ***hash_table;
int hash_size;
int size_log2;
int population;
int hash_shift;
stb_hash_func hash;
stb_compare_func eq;
stb_compare_func ineq;
void ***dupes;
};
#ifdef STB_DEFINE
int stb_dupe_numsets(stb_dupe *sd)
{
assert(sd->hash_table == NULL);
return stb_arr_len(sd->dupes);
}
void **stb_dupe_set(stb_dupe *sd, int num)
{
assert(sd->hash_table == NULL);
return sd->dupes[num];
}
int stb_dupe_set_count(stb_dupe *sd, int num)
{
assert(sd->hash_table == NULL);
return stb_arr_len(sd->dupes[num]);
}
stb_dupe *stb_dupe_create(stb_hash_func hash, stb_compare_func eq, int size,
stb_compare_func ineq)
{
int i, hsize;
stb_dupe *sd = (stb_dupe *) malloc(sizeof(*sd));
sd->size_log2 = 4;
hsize = 1 << sd->size_log2;
while (hsize * hsize < size) {
++sd->size_log2;
hsize *= 2;
}
sd->hash = hash;
sd->eq = eq;
sd->ineq = ineq;
sd->hash_shift = 0;
sd->population = 0;
sd->hash_size = hsize;
sd->hash_table = (void ***) malloc(sizeof(*sd->hash_table) * hsize);
for (i=0; i < hsize; ++i)
sd->hash_table[i] = NULL;
sd->dupes = NULL;
return sd;
}
void stb_dupe_add(stb_dupe *sd, void *item)
{
stb_uint32 hash = sd->hash(item, sd->hash_shift);
int z = hash & (sd->hash_size-1);
stb_arr_push(sd->hash_table[z], item);
++sd->population;
}
void stb_dupe_free(stb_dupe *sd)
{
int i;
for (i=0; i < stb_arr_len(sd->dupes); ++i)
if (sd->dupes[i])
stb_arr_free(sd->dupes[i]);
stb_arr_free(sd->dupes);
free(sd);
}
static stb_compare_func stb__compare;
static int stb__dupe_compare(const void *a, const void *b)
{
void *p = *(void **) a;
void *q = *(void **) b;
return stb__compare(p,q);
}
void stb_dupe_finish(stb_dupe *sd)
{
int i,j,k;
assert(sd->dupes == NULL);
for (i=0; i < sd->hash_size; ++i) {
void ** list = sd->hash_table[i];
if (list != NULL) {
int n = stb_arr_len(list);
// @TODO: measure to find good numbers instead of just making them up!
int thresh = (sd->ineq ? 200 : 20);
// if n is large enough to be worth it, and n is smaller than
// before (so we can guarantee we'll use a smaller hash table);
// and there are enough hash bits left, assuming full 32-bit hash
if (n > thresh && n < (sd->population >> 3) && sd->hash_shift + sd->size_log2*2 < 32) {
// recursively process this row using stb_dupe, O(N log log N)
stb_dupe *d = stb_dupe_create(sd->hash, sd->eq, n, sd->ineq);
d->hash_shift = stb_randLCG_explicit(sd->hash_shift);
for (j=0; j < n; ++j)
stb_dupe_add(d, list[j]);
stb_arr_free(sd->hash_table[i]);
stb_dupe_finish(d);
for (j=0; j < stb_arr_len(d->dupes); ++j) {
stb_arr_push(sd->dupes, d->dupes[j]);
d->dupes[j] = NULL; // take over ownership
}
stb_dupe_free(d);
} else if (sd->ineq) {
// process this row using qsort(), O(N log N)
stb__compare = sd->ineq;
qsort(list, n, sizeof(list[0]), stb__dupe_compare);
// find equal subsequences of the list
for (j=0; j < n-1; ) {
// find a subsequence from j..k
for (k=j; k < n; ++k)
// only use ineq so eq can be left undefined
if (sd->ineq(list[j], list[k]))
break;
// k is the first one not in the subsequence
if (k-j > 1) {
void **mylist = NULL;
stb_arr_setlen(mylist, k-j);
memcpy(mylist, list+j, sizeof(list[j]) * (k-j));
stb_arr_push(sd->dupes, mylist);
}
j = k;
}
stb_arr_free(sd->hash_table[i]);
} else {
// process this row using eq(), O(N^2)
for (j=0; j < n; ++j) {
if (list[j] != NULL) {
void **output = NULL;
for (k=j+1; k < n; ++k) {
if (sd->eq(list[j], list[k])) {
if (output == NULL)
stb_arr_push(output, list[j]);
stb_arr_push(output, list[k]);
list[k] = NULL;
}
}
list[j] = NULL;
if (output)
stb_arr_push(sd->dupes, output);
}
}
stb_arr_free(sd->hash_table[i]);
}
}
}
free(sd->hash_table);
sd->hash_table = NULL;
}
#endif
//////////////////////////////////////////////////////////////////////////////
//
// templatized Sort routine
//
// This is an attempt to implement a templated sorting algorithm.
// To use it, you have to explicitly instantiate it as a _function_,
// then you call that function. This allows the comparison to be inlined,
// giving the sort similar performance to C++ sorts.
//
// It implements quicksort with three-way-median partitioning (generally
// well-behaved), with a final insertion sort pass.
//
// When you define the compare expression, you should assume you have
// elements of your array pointed to by 'a' and 'b', and perform the comparison
// on those. OR you can use one or more statements; first say '0;', then
// write whatever code you want, and compute the result into a variable 'c'.
#define stb_declare_sort(FUNCNAME, TYPE) \
void FUNCNAME(TYPE *p, int n)
#define stb_define_sort(FUNCNAME,TYPE,COMPARE) \
stb__define_sort( void, FUNCNAME,TYPE,COMPARE)
#define stb_define_sort_static(FUNCNAME,TYPE,COMPARE) \
stb__define_sort(static void, FUNCNAME,TYPE,COMPARE)
#define stb__define_sort(MODE, FUNCNAME, TYPE, COMPARE) \
\
static void STB_(FUNCNAME,_ins_sort)(TYPE *p, int n) \
{ \
int i,j; \
for (i=1; i < n; ++i) { \
TYPE t = p[i], *a = &t; \
j = i; \
while (j > 0) { \
TYPE *b = &p[j-1]; \
int c = COMPARE; \
if (!c) break; \
p[j] = p[j-1]; \
--j; \
} \
if (i != j) \
p[j] = t; \
} \
} \
\
static void STB_(FUNCNAME,_quicksort)(TYPE *p, int n) \
{ \
/* threshold for transitioning to insertion sort */ \
while (n > 12) { \
TYPE *a,*b,t; \
int c01,c12,c,m,i,j; \
\
/* compute median of three */ \
m = n >> 1; \
a = &p[0]; \
b = &p[m]; \
c = COMPARE; \
c01 = c; \
a = &p[m]; \
b = &p[n-1]; \
c = COMPARE; \
c12 = c; \
/* if 0 >= mid >= end, or 0 < mid < end, then use mid */ \
if (c01 != c12) { \
/* otherwise, we'll need to swap something else to middle */ \
int z; \
a = &p[0]; \
b = &p[n-1]; \
c = COMPARE; \
/* 0>mid && mid<n: 0>n => n; 0<n => 0 */ \
/* 0<mid && mid>n: 0>n => 0; 0<n => n */ \
z = (c == c12) ? 0 : n-1; \
t = p[z]; \
p[z] = p[m]; \
p[m] = t; \
} \
/* now p[m] is the median-of-three */ \
/* swap it to the beginning so it won't move around */ \
t = p[0]; \
p[0] = p[m]; \
p[m] = t; \
\
/* partition loop */ \
i=1; \
j=n-1; \
for(;;) { \
/* handling of equality is crucial here */ \
/* for sentinels & efficiency with duplicates */ \
b = &p[0]; \
for (;;++i) { \
a=&p[i]; \
c = COMPARE; \
if (!c) break; \
} \
a = &p[0]; \
for (;;--j) { \
b=&p[j]; \
c = COMPARE; \
if (!c) break; \
} \
/* make sure we haven't crossed */ \
if (i >= j) break; \
t = p[i]; \
p[i] = p[j]; \
p[j] = t; \
\
++i; \
--j; \
} \
/* recurse on smaller side, iterate on larger */ \
if (j < (n-i)) { \
STB_(FUNCNAME,_quicksort)(p,j); \
p = p+i; \
n = n-i; \
} else { \
STB_(FUNCNAME,_quicksort)(p+i, n-i); \
n = j; \
} \
} \
} \
\
MODE FUNCNAME(TYPE *p, int n) \
{ \
STB_(FUNCNAME, _quicksort)(p, n); \
STB_(FUNCNAME, _ins_sort)(p, n); \
} \
//////////////////////////////////////////////////////////////////////////////
//
// stb_bitset an array of booleans indexed by integers
//
typedef stb_uint32 stb_bitset;
STB_EXTERN stb_bitset *stb_bitset_new(int value, int len);
#define stb_bitset_clearall(arr,len) (memset(arr, 0, 4 * (len)))
#define stb_bitset_setall(arr,len) (memset(arr, 255, 4 * (len)))
#define stb_bitset_setbit(arr,n) ((arr)[(n) >> 5] |= (1 << (n & 31)))
#define stb_bitset_clearbit(arr,n) ((arr)[(n) >> 5] &= ~(1 << (n & 31)))
#define stb_bitset_testbit(arr,n) ((arr)[(n) >> 5] & (1 << (n & 31)))
STB_EXTERN stb_bitset *stb_bitset_union(stb_bitset *p0, stb_bitset *p1, int len);
STB_EXTERN int *stb_bitset_getlist(stb_bitset *out, int start, int end);
STB_EXTERN int stb_bitset_eq(stb_bitset *p0, stb_bitset *p1, int len);
STB_EXTERN int stb_bitset_disjoint(stb_bitset *p0, stb_bitset *p1, int len);
STB_EXTERN int stb_bitset_disjoint_0(stb_bitset *p0, stb_bitset *p1, int len);
STB_EXTERN int stb_bitset_subset(stb_bitset *bigger, stb_bitset *smaller, int len);
STB_EXTERN int stb_bitset_unioneq_changed(stb_bitset *p0, stb_bitset *p1, int len);
#ifdef STB_DEFINE
int stb_bitset_eq(stb_bitset *p0, stb_bitset *p1, int len)
{
int i;
for (i=0; i < len; ++i)
if (p0[i] != p1[i]) return 0;
return 1;
}
int stb_bitset_disjoint(stb_bitset *p0, stb_bitset *p1, int len)
{
int i;
for (i=0; i < len; ++i)
if (p0[i] & p1[i]) return 0;
return 1;
}
int stb_bitset_disjoint_0(stb_bitset *p0, stb_bitset *p1, int len)
{
int i;
for (i=0; i < len; ++i)
if ((p0[i] | p1[i]) != 0xffffffff) return 0;
return 1;
}
int stb_bitset_subset(stb_bitset *bigger, stb_bitset *smaller, int len)
{
int i;
for (i=0; i < len; ++i)
if ((bigger[i] & smaller[i]) != smaller[i]) return 0;
return 1;
}
stb_bitset *stb_bitset_union(stb_bitset *p0, stb_bitset *p1, int len)
{
int i;
stb_bitset *d = (stb_bitset *) malloc(sizeof(*d) * len);
for (i=0; i < len; ++i) d[i] = p0[i] | p1[i];
return d;
}
int stb_bitset_unioneq_changed(stb_bitset *p0, stb_bitset *p1, int len)
{
int i, changed=0;
for (i=0; i < len; ++i) {
stb_bitset d = p0[i] | p1[i];
if (d != p0[i]) {
p0[i] = d;
changed = 1;
}
}
return changed;
}
stb_bitset *stb_bitset_new(int value, int len)
{
int i;
stb_bitset *d = (stb_bitset *) malloc(sizeof(*d) * len);
if (value) value = 0xffffffff;
for (i=0; i < len; ++i) d[i] = value;
return d;
}
int *stb_bitset_getlist(stb_bitset *out, int start, int end)
{
int *list = NULL;
int i;
for (i=start; i < end; ++i)
if (stb_bitset_testbit(out, i))
stb_arr_push(list, i);
return list;
}
#endif
//////////////////////////////////////////////////////////////////////////////
//
// stb_wordwrap quality word-wrapping for fixed-width fonts
//
STB_EXTERN int stb_wordwrap(int *pairs, int pair_max, int count, char *str);
STB_EXTERN int *stb_wordwrapalloc(int count, char *str);
#ifdef STB_DEFINE
int stb_wordwrap(int *pairs, int pair_max, int count, char *str)
{
int n=0,i=0, start=0,nonwhite=0;
if (pairs == NULL) pair_max = 0x7ffffff0;
else pair_max *= 2;
// parse
for(;;) {
int s=i; // first whitespace char; last nonwhite+1
int w; // word start
// accept whitespace
while (isspace(str[i])) {
if (str[i] == '\n' || str[i] == '\r') {
if (str[i] + str[i+1] == '\n' + '\r') ++i;
if (n >= pair_max) return -1;
if (pairs) pairs[n] = start, pairs[n+1] = s-start;
n += 2;
nonwhite=0;
start = i+1;
s = start;
}
++i;
}
if (i >= start+count) {
// we've gone off the end using whitespace
if (nonwhite) {
if (n >= pair_max) return -1;
if (pairs) pairs[n] = start, pairs[n+1] = s-start;
n += 2;
start = s = i;
nonwhite=0;
} else {
// output all the whitespace
while (i >= start+count) {
if (n >= pair_max) return -1;
if (pairs) pairs[n] = start, pairs[n+1] = count;
n += 2;
start += count;
}
s = start;
}
}
if (str[i] == 0) break;
// now scan out a word and see if it fits
w = i;
while (str[i] && !isspace(str[i])) {
++i;
}
// wrapped?
if (i > start + count) {
// huge?
if (i-s <= count) {
if (n >= pair_max) return -1;
if (pairs) pairs[n] = start, pairs[n+1] = s-start;
n += 2;
start = w;
} else {
// This word is longer than one line. If we wrap it onto N lines
// there are leftover chars. do those chars fit on the cur line?
// But if we have leading whitespace, we force it to start here.
if ((w-start) + ((i-w) % count) <= count || !nonwhite) {
// output a full line
if (n >= pair_max) return -1;
if (pairs) pairs[n] = start, pairs[n+1] = count;
n += 2;
start += count;
w = start;
} else {
// output a partial line, trimming trailing whitespace
if (s != start) {
if (n >= pair_max) return -1;
if (pairs) pairs[n] = start, pairs[n+1] = s-start;
n += 2;
start = w;
}
}
// now output full lines as needed
while (start + count <= i) {
if (n >= pair_max) return -1;
if (pairs) pairs[n] = start, pairs[n+1] = count;
n += 2;
start += count;
}
}
}
nonwhite=1;
}
if (start < i) {
if (n >= pair_max) return -1;
if (pairs) pairs[n] = start, pairs[n+1] = i-start;
n += 2;
}
return n>>1;
}
int *stb_wordwrapalloc(int count, char *str)
{
int n = stb_wordwrap(NULL,0,count,str);
int *z = NULL;
stb_arr_setlen(z, n*2);
stb_wordwrap(z, n, count, str);
return z;
}
#endif
//////////////////////////////////////////////////////////////////////////////
//
// stb_match: wildcards and regexping
//
STB_EXTERN int stb_wildmatch (char *expr, char *candidate);
STB_EXTERN int stb_wildmatchi(char *expr, char *candidate);
STB_EXTERN int stb_wildfind (char *expr, char *candidate);
STB_EXTERN int stb_wildfindi (char *expr, char *candidate);
STB_EXTERN int stb_regex(char *regex, char *candidate);
typedef struct stb_matcher stb_matcher;
STB_EXTERN stb_matcher *stb_regex_matcher(char *regex);
STB_EXTERN int stb_matcher_match(stb_matcher *m, char *str);
STB_EXTERN int stb_matcher_find(stb_matcher *m, char *str);
STB_EXTERN void stb_matcher_free(stb_matcher *f);
STB_EXTERN stb_matcher *stb_lex_matcher(void);
STB_EXTERN int stb_lex_item(stb_matcher *m, const char *str, int result);
STB_EXTERN int stb_lex_item_wild(stb_matcher *matcher, const char *regex, int result);
STB_EXTERN int stb_lex(stb_matcher *m, char *str, int *len);
#ifdef STB_DEFINE
static int stb__match_qstring(char *candidate, char *qstring, int qlen, int insensitive)
{
int i;
if (insensitive) {
for (i=0; i < qlen; ++i)
if (qstring[i] == '?') {
if (!candidate[i]) return 0;
} else
if (tolower(qstring[i]) != tolower(candidate[i]))
return 0;
} else {
for (i=0; i < qlen; ++i)
if (qstring[i] == '?') {
if (!candidate[i]) return 0;
} else
if (qstring[i] != candidate[i])
return 0;
}
return 1;
}
static int stb__find_qstring(char *candidate, char *qstring, int qlen, int insensitive)
{
char c;
int offset=0;
while (*qstring == '?') {
++qstring;
--qlen;
++candidate;
if (qlen == 0) return 0;
if (*candidate == 0) return -1;
}
c = *qstring++;
--qlen;
if (insensitive) c = tolower(c);
while (candidate[offset]) {
if (c == (insensitive ? tolower(candidate[offset]) : candidate[offset]))
if (stb__match_qstring(candidate+offset+1, qstring, qlen, insensitive))
return offset;
++offset;
}
return -1;
}
int stb__wildmatch_raw2(char *expr, char *candidate, int search, int insensitive)
{
int where=0;
int start = -1;
if (!search) {
// parse to first '*'
if (*expr != '*')
start = 0;
while (*expr != '*') {
if (!*expr)
return *candidate == 0 ? 0 : -1;
if (*expr == '?') {
if (!*candidate) return -1;
} else {
if (insensitive) {
if (tolower(*candidate) != tolower(*expr))
return -1;
} else
if (*candidate != *expr)
return -1;
}
++candidate, ++expr, ++where;
}
} else {
// 0-length search string
if (!*expr)
return 0;
}
assert(search || *expr == '*');
if (!search)
++expr;
// implicit '*' at this point
while (*expr) {
int o=0;
// combine redundant * characters
while (expr[0] == '*') ++expr;
// ok, at this point, expr[-1] == '*',
// and expr[0] != '*'
if (!expr[0]) return start >= 0 ? start : 0;
// now find next '*'
o = 0;
while (expr[o] != '*') {
if (expr[o] == 0)
break;
++o;
}
// if no '*', scan to end, then match at end
if (expr[o] == 0 && !search) {
int z;
for (z=0; z < o; ++z)
if (candidate[z] == 0)
return -1;
while (candidate[z])
++z;
// ok, now check if they match
if (stb__match_qstring(candidate+z-o, expr, o, insensitive))
return start >= 0 ? start : 0;
return -1;
} else {
// if yes '*', then do stb__find_qmatch on the intervening chars
int n = stb__find_qstring(candidate, expr, o, insensitive);
if (n < 0)
return -1;
if (start < 0)
start = where + n;
expr += o;
candidate += n+o;
}
if (*expr == 0) {
assert(search);
return start;
}
assert(*expr == '*');
++expr;
}
return start >= 0 ? start : 0;
}
int stb__wildmatch_raw(char *expr, char *candidate, int search, int insensitive)
{
char buffer[256];
// handle multiple search strings
char *s = strchr(expr, ';');
char *last = expr;
while (s) {
int z;
// need to allow for non-writeable strings... assume they're small
if (s - last < 256) {
stb_strncpy(buffer, last, (int) (s-last+1));
z = stb__wildmatch_raw2(buffer, candidate, search, insensitive);
} else {
*s = 0;
z = stb__wildmatch_raw2(last, candidate, search, insensitive);
*s = ';';
}
if (z >= 0) return z;
last = s+1;
s = strchr(last, ';');
}
return stb__wildmatch_raw2(last, candidate, search, insensitive);
}
int stb_wildmatch(char *expr, char *candidate)
{
return stb__wildmatch_raw(expr, candidate, 0,0) >= 0;
}
int stb_wildmatchi(char *expr, char *candidate)
{
return stb__wildmatch_raw(expr, candidate, 0,1) >= 0;
}
int stb_wildfind(char *expr, char *candidate)
{
return stb__wildmatch_raw(expr, candidate, 1,0);
}
int stb_wildfindi(char *expr, char *candidate)
{
return stb__wildmatch_raw(expr, candidate, 1,1);
}
typedef struct
{
stb_int16 transition[256];
} stb_dfa;
// an NFA node represents a state you're in; it then has
// an arbitrary number of edges dangling off of it
// note this isn't utf8-y
typedef struct
{
stb_int16 match; // character/set to match
stb_uint16 node; // output node to go to
} stb_nfa_edge;
typedef struct
{
stb_int16 goal; // does reaching this win the prize?
stb_uint8 active; // is this in the active list
stb_nfa_edge *out;
stb_uint16 *eps; // list of epsilon closures
} stb_nfa_node;
#define STB__DFA_UNDEF -1
#define STB__DFA_GOAL -2
#define STB__DFA_END -3
#define STB__DFA_MGOAL -4
#define STB__DFA_VALID 0
#define STB__NFA_STOP_GOAL -1
// compiled regexp
struct stb_matcher
{
stb_uint16 start_node;
stb_int16 dfa_start;
stb_uint32 *charset;
int num_charset;
int match_start;
stb_nfa_node *nodes;
int does_lex;
// dfa matcher
stb_dfa * dfa;
stb_uint32 * dfa_mapping;
stb_int16 * dfa_result;
int num_words_per_dfa;
};
static int stb__add_node(stb_matcher *matcher)
{
stb_nfa_node z;
z.active = 0;
z.eps = 0;
z.goal = 0;
z.out = 0;
stb_arr_push(matcher->nodes, z);
return stb_arr_len(matcher->nodes)-1;
}
static void stb__add_epsilon(stb_matcher *matcher, int from, int to)
{
assert(from != to);
if (matcher->nodes[from].eps == NULL)
stb_arr_malloc((void **) &matcher->nodes[from].eps, matcher);
stb_arr_push(matcher->nodes[from].eps, to);
}
static void stb__add_edge(stb_matcher *matcher, int from, int to, int type)
{
stb_nfa_edge z = { (stb_int16)type, (stb_uint16)to };
if (matcher->nodes[from].out == NULL)
stb_arr_malloc((void **) &matcher->nodes[from].out, matcher);
stb_arr_push(matcher->nodes[from].out, z);
}
static char *stb__reg_parse_alt(stb_matcher *m, int s, char *r, stb_uint16 *e);
static char *stb__reg_parse(stb_matcher *matcher, int start, char *regex, stb_uint16 *end)
{
int n;
int last_start = -1;
stb_uint16 last_end = start;
while (*regex) {
switch (*regex) {
case '(':
last_start = last_end;
regex = stb__reg_parse_alt(matcher, last_end, regex+1, &last_end);
if (regex == NULL || *regex != ')')
return NULL;
++regex;
break;
case '|':
case ')':
*end = last_end;
return regex;
case '?':
if (last_start < 0) return NULL;
stb__add_epsilon(matcher, last_start, last_end);
++regex;
break;
case '*':
if (last_start < 0) return NULL;
stb__add_epsilon(matcher, last_start, last_end);
// fall through
case '+':
if (last_start < 0) return NULL;
stb__add_epsilon(matcher, last_end, last_start);
// prevent links back to last_end from chaining to last_start
n = stb__add_node(matcher);
stb__add_epsilon(matcher, last_end, n);
last_end = n;
++regex;
break;
case '{': // not supported!
// @TODO: given {n,m}, clone last_start to last_end m times,
// and include epsilons from start to first m-n blocks
return NULL;
case '\\':
++regex;
if (!*regex) return NULL;
// fallthrough
default: // match exactly this character
n = stb__add_node(matcher);
stb__add_edge(matcher, last_end, n, *regex);
last_start = last_end;
last_end = n;
++regex;
break;
case '$':
n = stb__add_node(matcher);
stb__add_edge(matcher, last_end, n, '\n');
last_start = last_end;
last_end = n;
++regex;
break;
case '.':
n = stb__add_node(matcher);
stb__add_edge(matcher, last_end, n, -1);
last_start = last_end;
last_end = n;
++regex;
break;
case '[': {
stb_uint8 flags[256];
int invert = 0,z;
++regex;
if (matcher->num_charset == 0) {
matcher->charset = (stb_uint *) stb_malloc(matcher, sizeof(*matcher->charset) * 256);
memset(matcher->charset, 0, sizeof(*matcher->charset) * 256);
}
memset(flags,0,sizeof(flags));
// leading ^ is special
if (*regex == '^')
++regex, invert = 1;
// leading ] is special
if (*regex == ']') {
flags[(int) ']'] = 1;
++regex;
}
while (*regex != ']') {
stb_uint a;
if (!*regex) return NULL;
a = *regex++;
if (regex[0] == '-' && regex[1] != ']') {
stb_uint i,b = regex[1];
regex += 2;
if (b == 0) return NULL;
if (a > b) return NULL;
for (i=a; i <= b; ++i)
flags[i] = 1;
} else
flags[a] = 1;
}
++regex;
if (invert) {
int i;
for (i=0; i < 256; ++i)
flags[i] = 1-flags[i];
}
// now check if any existing charset matches
for (z=0; z < matcher->num_charset; ++z) {
int i, k[2] = { 0, 1 << z};
for (i=0; i < 256; ++i) {
unsigned int f = k[flags[i]];
if ((matcher->charset[i] & k[1]) != f)
break;
}
if (i == 256) break;
}
if (z == matcher->num_charset) {
int i;
++matcher->num_charset;
if (matcher->num_charset > 32) {
assert(0); /* NOTREACHED */
return NULL; // too many charsets, oops
}
for (i=0; i < 256; ++i)
if (flags[i])
matcher->charset[i] |= (1 << z);
}
n = stb__add_node(matcher);
stb__add_edge(matcher, last_end, n, -2 - z);
last_start = last_end;
last_end = n;
break;
}
}
}
*end = last_end;
return regex;
}
static char *stb__reg_parse_alt(stb_matcher *matcher, int start, char *regex, stb_uint16 *end)
{
stb_uint16 last_end = start;
stb_uint16 main_end;
int head, tail;
head = stb__add_node(matcher);
stb__add_epsilon(matcher, start, head);
regex = stb__reg_parse(matcher, head, regex, &last_end);
if (regex == NULL) return NULL;
if (*regex == 0 || *regex == ')') {
*end = last_end;
return regex;
}
main_end = last_end;
tail = stb__add_node(matcher);
stb__add_epsilon(matcher, last_end, tail);
// start alternatives from the same starting node; use epsilon
// transitions to combine their endings
while(*regex && *regex != ')') {
assert(*regex == '|');
head = stb__add_node(matcher);
stb__add_epsilon(matcher, start, head);
regex = stb__reg_parse(matcher, head, regex+1, &last_end);
if (regex == NULL)
return NULL;
stb__add_epsilon(matcher, last_end, tail);
}
*end = tail;
return regex;
}
static char *stb__wild_parse(stb_matcher *matcher, int start, char *str, stb_uint16 *end)
{
int n;
stb_uint16 last_end;
last_end = stb__add_node(matcher);
stb__add_epsilon(matcher, start, last_end);
while (*str) {
switch (*str) {
// fallthrough
default: // match exactly this character
n = stb__add_node(matcher);
if (toupper(*str) == tolower(*str)) {
stb__add_edge(matcher, last_end, n, *str);
} else {
stb__add_edge(matcher, last_end, n, tolower(*str));
stb__add_edge(matcher, last_end, n, toupper(*str));
}
last_end = n;
++str;
break;
case '?':
n = stb__add_node(matcher);
stb__add_edge(matcher, last_end, n, -1);
last_end = n;
++str;
break;
case '*':
n = stb__add_node(matcher);
stb__add_edge(matcher, last_end, n, -1);
stb__add_epsilon(matcher, last_end, n);
stb__add_epsilon(matcher, n, last_end);
last_end = n;
++str;
break;
}
}
// now require end of string to match
n = stb__add_node(matcher);
stb__add_edge(matcher, last_end, n, 0);
last_end = n;
*end = last_end;
return str;
}
static int stb__opt(stb_matcher *m, int n)
{
for(;;) {
stb_nfa_node *p = &m->nodes[n];
if (p->goal) return n;
if (stb_arr_len(p->out)) return n;
if (stb_arr_len(p->eps) != 1) return n;
n = p->eps[0];
}
}
static void stb__optimize(stb_matcher *m)
{
// if the target of any edge is a node with exactly
// one out-epsilon, shorten it
int i,j;
for (i=0; i < stb_arr_len(m->nodes); ++i) {
stb_nfa_node *p = &m->nodes[i];
for (j=0; j < stb_arr_len(p->out); ++j)
p->out[j].node = stb__opt(m,p->out[j].node);
for (j=0; j < stb_arr_len(p->eps); ++j)
p->eps[j] = stb__opt(m,p->eps[j] );
}
m->start_node = stb__opt(m,m->start_node);
}
void stb_matcher_free(stb_matcher *f)
{
stb_free(f);
}
static stb_matcher *stb__alloc_matcher(void)
{
stb_matcher *matcher = (stb_matcher *) stb_malloc(0,sizeof(*matcher));
matcher->start_node = 0;
stb_arr_malloc((void **) &matcher->nodes, matcher);
matcher->num_charset = 0;
matcher->match_start = 0;
matcher->does_lex = 0;
matcher->dfa_start = STB__DFA_UNDEF;
stb_arr_malloc((void **) &matcher->dfa, matcher);
stb_arr_malloc((void **) &matcher->dfa_mapping, matcher);
stb_arr_malloc((void **) &matcher->dfa_result, matcher);
stb__add_node(matcher);
return matcher;
}
static void stb__lex_reset(stb_matcher *matcher)
{
// flush cached dfa data
stb_arr_setlen(matcher->dfa, 0);
stb_arr_setlen(matcher->dfa_mapping, 0);
stb_arr_setlen(matcher->dfa_result, 0);
matcher->dfa_start = STB__DFA_UNDEF;
}
stb_matcher *stb_regex_matcher(char *regex)
{
char *z;
stb_uint16 end;
stb_matcher *matcher = stb__alloc_matcher();
if (*regex == '^') {
matcher->match_start = 1;
++regex;
}
z = stb__reg_parse_alt(matcher, matcher->start_node, regex, &end);
if (!z || *z) {
stb_free(matcher);
return NULL;
}
((matcher->nodes)[(int) end]).goal = STB__NFA_STOP_GOAL;
return matcher;
}
stb_matcher *stb_lex_matcher(void)
{
stb_matcher *matcher = stb__alloc_matcher();
matcher->match_start = 1;
matcher->does_lex = 1;
return matcher;
}
int stb_lex_item(stb_matcher *matcher, const char *regex, int result)
{
char *z;
stb_uint16 end;
z = stb__reg_parse_alt(matcher, matcher->start_node, (char*) regex, &end);
if (z == NULL)
return 0;
stb__lex_reset(matcher);
matcher->nodes[(int) end].goal = result;
return 1;
}
int stb_lex_item_wild(stb_matcher *matcher, const char *regex, int result)
{
char *z;
stb_uint16 end;
z = stb__wild_parse(matcher, matcher->start_node, (char*) regex, &end);
if (z == NULL)
return 0;
stb__lex_reset(matcher);
matcher->nodes[(int) end].goal = result;
return 1;
}
static void stb__clear(stb_matcher *m, stb_uint16 *list)
{
int i;
for (i=0; i < stb_arr_len(list); ++i)
m->nodes[(int) list[i]].active = 0;
}
static int stb__clear_goalcheck(stb_matcher *m, stb_uint16 *list)
{
int i, t=0;
for (i=0; i < stb_arr_len(list); ++i) {
t += m->nodes[(int) list[i]].goal;
m->nodes[(int) list[i]].active = 0;
}
return t;
}
static stb_uint16 * stb__add_if_inactive(stb_matcher *m, stb_uint16 *list, int n)
{
if (!m->nodes[n].active) {
stb_arr_push(list, n);
m->nodes[n].active = 1;
}
return list;
}
static stb_uint16 * stb__eps_closure(stb_matcher *m, stb_uint16 *list)
{
int i,n = stb_arr_len(list);
for(i=0; i < n; ++i) {
stb_uint16 *e = m->nodes[(int) list[i]].eps;
if (e) {
int j,k = stb_arr_len(e);
for (j=0; j < k; ++j)
list = stb__add_if_inactive(m, list, e[j]);
n = stb_arr_len(list);
}
}
return list;
}
int stb_matcher_match(stb_matcher *m, char *str)
{
int result = 0;
int i,j,y,z;
stb_uint16 *previous = NULL;
stb_uint16 *current = NULL;
stb_uint16 *temp;
stb_arr_setsize(previous, 4);
stb_arr_setsize(current, 4);
previous = stb__add_if_inactive(m, previous, m->start_node);
previous = stb__eps_closure(m,previous);
stb__clear(m, previous);
while (*str && stb_arr_len(previous)) {
y = stb_arr_len(previous);
for (i=0; i < y; ++i) {
stb_nfa_node *n = &m->nodes[(int) previous[i]];
z = stb_arr_len(n->out);
for (j=0; j < z; ++j) {
if (n->out[j].match >= 0) {
if (n->out[j].match == *str)
current = stb__add_if_inactive(m, current, n->out[j].node);
} else if (n->out[j].match == -1) {
if (*str != '\n')
current = stb__add_if_inactive(m, current, n->out[j].node);
} else if (n->out[j].match < -1) {
int z = -n->out[j].match - 2;
if (m->charset[(stb_uint8) *str] & (1 << z))
current = stb__add_if_inactive(m, current, n->out[j].node);
}
}
}
stb_arr_setlen(previous, 0);
temp = previous;
previous = current;
current = temp;
previous = stb__eps_closure(m,previous);
stb__clear(m, previous);
++str;
}
// transition to pick up a '$' at the end
y = stb_arr_len(previous);
for (i=0; i < y; ++i)
m->nodes[(int) previous[i]].active = 1;
for (i=0; i < y; ++i) {
stb_nfa_node *n = &m->nodes[(int) previous[i]];
z = stb_arr_len(n->out);
for (j=0; j < z; ++j) {
if (n->out[j].match == '\n')
current = stb__add_if_inactive(m, current, n->out[j].node);
}
}
previous = stb__eps_closure(m,previous);
stb__clear(m, previous);
y = stb_arr_len(previous);
for (i=0; i < y; ++i)
if (m->nodes[(int) previous[i]].goal)
result = 1;
stb_arr_free(previous);
stb_arr_free(current);
return result && *str == 0;
}
stb_int16 stb__get_dfa_node(stb_matcher *m, stb_uint16 *list)
{
stb_uint16 node;
stb_uint32 data[8], *state, *newstate;
int i,j,n;
state = (stb_uint32 *) stb_temp(data, m->num_words_per_dfa * 4);
memset(state, 0, m->num_words_per_dfa*4);
n = stb_arr_len(list);
for (i=0; i < n; ++i) {
int x = list[i];
state[x >> 5] |= 1 << (x & 31);
}
// @TODO use a hash table
n = stb_arr_len(m->dfa_mapping);
i=j=0;
for(; j < n; ++i, j += m->num_words_per_dfa) {
// @TODO special case for <= 32
if (!memcmp(state, m->dfa_mapping + j, m->num_words_per_dfa*4)) {
node = i;
goto done;
}
}
assert(stb_arr_len(m->dfa) == i);
node = i;
newstate = stb_arr_addn(m->dfa_mapping, m->num_words_per_dfa);
memcpy(newstate, state, m->num_words_per_dfa*4);
// set all transitions to 'unknown'
stb_arr_add(m->dfa);
memset(m->dfa[i].transition, -1, sizeof(m->dfa[i].transition));
if (m->does_lex) {
int result = -1;
n = stb_arr_len(list);
for (i=0; i < n; ++i) {
if (m->nodes[(int) list[i]].goal > result)
result = m->nodes[(int) list[i]].goal;
}
stb_arr_push(m->dfa_result, result);
}
done:
stb_tempfree(data, state);
return node;
}
static int stb__matcher_dfa(stb_matcher *m, char *str_c, int *len)
{
stb_uint8 *str = (stb_uint8 *) str_c;
stb_int16 node,prevnode;
stb_dfa *trans;
int match_length = 0;
stb_int16 match_result=0;
if (m->dfa_start == STB__DFA_UNDEF) {
stb_uint16 *list;
m->num_words_per_dfa = (stb_arr_len(m->nodes)+31) >> 5;
stb__optimize(m);
list = stb__add_if_inactive(m, NULL, m->start_node);
list = stb__eps_closure(m,list);
if (m->does_lex) {
m->dfa_start = stb__get_dfa_node(m,list);
stb__clear(m, list);
// DON'T allow start state to be a goal state!
// this allows people to specify regexes that can match 0
// characters without them actually matching (also we don't
// check _before_ advancing anyway
if (m->dfa_start <= STB__DFA_MGOAL)
m->dfa_start = -(m->dfa_start - STB__DFA_MGOAL);
} else {
if (stb__clear_goalcheck(m, list))
m->dfa_start = STB__DFA_GOAL;
else
m->dfa_start = stb__get_dfa_node(m,list);
}
stb_arr_free(list);
}
prevnode = STB__DFA_UNDEF;
node = m->dfa_start;
trans = m->dfa;
if (m->dfa_start == STB__DFA_GOAL)
return 1;
for(;;) {
assert(node >= STB__DFA_VALID);
// fast inner DFA loop; especially if STB__DFA_VALID is 0
do {
prevnode = node;
node = trans[node].transition[*str++];
} while (node >= STB__DFA_VALID);
assert(node >= STB__DFA_MGOAL - stb_arr_len(m->dfa));
assert(node < stb_arr_len(m->dfa));
// special case for lex: need _longest_ match, so notice goal
// state without stopping
if (node <= STB__DFA_MGOAL) {
match_length = (int) (str - (stb_uint8 *) str_c);
node = -(node - STB__DFA_MGOAL);
match_result = node;
continue;
}
// slow NFA->DFA conversion
// or we hit the goal or the end of the string, but those
// can only happen once per search...
if (node == STB__DFA_UNDEF) {
// build a list -- @TODO special case <= 32 states
// heck, use a more compact data structure for <= 16 and <= 8 ?!
// @TODO keep states/newstates around instead of reallocating them
stb_uint16 *states = NULL;
stb_uint16 *newstates = NULL;
int i,j,y,z;
stb_uint32 *flags = &m->dfa_mapping[prevnode * m->num_words_per_dfa];
assert(prevnode != STB__DFA_UNDEF);
stb_arr_setsize(states, 4);
stb_arr_setsize(newstates,4);
for (j=0; j < m->num_words_per_dfa; ++j) {
for (i=0; i < 32; ++i) {
if (*flags & (1 << i))
stb_arr_push(states, j*32+i);
}
++flags;
}
// states is now the states we were in in the previous node;
// so now we can compute what node it transitions to on str[-1]
y = stb_arr_len(states);
for (i=0; i < y; ++i) {
stb_nfa_node *n = &m->nodes[(int) states[i]];
z = stb_arr_len(n->out);
for (j=0; j < z; ++j) {
if (n->out[j].match >= 0) {
if (n->out[j].match == str[-1] || (str[-1] == 0 && n->out[j].match == '\n'))
newstates = stb__add_if_inactive(m, newstates, n->out[j].node);
} else if (n->out[j].match == -1) {
if (str[-1] != '\n' && str[-1])
newstates = stb__add_if_inactive(m, newstates, n->out[j].node);
} else if (n->out[j].match < -1) {
int z = -n->out[j].match - 2;
if (m->charset[str[-1]] & (1 << z))
newstates = stb__add_if_inactive(m, newstates, n->out[j].node);
}
}
}
// AND add in the start state!
if (!m->match_start || (str[-1] == '\n' && !m->does_lex))
newstates = stb__add_if_inactive(m, newstates, m->start_node);
// AND epsilon close it
newstates = stb__eps_closure(m, newstates);
// if it's a goal state, then that's all there is to it
if (stb__clear_goalcheck(m, newstates)) {
if (m->does_lex) {
match_length = (int) (str - (stb_uint8 *) str_c);
node = stb__get_dfa_node(m,newstates);
match_result = node;
node = -node + STB__DFA_MGOAL;
trans = m->dfa; // could have gotten realloc()ed
} else
node = STB__DFA_GOAL;
} else if (str[-1] == 0 || stb_arr_len(newstates) == 0) {
node = STB__DFA_END;
} else {
node = stb__get_dfa_node(m,newstates);
trans = m->dfa; // could have gotten realloc()ed
}
trans[prevnode].transition[str[-1]] = node;
if (node <= STB__DFA_MGOAL)
node = -(node - STB__DFA_MGOAL);
stb_arr_free(newstates);
stb_arr_free(states);
}
if (node == STB__DFA_GOAL) {
return 1;
}
if (node == STB__DFA_END) {
if (m->does_lex) {
if (match_result) {
if (len) *len = match_length;
return m->dfa_result[(int) match_result];
}
}
return 0;
}
assert(node != STB__DFA_UNDEF);
}
}
int stb_matcher_find(stb_matcher *m, char *str)
{
assert(m->does_lex == 0);
return stb__matcher_dfa(m, str, NULL);
}
int stb_lex(stb_matcher *m, char *str, int *len)
{
assert(m->does_lex);
return stb__matcher_dfa(m, str, len);
}
#ifdef STB_PERFECT_HASH
int stb_regex(char *regex, char *str)
{
static stb_perfect p;
static stb_matcher ** matchers;
static char ** regexps;
static char ** regexp_cache;
static unsigned short *mapping;
int z = stb_perfect_hash(&p, (int)(size_t) regex);
if (z >= 0) {
if (strcmp(regex, regexp_cache[(int) mapping[z]])) {
int i = mapping[z];
stb_matcher_free(matchers[i]);
free(regexp_cache[i]);
regexps[i] = regex;
regexp_cache[i] = stb_p_strdup(regex);
matchers[i] = stb_regex_matcher(regex);
}
} else {
int i,n;
if (regex == NULL) {
for (i=0; i < stb_arr_len(matchers); ++i) {
stb_matcher_free(matchers[i]);
free(regexp_cache[i]);
}
stb_arr_free(matchers);
stb_arr_free(regexps);
stb_arr_free(regexp_cache);
stb_perfect_destroy(&p);
free(mapping); mapping = NULL;
return -1;
}
stb_arr_push(regexps, regex);
stb_arr_push(regexp_cache, stb_p_strdup(regex));
stb_arr_push(matchers, stb_regex_matcher(regex));
stb_perfect_destroy(&p);
n = stb_perfect_create(&p, (unsigned int *) (char **) regexps, stb_arr_len(regexps));
mapping = (unsigned short *) realloc(mapping, n * sizeof(*mapping));
for (i=0; i < stb_arr_len(regexps); ++i)
mapping[stb_perfect_hash(&p, (int)(size_t) regexps[i])] = i;
z = stb_perfect_hash(&p, (int)(size_t) regex);
}
return stb_matcher_find(matchers[(int) mapping[z]], str);
}
#endif
#endif // STB_DEFINE
#if 0
//////////////////////////////////////////////////////////////////////////////
//
// C source-code introspection
//
// runtime structure
typedef struct
{
char *name;
char *type; // base type
char *comment; // content of comment field
int size; // size of base type
int offset; // field offset
int arrcount[8]; // array sizes; -1 = pointer indirection; 0 = end of list
} stb_info_field;
typedef struct
{
char *structname;
int size;
int num_fields;
stb_info_field *fields;
} stb_info_struct;
extern stb_info_struct stb_introspect_output[];
//
STB_EXTERN void stb_introspect_precompiled(stb_info_struct *compiled);
STB_EXTERN void stb__introspect(char *path, char *file);
#define stb_introspect_ship() stb__introspect(NULL, NULL, stb__introspect_output)
#ifdef STB_SHIP
#define stb_introspect() stb_introspect_ship()
#define stb_introspect_path(p) stb_introspect_ship()
#else
// bootstrapping: define stb_introspect() (or 'path') the first time
#define stb_introspect() stb__introspect(NULL, __FILE__, NULL)
#define stb_introspect_auto() stb__introspect(NULL, __FILE__, stb__introspect_output)
#define stb_introspect_path(p) stb__introspect(p, __FILE__, NULL)
#define stb_introspect_path(p) stb__introspect(p, __FILE__, NULL)
#endif
#ifdef STB_DEFINE
#ifndef STB_INTROSPECT_CPP
#ifdef __cplusplus
#define STB_INTROSPECT_CPP 1
#else
#define STB_INTROSPECT_CPP 0
#endif
#endif
void stb_introspect_precompiled(stb_info_struct *compiled)
{
}
static void stb__introspect_filename(char *buffer, char *path)
{
#if STB_INTROSPECT_CPP
stb_p_sprintf(buffer stb_p_size(9999), "%s/stb_introspect.cpp", path);
#else
stb_p_sprintf(buffer stb_p_size(9999), "%s/stb_introspect.c", path);
#endif
}
static void stb__introspect_compute(char *path, char *file)
{
int i;
char ** include_list = NULL;
char ** introspect_list = NULL;
FILE *f;
f = stb_p_fopen(file, "w");
if (!f) return;
fputs("// if you get compiler errors, change the following 0 to a 1:\n", f);
fputs("#define STB_INTROSPECT_INVALID 0\n\n", f);
fputs("// this will force the code to compile, and force the introspector\n", f);
fputs("// to run and then exit, allowing you to recompile\n\n\n", f);
fputs("#include \"stb.h\"\n\n",f );
fputs("#if STB_INTROSPECT_INVALID\n", f);
fputs(" stb_info_struct stb__introspect_output[] = { (void *) 1 }\n", f);
fputs("#else\n\n", f);
for (i=0; i < stb_arr_len(include_list); ++i)
fprintf(f, " #include \"%s\"\n", include_list[i]);
fputs(" stb_info_struct stb__introspect_output[] =\n{\n", f);
for (i=0; i < stb_arr_len(introspect_list); ++i)
fprintf(f, " stb_introspect_%s,\n", introspect_list[i]);
fputs(" };\n", f);
fputs("#endif\n", f);
fclose(f);
}
static stb_info_struct *stb__introspect_info;
#ifndef STB_SHIP
#endif
void stb__introspect(char *path, char *file, stb_info_struct *compiled)
{
static int first=1;
if (!first) return;
first=0;
stb__introspect_info = compiled;
#ifndef STB_SHIP
if (path || file) {
int bail_flag = compiled && compiled[0].structname == (void *) 1;
int needs_building = bail_flag;
struct stb__stat st;
char buffer[1024], buffer2[1024];
if (!path) {
stb_splitpath(buffer, file, STB_PATH);
path = buffer;
}
// bail if the source path doesn't exist
if (!stb_fexists(path)) return;
stb__introspect_filename(buffer2, path);
// get source/include files timestamps, compare to output-file timestamp;
// if mismatched, regenerate
if (stb__stat(buffer2, &st))
needs_building = STB_TRUE;
{
// find any file that contains an introspection command and is newer
// if needs_building is already true, we don't need to do this test,
// but we still need these arrays, so go ahead and get them
char **all[3];
all[0] = stb_readdir_files_mask(path, "*.h");
all[1] = stb_readdir_files_mask(path, "*.c");
all[2] = stb_readdir_files_mask(path, "*.cpp");
int i,j;
if (needs_building) {
for (j=0; j < 3; ++j) {
for (i=0; i < stb_arr_len(all[j]); ++i) {
struct stb__stat st2;
if (!stb__stat(all[j][i], &st2)) {
if (st.st_mtime < st2.st_mtime) {
char *z = stb_filec(all[j][i], NULL);
int found=STB_FALSE;
while (y) {
y = strstr(y, "//si");
if (y && isspace(y[4])) {
found = STB_TRUE;
break;
}
}
needs_building = STB_TRUE;
goto done;
}
}
}
}
done:;
}
char *z = stb_filec(all[i], NULL), *y = z;
int found=STB_FALSE;
while (y) {
y = strstr(y, "//si");
if (y && isspace(y[4])) {
found = STB_TRUE;
break;
}
}
if (found)
stb_arr_push(introspect_h, stb_p_strdup(all[i]));
free(z);
}
}
stb_readdir_free(all);
if (!needs_building) {
for (i=0; i < stb_arr_len(introspect_h); ++i) {
struct stb__stat st2;
if (!stb__stat(introspect_h[i], &st2))
if (st.st_mtime < st2.st_mtime)
needs_building = STB_TRUE;
}
}
if (needs_building) {
stb__introspect_compute(path, buffer2);
}
}
}
#endif
}
#endif
#endif
#ifdef STB_INTROSPECT
// compile-time code-generator
#define INTROSPECT(x) int main(int argc, char **argv) { stb__introspect(__FILE__); return 0; }
#define FILE(x)
void stb__introspect(char *filename)
{
char *file = stb_file(filename, NULL);
char *s = file, *t, **p;
char *out_name = "stb_introspect.c";
char *out_path;
STB_ARR(char) filelist = NULL;
int i,n;
if (!file) stb_fatal("Couldn't open %s", filename);
out_path = stb_splitpathdup(filename, STB_PATH);
// search for the macros
while (*s) {
char buffer[256];
while (*s && !isupper(*s)) ++s;
s = stb_strtok_invert(buffer, s, "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
s = stb_skipwhite(s);
if (*s == '(') {
++s;
t = strchr(s, ')');
if (t == NULL) stb_fatal("Error parsing %s", filename);
}
}
}
#endif
//////////////////////////////////////////////////////////////////////////////
//
// STB-C sliding-window dictionary compression
//
// This uses a DEFLATE-style sliding window, but no bitwise entropy.
// Everything is on byte boundaries, so you could then apply a byte-wise
// entropy code, though that's nowhere near as effective.
//
// An STB-C stream begins with a 16-byte header:
// 4 bytes: 0x57 0xBC 0x00 0x00
// 8 bytes: big-endian size of decompressed data, 64-bits
// 4 bytes: big-endian size of window (how far back decompressor may need)
//
// The following symbols appear in the stream (these were determined ad hoc,
// not by analysis):
//
// [dict] 00000100 yyyyyyyy yyyyyyyy yyyyyyyy xxxxxxxx xxxxxxxx
// [END] 00000101 11111010 cccccccc cccccccc cccccccc cccccccc
// [dict] 00000110 yyyyyyyy yyyyyyyy yyyyyyyy xxxxxxxx
// [literals] 00000111 zzzzzzzz zzzzzzzz
// [literals] 00001zzz zzzzzzzz
// [dict] 00010yyy yyyyyyyy yyyyyyyy xxxxxxxx xxxxxxxx
// [dict] 00011yyy yyyyyyyy yyyyyyyy xxxxxxxx
// [literals] 001zzzzz
// [dict] 01yyyyyy yyyyyyyy xxxxxxxx
// [dict] 1xxxxxxx yyyyyyyy
//
// xxxxxxxx: match length - 1
// yyyyyyyy: backwards distance - 1
// zzzzzzzz: num literals - 1
// cccccccc: adler32 checksum of decompressed data
// (all big-endian)
STB_EXTERN stb_uint stb_decompress_length(stb_uchar *input);
STB_EXTERN stb_uint stb_decompress(stb_uchar *out,stb_uchar *in,stb_uint len);
STB_EXTERN stb_uint stb_compress (stb_uchar *out,stb_uchar *in,stb_uint len);
STB_EXTERN void stb_compress_window(int z);
STB_EXTERN void stb_compress_hashsize(unsigned int z);
STB_EXTERN int stb_compress_tofile(char *filename, char *in, stb_uint len);
STB_EXTERN int stb_compress_intofile(FILE *f, char *input, stb_uint len);
STB_EXTERN char *stb_decompress_fromfile(char *filename, stb_uint *len);
STB_EXTERN int stb_compress_stream_start(FILE *f);
STB_EXTERN void stb_compress_stream_end(int close);
STB_EXTERN void stb_write(char *data, int data_len);
#ifdef STB_DEFINE
stb_uint stb_decompress_length(stb_uchar *input)
{
return (input[8] << 24) + (input[9] << 16) + (input[10] << 8) + input[11];
}
//////////////////// decompressor ///////////////////////
// simple implementation that just writes whole thing into big block
static unsigned char *stb__barrier;
static unsigned char *stb__barrier2;
static unsigned char *stb__barrier3;
static unsigned char *stb__barrier4;
static stb_uchar *stb__dout;
static void stb__match(stb_uchar *data, stb_uint length)
{
// INVERSE of memmove... write each byte before copying the next...
assert (stb__dout + length <= stb__barrier);
if (stb__dout + length > stb__barrier) { stb__dout += length; return; }
if (data < stb__barrier4) { stb__dout = stb__barrier+1; return; }
while (length--) *stb__dout++ = *data++;
}
static void stb__lit(stb_uchar *data, stb_uint length)
{
assert (stb__dout + length <= stb__barrier);
if (stb__dout + length > stb__barrier) { stb__dout += length; return; }
if (data < stb__barrier2) { stb__dout = stb__barrier+1; return; }
memcpy(stb__dout, data, length);
stb__dout += length;
}
#define stb__in2(x) ((i[x] << 8) + i[(x)+1])
#define stb__in3(x) ((i[x] << 16) + stb__in2((x)+1))
#define stb__in4(x) ((i[x] << 24) + stb__in3((x)+1))
static stb_uchar *stb_decompress_token(stb_uchar *i)
{
if (*i >= 0x20) { // use fewer if's for cases that expand small
if (*i >= 0x80) stb__match(stb__dout-i[1]-1, i[0] - 0x80 + 1), i += 2;
else if (*i >= 0x40) stb__match(stb__dout-(stb__in2(0) - 0x4000 + 1), i[2]+1), i += 3;
else /* *i >= 0x20 */ stb__lit(i+1, i[0] - 0x20 + 1), i += 1 + (i[0] - 0x20 + 1);
} else { // more ifs for cases that expand large, since overhead is amortized
if (*i >= 0x18) stb__match(stb__dout-(stb__in3(0) - 0x180000 + 1), i[3]+1), i += 4;
else if (*i >= 0x10) stb__match(stb__dout-(stb__in3(0) - 0x100000 + 1), stb__in2(3)+1), i += 5;
else if (*i >= 0x08) stb__lit(i+2, stb__in2(0) - 0x0800 + 1), i += 2 + (stb__in2(0) - 0x0800 + 1);
else if (*i == 0x07) stb__lit(i+3, stb__in2(1) + 1), i += 3 + (stb__in2(1) + 1);
else if (*i == 0x06) stb__match(stb__dout-(stb__in3(1)+1), i[4]+1), i += 5;
else if (*i == 0x04) stb__match(stb__dout-(stb__in3(1)+1), stb__in2(4)+1), i += 6;
}
return i;
}
stb_uint stb_decompress(stb_uchar *output, stb_uchar *i, stb_uint length)
{
stb_uint olen;
if (stb__in4(0) != 0x57bC0000) return 0;
if (stb__in4(4) != 0) return 0; // error! stream is > 4GB
olen = stb_decompress_length(i);
stb__barrier2 = i;
stb__barrier3 = i+length;
stb__barrier = output + olen;
stb__barrier4 = output;
i += 16;
stb__dout = output;
while (1) {
stb_uchar *old_i = i;
i = stb_decompress_token(i);
if (i == old_i) {
if (*i == 0x05 && i[1] == 0xfa) {
assert(stb__dout == output + olen);
if (stb__dout != output + olen) return 0;
if (stb_adler32(1, output, olen) != (stb_uint) stb__in4(2))
return 0;
return olen;
} else {
assert(0); /* NOTREACHED */
return 0;
}
}
assert(stb__dout <= output + olen);
if (stb__dout > output + olen)
return 0;
}
}
char *stb_decompress_fromfile(char *filename, unsigned int *len)
{
unsigned int n;
char *q;
unsigned char *p;
FILE *f = stb_p_fopen(filename, "rb"); if (f == NULL) return NULL;
fseek(f, 0, SEEK_END);
n = ftell(f);
fseek(f, 0, SEEK_SET);
p = (unsigned char * ) malloc(n); if (p == NULL) return NULL;
fread(p, 1, n, f);
fclose(f);
if (p == NULL) return NULL;
if (p[0] != 0x57 || p[1] != 0xBc || p[2] || p[3]) { free(p); return NULL; }
q = (char *) malloc(stb_decompress_length(p)+1);
if (!q) { free(p); return NULL; }
*len = stb_decompress((unsigned char *) q, p, n);
if (*len) q[*len] = 0;
free(p);
return q;
}
#if 0
// streaming decompressor
static struct
{
stb__uchar *in_buffer;
stb__uchar *match;
stb__uint pending_literals;
stb__uint pending_match;
} xx;
static void stb__match(stb_uchar *data, stb_uint length)
{
// INVERSE of memmove... write each byte before copying the next...
assert (stb__dout + length <= stb__barrier);
if (stb__dout + length > stb__barrier) { stb__dout += length; return; }
if (data < stb__barrier2) { stb__dout = stb__barrier+1; return; }
while (length--) *stb__dout++ = *data++;
}
static void stb__lit(stb_uchar *data, stb_uint length)
{
assert (stb__dout + length <= stb__barrier);
if (stb__dout + length > stb__barrier) { stb__dout += length; return; }
if (data < stb__barrier2) { stb__dout = stb__barrier+1; return; }
memcpy(stb__dout, data, length);
stb__dout += length;
}
static void sx_match(stb_uchar *data, stb_uint length)
{
xx.match = data;
xx.pending_match = length;
}
static void sx_lit(stb_uchar *data, stb_uint length)
{
xx.pending_lit = length;
}
static int stb_decompress_token_state(void)
{
stb__uchar *i = xx.in_buffer;
if (*i >= 0x20) { // use fewer if's for cases that expand small
if (*i >= 0x80) sx_match(stb__dout-i[1]-1, i[0] - 0x80 + 1), i += 2;
else if (*i >= 0x40) sx_match(stb__dout-(stb__in2(0) - 0x4000 + 1), i[2]+1), i += 3;
else /* *i >= 0x20 */ sx_lit(i+1, i[0] - 0x20 + 1), i += 1;
} else { // more ifs for cases that expand large, since overhead is amortized
if (*i >= 0x18) sx_match(stb__dout-(stb__in3(0) - 0x180000 + 1), i[3]+1), i += 4;
else if (*i >= 0x10) sx_match(stb__dout-(stb__in3(0) - 0x100000 + 1), stb__in2(3)+1), i += 5;
else if (*i >= 0x08) sx_lit(i+2, stb__in2(0) - 0x0800 + 1), i += 2;
else if (*i == 0x07) sx_lit(i+3, stb__in2(1) + 1), i += 3;
else if (*i == 0x06) sx_match(stb__dout-(stb__in3(1)+1), i[4]+1), i += 5;
else if (*i == 0x04) sx_match(stb__dout-(stb__in3(1)+1), stb__in2(4)+1), i += 6;
else return 0;
}
xx.in_buffer = i;
return 1;
}
#endif
//////////////////// compressor ///////////////////////
static unsigned int stb_matchlen(stb_uchar *m1, stb_uchar *m2, stb_uint maxlen)
{
stb_uint i;
for (i=0; i < maxlen; ++i)
if (m1[i] != m2[i]) return i;
return i;
}
// simple implementation that just takes the source data in a big block
static stb_uchar *stb__out;
static FILE *stb__outfile;
static stb_uint stb__outbytes;
static void stb__write(unsigned char v)
{
fputc(v, stb__outfile);
++stb__outbytes;
}
#define stb_out(v) (stb__out ? (void)(*stb__out++ = (stb_uchar) (v)) : stb__write((stb_uchar) (v)))
static void stb_out2(stb_uint v)
{
stb_out(v >> 8);
stb_out(v);
}
static void stb_out3(stb_uint v) { stb_out(v >> 16); stb_out(v >> 8); stb_out(v); }
static void stb_out4(stb_uint v) { stb_out(v >> 24); stb_out(v >> 16);
stb_out(v >> 8 ); stb_out(v); }
static void outliterals(stb_uchar *in, ptrdiff_t numlit)
{
while (numlit > 65536) {
outliterals(in,65536);
in += 65536;
numlit -= 65536;
}
if (numlit == 0) ;
else if (numlit <= 32) stb_out (0x000020 + (stb_uint) numlit-1);
else if (numlit <= 2048) stb_out2(0x000800 + (stb_uint) numlit-1);
else /* numlit <= 65536) */ stb_out3(0x070000 + (stb_uint) numlit-1);
if (stb__out) {
memcpy(stb__out,in,numlit);
stb__out += numlit;
} else
fwrite(in, 1, numlit, stb__outfile);
}
static int stb__window = 0x40000; // 256K
void stb_compress_window(int z)
{
if (z >= 0x1000000) z = 0x1000000; // limit of implementation
if (z < 0x100) z = 0x100; // insanely small
stb__window = z;
}
static int stb_not_crap(int best, int dist)
{
return ((best > 2 && dist <= 0x00100)
|| (best > 5 && dist <= 0x04000)
|| (best > 7 && dist <= 0x80000));
}
static stb_uint stb__hashsize = 32768;
void stb_compress_hashsize(unsigned int y)
{
unsigned int z = 1024;
while (z < y) z <<= 1;
stb__hashsize = z >> 2; // pass in bytes, store #pointers
}
// note that you can play with the hashing functions all you
// want without needing to change the decompressor
#define stb__hc(q,h,c) (((h) << 7) + ((h) >> 25) + q[c])
#define stb__hc2(q,h,c,d) (((h) << 14) + ((h) >> 18) + (q[c] << 7) + q[d])
#define stb__hc3(q,c,d,e) ((q[c] << 14) + (q[d] << 7) + q[e])
static stb_uint32 stb__running_adler;
static int stb_compress_chunk(stb_uchar *history,
stb_uchar *start,
stb_uchar *end,
int length,
int *pending_literals,
stb_uchar **chash,
stb_uint mask)
{
int window = stb__window;
stb_uint match_max;
stb_uchar *lit_start = start - *pending_literals;
stb_uchar *q = start;
#define STB__SCRAMBLE(h) (((h) + ((h) >> 16)) & mask)
// stop short of the end so we don't scan off the end doing
// the hashing; this means we won't compress the last few bytes
// unless they were part of something longer
while (q < start+length && q+12 < end) {
int m;
stb_uint h1,h2,h3,h4, h;
stb_uchar *t;
int best = 2, dist=0;
if (q+65536 > end)
match_max = (stb_uint) (end-q);
else
match_max = 65536u;
#define stb__nc(b,d) ((d) <= window && ((b) > 9 || stb_not_crap(b,d)))
#define STB__TRY(t,p) /* avoid retrying a match we already tried */ \
if (p ? dist != (int) (q-t) : 1) \
if ((m = (int) stb_matchlen(t, q, match_max)) > best)\
if (stb__nc(m,(int) (q-(t)))) \
best = m, dist = (int) (q - (t))
// rather than search for all matches, only try 4 candidate locations,
// chosen based on 4 different hash functions of different lengths.
// this strategy is inspired by LZO; hashing is unrolled here using the
// 'hc' macro
h = stb__hc3(q,0, 1, 2); h1 = STB__SCRAMBLE(h);
t = chash[h1]; if (t) STB__TRY(t,0);
h = stb__hc2(q,h, 3, 4); h2 = STB__SCRAMBLE(h);
h = stb__hc2(q,h, 5, 6); t = chash[h2]; if (t) STB__TRY(t,1);
h = stb__hc2(q,h, 7, 8); h3 = STB__SCRAMBLE(h);
h = stb__hc2(q,h, 9,10); t = chash[h3]; if (t) STB__TRY(t,1);
h = stb__hc2(q,h,11,12); h4 = STB__SCRAMBLE(h);
t = chash[h4]; if (t) STB__TRY(t,1);
// because we use a shared hash table, can only update it
// _after_ we've probed all of them
chash[h1] = chash[h2] = chash[h3] = chash[h4] = q;
if (best > 2)
assert(dist > 0);
// see if our best match qualifies
if (best < 3) { // fast path literals
++q;
} else if (best > 2 && best <= 0x80 && dist <= 0x100) {
outliterals(lit_start, q-lit_start); lit_start = (q += best);
stb_out(0x80 + best-1);
stb_out(dist-1);
} else if (best > 5 && best <= 0x100 && dist <= 0x4000) {
outliterals(lit_start, q-lit_start); lit_start = (q += best);
stb_out2(0x4000 + dist-1);
stb_out(best-1);
} else if (best > 7 && best <= 0x100 && dist <= 0x80000) {
outliterals(lit_start, q-lit_start); lit_start = (q += best);
stb_out3(0x180000 + dist-1);
stb_out(best-1);
} else if (best > 8 && best <= 0x10000 && dist <= 0x80000) {
outliterals(lit_start, q-lit_start); lit_start = (q += best);
stb_out3(0x100000 + dist-1);
stb_out2(best-1);
} else if (best > 9 && dist <= 0x1000000) {
if (best > 65536) best = 65536;
outliterals(lit_start, q-lit_start); lit_start = (q += best);
if (best <= 0x100) {
stb_out(0x06);
stb_out3(dist-1);
stb_out(best-1);
} else {
stb_out(0x04);
stb_out3(dist-1);
stb_out2(best-1);
}
} else { // fallback literals if no match was a balanced tradeoff
++q;
}
}
// if we didn't get all the way, add the rest to literals
if (q-start < length)
q = start+length;
// the literals are everything from lit_start to q
*pending_literals = (int) (q - lit_start);
stb__running_adler = stb_adler32(stb__running_adler, start, (int) (q - start));
return (int) (q - start);
}
static int stb_compress_inner(stb_uchar *input, stb_uint length)
{
int literals = 0;
stb_uint len,i;
stb_uchar **chash;
chash = (stb_uchar**) malloc(stb__hashsize * sizeof(stb_uchar*));
if (chash == NULL) return 0; // failure
for (i=0; i < stb__hashsize; ++i)
chash[i] = NULL;
// stream signature
stb_out(0x57); stb_out(0xbc);
stb_out2(0);
stb_out4(0); // 64-bit length requires 32-bit leading 0
stb_out4(length);
stb_out4(stb__window);
stb__running_adler = 1;
len = stb_compress_chunk(input, input, input+length, length, &literals, chash, stb__hashsize-1);
assert(len == length);
outliterals(input+length - literals, literals);
free(chash);
stb_out2(0x05fa); // end opcode
stb_out4(stb__running_adler);
return 1; // success
}
stb_uint stb_compress(stb_uchar *out, stb_uchar *input, stb_uint length)
{
stb__out = out;
stb__outfile = NULL;
stb_compress_inner(input, length);
return (stb_uint) (stb__out - out);
}
int stb_compress_tofile(char *filename, char *input, unsigned int length)
{
//int maxlen = length + 512 + (length >> 2); // total guess
//char *buffer = (char *) malloc(maxlen);
//int blen = stb_compress((stb_uchar*)buffer, (stb_uchar*)input, length);
stb__out = NULL;
stb__outfile = stb_p_fopen(filename, "wb");
if (!stb__outfile) return 0;
stb__outbytes = 0;
if (!stb_compress_inner((stb_uchar*)input, length))
return 0;
fclose(stb__outfile);
return stb__outbytes;
}
int stb_compress_intofile(FILE *f, char *input, unsigned int length)
{
//int maxlen = length + 512 + (length >> 2); // total guess
//char *buffer = (char*)malloc(maxlen);
//int blen = stb_compress((stb_uchar*)buffer, (stb_uchar*)input, length);
stb__out = NULL;
stb__outfile = f;
if (!stb__outfile) return 0;
stb__outbytes = 0;
if (!stb_compress_inner((stb_uchar*)input, length))
return 0;
return stb__outbytes;
}
////////////////////// streaming I/O version /////////////////////
static size_t stb_out_backpatch_id(void)
{
if (stb__out)
return (size_t) stb__out;
else
return ftell(stb__outfile);
}
static void stb_out_backpatch(size_t id, stb_uint value)
{
stb_uchar data[4] = { (stb_uchar)(value >> 24), (stb_uchar)(value >> 16), (stb_uchar)(value >> 8), (stb_uchar)(value) };
if (stb__out) {
memcpy((void *) id, data, 4);
} else {
stb_uint where = ftell(stb__outfile);
fseek(stb__outfile, (long) id, SEEK_SET);
fwrite(data, 4, 1, stb__outfile);
fseek(stb__outfile, where, SEEK_SET);
}
}
// ok, the wraparound buffer was a total failure. let's instead
// use a copying-in-place buffer, which lets us share the code.
// This is way less efficient but it'll do for now.
static struct
{
stb_uchar *buffer;
int size; // physical size of buffer in bytes
int valid; // amount of valid data in bytes
int start; // bytes of data already output
int window;
int fsize;
int pending_literals; // bytes not-quite output but counted in start
int length_id;
stb_uint total_bytes;
stb_uchar **chash;
stb_uint hashmask;
} xtb;
static int stb_compress_streaming_start(void)
{
stb_uint i;
xtb.size = stb__window * 3;
xtb.buffer = (stb_uchar*)malloc(xtb.size);
if (!xtb.buffer) return 0;
xtb.chash = (stb_uchar**)malloc(sizeof(*xtb.chash) * stb__hashsize);
if (!xtb.chash) {
free(xtb.buffer);
return 0;
}
for (i=0; i < stb__hashsize; ++i)
xtb.chash[i] = NULL;
xtb.hashmask = stb__hashsize-1;
xtb.valid = 0;
xtb.start = 0;
xtb.window = stb__window;
xtb.fsize = stb__window;
xtb.pending_literals = 0;
xtb.total_bytes = 0;
// stream signature
stb_out(0x57); stb_out(0xbc); stb_out2(0);
stb_out4(0); // 64-bit length requires 32-bit leading 0
xtb.length_id = (int) stb_out_backpatch_id();
stb_out4(0); // we don't know the output length yet
stb_out4(stb__window);
stb__running_adler = 1;
return 1;
}
static int stb_compress_streaming_end(void)
{
// flush out any remaining data
stb_compress_chunk(xtb.buffer, xtb.buffer+xtb.start, xtb.buffer+xtb.valid,
xtb.valid-xtb.start, &xtb.pending_literals, xtb.chash, xtb.hashmask);
// write out pending literals
outliterals(xtb.buffer + xtb.valid - xtb.pending_literals, xtb.pending_literals);
stb_out2(0x05fa); // end opcode
stb_out4(stb__running_adler);
stb_out_backpatch(xtb.length_id, xtb.total_bytes);
free(xtb.buffer);
free(xtb.chash);
return 1;
}
void stb_write(char *data, int data_len)
{
stb_uint i;
// @TODO: fast path for filling the buffer and doing nothing else
// if (xtb.valid + data_len < xtb.size)
xtb.total_bytes += data_len;
while (data_len) {
// fill buffer
if (xtb.valid < xtb.size) {
int amt = xtb.size - xtb.valid;
if (data_len < amt) amt = data_len;
memcpy(xtb.buffer + xtb.valid, data, amt);
data_len -= amt;
data += amt;
xtb.valid += amt;
}
if (xtb.valid < xtb.size)
return;
// at this point, the buffer is full
// if we can process some data, go for it; make sure
// we leave an 'fsize's worth of data, though
if (xtb.start + xtb.fsize < xtb.valid) {
int amount = (xtb.valid - xtb.fsize) - xtb.start;
int n;
assert(amount > 0);
n = stb_compress_chunk(xtb.buffer, xtb.buffer + xtb.start, xtb.buffer + xtb.valid,
amount, &xtb.pending_literals, xtb.chash, xtb.hashmask);
xtb.start += n;
}
assert(xtb.start + xtb.fsize >= xtb.valid);
// at this point, our future size is too small, so we
// need to flush some history. we, in fact, flush exactly
// one window's worth of history
{
int flush = xtb.window;
assert(xtb.start >= flush);
assert(xtb.valid >= flush);
// if 'pending literals' extends back into the shift region,
// write them out
if (xtb.start - xtb.pending_literals < flush) {
outliterals(xtb.buffer + xtb.start - xtb.pending_literals, xtb.pending_literals);
xtb.pending_literals = 0;
}
// now shift the window
memmove(xtb.buffer, xtb.buffer + flush, xtb.valid - flush);
xtb.start -= flush;
xtb.valid -= flush;
for (i=0; i <= xtb.hashmask; ++i)
if (xtb.chash[i] < xtb.buffer + flush)
xtb.chash[i] = NULL;
else
xtb.chash[i] -= flush;
}
// and now that we've made room for more data, go back to the top
}
}
int stb_compress_stream_start(FILE *f)
{
stb__out = NULL;
stb__outfile = f;
if (f == NULL)
return 0;
if (!stb_compress_streaming_start())
return 0;
return 1;
}
void stb_compress_stream_end(int close)
{
stb_compress_streaming_end();
if (close && stb__outfile) {
fclose(stb__outfile);
}
}
#endif // STB_DEFINE
//////////////////////////////////////////////////////////////////////////////
//
// File abstraction... tired of not having this... we can write
// compressors to be layers over these that auto-close their children.
typedef struct stbfile
{
int (*getbyte)(struct stbfile *); // -1 on EOF
unsigned int (*getdata)(struct stbfile *, void *block, unsigned int len);
int (*putbyte)(struct stbfile *, int byte);
unsigned int (*putdata)(struct stbfile *, void *block, unsigned int len);
unsigned int (*size)(struct stbfile *);
unsigned int (*tell)(struct stbfile *);
void (*backpatch)(struct stbfile *, unsigned int tell, void *block, unsigned int len);
void (*close)(struct stbfile *);
FILE *f; // file to fread/fwrite
unsigned char *buffer; // input/output buffer
unsigned char *indata, *inend; // input buffer
union {
int various;
void *ptr;
};
} stbfile;
STB_EXTERN unsigned int stb_getc(stbfile *f); // read
STB_EXTERN int stb_putc(stbfile *f, int ch); // write
STB_EXTERN unsigned int stb_getdata(stbfile *f, void *buffer, unsigned int len); // read
STB_EXTERN unsigned int stb_putdata(stbfile *f, void *buffer, unsigned int len); // write
STB_EXTERN unsigned int stb_tell(stbfile *f); // read
STB_EXTERN unsigned int stb_size(stbfile *f); // read/write
STB_EXTERN void stb_backpatch(stbfile *f, unsigned int tell, void *buffer, unsigned int len); // write
#ifdef STB_DEFINE
unsigned int stb_getc(stbfile *f) { return f->getbyte(f); }
int stb_putc(stbfile *f, int ch) { return f->putbyte(f, ch); }
unsigned int stb_getdata(stbfile *f, void *buffer, unsigned int len)
{
return f->getdata(f, buffer, len);
}
unsigned int stb_putdata(stbfile *f, void *buffer, unsigned int len)
{
return f->putdata(f, buffer, len);
}
void stb_close(stbfile *f)
{
f->close(f);
free(f);
}
unsigned int stb_tell(stbfile *f) { return f->tell(f); }
unsigned int stb_size(stbfile *f) { return f->size(f); }
void stb_backpatch(stbfile *f, unsigned int tell, void *buffer, unsigned int len)
{
f->backpatch(f,tell,buffer,len);
}
// FILE * implementation
static int stb__fgetbyte(stbfile *f) { return fgetc(f->f); }
static int stb__fputbyte(stbfile *f, int ch) { return fputc(ch, f->f)==0; }
static unsigned int stb__fgetdata(stbfile *f, void *buffer, unsigned int len) { return (unsigned int) fread(buffer,1,len,f->f); }
static unsigned int stb__fputdata(stbfile *f, void *buffer, unsigned int len) { return (unsigned int) fwrite(buffer,1,len,f->f); }
static unsigned int stb__fsize(stbfile *f) { return (unsigned int) stb_filelen(f->f); }
static unsigned int stb__ftell(stbfile *f) { return (unsigned int) ftell(f->f); }
static void stb__fbackpatch(stbfile *f, unsigned int where, void *buffer, unsigned int len)
{
fseek(f->f, where, SEEK_SET);
fwrite(buffer, 1, len, f->f);
fseek(f->f, 0, SEEK_END);
}
static void stb__fclose(stbfile *f) { fclose(f->f); }
stbfile *stb_openf(FILE *f)
{
stbfile m = { stb__fgetbyte, stb__fgetdata,
stb__fputbyte, stb__fputdata,
stb__fsize, stb__ftell, stb__fbackpatch, stb__fclose,
0,0,0, };
stbfile *z = (stbfile *) malloc(sizeof(*z));
if (z) {
*z = m;
z->f = f;
}
return z;
}
static int stb__nogetbyte(stbfile *f) { assert(0); return -1; }
static unsigned int stb__nogetdata(stbfile *f, void *buffer, unsigned int len) { assert(0); return 0; }
static int stb__noputbyte(stbfile *f, int ch) { assert(0); return 0; }
static unsigned int stb__noputdata(stbfile *f, void *buffer, unsigned int len) { assert(0); return 0; }
static void stb__nobackpatch(stbfile *f, unsigned int where, void *buffer, unsigned int len) { assert(0); }
static int stb__bgetbyte(stbfile *s)
{
if (s->indata < s->inend)
return *s->indata++;
else
return -1;
}
static unsigned int stb__bgetdata(stbfile *s, void *buffer, unsigned int len)
{
if (s->indata + len > s->inend)
len = (unsigned int) (s->inend - s->indata);
memcpy(buffer, s->indata, len);
s->indata += len;
return len;
}
static unsigned int stb__bsize(stbfile *s) { return (unsigned int) (s->inend - s->buffer); }
static unsigned int stb__btell(stbfile *s) { return (unsigned int) (s->indata - s->buffer); }
static void stb__bclose(stbfile *s)
{
if (s->various)
free(s->buffer);
}
stbfile *stb_open_inbuffer(void *buffer, unsigned int len)
{
stbfile m = { stb__bgetbyte, stb__bgetdata,
stb__noputbyte, stb__noputdata,
stb__bsize, stb__btell, stb__nobackpatch, stb__bclose };
stbfile *z = (stbfile *) malloc(sizeof(*z));
if (z) {
*z = m;
z->buffer = (unsigned char *) buffer;
z->indata = z->buffer;
z->inend = z->indata + len;
}
return z;
}
stbfile *stb_open_inbuffer_free(void *buffer, unsigned int len)
{
stbfile *z = stb_open_inbuffer(buffer, len);
if (z)
z->various = 1; // free
return z;
}
#ifndef STB_VERSION
// if we've been cut-and-pasted elsewhere, you get a limited
// version of stb_open, without the 'k' flag and utf8 support
static void stb__fclose2(stbfile *f)
{
fclose(f->f);
}
stbfile *stb_open(char *filename, char *mode)
{
FILE *f = stb_p_fopen(filename, mode);
stbfile *s;
if (f == NULL) return NULL;
s = stb_openf(f);
if (s)
s->close = stb__fclose2;
return s;
}
#else
// the full version depends on some code in stb.h; this
// also includes the memory buffer output format implemented with stb_arr
static void stb__fclose2(stbfile *f)
{
stb_fclose(f->f, f->various);
}
stbfile *stb_open(char *filename, char *mode)
{
FILE *f = stb_fopen(filename, mode[0] == 'k' ? mode+1 : mode);
stbfile *s;
if (f == NULL) return NULL;
s = stb_openf(f);
if (s) {
s->close = stb__fclose2;
s->various = mode[0] == 'k' ? stb_keep_if_different : stb_keep_yes;
}
return s;
}
static int stb__aputbyte(stbfile *f, int ch)
{
stb_arr_push(f->buffer, ch);
return 1;
}
static unsigned int stb__aputdata(stbfile *f, void *data, unsigned int len)
{
memcpy(stb_arr_addn(f->buffer, (int) len), data, len);
return len;
}
static unsigned int stb__asize(stbfile *f) { return stb_arr_len(f->buffer); }
static void stb__abackpatch(stbfile *f, unsigned int where, void *data, unsigned int len)
{
memcpy(f->buffer+where, data, len);
}
static void stb__aclose(stbfile *f)
{
*(unsigned char **) f->ptr = f->buffer;
}
stbfile *stb_open_outbuffer(unsigned char **update_on_close)
{
stbfile m = { stb__nogetbyte, stb__nogetdata,
stb__aputbyte, stb__aputdata,
stb__asize, stb__asize, stb__abackpatch, stb__aclose };
stbfile *z = (stbfile *) malloc(sizeof(*z));
if (z) {
z->ptr = update_on_close;
*z = m;
}
return z;
}
#endif
#endif
//////////////////////////////////////////////////////////////////////////////
//
// Arithmetic coder... based on cbloom's notes on the subject, should be
// less code than a huffman code.
typedef struct
{
unsigned int range_low;
unsigned int range_high;
unsigned int code, range; // decode
int buffered_u8;
int pending_ffs;
stbfile *output;
} stb_arith;
STB_EXTERN void stb_arith_init_encode(stb_arith *a, stbfile *out);
STB_EXTERN void stb_arith_init_decode(stb_arith *a, stbfile *in);
STB_EXTERN stbfile *stb_arith_encode_close(stb_arith *a);
STB_EXTERN stbfile *stb_arith_decode_close(stb_arith *a);
STB_EXTERN void stb_arith_encode(stb_arith *a, unsigned int totalfreq, unsigned int freq, unsigned int cumfreq);
STB_EXTERN void stb_arith_encode_log2(stb_arith *a, unsigned int totalfreq2, unsigned int freq, unsigned int cumfreq);
STB_EXTERN unsigned int stb_arith_decode_value(stb_arith *a, unsigned int totalfreq);
STB_EXTERN void stb_arith_decode_advance(stb_arith *a, unsigned int totalfreq, unsigned int freq, unsigned int cumfreq);
STB_EXTERN unsigned int stb_arith_decode_value_log2(stb_arith *a, unsigned int totalfreq2);
STB_EXTERN void stb_arith_decode_advance_log2(stb_arith *a, unsigned int totalfreq2, unsigned int freq, unsigned int cumfreq);
STB_EXTERN void stb_arith_encode_byte(stb_arith *a, int byte);
STB_EXTERN int stb_arith_decode_byte(stb_arith *a);
// this is a memory-inefficient way of doing things, but it's
// fast(?) and simple
typedef struct
{
unsigned short cumfreq;
unsigned short samples;
} stb_arith_symstate_item;
typedef struct
{
int num_sym;
unsigned int pow2;
int countdown;
stb_arith_symstate_item data[1];
} stb_arith_symstate;
#ifdef STB_DEFINE
void stb_arith_init_encode(stb_arith *a, stbfile *out)
{
a->range_low = 0;
a->range_high = 0xffffffff;
a->pending_ffs = -1; // means no buffered character currently, to speed up normal case
a->output = out;
}
static void stb__arith_carry(stb_arith *a)
{
int i;
assert(a->pending_ffs != -1); // can't carry with no data
stb_putc(a->output, a->buffered_u8);
for (i=0; i < a->pending_ffs; ++i)
stb_putc(a->output, 0);
}
static void stb__arith_putbyte(stb_arith *a, int byte)
{
if (a->pending_ffs) {
if (a->pending_ffs == -1) { // means no buffered data; encoded for fast path efficiency
if (byte == 0xff)
stb_putc(a->output, byte); // just write it immediately
else {
a->buffered_u8 = byte;
a->pending_ffs = 0;
}
} else if (byte == 0xff) {
++a->pending_ffs;
} else {
int i;
stb_putc(a->output, a->buffered_u8);
for (i=0; i < a->pending_ffs; ++i)
stb_putc(a->output, 0xff);
}
} else if (byte == 0xff) {
++a->pending_ffs;
} else {
// fast path
stb_putc(a->output, a->buffered_u8);
a->buffered_u8 = byte;
}
}
static void stb__arith_flush(stb_arith *a)
{
if (a->pending_ffs >= 0) {
int i;
stb_putc(a->output, a->buffered_u8);
for (i=0; i < a->pending_ffs; ++i)
stb_putc(a->output, 0xff);
}
}
static void stb__renorm_encoder(stb_arith *a)
{
stb__arith_putbyte(a, a->range_low >> 24);
a->range_low <<= 8;
a->range_high = (a->range_high << 8) | 0xff;
}
static void stb__renorm_decoder(stb_arith *a)
{
int c = stb_getc(a->output);
a->code = (a->code << 8) + (c >= 0 ? c : 0); // if EOF, insert 0
}
void stb_arith_encode(stb_arith *a, unsigned int totalfreq, unsigned int freq, unsigned int cumfreq)
{
unsigned int range = a->range_high - a->range_low;
unsigned int old = a->range_low;
range /= totalfreq;
a->range_low += range * cumfreq;
a->range_high = a->range_low + range*freq;
if (a->range_low < old)
stb__arith_carry(a);
while (a->range_high - a->range_low < 0x1000000)
stb__renorm_encoder(a);
}
void stb_arith_encode_log2(stb_arith *a, unsigned int totalfreq2, unsigned int freq, unsigned int cumfreq)
{
unsigned int range = a->range_high - a->range_low;
unsigned int old = a->range_low;
range >>= totalfreq2;
a->range_low += range * cumfreq;
a->range_high = a->range_low + range*freq;
if (a->range_low < old)
stb__arith_carry(a);
while (a->range_high - a->range_low < 0x1000000)
stb__renorm_encoder(a);
}
unsigned int stb_arith_decode_value(stb_arith *a, unsigned int totalfreq)
{
unsigned int freqsize = a->range / totalfreq;
unsigned int z = a->code / freqsize;
return z >= totalfreq ? totalfreq-1 : z;
}
void stb_arith_decode_advance(stb_arith *a, unsigned int totalfreq, unsigned int freq, unsigned int cumfreq)
{
unsigned int freqsize = a->range / totalfreq; // @OPTIMIZE, share with above divide somehow?
a->code -= freqsize * cumfreq;
a->range = freqsize * freq;
while (a->range < 0x1000000)
stb__renorm_decoder(a);
}
unsigned int stb_arith_decode_value_log2(stb_arith *a, unsigned int totalfreq2)
{
unsigned int freqsize = a->range >> totalfreq2;
unsigned int z = a->code / freqsize;
return z >= (1U<<totalfreq2) ? (1U<<totalfreq2)-1 : z;
}
void stb_arith_decode_advance_log2(stb_arith *a, unsigned int totalfreq2, unsigned int freq, unsigned int cumfreq)
{
unsigned int freqsize = a->range >> totalfreq2;
a->code -= freqsize * cumfreq;
a->range = freqsize * freq;
while (a->range < 0x1000000)
stb__renorm_decoder(a);
}
stbfile *stb_arith_encode_close(stb_arith *a)
{
// put exactly as many bytes as we'll read, so we can turn on/off arithmetic coding in a stream
stb__arith_putbyte(a, a->range_low >> 24);
stb__arith_putbyte(a, a->range_low >> 16);
stb__arith_putbyte(a, a->range_low >> 8);
stb__arith_putbyte(a, a->range_low >> 0);
stb__arith_flush(a);
return a->output;
}
stbfile *stb_arith_decode_close(stb_arith *a)
{
return a->output;
}
#endif
//////////////////////////////////////////////////////////////////////////////
//
// Threads
//
#ifndef _WIN32
#ifdef STB_THREADS
#error "threads not implemented except for Windows"
#endif
#endif
// call this function to free any global variables for memory testing
STB_EXTERN void stb_thread_cleanup(void);
typedef void * (*stb_thread_func)(void *);
// do not rely on these types, this is an implementation detail.
// compare against STB_THREAD_NULL and ST_SEMAPHORE_NULL
typedef void *stb_thread;
typedef void *stb_semaphore;
typedef void *stb_mutex;
typedef struct stb__sync *stb_sync;
#define STB_SEMAPHORE_NULL NULL
#define STB_THREAD_NULL NULL
#define STB_MUTEX_NULL NULL
#define STB_SYNC_NULL NULL
// get the number of processors (limited to those in the affinity mask for this process).
STB_EXTERN int stb_processor_count(void);
// force to run on a single core -- needed for RDTSC to work, e.g. for iprof
STB_EXTERN void stb_force_uniprocessor(void);
// stb_work functions: queue up work to be done by some worker threads
// set number of threads to serve the queue; you can change this on the fly,
// but if you decrease it, it won't decrease until things currently on the
// queue are finished
STB_EXTERN void stb_work_numthreads(int n);
// set maximum number of units in the queue; you can only set this BEFORE running any work functions
STB_EXTERN int stb_work_maxunits(int n);
// enqueue some work to be done (can do this from any thread, or even from a piece of work);
// return value of f is stored in *return_code if non-NULL
STB_EXTERN int stb_work(stb_thread_func f, void *d, volatile void **return_code);
// as above, but stb_sync_reach is called on 'rel' after work is complete
STB_EXTERN int stb_work_reach(stb_thread_func f, void *d, volatile void **return_code, stb_sync rel);
// necessary to call this when using volatile to order writes/reads
STB_EXTERN void stb_barrier(void);
// support for independent queues with their own threads
typedef struct stb__workqueue stb_workqueue;
STB_EXTERN stb_workqueue*stb_workq_new(int numthreads, int max_units);
STB_EXTERN stb_workqueue*stb_workq_new_flags(int numthreads, int max_units, int no_add_mutex, int no_remove_mutex);
STB_EXTERN void stb_workq_delete(stb_workqueue *q);
STB_EXTERN void stb_workq_numthreads(stb_workqueue *q, int n);
STB_EXTERN int stb_workq(stb_workqueue *q, stb_thread_func f, void *d, volatile void **return_code);
STB_EXTERN int stb_workq_reach(stb_workqueue *q, stb_thread_func f, void *d, volatile void **return_code, stb_sync rel);
STB_EXTERN int stb_workq_length(stb_workqueue *q);
STB_EXTERN stb_thread stb_create_thread (stb_thread_func f, void *d);
STB_EXTERN stb_thread stb_create_thread2(stb_thread_func f, void *d, volatile void **return_code, stb_semaphore rel);
STB_EXTERN void stb_destroy_thread(stb_thread t);
STB_EXTERN stb_semaphore stb_sem_new(int max_val);
STB_EXTERN stb_semaphore stb_sem_new_extra(int max_val, int start_val);
STB_EXTERN void stb_sem_delete (stb_semaphore s);
STB_EXTERN void stb_sem_waitfor(stb_semaphore s);
STB_EXTERN void stb_sem_release(stb_semaphore s);
STB_EXTERN stb_mutex stb_mutex_new(void);
STB_EXTERN void stb_mutex_delete(stb_mutex m);
STB_EXTERN void stb_mutex_begin(stb_mutex m);
STB_EXTERN void stb_mutex_end(stb_mutex m);
STB_EXTERN stb_sync stb_sync_new(void);
STB_EXTERN void stb_sync_delete(stb_sync s);
STB_EXTERN int stb_sync_set_target(stb_sync s, int count);
STB_EXTERN void stb_sync_reach_and_wait(stb_sync s); // wait for 'target' reachers
STB_EXTERN int stb_sync_reach(stb_sync s);
typedef struct stb__threadqueue stb_threadqueue;
#define STB_THREADQ_DYNAMIC 0
STB_EXTERN stb_threadqueue *stb_threadq_new(int item_size, int num_items, int many_add, int many_remove);
STB_EXTERN void stb_threadq_delete(stb_threadqueue *tq);
STB_EXTERN int stb_threadq_get(stb_threadqueue *tq, void *output);
STB_EXTERN void stb_threadq_get_block(stb_threadqueue *tq, void *output);
STB_EXTERN int stb_threadq_add(stb_threadqueue *tq, void *input);
// can return FALSE if STB_THREADQ_DYNAMIC and attempt to grow fails
STB_EXTERN int stb_threadq_add_block(stb_threadqueue *tq, void *input);
#ifdef STB_THREADS
#ifdef STB_DEFINE
typedef struct
{
stb_thread_func f;
void *d;
volatile void **return_val;
stb_semaphore sem;
} stb__thread;
// this is initialized along all possible paths to create threads, therefore
// it's always initialized before any other threads are create, therefore
// it's free of races AS LONG AS you only create threads through stb_*
static stb_mutex stb__threadmutex, stb__workmutex;
static void stb__threadmutex_init(void)
{
if (stb__threadmutex == STB_SEMAPHORE_NULL) {
stb__threadmutex = stb_mutex_new();
stb__workmutex = stb_mutex_new();
}
}
#ifdef STB_THREAD_TEST
volatile float stb__t1=1, stb__t2;
static void stb__wait(int n)
{
float z = 0;
int i;
for (i=0; i < n; ++i)
z += 1 / (stb__t1+i);
stb__t2 = z;
}
#else
#define stb__wait(x)
#endif
#ifdef _WIN32
// avoid including windows.h -- note that our definitions aren't
// exactly the same (we don't define the security descriptor struct)
// so if you want to include windows.h, make sure you do it first.
#include <process.h>
#ifndef _WINDOWS_ // check windows.h guard
#define STB__IMPORT STB_EXTERN __declspec(dllimport)
#define STB__DW unsigned long
STB__IMPORT int __stdcall TerminateThread(void *, STB__DW);
STB__IMPORT void * __stdcall CreateSemaphoreA(void *sec, long,long,char*);
STB__IMPORT int __stdcall CloseHandle(void *);
STB__IMPORT STB__DW __stdcall WaitForSingleObject(void *, STB__DW);
STB__IMPORT int __stdcall ReleaseSemaphore(void *, long, long *);
STB__IMPORT void __stdcall Sleep(STB__DW);
#endif
// necessary to call this when using volatile to order writes/reads
void stb_barrier(void)
{
#ifdef MemoryBarrier
MemoryBarrier();
#else
long temp;
__asm xchg temp,eax;
#endif
}
static void stb__thread_run(void *t)
{
void *res;
stb__thread info = * (stb__thread *) t;
free(t);
res = info.f(info.d);
if (info.return_val)
*info.return_val = res;
if (info.sem != STB_SEMAPHORE_NULL)
stb_sem_release(info.sem);
}
static stb_thread stb_create_thread_raw(stb_thread_func f, void *d, volatile void **return_code, stb_semaphore rel)
{
#ifdef _MT
#if defined(STB_FASTMALLOC) && !defined(STB_FASTMALLOC_ITS_OKAY_I_ONLY_MALLOC_IN_ONE_THREAD)
stb_fatal("Error! Cannot use STB_FASTMALLOC with threads.\n");
return STB_THREAD_NULL;
#else
unsigned long id;
stb__thread *data = (stb__thread *) malloc(sizeof(*data));
if (!data) return NULL;
stb__threadmutex_init();
data->f = f;
data->d = d;
data->return_val = return_code;
data->sem = rel;
id = _beginthread(stb__thread_run, 0, data);
if (id == -1) return NULL;
return (void *) id;
#endif
#else
#ifdef STB_NO_STB_STRINGS
stb_fatal("Invalid compilation");
#else
stb_fatal("Must compile mult-threaded to use stb_thread/stb_work.");
#endif
return NULL;
#endif
}
// trivial win32 wrappers
void stb_destroy_thread(stb_thread t) { TerminateThread(t,0); }
stb_semaphore stb_sem_new(int maxv) {return CreateSemaphoreA(NULL,0,maxv,NULL); }
stb_semaphore stb_sem_new_extra(int maxv,int start){return CreateSemaphoreA(NULL,start,maxv,NULL); }
void stb_sem_delete(stb_semaphore s) { if (s != NULL) CloseHandle(s); }
void stb_sem_waitfor(stb_semaphore s) { WaitForSingleObject(s, 0xffffffff); } // INFINITE
void stb_sem_release(stb_semaphore s) { ReleaseSemaphore(s,1,NULL); }
static void stb__thread_sleep(int ms) { Sleep(ms); }
#ifndef _WINDOWS_
STB__IMPORT int __stdcall GetProcessAffinityMask(void *, STB__DW *, STB__DW *);
STB__IMPORT void * __stdcall GetCurrentProcess(void);
STB__IMPORT int __stdcall SetProcessAffinityMask(void *, STB__DW);
#endif
int stb_processor_count(void)
{
unsigned long proc,sys;
GetProcessAffinityMask(GetCurrentProcess(), &proc, &sys);
return stb_bitcount(proc);
}
void stb_force_uniprocessor(void)
{
unsigned long proc,sys;
GetProcessAffinityMask(GetCurrentProcess(), &proc, &sys);
if (stb_bitcount(proc) > 1) {
int z;
for (z=0; z < 32; ++z)
if (proc & (1 << z))
break;
if (z < 32) {
proc = 1 << z;
SetProcessAffinityMask(GetCurrentProcess(), proc);
}
}
}
#ifdef _WINDOWS_
#define STB_MUTEX_NATIVE
void *stb_mutex_new(void)
{
CRITICAL_SECTION *p = (CRITICAL_SECTION *) malloc(sizeof(*p));
if (p)
#if _WIN32_WINNT >= 0x0500
InitializeCriticalSectionAndSpinCount(p, 500);
#else
InitializeCriticalSection(p);
#endif
return p;
}
void stb_mutex_delete(void *p)
{
if (p) {
DeleteCriticalSection((CRITICAL_SECTION *) p);
free(p);
}
}
void stb_mutex_begin(void *p)
{
stb__wait(500);
if (p)
EnterCriticalSection((CRITICAL_SECTION *) p);
}
void stb_mutex_end(void *p)
{
if (p)
LeaveCriticalSection((CRITICAL_SECTION *) p);
stb__wait(500);
}
#endif // _WINDOWS_
#if 0
// for future reference,
// InterlockedCompareExchange for x86:
int cas64_mp(void * dest, void * xcmp, void * xxchg) {
__asm
{
mov esi, [xxchg] ; exchange
mov ebx, [esi + 0]
mov ecx, [esi + 4]
mov esi, [xcmp] ; comparand
mov eax, [esi + 0]
mov edx, [esi + 4]
mov edi, [dest] ; destination
lock cmpxchg8b [edi]
jz yyyy;
mov [esi + 0], eax;
mov [esi + 4], edx;
yyyy:
xor eax, eax;
setz al;
};
inline unsigned __int64 _InterlockedCompareExchange64(volatile unsigned __int64 *dest
,unsigned __int64 exchange
,unsigned __int64 comperand)
{
//value returned in eax::edx
__asm {
lea esi,comperand;
lea edi,exchange;
mov eax,[esi];
mov edx,4[esi];
mov ebx,[edi];
mov ecx,4[edi];
mov esi,dest;
lock CMPXCHG8B [esi];
}
#endif // #if 0
#endif // _WIN32
stb_thread stb_create_thread2(stb_thread_func f, void *d, volatile void **return_code, stb_semaphore rel)
{
return stb_create_thread_raw(f,d,return_code,rel);
}
stb_thread stb_create_thread(stb_thread_func f, void *d)
{
return stb_create_thread2(f,d,NULL,STB_SEMAPHORE_NULL);
}
// mutex implemented by wrapping semaphore
#ifndef STB_MUTEX_NATIVE
stb_mutex stb_mutex_new(void) { return stb_sem_new_extra(1,1); }
void stb_mutex_delete(stb_mutex m) { stb_sem_delete (m); }
void stb_mutex_begin(stb_mutex m) { stb__wait(500); if (m) stb_sem_waitfor(m); }
void stb_mutex_end(stb_mutex m) { if (m) stb_sem_release(m); stb__wait(500); }
#endif
// thread merge operation
struct stb__sync
{
int target; // target number of threads to hit it
int sofar; // total threads that hit it
int waiting; // total threads waiting
stb_mutex start; // mutex to prevent starting again before finishing previous
stb_mutex mutex; // mutex while tweaking state
stb_semaphore release; // semaphore wake up waiting threads
// we have to wake them up one at a time, rather than using a single release
// call, because win32 semaphores don't let you dynamically change the max count!
};
stb_sync stb_sync_new(void)
{
stb_sync s = (stb_sync) malloc(sizeof(*s));
if (!s) return s;
s->target = s->sofar = s->waiting = 0;
s->mutex = stb_mutex_new();
s->start = stb_mutex_new();
s->release = stb_sem_new(1);
if (s->mutex == STB_MUTEX_NULL || s->release == STB_SEMAPHORE_NULL || s->start == STB_MUTEX_NULL) {
stb_mutex_delete(s->mutex);
stb_mutex_delete(s->mutex);
stb_sem_delete(s->release);
free(s);
return NULL;
}
return s;
}
void stb_sync_delete(stb_sync s)
{
if (s->waiting) {
// it's bad to delete while there are threads waiting!
// shall we wait for them to reach, or just bail? just bail
assert(0);
}
stb_mutex_delete(s->mutex);
stb_mutex_delete(s->release);
free(s);
}
int stb_sync_set_target(stb_sync s, int count)
{
// don't allow setting a target until the last one is fully released;
// note that this can lead to inefficient pipelining, and maybe we'd
// be better off ping-ponging between two internal syncs?
// I tried seeing how often this happened using TryEnterCriticalSection
// and could _never_ get it to happen in imv(stb), even with more threads
// than processors. So who knows!
stb_mutex_begin(s->start);
// this mutex is pointless, since it's not valid for threads
// to call reach() before anyone calls set_target() anyway
stb_mutex_begin(s->mutex);
assert(s->target == 0); // enforced by start mutex
s->target = count;
s->sofar = 0;
s->waiting = 0;
stb_mutex_end(s->mutex);
return STB_TRUE;
}
void stb__sync_release(stb_sync s)
{
if (s->waiting)
stb_sem_release(s->release);
else {
s->target = 0;
stb_mutex_end(s->start);
}
}
int stb_sync_reach(stb_sync s)
{
int n;
stb_mutex_begin(s->mutex);
assert(s->sofar < s->target);
n = ++s->sofar; // record this value to avoid possible race if we did 'return s->sofar';
if (s->sofar == s->target)
stb__sync_release(s);
stb_mutex_end(s->mutex);
return n;
}
void stb_sync_reach_and_wait(stb_sync s)
{
stb_mutex_begin(s->mutex);
assert(s->sofar < s->target);
++s->sofar;
if (s->sofar == s->target) {
stb__sync_release(s);
stb_mutex_end(s->mutex);
} else {
++s->waiting; // we're waiting, so one more waiter
stb_mutex_end(s->mutex); // release the mutex to other threads
stb_sem_waitfor(s->release); // wait for merge completion
stb_mutex_begin(s->mutex); // on merge completion, grab the mutex
--s->waiting; // we're done waiting
stb__sync_release(s); // restart the next waiter
stb_mutex_end(s->mutex); // and now we're done
// this ends the same as the first case, but it's a lot
// clearer to understand without sharing the code
}
}
struct stb__threadqueue
{
stb_mutex add, remove;
stb_semaphore nonempty, nonfull;
int head_blockers; // number of threads blocking--used to know whether to release(avail)
int tail_blockers;
int head, tail, array_size, growable;
int item_size;
char *data;
};
static int stb__tq_wrap(volatile stb_threadqueue *z, int p)
{
if (p == z->array_size)
return p - z->array_size;
else
return p;
}
int stb__threadq_get_raw(stb_threadqueue *tq2, void *output, int block)
{
volatile stb_threadqueue *tq = (volatile stb_threadqueue *) tq2;
if (tq->head == tq->tail && !block) return 0;
stb_mutex_begin(tq->remove);
while (tq->head == tq->tail) {
if (!block) {
stb_mutex_end(tq->remove);
return 0;
}
++tq->head_blockers;
stb_mutex_end(tq->remove);
stb_sem_waitfor(tq->nonempty);
stb_mutex_begin(tq->remove);
--tq->head_blockers;
}
memcpy(output, tq->data + tq->head*tq->item_size, tq->item_size);
stb_barrier();
tq->head = stb__tq_wrap(tq, tq->head+1);
stb_sem_release(tq->nonfull);
if (tq->head_blockers) // can't check if actually non-empty due to race?
stb_sem_release(tq->nonempty); // if there are other blockers, wake one
stb_mutex_end(tq->remove);
return STB_TRUE;
}
int stb__threadq_grow(volatile stb_threadqueue *tq)
{
int n;
char *p;
assert(tq->remove != STB_MUTEX_NULL); // must have this to allow growth!
stb_mutex_begin(tq->remove);
n = tq->array_size * 2;
p = (char *) realloc(tq->data, n * tq->item_size);
if (p == NULL) {
stb_mutex_end(tq->remove);
stb_mutex_end(tq->add);
return STB_FALSE;
}
if (tq->tail < tq->head) {
memcpy(p + tq->array_size * tq->item_size, p, tq->tail * tq->item_size);
tq->tail += tq->array_size;
}
tq->data = p;
tq->array_size = n;
stb_mutex_end(tq->remove);
return STB_TRUE;
}
int stb__threadq_add_raw(stb_threadqueue *tq2, void *input, int block)
{
int tail,pos;
volatile stb_threadqueue *tq = (volatile stb_threadqueue *) tq2;
stb_mutex_begin(tq->add);
for(;;) {
pos = tq->tail;
tail = stb__tq_wrap(tq, pos+1);
if (tail != tq->head) break;
// full
if (tq->growable) {
if (!stb__threadq_grow(tq)) {
stb_mutex_end(tq->add);
return STB_FALSE; // out of memory
}
} else if (!block) {
stb_mutex_end(tq->add);
return STB_FALSE;
} else {
++tq->tail_blockers;
stb_mutex_end(tq->add);
stb_sem_waitfor(tq->nonfull);
stb_mutex_begin(tq->add);
--tq->tail_blockers;
}
}
memcpy(tq->data + tq->item_size * pos, input, tq->item_size);
stb_barrier();
tq->tail = tail;
stb_sem_release(tq->nonempty);
if (tq->tail_blockers) // can't check if actually non-full due to race?
stb_sem_release(tq->nonfull);
stb_mutex_end(tq->add);
return STB_TRUE;
}
int stb_threadq_length(stb_threadqueue *tq2)
{
int a,b,n;
volatile stb_threadqueue *tq = (volatile stb_threadqueue *) tq2;
stb_mutex_begin(tq->add);
a = tq->head;
b = tq->tail;
n = tq->array_size;
stb_mutex_end(tq->add);
if (a > b) b += n;
return b-a;
}
int stb_threadq_get(stb_threadqueue *tq, void *output)
{
return stb__threadq_get_raw(tq, output, STB_FALSE);
}
void stb_threadq_get_block(stb_threadqueue *tq, void *output)
{
stb__threadq_get_raw(tq, output, STB_TRUE);
}
int stb_threadq_add(stb_threadqueue *tq, void *input)
{
return stb__threadq_add_raw(tq, input, STB_FALSE);
}
int stb_threadq_add_block(stb_threadqueue *tq, void *input)
{
return stb__threadq_add_raw(tq, input, STB_TRUE);
}
void stb_threadq_delete(stb_threadqueue *tq)
{
if (tq) {
free(tq->data);
stb_mutex_delete(tq->add);
stb_mutex_delete(tq->remove);
stb_sem_delete(tq->nonempty);
stb_sem_delete(tq->nonfull);
free(tq);
}
}
#define STB_THREADQUEUE_DYNAMIC 0
stb_threadqueue *stb_threadq_new(int item_size, int num_items, int many_add, int many_remove)
{
int error=0;
stb_threadqueue *tq = (stb_threadqueue *) malloc(sizeof(*tq));
if (tq == NULL) return NULL;
if (num_items == STB_THREADQUEUE_DYNAMIC) {
tq->growable = STB_TRUE;
num_items = 32;
} else
tq->growable = STB_FALSE;
tq->item_size = item_size;
tq->array_size = num_items+1;
tq->add = tq->remove = STB_MUTEX_NULL;
tq->nonempty = tq->nonfull = STB_SEMAPHORE_NULL;
tq->data = NULL;
if (many_add)
{ tq->add = stb_mutex_new(); if (tq->add == STB_MUTEX_NULL) goto error; }
if (many_remove || tq->growable)
{ tq->remove = stb_mutex_new(); if (tq->remove == STB_MUTEX_NULL) goto error; }
tq->nonempty = stb_sem_new(1); if (tq->nonempty == STB_SEMAPHORE_NULL) goto error;
tq->nonfull = stb_sem_new(1); if (tq->nonfull == STB_SEMAPHORE_NULL) goto error;
tq->data = (char *) malloc(tq->item_size * tq->array_size);
if (tq->data == NULL) goto error;
tq->head = tq->tail = 0;
tq->head_blockers = tq->tail_blockers = 0;
return tq;
error:
stb_threadq_delete(tq);
return NULL;
}
typedef struct
{
stb_thread_func f;
void *d;
volatile void **retval;
stb_sync sync;
} stb__workinfo;
//static volatile stb__workinfo *stb__work;
struct stb__workqueue
{
int numthreads;
stb_threadqueue *tq;
};
static stb_workqueue *stb__work_global;
static void *stb__thread_workloop(void *p)
{
volatile stb_workqueue *q = (volatile stb_workqueue *) p;
for(;;) {
void *z;
stb__workinfo w;
stb_threadq_get_block(q->tq, &w);
if (w.f == NULL) // null work is a signal to end the thread
return NULL;
z = w.f(w.d);
if (w.retval) { stb_barrier(); *w.retval = z; }
if (w.sync != STB_SYNC_NULL) stb_sync_reach(w.sync);
}
}
stb_workqueue *stb_workq_new(int num_threads, int max_units)
{
return stb_workq_new_flags(num_threads, max_units, 0,0);
}
stb_workqueue *stb_workq_new_flags(int numthreads, int max_units, int no_add_mutex, int no_remove_mutex)
{
stb_workqueue *q = (stb_workqueue *) malloc(sizeof(*q));
if (q == NULL) return NULL;
q->tq = stb_threadq_new(sizeof(stb__workinfo), max_units, !no_add_mutex, !no_remove_mutex);
if (q->tq == NULL) { free(q); return NULL; }
q->numthreads = 0;
stb_workq_numthreads(q, numthreads);
return q;
}
void stb_workq_delete(stb_workqueue *q)
{
while (stb_workq_length(q) != 0)
stb__thread_sleep(1);
stb_threadq_delete(q->tq);
free(q);
}
static int stb__work_maxitems = STB_THREADQUEUE_DYNAMIC;
static void stb_work_init(int num_threads)
{
if (stb__work_global == NULL) {
stb__threadmutex_init();
stb_mutex_begin(stb__workmutex);
stb_barrier();
if (*(stb_workqueue * volatile *) &stb__work_global == NULL)
stb__work_global = stb_workq_new(num_threads, stb__work_maxitems);
stb_mutex_end(stb__workmutex);
}
}
static int stb__work_raw(stb_workqueue *q, stb_thread_func f, void *d, volatile void **return_code, stb_sync rel)
{
stb__workinfo w;
if (q == NULL) {
stb_work_init(1);
q = stb__work_global;
}
w.f = f;
w.d = d;
w.retval = return_code;
w.sync = rel;
return stb_threadq_add(q->tq, &w);
}
int stb_workq_length(stb_workqueue *q)
{
return stb_threadq_length(q->tq);
}
int stb_workq(stb_workqueue *q, stb_thread_func f, void *d, volatile void **return_code)
{
if (f == NULL) return 0;
return stb_workq_reach(q, f, d, return_code, NULL);
}
int stb_workq_reach(stb_workqueue *q, stb_thread_func f, void *d, volatile void **return_code, stb_sync rel)
{
if (f == NULL) return 0;
return stb__work_raw(q, f, d, return_code, rel);
}
static void stb__workq_numthreads(stb_workqueue *q, int n)
{
while (q->numthreads < n) {
stb_create_thread(stb__thread_workloop, q);
++q->numthreads;
}
while (q->numthreads > n) {
stb__work_raw(q, NULL, NULL, NULL, NULL);
--q->numthreads;
}
}
void stb_workq_numthreads(stb_workqueue *q, int n)
{
stb_mutex_begin(stb__threadmutex);
stb__workq_numthreads(q,n);
stb_mutex_end(stb__threadmutex);
}
int stb_work_maxunits(int n)
{
if (stb__work_global == NULL) {
stb__work_maxitems = n;
stb_work_init(1);
}
return stb__work_maxitems;
}
int stb_work(stb_thread_func f, void *d, volatile void **return_code)
{
return stb_workq(stb__work_global, f,d,return_code);
}
int stb_work_reach(stb_thread_func f, void *d, volatile void **return_code, stb_sync rel)
{
return stb_workq_reach(stb__work_global, f,d,return_code,rel);
}
void stb_work_numthreads(int n)
{
if (stb__work_global == NULL)
stb_work_init(n);
else
stb_workq_numthreads(stb__work_global, n);
}
#endif // STB_DEFINE
//////////////////////////////////////////////////////////////////////////////
//
// Background disk I/O
//
//
#define STB_BGIO_READ_ALL (-1)
STB_EXTERN int stb_bgio_read (char *filename, int offset, int len, stb_uchar **result, int *olen);
STB_EXTERN int stb_bgio_readf (FILE *f , int offset, int len, stb_uchar **result, int *olen);
STB_EXTERN int stb_bgio_read_to (char *filename, int offset, int len, stb_uchar *buffer, int *olen);
STB_EXTERN int stb_bgio_readf_to(FILE *f , int offset, int len, stb_uchar *buffer, int *olen);
typedef struct
{
int have_data;
int is_valid;
int is_dir;
time_t filetime;
stb_int64 filesize;
} stb_bgstat;
STB_EXTERN int stb_bgio_stat (char *filename, stb_bgstat *result);
#ifdef STB_DEFINE
static stb_workqueue *stb__diskio;
static stb_mutex stb__diskio_mutex;
void stb_thread_cleanup(void)
{
if (stb__work_global) stb_workq_delete(stb__work_global); stb__work_global = NULL;
if (stb__threadmutex) stb_mutex_delete(stb__threadmutex); stb__threadmutex = NULL;
if (stb__workmutex) stb_mutex_delete(stb__workmutex); stb__workmutex = NULL;
if (stb__diskio) stb_workq_delete(stb__diskio); stb__diskio = NULL;
if (stb__diskio_mutex)stb_mutex_delete(stb__diskio_mutex);stb__diskio_mutex= NULL;
}
typedef struct
{
char *filename;
FILE *f;
int offset;
int len;
stb_bgstat *stat_out;
stb_uchar *output;
stb_uchar **result;
int *len_output;
int *flag;
} stb__disk_command;
#define STB__MAX_DISK_COMMAND 100
static stb__disk_command stb__dc_queue[STB__MAX_DISK_COMMAND];
static int stb__dc_offset;
void stb__io_init(void)
{
if (!stb__diskio) {
stb__threadmutex_init();
stb_mutex_begin(stb__threadmutex);
stb_barrier();
if (*(stb_thread * volatile *) &stb__diskio == NULL) {
stb__diskio_mutex = stb_mutex_new();
// use many threads so OS can try to schedule seeks
stb__diskio = stb_workq_new_flags(16,STB__MAX_DISK_COMMAND,STB_FALSE,STB_FALSE);
}
stb_mutex_end(stb__threadmutex);
}
}
static void * stb__io_error(stb__disk_command *dc)
{
if (dc->len_output) *dc->len_output = 0;
if (dc->result) *dc->result = NULL;
if (dc->flag) *dc->flag = -1;
return NULL;
}
static void * stb__io_task(void *p)
{
stb__disk_command *dc = (stb__disk_command *) p;
int len;
FILE *f;
stb_uchar *buf;
if (dc->stat_out) {
struct _stati64 s;
if (!_stati64(dc->filename, &s)) {
dc->stat_out->filesize = s.st_size;
dc->stat_out->filetime = s.st_mtime;
dc->stat_out->is_dir = s.st_mode & _S_IFDIR;
dc->stat_out->is_valid = (s.st_mode & _S_IFREG) || dc->stat_out->is_dir;
} else
dc->stat_out->is_valid = 0;
stb_barrier();
dc->stat_out->have_data = 1;
free(dc->filename);
return 0;
}
if (dc->f) {
#ifdef WIN32
f = _fdopen(_dup(_fileno(dc->f)), "rb");
#else
f = fdopen(dup(fileno(dc->f)), "rb");
#endif
if (!f)
return stb__io_error(dc);
} else {
f = fopen(dc->filename, "rb");
free(dc->filename);
if (!f)
return stb__io_error(dc);
}
len = dc->len;
if (len < 0) {
fseek(f, 0, SEEK_END);
len = ftell(f) - dc->offset;
}
if (fseek(f, dc->offset, SEEK_SET)) {
fclose(f);
return stb__io_error(dc);
}
if (dc->output)
buf = dc->output;
else {
buf = (stb_uchar *) malloc(len);
if (buf == NULL) {
fclose(f);
return stb__io_error(dc);
}
}
len = fread(buf, 1, len, f);
fclose(f);
if (dc->len_output) *dc->len_output = len;
if (dc->result) *dc->result = buf;
if (dc->flag) *dc->flag = 1;
return NULL;
}
int stb__io_add(char *fname, FILE *f, int off, int len, stb_uchar *out, stb_uchar **result, int *olen, int *flag, stb_bgstat *stat)
{
int res;
stb__io_init();
// do memory allocation outside of mutex
if (fname) fname = stb_p_strdup(fname);
stb_mutex_begin(stb__diskio_mutex);
{
stb__disk_command *dc = &stb__dc_queue[stb__dc_offset];
dc->filename = fname;
dc->f = f;
dc->offset = off;
dc->len = len;
dc->output = out;
dc->result = result;
dc->len_output = olen;
dc->flag = flag;
dc->stat_out = stat;
res = stb_workq(stb__diskio, stb__io_task, dc, NULL);
if (res)
stb__dc_offset = (stb__dc_offset + 1 == STB__MAX_DISK_COMMAND ? 0 : stb__dc_offset+1);
}
stb_mutex_end(stb__diskio_mutex);
return res;
}
int stb_bgio_read(char *filename, int offset, int len, stb_uchar **result, int *olen)
{
return stb__io_add(filename,NULL,offset,len,NULL,result,olen,NULL,NULL);
}
int stb_bgio_readf(FILE *f, int offset, int len, stb_uchar **result, int *olen)
{
return stb__io_add(NULL,f,offset,len,NULL,result,olen,NULL,NULL);
}
int stb_bgio_read_to(char *filename, int offset, int len, stb_uchar *buffer, int *olen)
{
return stb__io_add(filename,NULL,offset,len,buffer,NULL,olen,NULL,NULL);
}
int stb_bgio_readf_to(FILE *f, int offset, int len, stb_uchar *buffer, int *olen)
{
return stb__io_add(NULL,f,offset,len,buffer,NULL,olen,NULL,NULL);
}
STB_EXTERN int stb_bgio_stat (char *filename, stb_bgstat *result)
{
result->have_data = 0;
return stb__io_add(filename,NULL,0,0,0,NULL,0,NULL, result);
}
#endif
#endif
//////////////////////////////////////////////////////////////////////////////
//
// Fast malloc implementation
//
// This is a clone of TCMalloc, but without the thread support.
// 1. large objects are allocated directly, page-aligned
// 2. small objects are allocated in homogeonous heaps, 0 overhead
//
// We keep an allocation table for pages a la TCMalloc. This would
// require 4MB for the entire address space, but we only allocate
// the parts that are in use. The overhead from using homogenous heaps
// everywhere is 3MB. (That is, if you allocate 1 object of each size,
// you'll use 3MB.)
#if defined(STB_DEFINE) && ((defined(_WIN32) && !defined(_M_AMD64)) || defined(STB_FASTMALLOC))
#ifdef _WIN32
#ifndef _WINDOWS_
#ifndef STB__IMPORT
#define STB__IMPORT STB_EXTERN __declspec(dllimport)
#define STB__DW unsigned long
#endif
STB__IMPORT void * __stdcall VirtualAlloc(void *p, unsigned long size, unsigned long type, unsigned long protect);
STB__IMPORT int __stdcall VirtualFree(void *p, unsigned long size, unsigned long freetype);
#endif
#define stb__alloc_pages_raw(x) (stb_uint32) VirtualAlloc(NULL, (x), 0x3000, 0x04)
#define stb__dealloc_pages_raw(p) VirtualFree((void *) p, 0, 0x8000)
#else
#error "Platform not currently supported"
#endif
typedef struct stb__span
{
int start, len;
struct stb__span *next, *prev;
void *first_free;
unsigned short list; // 1..256 free; 257..511 sizeclass; 0=large block
short allocations; // # outstanding allocations for sizeclass
} stb__span; // 24
static stb__span **stb__span_for_page;
static int stb__firstpage, stb__lastpage;
static void stb__update_page_range(int first, int last)
{
stb__span **sfp;
int i, f,l;
if (first >= stb__firstpage && last <= stb__lastpage) return;
if (stb__span_for_page == NULL) {
f = first;
l = f+stb_max(last-f, 16384);
l = stb_min(l, 1<<20);
} else if (last > stb__lastpage) {
f = stb__firstpage;
l = f + (stb__lastpage - f) * 2;
l = stb_clamp(last, l,1<<20);
} else {
l = stb__lastpage;
f = l - (l - stb__firstpage) * 2;
f = stb_clamp(f, 0,first);
}
sfp = (stb__span **) stb__alloc_pages_raw(sizeof(void *) * (l-f));
for (i=f; i < stb__firstpage; ++i) sfp[i - f] = NULL;
for ( ; i < stb__lastpage ; ++i) sfp[i - f] = stb__span_for_page[i - stb__firstpage];
for ( ; i < l ; ++i) sfp[i - f] = NULL;
if (stb__span_for_page) stb__dealloc_pages_raw(stb__span_for_page);
stb__firstpage = f;
stb__lastpage = l;
stb__span_for_page = sfp;
}
static stb__span *stb__span_free=NULL;
static stb__span *stb__span_first, *stb__span_end;
static stb__span *stb__span_alloc(void)
{
stb__span *s = stb__span_free;
if (s)
stb__span_free = s->next;
else {
if (!stb__span_first) {
stb__span_first = (stb__span *) stb__alloc_pages_raw(65536);
if (stb__span_first == NULL) return NULL;
stb__span_end = stb__span_first + (65536 / sizeof(stb__span));
}
s = stb__span_first++;
if (stb__span_first == stb__span_end) stb__span_first = NULL;
}
return s;
}
static stb__span *stb__spanlist[512];
static void stb__spanlist_unlink(stb__span *s)
{
if (s->prev)
s->prev->next = s->next;
else {
int n = s->list;
assert(stb__spanlist[n] == s);
stb__spanlist[n] = s->next;
}
if (s->next)
s->next->prev = s->prev;
s->next = s->prev = NULL;
s->list = 0;
}
static void stb__spanlist_add(int n, stb__span *s)
{
s->list = n;
s->next = stb__spanlist[n];
s->prev = NULL;
stb__spanlist[n] = s;
if (s->next) s->next->prev = s;
}
#define stb__page_shift 12
#define stb__page_size (1 << stb__page_shift)
#define stb__page_number(x) ((x) >> stb__page_shift)
#define stb__page_address(x) ((x) << stb__page_shift)
static void stb__set_span_for_page(stb__span *s)
{
int i;
for (i=0; i < s->len; ++i)
stb__span_for_page[s->start + i - stb__firstpage] = s;
}
static stb__span *stb__coalesce(stb__span *a, stb__span *b)
{
assert(a->start + a->len == b->start);
if (a->list) stb__spanlist_unlink(a);
if (b->list) stb__spanlist_unlink(b);
a->len += b->len;
b->len = 0;
b->next = stb__span_free;
stb__span_free = b;
stb__set_span_for_page(a);
return a;
}
static void stb__free_span(stb__span *s)
{
stb__span *n = NULL;
if (s->start > stb__firstpage) {
n = stb__span_for_page[s->start-1 - stb__firstpage];
if (n && n->allocations == -2 && n->start + n->len == s->start) s = stb__coalesce(n,s);
}
if (s->start + s->len < stb__lastpage) {
n = stb__span_for_page[s->start + s->len - stb__firstpage];
if (n && n->allocations == -2 && s->start + s->len == n->start) s = stb__coalesce(s,n);
}
s->allocations = -2;
stb__spanlist_add(s->len > 256 ? 256 : s->len, s);
}
static stb__span *stb__alloc_pages(int num)
{
stb__span *s = stb__span_alloc();
int p;
if (!s) return NULL;
p = stb__alloc_pages_raw(num << stb__page_shift);
if (p == 0) { s->next = stb__span_free; stb__span_free = s; return 0; }
assert(stb__page_address(stb__page_number(p)) == p);
p = stb__page_number(p);
stb__update_page_range(p, p+num);
s->start = p;
s->len = num;
s->next = NULL;
s->prev = NULL;
stb__set_span_for_page(s);
return s;
}
static stb__span *stb__alloc_span(int pagecount)
{
int i;
stb__span *p = NULL;
for(i=pagecount; i < 256; ++i)
if (stb__spanlist[i]) {
p = stb__spanlist[i];
break;
}
if (!p) {
p = stb__spanlist[256];
while (p && p->len < pagecount)
p = p->next;
}
if (!p) {
p = stb__alloc_pages(pagecount < 16 ? 16 : pagecount);
if (p == NULL) return 0;
} else
stb__spanlist_unlink(p);
if (p->len > pagecount) {
stb__span *q = stb__span_alloc();
if (q) {
q->start = p->start + pagecount;
q->len = p->len - pagecount;
p->len = pagecount;
for (i=0; i < q->len; ++i)
stb__span_for_page[q->start+i - stb__firstpage] = q;
stb__spanlist_add(q->len > 256 ? 256 : q->len, q);
}
}
return p;
}
#define STB__MAX_SMALL_SIZE 32768
#define STB__MAX_SIZE_CLASSES 256
static unsigned char stb__class_base[32];
static unsigned char stb__class_shift[32];
static unsigned char stb__pages_for_class[STB__MAX_SIZE_CLASSES];
static int stb__size_for_class[STB__MAX_SIZE_CLASSES];
stb__span *stb__get_nonempty_sizeclass(int c)
{
int s = c + 256, i, size, tsize; // remap to span-list index
char *z;
void *q;
stb__span *p = stb__spanlist[s];
if (p) {
if (p->first_free) return p; // fast path: it's in the first one in list
for (p=p->next; p; p=p->next)
if (p->first_free) {
// move to front for future queries
stb__spanlist_unlink(p);
stb__spanlist_add(s, p);
return p;
}
}
// no non-empty ones, so allocate a new one
p = stb__alloc_span(stb__pages_for_class[c]);
if (!p) return NULL;
// create the free list up front
size = stb__size_for_class[c];
tsize = stb__pages_for_class[c] << stb__page_shift;
i = 0;
z = (char *) stb__page_address(p->start);
q = NULL;
while (i + size <= tsize) {
* (void **) z = q; q = z;
z += size;
i += size;
}
p->first_free = q;
p->allocations = 0;
stb__spanlist_add(s,p);
return p;
}
static int stb__sizeclass(size_t sz)
{
int z = stb_log2_floor(sz); // -1 below to group e.g. 13,14,15,16 correctly
return stb__class_base[z] + ((sz-1) >> stb__class_shift[z]);
}
static void stb__init_sizeclass(void)
{
int i, size, overhead;
int align_shift = 2; // allow 4-byte and 12-byte blocks as well, vs. TCMalloc
int next_class = 1;
int last_log = 0;
for (i = 0; i < align_shift; i++) {
stb__class_base [i] = next_class;
stb__class_shift[i] = align_shift;
}
for (size = 1 << align_shift; size <= STB__MAX_SMALL_SIZE; size += 1 << align_shift) {
i = stb_log2_floor(size);
if (i > last_log) {
if (size == 16) ++align_shift; // switch from 4-byte to 8-byte alignment
else if (size >= 128 && align_shift < 8) ++align_shift;
stb__class_base[i] = next_class - ((size-1) >> align_shift);
stb__class_shift[i] = align_shift;
last_log = i;
}
stb__size_for_class[next_class++] = size;
}
for (i=1; i <= STB__MAX_SMALL_SIZE; ++i)
assert(i <= stb__size_for_class[stb__sizeclass(i)]);
overhead = 0;
for (i = 1; i < next_class; i++) {
int s = stb__size_for_class[i];
size = stb__page_size;
while (size % s > size >> 3)
size += stb__page_size;
stb__pages_for_class[i] = (unsigned char) (size >> stb__page_shift);
overhead += size;
}
assert(overhead < (4 << 20)); // make sure it's under 4MB of overhead
}
#ifdef STB_DEBUG
#define stb__smemset(a,b,c) memset((void *) a, b, c)
#elif defined(STB_FASTMALLOC_INIT)
#define stb__smemset(a,b,c) memset((void *) a, b, c)
#else
#define stb__smemset(a,b,c)
#endif
void *stb_smalloc(size_t sz)
{
stb__span *s;
if (sz == 0) return NULL;
if (stb__size_for_class[1] == 0) stb__init_sizeclass();
if (sz > STB__MAX_SMALL_SIZE) {
s = stb__alloc_span((sz + stb__page_size - 1) >> stb__page_shift);
if (s == NULL) return NULL;
s->list = 0;
s->next = s->prev = NULL;
s->allocations = -32767;
stb__smemset(stb__page_address(s->start), 0xcd, (sz+3)&~3);
return (void *) stb__page_address(s->start);
} else {
void *p;
int c = stb__sizeclass(sz);
s = stb__spanlist[256+c];
if (!s || !s->first_free)
s = stb__get_nonempty_sizeclass(c);
if (s == NULL) return NULL;
p = s->first_free;
s->first_free = * (void **) p;
++s->allocations;
stb__smemset(p,0xcd, sz);
return p;
}
}
int stb_ssize(void *p)
{
stb__span *s;
if (p == NULL) return 0;
s = stb__span_for_page[stb__page_number((stb_uint) p) - stb__firstpage];
if (s->list >= 256) {
return stb__size_for_class[s->list - 256];
} else {
assert(s->list == 0);
return s->len << stb__page_shift;
}
}
void stb_sfree(void *p)
{
stb__span *s;
if (p == NULL) return;
s = stb__span_for_page[stb__page_number((stb_uint) p) - stb__firstpage];
if (s->list >= 256) {
stb__smemset(p, 0xfe, stb__size_for_class[s->list-256]);
* (void **) p = s->first_free;
s->first_free = p;
if (--s->allocations == 0) {
stb__spanlist_unlink(s);
stb__free_span(s);
}
} else {
assert(s->list == 0);
stb__smemset(p, 0xfe, stb_ssize(p));
stb__free_span(s);
}
}
void *stb_srealloc(void *p, size_t sz)
{
size_t cur_size;
if (p == NULL) return stb_smalloc(sz);
if (sz == 0) { stb_sfree(p); return NULL; }
cur_size = stb_ssize(p);
if (sz > cur_size || sz <= (cur_size >> 1)) {
void *q;
if (sz > cur_size && sz < (cur_size << 1)) sz = cur_size << 1;
q = stb_smalloc(sz); if (q == NULL) return NULL;
memcpy(q, p, sz < cur_size ? sz : cur_size);
stb_sfree(p);
return q;
}
return p;
}
void *stb_scalloc(size_t n, size_t sz)
{
void *p;
if (n == 0 || sz == 0) return NULL;
if (stb_log2_ceil(n) + stb_log2_ceil(n) >= 32) return NULL;
p = stb_smalloc(n*sz);
if (p) memset(p, 0, n*sz);
return p;
}
char *stb_sstrdup(char *s)
{
int n = strlen(s);
char *p = (char *) stb_smalloc(n+1);
if (p) stb_p_strcpy_s(p,n+1,s);
return p;
}
#endif // STB_DEFINE
//////////////////////////////////////////////////////////////////////////////
//
// Source code constants
//
// This is a trivial system to let you specify constants in source code,
// then while running you can change the constants.
//
// Note that you can't wrap the #defines, because we need to know their
// names. So we provide a pre-wrapped version without 'STB_' for convenience;
// to request it, #define STB_CONVENIENT_H, yielding:
// KI -- integer
// KU -- unsigned integer
// KF -- float
// KD -- double
// KS -- string constant
//
// Defaults to functioning in debug build, not in release builds.
// To force on, define STB_ALWAYS_H
#ifdef STB_CONVENIENT_H
#define KI(x) STB_I(x)
#define KU(x) STB_UI(x)
#define KF(x) STB_F(x)
#define KD(x) STB_D(x)
#define KS(x) STB_S(x)
#endif
STB_EXTERN void stb_source_path(char *str);
#ifdef STB_DEFINE
char *stb__source_path;
void stb_source_path(char *path)
{
stb__source_path = path;
}
char *stb__get_sourcefile_path(char *file)
{
static char filebuf[512];
if (stb__source_path) {
stb_p_sprintf(filebuf stb_p_size(sizeof(filebuf)), "%s/%s", stb__source_path, file);
if (stb_fexists(filebuf)) return filebuf;
}
if (stb_fexists(file)) return file;
stb_p_sprintf(filebuf stb_p_size(sizeof(filebuf)), "../%s", file);
if (!stb_fexists(filebuf)) return filebuf;
return file;
}
#endif
#define STB_F(x) ((float) STB_H(x))
#define STB_UI(x) ((unsigned int) STB_I(x))
#if !defined(STB_DEBUG) && !defined(STB_ALWAYS_H)
#define STB_D(x) ((double) (x))
#define STB_I(x) ((int) (x))
#define STB_S(x) ((char *) (x))
#else
#define STB_D(x) stb__double_constant(__FILE__, __LINE__-1, (x))
#define STB_I(x) stb__int_constant(__FILE__, __LINE__-1, (x))
#define STB_S(x) stb__string_constant(__FILE__, __LINE__-1, (x))
STB_EXTERN double stb__double_constant(char *file, int line, double x);
STB_EXTERN int stb__int_constant(char *file, int line, int x);
STB_EXTERN char * stb__string_constant(char *file, int line, char *str);
#ifdef STB_DEFINE
enum
{
STB__CTYPE_int,
STB__CTYPE_uint,
STB__CTYPE_float,
STB__CTYPE_double,
STB__CTYPE_string,
};
typedef struct
{
int line;
int type;
union {
int ival;
double dval;
char *sval;
};
} stb__Entry;
typedef struct
{
stb__Entry *entries;
char *filename;
time_t timestamp;
char **file_data;
int file_len;
unsigned short *line_index;
} stb__FileEntry;
static void stb__constant_parse(stb__FileEntry *f, int i)
{
char *s;
int n;
if (!stb_arr_valid(f->entries, i)) return;
n = f->entries[i].line;
if (n >= f->file_len) return;
s = f->file_data[n];
switch (f->entries[i].type) {
case STB__CTYPE_float:
while (*s) {
if (!strncmp(s, "STB_D(", 6)) { s+=6; goto matched_float; }
if (!strncmp(s, "STB_F(", 6)) { s+=6; goto matched_float; }
if (!strncmp(s, "KD(", 3)) { s+=3; goto matched_float; }
if (!strncmp(s, "KF(", 3)) { s+=3; goto matched_float; }
++s;
}
break;
matched_float:
f->entries[i].dval = strtod(s, NULL);
break;
case STB__CTYPE_int:
while (*s) {
if (!strncmp(s, "STB_I(", 6)) { s+=6; goto matched_int; }
if (!strncmp(s, "STB_UI(", 7)) { s+=7; goto matched_int; }
if (!strncmp(s, "KI(", 3)) { s+=3; goto matched_int; }
if (!strncmp(s, "KU(", 3)) { s+=3; goto matched_int; }
++s;
}
break;
matched_int: {
int neg=0;
s = stb_skipwhite(s);
while (*s == '-') { neg = !neg; s = stb_skipwhite(s+1); } // handle '- - 5', pointlessly
if (s[0] == '0' && tolower(s[1]) == 'x')
f->entries[i].ival = strtol(s, NULL, 16);
else if (s[0] == '0')
f->entries[i].ival = strtol(s, NULL, 8);
else
f->entries[i].ival = strtol(s, NULL, 10);
if (neg) f->entries[i].ival = -f->entries[i].ival;
break;
}
case STB__CTYPE_string:
// @TODO
break;
}
}
static stb_sdict *stb__constant_file_hash;
stb__Entry *stb__constant_get_entry(char *filename, int line, int type)
{
int i;
stb__FileEntry *f;
if (stb__constant_file_hash == NULL)
stb__constant_file_hash = stb_sdict_new(STB_TRUE);
f = (stb__FileEntry*) stb_sdict_get(stb__constant_file_hash, filename);
if (f == NULL) {
char *s = stb__get_sourcefile_path(filename);
if (s == NULL || !stb_fexists(s)) return 0;
f = (stb__FileEntry *) malloc(sizeof(*f));
f->timestamp = stb_ftimestamp(s);
f->file_data = stb_stringfile(s, &f->file_len);
f->filename = stb_p_strdup(s); // cache the full path
f->entries = NULL;
f->line_index = 0;
stb_arr_setlen(f->line_index, f->file_len);
memset(f->line_index, 0xff, stb_arr_storage(f->line_index));
} else {
time_t t = stb_ftimestamp(f->filename);
if (f->timestamp != t) {
f->timestamp = t;
free(f->file_data);
f->file_data = stb_stringfile(f->filename, &f->file_len);
stb_arr_setlen(f->line_index, f->file_len);
for (i=0; i < stb_arr_len(f->entries); ++i)
stb__constant_parse(f, i);
}
}
if (line >= f->file_len) return 0;
if (f->line_index[line] >= stb_arr_len(f->entries)) {
// need a new entry
int n = stb_arr_len(f->entries);
stb__Entry e;
e.line = line;
if (line < f->file_len)
f->line_index[line] = n;
e.type = type;
stb_arr_push(f->entries, e);
stb__constant_parse(f, n);
}
return f->entries + f->line_index[line];
}
double stb__double_constant(char *file, int line, double x)
{
stb__Entry *e = stb__constant_get_entry(file, line, STB__CTYPE_float);
if (!e) return x;
return e->dval;
}
int stb__int_constant(char *file, int line, int x)
{
stb__Entry *e = stb__constant_get_entry(file, line, STB__CTYPE_int);
if (!e) return x;
return e->ival;
}
char * stb__string_constant(char *file, int line, char *x)
{
stb__Entry *e = stb__constant_get_entry(file, line, STB__CTYPE_string);
if (!e) return x;
return e->sval;
}
#endif // STB_DEFINE
#endif // !STB_DEBUG && !STB_ALWAYS_H
#ifdef STB_STUA
#error "STUA is no longer supported"
//////////////////////////////////////////////////////////////////////////
//
// stua: little scripting language
//
// define STB_STUA to compile it
//
// see http://nothings.org/stb/stb_stua.html for documentation
//
// basic parsing model:
//
// lexical analysis
// use stb_lex() to parse tokens; keywords get their own tokens
//
// parsing:
// recursive descent parser. too much of a hassle to make an unambiguous
// LR(1) grammar, and one-pass generation is clumsier (recursive descent
// makes it easier to e.g. compile nested functions). on the other hand,
// dictionary syntax required hackery to get extra lookahead.
//
// codegen:
// output into an evaluation tree, using array indices as 'pointers'
//
// run:
// traverse the tree; support for 'break/continue/return' is tricky
//
// garbage collection:
// stu__mark and sweep; explicit stack with non-stu__compile_global_scope roots
typedef stb_int32 stua_obj;
typedef stb_idict stua_dict;
STB_EXTERN void stua_run_script(char *s);
STB_EXTERN void stua_uninit(void);
extern stua_obj stua_globals;
STB_EXTERN double stua_number(stua_obj z);
STB_EXTERN stua_obj stua_getnil(void);
STB_EXTERN stua_obj stua_getfalse(void);
STB_EXTERN stua_obj stua_gettrue(void);
STB_EXTERN stua_obj stua_string(char *z);
STB_EXTERN stua_obj stua_make_number(double d);
STB_EXTERN stua_obj stua_box(int type, void *data, int size);
enum
{
STUA_op_negate=129,
STUA_op_shl, STUA_op_ge,
STUA_op_shr, STUA_op_le,
STUA_op_shru,
STUA_op_last
};
#define STUA_NO_VALUE 2 // equivalent to a tagged NULL
STB_EXTERN stua_obj (*stua_overload)(int op, stua_obj a, stua_obj b, stua_obj c);
STB_EXTERN stua_obj stua_error(char *err, ...);
STB_EXTERN stua_obj stua_pushroot(stua_obj o);
STB_EXTERN void stua_poproot ( void );
#ifdef STB_DEFINE
// INTERPRETER
// 31-bit floating point implementation
// force the (1 << 30) bit (2nd highest bit) to be zero by re-biasing the exponent;
// then shift and set the bottom bit
static stua_obj stu__floatp(float *f)
{
unsigned int n = *(unsigned int *) f;
unsigned int e = n & (0xff << 23);
assert(sizeof(int) == 4 && sizeof(float) == 4);
if (!e) // zero?
n = n; // no change
else if (e < (64 << 23)) // underflow of the packed encoding?
n = (n & 0x80000000); // signed 0
else if (e > (190 << 23)) // overflow of the encoding? (or INF or NAN)
n = (n & 0x80000000) + (127 << 23); // new INF encoding
else
n -= 0x20000000;
// now we need to shuffle the bits so that the spare bit is at the bottom
assert((n & 0x40000000) == 0);
return (n & 0x80000000) + (n << 1) + 1;
}
static unsigned char stu__getfloat_addend[256];
static float stu__getfloat(stua_obj v)
{
unsigned int n;
unsigned int e = ((unsigned int) v) >> 24;
n = (int) v >> 1; // preserve high bit
n += stu__getfloat_addend[e] << 24;
return *(float *) &n;
}
stua_obj stua_float(float f)
{
return stu__floatp(&f);
}
static void stu__float_init(void)
{
int i;
stu__getfloat_addend[0] = 0; // do nothing to biased exponent of 0
for (i=1; i < 127; ++i)
stu__getfloat_addend[i] = 32; // undo the -0x20000000
stu__getfloat_addend[127] = 64; // convert packed INF to INF (0x3f -> 0x7f)
for (i=0; i < 128; ++i) // for signed floats, remove the bit we just shifted down
stu__getfloat_addend[128+i] = stu__getfloat_addend[i] - 64;
}
// Tagged data type implementation
// TAGS:
#define stu__int_tag 0 // of 2 bits // 00 int
#define stu__float_tag 1 // of 1 bit // 01 float
#define stu__ptr_tag 2 // of 2 bits // 10 boxed
// 11 float
#define stu__tag(x) ((x) & 3)
#define stu__number(x) (stu__tag(x) != stu__ptr_tag)
#define stu__isint(x) (stu__tag(x) == stu__int_tag)
#define stu__int(x) ((x) >> 2)
#define stu__float(x) (stu__getfloat(x))
#define stu__makeint(v) ((v)*4+stu__int_tag)
// boxed data, and tag support for boxed data
enum
{
STU___float = 1, STU___int = 2,
STU___number = 3, STU___string = 4,
STU___function = 5, STU___dict = 6,
STU___boolean = 7, STU___error = 8,
};
// boxed data
#define STU__BOX short type, stua_gc
typedef struct stu__box { STU__BOX; } stu__box;
stu__box stu__nil = { 0, 1 };
stu__box stu__true = { STU___boolean, 1, };
stu__box stu__false = { STU___boolean, 1, };
#define stu__makeptr(v) ((stua_obj) (v) + stu__ptr_tag)
#define stua_nil stu__makeptr(&stu__nil)
#define stua_true stu__makeptr(&stu__true)
#define stua_false stu__makeptr(&stu__false)
stua_obj stua_getnil(void) { return stua_nil; }
stua_obj stua_getfalse(void) { return stua_false; }
stua_obj stua_gettrue(void) { return stua_true; }
#define stu__ptr(x) ((stu__box *) ((x) - stu__ptr_tag))
#define stu__checkt(t,x) ((t) == STU___float ? ((x) & 1) == stu__float_tag : \
(t) == STU___int ? stu__isint(x) : \
(t) == STU___number ? stu__number(x) : \
stu__tag(x) == stu__ptr_tag && stu__ptr(x)->type == (t))
typedef struct
{
STU__BOX;
void *ptr;
} stu__wrapper;
// implementation of a 'function' or function + closure
typedef struct stu__func
{
STU__BOX;
stua_obj closure_source; // 0 - regular function; 4 - C function
// if closure, pointer to source function
union {
stua_obj closure_data; // partial-application data
void *store; // pointer to free that holds 'code'
stua_obj (*func)(stua_dict *context);
} f;
// closure ends here
short *code;
int num_param;
stua_obj *param; // list of parameter strings
} stu__func;
// apply this to 'short *code' to get at data
#define stu__const(f) ((stua_obj *) (f))
static void stu__free_func(stu__func *f)
{
if (f->closure_source == 0) free(f->f.store);
if ((stb_uint) f->closure_source <= 4) free(f->param);
free(f);
}
#define stu__pd(x) ((stua_dict *) stu__ptr(x))
#define stu__pw(x) ((stu__wrapper *) stu__ptr(x))
#define stu__pf(x) ((stu__func *) stu__ptr(x))
// garbage-collection
static stu__box ** stu__gc_ptrlist;
static stua_obj * stu__gc_root_stack;
stua_obj stua_pushroot(stua_obj o) { stb_arr_push(stu__gc_root_stack, o); return o; }
void stua_poproot ( void ) { stb_arr_pop(stu__gc_root_stack); }
static stb_sdict *stu__strings;
static void stu__mark(stua_obj z)
{
int i;
stu__box *p = stu__ptr(z);
if (p->stua_gc == 1) return; // already marked
assert(p->stua_gc == 0);
p->stua_gc = 1;
switch(p->type) {
case STU___function: {
stu__func *f = (stu__func *) p;
if ((stb_uint) f->closure_source <= 4) {
if (f->closure_source == 0) {
for (i=1; i <= f->code[0]; ++i)
if (!stu__number(((stua_obj *) f->code)[-i]))
stu__mark(((stua_obj *) f->code)[-i]);
}
for (i=0; i < f->num_param; ++i)
stu__mark(f->param[i]);
} else {
stu__mark(f->closure_source);
stu__mark(f->f.closure_data);
}
break;
}
case STU___dict: {
stua_dict *e = (stua_dict *) p;
for (i=0; i < e->limit; ++i)
if (e->table[i].k != STB_IEMPTY && e->table[i].k != STB_IDEL) {
if (!stu__number(e->table[i].k)) stu__mark((int) e->table[i].k);
if (!stu__number(e->table[i].v)) stu__mark((int) e->table[i].v);
}
break;
}
}
}
static int stu__num_allocs, stu__size_allocs;
static stua_obj stu__flow_val = stua_nil; // used for break & return
static void stua_gc(int force)
{
int i;
if (!force && stu__num_allocs == 0 && stu__size_allocs == 0) return;
stu__num_allocs = stu__size_allocs = 0;
//printf("[gc]\n");
// clear marks
for (i=0; i < stb_arr_len(stu__gc_ptrlist); ++i)
stu__gc_ptrlist[i]->stua_gc = 0;
// stu__mark everything reachable
stu__nil.stua_gc = stu__true.stua_gc = stu__false.stua_gc = 1;
stu__mark(stua_globals);
if (!stu__number(stu__flow_val))
stu__mark(stu__flow_val);
for (i=0; i < stb_arr_len(stu__gc_root_stack); ++i)
if (!stu__number(stu__gc_root_stack[i]))
stu__mark(stu__gc_root_stack[i]);
// sweep unreachables
for (i=0; i < stb_arr_len(stu__gc_ptrlist);) {
stu__box *z = stu__gc_ptrlist[i];
if (!z->stua_gc) {
switch (z->type) {
case STU___dict: stb_idict_destroy((stua_dict *) z); break;
case STU___error: free(((stu__wrapper *) z)->ptr); break;
case STU___string: stb_sdict_remove(stu__strings, (char*) ((stu__wrapper *) z)->ptr, NULL); free(z); break;
case STU___function: stu__free_func((stu__func *) z); break;
}
// swap in the last item over this, and repeat
z = stb_arr_pop(stu__gc_ptrlist);
stu__gc_ptrlist[i] = z;
} else
++i;
}
}
static void stu__consider_gc(stua_obj x)
{
if (stu__size_allocs < 100000) return;
if (stu__num_allocs < 10 && stu__size_allocs < 1000000) return;
stb_arr_push(stu__gc_root_stack, x);
stua_gc(0);
stb_arr_pop(stu__gc_root_stack);
}
static stua_obj stu__makeobj(int type, void *data, int size, int safe_to_gc)
{
stua_obj x = stu__makeptr(data);
((stu__box *) data)->type = type;
stb_arr_push(stu__gc_ptrlist, (stu__box *) data);
stu__num_allocs += 1;
stu__size_allocs += size;
if (safe_to_gc) stu__consider_gc(x);
return x;
}
stua_obj stua_box(int type, void *data, int size)
{
stu__wrapper *p = (stu__wrapper *) malloc(sizeof(*p));
p->ptr = data;
return stu__makeobj(type, p, size, 0);
}
// a stu string can be directly compared for equality, because
// they go into a hash table
stua_obj stua_string(char *z)
{
stu__wrapper *b = (stu__wrapper *) stb_sdict_get(stu__strings, z);
if (b == NULL) {
int o = stua_box(STU___string, NULL, strlen(z) + sizeof(*b));
b = stu__pw(o);
stb_sdict_add(stu__strings, z, b);
stb_sdict_getkey(stu__strings, z, (char **) &b->ptr);
}
return stu__makeptr(b);
}
// stb_obj dictionary is just an stb_idict
static void stu__set(stua_dict *d, stua_obj k, stua_obj v)
{ if (stb_idict_set(d, k, v)) stu__size_allocs += 8; }
static stua_obj stu__get(stua_dict *d, stua_obj k, stua_obj res)
{
stb_idict_get_flag(d, k, &res);
return res;
}
static stua_obj make_string(char *z, int len)
{
stua_obj s;
char temp[256], *q = (char *) stb_temp(temp, len+1), *p = q;
while (len > 0) {
if (*z == '\\') {
if (z[1] == 'n') *p = '\n';
else if (z[1] == 'r') *p = '\r';
else if (z[1] == 't') *p = '\t';
else *p = z[1];
p += 1; z += 2; len -= 2;
} else {
*p++ = *z++; len -= 1;
}
}
*p = 0;
s = stua_string(q);
stb_tempfree(temp, q);
return s;
}
enum token_names
{
T__none=128,
ST_shl = STUA_op_shl, ST_ge = STUA_op_ge,
ST_shr = STUA_op_shr, ST_le = STUA_op_le,
ST_shru = STUA_op_shru, STU__negate = STUA_op_negate,
ST__reset_numbering = STUA_op_last,
ST_white,
ST_id, ST_float, ST_decimal, ST_hex, ST_char,ST_string, ST_number,
// make sure the keywords come _AFTER_ ST_id, so stb_lex prefer them
ST_if, ST_while, ST_for, ST_eq, ST_nil,
ST_then, ST_do, ST_in, ST_ne, ST_true,
ST_else, ST_break, ST_let, ST_and, ST_false,
ST_elseif, ST_continue, ST_into, ST_or, ST_repeat,
ST_end, ST_as, ST_return, ST_var, ST_func,
ST_catch, ST__frame,
ST__max_terminals,
STU__defaultparm, STU__seq,
};
static stua_dict * stu__globaldict;
stua_obj stua_globals;
static enum
{
FLOW_normal, FLOW_continue, FLOW_break, FLOW_return, FLOW_error,
} stu__flow;
stua_obj stua_error(char *z, ...)
{
stua_obj a;
char temp[4096], *x;
va_list v; va_start(v,z); vsprintf(temp, z, v); va_end(v);
x = stb_p_strdup(temp);
a = stua_box(STU___error, x, strlen(x));
stu__flow = FLOW_error;
stu__flow_val = a;
return stua_nil;
}
double stua_number(stua_obj z)
{
return stu__tag(z) == stu__int_tag ? stu__int(z) : stu__float(z);
}
stua_obj stua_make_number(double d)
{
double e = floor(d);
if (e == d && e < (1 << 29) && e >= -(1 << 29))
return stu__makeint((int) e);
else
return stua_float((float) d);
}
stua_obj (*stua_overload)(int op, stua_obj a, stua_obj b, stua_obj c) = NULL;
static stua_obj stu__op(int op, stua_obj a, stua_obj b, stua_obj c)
{
stua_obj r = STUA_NO_VALUE;
if (op == '+') {
if (stu__checkt(STU___string, a) && stu__checkt(STU___string, b)) {
;// @TODO: string concatenation
} else if (stu__checkt(STU___function, a) && stu__checkt(STU___dict, b)) {
stu__func *f = (stu__func *) malloc(12);
assert(offsetof(stu__func, code)==12);
f->closure_source = a;
f->f.closure_data = b;
return stu__makeobj(STU___function, f, 16, 1);
}
}
if (stua_overload) r = stua_overload(op,a,b,c);
if (stu__flow != FLOW_error && r == STUA_NO_VALUE)
stua_error("Typecheck for operator %d", op), r=stua_nil;
return r;
}
#define STU__EVAL2(a,b) \
a = stu__eval(stu__f[n+1]); if (stu__flow) break; stua_pushroot(a); \
b = stu__eval(stu__f[n+2]); stua_poproot(); if (stu__flow) break;
#define STU__FB(op) \
STU__EVAL2(a,b) \
if (stu__tag(a) == stu__int_tag && stu__tag(b) == stu__int_tag) \
return ((a) op (b)); \
if (stu__number(a) && stu__number(b)) \
return stua_make_number(stua_number(a) op stua_number(b)); \
return stu__op(stu__f[n], a,b, stua_nil)
#define STU__F(op) \
STU__EVAL2(a,b) \
if (stu__number(a) && stu__number(b)) \
return stua_make_number(stua_number(a) op stua_number(b)); \
return stu__op(stu__f[n], a,b, stua_nil)
#define STU__I(op) \
STU__EVAL2(a,b) \
if (stu__tag(a) == stu__int_tag && stu__tag(b) == stu__int_tag) \
return stu__makeint(stu__int(a) op stu__int(b)); \
return stu__op(stu__f[n], a,b, stua_nil)
#define STU__C(op) \
STU__EVAL2(a,b) \
if (stu__number(a) && stu__number(b)) \
return (stua_number(a) op stua_number(b)) ? stua_true : stua_false; \
return stu__op(stu__f[n], a,b, stua_nil)
#define STU__CE(op) \
STU__EVAL2(a,b) \
return (a op b) ? stua_true : stua_false
static short *stu__f;
static stua_obj stu__f_obj;
static stua_dict *stu__c;
static stua_obj stu__funceval(stua_obj fo, stua_obj co);
static int stu__cond(stua_obj x)
{
if (stu__flow) return 0;
if (!stu__checkt(STU___boolean, x))
x = stu__op('!', x, stua_nil, stua_nil);
if (x == stua_true ) return 1;
if (x == stua_false) return 0;
stu__flow = FLOW_error;
return 0;
}
// had to manually eliminate tailcall recursion for debugging complex stuff
#define TAILCALL(x) n = (x); goto top;
static stua_obj stu__eval(int n)
{
top:
if (stu__flow >= FLOW_return) return stua_nil; // is this needed?
if (n < 0) return stu__const(stu__f)[n];
assert(n != 0 && n != 1);
switch (stu__f[n]) {
stua_obj a,b,c;
case ST_catch: a = stu__eval(stu__f[n+1]);
if (stu__flow == FLOW_error) { a=stu__flow_val; stu__flow = FLOW_normal; }
return a;
case ST_var: b = stu__eval(stu__f[n+2]); if (stu__flow) break;
stu__set(stu__c, stu__const(stu__f)[stu__f[n+1]], b);
return b;
case STU__seq: stu__eval(stu__f[n+1]); if (stu__flow) break;
TAILCALL(stu__f[n+2]);
case ST_if: if (!stu__cond(stu__eval(stu__f[n+1]))) return stua_nil;
TAILCALL(stu__f[n+2]);
case ST_else: a = stu__cond(stu__eval(stu__f[n+1]));
TAILCALL(stu__f[n + 2 + !a]);
#define STU__HANDLE_BREAK \
if (stu__flow >= FLOW_break) { \
if (stu__flow == FLOW_break) { \
a = stu__flow_val; \
stu__flow = FLOW_normal; \
stu__flow_val = stua_nil; \
return a; \
} \
return stua_nil; \
}
case ST_as: stu__eval(stu__f[n+3]);
STU__HANDLE_BREAK
// fallthrough!
case ST_while: a = stua_nil; stua_pushroot(a);
while (stu__cond(stu__eval(stu__f[n+1]))) {
stua_poproot();
a = stu__eval(stu__f[n+2]);
STU__HANDLE_BREAK
stu__flow = FLOW_normal; // clear 'continue' flag
stua_pushroot(a);
if (stu__f[n+3]) stu__eval(stu__f[n+3]);
STU__HANDLE_BREAK
stu__flow = FLOW_normal; // clear 'continue' flag
}
stua_poproot();
return a;
case ST_break: stu__flow = FLOW_break; stu__flow_val = stu__eval(stu__f[n+1]); break;
case ST_continue:stu__flow = FLOW_continue; break;
case ST_return: stu__flow = FLOW_return; stu__flow_val = stu__eval(stu__f[n+1]); break;
case ST__frame: return stu__f_obj;
case '[': STU__EVAL2(a,b);
if (stu__checkt(STU___dict, a))
return stu__get(stu__pd(a), b, stua_nil);
return stu__op(stu__f[n], a, b, stua_nil);
case '=': a = stu__eval(stu__f[n+2]); if (stu__flow) break;
n = stu__f[n+1];
if (stu__f[n] == ST_id) {
if (!stb_idict_update(stu__c, stu__const(stu__f)[stu__f[n+1]], a))
if (!stb_idict_update(stu__globaldict, stu__const(stu__f)[stu__f[n+1]], a))
return stua_error("Assignment to undefined variable");
} else if (stu__f[n] == '[') {
stua_pushroot(a);
b = stu__eval(stu__f[n+1]); if (stu__flow) { stua_poproot(); break; }
stua_pushroot(b);
c = stu__eval(stu__f[n+2]); stua_poproot(); stua_poproot();
if (stu__flow) break;
if (!stu__checkt(STU___dict, b)) return stua_nil;
stu__set(stu__pd(b), c, a);
} else {
return stu__op(stu__f[n], stu__eval(n), a, stua_nil);
}
return a;
case STU__defaultparm:
a = stu__eval(stu__f[n+2]);
stu__flow = FLOW_normal;
if (stb_idict_add(stu__c, stu__const(stu__f)[stu__f[n+1]], a))
stu__size_allocs += 8;
return stua_nil;
case ST_id: a = stu__get(stu__c, stu__const(stu__f)[stu__f[n+1]], STUA_NO_VALUE); // try local variable
return a != STUA_NO_VALUE // else try stu__compile_global_scope variable
? a : stu__get(stu__globaldict, stu__const(stu__f)[stu__f[n+1]], stua_nil);
case STU__negate:a = stu__eval(stu__f[n+1]); if (stu__flow) break;
return stu__isint(a) ? -a : stu__op(stu__f[n], a, stua_nil, stua_nil);
case '~': a = stu__eval(stu__f[n+1]); if (stu__flow) break;
return stu__isint(a) ? (~a)&~3 : stu__op(stu__f[n], a, stua_nil, stua_nil);
case '!': a = stu__eval(stu__f[n+1]); if (stu__flow) break;
a = stu__cond(a); if (stu__flow) break;
return a ? stua_true : stua_false;
case ST_eq: STU__CE(==); case ST_le: STU__C(<=); case '<': STU__C(<);
case ST_ne: STU__CE(!=); case ST_ge: STU__C(>=); case '>': STU__C(>);
case '+' : STU__FB(+); case '*': STU__F(*); case '&': STU__I(&); case ST_shl: STU__I(<<);
case '-' : STU__FB(-); case '/': STU__F(/); case '|': STU__I(|); case ST_shr: STU__I(>>);
case '%': STU__I(%); case '^': STU__I(^);
case ST_shru: STU__EVAL2(a,b);
if (stu__tag(a) == stu__int_tag && stu__tag(b) == stu__int_tag)
return stu__makeint((unsigned) stu__int(a) >> stu__int(b));
return stu__op(stu__f[n], a,b, stua_nil);
case ST_and: a = stu__eval(stu__f[n+1]); b = stu__cond(a); if (stu__flow) break;
return a ? stu__eval(stu__f[n+2]) : a;
case ST_or : a = stu__eval(stu__f[n+1]); b = stu__cond(a); if (stu__flow) break;
return a ? b : stu__eval(stu__f[n+2]);
case'(':case':': STU__EVAL2(a,b);
if (!stu__checkt(STU___function, a))
return stu__op(stu__f[n], a,b, stua_nil);
if (!stu__checkt(STU___dict, b))
return stua_nil;
if (stu__f[n] == ':')
b = stu__makeobj(STU___dict, stb_idict_copy(stu__pd(b)), stb_idict_memory_usage(stu__pd(b)), 0);
a = stu__funceval(a,b);
return a;
case '{' : {
stua_dict *d;
d = stb_idict_new_size(stu__f[n+1] > 40 ? 64 : 16);
if (d == NULL)
return stua_nil; // breakpoint fodder
c = stu__makeobj(STU___dict, d, 32, 1);
stua_pushroot(c);
a = stu__f[n+1];
for (b=0; b < a; ++b) {
stua_obj x = stua_pushroot(stu__eval(stu__f[n+2 + b*2 + 0]));
stua_obj y = stu__eval(stu__f[n+2 + b*2 + 1]);
stua_poproot();
if (stu__flow) { stua_poproot(); return stua_nil; }
stu__set(d, x, y);
}
stua_poproot();
return c;
}
default: if (stu__f[n] < 0) return stu__const(stu__f)[stu__f[n]];
assert(0); /* NOTREACHED */ // internal error!
}
return stua_nil;
}
int stb__stua_nesting;
static stua_obj stu__funceval(stua_obj fo, stua_obj co)
{
stu__func *f = stu__pf(fo);
stua_dict *context = stu__pd(co);
int i,j;
stua_obj p;
short *tf = stu__f; // save previous function
stua_dict *tc = stu__c;
if (stu__flow == FLOW_error) return stua_nil;
assert(stu__flow == FLOW_normal);
stua_pushroot(fo);
stua_pushroot(co);
stu__consider_gc(stua_nil);
while ((stb_uint) f->closure_source > 4) {
// add data from closure to context
stua_dict *e = (stua_dict *) stu__pd(f->f.closure_data);
for (i=0; i < e->limit; ++i)
if (e->table[i].k != STB_IEMPTY && e->table[i].k != STB_IDEL)
if (stb_idict_add(context, e->table[i].k, e->table[i].v))
stu__size_allocs += 8;
// use add so if it's already defined, we don't override it; that way
// explicit parameters win over applied ones, and most recent applications
// win over previous ones
f = stu__pf(f->closure_source);
}
for (j=0, i=0; i < f->num_param; ++i)
// if it doesn't already exist, add it from the numbered parameters
if (stb_idict_add(context, f->param[i], stu__get(context, stu__int(j), stua_nil)))
++j;
// @TODO: if (stu__get(context, stu__int(f->num_param+1)) != STUA_NO_VALUE) // error: too many parameters
// @TODO: ditto too few parameters
if (f->closure_source == 4)
p = f->f.func(context);
else {
stu__f = f->code, stu__c = context;
stu__f_obj = co;
++stb__stua_nesting;
if (stu__f[1])
p = stu__eval(stu__f[1]);
else
p = stua_nil;
--stb__stua_nesting;
stu__f = tf, stu__c = tc; // restore previous function
if (stu__flow == FLOW_return) {
stu__flow = FLOW_normal;
p = stu__flow_val;
stu__flow_val = stua_nil;
}
}
stua_poproot();
stua_poproot();
return p;
}
// Parser
static int stu__tok;
static stua_obj stu__tokval;
static char *stu__curbuf, *stu__bufstart;
static stb_matcher *stu__lex_matcher;
static unsigned char stu__prec[ST__max_terminals], stu__end[ST__max_terminals];
static void stu__nexttoken(void)
{
int len;
retry:
stu__tok = stb_lex(stu__lex_matcher, stu__curbuf, &len);
if (stu__tok == 0)
return;
switch(stu__tok) {
case ST_white : stu__curbuf += len; goto retry;
case T__none : stu__tok = *stu__curbuf; break;
case ST_string: stu__tokval = make_string(stu__curbuf+1, len-2); break;
case ST_id : stu__tokval = make_string(stu__curbuf, len); break;
case ST_hex : stu__tokval = stu__makeint(strtol(stu__curbuf+2,NULL,16)); stu__tok = ST_number; break;
case ST_decimal: stu__tokval = stu__makeint(strtol(stu__curbuf ,NULL,10)); stu__tok = ST_number; break;
case ST_float : stu__tokval = stua_float((float) atof(stu__curbuf)) ; stu__tok = ST_number; break;
case ST_char : stu__tokval = stu__curbuf[2] == '\\' ? stu__curbuf[3] : stu__curbuf[2];
if (stu__curbuf[3] == 't') stu__tokval = '\t';
if (stu__curbuf[3] == 'n') stu__tokval = '\n';
if (stu__curbuf[3] == 'r') stu__tokval = '\r';
stu__tokval = stu__makeint(stu__tokval);
stu__tok = ST_number;
break;
}
stu__curbuf += len;
}
static struct { int stu__tok; char *regex; } stu__lexemes[] =
{
ST_white , "([ \t\n\r]|/\\*(.|\n)*\\*/|//[^\r\n]*([\r\n]|$))+",
ST_id , "[_a-zA-Z][_a-zA-Z0-9]*",
ST_hex , "0x[0-9a-fA-F]+",
ST_decimal, "[0-9]+[0-9]*",
ST_float , "[0-9]+\\.?[0-9]*([eE][-+]?[0-9]+)?",
ST_float , "\\.[0-9]+([eE][-+]?[0-9]+)?",
ST_char , "c'(\\\\.|[^\\'])'",
ST_string , "\"(\\\\.|[^\\\"\n\r])*\"",
ST_string , "\'(\\\\.|[^\\\'\n\r])*\'",
#define stua_key4(a,b,c,d) ST_##a, #a, ST_##b, #b, ST_##c, #c, ST_##d, #d,
stua_key4(if,then,else,elseif) stua_key4(while,do,for,in)
stua_key4(func,var,let,break) stua_key4(nil,true,false,end)
stua_key4(return,continue,as,repeat) stua_key4(_frame,catch,catch,catch)
ST_shl, "<<", ST_and, "&&", ST_eq, "==", ST_ge, ">=",
ST_shr, ">>", ST_or , "||", ST_ne, "!=", ST_le, "<=",
ST_shru,">>>", ST_into, "=>",
T__none, ".",
};
typedef struct
{
stua_obj *data; // constants being compiled
short *code; // code being compiled
stua_dict *locals;
short *non_local_refs;
} stu__comp_func;
static stu__comp_func stu__pfunc;
static stu__comp_func *func_stack = NULL;
static void stu__push_func_comp(void)
{
stb_arr_push(func_stack, stu__pfunc);
stu__pfunc.data = NULL;
stu__pfunc.code = NULL;
stu__pfunc.locals = stb_idict_new_size(16);
stu__pfunc.non_local_refs = NULL;
stb_arr_push(stu__pfunc.code, 0); // number of data items
stb_arr_push(stu__pfunc.code, 1); // starting execution address
}
static void stu__pop_func_comp(void)
{
stb_arr_free(stu__pfunc.code);
stb_arr_free(stu__pfunc.data);
stb_idict_destroy(stu__pfunc.locals);
stb_arr_free(stu__pfunc.non_local_refs);
stu__pfunc = stb_arr_pop(func_stack);
}
// if an id is a reference to an outer lexical scope, this
// function returns the "name" of it, and updates the stack
// structures to make sure the names are propagated in.
static int stu__nonlocal_id(stua_obj var_obj)
{
stua_obj dummy, var = var_obj;
int i, n = stb_arr_len(func_stack), j,k;
if (stb_idict_get_flag(stu__pfunc.locals, var, &dummy)) return 0;
for (i=n-1; i > 1; --i) {
if (stb_idict_get_flag(func_stack[i].locals, var, &dummy))
break;
}
if (i <= 1) return 0; // stu__compile_global_scope
j = i; // need to access variable from j'th frame
for (i=0; i < stb_arr_len(stu__pfunc.non_local_refs); ++i)
if (stu__pfunc.non_local_refs[i] == j) return j-n;
stb_arr_push(stu__pfunc.non_local_refs, j-n);
// now make sure all the parents propagate it down
for (k=n-1; k > 1; --k) {
if (j-k >= 0) return j-n; // comes direct from this parent
for(i=0; i < stb_arr_len(func_stack[k].non_local_refs); ++i)
if (func_stack[k].non_local_refs[i] == j-k)
return j-n;
stb_arr_push(func_stack[k].non_local_refs, j-k);
}
assert (k != 1);
return j-n;
}
static int stu__off(void) { return stb_arr_len(stu__pfunc.code); }
static void stu__cc(int a)
{
assert(a >= -2000 && a < 5000);
stb_arr_push(stu__pfunc.code, a);
}
static int stu__cc1(int a) { stu__cc(a); return stu__off()-1; }
static int stu__cc2(int a, int b) { stu__cc(a); stu__cc(b); return stu__off()-2; }
static int stu__cc3(int a, int b, int c) {
if (a == '=') assert(c != 0);
stu__cc(a); stu__cc(b); stu__cc(c); return stu__off()-3; }
static int stu__cc4(int a, int b, int c, int d) { stu__cc(a); stu__cc(b); stu__cc(c); stu__cc(d); return stu__off()-4; }
static int stu__cdv(stua_obj p)
{
int i;
assert(p != STUA_NO_VALUE);
for (i=0; i < stb_arr_len(stu__pfunc.data); ++i)
if (stu__pfunc.data[i] == p)
break;
if (i == stb_arr_len(stu__pfunc.data))
stb_arr_push(stu__pfunc.data, p);
return ~i;
}
static int stu__cdt(void)
{
int z = stu__cdv(stu__tokval);
stu__nexttoken();
return z;
}
static int stu__seq(int a, int b)
{
return !a ? b : !b ? a : stu__cc3(STU__seq, a,b);
}
static char stu__comp_err_str[1024];
static int stu__comp_err_line;
static int stu__err(char *str, ...)
{
va_list v;
char *s = stu__bufstart;
stu__comp_err_line = 1;
while (s < stu__curbuf) {
if (s[0] == '\n' || s[0] == '\r') {
if (s[0]+s[1] == '\n' + '\r') ++s;
++stu__comp_err_line;
}
++s;
}
va_start(v, str);
vsprintf(stu__comp_err_str, str, v);
va_end(v);
return 0;
}
static int stu__accept(int p)
{
if (stu__tok != p) return 0;
stu__nexttoken();
return 1;
}
static int stu__demand(int p)
{
if (stu__accept(p)) return 1;
return stu__err("Didn't find expected stu__tok");
}
static int stu__demandv(int p, stua_obj *val)
{
if (stu__tok == p || p==0) {
*val = stu__tokval;
stu__nexttoken();
return 1;
} else
return 0;
}
static int stu__expr(int p);
int stu__nexpr(int p) { stu__nexttoken(); return stu__expr(p); }
static int stu__statements(int once, int as);
static int stu__parse_if(void) // parse both ST_if and ST_elseif
{
int b,c,a;
a = stu__nexpr(1); if (!a) return 0;
if (!stu__demand(ST_then)) return stu__err("expecting THEN");
b = stu__statements(0,0); if (!b) return 0;
if (b == 1) b = -1;
if (stu__tok == ST_elseif) {
return stu__parse_if();
} else if (stu__accept(ST_else)) {
c = stu__statements(0,0); if (!c) return 0;
if (!stu__demand(ST_end)) return stu__err("expecting END after else clause");
return stu__cc4(ST_else, a, b, c);
} else {
if (!stu__demand(ST_end)) return stu__err("expecting END in if statement");
return stu__cc3(ST_if, a, b);
}
}
int stu__varinit(int z, int in_globals)
{
int a,b;
stu__nexttoken();
while (stu__demandv(ST_id, &b)) {
if (!stb_idict_add(stu__pfunc.locals, b, 1))
if (!in_globals) return stu__err("Redefined variable %s.", stu__pw(b)->ptr);
if (stu__accept('=')) {
a = stu__expr(1); if (!a) return 0;
} else
a = stu__cdv(stua_nil);
z = stu__seq(z, stu__cc3(ST_var, stu__cdv(b), a));
if (!stu__accept(',')) break;
}
return z;
}
static int stu__compile_unary(int z, int outparm, int require_inparm)
{
int op = stu__tok, a, b;
stu__nexttoken();
if (outparm) {
if (require_inparm || (stu__tok && stu__tok != ST_end && stu__tok != ST_else && stu__tok != ST_elseif && stu__tok !=';')) {
a = stu__expr(1); if (!a) return 0;
} else
a = stu__cdv(stua_nil);
b = stu__cc2(op, a);
} else
b = stu__cc1(op);
return stu__seq(z,b);
}
static int stu__assign(void)
{
int z;
stu__accept(ST_let);
z = stu__expr(1); if (!z) return 0;
if (stu__accept('=')) {
int y,p = (z >= 0 ? stu__pfunc.code[z] : 0);
if (z < 0 || (p != ST_id && p != '[')) return stu__err("Invalid lvalue in assignment");
y = stu__assign(); if (!y) return 0;
z = stu__cc3('=', z, y);
}
return z;
}
static int stu__statements(int once, int stop_while)
{
int a,b, c, z=0;
for(;;) {
switch (stu__tok) {
case ST_if : a = stu__parse_if(); if (!a) return 0;
z = stu__seq(z, a);
break;
case ST_while : if (stop_while) return (z ? z:1);
a = stu__nexpr(1); if (!a) return 0;
if (stu__accept(ST_as)) c = stu__statements(0,0); else c = 0;
if (!stu__demand(ST_do)) return stu__err("expecting DO");
b = stu__statements(0,0); if (!b) return 0;
if (!stu__demand(ST_end)) return stu__err("expecting END");
if (b == 1) b = -1;
z = stu__seq(z, stu__cc4(ST_while, a, b, c));
break;
case ST_repeat : stu__nexttoken();
c = stu__statements(0,1); if (!c) return 0;
if (!stu__demand(ST_while)) return stu__err("expecting WHILE");
a = stu__expr(1); if (!a) return 0;
if (!stu__demand(ST_do)) return stu__err("expecting DO");
b = stu__statements(0,0); if (!b) return 0;
if (!stu__demand(ST_end)) return stu__err("expecting END");
if (b == 1) b = -1;
z = stu__seq(z, stu__cc4(ST_as, a, b, c));
break;
case ST_catch : a = stu__nexpr(1); if (!a) return 0;
z = stu__seq(z, stu__cc2(ST_catch, a));
break;
case ST_var : z = stu__varinit(z,0); break;
case ST_return : z = stu__compile_unary(z,1,1); break;
case ST_continue:z = stu__compile_unary(z,0,0); break;
case ST_break : z = stu__compile_unary(z,1,0); break;
case ST_into : if (z == 0 && !once) return stu__err("=> cannot be first statement in block");
a = stu__nexpr(99);
b = (a >= 0? stu__pfunc.code[a] : 0);
if (a < 0 || (b != ST_id && b != '[')) return stu__err("Invalid lvalue on right side of =>");
z = stu__cc3('=', a, z);
break;
default : if (stu__end[stu__tok]) return once ? 0 : (z ? z:1);
a = stu__assign(); if (!a) return 0;
stu__accept(';');
if (stu__tok && !stu__end[stu__tok]) {
if (a < 0)
return stu__err("Constant has no effect");
if (stu__pfunc.code[a] != '(' && stu__pfunc.code[a] != '=')
return stu__err("Expression has no effect");
}
z = stu__seq(z, a);
break;
}
if (!z) return 0;
stu__accept(';');
if (once && stu__tok != ST_into) return z;
}
}
static int stu__postexpr(int z, int p);
static int stu__dictdef(int end, int *count)
{
int z,n=0,i,flags=0;
short *dict=NULL;
stu__nexttoken();
while (stu__tok != end) {
if (stu__tok == ST_id) {
stua_obj id = stu__tokval;
stu__nexttoken();
if (stu__tok == '=') {
flags |= 1;
stb_arr_push(dict, stu__cdv(id));
z = stu__nexpr(1); if (!z) return 0;
} else {
z = stu__cc2(ST_id, stu__cdv(id));
z = stu__postexpr(z,1); if (!z) return 0;
flags |= 2;
stb_arr_push(dict, stu__cdv(stu__makeint(n++)));
}
} else {
z = stu__expr(1); if (!z) return 0;
flags |= 2;
stb_arr_push(dict, stu__cdv(stu__makeint(n++)));
}
if (end != ')' && flags == 3) { z=stu__err("can't mix initialized and uninitialized defs"); goto done;}
stb_arr_push(dict, z);
if (!stu__accept(',')) break;
}
if (!stu__demand(end))
return stu__err(end == ')' ? "Expecting ) at end of function call"
: "Expecting } at end of dictionary definition");
z = stu__cc2('{', stb_arr_len(dict)/2);
for (i=0; i < stb_arr_len(dict); ++i)
stu__cc(dict[i]);
if (count) *count = n;
done:
stb_arr_free(dict);
return z;
}
static int stu__comp_id(void)
{
int z,d;
d = stu__nonlocal_id(stu__tokval);
if (d == 0)
return z = stu__cc2(ST_id, stu__cdt());
// access a non-local frame by naming it with the appropriate int
assert(d < 0);
z = stu__cdv(d); // relative frame # is the 'variable' in our local frame
z = stu__cc2(ST_id, z); // now access that dictionary
return stu__cc3('[', z, stu__cdt()); // now access the variable from that dir
}
static stua_obj stu__funcdef(stua_obj *id, stua_obj *func);
static int stu__expr(int p)
{
int z;
// unary
switch (stu__tok) {
case ST_number: z = stu__cdt(); break;
case ST_string: z = stu__cdt(); break; // @TODO - string concatenation like C
case ST_id : z = stu__comp_id(); break;
case ST__frame: z = stu__cc1(ST__frame); stu__nexttoken(); break;
case ST_func : z = stu__funcdef(NULL,NULL); break;
case ST_if : z = stu__parse_if(); break;
case ST_nil : z = stu__cdv(stua_nil); stu__nexttoken(); break;
case ST_true : z = stu__cdv(stua_true); stu__nexttoken(); break;
case ST_false : z = stu__cdv(stua_false); stu__nexttoken(); break;
case '-' : z = stu__nexpr(99); if (z) z=stu__cc2(STU__negate,z); else return z; break;
case '!' : z = stu__nexpr(99); if (z) z=stu__cc2('!',z); else return z; break;
case '~' : z = stu__nexpr(99); if (z) z=stu__cc2('~',z); else return z; break;
case '{' : z = stu__dictdef('}', NULL); break;
default : return stu__err("Unexpected token");
case '(' : stu__nexttoken(); z = stu__statements(0,0); if (!stu__demand(')')) return stu__err("Expecting )");
}
return stu__postexpr(z,p);
}
static int stu__postexpr(int z, int p)
{
int q;
// postfix
while (stu__tok == '(' || stu__tok == '[' || stu__tok == '.') {
if (stu__accept('.')) {
// MUST be followed by a plain identifier! use [] for other stuff
if (stu__tok != ST_id) return stu__err("Must follow . with plain name; try [] instead");
z = stu__cc3('[', z, stu__cdv(stu__tokval));
stu__nexttoken();
} else if (stu__accept('[')) {
while (stu__tok != ']') {
int r = stu__expr(1); if (!r) return 0;
z = stu__cc3('[', z, r);
if (!stu__accept(',')) break;
}
if (!stu__demand(']')) return stu__err("Expecting ]");
} else {
int n, p = stu__dictdef(')', &n); if (!p) return 0;
#if 0 // this is incorrect!
if (z > 0 && stu__pfunc.code[z] == ST_id) {
stua_obj q = stu__get(stu__globaldict, stu__pfunc.data[-stu__pfunc.code[z+1]-1], stua_nil);
if (stu__checkt(STU___function, q))
if ((stu__pf(q))->num_param != n)
return stu__err("Incorrect number of parameters");
}
#endif
z = stu__cc3('(', z, p);
}
}
// binop - this implementation taken from lcc
for (q=stu__prec[stu__tok]; q >= p; --q) {
while (stu__prec[stu__tok] == q) {
int o = stu__tok, y = stu__nexpr(p+1); if (!y) return 0;
z = stu__cc3(o,z,y);
}
}
return z;
}
static stua_obj stu__finish_func(stua_obj *param, int start)
{
int n, size;
stu__func *f = (stu__func *) malloc(sizeof(*f));
f->closure_source = 0;
f->num_param = stb_arr_len(param);
f->param = (int *) stb_copy(param, f->num_param * sizeof(*f->param));
size = stb_arr_storage(stu__pfunc.code) + stb_arr_storage(stu__pfunc.data) + sizeof(*f) + 8;
f->f.store = malloc(stb_arr_storage(stu__pfunc.code) + stb_arr_storage(stu__pfunc.data));
f->code = (short *) ((char *) f->f.store + stb_arr_storage(stu__pfunc.data));
memcpy(f->code, stu__pfunc.code, stb_arr_storage(stu__pfunc.code));
f->code[1] = start;
f->code[0] = stb_arr_len(stu__pfunc.data);
for (n=0; n < f->code[0]; ++n)
((stua_obj *) f->code)[-1-n] = stu__pfunc.data[n];
return stu__makeobj(STU___function, f, size, 0);
}
static int stu__funcdef(stua_obj *id, stua_obj *result)
{
int n,z=0,i,q;
stua_obj *param = NULL;
short *nonlocal;
stua_obj v,f=stua_nil;
assert(stu__tok == ST_func);
stu__nexttoken();
if (id) {
if (!stu__demandv(ST_id, id)) return stu__err("Expecting function name");
} else
stu__accept(ST_id);
if (!stu__demand('(')) return stu__err("Expecting ( for function parameter");
stu__push_func_comp();
while (stu__tok != ')') {
if (!stu__demandv(ST_id, &v)) { z=stu__err("Expecting parameter name"); goto done; }
stb_idict_add(stu__pfunc.locals, v, 1);
if (stu__tok == '=') {
n = stu__nexpr(1); if (!n) { z=0; goto done; }
z = stu__seq(z, stu__cc3(STU__defaultparm, stu__cdv(v), n));
} else
stb_arr_push(param, v);
if (!stu__accept(',')) break;
}
if (!stu__demand(')')) { z=stu__err("Expecting ) at end of parameter list"); goto done; }
n = stu__statements(0,0); if (!n) { z=0; goto done; }
if (!stu__demand(ST_end)) { z=stu__err("Expecting END at end of function"); goto done; }
if (n == 1) n = 0;
n = stu__seq(z,n);
f = stu__finish_func(param, n);
if (result) { *result = f; z=1; stu__pop_func_comp(); }
else {
nonlocal = stu__pfunc.non_local_refs;
stu__pfunc.non_local_refs = NULL;
stu__pop_func_comp();
z = stu__cdv(f);
if (nonlocal) { // build a closure with references to the needed frames
short *initcode = NULL;
for (i=0; i < stb_arr_len(nonlocal); ++i) {
int k = nonlocal[i], p;
stb_arr_push(initcode, stu__cdv(k));
if (k == -1) p = stu__cc1(ST__frame);
else { p = stu__cdv(stu__makeint(k+1)); p = stu__cc2(ST_id, p); }
stb_arr_push(initcode, p);
}
q = stu__cc2('{', stb_arr_len(nonlocal));
for (i=0; i < stb_arr_len(initcode); ++i)
stu__cc(initcode[i]);
z = stu__cc3('+', z, q);
stb_arr_free(initcode);
}
stb_arr_free(nonlocal);
}
done:
stb_arr_free(param);
if (!z) stu__pop_func_comp();
return z;
}
static int stu__compile_global_scope(void)
{
stua_obj o;
int z=0;
stu__push_func_comp();
while (stu__tok != 0) {
if (stu__tok == ST_func) {
stua_obj id, f;
if (!stu__funcdef(&id,&f))
goto error;
stu__set(stu__globaldict, id, f);
} else if (stu__tok == ST_var) {
z = stu__varinit(z,1); if (!z) goto error;
} else {
int y = stu__statements(1,0); if (!y) goto error;
z = stu__seq(z,y);
}
stu__accept(';');
}
o = stu__finish_func(NULL, z);
stu__pop_func_comp();
o = stu__funceval(o, stua_globals); // initialize stu__globaldict
if (stu__flow == FLOW_error)
printf("Error: %s\n", ((stu__wrapper *) stu__ptr(stu__flow_val))->ptr);
return 1;
error:
stu__pop_func_comp();
return 0;
}
stua_obj stu__myprint(stua_dict *context)
{
stua_obj x = stu__get(context, stua_string("x"), stua_nil);
if ((x & 1) == stu__float_tag) printf("%f", stu__getfloat(x));
else if (stu__tag(x) == stu__int_tag) printf("%d", stu__int(x));
else {
stu__wrapper *s = stu__pw(x);
if (s->type == STU___string || s->type == STU___error)
printf("%s", s->ptr);
else if (s->type == STU___dict) printf("{{dictionary}}");
else if (s->type == STU___function) printf("[[function]]");
else
printf("[[ERROR:%s]]", s->ptr);
}
return x;
}
void stua_init(void)
{
if (!stu__globaldict) {
int i;
stua_obj s;
stu__func *f;
stu__prec[ST_and] = stu__prec[ST_or] = 1;
stu__prec[ST_eq ] = stu__prec[ST_ne] = stu__prec[ST_le] =
stu__prec[ST_ge] = stu__prec['>' ] = stu__prec['<'] = 2;
stu__prec[':'] = 3;
stu__prec['&'] = stu__prec['|'] = stu__prec['^'] = 4;
stu__prec['+'] = stu__prec['-'] = 5;
stu__prec['*'] = stu__prec['/'] = stu__prec['%'] =
stu__prec[ST_shl]= stu__prec[ST_shr]= stu__prec[ST_shru]= 6;
stu__end[')'] = stu__end[ST_end] = stu__end[ST_else] = 1;
stu__end[ST_do] = stu__end[ST_elseif] = 1;
stu__float_init();
stu__lex_matcher = stb_lex_matcher();
for (i=0; i < sizeof(stu__lexemes)/sizeof(stu__lexemes[0]); ++i)
stb_lex_item(stu__lex_matcher, stu__lexemes[i].regex, stu__lexemes[i].stu__tok);
stu__globaldict = stb_idict_new_size(64);
stua_globals = stu__makeobj(STU___dict, stu__globaldict, 0,0);
stu__strings = stb_sdict_new(0);
stu__curbuf = stu__bufstart = "func _print(x) end\n"
"func print()\n var x=0 while _frame[x] != nil as x=x+1 do _print(_frame[x]) end end\n";
stu__nexttoken();
if (!stu__compile_global_scope())
printf("Compile error in line %d: %s\n", stu__comp_err_line, stu__comp_err_str);
s = stu__get(stu__globaldict, stua_string("_print"), stua_nil);
if (stu__tag(s) == stu__ptr_tag && stu__ptr(s)->type == STU___function) {
f = stu__pf(s);
free(f->f.store);
f->closure_source = 4;
f->f.func = stu__myprint;
f->code = NULL;
}
}
}
void stua_uninit(void)
{
if (stu__globaldict) {
stb_idict_remove_all(stu__globaldict);
stb_arr_setlen(stu__gc_root_stack, 0);
stua_gc(1);
stb_idict_destroy(stu__globaldict);
stb_sdict_delete(stu__strings);
stb_matcher_free(stu__lex_matcher);
stb_arr_free(stu__gc_ptrlist);
stb_arr_free(func_stack);
stb_arr_free(stu__gc_root_stack);
stu__globaldict = NULL;
}
}
void stua_run_script(char *s)
{
stua_init();
stu__curbuf = stu__bufstart = s;
stu__nexttoken();
stu__flow = FLOW_normal;
if (!stu__compile_global_scope())
printf("Compile error in line %d: %s\n", stu__comp_err_line, stu__comp_err_str);
stua_gc(1);
}
#endif // STB_DEFINE
#endif // STB_STUA
#undef STB_EXTERN
#endif // STB_INCLUDE_STB_H
/*
------------------------------------------------------------------------------
This software is available under 2 licenses -- choose whichever you prefer.
------------------------------------------------------------------------------
ALTERNATIVE A - MIT License
Copyright (c) 2017 Sean Barrett
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
------------------------------------------------------------------------------
ALTERNATIVE B - Public Domain (www.unlicense.org)
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
software, either in source code form or as a compiled binary, for any purpose,
commercial or non-commercial, and by any means.
In jurisdictions that recognize copyright laws, the author or authors of this
software dedicate any and all copyright interest in the software to the public
domain. We make this dedication for the benefit of the public at large and to
the detriment of our heirs and successors. We intend this dedication to be an
overt act of relinquishment in perpetuity of all present and future rights to
this software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
------------------------------------------------------------------------------
*/
| h |
oneAPI-samples | data/projects/oneAPI-samples/DirectProgramming/C++SYCL_FPGA/Tutorials/DesignPatterns/compute_units/src/compute_units.cpp | #include <sycl/sycl.hpp>
#include <sycl/ext/intel/fpga_extensions.hpp>
#include <iostream>
#include "exception_handler.hpp"
#include "compute_units.hpp"
#include "pipe_utils.hpp" // Included from DirectProgramming/C++SYCL_FPGA/include/
using namespace sycl;
constexpr float kTestData = 555;
constexpr size_t kEngines = 5;
using Pipes = fpga_tools::PipeArray<class MyPipe, float, 1, kEngines + 1>;
// Forward declare the kernel names in the global scope.
// This FPGA best practice reduces name mangling in the optimization reports.
class Source;
class Sink;
template <std::size_t ID> class ChainComputeUnit;
// Write the data into the chain
void SourceKernel(queue &q, float data) {
q.single_task<Source>([=] { Pipes::PipeAt<0>::write(data); });
}
// Get the data out of the chain and return it to the host
void SinkKernel(queue &q, float &out_data) {
// The verbose buffer syntax is necessary here,
// since out_data is just a single scalar value
// and its size can not be inferred automatically
buffer<float, 1> out_buf(&out_data, 1);
q.submit([&](handler &h) {
accessor out_accessor(out_buf, h, write_only, no_init);
h.single_task<Sink>([=] {
out_accessor[0] = Pipes::PipeAt<kEngines>::read();
});
});
}
int main() {
#if FPGA_SIMULATOR
auto selector = sycl::ext::intel::fpga_simulator_selector_v;
#elif FPGA_HARDWARE
auto selector = sycl::ext::intel::fpga_selector_v;
#else // #if FPGA_EMULATOR
auto selector = sycl::ext::intel::fpga_emulator_selector_v;
#endif
float out_data = 0;
try {
queue q(selector, fpga_tools::exception_handler);
sycl::device device = q.get_device();
std::cout << "Running on device: "
<< device.get_info<sycl::info::device::name>().c_str()
<< std::endl;
// Enqueue the Source kernel
SourceKernel(q, kTestData);
// Enqueue the chain of kEngines compute units
// Compute unit must take a single argument, its ID
SubmitComputeUnits<kEngines, ChainComputeUnit>(q, [=](auto ID) {
auto f = Pipes::PipeAt<ID>::read();
// Pass the data to the next compute unit in the chain
// The compute unit with ID k reads from pipe k and writes to pipe
// k + 1
Pipes::PipeAt<ID + 1>::write(f);
});
// Enqueue the Sink kernel
SinkKernel(q, out_data);
} catch (sycl::exception const &e) {
// Catches exceptions in the host code
std::cerr << "Caught a SYCL host exception:\n" << e.what() << "\n";
// Most likely the runtime couldn't find FPGA hardware!
if (e.code().value() == CL_DEVICE_NOT_FOUND) {
std::cerr << "If you are targeting an FPGA, please ensure that your "
"system has a correctly configured FPGA board.\n";
std::cerr << "Run sys_check in the oneAPI root directory to verify.\n";
std::cerr << "If you are targeting the FPGA emulator, compile with "
"-DFPGA_EMULATOR.\n";
}
std::terminate();
}
// Verify result
if (out_data != kTestData) {
std::cout << "FAILED: The results are incorrect\n";
std::cout << "Expected: " << kTestData << " Got: " << out_data << "\n";
return 1;
}
std::cout << "PASSED: The results are correct\n";
return 0;
}
| cpp |
oneAPI-samples | data/projects/oneAPI-samples/DirectProgramming/C++SYCL_FPGA/Tutorials/DesignPatterns/compute_units/src/compute_units.hpp | #include <sycl/sycl.hpp>
#include <utility>
namespace {
template <typename Func, template <std::size_t> typename Name,
std::size_t Index>
class SubmitOneComputeUnit {
public:
SubmitOneComputeUnit(Func &&f, sycl::queue &q) {
q.single_task<Name<Index>>([=] {
static_assert(
std::is_invocable_v<Func, std::integral_constant<std::size_t, Index>>,
"The callable Func passed to SubmitComputeUnits must take a single "
"argument of type auto");
f(std::integral_constant<std::size_t, Index>());
});
}
};
template <template <std::size_t> typename Name, typename Func,
std::size_t... Indices>
inline constexpr void ComputeUnitUnroller(sycl::queue &q, Func &&f,
std::index_sequence<Indices...>) {
(SubmitOneComputeUnit<Func, Name, Indices>(f, q), ...); // fold expression
}
} // namespace
template <std::size_t N, // Number of compute units
template <std::size_t ID> typename Name, // Name for the compute units
typename Func> // Callable defining compute
// units' functionality
// Func must take a single argument. This argument is the compute unit's ID.
// The compute unit ID is a constexpr, and it can be used to specialize
// the kernel's functionality.
// Note: the type of Func's single argument must be 'auto', because Func
// will be called with various indices (i.e., the ID for each compute unit)
constexpr void SubmitComputeUnits(sycl::queue &q, Func &&f) {
std::make_index_sequence<N> indices;
ComputeUnitUnroller<Name>(q, f, indices);
}
| hpp |
oneAPI-samples | data/projects/oneAPI-samples/DirectProgramming/C++SYCL_FPGA/Tutorials/DesignPatterns/simple_host_streaming/src/simple_host_streaming.cpp | #include <algorithm>
#include <array>
#include <chrono>
#include <iomanip>
#include <functional>
#include <numeric>
#include <queue>
#include <random>
#include <string>
#include <type_traits>
#include <utility>
#include <sycl/sycl.hpp>
#include <sycl/ext/intel/fpga_extensions.hpp>
#include "exception_handler.hpp"
#include "single_kernel.hpp"
#include "multi_kernel.hpp"
using namespace sycl;
using namespace std::chrono;
// data types and constants
// NOTE: this tutorial assumes you are using a sycl::vec datatype. Therefore,
// 'Type' can only be changed to a different vector datatype (e.g. int16,
// long8, etc...)
using Type = long8;
///////////////////////////////////////////////////////////////////////////////
// forward declaration of the functions in this file
// the function definitions are all below the main() function in this file
template<typename T>
void DoWorkOffload(queue& q, T* in, T* out, size_t total_count,
size_t iterations);
template<typename T>
void DoWorkSingleKernel(queue& q, T* in, T* out,
size_t chunks, size_t chunk_count, size_t total_count,
size_t inflight_kernels, size_t iterations);
template <typename T>
void DoWorkMultiKernel(queue& q, T* in, T* out,
size_t chunks, size_t chunk_count, size_t total_count,
size_t inflight_kernels, size_t iterations);
template<typename T>
void PrintPerformanceInfo(std::string print_prefix, size_t count,
std::vector<double>& latency_ms,
std::vector<double>& process_time_ms);
///////////////////////////////////////////////////////////////////////////////
int main(int argc, char* argv[]) {
// default values
#if defined(FPGA_EMULATOR)
size_t chunks = 1 << 4; // 16
size_t chunk_count = 1 << 8; // 256
size_t iterations = 2;
#elif defined(FPGA_SIMULATOR)
size_t chunks = 1 << 3; // 8
size_t chunk_count = 1 << 7; // 128
size_t iterations = 2;
#else
size_t chunks = 1 << 9; // 512
size_t chunk_count = 1 << 15; // 32768
size_t iterations = 5;
#endif
// This is the number of kernels we will have in the queue at a single time.
// If this number is set too low (e.g. 1) then we don't take advantage of
// fast kernel relaunch (see the README). If this number is set to high,
// then the first kernel launched finishes before we are done launching all
// the kernels and therefore throughput is decreased.
size_t inflight_kernels = 2;
// parse the command line arguments
for (int i = 1; i < argc; i++) {
std::string arg(argv[i]);
if (arg == "--help" || arg == "-h") {
std::cout << "USAGE: "
<< "./simple_host_streaming "
<< "[--chunks=<int>] "
<< "[--chunk_count=<int>] "
<< "[--inflight_kernels=<int>] "
<< "[--iterations=<int>]\n";
return 0;
} else {
std::string str_after_equals = arg.substr(arg.find("=") + 1);
if (arg.find("--chunks=") == 0) {
chunks = atoi(str_after_equals.c_str());
} else if (arg.find("--chunk_count=") == 0) {
chunk_count = atoi(str_after_equals.c_str());
} else if (arg.find("--inflight_kernels=") == 0) {
inflight_kernels = atoi(str_after_equals.c_str());
} else if (arg.find("--iterations=") == 0) {
iterations = std::max(2, atoi(str_after_equals.c_str()) + 1);
} else {
std::cout << "WARNING: ignoring unknown argument '" << arg << "'\n";
}
}
}
// check the chunks
if (chunks <= 0) {
std::cerr << "ERROR: 'chunks' must be greater than 0\n";
std::terminate();
}
// check the chunk size
if (chunk_count <= 0) {
std::cerr << "ERROR: 'chunk_count' must be greater than 0\n";
std::terminate();
}
// check inflight_kernels
if (inflight_kernels <= 0) {
std::cerr << "ERROR: 'inflight_kernels' must be positive\n";
std::terminate();
}
// check the number of iterations
if (iterations <= 0) {
std::cerr << "ERROR: 'iterations' must be positive\n";
std::terminate();
}
// compute the total number of elements
size_t total_count = chunks * chunk_count;
std::cout << "# Chunks: " << chunks << "\n";
std::cout << "Chunk count: " << chunk_count << "\n";
std::cout << "Total count: " << total_count << "\n";
std::cout << "Iterations: " << iterations-1 << "\n";
std::cout << "\n";
bool passed = true;
try {
// device selector
#if FPGA_SIMULATOR
auto selector = sycl::ext::intel::fpga_simulator_selector_v;
#elif FPGA_HARDWARE
auto selector = sycl::ext::intel::fpga_selector_v;
#else // #if FPGA_EMULATOR
auto selector = sycl::ext::intel::fpga_emulator_selector_v;
#endif
// queue properties to enable profiling
property_list prop_list { property::queue::enable_profiling() };
// create the device queue
queue q(selector, fpga_tools::exception_handler, prop_list);
// make sure the device supports USM host allocations
auto device = q.get_device();
if (!device.get_info<info::device::usm_host_allocations>()) {
std::cerr << "ERROR: The selected device does not support USM host"
<< " allocations\n";
std::terminate();
}
std::cout << "Running on device: "
<< device.get_info<sycl::info::device::name>().c_str()
<< std::endl;
// the USM input and output data
Type *in, *out;
if ((in = malloc_host<Type>(total_count, q)) == nullptr) {
std::cerr << "ERROR: could not allocate space for 'in'\n";
std::terminate();
}
if ((out = malloc_host<Type>(total_count, q)) == nullptr) {
std::cerr << "ERROR: could not allocate space for 'out'\n";
std::terminate();
}
// generate the random input data
// NOTE: by generating all of the data ahead of time, we are essentially
// assuming that the producer of data (producing data for the FPGA to
// consume) has infinite bandwidth. However, if the producer of data cannot
// produce data faster than our FPGA can consume it, the CPU producer will
// bottleneck the total throughput of the design.
std::generate_n(in, total_count, [] { return Type(rand() % 100); });
// a lambda function to validate the results
auto validate_results = [&] {
for (size_t i = 0; i < total_count; i++) {
auto comp = (in[i] == out[i]);
for (auto j = 0; j < comp.size(); j++) {
if (!comp[j]) {
std::cerr << "ERROR: Values do not match, "
<< "in[" << i << "][" << j << "]:" << in[i][j]
<< " != out[" << i << "]["<< j << "]:" << out[i][j]
<< "\n";
return false;
}
}
}
return true;
};
////////////////////////////////////////////////////////////////////////////
// run the offload version, which is NOT optimized for latency at all
std::cout << "Running the basic offload kernel\n";
DoWorkOffload(q, in, out, total_count, iterations);
// validate the results using the lambda
passed &= validate_results();
std::cout << "\n";
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
// run the optimized (for latency) version that uses fast kernel relaunch
// by keeping at most 'inflight_kernels' in the SYCL queue at a time
std::cout << "Running the latency optimized single-kernel design\n";
DoWorkSingleKernel(q, in, out, chunks, chunk_count, total_count,
inflight_kernels, iterations);
// validate the results using the lambda
passed &= validate_results();
std::cout << "\n";
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
// run the optimized (for latency) version with multiple kernels that uses
// fast kernel relaunch by keeping at most 'inflight_kernels' in the SYCL
// queue at a time
std::cout << "Running the latency optimized multi-kernel design\n";
DoWorkMultiKernel(q, in, out, chunks, chunk_count, total_count,
inflight_kernels, iterations);
// validate the results using the lambda
passed &= validate_results();
std::cout << "\n";
////////////////////////////////////////////////////////////////////////////
// free the USM pointers
sycl::free(in, q);
sycl::free(out, q);
} catch (exception const& e) {
// Catches exceptions in the host code
std::cerr << "Caught a SYCL host exception:\n" << e.what() << "\n";
// Most likely the runtime couldn't find FPGA hardware!
if (e.code().value() == CL_DEVICE_NOT_FOUND) {
std::cerr << "If you are targeting an FPGA, please ensure that your "
"system has a correctly configured FPGA board.\n";
std::cerr << "Run sys_check in the oneAPI root directory to verify.\n";
std::cerr << "If you are targeting the FPGA emulator, compile with "
"-DFPGA_EMULATOR.\n";
std::cerr << "If you are targeting the FPGA simulator, compile with "
"-DFPGA_SIMULATOR.\n";
}
std::terminate();
}
if(passed) {
std::cout << "PASSED\n";
return 0;
} else {
std::cout << "FAILED\n";
return 1;
}
}
// the basic offload kernel version (doesn't care about latency)
template<typename T>
void DoWorkOffload(queue& q, T* in, T* out, size_t total_count,
size_t iterations) {
// timing data
std::vector<double> latency_ms(iterations);
std::vector<double> process_time_ms(iterations);
for (size_t i = 0; i < iterations; i++) {
auto start = high_resolution_clock::now();
// submit single kernel for entire buffer
// this function is defined in 'single_kernel.hpp'
auto e = SubmitSingleWorker(q, in, out, total_count);
// wait on the kernel to finish
e.wait();
auto end = high_resolution_clock::now();
// compute latency and processing time
duration<double, std::milli> process_time = end - start;
// in offload designs, the processing time and latency are identical
// since the synchronization between the host and device is coarse grain
// (i.e. the synchronization happens once ALL the data has been processed).
latency_ms[i] = process_time.count();
process_time_ms[i] = process_time.count();
}
// compute and print timing information
PrintPerformanceInfo<T>("Offload",
total_count, latency_ms, process_time_ms);
}
// The single-kernel version of the design.
// This function optimizes for latency (while maintaining throughput) by
// breaking the computation into 'chunks' and launching kernels for each
// chunk. The synchronization of the kernel ending tells the host that the data
// for the given chunk is ready in the output buffer.
template <typename T>
void DoWorkSingleKernel(queue& q, T* in, T* out,
size_t chunks, size_t chunk_count, size_t total_count,
size_t inflight_kernels, size_t iterations) {
// timing data
std::vector<double> latency_ms(iterations);
std::vector<double> process_time_ms(iterations);
// count the number of chunks for which kernels have been started
size_t in_chunk = 0;
// count the number of chunks for which kernels have finished
size_t out_chunk = 0;
// use a queue to track the kernels in flight
// By queueing multiple kernels before waiting on the oldest to finish
// (inflight_kernels) we still have kernels in the SYCL queue and ready to
// launch while we call event.wait() on the oldest kernel in the queue.
// However, if we set 'inflight_kernels' too high, then the time to launch
// the first set of kernels will be longer than the time for the first kernel
// to finish and our latency and throughput will be negatively affected.
std::queue<event> event_q;
for (size_t i = 0; i < iterations; i++) {
// reset the output data to catch any untouched data
std::fill_n(out, total_count, -1);
// reset counters
in_chunk = 0;
out_chunk = 0;
// clear the queue
std::queue<event> clear_q;
std::swap(event_q, clear_q);
// latency timers
high_resolution_clock::time_point first_data_in, first_data_out;
auto start = high_resolution_clock::now();
do {
// if we still have kernels to launch, launch them in here
if (in_chunk < chunks) {
// launch the kernel
size_t chunk_offset = in_chunk*chunk_count;
// this function is defined in 'single_kernel.hpp'
auto e = SubmitSingleWorker(q, in + chunk_offset, out + chunk_offset,
chunk_count);
// push the kernel event into the queue
event_q.push(e);
// if this is the first chunk, track the time
if (in_chunk == 0) first_data_in = high_resolution_clock::now();
in_chunk++;
}
// wait on the earliest kernel to finish if either condition is met:
// 1) there are a certain number kernels in flight
// 2) all of the kernels have been launched
if ((event_q.size() >= inflight_kernels) || (in_chunk >= chunks)) {
// pop the earliest kernel event we are waiting on
auto e = event_q.front();
event_q.pop();
// wait on it to finish
e.wait();
// track the time if this is the first producer/consumer pair
if (out_chunk == 0) first_data_out = high_resolution_clock::now();
// The synchronization of the kernels ending tells us that, at this
// point, the first 'out_chunk' chunks are valid on the host.
// NOTE: This is the point where you would consume the output data
// at (out + out_chunk*chunk_size).
out_chunk++;
}
} while (out_chunk < chunks);
auto end = high_resolution_clock::now();
// compute latency and processing time
duration<double, std::milli> latency = first_data_out - first_data_in;
duration<double, std::milli> process_time = end - start;
latency_ms[i] = latency.count();
process_time_ms[i] = process_time.count();
}
// compute and print timing information
PrintPerformanceInfo<T>("Single-kernel",
total_count, latency_ms, process_time_ms);
}
//
// The multi-kernel version of the design.
// Like the single-kernel version of the design, this design optimizes for
// latency (while maintaining throughput) by breaking the producing and
// consuming of data into chunks. That is, the main kernel pipeline (K0,
// K1, and K2 from SubmitMultiKernelWorkers above) are enqueued ONCE but
// the producer and consumer kernels, that feed and consume data to the
// the kernel pipeline, are broken into smaller chunks. The synchronization of
// the producer and consumer kernels (specifically, the consumer kernel)
// signals to the host that a new chunk of data is ready in host memory.
// See the README file for more information on why a producer and consumer
// kernel are created for this design style.
//
// The following is a block diagram of this kernel this function creates:
//
// in |---| ProducePipe |----| Pipe0 |----| Pipe1 |----| ConsumePipe |---| out
// --->| P |============>| K0 |======>| K1 |======>| K2 |============>| C |---->
// |---| |----| |----| |----| |---|
//
// the pipes used to produce/consume data
using ProducePipe = pipe<class ProducePipeClass, Type>;
using ConsumePipe = pipe<class ConsumePipeClass, Type>;
template <typename T>
void DoWorkMultiKernel(queue& q, T* in, T* out,
size_t chunks, size_t chunk_count, size_t total_count,
size_t inflight_kernels, size_t iterations) {
// timing data
std::vector<double> latency_ms(iterations);
std::vector<double> process_time_ms(iterations);
// count the number of chunks for which kernels have been started
size_t in_chunk = 0;
// count the number of chunks for which kernels have finished
size_t out_chunk = 0;
// use a queue to track the kernels in flight
std::queue<std::pair<event,event>> event_q;
for (size_t i = 0; i < iterations; i++) {
// reset the output data to catch any untouched data
std::fill_n(out, total_count, -1);
// reset counters
in_chunk = 0;
out_chunk = 0;
// clear the queue
std::queue<std::pair<event,event>> clear_q;
std::swap(event_q, clear_q);
// latency timers
high_resolution_clock::time_point first_data_in, first_data_out;
// launch the worker kernels
// NOTE: these kernels will process ALL of the data (total_count)
// while the producer/consumer will be broken into chunks
// this function is defined in 'multi_kernel.hpp'
auto events = SubmitMultiKernelWorkers<T,
ProducePipe,
ConsumePipe>(q, total_count);
auto start = high_resolution_clock::now();
do {
// if we still have kernels to launch, launch them in here
if (in_chunk < chunks) {
// launch the producer/consumer pair for the next chunk of data
size_t chunk_offset = in_chunk*chunk_count;
// these functions are defined in 'multi_kernel.hpp'
event p_e = SubmitProducer<T, ProducePipe>(q, in + chunk_offset,
chunk_count);
event c_e = SubmitConsumer<T, ConsumePipe>(q, out + chunk_offset,
chunk_count);
// push the kernel event into the queue
event_q.push(std::make_pair(p_e, c_e));
// if this is the first chunk, track the time
if (in_chunk == 0) first_data_in = high_resolution_clock::now();
in_chunk++;
}
// wait on the oldest kernel to finish if any of these conditions are met:
// 1) there are a certain number kernels in flight
// 2) all of the kernels have been launched
//
// NOTE: 'inflight_kernels' is now the number of inflight
// producer/consumer kernel pairs
if ((event_q.size() >= inflight_kernels) || (in_chunk >= chunks)) {
// grab the oldest kernel event we are waiting on
auto event_pair = event_q.front();
event_q.pop();
// wait on the producer/consumer kernel pair to finish
event_pair.first.wait(); // producer
event_pair.second.wait(); // consumer
// track the time if this is the first producer/consumer pair
if (out_chunk == 0) first_data_out = high_resolution_clock::now();
// at this point the first 'out_chunk' chunks are ready to be
// processed on the host
out_chunk++;
}
} while(out_chunk < chunks);
// wait for the worker kernels to finish, which should be done quickly
// since all producer/consumer pairs are done
for (auto& e : events) {
e.wait();
}
auto end = high_resolution_clock::now();
// compute latency and processing time
duration<double, std::milli> latency = first_data_out - first_data_in;
duration<double, std::milli> process_time = end - start;
latency_ms[i] = latency.count();
process_time_ms[i] = process_time.count();
}
// compute and print timing information
PrintPerformanceInfo<T>("Multi-kernel",
total_count, latency_ms, process_time_ms);
}
// a helper function to compute and print the performance info
template<typename T>
void PrintPerformanceInfo(std::string print_prefix, size_t count,
std::vector<double>& latency_ms,
std::vector<double>& process_time_ms) {
// compute the input size in MB
double input_size_megabytes = (sizeof(T) * count) * 1e-6;
// compute the average latency and processing time
double iterations = latency_ms.size() - 1;
double avg_latency_ms = std::accumulate(latency_ms.begin() + 1,
latency_ms.end(),
0.0) / iterations;
double avg_processing_time_ms = std::accumulate(process_time_ms.begin() + 1,
process_time_ms.end(),
0.0) / iterations;
// compute the throughput
double avg_tp_mb_s = input_size_megabytes / (avg_processing_time_ms * 1e-3);
// print info
std::cout << std::fixed << std::setprecision(4);
std::cout << print_prefix
<< " average latency: " << avg_latency_ms << " ms\n";
std::cout << print_prefix
<< " average throughput: " << avg_tp_mb_s << " MB/s\n";
}
| cpp |
Subsets and Splits