Compare commits

..

1 Commits

Author SHA1 Message Date
(no author)
b44b7fbe9a This commit was manufactured by cvs2svn to create branch
'DARIN_UnixAsyncDNS_20010109_BASE'.

git-svn-id: svn://10.0.0.236/branches/DARIN_UnixAsyncDNS_20010109_BASE@83089 18797224-902f-48f8-a5cc-f745e15eee43
2000-11-29 23:28:05 +00:00
19 changed files with 4393 additions and 5885 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -1,219 +0,0 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
* ex: set tabstop=8 softtabstop=2 shiftwidth=2 expandtab:
*
* The contents of this file are subject to the Netscape Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/NPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All
* Rights Reserved.
*
* Contributor(s):
*/
#ifndef nsFontMetricsGTK_h__
#define nsFontMetricsGTK_h__
#include "nsDeviceContextGTK.h"
#include "nsIFontMetrics.h"
#include "nsIFontEnumerator.h"
#include "nsFont.h"
#include "nsString.h"
#include "nsUnitConversion.h"
#include "nsIDeviceContext.h"
#include "nsCRT.h"
#include "nsCOMPtr.h"
#include "nsRenderingContextGTK.h"
#include "nsICharRepresentable.h"
#include <gdk/gdk.h>
#include <gdk/gdkx.h>
#undef FONT_HAS_GLYPH
#define FONT_HAS_GLYPH(map, char) IS_REPRESENTABLE(map, char)
typedef struct nsFontCharSetInfo nsFontCharSetInfo;
typedef gint (*nsFontCharSetConverter)(nsFontCharSetInfo* aSelf,
XFontStruct* aFont, const PRUnichar* aSrcBuf, PRInt32 aSrcLen,
char* aDestBuf, PRInt32 aDestLen);
struct nsFontCharSet;
struct nsFontFamily;
struct nsFontNode;
struct nsFontStretch;
class nsFontGTKUserDefined;
class nsFontMetricsGTK;
class nsFontGTK
{
public:
nsFontGTK();
virtual ~nsFontGTK();
NS_DECL_AND_IMPL_ZEROING_OPERATOR_NEW
void LoadFont(void);
PRBool IsEmptyFont(GdkFont*);
inline int SupportsChar(PRUnichar aChar)
{ return mFont && FONT_HAS_GLYPH(mMap, aChar); };
virtual GdkFont* GetGDKFont(void);
virtual PRBool GetGDKFontIs10646(void);
virtual gint GetWidth(const PRUnichar* aString, PRUint32 aLength) = 0;
virtual gint DrawString(nsRenderingContextGTK* aContext,
nsDrawingSurfaceGTK* aSurface, nscoord aX,
nscoord aY, const PRUnichar* aString,
PRUint32 aLength) = 0;
#ifdef MOZ_MATHML
// bounding metrics for a string
// remember returned values are not in app units
// - to emulate GetWidth () above
virtual nsresult
GetBoundingMetrics(const PRUnichar* aString,
PRUint32 aLength,
nsBoundingMetrics& aBoundingMetrics) = 0;
#endif
PRUint32* mMap;
nsFontCharSetInfo* mCharSetInfo;
char* mName;
nsFontGTKUserDefined* mUserDefinedFont;
PRUint16 mSize;
PRInt16 mBaselineAdjust;
protected:
GdkFont* mFont;
PRBool mAlreadyCalledLoadFont;
};
class nsFontMetricsGTK : public nsIFontMetrics
{
public:
nsFontMetricsGTK();
virtual ~nsFontMetricsGTK();
NS_DECL_AND_IMPL_ZEROING_OPERATOR_NEW
NS_DECL_ISUPPORTS
NS_IMETHOD Init(const nsFont& aFont, nsIAtom* aLangGroup,
nsIDeviceContext* aContext);
NS_IMETHOD Destroy();
NS_IMETHOD GetXHeight(nscoord& aResult);
NS_IMETHOD GetSuperscriptOffset(nscoord& aResult);
NS_IMETHOD GetSubscriptOffset(nscoord& aResult);
NS_IMETHOD GetStrikeout(nscoord& aOffset, nscoord& aSize);
NS_IMETHOD GetUnderline(nscoord& aOffset, nscoord& aSize);
NS_IMETHOD GetHeight(nscoord &aHeight);
NS_IMETHOD GetNormalLineHeight(nscoord &aHeight);
NS_IMETHOD GetLeading(nscoord &aLeading);
NS_IMETHOD GetEmHeight(nscoord &aHeight);
NS_IMETHOD GetEmAscent(nscoord &aAscent);
NS_IMETHOD GetEmDescent(nscoord &aDescent);
NS_IMETHOD GetMaxHeight(nscoord &aHeight);
NS_IMETHOD GetMaxAscent(nscoord &aAscent);
NS_IMETHOD GetMaxDescent(nscoord &aDescent);
NS_IMETHOD GetMaxAdvance(nscoord &aAdvance);
NS_IMETHOD GetFont(const nsFont *&aFont);
NS_IMETHOD GetLangGroup(nsIAtom** aLangGroup);
NS_IMETHOD GetFontHandle(nsFontHandle &aHandle);
virtual nsresult GetSpaceWidth(nscoord &aSpaceWidth);
nsFontGTK* FindFont(PRUnichar aChar);
nsFontGTK* FindUserDefinedFont(PRUnichar aChar);
nsFontGTK* FindStyleSheetSpecificFont(PRUnichar aChar);
nsFontGTK* FindStyleSheetGenericFont(PRUnichar aChar);
nsFontGTK* FindLangGroupPrefFont(nsIAtom* aLangGroup, PRUnichar aChar);
nsFontGTK* FindLangGroupFont(nsIAtom* aLangGroup, PRUnichar aChar, nsCString* aName);
nsFontGTK* FindAnyFont(PRUnichar aChar);
nsFontGTK* FindSubstituteFont(PRUnichar aChar);
nsFontGTK* SearchNode(nsFontNode* aNode, PRUnichar aChar);
nsFontGTK* TryAliases(nsCString* aName, PRUnichar aChar);
nsFontGTK* TryFamily(nsCString* aName, PRUnichar aChar);
nsFontGTK* TryNode(nsCString* aName, PRUnichar aChar);
nsFontGTK* TryNodes(nsAWritableCString &aFFREName, PRUnichar aChar);
nsFontGTK* TryLangGroup(nsIAtom* aLangGroup, nsCString* aName, PRUnichar aChar);
nsFontGTK* AddToLoadedFontsList(nsFontGTK* aFont);
nsFontGTK* PickASizeAndLoad(nsFontStretch* aStretch,
nsFontCharSetInfo* aCharSet,
PRUnichar aChar,
const char *aName);
static nsresult FamilyExists(const nsString& aFontName);
//friend struct nsFontGTK;
nsFontGTK **mLoadedFonts;
PRUint16 mLoadedFontsAlloc;
PRUint16 mLoadedFontsCount;
nsFontGTK *mSubstituteFont;
nsCStringArray mFonts;
PRUint16 mFontsIndex;
nsVoidArray mFontIsGeneric;
nsCAutoString mDefaultFont;
nsCString *mGeneric;
nsCOMPtr<nsIAtom> mLangGroup;
nsCAutoString mUserDefined;
PRUint8 mTriedAllGenerics;
PRUint8 mIsUserDefined;
protected:
void RealizeFont();
nsIDeviceContext *mDeviceContext;
nsFont *mFont;
nsFontGTK *mWesternFont;
nscoord mLeading;
nscoord mEmHeight;
nscoord mEmAscent;
nscoord mEmDescent;
nscoord mMaxHeight;
nscoord mMaxAscent;
nscoord mMaxDescent;
nscoord mMaxAdvance;
nscoord mXHeight;
nscoord mSuperscriptOffset;
nscoord mSubscriptOffset;
nscoord mStrikeoutSize;
nscoord mStrikeoutOffset;
nscoord mUnderlineSize;
nscoord mUnderlineOffset;
nscoord mSpaceWidth;
PRUint16 mPixelSize;
PRUint8 mStretchIndex;
PRUint8 mStyleIndex;
nsFontCharSetConverter mDocConverterType;
};
class nsFontEnumeratorGTK : public nsIFontEnumerator
{
public:
nsFontEnumeratorGTK();
NS_DECL_ISUPPORTS
NS_DECL_NSIFONTENUMERATOR
};
#endif

