use size_t where appropriate instead of unsigned int

This commit is contained in:
Sean Middleditch 2009-03-19 02:08:46 -04:00
parent 8daf77446e
commit 340a51b1e0
5 changed files with 29 additions and 31 deletions

8
README
View File

@ -87,7 +87,7 @@ IIa. Initialization
IIb. Receiving Data IIb. Receiving Data
void libtelnet_push(libtelnet_t *telnet, void libtelnet_push(libtelnet_t *telnet,
const unsigned char *buffer, unsigned int size, void *user_data); const char *buffer, unsigned int size, void *user_data);
When your application receives data over the socket from the When your application receives data over the socket from the
remote end, it must pass the received bytes into this function. remote end, it must pass the received bytes into this function.
@ -125,12 +125,12 @@ IIc. Sending Data
negotiate. negotiate.
void libtelnet_send_data(libtelnet_t *telnet, void libtelnet_send_data(libtelnet_t *telnet,
const unsigned char *buffer, unsigned int size); const char *buffer, unsigned int size);
Sends raw data, which would be either the process output from Sends raw data, which would be either the process output from
a server or the user input from a client. a server or the user input from a client.
void libtelnet_send_subnegotiation(libtelnet_t *telnet, void libtelnet_send_subnegotiation(libtelnet_t *telnet,
unsigned char telopt, const unsigned char *buffer, unsigned char telopt, const char *buffer,
unsigned int size); unsigned int size);
Sends a TELNET sub-negotiation command. The telopt parameter Sends a TELNET sub-negotiation command. The telopt parameter
is the sub-negotiation option. is the sub-negotiation option.
@ -187,7 +187,7 @@ IId. Event Handling
pointer passed to libtelnet_init(). pointer passed to libtelnet_init().
struct libtelnet_event_t { struct libtelnet_event_t {
const unsigned char *buffer; const char *buffer;
unsigned int size; unsigned int size;
libtelnet_event_type_t type; libtelnet_event_type_t type;
unsigned char command; unsigned char command;

View File

@ -35,21 +35,21 @@
#define RFC1143_WANTYES_OP (RFC1143_WANTYES|RFC1143_OP) #define RFC1143_WANTYES_OP (RFC1143_WANTYES|RFC1143_OP)
/* buffer sizes */ /* buffer sizes */
static const unsigned int _buffer_sizes[] = { static const size_t _buffer_sizes[] = {
0, 0,
512, 512,
2048, 2048,
8192, 8192,
16384, 16384,
}; };
static const unsigned int _buffer_sizes_count = sizeof(_buffer_sizes) / static const size_t _buffer_sizes_count = sizeof(_buffer_sizes) /
sizeof(_buffer_sizes[0]); sizeof(_buffer_sizes[0]);
/* event dispatch helper; return value is value of the accept field of the /* event dispatch helper; return value is value of the accept field of the
* event struct after dispatch; used for the funky REQUEST event */ * event struct after dispatch; used for the funky REQUEST event */
static int _event(libtelnet_t *telnet, libtelnet_event_type_t type, static int _event(libtelnet_t *telnet, libtelnet_event_type_t type,
unsigned char command, unsigned char telopt, unsigned char command, unsigned char telopt,
const char *buffer, unsigned int size) { const char *buffer, size_t size) {
libtelnet_event_t ev; libtelnet_event_t ev;
ev.buffer = buffer; ev.buffer = buffer;
ev.size = size; ev.size = size;
@ -129,7 +129,7 @@ libtelnet_error_t _init_zlib(libtelnet_t *telnet, int deflate, int err_fatal) {
/* push bytes out, compressing them first if need be */ /* push bytes out, compressing them first if need be */
static void _send(libtelnet_t *telnet, const char *buffer, static void _send(libtelnet_t *telnet, const char *buffer,
unsigned int size) { size_t size) {
#ifdef HAVE_ZLIB #ifdef HAVE_ZLIB
/* if we have a deflate (compression) zlib box, use it */ /* if we have a deflate (compression) zlib box, use it */
if (telnet->z != 0 && telnet->flags & LIBTELNET_PFLAG_DEFLATE) { if (telnet->z != 0 && telnet->flags & LIBTELNET_PFLAG_DEFLATE) {
@ -417,7 +417,7 @@ void libtelnet_free(libtelnet_t *telnet) {
static libtelnet_error_t _buffer_byte(libtelnet_t *telnet, static libtelnet_error_t _buffer_byte(libtelnet_t *telnet,
unsigned char byte) { unsigned char byte) {
char *new_buffer; char *new_buffer;
unsigned int i; size_t i;
/* check if we're out of room */ /* check if we're out of room */
if (telnet->buffer_pos == telnet->buffer_size) { if (telnet->buffer_pos == telnet->buffer_size) {
@ -455,9 +455,9 @@ static libtelnet_error_t _buffer_byte(libtelnet_t *telnet,
} }
static void _process(libtelnet_t *telnet, const char *buffer, static void _process(libtelnet_t *telnet, const char *buffer,
unsigned int size) { size_t size) {
unsigned char byte; unsigned char byte;
unsigned int i, start; size_t i, start;
for (i = start = 0; i != size; ++i) { for (i = start = 0; i != size; ++i) {
byte = buffer[i]; byte = buffer[i];
switch (telnet->state) { switch (telnet->state) {
@ -615,7 +615,7 @@ static void _process(libtelnet_t *telnet, const char *buffer,
/* push a bytes into the state tracker */ /* push a bytes into the state tracker */
void libtelnet_push(libtelnet_t *telnet, const char *buffer, void libtelnet_push(libtelnet_t *telnet, const char *buffer,
unsigned int size) { size_t size) {
#ifdef HAVE_ZLIB #ifdef HAVE_ZLIB
/* if we have an inflate (decompression) zlib stream, use it */ /* if we have an inflate (decompression) zlib stream, use it */
if (telnet->z != 0 && !(telnet->flags & LIBTELNET_PFLAG_DEFLATE)) { if (telnet->z != 0 && !(telnet->flags & LIBTELNET_PFLAG_DEFLATE)) {
@ -797,8 +797,8 @@ void libtelnet_send_negotiate(libtelnet_t *telnet, unsigned char cmd,
/* send non-command data (escapes IAC bytes) */ /* send non-command data (escapes IAC bytes) */
void libtelnet_send_data(libtelnet_t *telnet, const char *buffer, void libtelnet_send_data(libtelnet_t *telnet, const char *buffer,
unsigned int size) { size_t size) {
unsigned int i, l; size_t i, l;
for (l = i = 0; i != size; ++i) { for (l = i = 0; i != size; ++i) {
/* dump prior portion of text, send escaped bytes */ /* dump prior portion of text, send escaped bytes */
@ -820,7 +820,7 @@ void libtelnet_send_data(libtelnet_t *telnet, const char *buffer,
/* send sub-request */ /* send sub-request */
void libtelnet_send_subnegotiation(libtelnet_t *telnet, unsigned char telopt, void libtelnet_send_subnegotiation(libtelnet_t *telnet, unsigned char telopt,
const char *buffer, unsigned int size) { const char *buffer, size_t size) {
libtelnet_send_telopt(telnet, LIBTELNET_SB, telopt); libtelnet_send_telopt(telnet, LIBTELNET_SB, telopt);
libtelnet_send_data(telnet, buffer, size); libtelnet_send_data(telnet, buffer, size);
libtelnet_send_command(telnet, LIBTELNET_SE); libtelnet_send_command(telnet, LIBTELNET_SE);

View File

@ -136,7 +136,7 @@ typedef enum libtelnet_event_type_t libtelnet_event_type_t;
struct libtelnet_event_t { struct libtelnet_event_t {
/* data buffer: for DATA, SEND, SUBNEGOTIATION, and ERROR events */ /* data buffer: for DATA, SEND, SUBNEGOTIATION, and ERROR events */
const char *buffer; const char *buffer;
unsigned int size; size_t size;
/* type of event */ /* type of event */
enum libtelnet_event_type_t type; enum libtelnet_event_type_t type;
/* IAC command */ /* IAC command */
@ -172,9 +172,9 @@ struct libtelnet_t {
/* sub-request buffer */ /* sub-request buffer */
char *buffer; char *buffer;
/* current size of the buffer */ /* current size of the buffer */
unsigned int buffer_size; size_t buffer_size;
/* current buffer write position (also length of buffer data) */ /* current buffer write position (also length of buffer data) */
unsigned int buffer_pos; size_t buffer_pos;
/* current state */ /* current state */
enum libtelnet_state_t state; enum libtelnet_state_t state;
/* option flags */ /* option flags */
@ -194,7 +194,7 @@ extern void libtelnet_free(libtelnet_t *telnet);
/* push a byte buffer into the state tracker */ /* push a byte buffer into the state tracker */
extern void libtelnet_push(libtelnet_t *telnet, const char *buffer, extern void libtelnet_push(libtelnet_t *telnet, const char *buffer,
unsigned int size); size_t size);
/* send an iac command */ /* send an iac command */
extern void libtelnet_send_command(libtelnet_t *telnet, unsigned char cmd); extern void libtelnet_send_command(libtelnet_t *telnet, unsigned char cmd);
@ -212,7 +212,7 @@ extern void libtelnet_send_negotiate(libtelnet_t *telnet, unsigned char cmd,
/* send non-command data (escapes IAC bytes) */ /* send non-command data (escapes IAC bytes) */
extern void libtelnet_send_data(libtelnet_t *telnet, extern void libtelnet_send_data(libtelnet_t *telnet,
const char *buffer, unsigned int size); const char *buffer, size_t size);
/* send sub-request, equivalent to: /* send sub-request, equivalent to:
* libtelnet_send_telopt(telnet, LIBTELNET_SB, telopt) * libtelnet_send_telopt(telnet, LIBTELNET_SB, telopt)
@ -222,7 +222,7 @@ extern void libtelnet_send_data(libtelnet_t *telnet,
* thare are most easily implemented with a series of send_data calls. * thare are most easily implemented with a series of send_data calls.
*/ */
extern void libtelnet_send_subnegotiation(libtelnet_t *telnet, extern void libtelnet_send_subnegotiation(libtelnet_t *telnet,
unsigned char telopt, const char *buffer, unsigned int size); unsigned char telopt, const char *buffer, size_t size);
/* begin sending compressed data (server only) */ /* begin sending compressed data (server only) */
extern void libtelnet_begin_compress2(libtelnet_t *telnet); extern void libtelnet_begin_compress2(libtelnet_t *telnet);

View File

@ -57,7 +57,7 @@ static void _input(char *buffer, int size) {
} }
} }
static void _send(int sock, const char *buffer, unsigned int size) { static void _send(int sock, const char *buffer, size_t size) {
int rs; int rs;
/* send data */ /* send data */
@ -132,7 +132,7 @@ static void _event_handler(libtelnet_t *telnet, libtelnet_event_t *ev,
break; break;
/* error */ /* error */
case LIBTELNET_EV_ERROR: case LIBTELNET_EV_ERROR:
fprintf(stderr, "ERROR: %.*s\n", ev->size, ev->buffer); fprintf(stderr, "ERROR: %s\n", ev->buffer);
exit(1); exit(1);
default: default:
/* ignore */ /* ignore */

View File

@ -129,8 +129,8 @@ static const char *get_opt(unsigned char opt) {
} }
} }
static void print_buffer(const char *buffer, unsigned int size) { static void print_buffer(const char *buffer, size_t size) {
unsigned int i; size_t i;
for (i = 0; i != size; ++i) { for (i = 0; i != size; ++i) {
if (buffer[i] == ' ' || (isprint(buffer[i]) && !isspace(buffer[i]))) if (buffer[i] == ' ' || (isprint(buffer[i]) && !isspace(buffer[i])))
printf("%c", (char)buffer[i]); printf("%c", (char)buffer[i]);
@ -142,7 +142,7 @@ static void print_buffer(const char *buffer, unsigned int size) {
} }
} }
static void _send(int sock, const char *buffer, unsigned int size) { static void _send(int sock, const char *buffer, size_t size) {
int rs; int rs;
/* send data */ /* send data */
@ -227,7 +227,7 @@ static void _event_handler(libtelnet_t *telnet, libtelnet_event_t *ev,
printf("%s SUB %d (%s)", conn->name, (int)ev->telopt, printf("%s SUB %d (%s)", conn->name, (int)ev->telopt,
get_opt(ev->telopt)); get_opt(ev->telopt));
if (ev->size > 0) { if (ev->size > 0) {
printf(" [%u]: ", ev->size); printf(" [%zi]: ", ev->size);
print_buffer(ev->buffer, ev->size); print_buffer(ev->buffer, ev->size);
} }
printf(COLOR_NORMAL "\n"); printf(COLOR_NORMAL "\n");
@ -242,13 +242,11 @@ static void _event_handler(libtelnet_t *telnet, libtelnet_event_t *ev,
break; break;
/* warning */ /* warning */
case LIBTELNET_EV_WARNING: case LIBTELNET_EV_WARNING:
printf("%s WARNING: %.*s" COLOR_NORMAL "\n", conn->name, ev->size, printf("%s WARNING: %s" COLOR_NORMAL "\n", conn->name, ev->buffer);
ev->buffer);
break; break;
/* error */ /* error */
case LIBTELNET_EV_ERROR: case LIBTELNET_EV_ERROR:
printf("%s ERROR: %.*s" COLOR_NORMAL "\n", conn->name, ev->size, printf("%s ERROR: %s" COLOR_NORMAL "\n", conn->name, ev->buffer);
ev->buffer);
exit(1); exit(1);
} }
} }