mirror of
https://github.com/nmap/nmap.git
synced 2026-02-06 21:46:34 +00:00
Update to libpcap 1.9.1 (initial commit, no Nmap-specific patches)
This commit is contained in:
101
libpcap/missing/asprintf.c
Normal file
101
libpcap/missing/asprintf.c
Normal file
@@ -0,0 +1,101 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
#include "portability.h"
|
||||
|
||||
/*
|
||||
* vasprintf() and asprintf() for platforms with a C99-compliant
|
||||
* snprintf() - so that, if you format into a 1-byte buffer, it
|
||||
* will return how many characters it would have produced had
|
||||
* it been given an infinite-sized buffer.
|
||||
*/
|
||||
int
|
||||
pcap_vasprintf(char **strp, const char *format, va_list args)
|
||||
{
|
||||
char buf;
|
||||
int len;
|
||||
size_t str_size;
|
||||
char *str;
|
||||
int ret;
|
||||
|
||||
/*
|
||||
* XXX - the C99 standard says, in section 7.19.6.5 "Thes
|
||||
* nprintf function":
|
||||
*
|
||||
* The snprintf function is equivalent to fprintf, except that
|
||||
* the output is written into an array (specified by argument s)
|
||||
* rather than to a stream. If n is zero, nothing is written,
|
||||
* and s may be a null pointer. Otherwise, output characters
|
||||
* beyond the n-1st are discarded rather than being written
|
||||
* to the array, and a null character is written at the end
|
||||
* of the characters actually written into the array.
|
||||
*
|
||||
* ...
|
||||
*
|
||||
* The snprintf function returns the number of characters that
|
||||
* would have been written had n been sufficiently large, not
|
||||
* counting the terminating null character, or a negative value
|
||||
* if an encoding error occurred. Thus, the null-terminated
|
||||
* output has been completely written if and only if the returned
|
||||
* value is nonnegative and less than n.
|
||||
*
|
||||
* That doesn't make it entirely clear whether, if a null buffer
|
||||
* pointer and a zero count are passed, it will return the number
|
||||
* of characters that would have been written had a buffer been
|
||||
* passed.
|
||||
*
|
||||
* And, even if C99 *does*, in fact, say it has to work, it
|
||||
* doesn't work in Solaris 8, for example - it returns -1 for
|
||||
* NULL/0, but returns the correct character count for a 1-byte
|
||||
* buffer.
|
||||
*
|
||||
* So we pass a one-character pointer in order to find out how
|
||||
* many characters this format and those arguments will need
|
||||
* without actually generating any more of those characters
|
||||
* than we need.
|
||||
*
|
||||
* (The fact that it might happen to work with GNU libc or with
|
||||
* various BSD libcs is completely uninteresting, as those tend
|
||||
* to have asprintf() already and thus don't even *need* this
|
||||
* code; this is for use in those UN*Xes that *don't* have
|
||||
* asprintf().)
|
||||
*/
|
||||
len = vsnprintf(&buf, sizeof buf, format, args);
|
||||
if (len == -1) {
|
||||
*strp = NULL;
|
||||
return (-1);
|
||||
}
|
||||
str_size = len + 1;
|
||||
str = malloc(str_size);
|
||||
if (str == NULL) {
|
||||
*strp = NULL;
|
||||
return (-1);
|
||||
}
|
||||
ret = vsnprintf(str, str_size, format, args);
|
||||
if (ret == -1) {
|
||||
free(str);
|
||||
*strp = NULL;
|
||||
return (-1);
|
||||
}
|
||||
*strp = str;
|
||||
/*
|
||||
* vsnprintf() shouldn't truncate the string, as we have
|
||||
* allocated a buffer large enough to hold the string, so its
|
||||
* return value should be the number of characters written.
|
||||
*/
|
||||
return (ret);
|
||||
}
|
||||
|
||||
int
|
||||
pcap_asprintf(char **strp, const char *format, ...)
|
||||
{
|
||||
va_list args;
|
||||
int ret;
|
||||
|
||||
va_start(args, format);
|
||||
ret = pcap_vasprintf(strp, format, args);
|
||||
va_end(args);
|
||||
return (ret);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 1995-1999 Kungliga Tekniska Högskolan
|
||||
* Copyright (c) 1995-1999 Kungliga Tekniska Högskolan
|
||||
* (Royal Institute of Technology, Stockholm, Sweden).
|
||||
* All rights reserved.
|
||||
*
|
||||
@@ -31,6 +31,10 @@
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/*
|
||||
* We use this for platforms that don't have snprintf() at all.
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <config.h>
|
||||
#endif
|
||||
@@ -42,7 +46,7 @@
|
||||
#include <ctype.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#include <pcap-int.h>
|
||||
#include "portability.h"
|
||||
|
||||
enum format_flags {
|
||||
minus_flag = 1,
|
||||
@@ -525,6 +529,7 @@ pcap_asnprintf (char **ret, size_t max_sz, const char *format, ...)
|
||||
|
||||
va_start(args, format);
|
||||
val = pcap_vasnprintf (ret, max_sz, format, args);
|
||||
va_end(args);
|
||||
|
||||
#ifdef PARANOIA
|
||||
{
|
||||
@@ -534,14 +539,15 @@ pcap_asnprintf (char **ret, size_t max_sz, const char *format, ...)
|
||||
if (tmp == NULL)
|
||||
abort ();
|
||||
|
||||
va_start(args, format);
|
||||
ret2 = pcap_vsprintf (tmp, format, args);
|
||||
va_end(args);
|
||||
if (val != ret2 || strcmp(*ret, tmp))
|
||||
abort ();
|
||||
free (tmp);
|
||||
}
|
||||
#endif
|
||||
|
||||
va_end(args);
|
||||
return val;
|
||||
}
|
||||
#endif
|
||||
|
||||
61
libpcap/missing/strlcat.c
Normal file
61
libpcap/missing/strlcat.c
Normal file
@@ -0,0 +1,61 @@
|
||||
/* $OpenBSD: pcap_strlcat.c,v 1.15 2015/03/02 21:41:08 millert Exp $ */
|
||||
|
||||
/*
|
||||
* Copyright (c) 1998, 2015 Todd C. Miller <Todd.Miller@courtesan.com>
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <config.h>
|
||||
#endif
|
||||
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "portability.h"
|
||||
|
||||
/*
|
||||
* Appends src to string dst of size dsize (unlike strncat, dsize is the
|
||||
* full size of dst, not space left). At most dsize-1 characters
|
||||
* will be copied. Always NUL terminates (unless dsize <= strlen(dst)).
|
||||
* Returns strlen(src) + MIN(dsize, strlen(initial dst)).
|
||||
* If retval >= dsize, truncation occurred.
|
||||
*/
|
||||
size_t
|
||||
pcap_strlcat(char * restrict dst, const char * restrict src, size_t dsize)
|
||||
{
|
||||
const char *odst = dst;
|
||||
const char *osrc = src;
|
||||
size_t n = dsize;
|
||||
size_t dlen;
|
||||
|
||||
/* Find the end of dst and adjust bytes left but don't go past end. */
|
||||
while (n-- != 0 && *dst != '\0')
|
||||
dst++;
|
||||
dlen = dst - odst;
|
||||
n = dsize - dlen;
|
||||
|
||||
if (n-- == 0)
|
||||
return(dlen + strlen(src));
|
||||
while (*src != '\0') {
|
||||
if (n != 0) {
|
||||
*dst++ = *src;
|
||||
n--;
|
||||
}
|
||||
src++;
|
||||
}
|
||||
*dst = '\0';
|
||||
|
||||
return(dlen + (src - osrc)); /* count does not include NUL */
|
||||
}
|
||||
56
libpcap/missing/strlcpy.c
Normal file
56
libpcap/missing/strlcpy.c
Normal file
@@ -0,0 +1,56 @@
|
||||
/* $OpenBSD: pcap_strlcpy.c,v 1.12 2015/01/15 03:54:12 millert Exp $ */
|
||||
|
||||
/*
|
||||
* Copyright (c) 1998, 2015 Todd C. Miller <Todd.Miller@courtesan.com>
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <config.h>
|
||||
#endif
|
||||
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "portability.h"
|
||||
|
||||
/*
|
||||
* Copy string src to buffer dst of size dsize. At most dsize-1
|
||||
* chars will be copied. Always NUL terminates (unless dsize == 0).
|
||||
* Returns strlen(src); if retval >= dsize, truncation occurred.
|
||||
*/
|
||||
size_t
|
||||
pcap_strlcpy(char * restrict dst, const char * restrict src, size_t dsize)
|
||||
{
|
||||
const char *osrc = src;
|
||||
size_t nleft = dsize;
|
||||
|
||||
/* Copy as many bytes as will fit. */
|
||||
if (nleft != 0) {
|
||||
while (--nleft != 0) {
|
||||
if ((*dst++ = *src++) == '\0')
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* Not enough room in dst, add NUL and traverse rest of src. */
|
||||
if (nleft == 0) {
|
||||
if (dsize != 0)
|
||||
*dst = '\0'; /* NUL-terminate dst */
|
||||
while (*src++)
|
||||
;
|
||||
}
|
||||
|
||||
return(src - osrc - 1); /* count does not include NUL */
|
||||
}
|
||||
51
libpcap/missing/win_asprintf.c
Normal file
51
libpcap/missing/win_asprintf.c
Normal file
@@ -0,0 +1,51 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
#include "portability.h"
|
||||
|
||||
int
|
||||
pcap_vasprintf(char **strp, const char *format, va_list args)
|
||||
{
|
||||
int len;
|
||||
size_t str_size;
|
||||
char *str;
|
||||
int ret;
|
||||
|
||||
len = _vscprintf(format, args);
|
||||
if (len == -1) {
|
||||
*strp = NULL;
|
||||
return (-1);
|
||||
}
|
||||
str_size = len + 1;
|
||||
str = malloc(str_size);
|
||||
if (str == NULL) {
|
||||
*strp = NULL;
|
||||
return (-1);
|
||||
}
|
||||
ret = pcap_vsnprintf(str, str_size, format, args);
|
||||
if (ret == -1) {
|
||||
free(str);
|
||||
*strp = NULL;
|
||||
return (-1);
|
||||
}
|
||||
*strp = str;
|
||||
/*
|
||||
* pcap_vsnprintf() shouldn't truncate the string, as we have
|
||||
* allocated a buffer large enough to hold the string, so its
|
||||
* return value should be the number of characters printed.
|
||||
*/
|
||||
return (ret);
|
||||
}
|
||||
|
||||
int
|
||||
pcap_asprintf(char **strp, const char *format, ...)
|
||||
{
|
||||
va_list args;
|
||||
int ret;
|
||||
|
||||
va_start(args, format);
|
||||
ret = pcap_vasprintf(strp, format, args);
|
||||
va_end(args);
|
||||
return (ret);
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
#include "portability.h"
|
||||
|
||||
int
|
||||
pcap_vsnprintf(char *str, size_t str_size, const char *format, va_list args)
|
||||
{
|
||||
@@ -13,6 +15,16 @@ pcap_vsnprintf(char *str, size_t str_size, const char *format, va_list args)
|
||||
* that str is null-terminated, but C99's vsnprintf()
|
||||
* and snprintf() do, and we want to offer C99 behavior,
|
||||
* so forcibly null-terminate the string.
|
||||
*
|
||||
* We don't, however, offer C99 behavior for the return
|
||||
* value; _vsnprintf_s() returns -1, not the number of
|
||||
* characters that would have been put into the buffer
|
||||
* had it been large enough, if the string is truncated.
|
||||
* The only way to get that value is to use _vscprintf();
|
||||
* getting that count isn't worth the re-formatting.
|
||||
*
|
||||
* XXX - does _vsnprintf_s() return -1 on a formatting
|
||||
* error?
|
||||
*/
|
||||
str[str_size - 1] = '\0';
|
||||
return (ret);
|
||||
|
||||
Reference in New Issue
Block a user