View File

@@ -0,0 +1,32 @@
#
# The contents of this file are subject to the Netscape Public
# License Version 1.1 (the "License"); you may not use this file
# except in compliance with the License. You may obtain a copy of
# the License at http://www.mozilla.org/NPL/
#
# Software distributed under the License is distributed on an "AS
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
# implied. See the License for the specific language governing
# rights and limitations under the License.
#
# The Original Code is mozilla.org code.
#
# The Initial Developer of the Original Code is Netscape
# Communications Corporation. Portions created by Netscape are
# Copyright (C) 1998 Netscape Communications Corporation. All
# Rights Reserved.
#
# Contributor(s):
#
DEPTH = ../..
topsrcdir = @top_srcdir@
srcdir = @srcdir@
VPATH = @srcdir@
include $(DEPTH)/config/autoconf.mk
DIRS = public src
include $(topsrcdir)/config/rules.mk

View File

@@ -0,0 +1,48 @@
#
# The contents of this file are subject to the Netscape Public
# License Version 1.1 (the "License"); you may not use this file
# except in compliance with the License. You may obtain a copy of
# the License at http://www.mozilla.org/NPL/
#
# Software distributed under the License is distributed on an "AS
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
# implied. See the License for the specific language governing
# rights and limitations under the License.
#
# The Original Code is mozilla.org code.
#
# The Initial Developer of the Original Code is Netscape
# Communications Corporation. Portions created by Netscape are
# Copyright (C) 1998 Netscape Communications Corporation. All
# Rights Reserved.
#
# Contributor(s):
#
DEPTH = ../../..
topsrcdir = @top_srcdir@
srcdir = @srcdir@
VPATH = @srcdir@
include $(DEPTH)/config/autoconf.mk
MODULE = necko
CPPSRCS = nsDnsAsyncLookup.cpp
SIMPLE_PROGRAMS = $(CPPSRCS:.cpp=)
EXPORTS = \
unix_dns.h \
$(NULL)
LIBS = \
$(NSPR_LIBS) \
$(NULL)
include $(topsrcdir)/config/rules.mk
# DEFINES += -D"GETHOSTBYNAME_DELAY=5" -DNO_SOCKS_NS_KLUDGE
DEFINES += -DNO_SOCKS_NS_KLUDGE

View File

