Fixed SDL_SwapBE16 issue on client side.

< Bubblegumdrop> SDLNet_UDP_Open seems to call SDLNet_Read16 (which is just SDL_SwapBE16)
< Bubblegumdrop> But on the server side I don't use SDLNet_UDP_Open to host the port
< whitt> probably the sender was flipping it but the receiver wasn't and so they were incompatible
< Bubblegumdrop> removing the SDL_SwapBE16 on the client side fixed it
This commit is contained in:
Bubblegumdrop 2022-01-03 14:48:50 -05:00
parent 46b2ed80ae
commit 6a8f90500f
14 changed files with 294 additions and 117 deletions

View File

@ -31,7 +31,8 @@ $(shell $(MKDIR) $(dir $(DEPS)) >/dev/null)
COMMON_OBJS := \
$(OBJDIR)/src/lib_lua_common.o \
$(OBJDIR)/src/lib_SDL_common.o \
$(OBJDIR)/src/signal_common.o
$(OBJDIR)/src/signal_common.o \
$(OBJDIR)/src/util.o
SERVER_OBJS := \
$(OBJDIR)/src/server_main.o \
@ -40,7 +41,6 @@ SERVER_OBJS := \
CLIENT_OBJS := \
$(OBJDIR)/src/camera.o \
$(OBJDIR)/src/client_common.o \
$(OBJDIR)/src/client_main.o \
$(OBJDIR)/src/Client.o \
$(OBJDIR)/src/cube.o \
@ -148,14 +148,16 @@ main_server: $(SERVER_OBJS)
test: $(TEST_PROGS)
test/test_FileIO: $(TEST_OBJ_COMMON)
test/test_FileIO: $(TEST_OBJ_COMMON) $(OBJDIR)/src/util.o
test/test_File_Write_UDP: $(TEST_OBJ_COMMON) $(OBJDIR)/src/client_common.o $(OBJDIR)/src/UDP_Write.o $(OBJDIR)/src/Client.o
test/test_File_Write_UDP: $(TEST_OBJ_COMMON) $(OBJDIR)/src/UDP_Write.o $(OBJDIR)/src/Client.o
test/test_Object: $(TEST_OBJ_COMMON) $(OBJDIR)/src/object.o $(OBJDIR)/src/quad.o $(OBJDIR)/src/cube.o $(OBJDIR)/src/lib_GL_common.o
test/test_lua: $(TEST_OBJ_COMMON) $(OBJDIR)/src/lib_lua_common.o
test/test_Client: $(TEST_OBJ_COMMON) $(OBJDIR)/src/UDP_Write.o $(OBJDIR)/src/Client.o
$(OBJDIR)/%.o: %.c
$(OBJDIR)/%.o: %.c $(DEPDIR)/%.d
$(PRECOMPILE)

View File

@ -1,5 +1,7 @@
#pragma once
#include "debug.h"
#include <string>
#include <vector>
@ -14,8 +16,17 @@ class Client
public:
Client (void)
{
DEBUG_LOG ("Client::Client ()");
}
~Client (void)
{
DEBUG_LOG ("Client::~Client ()");
}
Client (const char* host, const Uint16 port, const int channel);
UDPsocket UDP_Open (const char* host, const Uint16 port, const int channel);
void UDP_CloseAll (void);
UDPsocket First (void)
{
return UDPsocks.at (0);
}
};

View File

@ -11,7 +11,7 @@
class Server
{
bool mProcessPacketCallbackRunning;
SDL_mutex* mProcessPacketMutex;
SDL_TimerID mProcessPacketCallbackTimerID;
static Uint32 ProcessPacketCallback (Uint32, void*);
public:
@ -20,19 +20,31 @@ class Server
UDPpacket **UDPPacketV;
Server (void)
{
mProcessPacketCallbackRunning = false;
UDPsocks.clear ();
SocketSet = NULL;
UDPPacketV = NULL;
mProcessPacketMutex = NULL;
}
~Server (void)
{
// UDP_CloseAll ();
// mProcessPacketMutex = SDL_CreateMutex ();
// UDPsocks.clear ();
// SocketSet = NULL;
// UDPPacketV = NULL;
}
Server (const char* host, const Uint16 port);
UDPsocket UDP_Open (Uint16 port);
void UDP_CloseAll (void);
void ProcessPacket (UDPpacket* packet);
void Start (void);
void Stop (void);
bool ProcessPacketCallbackRunning (void)
int Start (void);
int Stop (void);
int Lock (void)
{
return mProcessPacketCallbackRunning;
return SDL_LockMutex (mProcessPacketMutex);
}
int Unlock (void)
{
return SDL_UnlockMutex (mProcessPacketMutex);
}
};

