+#include <inttypes.h>
+
+#ifdef USE_POLARSSL
+#define SSLI_ERROR_WANT_READ -0x0F300 /* PolarSSL v0.x.x uses -0x0f00 -> --0x0f90, v1.x.x uses -0x7080 -> -0x7e80 */
+#define SSLI_ERROR_WANT_WRITE -0x0F310
+
+#define SSLI_ERROR_ZERO_RETURN 0
+#define SSLI_ERROR_CONNRESET POLARSSL_ERR_NET_CONN_RESET
+#define SSLI_ERROR_SYSCALL POLARSSL_ERR_NET_RECV_FAILED
+
+typedef ssl_context SSL_handle_t;
+
+#else
+
+#define SSLI_ERROR_WANT_READ SSL_ERROR_WANT_READ
+#define SSLI_ERROR_WANT_WRITE SSL_ERROR_WANT_WRITE
+#define SSLI_ERROR_ZERO_RETURN SSL_ERROR_ZERO_RETURN
+#define SSLI_ERROR_CONNRESET SSL_ERROR_ZERO_RETURN
+#define SSLI_ERROR_SYSCALL SSL_ERROR_SYSCALL
+
+typedef SSL SSL_handle_t;
+
+#endif
+
+void SSLi_init(void);
+void SSLi_deinit(void);
+SSL_handle_t *SSLi_newconnection(int *fd, bool_t *SSLready);
+bool_t SSLi_getSHA1Hash(SSL_handle_t *ssl, uint8_t *hash);
+void SSLi_closeconnection(SSL_handle_t *ssl);
+int SSLi_nonblockaccept(SSL_handle_t *ssl, bool_t *SSLready);
+int SSLi_read(SSL_handle_t *ssl, uint8_t *buf, int len);
+int SSLi_write(SSL_handle_t *ssl, uint8_t *buf, int len);
+int SSLi_get_error(SSL_handle_t *ssl, int code);
+bool_t SSLi_data_pending(SSL_handle_t *ssl);
+void SSLi_shutdown(SSL_handle_t *ssl);
+void SSLi_free(SSL_handle_t *ssl);
+
+static inline void SSLi_hash2hex(uint8_t *hash, char *out)
+{
+ int i, offset = 0;
+ for (i = 0; i < 20; i++)
+ offset += sprintf(out + offset, "%02x", hash[i]);
+}
+static inline void SSLi_hex2hash(char *in, uint8_t *hash)
+{
+ int i, offset = 0;
+ char byte[3];
+ int scanned;