]> The Tcpdump Group git mirrors - libpcap/commitdiff
Clean up some name to address translation code.
authorDenis Ovsienko <[email protected]>
Sun, 26 Jan 2025 09:05:34 +0000 (09:05 +0000)
committerDenis Ovsienko <[email protected]>
Thu, 30 Jan 2025 19:45:58 +0000 (19:45 +0000)
Both nametoaddr.c and etherent.c are always compiled and each has an
identical static copy of xdtoi().  Rename the function to
pcapint_xdtoi(), leave only one copy in nametoaddr.c and add a
declaration to nametoaddr.h.

Rename __pcap_atoin() to pcapint_atoin(), rename __pcap_atodn() to
pcapint_atodn().  Add a header guard to nametoaddr.h.

etherent.c
gencode.c
nametoaddr.c
nametoaddr.h

index fd228b81f61da36427b2236a9817bf8deb334720..f367c9ddf6f90328f71545c43592ff394192fe98 100644 (file)
@@ -28,6 +28,7 @@
 #include <string.h>
 
 #include "pcap-int.h"
+#include "nametoaddr.h"
 
 #include <pcap/namedb.h>
 
 static inline int skip_space(FILE *);
 static inline int skip_line(FILE *);
 
-/* Hex digit to integer. */
-static inline u_char
-xdtoi(u_char c)
-{
-       if (c >= '0' && c <= '9')
-               return (u_char)(c - '0');
-       else if (c >= 'a' && c <= 'f')
-               return (u_char)(c - 'a' + 10);
-       else
-               return (u_char)(c - 'A' + 10);
-}
-
 /*
  * Skip linear white space (space and tab) and any CRs before LF.
  * Stop when we hit a non-white-space character or an end-of-line LF.
@@ -109,13 +98,13 @@ pcap_next_etherent(FILE *fp)
 
                /* must be the start of an address */
                for (i = 0; i < 6; i += 1) {
-                       d = xdtoi((u_char)c);
+                       d = pcapint_xdtoi((u_char)c);
                        c = getc(fp);
                        if (c == EOF)
                                return (NULL);
                        if (PCAP_ISXDIGIT(c)) {
                                d <<= 4;
-                               d |= xdtoi((u_char)c);
+                               d |= pcapint_xdtoi((u_char)c);
                                c = getc(fp);
                                if (c == EOF)
                                        return (NULL);
index 5c94e60d45732d46d311a3a84023d6e9a44d8195..dd0189b8b0f6b5f0273ee0b6ceda2c773717108a 100644 (file)
--- a/gencode.c
+++ b/gencode.c
@@ -7422,14 +7422,14 @@ gen_mcode(compiler_state_t *cstate, const char *s1, const char *s2,
        if (setjmp(cstate->top_ctx))
                return (NULL);
 
-       nlen = __pcap_atoin(s1, &n);
+       nlen = pcapint_atoin(s1, &n);
        if (nlen < 0)
                bpf_error(cstate, "invalid IPv4 address '%s'", s1);
        /* Promote short ipaddr */
        n <<= 32 - nlen;
 
        if (s2 != NULL) {
-               mlen = __pcap_atoin(s2, &m);
+               mlen = pcapint_atoin(s2, &m);
                if (mlen < 0)
                        bpf_error(cstate, "invalid IPv4 address '%s'", s2);
                /* Promote short ipaddr */
@@ -7497,7 +7497,7 @@ gen_ncode(compiler_state_t *cstate, const char *s, bpf_u_int32 v, struct qual q)
                 * {N}.{N}.{N}.{N}, of which only the first potentially stands
                 * for a valid DECnet address.
                 */
-               vlen = __pcap_atodn(s, &v);
+               vlen = pcapint_atodn(s, &v);
                if (vlen == 0)
                        bpf_error(cstate, "invalid DECnet address '%s'", s);
        } else {
@@ -7506,7 +7506,7 @@ gen_ncode(compiler_state_t *cstate, const char *s, bpf_u_int32 v, struct qual q)
                 * {N}.{N}.{N}.{N}, all of which potentially stand for a valid
                 * IPv4 address.
                 */
-               vlen = __pcap_atoin(s, &v);
+               vlen = pcapint_atoin(s, &v);
                if (vlen < 0)
                        bpf_error(cstate, "invalid IPv4 address '%s'", s);
        }
index cc4ab86806cdfee8a304bb22ea348b63e3b2f134..ee74ffdfb3d6aef22e8e86c580e1a29f5993209c 100644 (file)
@@ -601,8 +601,8 @@ pcap_nametollc(const char *s)
 }
 
 /* Hex digit to 8-bit unsigned integer. */
-static inline u_char
-xdtoi(u_char c)
+u_char
+pcapint_xdtoi(const u_char c)
 {
        if (c >= '0' && c <= '9')
                return (u_char)(c - '0');
@@ -613,7 +613,7 @@ xdtoi(u_char c)
 }
 
 int
-__pcap_atoin(const char *s, bpf_u_int32 *addr)
+pcapint_atoin(const char *s, bpf_u_int32 *addr)
 {
        u_int n;
        int len;
@@ -649,7 +649,7 @@ __pcap_atoin(const char *s, bpf_u_int32 *addr)
  * normal addressing purposes, but either can appear on the wire.
  */
 int
-__pcap_atodn(const char *s, bpf_u_int32 *addr)
+pcapint_atodn(const char *s, bpf_u_int32 *addr)
 {
 #define AREASHIFT 10
 #define AREAMASK 0176000
@@ -753,10 +753,10 @@ pcap_ether_aton(const char *s)
        while (*s) {
                if (*s == ':' || *s == '.' || *s == '-')
                        s += 1;
-               d = xdtoi(*s++);
+               d = pcapint_xdtoi(*s++);
                if (PCAP_ISXDIGIT(*s)) {
                        d <<= 4;
-                       d |= xdtoi(*s++);
+                       d |= pcapint_xdtoi(*s++);
                }
                *ep++ = d;
        }
index 8cf718acf610afeed99241da892712dc62935fcf..863918fad2fd0e30919453615a989ad84948136f 100644 (file)
@@ -31,6 +31,9 @@
  * SUCH DAMAGE.
  */
 
+#ifndef nametoaddr_h_
+#define nametoaddr_h_
+
 #ifdef __cplusplus
 extern "C" {
 #endif
@@ -39,9 +42,12 @@ extern "C" {
  * Routines used for name-or-address-string-to-address resolution
  * that are *not* exported to code using libpcap.
  */
-int __pcap_atodn(const char *, bpf_u_int32 *);
-int __pcap_atoin(const char *, bpf_u_int32 *);
+extern int pcapint_atodn(const char *, bpf_u_int32 *);
+extern int pcapint_atoin(const char *, bpf_u_int32 *);
+extern u_char pcapint_xdtoi(const u_char);
 
 #ifdef __cplusplus
 }
 #endif
+
+#endif // nametoaddr_h_