diff options
author | wolfbeast <mcwerewolf@wolfbeast.com> | 2020-01-02 21:01:38 +0100 |
---|---|---|
committer | wolfbeast <mcwerewolf@wolfbeast.com> | 2020-01-02 21:01:38 +0100 |
commit | f7d30133221896638f7bf4f66c504255c4b14f48 (patch) | |
tree | 5f3e07a049f388a3a309a615b8884318f6668a98 /nsprpub/pr/src/io | |
parent | 26b297510a11758727438df4669357a2a2bc42ce (diff) | |
download | uxp-f7d30133221896638f7bf4f66c504255c4b14f48.tar.gz |
Issue #1338 - Part 1: Update NSPR to 4.24
Diffstat (limited to 'nsprpub/pr/src/io')
-rw-r--r-- | nsprpub/pr/src/io/prdir.c | 92 | ||||
-rw-r--r-- | nsprpub/pr/src/io/prfdcach.c | 52 | ||||
-rw-r--r-- | nsprpub/pr/src/io/prfile.c | 349 | ||||
-rw-r--r-- | nsprpub/pr/src/io/prio.c | 130 | ||||
-rw-r--r-- | nsprpub/pr/src/io/priometh.c | 139 | ||||
-rw-r--r-- | nsprpub/pr/src/io/pripv6.c | 338 | ||||
-rw-r--r-- | nsprpub/pr/src/io/prlayer.c | 279 | ||||
-rw-r--r-- | nsprpub/pr/src/io/prlog.c | 52 | ||||
-rw-r--r-- | nsprpub/pr/src/io/prmapopt.c | 66 | ||||
-rw-r--r-- | nsprpub/pr/src/io/prmmap.c | 14 | ||||
-rw-r--r-- | nsprpub/pr/src/io/prmwait.c | 267 | ||||
-rw-r--r-- | nsprpub/pr/src/io/prpolevt.c | 50 | ||||
-rw-r--r-- | nsprpub/pr/src/io/prprf.c | 1486 | ||||
-rw-r--r-- | nsprpub/pr/src/io/prscanf.c | 82 | ||||
-rw-r--r-- | nsprpub/pr/src/io/prsocket.c | 2011 |
15 files changed, 2841 insertions, 2566 deletions
diff --git a/nsprpub/pr/src/io/prdir.c b/nsprpub/pr/src/io/prdir.c index 3701a6a4bd..365afefd2f 100644 --- a/nsprpub/pr/src/io/prdir.c +++ b/nsprpub/pr/src/io/prdir.c @@ -18,8 +18,8 @@ PR_IMPLEMENT(PRDir*) PR_OpenDir(const char *name) return NULL; } } else { - PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); - } + PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); + } return dir; } @@ -33,51 +33,57 @@ PR_IMPLEMENT(PRDirEntry*) PR_ReadDir(PRDir *dir, PRDirFlags flags) PR_IMPLEMENT(PRStatus) PR_CloseDir(PRDir *dir) { -PRInt32 rv; + PRInt32 rv; if (dir) { rv = _PR_MD_CLOSE_DIR(&dir->md); - PR_DELETE(dir); - if (rv < 0) { - return PR_FAILURE; - } else - return PR_SUCCESS; + PR_DELETE(dir); + if (rv < 0) { + return PR_FAILURE; + } else { + return PR_SUCCESS; + } } - return PR_SUCCESS; + return PR_SUCCESS; } PR_IMPLEMENT(PRStatus) PR_MkDir(const char *name, PRIntn mode) { -PRInt32 rv; + PRInt32 rv; - rv = _PR_MD_MKDIR(name, mode); - if (rv < 0) { - return PR_FAILURE; - } else - return PR_SUCCESS; + rv = _PR_MD_MKDIR(name, mode); + if (rv < 0) { + return PR_FAILURE; + } else { + return PR_SUCCESS; + } } PR_IMPLEMENT(PRStatus) PR_MakeDir(const char *name, PRIntn mode) { -PRInt32 rv; - - if (!_pr_initialized) _PR_ImplicitInitialization(); - rv = _PR_MD_MAKE_DIR(name, mode); - if (rv < 0) { - return PR_FAILURE; - } else - return PR_SUCCESS; + PRInt32 rv; + + if (!_pr_initialized) { + _PR_ImplicitInitialization(); + } + rv = _PR_MD_MAKE_DIR(name, mode); + if (rv < 0) { + return PR_FAILURE; + } else { + return PR_SUCCESS; + } } PR_IMPLEMENT(PRStatus) PR_RmDir(const char *name) { -PRInt32 rv; + PRInt32 rv; - rv = _PR_MD_RMDIR(name); - if (rv < 0) { - return PR_FAILURE; - } else - return PR_SUCCESS; + rv = _PR_MD_RMDIR(name); + if (rv < 0) { + return PR_FAILURE; + } else { + return PR_SUCCESS; + } } #ifdef MOZ_UNICODE @@ -85,7 +91,7 @@ PRInt32 rv; * UTF16 Interface */ PR_IMPLEMENT(PRDirUTF16*) PR_OpenDirUTF16(const PRUnichar *name) -{ +{ PRDirUTF16 *dir; PRStatus sts; @@ -100,10 +106,10 @@ PR_IMPLEMENT(PRDirUTF16*) PR_OpenDirUTF16(const PRUnichar *name) PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); } return dir; -} - +} + PR_IMPLEMENT(PRDirEntryUTF16*) PR_ReadDirUTF16(PRDirUTF16 *dir, PRDirFlags flags) -{ +{ /* * _MD_READ_DIR_UTF16 return a PRUnichar* to the name; allocation in * machine-dependent code @@ -111,20 +117,22 @@ PR_IMPLEMENT(PRDirEntryUTF16*) PR_ReadDirUTF16(PRDirUTF16 *dir, PRDirFlags flags PRUnichar* name = _PR_MD_READ_DIR_UTF16(&dir->md, flags); dir->d.name = name; return name ? &dir->d : NULL; -} - +} + PR_IMPLEMENT(PRStatus) PR_CloseDirUTF16(PRDirUTF16 *dir) -{ - PRInt32 rv; +{ + PRInt32 rv; if (dir) { rv = _PR_MD_CLOSE_DIR_UTF16(&dir->md); PR_DELETE(dir); - if (rv < 0) - return PR_FAILURE; - else - return PR_SUCCESS; - } + if (rv < 0) { + return PR_FAILURE; + } + else { + return PR_SUCCESS; + } + } return PR_SUCCESS; } diff --git a/nsprpub/pr/src/io/prfdcach.c b/nsprpub/pr/src/io/prfdcach.c index 17b71fe6bf..ecfe3d39cc 100644 --- a/nsprpub/pr/src/io/prfdcach.c +++ b/nsprpub/pr/src/io/prfdcach.c @@ -63,8 +63,12 @@ PRFileDesc *_PR_Getfd(void) { do { - if (NULL == _pr_fd_cache.head) goto allocate; /* nothing there */ - if (_pr_fd_cache.count < _pr_fd_cache.limit_low) goto allocate; + if (NULL == _pr_fd_cache.head) { + goto allocate; /* nothing there */ + } + if (_pr_fd_cache.count < _pr_fd_cache.limit_low) { + goto allocate; + } /* we "should" be able to extract an fd from the cache */ PR_Lock(_pr_fd_cache.ml); /* need the lock to do this safely */ @@ -104,10 +108,16 @@ allocate: if (NULL != fd) { fd->secret = PR_NEW(PRFilePrivate); - if (NULL == fd->secret) PR_DELETE(fd); + if (NULL == fd->secret) { + PR_DELETE(fd); + } + } + if (NULL != fd) { + goto finished; + } + else { + return NULL; } - if (NULL != fd) goto finished; - else return NULL; } /* _PR_Getfd */ @@ -157,10 +167,14 @@ PR_IMPLEMENT(PRStatus) PR_SetFDCacheSize(PRIntn low, PRIntn high) ** turn the caches off, or turn them on. It is not dependent ** on the compilation setting of DEBUG. */ - if (!_pr_initialized) _PR_ImplicitInitialization(); + if (!_pr_initialized) { + _PR_ImplicitInitialization(); + } + + if (low > high) { + low = high; /* sanity check the params */ + } - if (low > high) low = high; /* sanity check the params */ - PR_Lock(_pr_fd_cache.ml); _pr_fd_cache.limit_high = high; _pr_fd_cache.limit_low = low; @@ -179,7 +193,7 @@ void _PR_InitFdCache(void) const char *low = PR_GetEnv("NSPR_FD_CACHE_SIZE_LOW"); const char *high = PR_GetEnv("NSPR_FD_CACHE_SIZE_HIGH"); - /* + /* ** _low is allowed to be zero, _high is not. ** If _high is zero, we're not doing the caching. */ @@ -191,19 +205,27 @@ void _PR_InitFdCache(void) _pr_fd_cache.limit_high = 0; #endif /* defined(DEBUG) */ - if (NULL != low) _pr_fd_cache.limit_low = atoi(low); - if (NULL != high) _pr_fd_cache.limit_high = atoi(high); + if (NULL != low) { + _pr_fd_cache.limit_low = atoi(low); + } + if (NULL != high) { + _pr_fd_cache.limit_high = atoi(high); + } - if (_pr_fd_cache.limit_low < 0) + if (_pr_fd_cache.limit_low < 0) { _pr_fd_cache.limit_low = 0; - if (_pr_fd_cache.limit_low > FD_SETSIZE) + } + if (_pr_fd_cache.limit_low > FD_SETSIZE) { _pr_fd_cache.limit_low = FD_SETSIZE; + } - if (_pr_fd_cache.limit_high > FD_SETSIZE) + if (_pr_fd_cache.limit_high > FD_SETSIZE) { _pr_fd_cache.limit_high = FD_SETSIZE; + } - if (_pr_fd_cache.limit_high < _pr_fd_cache.limit_low) + if (_pr_fd_cache.limit_high < _pr_fd_cache.limit_low) { _pr_fd_cache.limit_high = _pr_fd_cache.limit_low; + } _pr_fd_cache.ml = PR_NewLock(); PR_ASSERT(NULL != _pr_fd_cache.ml); diff --git a/nsprpub/pr/src/io/prfile.c b/nsprpub/pr/src/io/prfile.c index e32031bbb9..4b07baf41b 100644 --- a/nsprpub/pr/src/io/prfile.c +++ b/nsprpub/pr/src/io/prfile.c @@ -28,21 +28,22 @@ static PRInt32 PR_CALLBACK FileRead(PRFileDesc *fd, void *buf, PRInt32 amount) PRThread *me = _PR_MD_CURRENT_THREAD(); if (_PR_PENDING_INTERRUPT(me)) { - me->flags &= ~_PR_INTERRUPT; - PR_SetError(PR_PENDING_INTERRUPT_ERROR, 0); - rv = -1; + me->flags &= ~_PR_INTERRUPT; + PR_SetError(PR_PENDING_INTERRUPT_ERROR, 0); + rv = -1; } if (_PR_IO_PENDING(me)) { PR_SetError(PR_IO_PENDING_ERROR, 0); - rv = -1; + rv = -1; + } + if (rv == -1) { + return rv; } - if (rv == -1) - return rv; - rv = _PR_MD_READ(fd, buf, amount); - if (rv < 0) { - PR_ASSERT(rv == -1); - } + rv = _PR_MD_READ(fd, buf, amount); + if (rv < 0) { + PR_ASSERT(rv == -1); + } PR_LOG(_pr_io_lm, PR_LOG_MAX, ("read -> %d", rv)); return rv; } @@ -56,14 +57,15 @@ static PRInt32 PR_CALLBACK FileWrite(PRFileDesc *fd, const void *buf, PRInt32 am if (_PR_PENDING_INTERRUPT(me)) { me->flags &= ~_PR_INTERRUPT; PR_SetError(PR_PENDING_INTERRUPT_ERROR, 0); - rv = -1; + rv = -1; } if (_PR_IO_PENDING(me)) { PR_SetError(PR_IO_PENDING_ERROR, 0); - rv = -1; + rv = -1; + } + if (rv != 0) { + return rv; } - if (rv != 0) - return rv; count = 0; #if !defined(_PR_HAVE_O_APPEND) /* Bugzilla: 4090, 276330 */ @@ -74,17 +76,17 @@ static PRInt32 PR_CALLBACK FileWrite(PRFileDesc *fd, const void *buf, PRInt32 am } /* if (fd->secret->appendMode...) */ #endif /* _PR_HAVE_O_APPEND */ while (amount > 0) { - temp = _PR_MD_WRITE(fd, buf, amount); - if (temp < 0) { - count = -1; - break; - } - count += temp; - if (fd->secret->nonblocking) { - break; - } - buf = (const void*) ((const char*)buf + temp); - amount -= temp; + temp = _PR_MD_WRITE(fd, buf, amount); + if (temp < 0) { + count = -1; + break; + } + count += temp; + if (fd->secret->nonblocking) { + break; + } + buf = (const void*) ((const char*)buf + temp); + amount -= temp; } PR_LOG(_pr_io_lm, PR_LOG_MAX, ("write -> %d", count)); return count; @@ -112,8 +114,9 @@ static PRInt32 PR_CALLBACK FileAvailable(PRFileDesc *fd) cur = _PR_MD_LSEEK(fd, 0, PR_SEEK_CUR); - if (cur >= 0) - end = _PR_MD_LSEEK(fd, 0, PR_SEEK_END); + if (cur >= 0) { + end = _PR_MD_LSEEK(fd, 0, PR_SEEK_END); + } if ((cur < 0) || (end < 0)) { return -1; @@ -133,10 +136,13 @@ static PRInt64 PR_CALLBACK FileAvailable64(PRFileDesc *fd) LL_I2L(minus_one, -1); cur = _PR_MD_LSEEK64(fd, LL_ZERO, PR_SEEK_CUR); - if (LL_GE_ZERO(cur)) - end = _PR_MD_LSEEK64(fd, LL_ZERO, PR_SEEK_END); + if (LL_GE_ZERO(cur)) { + end = _PR_MD_LSEEK64(fd, LL_ZERO, PR_SEEK_END); + } - if (!LL_GE_ZERO(cur) || !LL_GE_ZERO(end)) return minus_one; + if (!LL_GE_ZERO(cur) || !LL_GE_ZERO(end)) { + return minus_one; + } LL_SUB(result, end, cur); (void)_PR_MD_LSEEK64(fd, cur, PR_SEEK_SET); @@ -146,42 +152,47 @@ static PRInt64 PR_CALLBACK FileAvailable64(PRFileDesc *fd) static PRInt32 PR_CALLBACK PipeAvailable(PRFileDesc *fd) { - PRInt32 rv; - rv = _PR_MD_PIPEAVAILABLE(fd); - return rv; + PRInt32 rv; + rv = _PR_MD_PIPEAVAILABLE(fd); + return rv; } static PRInt64 PR_CALLBACK PipeAvailable64(PRFileDesc *fd) { PRInt64 rv; LL_I2L(rv, _PR_MD_PIPEAVAILABLE(fd)); - return rv; + return rv; } static PRStatus PR_CALLBACK PipeSync(PRFileDesc *fd) { - return PR_SUCCESS; + return PR_SUCCESS; } static PRStatus PR_CALLBACK FileGetInfo(PRFileDesc *fd, PRFileInfo *info) { - PRInt32 rv; + PRInt32 rv; rv = _PR_MD_GETOPENFILEINFO(fd, info); if (rv < 0) { - return PR_FAILURE; - } else - return PR_SUCCESS; + return PR_FAILURE; + } else { + return PR_SUCCESS; + } } static PRStatus PR_CALLBACK FileGetInfo64(PRFileDesc *fd, PRFileInfo64 *info) { /* $$$$ NOT YET IMPLEMENTED */ - PRInt32 rv; + PRInt32 rv; rv = _PR_MD_GETOPENFILEINFO64(fd, info); - if (rv < 0) return PR_FAILURE; - else return PR_SUCCESS; + if (rv < 0) { + return PR_FAILURE; + } + else { + return PR_SUCCESS; + } } static PRStatus PR_CALLBACK FileSync(PRFileDesc *fd) @@ -189,7 +200,7 @@ static PRStatus PR_CALLBACK FileSync(PRFileDesc *fd) PRInt32 result; result = _PR_MD_FSYNC(fd); if (result < 0) { - return PR_FAILURE; + return PR_FAILURE; } return PR_SUCCESS; } @@ -197,7 +208,7 @@ static PRStatus PR_CALLBACK FileSync(PRFileDesc *fd) static PRStatus PR_CALLBACK FileClose(PRFileDesc *fd) { if (!fd || !fd->secret - || (fd->secret->state != _PR_FILEDESC_OPEN + || (fd->secret->state != _PR_FILEDESC_OPEN && fd->secret->state != _PR_FILEDESC_CLOSED)) { PR_SetError(PR_BAD_DESCRIPTOR_ERROR, 0); return PR_FAILURE; @@ -232,30 +243,30 @@ static PRIOMethods _pr_fileMethods = { FileSeek64, FileGetInfo, FileGetInfo64, - (PRWritevFN)_PR_InvalidInt, - (PRConnectFN)_PR_InvalidStatus, - (PRAcceptFN)_PR_InvalidDesc, - (PRBindFN)_PR_InvalidStatus, - (PRListenFN)_PR_InvalidStatus, - (PRShutdownFN)_PR_InvalidStatus, - (PRRecvFN)_PR_InvalidInt, - (PRSendFN)_PR_InvalidInt, - (PRRecvfromFN)_PR_InvalidInt, - (PRSendtoFN)_PR_InvalidInt, - FilePoll, - (PRAcceptreadFN)_PR_InvalidInt, - (PRTransmitfileFN)_PR_InvalidInt, - (PRGetsocknameFN)_PR_InvalidStatus, - (PRGetpeernameFN)_PR_InvalidStatus, - (PRReservedFN)_PR_InvalidInt, - (PRReservedFN)_PR_InvalidInt, - (PRGetsocketoptionFN)_PR_InvalidStatus, + (PRWritevFN)_PR_InvalidInt, + (PRConnectFN)_PR_InvalidStatus, + (PRAcceptFN)_PR_InvalidDesc, + (PRBindFN)_PR_InvalidStatus, + (PRListenFN)_PR_InvalidStatus, + (PRShutdownFN)_PR_InvalidStatus, + (PRRecvFN)_PR_InvalidInt, + (PRSendFN)_PR_InvalidInt, + (PRRecvfromFN)_PR_InvalidInt, + (PRSendtoFN)_PR_InvalidInt, + FilePoll, + (PRAcceptreadFN)_PR_InvalidInt, + (PRTransmitfileFN)_PR_InvalidInt, + (PRGetsocknameFN)_PR_InvalidStatus, + (PRGetpeernameFN)_PR_InvalidStatus, + (PRReservedFN)_PR_InvalidInt, + (PRReservedFN)_PR_InvalidInt, + (PRGetsocketoptionFN)_PR_InvalidStatus, (PRSetsocketoptionFN)_PR_InvalidStatus, - (PRSendfileFN)_PR_InvalidInt, - (PRConnectcontinueFN)_PR_InvalidStatus, - (PRReservedFN)_PR_InvalidInt, - (PRReservedFN)_PR_InvalidInt, - (PRReservedFN)_PR_InvalidInt, + (PRSendfileFN)_PR_InvalidInt, + (PRConnectcontinueFN)_PR_InvalidStatus, + (PRReservedFN)_PR_InvalidInt, + (PRReservedFN)_PR_InvalidInt, + (PRReservedFN)_PR_InvalidInt, (PRReservedFN)_PR_InvalidInt }; @@ -276,30 +287,30 @@ static PRIOMethods _pr_pipeMethods = { (PRSeek64FN)_PR_InvalidInt64, (PRFileInfoFN)_PR_InvalidStatus, (PRFileInfo64FN)_PR_InvalidStatus, - (PRWritevFN)_PR_InvalidInt, - (PRConnectFN)_PR_InvalidStatus, - (PRAcceptFN)_PR_InvalidDesc, - (PRBindFN)_PR_InvalidStatus, - (PRListenFN)_PR_InvalidStatus, - (PRShutdownFN)_PR_InvalidStatus, - (PRRecvFN)_PR_InvalidInt, - (PRSendFN)_PR_InvalidInt, - (PRRecvfromFN)_PR_InvalidInt, - (PRSendtoFN)_PR_InvalidInt, - FilePoll, - (PRAcceptreadFN)_PR_InvalidInt, - (PRTransmitfileFN)_PR_InvalidInt, - (PRGetsocknameFN)_PR_InvalidStatus, - (PRGetpeernameFN)_PR_InvalidStatus, - (PRReservedFN)_PR_InvalidInt, - (PRReservedFN)_PR_InvalidInt, - (PRGetsocketoptionFN)_PR_InvalidStatus, + (PRWritevFN)_PR_InvalidInt, + (PRConnectFN)_PR_InvalidStatus, + (PRAcceptFN)_PR_InvalidDesc, + (PRBindFN)_PR_InvalidStatus, + (PRListenFN)_PR_InvalidStatus, + (PRShutdownFN)_PR_InvalidStatus, + (PRRecvFN)_PR_InvalidInt, + (PRSendFN)_PR_InvalidInt, + (PRRecvfromFN)_PR_InvalidInt, + (PRSendtoFN)_PR_InvalidInt, + FilePoll, + (PRAcceptreadFN)_PR_InvalidInt, + (PRTransmitfileFN)_PR_InvalidInt, + (PRGetsocknameFN)_PR_InvalidStatus, + (PRGetpeernameFN)_PR_InvalidStatus, + (PRReservedFN)_PR_InvalidInt, + (PRReservedFN)_PR_InvalidInt, + (PRGetsocketoptionFN)_PR_InvalidStatus, (PRSetsocketoptionFN)_PR_InvalidStatus, - (PRSendfileFN)_PR_InvalidInt, - (PRConnectcontinueFN)_PR_InvalidStatus, - (PRReservedFN)_PR_InvalidInt, - (PRReservedFN)_PR_InvalidInt, - (PRReservedFN)_PR_InvalidInt, + (PRSendfileFN)_PR_InvalidInt, + (PRConnectcontinueFN)_PR_InvalidStatus, + (PRReservedFN)_PR_InvalidInt, + (PRReservedFN)_PR_InvalidInt, + (PRReservedFN)_PR_InvalidInt, (PRReservedFN)_PR_InvalidInt }; @@ -316,7 +327,9 @@ PR_IMPLEMENT(PRFileDesc*) PR_Open(const char *name, PRIntn flags, PRIntn mode) PRBool appendMode = ( PR_APPEND & flags )? PR_TRUE : PR_FALSE; #endif - if (!_pr_initialized) _PR_ImplicitInitialization(); + if (!_pr_initialized) { + _PR_ImplicitInitialization(); + } /* Map pr open flags and mode to os specific flags */ @@ -344,7 +357,9 @@ PR_IMPLEMENT(PRFileDesc*) PR_OpenFile( PRBool appendMode = ( PR_APPEND & flags )? PR_TRUE : PR_FALSE; #endif - if (!_pr_initialized) _PR_ImplicitInitialization(); + if (!_pr_initialized) { + _PR_ImplicitInitialization(); + } /* Map pr open flags and mode to os specific flags */ @@ -369,8 +384,8 @@ PR_IMPLEMENT(PRInt32) PR_GetSysfdTableMax(void) struct rlimit rlim; if ( getrlimit(RLIMIT_NOFILE, &rlim) < 0) { - /* XXX need to call PR_SetError() */ - return -1; + /* XXX need to call PR_SetError() */ + return -1; } return rlim.rlim_max; @@ -388,9 +403,6 @@ PR_IMPLEMENT(PRInt32) PR_GetSysfdTableMax(void) ULONG ulCurMaxFH = 0; DosSetRelMaxFH(&ulReqCount, &ulCurMaxFH); return ulCurMaxFH; -#elif defined(XP_BEOS) - PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0); - return -1; #else write me; #endif @@ -402,19 +414,23 @@ PR_IMPLEMENT(PRInt32) PR_SetSysfdTableSize(int table_size) struct rlimit rlim; PRInt32 tableMax = PR_GetSysfdTableMax(); - if (tableMax < 0) + if (tableMax < 0) { return -1; + } - if (tableMax > FD_SETSIZE) + if (tableMax > FD_SETSIZE) { tableMax = FD_SETSIZE; + } rlim.rlim_max = tableMax; /* Grow as much as we can; even if too big */ - if ( rlim.rlim_max < table_size ) + if ( rlim.rlim_max < table_size ) { rlim.rlim_cur = rlim.rlim_max; - else + } + else { rlim.rlim_cur = table_size; + } if ( setrlimit(RLIMIT_NOFILE, &rlim) < 0) { /* XXX need to call PR_SetError() */ @@ -425,16 +441,18 @@ PR_IMPLEMENT(PRInt32) PR_SetSysfdTableSize(int table_size) #elif defined(XP_OS2) PRInt32 tableMax = PR_GetSysfdTableMax(); if (table_size > tableMax) { - APIRET rc = NO_ERROR; - rc = DosSetMaxFH(table_size); - if (rc == NO_ERROR) - return table_size; - else - return -1; - } + APIRET rc = NO_ERROR; + rc = DosSetMaxFH(table_size); + if (rc == NO_ERROR) { + return table_size; + } + else { + return -1; + } + } return tableMax; #elif defined(AIX) || defined(QNX) \ - || defined(WIN32) || defined(WIN16) || defined(XP_BEOS) + || defined(WIN32) || defined(WIN16) PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0); return -1; #else @@ -444,31 +462,35 @@ PR_IMPLEMENT(PRInt32) PR_SetSysfdTableSize(int table_size) PR_IMPLEMENT(PRStatus) PR_Delete(const char *name) { - PRInt32 rv; + PRInt32 rv; - rv = _PR_MD_DELETE(name); - if (rv < 0) { - return PR_FAILURE; - } else - return PR_SUCCESS; + rv = _PR_MD_DELETE(name); + if (rv < 0) { + return PR_FAILURE; + } else { + return PR_SUCCESS; + } } PR_IMPLEMENT(PRStatus) PR_GetFileInfo(const char *fn, PRFileInfo *info) { - PRInt32 rv; + PRInt32 rv; - rv = _PR_MD_GETFILEINFO(fn, info); - if (rv < 0) { - return PR_FAILURE; - } else - return PR_SUCCESS; + rv = _PR_MD_GETFILEINFO(fn, info); + if (rv < 0) { + return PR_FAILURE; + } else { + return PR_SUCCESS; + } } PR_IMPLEMENT(PRStatus) PR_GetFileInfo64(const char *fn, PRFileInfo64 *info) { PRInt32 rv; - if (!_pr_initialized) _PR_ImplicitInitialization(); + if (!_pr_initialized) { + _PR_ImplicitInitialization(); + } rv = _PR_MD_GETFILEINFO64(fn, info); if (rv < 0) { return PR_FAILURE; @@ -479,34 +501,38 @@ PR_IMPLEMENT(PRStatus) PR_GetFileInfo64(const char *fn, PRFileInfo64 *info) PR_IMPLEMENT(PRStatus) PR_Rename(const char *from, const char *to) { - PRInt32 rv; + PRInt32 rv; - rv = _PR_MD_RENAME(from, to); - if (rv < 0) { - return PR_FAILURE; - } else - return PR_SUCCESS; + rv = _PR_MD_RENAME(from, to); + if (rv < 0) { + return PR_FAILURE; + } else { + return PR_SUCCESS; + } } PR_IMPLEMENT(PRStatus) PR_Access(const char *name, PRAccessHow how) { -PRInt32 rv; + PRInt32 rv; - rv = _PR_MD_ACCESS(name, how); - if (rv < 0) { - return PR_FAILURE; - } else - return PR_SUCCESS; + rv = _PR_MD_ACCESS(name, how); + if (rv < 0) { + return PR_FAILURE; + } else { + return PR_SUCCESS; + } } /* -** Import an existing OS file to NSPR +** Import an existing OS file to NSPR */ PR_IMPLEMENT(PRFileDesc*) PR_ImportFile(PROsfd osfd) { PRFileDesc *fd = NULL; - if (!_pr_initialized) _PR_ImplicitInitialization(); + if (!_pr_initialized) { + _PR_ImplicitInitialization(); + } fd = PR_AllocFileDesc(osfd, &_pr_fileMethods); if( !fd ) { @@ -519,13 +545,15 @@ PR_IMPLEMENT(PRFileDesc*) PR_ImportFile(PROsfd osfd) } /* -** Import an existing OS pipe to NSPR +** Import an existing OS pipe to NSPR */ PR_IMPLEMENT(PRFileDesc*) PR_ImportPipe(PROsfd osfd) { PRFileDesc *fd = NULL; - if (!_pr_initialized) _PR_ImplicitInitialization(); + if (!_pr_initialized) { + _PR_ImplicitInitialization(); + } fd = PR_AllocFileDesc(osfd, &_pr_pipeMethods); if( !fd ) { @@ -552,7 +580,7 @@ PR_IMPLEMENT(PRFileDesc*) PR_ImportPipe(PROsfd osfd) * nspr 1.0. Therefore, it still uses the nspr 1.0 error-reporting * mechanism -- returns a PRInt32, which is the error code when the call * fails. - * + * * If we need this function in nspr 2.0, it should be changed to * return PRStatus, as follows: * @@ -574,7 +602,7 @@ PR_IMPLEMENT(PRInt32) PR_Stat(const char *name, struct stat *buf) PRInt32 rv; rv = _PR_MD_STAT(name, buf); - return rv; + return rv; } #endif /* !defined(WIN16) */ @@ -594,8 +622,9 @@ PR_IMPLEMENT(PRStatus) PR_LockFile(PRFileDesc *fd) #endif PR_Lock(_pr_flock_lock); - while (fd->secret->lockCount == -1) + while (fd->secret->lockCount == -1) { PR_WaitCondVar(_pr_flock_cv, PR_INTERVAL_NO_TIMEOUT); + } if (fd->secret->lockCount == 0) { fd->secret->lockCount = -1; PR_Unlock(_pr_flock_lock); @@ -607,7 +636,7 @@ PR_IMPLEMENT(PRStatus) PR_LockFile(PRFileDesc *fd) fd->secret->lockCount++; } PR_Unlock(_pr_flock_lock); - + return status; } @@ -628,8 +657,9 @@ PR_IMPLEMENT(PRStatus) PR_TLockFile(PRFileDesc *fd) if (fd->secret->lockCount == 0) { status = _PR_MD_TLOCKFILE(fd->secret->md.osfd); PR_ASSERT(status == PR_SUCCESS || fd->secret->lockCount == 0); - if (status == PR_SUCCESS) + if (status == PR_SUCCESS) { fd->secret->lockCount = 1; + } } else { fd->secret->lockCount++; } @@ -645,8 +675,9 @@ PR_IMPLEMENT(PRStatus) PR_UnlockFile(PRFileDesc *fd) PR_Lock(_pr_flock_lock); if (fd->secret->lockCount == 1) { rv = _PR_MD_UNLOCKFILE(fd->secret->md.osfd); - if (rv == PR_SUCCESS) + if (rv == PR_SUCCESS) { fd->secret->lockCount = 0; + } } else { fd->secret->lockCount--; } @@ -664,7 +695,9 @@ PR_IMPLEMENT(PRStatus) PR_CreatePipe( HANDLE readEnd, writeEnd; SECURITY_ATTRIBUTES pipeAttributes; - if (!_pr_initialized) _PR_ImplicitInitialization(); + if (!_pr_initialized) { + _PR_ImplicitInitialization(); + } ZeroMemory(&pipeAttributes, sizeof(pipeAttributes)); pipeAttributes.nLength = sizeof(pipeAttributes); @@ -692,14 +725,16 @@ PR_IMPLEMENT(PRStatus) PR_CreatePipe( (*readPipe)->secret->inheritable = _PR_TRI_TRUE; (*writePipe)->secret->inheritable = _PR_TRI_TRUE; return PR_SUCCESS; -#elif defined(XP_UNIX) || defined(XP_OS2) || defined(XP_BEOS) +#elif defined(XP_UNIX) || defined(XP_OS2) #ifdef XP_OS2 HFILE pipefd[2]; #else int pipefd[2]; #endif - if (!_pr_initialized) _PR_ImplicitInitialization(); + if (!_pr_initialized) { + _PR_ImplicitInitialization(); + } #ifdef XP_OS2 if (DosCreatePipe(&pipefd[0], &pipefd[1], 4096) != 0) { @@ -722,13 +757,9 @@ PR_IMPLEMENT(PRStatus) PR_CreatePipe( close(pipefd[1]); return PR_FAILURE; } -#ifndef XP_BEOS /* Pipes are nonblocking on BeOS */ _PR_MD_MAKE_NONBLOCK(*readPipe); -#endif _PR_MD_INIT_FD_INHERITABLE(*readPipe, PR_FALSE); -#ifndef XP_BEOS /* Pipes are nonblocking on BeOS */ _PR_MD_MAKE_NONBLOCK(*writePipe); -#endif _PR_MD_INIT_FD_INHERITABLE(*writePipe, PR_FALSE); return PR_SUCCESS; #else @@ -741,15 +772,17 @@ PR_IMPLEMENT(PRStatus) PR_CreatePipe( /* ================ UTF16 Interfaces ================================ */ PR_IMPLEMENT(PRFileDesc*) PR_OpenFileUTF16( const PRUnichar *name, PRIntn flags, PRIntn mode) -{ +{ PROsfd osfd; PRFileDesc *fd = 0; #if !defined(_PR_HAVE_O_APPEND) PRBool appendMode = ( PR_APPEND & flags )? PR_TRUE : PR_FALSE; #endif - - if (!_pr_initialized) _PR_ImplicitInitialization(); - + + if (!_pr_initialized) { + _PR_ImplicitInitialization(); + } + /* Map pr open flags and mode to os specific flags */ osfd = _PR_MD_OPEN_FILE_UTF16(name, flags, mode); if (osfd != -1) { @@ -765,12 +798,14 @@ PR_IMPLEMENT(PRFileDesc*) PR_OpenFileUTF16( } return fd; } - + PR_IMPLEMENT(PRStatus) PR_GetFileInfo64UTF16(const PRUnichar *fn, PRFileInfo64 *info) { PRInt32 rv; - if (!_pr_initialized) _PR_ImplicitInitialization(); + if (!_pr_initialized) { + _PR_ImplicitInitialization(); + } rv = _PR_MD_GETFILEINFO64_UTF16(fn, info); if (rv < 0) { return PR_FAILURE; diff --git a/nsprpub/pr/src/io/prio.c b/nsprpub/pr/src/io/prio.c index 10ae5e098c..745d7721b9 100644 --- a/nsprpub/pr/src/io/prio.c +++ b/nsprpub/pr/src/io/prio.c @@ -40,11 +40,11 @@ void _PR_InitIO(void) #ifdef WIN32 _pr_stdin = PR_AllocFileDesc((PROsfd)GetStdHandle(STD_INPUT_HANDLE), - methods); + methods); _pr_stdout = PR_AllocFileDesc((PROsfd)GetStdHandle(STD_OUTPUT_HANDLE), - methods); + methods); _pr_stderr = PR_AllocFileDesc((PROsfd)GetStdHandle(STD_ERROR_HANDLE), - methods); + methods); #ifdef WINNT _pr_stdin->secret->md.sync_file_io = PR_TRUE; _pr_stdout->secret->md.sync_file_io = PR_TRUE; @@ -88,8 +88,10 @@ PR_IMPLEMENT(PRFileDesc*) PR_GetSpecialFD(PRSpecialFD osfd) PRFileDesc *result = NULL; PR_ASSERT((int) osfd >= PR_StandardInput && osfd <= PR_StandardError); - if (!_pr_initialized) _PR_ImplicitInitialization(); - + if (!_pr_initialized) { + _PR_ImplicitInitialization(); + } + switch (osfd) { case PR_StandardInput: result = _pr_stdin; break; @@ -107,25 +109,25 @@ PR_IMPLEMENT(PRFileDesc*) PR_AllocFileDesc( PRFileDesc *fd; #ifdef XP_UNIX - /* - * Assert that the file descriptor is small enough to fit in the - * fd_set passed to select - */ - PR_ASSERT(osfd < FD_SETSIZE); + /* + * Assert that the file descriptor is small enough to fit in the + * fd_set passed to select + */ + PR_ASSERT(osfd < FD_SETSIZE); #endif fd = _PR_Getfd(); if (fd) { /* Initialize the members of PRFileDesc and PRFilePrivate */ fd->methods = methods; fd->secret->state = _PR_FILEDESC_OPEN; - fd->secret->md.osfd = osfd; + fd->secret->md.osfd = osfd; #if defined(_WIN64) fd->secret->alreadyConnected = PR_FALSE; fd->secret->overlappedActive = PR_FALSE; #endif _PR_MD_INIT_FILEDESC(fd); } else { - PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); + PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); } return fd; @@ -144,62 +146,62 @@ PRLock *_fd_waiting_for_overlapped_done_lock = NULL; void CheckOverlappedPendingSocketsAreDone() { - if (!_fd_waiting_for_overlapped_done_lock || - !_fd_waiting_for_overlapped_done) { - return; - } - - PR_Lock(_fd_waiting_for_overlapped_done_lock); - - PRFileDescList *cur = _fd_waiting_for_overlapped_done; - PRFileDescList *previous = NULL; - while (cur) { - PR_ASSERT(cur->fd->secret->overlappedActive); - PRFileDesc *fd = cur->fd; - DWORD rvSent; - if (GetOverlappedResult((HANDLE)fd->secret->md.osfd, &fd->secret->ol, &rvSent, FALSE) == TRUE) { - fd->secret->overlappedActive = PR_FALSE; - PR_LOG(_pr_io_lm, PR_LOG_MIN, - ("CheckOverlappedPendingSocketsAreDone GetOverlappedResult succeeded\n")); - } else { - DWORD err = WSAGetLastError(); - PR_LOG(_pr_io_lm, PR_LOG_MIN, - ("CheckOverlappedPendingSocketsAreDone GetOverlappedResult failed %d\n", err)); - if (err != ERROR_IO_INCOMPLETE) { - fd->secret->overlappedActive = PR_FALSE; - } + if (!_fd_waiting_for_overlapped_done_lock || + !_fd_waiting_for_overlapped_done) { + return; } - if (!fd->secret->overlappedActive) { + PR_Lock(_fd_waiting_for_overlapped_done_lock); + + PRFileDescList *cur = _fd_waiting_for_overlapped_done; + PRFileDescList *previous = NULL; + while (cur) { + PR_ASSERT(cur->fd->secret->overlappedActive); + PRFileDesc *fd = cur->fd; + DWORD rvSent; + if (GetOverlappedResult((HANDLE)fd->secret->md.osfd, &fd->secret->ol, &rvSent, FALSE) == TRUE) { + fd->secret->overlappedActive = PR_FALSE; + PR_LOG(_pr_io_lm, PR_LOG_MIN, + ("CheckOverlappedPendingSocketsAreDone GetOverlappedResult succeeded\n")); + } else { + DWORD err = WSAGetLastError(); + PR_LOG(_pr_io_lm, PR_LOG_MIN, + ("CheckOverlappedPendingSocketsAreDone GetOverlappedResult failed %d\n", err)); + if (err != ERROR_IO_INCOMPLETE) { + fd->secret->overlappedActive = PR_FALSE; + } + } + + if (!fd->secret->overlappedActive) { - _PR_MD_CLOSE_SOCKET(fd->secret->md.osfd); - fd->secret->state = _PR_FILEDESC_CLOSED; + _PR_MD_CLOSE_SOCKET(fd->secret->md.osfd); + fd->secret->state = _PR_FILEDESC_CLOSED; #ifdef _PR_HAVE_PEEK_BUFFER - if (fd->secret->peekBuffer) { - PR_ASSERT(fd->secret->peekBufSize > 0); - PR_DELETE(fd->secret->peekBuffer); - fd->secret->peekBufSize = 0; - fd->secret->peekBytes = 0; - } + if (fd->secret->peekBuffer) { + PR_ASSERT(fd->secret->peekBufSize > 0); + PR_DELETE(fd->secret->peekBuffer); + fd->secret->peekBufSize = 0; + fd->secret->peekBytes = 0; + } #endif - PR_FreeFileDesc(fd); - - if (previous) { - previous->next = cur->next; - } else { - _fd_waiting_for_overlapped_done = cur->next; - } - PRFileDescList *del = cur; - cur = cur->next; - PR_Free(del); - } else { - previous = cur; - cur = cur->next; + PR_FreeFileDesc(fd); + + if (previous) { + previous->next = cur->next; + } else { + _fd_waiting_for_overlapped_done = cur->next; + } + PRFileDescList *del = cur; + cur = cur->next; + PR_Free(del); + } else { + previous = cur; + cur = cur->next; + } } - } - PR_Unlock(_fd_waiting_for_overlapped_done_lock); + PR_Unlock(_fd_waiting_for_overlapped_done_lock); } #endif @@ -209,11 +211,11 @@ void CheckOverlappedPendingSocketsAreDone() PR_IMPLEMENT(PRInt32) PR_Poll(PRPollDesc *pds, PRIntn npds, PRIntervalTime timeout) { #if defined(_WIN64) && defined(WIN95) - // For each iteration check if TFO overlapped IOs are down. - CheckOverlappedPendingSocketsAreDone(); + // For each iteration check if TFO overlapped IOs are down. + CheckOverlappedPendingSocketsAreDone(); #endif - return(_PR_MD_PR_POLL(pds, npds, timeout)); + return(_PR_MD_PR_POLL(pds, npds, timeout)); } /* @@ -223,7 +225,7 @@ PR_IMPLEMENT(PRStatus) PR_SetFDInheritable( PRFileDesc *fd, PRBool inheritable) { -#if defined(XP_UNIX) || defined(WIN32) || defined(XP_OS2) || defined(XP_BEOS) +#if defined(XP_UNIX) || defined(WIN32) || defined(XP_OS2) /* * Only a non-layered, NSPR file descriptor can be inherited * by a child process. diff --git a/nsprpub/pr/src/io/priometh.c b/nsprpub/pr/src/io/priometh.c index 4208767d54..508719a809 100644 --- a/nsprpub/pr/src/io/priometh.c +++ b/nsprpub/pr/src/io/priometh.c @@ -22,26 +22,26 @@ PRIOMethods _pr_faulty_methods = { (PRSeek64FN)_PR_InvalidInt64, (PRFileInfoFN)_PR_InvalidStatus, (PRFileInfo64FN)_PR_InvalidStatus, - (PRWritevFN)_PR_InvalidInt, - (PRConnectFN)_PR_InvalidStatus, - (PRAcceptFN)_PR_InvalidDesc, - (PRBindFN)_PR_InvalidStatus, - (PRListenFN)_PR_InvalidStatus, - (PRShutdownFN)_PR_InvalidStatus, - (PRRecvFN)_PR_InvalidInt, - (PRSendFN)_PR_InvalidInt, - (PRRecvfromFN)_PR_InvalidInt, - (PRSendtoFN)_PR_InvalidInt, + (PRWritevFN)_PR_InvalidInt, + (PRConnectFN)_PR_InvalidStatus, + (PRAcceptFN)_PR_InvalidDesc, + (PRBindFN)_PR_InvalidStatus, + (PRListenFN)_PR_InvalidStatus, + (PRShutdownFN)_PR_InvalidStatus, + (PRRecvFN)_PR_InvalidInt, + (PRSendFN)_PR_InvalidInt, + (PRRecvfromFN)_PR_InvalidInt, + (PRSendtoFN)_PR_InvalidInt, (PRPollFN)_PR_InvalidInt16, - (PRAcceptreadFN)_PR_InvalidInt, - (PRTransmitfileFN)_PR_InvalidInt, - (PRGetsocknameFN)_PR_InvalidStatus, - (PRGetpeernameFN)_PR_InvalidStatus, - (PRReservedFN)_PR_InvalidInt, - (PRReservedFN)_PR_InvalidInt, + (PRAcceptreadFN)_PR_InvalidInt, + (PRTransmitfileFN)_PR_InvalidInt, + (PRGetsocknameFN)_PR_InvalidStatus, + (PRGetpeernameFN)_PR_InvalidStatus, + (PRReservedFN)_PR_InvalidInt, + (PRReservedFN)_PR_InvalidInt, (PRGetsocketoptionFN)_PR_InvalidStatus, (PRSetsocketoptionFN)_PR_InvalidStatus, - (PRSendfileFN)_PR_InvalidInt, + (PRSendfileFN)_PR_InvalidInt, (PRConnectcontinueFN)_PR_InvalidStatus, (PRReservedFN)_PR_InvalidInt, (PRReservedFN)_PR_InvalidInt, @@ -106,159 +106,159 @@ PR_IMPLEMENT(PRStatus) PR_Close(PRFileDesc *fd) PR_IMPLEMENT(PRInt32) PR_Read(PRFileDesc *fd, void *buf, PRInt32 amount) { - return((fd->methods->read)(fd,buf,amount)); + return((fd->methods->read)(fd,buf,amount)); } PR_IMPLEMENT(PRInt32) PR_Write(PRFileDesc *fd, const void *buf, PRInt32 amount) { - return((fd->methods->write)(fd,buf,amount)); + return((fd->methods->write)(fd,buf,amount)); } PR_IMPLEMENT(PRInt32) PR_Seek(PRFileDesc *fd, PRInt32 offset, PRSeekWhence whence) { - return((fd->methods->seek)(fd, offset, whence)); + return((fd->methods->seek)(fd, offset, whence)); } PR_IMPLEMENT(PRInt64) PR_Seek64(PRFileDesc *fd, PRInt64 offset, PRSeekWhence whence) { - return((fd->methods->seek64)(fd, offset, whence)); + return((fd->methods->seek64)(fd, offset, whence)); } PR_IMPLEMENT(PRInt32) PR_Available(PRFileDesc *fd) { - return((fd->methods->available)(fd)); + return((fd->methods->available)(fd)); } PR_IMPLEMENT(PRInt64) PR_Available64(PRFileDesc *fd) { - return((fd->methods->available64)(fd)); + return((fd->methods->available64)(fd)); } PR_IMPLEMENT(PRStatus) PR_GetOpenFileInfo(PRFileDesc *fd, PRFileInfo *info) { - return((fd->methods->fileInfo)(fd, info)); + return((fd->methods->fileInfo)(fd, info)); } PR_IMPLEMENT(PRStatus) PR_GetOpenFileInfo64(PRFileDesc *fd, PRFileInfo64 *info) { - return((fd->methods->fileInfo64)(fd, info)); + return((fd->methods->fileInfo64)(fd, info)); } PR_IMPLEMENT(PRStatus) PR_Sync(PRFileDesc *fd) { - return((fd->methods->fsync)(fd)); + return((fd->methods->fsync)(fd)); } PR_IMPLEMENT(PRStatus) PR_Connect( PRFileDesc *fd, const PRNetAddr *addr, PRIntervalTime timeout) { - return((fd->methods->connect)(fd,addr,timeout)); + return((fd->methods->connect)(fd,addr,timeout)); } PR_IMPLEMENT(PRStatus) PR_ConnectContinue( PRFileDesc *fd, PRInt16 out_flags) { - return((fd->methods->connectcontinue)(fd,out_flags)); + return((fd->methods->connectcontinue)(fd,out_flags)); } PR_IMPLEMENT(PRFileDesc*) PR_Accept(PRFileDesc *fd, PRNetAddr *addr, -PRIntervalTime timeout) + PRIntervalTime timeout) { - return((fd->methods->accept)(fd,addr,timeout)); + return((fd->methods->accept)(fd,addr,timeout)); } PR_IMPLEMENT(PRStatus) PR_Bind(PRFileDesc *fd, const PRNetAddr *addr) { - return((fd->methods->bind)(fd,addr)); + return((fd->methods->bind)(fd,addr)); } PR_IMPLEMENT(PRStatus) PR_Shutdown(PRFileDesc *fd, PRShutdownHow how) { - return((fd->methods->shutdown)(fd,how)); + return((fd->methods->shutdown)(fd,how)); } PR_IMPLEMENT(PRStatus) PR_Listen(PRFileDesc *fd, PRIntn backlog) { - return((fd->methods->listen)(fd,backlog)); + return((fd->methods->listen)(fd,backlog)); } PR_IMPLEMENT(PRInt32) PR_Recv(PRFileDesc *fd, void *buf, PRInt32 amount, -PRIntn flags, PRIntervalTime timeout) + PRIntn flags, PRIntervalTime timeout) { - return((fd->methods->recv)(fd,buf,amount,flags,timeout)); + return((fd->methods->recv)(fd,buf,amount,flags,timeout)); } PR_IMPLEMENT(PRInt32) PR_Send(PRFileDesc *fd, const void *buf, PRInt32 amount, -PRIntn flags, PRIntervalTime timeout) + PRIntn flags, PRIntervalTime timeout) { - return((fd->methods->send)(fd,buf,amount,flags,timeout)); + return((fd->methods->send)(fd,buf,amount,flags,timeout)); } PR_IMPLEMENT(PRInt32) PR_Writev(PRFileDesc *fd, const PRIOVec *iov, -PRInt32 iov_size, PRIntervalTime timeout) + PRInt32 iov_size, PRIntervalTime timeout) { if (iov_size > PR_MAX_IOVECTOR_SIZE) { PR_SetError(PR_BUFFER_OVERFLOW_ERROR, 0); return -1; } - return((fd->methods->writev)(fd,iov,iov_size,timeout)); + return((fd->methods->writev)(fd,iov,iov_size,timeout)); } PR_IMPLEMENT(PRInt32) PR_RecvFrom(PRFileDesc *fd, void *buf, PRInt32 amount, -PRIntn flags, PRNetAddr *addr, PRIntervalTime timeout) + PRIntn flags, PRNetAddr *addr, PRIntervalTime timeout) { - return((fd->methods->recvfrom)(fd,buf,amount,flags,addr,timeout)); + return((fd->methods->recvfrom)(fd,buf,amount,flags,addr,timeout)); } PR_IMPLEMENT(PRInt32) PR_SendTo( PRFileDesc *fd, const void *buf, PRInt32 amount, PRIntn flags, const PRNetAddr *addr, PRIntervalTime timeout) { - return((fd->methods->sendto)(fd,buf,amount,flags,addr,timeout)); + return((fd->methods->sendto)(fd,buf,amount,flags,addr,timeout)); } PR_IMPLEMENT(PRInt32) PR_TransmitFile( PRFileDesc *sd, PRFileDesc *fd, const void *hdr, PRInt32 hlen, PRTransmitFileFlags flags, PRIntervalTime timeout) { - return((sd->methods->transmitfile)(sd,fd,hdr,hlen,flags,timeout)); + return((sd->methods->transmitfile)(sd,fd,hdr,hlen,flags,timeout)); } PR_IMPLEMENT(PRInt32) PR_AcceptRead( PRFileDesc *sd, PRFileDesc **nd, PRNetAddr **raddr, void *buf, PRInt32 amount, PRIntervalTime timeout) { - return((sd->methods->acceptread)(sd, nd, raddr, buf, amount,timeout)); + return((sd->methods->acceptread)(sd, nd, raddr, buf, amount,timeout)); } PR_IMPLEMENT(PRStatus) PR_GetSockName(PRFileDesc *fd, PRNetAddr *addr) { - return((fd->methods->getsockname)(fd,addr)); + return((fd->methods->getsockname)(fd,addr)); } PR_IMPLEMENT(PRStatus) PR_GetPeerName(PRFileDesc *fd, PRNetAddr *addr) { - return((fd->methods->getpeername)(fd,addr)); + return((fd->methods->getpeername)(fd,addr)); } PR_IMPLEMENT(PRStatus) PR_GetSocketOption( PRFileDesc *fd, PRSocketOptionData *data) { - return((fd->methods->getsocketoption)(fd, data)); + return((fd->methods->getsocketoption)(fd, data)); } PR_IMPLEMENT(PRStatus) PR_SetSocketOption( PRFileDesc *fd, const PRSocketOptionData *data) { - return((fd->methods->setsocketoption)(fd, data)); + return((fd->methods->setsocketoption)(fd, data)); } PR_IMPLEMENT(PRInt32) PR_SendFile( - PRFileDesc *sd, PRSendFileData *sfd, - PRTransmitFileFlags flags, PRIntervalTime timeout) + PRFileDesc *sd, PRSendFileData *sfd, + PRTransmitFileFlags flags, PRIntervalTime timeout) { - return((sd->methods->sendfile)(sd,sfd,flags,timeout)); + return((sd->methods->sendfile)(sd,sfd,flags,timeout)); } PR_IMPLEMENT(PRInt32) PR_EmulateAcceptRead( @@ -274,7 +274,9 @@ PR_IMPLEMENT(PRInt32) PR_EmulateAcceptRead( ** operation - it waits indefinitely. */ accepted = PR_Accept(sd, &remote, PR_INTERVAL_NO_TIMEOUT); - if (NULL == accepted) return rv; + if (NULL == accepted) { + return rv; + } rv = PR_Recv(accepted, buf, amount, 0, timeout); if (rv >= 0) @@ -299,7 +301,7 @@ PR_IMPLEMENT(PRInt32) PR_EmulateAcceptRead( * they are sent before and after the file, respectively. * * PR_TRANSMITFILE_CLOSE_SOCKET flag - close socket after sending file - * + * * return number of bytes sent or -1 on error * */ @@ -310,7 +312,7 @@ PR_IMPLEMENT(PRInt32) PR_EmulateAcceptRead( * An implementation based on memory-mapped files */ -#define SENDFILE_MMAP_CHUNK (256 * 1024) +#define SENDFILE_MMAP_CHUNK (256 * 1024) PR_IMPLEMENT(PRInt32) PR_EmulateSendFile( PRFileDesc *sd, PRSendFileData *sfd, @@ -333,7 +335,7 @@ PR_IMPLEMENT(PRInt32) PR_EmulateSendFile( goto done; } if (sfd->file_nbytes && - (info.size < (sfd->file_offset + sfd->file_nbytes))) { + (info.size < (sfd->file_offset + sfd->file_nbytes))) { /* * there are fewer bytes in file to send than specified */ @@ -341,10 +343,12 @@ PR_IMPLEMENT(PRInt32) PR_EmulateSendFile( count = -1; goto done; } - if (sfd->file_nbytes) + if (sfd->file_nbytes) { file_bytes = sfd->file_nbytes; - else + } + else { file_bytes = info.size - sfd->file_offset; + } alignment = PR_GetMemMapAlignment(); @@ -400,8 +404,9 @@ PR_IMPLEMENT(PRInt32) PR_EmulateSendFile( index++; } rv = PR_Writev(sd, iov, index, timeout); - if (len) + if (len) { PR_MemUnmap(addr, mmap_len); + } if (rv < 0) { count = -1; goto done; @@ -411,8 +416,9 @@ PR_IMPLEMENT(PRInt32) PR_EmulateSendFile( file_bytes -= len; count += rv; - if (!file_bytes) /* header, file and trailer are sent */ + if (!file_bytes) { /* header, file and trailer are sent */ goto done; + } /* * send remaining bytes of the file, if any @@ -449,14 +455,17 @@ PR_IMPLEMENT(PRInt32) PR_EmulateSendFile( if (rv >= 0) { PR_ASSERT(rv == sfd->tlen); count += rv; - } else + } else { count = -1; + } } done: - if (mapHandle) + if (mapHandle) { PR_CloseFileMap(mapHandle); - if ((count >= 0) && (flags & PR_TRANSMITFILE_CLOSE_SOCKET)) + } + if ((count >= 0) && (flags & PR_TRANSMITFILE_CLOSE_SOCKET)) { PR_Close(sd); + } return count; } @@ -584,10 +593,12 @@ PR_IMPLEMENT(PRInt32) PR_EmulateSendFile( rv = count; done: - if (buf) + if (buf) { PR_DELETE(buf); - if ((rv >= 0) && (flags & PR_TRANSMITFILE_CLOSE_SOCKET)) + } + if ((rv >= 0) && (flags & PR_TRANSMITFILE_CLOSE_SOCKET)) { PR_Close(sd); + } return rv; } diff --git a/nsprpub/pr/src/io/pripv6.c b/nsprpub/pr/src/io/pripv6.c index af7de49dea..1c299652e6 100644 --- a/nsprpub/pr/src/io/pripv6.c +++ b/nsprpub/pr/src/io/pripv6.c @@ -16,117 +16,117 @@ static PRIOMethods ipv6_to_v4_tcpMethods; static PRIOMethods ipv6_to_v4_udpMethods; static PRDescIdentity _pr_ipv6_to_ipv4_id; extern PRBool IsValidNetAddr(const PRNetAddr *addr); -extern PRIPv6Addr _pr_in6addr_any; -extern PRIPv6Addr _pr_in6addr_loopback; +extern const PRIPv6Addr _pr_in6addr_any; +extern const PRIPv6Addr _pr_in6addr_loopback; /* * convert an IPv4-mapped IPv6 addr to an IPv4 addr */ static void _PR_ConvertToIpv4NetAddr(const PRNetAddr *src_v6addr, - PRNetAddr *dst_v4addr) + PRNetAddr *dst_v4addr) { -const PRUint8 *srcp; + const PRUint8 *srcp; - PR_ASSERT(PR_AF_INET6 == src_v6addr->ipv6.family); + PR_ASSERT(PR_AF_INET6 == src_v6addr->ipv6.family); - if (PR_IsNetAddrType(src_v6addr, PR_IpAddrV4Mapped)) { - srcp = src_v6addr->ipv6.ip.pr_s6_addr; - memcpy((char *) &dst_v4addr->inet.ip, srcp + 12, 4); + if (PR_IsNetAddrType(src_v6addr, PR_IpAddrV4Mapped)) { + srcp = src_v6addr->ipv6.ip.pr_s6_addr; + memcpy((char *) &dst_v4addr->inet.ip, srcp + 12, 4); } else if (PR_IsNetAddrType(src_v6addr, PR_IpAddrAny)) { dst_v4addr->inet.ip = htonl(INADDR_ANY); } else if (PR_IsNetAddrType(src_v6addr, PR_IpAddrLoopback)) { dst_v4addr->inet.ip = htonl(INADDR_LOOPBACK); } - dst_v4addr->inet.family = PR_AF_INET; - dst_v4addr->inet.port = src_v6addr->ipv6.port; + dst_v4addr->inet.family = PR_AF_INET; + dst_v4addr->inet.port = src_v6addr->ipv6.port; } /* * convert an IPv4 addr to an IPv4-mapped IPv6 addr */ static void _PR_ConvertToIpv6NetAddr(const PRNetAddr *src_v4addr, - PRNetAddr *dst_v6addr) + PRNetAddr *dst_v6addr) { -PRUint8 *dstp; - - PR_ASSERT(PR_AF_INET == src_v4addr->inet.family); - dst_v6addr->ipv6.family = PR_AF_INET6; - dst_v6addr->ipv6.port = src_v4addr->inet.port; - - if (htonl(INADDR_ANY) == src_v4addr->inet.ip) { - dst_v6addr->ipv6.ip = _pr_in6addr_any; - } else { - dstp = dst_v6addr->ipv6.ip.pr_s6_addr; - memset(dstp, 0, 10); - memset(dstp + 10, 0xff, 2); - memcpy(dstp + 12,(char *) &src_v4addr->inet.ip, 4); - } + PRUint8 *dstp; + + PR_ASSERT(PR_AF_INET == src_v4addr->inet.family); + dst_v6addr->ipv6.family = PR_AF_INET6; + dst_v6addr->ipv6.port = src_v4addr->inet.port; + + if (htonl(INADDR_ANY) == src_v4addr->inet.ip) { + dst_v6addr->ipv6.ip = _pr_in6addr_any; + } else { + dstp = dst_v6addr->ipv6.ip.pr_s6_addr; + memset(dstp, 0, 10); + memset(dstp + 10, 0xff, 2); + memcpy(dstp + 12,(char *) &src_v4addr->inet.ip, 4); + } } static PRStatus PR_CALLBACK Ipv6ToIpv4SocketBind(PRFileDesc *fd, - const PRNetAddr *addr) + const PRNetAddr *addr) { - PRNetAddr tmp_ipv4addr; - const PRNetAddr *tmp_addrp; - PRFileDesc *lo = fd->lower; + PRNetAddr tmp_ipv4addr; + const PRNetAddr *tmp_addrp; + PRFileDesc *lo = fd->lower; - if (PR_AF_INET6 != addr->raw.family) { + if (PR_AF_INET6 != addr->raw.family) { PR_SetError(PR_ADDRESS_NOT_SUPPORTED_ERROR, 0); - return PR_FAILURE; - } - if (PR_IsNetAddrType(addr, PR_IpAddrV4Mapped) || - PR_IsNetAddrType(addr, PR_IpAddrAny)) { - _PR_ConvertToIpv4NetAddr(addr, &tmp_ipv4addr); - tmp_addrp = &tmp_ipv4addr; - } else { + return PR_FAILURE; + } + if (PR_IsNetAddrType(addr, PR_IpAddrV4Mapped) || + PR_IsNetAddrType(addr, PR_IpAddrAny)) { + _PR_ConvertToIpv4NetAddr(addr, &tmp_ipv4addr); + tmp_addrp = &tmp_ipv4addr; + } else { PR_SetError(PR_NETWORK_UNREACHABLE_ERROR, 0); - return PR_FAILURE; - } - return((lo->methods->bind)(lo,tmp_addrp)); + return PR_FAILURE; + } + return((lo->methods->bind)(lo,tmp_addrp)); } static PRStatus PR_CALLBACK Ipv6ToIpv4SocketConnect( PRFileDesc *fd, const PRNetAddr *addr, PRIntervalTime timeout) { - PRNetAddr tmp_ipv4addr; - const PRNetAddr *tmp_addrp; + PRNetAddr tmp_ipv4addr; + const PRNetAddr *tmp_addrp; - if (PR_AF_INET6 != addr->raw.family) { + if (PR_AF_INET6 != addr->raw.family) { PR_SetError(PR_ADDRESS_NOT_SUPPORTED_ERROR, 0); - return PR_FAILURE; - } - if (PR_IsNetAddrType(addr, PR_IpAddrV4Mapped) || - PR_IsNetAddrType(addr, PR_IpAddrLoopback)) { - _PR_ConvertToIpv4NetAddr(addr, &tmp_ipv4addr); - tmp_addrp = &tmp_ipv4addr; - } else { + return PR_FAILURE; + } + if (PR_IsNetAddrType(addr, PR_IpAddrV4Mapped) || + PR_IsNetAddrType(addr, PR_IpAddrLoopback)) { + _PR_ConvertToIpv4NetAddr(addr, &tmp_ipv4addr); + tmp_addrp = &tmp_ipv4addr; + } else { PR_SetError(PR_NETWORK_UNREACHABLE_ERROR, 0); - return PR_FAILURE; - } - return (fd->lower->methods->connect)(fd->lower, tmp_addrp, timeout); + return PR_FAILURE; + } + return (fd->lower->methods->connect)(fd->lower, tmp_addrp, timeout); } static PRInt32 PR_CALLBACK Ipv6ToIpv4SocketSendTo( PRFileDesc *fd, const void *buf, PRInt32 amount, PRIntn flags, const PRNetAddr *addr, PRIntervalTime timeout) { - PRNetAddr tmp_ipv4addr; - const PRNetAddr *tmp_addrp; + PRNetAddr tmp_ipv4addr; + const PRNetAddr *tmp_addrp; - if (PR_AF_INET6 != addr->raw.family) { + if (PR_AF_INET6 != addr->raw.family) { PR_SetError(PR_ADDRESS_NOT_SUPPORTED_ERROR, 0); - return PR_FAILURE; - } - if (PR_IsNetAddrType(addr, PR_IpAddrV4Mapped) || - PR_IsNetAddrType(addr, PR_IpAddrLoopback)) { - _PR_ConvertToIpv4NetAddr(addr, &tmp_ipv4addr); - tmp_addrp = &tmp_ipv4addr; - } else { + return PR_FAILURE; + } + if (PR_IsNetAddrType(addr, PR_IpAddrV4Mapped) || + PR_IsNetAddrType(addr, PR_IpAddrLoopback)) { + _PR_ConvertToIpv4NetAddr(addr, &tmp_ipv4addr); + tmp_addrp = &tmp_ipv4addr; + } else { PR_SetError(PR_NETWORK_UNREACHABLE_ERROR, 0); - return PR_FAILURE; - } + return PR_FAILURE; + } return (fd->lower->methods->sendto)( - fd->lower, buf, amount, flags, tmp_addrp, timeout); + fd->lower, buf, amount, flags, tmp_addrp, timeout); } static PRFileDesc* PR_CALLBACK Ipv6ToIpv4SocketAccept ( @@ -135,7 +135,7 @@ static PRFileDesc* PR_CALLBACK Ipv6ToIpv4SocketAccept ( PRStatus rv; PRFileDesc *newfd; PRFileDesc *newstack; - PRNetAddr tmp_ipv4addr; + PRNetAddr tmp_ipv4addr; PRNetAddr *addrlower = NULL; PR_ASSERT(fd != NULL); @@ -149,16 +149,18 @@ static PRFileDesc* PR_CALLBACK Ipv6ToIpv4SocketAccept ( } *newstack = *fd; /* make a copy of the accepting layer */ - if (addr) + if (addr) { addrlower = &tmp_ipv4addr; + } newfd = (fd->lower->methods->accept)(fd->lower, addrlower, timeout); if (NULL == newfd) { PR_DELETE(newstack); return NULL; } - if (addr) + if (addr) { _PR_ConvertToIpv6NetAddr(&tmp_ipv4addr, addr); + } rv = PR_PushIOLayer(newfd, PR_TOP_IO_LAYER, newstack); PR_ASSERT(PR_SUCCESS == rv); @@ -166,12 +168,12 @@ static PRFileDesc* PR_CALLBACK Ipv6ToIpv4SocketAccept ( } static PRInt32 PR_CALLBACK Ipv6ToIpv4SocketAcceptRead(PRFileDesc *sd, - PRFileDesc **nd, PRNetAddr **ipv6_raddr, void *buf, PRInt32 amount, - PRIntervalTime timeout) + PRFileDesc **nd, PRNetAddr **ipv6_raddr, void *buf, PRInt32 amount, + PRIntervalTime timeout) { PRInt32 nbytes; PRStatus rv; - PRNetAddr tmp_ipv4addr; + PRNetAddr tmp_ipv4addr; PRFileDesc *newstack; PR_ASSERT(sd != NULL); @@ -186,14 +188,14 @@ static PRInt32 PR_CALLBACK Ipv6ToIpv4SocketAcceptRead(PRFileDesc *sd, *newstack = *sd; /* make a copy of the accepting layer */ nbytes = sd->lower->methods->acceptread( - sd->lower, nd, ipv6_raddr, buf, amount, timeout); + sd->lower, nd, ipv6_raddr, buf, amount, timeout); if (-1 == nbytes) { PR_DELETE(newstack); return nbytes; } - tmp_ipv4addr = **ipv6_raddr; /* copy */ - _PR_ConvertToIpv6NetAddr(&tmp_ipv4addr, *ipv6_raddr); + tmp_ipv4addr = **ipv6_raddr; /* copy */ + _PR_ConvertToIpv6NetAddr(&tmp_ipv4addr, *ipv6_raddr); /* this PR_PushIOLayer call cannot fail */ rv = PR_PushIOLayer(*nd, PR_TOP_IO_LAYER, newstack); @@ -202,52 +204,52 @@ static PRInt32 PR_CALLBACK Ipv6ToIpv4SocketAcceptRead(PRFileDesc *sd, } static PRStatus PR_CALLBACK Ipv6ToIpv4SocketGetName(PRFileDesc *fd, - PRNetAddr *ipv6addr) + PRNetAddr *ipv6addr) { - PRStatus result; - PRNetAddr tmp_ipv4addr; - - result = (fd->lower->methods->getsockname)(fd->lower, &tmp_ipv4addr); - if (PR_SUCCESS == result) { - _PR_ConvertToIpv6NetAddr(&tmp_ipv4addr, ipv6addr); - PR_ASSERT(IsValidNetAddr(ipv6addr) == PR_TRUE); - } - return result; + PRStatus result; + PRNetAddr tmp_ipv4addr; + + result = (fd->lower->methods->getsockname)(fd->lower, &tmp_ipv4addr); + if (PR_SUCCESS == result) { + _PR_ConvertToIpv6NetAddr(&tmp_ipv4addr, ipv6addr); + PR_ASSERT(IsValidNetAddr(ipv6addr) == PR_TRUE); + } + return result; } static PRStatus PR_CALLBACK Ipv6ToIpv4SocketGetPeerName(PRFileDesc *fd, - PRNetAddr *ipv6addr) + PRNetAddr *ipv6addr) { - PRStatus result; - PRNetAddr tmp_ipv4addr; - - result = (fd->lower->methods->getpeername)(fd->lower, &tmp_ipv4addr); - if (PR_SUCCESS == result) { - _PR_ConvertToIpv6NetAddr(&tmp_ipv4addr, ipv6addr); - PR_ASSERT(IsValidNetAddr(ipv6addr) == PR_TRUE); - } - return result; + PRStatus result; + PRNetAddr tmp_ipv4addr; + + result = (fd->lower->methods->getpeername)(fd->lower, &tmp_ipv4addr); + if (PR_SUCCESS == result) { + _PR_ConvertToIpv6NetAddr(&tmp_ipv4addr, ipv6addr); + PR_ASSERT(IsValidNetAddr(ipv6addr) == PR_TRUE); + } + return result; } static PRInt32 PR_CALLBACK Ipv6ToIpv4SocketRecvFrom(PRFileDesc *fd, void *buf, - PRInt32 amount, PRIntn flags, PRNetAddr *ipv6addr, - PRIntervalTime timeout) + PRInt32 amount, PRIntn flags, PRNetAddr *ipv6addr, + PRIntervalTime timeout) { - PRNetAddr tmp_ipv4addr; - PRInt32 result; + PRNetAddr tmp_ipv4addr; + PRInt32 result; result = (fd->lower->methods->recvfrom)( - fd->lower, buf, amount, flags, &tmp_ipv4addr, timeout); - if (-1 != result) { - _PR_ConvertToIpv6NetAddr(&tmp_ipv4addr, ipv6addr); - PR_ASSERT(IsValidNetAddr(ipv6addr) == PR_TRUE); - } - return result; + fd->lower, buf, amount, flags, &tmp_ipv4addr, timeout); + if (-1 != result) { + _PR_ConvertToIpv6NetAddr(&tmp_ipv4addr, ipv6addr); + PR_ASSERT(IsValidNetAddr(ipv6addr) == PR_TRUE); + } + return result; } #if defined(_PR_INET6_PROBE) static PRBool ipv6_is_present; -extern PRBool _pr_test_ipv6_socket(void); +PR_EXTERN(PRBool) _pr_test_ipv6_socket(void); #if !defined(_PR_INET6) && defined(_PR_HAVE_GETIPNODEBYNAME) extern PRStatus _pr_find_getipnodebyname(void); @@ -261,13 +263,15 @@ static PRBool _pr_probe_ipv6_presence(void) { #if !defined(_PR_INET6) && defined(_PR_HAVE_GETIPNODEBYNAME) - if (_pr_find_getipnodebyname() != PR_SUCCESS) + if (_pr_find_getipnodebyname() != PR_SUCCESS) { return PR_FALSE; + } #endif #if !defined(_PR_INET6) && defined(_PR_HAVE_GETADDRINFO) - if (_pr_find_getaddrinfo() != PR_SUCCESS) + if (_pr_find_getaddrinfo() != PR_SUCCESS) { return PR_FALSE; + } #endif return _pr_test_ipv6_socket(); @@ -282,83 +286,87 @@ static PRStatus PR_CALLBACK _pr_init_ipv6(void) #if defined(_PR_INET6_PROBE) ipv6_is_present = _pr_probe_ipv6_presence(); - if (ipv6_is_present) + if (ipv6_is_present) { return PR_SUCCESS; + } #endif _pr_ipv6_to_ipv4_id = PR_GetUniqueIdentity("Ipv6_to_Ipv4 layer"); PR_ASSERT(PR_INVALID_IO_LAYER != _pr_ipv6_to_ipv4_id); - stubMethods = PR_GetDefaultIOMethods(); - - ipv6_to_v4_tcpMethods = *stubMethods; /* first get the entire batch */ - /* then override the ones we care about */ - ipv6_to_v4_tcpMethods.connect = Ipv6ToIpv4SocketConnect; - ipv6_to_v4_tcpMethods.bind = Ipv6ToIpv4SocketBind; - ipv6_to_v4_tcpMethods.accept = Ipv6ToIpv4SocketAccept; - ipv6_to_v4_tcpMethods.acceptread = Ipv6ToIpv4SocketAcceptRead; - ipv6_to_v4_tcpMethods.getsockname = Ipv6ToIpv4SocketGetName; - ipv6_to_v4_tcpMethods.getpeername = Ipv6ToIpv4SocketGetPeerName; -/* - ipv6_to_v4_tcpMethods.getsocketoption = Ipv6ToIpv4GetSocketOption; - ipv6_to_v4_tcpMethods.setsocketoption = Ipv6ToIpv4SetSocketOption; -*/ - ipv6_to_v4_udpMethods = *stubMethods; /* first get the entire batch */ - /* then override the ones we care about */ - ipv6_to_v4_udpMethods.connect = Ipv6ToIpv4SocketConnect; - ipv6_to_v4_udpMethods.bind = Ipv6ToIpv4SocketBind; - ipv6_to_v4_udpMethods.sendto = Ipv6ToIpv4SocketSendTo; - ipv6_to_v4_udpMethods.recvfrom = Ipv6ToIpv4SocketRecvFrom; - ipv6_to_v4_udpMethods.getsockname = Ipv6ToIpv4SocketGetName; - ipv6_to_v4_udpMethods.getpeername = Ipv6ToIpv4SocketGetPeerName; -/* - ipv6_to_v4_udpMethods.getsocketoption = Ipv6ToIpv4GetSocketOption; - ipv6_to_v4_udpMethods.setsocketoption = Ipv6ToIpv4SetSocketOption; -*/ - return PR_SUCCESS; + stubMethods = PR_GetDefaultIOMethods(); + + ipv6_to_v4_tcpMethods = *stubMethods; /* first get the entire batch */ + /* then override the ones we care about */ + ipv6_to_v4_tcpMethods.connect = Ipv6ToIpv4SocketConnect; + ipv6_to_v4_tcpMethods.bind = Ipv6ToIpv4SocketBind; + ipv6_to_v4_tcpMethods.accept = Ipv6ToIpv4SocketAccept; + ipv6_to_v4_tcpMethods.acceptread = Ipv6ToIpv4SocketAcceptRead; + ipv6_to_v4_tcpMethods.getsockname = Ipv6ToIpv4SocketGetName; + ipv6_to_v4_tcpMethods.getpeername = Ipv6ToIpv4SocketGetPeerName; + /* + ipv6_to_v4_tcpMethods.getsocketoption = Ipv6ToIpv4GetSocketOption; + ipv6_to_v4_tcpMethods.setsocketoption = Ipv6ToIpv4SetSocketOption; + */ + ipv6_to_v4_udpMethods = *stubMethods; /* first get the entire batch */ + /* then override the ones we care about */ + ipv6_to_v4_udpMethods.connect = Ipv6ToIpv4SocketConnect; + ipv6_to_v4_udpMethods.bind = Ipv6ToIpv4SocketBind; + ipv6_to_v4_udpMethods.sendto = Ipv6ToIpv4SocketSendTo; + ipv6_to_v4_udpMethods.recvfrom = Ipv6ToIpv4SocketRecvFrom; + ipv6_to_v4_udpMethods.getsockname = Ipv6ToIpv4SocketGetName; + ipv6_to_v4_udpMethods.getpeername = Ipv6ToIpv4SocketGetPeerName; + /* + ipv6_to_v4_udpMethods.getsocketoption = Ipv6ToIpv4GetSocketOption; + ipv6_to_v4_udpMethods.setsocketoption = Ipv6ToIpv4SetSocketOption; + */ + return PR_SUCCESS; } #if defined(_PR_INET6_PROBE) PRBool _pr_ipv6_is_present(void) { - if (PR_CallOnce(&_pr_init_ipv6_once, _pr_init_ipv6) != PR_SUCCESS) + if (PR_CallOnce(&_pr_init_ipv6_once, _pr_init_ipv6) != PR_SUCCESS) { return PR_FALSE; + } return ipv6_is_present; } #endif PR_IMPLEMENT(PRStatus) _pr_push_ipv6toipv4_layer(PRFileDesc *fd) { - PRFileDesc *ipv6_fd = NULL; - - if (PR_CallOnce(&_pr_init_ipv6_once, _pr_init_ipv6) != PR_SUCCESS) - return PR_FAILURE; - - /* - * For platforms with no support for IPv6 - * create layered socket for IPv4-mapped IPv6 addresses - */ - if (fd->methods->file_type == PR_DESC_SOCKET_TCP) - ipv6_fd = PR_CreateIOLayerStub(_pr_ipv6_to_ipv4_id, - &ipv6_to_v4_tcpMethods); - else - ipv6_fd = PR_CreateIOLayerStub(_pr_ipv6_to_ipv4_id, - &ipv6_to_v4_udpMethods); - if (NULL == ipv6_fd) { - goto errorExit; - } - ipv6_fd->secret = NULL; - - if (PR_PushIOLayer(fd, PR_TOP_IO_LAYER, ipv6_fd) == PR_FAILURE) { - goto errorExit; - } - - return PR_SUCCESS; + PRFileDesc *ipv6_fd = NULL; + + if (PR_CallOnce(&_pr_init_ipv6_once, _pr_init_ipv6) != PR_SUCCESS) { + return PR_FAILURE; + } + + /* + * For platforms with no support for IPv6 + * create layered socket for IPv4-mapped IPv6 addresses + */ + if (fd->methods->file_type == PR_DESC_SOCKET_TCP) + ipv6_fd = PR_CreateIOLayerStub(_pr_ipv6_to_ipv4_id, + &ipv6_to_v4_tcpMethods); + else + ipv6_fd = PR_CreateIOLayerStub(_pr_ipv6_to_ipv4_id, + &ipv6_to_v4_udpMethods); + if (NULL == ipv6_fd) { + goto errorExit; + } + ipv6_fd->secret = NULL; + + if (PR_PushIOLayer(fd, PR_TOP_IO_LAYER, ipv6_fd) == PR_FAILURE) { + goto errorExit; + } + + return PR_SUCCESS; errorExit: - if (ipv6_fd) - ipv6_fd->dtor(ipv6_fd); - return PR_FAILURE; + if (ipv6_fd) { + ipv6_fd->dtor(ipv6_fd); + } + return PR_FAILURE; } #endif /* !defined(_PR_INET6) || defined(_PR_INET6_PROBE) */ diff --git a/nsprpub/pr/src/io/prlayer.c b/nsprpub/pr/src/io/prlayer.c index 00da0287a1..1e63b7b723 100644 --- a/nsprpub/pr/src/io/prlayer.c +++ b/nsprpub/pr/src/io/prlayer.c @@ -21,8 +21,12 @@ static PRStatus _PR_DestroyIOLayer(PRFileDesc *stack); void PR_CALLBACK pl_FDDestructor(PRFileDesc *fd) { PR_ASSERT(fd != NULL); - if (NULL != fd->lower) fd->lower->higher = fd->higher; - if (NULL != fd->higher) fd->higher->lower = fd->lower; + if (NULL != fd->lower) { + fd->lower->higher = fd->higher; + } + if (NULL != fd->higher) { + fd->higher->lower = fd->lower; + } PR_DELETE(fd); } @@ -32,42 +36,42 @@ void PR_CALLBACK pl_FDDestructor(PRFileDesc *fd) static PRStatus PR_CALLBACK pl_TopClose (PRFileDesc *fd) { PRFileDesc *top, *lower; - PRStatus rv; + PRStatus rv; PR_ASSERT(fd != NULL); PR_ASSERT(fd->lower != NULL); PR_ASSERT(fd->secret == NULL); PR_ASSERT(fd->methods->file_type == PR_DESC_LAYERED); - if (PR_IO_LAYER_HEAD == fd->identity) { - /* - * new style stack; close all the layers, before deleting the - * stack head - */ - rv = fd->lower->methods->close(fd->lower); - _PR_DestroyIOLayer(fd); - return rv; - } - if ((fd->higher) && (PR_IO_LAYER_HEAD == fd->higher->identity)) { - /* - * lower layers of new style stack - */ - lower = fd->lower; - /* - * pop and cleanup current layer - */ - top = PR_PopIOLayer(fd->higher, PR_TOP_IO_LAYER); - top->dtor(top); - /* - * then call lower layer - */ - return (lower->methods->close(lower)); - } else { - /* old style stack */ - top = PR_PopIOLayer(fd, PR_TOP_IO_LAYER); - top->dtor(top); - return (fd->methods->close)(fd); - } + if (PR_IO_LAYER_HEAD == fd->identity) { + /* + * new style stack; close all the layers, before deleting the + * stack head + */ + rv = fd->lower->methods->close(fd->lower); + _PR_DestroyIOLayer(fd); + return rv; + } + if ((fd->higher) && (PR_IO_LAYER_HEAD == fd->higher->identity)) { + /* + * lower layers of new style stack + */ + lower = fd->lower; + /* + * pop and cleanup current layer + */ + top = PR_PopIOLayer(fd->higher, PR_TOP_IO_LAYER); + top->dtor(top); + /* + * then call lower layer + */ + return (lower->methods->close(lower)); + } else { + /* old style stack */ + top = PR_PopIOLayer(fd, PR_TOP_IO_LAYER); + top->dtor(top); + return (fd->methods->close)(fd); + } } static PRInt32 PR_CALLBACK pl_DefRead (PRFileDesc *fd, void *buf, PRInt32 amount) @@ -146,7 +150,7 @@ static PRStatus PR_CALLBACK pl_DefFileInfo64 (PRFileDesc *fd, PRFileInfo64 *info } static PRInt32 PR_CALLBACK pl_DefWritev (PRFileDesc *fd, const PRIOVec *iov, - PRInt32 size, PRIntervalTime timeout) + PRInt32 size, PRIntervalTime timeout) { PR_ASSERT(fd != NULL); PR_ASSERT(fd->lower != NULL); @@ -178,15 +182,16 @@ static PRFileDesc* PR_CALLBACK pl_TopAccept ( PRStatus rv; PRFileDesc *newfd, *layer = fd; PRFileDesc *newstack; - PRBool newstyle_stack = PR_FALSE; + PRBool newstyle_stack = PR_FALSE; PR_ASSERT(fd != NULL); PR_ASSERT(fd->lower != NULL); - /* test for new style stack */ - while (NULL != layer->higher) - layer = layer->higher; - newstyle_stack = (PR_IO_LAYER_HEAD == layer->identity) ? PR_TRUE : PR_FALSE; + /* test for new style stack */ + while (NULL != layer->higher) { + layer = layer->higher; + } + newstyle_stack = (PR_IO_LAYER_HEAD == layer->identity) ? PR_TRUE : PR_FALSE; newstack = PR_NEW(PRFileDesc); if (NULL == newstack) { @@ -246,7 +251,7 @@ static PRInt32 PR_CALLBACK pl_DefRecv ( PR_ASSERT(fd->lower != NULL); return (fd->lower->methods->recv)( - fd->lower, buf, amount, flags, timeout); + fd->lower, buf, amount, flags, timeout); } static PRInt32 PR_CALLBACK pl_DefSend ( @@ -267,7 +272,7 @@ static PRInt32 PR_CALLBACK pl_DefRecvfrom ( PR_ASSERT(fd->lower != NULL); return (fd->lower->methods->recvfrom)( - fd->lower, buf, amount, flags, addr, timeout); + fd->lower, buf, amount, flags, addr, timeout); } static PRInt32 PR_CALLBACK pl_DefSendto ( @@ -278,7 +283,7 @@ static PRInt32 PR_CALLBACK pl_DefSendto ( PR_ASSERT(fd->lower != NULL); return (fd->lower->methods->sendto)( - fd->lower, buf, amount, flags, addr, timeout); + fd->lower, buf, amount, flags, addr, timeout); } static PRInt16 PR_CALLBACK pl_DefPoll ( @@ -298,15 +303,16 @@ static PRInt32 PR_CALLBACK pl_DefAcceptread ( PRStatus rv; PRFileDesc *newstack; PRFileDesc *layer = sd; - PRBool newstyle_stack = PR_FALSE; + PRBool newstyle_stack = PR_FALSE; PR_ASSERT(sd != NULL); PR_ASSERT(sd->lower != NULL); - /* test for new style stack */ - while (NULL != layer->higher) - layer = layer->higher; - newstyle_stack = (PR_IO_LAYER_HEAD == layer->identity) ? PR_TRUE : PR_FALSE; + /* test for new style stack */ + while (NULL != layer->higher) { + layer = layer->higher; + } + newstyle_stack = (PR_IO_LAYER_HEAD == layer->identity) ? PR_TRUE : PR_FALSE; newstack = PR_NEW(PRFileDesc); if (NULL == newstack) { @@ -316,18 +322,18 @@ static PRInt32 PR_CALLBACK pl_DefAcceptread ( *newstack = *sd; /* make a copy of the accepting layer */ nbytes = sd->lower->methods->acceptread( - sd->lower, nd, raddr, buf, amount, t); + sd->lower, nd, raddr, buf, amount, t); if (-1 == nbytes) { PR_DELETE(newstack); return nbytes; } if (newstyle_stack) { - newstack->lower = *nd; - (*nd)->higher = newstack; - *nd = newstack; - return nbytes; - } + newstack->lower = *nd; + (*nd)->higher = newstack; + *nd = newstack; + return nbytes; + } /* this PR_PushIOLayer call cannot fail */ rv = PR_PushIOLayer(*nd, PR_TOP_IO_LAYER, newstack); PR_ASSERT(PR_SUCCESS == rv); @@ -342,7 +348,7 @@ static PRInt32 PR_CALLBACK pl_DefTransmitfile ( PR_ASSERT(sd->lower != NULL); return sd->lower->methods->transmitfile( - sd->lower, fd, headers, hlen, flags, t); + sd->lower, fd, headers, hlen, flags, t); } static PRStatus PR_CALLBACK pl_DefGetsockname (PRFileDesc *fd, PRNetAddr *addr) @@ -380,14 +386,14 @@ static PRStatus PR_CALLBACK pl_DefSetsocketoption ( } static PRInt32 PR_CALLBACK pl_DefSendfile ( - PRFileDesc *sd, PRSendFileData *sfd, - PRTransmitFileFlags flags, PRIntervalTime timeout) + PRFileDesc *sd, PRSendFileData *sfd, + PRTransmitFileFlags flags, PRIntervalTime timeout) { PR_ASSERT(sd != NULL); PR_ASSERT(sd->lower != NULL); return sd->lower->methods->sendfile( - sd->lower, sfd, flags, timeout); + sd->lower, sfd, flags, timeout); } /* Methods for the top of the stack. Just call down to the next fd. */ @@ -440,13 +446,15 @@ PR_IMPLEMENT(PRFileDesc*) PR_CreateIOLayerStub( { PRFileDesc *fd = NULL; PR_ASSERT((PR_NSPR_IO_LAYER != ident) && (PR_TOP_IO_LAYER != ident)); - if ((PR_NSPR_IO_LAYER == ident) || (PR_TOP_IO_LAYER == ident)) + if ((PR_NSPR_IO_LAYER == ident) || (PR_TOP_IO_LAYER == ident)) { PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0); + } else { fd = PR_NEWZAP(PRFileDesc); - if (NULL == fd) + if (NULL == fd) { PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); + } else { fd->methods = methods; @@ -459,41 +467,43 @@ PR_IMPLEMENT(PRFileDesc*) PR_CreateIOLayerStub( /* * PR_CreateIOLayer - * Create a new style stack, where the stack top is a dummy header. - * Unlike the old style stacks, the contents of the stack head - * are not modified when a layer is pushed onto or popped from a new - * style stack. + * Create a new style stack, where the stack top is a dummy header. + * Unlike the old style stacks, the contents of the stack head + * are not modified when a layer is pushed onto or popped from a new + * style stack. */ PR_IMPLEMENT(PRFileDesc*) PR_CreateIOLayer(PRFileDesc *top) { PRFileDesc *fd = NULL; - fd = PR_NEWZAP(PRFileDesc); - if (NULL == fd) - PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); - else - { - fd->methods = &pl_methods; - fd->dtor = pl_FDDestructor; - fd->identity = PR_IO_LAYER_HEAD; - fd->higher = NULL; - fd->lower = top; - top->higher = fd; - top->lower = NULL; - } + fd = PR_NEWZAP(PRFileDesc); + if (NULL == fd) { + PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); + } + else + { + fd->methods = &pl_methods; + fd->dtor = pl_FDDestructor; + fd->identity = PR_IO_LAYER_HEAD; + fd->higher = NULL; + fd->lower = top; + top->higher = fd; + top->lower = NULL; + } return fd; } /* PR_CreateIOLayer */ /* * _PR_DestroyIOLayer - * Delete the stack head of a new style stack. + * Delete the stack head of a new style stack. */ static PRStatus _PR_DestroyIOLayer(PRFileDesc *stack) { - if (NULL == stack) + if (NULL == stack) { return PR_FAILURE; + } PR_DELETE(stack); return PR_SUCCESS; @@ -516,24 +526,24 @@ PR_IMPLEMENT(PRStatus) PR_PushIOLayer( if (stack == insert) { - /* going on top of the stack */ - /* old-style stack */ - PRFileDesc copy = *stack; - *stack = *fd; - *fd = copy; - fd->higher = stack; - if (fd->lower) - { - PR_ASSERT(fd->lower->higher == stack); - fd->lower->higher = fd; - } - stack->lower = fd; - stack->higher = NULL; - } else { + /* going on top of the stack */ + /* old-style stack */ + PRFileDesc copy = *stack; + *stack = *fd; + *fd = copy; + fd->higher = stack; + if (fd->lower) + { + PR_ASSERT(fd->lower->higher == stack); + fd->lower->higher = fd; + } + stack->lower = fd; + stack->higher = NULL; + } else { /* - * going somewhere in the middle of the stack for both old and new - * style stacks, or going on top of stack for new style stack - */ + * going somewhere in the middle of the stack for both old and new + * style stacks, or going on top of stack for new style stack + */ fd->lower = insert; fd->higher = insert->higher; @@ -559,7 +569,7 @@ PR_IMPLEMENT(PRFileDesc*) PR_PopIOLayer(PRFileDesc *stack, PRDescIdentity id) if (extract == stack) { /* popping top layer of the stack */ - /* old style stack */ + /* old style stack */ PRFileDesc copy = *stack; extract = stack->lower; *stack = *extract; @@ -569,16 +579,16 @@ PR_IMPLEMENT(PRFileDesc*) PR_PopIOLayer(PRFileDesc *stack, PRDescIdentity id) PR_ASSERT(stack->lower->higher == extract); stack->lower->higher = stack; } - } else if ((PR_IO_LAYER_HEAD == stack->identity) && - (extract == stack->lower) && (extract->lower == NULL)) { - /* - * new style stack - * popping the only layer in the stack; delete the stack too - */ - stack->lower = NULL; - _PR_DestroyIOLayer(stack); - } else { - /* for both kinds of stacks */ + } else if ((PR_IO_LAYER_HEAD == stack->identity) && + (extract == stack->lower) && (extract->lower == NULL)) { + /* + * new style stack + * popping the only layer in the stack; delete the stack too + */ + stack->lower = NULL; + _PR_DestroyIOLayer(stack); + } else { + /* for both kinds of stacks */ extract->lower->higher = extract->higher; extract->higher->lower = extract->lower; } @@ -602,7 +612,9 @@ PR_IMPLEMENT(PRDescIdentity) PR_GetUniqueIdentity(const char *layer_name) PRDescIdentity identity, length; char **names = NULL, *name = NULL, **old = NULL; - if (!_pr_initialized) _PR_ImplicitInitialization(); + if (!_pr_initialized) { + _PR_ImplicitInitialization(); + } PR_ASSERT((PRDescIdentity)0x7fff > identity_cache.ident); @@ -634,7 +646,9 @@ retry: names = (char**)PR_CALLOC(length * sizeof(char*)); if (NULL == names) { - if (NULL != name) PR_DELETE(name); + if (NULL != name) { + PR_DELETE(name); + } PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); return PR_INVALID_IO_LAYER; } @@ -664,7 +678,9 @@ retry: else { PR_Unlock(identity_cache.ml); - if (NULL != names) PR_DELETE(names); + if (NULL != names) { + PR_DELETE(names); + } goto retry; } } @@ -676,8 +692,12 @@ retry: PR_ASSERT(identity_cache.ident < identity_cache.length); PR_Unlock(identity_cache.ml); - if (NULL != old) PR_DELETE(old); - if (NULL != names) PR_DELETE(names); + if (NULL != old) { + PR_DELETE(old); + } + if (NULL != names) { + PR_DELETE(names); + } return identity; } /* PR_GetUniqueIdentity */ @@ -685,13 +705,15 @@ retry: PR_IMPLEMENT(const char*) PR_GetNameForIdentity(PRDescIdentity ident) { const char *rv = NULL; - if (!_pr_initialized) _PR_ImplicitInitialization(); + if (!_pr_initialized) { + _PR_ImplicitInitialization(); + } if ((PR_TOP_IO_LAYER != ident) && (ident >= 0)) { - PR_Lock(identity_cache.ml); - PR_ASSERT(ident <= identity_cache.ident); - rv = (ident > identity_cache.ident) ? NULL : identity_cache.name[ident]; - PR_Unlock(identity_cache.ml); + PR_Lock(identity_cache.ml); + PR_ASSERT(ident <= identity_cache.ident); + rv = (ident > identity_cache.ident) ? NULL : identity_cache.name[ident]; + PR_Unlock(identity_cache.ml); } return rv; @@ -701,9 +723,9 @@ PR_IMPLEMENT(PRDescIdentity) PR_GetLayersIdentity(PRFileDesc* fd) { PR_ASSERT(NULL != fd); if (PR_IO_LAYER_HEAD == fd->identity) { - PR_ASSERT(NULL != fd->lower); - return fd->lower->identity; - } + PR_ASSERT(NULL != fd->lower); + return fd->lower->identity; + } return fd->identity; } /* PR_GetLayersIdentity */ @@ -712,19 +734,23 @@ PR_IMPLEMENT(PRFileDesc*) PR_GetIdentitiesLayer(PRFileDesc* fd, PRDescIdentity i PRFileDesc *layer = fd; if (PR_TOP_IO_LAYER == id) { - if (PR_IO_LAYER_HEAD == fd->identity) { - return fd->lower; - } - return fd; - } + if (PR_IO_LAYER_HEAD == fd->identity) { + return fd->lower; + } + return fd; + } for (layer = fd; layer != NULL; layer = layer->lower) { - if (id == layer->identity) return layer; + if (id == layer->identity) { + return layer; + } } for (layer = fd; layer != NULL; layer = layer->higher) { - if (id == layer->identity) return layer; + if (id == layer->identity) { + return layer; + } } return NULL; } /* PR_GetIdentitiesLayer */ @@ -748,8 +774,9 @@ void _PR_CleanupLayerCache(void) { PRDescIdentity ident; - for (ident = 0; ident <= identity_cache.ident; ident++) + for (ident = 0; ident <= identity_cache.ident; ident++) { PR_DELETE(identity_cache.name[ident]); + } PR_DELETE(identity_cache.name); } diff --git a/nsprpub/pr/src/io/prlog.c b/nsprpub/pr/src/io/prlog.c index 6098460ede..52bd6abc5d 100644 --- a/nsprpub/pr/src/io/prlog.c +++ b/nsprpub/pr/src/io/prlog.c @@ -166,8 +166,9 @@ PRIntn strcasecmp(const char *a, const char *b) const unsigned char *ua = (const unsigned char *)a; const unsigned char *ub = (const unsigned char *)b; - if( ((const char *)0 == a) || (const char *)0 == b ) + if( ((const char *)0 == a) || (const char *)0 == b ) { return (PRIntn)(a-b); + } while( (uc[*ua] == uc[*ub]) && ('\0' != *a) ) { @@ -201,7 +202,9 @@ void _PR_InitLog(void) count = sscanf(&ev[pos], "%63[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-]%n:%d%n", module, &delta, &level, &delta); pos += delta; - if (count == 0) break; + if (count == 0) { + break; + } /* ** If count == 2, then we got module and level. If count @@ -223,7 +226,9 @@ void _PR_InitLog(void) (0 == strcasecmp (module, "all")) ? PR_TRUE : PR_FALSE; while (lm != NULL) { - if (skip_modcheck) lm -> level = (PRLogModuleLevel)level; + if (skip_modcheck) { + lm -> level = (PRLogModuleLevel)level; + } else if (strcasecmp(module, lm->name) == 0) { lm->level = (PRLogModuleLevel)level; break; @@ -234,7 +239,9 @@ void _PR_InitLog(void) /*found:*/ count = sscanf(&ev[pos], " , %n", &delta); pos += delta; - if (count == EOF) break; + if (count == EOF) { + break; + } } PR_SetLogBuffering(isSync ? 0 : bufSize); @@ -274,7 +281,7 @@ void _PR_LogCleanup(void) #ifdef XP_PC && logFile != WIN32_DEBUG_FILE #endif - ) { + ) { fclose(logFile); } #else @@ -284,8 +291,9 @@ void _PR_LogCleanup(void) #endif logFile = NULL; - if (logBuf) + if (logBuf) { PR_DELETE(logBuf); + } while (lm != NULL) { PRLogModuleInfo *next = lm->next; @@ -318,7 +326,9 @@ static void _PR_SetLogModuleLevel( PRLogModuleInfo *lm ) count = sscanf(&ev[pos], "%63[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-]%n:%d%n", module, &delta, &level, &delta); pos += delta; - if (count == 0) break; + if (count == 0) { + break; + } /* ** If count == 2, then we got module and level. If count @@ -334,7 +344,9 @@ static void _PR_SetLogModuleLevel( PRLogModuleInfo *lm ) } count = sscanf(&ev[pos], " , %n", &delta); pos += delta; - if (count == EOF) break; + if (count == EOF) { + break; + } } } } /* end _PR_SetLogModuleLevel() */ @@ -343,7 +355,9 @@ PR_IMPLEMENT(PRLogModuleInfo*) PR_NewLogModule(const char *name) { PRLogModuleInfo *lm; - if (!_pr_initialized) _PR_ImplicitInitialization(); + if (!_pr_initialized) { + _PR_ImplicitInitialization(); + } lm = PR_NEWZAP(PRLogModuleInfo); if (lm) { @@ -371,8 +385,9 @@ PR_IMPLEMENT(PRBool) PR_SetLogFile(const char *file) { const char *mode = appendToLog ? "a" : "w"; newLogFile = fopen(file, mode); - if (!newLogFile) + if (!newLogFile) { return PR_FALSE; + } #ifndef WINCE /* _IONBF does not exist in the Windows Mobile 6 SDK. */ /* We do buffering ourselves. */ @@ -385,7 +400,7 @@ PR_IMPLEMENT(PRBool) PR_SetLogFile(const char *file) #ifdef XP_PC && logFile != WIN32_DEBUG_FILE #endif - ) { + ) { fclose(logFile); } logFile = newLogFile; @@ -414,8 +429,9 @@ PR_IMPLEMENT(void) PR_SetLogBuffering(PRIntn buffer_size) { PR_LogFlush(); - if (logBuf) + if (logBuf) { PR_DELETE(logBuf); + } if (buffer_size >= LINE_BUF_SIZE) { logp = logBuf = (char*) PR_MALLOC(buffer_size); @@ -432,7 +448,9 @@ PR_IMPLEMENT(void) PR_LogPrint(const char *fmt, ...) PRThread *me; PRExplodedTime now; - if (!_pr_initialized) _PR_ImplicitInitialization(); + if (!_pr_initialized) { + _PR_ImplicitInitialization(); + } if (!logFile) { return; @@ -520,10 +538,10 @@ PR_IMPLEMENT(void) PR_LogFlush(void) { if (logBuf && logFile) { _PR_LOCK_LOG(); - if (logp > logBuf) { - _PUT_LOG(logFile, logBuf, logp - logBuf); - logp = logBuf; - } + if (logp > logBuf) { + _PUT_LOG(logFile, logBuf, logp - logBuf); + logp = logBuf; + } _PR_UNLOCK_LOG(); } } diff --git a/nsprpub/pr/src/io/prmapopt.c b/nsprpub/pr/src/io/prmapopt.c index f92a76b2bd..b6653e4bab 100644 --- a/nsprpub/pr/src/io/prmapopt.c +++ b/nsprpub/pr/src/io/prmapopt.c @@ -62,11 +62,10 @@ PRStatus PR_CALLBACK _PR_SocketGetSocketOption(PRFileDesc *fd, PRSocketOptionDat { case PR_SockOpt_Linger: { -#if !defined(XP_BEOS) || defined(BONE_VERSION) struct linger linger; length = sizeof(linger); rv = _PR_MD_GETSOCKOPT( - fd, level, name, (char *) &linger, &length); + fd, level, name, (char *) &linger, &length); if (PR_SUCCESS == rv) { PR_ASSERT(sizeof(linger) == length); @@ -76,10 +75,6 @@ PRStatus PR_CALLBACK _PR_SocketGetSocketOption(PRFileDesc *fd, PRSocketOptionDat PR_SecondsToInterval(linger.l_linger); } break; -#else - PR_SetError( PR_NOT_IMPLEMENTED_ERROR, 0 ); - return PR_FAILURE; -#endif } case PR_SockOpt_Reuseaddr: case PR_SockOpt_Keepalive: @@ -94,9 +89,10 @@ PRStatus PR_CALLBACK _PR_SocketGetSocketOption(PRFileDesc *fd, PRSocketOptionDat #endif length = sizeof(value); rv = _PR_MD_GETSOCKOPT( - fd, level, name, (char*)&value, &length); - if (PR_SUCCESS == rv) + fd, level, name, (char*)&value, &length); + if (PR_SUCCESS == rv) { data->value.reuse_addr = (0 == value) ? PR_FALSE : PR_TRUE; + } break; } case PR_SockOpt_McastLoopback: @@ -108,9 +104,10 @@ PRStatus PR_CALLBACK _PR_SocketGetSocketOption(PRFileDesc *fd, PRSocketOptionDat #endif length = sizeof(bool); rv = _PR_MD_GETSOCKOPT( - fd, level, name, (char*)&bool, &length); - if (PR_SUCCESS == rv) + fd, level, name, (char*)&bool, &length); + if (PR_SUCCESS == rv) { data->value.mcast_loopback = (0 == bool) ? PR_FALSE : PR_TRUE; + } break; } case PR_SockOpt_RecvBufferSize: @@ -120,9 +117,10 @@ PRStatus PR_CALLBACK _PR_SocketGetSocketOption(PRFileDesc *fd, PRSocketOptionDat PRIntn value; length = sizeof(value); rv = _PR_MD_GETSOCKOPT( - fd, level, name, (char*)&value, &length); - if (PR_SUCCESS == rv) + fd, level, name, (char*)&value, &length); + if (PR_SUCCESS == rv) { data->value.recv_buffer_size = value; + } break; } case PR_SockOpt_IpTimeToLive: @@ -131,7 +129,7 @@ PRStatus PR_CALLBACK _PR_SocketGetSocketOption(PRFileDesc *fd, PRSocketOptionDat /* These options should really be an int (or PRIntn). */ length = sizeof(PRUintn); rv = _PR_MD_GETSOCKOPT( - fd, level, name, (char*)&data->value.ip_ttl, &length); + fd, level, name, (char*)&data->value.ip_ttl, &length); break; } case PR_SockOpt_McastTimeToLive: @@ -143,9 +141,10 @@ PRStatus PR_CALLBACK _PR_SocketGetSocketOption(PRFileDesc *fd, PRSocketOptionDat #endif length = sizeof(ttl); rv = _PR_MD_GETSOCKOPT( - fd, level, name, (char*)&ttl, &length); - if (PR_SUCCESS == rv) + fd, level, name, (char*)&ttl, &length); + if (PR_SUCCESS == rv) { data->value.mcast_ttl = ttl; + } break; } #ifdef IP_ADD_MEMBERSHIP @@ -155,7 +154,7 @@ PRStatus PR_CALLBACK _PR_SocketGetSocketOption(PRFileDesc *fd, PRSocketOptionDat struct ip_mreq mreq; length = sizeof(mreq); rv = _PR_MD_GETSOCKOPT( - fd, level, name, (char*)&mreq, &length); + fd, level, name, (char*)&mreq, &length); if (PR_SUCCESS == rv) { data->value.add_member.mcaddr.inet.ip = @@ -171,14 +170,14 @@ PRStatus PR_CALLBACK _PR_SocketGetSocketOption(PRFileDesc *fd, PRSocketOptionDat /* This option is a struct in_addr. */ length = sizeof(data->value.mcast_if.inet.ip); rv = _PR_MD_GETSOCKOPT( - fd, level, name, - (char*)&data->value.mcast_if.inet.ip, &length); + fd, level, name, + (char*)&data->value.mcast_if.inet.ip, &length); break; } default: PR_NOT_REACHED("Unknown socket option"); break; - } + } } return rv; } /* _PR_SocketGetSocketOption */ @@ -196,7 +195,7 @@ PRStatus PR_CALLBACK _PR_SocketSetSocketOption(PRFileDesc *fd, const PRSocketOpt { #ifdef WINNT PR_ASSERT((fd->secret->md.io_model_committed == PR_FALSE) - || (fd->secret->nonblocking == data->value.non_blocking)); + || (fd->secret->nonblocking == data->value.non_blocking)); if (fd->secret->md.io_model_committed && (fd->secret->nonblocking != data->value.non_blocking)) { @@ -221,17 +220,12 @@ PRStatus PR_CALLBACK _PR_SocketSetSocketOption(PRFileDesc *fd, const PRSocketOpt { case PR_SockOpt_Linger: { -#if !defined(XP_BEOS) || defined(BONE_VERSION) struct linger linger; linger.l_onoff = data->value.linger.polarity; linger.l_linger = PR_IntervalToSeconds(data->value.linger.linger); rv = _PR_MD_SETSOCKOPT( - fd, level, name, (char*)&linger, sizeof(linger)); + fd, level, name, (char*)&linger, sizeof(linger)); break; -#else - PR_SetError( PR_NOT_IMPLEMENTED_ERROR, 0 ); - return PR_FAILURE; -#endif } case PR_SockOpt_Reuseaddr: case PR_SockOpt_Keepalive: @@ -246,7 +240,7 @@ PRStatus PR_CALLBACK _PR_SocketSetSocketOption(PRFileDesc *fd, const PRSocketOpt #endif value = (data->value.reuse_addr) ? 1 : 0; rv = _PR_MD_SETSOCKOPT( - fd, level, name, (char*)&value, sizeof(value)); + fd, level, name, (char*)&value, sizeof(value)); break; } case PR_SockOpt_McastLoopback: @@ -258,7 +252,7 @@ PRStatus PR_CALLBACK _PR_SocketSetSocketOption(PRFileDesc *fd, const PRSocketOpt #endif bool = data->value.mcast_loopback ? 1 : 0; rv = _PR_MD_SETSOCKOPT( - fd, level, name, (char*)&bool, sizeof(bool)); + fd, level, name, (char*)&bool, sizeof(bool)); break; } case PR_SockOpt_RecvBufferSize: @@ -267,7 +261,7 @@ PRStatus PR_CALLBACK _PR_SocketSetSocketOption(PRFileDesc *fd, const PRSocketOpt { PRIntn value = data->value.recv_buffer_size; rv = _PR_MD_SETSOCKOPT( - fd, level, name, (char*)&value, sizeof(value)); + fd, level, name, (char*)&value, sizeof(value)); break; } case PR_SockOpt_IpTimeToLive: @@ -275,7 +269,7 @@ PRStatus PR_CALLBACK _PR_SocketSetSocketOption(PRFileDesc *fd, const PRSocketOpt { /* These options should really be an int (or PRIntn). */ rv = _PR_MD_SETSOCKOPT( - fd, level, name, (char*)&data->value.ip_ttl, sizeof(PRUintn)); + fd, level, name, (char*)&data->value.ip_ttl, sizeof(PRUintn)); break; } case PR_SockOpt_McastTimeToLive: @@ -287,7 +281,7 @@ PRStatus PR_CALLBACK _PR_SocketSetSocketOption(PRFileDesc *fd, const PRSocketOpt #endif ttl = data->value.mcast_ttl; rv = _PR_MD_SETSOCKOPT( - fd, level, name, (char*)&ttl, sizeof(ttl)); + fd, level, name, (char*)&ttl, sizeof(ttl)); break; } #ifdef IP_ADD_MEMBERSHIP @@ -300,7 +294,7 @@ PRStatus PR_CALLBACK _PR_SocketSetSocketOption(PRFileDesc *fd, const PRSocketOpt mreq.imr_interface.s_addr = data->value.add_member.ifaddr.inet.ip; rv = _PR_MD_SETSOCKOPT( - fd, level, name, (char*)&mreq, sizeof(mreq)); + fd, level, name, (char*)&mreq, sizeof(mreq)); break; } #endif /* IP_ADD_MEMBERSHIP */ @@ -308,14 +302,14 @@ PRStatus PR_CALLBACK _PR_SocketSetSocketOption(PRFileDesc *fd, const PRSocketOpt { /* This option is a struct in_addr. */ rv = _PR_MD_SETSOCKOPT( - fd, level, name, (char*)&data->value.mcast_if.inet.ip, - sizeof(data->value.mcast_if.inet.ip)); + fd, level, name, (char*)&data->value.mcast_if.inet.ip, + sizeof(data->value.mcast_if.inet.ip)); break; } default: PR_NOT_REACHED("Unknown socket option"); break; - } + } } return rv; } /* _PR_SocketSetSocketOption */ @@ -441,7 +435,7 @@ PRStatus _PR_MapOptionName( }; if ((optname < PR_SockOpt_Linger) - || (optname >= PR_SockOpt_Last)) + || (optname >= PR_SockOpt_Last)) { PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0); return PR_FAILURE; diff --git a/nsprpub/pr/src/io/prmmap.c b/nsprpub/pr/src/io/prmmap.c index 64f7ed4e83..b0adf88676 100644 --- a/nsprpub/pr/src/io/prmmap.c +++ b/nsprpub/pr/src/io/prmmap.c @@ -21,19 +21,19 @@ PR_IMPLEMENT(PRFileMap *) PR_CreateFileMap( PRFileMap *fmap; PR_ASSERT(prot == PR_PROT_READONLY || prot == PR_PROT_READWRITE - || prot == PR_PROT_WRITECOPY); + || prot == PR_PROT_WRITECOPY); fmap = PR_NEWZAP(PRFileMap); if (NULL == fmap) { - PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); - return NULL; + PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); + return NULL; } fmap->fd = fd; fmap->prot = prot; if (_PR_MD_CREATE_FILE_MAP(fmap, size) == PR_SUCCESS) { - return fmap; + return fmap; } - PR_DELETE(fmap); - return NULL; + PR_DELETE(fmap); + return NULL; } PR_IMPLEMENT(PRInt32) PR_GetMemMapAlignment(void) @@ -64,5 +64,5 @@ PR_IMPLEMENT(PRStatus) PR_SyncMemMap( void *addr, PRUint32 len) { - return _PR_MD_SYNC_MEM_MAP(fd, addr, len); + return _PR_MD_SYNC_MEM_MAP(fd, addr, len); } diff --git a/nsprpub/pr/src/io/prmwait.c b/nsprpub/pr/src/io/prmwait.c index ab32fb5d8c..62e35ab6fb 100644 --- a/nsprpub/pr/src/io/prmwait.c +++ b/nsprpub/pr/src/io/prmwait.c @@ -37,7 +37,7 @@ struct { static PRStatus TimerInit(void); static void TimerManager(void *arg); static TimerEvent *CreateTimer(PRIntervalTime timeout, - void (*func)(void *), void *arg); + void (*func)(void *), void *arg); static PRBool CancelTimer(TimerEvent *timer); static void TimerManager(void *arg) @@ -81,7 +81,7 @@ static void TimerManager(void *arg) { timeout = (PRIntervalTime)(timer->absolute - now); PR_WaitCondVar(tm_vars.new_timer, timeout); - } + } } } PR_Unlock(tm_vars.ml); @@ -143,7 +143,7 @@ static PRBool CancelTimer(TimerEvent *timer) } PR_Unlock(tm_vars.ml); PR_DELETE(timer); - return canceled; + return canceled; } static PRStatus TimerInit(void) @@ -165,8 +165,8 @@ static PRStatus TimerInit(void) } PR_INIT_CLIST(&tm_vars.timer_queue); tm_vars.manager_thread = PR_CreateThread( - PR_SYSTEM_THREAD, TimerManager, NULL, PR_PRIORITY_NORMAL, - PR_LOCAL_THREAD, PR_UNJOINABLE_THREAD, 0); + PR_SYSTEM_THREAD, TimerManager, NULL, PR_PRIORITY_NORMAL, + PR_LOCAL_THREAD, PR_UNJOINABLE_THREAD, 0); if (NULL == tm_vars.manager_thread) { goto failed; @@ -231,7 +231,9 @@ static PRWaitGroup *MW_Init2(void) if (NULL == group) /* there is this special case */ { group = PR_CreateWaitGroup(_PR_DEFAULT_HASH_LENGTH); - if (NULL == group) goto failed_alloc; + if (NULL == group) { + goto failed_alloc; + } PR_Lock(mw_lock); if (NULL == mw_state->group) { @@ -239,7 +241,9 @@ static PRWaitGroup *MW_Init2(void) group = NULL; } PR_Unlock(mw_lock); - if (group != NULL) (void)PR_DestroyWaitGroup(group); + if (group != NULL) { + (void)PR_DestroyWaitGroup(group); + } group = mw_state->group; /* somebody beat us to it */ } failed_alloc: @@ -301,7 +305,7 @@ static _PR_HashStory MW_AddHashInternal(PRRecvWait *desc, _PRWaiterHash *hash) } hidx = (hidx + hoffset) % (hash->length); } - return _prmw_rehash; + return _prmw_rehash; } /* MW_AddHashInternal */ static _PR_HashStory MW_ExpandHashInternal(PRWaitGroup *group) @@ -314,7 +318,8 @@ static _PR_HashStory MW_ExpandHashInternal(PRWaitGroup *group) static const PRInt32 prime_number[] = { _PR_DEFAULT_HASH_LENGTH, 179, 521, 907, 1427, - 2711, 3917, 5021, 8219, 11549, 18911, 26711, 33749, 44771}; + 2711, 3917, 5021, 8219, 11549, 18911, 26711, 33749, 44771 + }; PRUintn primes = (sizeof(prime_number) / sizeof(PRInt32)); /* look up the next size we'd like to use for the hash table */ @@ -337,7 +342,7 @@ static _PR_HashStory MW_ExpandHashInternal(PRWaitGroup *group) /* allocate the new hash table and fill it in with the old */ newHash = (_PRWaiterHash*)PR_CALLOC( - sizeof(_PRWaiterHash) + (length * sizeof(PRRecvWait*))); + sizeof(_PRWaiterHash) + (length * sizeof(PRRecvWait*))); if (NULL == newHash) { PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); @@ -347,7 +352,7 @@ static _PR_HashStory MW_ExpandHashInternal(PRWaitGroup *group) newHash->length = length; retry = PR_FALSE; for (desc = &oldHash->recv_wait; - newHash->count < oldHash->count; ++desc) + newHash->count < oldHash->count; ++desc) { PR_ASSERT(desc < &oldHash->recv_wait + oldHash->length); if (NULL != *desc) @@ -362,7 +367,9 @@ static _PR_HashStory MW_ExpandHashInternal(PRWaitGroup *group) } } } - if (retry) continue; + if (retry) { + continue; + } PR_DELETE(group->waiter); group->waiter = newHash; @@ -408,11 +415,13 @@ static PRRecvWait **_MW_LookupInternal(PRWaitGroup *group, PRFileDesc *fd) _PRWaiterHash *hash = group->waiter; PRUintn hidx = _MW_HASH(fd, hash->length); PRUintn hoffset = 0; - + while (rehash-- > 0) { desc = (&hash->recv_wait) + hidx; - if ((*desc != NULL) && ((*desc)->fd == fd)) return desc; + if ((*desc != NULL) && ((*desc)->fd == fd)) { + return desc; + } if (0 == hoffset) { hoffset = _MW_HASH2(fd, hash->length); @@ -447,7 +456,9 @@ static PRStatus _MW_PollInternal(PRWaitGroup *group) PR_SetError(PR_INVALID_STATE_ERROR, 0); goto aborted; } - if (_MW_ABORTED(st)) goto aborted; + if (_MW_ABORTED(st)) { + goto aborted; + } } /* @@ -470,8 +481,9 @@ static PRStatus _MW_PollInternal(PRWaitGroup *group) PR_Lock(group->ml); goto failed_alloc; } - if (NULL != old_polling_list) + if (NULL != old_polling_list) { PR_DELETE(old_polling_list); + } PR_Lock(group->ml); if (_prmw_running != group->state) { @@ -492,22 +504,24 @@ static PRStatus _MW_PollInternal(PRWaitGroup *group) for (count = 0; count < group->waiter->count; ++waiter) { PR_ASSERT(waiter < &group->waiter->recv_wait - + group->waiter->length); + + group->waiter->length); if (NULL != *waiter) /* a live one! */ { if ((PR_INTERVAL_NO_TIMEOUT != (*waiter)->timeout) - && (since_last_poll >= (*waiter)->timeout)) + && (since_last_poll >= (*waiter)->timeout)) { _MW_DoneInternal(group, waiter, PR_MW_TIMEOUT); + } else { if (PR_INTERVAL_NO_TIMEOUT != (*waiter)->timeout) { (*waiter)->timeout -= since_last_poll; - if ((*waiter)->timeout < polling_interval) + if ((*waiter)->timeout < polling_interval) { polling_interval = (*waiter)->timeout; + } } PR_ASSERT(poll_list < group->polling_list - + group->polling_count); + + group->polling_count); poll_list->fd = (*waiter)->fd; poll_list->in_flags = PR_POLL_READ; poll_list->out_flags = 0; @@ -520,7 +534,7 @@ static PRStatus _MW_PollInternal(PRWaitGroup *group) count += 1; } } - } + } PR_ASSERT(count == group->waiter->count); @@ -529,9 +543,13 @@ static PRStatus _MW_PollInternal(PRWaitGroup *group) ** we need to return. */ if ((!PR_CLIST_IS_EMPTY(&group->io_ready)) - && (1 == group->waiting_threads)) break; + && (1 == group->waiting_threads)) { + break; + } - if (0 == count) continue; /* wait for new business */ + if (0 == count) { + continue; /* wait for new business */ + } group->last_poll = now; @@ -553,7 +571,7 @@ static PRStatus _MW_PollInternal(PRWaitGroup *group) else if (0 < count_ready) { for (poll_list = group->polling_list; count > 0; - poll_list++, count--) + poll_list++, count--) { PR_ASSERT( poll_list < group->polling_list + group->polling_count); @@ -564,8 +582,9 @@ static PRStatus _MW_PollInternal(PRWaitGroup *group) ** If 'waiter' is NULL, that means the wait receive ** descriptor has been canceled. */ - if (NULL != waiter) + if (NULL != waiter) { _MW_DoneInternal(group, waiter, PR_MW_SUCCESS); + } } } } @@ -576,7 +595,9 @@ static PRStatus _MW_PollInternal(PRWaitGroup *group) ** belongs to the client. */ if ((!PR_CLIST_IS_EMPTY(&group->io_ready)) - && (1 == group->waiting_threads)) break; + && (1 == group->waiting_threads)) { + break; + } } rv = PR_SUCCESS; @@ -604,7 +625,7 @@ static PRMWGroupState MW_TestForShutdownInternal(PRWaitGroup *group) ** to make sure no more threads are made to wait. */ if ((_prmw_stopping == rv) - && (0 == group->waiting_threads)) + && (0 == group->waiting_threads)) { rv = group->state = _prmw_stopped; PR_NotifyCondVar(group->mw_manage); @@ -617,15 +638,17 @@ static void _MW_InitialRecv(PRCList *io_ready) { PRRecvWait *desc = (PRRecvWait*)io_ready; if ((NULL == desc->buffer.start) - || (0 == desc->buffer.length)) + || (0 == desc->buffer.length)) { desc->bytesRecv = 0; + } else { desc->bytesRecv = (desc->fd->methods->recv)( - desc->fd, desc->buffer.start, - desc->buffer.length, 0, desc->timeout); - if (desc->bytesRecv < 0) /* SetError should already be there */ + desc->fd, desc->buffer.start, + desc->buffer.length, 0, desc->timeout); + if (desc->bytesRecv < 0) { /* SetError should already be there */ desc->outcome = PR_MW_FAILURE; + } } } /* _MW_InitialRecv */ #endif @@ -636,9 +659,9 @@ static void NT_TimeProc(void *arg) _MDOverlapped *overlapped = (_MDOverlapped *)arg; PRRecvWait *desc = overlapped->data.mw.desc; PRFileDesc *bottom; - + if (InterlockedCompareExchange((LONG *)&desc->outcome, - (LONG)PR_MW_TIMEOUT, (LONG)PR_MW_PENDING) != (LONG)PR_MW_PENDING) + (LONG)PR_MW_TIMEOUT, (LONG)PR_MW_PENDING) != (LONG)PR_MW_PENDING) { /* This wait recv descriptor has already completed. */ return; @@ -712,7 +735,9 @@ PR_IMPLEMENT(PRStatus) PR_AddWaitFileDesc( PRFileDesc *bottom; #endif - if (!_pr_initialized) _PR_ImplicitInitialization(); + if (!_pr_initialized) { + _PR_ImplicitInitialization(); + } if ((NULL == group) && (NULL == (group = MW_Init2()))) { return rv; @@ -744,15 +769,20 @@ PR_IMPLEMENT(PRStatus) PR_AddWaitFileDesc( ** of the timing interval. As long as the list doesn't go empty, ** it will maintain itself. */ - if (0 == group->waiter->count) + if (0 == group->waiter->count) { group->last_poll = PR_IntervalNow(); + } do { hrv = MW_AddHashInternal(desc, group->waiter); - if (_prmw_rehash != hrv) break; + if (_prmw_rehash != hrv) { + break; + } hrv = MW_ExpandHashInternal(group); /* gruesome */ - if (_prmw_success != hrv) break; + if (_prmw_success != hrv) { + break; + } } while (PR_TRUE); #ifdef WINNT @@ -777,9 +807,9 @@ PR_IMPLEMENT(PRStatus) PR_AddWaitFileDesc( if (desc->timeout != PR_INTERVAL_NO_TIMEOUT) { overlapped->data.mw.timer = CreateTimer( - desc->timeout, - NT_TimeProc, - overlapped); + desc->timeout, + NT_TimeProc, + overlapped); if (0 == overlapped->data.mw.timer) { NT_HashRemove(group, desc->fd); @@ -801,7 +831,7 @@ PR_IMPLEMENT(PRStatus) PR_AddWaitFileDesc( PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0); return PR_FAILURE; } - hFile = (HANDLE)bottom->secret->md.osfd; + hFile = (HANDLE)bottom->secret->md.osfd; if (!bottom->secret->md.io_model_committed) { PRInt32 st; @@ -810,16 +840,16 @@ PR_IMPLEMENT(PRStatus) PR_AddWaitFileDesc( bottom->secret->md.io_model_committed = PR_TRUE; } bResult = ReadFile(hFile, - desc->buffer.start, - (DWORD)desc->buffer.length, - NULL, - &overlapped->overlapped); + desc->buffer.start, + (DWORD)desc->buffer.length, + NULL, + &overlapped->overlapped); if (FALSE == bResult && (dwError = GetLastError()) != ERROR_IO_PENDING) { if (desc->timeout != PR_INTERVAL_NO_TIMEOUT) { if (InterlockedCompareExchange((LONG *)&desc->outcome, - (LONG)PR_MW_FAILURE, (LONG)PR_MW_PENDING) + (LONG)PR_MW_FAILURE, (LONG)PR_MW_PENDING) == (LONG)PR_MW_PENDING) { CancelTimer(overlapped->data.mw.timer); @@ -840,11 +870,15 @@ PR_IMPLEMENT(PRRecvWait*) PR_WaitRecvReady(PRWaitGroup *group) PRCList *io_ready = NULL; #ifdef WINNT PRThread *me = _PR_MD_CURRENT_THREAD(); - _MDOverlapped *overlapped; + _MDOverlapped *overlapped; #endif - if (!_pr_initialized) _PR_ImplicitInitialization(); - if ((NULL == group) && (NULL == (group = MW_Init2()))) goto failed_init; + if (!_pr_initialized) { + _PR_ImplicitInitialization(); + } + if ((NULL == group) && (NULL == (group = MW_Init2()))) { + goto failed_init; + } PR_Lock(group->ml); @@ -890,7 +924,7 @@ PR_IMPLEMENT(PRRecvWait*) PR_WaitRecvReady(PRWaitGroup *group) PR_REMOVE_LINK(io_ready); _PR_MD_UNLOCK(&group->mdlock); overlapped = (_MDOverlapped *) - ((char *)io_ready - offsetof(_MDOverlapped, data)); + ((char *)io_ready - offsetof(_MDOverlapped, data)); io_ready = &overlapped->data.mw.desc->internal; #else do @@ -915,7 +949,9 @@ PR_IMPLEMENT(PRRecvWait*) PR_WaitRecvReady(PRWaitGroup *group) ** The polling function should only return w/ failure or ** with some I/O ready. */ - if (PR_FAILURE == _MW_PollInternal(group)) goto failed_poll; + if (PR_FAILURE == _MW_PollInternal(group)) { + goto failed_poll; + } } else { @@ -934,7 +970,7 @@ PR_IMPLEMENT(PRRecvWait*) PR_WaitRecvReady(PRWaitGroup *group) ** it is still full of if's with continue and goto. */ PRStatus st; - do + do { st = PR_WaitCondVar(group->io_complete, PR_INTERVAL_NO_TIMEOUT); if (_prmw_running != group->state) @@ -942,7 +978,9 @@ PR_IMPLEMENT(PRRecvWait*) PR_WaitRecvReady(PRWaitGroup *group) PR_SetError(PR_INVALID_STATE_ERROR, 0); goto aborted; } - if (_MW_ABORTED(st) || (NULL == group->poller)) break; + if (_MW_ABORTED(st) || (NULL == group->poller)) { + break; + } } while (PR_CLIST_IS_EMPTY(&group->io_ready)); /* @@ -954,9 +992,10 @@ PR_IMPLEMENT(PRRecvWait*) PR_WaitRecvReady(PRWaitGroup *group) if (_MW_ABORTED(st)) { if ((NULL == group->poller - || !PR_CLIST_IS_EMPTY(&group->io_ready)) - && group->waiting_threads > 1) + || !PR_CLIST_IS_EMPTY(&group->io_ready)) + && group->waiting_threads > 1) { PR_NotifyCondVar(group->io_complete); + } goto aborted; } @@ -966,13 +1005,15 @@ PR_IMPLEMENT(PRRecvWait*) PR_WaitRecvReady(PRWaitGroup *group) ** i/o ready, it has a higher priority. I want to ** process the ready i/o first and wake up another ** thread to be the new poller. - */ + */ if (NULL == group->poller) { - if (PR_CLIST_IS_EMPTY(&group->io_ready)) + if (PR_CLIST_IS_EMPTY(&group->io_ready)) { continue; - if (group->waiting_threads > 1) + } + if (group->waiting_threads > 1) { PR_NotifyCondVar(group->io_complete); + } } } PR_ASSERT(!PR_CLIST_IS_EMPTY(&group->io_ready)); @@ -1025,13 +1066,13 @@ failed_init: if (NULL != overlapped->data.mw.timer) { PR_ASSERT(PR_INTERVAL_NO_TIMEOUT - != overlapped->data.mw.desc->timeout); + != overlapped->data.mw.desc->timeout); CancelTimer(overlapped->data.mw.timer); } else { PR_ASSERT(PR_INTERVAL_NO_TIMEOUT - == overlapped->data.mw.desc->timeout); + == overlapped->data.mw.desc->timeout); } PR_DELETE(overlapped); #endif @@ -1045,7 +1086,9 @@ PR_IMPLEMENT(PRStatus) PR_CancelWaitFileDesc(PRWaitGroup *group, PRRecvWait *des PRRecvWait **recv_wait; #endif PRStatus rv = PR_SUCCESS; - if (NULL == group) group = mw_state->group; + if (NULL == group) { + group = mw_state->group; + } PR_ASSERT(NULL != group); if (NULL == group) { @@ -1064,7 +1107,7 @@ PR_IMPLEMENT(PRStatus) PR_CancelWaitFileDesc(PRWaitGroup *group, PRRecvWait *des #ifdef WINNT if (InterlockedCompareExchange((LONG *)&desc->outcome, - (LONG)PR_MW_INTERRUPT, (LONG)PR_MW_PENDING) == (LONG)PR_MW_PENDING) + (LONG)PR_MW_INTERRUPT, (LONG)PR_MW_PENDING) == (LONG)PR_MW_PENDING) { PRFileDesc *bottom = PR_GetIdentitiesLayer(desc->fd, PR_NSPR_IO_LAYER); PR_ASSERT(NULL != bottom); @@ -1097,7 +1140,9 @@ PR_IMPLEMENT(PRStatus) PR_CancelWaitFileDesc(PRWaitGroup *group, PRRecvWait *des do { PRRecvWait *done = (PRRecvWait*)head; - if (done == desc) goto unlock; + if (done == desc) { + goto unlock; + } head = PR_NEXT_LINK(head); } while (head != &group->io_ready); } @@ -1120,7 +1165,9 @@ PR_IMPLEMENT(PRRecvWait*) PR_CancelWaitGroup(PRWaitGroup *group) PRThread *me = _PR_MD_CURRENT_THREAD(); #endif - if (NULL == group) group = mw_state->group; + if (NULL == group) { + group = mw_state->group; + } PR_ASSERT(NULL != group); if (NULL == group) { @@ -1131,17 +1178,20 @@ PR_IMPLEMENT(PRRecvWait*) PR_CancelWaitGroup(PRWaitGroup *group) PR_Lock(group->ml); if (_prmw_stopped != group->state) { - if (_prmw_running == group->state) - group->state = _prmw_stopping; /* so nothing new comes in */ - if (0 == group->waiting_threads) /* is there anybody else? */ - group->state = _prmw_stopped; /* we can stop right now */ + if (_prmw_running == group->state) { + group->state = _prmw_stopping; /* so nothing new comes in */ + } + if (0 == group->waiting_threads) { /* is there anybody else? */ + group->state = _prmw_stopped; /* we can stop right now */ + } else { PR_NotifyAllCondVar(group->new_business); PR_NotifyAllCondVar(group->io_complete); } - while (_prmw_stopped != group->state) + while (_prmw_stopped != group->state) { (void)PR_WaitCondVar(group->mw_manage, PR_INTERVAL_NO_TIMEOUT); + } } #ifdef WINNT @@ -1155,11 +1205,11 @@ PR_IMPLEMENT(PRRecvWait*) PR_CancelWaitGroup(PRWaitGroup *group) if (NULL != *desc) { if (InterlockedCompareExchange((LONG *)&(*desc)->outcome, - (LONG)PR_MW_INTERRUPT, (LONG)PR_MW_PENDING) + (LONG)PR_MW_INTERRUPT, (LONG)PR_MW_PENDING) == (LONG)PR_MW_PENDING) { PRFileDesc *bottom = PR_GetIdentitiesLayer( - (*desc)->fd, PR_NSPR_IO_LAYER); + (*desc)->fd, PR_NSPR_IO_LAYER); PR_ASSERT(NULL != bottom); if (NULL == bottom) { @@ -1173,7 +1223,7 @@ PR_IMPLEMENT(PRRecvWait*) PR_CancelWaitGroup(PRWaitGroup *group) if (closesocket(bottom->secret->md.osfd) == SOCKET_ERROR) { fprintf(stderr, "closesocket failed: %d\n", - WSAGetLastError()); + WSAGetLastError()); exit(1); } } @@ -1202,32 +1252,34 @@ PR_IMPLEMENT(PRRecvWait*) PR_CancelWaitGroup(PRWaitGroup *group) for (desc = &group->waiter->recv_wait; group->waiter->count > 0; ++desc) { PR_ASSERT(desc < &group->waiter->recv_wait + group->waiter->length); - if (NULL != *desc) + if (NULL != *desc) { _MW_DoneInternal(group, desc, PR_MW_INTERRUPT); + } } #endif /* take first element of finished list and return it or NULL */ - if (PR_CLIST_IS_EMPTY(&group->io_ready)) + if (PR_CLIST_IS_EMPTY(&group->io_ready)) { PR_SetError(PR_GROUP_EMPTY_ERROR, 0); + } else { PRCList *head = PR_LIST_HEAD(&group->io_ready); PR_REMOVE_AND_INIT_LINK(head); #ifdef WINNT overlapped = (_MDOverlapped *) - ((char *)head - offsetof(_MDOverlapped, data)); + ((char *)head - offsetof(_MDOverlapped, data)); head = &overlapped->data.mw.desc->internal; if (NULL != overlapped->data.mw.timer) { PR_ASSERT(PR_INTERVAL_NO_TIMEOUT - != overlapped->data.mw.desc->timeout); + != overlapped->data.mw.desc->timeout); CancelTimer(overlapped->data.mw.timer); } else { PR_ASSERT(PR_INTERVAL_NO_TIMEOUT - == overlapped->data.mw.desc->timeout); + == overlapped->data.mw.desc->timeout); } PR_DELETE(overlapped); #endif @@ -1253,23 +1305,33 @@ PR_IMPLEMENT(PRWaitGroup*) PR_CreateWaitGroup(PRInt32 size /* ignored */) } /* the wait group itself */ wg->ml = PR_NewLock(); - if (NULL == wg->ml) goto failed_lock; + if (NULL == wg->ml) { + goto failed_lock; + } wg->io_taken = PR_NewCondVar(wg->ml); - if (NULL == wg->io_taken) goto failed_cvar0; + if (NULL == wg->io_taken) { + goto failed_cvar0; + } wg->io_complete = PR_NewCondVar(wg->ml); - if (NULL == wg->io_complete) goto failed_cvar1; + if (NULL == wg->io_complete) { + goto failed_cvar1; + } wg->new_business = PR_NewCondVar(wg->ml); - if (NULL == wg->new_business) goto failed_cvar2; + if (NULL == wg->new_business) { + goto failed_cvar2; + } wg->mw_manage = PR_NewCondVar(wg->ml); - if (NULL == wg->mw_manage) goto failed_cvar3; + if (NULL == wg->mw_manage) { + goto failed_cvar3; + } PR_INIT_CLIST(&wg->group_link); PR_INIT_CLIST(&wg->io_ready); /* the waiters sequence */ wg->waiter = (_PRWaiterHash*)PR_CALLOC( - sizeof(_PRWaiterHash) + - (_PR_DEFAULT_HASH_LENGTH * sizeof(PRRecvWait*))); + sizeof(_PRWaiterHash) + + (_PR_DEFAULT_HASH_LENGTH * sizeof(PRRecvWait*))); if (NULL == wg->waiter) { PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); @@ -1309,14 +1371,16 @@ failed: PR_IMPLEMENT(PRStatus) PR_DestroyWaitGroup(PRWaitGroup *group) { PRStatus rv = PR_SUCCESS; - if (NULL == group) group = mw_state->group; + if (NULL == group) { + group = mw_state->group; + } PR_ASSERT(NULL != group); if (NULL != group) { PR_Lock(group->ml); if ((group->waiting_threads == 0) - && (group->waiter->count == 0) - && PR_CLIST_IS_EMPTY(&group->io_ready)) + && (group->waiter->count == 0) + && PR_CLIST_IS_EMPTY(&group->io_ready)) { group->state = _prmw_stopped; } @@ -1326,7 +1390,9 @@ PR_IMPLEMENT(PRStatus) PR_DestroyWaitGroup(PRWaitGroup *group) rv = PR_FAILURE; } PR_Unlock(group->ml); - if (PR_FAILURE == rv) return rv; + if (PR_FAILURE == rv) { + return rv; + } PR_Lock(mw_lock); PR_REMOVE_LINK(&group->group_link); @@ -1347,7 +1413,9 @@ PR_IMPLEMENT(PRStatus) PR_DestroyWaitGroup(PRWaitGroup *group) PR_DestroyCondVar(group->io_complete); PR_DestroyCondVar(group->io_taken); PR_DestroyLock(group->ml); - if (group == mw_state->group) mw_state->group = NULL; + if (group == mw_state->group) { + mw_state->group = NULL; + } PR_DELETE(group); } else @@ -1368,7 +1436,9 @@ PR_IMPLEMENT(PRStatus) PR_DestroyWaitGroup(PRWaitGroup *group) PR_IMPLEMENT(PRMWaitEnumerator*) PR_CreateMWaitEnumerator(PRWaitGroup *group) { PRMWaitEnumerator *enumerator = PR_NEWZAP(PRMWaitEnumerator); - if (NULL == enumerator) PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); + if (NULL == enumerator) { + PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); + } else { enumerator->group = group; @@ -1395,12 +1465,14 @@ PR_IMPLEMENT(PRRecvWait*) PR_EnumerateWaitGroup( PRMWaitEnumerator *enumerator, const PRRecvWait *previous) { PRRecvWait *result = NULL; - + /* entry point sanity checking */ PR_ASSERT(NULL != enumerator); PR_ASSERT(_PR_ENUM_SEALED == enumerator->seal); if ((NULL == enumerator) - || (_PR_ENUM_SEALED != enumerator->seal)) goto bad_argument; + || (_PR_ENUM_SEALED != enumerator->seal)) { + goto bad_argument; + } /* beginning of enumeration */ if (NULL == previous) @@ -1424,11 +1496,14 @@ PR_IMPLEMENT(PRRecvWait*) PR_EnumerateWaitGroup( { PRThread *me = PR_GetCurrentThread(); PR_ASSERT(me == enumerator->thread); - if (me != enumerator->thread) goto bad_argument; + if (me != enumerator->thread) { + goto bad_argument; + } /* need to restart the enumeration */ - if (enumerator->p_timestamp != enumerator->group->p_timestamp) + if (enumerator->p_timestamp != enumerator->group->p_timestamp) { return PR_EnumerateWaitGroup(enumerator, NULL); + } } /* actually progress the enumeration */ @@ -1439,7 +1514,9 @@ PR_IMPLEMENT(PRRecvWait*) PR_EnumerateWaitGroup( #endif while (enumerator->index++ < enumerator->group->waiter->length) { - if (NULL != (result = *(enumerator->waiter)++)) break; + if (NULL != (result = *(enumerator->waiter)++)) { + break; + } } #if defined(WINNT) _PR_MD_UNLOCK(&enumerator->group->mdlock); diff --git a/nsprpub/pr/src/io/prpolevt.c b/nsprpub/pr/src/io/prpolevt.c index ac3eb101ae..1b664ba24e 100644 --- a/nsprpub/pr/src/io/prpolevt.c +++ b/nsprpub/pr/src/io/prpolevt.c @@ -62,30 +62,30 @@ static PRIOMethods _pr_polevt_methods = { (PRSeek64FN)_PR_InvalidInt64, (PRFileInfoFN)_PR_InvalidStatus, (PRFileInfo64FN)_PR_InvalidStatus, - (PRWritevFN)_PR_InvalidInt, - (PRConnectFN)_PR_InvalidStatus, - (PRAcceptFN)_PR_InvalidDesc, - (PRBindFN)_PR_InvalidStatus, - (PRListenFN)_PR_InvalidStatus, - (PRShutdownFN)_PR_InvalidStatus, - (PRRecvFN)_PR_InvalidInt, - (PRSendFN)_PR_InvalidInt, - (PRRecvfromFN)_PR_InvalidInt, - (PRSendtoFN)_PR_InvalidInt, + (PRWritevFN)_PR_InvalidInt, + (PRConnectFN)_PR_InvalidStatus, + (PRAcceptFN)_PR_InvalidDesc, + (PRBindFN)_PR_InvalidStatus, + (PRListenFN)_PR_InvalidStatus, + (PRShutdownFN)_PR_InvalidStatus, + (PRRecvFN)_PR_InvalidInt, + (PRSendFN)_PR_InvalidInt, + (PRRecvfromFN)_PR_InvalidInt, + (PRSendtoFN)_PR_InvalidInt, _pr_PolEvtPoll, - (PRAcceptreadFN)_PR_InvalidInt, - (PRTransmitfileFN)_PR_InvalidInt, - (PRGetsocknameFN)_PR_InvalidStatus, - (PRGetpeernameFN)_PR_InvalidStatus, - (PRReservedFN)_PR_InvalidInt, - (PRReservedFN)_PR_InvalidInt, + (PRAcceptreadFN)_PR_InvalidInt, + (PRTransmitfileFN)_PR_InvalidInt, + (PRGetsocknameFN)_PR_InvalidStatus, + (PRGetpeernameFN)_PR_InvalidStatus, + (PRReservedFN)_PR_InvalidInt, + (PRReservedFN)_PR_InvalidInt, (PRGetsocketoptionFN)_PR_InvalidStatus, (PRSetsocketoptionFN)_PR_InvalidStatus, - (PRSendfileFN)_PR_InvalidInt, - (PRConnectcontinueFN)_PR_InvalidStatus, - (PRReservedFN)_PR_InvalidInt, - (PRReservedFN)_PR_InvalidInt, - (PRReservedFN)_PR_InvalidInt, + (PRSendfileFN)_PR_InvalidInt, + (PRConnectcontinueFN)_PR_InvalidStatus, + (PRReservedFN)_PR_InvalidInt, + (PRReservedFN)_PR_InvalidInt, + (PRReservedFN)_PR_InvalidInt, (PRReservedFN)_PR_InvalidInt }; @@ -130,7 +130,7 @@ PR_IMPLEMENT(PRFileDesc *) PR_NewPollableEvent(void) event = PR_CreateIOLayerStub(_pr_polevt_id, &_pr_polevt_methods); if (NULL == event) { goto errorExit; - } + } event->secret = PR_NEW(PRFilePrivate); if (event->secret == NULL) { PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); @@ -147,9 +147,9 @@ PR_IMPLEMENT(PRFileDesc *) PR_NewPollableEvent(void) fd[0] = fd[1] = NULL; goto errorExit; } - /* - * set the TCP_NODELAY option to reduce notification latency - */ + /* + * set the TCP_NODELAY option to reduce notification latency + */ socket_opt.option = PR_SockOpt_NoDelay; socket_opt.value.no_delay = PR_TRUE; rv = PR_SetSocketOption(fd[1], &socket_opt); diff --git a/nsprpub/pr/src/io/prprf.c b/nsprpub/pr/src/io/prprf.c index 8475c77137..9b6072e268 100644 --- a/nsprpub/pr/src/io/prprf.c +++ b/nsprpub/pr/src/io/prprf.c @@ -49,17 +49,17 @@ struct SprintfStateStr { struct NumArg { int type; /* type of the numbered argument */ union { /* the numbered argument */ - int i; - unsigned int ui; - PRInt32 i32; - PRUint32 ui32; - PRInt64 ll; - PRUint64 ull; - double d; - const char *s; - int *ip; + int i; + unsigned int ui; + PRInt32 i32; + PRUint32 ui32; + PRInt64 ll; + PRUint64 ull; + double d; + const char *s; + int *ip; #ifdef WIN32 - const WCHAR *ws; + const WCHAR *ws; #endif } u; }; @@ -71,63 +71,63 @@ struct NumArg { ** and their corresponding unsigned versions must have the subsequent ** odd value. */ -#define TYPE_INT16 0 -#define TYPE_UINT16 1 -#define TYPE_INTN 2 -#define TYPE_UINTN 3 -#define TYPE_INT32 4 -#define TYPE_UINT32 5 -#define TYPE_INT64 6 -#define TYPE_UINT64 7 -#define TYPE_STRING 8 -#define TYPE_DOUBLE 9 -#define TYPE_INTSTR 10 +#define TYPE_INT16 0 +#define TYPE_UINT16 1 +#define TYPE_INTN 2 +#define TYPE_UINTN 3 +#define TYPE_INT32 4 +#define TYPE_UINT32 5 +#define TYPE_INT64 6 +#define TYPE_UINT64 7 +#define TYPE_STRING 8 +#define TYPE_DOUBLE 9 +#define TYPE_INTSTR 10 #ifdef WIN32 -#define TYPE_WSTRING 11 +#define TYPE_WSTRING 11 #endif -#define TYPE_UNKNOWN 20 +#define TYPE_UNKNOWN 20 -#define FLAG_LEFT 0x1 -#define FLAG_SIGNED 0x2 -#define FLAG_SPACED 0x4 -#define FLAG_ZEROS 0x8 -#define FLAG_NEG 0x10 +#define FLAG_LEFT 0x1 +#define FLAG_SIGNED 0x2 +#define FLAG_SPACED 0x4 +#define FLAG_ZEROS 0x8 +#define FLAG_NEG 0x10 /* ** Fill into the buffer using the data in src */ static int fill2(SprintfState *ss, const char *src, int srclen, int width, - int flags) + int flags) { char space = ' '; int rv; width -= srclen; - if ((width > 0) && ((flags & FLAG_LEFT) == 0)) { /* Right adjusting */ - if (flags & FLAG_ZEROS) { - space = '0'; - } - while (--width >= 0) { - rv = (*ss->stuff)(ss, &space, 1); - if (rv < 0) { - return rv; - } - } + if ((width > 0) && ((flags & FLAG_LEFT) == 0)) { /* Right adjusting */ + if (flags & FLAG_ZEROS) { + space = '0'; + } + while (--width >= 0) { + rv = (*ss->stuff)(ss, &space, 1); + if (rv < 0) { + return rv; + } + } } /* Copy out the source data */ rv = (*ss->stuff)(ss, src, srclen); if (rv < 0) { - return rv; + return rv; } - if ((width > 0) && ((flags & FLAG_LEFT) != 0)) { /* Left adjusting */ - while (--width >= 0) { - rv = (*ss->stuff)(ss, &space, 1); - if (rv < 0) { - return rv; - } - } + if ((width > 0) && ((flags & FLAG_LEFT) != 0)) { /* Left adjusting */ + while (--width >= 0) { + rv = (*ss->stuff)(ss, &space, 1); + if (rv < 0) { + return rv; + } + } } return 0; } @@ -136,7 +136,7 @@ static int fill2(SprintfState *ss, const char *src, int srclen, int width, ** Fill a number. The order is: optional-sign zero-filling conversion-digits */ static int fill_n(SprintfState *ss, const char *src, int srclen, int width, - int prec, int type, int flags) + int prec, int type, int flags) { int zerowidth = 0; int precwidth = 0; @@ -148,77 +148,77 @@ static int fill_n(SprintfState *ss, const char *src, int srclen, int width, char sign; if ((type & 1) == 0) { - if (flags & FLAG_NEG) { - sign = '-'; - signwidth = 1; - } else if (flags & FLAG_SIGNED) { - sign = '+'; - signwidth = 1; - } else if (flags & FLAG_SPACED) { - sign = ' '; - signwidth = 1; - } + if (flags & FLAG_NEG) { + sign = '-'; + signwidth = 1; + } else if (flags & FLAG_SIGNED) { + sign = '+'; + signwidth = 1; + } else if (flags & FLAG_SPACED) { + sign = ' '; + signwidth = 1; + } } cvtwidth = signwidth + srclen; if (prec > 0) { - if (prec > srclen) { - precwidth = prec - srclen; /* Need zero filling */ - cvtwidth += precwidth; - } + if (prec > srclen) { + precwidth = prec - srclen; /* Need zero filling */ + cvtwidth += precwidth; + } } if ((flags & FLAG_ZEROS) && (prec < 0)) { - if (width > cvtwidth) { - zerowidth = width - cvtwidth; /* Zero filling */ - cvtwidth += zerowidth; - } + if (width > cvtwidth) { + zerowidth = width - cvtwidth; /* Zero filling */ + cvtwidth += zerowidth; + } } if (flags & FLAG_LEFT) { - if (width > cvtwidth) { - /* Space filling on the right (i.e. left adjusting) */ - rightspaces = width - cvtwidth; - } + if (width > cvtwidth) { + /* Space filling on the right (i.e. left adjusting) */ + rightspaces = width - cvtwidth; + } } else { - if (width > cvtwidth) { - /* Space filling on the left (i.e. right adjusting) */ - leftspaces = width - cvtwidth; - } + if (width > cvtwidth) { + /* Space filling on the left (i.e. right adjusting) */ + leftspaces = width - cvtwidth; + } } while (--leftspaces >= 0) { - rv = (*ss->stuff)(ss, " ", 1); - if (rv < 0) { - return rv; - } + rv = (*ss->stuff)(ss, " ", 1); + if (rv < 0) { + return rv; + } } if (signwidth) { - rv = (*ss->stuff)(ss, &sign, 1); - if (rv < 0) { - return rv; - } + rv = (*ss->stuff)(ss, &sign, 1); + if (rv < 0) { + return rv; + } } while (--precwidth >= 0) { - rv = (*ss->stuff)(ss, "0", 1); - if (rv < 0) { - return rv; - } + rv = (*ss->stuff)(ss, "0", 1); + if (rv < 0) { + return rv; + } } while (--zerowidth >= 0) { - rv = (*ss->stuff)(ss, "0", 1); - if (rv < 0) { - return rv; - } + rv = (*ss->stuff)(ss, "0", 1); + if (rv < 0) { + return rv; + } } rv = (*ss->stuff)(ss, src, srclen); if (rv < 0) { - return rv; + return rv; } while (--rightspaces >= 0) { - rv = (*ss->stuff)(ss, " ", 1); - if (rv < 0) { - return rv; - } + rv = (*ss->stuff)(ss, " ", 1); + if (rv < 0) { + return rv; + } } return 0; } @@ -227,7 +227,7 @@ static int fill_n(SprintfState *ss, const char *src, int srclen, int width, ** Convert a long into its printable form */ static int cvt_l(SprintfState *ss, long num, int width, int prec, int radix, - int type, int flags, const char *hexp) + int type, int flags, const char *hexp) { char cvtbuf[100]; char *cvt; @@ -235,7 +235,7 @@ static int cvt_l(SprintfState *ss, long num, int width, int prec, int radix, /* according to the man page this needs to happen */ if ((prec == 0) && (num == 0)) { - return 0; + return 0; } /* @@ -246,14 +246,14 @@ static int cvt_l(SprintfState *ss, long num, int width, int prec, int radix, cvt = cvtbuf + sizeof(cvtbuf); digits = 0; while (num) { - int digit = (((unsigned long)num) % radix) & 0xF; - *--cvt = hexp[digit]; - digits++; - num = (long)(((unsigned long)num) / radix); + int digit = (((unsigned long)num) % radix) & 0xF; + *--cvt = hexp[digit]; + digits++; + num = (long)(((unsigned long)num) / radix); } if (digits == 0) { - *--cvt = '0'; - digits++; + *--cvt = '0'; + digits++; } /* @@ -267,7 +267,7 @@ static int cvt_l(SprintfState *ss, long num, int width, int prec, int radix, ** Convert a 64-bit integer into its printable form */ static int cvt_ll(SprintfState *ss, PRInt64 num, int width, int prec, int radix, - int type, int flags, const char *hexp) + int type, int flags, const char *hexp) { char cvtbuf[100]; char *cvt; @@ -276,7 +276,7 @@ static int cvt_ll(SprintfState *ss, PRInt64 num, int width, int prec, int radix, /* according to the man page this needs to happen */ if ((prec == 0) && (LL_IS_ZERO(num))) { - return 0; + return 0; } /* @@ -288,17 +288,17 @@ static int cvt_ll(SprintfState *ss, PRInt64 num, int width, int prec, int radix, cvt = cvtbuf + sizeof(cvtbuf); digits = 0; while (!LL_IS_ZERO(num)) { - PRInt32 digit; - PRInt64 quot, rem; - LL_UDIVMOD(", &rem, num, rad); - LL_L2I(digit, rem); - *--cvt = hexp[digit & 0xf]; - digits++; - num = quot; + PRInt32 digit; + PRInt64 quot, rem; + LL_UDIVMOD(", &rem, num, rad); + LL_L2I(digit, rem); + *--cvt = hexp[digit & 0xf]; + digits++; + num = quot; } if (digits == 0) { - *--cvt = '0'; - digits++; + *--cvt = '0'; + digits++; } /* @@ -321,8 +321,8 @@ static int cvt_f(SprintfState *ss, double d, const char *fmt0, const char *fmt1) int amount = fmt1 - fmt0; if (amount <= 0 || amount >= sizeof(fin)) { - /* Totally bogus % command to snprintf. Just ignore it */ - return 0; + /* Totally bogus % command to snprintf. Just ignore it */ + return 0; } memcpy(fin, fmt0, amount); fin[amount] = 0; @@ -352,26 +352,27 @@ static int cvt_f(SprintfState *ss, double d, const char *fmt0, const char *fmt1) ** where -1 means until NUL. */ static int cvt_s(SprintfState *ss, const char *str, int width, int prec, - int flags) + int flags) { int slen; - if (prec == 0) - return 0; + if (prec == 0) { + return 0; + } /* Limit string length by precision value */ if (!str) { - str = "(null)"; - } + str = "(null)"; + } if (prec > 0) { - /* this is: slen = strnlen(str, prec); */ - register const char *s; + /* this is: slen = strnlen(str, prec); */ + register const char *s; - for(s = str; prec && *s; s++, prec-- ) - ; - slen = s - str; + for(s = str; prec && *s; s++, prec-- ) + ; + slen = s - str; } else { - slen = strlen(str); + slen = strlen(str); } /* and away we go */ @@ -381,7 +382,7 @@ static int cvt_s(SprintfState *ss, const char *str, int width, int prec, /* ** BuildArgArray stands for Numbered Argument list Sprintf ** for example, -** fmt = "%4$i, %2$d, %3s, %1d"; +** fmt = "%4$i, %2$d, %3s, %1d"; ** the number must start from 1, and no gap among them */ @@ -391,61 +392,63 @@ static struct NumArg* BuildArgArray( const char *fmt, va_list ap, int* rv, struc const char* p; char c; struct NumArg* nas; - + /* - ** first pass: - ** determine how many legal % I have got, then allocate space + ** first pass: + ** determine how many legal % I have got, then allocate space */ p = fmt; *rv = 0; i = 0; - while( ( c = *p++ ) != 0 ){ - if( c != '%' ) - continue; - if( ( c = *p++ ) == '%' ) /* skip %% case */ - continue; - - while( c != 0 ){ - if( c > '9' || c < '0' ){ - if( c == '$' ){ /* numbered argument case */ - if( i > 0 ){ - *rv = -1; - return NULL; - } - number++; - } else{ /* non-numbered argument case */ - if( number > 0 ){ - *rv = -1; - return NULL; - } - i = 1; - } - break; - } - - c = *p++; - } + while( ( c = *p++ ) != 0 ) { + if( c != '%' ) { + continue; + } + if( ( c = *p++ ) == '%' ) { /* skip %% case */ + continue; + } + + while( c != 0 ) { + if( c > '9' || c < '0' ) { + if( c == '$' ) { /* numbered argument case */ + if( i > 0 ) { + *rv = -1; + return NULL; + } + number++; + } else { /* non-numbered argument case */ + if( number > 0 ) { + *rv = -1; + return NULL; + } + i = 1; + } + break; + } + + c = *p++; + } } - if( number == 0 ){ - return NULL; + if( number == 0 ) { + return NULL; } - - if( number > NAS_DEFAULT_NUM ){ - nas = (struct NumArg*)PR_MALLOC( number * sizeof( struct NumArg ) ); - if( !nas ){ - *rv = -1; - return NULL; - } + + if( number > NAS_DEFAULT_NUM ) { + nas = (struct NumArg*)PR_MALLOC( number * sizeof( struct NumArg ) ); + if( !nas ) { + *rv = -1; + return NULL; + } } else { - nas = nasArray; + nas = nasArray; } - for( i = 0; i < number; i++ ){ - nas[i].type = TYPE_UNKNOWN; + for( i = 0; i < number; i++ ) { + nas[i].type = TYPE_UNKNOWN; } @@ -455,143 +458,148 @@ static struct NumArg* BuildArgArray( const char *fmt, va_list ap, int* rv, struc */ p = fmt; - while( ( c = *p++ ) != 0 ){ - if( c != '%' ) continue; - c = *p++; - if( c == '%' ) continue; - - cn = 0; - while( c && c != '$' ){ /* should improve error check later */ - cn = cn*10 + c - '0'; - c = *p++; - } - - if( !c || cn < 1 || cn > number ){ - *rv = -1; - break; + while( ( c = *p++ ) != 0 ) { + if( c != '%' ) { + continue; + } + c = *p++; + if( c == '%' ) { + continue; + } + + cn = 0; + while( c && c != '$' ) { /* should improve error check later */ + cn = cn*10 + c - '0'; + c = *p++; + } + + if( !c || cn < 1 || cn > number ) { + *rv = -1; + break; } - /* nas[cn] starts from 0, and make sure nas[cn].type is not assigned */ + /* nas[cn] starts from 0, and make sure nas[cn].type is not assigned */ cn--; - if( nas[cn].type != TYPE_UNKNOWN ) - continue; + if( nas[cn].type != TYPE_UNKNOWN ) { + continue; + } c = *p++; /* width */ if (c == '*') { - /* not supported feature, for the argument is not numbered */ - *rv = -1; - break; - } - - while ((c >= '0') && (c <= '9')) { - c = *p++; - } - - /* precision */ - if (c == '.') { - c = *p++; - if (c == '*') { - /* not supported feature, for the argument is not numbered */ - *rv = -1; - break; - } - - while ((c >= '0') && (c <= '9')) { - c = *p++; - } - } - - /* size */ - nas[cn].type = TYPE_INTN; - if (c == 'h') { - nas[cn].type = TYPE_INT16; - c = *p++; - } else if (c == 'L') { - /* XXX not quite sure here */ - nas[cn].type = TYPE_INT64; - c = *p++; - } else if (c == 'l') { - nas[cn].type = TYPE_INT32; - c = *p++; - if (c == 'l') { - nas[cn].type = TYPE_INT64; - c = *p++; - } - } else if (c == 'z') { - if (sizeof(size_t) == sizeof(PRInt32)) { - nas[ cn ].type = TYPE_INT32; - } else if (sizeof(size_t) == sizeof(PRInt64)) { - nas[ cn ].type = TYPE_INT64; - } else { - nas[ cn ].type = TYPE_UNKNOWN; - } - c = *p++; - } - - /* format */ - switch (c) { - case 'd': - case 'c': - case 'i': - case 'o': - case 'u': - case 'x': - case 'X': - break; - - case 'e': - case 'f': - case 'g': - nas[ cn ].type = TYPE_DOUBLE; - break; - - case 'p': - /* XXX should use cpp */ - if (sizeof(void *) == sizeof(PRInt32)) { - nas[ cn ].type = TYPE_UINT32; - } else if (sizeof(void *) == sizeof(PRInt64)) { - nas[ cn ].type = TYPE_UINT64; - } else if (sizeof(void *) == sizeof(PRIntn)) { - nas[ cn ].type = TYPE_UINTN; - } else { - nas[ cn ].type = TYPE_UNKNOWN; - } - break; - - case 'S': + /* not supported feature, for the argument is not numbered */ + *rv = -1; + break; + } + + while ((c >= '0') && (c <= '9')) { + c = *p++; + } + + /* precision */ + if (c == '.') { + c = *p++; + if (c == '*') { + /* not supported feature, for the argument is not numbered */ + *rv = -1; + break; + } + + while ((c >= '0') && (c <= '9')) { + c = *p++; + } + } + + /* size */ + nas[cn].type = TYPE_INTN; + if (c == 'h') { + nas[cn].type = TYPE_INT16; + c = *p++; + } else if (c == 'L') { + /* XXX not quite sure here */ + nas[cn].type = TYPE_INT64; + c = *p++; + } else if (c == 'l') { + nas[cn].type = TYPE_INT32; + c = *p++; + if (c == 'l') { + nas[cn].type = TYPE_INT64; + c = *p++; + } + } else if (c == 'z') { + if (sizeof(size_t) == sizeof(PRInt32)) { + nas[ cn ].type = TYPE_INT32; + } else if (sizeof(size_t) == sizeof(PRInt64)) { + nas[ cn ].type = TYPE_INT64; + } else { + nas[ cn ].type = TYPE_UNKNOWN; + } + c = *p++; + } + + /* format */ + switch (c) { + case 'd': + case 'c': + case 'i': + case 'o': + case 'u': + case 'x': + case 'X': + break; + + case 'e': + case 'f': + case 'g': + nas[ cn ].type = TYPE_DOUBLE; + break; + + case 'p': + /* XXX should use cpp */ + if (sizeof(void *) == sizeof(PRInt32)) { + nas[ cn ].type = TYPE_UINT32; + } else if (sizeof(void *) == sizeof(PRInt64)) { + nas[ cn ].type = TYPE_UINT64; + } else if (sizeof(void *) == sizeof(PRIntn)) { + nas[ cn ].type = TYPE_UINTN; + } else { + nas[ cn ].type = TYPE_UNKNOWN; + } + break; + + case 'S': #ifdef WIN32 - nas[ cn ].type = TYPE_WSTRING; - break; + nas[ cn ].type = TYPE_WSTRING; + break; #endif - case 'C': - case 'E': - case 'G': - /* XXX not supported I suppose */ - PR_ASSERT(0); - nas[ cn ].type = TYPE_UNKNOWN; - break; - - case 's': - nas[ cn ].type = TYPE_STRING; - break; - - case 'n': - nas[ cn ].type = TYPE_INTSTR; - break; - - default: - PR_ASSERT(0); - nas[ cn ].type = TYPE_UNKNOWN; - break; - } - - /* get a legal para. */ - if( nas[ cn ].type == TYPE_UNKNOWN ){ - *rv = -1; - break; - } + case 'C': + case 'E': + case 'G': + /* XXX not supported I suppose */ + PR_ASSERT(0); + nas[ cn ].type = TYPE_UNKNOWN; + break; + + case 's': + nas[ cn ].type = TYPE_STRING; + break; + + case 'n': + nas[ cn ].type = TYPE_INTSTR; + break; + + default: + PR_ASSERT(0); + nas[ cn ].type = TYPE_UNKNOWN; + break; + } + + /* get a legal para. */ + if( nas[ cn ].type == TYPE_UNKNOWN ) { + *rv = -1; + break; + } } @@ -600,72 +608,74 @@ static struct NumArg* BuildArgArray( const char *fmt, va_list ap, int* rv, struc ** fill the nas[cn].ap */ - if( *rv < 0 ){ - if( nas != nasArray ) - PR_DELETE( nas ); - return NULL; + if( *rv < 0 ) { + if( nas != nasArray ) { + PR_DELETE( nas ); + } + return NULL; } cn = 0; - while( cn < number ){ - if( nas[cn].type == TYPE_UNKNOWN ){ - cn++; - continue; - } - - switch( nas[cn].type ){ - case TYPE_INT16: - case TYPE_UINT16: - case TYPE_INTN: - nas[cn].u.i = va_arg( ap, int ); - break; - - case TYPE_UINTN: - nas[cn].u.ui = va_arg( ap, unsigned int ); - break; - - case TYPE_INT32: - nas[cn].u.i32 = va_arg( ap, PRInt32 ); - break; - - case TYPE_UINT32: - nas[cn].u.ui32 = va_arg( ap, PRUint32 ); - break; - - case TYPE_INT64: - nas[cn].u.ll = va_arg( ap, PRInt64 ); - break; - - case TYPE_UINT64: - nas[cn].u.ull = va_arg( ap, PRUint64 ); - break; - - case TYPE_STRING: - nas[cn].u.s = va_arg( ap, char* ); - break; + while( cn < number ) { + if( nas[cn].type == TYPE_UNKNOWN ) { + cn++; + continue; + } + + switch( nas[cn].type ) { + case TYPE_INT16: + case TYPE_UINT16: + case TYPE_INTN: + nas[cn].u.i = va_arg( ap, int ); + break; + + case TYPE_UINTN: + nas[cn].u.ui = va_arg( ap, unsigned int ); + break; + + case TYPE_INT32: + nas[cn].u.i32 = va_arg( ap, PRInt32 ); + break; + + case TYPE_UINT32: + nas[cn].u.ui32 = va_arg( ap, PRUint32 ); + break; + + case TYPE_INT64: + nas[cn].u.ll = va_arg( ap, PRInt64 ); + break; + + case TYPE_UINT64: + nas[cn].u.ull = va_arg( ap, PRUint64 ); + break; + + case TYPE_STRING: + nas[cn].u.s = va_arg( ap, char* ); + break; #ifdef WIN32 - case TYPE_WSTRING: - nas[cn].u.ws = va_arg( ap, WCHAR* ); - break; + case TYPE_WSTRING: + nas[cn].u.ws = va_arg( ap, WCHAR* ); + break; #endif - case TYPE_INTSTR: - nas[cn].u.ip = va_arg( ap, int* ); - break; + case TYPE_INTSTR: + nas[cn].u.ip = va_arg( ap, int* ); + break; - case TYPE_DOUBLE: - nas[cn].u.d = va_arg( ap, double ); - break; + case TYPE_DOUBLE: + nas[cn].u.d = va_arg( ap, double ); + break; - default: - if( nas != nasArray ) - PR_DELETE( nas ); - *rv = -1; - return NULL; - } + default: + if( nas != nasArray ) { + PR_DELETE( nas ); + } + *rv = -1; + return NULL; + } - cn++; + cn++; } @@ -680,15 +690,15 @@ static int dosprintf(SprintfState *ss, const char *fmt, va_list ap) char c; int flags, width, prec, radix, type; union { - char ch; - int i; - long l; - PRInt64 ll; - double d; - const char *s; - int *ip; + char ch; + int i; + long l; + PRInt64 ll; + double d; + const char *s; + int *ip; #ifdef WIN32 - const WCHAR *ws; + const WCHAR *ws; #endif } u; const char *fmt0; @@ -711,344 +721,359 @@ static int dosprintf(SprintfState *ss, const char *fmt, va_list ap) */ nas = BuildArgArray( fmt, ap, &rv, nasArray ); - if( rv < 0 ){ - /* the fmt contains error Numbered Argument format, jliu@netscape.com */ - PR_ASSERT(0); - return rv; + if( rv < 0 ) { + /* the fmt contains error Numbered Argument format, jliu@netscape.com */ + PR_ASSERT(0); + return rv; } while ((c = *fmt++) != 0) { - if (c != '%') { - rv = (*ss->stuff)(ss, fmt - 1, 1); - if (rv < 0) { - return rv; - } - continue; - } - fmt0 = fmt - 1; - - /* - ** Gobble up the % format string. Hopefully we have handled all - ** of the strange cases! - */ - flags = 0; - c = *fmt++; - if (c == '%') { - /* quoting a % with %% */ - rv = (*ss->stuff)(ss, fmt - 1, 1); - if (rv < 0) { - return rv; - } - continue; - } - - if( nas != NULL ){ - /* the fmt contains the Numbered Arguments feature */ - i = 0; - while( c && c != '$' ){ /* should improve error check later */ - i = ( i * 10 ) + ( c - '0' ); - c = *fmt++; - } - - if( nas[i-1].type == TYPE_UNKNOWN ){ - if( nas && ( nas != nasArray ) ) - PR_DELETE( nas ); - return -1; - } - - nap = &nas[i-1]; - dolPt = fmt; - c = *fmt++; - } - - /* - * Examine optional flags. Note that we do not implement the - * '#' flag of sprintf(). The ANSI C spec. of the '#' flag is - * somewhat ambiguous and not ideal, which is perhaps why - * the various sprintf() implementations are inconsistent - * on this feature. - */ - while ((c == '-') || (c == '+') || (c == ' ') || (c == '0')) { - if (c == '-') flags |= FLAG_LEFT; - if (c == '+') flags |= FLAG_SIGNED; - if (c == ' ') flags |= FLAG_SPACED; - if (c == '0') flags |= FLAG_ZEROS; - c = *fmt++; - } - if (flags & FLAG_SIGNED) flags &= ~FLAG_SPACED; - if (flags & FLAG_LEFT) flags &= ~FLAG_ZEROS; - - /* width */ - if (c == '*') { - c = *fmt++; - width = va_arg(ap, int); - } else { - width = 0; - while ((c >= '0') && (c <= '9')) { - width = (width * 10) + (c - '0'); - c = *fmt++; - } - } - - /* precision */ - prec = -1; - if (c == '.') { - c = *fmt++; - if (c == '*') { - c = *fmt++; - prec = va_arg(ap, int); - } else { - prec = 0; - while ((c >= '0') && (c <= '9')) { - prec = (prec * 10) + (c - '0'); - c = *fmt++; - } - } - } - - /* size */ - type = TYPE_INTN; - if (c == 'h') { - type = TYPE_INT16; - c = *fmt++; - } else if (c == 'L') { - /* XXX not quite sure here */ - type = TYPE_INT64; - c = *fmt++; - } else if (c == 'l') { - type = TYPE_INT32; - c = *fmt++; - if (c == 'l') { - type = TYPE_INT64; - c = *fmt++; - } - } else if (c == 'z') { - if (sizeof(size_t) == sizeof(PRInt32)) { - type = TYPE_INT32; - } else if (sizeof(size_t) == sizeof(PRInt64)) { - type = TYPE_INT64; - } - c = *fmt++; - } - - /* format */ - hexp = hex; - switch (c) { - case 'd': case 'i': /* decimal/integer */ - radix = 10; - goto fetch_and_convert; - - case 'o': /* octal */ - radix = 8; - type |= 1; - goto fetch_and_convert; - - case 'u': /* unsigned decimal */ - radix = 10; - type |= 1; - goto fetch_and_convert; - - case 'x': /* unsigned hex */ - radix = 16; - type |= 1; - goto fetch_and_convert; - - case 'X': /* unsigned HEX */ - radix = 16; - hexp = HEX; - type |= 1; - goto fetch_and_convert; - - fetch_and_convert: - switch (type) { - case TYPE_INT16: - u.l = nas ? nap->u.i : va_arg(ap, int); - if (u.l < 0) { - u.l = -u.l; - flags |= FLAG_NEG; - } - goto do_long; - case TYPE_UINT16: - u.l = (nas ? nap->u.i : va_arg(ap, int)) & 0xffff; - goto do_long; - case TYPE_INTN: - u.l = nas ? nap->u.i : va_arg(ap, int); - if (u.l < 0) { - u.l = -u.l; - flags |= FLAG_NEG; - } - goto do_long; - case TYPE_UINTN: - u.l = (long)(nas ? nap->u.ui : va_arg(ap, unsigned int)); - goto do_long; - - case TYPE_INT32: - u.l = nas ? nap->u.i32 : va_arg(ap, PRInt32); - if (u.l < 0) { - u.l = -u.l; - flags |= FLAG_NEG; - } - goto do_long; - case TYPE_UINT32: - u.l = (long)(nas ? nap->u.ui32 : va_arg(ap, PRUint32)); - do_long: - rv = cvt_l(ss, u.l, width, prec, radix, type, flags, hexp); - if (rv < 0) { - return rv; - } - break; - - case TYPE_INT64: - u.ll = nas ? nap->u.ll : va_arg(ap, PRInt64); - if (!LL_GE_ZERO(u.ll)) { - LL_NEG(u.ll, u.ll); - flags |= FLAG_NEG; - } - goto do_longlong; - case TYPE_UINT64: - u.ll = nas ? nap->u.ull : va_arg(ap, PRUint64); - do_longlong: - rv = cvt_ll(ss, u.ll, width, prec, radix, type, flags, hexp); - if (rv < 0) { - return rv; - } - break; - } - break; - - case 'e': - case 'E': - case 'f': - case 'g': - u.d = nas ? nap->u.d : va_arg(ap, double); - if( nas != NULL ){ - i = fmt - dolPt; - if( i < sizeof( pattern ) ){ - pattern[0] = '%'; - memcpy( &pattern[1], dolPt, i ); - rv = cvt_f(ss, u.d, pattern, &pattern[i+1] ); - } - } else - rv = cvt_f(ss, u.d, fmt0, fmt); - - if (rv < 0) { - return rv; - } - break; - - case 'c': - u.ch = nas ? nap->u.i : va_arg(ap, int); - if ((flags & FLAG_LEFT) == 0) { - while (width-- > 1) { - rv = (*ss->stuff)(ss, " ", 1); - if (rv < 0) { - return rv; - } + if (c != '%') { + rv = (*ss->stuff)(ss, fmt - 1, 1); + if (rv < 0) { + return rv; + } + continue; + } + fmt0 = fmt - 1; + + /* + ** Gobble up the % format string. Hopefully we have handled all + ** of the strange cases! + */ + flags = 0; + c = *fmt++; + if (c == '%') { + /* quoting a % with %% */ + rv = (*ss->stuff)(ss, fmt - 1, 1); + if (rv < 0) { + return rv; + } + continue; + } + + if( nas != NULL ) { + /* the fmt contains the Numbered Arguments feature */ + i = 0; + while( c && c != '$' ) { /* should improve error check later */ + i = ( i * 10 ) + ( c - '0' ); + c = *fmt++; + } + + if( nas[i-1].type == TYPE_UNKNOWN ) { + if( nas && ( nas != nasArray ) ) { + PR_DELETE( nas ); } + return -1; } - rv = (*ss->stuff)(ss, &u.ch, 1); - if (rv < 0) { - return rv; - } - if (flags & FLAG_LEFT) { - while (width-- > 1) { - rv = (*ss->stuff)(ss, " ", 1); - if (rv < 0) { - return rv; - } + + nap = &nas[i-1]; + dolPt = fmt; + c = *fmt++; + } + + /* + * Examine optional flags. Note that we do not implement the + * '#' flag of sprintf(). The ANSI C spec. of the '#' flag is + * somewhat ambiguous and not ideal, which is perhaps why + * the various sprintf() implementations are inconsistent + * on this feature. + */ + while ((c == '-') || (c == '+') || (c == ' ') || (c == '0')) { + if (c == '-') { + flags |= FLAG_LEFT; + } + if (c == '+') { + flags |= FLAG_SIGNED; + } + if (c == ' ') { + flags |= FLAG_SPACED; + } + if (c == '0') { + flags |= FLAG_ZEROS; + } + c = *fmt++; + } + if (flags & FLAG_SIGNED) { + flags &= ~FLAG_SPACED; + } + if (flags & FLAG_LEFT) { + flags &= ~FLAG_ZEROS; + } + + /* width */ + if (c == '*') { + c = *fmt++; + width = va_arg(ap, int); + } else { + width = 0; + while ((c >= '0') && (c <= '9')) { + width = (width * 10) + (c - '0'); + c = *fmt++; + } + } + + /* precision */ + prec = -1; + if (c == '.') { + c = *fmt++; + if (c == '*') { + c = *fmt++; + prec = va_arg(ap, int); + } else { + prec = 0; + while ((c >= '0') && (c <= '9')) { + prec = (prec * 10) + (c - '0'); + c = *fmt++; } } - break; - - case 'p': - if (sizeof(void *) == sizeof(PRInt32)) { - type = TYPE_UINT32; - } else if (sizeof(void *) == sizeof(PRInt64)) { - type = TYPE_UINT64; - } else if (sizeof(void *) == sizeof(int)) { - type = TYPE_UINTN; - } else { - PR_ASSERT(0); - break; - } - radix = 16; - goto fetch_and_convert; + } + + /* size */ + type = TYPE_INTN; + if (c == 'h') { + type = TYPE_INT16; + c = *fmt++; + } else if (c == 'L') { + /* XXX not quite sure here */ + type = TYPE_INT64; + c = *fmt++; + } else if (c == 'l') { + type = TYPE_INT32; + c = *fmt++; + if (c == 'l') { + type = TYPE_INT64; + c = *fmt++; + } + } else if (c == 'z') { + if (sizeof(size_t) == sizeof(PRInt32)) { + type = TYPE_INT32; + } else if (sizeof(size_t) == sizeof(PRInt64)) { + type = TYPE_INT64; + } + c = *fmt++; + } + + /* format */ + hexp = hex; + switch (c) { + case 'd': case 'i': /* decimal/integer */ + radix = 10; + goto fetch_and_convert; + + case 'o': /* octal */ + radix = 8; + type |= 1; + goto fetch_and_convert; + + case 'u': /* unsigned decimal */ + radix = 10; + type |= 1; + goto fetch_and_convert; + + case 'x': /* unsigned hex */ + radix = 16; + type |= 1; + goto fetch_and_convert; + + case 'X': /* unsigned HEX */ + radix = 16; + hexp = HEX; + type |= 1; + goto fetch_and_convert; + +fetch_and_convert: + switch (type) { + case TYPE_INT16: + u.l = nas ? nap->u.i : va_arg(ap, int); + if (u.l < 0) { + u.l = -u.l; + flags |= FLAG_NEG; + } + goto do_long; + case TYPE_UINT16: + u.l = (nas ? nap->u.i : va_arg(ap, int)) & 0xffff; + goto do_long; + case TYPE_INTN: + u.l = nas ? nap->u.i : va_arg(ap, int); + if (u.l < 0) { + u.l = -u.l; + flags |= FLAG_NEG; + } + goto do_long; + case TYPE_UINTN: + u.l = (long)(nas ? nap->u.ui : va_arg(ap, unsigned int)); + goto do_long; + + case TYPE_INT32: + u.l = nas ? nap->u.i32 : va_arg(ap, PRInt32); + if (u.l < 0) { + u.l = -u.l; + flags |= FLAG_NEG; + } + goto do_long; + case TYPE_UINT32: + u.l = (long)(nas ? nap->u.ui32 : va_arg(ap, PRUint32)); +do_long: + rv = cvt_l(ss, u.l, width, prec, radix, type, flags, hexp); + if (rv < 0) { + return rv; + } + break; + + case TYPE_INT64: + u.ll = nas ? nap->u.ll : va_arg(ap, PRInt64); + if (!LL_GE_ZERO(u.ll)) { + LL_NEG(u.ll, u.ll); + flags |= FLAG_NEG; + } + goto do_longlong; + case TYPE_UINT64: + u.ll = nas ? nap->u.ull : va_arg(ap, PRUint64); +do_longlong: + rv = cvt_ll(ss, u.ll, width, prec, radix, type, flags, hexp); + if (rv < 0) { + return rv; + } + break; + } + break; + + case 'e': + case 'E': + case 'f': + case 'g': + u.d = nas ? nap->u.d : va_arg(ap, double); + if( nas != NULL ) { + i = fmt - dolPt; + if( i < sizeof( pattern ) ) { + pattern[0] = '%'; + memcpy( &pattern[1], dolPt, i ); + rv = cvt_f(ss, u.d, pattern, &pattern[i+1] ); + } + } else { + rv = cvt_f(ss, u.d, fmt0, fmt); + } + + if (rv < 0) { + return rv; + } + break; + + case 'c': + u.ch = nas ? nap->u.i : va_arg(ap, int); + if ((flags & FLAG_LEFT) == 0) { + while (width-- > 1) { + rv = (*ss->stuff)(ss, " ", 1); + if (rv < 0) { + return rv; + } + } + } + rv = (*ss->stuff)(ss, &u.ch, 1); + if (rv < 0) { + return rv; + } + if (flags & FLAG_LEFT) { + while (width-- > 1) { + rv = (*ss->stuff)(ss, " ", 1); + if (rv < 0) { + return rv; + } + } + } + break; + + case 'p': + if (sizeof(void *) == sizeof(PRInt32)) { + type = TYPE_UINT32; + } else if (sizeof(void *) == sizeof(PRInt64)) { + type = TYPE_UINT64; + } else if (sizeof(void *) == sizeof(int)) { + type = TYPE_UINTN; + } else { + PR_ASSERT(0); + break; + } + radix = 16; + goto fetch_and_convert; #ifndef WIN32 - case 'S': - /* XXX not supported I suppose */ - PR_ASSERT(0); - break; + case 'S': + /* XXX not supported I suppose */ + PR_ASSERT(0); + break; #endif #if 0 - case 'C': - case 'E': - case 'G': - /* XXX not supported I suppose */ - PR_ASSERT(0); - break; + case 'C': + case 'E': + case 'G': + /* XXX not supported I suppose */ + PR_ASSERT(0); + break; #endif #ifdef WIN32 - case 'S': - u.ws = nas ? nap->u.ws : va_arg(ap, const WCHAR*); - - /* Get the required size in rv */ - rv = WideCharToMultiByte(CP_ACP, 0, u.ws, -1, NULL, 0, NULL, NULL); - if (rv == 0) - rv = 1; - pBuf = PR_MALLOC(rv); - WideCharToMultiByte(CP_ACP, 0, u.ws, -1, pBuf, (int)rv, NULL, NULL); - pBuf[rv-1] = '\0'; - - rv = cvt_s(ss, pBuf, width, prec, flags); - - /* We don't need the allocated buffer anymore */ - PR_Free(pBuf); - if (rv < 0) { - return rv; - } - break; + case 'S': + u.ws = nas ? nap->u.ws : va_arg(ap, const WCHAR*); + + /* Get the required size in rv */ + rv = WideCharToMultiByte(CP_ACP, 0, u.ws, -1, NULL, 0, NULL, NULL); + if (rv == 0) { + rv = 1; + } + pBuf = PR_MALLOC(rv); + WideCharToMultiByte(CP_ACP, 0, u.ws, -1, pBuf, (int)rv, NULL, NULL); + pBuf[rv-1] = '\0'; + + rv = cvt_s(ss, pBuf, width, prec, flags); + + /* We don't need the allocated buffer anymore */ + PR_Free(pBuf); + if (rv < 0) { + return rv; + } + break; #endif - case 's': - u.s = nas ? nap->u.s : va_arg(ap, const char*); - rv = cvt_s(ss, u.s, width, prec, flags); - if (rv < 0) { - return rv; - } - break; - - case 'n': - u.ip = nas ? nap->u.ip : va_arg(ap, int*); - if (u.ip) { - *u.ip = ss->cur - ss->base; - } - break; - - default: - /* Not a % token after all... skip it */ + case 's': + u.s = nas ? nap->u.s : va_arg(ap, const char*); + rv = cvt_s(ss, u.s, width, prec, flags); + if (rv < 0) { + return rv; + } + break; + + case 'n': + u.ip = nas ? nap->u.ip : va_arg(ap, int*); + if (u.ip) { + *u.ip = ss->cur - ss->base; + } + break; + + default: + /* Not a % token after all... skip it */ #if 0 - PR_ASSERT(0); + PR_ASSERT(0); #endif - rv = (*ss->stuff)(ss, "%", 1); - if (rv < 0) { - return rv; - } - rv = (*ss->stuff)(ss, fmt - 1, 1); - if (rv < 0) { - return rv; - } - } + rv = (*ss->stuff)(ss, "%", 1); + if (rv < 0) { + return rv; + } + rv = (*ss->stuff)(ss, fmt - 1, 1); + if (rv < 0) { + return rv; + } + } } /* Stuff trailing NUL */ rv = (*ss->stuff)(ss, "\0", 1); - if( nas && ( nas != nasArray ) ){ - PR_DELETE( nas ); + if( nas && ( nas != nasArray ) ) { + PR_DELETE( nas ); } return rv; @@ -1065,18 +1090,18 @@ static int FuncStuff(SprintfState *ss, const char *sp, PRUint32 len) ** if ss->maxlen + len would overflow or be greater than PR_INT32_MAX. */ if (PR_UINT32_MAX - ss->maxlen < len || ss->maxlen + len > PR_INT32_MAX) { - return -1; + return -1; } rv = (*ss->func)(ss->arg, sp, len); if (rv < 0) { - return rv; + return rv; } ss->maxlen += len; return 0; } -PR_IMPLEMENT(PRUint32) PR_sxprintf(PRStuffFunc func, void *arg, - const char *fmt, ...) +PR_IMPLEMENT(PRUint32) PR_sxprintf(PRStuffFunc func, void *arg, + const char *fmt, ...) { va_list ap; PRUint32 rv; @@ -1087,8 +1112,8 @@ PR_IMPLEMENT(PRUint32) PR_sxprintf(PRStuffFunc func, void *arg, return rv; } -PR_IMPLEMENT(PRUint32) PR_vsxprintf(PRStuffFunc func, void *arg, - const char *fmt, va_list ap) +PR_IMPLEMENT(PRUint32) PR_vsxprintf(PRStuffFunc func, void *arg, + const char *fmt, va_list ap) { SprintfState ss; int rv; @@ -1113,38 +1138,38 @@ static int GrowStuff(SprintfState *ss, const char *sp, PRUint32 len) off = ss->cur - ss->base; if (PR_UINT32_MAX - len < off) { - /* off + len would be too big. */ - return -1; + /* off + len would be too big. */ + return -1; } if (off + len >= ss->maxlen) { - /* Grow the buffer */ - PRUint32 increment = (len > 32) ? len : 32; - if (PR_UINT32_MAX - ss->maxlen < increment) { - /* ss->maxlen + increment would overflow. */ - return -1; - } - newlen = ss->maxlen + increment; - if (newlen > PR_INT32_MAX) { - return -1; - } - if (ss->base) { - newbase = (char*) PR_REALLOC(ss->base, newlen); - } else { - newbase = (char*) PR_MALLOC(newlen); - } - if (!newbase) { - /* Ran out of memory */ - return -1; - } - ss->base = newbase; - ss->maxlen = newlen; - ss->cur = ss->base + off; + /* Grow the buffer */ + PRUint32 increment = (len > 32) ? len : 32; + if (PR_UINT32_MAX - ss->maxlen < increment) { + /* ss->maxlen + increment would overflow. */ + return -1; + } + newlen = ss->maxlen + increment; + if (newlen > PR_INT32_MAX) { + return -1; + } + if (ss->base) { + newbase = (char*) PR_REALLOC(ss->base, newlen); + } else { + newbase = (char*) PR_MALLOC(newlen); + } + if (!newbase) { + /* Ran out of memory */ + return -1; + } + ss->base = newbase; + ss->maxlen = newlen; + ss->cur = ss->base + off; } /* Copy data */ while (len) { - --len; - *ss->cur++ = *sp++; + --len; + *ss->cur++ = *sp++; } PR_ASSERT((PRUint32)(ss->cur - ss->base) <= ss->maxlen); return 0; @@ -1169,7 +1194,7 @@ PR_IMPLEMENT(char *) PR_smprintf(const char *fmt, ...) */ PR_IMPLEMENT(void) PR_smprintf_free(char *mem) { - PR_DELETE(mem); + PR_DELETE(mem); } PR_IMPLEMENT(char *) PR_vsmprintf(const char *fmt, va_list ap) @@ -1183,10 +1208,10 @@ PR_IMPLEMENT(char *) PR_vsmprintf(const char *fmt, va_list ap) ss.maxlen = 0; rv = dosprintf(&ss, fmt, ap); if (rv < 0) { - if (ss.base) { - PR_DELETE(ss.base); - } - return 0; + if (ss.base) { + PR_DELETE(ss.base); + } + return 0; } return ss.base; } @@ -1199,11 +1224,11 @@ static int LimitStuff(SprintfState *ss, const char *sp, PRUint32 len) PRUint32 limit = ss->maxlen - (ss->cur - ss->base); if (len > limit) { - len = limit; + len = limit; } while (len) { - --len; - *ss->cur++ = *sp++; + --len; + *ss->cur++ = *sp++; } return 0; } @@ -1224,14 +1249,14 @@ PR_IMPLEMENT(PRUint32) PR_snprintf(char *out, PRUint32 outlen, const char *fmt, } PR_IMPLEMENT(PRUint32) PR_vsnprintf(char *out, PRUint32 outlen,const char *fmt, - va_list ap) + va_list ap) { SprintfState ss; PRUint32 n; PR_ASSERT(outlen != 0 && outlen <= PR_INT32_MAX); if (outlen == 0 || outlen > PR_INT32_MAX) { - return 0; + return 0; } ss.stuff = LimitStuff; @@ -1241,8 +1266,9 @@ PR_IMPLEMENT(PRUint32) PR_vsnprintf(char *out, PRUint32 outlen,const char *fmt, (void) dosprintf(&ss, fmt, ap); /* If we added chars, and we didn't append a null, do it now. */ - if( (ss.cur != ss.base) && (*(ss.cur - 1) != '\0') ) + if( (ss.cur != ss.base) && (*(ss.cur - 1) != '\0') ) { *(ss.cur - 1) = '\0'; + } n = ss.cur - ss.base; return n ? n - 1 : n; @@ -1266,24 +1292,24 @@ PR_IMPLEMENT(char *) PR_vsprintf_append(char *last, const char *fmt, va_list ap) ss.stuff = GrowStuff; if (last) { - size_t lastlen = strlen(last); - if (lastlen > PR_INT32_MAX) { - return 0; - } - ss.base = last; - ss.cur = last + lastlen; - ss.maxlen = lastlen; + size_t lastlen = strlen(last); + if (lastlen > PR_INT32_MAX) { + return 0; + } + ss.base = last; + ss.cur = last + lastlen; + ss.maxlen = lastlen; } else { - ss.base = 0; - ss.cur = 0; - ss.maxlen = 0; + ss.base = 0; + ss.cur = 0; + ss.maxlen = 0; } rv = dosprintf(&ss, fmt, ap); if (rv < 0) { - if (ss.base) { - PR_DELETE(ss.base); - } - return 0; + if (ss.base) { + PR_DELETE(ss.base); + } + return 0; } return ss.base; } diff --git a/nsprpub/pr/src/io/prscanf.c b/nsprpub/pr/src/io/prscanf.c index 377a6e6436..9923ea27c7 100644 --- a/nsprpub/pr/src/io/prscanf.c +++ b/nsprpub/pr/src/io/prscanf.c @@ -30,7 +30,7 @@ typedef int (*_PRGetCharFN)(void *stream); /* * A function that pushes the character 'ch' back to 'stream'. */ -typedef void (*_PRUngetCharFN)(void *stream, int ch); +typedef void (*_PRUngetCharFN)(void *stream, int ch); /* * The size specifier for the integer and floating point number @@ -89,7 +89,7 @@ typedef struct { * 'str' is assumed to be a representation of the integer in * base 'base'. * - * Warning: + * Warning: * - Only handle base 8, 10, and 16. * - No overflow checking. */ @@ -139,7 +139,7 @@ _pr_strtoull(const char *str, char **endptr, int base) cPtr += 2; } else { base = 8; - } + } } PR_ASSERT(base != 0); LL_I2L(base64, base); @@ -230,8 +230,8 @@ GetInt(ScanfState *state, int code) *p++ = ch; GET_IF_WITHIN_WIDTH(state, ch); if (WITHIN_WIDTH(state) - && (ch == 'x' || ch == 'X') - && (base == 0 || base == 16)) { + && (ch == 'x' || ch == 'X') + && (base == 0 || base == 16)) { base = 16; *p++ = ch; GET_IF_WITHIN_WIDTH(state, ch); @@ -365,11 +365,7 @@ GetFloat(ScanfState *state) if (state->sizeSpec == _PR_size_l) { *va_arg(state->ap, PRFloat64 *) = dval; } else if (state->sizeSpec == _PR_size_L) { -#if defined(OSF1) || defined(IRIX) - *va_arg(state->ap, double *) = dval; -#else *va_arg(state->ap, long double *) = dval; -#endif } else { *va_arg(state->ap, float *) = (float) dval; } @@ -482,45 +478,45 @@ Convert(ScanfState *state, const char *fmt) } break; case '[': - { - PRBool complement = PR_FALSE; - const char *closeBracket; - size_t n; + { + PRBool complement = PR_FALSE; + const char *closeBracket; + size_t n; - if (*++cPtr == '^') { - complement = PR_TRUE; - cPtr++; - } - closeBracket = strchr(*cPtr == ']' ? cPtr + 1 : cPtr, ']'); - if (closeBracket == NULL) { - return NULL; - } - n = closeBracket - cPtr; - if (state->width == 0) { - state->width = INT_MAX; - } - if (state->assign) { - cArg = va_arg(state->ap, char *); - } - for (; state->width > 0; state->width--) { - ch = GET(state); - if ((ch == EOF) - || (!complement && !memchr(cPtr, ch, n)) - || (complement && memchr(cPtr, ch, n))) { - UNGET(state, ch); - break; - } - if (state->assign) { - *cArg++ = ch; - } + if (*++cPtr == '^') { + complement = PR_TRUE; + cPtr++; + } + closeBracket = strchr(*cPtr == ']' ? cPtr + 1 : cPtr, ']'); + if (closeBracket == NULL) { + return NULL; + } + n = closeBracket - cPtr; + if (state->width == 0) { + state->width = INT_MAX; + } + if (state->assign) { + cArg = va_arg(state->ap, char *); + } + for (; state->width > 0; state->width--) { + ch = GET(state); + if ((ch == EOF) + || (!complement && !memchr(cPtr, ch, n)) + || (complement && memchr(cPtr, ch, n))) { + UNGET(state, ch); + break; } if (state->assign) { - *cArg = '\0'; - state->converted = PR_TRUE; + *cArg++ = ch; } - cPtr = closeBracket; } - break; + if (state->assign) { + *cArg = '\0'; + state->converted = PR_TRUE; + } + cPtr = closeBracket; + } + break; default: return NULL; } diff --git a/nsprpub/pr/src/io/prsocket.c b/nsprpub/pr/src/io/prsocket.c index 26f7a245d2..e0f917c16a 100644 --- a/nsprpub/pr/src/io/prsocket.c +++ b/nsprpub/pr/src/io/prsocket.c @@ -22,10 +22,10 @@ PRBool IsValidNetAddr(const PRNetAddr *addr) { if ((addr != NULL) #if defined(XP_UNIX) || defined(XP_OS2) - && (addr->raw.family != PR_AF_LOCAL) + && (addr->raw.family != PR_AF_LOCAL) #endif - && (addr->raw.family != PR_AF_INET6) - && (addr->raw.family != PR_AF_INET)) { + && (addr->raw.family != PR_AF_INET6) + && (addr->raw.family != PR_AF_INET)) { return PR_FALSE; } return PR_TRUE; @@ -39,9 +39,9 @@ static PRBool IsValidNetAddrLen(const PRNetAddr *addr, PRInt32 addr_len) */ if ((addr != NULL) #if defined(XP_UNIX) || defined(XP_OS2) - && (addr->raw.family != AF_UNIX) + && (addr->raw.family != AF_UNIX) #endif - && (PR_NETADDR_SIZE(addr) != addr_len)) { + && (PR_NETADDR_SIZE(addr) != addr_len)) { #if defined(LINUX) && __GLIBC__ == 2 && __GLIBC_MINOR__ == 1 /* * In glibc 2.1, struct sockaddr_in6 is 24 bytes. In glibc 2.2 @@ -51,7 +51,7 @@ static PRBool IsValidNetAddrLen(const PRNetAddr *addr, PRInt32 addr_len) * We need to allow that. (Bugzilla bug #77264) */ if ((PR_AF_INET6 == addr->raw.family) - && (sizeof(addr->ipv6) == addr_len)) { + && (sizeof(addr->ipv6) == addr_len)) { return PR_TRUE; } #endif @@ -74,129 +74,138 @@ static PRBool IsValidNetAddrLen(const PRNetAddr *addr, PRInt32 addr_len) #endif /* DEBUG */ static PRInt32 PR_CALLBACK SocketWritev(PRFileDesc *fd, const PRIOVec *iov, -PRInt32 iov_size, PRIntervalTime timeout) + PRInt32 iov_size, PRIntervalTime timeout) { - PRThread *me = _PR_MD_CURRENT_THREAD(); - int w = 0; - const PRIOVec *tmp_iov; + PRThread *me = _PR_MD_CURRENT_THREAD(); + int w = 0; + const PRIOVec *tmp_iov; #define LOCAL_MAXIOV 8 - PRIOVec local_iov[LOCAL_MAXIOV]; - PRIOVec *iov_copy = NULL; - int tmp_out; - int index, iov_cnt; - int count=0, sz = 0; /* 'count' is the return value. */ - - if (_PR_PENDING_INTERRUPT(me)) { - me->flags &= ~_PR_INTERRUPT; - PR_SetError(PR_PENDING_INTERRUPT_ERROR, 0); - return -1; - } - if (_PR_IO_PENDING(me)) { - PR_SetError(PR_IO_PENDING_ERROR, 0); - return -1; - } + PRIOVec local_iov[LOCAL_MAXIOV]; + PRIOVec *iov_copy = NULL; + int tmp_out; + int index, iov_cnt; + int count=0, sz = 0; /* 'count' is the return value. */ + + if (_PR_PENDING_INTERRUPT(me)) { + me->flags &= ~_PR_INTERRUPT; + PR_SetError(PR_PENDING_INTERRUPT_ERROR, 0); + return -1; + } + if (_PR_IO_PENDING(me)) { + PR_SetError(PR_IO_PENDING_ERROR, 0); + return -1; + } /* * Assume the first writev will succeed. Copy iov's only on * failure. */ tmp_iov = iov; - for (index = 0; index < iov_size; index++) + for (index = 0; index < iov_size; index++) { sz += iov[index].iov_len; + } + + iov_cnt = iov_size; + + while (sz > 0) { + + w = _PR_MD_WRITEV(fd, tmp_iov, iov_cnt, timeout); + if (w < 0) { + count = -1; + break; + } + count += w; + if (fd->secret->nonblocking) { + break; + } + sz -= w; + + if (sz > 0) { + /* find the next unwritten vector */ + for ( index = 0, tmp_out = count; + tmp_out >= iov[index].iov_len; + tmp_out -= iov[index].iov_len, index++) {;} /* nothing to execute */ + + if (tmp_iov == iov) { + /* + * The first writev failed so we + * must copy iov's around. + * Avoid calloc/free if there + * are few enough iov's. + */ + if (iov_size - index <= LOCAL_MAXIOV) { + iov_copy = local_iov; + } + else if ((iov_copy = (PRIOVec *) PR_CALLOC((iov_size - index) * + sizeof *iov_copy)) == NULL) { + PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); + return -1; + } + tmp_iov = iov_copy; + } + + PR_ASSERT(tmp_iov == iov_copy); + + /* fill in the first partial read */ + iov_copy[0].iov_base = &(((char *)iov[index].iov_base)[tmp_out]); + iov_copy[0].iov_len = iov[index].iov_len - tmp_out; + index++; + + /* copy the remaining vectors */ + for (iov_cnt=1; index<iov_size; iov_cnt++, index++) { + iov_copy[iov_cnt].iov_base = iov[index].iov_base; + iov_copy[iov_cnt].iov_len = iov[index].iov_len; + } + } + } - iov_cnt = iov_size; - - while (sz > 0) { - - w = _PR_MD_WRITEV(fd, tmp_iov, iov_cnt, timeout); - if (w < 0) { - count = -1; - break; - } - count += w; - if (fd->secret->nonblocking) { - break; - } - sz -= w; - - if (sz > 0) { - /* find the next unwritten vector */ - for ( index = 0, tmp_out = count; - tmp_out >= iov[index].iov_len; - tmp_out -= iov[index].iov_len, index++){;} /* nothing to execute */ - - if (tmp_iov == iov) { - /* - * The first writev failed so we - * must copy iov's around. - * Avoid calloc/free if there - * are few enough iov's. - */ - if (iov_size - index <= LOCAL_MAXIOV) - iov_copy = local_iov; - else if ((iov_copy = (PRIOVec *) PR_CALLOC((iov_size - index) * - sizeof *iov_copy)) == NULL) { - PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); - return -1; - } - tmp_iov = iov_copy; - } - - PR_ASSERT(tmp_iov == iov_copy); - - /* fill in the first partial read */ - iov_copy[0].iov_base = &(((char *)iov[index].iov_base)[tmp_out]); - iov_copy[0].iov_len = iov[index].iov_len - tmp_out; - index++; - - /* copy the remaining vectors */ - for (iov_cnt=1; index<iov_size; iov_cnt++, index++) { - iov_copy[iov_cnt].iov_base = iov[index].iov_base; - iov_copy[iov_cnt].iov_len = iov[index].iov_len; - } - } - } - - if (iov_copy != local_iov) - PR_DELETE(iov_copy); - return count; + if (iov_copy != local_iov) { + PR_DELETE(iov_copy); + } + return count; } /************************************************************************/ PR_IMPLEMENT(PRFileDesc *) PR_ImportTCPSocket(PROsfd osfd) { -PRFileDesc *fd; + PRFileDesc *fd; - if (!_pr_initialized) _PR_ImplicitInitialization(); - fd = PR_AllocFileDesc(osfd, PR_GetTCPMethods()); - if (fd != NULL) { - _PR_MD_MAKE_NONBLOCK(fd); - _PR_MD_INIT_FD_INHERITABLE(fd, PR_TRUE); + if (!_pr_initialized) { + _PR_ImplicitInitialization(); + } + fd = PR_AllocFileDesc(osfd, PR_GetTCPMethods()); + if (fd != NULL) { + _PR_MD_MAKE_NONBLOCK(fd); + _PR_MD_INIT_FD_INHERITABLE(fd, PR_TRUE); #ifdef _PR_NEED_SECRET_AF - /* this means we can only import IPv4 sockets here. - * but this is what the function in ptio.c does. - * We need a way to import IPv6 sockets, too. - */ - fd->secret->af = AF_INET; + /* this means we can only import IPv4 sockets here. + * but this is what the function in ptio.c does. + * We need a way to import IPv6 sockets, too. + */ + fd->secret->af = AF_INET; #endif - } else - _PR_MD_CLOSE_SOCKET(osfd); - return(fd); + } else { + _PR_MD_CLOSE_SOCKET(osfd); + } + return(fd); } PR_IMPLEMENT(PRFileDesc *) PR_ImportUDPSocket(PROsfd osfd) { -PRFileDesc *fd; - - if (!_pr_initialized) _PR_ImplicitInitialization(); - fd = PR_AllocFileDesc(osfd, PR_GetUDPMethods()); - if (fd != NULL) { - _PR_MD_MAKE_NONBLOCK(fd); - _PR_MD_INIT_FD_INHERITABLE(fd, PR_TRUE); - } else - _PR_MD_CLOSE_SOCKET(osfd); - return(fd); + PRFileDesc *fd; + + if (!_pr_initialized) { + _PR_ImplicitInitialization(); + } + fd = PR_AllocFileDesc(osfd, PR_GetUDPMethods()); + if (fd != NULL) { + _PR_MD_MAKE_NONBLOCK(fd); + _PR_MD_INIT_FD_INHERITABLE(fd, PR_TRUE); + } else { + _PR_MD_CLOSE_SOCKET(osfd); + } + return(fd); } @@ -206,16 +215,20 @@ PR_IMPLEMENT(PRFileDesc*) PR_CreateSocketPollFd(PROsfd osfd) { PRFileDesc *fd; - if (!_pr_initialized) _PR_ImplicitInitialization(); + if (!_pr_initialized) { + _PR_ImplicitInitialization(); + } fd = _PR_Getfd(); - if (fd == NULL) PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); + if (fd == NULL) { + PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); + } else { fd->secret->md.osfd = osfd; fd->secret->inheritable = _PR_TRI_FALSE; - fd->secret->state = _PR_FILEDESC_OPEN; + fd->secret->state = _PR_FILEDESC_OPEN; fd->methods = PR_GetSocketPollFdMethods(); } @@ -237,32 +250,34 @@ PR_IMPLEMENT(PRStatus) PR_DestroySocketPollFd(PRFileDesc *fd) static PRStatus PR_CALLBACK SocketConnect( PRFileDesc *fd, const PRNetAddr *addr, PRIntervalTime timeout) { - PRInt32 rv; /* Return value of _PR_MD_CONNECT */ + PRInt32 rv; /* Return value of _PR_MD_CONNECT */ const PRNetAddr *addrp = addr; #if defined(_PR_INET6) - PRNetAddr addrCopy; + PRNetAddr addrCopy; #endif - PRThread *me = _PR_MD_CURRENT_THREAD(); + PRThread *me = _PR_MD_CURRENT_THREAD(); - if (_PR_PENDING_INTERRUPT(me)) { - me->flags &= ~_PR_INTERRUPT; - PR_SetError(PR_PENDING_INTERRUPT_ERROR, 0); - return PR_FAILURE; - } + if (_PR_PENDING_INTERRUPT(me)) { + me->flags &= ~_PR_INTERRUPT; + PR_SetError(PR_PENDING_INTERRUPT_ERROR, 0); + return PR_FAILURE; + } #if defined(_PR_INET6) - if (addr->raw.family == PR_AF_INET6) { - addrCopy = *addr; - addrCopy.raw.family = AF_INET6; - addrp = &addrCopy; - } + if (addr->raw.family == PR_AF_INET6) { + addrCopy = *addr; + addrCopy.raw.family = AF_INET6; + addrp = &addrCopy; + } #endif - rv = _PR_MD_CONNECT(fd, addrp, PR_NETADDR_SIZE(addr), timeout); - PR_LOG(_pr_io_lm, PR_LOG_MAX, ("connect -> %d", rv)); - if (rv == 0) - return PR_SUCCESS; - else - return PR_FAILURE; + rv = _PR_MD_CONNECT(fd, addrp, PR_NETADDR_SIZE(addr), timeout); + PR_LOG(_pr_io_lm, PR_LOG_MAX, ("connect -> %d", rv)); + if (rv == 0) { + return PR_SUCCESS; + } + else { + return PR_FAILURE; + } } static PRStatus PR_CALLBACK SocketConnectContinue( @@ -297,7 +312,7 @@ static PRStatus PR_CALLBACK SocketConnectContinue( if (out_flags & PR_POLL_EXCEPT) { int len = sizeof(err); if (getsockopt(osfd, (int)SOL_SOCKET, SO_ERROR, (char *) &err, &len) - == SOCKET_ERROR) { + == SOCKET_ERROR) { _PR_MD_MAP_GETSOCKOPT_ERROR(WSAGetLastError()); return PR_FAILURE; } @@ -342,7 +357,7 @@ static PRStatus PR_CALLBACK SocketConnectContinue( if (GetOverlappedResult(osfd, &fd->secret->ol, &rvSent, FALSE) == TRUE) { fd->secret->overlappedActive = PR_FALSE; PR_LOG(_pr_io_lm, PR_LOG_MIN, - ("SocketConnectContinue GetOverlappedResult succeeded\n")); + ("SocketConnectContinue GetOverlappedResult succeeded\n")); /* When ConnectEx is used, all previously set socket options and * property are not enabled and to enable them * SO_UPDATE_CONNECT_CONTEXT option need to be set. */ @@ -357,7 +372,7 @@ static PRStatus PR_CALLBACK SocketConnectContinue( } else { err = WSAGetLastError(); PR_LOG(_pr_io_lm, PR_LOG_MIN, - ("SocketConnectContinue GetOverlappedResult failed %d\n", err)); + ("SocketConnectContinue GetOverlappedResult failed %d\n", err)); if (err != ERROR_IO_INCOMPLETE) { _PR_MD_MAP_CONNECT_ERROR(err); fd->secret->overlappedActive = PR_FALSE; @@ -381,25 +396,6 @@ static PRStatus PR_CALLBACK SocketConnectContinue( } return PR_SUCCESS; -#elif defined(XP_BEOS) - -#ifdef BONE_VERSION /* bug 122364 */ - /* temporary workaround until getsockopt(SO_ERROR) works in BONE */ - if (out_flags & PR_POLL_EXCEPT) { - PR_SetError(PR_CONNECT_REFUSED_ERROR, 0); - return PR_FAILURE; - } - PR_ASSERT(out_flags & PR_POLL_WRITE); - return PR_SUCCESS; -#else - err = _MD_beos_get_nonblocking_connect_error(fd); - if( err != 0 ) { - _PR_MD_MAP_CONNECT_ERROR(err); - return PR_FAILURE; - } - else - return PR_SUCCESS; -#endif /* BONE_VERSION */ #else PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0); @@ -420,402 +416,405 @@ PR_IMPLEMENT(PRStatus) PR_GetConnectStatus(const PRPollDesc *pd) } static PRFileDesc* PR_CALLBACK SocketAccept(PRFileDesc *fd, PRNetAddr *addr, -PRIntervalTime timeout) + PRIntervalTime timeout) { - PROsfd osfd; - PRFileDesc *fd2; - PRUint32 al; - PRThread *me = _PR_MD_CURRENT_THREAD(); + PROsfd osfd; + PRFileDesc *fd2; + PRUint32 al; + PRThread *me = _PR_MD_CURRENT_THREAD(); #ifdef WINNT - PRNetAddr addrCopy; + PRNetAddr addrCopy; #endif - if (_PR_PENDING_INTERRUPT(me)) { - me->flags &= ~_PR_INTERRUPT; - PR_SetError(PR_PENDING_INTERRUPT_ERROR, 0); - return 0; - } - if (_PR_IO_PENDING(me)) { - PR_SetError(PR_IO_PENDING_ERROR, 0); - return 0; - } + if (_PR_PENDING_INTERRUPT(me)) { + me->flags &= ~_PR_INTERRUPT; + PR_SetError(PR_PENDING_INTERRUPT_ERROR, 0); + return 0; + } + if (_PR_IO_PENDING(me)) { + PR_SetError(PR_IO_PENDING_ERROR, 0); + return 0; + } #ifdef WINNT - if (addr == NULL) { - addr = &addrCopy; - } + if (addr == NULL) { + addr = &addrCopy; + } #endif - al = sizeof(PRNetAddr); - osfd = _PR_MD_ACCEPT(fd, addr, &al, timeout); - if (osfd == -1) - return 0; - - fd2 = PR_AllocFileDesc(osfd, PR_GetTCPMethods()); - if (!fd2) { - _PR_MD_CLOSE_SOCKET(osfd); - return NULL; - } - - fd2->secret->nonblocking = fd->secret->nonblocking; - fd2->secret->inheritable = fd->secret->inheritable; + al = sizeof(PRNetAddr); + osfd = _PR_MD_ACCEPT(fd, addr, &al, timeout); + if (osfd == -1) { + return 0; + } + + fd2 = PR_AllocFileDesc(osfd, PR_GetTCPMethods()); + if (!fd2) { + _PR_MD_CLOSE_SOCKET(osfd); + return NULL; + } + + fd2->secret->nonblocking = fd->secret->nonblocking; + fd2->secret->inheritable = fd->secret->inheritable; #ifdef WINNT - if (!fd2->secret->nonblocking && fd2->secret->inheritable != _PR_TRI_TRUE) { - /* - * The new socket has been associated with an I/O - * completion port. There is no going back. - */ - fd2->secret->md.io_model_committed = PR_TRUE; - } - PR_ASSERT(al == PR_NETADDR_SIZE(addr)); - fd2->secret->md.accepted_socket = PR_TRUE; - memcpy(&fd2->secret->md.peer_addr, addr, al); + if (!fd2->secret->nonblocking && fd2->secret->inheritable != _PR_TRI_TRUE) { + /* + * The new socket has been associated with an I/O + * completion port. There is no going back. + */ + fd2->secret->md.io_model_committed = PR_TRUE; + } + PR_ASSERT(al == PR_NETADDR_SIZE(addr)); + fd2->secret->md.accepted_socket = PR_TRUE; + memcpy(&fd2->secret->md.peer_addr, addr, al); #endif - /* - * On some platforms, the new socket created by accept() - * inherits the nonblocking (or overlapped io) attribute - * of the listening socket. As an optimization, these - * platforms can skip the following _PR_MD_MAKE_NONBLOCK - * call. - */ -#if !defined(SOLARIS) && !defined(IRIX) && !defined(WINNT) - _PR_MD_MAKE_NONBLOCK(fd2); + /* + * On some platforms, the new socket created by accept() + * inherits the nonblocking (or overlapped io) attribute + * of the listening socket. As an optimization, these + * platforms can skip the following _PR_MD_MAKE_NONBLOCK + * call. + */ +#if !defined(SOLARIS) && !defined(WINNT) + _PR_MD_MAKE_NONBLOCK(fd2); #endif #ifdef _PR_INET6 - if (addr && (AF_INET6 == addr->raw.family)) + if (addr && (AF_INET6 == addr->raw.family)) { addr->raw.family = PR_AF_INET6; + } #endif - PR_ASSERT(IsValidNetAddr(addr) == PR_TRUE); - PR_ASSERT(IsValidNetAddrLen(addr, al) == PR_TRUE); + PR_ASSERT(IsValidNetAddr(addr) == PR_TRUE); + PR_ASSERT(IsValidNetAddrLen(addr, al) == PR_TRUE); - return fd2; + return fd2; } #ifdef WINNT PR_IMPLEMENT(PRFileDesc*) PR_NTFast_Accept(PRFileDesc *fd, PRNetAddr *addr, -PRIntervalTime timeout) + PRIntervalTime timeout) { - PROsfd osfd; - PRFileDesc *fd2; - PRIntn al; - PRThread *me = _PR_MD_CURRENT_THREAD(); - PRNetAddr addrCopy; - - if (_PR_PENDING_INTERRUPT(me)) { - me->flags &= ~_PR_INTERRUPT; - PR_SetError(PR_PENDING_INTERRUPT_ERROR, 0); - return 0; - } - if (_PR_IO_PENDING(me)) { - PR_SetError(PR_IO_PENDING_ERROR, 0); - return 0; - } - - if (addr == NULL) { - addr = &addrCopy; - } - al = PR_NETADDR_SIZE(addr); - osfd = _PR_MD_FAST_ACCEPT(fd, addr, &al, timeout, PR_TRUE, NULL, NULL); - if (osfd == -1) { - return 0; - } - - fd2 = PR_AllocFileDesc(osfd, PR_GetTCPMethods()); - if (!fd2) { - _PR_MD_CLOSE_SOCKET(osfd); - } else { - fd2->secret->nonblocking = fd->secret->nonblocking; - fd2->secret->md.io_model_committed = PR_TRUE; - PR_ASSERT(al == PR_NETADDR_SIZE(addr)); - fd2->secret->md.accepted_socket = PR_TRUE; - memcpy(&fd2->secret->md.peer_addr, addr, al); + PROsfd osfd; + PRFileDesc *fd2; + PRIntn al; + PRThread *me = _PR_MD_CURRENT_THREAD(); + PRNetAddr addrCopy; + + if (_PR_PENDING_INTERRUPT(me)) { + me->flags &= ~_PR_INTERRUPT; + PR_SetError(PR_PENDING_INTERRUPT_ERROR, 0); + return 0; + } + if (_PR_IO_PENDING(me)) { + PR_SetError(PR_IO_PENDING_ERROR, 0); + return 0; + } + + if (addr == NULL) { + addr = &addrCopy; + } + al = PR_NETADDR_SIZE(addr); + osfd = _PR_MD_FAST_ACCEPT(fd, addr, &al, timeout, PR_TRUE, NULL, NULL); + if (osfd == -1) { + return 0; + } + + fd2 = PR_AllocFileDesc(osfd, PR_GetTCPMethods()); + if (!fd2) { + _PR_MD_CLOSE_SOCKET(osfd); + } else { + fd2->secret->nonblocking = fd->secret->nonblocking; + fd2->secret->md.io_model_committed = PR_TRUE; + PR_ASSERT(al == PR_NETADDR_SIZE(addr)); + fd2->secret->md.accepted_socket = PR_TRUE; + memcpy(&fd2->secret->md.peer_addr, addr, al); #ifdef _PR_INET6 - if (AF_INET6 == addr->raw.family) - addr->raw.family = PR_AF_INET6; + if (AF_INET6 == addr->raw.family) { + addr->raw.family = PR_AF_INET6; + } #endif #ifdef _PR_NEED_SECRET_AF - fd2->secret->af = fd->secret->af; + fd2->secret->af = fd->secret->af; #endif - } - return fd2; + } + return fd2; } #endif /* WINNT */ static PRStatus PR_CALLBACK SocketBind(PRFileDesc *fd, const PRNetAddr *addr) { - PRInt32 result; + PRInt32 result; const PRNetAddr *addrp = addr; #if defined(_PR_INET6) - PRNetAddr addrCopy; + PRNetAddr addrCopy; #endif - PR_ASSERT(IsValidNetAddr(addr) == PR_TRUE); + PR_ASSERT(IsValidNetAddr(addr) == PR_TRUE); #ifdef XP_UNIX - if (addr->raw.family == AF_UNIX) { - /* Disallow relative pathnames */ - if (addr->local.path[0] != '/') { - PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0); - return PR_FAILURE; - } - } + if (addr->raw.family == AF_UNIX) { + /* Disallow relative pathnames */ + if (addr->local.path[0] != '/') { + PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0); + return PR_FAILURE; + } + } #endif /* XP_UNIX */ #if defined(_PR_INET6) - if (addr->raw.family == PR_AF_INET6) { - addrCopy = *addr; - addrCopy.raw.family = AF_INET6; - addrp = &addrCopy; - } + if (addr->raw.family == PR_AF_INET6) { + addrCopy = *addr; + addrCopy.raw.family = AF_INET6; + addrp = &addrCopy; + } #endif - result = _PR_MD_BIND(fd, addrp, PR_NETADDR_SIZE(addr)); - if (result < 0) { - return PR_FAILURE; - } - return PR_SUCCESS; + result = _PR_MD_BIND(fd, addrp, PR_NETADDR_SIZE(addr)); + if (result < 0) { + return PR_FAILURE; + } + return PR_SUCCESS; } static PRStatus PR_CALLBACK SocketListen(PRFileDesc *fd, PRIntn backlog) { - PRInt32 result; + PRInt32 result; - result = _PR_MD_LISTEN(fd, backlog); - if (result < 0) { - return PR_FAILURE; - } - return PR_SUCCESS; + result = _PR_MD_LISTEN(fd, backlog); + if (result < 0) { + return PR_FAILURE; + } + return PR_SUCCESS; } static PRStatus PR_CALLBACK SocketShutdown(PRFileDesc *fd, PRIntn how) { - PRInt32 result; + PRInt32 result; - result = _PR_MD_SHUTDOWN(fd, how); - if (result < 0) { - return PR_FAILURE; - } - return PR_SUCCESS; + result = _PR_MD_SHUTDOWN(fd, how); + if (result < 0) { + return PR_FAILURE; + } + return PR_SUCCESS; } static PRInt32 PR_CALLBACK SocketRecv(PRFileDesc *fd, void *buf, PRInt32 amount, PRIntn flags, -PRIntervalTime timeout) + PRIntervalTime timeout) { - PRInt32 rv; - PRThread *me = _PR_MD_CURRENT_THREAD(); - - if ((flags != 0) && (flags != PR_MSG_PEEK)) { - PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0); - return -1; - } - if (_PR_PENDING_INTERRUPT(me)) { - me->flags &= ~_PR_INTERRUPT; - PR_SetError(PR_PENDING_INTERRUPT_ERROR, 0); - return -1; - } - if (_PR_IO_PENDING(me)) { - PR_SetError(PR_IO_PENDING_ERROR, 0); - return -1; - } - - PR_LOG(_pr_io_lm, PR_LOG_MAX, - ("recv: fd=%p osfd=%" PR_PRIdOSFD " buf=%p amount=%d flags=%d", - fd, fd->secret->md.osfd, buf, amount, flags)); + PRInt32 rv; + PRThread *me = _PR_MD_CURRENT_THREAD(); + + if ((flags != 0) && (flags != PR_MSG_PEEK)) { + PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0); + return -1; + } + if (_PR_PENDING_INTERRUPT(me)) { + me->flags &= ~_PR_INTERRUPT; + PR_SetError(PR_PENDING_INTERRUPT_ERROR, 0); + return -1; + } + if (_PR_IO_PENDING(me)) { + PR_SetError(PR_IO_PENDING_ERROR, 0); + return -1; + } + + PR_LOG(_pr_io_lm, PR_LOG_MAX, + ("recv: fd=%p osfd=%" PR_PRIdOSFD " buf=%p amount=%d flags=%d", + fd, fd->secret->md.osfd, buf, amount, flags)); #ifdef _PR_HAVE_PEEK_BUFFER - if (fd->secret->peekBytes != 0) { - rv = (amount < fd->secret->peekBytes) ? - amount : fd->secret->peekBytes; - memcpy(buf, fd->secret->peekBuffer, rv); - if (flags == 0) { - /* consume the bytes in the peek buffer */ - fd->secret->peekBytes -= rv; - if (fd->secret->peekBytes != 0) { - memmove(fd->secret->peekBuffer, - fd->secret->peekBuffer + rv, - fd->secret->peekBytes); - } - } - return rv; - } - - /* allocate peek buffer, if necessary */ - if ((PR_MSG_PEEK == flags) && _PR_FD_NEED_EMULATE_MSG_PEEK(fd)) { - PR_ASSERT(0 == fd->secret->peekBytes); - /* impose a max size on the peek buffer */ - if (amount > _PR_PEEK_BUFFER_MAX) { - amount = _PR_PEEK_BUFFER_MAX; - } - if (fd->secret->peekBufSize < amount) { - if (fd->secret->peekBuffer) { - PR_Free(fd->secret->peekBuffer); - } - fd->secret->peekBufSize = amount; - fd->secret->peekBuffer = PR_Malloc(amount); - if (NULL == fd->secret->peekBuffer) { - fd->secret->peekBufSize = 0; - PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); - return -1; - } - } - } + if (fd->secret->peekBytes != 0) { + rv = (amount < fd->secret->peekBytes) ? + amount : fd->secret->peekBytes; + memcpy(buf, fd->secret->peekBuffer, rv); + if (flags == 0) { + /* consume the bytes in the peek buffer */ + fd->secret->peekBytes -= rv; + if (fd->secret->peekBytes != 0) { + memmove(fd->secret->peekBuffer, + fd->secret->peekBuffer + rv, + fd->secret->peekBytes); + } + } + return rv; + } + + /* allocate peek buffer, if necessary */ + if ((PR_MSG_PEEK == flags) && _PR_FD_NEED_EMULATE_MSG_PEEK(fd)) { + PR_ASSERT(0 == fd->secret->peekBytes); + /* impose a max size on the peek buffer */ + if (amount > _PR_PEEK_BUFFER_MAX) { + amount = _PR_PEEK_BUFFER_MAX; + } + if (fd->secret->peekBufSize < amount) { + if (fd->secret->peekBuffer) { + PR_Free(fd->secret->peekBuffer); + } + fd->secret->peekBufSize = amount; + fd->secret->peekBuffer = PR_Malloc(amount); + if (NULL == fd->secret->peekBuffer) { + fd->secret->peekBufSize = 0; + PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); + return -1; + } + } + } #endif - rv = _PR_MD_RECV(fd, buf, amount, flags, timeout); - PR_LOG(_pr_io_lm, PR_LOG_MAX, ("recv -> %d, error = %d, os error = %d", - rv, PR_GetError(), PR_GetOSError())); + rv = _PR_MD_RECV(fd, buf, amount, flags, timeout); + PR_LOG(_pr_io_lm, PR_LOG_MAX, ("recv -> %d, error = %d, os error = %d", + rv, PR_GetError(), PR_GetOSError())); #ifdef _PR_HAVE_PEEK_BUFFER - if ((PR_MSG_PEEK == flags) && _PR_FD_NEED_EMULATE_MSG_PEEK(fd)) { - if (rv > 0) { - memcpy(fd->secret->peekBuffer, buf, rv); - fd->secret->peekBytes = rv; - } - } + if ((PR_MSG_PEEK == flags) && _PR_FD_NEED_EMULATE_MSG_PEEK(fd)) { + if (rv > 0) { + memcpy(fd->secret->peekBuffer, buf, rv); + fd->secret->peekBytes = rv; + } + } #endif - return rv; + return rv; } static PRInt32 PR_CALLBACK SocketRead(PRFileDesc *fd, void *buf, PRInt32 amount) { - return SocketRecv(fd, buf, amount, 0, PR_INTERVAL_NO_TIMEOUT); + return SocketRecv(fd, buf, amount, 0, PR_INTERVAL_NO_TIMEOUT); } static PRInt32 PR_CALLBACK SocketSend(PRFileDesc *fd, const void *buf, PRInt32 amount, -PRIntn flags, PRIntervalTime timeout) + PRIntn flags, PRIntervalTime timeout) { - PRInt32 temp, count; - PRThread *me = _PR_MD_CURRENT_THREAD(); - - if (_PR_PENDING_INTERRUPT(me)) { - me->flags &= ~_PR_INTERRUPT; - PR_SetError(PR_PENDING_INTERRUPT_ERROR, 0); - return -1; - } - if (_PR_IO_PENDING(me)) { - PR_SetError(PR_IO_PENDING_ERROR, 0); - return -1; - } - - count = 0; - while (amount > 0) { - PR_LOG(_pr_io_lm, PR_LOG_MAX, - ("send: fd=%p osfd=%" PR_PRIdOSFD " buf=%p amount=%d", - fd, fd->secret->md.osfd, buf, amount)); - temp = _PR_MD_SEND(fd, buf, amount, flags, timeout); - if (temp < 0) { - count = -1; - break; - } - - count += temp; - if (fd->secret->nonblocking) { - break; - } - buf = (const void*) ((const char*)buf + temp); - - amount -= temp; - } - PR_LOG(_pr_io_lm, PR_LOG_MAX, ("send -> %d", count)); - return count; + PRInt32 temp, count; + PRThread *me = _PR_MD_CURRENT_THREAD(); + + if (_PR_PENDING_INTERRUPT(me)) { + me->flags &= ~_PR_INTERRUPT; + PR_SetError(PR_PENDING_INTERRUPT_ERROR, 0); + return -1; + } + if (_PR_IO_PENDING(me)) { + PR_SetError(PR_IO_PENDING_ERROR, 0); + return -1; + } + + count = 0; + while (amount > 0) { + PR_LOG(_pr_io_lm, PR_LOG_MAX, + ("send: fd=%p osfd=%" PR_PRIdOSFD " buf=%p amount=%d", + fd, fd->secret->md.osfd, buf, amount)); + temp = _PR_MD_SEND(fd, buf, amount, flags, timeout); + if (temp < 0) { + count = -1; + break; + } + + count += temp; + if (fd->secret->nonblocking) { + break; + } + buf = (const void*) ((const char*)buf + temp); + + amount -= temp; + } + PR_LOG(_pr_io_lm, PR_LOG_MAX, ("send -> %d", count)); + return count; } static PRInt32 PR_CALLBACK SocketWrite(PRFileDesc *fd, const void *buf, PRInt32 amount) { - return SocketSend(fd, buf, amount, 0, PR_INTERVAL_NO_TIMEOUT); + return SocketSend(fd, buf, amount, 0, PR_INTERVAL_NO_TIMEOUT); } static PRStatus PR_CALLBACK SocketClose(PRFileDesc *fd) { - if (!fd || !fd->secret - || (fd->secret->state != _PR_FILEDESC_OPEN - && fd->secret->state != _PR_FILEDESC_CLOSED)) { - PR_SetError(PR_BAD_DESCRIPTOR_ERROR, 0); - return PR_FAILURE; - } - - if (fd->secret->state == _PR_FILEDESC_OPEN) { + if (!fd || !fd->secret + || (fd->secret->state != _PR_FILEDESC_OPEN + && fd->secret->state != _PR_FILEDESC_CLOSED)) { + PR_SetError(PR_BAD_DESCRIPTOR_ERROR, 0); + return PR_FAILURE; + } + + if (fd->secret->state == _PR_FILEDESC_OPEN) { #if defined(_WIN64) && defined(WIN95) - /* TCP Fast Open on Windows must use ConnectEx, which uses overlapped - * input/output. Before closing such a socket we must cancelIO. - */ - if (fd->secret->overlappedActive) { - PR_ASSERT(fd->secret->nonblocking); - if (CancelIo((HANDLE) fd->secret->md.osfd) == TRUE) { - PR_LOG(_pr_io_lm, PR_LOG_MIN, - ("SocketClose - CancelIo succeeded\n")); - } else { - DWORD err = WSAGetLastError(); - PR_LOG(_pr_io_lm, PR_LOG_MIN, - ("SocketClose - CancelIo failed err=%x\n", err)); - } - - DWORD rvSent; - if (GetOverlappedResult((HANDLE)fd->secret->md.osfd, &fd->secret->ol, &rvSent, FALSE) == TRUE) { - fd->secret->overlappedActive = PR_FALSE; - PR_LOG(_pr_io_lm, PR_LOG_MIN, - ("SocketClose GetOverlappedResult succeeded\n")); - } else { - DWORD err = WSAGetLastError(); - PR_LOG(_pr_io_lm, PR_LOG_MIN, - ("SocketClose GetOverlappedResult failed %d\n", err)); - if (err != ERROR_IO_INCOMPLETE) { - _PR_MD_MAP_CONNECT_ERROR(err); - fd->secret->overlappedActive = PR_FALSE; + /* TCP Fast Open on Windows must use ConnectEx, which uses overlapped + * input/output. Before closing such a socket we must cancelIO. + */ + if (fd->secret->overlappedActive) { + PR_ASSERT(fd->secret->nonblocking); + if (CancelIo((HANDLE) fd->secret->md.osfd) == TRUE) { + PR_LOG(_pr_io_lm, PR_LOG_MIN, + ("SocketClose - CancelIo succeeded\n")); + } else { + DWORD err = WSAGetLastError(); + PR_LOG(_pr_io_lm, PR_LOG_MIN, + ("SocketClose - CancelIo failed err=%x\n", err)); + } + + DWORD rvSent; + if (GetOverlappedResult((HANDLE)fd->secret->md.osfd, &fd->secret->ol, &rvSent, FALSE) == TRUE) { + fd->secret->overlappedActive = PR_FALSE; + PR_LOG(_pr_io_lm, PR_LOG_MIN, + ("SocketClose GetOverlappedResult succeeded\n")); + } else { + DWORD err = WSAGetLastError(); + PR_LOG(_pr_io_lm, PR_LOG_MIN, + ("SocketClose GetOverlappedResult failed %d\n", err)); + if (err != ERROR_IO_INCOMPLETE) { + _PR_MD_MAP_CONNECT_ERROR(err); + fd->secret->overlappedActive = PR_FALSE; + } + } } - } - } - if (fd->secret->overlappedActive && - _fd_waiting_for_overlapped_done_lock) { - // Put osfd into the list to be checked later. - PRFileDescList *forWaiting = PR_NEW(PRFileDescList); - if (!forWaiting) { - PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); - return PR_FAILURE; - } - forWaiting->fd = fd; + if (fd->secret->overlappedActive && + _fd_waiting_for_overlapped_done_lock) { + // Put osfd into the list to be checked later. + PRFileDescList *forWaiting = PR_NEW(PRFileDescList); + if (!forWaiting) { + PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); + return PR_FAILURE; + } + forWaiting->fd = fd; - PR_Lock(_fd_waiting_for_overlapped_done_lock); - forWaiting->next = _fd_waiting_for_overlapped_done; - _fd_waiting_for_overlapped_done = forWaiting; - PR_Unlock(_fd_waiting_for_overlapped_done_lock); + PR_Lock(_fd_waiting_for_overlapped_done_lock); + forWaiting->next = _fd_waiting_for_overlapped_done; + _fd_waiting_for_overlapped_done = forWaiting; + PR_Unlock(_fd_waiting_for_overlapped_done_lock); - return PR_SUCCESS; - } + return PR_SUCCESS; + } #endif - if (_PR_MD_CLOSE_SOCKET(fd->secret->md.osfd) < 0) { - return PR_FAILURE; - } - fd->secret->state = _PR_FILEDESC_CLOSED; - } + if (_PR_MD_CLOSE_SOCKET(fd->secret->md.osfd) < 0) { + return PR_FAILURE; + } + fd->secret->state = _PR_FILEDESC_CLOSED; + } #ifdef _PR_HAVE_PEEK_BUFFER - if (fd->secret->peekBuffer) { - PR_ASSERT(fd->secret->peekBufSize > 0); - PR_DELETE(fd->secret->peekBuffer); - fd->secret->peekBufSize = 0; - fd->secret->peekBytes = 0; - } + if (fd->secret->peekBuffer) { + PR_ASSERT(fd->secret->peekBufSize > 0); + PR_DELETE(fd->secret->peekBuffer); + fd->secret->peekBufSize = 0; + fd->secret->peekBytes = 0; + } #endif - PR_FreeFileDesc(fd); - return PR_SUCCESS; + PR_FreeFileDesc(fd); + return PR_SUCCESS; } static PRInt32 PR_CALLBACK SocketAvailable(PRFileDesc *fd) { - PRInt32 rv; + PRInt32 rv; #ifdef _PR_HAVE_PEEK_BUFFER - if (fd->secret->peekBytes != 0) { - return fd->secret->peekBytes; - } + if (fd->secret->peekBytes != 0) { + return fd->secret->peekBytes; + } #endif - rv = _PR_MD_SOCKETAVAILABLE(fd); - return rv; + rv = _PR_MD_SOCKETAVAILABLE(fd); + return rv; } static PRInt64 PR_CALLBACK SocketAvailable64(PRFileDesc *fd) @@ -828,60 +827,60 @@ static PRInt64 PR_CALLBACK SocketAvailable64(PRFileDesc *fd) } #endif LL_I2L(rv, _PR_MD_SOCKETAVAILABLE(fd)); - return rv; + return rv; } static PRStatus PR_CALLBACK SocketSync(PRFileDesc *fd) { - return PR_SUCCESS; + return PR_SUCCESS; } static PRInt32 PR_CALLBACK SocketSendTo( PRFileDesc *fd, const void *buf, PRInt32 amount, PRIntn flags, const PRNetAddr *addr, PRIntervalTime timeout) { - PRInt32 temp, count; + PRInt32 temp, count; const PRNetAddr *addrp = addr; #if defined(_PR_INET6) - PRNetAddr addrCopy; + PRNetAddr addrCopy; #endif - PRThread *me = _PR_MD_CURRENT_THREAD(); - - if (_PR_PENDING_INTERRUPT(me)) { - me->flags &= ~_PR_INTERRUPT; - PR_SetError(PR_PENDING_INTERRUPT_ERROR, 0); - return -1; - } - if (_PR_IO_PENDING(me)) { - PR_SetError(PR_IO_PENDING_ERROR, 0); - return -1; - } - - PR_ASSERT(IsValidNetAddr(addr) == PR_TRUE); + PRThread *me = _PR_MD_CURRENT_THREAD(); + + if (_PR_PENDING_INTERRUPT(me)) { + me->flags &= ~_PR_INTERRUPT; + PR_SetError(PR_PENDING_INTERRUPT_ERROR, 0); + return -1; + } + if (_PR_IO_PENDING(me)) { + PR_SetError(PR_IO_PENDING_ERROR, 0); + return -1; + } + + PR_ASSERT(IsValidNetAddr(addr) == PR_TRUE); #if defined(_PR_INET6) - if (addr->raw.family == PR_AF_INET6) { - addrCopy = *addr; - addrCopy.raw.family = AF_INET6; - addrp = &addrCopy; - } + if (addr->raw.family == PR_AF_INET6) { + addrCopy = *addr; + addrCopy.raw.family = AF_INET6; + addrp = &addrCopy; + } #endif - count = 0; - do { - temp = _PR_MD_SENDTO(fd, buf, amount, flags, - addrp, PR_NETADDR_SIZE(addr), timeout); - if (temp < 0) { - count = -1; - break; - } - count += temp; - if (fd->secret->nonblocking) { - break; - } - buf = (const void*) ((const char*)buf + temp); - amount -= temp; - } while (amount > 0); - return count; + count = 0; + do { + temp = _PR_MD_SENDTO(fd, buf, amount, flags, + addrp, PR_NETADDR_SIZE(addr), timeout); + if (temp < 0) { + count = -1; + break; + } + count += temp; + if (fd->secret->nonblocking) { + break; + } + buf = (const void*) ((const char*)buf + temp); + amount -= temp; + } while (amount > 0); + return count; } #if defined(_WIN64) && defined(WIN95) @@ -935,194 +934,198 @@ static PRInt32 PR_CALLBACK SocketTCPSendTo( #endif static PRInt32 PR_CALLBACK SocketRecvFrom(PRFileDesc *fd, void *buf, PRInt32 amount, -PRIntn flags, PRNetAddr *addr, PRIntervalTime timeout) + PRIntn flags, PRNetAddr *addr, PRIntervalTime timeout) { - PRInt32 rv; - PRUint32 al; - PRThread *me = _PR_MD_CURRENT_THREAD(); - - if (_PR_PENDING_INTERRUPT(me)) { - me->flags &= ~_PR_INTERRUPT; - PR_SetError(PR_PENDING_INTERRUPT_ERROR, 0); - return -1; - } - if (_PR_IO_PENDING(me)) { - PR_SetError(PR_IO_PENDING_ERROR, 0); - return -1; - } - - al = sizeof(PRNetAddr); - rv = _PR_MD_RECVFROM(fd, buf, amount, flags, addr, &al, timeout); + PRInt32 rv; + PRUint32 al; + PRThread *me = _PR_MD_CURRENT_THREAD(); + + if (_PR_PENDING_INTERRUPT(me)) { + me->flags &= ~_PR_INTERRUPT; + PR_SetError(PR_PENDING_INTERRUPT_ERROR, 0); + return -1; + } + if (_PR_IO_PENDING(me)) { + PR_SetError(PR_IO_PENDING_ERROR, 0); + return -1; + } + + al = sizeof(PRNetAddr); + rv = _PR_MD_RECVFROM(fd, buf, amount, flags, addr, &al, timeout); #ifdef _PR_INET6 - if (addr && (AF_INET6 == addr->raw.family)) + if (addr && (AF_INET6 == addr->raw.family)) { addr->raw.family = PR_AF_INET6; + } #endif - return rv; + return rv; } -static PRInt32 PR_CALLBACK SocketAcceptRead(PRFileDesc *sd, PRFileDesc **nd, -PRNetAddr **raddr, void *buf, PRInt32 amount, -PRIntervalTime timeout) +static PRInt32 PR_CALLBACK SocketAcceptRead(PRFileDesc *sd, PRFileDesc **nd, + PRNetAddr **raddr, void *buf, PRInt32 amount, + PRIntervalTime timeout) { - PRInt32 rv; - PRThread *me = _PR_MD_CURRENT_THREAD(); - - if (_PR_PENDING_INTERRUPT(me)) { - me->flags &= ~_PR_INTERRUPT; - PR_SetError(PR_PENDING_INTERRUPT_ERROR, 0); - return -1; - } - if (_PR_IO_PENDING(me)) { - PR_SetError(PR_IO_PENDING_ERROR, 0); - return -1; - } - /* The socket must be in blocking mode. */ - if (sd->secret->nonblocking) { - PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0); - return -1; - } - *nd = NULL; + PRInt32 rv; + PRThread *me = _PR_MD_CURRENT_THREAD(); + + if (_PR_PENDING_INTERRUPT(me)) { + me->flags &= ~_PR_INTERRUPT; + PR_SetError(PR_PENDING_INTERRUPT_ERROR, 0); + return -1; + } + if (_PR_IO_PENDING(me)) { + PR_SetError(PR_IO_PENDING_ERROR, 0); + return -1; + } + /* The socket must be in blocking mode. */ + if (sd->secret->nonblocking) { + PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0); + return -1; + } + *nd = NULL; #if defined(WINNT) - { - PROsfd newSock; - PRNetAddr *raddrCopy; - - if (raddr == NULL) { - raddr = &raddrCopy; - } - rv = _PR_MD_ACCEPT_READ(sd, &newSock, raddr, buf, amount, timeout); - if (rv < 0) { - rv = -1; - } else { - /* Successfully accepted and read; create the new PRFileDesc */ - *nd = PR_AllocFileDesc(newSock, PR_GetTCPMethods()); - if (*nd == 0) { - _PR_MD_CLOSE_SOCKET(newSock); - /* PR_AllocFileDesc() has invoked PR_SetError(). */ - rv = -1; - } else { - (*nd)->secret->md.io_model_committed = PR_TRUE; - (*nd)->secret->md.accepted_socket = PR_TRUE; - memcpy(&(*nd)->secret->md.peer_addr, *raddr, - PR_NETADDR_SIZE(*raddr)); + { + PROsfd newSock; + PRNetAddr *raddrCopy; + + if (raddr == NULL) { + raddr = &raddrCopy; + } + rv = _PR_MD_ACCEPT_READ(sd, &newSock, raddr, buf, amount, timeout); + if (rv < 0) { + rv = -1; + } else { + /* Successfully accepted and read; create the new PRFileDesc */ + *nd = PR_AllocFileDesc(newSock, PR_GetTCPMethods()); + if (*nd == 0) { + _PR_MD_CLOSE_SOCKET(newSock); + /* PR_AllocFileDesc() has invoked PR_SetError(). */ + rv = -1; + } else { + (*nd)->secret->md.io_model_committed = PR_TRUE; + (*nd)->secret->md.accepted_socket = PR_TRUE; + memcpy(&(*nd)->secret->md.peer_addr, *raddr, + PR_NETADDR_SIZE(*raddr)); #ifdef _PR_INET6 - if (AF_INET6 == *raddr->raw.family) - *raddr->raw.family = PR_AF_INET6; + if (AF_INET6 == *raddr->raw.family) { + *raddr->raw.family = PR_AF_INET6; + } #endif - } - } - } + } + } + } #else - rv = PR_EmulateAcceptRead(sd, nd, raddr, buf, amount, timeout); + rv = PR_EmulateAcceptRead(sd, nd, raddr, buf, amount, timeout); #endif - return rv; + return rv; } #ifdef WINNT -PR_IMPLEMENT(PRInt32) PR_NTFast_AcceptRead(PRFileDesc *sd, PRFileDesc **nd, -PRNetAddr **raddr, void *buf, PRInt32 amount, -PRIntervalTime timeout) +PR_IMPLEMENT(PRInt32) PR_NTFast_AcceptRead(PRFileDesc *sd, PRFileDesc **nd, + PRNetAddr **raddr, void *buf, PRInt32 amount, + PRIntervalTime timeout) { - PRInt32 rv; - PROsfd newSock; - PRThread *me = _PR_MD_CURRENT_THREAD(); - PRNetAddr *raddrCopy; - - if (_PR_PENDING_INTERRUPT(me)) { - me->flags &= ~_PR_INTERRUPT; - PR_SetError(PR_PENDING_INTERRUPT_ERROR, 0); - return -1; - } - if (_PR_IO_PENDING(me)) { - PR_SetError(PR_IO_PENDING_ERROR, 0); - return -1; - } - *nd = NULL; - - if (raddr == NULL) { - raddr = &raddrCopy; - } - rv = _PR_MD_FAST_ACCEPT_READ(sd, &newSock, raddr, buf, amount, - timeout, PR_TRUE, NULL, NULL); - if (rv < 0) { - rv = -1; - } else { - /* Successfully accepted and read; create the new PRFileDesc */ - *nd = PR_AllocFileDesc(newSock, PR_GetTCPMethods()); - if (*nd == 0) { - _PR_MD_CLOSE_SOCKET(newSock); - /* PR_AllocFileDesc() has invoked PR_SetError(). */ - rv = -1; - } else { - (*nd)->secret->md.io_model_committed = PR_TRUE; - (*nd)->secret->md.accepted_socket = PR_TRUE; - memcpy(&(*nd)->secret->md.peer_addr, *raddr, - PR_NETADDR_SIZE(*raddr)); + PRInt32 rv; + PROsfd newSock; + PRThread *me = _PR_MD_CURRENT_THREAD(); + PRNetAddr *raddrCopy; + + if (_PR_PENDING_INTERRUPT(me)) { + me->flags &= ~_PR_INTERRUPT; + PR_SetError(PR_PENDING_INTERRUPT_ERROR, 0); + return -1; + } + if (_PR_IO_PENDING(me)) { + PR_SetError(PR_IO_PENDING_ERROR, 0); + return -1; + } + *nd = NULL; + + if (raddr == NULL) { + raddr = &raddrCopy; + } + rv = _PR_MD_FAST_ACCEPT_READ(sd, &newSock, raddr, buf, amount, + timeout, PR_TRUE, NULL, NULL); + if (rv < 0) { + rv = -1; + } else { + /* Successfully accepted and read; create the new PRFileDesc */ + *nd = PR_AllocFileDesc(newSock, PR_GetTCPMethods()); + if (*nd == 0) { + _PR_MD_CLOSE_SOCKET(newSock); + /* PR_AllocFileDesc() has invoked PR_SetError(). */ + rv = -1; + } else { + (*nd)->secret->md.io_model_committed = PR_TRUE; + (*nd)->secret->md.accepted_socket = PR_TRUE; + memcpy(&(*nd)->secret->md.peer_addr, *raddr, + PR_NETADDR_SIZE(*raddr)); #ifdef _PR_INET6 - if (AF_INET6 == *raddr->raw.family) - *raddr->raw.family = PR_AF_INET6; + if (AF_INET6 == *raddr->raw.family) { + *raddr->raw.family = PR_AF_INET6; + } #endif #ifdef _PR_NEED_SECRET_AF - (*nd)->secret->af = sd->secret->af; + (*nd)->secret->af = sd->secret->af; #endif - } - } - return rv; + } + } + return rv; } PR_IMPLEMENT(PRInt32) PR_NTFast_AcceptRead_WithTimeoutCallback( -PRFileDesc *sd, PRFileDesc **nd, -PRNetAddr **raddr, void *buf, PRInt32 amount, -PRIntervalTime timeout, -_PR_AcceptTimeoutCallback callback, -void *callbackArg) + PRFileDesc *sd, PRFileDesc **nd, + PRNetAddr **raddr, void *buf, PRInt32 amount, + PRIntervalTime timeout, + _PR_AcceptTimeoutCallback callback, + void *callbackArg) { - PRInt32 rv; - PROsfd newSock; - PRThread *me = _PR_MD_CURRENT_THREAD(); - PRNetAddr *raddrCopy; - - if (_PR_PENDING_INTERRUPT(me)) { - me->flags &= ~_PR_INTERRUPT; - PR_SetError(PR_PENDING_INTERRUPT_ERROR, 0); - return -1; - } - if (_PR_IO_PENDING(me)) { - PR_SetError(PR_IO_PENDING_ERROR, 0); - return -1; - } - *nd = NULL; - - if (raddr == NULL) { - raddr = &raddrCopy; - } - rv = _PR_MD_FAST_ACCEPT_READ(sd, &newSock, raddr, buf, amount, - timeout, PR_TRUE, callback, callbackArg); - if (rv < 0) { - rv = -1; - } else { - /* Successfully accepted and read; create the new PRFileDesc */ - *nd = PR_AllocFileDesc(newSock, PR_GetTCPMethods()); - if (*nd == 0) { - _PR_MD_CLOSE_SOCKET(newSock); - /* PR_AllocFileDesc() has invoked PR_SetError(). */ - rv = -1; - } else { - (*nd)->secret->md.io_model_committed = PR_TRUE; - (*nd)->secret->md.accepted_socket = PR_TRUE; - memcpy(&(*nd)->secret->md.peer_addr, *raddr, - PR_NETADDR_SIZE(*raddr)); + PRInt32 rv; + PROsfd newSock; + PRThread *me = _PR_MD_CURRENT_THREAD(); + PRNetAddr *raddrCopy; + + if (_PR_PENDING_INTERRUPT(me)) { + me->flags &= ~_PR_INTERRUPT; + PR_SetError(PR_PENDING_INTERRUPT_ERROR, 0); + return -1; + } + if (_PR_IO_PENDING(me)) { + PR_SetError(PR_IO_PENDING_ERROR, 0); + return -1; + } + *nd = NULL; + + if (raddr == NULL) { + raddr = &raddrCopy; + } + rv = _PR_MD_FAST_ACCEPT_READ(sd, &newSock, raddr, buf, amount, + timeout, PR_TRUE, callback, callbackArg); + if (rv < 0) { + rv = -1; + } else { + /* Successfully accepted and read; create the new PRFileDesc */ + *nd = PR_AllocFileDesc(newSock, PR_GetTCPMethods()); + if (*nd == 0) { + _PR_MD_CLOSE_SOCKET(newSock); + /* PR_AllocFileDesc() has invoked PR_SetError(). */ + rv = -1; + } else { + (*nd)->secret->md.io_model_committed = PR_TRUE; + (*nd)->secret->md.accepted_socket = PR_TRUE; + memcpy(&(*nd)->secret->md.peer_addr, *raddr, + PR_NETADDR_SIZE(*raddr)); #ifdef _PR_INET6 - if (AF_INET6 == *raddr->raw.family) - *raddr->raw.family = PR_AF_INET6; + if (AF_INET6 == *raddr->raw.family) { + *raddr->raw.family = PR_AF_INET6; + } #endif #ifdef _PR_NEED_SECRET_AF - (*nd)->secret->af = sd->secret->af; + (*nd)->secret->af = sd->secret->af; #endif - } - } - return rv; + } + } + return rv; } #endif /* WINNT */ @@ -1130,8 +1133,8 @@ void *callbackArg) PR_IMPLEMENT(void) PR_NTFast_UpdateAcceptContext(PRFileDesc *socket, PRFileDesc *acceptSocket) { - _PR_MD_UPDATE_ACCEPT_CONTEXT( - socket->secret->md.osfd, acceptSocket->secret->md.osfd); + _PR_MD_UPDATE_ACCEPT_CONTEXT( + socket->secret->md.osfd, acceptSocket->secret->md.osfd); } #endif /* WINNT */ @@ -1139,93 +1142,95 @@ static PRInt32 PR_CALLBACK SocketSendFile( PRFileDesc *sd, PRSendFileData *sfd, PRTransmitFileFlags flags, PRIntervalTime timeout) { - PRInt32 rv; - PRThread *me = _PR_MD_CURRENT_THREAD(); - - if (_PR_PENDING_INTERRUPT(me)) { - me->flags &= ~_PR_INTERRUPT; - PR_SetError(PR_PENDING_INTERRUPT_ERROR, 0); - return -1; - } - if (_PR_IO_PENDING(me)) { - PR_SetError(PR_IO_PENDING_ERROR, 0); - return -1; - } - /* The socket must be in blocking mode. */ - if (sd->secret->nonblocking) { - PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0); - return -1; - } + PRInt32 rv; + PRThread *me = _PR_MD_CURRENT_THREAD(); + + if (_PR_PENDING_INTERRUPT(me)) { + me->flags &= ~_PR_INTERRUPT; + PR_SetError(PR_PENDING_INTERRUPT_ERROR, 0); + return -1; + } + if (_PR_IO_PENDING(me)) { + PR_SetError(PR_IO_PENDING_ERROR, 0); + return -1; + } + /* The socket must be in blocking mode. */ + if (sd->secret->nonblocking) { + PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0); + return -1; + } #if defined(WINNT) - rv = _PR_MD_SENDFILE(sd, sfd, flags, timeout); - if ((rv >= 0) && (flags == PR_TRANSMITFILE_CLOSE_SOCKET)) { - /* - * This should be kept the same as SocketClose, except - * that _PR_MD_CLOSE_SOCKET(sd->secret->md.osfd) should - * not be called because the socket will be recycled. - */ - PR_FreeFileDesc(sd); - } + rv = _PR_MD_SENDFILE(sd, sfd, flags, timeout); + if ((rv >= 0) && (flags == PR_TRANSMITFILE_CLOSE_SOCKET)) { + /* + * This should be kept the same as SocketClose, except + * that _PR_MD_CLOSE_SOCKET(sd->secret->md.osfd) should + * not be called because the socket will be recycled. + */ + PR_FreeFileDesc(sd); + } #else - rv = PR_EmulateSendFile(sd, sfd, flags, timeout); -#endif /* WINNT */ + rv = PR_EmulateSendFile(sd, sfd, flags, timeout); +#endif /* WINNT */ - return rv; + return rv; } -static PRInt32 PR_CALLBACK SocketTransmitFile(PRFileDesc *sd, PRFileDesc *fd, -const void *headers, PRInt32 hlen, PRTransmitFileFlags flags, -PRIntervalTime timeout) +static PRInt32 PR_CALLBACK SocketTransmitFile(PRFileDesc *sd, PRFileDesc *fd, + const void *headers, PRInt32 hlen, PRTransmitFileFlags flags, + PRIntervalTime timeout) { - PRSendFileData sfd; + PRSendFileData sfd; - sfd.fd = fd; - sfd.file_offset = 0; - sfd.file_nbytes = 0; - sfd.header = headers; - sfd.hlen = hlen; - sfd.trailer = NULL; - sfd.tlen = 0; + sfd.fd = fd; + sfd.file_offset = 0; + sfd.file_nbytes = 0; + sfd.header = headers; + sfd.hlen = hlen; + sfd.trailer = NULL; + sfd.tlen = 0; - return(SocketSendFile(sd, &sfd, flags, timeout)); + return(SocketSendFile(sd, &sfd, flags, timeout)); } static PRStatus PR_CALLBACK SocketGetName(PRFileDesc *fd, PRNetAddr *addr) { - PRInt32 result; - PRUint32 addrlen; - - addrlen = sizeof(PRNetAddr); - result = _PR_MD_GETSOCKNAME(fd, addr, &addrlen); - if (result < 0) { - return PR_FAILURE; - } + PRInt32 result; + PRUint32 addrlen; + + addrlen = sizeof(PRNetAddr); + result = _PR_MD_GETSOCKNAME(fd, addr, &addrlen); + if (result < 0) { + return PR_FAILURE; + } #ifdef _PR_INET6 - if (AF_INET6 == addr->raw.family) + if (AF_INET6 == addr->raw.family) { addr->raw.family = PR_AF_INET6; + } #endif - PR_ASSERT(IsValidNetAddr(addr) == PR_TRUE); - PR_ASSERT(IsValidNetAddrLen(addr, addrlen) == PR_TRUE); - return PR_SUCCESS; + PR_ASSERT(IsValidNetAddr(addr) == PR_TRUE); + PR_ASSERT(IsValidNetAddrLen(addr, addrlen) == PR_TRUE); + return PR_SUCCESS; } static PRStatus PR_CALLBACK SocketGetPeerName(PRFileDesc *fd, PRNetAddr *addr) { - PRInt32 result; - PRUint32 addrlen; - - addrlen = sizeof(PRNetAddr); - result = _PR_MD_GETPEERNAME(fd, addr, &addrlen); - if (result < 0) { - return PR_FAILURE; - } + PRInt32 result; + PRUint32 addrlen; + + addrlen = sizeof(PRNetAddr); + result = _PR_MD_GETPEERNAME(fd, addr, &addrlen); + if (result < 0) { + return PR_FAILURE; + } #ifdef _PR_INET6 - if (AF_INET6 == addr->raw.family) + if (AF_INET6 == addr->raw.family) { addr->raw.family = PR_AF_INET6; + } #endif - PR_ASSERT(IsValidNetAddr(addr) == PR_TRUE); - PR_ASSERT(IsValidNetAddrLen(addr, addrlen) == PR_TRUE); - return PR_SUCCESS; + PR_ASSERT(IsValidNetAddr(addr) == PR_TRUE); + PR_ASSERT(IsValidNetAddrLen(addr, addrlen) == PR_TRUE); + return PR_SUCCESS; } static PRInt16 PR_CALLBACK SocketPoll( @@ -1236,7 +1241,7 @@ static PRInt16 PR_CALLBACK SocketPoll( #if defined(_WIN64) if (in_flags & PR_POLL_WRITE) { if (fd->secret->alreadyConnected) { - out_flags = PR_POLL_WRITE; + *out_flags = PR_POLL_WRITE; return PR_POLL_WRITE; } } @@ -1245,84 +1250,84 @@ static PRInt16 PR_CALLBACK SocketPoll( } /* SocketPoll */ static PRIOMethods tcpMethods = { - PR_DESC_SOCKET_TCP, - SocketClose, - SocketRead, - SocketWrite, - SocketAvailable, - SocketAvailable64, - SocketSync, - (PRSeekFN)_PR_InvalidInt, - (PRSeek64FN)_PR_InvalidInt64, - (PRFileInfoFN)_PR_InvalidStatus, - (PRFileInfo64FN)_PR_InvalidStatus, - SocketWritev, - SocketConnect, - SocketAccept, - SocketBind, - SocketListen, - SocketShutdown, - SocketRecv, - SocketSend, - (PRRecvfromFN)_PR_InvalidInt, + PR_DESC_SOCKET_TCP, + SocketClose, + SocketRead, + SocketWrite, + SocketAvailable, + SocketAvailable64, + SocketSync, + (PRSeekFN)_PR_InvalidInt, + (PRSeek64FN)_PR_InvalidInt64, + (PRFileInfoFN)_PR_InvalidStatus, + (PRFileInfo64FN)_PR_InvalidStatus, + SocketWritev, + SocketConnect, + SocketAccept, + SocketBind, + SocketListen, + SocketShutdown, + SocketRecv, + SocketSend, + (PRRecvfromFN)_PR_InvalidInt, #if defined(_WIN64) && defined(WIN95) - SocketTCPSendTo, /* This is for fast open. We imitate Linux interface. */ + SocketTCPSendTo, /* This is for fast open. We imitate Linux interface. */ #else - (PRSendtoFN)_PR_InvalidInt, + (PRSendtoFN)_PR_InvalidInt, #endif - SocketPoll, - SocketAcceptRead, - SocketTransmitFile, - SocketGetName, - SocketGetPeerName, - (PRReservedFN)_PR_InvalidInt, - (PRReservedFN)_PR_InvalidInt, - _PR_SocketGetSocketOption, - _PR_SocketSetSocketOption, - SocketSendFile, + SocketPoll, + SocketAcceptRead, + SocketTransmitFile, + SocketGetName, + SocketGetPeerName, + (PRReservedFN)_PR_InvalidInt, + (PRReservedFN)_PR_InvalidInt, + _PR_SocketGetSocketOption, + _PR_SocketSetSocketOption, + SocketSendFile, SocketConnectContinue, - (PRReservedFN)_PR_InvalidInt, - (PRReservedFN)_PR_InvalidInt, - (PRReservedFN)_PR_InvalidInt, + (PRReservedFN)_PR_InvalidInt, + (PRReservedFN)_PR_InvalidInt, + (PRReservedFN)_PR_InvalidInt, (PRReservedFN)_PR_InvalidInt }; static PRIOMethods udpMethods = { - PR_DESC_SOCKET_UDP, - SocketClose, - SocketRead, - SocketWrite, - SocketAvailable, - SocketAvailable64, - SocketSync, - (PRSeekFN)_PR_InvalidInt, - (PRSeek64FN)_PR_InvalidInt64, - (PRFileInfoFN)_PR_InvalidStatus, - (PRFileInfo64FN)_PR_InvalidStatus, - SocketWritev, - SocketConnect, - (PRAcceptFN)_PR_InvalidDesc, - SocketBind, - SocketListen, - SocketShutdown, - SocketRecv, - SocketSend, - SocketRecvFrom, - SocketSendTo, - SocketPoll, - (PRAcceptreadFN)_PR_InvalidInt, - (PRTransmitfileFN)_PR_InvalidInt, - SocketGetName, - SocketGetPeerName, - (PRReservedFN)_PR_InvalidInt, - (PRReservedFN)_PR_InvalidInt, - _PR_SocketGetSocketOption, - _PR_SocketSetSocketOption, - (PRSendfileFN)_PR_InvalidInt, - (PRConnectcontinueFN)_PR_InvalidStatus, - (PRReservedFN)_PR_InvalidInt, - (PRReservedFN)_PR_InvalidInt, - (PRReservedFN)_PR_InvalidInt, + PR_DESC_SOCKET_UDP, + SocketClose, + SocketRead, + SocketWrite, + SocketAvailable, + SocketAvailable64, + SocketSync, + (PRSeekFN)_PR_InvalidInt, + (PRSeek64FN)_PR_InvalidInt64, + (PRFileInfoFN)_PR_InvalidStatus, + (PRFileInfo64FN)_PR_InvalidStatus, + SocketWritev, + SocketConnect, + (PRAcceptFN)_PR_InvalidDesc, + SocketBind, + SocketListen, + SocketShutdown, + SocketRecv, + SocketSend, + SocketRecvFrom, + SocketSendTo, + SocketPoll, + (PRAcceptreadFN)_PR_InvalidInt, + (PRTransmitfileFN)_PR_InvalidInt, + SocketGetName, + SocketGetPeerName, + (PRReservedFN)_PR_InvalidInt, + (PRReservedFN)_PR_InvalidInt, + _PR_SocketGetSocketOption, + _PR_SocketSetSocketOption, + (PRSendfileFN)_PR_InvalidInt, + (PRConnectcontinueFN)_PR_InvalidStatus, + (PRReservedFN)_PR_InvalidInt, + (PRReservedFN)_PR_InvalidInt, + (PRReservedFN)_PR_InvalidInt, (PRReservedFN)_PR_InvalidInt }; @@ -1339,41 +1344,41 @@ static PRIOMethods socketpollfdMethods = { (PRSeek64FN)_PR_InvalidInt64, (PRFileInfoFN)_PR_InvalidStatus, (PRFileInfo64FN)_PR_InvalidStatus, - (PRWritevFN)_PR_InvalidInt, - (PRConnectFN)_PR_InvalidStatus, - (PRAcceptFN)_PR_InvalidDesc, - (PRBindFN)_PR_InvalidStatus, - (PRListenFN)_PR_InvalidStatus, - (PRShutdownFN)_PR_InvalidStatus, - (PRRecvFN)_PR_InvalidInt, - (PRSendFN)_PR_InvalidInt, - (PRRecvfromFN)_PR_InvalidInt, - (PRSendtoFN)_PR_InvalidInt, - SocketPoll, - (PRAcceptreadFN)_PR_InvalidInt, - (PRTransmitfileFN)_PR_InvalidInt, - (PRGetsocknameFN)_PR_InvalidStatus, - (PRGetpeernameFN)_PR_InvalidStatus, - (PRReservedFN)_PR_InvalidInt, - (PRReservedFN)_PR_InvalidInt, + (PRWritevFN)_PR_InvalidInt, + (PRConnectFN)_PR_InvalidStatus, + (PRAcceptFN)_PR_InvalidDesc, + (PRBindFN)_PR_InvalidStatus, + (PRListenFN)_PR_InvalidStatus, + (PRShutdownFN)_PR_InvalidStatus, + (PRRecvFN)_PR_InvalidInt, + (PRSendFN)_PR_InvalidInt, + (PRRecvfromFN)_PR_InvalidInt, + (PRSendtoFN)_PR_InvalidInt, + SocketPoll, + (PRAcceptreadFN)_PR_InvalidInt, + (PRTransmitfileFN)_PR_InvalidInt, + (PRGetsocknameFN)_PR_InvalidStatus, + (PRGetpeernameFN)_PR_InvalidStatus, + (PRReservedFN)_PR_InvalidInt, + (PRReservedFN)_PR_InvalidInt, (PRGetsocketoptionFN)_PR_InvalidStatus, (PRSetsocketoptionFN)_PR_InvalidStatus, - (PRSendfileFN)_PR_InvalidInt, - (PRConnectcontinueFN)_PR_InvalidStatus, - (PRReservedFN)_PR_InvalidInt, - (PRReservedFN)_PR_InvalidInt, - (PRReservedFN)_PR_InvalidInt, + (PRSendfileFN)_PR_InvalidInt, + (PRConnectcontinueFN)_PR_InvalidStatus, + (PRReservedFN)_PR_InvalidInt, + (PRReservedFN)_PR_InvalidInt, + (PRReservedFN)_PR_InvalidInt, (PRReservedFN)_PR_InvalidInt }; PR_IMPLEMENT(const PRIOMethods*) PR_GetTCPMethods() { - return &tcpMethods; + return &tcpMethods; } PR_IMPLEMENT(const PRIOMethods*) PR_GetUDPMethods() { - return &udpMethods; + return &udpMethods; } static const PRIOMethods* PR_GetSocketPollFdMethods() @@ -1390,136 +1395,146 @@ extern PRBool _pr_ipv6_is_present(void); PR_IMPLEMENT(PRBool) _pr_test_ipv6_socket() { - PROsfd osfd; - - osfd = _PR_MD_SOCKET(AF_INET6, SOCK_STREAM, 0); - if (osfd != -1) { - _PR_MD_CLOSE_SOCKET(osfd); - return PR_TRUE; - } - return PR_FALSE; + PROsfd osfd; + + osfd = _PR_MD_SOCKET(AF_INET6, SOCK_STREAM, 0); + if (osfd != -1) { + _PR_MD_CLOSE_SOCKET(osfd); + return PR_TRUE; + } + return PR_FALSE; } -#endif /* _PR_INET6_PROBE */ +#endif /* _PR_INET6_PROBE */ #endif PR_IMPLEMENT(PRFileDesc*) PR_Socket(PRInt32 domain, PRInt32 type, PRInt32 proto) { - PROsfd osfd; - PRFileDesc *fd; - PRInt32 tmp_domain = domain; + PROsfd osfd; + PRFileDesc *fd; + PRInt32 tmp_domain = domain; - if (!_pr_initialized) _PR_ImplicitInitialization(); - if (PR_AF_INET != domain - && PR_AF_INET6 != domain + if (!_pr_initialized) { + _PR_ImplicitInitialization(); + } + if (PR_AF_INET != domain + && PR_AF_INET6 != domain #if defined(XP_UNIX) || defined(XP_OS2) - && PR_AF_LOCAL != domain + && PR_AF_LOCAL != domain #endif - ) { - PR_SetError(PR_ADDRESS_NOT_SUPPORTED_ERROR, 0); - return NULL; - } + ) { + PR_SetError(PR_ADDRESS_NOT_SUPPORTED_ERROR, 0); + return NULL; + } #if defined(_PR_INET6_PROBE) - if (PR_AF_INET6 == domain) - domain = _pr_ipv6_is_present() ? AF_INET6 : AF_INET; + if (PR_AF_INET6 == domain) { + domain = _pr_ipv6_is_present() ? AF_INET6 : AF_INET; + } #elif defined(_PR_INET6) - if (PR_AF_INET6 == domain) - domain = AF_INET6; + if (PR_AF_INET6 == domain) { + domain = AF_INET6; + } #else - if (PR_AF_INET6 == domain) - domain = AF_INET; -#endif /* _PR_INET6 */ - osfd = _PR_MD_SOCKET(domain, type, proto); - if (osfd == -1) { - return 0; - } - if (type == SOCK_STREAM) - fd = PR_AllocFileDesc(osfd, PR_GetTCPMethods()); - else - fd = PR_AllocFileDesc(osfd, PR_GetUDPMethods()); - /* - * Make the sockets non-blocking - */ - if (fd != NULL) { - _PR_MD_MAKE_NONBLOCK(fd); - _PR_MD_INIT_FD_INHERITABLE(fd, PR_FALSE); + if (PR_AF_INET6 == domain) { + domain = AF_INET; + } +#endif /* _PR_INET6 */ + osfd = _PR_MD_SOCKET(domain, type, proto); + if (osfd == -1) { + return 0; + } + if (type == SOCK_STREAM) { + fd = PR_AllocFileDesc(osfd, PR_GetTCPMethods()); + } + else { + fd = PR_AllocFileDesc(osfd, PR_GetUDPMethods()); + } + /* + * Make the sockets non-blocking + */ + if (fd != NULL) { + _PR_MD_MAKE_NONBLOCK(fd); + _PR_MD_INIT_FD_INHERITABLE(fd, PR_FALSE); #ifdef _PR_NEED_SECRET_AF - fd->secret->af = domain; + fd->secret->af = domain; #endif #if defined(_PR_INET6_PROBE) || !defined(_PR_INET6) - /* - * For platforms with no support for IPv6 - * create layered socket for IPv4-mapped IPv6 addresses - */ - if (PR_AF_INET6 == tmp_domain && PR_AF_INET == domain) { - if (PR_FAILURE == _pr_push_ipv6toipv4_layer(fd)) { - PR_Close(fd); - fd = NULL; - } - } + /* + * For platforms with no support for IPv6 + * create layered socket for IPv4-mapped IPv6 addresses + */ + if (PR_AF_INET6 == tmp_domain && PR_AF_INET == domain) { + if (PR_FAILURE == _pr_push_ipv6toipv4_layer(fd)) { + PR_Close(fd); + fd = NULL; + } + } #endif - } else - _PR_MD_CLOSE_SOCKET(osfd); + } else { + _PR_MD_CLOSE_SOCKET(osfd); + } - return fd; + return fd; } PR_IMPLEMENT(PRFileDesc *) PR_NewTCPSocket(void) { - PRInt32 domain = AF_INET; + PRInt32 domain = AF_INET; - return PR_Socket(domain, SOCK_STREAM, 0); + return PR_Socket(domain, SOCK_STREAM, 0); } PR_IMPLEMENT(PRFileDesc*) PR_NewUDPSocket(void) { - PRInt32 domain = AF_INET; + PRInt32 domain = AF_INET; - return PR_Socket(domain, SOCK_DGRAM, 0); + return PR_Socket(domain, SOCK_DGRAM, 0); } PR_IMPLEMENT(PRFileDesc *) PR_OpenTCPSocket(PRIntn af) { - return PR_Socket(af, SOCK_STREAM, 0); + return PR_Socket(af, SOCK_STREAM, 0); } PR_IMPLEMENT(PRFileDesc*) PR_OpenUDPSocket(PRIntn af) { - return PR_Socket(af, SOCK_DGRAM, 0); + return PR_Socket(af, SOCK_DGRAM, 0); } PR_IMPLEMENT(PRStatus) PR_NewTCPSocketPair(PRFileDesc *f[]) { #ifdef XP_UNIX - PRInt32 rv, osfd[2]; - - if (!_pr_initialized) _PR_ImplicitInitialization(); - - rv = _PR_MD_SOCKETPAIR(AF_UNIX, SOCK_STREAM, 0, osfd); - if (rv == -1) { - return PR_FAILURE; - } - - f[0] = PR_AllocFileDesc(osfd[0], PR_GetTCPMethods()); - if (!f[0]) { - _PR_MD_CLOSE_SOCKET(osfd[0]); - _PR_MD_CLOSE_SOCKET(osfd[1]); - /* PR_AllocFileDesc() has invoked PR_SetError(). */ - return PR_FAILURE; - } - f[1] = PR_AllocFileDesc(osfd[1], PR_GetTCPMethods()); - if (!f[1]) { - PR_Close(f[0]); - _PR_MD_CLOSE_SOCKET(osfd[1]); - /* PR_AllocFileDesc() has invoked PR_SetError(). */ - return PR_FAILURE; - } - _PR_MD_MAKE_NONBLOCK(f[0]); - _PR_MD_INIT_FD_INHERITABLE(f[0], PR_FALSE); - _PR_MD_MAKE_NONBLOCK(f[1]); - _PR_MD_INIT_FD_INHERITABLE(f[1], PR_FALSE); - return PR_SUCCESS; + PRInt32 rv, osfd[2]; + + if (!_pr_initialized) { + _PR_ImplicitInitialization(); + } + + rv = _PR_MD_SOCKETPAIR(AF_UNIX, SOCK_STREAM, 0, osfd); + if (rv == -1) { + return PR_FAILURE; + } + + f[0] = PR_AllocFileDesc(osfd[0], PR_GetTCPMethods()); + if (!f[0]) { + _PR_MD_CLOSE_SOCKET(osfd[0]); + _PR_MD_CLOSE_SOCKET(osfd[1]); + /* PR_AllocFileDesc() has invoked PR_SetError(). */ + return PR_FAILURE; + } + f[1] = PR_AllocFileDesc(osfd[1], PR_GetTCPMethods()); + if (!f[1]) { + PR_Close(f[0]); + _PR_MD_CLOSE_SOCKET(osfd[1]); + /* PR_AllocFileDesc() has invoked PR_SetError(). */ + return PR_FAILURE; + } + _PR_MD_MAKE_NONBLOCK(f[0]); + _PR_MD_INIT_FD_INHERITABLE(f[0], PR_FALSE); + _PR_MD_MAKE_NONBLOCK(f[1]); + _PR_MD_INIT_FD_INHERITABLE(f[1], PR_FALSE); + return PR_SUCCESS; #elif defined(WINNT) /* * A socket pair is often used for interprocess communication, @@ -1536,7 +1551,9 @@ PR_IMPLEMENT(PRStatus) PR_NewTCPSocketPair(PRFileDesc *f[]) struct sockaddr_in selfAddr, peerAddr; int addrLen; - if (!_pr_initialized) _PR_ImplicitInitialization(); + if (!_pr_initialized) { + _PR_ImplicitInitialization(); + } osfd[0] = osfd[1] = INVALID_SOCKET; listenSock = socket(AF_INET, SOCK_STREAM, 0); @@ -1548,11 +1565,11 @@ PR_IMPLEMENT(PRStatus) PR_NewTCPSocketPair(PRFileDesc *f[]) selfAddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); /* BugZilla: 35408 */ addrLen = sizeof(selfAddr); if (bind(listenSock, (struct sockaddr *) &selfAddr, - addrLen) == SOCKET_ERROR) { + addrLen) == SOCKET_ERROR) { goto failed; } if (getsockname(listenSock, (struct sockaddr *) &selfAddr, - &addrLen) == SOCKET_ERROR) { + &addrLen) == SOCKET_ERROR) { goto failed; } if (listen(listenSock, 5) == SOCKET_ERROR) { @@ -1574,7 +1591,7 @@ PR_IMPLEMENT(PRStatus) PR_NewTCPSocketPair(PRFileDesc *f[]) * will need to create another thread to call connect. */ if (connect(osfd[0], (struct sockaddr *) &selfAddr, - addrLen) == SOCKET_ERROR) { + addrLen) == SOCKET_ERROR) { goto failed; } /* @@ -1583,7 +1600,7 @@ PR_IMPLEMENT(PRStatus) PR_NewTCPSocketPair(PRFileDesc *f[]) * is made from our own socket osfd[0]. */ if (getsockname(osfd[0], (struct sockaddr *) &selfAddr, - &addrLen) == SOCKET_ERROR) { + &addrLen) == SOCKET_ERROR) { goto failed; } osfd[1] = accept(listenSock, (struct sockaddr *) &peerAddr, &addrLen); @@ -1677,7 +1694,7 @@ failed: * will need to create another thread to call connect. */ if (PR_Connect(f[0], &selfAddr, PR_INTERVAL_NO_TIMEOUT) - == PR_FAILURE) { + == PR_FAILURE) { goto failed; } /* @@ -1730,8 +1747,9 @@ PR_FileDesc2NativeHandle(PRFileDesc *fd) PR_IMPLEMENT(void) PR_ChangeFileDescNativeHandle(PRFileDesc *fd, PROsfd handle) { - if (fd) - fd->secret->md.osfd = handle; + if (fd) { + fd->secret->md.osfd = handle; + } } /* @@ -1741,69 +1759,69 @@ PR_ChangeFileDescNativeHandle(PRFileDesc *fd, PROsfd handle) PR_IMPLEMENT(void) PR_FD_ZERO(PR_fd_set *set) { - memset(set, 0, sizeof(PR_fd_set)); + memset(set, 0, sizeof(PR_fd_set)); } PR_IMPLEMENT(void) PR_FD_SET(PRFileDesc *fh, PR_fd_set *set) { - PR_ASSERT( set->hsize < PR_MAX_SELECT_DESC ); + PR_ASSERT( set->hsize < PR_MAX_SELECT_DESC ); - set->harray[set->hsize++] = fh; + set->harray[set->hsize++] = fh; } PR_IMPLEMENT(void) PR_FD_CLR(PRFileDesc *fh, PR_fd_set *set) { - PRUint32 index, index2; - - for (index = 0; index<set->hsize; index++) - if (set->harray[index] == fh) { - for (index2=index; index2 < (set->hsize-1); index2++) { - set->harray[index2] = set->harray[index2+1]; - } - set->hsize--; - break; - } + PRUint32 index, index2; + + for (index = 0; index<set->hsize; index++) + if (set->harray[index] == fh) { + for (index2=index; index2 < (set->hsize-1); index2++) { + set->harray[index2] = set->harray[index2+1]; + } + set->hsize--; + break; + } } PR_IMPLEMENT(PRInt32) PR_FD_ISSET(PRFileDesc *fh, PR_fd_set *set) { - PRUint32 index; - for (index = 0; index<set->hsize; index++) - if (set->harray[index] == fh) { - return 1; - } - return 0; + PRUint32 index; + for (index = 0; index<set->hsize; index++) + if (set->harray[index] == fh) { + return 1; + } + return 0; } PR_IMPLEMENT(void) PR_FD_NSET(PROsfd fd, PR_fd_set *set) { - PR_ASSERT( set->nsize < PR_MAX_SELECT_DESC ); + PR_ASSERT( set->nsize < PR_MAX_SELECT_DESC ); - set->narray[set->nsize++] = fd; + set->narray[set->nsize++] = fd; } PR_IMPLEMENT(void) PR_FD_NCLR(PROsfd fd, PR_fd_set *set) { - PRUint32 index, index2; - - for (index = 0; index<set->nsize; index++) - if (set->narray[index] == fd) { - for (index2=index; index2 < (set->nsize-1); index2++) { - set->narray[index2] = set->narray[index2+1]; - } - set->nsize--; - break; - } + PRUint32 index, index2; + + for (index = 0; index<set->nsize; index++) + if (set->narray[index] == fd) { + for (index2=index; index2 < (set->nsize-1); index2++) { + set->narray[index2] = set->narray[index2+1]; + } + set->nsize--; + break; + } } PR_IMPLEMENT(PRInt32) PR_FD_NISSET(PROsfd fd, PR_fd_set *set) { - PRUint32 index; - for (index = 0; index<set->nsize; index++) - if (set->narray[index] == fd) { - return 1; - } - return 0; + PRUint32 index; + for (index = 0; index<set->nsize; index++) + if (set->narray[index] == fd) { + return 1; + } + return 0; } @@ -1818,26 +1836,30 @@ static PRPollDesc *_pr_setfd( PRUintn fsidx, pdidx; PRPollDesc *poll = polldesc; - if (NULL == set) return poll; + if (NULL == set) { + return poll; + } - /* First set the pr file handle osfds */ - for (fsidx = 0; fsidx < set->hsize; fsidx++) - { - for (pdidx = 0; 1; pdidx++) + /* First set the pr file handle osfds */ + for (fsidx = 0; fsidx < set->hsize; fsidx++) + { + for (pdidx = 0; 1; pdidx++) { if ((PRFileDesc*)-1 == poll[pdidx].fd) { /* our vector is full - extend and condition it */ poll = (PRPollDesc*)PR_Realloc( - poll, (pdidx + 1 + PD_INCR) * sizeof(PRPollDesc)); - if (NULL == poll) goto out_of_memory; + poll, (pdidx + 1 + PD_INCR) * sizeof(PRPollDesc)); + if (NULL == poll) { + goto out_of_memory; + } memset( poll + pdidx * sizeof(PRPollDesc), 0, PD_INCR * sizeof(PRPollDesc)); poll[pdidx + PD_INCR].fd = (PRFileDesc*)-1; } if ((NULL == poll[pdidx].fd) - || (poll[pdidx].fd == set->harray[fsidx])) + || (poll[pdidx].fd == set->harray[fsidx])) { /* PR_ASSERT(0 == (poll[pdidx].in_flags & flags)); */ /* either empty or prevously defined */ @@ -1846,27 +1868,29 @@ static PRPollDesc *_pr_setfd( break; } } - } + } #if 0 - /* Second set the native osfds */ - for (fsidx = 0; fsidx < set->nsize; fsidx++) - { - for (pdidx = 0; ((PRFileDesc*)-1 != poll[pdidx].fd); pdidx++) + /* Second set the native osfds */ + for (fsidx = 0; fsidx < set->nsize; fsidx++) + { + for (pdidx = 0; ((PRFileDesc*)-1 != poll[pdidx].fd); pdidx++) { if ((PRFileDesc*)-1 == poll[pdidx].fd) { /* our vector is full - extend and condition it */ poll = PR_Realloc( - poll, (pdidx + PD_INCR) * sizeof(PRPollDesc)); - if (NULL == poll) goto out_of_memory; + poll, (pdidx + PD_INCR) * sizeof(PRPollDesc)); + if (NULL == poll) { + goto out_of_memory; + } memset( poll + pdidx * sizeof(PRPollDesc), 0, PD_INCR * sizeof(PRPollDesc)); poll[(pdidx + PD_INCR)].fd = (PRFileDesc*)-1; } if ((NULL == poll[pdidx].fd) - || (poll[pdidx].fd == set->narray[fsidx])) + || (poll[pdidx].fd == set->narray[fsidx])) { /* either empty or prevously defined */ poll[pdidx].fd = set->narray[fsidx]; @@ -1875,25 +1899,27 @@ static PRPollDesc *_pr_setfd( break; } } - } + } #endif /* 0 */ - return poll; + return poll; out_of_memory: - if (NULL != polldesc) PR_DELETE(polldesc); + if (NULL != polldesc) { + PR_DELETE(polldesc); + } return NULL; } /* _pr_setfd */ #endif /* !defined(NEED_SELECT) */ PR_IMPLEMENT(PRInt32) PR_Select( - PRInt32 unused, PR_fd_set *pr_rd, PR_fd_set *pr_wr, + PRInt32 unused, PR_fd_set *pr_rd, PR_fd_set *pr_wr, PR_fd_set *pr_ex, PRIntervalTime timeout) { #if !defined(NEED_SELECT) - PRInt32 npds = 0; + PRInt32 npds = 0; /* ** Find out how many fds are represented in the three lists. ** Then allocate a polling descriptor for the logical union @@ -1903,16 +1929,21 @@ PR_IMPLEMENT(PRInt32) PR_Select( PRPollDesc *copy, *poll; static PRBool warning = PR_TRUE; - if (warning) warning = _PR_Obsolete( "PR_Select()", "PR_Poll()"); + if (warning) { + warning = _PR_Obsolete( "PR_Select()", "PR_Poll()"); + } /* try to get an initial guesss at how much space we need */ npds = 0; - if ((NULL != pr_rd) && ((pr_rd->hsize + pr_rd->nsize - npds) > 0)) + if ((NULL != pr_rd) && ((pr_rd->hsize + pr_rd->nsize - npds) > 0)) { npds = pr_rd->hsize + pr_rd->nsize; - if ((NULL != pr_wr) && ((pr_wr->hsize + pr_wr->nsize - npds) > 0)) + } + if ((NULL != pr_wr) && ((pr_wr->hsize + pr_wr->nsize - npds) > 0)) { npds = pr_wr->hsize + pr_wr->nsize; - if ((NULL != pr_ex) && ((pr_ex->hsize + pr_ex->nsize - npds) > 0)) + } + if ((NULL != pr_ex) && ((pr_ex->hsize + pr_ex->nsize - npds) > 0)) { npds = pr_ex->hsize + pr_ex->nsize; + } if (0 == npds) { @@ -1921,15 +1952,23 @@ PR_IMPLEMENT(PRInt32) PR_Select( } copy = poll = (PRPollDesc*)PR_Calloc(npds + PD_INCR, sizeof(PRPollDesc)); - if (NULL == poll) goto out_of_memory; + if (NULL == poll) { + goto out_of_memory; + } poll[npds + PD_INCR - 1].fd = (PRFileDesc*)-1; poll = _pr_setfd(pr_rd, PR_POLL_READ, poll); - if (NULL == poll) goto out_of_memory; + if (NULL == poll) { + goto out_of_memory; + } poll = _pr_setfd(pr_wr, PR_POLL_WRITE, poll); - if (NULL == poll) goto out_of_memory; + if (NULL == poll) { + goto out_of_memory; + } poll = _pr_setfd(pr_ex, PR_POLL_EXCEPT, poll); - if (NULL == poll) goto out_of_memory; + if (NULL == poll) { + goto out_of_memory; + } unused = 0; while (NULL != poll[unused].fd && (PRFileDesc*)-1 != poll[unused].fd) { @@ -1942,9 +1981,15 @@ PR_IMPLEMENT(PRInt32) PR_Select( if (npds > 0) { /* Copy the results back into the fd sets */ - if (NULL != pr_rd) pr_rd->nsize = pr_rd->hsize = 0; - if (NULL != pr_wr) pr_wr->nsize = pr_wr->hsize = 0; - if (NULL != pr_ex) pr_ex->nsize = pr_ex->hsize = 0; + if (NULL != pr_rd) { + pr_rd->nsize = pr_rd->hsize = 0; + } + if (NULL != pr_wr) { + pr_wr->nsize = pr_wr->hsize = 0; + } + if (NULL != pr_ex) { + pr_ex->nsize = pr_ex->hsize = 0; + } for (copy = &poll[unused - 1]; copy >= poll; --copy) { if (copy->out_flags & PR_POLL_NVAL) @@ -1954,11 +1999,17 @@ PR_IMPLEMENT(PRInt32) PR_Select( break; } if (copy->out_flags & PR_POLL_READ) - if (NULL != pr_rd) pr_rd->harray[pr_rd->hsize++] = copy->fd; + if (NULL != pr_rd) { + pr_rd->harray[pr_rd->hsize++] = copy->fd; + } if (copy->out_flags & PR_POLL_WRITE) - if (NULL != pr_wr) pr_wr->harray[pr_wr->hsize++] = copy->fd; + if (NULL != pr_wr) { + pr_wr->harray[pr_wr->hsize++] = copy->fd; + } if (copy->out_flags & PR_POLL_EXCEPT) - if (NULL != pr_ex) pr_ex->harray[pr_ex->hsize++] = copy->fd; + if (NULL != pr_ex) { + pr_ex->harray[pr_ex->hsize++] = copy->fd; + } } } PR_DELETE(poll); @@ -1966,8 +2017,8 @@ PR_IMPLEMENT(PRInt32) PR_Select( return npds; out_of_memory: PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); - return -1; + return -1; #endif /* !defined(NEED_SELECT) */ - + } |