View File

@ -3,8 +3,8 @@
#ifdef DEBUG
#include <SDL_log.h>
#include <stdarg.h>
#define DEBUG_LOG(...) debug_log (__FILE__, __LINE__, __VA_ARGS__)
static void debug_log (const char* filename, const int lineno, const char* fmt, ...)
#define DEBUG_LOG(...) debug_log (__FILE__, __LINE__, __func__, __VA_ARGS__)
static void debug_log (const char* filename, const int lineno, const char* func, const char* fmt, ...)
{
char buf[8192];
va_list ap;
@ -14,7 +14,7 @@ static void debug_log (const char* filename, const int lineno, const char* fmt,
vsnprintf (buf, (sizeof buf) - 1, fmt, ap);
va_end (ap);
SDL_LogMessage (SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_DEBUG, "%s:%d: %s", filename, lineno, buf);
SDL_LogMessage (SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_DEBUG, "%s:%d: %s(): %s", filename, lineno, func, buf);
}

6
include/util.h Normal file
View File

@ -0,0 +1,6 @@
#pragma once
#include <stddef.h> /* size_t */
int IsProbablyAscii (const void *, const size_t);
void HexDump (const void *, const size_t);

View File

@ -1,10 +1,13 @@
#include "debug.h"
#include "Client.h"
Client::Client (const char* host, const Uint16 port, const int channel)
#include <arpa/inet.h> /* inet_ntop */
Client::Client (const char *host, const Uint16 port, const int channel)
{
UDPsocket udpsock;
if (NULL != (udpsock = UDP_Open (host, SDL_SwapBE16 (port), channel)))
DEBUG_LOG ("Attempting to connect %s:%d ch%d", host, port, channel);
if (NULL != (udpsock = UDP_Open (host, port, channel)))
{
mHost = host;
mPort = port;
@ -15,25 +18,28 @@ Client::Client (const char* host, const Uint16 port, const int channel)
UDPsocket
Client::UDP_Open (const char* host, const Uint16 port, const int channel)
Client::UDP_Open (const char *host, const Uint16 port, const int channel)
{
IPaddress address;
char hoststring[128];
IPaddress ipaddress;
UDPsocket udpsock;
if (!(udpsock = SDLNet_UDP_Open (0)))
{
DEBUG_LOG ("SDLNet_UDP_Open: %s", SDLNet_GetError ());
return NULL;
}
if (SDLNet_ResolveHost (&address, host, port) < 0)
if (SDLNet_ResolveHost (&ipaddress, host, port) < 0)
{
DEBUG_LOG ("SDLNet_ResolveHost: %s", SDLNet_GetError ());
return NULL;
}
if (SDLNet_UDP_Bind (udpsock, channel, &address) < 0)
if (SDLNet_UDP_Bind (udpsock, channel, &ipaddress) < 0)
{
DEBUG_LOG ("SDLNet_UDP_Bind: %s", SDLNet_GetError ());
return NULL;
}
inet_ntop (AF_INET, &ipaddress.host, hoststring, 128);
DEBUG_LOG ("Bound socket %s:%d ch%d", hoststring, ipaddress.port, channel);
return udpsock;
}

View File

@ -6,9 +6,9 @@
Server::Server (const char *host, const Uint16 port)
{
char hoststring[128];
IPaddress ipaddress;
UDPsocket udpsock;
char hoststring[128];
if (SDLNet_ResolveHost (&ipaddress, host, SDL_SwapBE16 (port)) < 0)
{
DEBUG_LOG ("SDLNet_ResolveHost: %s", SDLNet_GetError ());
@ -17,9 +17,8 @@ Server::Server (const char *host, const Uint16 port)
if (NULL != (udpsock = UDP_Open (ipaddress.port)))
{
UDPsocks.push_back (udpsock);
mProcessPacketCallbackRunning = false;
mProcessPacketMutex = SDL_CreateMutex ();
inet_ntop (AF_INET, &ipaddress.host, hoststring, 128);
SDL_Log ("Listening on %s:%d", hoststring, ipaddress.port);
}
else
{
@ -31,10 +30,8 @@ Server::Server (const char *host, const Uint16 port)
void
Server::UDP_CloseAll (void)
{
size_t i, nclients;
nclients = UDPsocks.size ();
SDL_Log ("Server::~Server %ld", nclients);
for (i = 0; i < nclients; i++)
const size_t nclients = UDPsocks.size ();
for (size_t i = 0; i < nclients; i++)
{
SDLNet_UDP_Close (UDPsocks[i]);
UDPsocks[i] = NULL;
@ -83,54 +80,88 @@ Server::ProcessPacket (UDPpacket * packet)
}
void
int
Server::Start (void)
{
size_t i;
int nclients, numused;
nclients = (int) UDPsocks.size ();
SDL_Log ("Server %d clients", nclients);
int l;
int numused;
if (!mProcessPacketMutex)
{
mProcessPacketMutex = SDL_CreateMutex ();
}
if ((l = Lock ()) < 0)
{
DEBUG_LOG ("Lock failed: %s", SDL_GetError ());
return l;
}
const size_t nclients = UDPsocks.size ();
if (!nclients)
{
SDL_Log ("No Clients!");
return -1;
}
else
{
DEBUG_LOG ("Serving %ld clients", nclients);
}
if (!(UDPPacketV = SDLNet_AllocPacketV (nclients, MAX_PACKET_SIZE)))
{
DEBUG_LOG ("SDLNet_AllocPacketV: %s", SDLNet_GetError ());
return;
return -1;
}
if (!(SocketSet = SDLNet_AllocSocketSet (nclients)))
{
DEBUG_LOG ("SDLNet_AllocSocketSet: %s", SDLNet_GetError ());
return;
return -1;
}
for (i = 0; i < (size_t) nclients; i++)
for (size_t i = 0; i < nclients; i++)
{
numused = SDLNet_UDP_AddSocket (SocketSet, UDPsocks[i]);
if (numused < 0)
{
DEBUG_LOG ("SDLNet_AddSocket: %s", SDLNet_GetError ());
return;
return -1;
}
}
mProcessPacketCallbackRunning = true;
mProcessPacketCallbackTimerID = SDL_AddTimer (10, Server::ProcessPacketCallback, (void *) this);
return Unlock ();
}
void
int
Server::Stop (void)
{
size_t i, nclients;
mProcessPacketCallbackRunning = false;
int l;
if ((l = Lock ()) < 0)
{
DEBUG_LOG ("Lock failed");
return l;
}
SDL_RemoveTimer (mProcessPacketCallbackTimerID);
nclients = UDPsocks.size ();
for (i = 0; i < nclients; i++)
const size_t nclients = UDPsocks.size ();
for (size_t i = 0; i < nclients; i++)
{
SDLNet_UDP_DelSocket (SocketSet, UDPsocks[i]);
}
SDLNet_FreeSocketSet (SocketSet);
SocketSet = NULL;
SDLNet_FreeSocketSet (SocketSet);
SDLNet_FreePacketV (UDPPacketV);
SocketSet = NULL;
UDPPacketV = NULL;
Unlock ();
SDL_DestroyMutex (mProcessPacketMutex);
mProcessPacketMutex = NULL;
return 0;
}
@ -141,10 +172,13 @@ Server::ProcessPacketCallback (Uint32 interval, void *param)
size_t i, j;
SDL_assert (NULL != param);
Server & server = *(Server *) param;
Server& server = *(Server *) param;
if (!server.ProcessPacketCallbackRunning ())
if (server.Lock () < 0)
{
DEBUG_LOG ("Lock failed");
return 0;
}
numready = SDLNet_CheckSockets (server.SocketSet, ~0);
if (numready < 0)
@ -175,5 +209,7 @@ Server::ProcessPacketCallback (Uint32 interval, void *param)
}
}
server.Unlock ();
return interval;
}

View File

@ -11,14 +11,16 @@
#include <errno.h>
#include <string.h>
#include <arpa/inet.h> /* inet_ntop */
/* XXX gross */
int
Buffer_Write_UDP (UDPsocket udpsock, const int channel, const size_t mtu, const void *buf, const size_t size)
{
char hoststring[128];
const char *bufPtr;
const char *bufPtrEnd;
int numsent, npackets;
IPaddress *address;
IPaddress *ipaddress;
size_t i, our_nsent, our_mtu;
UDPpacket **packetV;
SDL_assert (mtu > 0);
@ -30,19 +32,24 @@ Buffer_Write_UDP (UDPsocket udpsock, const int channel, const size_t mtu, const
npackets = ceil ((double) size / (double) our_mtu);
if (!npackets)
{
DEBUG_LOG ("File_Write_UDP: zero length packet\n");
DEBUG_LOG ("File_Write_UDP: zero length packet");
return 0;
}
packetV = SDLNet_AllocPacketV (npackets, our_mtu);
if (!packetV)
{
DEBUG_LOG ("SDLNet_AllocPacketV: %s\n", SDLNet_GetError ());
DEBUG_LOG ("SDLNet_AllocPacketV: %s", SDLNet_GetError ());
return 0;
}
i = 0;
bufPtr = (const char *) buf;
bufPtrEnd = bufPtr + size;
address = SDLNet_UDP_GetPeerAddress (udpsock, channel);
if (NULL == (ipaddress = SDLNet_UDP_GetPeerAddress (udpsock, channel)))
{
DEBUG_LOG ("SDLNet_UDP_GetPeerAddress: %s", SDLNet_GetError ());
}
inet_ntop (AF_INET, &ipaddress->host, hoststring, 128);
DEBUG_LOG ("Bound socket %s:%d ch%d", hoststring, ipaddress->port, channel);
size_t nsent = size;
while (i < (size_t) npackets && bufPtr < bufPtrEnd)
{
@ -50,7 +57,7 @@ Buffer_Write_UDP (UDPsocket udpsock, const int channel, const size_t mtu, const
if (our_nsent > our_mtu)
our_nsent = our_mtu;
packetV[i]->channel = channel;
SDL_memcpy (&packetV[i]->address, address, sizeof (IPaddress));
SDL_memcpy (&packetV[i]->address, ipaddress, sizeof (IPaddress));
SDL_memset (packetV[i]->data, 0, our_mtu);
SDL_memcpy (packetV[i]->data, bufPtr, our_nsent);
packetV[i]->len = our_nsent;
@ -61,7 +68,7 @@ Buffer_Write_UDP (UDPsocket udpsock, const int channel, const size_t mtu, const
numsent = SDLNet_UDP_SendV (udpsock, packetV, npackets);
if (!numsent)
{
SDL_Log ("SDLNet_UDP_SendV (%d): %s\n", npackets, SDLNet_GetError ());
SDL_Log ("SDLNet_UDP_SendV (%d): %s", npackets, SDLNet_GetError ());
}
SDLNet_FreePacketV (packetV);
return numsent;
@ -98,7 +105,7 @@ SendBuffer_UDP (const char *host, const Uint16 port, const int channel, const si
UDPsocket udpsock;
numsent = 0;
Client client (host, port, channel);
if (udpsock)
if (NULL != (udpsock = client.First ()))
{
numsent = Buffer_Write_UDP (udpsock, channel, mtu, buf, size);
client.UDP_CloseAll ();
@ -114,7 +121,7 @@ SendFile_UDP (const char *host, const Uint16 port, const int channel, const size
UDPsocket udpsock;
numsent = 0;
Client client (host, port, channel);
if (udpsock)
if (NULL != (udpsock = client.First ()))
{
numsent = File_Write_UDP (udpsock, channel, path, mtu);
client.UDP_CloseAll ();

View File

@ -81,6 +81,7 @@ struct Client_State
static std::shared_ptr<Cube> cube;
static std::shared_ptr<Quad> quad;
static std::shared_ptr<Client> client;
static Camera camera;
static ShaderProgram shader;
@ -90,8 +91,8 @@ static struct Uniform_Values values;
int
main (int argc, char **argv)
{
SDL_Event event;
glm::mat4 model, view, projection;
SDL_Event event;
my_Init (argc, argv);
@ -108,7 +109,8 @@ main (int argc, char **argv)
state.mtu = 1450;
state.quit = 0;
if (NULL == (state.udpsock = Client::UDP_Open (state.host, state.port, state.channel)))
client = std::make_shared<Client> (state.host, state.port, state.channel);
if (NULL == (state.udpsock = client->First ()))
{
SDL_Log ("Unable to open client port");
goto _out;
@ -156,7 +158,7 @@ main (int argc, char **argv)
}
_out:
client.UDP_CloseAll ();
client->UDP_CloseAll ();
SDL_GL_DeleteContext (state.GLContext);
SDL_DestroyWindow (state.Window);
common_SDL_Quit ();
@ -219,10 +221,8 @@ static int
my_SDL_MouseMotion (SDL_Event * event)
{
SDL_assert (NULL != event);
const float
xpos = event->motion.x;
const float
ypos = event->motion.y;
const float xpos = event->motion.x;
const float ypos = event->motion.y;
if (!state.InputCaptureMouse || state.ImGuiEnabled)
return SDL_FALSE;
@ -235,10 +235,8 @@ my_SDL_MouseMotion (SDL_Event * event)
}
// reversed since y-coordinates go from bottom to top
const float
Xoffset = xpos - values.iMouse.x;
const float
Yoffset = values.iMouse.y - ypos;
const float Xoffset = xpos - values.iMouse.x;
const float Yoffset = values.iMouse.y - ypos;
values.iMouse.x = xpos;
values.iMouse.y = ypos;
@ -296,10 +294,8 @@ my_Input (SDL_Event * event)
static int
my_AnalogInput (void)
{
const Uint8 *
kbd = SDL_GetKeyboardState (NULL);
const float
dt = 0.016;
const Uint8* kbd = SDL_GetKeyboardState (NULL);
const float dt = 0.016;
(void) state;
camera.ProcessKeyboard (kbd[SDL_SCANCODE_W] ? Camera::Movement::FORWARD : Camera::Movement::NONE, dt);
@ -333,13 +329,23 @@ my_Input_Key (SDL_Event * event)
{
if (down)
{
state.InputCaptureMouse = true;
state.InputFirstMouse = true;
state.InputCaptureMouse = true;
state.InputRelativeMouseMode = true;
common_SDL_ToggleFullscreen (SDL_GetWindowFromID (event->key.windowID));
}
}
break;
case SDLK_m:
{
if (down)
{
state.InputFirstMouse = true;
state.InputCaptureMouse = !state.InputCaptureMouse;
state.InputRelativeMouseMode = !state.InputRelativeMouseMode;
}
}
break;
case SDLK_r:
{
if (down)
@ -354,7 +360,12 @@ my_Input_Key (SDL_Event * event)
break;
default:
{
Buffer_Write_UDP (state.udpsock, state.channel, state.mtu, event, sizeof (SDL_Event));
if (event->key.keysym.scancode != SDL_GetScancodeFromKey (event->key.keysym.sym))
{
DEBUG_LOG ("Physical %s key acting as %s key", SDL_GetScancodeName (event->key.keysym.scancode), SDL_GetKeyName (event->key.keysym.sym));
}
// SDL_Log ("%d %d %ld", state.channel, state.mtu, sizeof (SDL_Event));
Buffer_Write_UDP (state.udpsock, state.channel, state.mtu, (const void*)event, sizeof (SDL_Event));
}
}
return quit;
@ -394,9 +405,9 @@ static void
my_GetOpt (int argc, char **argv)
{
int opt;
state.channel = 1;
state.host = "localhost";
state.port = 6666;
state.channel = 1;
while ((opt = getopt (argc, argv, "hp:s:")) != -1)
{
switch (opt)

View File

@ -6,6 +6,10 @@
#include "events_common.h"
#include "trim.h"
#include "lib_lua_common.h"
extern "C"
{
#include "util.h"
};
#include <vector>
@ -34,7 +38,11 @@ main (int argc, char **argv)
common_SDL_Init ();
server = Server (host, port);
server.Start ();
if (server.Start () < 0)
{
SDL_Log ("Couldn't start server!");
exit (EXIT_FAILURE);
}
done = false;
while (!done)
@ -110,15 +118,18 @@ my_ProcessPacket (UDPpacket* packet)
len = packet->len;
SDL_memcpy (&event, packet->data, len);
if ((event.type != SDL_KEYDOWN) && (event.type != SDL_KEYUP))
// SDL_Log ("%d, %x", packet->len, event.type);
if (!((event.type == SDL_KEYDOWN) || (event.type == SDL_KEYUP)))
{
std::string buf ((const char*)packet->data, len);
if (IsProbablyAscii (buf.c_str (), buf.length ()))
printf ("%s", buf.c_str ());
else
HexDump (buf.c_str (), buf.length ());
return;
}
if (event.key.keysym.scancode != SDL_GetScancodeFromKey (event.key.keysym.sym))
{
DEBUG_LOG ("Physical %s key acting as %s key", SDL_GetScancodeName (event.key.keysym.scancode), SDL_GetKeyName (event.key.keysym.sym));
}
std::string str = std::string (SDL_GetKeyName (event.key.keysym.sym));
bool down = (event.type == SDL_KEYDOWN);

47
src/util.c Normal file
View File

@ -0,0 +1,47 @@
#include "util.h"
#include <stdio.h> /* printf */
#include <stdint.h> /* uint8_t */
#include <math.h> /* round */
int
IsProbablyAscii (const void *buf, const size_t len)
{
const uint8_t* bufPtr;
const uint8_t* bufPtrEnd;
size_t nAsciiCount;
nAsciiCount = 0;
bufPtr = (const uint8_t*) buf;
bufPtrEnd = bufPtr + len;
while (bufPtr < bufPtrEnd && (NULL != bufPtr) && (*bufPtr != '\0'))
{
nAsciiCount += (*bufPtr >= ' ') && (*bufPtr <= '~');
bufPtr++;
}
return (int) round ((double) nAsciiCount / (double) len);
}
void
HexDump (const void *buf, const size_t len)
{
const uint8_t* bufPtr;
const uint8_t* bufPtrEnd;
const size_t width = 16;
size_t nout;
nout = 0;
bufPtr = (const uint8_t*) buf;
bufPtrEnd = bufPtr + len;
while (bufPtr < bufPtrEnd && (NULL != bufPtr))
{
if (nout == width)
{
printf ("\n");
nout = 0;
}
printf ("%02x ", *bufPtr);
bufPtr++;
nout++;
}
puts ("");
}

BIN
test/test_Client Executable file

Binary file not shown.

64
test/test_Client.cpp Normal file
View File

@ -0,0 +1,64 @@
#include "debug.h"
#include "Client.h"
#include "UDP_Write.h"
#include <string>
int
main (int argc, char **argv)
{
int numsent;
const char *host = "localhost";
const Uint16 port = 6666;
const int channel = 1;
const size_t mtu = 1450;
Client c;
UDPsocket udpsock;
c = Client (host, port, channel);
if (!(udpsock = c.First ()))
{
SDL_Log ("Client error");
return 1;
}
std::string buf = "Hello, world!\n";
numsent = Buffer_Write_UDP (udpsock, channel, mtu, buf.c_str (), buf.length ());
SDL_Log ("%d", numsent);
c.UDP_CloseAll ();
return 0;
#if 0
IPaddress ipaddress;
UDPsocket udpsock;
UDPpacket *packet;
udpsock = SDLNet_UDP_Open (0);
if (SDLNet_ResolveHost (&ipaddress, host, port) < 0)
{
DEBUG_LOG ("SDLNet_ResolveHost: %s", SDLNet_GetError ());
return 0;
}
if (SDLNet_UDP_Bind (udpsock, channel, &ipaddress) < 0)
{
DEBUG_LOG ("SDLNet_UDP_Bind: %s", SDLNet_GetError ());
return 0;
}
packet = SDLNet_AllocPacket (1024);
if (packet)
{
packet->len = sprintf ((char*)packet->data, "Hello World!");
packet->address = ipaddress;
numsent = SDLNet_UDP_Send (udpsock, packet->channel, packet);
if (!numsent)
{
DEBUG_LOG ("SDLNet_UDP_Send: %s\n", SDLNet_GetError ());
}
}
SDLNet_UDP_Unbind (udpsock, channel);
SDLNet_UDP_Close (udpsock);
udpsock = NULL;
#endif
}

View File

@ -1,4 +1,8 @@
#include "FileIO.h"
extern "C"
{
#include "util.h"
};
#include <stdint.h> /* uint8_t */
#include <stdio.h>
@ -7,48 +11,8 @@
#include <errno.h> /* errno */
#include <string.h> /* strerror */
static int
IsProbablyAscii (const void *buf, const size_t len)
{
const uint8_t *bufPtr;
const uint8_t *bufPtrEnd;
size_t nAsciiCount;
nAsciiCount = 0;
bufPtr = (const uint8_t *) buf;
bufPtrEnd = bufPtr + len;
while (bufPtr < bufPtrEnd && (NULL != bufPtr) && (*bufPtr != '\0'))
{
nAsciiCount += (*bufPtr >= ' ') && (*bufPtr <= '~');
bufPtr++;
}
return (int) round ((double) nAsciiCount / (double) len);
}
static void
HexDump (const void *buf, const size_t len)
{
const uint8_t *bufPtr;
const uint8_t *bufPtrEnd;
const size_t width = 16;
size_t nout;
nout = 0;
bufPtr = (const uint8_t *) buf;
bufPtrEnd = bufPtr + len;
while (bufPtr < bufPtrEnd && (NULL != bufPtr))
{
if (nout == width)
{
printf ("\n");
nout = 0;
}
printf ("%02x ", *bufPtr);
bufPtr++;
nout++;
}
puts ("");
}
int
main (int argc, char **argv)