@@ -0,0 +1,626 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Netscape Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/NPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All
* Rights Reserved.
*
* Contributor(s):
*/
/*
* nsDnsAsyncLookup.cpp --- standalone lightweight process to handle
* asyncrhonous dns lookup on Unix.
*/
/* Compile-time options:
* -DGETHOSTBYNAME_DELAY=N
* to insert an artificial delay of N seconds before each
* call to gethostbyname (in order to simulate DNS lossage.)
*
* -DNO_SOCKS_NS_KLUDGE
* Set this to *disable* the $SOCKS_NS kludge. Otherwise,
* that environment variable will be consulted for use as an
* alternate DNS root. It's historical; don't ask me...
*/
#if defined(XP_UNIX) && defined(UNIX_ASYNC_DNS)
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <netdb.h>
#include <signal.h>
#include <assert.h>
#include <sys/un.h>
#include "nspr.h"
#include "nsCRT.h"
#include "unix_dns.h"
#if defined(AIX) || defined(__linux)
#include <sys/select.h> // for fd_set
#endif
#if !defined(NO_SOCKS_NS_KLUDGE)
#include <arpa/inet.h> // for in_addr (from nameser.h)
#include <arpa/nameser.h> // for MAXDNAME (from resolv.h)
#include <resolv.h> // for res_init() and _res
#endif
#if defined(__linux)
// Didn't find gettdtablehi() or gettdtablesize() on linux. Using FD_SETSIZE
#define getdtablehi() FD_SETSIZE
#elif !defined(__irix)
// looks like Irix is the only one that has getdtablehi()?
#define getdtablehi() getdtablesize()
// If you find a system doesn't have getdtablesize try #define getdtablesize
// to FD_SETSIZE. And if you encounter a system that doesn't even have
// FD_SETSIZE, just grab your ankles and use 255.
#endif
#define BACKLOG 20 // maximum # of pending connections
#define ASSERT(x) assert(x)
static int listen_fd = -1;
///////////////////////////////////////////////////////////////////////////
// Name: dnsSocksKludge
//
// Description: Gross historical kludge.
// If the environment variable $SOCKS_NS is defined, stomp
// on the host that the DNS code uses for host lookup to be
// a specific ip address.
//
static void
dnsSocksKludge(void)
{
#ifndef NO_SOCKS_NS_KLUDGE
char *ns = getenv("SOCKS_NS");
if (ns && *ns)
{
// Gross hack added to Gross historical kludge - need to
// initialize resolv.h structure first with low-cost call
// to gethostbyname() before hacking _res. Subsequent call
// to gethostbyname() will then pick up $SOCKS_NS address.
//
gethostbyname("localhost");
res_init();
// _res is defined in resolv.h
_res.nsaddr_list[0].sin_addr.s_addr = inet_addr(ns);
_res.nscount = 1;
}
#endif /* !NO_SOCKS_NS_KLUDGE */
}
///////////////////////////////////////////////////////////////////////////
// Name: mySignalHandler
//
// Description: Signal handler. Close down the socket and exit.
//
static void
mySignalHandler (int sig)
{
// close down the socket.
close (listen_fd);
unlink (DNS_SOCK_NAME); //just in case close doesn't remove this
exit (0);
}
//////////////////////////////////////////////////////////////////////////
// Name: displaySysErr
//
// Description: Display system error.
//
static void
displaySysErr(char *name) {
perror ((const char *)name);
exit (1);
}
/////////////////////////////////////////////////////////////////////////
//
// The following data structure is used to hold information about a
// pending lookup request.
typedef struct dns_lookup {
long id; // id to identify this entry
pid_t pid; // process id of the helper process
int fd; // file descriptor used to get result from
int accept_fd; // file descriptor used to send hostent back
char *name; // name to lookup
struct dns_lookup *next; // pointer to the next entry
} dns_lookup;
static dns_lookup *dns_lookup_queue = 0;
////////////////////////////////////////////////////////////////////////
// Name: getNextDnsEntry
//
// Description: Return the idx entry to the caller. Bump the index up
// by 1 before the return. Similiar to an iterator.
//
static dns_lookup*
getNextDnsEntry (int *idx)
{
dns_lookup *obj = dns_lookup_queue;
if (*idx < 0)
return 0;
for (int i = 1; i < *idx; i++)
{
obj = obj->next;
if (!obj)
return 0;
}
*idx += 1;
return obj;
}
////////////////////////////////////////////////////////////////////////
// Name: addToDnsQueue
//
// Description: Add a lookup entry to the queue.
//
static void
addToDnsQueue (dns_lookup* obj)
{
dns_lookup* entry;
obj->next = 0;
if (!dns_lookup_queue)
{
dns_lookup_queue = obj;
return;
}
entry = dns_lookup_queue;
while (entry->next)
entry = entry->next;
entry->next = obj;
}
//////////////////////////////////////////////////////////////////////////
// Name: removeFromDnsQueue
//
// Description: Remove the specified entry from the queue.
//
static void
removeFromDnsQueue (dns_lookup *obj)
{
dns_lookup* entry;
if (!obj || !dns_lookup_queue)
return;
if (obj == dns_lookup_queue)
{
dns_lookup_queue = obj->next;
}
else
{
entry = dns_lookup_queue;
while (obj != entry->next)
entry = entry->next;
if (!entry || !entry->next)
return;
entry->next = obj->next;
}
if (obj->name)
{
free(obj->name);
obj->name = 0;
}
free (obj);
}
///////////////////////////////////////////////////////////////////////////
// Name: newLookupObject
//
// Description: Create a new lookup object and return a pointer to the
// object to the caller. 0 is returned on error.
//
static dns_lookup*
newLookupObject (const char *name)
{
static int dnsId = 0;
ASSERT (name);
char *str = strdup (name);
if (!str) return 0; // MK_OUT_OF_MEMORY
dns_lookup* obj = (dns_lookup *) malloc (sizeof (struct dns_lookup));
if (!obj)
{
free (str);
return 0;
}
memset( obj, 0, sizeof( struct dns_lookup ) );
obj->id = ++dnsId;
obj->name = str;
obj->fd = -1;
obj->accept_fd = -1;
addToDnsQueue (obj);
return (obj);
}
///////////////////////////////////////////////////////////////////////////
// Name: hostentToBytes
//
// Description: Pack the structure `hostent' into a character buffer.
//
//
static void
hostentToBytes (hostent *h, char *buf, int *size)
{
*size = 0;
int len = strlen (h->h_name);
char *p = buf;
*(int *)p = len; // Encode the length of the name
p += sizeof (int);
if (len > 0)
{
strcpy (p, h->h_name);
p += strlen (h->h_name);
}
int n;
// find out the number of aliases are present. The last entry in
// list is 0.
for (n = 0; h->h_aliases[n]; n++);
*(int *)p = n; // Encode the size of aliases into buf
p += sizeof (int);
int i;
// copy aliases to the buffer.
for (i = 0; i < n; i++)
{
len = strlen (h->h_aliases[i]);
*(int *)p = (int) len;
p += sizeof(int);
memcpy (p, h->h_aliases[i], (size_t) len);
p += len;
}
*(int *)p = h->h_addrtype; // Encode the member h_addrtype
p += sizeof (int);
*(int *)p = h->h_length; // Encode the member h_length
p += sizeof (int);
// find out the number of addresses in h_addr_list list.
for (n = 0; h->h_addr_list[n]; n++);
*(int *)p = n; // Encode the size of aliases into buf
p += sizeof (int);
// copy the address list into the buffer.
for (i = 0; i < n; i++)
{
len = strlen (h->h_addr_list[i]);
*(int *)p = (int) len;
p += sizeof(int);
memcpy (p, h->h_addr_list[i], (size_t) len);
p += len;
}
*size = p - buf;
}
////////////////////////////////////////////////////////////////////////
// Name: cancelLookup
//
// Description: Cancel an existing lookup request. Locate the right
// child helper process and kill it.
//
static void
cancelLookup (int id)
{
dns_lookup *obj = dns_lookup_queue;
while (obj)
{
if (id == obj->id)
break;
obj = obj->next;
}
if (obj && obj->pid)
{
kill (obj->pid, SIGQUIT);
pid_t pid2 = waitpid (obj->pid, 0, 0);
ASSERT ((obj->pid == pid2));
removeFromDnsQueue (obj);
}
}
//////////////////////////////////////////////////////////////////////////
// Name: blockingGethostbyname
//
// Description: Calls the blocking gethostbyname to perform the lookup.
// When done, pack the hostent struct into a character
// buffer and sent it back to the parent process via pipe.
//
static void
blockingGethostbyname (const char *name, int out_fd)
{
int i;
static int firstTime = true;
if (firstTime)
{
firstTime = 0;
dnsSocksKludge();
}
#ifdef GETHOSTBYNAME_DELAY
i = GETHOSTBYNAME_DELAY;
sleep(i);
#endif // GETHOSTBYNAME_DELAY
int size = 0;
char buf[BUFSIZ];
struct hostent *h = gethostbyname(name);
if (h)
{
*(int *)&buf[0] = (int) DNS_STATUS_GETHOSTBYNAME_OK;
char *p = buf + sizeof (int);
#if defined(DNS_DEBUG)
printf("gethostbyname complete\n");
for (i=0; h->h_addr_list[i]; i++);
printf("%d addresses for %s\n",i,h->h_name);
printf("address: ");
for (i = 0; i <= h->h_length; i++){
printf("%2.2x", (unsigned char)h->h_addr_list[0][i]);
}
printf("\n");
#endif
hostentToBytes (h, p, &size);
size = size + sizeof (int);
}
else
{
*(int *)&buf[0] = (int) DNS_STATUS_GETHOSTBYNAME_FAILED;
size = sizeof (int);
}
// Send response back to parent.
write(out_fd, buf, size);
}
//////////////////////////////////////////////////////////////////////////
// Name: spawnHelperProcess
//
// Description: Spawns a child helper process to do the standard Unix
// blocking dns lookup.
//
dns_lookup*
spawnHelperProcess (const char *name)
{
pid_t forked;
int fds[2];
dns_lookup* obj = newLookupObject (name);
if (!obj) return 0;
if (pipe(fds))
{
fprintf (stderr, "Can't make pipe\n");
return 0;
}
obj->fd = fds[0];
switch (forked = fork())
{
case -1:
fprintf (stderr, "Can't fork\n");
removeFromDnsQueue (obj);
break;
case 0: /* This is the forked process. */
close (fds[0]);
blockingGethostbyname (name, fds[1]);
/* Close the file and exit the process. */
close (fds[1]);
exit (0);
break;
default:
close (fds[1]);
obj->pid = forked;
return obj;
break;
}
// shouldn't get here
ASSERT (0);
return 0;
}
////////////////////////////////////////////////////////////////////////////
int main (int argc, char **argv)
{
/*
PRFileDesc* sock = PR_GetInheritedFD(DNS_SOCK_NAME);
if (sock == nsnull)
return -1;
*/
signal (SIGINT, mySignalHandler); // trap SIGINT
// TODO: more signals need to be trapped. Will do...
// Create a socket of type PF_UNIX to listen for dns lookup requests
listen_fd = socket (PF_UNIX, SOCK_STREAM, 0);
if (listen_fd == -1)
displaySysErr (argv[0]);
struct saddr_un {
sa_family_t sun_family;
char sun_path[108];
} unix_addr;
unix_addr.sun_family = AF_UNIX;
strcpy (unix_addr.sun_path, DNS_SOCK_NAME);
if (bind (listen_fd, (struct sockaddr*)&unix_addr, sizeof(unix_addr)) == -1)
displaySysErr (argv[0]);
if (listen (listen_fd, BACKLOG) == -1)
displaySysErr (argv[0]);
int accept_fd = -1;
/////////////////////////////////////////////////////////////////////////
// Loop to process incoming DNS lookup/cancel reqeusts. When lookup is
// is completed by the helper child process, a "hostent" structure will
// be sent back to the client.
/////////////////////////////////////////////////////////////////////////
while (1) {
fd_set readfds;
FD_ZERO (&readfds);
FD_SET (listen_fd, &readfds);
if (accept_fd > 0)
FD_SET (accept_fd, &readfds);
int idx = 0;
dns_lookup* obj;
while ((obj = getNextDnsEntry (&idx)))
FD_SET(obj->fd, &readfds);
// Select will return if any one of the fd is ready for read.
// Meaning either a request has arrived from Mozilla client or
// a helper process has sent the lookup result back to us.
int n = select (getdtablehi(), &readfds, 0, 0, 0);
if (n == -1)
continue;
if (FD_ISSET (listen_fd, &readfds))
{
struct sockaddr_in from;
int fromlen = sizeof (from);
accept_fd = accept (listen_fd, (struct sockaddr *)&from,
(socklen_t *)&fromlen);
if (accept_fd == -1)
displaySysErr (argv[0]);
}
int r;
char buffer[BUFSIZ];
if (accept_fd > 0 && FD_ISSET (accept_fd, &readfds))
{
// Read the request from client.
r = recv (accept_fd, buffer, sizeof(buffer), 0);
if (r > 0 && r < (int) sizeof(buffer)) {
buffer[r] = 0;
if (!(strncmp (buffer, "shutdown:", 9)))
break;
char *name = strchr ((const char *)buffer, ' ');
if (name && *name)
name++;
if (!name)
continue;
if (!strncmp (buffer, "kill:", 5))
{
// On kill, the name is really an id number
int id = (int) (*(int *)name);
cancelLookup (id);
close (accept_fd);
accept_fd = -1;
}
else if (!strncmp (buffer, "lookup:", 7))
{
#if defined(DNS_DEBUG)
printf("received lookup request for: %s\n",name);
#endif
obj = spawnHelperProcess (name);
obj->accept_fd = accept_fd;
char hId[5];
*(int *)&hId[0] = (int) obj->id;
if (!obj)
fprintf (stderr, "spawn Error\n");
else
{
send (obj->accept_fd, hId, sizeof (int), 0);
}
}
}
accept_fd = -1;
}
idx = 0;
while ((obj = getNextDnsEntry (&idx)))
{
// Check to see if any of the helper process is done with
// the lookup operation.
if (FD_ISSET(obj->fd, &readfds))
{
r = read (obj->fd, buffer, BUFSIZ);
int status;
// Send the reponse "hostent" back to client.
send (obj->accept_fd, buffer, r, 0);
close (obj->accept_fd);
removeFromDnsQueue (obj);
wait(&status);
close (obj->fd);
}
}
}
close (listen_fd);
unlink (DNS_SOCK_NAME);
exit (0);
}
#endif // XP_UNIX && UNIX_ASYNC_DNS

