lokinet/llarp/buffer.cpp
despair86 bdc54835c2 initial windows server port. Requires Windows 2000 Server or later.
- updated CMake build script
- builds with Microsoft C++ 19.1x. such builds require Windows 8.1 or later
  unless you have the .NET Server 2003-toolset (v141_xp)
- windows port requires a C++17 compiler since cpp17::filesystem is POSIX-only
- HAVE_CXX17_FILESYSTEM manual toggle in CMake. You must manually specify where
  std::[experimental::]filesystem is defined in LDFLAGS or CMAKE_x_LINKER_FLAGS.
- IPv6 support can be added at any time, and the windows sdk still has that
  inline getaddrinfo(3) if it can't find a suitable IPv6 stack.
- inline code for mingw-w64: there's a few bits and pieces still missing simply because
  mingw-w64 derives its windows sdk from wine and reactos, and then writing all the newer
  stuff into it by hand straight from the MSDN manpages.
- misc. C++11 stuff (nullptr and friends)
- Internal file handling code takes UTF-8 or plain 8-bit text, NTFS is UTF-16, so
  std::filesystem::path::c_str() is wchar_t. That's no good unless you first
  call std::filesystem::path::string().
- implemented getifaddrs(3) and if_nametoindex(3) on top of GetAdapters[Info|Addresses](2).
- updated readme with new info

BONUS: may implement Solaris/illumos IOCP someday...

-despair86
2018-08-01 23:41:02 -05:00

121 lines
2.3 KiB
C++

#include <llarp/buffer.h>
#include <llarp/endian.h>
#include <stdarg.h>
#include <stdio.h>
#ifndef ssize_t
#define ssize_t long
#endif
size_t
llarp_buffer_size_left(llarp_buffer_t buff)
{
size_t diff = buff.cur - buff.base;
if(diff > buff.sz)
return 0;
else
return buff.sz - diff;
}
bool
llarp_buffer_writef(llarp_buffer_t* buff, const char* fmt, ...)
{
int written;
ssize_t sz = llarp_buffer_size_left(*buff);
va_list args;
va_start(args, fmt);
written = vsnprintf((char*)buff->cur, sz, fmt, args);
va_end(args);
if(written <= 0)
return false;
if(sz < written)
return false;
buff->cur += written;
return true;
}
bool
llarp_buffer_write(llarp_buffer_t* buff, const void* data, size_t sz)
{
size_t left = llarp_buffer_size_left(*buff);
if(left >= sz)
{
memcpy(buff->cur, data, sz);
buff->cur += sz;
return true;
}
return false;
}
size_t
llarp_buffer_read_until(llarp_buffer_t* buff, char delim, byte_t* result,
size_t resultsize)
{
size_t read = 0;
while(*buff->cur != delim && resultsize
&& (buff->cur != buff->base + buff->sz))
{
*result = *buff->cur;
buff->cur++;
result++;
resultsize--;
read++;
}
if(llarp_buffer_size_left(*buff))
return read;
else
return 0;
}
bool
llarp_buffer_eq(llarp_buffer_t buf, const char* str)
{
while(*str && buf.cur != (buf.base + buf.sz))
{
if(*buf.cur != *str)
return false;
buf.cur++;
str++;
}
return *str == 0;
}
bool
llarp_buffer_put_uint16(llarp_buffer_t* buf, uint16_t i)
{
if(llarp_buffer_size_left(*buf) < sizeof(uint16_t))
return false;
htobe16buf(buf->cur, i);
buf->cur += sizeof(uint16_t);
return true;
}
bool
llarp_buffer_put_uint32(llarp_buffer_t* buf, uint32_t i)
{
if(llarp_buffer_size_left(*buf) < sizeof(uint32_t))
return false;
htobe32buf(buf->cur, i);
buf->cur += sizeof(uint32_t);
return true;
}
bool
llarp_buffer_read_uint16(llarp_buffer_t* buf, uint16_t* i)
{
if(llarp_buffer_size_left(*buf) < sizeof(uint16_t))
return false;
*i = bufbe16toh(buf->cur);
buf->cur += sizeof(uint16_t);
return true;
}
bool
llarp_buffer_put_uint32(llarp_buffer_t* buf, uint32_t* i)
{
if(llarp_buffer_size_left(*buf) < sizeof(uint32_t))
return false;
*i = bufbe32toh(buf->cur);
buf->cur += sizeof(uint32_t);
return true;
}