View File

@@ -0,0 +1,44 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Netscape Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/NPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All
* Rights Reserved.
*
* Contributor(s):
*/
#ifndef __UNIX_DNS_H__
#define __UNIX_DNS_H__
#define DNS_SOCK_NAME "/tmp/netscape_dns_lookup" // Name of unix socket
/* The internal status codes that are used; these follow the basic
SMTP/NNTP model of three-digit codes.
*/
#define DNS_STATUS_GETHOSTBYNAME_OK 101
#define DNS_STATUS_LOOKUP_OK 102
#define DNS_STATUS_KILLED_OK 103
#define DNS_STATUS_LOOKUP_STARTED 201
#define DNS_STATUS_GETHOSTBYNAME_FAILED 501
#define DNS_STATUS_LOOKUP_FAILED 502
#define DNS_STATUS_LOOKUP_NOT_STARTED 503
#define DNS_STATUS_KILL_FAILED 504
#define DNS_STATUS_UNIMPLEMENTED 601
#define DNS_STATUS_INTERNAL_ERROR 602
#endif /* __UNIX_DNS_H__ */

View File

@@ -0,0 +1,31 @@
#!gmake
#
# The contents of this file are subject to the Netscape Public
# License Version 1.1 (the "License"); you may not use this file
# except in compliance with the License. You may obtain a copy of
# the License at http://www.mozilla.org/NPL/
#
# Software distributed under the License is distributed on an "AS
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
# implied. See the License for the specific language governing
# rights and limitations under the License.
#
# The Original Code is mozilla.org code.
#
# The Initial Developer of the Original Code is Netscape
# Communications Corporation. Portions created by Netscape are
# Copyright (C) 1998 Netscape Communications Corporation. All
# Rights Reserved.
#
# Contributor(s):
DEPTH = ..\..
MODULE = necko
DIRS= \
public \
src \
$(NULL)
include <$(DEPTH)\config\rules.mak>

View File

@@ -0,0 +1,38 @@
#
# The contents of this file are subject to the Netscape Public
# License Version 1.1 (the "License"); you may not use this file
# except in compliance with the License. You may obtain a copy of
# the License at http://www.mozilla.org/NPL/
#
# Software distributed under the License is distributed on an "AS
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
# implied. See the License for the specific language governing
# rights and limitations under the License.
#
# The Original Code is mozilla.org code.
#
# The Initial Developer of the Original Code is Netscape
# Communications Corporation. Portions created by Netscape are
# Copyright (C) 1998 Netscape Communications Corporation. All
# Rights Reserved.
#
# Contributor(s):
#
DEPTH = ../../..
topsrcdir = @top_srcdir@
srcdir = @srcdir@
VPATH = @srcdir@
include $(DEPTH)/config/autoconf.mk
MODULE = necko
XPIDL_MODULE = necko_dns
XPIDLSRCS = \
nsIDNSListener.idl \
nsIDNSService.idl \
$(NULL)
include $(topsrcdir)/config/rules.mk

View File

@@ -0,0 +1,34 @@
#!gmake
#
# The contents of this file are subject to the Netscape Public
# License Version 1.1 (the "License"); you may not use this file
# except in compliance with the License. You may obtain a copy of
# the License at http://www.mozilla.org/NPL/
#
# Software distributed under the License is distributed on an "AS
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
# implied. See the License for the specific language governing
# rights and limitations under the License.
#
# The Original Code is mozilla.org code.
#
# The Initial Developer of the Original Code is Netscape
# Communications Corporation. Portions created by Netscape are
# Copyright (C) 1998 Netscape Communications Corporation. All
# Rights Reserved.
#
# Contributor(s):
MODULE = necko
DEPTH = ..\..\..
include <$(DEPTH)/config/config.mak>
XPIDL_MODULE = necko_dns
XPIDLSRCS = \
.\nsIDNSListener.idl \
.\nsIDNSService.idl \
$(NULL)
include <$(DEPTH)/config/rules.mak>

View File

@@ -0,0 +1,58 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* The contents of this file are subject to the Netscape Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/NPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1999 Netscape Communications Corporation. All
* Rights Reserved.
*
* Contributor(s):
*/
#include "nsISupports.idl"
%{C++
#include "prnetdb.h"
typedef struct nsHostEnt
{
PRHostEnt hostEnt;
char buffer[PR_NETDB_BUF_SIZE];
PRIntn bufLen;
char * bufPtr;
} nsHostEnt;
%}
[ptr] native nsHostEntStar(nsHostEnt);
[scriptable, uuid(7686cef0-206e-11d3-9348-00104ba0fd40)]
interface nsIDNSListener : nsISupports
{
/**
* Notify the listener that we are about to lookup the requested hostname.
*/
void OnStartLookup(in nsISupports ctxt, in string hostname);
/**
* Notify the listener that we have found one or more addresses for the hostname.
*/
[noscript] void OnFound(in nsISupports ctxt, in string hostname,
in nsHostEntStar entry);
/**
* Notify the listener that the lookup has completed.
*/
void OnStopLookup(in nsISupports ctxt, in string hostname, in nsresult status);
};

View File

@@ -0,0 +1,68 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* The contents of this file are subject to the Netscape Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/NPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1999 Netscape Communications Corporation. All
* Rights Reserved.
*
* Contributor(s):
*/
#include "nsISupports.idl"
%{C++
#define NS_DNSSERVICE_CID \
{ /* 718e7c81-f8b8-11d2-b951-c80918051d3c */ \
0x718e7c81, \
0xf8b8, \
0x11d2, \
{ 0xb9, 0x51, 0xc8, 0x09, 0x18, 0x05, 0x1d, 0x3c } \
}
%}
interface nsIRequest;
interface nsIDNSListener;
[scriptable, uuid(598f2f80-206f-11d3-9348-00104ba0fd40)]
interface nsIDNSService : nsISupports
{
nsIRequest lookup(in string hostname,
in nsIDNSListener listener,
in nsISupports ctxt);
/**
* Syncronously resolve the hostname to its IP address.
*/
string resolve(in string hostname);
/**
* Check if the specified address is in the network of the pattern
* using the specified mask. This function would probably go away
* once an implementation for it in JS exists for nsProxyAutoConfig.
* See http://www.mozilla.org/docs/netlib/pac.html for more info.
*/
boolean isInNet(in string ipaddr, in string pattern, in string mask);
void init();
void shutdown();
};
%{C++
#define NS_ERROR_UNKNOWN_HOST NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_NETWORK, 30)
%}

View File

@@ -0,0 +1,40 @@
#
# The contents of this file are subject to the Netscape Public
# License Version 1.1 (the "License"); you may not use this file
# except in compliance with the License. You may obtain a copy of
# the License at http://www.mozilla.org/NPL/
#
# Software distributed under the License is distributed on an "AS
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
# implied. See the License for the specific language governing
# rights and limitations under the License.
#
# The Original Code is mozilla.org code.
#
# The Initial Developer of the Original Code is Netscape
# Communications Corporation. Portions created by Netscape are
# Copyright (C) 1998 Netscape Communications Corporation. All
# Rights Reserved.
#
# Contributor(s):
#
DEPTH = ../../..
topsrcdir = @top_srcdir@
srcdir = @srcdir@
VPATH = @srcdir@
include $(DEPTH)/config/autoconf.mk
MODULE = necko
LIBRARY_NAME = neckodns_s
REQUIRES = xpcom
CPPSRCS = nsDnsService.cpp
# we don't want the shared lib, but we want to force the creation of a
# static lib.
override NO_SHARED_LIB=1
override NO_STATIC_LIB=
include $(topsrcdir)/config/rules.mk

View File

@@ -0,0 +1,42 @@
# The contents of this file are subject to the Netscape Public
# License Version 1.1 (the "License"); you may not use this file
# except in compliance with the License. You may obtain a copy of
# the License at http://www.mozilla.org/NPL/
#
# Software distributed under the License is distributed on an "AS
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
# implied. See the License for the specific language governing
# rights and limitations under the License.
#
# The Original Code is mozilla.org code.
#
# The Initial Developer of the Original Code is Netscape
# Communications Corporation. Portions created by Netscape are
# Copyright (C) 1998 Netscape Communications Corporation. All
# Rights Reserved.
#
# Contributor(s):
MODULE = necko
DEPTH = ..\..\..
LCFLAGS = -DWIN32_LEAN_AND_MEAN -D_IMPL_NS_NET
LIBRARY_NAME=neckodns_s
CPP_OBJS = \
.\$(OBJDIR)\nsDnsService.obj \
$(NULL)
INCS = $(INCS) \
-I$(DEPTH)\dist\include \
$(NULL)
include <$(DEPTH)\config\rules.mak>
install:: $(LIBRARY)
$(MAKE_INSTALL) $(LIBRARY) $(DIST)\lib
clobber::
rm -f $(DIST)\lib\$(LIBRARY_NAME).lib

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,111 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* The contents of this file are subject to the Netscape Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/NPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1999 Netscape Communications Corporation. All
* Rights Reserved.
*
* Contributor(s):
*/
#ifndef nsDNSService_h__
#define nsDNSService_h__
#include "nsIDNSService.h"
#include "nsIRunnable.h"
#include "nsIThread.h"
#include "nsISupportsArray.h"
#if defined(XP_MAC)
#include <OSUtils.h>
#include <OpenTransport.h>
#include <OpenTptInternet.h>
#elif defined (XP_WIN)
#include <windows.h>
#include <Winsock2.h>
#endif
#include "nsCOMPtr.h"
#include "nsHashtable.h"
#include "prmon.h"
#ifdef DEBUG
#define DNS_TIMING 1
#endif
class nsIDNSListener;
class nsDNSLookup;
class nsDNSService : public nsIDNSService,
public nsIRunnable
{
public:
NS_DECL_ISUPPORTS
NS_DECL_NSIRUNNABLE
NS_DECL_NSIDNSSERVICE
// nsDNSService methods:
nsDNSService();
virtual ~nsDNSService();
// Define a Create method to be used with a factory:
static NS_METHOD
Create(nsISupports* aOuter, const nsIID& aIID, void* *aResult);
friend class nsDNSLookup;
protected:
nsresult LateInit();
nsresult InitDNSThread();
nsresult GetLookupEntry(const char* hostName, nsDNSLookup* *result);
static nsDNSService * gService;
static PRBool gNeedLateInitialization;
nsCOMPtr<nsIThread> mThread;
nsresult mState;
PRMonitor* mMonitor;
nsSupportsHashtable mLookups; // of nsDNSLookups
#if defined(XP_MAC)
friend pascal void nsDnsServiceNotifierRoutine(void * contextPtr, OTEventCode code, OTResult result, void * cookie);
PRBool mThreadRunning;
InetSvcRef mServiceRef;
QHdr mCompletionQueue;
#if TARGET_CARBON
OTClientContextPtr mClientContext;
#endif /* TARGET_CARBON */
OTNotifyUPP nsDnsServiceNotifierRoutineUPP;
#endif /* XP_MAC */
#if defined(XP_WIN)
PRUint32 AllocMsgID(void);
void FreeMsgID(PRUint32 msgID);
friend static LRESULT CALLBACK nsDNSEventProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
LRESULT ProcessLookup(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
HWND mDNSWindow;
PRUint32 mMsgIDBitVector[4];
#endif /* XP_WIN */
#ifdef DNS_TIMING
double mCount;
double mTimes;
double mSquaredTimes;
FILE* mOut;
friend class nsDNSRequest;
#endif
char* mMyIPAddress;
};
#endif /* nsDNSService_h__ */

File diff suppressed because it is too large Load Diff

View File

@@ -1,134 +0,0 @@
#
# The contents of this file are subject to the Netscape Public
# License Version 1.1 (the "License"); you may not use this file
# except in compliance with the License. You may obtain a copy of
# the License at http://www.mozilla.org/NPL/
#
# Software distributed under the License is distributed on an "AS
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
# implied. See the License for the specific language governing
# rights and limitations under the License.
#
# The Original Code is mozilla.org code.
#
# The Initial Developer of the Original Code is Netscape
# Communications Corporation. Portions created by Netscape are
# Copyright (C) 1998 Netscape Communications Corporation. All
# Rights Reserved.
#
# Contributor(s):
#
DEPTH = ../..
topsrcdir = @top_srcdir@
srcdir = @srcdir@
VPATH = @srcdir@
include $(DEPTH)/config/autoconf.mk
MODULE = xpcom
XPIDL_MODULE = xpcom_ds
LIBRARY_NAME = xpcomds_s
REQUIRES = unicharutil string
CSRCS = \
pldhash.c \
plvector.c \
$(NULL)
CPPSRCS = \
nsArena.cpp \
nsAtomTable.cpp \
nsAtomService.cpp \
nsAVLTree.cpp \
nsByteBuffer.cpp \
nsCRT.cpp \
nsConjoiningEnumerator.cpp \
nsDeque.cpp \
nsEmptyEnumerator.cpp \
nsEnumeratorUtils.cpp \
nsFixedSizeAllocator.cpp \
nsHashtable.cpp \
nsHashtableEnumerator.cpp \
nsObserver.cpp \
nsObserverList.cpp \
nsObserverService.cpp \
nsProperties.cpp \
nsPersistentProperties.cpp \
nsQuickSort.cpp \
nsSizeOfHandler.cpp \
nsStaticNameTable.cpp \
nsStatistics.cpp \
nsStringMap.cpp \
nsSupportsArray.cpp \
nsSupportsArrayEnumerator.cpp \
nsSupportsPrimitives.cpp \
nsUnicharBuffer.cpp \
nsVoidArray.cpp \
nsVoidBTree.cpp \
nsTextFormatter.cpp \
nsTimelineService.cpp \
$(NULL)
EXPORTS = \
nsAVLTree.h \
nsAtomService.h \
nsCppSharedAllocator.h \
nsCRT.h \
nsDeque.h \
nsEnumeratorUtils.h \
nsFixedSizeAllocator.h \
nsHashtable.h \
nsHashtableEnumerator.h \
nsIArena.h \
nsIByteBuffer.h \
nsIObserverList.h \
nsISimpleEnumerator.h \
nsISizeOfHandler.h \
nsIUnicharBuffer.h \
nsInt64.h \
nsQuickSort.h \
nsStaticNameTable.h \
nsStatistics.h \
nsStringMap.h \
nsSupportsArray.h \
nsSupportsPrimitives.h \
nsTime.h \
nsUnitConversion.h \
nsVector.h \
nsVoidArray.h \
nsVoidBTree.h \
pldhash.h \
plvector.h \
nsTextFormatter.h \
$(NULL)
XPIDLSRCS = \
nsIAtom.idl \
nsIAtomService.idl \
nsICollection.idl \
nsIEnumerator.idl \
nsIObserver.idl \
nsIObserverService.idl \
nsIPersistentProperties2.idl \
nsIProperties.idl \
nsISerializable.idl \
nsIStopwatch.idl \
nsISupportsArray.idl \
nsISupportsIterators.idl \
nsISupportsPrimitives.idl \
nsITimelineService.idl \
$(NULL)
EXPORTS := $(addprefix $(srcdir)/, $(EXPORTS))
# we don't want the shared lib, but we want to force the creation of a static lib.
FORCE_STATIC_LIB = 1
# Force use of PIC
FORCE_USE_PIC = 1
include $(topsrcdir)/config/rules.mk
DEFINES += -D_IMPL_NS_COM -D_IMPL_NS_BASE

View File

@@ -1,300 +0,0 @@
/*
* The contents of this file are subject to the Mozilla Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is Mozilla Communicator client code.
*
* The Initial Developer of the Original Code is James L. Nance
* Portions created by James L. Nance are Copyright (C) 2001
* James L. Nance. All Rights Reserved.
*
* Contributor(s): Patricia Jewell Nance, Jesse Jacob Nance
*
* Alternatively, the contents of this file may be used under the
* terms of the GNU General Public License Version 2 or later (the
* "GPL"), in which case the provisions of the GPL are applicable
* instead of those above. If you wish to allow use of your
* version of this file only under the terms of the GPL and not to
* allow others to use your version of this file under the MPL,
* indicate your decision by deleting the provisions above and
* replace them with the notice and other provisions required by
* the GPL. If you do not delete the provisions above, a recipient
* may use your version of this file under either the MPL or the
* GPL.
*/
// #define TEST_PATRICIA
#if defined(TEST_PATRICIA)
# include "stdlib.h"
# include "stdio.h"
# include "string.h"
# define PRBool bool
# define PRUint32 unsigned int
# define PRInt32 int
# define PR_CALLBACK
# define PLArenaPool int
# define PL_FinishArenaPool(a)
# define PL_InitArenaPool(a, b, c, d)
# define PL_ArenaAllocate(a, size) malloc(size)
# define PR_TRUE true
# define PR_FALSE false
struct nsCRT {
static int strlen(const char *a) {return ::strlen(a);}
static int memcmp(const void *a, const void *b, PRUint32 c) {
return ::memcmp(a,b,c);
}
};
#else
# include "nsCRT.h"
#endif
#include "nsStringMap.h"
const char nsStringMap::zero_str[] = "\0";
nsStringMap::~nsStringMap()
{
// Get rid of the arena memory
PL_FinishArenaPool(&mPool);
}
nsStringMap::nsStringMap() : numEntries(0)
{
// Initialize the head
head.l = head.r = &head;
head.bit = ~0;
head.key = zero_str;
head.len = 1;
head.obj = 0;
// Initialize the arena. Guess that a 512 byte block size is good
PL_InitArenaPool(&mPool, "nsStringMap", 512, sizeof(void*));
}
void
nsStringMap::Reset()
{
// Initialize the head
head.l = head.r = &head;
head.bit = ~0;
head.key = zero_str;
head.len = 1;
head.obj = 0;
// Reinitialize the Arena
PL_FinishArenaPool(&mPool);
PL_InitArenaPool(&mPool, "nsStringMap", 512, sizeof(void*));
}
void
nsStringMap::Reset(nsStringMapEnumFunc destroyFunc, void *aClosure)
{
Enumerate(destroyFunc, aClosure);
Reset();
}
nsStringMap::Patricia *
nsStringMap::newNode()
{
return (Patricia*) PL_ArenaAllocate(&mPool, sizeof(Patricia));
}
nsStringMap::Patricia *
nsStringMap::searchDown(BitTester &key)
{
// The head node only branches to the left, so we can optimize here.
Patricia *x = head.l;
PRUint32 lastBits;
do {
lastBits = x->bit;
if(key.isset(lastBits))
x = x->r;
else
x = x->l;
} while(lastBits > x->bit);
return x;
}
void*
nsStringMap::Get(const char *str, PRUint32 slen)
{
BitTester key(str, slen);
Patricia *t = searchDown(key);
if(!key.memcmp(t->key, t->len)) {
return t->obj;
}
return 0;
}
void*
nsStringMap::Get(const char *str)
{
BitTester key(str);
Patricia *t = searchDown(key);
if(!key.memcmp(t->key, t->len)) {
return t->obj;
}
return 0;
}
PRBool
nsStringMap::Put(const char *str, void *obj, PRBool copy)
{
PRUint32 slen = nsCRT::strlen(str);
return Put(str, slen, obj, copy);
}
PRBool
nsStringMap::Put(const char *str, PRUint32 slen, void *obj, PRBool copy)
{
if(copy) {
PRUint32 mask = sizeof(double) - 1;
PRUint32 asize = (slen+mask) & ~mask;
char *tstr = (char*) PL_ArenaAllocate(&mPool, asize);
memcpy(tstr, str, slen);
str = tstr;
}
BitTester key(str, slen);
Patricia *t = searchDown(key);
if(!key.memcmp(t->key, t->len)) {
t->obj = obj;
return PR_TRUE;
}
// This is somewhat ugly. We need to find the maximum bit position that
// differs, but this is complicated by the fact that we have random length
// data. Assume that data past the end of the string is 0.
const PRUint32 klen = key.datalen();
const PRUint32 tlen = t->len;
PRUint32 bpos;
if(klen>tlen) {
bpos = 8 * klen - 1;
while(!BitTester::isset_checked(str, bpos)) --bpos;
} else if(tlen>klen) {
bpos = 8 * tlen - 1;
while(!BitTester::isset_checked(t->key, bpos)) --bpos;
} else /* equal */ {
bpos = 8 * tlen - 1;
while(BitTester::bitsequal(t->key, str, bpos)) --bpos;
}
Patricia *p, *x = &head;
do {
p = x;
x = key.isset(x->bit) ? x->r : x->l;
} while(x->bit > bpos && p->bit > x->bit);
t = newNode();
if(!t) {
return PR_FALSE;
}
t->key = str;
t->len = key.datalen();
t->obj = obj;
t->bit = bpos;
if(key.isset(t->bit)) {
t->r = t;
t->l = x;
} else {
t->r = x;
t->l = t;
}
if(key.isset(p->bit)) {
p->r = t;
} else {
p->l = t;
}
return PR_TRUE;
}
void
nsStringMap::enumerate_recurse(
nsStringMapEnumFunc aEnumFunc, void* aClosure, Patricia *node)
{
aEnumFunc(node->key, node->obj, aClosure);
if(node->l && node->l->bit<node->bit)
enumerate_recurse(aEnumFunc, aClosure, node->l);
if(node->r && node->r->bit<node->bit)
enumerate_recurse(aEnumFunc, aClosure, node->r);
}
void
nsStringMap::Enumerate(nsStringMapEnumFunc aEnumFunc, void *aClosure)
{
// We dont want to process head, its a sentinal
if(head.l && head.l->bit<head.bit)
enumerate_recurse(aEnumFunc, aClosure, head.l);
if(head.r && head.r->bit<head.bit)
enumerate_recurse(aEnumFunc, aClosure, head.r);
}
#if defined(TEST_PATRICIA)
PRBool etest(const char *key, void *data, void *closure)
{
printf("%s\n", key);
return PR_TRUE;
}
int main()
{
nsStringMap map;
const char *strings[] = {
"I am number 1 string",
"I am number 2 string",
"I am number 3 string",
"a different string",
"a similar string",
"I am a very long string and I want to make sure we can handle this",
"I am a very long string and I want to make sure we can handle this too",
0
};
int idx;
for(idx=0; strings[idx]; ++idx) {
map.Put(strings[idx], (void*)(1+idx));
}
printf("Lookup Test\n");
while(--idx>=0) {
void *ptr = map.Get(strings[idx]);
printf("%d: %s\n", (long)ptr, strings[idx]);
}
printf("\nEnumeration Test\n");
map.Enumerate(etest, 0);
return 0;
}
#endif

View File

@@ -1,112 +0,0 @@
#ifndef nsStringMap_h__
#define nsStringMap_h__
/*
* The contents of this file are subject to the Mozilla Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is Mozilla Communicator client code.
*
* The Initial Developer of the Original Code is James L. Nance
* Portions created by James L. Nance are Copyright (C) 2001
* James L. Nance. All Rights Reserved.
*
* Contributor(s): Patricia Jewell Nance, Jesse Jacob Nance
*
* Alternatively, the contents of this file may be used under the
* terms of the GNU General Public License Version 2 or later (the
* "GPL"), in which case the provisions of the GPL are applicable
* instead of those above. If you wish to allow use of your
* version of this file only under the terms of the GPL and not to
* allow others to use your version of this file under the MPL,
* indicate your decision by deleting the provisions above and
* replace them with the notice and other provisions required by
* the GPL. If you do not delete the provisions above, a recipient
* may use your version of this file under either the MPL or the
* GPL.
*/
#if !defined(TEST_PATRICIA)
# include "nscore.h"
# include "prtypes.h"
# include "plarena.h"
#endif
typedef PRBool (*PR_CALLBACK nsStringMapEnumFunc) (
const char *aKey, void *aData, void *aClosure);
class nsStringMap
{
public:
nsStringMap();
~nsStringMap();
PRBool Put(const char *str, PRUint32 slen, void *obj, PRBool copy=PR_FALSE);
PRBool Put(const char *str, void *obj, PRBool copy=PR_FALSE);
void* Get(const char *str);
void* Get(const char *str, PRUint32 slen);
void Reset();
void Reset(nsStringMapEnumFunc destroyFunc, void *aClosure = 0);
void Enumerate(nsStringMapEnumFunc aEnumFunc, void *aClosure = 0);
struct Patricia {
Patricia *l, *r;
PRUint32 bit; // Bit position for l/r comp
const char *key;
PRUint32 len;
void *obj;
};
// The BitTester class is used to test a particular bit position in an
// array of characters. It does not assign any special meaning to 0
// characters. Bits past the end of the array are treated as 0
class BitTester {
const PRUint32 slen;
const char *cstr;
public:
BitTester(const char *s) : slen(nsCRT::strlen(s)), cstr(s) {}
BitTester(const char *s, PRUint32 l) : slen(l), cstr(s) {}
PRInt32 memcmp(const char *ostr, PRUint32 olen) {
if(olen==slen) {
return ::memcmp((void*)cstr, ostr, slen);
} else {
return olen - slen;
}
}
PRUint32 datalen() const {return slen;}
static PRBool isset_checked(const char *str, PRUint32 idx) {
return (str[idx/8] & (1<<(idx & 7))) != 0;
}
static PRBool
bitsequal(const char *str1, const char*str2, PRUint32 idx) {
return (str1[idx/8] & (1<<(idx&7)))==(str2[idx/8] & (1<<(idx&7)));
}
PRBool isset(PRUint32 idx) {
const PRUint32 base = idx/8;
if(base>=slen) return 0;
return (cstr[base] & (1<<(idx & 7))) != 0;
}
};
private:
PLArenaPool mPool;
Patricia *newNode();
Patricia *searchDown(BitTester&);
void enumerate_recurse(nsStringMapEnumFunc, void*, Patricia*);
Patricia head; // Sentinal node
PRInt32 numEntries;
static const char zero_str[];
};
#endif