From 3562e78743202e43aec8727005182a2558117eca Mon Sep 17 00:00:00 2001 From: marha Date: Sun, 28 Jun 2009 22:07:26 +0000 Subject: Checked in the following released items: xkeyboard-config-1.4.tar.gz ttf-bitstream-vera-1.10.tar.gz font-alias-1.0.1.tar.gz font-sun-misc-1.0.0.tar.gz font-sun-misc-1.0.0.tar.gz font-sony-misc-1.0.0.tar.gz font-schumacher-misc-1.0.0.tar.gz font-mutt-misc-1.0.0.tar.gz font-misc-misc-1.0.0.tar.gz font-misc-meltho-1.0.0.tar.gz font-micro-misc-1.0.0.tar.gz font-jis-misc-1.0.0.tar.gz font-isas-misc-1.0.0.tar.gz font-dec-misc-1.0.0.tar.gz font-daewoo-misc-1.0.0.tar.gz font-cursor-misc-1.0.0.tar.gz font-arabic-misc-1.0.0.tar.gz font-winitzki-cyrillic-1.0.0.tar.gz font-misc-cyrillic-1.0.0.tar.gz font-cronyx-cyrillic-1.0.0.tar.gz font-screen-cyrillic-1.0.1.tar.gz font-xfree86-type1-1.0.1.tar.gz font-adobe-utopia-type1-1.0.1.tar.gz font-ibm-type1-1.0.0.tar.gz font-bitstream-type1-1.0.0.tar.gz font-bitstream-speedo-1.0.0.tar.gz font-bh-ttf-1.0.0.tar.gz font-bh-type1-1.0.0.tar.gz font-bitstream-100dpi-1.0.0.tar.gz font-bh-lucidatypewriter-100dpi-1.0.0.tar.gz font-bh-100dpi-1.0.0.tar.gz font-adobe-utopia-100dpi-1.0.1.tar.gz font-adobe-100dpi-1.0.0.tar.gz font-util-1.0.1.tar.gz font-bitstream-75dpi-1.0.0.tar.gz font-bh-lucidatypewriter-75dpi-1.0.0.tar.gz font-adobe-utopia-75dpi-1.0.1.tar.gz font-bh-75dpi-1.0.0.tar.gz bdftopcf-1.0.1.tar.gz font-adobe-75dpi-1.0.0.tar.gz mkfontscale-1.0.6.tar.gz openssl-0.9.8k.tar.gz bigreqsproto-1.0.2.tar.gz xtrans-1.2.2.tar.gz resourceproto-1.0.2.tar.gz inputproto-1.4.4.tar.gz compositeproto-0.4.tar.gz damageproto-1.1.0.tar.gz zlib-1.2.3.tar.gz xkbcomp-1.0.5.tar.gz freetype-2.3.9.tar.gz pthreads-w32-2-8-0-release.tar.gz pixman-0.12.0.tar.gz kbproto-1.0.3.tar.gz evieext-1.0.2.tar.gz fixesproto-4.0.tar.gz recordproto-1.13.2.tar.gz randrproto-1.2.2.tar.gz scrnsaverproto-1.1.0.tar.gz renderproto-0.9.3.tar.gz xcmiscproto-1.1.2.tar.gz fontsproto-2.0.2.tar.gz xextproto-7.0.3.tar.gz xproto-7.0.14.tar.gz libXdmcp-1.0.2.tar.gz libxkbfile-1.0.5.tar.gz libfontenc-1.0.4.tar.gz libXfont-1.3.4.tar.gz libX11-1.1.5.tar.gz libXau-1.0.4.tar.gz libxcb-1.1.tar.gz xorg-server-1.5.3.tar.gz --- openssl/MacOS/GetHTTPS.src/CPStringUtils.cpp | 2753 ++++++++++++++++++++++++++ openssl/MacOS/GetHTTPS.src/CPStringUtils.hpp | 104 + openssl/MacOS/GetHTTPS.src/ErrorHandling.cpp | 170 ++ openssl/MacOS/GetHTTPS.src/ErrorHandling.hpp | 147 ++ openssl/MacOS/GetHTTPS.src/GetHTTPS.cpp | 209 ++ openssl/MacOS/GetHTTPS.src/MacSocket.cpp | 1607 +++++++++++++++ openssl/MacOS/GetHTTPS.src/MacSocket.h | 103 + 7 files changed, 5093 insertions(+) create mode 100644 openssl/MacOS/GetHTTPS.src/CPStringUtils.cpp create mode 100644 openssl/MacOS/GetHTTPS.src/CPStringUtils.hpp create mode 100644 openssl/MacOS/GetHTTPS.src/ErrorHandling.cpp create mode 100644 openssl/MacOS/GetHTTPS.src/ErrorHandling.hpp create mode 100644 openssl/MacOS/GetHTTPS.src/GetHTTPS.cpp create mode 100644 openssl/MacOS/GetHTTPS.src/MacSocket.cpp create mode 100644 openssl/MacOS/GetHTTPS.src/MacSocket.h (limited to 'openssl/MacOS/GetHTTPS.src') diff --git a/openssl/MacOS/GetHTTPS.src/CPStringUtils.cpp b/openssl/MacOS/GetHTTPS.src/CPStringUtils.cpp new file mode 100644 index 000000000..617aae2c7 --- /dev/null +++ b/openssl/MacOS/GetHTTPS.src/CPStringUtils.cpp @@ -0,0 +1,2753 @@ +/* ==================================================================== + * Copyright (c) 1998-1999 The OpenSSL Project. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. All advertising materials mentioning features or use of this + * software must display the following acknowledgment: + * "This product includes software developed by the OpenSSL Project + * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" + * + * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to + * endorse or promote products derived from this software without + * prior written permission. For written permission, please contact + * openssl-core@openssl.org. + * + * 5. Products derived from this software may not be called "OpenSSL" + * nor may "OpenSSL" appear in their names without prior written + * permission of the OpenSSL Project. + * + * 6. Redistributions of any form whatsoever must retain the following + * acknowledgment: + * "This product includes software developed by the OpenSSL Project + * for use in the OpenSSL Toolkit (http://www.openssl.org/)" + * + * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY + * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + * ==================================================================== + * + * This product includes cryptographic software written by Eric Young + * (eay@cryptsoft.com). This product includes software written by Tim + * Hudson (tjh@cryptsoft.com). + * + */ + + + + #include "CPStringUtils.hpp" +#include "ErrorHandling.hpp" + + + +#define kNumberFormatString "\p########0.00#######;-########0.00#######" + + + +// Useful utility functions which could be optimized a whole lot + + +void CopyPStrToCStr(const unsigned char *thePStr,char *theCStr,const int maxCStrLength) +{ +int i,numPChars; + + + if (thePStr != nil && theCStr != nil && maxCStrLength > 0) + { + numPChars = thePStr[0]; + + for (i = 0;;i++) + { + if (i >= numPChars || i >= maxCStrLength - 1) + { + theCStr[i] = 0; + + break; + } + + else + { + theCStr[i] = thePStr[i + 1]; + } + } + } +} + + +void CopyPStrToPStr(const unsigned char *theSrcPStr,unsigned char *theDstPStr,const int maxDstStrLength) +{ +int theMaxDstStrLength; + + + theMaxDstStrLength = maxDstStrLength; + + + if (theDstPStr != nil && theSrcPStr != nil && theMaxDstStrLength > 0) + { + if (theMaxDstStrLength > 255) + { + theMaxDstStrLength = 255; + } + + + if (theMaxDstStrLength - 1 < theSrcPStr[0]) + { + BlockMove(theSrcPStr + 1,theDstPStr + 1,theMaxDstStrLength - 1); + + theDstPStr[0] = theMaxDstStrLength - 1; + } + + else + { + BlockMove(theSrcPStr,theDstPStr,theSrcPStr[0] + 1); + } + } +} + + +void CopyCStrToCStr(const char *theSrcCStr,char *theDstCStr,const int maxDstStrLength) +{ +int i; + + + if (theDstCStr != nil && theSrcCStr != nil && maxDstStrLength > 0) + { + for (i = 0;;i++) + { + if (theSrcCStr[i] == 0 || i >= maxDstStrLength - 1) + { + theDstCStr[i] = 0; + + break; + } + + else + { + theDstCStr[i] = theSrcCStr[i]; + } + } + } +} + + + +void CopyCSubstrToCStr(const char *theSrcCStr,const int maxCharsToCopy,char *theDstCStr,const int maxDstStrLength) +{ +int i; + + + if (theDstCStr != nil && theSrcCStr != nil && maxDstStrLength > 0) + { + for (i = 0;;i++) + { + if (theSrcCStr[i] == 0 || i >= maxDstStrLength - 1 || i >= maxCharsToCopy) + { + theDstCStr[i] = 0; + + break; + } + + else + { + theDstCStr[i] = theSrcCStr[i]; + } + } + } +} + + + +void CopyCSubstrToPStr(const char *theSrcCStr,const int maxCharsToCopy,unsigned char *theDstPStr,const int maxDstStrLength) +{ +int i; +int theMaxDstStrLength; + + + theMaxDstStrLength = maxDstStrLength; + + if (theDstPStr != nil && theSrcCStr != nil && theMaxDstStrLength > 0) + { + if (theMaxDstStrLength > 255) + { + theMaxDstStrLength = 255; + } + + + for (i = 0;;i++) + { + if (theSrcCStr[i] == 0 || i >= theMaxDstStrLength - 1 || i >= maxCharsToCopy) + { + theDstPStr[0] = i; + + break; + } + + else + { + theDstPStr[i + 1] = theSrcCStr[i]; + } + } + } +} + + + +void CopyCStrToPStr(const char *theSrcCStr,unsigned char *theDstPStr,const int maxDstStrLength) +{ +int i; +int theMaxDstStrLength; + + + theMaxDstStrLength = maxDstStrLength; + + if (theDstPStr != nil && theSrcCStr != nil && theMaxDstStrLength > 0) + { + if (theMaxDstStrLength > 255) + { + theMaxDstStrLength = 255; + } + + + for (i = 0;;i++) + { + if (i >= theMaxDstStrLength - 1 || theSrcCStr[i] == 0) + { + theDstPStr[0] = i; + + break; + } + + else + { + theDstPStr[i + 1] = theSrcCStr[i]; + } + } + } +} + + +void ConcatPStrToCStr(const unsigned char *thePStr,char *theCStr,const int maxCStrLength) +{ +int i,numPChars,cStrLength; + + + if (thePStr != nil && theCStr != nil && maxCStrLength > 0) + { + for (cStrLength = 0;theCStr[cStrLength] != 0;cStrLength++) + { + + } + + + numPChars = thePStr[0]; + + + for (i = 0;;i++) + { + if (i >= numPChars || cStrLength >= maxCStrLength - 1) + { + theCStr[cStrLength++] = 0; + + break; + } + + else + { + theCStr[cStrLength++] = thePStr[i + 1]; + } + } + } +} + + + +void ConcatPStrToPStr(const unsigned char *theSrcPStr,unsigned char *theDstPStr,const int maxDstStrLength) +{ +int theMaxDstStrLength; + + + theMaxDstStrLength = maxDstStrLength; + + if (theSrcPStr != nil && theDstPStr != nil && theMaxDstStrLength > 0) + { + if (theMaxDstStrLength > 255) + { + theMaxDstStrLength = 255; + } + + + if (theMaxDstStrLength - theDstPStr[0] - 1 < theSrcPStr[0]) + { + BlockMove(theSrcPStr + 1,theDstPStr + theDstPStr[0] + 1,theMaxDstStrLength - 1 - theDstPStr[0]); + + theDstPStr[0] = theMaxDstStrLength - 1; + } + + else + { + BlockMove(theSrcPStr + 1,theDstPStr + theDstPStr[0] + 1,theSrcPStr[0]); + + theDstPStr[0] += theSrcPStr[0]; + } + } +} + + + +void ConcatCStrToPStr(const char *theSrcCStr,unsigned char *theDstPStr,const int maxDstStrLength) +{ +int i,thePStrLength; +int theMaxDstStrLength; + + + theMaxDstStrLength = maxDstStrLength; + + if (theSrcCStr != nil && theDstPStr != nil && theMaxDstStrLength > 0) + { + if (theMaxDstStrLength > 255) + { + theMaxDstStrLength = 255; + } + + + thePStrLength = theDstPStr[0]; + + for (i = 0;;i++) + { + if (theSrcCStr[i] == 0 || thePStrLength >= theMaxDstStrLength - 1) + { + theDstPStr[0] = thePStrLength; + + break; + } + + else + { + theDstPStr[thePStrLength + 1] = theSrcCStr[i]; + + thePStrLength++; + } + } + } +} + + + +void ConcatCStrToCStr(const char *theSrcCStr,char *theDstCStr,const int maxCStrLength) +{ +int cStrLength; + + + if (theSrcCStr != nil && theDstCStr != nil && maxCStrLength > 0) + { + for (cStrLength = 0;theDstCStr[cStrLength] != 0;cStrLength++) + { + + } + + + for (;;) + { + if (*theSrcCStr == 0 || cStrLength >= maxCStrLength - 1) + { + theDstCStr[cStrLength++] = 0; + + break; + } + + else + { + theDstCStr[cStrLength++] = *theSrcCStr++; + } + } + } +} + + + +void ConcatCharToCStr(const char theChar,char *theDstCStr,const int maxCStrLength) +{ +int cStrLength; + + + if (theDstCStr != nil && maxCStrLength > 0) + { + cStrLength = CStrLength(theDstCStr); + + if (cStrLength < maxCStrLength - 1) + { + theDstCStr[cStrLength++] = theChar; + theDstCStr[cStrLength++] = '\0'; + } + } +} + + + +void ConcatCharToPStr(const char theChar,unsigned char *theDstPStr,const int maxPStrLength) +{ +int pStrLength; + + + if (theDstPStr != nil && maxPStrLength > 0) + { + pStrLength = PStrLength(theDstPStr); + + if (pStrLength < maxPStrLength - 1 && pStrLength < 255) + { + theDstPStr[pStrLength + 1] = theChar; + theDstPStr[0] += 1; + } + } +} + + + + +int CompareCStrs(const char *theFirstCStr,const char *theSecondCStr,const Boolean ignoreCase) +{ +int returnValue; +char firstChar,secondChar; + + + returnValue = 0; + + + if (theFirstCStr != nil && theSecondCStr != nil) + { + for (;;) + { + firstChar = *theFirstCStr; + secondChar = *theSecondCStr; + + if (ignoreCase == true) + { + if (firstChar >= 'A' && firstChar <= 'Z') + { + firstChar = 'a' + (firstChar - 'A'); + } + + if (secondChar >= 'A' && secondChar <= 'Z') + { + secondChar = 'a' + (secondChar - 'A'); + } + } + + + if (firstChar == 0 && secondChar != 0) + { + returnValue = -1; + + break; + } + + else if (firstChar != 0 && secondChar == 0) + { + returnValue = 1; + + break; + } + + else if (firstChar == 0 && secondChar == 0) + { + returnValue = 0; + + break; + } + + else if (firstChar < secondChar) + { + returnValue = -1; + + break; + } + + else if (firstChar > secondChar) + { + returnValue = 1; + + break; + } + + theFirstCStr++; + theSecondCStr++; + } + } + + + return(returnValue); +} + + + +Boolean CStrsAreEqual(const char *theFirstCStr,const char *theSecondCStr,const Boolean ignoreCase) +{ + if (CompareCStrs(theFirstCStr,theSecondCStr,ignoreCase) == 0) + { + return true; + } + + else + { + return false; + } +} + + +Boolean PStrsAreEqual(const unsigned char *theFirstPStr,const unsigned char *theSecondPStr,const Boolean ignoreCase) +{ + if (ComparePStrs(theFirstPStr,theSecondPStr,ignoreCase) == 0) + { + return true; + } + + else + { + return false; + } +} + + + +int ComparePStrs(const unsigned char *theFirstPStr,const unsigned char *theSecondPStr,const Boolean ignoreCase) +{ +int i,returnValue; +char firstChar,secondChar; + + + returnValue = 0; + + + if (theFirstPStr != nil && theSecondPStr != nil) + { + for (i = 1;;i++) + { + firstChar = theFirstPStr[i]; + secondChar = theSecondPStr[i]; + + if (ignoreCase == true) + { + if (firstChar >= 'A' && firstChar <= 'Z') + { + firstChar = 'a' + (firstChar - 'A'); + } + + if (secondChar >= 'A' && secondChar <= 'Z') + { + secondChar = 'a' + (secondChar - 'A'); + } + } + + + if (theFirstPStr[0] < i && theSecondPStr[0] >= i) + { + returnValue = -1; + + break; + } + + else if (theFirstPStr[0] >= i && theSecondPStr[0] < i) + { + returnValue = 1; + + break; + } + + else if (theFirstPStr[0] < i && theSecondPStr[0] < i) + { + returnValue = 0; + + break; + } + + else if (firstChar < secondChar) + { + returnValue = -1; + + break; + } + + else if (firstChar > secondChar) + { + returnValue = 1; + + break; + } + } + } + + + return(returnValue); +} + + + +int CompareCStrToPStr(const char *theCStr,const unsigned char *thePStr,const Boolean ignoreCase) +{ +int returnValue; +char tempString[256]; + + + returnValue = 0; + + if (theCStr != nil && thePStr != nil) + { + CopyPStrToCStr(thePStr,tempString,sizeof(tempString)); + + returnValue = CompareCStrs(theCStr,tempString,ignoreCase); + } + + + return(returnValue); +} + + + +void ConcatLongIntToCStr(const long theNum,char *theCStr,const int maxCStrLength,const int numDigits) +{ +Str255 theStr255; + + + NumToString(theNum,theStr255); + + + if (numDigits > 0) + { + int charsToInsert; + + + charsToInsert = numDigits - PStrLength(theStr255); + + if (charsToInsert > 0) + { + char tempString[256]; + + CopyCStrToCStr("",tempString,sizeof(tempString)); + + for (;charsToInsert > 0;charsToInsert--) + { + ConcatCStrToCStr("0",tempString,sizeof(tempString)); + } + + ConcatPStrToCStr(theStr255,tempString,sizeof(tempString)); + + CopyCStrToPStr(tempString,theStr255,sizeof(theStr255)); + } + } + + + ConcatPStrToCStr(theStr255,theCStr,maxCStrLength); +} + + + + +void ConcatLongIntToPStr(const long theNum,unsigned char *thePStr,const int maxPStrLength,const int numDigits) +{ +Str255 theStr255; + + + NumToString(theNum,theStr255); + + + if (numDigits > 0) + { + int charsToInsert; + + + charsToInsert = numDigits - PStrLength(theStr255); + + if (charsToInsert > 0) + { + char tempString[256]; + + CopyCStrToCStr("",tempString,sizeof(tempString)); + + for (;charsToInsert > 0;charsToInsert--) + { + ConcatCStrToCStr("0",tempString,sizeof(tempString)); + } + + ConcatPStrToCStr(theStr255,tempString,sizeof(tempString)); + + CopyCStrToPStr(tempString,theStr255,sizeof(theStr255)); + } + } + + + ConcatPStrToPStr(theStr255,thePStr,maxPStrLength); +} + + + +void CopyCStrAndConcatLongIntToCStr(const char *theSrcCStr,const long theNum,char *theDstCStr,const int maxDstStrLength) +{ + CopyCStrToCStr(theSrcCStr,theDstCStr,maxDstStrLength); + + ConcatLongIntToCStr(theNum,theDstCStr,maxDstStrLength); +} + + + +void CopyLongIntToCStr(const long theNum,char *theCStr,const int maxCStrLength,const int numDigits) +{ +Str255 theStr255; + + + NumToString(theNum,theStr255); + + + if (numDigits > 0) + { + int charsToInsert; + + + charsToInsert = numDigits - PStrLength(theStr255); + + if (charsToInsert > 0) + { + char tempString[256]; + + CopyCStrToCStr("",tempString,sizeof(tempString)); + + for (;charsToInsert > 0;charsToInsert--) + { + ConcatCStrToCStr("0",tempString,sizeof(tempString)); + } + + ConcatPStrToCStr(theStr255,tempString,sizeof(tempString)); + + CopyCStrToPStr(tempString,theStr255,sizeof(theStr255)); + } + } + + + CopyPStrToCStr(theStr255,theCStr,maxCStrLength); +} + + + + + +void CopyUnsignedLongIntToCStr(const unsigned long theNum,char *theCStr,const int maxCStrLength) +{ +char tempString[256]; +int srcCharIndex,dstCharIndex; +unsigned long tempNum,quotient,remainder; + + + if (theNum == 0) + { + CopyCStrToCStr("0",theCStr,maxCStrLength); + } + + else + { + srcCharIndex = 0; + + tempNum = theNum; + + for (;;) + { + if (srcCharIndex >= sizeof(tempString) - 1 || tempNum == 0) + { + for (dstCharIndex = 0;;) + { + if (dstCharIndex >= maxCStrLength - 1 || srcCharIndex <= 0) + { + theCStr[dstCharIndex] = 0; + + break; + } + + theCStr[dstCharIndex++] = tempString[--srcCharIndex]; + } + + break; + } + + + quotient = tempNum / 10; + + remainder = tempNum - (quotient * 10); + + tempString[srcCharIndex] = '0' + remainder; + + srcCharIndex++; + + tempNum = quotient; + } + } +} + + + + +void CopyLongIntToPStr(const long theNum,unsigned char *thePStr,const int maxPStrLength,const int numDigits) +{ +char tempString[256]; + + + CopyLongIntToCStr(theNum,tempString,sizeof(tempString),numDigits); + + CopyCStrToPStr(tempString,thePStr,maxPStrLength); +} + + + +OSErr CopyLongIntToNewHandle(const long inTheLongInt,Handle *theHandle) +{ +OSErr errCode = noErr; +char tempString[32]; + + + CopyLongIntToCStr(inTheLongInt,tempString,sizeof(tempString)); + + errCode = CopyCStrToNewHandle(tempString,theHandle); + + return(errCode); +} + + +OSErr CopyLongIntToExistingHandle(const long inTheLongInt,Handle theHandle) +{ +OSErr errCode = noErr; +char tempString[32]; + + + CopyLongIntToCStr(inTheLongInt,tempString,sizeof(tempString)); + + errCode = CopyCStrToExistingHandle(tempString,theHandle); + + return(errCode); +} + + + + +OSErr CopyCStrToExistingHandle(const char *theCString,Handle theHandle) +{ +OSErr errCode = noErr; +long stringLength; + + + if (theCString == nil) + { + SetErrorMessageAndBail(("CopyCStrToExistingHandle: Bad parameter, theCString == nil")); + } + + if (theHandle == nil) + { + SetErrorMessageAndBail(("CopyCStrToExistingHandle: Bad parameter, theHandle == nil")); + } + + if (*theHandle == nil) + { + SetErrorMessageAndBail(("CopyCStrToExistingHandle: Bad parameter, *theHandle == nil")); + } + + + + stringLength = CStrLength(theCString) + 1; + + SetHandleSize(theHandle,stringLength); + + if (GetHandleSize(theHandle) < stringLength) + { + SetErrorMessageAndLongIntAndBail("CopyCStrToExistingHandle: Can't set Handle size, MemError() = ",MemError()); + } + + + ::BlockMove(theCString,*theHandle,stringLength); + + +EXITPOINT: + + return(errCode); +} + + + + + +OSErr CopyCStrToNewHandle(const char *theCString,Handle *theHandle) +{ +OSErr errCode = noErr; +long stringLength; + + + if (theCString == nil) + { + SetErrorMessageAndBail(("CopyCStrToNewHandle: Bad parameter, theCString == nil")); + } + + if (theHandle == nil) + { + SetErrorMessageAndBail(("CopyCStrToNewHandle: Bad parameter, theHandle == nil")); + } + + + + stringLength = CStrLength(theCString) + 1; + + *theHandle = NewHandle(stringLength); + + if (*theHandle == nil) + { + SetErrorMessageAndLongIntAndBail("CopyCStrToNewHandle: Can't allocate Handle, MemError() = ",MemError()); + } + + + ::BlockMove(theCString,**theHandle,stringLength); + + +EXITPOINT: + + return(errCode); +} + + + +OSErr CopyPStrToNewHandle(const unsigned char *thePString,Handle *theHandle) +{ +OSErr errCode = noErr; +long stringLength; + + + if (thePString == nil) + { + SetErrorMessageAndBail(("CopyPStrToNewHandle: Bad parameter, thePString == nil")); + } + + if (theHandle == nil) + { + SetErrorMessageAndBail(("CopyPStrToNewHandle: Bad parameter, theHandle == nil")); + } + + + + stringLength = PStrLength(thePString) + 1; + + *theHandle = NewHandle(stringLength); + + if (*theHandle == nil) + { + SetErrorMessageAndLongIntAndBail("CopyPStrToNewHandle: Can't allocate Handle, MemError() = ",MemError()); + } + + + if (stringLength > 1) + { + BlockMove(thePString + 1,**theHandle,stringLength - 1); + } + + (**theHandle)[stringLength - 1] = 0; + + +EXITPOINT: + + return(errCode); +} + + +OSErr AppendPStrToHandle(const unsigned char *thePString,Handle theHandle,long *currentLength) +{ +OSErr errCode = noErr; +char tempString[256]; + + + CopyPStrToCStr(thePString,tempString,sizeof(tempString)); + + errCode = AppendCStrToHandle(tempString,theHandle,currentLength); + + +EXITPOINT: + + return(errCode); +} + + + +OSErr AppendCStrToHandle(const char *theCString,Handle theHandle,long *currentLength,long *maxLength) +{ +OSErr errCode = noErr; +long handleMaxLength,handleCurrentLength,stringLength,byteCount; + + + if (theCString == nil) + { + SetErrorMessageAndBail(("AppendCStrToHandle: Bad parameter, theCString == nil")); + } + + if (theHandle == nil) + { + SetErrorMessageAndBail(("AppendCStrToHandle: Bad parameter, theHandle == nil")); + } + + + if (maxLength != nil) + { + handleMaxLength = *maxLength; + } + + else + { + handleMaxLength = GetHandleSize(theHandle); + } + + + if (currentLength != nil && *currentLength >= 0) + { + handleCurrentLength = *currentLength; + } + + else + { + handleCurrentLength = CStrLength(*theHandle); + } + + + stringLength = CStrLength(theCString); + + byteCount = handleCurrentLength + stringLength + 1; + + if (byteCount > handleMaxLength) + { + SetHandleSize(theHandle,handleCurrentLength + stringLength + 1); + + if (maxLength != nil) + { + *maxLength = GetHandleSize(theHandle); + + handleMaxLength = *maxLength; + } + + else + { + handleMaxLength = GetHandleSize(theHandle); + } + + if (byteCount > handleMaxLength) + { + SetErrorMessageAndLongIntAndBail("AppendCStrToHandle: Can't increase Handle allocation, MemError() = ",MemError()); + } + } + + + BlockMove(theCString,*theHandle + handleCurrentLength,stringLength + 1); + + + if (currentLength != nil) + { + *currentLength += stringLength; + } + + + errCode = noErr; + + +EXITPOINT: + + return(errCode); +} + + + +OSErr AppendCharsToHandle(const char *theChars,const int numChars,Handle theHandle,long *currentLength,long *maxLength) +{ +OSErr errCode = noErr; +long handleMaxLength,handleCurrentLength,byteCount; + + + if (theChars == nil) + { + SetErrorMessageAndBail(("AppendCharsToHandle: Bad parameter, theChars == nil")); + } + + if (theHandle == nil) + { + SetErrorMessageAndBail(("AppendCharsToHandle: Bad parameter, theHandle == nil")); + } + + + if (maxLength != nil) + { + handleMaxLength = *maxLength; + } + + else + { + handleMaxLength = GetHandleSize(theHandle); + } + + + if (currentLength != nil && *currentLength >= 0) + { + handleCurrentLength = *currentLength; + } + + else + { + handleCurrentLength = CStrLength(*theHandle); + } + + + byteCount = handleCurrentLength + numChars + 1; + + if (byteCount > handleMaxLength) + { + SetHandleSize(theHandle,handleCurrentLength + numChars + 1); + + if (maxLength != nil) + { + *maxLength = GetHandleSize(theHandle); + + handleMaxLength = *maxLength; + } + + else + { + handleMaxLength = GetHandleSize(theHandle); + } + + if (byteCount > handleMaxLength) + { + SetErrorMessageAndLongIntAndBail("AppendCharsToHandle: Can't increase Handle allocation, MemError() = ",MemError()); + } + } + + + BlockMove(theChars,*theHandle + handleCurrentLength,numChars); + + (*theHandle)[handleCurrentLength + numChars] = '\0'; + + if (currentLength != nil) + { + *currentLength += numChars; + } + + + errCode = noErr; + + +EXITPOINT: + + return(errCode); +} + + + +OSErr AppendLongIntToHandle(const long inTheLongInt,Handle theHandle,long *currentLength) +{ +OSErr errCode = noErr; +char tempString[32]; + + + CopyLongIntToCStr(inTheLongInt,tempString,sizeof(tempString)); + + errCode = AppendCStrToHandle(tempString,theHandle,currentLength); + + return(errCode); +} + + + + +long CStrLength(const char *theCString) +{ +long cStrLength = 0; + + + if (theCString != nil) + { + for (cStrLength = 0;theCString[cStrLength] != 0;cStrLength++) + { + + } + } + + + return(cStrLength); +} + + + +long PStrLength(const unsigned char *thePString) +{ +long pStrLength = 0; + + + if (thePString != nil) + { + pStrLength = thePString[0]; + } + + + return(pStrLength); +} + + + + + +void ZeroMem(void *theMemPtr,const unsigned long numBytes) +{ +unsigned char *theBytePtr; +unsigned long *theLongPtr; +unsigned long numSingleBytes; +unsigned long theNumBytes; + + + theNumBytes = numBytes; + + if (theMemPtr != nil && theNumBytes > 0) + { + theBytePtr = (unsigned char *) theMemPtr; + + numSingleBytes = (unsigned long) theBytePtr & 0x0003; + + while (numSingleBytes > 0) + { + *theBytePtr++ = 0; + + theNumBytes--; + numSingleBytes--; + } + + + theLongPtr = (unsigned long *) theBytePtr; + + while (theNumBytes >= 4) + { + *theLongPtr++ = 0; + + theNumBytes -= 4; + } + + + theBytePtr = (unsigned char *) theLongPtr; + + while (theNumBytes > 0) + { + *theBytePtr++ = 0; + + theNumBytes--; + } + } +} + + + + +char *FindCharInCStr(const char theChar,const char *theCString) +{ +char *theStringSearchPtr; + + + theStringSearchPtr = (char *) theCString; + + if (theStringSearchPtr != nil) + { + while (*theStringSearchPtr != '\0' && *theStringSearchPtr != theChar) + { + theStringSearchPtr++; + } + + if (*theStringSearchPtr == '\0') + { + theStringSearchPtr = nil; + } + } + + return(theStringSearchPtr); +} + + + +long FindCharOffsetInCStr(const char theChar,const char *theCString,const Boolean inIgnoreCase) +{ +long theOffset = -1; + + + if (theCString != nil) + { + theOffset = 0; + + + if (inIgnoreCase) + { + char searchChar = theChar; + + if (searchChar >= 'a' && searchChar <= 'z') + { + searchChar = searchChar - 'a' + 'A'; + } + + + while (*theCString != 0) + { + char currentChar = *theCString; + + if (currentChar >= 'a' && currentChar <= 'z') + { + currentChar = currentChar - 'a' + 'A'; + } + + if (currentChar == searchChar) + { + break; + } + + theCString++; + theOffset++; + } + } + + else + { + while (*theCString != 0 && *theCString != theChar) + { + theCString++; + theOffset++; + } + } + + if (*theCString == 0) + { + theOffset = -1; + } + } + + return(theOffset); +} + + +long FindCStrOffsetInCStr(const char *theCSubstring,const char *theCString,const Boolean inIgnoreCase) +{ +long theOffset = -1; + + + if (theCSubstring != nil && theCString != nil) + { + for (theOffset = 0;;theOffset++) + { + if (theCString[theOffset] == 0) + { + theOffset = -1; + + goto EXITPOINT; + } + + + for (const char *tempSubstringPtr = theCSubstring,*tempCStringPtr = theCString + theOffset;;tempSubstringPtr++,tempCStringPtr++) + { + if (*tempSubstringPtr == 0) + { + goto EXITPOINT; + } + + else if (*tempCStringPtr == 0) + { + break; + } + + char searchChar = *tempSubstringPtr; + char currentChar = *tempCStringPtr; + + if (inIgnoreCase && searchChar >= 'a' && searchChar <= 'z') + { + searchChar = searchChar - 'a' + 'A'; + } + + if (inIgnoreCase && currentChar >= 'a' && currentChar <= 'z') + { + currentChar = currentChar - 'a' + 'A'; + } + + if (currentChar != searchChar) + { + break; + } + } + } + + theOffset = -1; + } + + +EXITPOINT: + + return(theOffset); +} + + + +void InsertCStrIntoCStr(const char *theSrcCStr,const int theInsertionOffset,char *theDstCStr,const int maxDstStrLength) +{ +int currentLength; +int insertLength; +int numCharsToInsert; +int numCharsToShift; + + + if (theDstCStr != nil && theSrcCStr != nil && maxDstStrLength > 0 && theInsertionOffset < maxDstStrLength - 1) + { + currentLength = CStrLength(theDstCStr); + + insertLength = CStrLength(theSrcCStr); + + + if (theInsertionOffset + insertLength < maxDstStrLength - 1) + { + numCharsToInsert = insertLength; + } + + else + { + numCharsToInsert = maxDstStrLength - 1 - theInsertionOffset; + } + + + if (numCharsToInsert + currentLength < maxDstStrLength - 1) + { + numCharsToShift = currentLength - theInsertionOffset; + } + + else + { + numCharsToShift = maxDstStrLength - 1 - theInsertionOffset - numCharsToInsert; + } + + + if (numCharsToShift > 0) + { + BlockMove(theDstCStr + theInsertionOffset,theDstCStr + theInsertionOffset + numCharsToInsert,numCharsToShift); + } + + if (numCharsToInsert > 0) + { + BlockMove(theSrcCStr,theDstCStr + theInsertionOffset,numCharsToInsert); + } + + theDstCStr[theInsertionOffset + numCharsToInsert + numCharsToShift] = 0; + } +} + + + +void InsertPStrIntoCStr(const unsigned char *theSrcPStr,const int theInsertionOffset,char *theDstCStr,const int maxDstStrLength) +{ +int currentLength; +int insertLength; +int numCharsToInsert; +int numCharsToShift; + + + if (theDstCStr != nil && theSrcPStr != nil && maxDstStrLength > 0 && theInsertionOffset < maxDstStrLength - 1) + { + currentLength = CStrLength(theDstCStr); + + insertLength = PStrLength(theSrcPStr); + + + if (theInsertionOffset + insertLength < maxDstStrLength - 1) + { + numCharsToInsert = insertLength; + } + + else + { + numCharsToInsert = maxDstStrLength - 1 - theInsertionOffset; + } + + + if (numCharsToInsert + currentLength < maxDstStrLength - 1) + { + numCharsToShift = currentLength - theInsertionOffset; + } + + else + { + numCharsToShift = maxDstStrLength - 1 - theInsertionOffset - numCharsToInsert; + } + + + if (numCharsToShift > 0) + { + BlockMove(theDstCStr + theInsertionOffset,theDstCStr + theInsertionOffset + numCharsToInsert,numCharsToShift); + } + + if (numCharsToInsert > 0) + { + BlockMove(theSrcPStr + 1,theDstCStr + theInsertionOffset,numCharsToInsert); + } + + theDstCStr[theInsertionOffset + numCharsToInsert + numCharsToShift] = 0; + } +} + + + +OSErr InsertCStrIntoHandle(const char *theCString,Handle theHandle,const long inInsertOffset) +{ +OSErr errCode; +int currentLength; +int insertLength; + + + SetErrorMessageAndBailIfNil(theCString,"InsertCStrIntoHandle: Bad parameter, theCString == nil"); + + SetErrorMessageAndBailIfNil(theHandle,"InsertCStrIntoHandle: Bad parameter, theHandle == nil"); + + currentLength = CStrLength(*theHandle); + + if (currentLength + 1 > ::GetHandleSize(theHandle)) + { + SetErrorMessageAndBail("InsertCStrIntoHandle: Handle has been overflowed"); + } + + if (inInsertOffset > currentLength) + { + SetErrorMessageAndBail("InsertCStrIntoHandle: Insertion offset is greater than string length"); + } + + insertLength = CStrLength(theCString); + + ::SetHandleSize(theHandle,currentLength + 1 + insertLength); + + if (::GetHandleSize(theHandle) < currentLength + 1 + insertLength) + { + SetErrorMessageAndLongIntAndBail("InsertCStrIntoHandle: Can't expand storage for Handle, MemError() = ",MemError()); + } + + ::BlockMove(*theHandle + inInsertOffset,*theHandle + inInsertOffset + insertLength,currentLength - inInsertOffset + 1); + + ::BlockMove(theCString,*theHandle + inInsertOffset,insertLength); + + + errCode = noErr; + + +EXITPOINT: + + return(errCode); +} + + + + +void CopyCStrAndInsert1LongIntIntoCStr(const char *theSrcCStr,const long theNum,char *theDstCStr,const int maxDstStrLength) +{ + CopyCStrAndInsertCStrLongIntIntoCStr(theSrcCStr,nil,theNum,theDstCStr,maxDstStrLength); +} + + +void CopyCStrAndInsert2LongIntsIntoCStr(const char *theSrcCStr,const long long1,const long long2,char *theDstCStr,const int maxDstStrLength) +{ +const long theLongInts[] = { long1,long2 }; + + CopyCStrAndInsertCStrsLongIntsIntoCStr(theSrcCStr,nil,theLongInts,theDstCStr,maxDstStrLength); +} + + +void CopyCStrAndInsert3LongIntsIntoCStr(const char *theSrcCStr,const long long1,const long long2,const long long3,char *theDstCStr,const int maxDstStrLength) +{ +const long theLongInts[] = { long1,long2,long3 }; + + CopyCStrAndInsertCStrsLongIntsIntoCStr(theSrcCStr,nil,theLongInts,theDstCStr,maxDstStrLength); +} + + +void CopyCStrAndInsertCStrIntoCStr(const char *theSrcCStr,const char *theInsertCStr,char *theDstCStr,const int maxDstStrLength) +{ +const char *theCStrs[2] = { theInsertCStr,nil }; + + CopyCStrAndInsertCStrsLongIntsIntoCStr(theSrcCStr,theCStrs,nil,theDstCStr,maxDstStrLength); +} + + + +void CopyCStrAndInsertCStrLongIntIntoCStr(const char *theSrcCStr,const char *theInsertCStr,const long theNum,char *theDstCStr,const int maxDstStrLength) +{ +const char *theCStrs[2] = { theInsertCStr,nil }; +const long theLongInts[1] = { theNum }; + + CopyCStrAndInsertCStrsLongIntsIntoCStr(theSrcCStr,theCStrs,theLongInts,theDstCStr,maxDstStrLength); +} + + + +void CopyCStrAndInsertCStrsLongIntsIntoCStr(const char *theSrcCStr,const char **theInsertCStrs,const long *theLongInts,char *theDstCStr,const int maxDstStrLength) +{ +int dstCharIndex,srcCharIndex,theMaxDstStrLength; +int theCStrIndex = 0; +int theLongIntIndex = 0; + + + theMaxDstStrLength = maxDstStrLength; + + if (theDstCStr != nil && theSrcCStr != nil && theMaxDstStrLength > 0) + { + dstCharIndex = 0; + + srcCharIndex = 0; + + + // Allow room for NULL at end of string + + theMaxDstStrLength--; + + + for (;;) + { + // Hit end of buffer? + + if (dstCharIndex >= theMaxDstStrLength) + { + theDstCStr[dstCharIndex++] = 0; + + goto EXITPOINT; + } + + // End of source string? + + else if (theSrcCStr[srcCharIndex] == 0) + { + theDstCStr[dstCharIndex++] = 0; + + goto EXITPOINT; + } + + // Did we find a '%s'? + + else if (theInsertCStrs != nil && theInsertCStrs[theCStrIndex] != nil && theSrcCStr[srcCharIndex] == '%' && theSrcCStr[srcCharIndex + 1] == 's') + { + // Skip over the '%s' + + srcCharIndex += 2; + + + // Terminate the dest string and then concat the string + + theDstCStr[dstCharIndex] = 0; + + ConcatCStrToCStr(theInsertCStrs[theCStrIndex],theDstCStr,theMaxDstStrLength); + + dstCharIndex = CStrLength(theDstCStr); + + theCStrIndex++; + } + + // Did we find a '%ld'? + + else if (theLongInts != nil && theSrcCStr[srcCharIndex] == '%' && theSrcCStr[srcCharIndex + 1] == 'l' && theSrcCStr[srcCharIndex + 2] == 'd') + { + // Skip over the '%ld' + + srcCharIndex += 3; + + + // Terminate the dest string and then concat the number + + theDstCStr[dstCharIndex] = 0; + + ConcatLongIntToCStr(theLongInts[theLongIntIndex],theDstCStr,theMaxDstStrLength); + + theLongIntIndex++; + + dstCharIndex = CStrLength(theDstCStr); + } + + else + { + theDstCStr[dstCharIndex++] = theSrcCStr[srcCharIndex++]; + } + } + } + + + +EXITPOINT: + + return; +} + + + + + +OSErr CopyCStrAndInsertCStrLongIntIntoHandle(const char *theSrcCStr,const char *theInsertCStr,const long theNum,Handle *theHandle) +{ +OSErr errCode; +long byteCount; + + + if (theHandle != nil) + { + byteCount = CStrLength(theSrcCStr) + CStrLength(theInsertCStr) + 32; + + *theHandle = NewHandle(byteCount); + + if (*theHandle == nil) + { + SetErrorMessageAndLongIntAndBail("CopyCStrAndInsertCStrLongIntIntoHandle: Can't allocate Handle, MemError() = ",MemError()); + } + + + HLock(*theHandle); + + CopyCStrAndInsertCStrLongIntIntoCStr(theSrcCStr,theInsertCStr,theNum,**theHandle,byteCount); + + HUnlock(*theHandle); + } + + errCode = noErr; + + +EXITPOINT: + + return(errCode); +} + + + + + +OSErr CopyIndexedWordToCStr(char *theSrcCStr,int whichWord,char *theDstCStr,int maxDstCStrLength) +{ +OSErr errCode; +char *srcCharPtr,*dstCharPtr; +int wordCount; +int byteCount; + + + if (theSrcCStr == nil) + { + SetErrorMessageAndBail(("CopyIndexedWordToCStr: Bad parameter, theSrcCStr == nil")); + } + + if (theDstCStr == nil) + { + SetErrorMessageAndBail(("CopyIndexedWordToCStr: Bad parameter, theDstCStr == nil")); + } + + if (whichWord < 0) + { + SetErrorMessageAndBail(("CopyIndexedWordToCStr: Bad parameter, whichWord < 0")); + } + + if (maxDstCStrLength <= 0) + { + SetErrorMessageAndBail(("CopyIndexedWordToCStr: Bad parameter, maxDstCStrLength <= 0")); + } + + + *theDstCStr = '\0'; + + srcCharPtr = theSrcCStr; + + while (*srcCharPtr == ' ' || *srcCharPtr == '\t') + { + srcCharPtr++; + } + + + for (wordCount = 0;wordCount < whichWord;wordCount++) + { + while (*srcCharPtr != ' ' && *srcCharPtr != '\t' && *srcCharPtr != '\r' && *srcCharPtr != '\n' && *srcCharPtr != '\0') + { + srcCharPtr++; + } + + if (*srcCharPtr == '\r' || *srcCharPtr == '\n' || *srcCharPtr == '\0') + { + errCode = noErr; + + goto EXITPOINT; + } + + while (*srcCharPtr == ' ' || *srcCharPtr == '\t') + { + srcCharPtr++; + } + + if (*srcCharPtr == '\r' || *srcCharPtr == '\n' || *srcCharPtr == '\0') + { + errCode = noErr; + + goto EXITPOINT; + } + } + + + dstCharPtr = theDstCStr; + byteCount = 0; + + + for(;;) + { + if (byteCount >= maxDstCStrLength - 1 || *srcCharPtr == '\0' || *srcCharPtr == ' ' || *srcCharPtr == '\t' || *srcCharPtr == '\r' || *srcCharPtr == '\n') + { + *dstCharPtr = '\0'; + break; + } + + *dstCharPtr++ = *srcCharPtr++; + + byteCount++; + } + + + errCode = noErr; + + +EXITPOINT: + + return(errCode); +} + + + + + +OSErr CopyIndexedWordToNewHandle(char *theSrcCStr,int whichWord,Handle *outTheHandle) +{ +OSErr errCode; +char *srcCharPtr; +int wordCount; +int byteCount; + + + if (theSrcCStr == nil) + { + SetErrorMessageAndBail(("CopyIndexedWordToNewHandle: Bad parameter, theSrcCStr == nil")); + } + + if (outTheHandle == nil) + { + SetErrorMessageAndBail(("CopyIndexedWordToNewHandle: Bad parameter, outTheHandle == nil")); + } + + if (whichWord < 0) + { + SetErrorMessageAndBail(("CopyIndexedWordToNewHandle: Bad parameter, whichWord < 0")); + } + + + *outTheHandle = nil; + + + srcCharPtr = theSrcCStr; + + while (*srcCharPtr == ' ' || *srcCharPtr == '\t') + { + srcCharPtr++; + } + + + for (wordCount = 0;wordCount < whichWord;wordCount++) + { + while (*srcCharPtr != ' ' && *srcCharPtr != '\t' && *srcCharPtr != '\r' && *srcCharPtr != '\n' && *srcCharPtr != '\0') + { + srcCharPtr++; + } + + if (*srcCharPtr == '\r' || *srcCharPtr == '\n' || *srcCharPtr == '\0') + { + break; + } + + while (*srcCharPtr == ' ' || *srcCharPtr == '\t') + { + srcCharPtr++; + } + + if (*srcCharPtr == '\r' || *srcCharPtr == '\n' || *srcCharPtr == '\0') + { + break; + } + } + + + for (byteCount = 0;;byteCount++) + { + if (srcCharPtr[byteCount] == ' ' || srcCharPtr[byteCount] == '\t' || srcCharPtr[byteCount] == '\r' || srcCharPtr[byteCount] == '\n' || srcCharPtr[byteCount] == '\0') + { + break; + } + } + + + *outTheHandle = NewHandle(byteCount + 1); + + if (*outTheHandle == nil) + { + SetErrorMessageAndLongIntAndBail("CopyIndexedWordToNewHandle: Can't allocate Handle, MemError() = ",MemError()); + } + + + ::BlockMove(srcCharPtr,**outTheHandle,byteCount); + + (**outTheHandle)[byteCount] = '\0'; + + errCode = noErr; + + +EXITPOINT: + + return(errCode); +} + + + +OSErr CopyIndexedLineToCStr(const char *theSrcCStr,int inWhichLine,int *lineEndIndex,Boolean *gotLastLine,char *theDstCStr,const int maxDstCStrLength) +{ +OSErr errCode; +int theCurrentLine; +int theCurrentLineOffset; +int theEOSOffset; + + + if (theSrcCStr == nil) + { + SetErrorMessageAndBail(("CopyIndexedLineToCStr: Bad parameter, theSrcCStr == nil")); + } + + if (theDstCStr == nil) + { + SetErrorMessageAndBail(("CopyIndexedLineToCStr: Bad parameter, theDstCStr == nil")); + } + + if (inWhichLine < 0) + { + SetErrorMessageAndBail(("CopyIndexedLineToCStr: Bad parameter, inWhichLine < 0")); + } + + if (maxDstCStrLength <= 0) + { + SetErrorMessageAndBail(("CopyIndexedLineToCStr: Bad parameter, maxDstCStrLength <= 0")); + } + + + if (gotLastLine != nil) + { + *gotLastLine = false; + } + + + *theDstCStr = 0; + + theCurrentLineOffset = 0; + + theCurrentLine = 0; + + + while (theCurrentLine < inWhichLine) + { + while (theSrcCStr[theCurrentLineOffset] != '\r' && theSrcCStr[theCurrentLineOffset] != 0) + { + theCurrentLineOffset++; + } + + if (theSrcCStr[theCurrentLineOffset] == 0) + { + break; + } + + theCurrentLineOffset++; + theCurrentLine++; + } + + if (theSrcCStr[theCurrentLineOffset] == 0) + { + SetErrorMessageAndLongIntAndBail("CopyIndexedLineToCStr: Too few lines in source text, can't get line ",inWhichLine); + } + + + theEOSOffset = FindCharOffsetInCStr('\r',theSrcCStr + theCurrentLineOffset); + + if (theEOSOffset >= 0) + { + CopyCSubstrToCStr(theSrcCStr + theCurrentLineOffset,theEOSOffset,theDstCStr,maxDstCStrLength); + + if (gotLastLine != nil) + { + *gotLastLine = false; + } + + if (lineEndIndex != nil) + { + *lineEndIndex = theEOSOffset; + } + } + + else + { + theEOSOffset = CStrLength(theSrcCStr + theCurrentLineOffset); + + CopyCSubstrToCStr(theSrcCStr + theCurrentLineOffset,theEOSOffset,theDstCStr,maxDstCStrLength); + + if (gotLastLine != nil) + { + *gotLastLine = true; + } + + if (lineEndIndex != nil) + { + *lineEndIndex = theEOSOffset; + } + } + + + errCode = noErr; + + +EXITPOINT: + + return(errCode); +} + + + +OSErr CopyIndexedLineToNewHandle(const char *theSrcCStr,int inWhichLine,Handle *outNewHandle) +{ +OSErr errCode; +int theCurrentLine; +int theCurrentLineOffset; +int byteCount; + + + SetErrorMessageAndBailIfNil(theSrcCStr,"CopyIndexedLineToNewHandle: Bad parameter, theSrcCStr == nil"); + SetErrorMessageAndBailIfNil(outNewHandle,"CopyIndexedLineToNewHandle: Bad parameter, outNewHandle == nil"); + + if (inWhichLine < 0) + { + SetErrorMessageAndBail(("CopyIndexedLineToNewHandle: Bad parameter, inWhichLine < 0")); + } + + + theCurrentLineOffset = 0; + + theCurrentLine = 0; + + + while (theCurrentLine < inWhichLine) + { + while (theSrcCStr[theCurrentLineOffset] != '\r' && theSrcCStr[theCurrentLineOffset] != '\0') + { + theCurrentLineOffset++; + } + + if (theSrcCStr[theCurrentLineOffset] == '\0') + { + break; + } + + theCurrentLineOffset++; + theCurrentLine++; + } + + if (theSrcCStr[theCurrentLineOffset] == '\0') + { + SetErrorMessageAndLongIntAndBail("CopyIndexedLineToNewHandle: Too few lines in source text, can't get line #",inWhichLine); + } + + + byteCount = 0; + + while (theSrcCStr[theCurrentLineOffset + byteCount] != '\r' && theSrcCStr[theCurrentLineOffset + byteCount] != '\0') + { + byteCount++; + } + + + *outNewHandle = NewHandle(byteCount + 1); + + if (*outNewHandle == nil) + { + SetErrorMessageAndLongIntAndBail("CopyIndexedLineToNewHandle: Can't allocate Handle, MemError() = ",MemError()); + } + + ::BlockMove(theSrcCStr + theCurrentLineOffset,**outNewHandle,byteCount); + + (**outNewHandle)[byteCount] = '\0'; + + errCode = noErr; + + +EXITPOINT: + + return(errCode); +} + + + + +OSErr CountDigits(const char *inCStr,int *outNumIntegerDigits,int *outNumFractDigits) +{ +OSErr errCode = noErr; +int numIntDigits = 0; +int numFractDigits = 0; +int digitIndex = 0; + + + SetErrorMessageAndBailIfNil(inCStr,"CountDigits: Bad parameter, theSrcCStr == nil"); + SetErrorMessageAndBailIfNil(outNumIntegerDigits,"CountDigits: Bad parameter, outNumIntegerDigits == nil"); + SetErrorMessageAndBailIfNil(outNumFractDigits,"CountDigits: Bad parameter, outNumFractDigits == nil"); + + digitIndex = 0; + + while (inCStr[digitIndex] >= '0' && inCStr[digitIndex] <= '9') + { + digitIndex++; + numIntDigits++; + } + + if (inCStr[digitIndex] == '.') + { + digitIndex++; + + while (inCStr[digitIndex] >= '0' && inCStr[digitIndex] <= '9') + { + digitIndex++; + numFractDigits++; + } + } + + *outNumIntegerDigits = numIntDigits; + + *outNumFractDigits = numFractDigits; + + errCode = noErr; + +EXITPOINT: + + return(errCode); +} + + + +OSErr ExtractIntFromCStr(const char *theSrcCStr,int *outInt,Boolean skipLeadingSpaces) +{ +OSErr errCode; +int theCharIndex; + + + if (theSrcCStr == nil) + { + SetErrorMessageAndBail(("ExtractIntFromCStr: Bad parameter, theSrcCStr == nil")); + } + + if (outInt == nil) + { + SetErrorMessageAndBail(("ExtractIntFromCStr: Bad parameter, outInt == nil")); + } + + + *outInt = 0; + + theCharIndex = 0; + + if (skipLeadingSpaces == true) + { + while (theSrcCStr[theCharIndex] == ' ') + { + theCharIndex++; + } + } + + if (theSrcCStr[theCharIndex] < '0' || theSrcCStr[theCharIndex] > '9') + { + SetErrorMessageAndBail(("ExtractIntFromCStr: Bad parameter, theSrcCStr contains a bogus numeric representation")); + } + + + while (theSrcCStr[theCharIndex] >= '0' && theSrcCStr[theCharIndex] <= '9') + { + *outInt = (*outInt * 10) + (theSrcCStr[theCharIndex] - '0'); + + theCharIndex++; + } + + + errCode = noErr; + + +EXITPOINT: + + return(errCode); +} + + + +OSErr ExtractIntFromPStr(const unsigned char *theSrcPStr,int *outInt,Boolean skipLeadingSpaces) +{ +OSErr errCode; +char theCStr[256]; + + + if (theSrcPStr == nil) + { + SetErrorMessageAndBail(("ExtractIntFromPStr: Bad parameter, theSrcPStr == nil")); + } + + if (outInt == nil) + { + SetErrorMessageAndBail(("ExtractIntFromPStr: Bad parameter, outInt == nil")); + } + + + CopyPStrToCStr(theSrcPStr,theCStr,sizeof(theCStr)); + + + errCode = ExtractIntFromCStr(theCStr,outInt,skipLeadingSpaces); + + +EXITPOINT: + + return(errCode); +} + + + +int CountOccurencesOfCharInCStr(const char inChar,const char *inSrcCStr) +{ +int theSrcCharIndex; +int numOccurrences = -1; + + + if (inSrcCStr != nil && inChar != '\0') + { + numOccurrences = 0; + + for (theSrcCharIndex = 0;inSrcCStr[theSrcCharIndex] != '\0';theSrcCharIndex++) + { + if (inSrcCStr[theSrcCharIndex] == inChar) + { + numOccurrences++; + } + } + } + + return(numOccurrences); +} + + +int CountWordsInCStr(const char *inSrcCStr) +{ +int numWords = -1; + + + if (inSrcCStr != nil) + { + numWords = 0; + + // Skip lead spaces + + while (*inSrcCStr == ' ') + { + inSrcCStr++; + } + + while (*inSrcCStr != '\0') + { + numWords++; + + while (*inSrcCStr != ' ' && *inSrcCStr != '\0') + { + inSrcCStr++; + } + + while (*inSrcCStr == ' ') + { + inSrcCStr++; + } + } + } + + return(numWords); +} + + + + +void ConvertCStrToUpperCase(char *theSrcCStr) +{ +char *theCharPtr; + + + if (theSrcCStr != nil) + { + theCharPtr = theSrcCStr; + + while (*theCharPtr != 0) + { + if (*theCharPtr >= 'a' && *theCharPtr <= 'z') + { + *theCharPtr = *theCharPtr - 'a' + 'A'; + } + + theCharPtr++; + } + } +} + + + + + + + +void ExtractCStrItemFromCStr(const char *inSrcCStr,const char inItemDelimiter,const int inItemNumber,Boolean *foundItem,char *outDstCharPtr,const int inDstCharPtrMaxLength,const Boolean inTreatMultipleDelimsAsSingleDelim) +{ +int theItem; +int theSrcCharIndex; +int theDstCharIndex; + + + if (foundItem != nil) + { + *foundItem = false; + } + + + if (outDstCharPtr != nil && inDstCharPtrMaxLength > 0 && inItemNumber >= 0 && inItemDelimiter != 0) + { + *outDstCharPtr = 0; + + + theSrcCharIndex = 0; + + for (theItem = 0;theItem < inItemNumber;theItem++) + { + while (inSrcCStr[theSrcCharIndex] != inItemDelimiter && inSrcCStr[theSrcCharIndex] != '\0') + { + theSrcCharIndex++; + } + + if (inSrcCStr[theSrcCharIndex] == inItemDelimiter) + { + theSrcCharIndex++; + + if (inTreatMultipleDelimsAsSingleDelim) + { + while (inSrcCStr[theSrcCharIndex] == inItemDelimiter) + { + theSrcCharIndex++; + } + } + } + + + if (inSrcCStr[theSrcCharIndex] == '\0') + { + goto EXITPOINT; + } + } + + + if (foundItem != nil) + { + *foundItem = true; + } + + + theDstCharIndex = 0; + + for (;;) + { + if (inSrcCStr[theSrcCharIndex] == 0 || inSrcCStr[theSrcCharIndex] == inItemDelimiter || theDstCharIndex >= inDstCharPtrMaxLength - 1) + { + outDstCharPtr[theDstCharIndex] = 0; + + break; + } + + outDstCharPtr[theDstCharIndex++] = inSrcCStr[theSrcCharIndex++]; + } + } + + +EXITPOINT: + + return; +} + + + +OSErr ExtractCStrItemFromCStrIntoNewHandle(const char *inSrcCStr,const char inItemDelimiter,const int inItemNumber,Boolean *foundItem,Handle *outNewHandle,const Boolean inTreatMultipleDelimsAsSingleDelim) +{ +OSErr errCode; +int theItem; +int theSrcCharIndex; +int theItemLength; + + + if (inSrcCStr == nil) + { + SetErrorMessage("ExtractCStrItemFromCStrIntoNewHandle: Bad parameter, inSrcCStr == nil"); + errCode = kGenericError; + goto EXITPOINT; + } + + if (outNewHandle == nil) + { + SetErrorMessage("ExtractCStrItemFromCStrIntoNewHandle: Bad parameter, outNewHandle == nil"); + errCode = kGenericError; + goto EXITPOINT; + } + + if (foundItem == nil) + { + SetErrorMessage("ExtractCStrItemFromCStrIntoNewHandle: Bad parameter, foundItem == nil"); + errCode = kGenericError; + goto EXITPOINT; + } + + if (inItemNumber < 0) + { + SetErrorMessage("ExtractCStrItemFromCStrIntoNewHandle: Bad parameter, inItemNumber < 0"); + errCode = kGenericError; + goto EXITPOINT; + } + + if (inItemDelimiter == 0) + { + SetErrorMessage("ExtractCStrItemFromCStrIntoNewHandle: Bad parameter, inItemDelimiter == 0"); + errCode = kGenericError; + goto EXITPOINT; + } + + + *foundItem = false; + + theSrcCharIndex = 0; + + for (theItem = 0;theItem < inItemNumber;theItem++) + { + while (inSrcCStr[theSrcCharIndex] != inItemDelimiter && inSrcCStr[theSrcCharIndex] != '\0') + { + theSrcCharIndex++; + } + + if (inSrcCStr[theSrcCharIndex] == inItemDelimiter) + { + theSrcCharIndex++; + + if (inTreatMultipleDelimsAsSingleDelim) + { + while (inSrcCStr[theSrcCharIndex] == inItemDelimiter) + { + theSrcCharIndex++; + } + } + } + + + if (inSrcCStr[theSrcCharIndex] == '\0') + { + errCode = noErr; + + goto EXITPOINT; + } + } + + + *foundItem = true; + + + for (theItemLength = 0;;theItemLength++) + { + if (inSrcCStr[theSrcCharIndex + theItemLength] == 0 || inSrcCStr[theSrcCharIndex + theItemLength] == inItemDelimiter) + { + break; + } + } + + + *outNewHandle = NewHandle(theItemLength + 1); + + if (*outNewHandle == nil) + { + SetErrorMessageAndLongIntAndBail("ExtractCStrItemFromCStrIntoNewHandle: Can't allocate Handle, MemError() = ",MemError()); + } + + + BlockMove(inSrcCStr + theSrcCharIndex,**outNewHandle,theItemLength); + + (**outNewHandle)[theItemLength] = 0; + + errCode = noErr; + + +EXITPOINT: + + return(errCode); +} + + + + + + +OSErr ExtractFloatFromCStr(const char *inCString,extended80 *outFloat) +{ +OSErr errCode; +Str255 theStr255; +Handle theNumberPartsTableHandle = nil; +long theNumberPartsOffset,theNumberPartsLength; +FormatResultType theFormatResultType; +NumberParts theNumberPartsTable; +NumFormatStringRec theNumFormatStringRec; + + + if (inCString == nil) + { + SetErrorMessage("ExtractFloatFromCStr: Bad parameter, inCString == nil"); + errCode = kGenericError; + goto EXITPOINT; + } + + if (outFloat == nil) + { + SetErrorMessage("ExtractFloatFromCStr: Bad parameter, outFloat == nil"); + errCode = kGenericError; + goto EXITPOINT; + } + + +// GetIntlResourceTable(smRoman,smNumberPartsTable,&theNumberPartsTableHandle,&theNumberPartsOffset,&theNumberPartsLength); + + GetIntlResourceTable(GetScriptManagerVariable(smSysScript),smNumberPartsTable,&theNumberPartsTableHandle,&theNumberPartsOffset,&theNumberPartsLength); + + if (theNumberPartsTableHandle == nil) + { + SetErrorMessage("ExtractFloatFromCStr: Can't get number parts table for converting string representations to/from numeric representations"); + errCode = kGenericError; + goto EXITPOINT; + } + + if (theNumberPartsLength > sizeof(theNumberPartsTable)) + { + SetErrorMessage("ExtractFloatFromCStr: Number parts table has bad length"); + errCode = kGenericError; + goto EXITPOINT; + } + + + BlockMove(*theNumberPartsTableHandle + theNumberPartsOffset,&theNumberPartsTable,theNumberPartsLength); + + + theFormatResultType = (FormatResultType) StringToFormatRec(kNumberFormatString,&theNumberPartsTable,&theNumFormatStringRec); + + if (theFormatResultType != fFormatOK) + { + SetErrorMessage("ExtractFloatFromCStr: StringToFormatRec() != fFormatOK"); + errCode = kGenericError; + goto EXITPOINT; + } + + + CopyCStrToPStr(inCString,theStr255,sizeof(theStr255)); + + + theFormatResultType = (FormatResultType) StringToExtended(theStr255,&theNumFormatStringRec,&theNumberPartsTable,outFloat); + + if (theFormatResultType != fFormatOK && theFormatResultType != fBestGuess) + { + SetErrorMessageAndLongIntAndBail("ExtractFloatFromCStr: StringToExtended() = ",theFormatResultType); + } + + + errCode = noErr; + + +EXITPOINT: + + return(errCode); +} + + + +OSErr CopyFloatToCStr(const extended80 *theFloat,char *theCStr,const int maxCStrLength,const int inMaxNumIntDigits,const int inMaxNumFractDigits) +{ +OSErr errCode; +Str255 theStr255; +Handle theNumberPartsTableHandle = nil; +long theNumberPartsOffset,theNumberPartsLength; +FormatResultType theFormatResultType; +NumberParts theNumberPartsTable; +NumFormatStringRec theNumFormatStringRec; + + + if (theCStr == nil) + { + SetErrorMessage("CopyFloatToCStr: Bad parameter, theCStr == nil"); + errCode = kGenericError; + goto EXITPOINT; + } + + if (theFloat == nil) + { + SetErrorMessage("CopyFloatToCStr: Bad parameter, theFloat == nil"); + errCode = kGenericError; + goto EXITPOINT; + } + + +// GetIntlResourceTable(smRoman,smNumberPartsTable,&theNumberPartsTableHandle,&theNumberPartsOffset,&theNumberPartsLength); + + GetIntlResourceTable(GetScriptManagerVariable(smSysScript),smNumberPartsTable,&theNumberPartsTableHandle,&theNumberPartsOffset,&theNumberPartsLength); + + if (theNumberPartsTableHandle == nil) + { + SetErrorMessage("CopyFloatToCStr: Can't get number parts table for converting string representations to/from numeric representations"); + errCode = kGenericError; + goto EXITPOINT; + } + + if (theNumberPartsLength > sizeof(theNumberPartsTable)) + { + SetErrorMessage("CopyFloatToCStr: Number parts table has bad length"); + errCode = kGenericError; + goto EXITPOINT; + } + + + BlockMove(*theNumberPartsTableHandle + theNumberPartsOffset,&theNumberPartsTable,theNumberPartsLength); + + + if (inMaxNumIntDigits >= 0 || inMaxNumFractDigits >= 0) + { + char numberFormat[64]; + int numberFormatLength = 0; + + for (int i = 0;i < inMaxNumIntDigits && numberFormatLength < sizeof(numberFormat) - 1;i++) + { + numberFormat[numberFormatLength++] = '0'; + } + + if (inMaxNumFractDigits > 0 && numberFormatLength < sizeof(numberFormat) - 1) + { + numberFormat[numberFormatLength++] = '.'; + + for (int i = 0;i < inMaxNumFractDigits && numberFormatLength < sizeof(numberFormat) - 1;i++) + { + numberFormat[numberFormatLength++] = '0'; + } + } + + + if (numberFormatLength < sizeof(numberFormat) - 1) + { + numberFormat[numberFormatLength++] = ';'; + } + + if (numberFormatLength < sizeof(numberFormat) - 1) + { + numberFormat[numberFormatLength++] = '-'; + } + + + for (int i = 0;i < inMaxNumIntDigits && numberFormatLength < sizeof(numberFormat) - 1;i++) + { + numberFormat[numberFormatLength++] = '0'; + } + + if (inMaxNumFractDigits > 0 && numberFormatLength < sizeof(numberFormat) - 1) + { + numberFormat[numberFormatLength++] = '.'; + + for (int i = 0;i < inMaxNumFractDigits && numberFormatLength < sizeof(numberFormat) - 1;i++) + { + numberFormat[numberFormatLength++] = '0'; + } + } + + numberFormat[numberFormatLength] = '\0'; + + + Str255 tempStr255; + + CopyCStrToPStr(numberFormat,tempStr255,sizeof(tempStr255)); + + theFormatResultType = (FormatResultType) StringToFormatRec(tempStr255,&theNumberPartsTable,&theNumFormatStringRec); + } + + else + { + theFormatResultType = (FormatResultType) StringToFormatRec(kNumberFormatString,&theNumberPartsTable,&theNumFormatStringRec); + } + + if (theFormatResultType != fFormatOK) + { + SetErrorMessage("CopyFloatToCStr: StringToFormatRec() != fFormatOK"); + errCode = kGenericError; + goto EXITPOINT; + } + + + theFormatResultType = (FormatResultType) ExtendedToString(theFloat,&theNumFormatStringRec,&theNumberPartsTable,theStr255); + + if (theFormatResultType != fFormatOK) + { + SetErrorMessage("CopyFloatToCStr: ExtendedToString() != fFormatOK"); + errCode = kGenericError; + goto EXITPOINT; + } + + + CopyPStrToCStr(theStr255,theCStr,maxCStrLength); + + errCode = noErr; + + +EXITPOINT: + + return(errCode); +} + + + + + +void SkipWhiteSpace(char **ioSrcCharPtr,const Boolean inStopAtEOL) +{ + if (ioSrcCharPtr != nil && *ioSrcCharPtr != nil) + { + if (inStopAtEOL) + { + while ((**ioSrcCharPtr == ' ' || **ioSrcCharPtr == '\t') && **ioSrcCharPtr != '\r' && **ioSrcCharPtr != '\n') + { + *ioSrcCharPtr++; + } + } + + else + { + while (**ioSrcCharPtr == ' ' || **ioSrcCharPtr == '\t') + { + *ioSrcCharPtr++; + } + } + } +} diff --git a/openssl/MacOS/GetHTTPS.src/CPStringUtils.hpp b/openssl/MacOS/GetHTTPS.src/CPStringUtils.hpp new file mode 100644 index 000000000..5045c4101 --- /dev/null +++ b/openssl/MacOS/GetHTTPS.src/CPStringUtils.hpp @@ -0,0 +1,104 @@ +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + +void CopyPStrToCStr(const unsigned char *thePStr,char *theCStr,const int maxCStrLength); +void CopyPStrToPStr(const unsigned char *theSrcPStr,unsigned char *theDstPStr,const int maxDstStrLength); +void CopyCStrToCStr(const char *theSrcCStr,char *theDstCStr,const int maxDstStrLength); +void CopyCStrToPStr(const char *theSrcCStr,unsigned char *theDstPStr,const int maxDstStrLength); +void ConcatPStrToCStr(const unsigned char *thePStr,char *theCStr,const int maxCStrLength); +void ConcatPStrToPStr(const unsigned char *theSrcPStr,unsigned char *theDstPStr,const int maxDstStrLength); +void ConcatCStrToPStr(const char *theSrcCStr,unsigned char *theDstPStr,const int maxDstStrLength); +void ConcatCStrToCStr(const char *theSrcCStr,char *theDstCStr,const int maxCStrLength); + +void ConcatCharToCStr(const char theChar,char *theDstCStr,const int maxCStrLength); +void ConcatCharToPStr(const char theChar,unsigned char *theDstPStr,const int maxPStrLength); + +int ComparePStrs(const unsigned char *theFirstPStr,const unsigned char *theSecondPStr,const Boolean ignoreCase = true); +int CompareCStrs(const char *theFirstCStr,const char *theSecondCStr,const Boolean ignoreCase = true); +int CompareCStrToPStr(const char *theCStr,const unsigned char *thePStr,const Boolean ignoreCase = true); + +Boolean CStrsAreEqual(const char *theFirstCStr,const char *theSecondCStr,const Boolean ignoreCase = true); +Boolean PStrsAreEqual(const unsigned char *theFirstCStr,const unsigned char *theSecondCStr,const Boolean ignoreCase = true); + +void CopyLongIntToCStr(const long theNum,char *theCStr,const int maxCStrLength,const int numDigits = -1); +void CopyUnsignedLongIntToCStr(const unsigned long theNum,char *theCStr,const int maxCStrLength); +void ConcatLongIntToCStr(const long theNum,char *theCStr,const int maxCStrLength,const int numDigits = -1); +void CopyCStrAndConcatLongIntToCStr(const char *theSrcCStr,const long theNum,char *theDstCStr,const int maxDstStrLength); + +void CopyLongIntToPStr(const long theNum,unsigned char *thePStr,const int maxPStrLength,const int numDigits = -1); +void ConcatLongIntToPStr(const long theNum,unsigned char *thePStr,const int maxPStrLength,const int numDigits = -1); + +long CStrLength(const char *theCString); +long PStrLength(const unsigned char *thePString); + +OSErr CopyCStrToExistingHandle(const char *theCString,Handle theHandle); +OSErr CopyLongIntToExistingHandle(const long inTheLongInt,Handle theHandle); + +OSErr CopyCStrToNewHandle(const char *theCString,Handle *theHandle); +OSErr CopyPStrToNewHandle(const unsigned char *thePString,Handle *theHandle); +OSErr CopyLongIntToNewHandle(const long inTheLongInt,Handle *theHandle); + +OSErr AppendCStrToHandle(const char *theCString,Handle theHandle,long *currentLength = nil,long *maxLength = nil); +OSErr AppendCharsToHandle(const char *theChars,const int numChars,Handle theHandle,long *currentLength = nil,long *maxLength = nil); +OSErr AppendPStrToHandle(const unsigned char *thePString,Handle theHandle,long *currentLength = nil); +OSErr AppendLongIntToHandle(const long inTheLongInt,Handle theHandle,long *currentLength = nil); + +void ZeroMem(void *theMemPtr,const unsigned long numBytes); + +char *FindCharInCStr(const char theChar,const char *theCString); +long FindCharOffsetInCStr(const char theChar,const char *theCString,const Boolean inIgnoreCase = false); +long FindCStrOffsetInCStr(const char *theCSubstring,const char *theCString,const Boolean inIgnoreCase = false); + +void CopyCSubstrToCStr(const char *theSrcCStr,const int maxCharsToCopy,char *theDstCStr,const int maxDstStrLength); +void CopyCSubstrToPStr(const char *theSrcCStr,const int maxCharsToCopy,unsigned char *theDstPStr,const int maxDstStrLength); + +void InsertCStrIntoCStr(const char *theSrcCStr,const int theInsertionOffset,char *theDstCStr,const int maxDstStrLength); +void InsertPStrIntoCStr(const unsigned char *theSrcPStr,const int theInsertionOffset,char *theDstCStr,const int maxDstStrLength); +OSErr InsertCStrIntoHandle(const char *theCString,Handle theHandle,const long inInsertOffset); + +void CopyCStrAndInsertCStrIntoCStr(const char *theSrcCStr,const char *theInsertCStr,char *theDstCStr,const int maxDstStrLength); + +void CopyCStrAndInsertCStrsLongIntsIntoCStr(const char *theSrcCStr,const char **theInsertCStrs,const long *theLongInts,char *theDstCStr,const int maxDstStrLength); + +void CopyCStrAndInsert1LongIntIntoCStr(const char *theSrcCStr,const long theNum,char *theDstCStr,const int maxDstStrLength); +void CopyCStrAndInsert2LongIntsIntoCStr(const char *theSrcCStr,const long long1,const long long2,char *theDstCStr,const int maxDstStrLength); +void CopyCStrAndInsert3LongIntsIntoCStr(const char *theSrcCStr,const long long1,const long long2,const long long3,char *theDstCStr,const int maxDstStrLength); + +void CopyCStrAndInsertCStrLongIntIntoCStr(const char *theSrcCStr,const char *theInsertCStr,const long theNum,char *theDstCStr,const int maxDstStrLength); +OSErr CopyCStrAndInsertCStrLongIntIntoHandle(const char *theSrcCStr,const char *theInsertCStr,const long theNum,Handle *theHandle); + + +OSErr CopyIndexedWordToCStr(char *theSrcCStr,int whichWord,char *theDstCStr,int maxDstCStrLength); +OSErr CopyIndexedWordToNewHandle(char *theSrcCStr,int whichWord,Handle *outTheHandle); + +OSErr CopyIndexedLineToCStr(const char *theSrcCStr,int inWhichLine,int *lineEndIndex,Boolean *gotLastLine,char *theDstCStr,const int maxDstCStrLength); +OSErr CopyIndexedLineToNewHandle(const char *theSrcCStr,int inWhichLine,Handle *outNewHandle); + +OSErr ExtractIntFromCStr(const char *theSrcCStr,int *outInt,Boolean skipLeadingSpaces = true); +OSErr ExtractIntFromPStr(const unsigned char *theSrcPStr,int *outInt,Boolean skipLeadingSpaces = true); + + +void ConvertCStrToUpperCase(char *theSrcCStr); + + +int CountOccurencesOfCharInCStr(const char inChar,const char *inSrcCStr); +int CountWordsInCStr(const char *inSrcCStr); + +OSErr CountDigits(const char *inCStr,int *outNumIntegerDigits,int *outNumFractDigits); + +void ExtractCStrItemFromCStr(const char *inSrcCStr,const char inItemDelimiter,const int inItemNumber,Boolean *foundItem,char *outDstCharPtr,const int inDstCharPtrMaxLength,const Boolean inTreatMultipleDelimsAsSingleDelim = false); +OSErr ExtractCStrItemFromCStrIntoNewHandle(const char *inSrcCStr,const char inItemDelimiter,const int inItemNumber,Boolean *foundItem,Handle *outNewHandle,const Boolean inTreatMultipleDelimsAsSingleDelim = false); + + +OSErr ExtractFloatFromCStr(const char *inCString,extended80 *outFloat); +OSErr CopyFloatToCStr(const extended80 *theFloat,char *theCStr,const int maxCStrLength,const int inMaxNumIntDigits = -1,const int inMaxNumFractDigits = -1); + +void SkipWhiteSpace(char **ioSrcCharPtr,const Boolean inStopAtEOL = false); + + +#ifdef __cplusplus +} +#endif diff --git a/openssl/MacOS/GetHTTPS.src/ErrorHandling.cpp b/openssl/MacOS/GetHTTPS.src/ErrorHandling.cpp new file mode 100644 index 000000000..80b6a675f --- /dev/null +++ b/openssl/MacOS/GetHTTPS.src/ErrorHandling.cpp @@ -0,0 +1,170 @@ +/* ==================================================================== + * Copyright (c) 1998-1999 The OpenSSL Project. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. All advertising materials mentioning features or use of this + * software must display the following acknowledgment: + * "This product includes software developed by the OpenSSL Project + * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" + * + * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to + * endorse or promote products derived from this software without + * prior written permission. For written permission, please contact + * openssl-core@openssl.org. + * + * 5. Products derived from this software may not be called "OpenSSL" + * nor may "OpenSSL" appear in their names without prior written + * permission of the OpenSSL Project. + * + * 6. Redistributions of any form whatsoever must retain the following + * acknowledgment: + * "This product includes software developed by the OpenSSL Project + * for use in the OpenSSL Toolkit (http://www.openssl.org/)" + * + * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY + * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + * ==================================================================== + * + * This product includes cryptographic software written by Eric Young + * (eay@cryptsoft.com). This product includes software written by Tim + * Hudson (tjh@cryptsoft.com). + * + */ + + + + #include "ErrorHandling.hpp" +#include "CPStringUtils.hpp" + +#ifdef __EXCEPTIONS_ENABLED__ + #include "CMyException.hpp" +#endif + + +static char gErrorMessageBuffer[512]; + +char *gErrorMessage = gErrorMessageBuffer; +int gErrorMessageMaxLength = sizeof(gErrorMessageBuffer); + + + +void SetErrorMessage(const char *theErrorMessage) +{ + if (theErrorMessage != nil) + { + CopyCStrToCStr(theErrorMessage,gErrorMessage,gErrorMessageMaxLength); + } +} + + +void SetErrorMessageAndAppendLongInt(const char *theErrorMessage,const long theLongInt) +{ + if (theErrorMessage != nil) + { + CopyCStrAndConcatLongIntToCStr(theErrorMessage,theLongInt,gErrorMessage,gErrorMessageMaxLength); + } +} + +void SetErrorMessageAndCStrAndLongInt(const char *theErrorMessage,const char * theCStr,const long theLongInt) +{ + if (theErrorMessage != nil) + { + CopyCStrAndInsertCStrLongIntIntoCStr(theErrorMessage,theCStr,theLongInt,gErrorMessage,gErrorMessageMaxLength); + } + +} + +void SetErrorMessageAndCStr(const char *theErrorMessage,const char * theCStr) +{ + if (theErrorMessage != nil) + { + CopyCStrAndInsertCStrLongIntIntoCStr(theErrorMessage,theCStr,-1,gErrorMessage,gErrorMessageMaxLength); + } +} + + +void AppendCStrToErrorMessage(const char *theErrorMessage) +{ + if (theErrorMessage != nil) + { + ConcatCStrToCStr(theErrorMessage,gErrorMessage,gErrorMessageMaxLength); + } +} + + +void AppendLongIntToErrorMessage(const long theLongInt) +{ + ConcatLongIntToCStr(theLongInt,gErrorMessage,gErrorMessageMaxLength); +} + + + +char *GetErrorMessage(void) +{ + return gErrorMessage; +} + + +OSErr GetErrorMessageInNewHandle(Handle *inoutHandle) +{ +OSErr errCode; + + + errCode = CopyCStrToNewHandle(gErrorMessage,inoutHandle); + + return(errCode); +} + + +OSErr GetErrorMessageInExistingHandle(Handle inoutHandle) +{ +OSErr errCode; + + + errCode = CopyCStrToExistingHandle(gErrorMessage,inoutHandle); + + return(errCode); +} + + + +OSErr AppendErrorMessageToHandle(Handle inoutHandle) +{ +OSErr errCode; + + + errCode = AppendCStrToHandle(gErrorMessage,inoutHandle,nil); + + return(errCode); +} + + +#ifdef __EXCEPTIONS_ENABLED__ + +void ThrowErrorMessageException(void) +{ + ThrowDescriptiveException(gErrorMessage); +} + +#endif diff --git a/openssl/MacOS/GetHTTPS.src/ErrorHandling.hpp b/openssl/MacOS/GetHTTPS.src/ErrorHandling.hpp new file mode 100644 index 000000000..fbfbe786b --- /dev/null +++ b/openssl/MacOS/GetHTTPS.src/ErrorHandling.hpp @@ -0,0 +1,147 @@ +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef kGenericError + #define kGenericError -1 +#endif + +extern char *gErrorMessage; + + +void SetErrorMessage(const char *theErrorMessage); +void SetErrorMessageAndAppendLongInt(const char *theErrorMessage,const long theLongInt); +void SetErrorMessageAndCStrAndLongInt(const char *theErrorMessage,const char * theCStr,const long theLongInt); +void SetErrorMessageAndCStr(const char *theErrorMessage,const char * theCStr); +void AppendCStrToErrorMessage(const char *theErrorMessage); +void AppendLongIntToErrorMessage(const long theLongInt); + + +char *GetErrorMessage(void); +OSErr GetErrorMessageInNewHandle(Handle *inoutHandle); +OSErr GetErrorMessageInExistingHandle(Handle inoutHandle); +OSErr AppendErrorMessageToHandle(Handle inoutHandle); + + +#ifdef __EXCEPTIONS_ENABLED__ + void ThrowErrorMessageException(void); +#endif + + + +// A bunch of evil macros that would be unnecessary if I were always using C++ ! + +#define SetErrorMessageAndBailIfNil(theArg,theMessage) \ +{ \ + if (theArg == nil) \ + { \ + SetErrorMessage(theMessage); \ + errCode = kGenericError; \ + goto EXITPOINT; \ + } \ +} + + +#define SetErrorMessageAndBail(theMessage) \ +{ \ + SetErrorMessage(theMessage); \ + errCode = kGenericError; \ + goto EXITPOINT; \ +} + + +#define SetErrorMessageAndLongIntAndBail(theMessage,theLongInt) \ +{ \ + SetErrorMessageAndAppendLongInt(theMessage,theLongInt); \ + errCode = kGenericError; \ + goto EXITPOINT; \ +} + + +#define SetErrorMessageAndLongIntAndBailIfError(theErrCode,theMessage,theLongInt) \ +{ \ + if (theErrCode != noErr) \ + { \ + SetErrorMessageAndAppendLongInt(theMessage,theLongInt); \ + errCode = theErrCode; \ + goto EXITPOINT; \ + } \ +} + + +#define SetErrorMessageCStrLongIntAndBailIfError(theErrCode,theMessage,theCStr,theLongInt) \ +{ \ + if (theErrCode != noErr) \ + { \ + SetErrorMessageAndCStrAndLongInt(theMessage,theCStr,theLongInt); \ + errCode = theErrCode; \ + goto EXITPOINT; \ + } \ +} + + +#define SetErrorMessageAndCStrAndBail(theMessage,theCStr) \ +{ \ + SetErrorMessageAndCStr(theMessage,theCStr); \ + errCode = kGenericError; \ + goto EXITPOINT; \ +} + + +#define SetErrorMessageAndBailIfError(theErrCode,theMessage) \ +{ \ + if (theErrCode != noErr) \ + { \ + SetErrorMessage(theMessage); \ + errCode = theErrCode; \ + goto EXITPOINT; \ + } \ +} + + +#define SetErrorMessageAndLongIntAndBailIfNil(theArg,theMessage,theLongInt) \ +{ \ + if (theArg == nil) \ + { \ + SetErrorMessageAndAppendLongInt(theMessage,theLongInt); \ + errCode = kGenericError; \ + goto EXITPOINT; \ + } \ +} + + +#define BailIfError(theErrCode) \ +{ \ + if ((theErrCode) != noErr) \ + { \ + goto EXITPOINT; \ + } \ +} + + +#define SetErrCodeAndBail(theErrCode) \ +{ \ + errCode = theErrCode; \ + \ + goto EXITPOINT; \ +} + + +#define SetErrorCodeAndMessageAndBail(theErrCode,theMessage) \ +{ \ + SetErrorMessage(theMessage); \ + errCode = theErrCode; \ + goto EXITPOINT; \ +} + + +#define BailNow() \ +{ \ + errCode = kGenericError; \ + goto EXITPOINT; \ +} + + +#ifdef __cplusplus +} +#endif diff --git a/openssl/MacOS/GetHTTPS.src/GetHTTPS.cpp b/openssl/MacOS/GetHTTPS.src/GetHTTPS.cpp new file mode 100644 index 000000000..3a5e3f018 --- /dev/null +++ b/openssl/MacOS/GetHTTPS.src/GetHTTPS.cpp @@ -0,0 +1,209 @@ +/* + * An demo illustrating how to retrieve a URI from a secure HTTP server. + * + * Author: Roy Wood + * Date: September 7, 1999 + * Comments: This relies heavily on my MacSockets library. + * This project is also set up so that it expects the OpenSSL source folder (0.9.4 as I write this) + * to live in a folder called "OpenSSL-0.9.4" in this project's parent folder. For example: + * + * Macintosh HD: + * Development: + * OpenSSL-0.9.4: + * (OpenSSL sources here) + * OpenSSL Example: + * (OpenSSL example junk here) + * + * + * Also-- before attempting to compile this, make sure the aliases in "OpenSSL-0.9.4:include:openssl" + * are installed! Use the AppleScript applet in the "openssl-0.9.4" folder to do this! + */ +/* modified to seed the PRNG */ +/* modified to use CRandomizer for seeding */ + + +// Include some funky libs I've developed over time + +#include "CPStringUtils.hpp" +#include "ErrorHandling.hpp" +#include "MacSocket.h" +#include "Randomizer.h" + +// We use the OpenSSL implementation of SSL.... +// This was a lot of work to finally get going, though you wouldn't know it by the results! + +#include +#include + +#include + +// Let's try grabbing some data from here: + +#define kHTTPS_DNS "www.apache-ssl.org" +#define kHTTPS_Port 443 +#define kHTTPS_URI "/" + + +// Forward-declare this + +OSErr MyMacSocket_IdleWaitCallback(void *inUserRefPtr); + +// My idle-wait callback. Doesn't do much, does it? Silly cooperative multitasking. + +OSErr MyMacSocket_IdleWaitCallback(void *inUserRefPtr) +{ +#pragma unused(inUserRefPtr) + +EventRecord theEvent; + ::EventAvail(everyEvent,&theEvent); + + CRandomizer *randomizer = (CRandomizer*)inUserRefPtr; + if (randomizer) + randomizer->PeriodicAction(); + + return(noErr); +} + + +// Finally! + +void main(void) +{ + OSErr errCode; + int theSocket = -1; + int theTimeout = 30; + + SSL_CTX *ssl_ctx = nil; + SSL *ssl = nil; + + char tempString[256]; + UnsignedWide microTickCount; + + + CRandomizer randomizer; + + printf("OpenSSL Demo by Roy Wood, roy@centricsystems.ca\n\n"); + + BailIfError(errCode = MacSocket_Startup()); + + + + // Create a socket-like object + + BailIfError(errCode = MacSocket_socket(&theSocket,false,theTimeout * 60,MyMacSocket_IdleWaitCallback,&randomizer)); + + + // Set up the connect string and try to connect + + CopyCStrAndInsertCStrLongIntIntoCStr("%s:%ld",kHTTPS_DNS,kHTTPS_Port,tempString,sizeof(tempString)); + + printf("Connecting to %s....\n",tempString); + + BailIfError(errCode = MacSocket_connect(theSocket,tempString)); + + + // Init SSL stuff + + SSL_load_error_strings(); + + SSLeay_add_ssl_algorithms(); + + + // Pick the SSL method + +// ssl_ctx = SSL_CTX_new(SSLv2_client_method()); + ssl_ctx = SSL_CTX_new(SSLv23_client_method()); +// ssl_ctx = SSL_CTX_new(SSLv3_client_method()); + + + // Create an SSL thingey and try to negotiate the connection + + ssl = SSL_new(ssl_ctx); + + SSL_set_fd(ssl,theSocket); + + errCode = SSL_connect(ssl); + + if (errCode < 0) + { + SetErrorMessageAndLongIntAndBail("OpenSSL: Can't initiate SSL connection, SSL_connect() = ",errCode); + } + + // Request the URI from the host + + CopyCStrToCStr("GET ",tempString,sizeof(tempString)); + ConcatCStrToCStr(kHTTPS_URI,tempString,sizeof(tempString)); + ConcatCStrToCStr(" HTTP/1.0\r\n\r\n",tempString,sizeof(tempString)); + + + errCode = SSL_write(ssl,tempString,CStrLength(tempString)); + + if (errCode < 0) + { + SetErrorMessageAndLongIntAndBail("OpenSSL: Error writing data via ssl, SSL_write() = ",errCode); + } + + + for (;;) + { + char tempString[256]; + int bytesRead; + + + // Read some bytes and dump them to the console + + bytesRead = SSL_read(ssl,tempString,sizeof(tempString) - 1); + + if (bytesRead == 0 && MacSocket_RemoteEndIsClosing(theSocket)) + { + break; + } + + else if (bytesRead < 0) + { + SetErrorMessageAndLongIntAndBail("OpenSSL: Error reading data via ssl, SSL_read() = ",bytesRead); + } + + + tempString[bytesRead] = '\0'; + + printf("%s", tempString); + } + + printf("\n\n\n"); + + // All done! + + errCode = noErr; + + +EXITPOINT: + + // Clean up and go home + + if (theSocket >= 0) + { + MacSocket_close(theSocket); + } + + if (ssl != nil) + { + SSL_free(ssl); + } + + if (ssl_ctx != nil) + { + SSL_CTX_free(ssl_ctx); + } + + + if (errCode != noErr) + { + printf("An error occurred:\n"); + + printf("%s",GetErrorMessage()); + } + + + MacSocket_Shutdown(); +} diff --git a/openssl/MacOS/GetHTTPS.src/MacSocket.cpp b/openssl/MacOS/GetHTTPS.src/MacSocket.cpp new file mode 100644 index 000000000..c95d804d5 --- /dev/null +++ b/openssl/MacOS/GetHTTPS.src/MacSocket.cpp @@ -0,0 +1,1607 @@ +/* + * A simple socket-like package. + * This could undoubtedly be improved, since it does polling and busy-waiting. + * At least it uses asynch I/O and implements timeouts! + * + * Other funkiness includes the use of my own (possibly brain-damaged) error-handling infrastructure. + * + * -Roy Wood (roy@centricsystems.ca) + * + */ + + +/* ==================================================================== + * Copyright (c) 1998-1999 The OpenSSL Project. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. All advertising materials mentioning features or use of this + * software must display the following acknowledgment: + * "This product includes software developed by the OpenSSL Project + * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" + * + * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to + * endorse or promote products derived from this software without + * prior written permission. For written permission, please contact + * openssl-core@openssl.org. + * + * 5. Products derived from this software may not be called "OpenSSL" + * nor may "OpenSSL" appear in their names without prior written + * permission of the OpenSSL Project. + * + * 6. Redistributions of any form whatsoever must retain the following + * acknowledgment: + * "This product includes software developed by the OpenSSL Project + * for use in the OpenSSL Toolkit (http://www.openssl.org/)" + * + * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY + * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + * ==================================================================== + * + * This product includes cryptographic software written by Eric Young + * (eay@cryptsoft.com). This product includes software written by Tim + * Hudson (tjh@cryptsoft.com). + * + */ + + + + + +#include "MacSocket.h" + +#include + +#include +#include +#include + + + +#include "CPStringUtils.hpp" +#include "ErrorHandling.hpp" + + +// #define MACSOCKET_DEBUG 1 + +#ifdef MACSOCKET_DEBUG + #include +#endif + + + +extern int errno; + + +#define kMaxNumSockets 4 + + +struct SocketStruct +{ + Boolean mIsInUse; + + Boolean mEndpointIsBound; + + Boolean mLocalEndIsConnected; + Boolean mRemoteEndIsConnected; + + Boolean mReceivedTOpenComplete; + Boolean mReceivedTBindComplete; + Boolean mReceivedTConnect; + Boolean mReceivedTListen; + Boolean mReceivedTPassCon; + Boolean mReceivedTDisconnect; + Boolean mReceivedTOrdRel; + Boolean mReceivedTDisconnectComplete; + + long mTimeoutTicks; + long mOperationStartTicks; + + MacSocket_IdleWaitCallback mIdleWaitCallback; + void *mUserRefPtr; + + OTEventCode mExpectedCode; + OTResult mAsyncOperationResult; + + EndpointRef mEndPointRef; + TBind *mBindRequestedAddrInfo; + TBind *mAssignedAddrInfo; + TCall *mRemoteAddrInfo; + + Boolean mReadyToReadData; + Boolean mReadyToWriteData; + + Ptr mReadBuffer; + Ptr mWriteBuffer; + + int mLastError; + char mErrMessage[256]; +}; + +typedef struct SocketStruct SocketStruct; + + +static SocketStruct sSockets[kMaxNumSockets]; +static Boolean sSocketsSetup = false; + + + + +static OSErr MyBusyWait(SocketStruct *ioSocket,Boolean returnImmediatelyOnError,OTResult *outOTResult,Boolean *inAsyncOperationCompleteFlag); + +static pascal void OTNonYieldingNotifier(void *contextPtr,OTEventCode code,OTResult result,void *cookie); + +static Boolean SocketIndexIsValid(const int inSocketNum); + +static void InitSocket(SocketStruct *ioSocket); + +static void PrepareForAsyncOperation(SocketStruct *ioSocket,const OTEventCode inExpectedCode); + +static Boolean TimeoutElapsed(const SocketStruct *inSocket); + +static OSStatus NegotiateIPReuseAddrOption(EndpointRef inEndpoint,const Boolean inEnableReuseIP); + + + +void MacSocket_GetSocketErrorInfo(const int inSocketNum,int *outSocketErrCode,char *outSocketErrString,const int inSocketErrStringMaxLength) +{ + if (outSocketErrCode != nil) + { + *outSocketErrCode = -1; + } + + if (outSocketErrString != nil) + { + CopyCStrToCStr("",outSocketErrString,inSocketErrStringMaxLength); + } + + + if (SocketIndexIsValid(inSocketNum)) + { + SocketStruct *theSocketStruct = &(sSockets[inSocketNum]); + + + if (outSocketErrCode != nil) + { + *outSocketErrCode = theSocketStruct->mLastError; + } + + if (outSocketErrString != nil) + { + CopyCStrToCStr(theSocketStruct->mErrMessage,outSocketErrString,inSocketErrStringMaxLength); + } + } +} + + +void MacSocket_SetUserRefPtr(const int inSocketNum,void *inNewRefPtr) +{ + if (SocketIndexIsValid(inSocketNum)) + { + SocketStruct *theSocketStruct = &(sSockets[inSocketNum]); + + theSocketStruct->mUserRefPtr = inNewRefPtr; + } +} + + + +void MacSocket_GetLocalIPAndPort(const int inSocketNum,char *outIPAndPort,const int inIPAndPortLength) +{ + if (outIPAndPort != nil && SocketIndexIsValid(inSocketNum)) + { + char tempString[256]; + SocketStruct *theSocketStruct = &(sSockets[inSocketNum]); + + + CopyCStrToCStr("",tempString,sizeof(tempString)); + + if (theSocketStruct->mAssignedAddrInfo != nil) + { + InetAddress *theInetAddress = (InetAddress *) theSocketStruct->mAssignedAddrInfo->addr.buf; + InetHost theInetHost = theInetAddress->fHost; + + if (theInetHost == 0) + { + InetInterfaceInfo theInetInterfaceInfo; + + if (::OTInetGetInterfaceInfo(&theInetInterfaceInfo,kDefaultInetInterface) == noErr) + { + theInetHost = theInetInterfaceInfo.fAddress; + } + } + + ::OTInetHostToString(theInetHost,tempString); + + ConcatCStrToCStr(":",tempString,sizeof(tempString)); + ConcatLongIntToCStr(theInetAddress->fPort,tempString,sizeof(tempString)); + } + + CopyCStrToCStr(tempString,outIPAndPort,inIPAndPortLength); + } +} + + + +void MacSocket_GetRemoteIPAndPort(const int inSocketNum,char *outIPAndPort,const int inIPAndPortLength) +{ + if (outIPAndPort != nil && SocketIndexIsValid(inSocketNum)) + { + char tempString[256]; + SocketStruct *theSocketStruct = &(sSockets[inSocketNum]); + + + CopyCStrToCStr("",tempString,sizeof(tempString)); + + if (theSocketStruct->mRemoteAddrInfo != nil) + { + InetAddress *theInetAddress = (InetAddress *) theSocketStruct->mRemoteAddrInfo->addr.buf; + InetHost theInetHost = theInetAddress->fHost; + + if (theInetHost == 0) + { + InetInterfaceInfo theInetInterfaceInfo; + + if (::OTInetGetInterfaceInfo(&theInetInterfaceInfo,kDefaultInetInterface) == noErr) + { + theInetHost = theInetInterfaceInfo.fAddress; + } + } + + ::OTInetHostToString(theInetHost,tempString); + + ConcatCStrToCStr(":",tempString,sizeof(tempString)); + ConcatLongIntToCStr(theInetAddress->fPort,tempString,sizeof(tempString)); + } + + CopyCStrToCStr(tempString,outIPAndPort,inIPAndPortLength); + } +} + + + +Boolean MacSocket_RemoteEndIsClosing(const int inSocketNum) +{ +Boolean theResult = false; + + if (SocketIndexIsValid(inSocketNum)) + { + SocketStruct *theSocketStruct = &(sSockets[inSocketNum]); + + theResult = theSocketStruct->mReceivedTOrdRel; + } + + return(theResult); +} + + + +Boolean MacSocket_ListenCompleted(const int inSocketNum) +{ +Boolean theResult = false; + + if (SocketIndexIsValid(inSocketNum)) + { + SocketStruct *theSocketStruct = &(sSockets[inSocketNum]); + + theResult = theSocketStruct->mReceivedTPassCon; + } + + return(theResult); +} + + + +Boolean MacSocket_RemoteEndIsOpen(const int inSocketNum) +{ + if (SocketIndexIsValid(inSocketNum)) + { + SocketStruct *theSocketStruct = &(sSockets[inSocketNum]); + + return(theSocketStruct->mRemoteEndIsConnected); + } + + else + { + return(false); + } +} + + + +Boolean MacSocket_LocalEndIsOpen(const int inSocketNum) +{ + if (SocketIndexIsValid(inSocketNum)) + { + SocketStruct *theSocketStruct = &(sSockets[inSocketNum]); + + return(theSocketStruct->mLocalEndIsConnected); + } + + else + { + return(false); + } +} + + + +static Boolean TimeoutElapsed(const SocketStruct *inSocket) +{ +Boolean timeIsUp = false; + + if (inSocket != nil && inSocket->mTimeoutTicks > 0 && ::TickCount() > inSocket->mOperationStartTicks + inSocket->mTimeoutTicks) + { + timeIsUp = true; + } + + + return(timeIsUp); +} + + + +static Boolean SocketIndexIsValid(const int inSocketNum) +{ + if (inSocketNum >= 0 && inSocketNum < kMaxNumSockets && sSockets[inSocketNum].mEndPointRef != kOTInvalidEndpointRef) + { + return(true); + } + + else + { + return(false); + } +} + + + +static void InitSocket(SocketStruct *ioSocket) +{ + ioSocket->mIsInUse = false; + + ioSocket->mEndpointIsBound = false; + + ioSocket->mLocalEndIsConnected = false; + ioSocket->mRemoteEndIsConnected = false; + + ioSocket->mReceivedTOpenComplete = false; + ioSocket->mReceivedTBindComplete = false; + ioSocket->mReceivedTConnect = false; + ioSocket->mReceivedTListen = false; + ioSocket->mReceivedTPassCon = false; + ioSocket->mReceivedTDisconnect = false; + ioSocket->mReceivedTOrdRel = false; + ioSocket->mReceivedTDisconnectComplete = false; + + ioSocket->mTimeoutTicks = 30 * 60; + ioSocket->mOperationStartTicks = -1; + + ioSocket->mIdleWaitCallback = nil; + ioSocket->mUserRefPtr = nil; + + ioSocket->mExpectedCode = 0; + ioSocket->mAsyncOperationResult = noErr; + + ioSocket->mEndPointRef = kOTInvalidEndpointRef; + + ioSocket->mBindRequestedAddrInfo = nil; + ioSocket->mAssignedAddrInfo = nil; + ioSocket->mRemoteAddrInfo = nil; + + ioSocket->mReadyToReadData = false; + ioSocket->mReadyToWriteData = true; + + ioSocket->mReadBuffer = nil; + ioSocket->mWriteBuffer = nil; + + ioSocket->mLastError = noErr; + CopyCStrToCStr("",ioSocket->mErrMessage,sizeof(ioSocket->mErrMessage)); +} + + + +static void PrepareForAsyncOperation(SocketStruct *ioSocket,const OTEventCode inExpectedCode) +{ + ioSocket->mOperationStartTicks = ::TickCount(); + + ioSocket->mAsyncOperationResult = noErr; + + ioSocket->mExpectedCode = inExpectedCode; +} + + +// The wait function.... + +static OSErr MyBusyWait(SocketStruct *ioSocket,Boolean returnImmediatelyOnError,OTResult *outOTResult,Boolean *inAsyncOperationCompleteFlag) +{ +OSErr errCode = noErr; +OTResult theOTResult = noErr; + + + SetErrorMessageAndBailIfNil(ioSocket,"MyBusyWait: Bad parameter, ioSocket = nil"); + SetErrorMessageAndBailIfNil(inAsyncOperationCompleteFlag,"MyBusyWait: Bad parameter, inAsyncOperationCompleteFlag = nil"); + + for (;;) + { + if (*inAsyncOperationCompleteFlag) + { + theOTResult = ioSocket->mAsyncOperationResult; + + break; + } + + if (ioSocket->mIdleWaitCallback != nil) + { + theOTResult = (*(ioSocket->mIdleWaitCallback))(ioSocket->mUserRefPtr); + + if (theOTResult != noErr && returnImmediatelyOnError) + { + break; + } + } + + if (TimeoutElapsed(ioSocket)) + { + theOTResult = kMacSocket_TimeoutErr; + + break; + } + } + + +EXITPOINT: + + if (outOTResult != nil) + { + *outOTResult = theOTResult; + } + + return(errCode); +} + + + +// I used to do thread switching, but stopped. It could easily be rolled back in though.... + +static pascal void OTNonYieldingNotifier(void *contextPtr,OTEventCode code,OTResult result,void *cookie) +{ +SocketStruct *theSocketStruct = (SocketStruct *) contextPtr; + + if (theSocketStruct != nil) + { + if (theSocketStruct->mExpectedCode != 0 && code == theSocketStruct->mExpectedCode) + { + theSocketStruct->mAsyncOperationResult = result; + + theSocketStruct->mExpectedCode = 0; + } + + + switch (code) + { + case T_OPENCOMPLETE: + { + theSocketStruct->mReceivedTOpenComplete = true; + + theSocketStruct->mEndPointRef = (EndpointRef) cookie; + + break; + } + + + case T_BINDCOMPLETE: + { + theSocketStruct->mReceivedTBindComplete = true; + + break; + } + + + case T_CONNECT: + { + theSocketStruct->mReceivedTConnect = true; + + theSocketStruct->mLocalEndIsConnected = true; + + theSocketStruct->mRemoteEndIsConnected = true; + + break; + } + + + case T_LISTEN: + { + theSocketStruct->mReceivedTListen = true; + + break; + } + + + case T_PASSCON: + { + theSocketStruct->mReceivedTPassCon = true; + + theSocketStruct->mLocalEndIsConnected = true; + + theSocketStruct->mRemoteEndIsConnected = true; + + break; + } + + + case T_DATA: + { + theSocketStruct->mReadyToReadData = true; + + break; + } + + case T_GODATA: + { + theSocketStruct->mReadyToWriteData = true; + + break; + } + + case T_DISCONNECT: + { + theSocketStruct->mReceivedTDisconnect = true; + + theSocketStruct->mRemoteEndIsConnected = false; + + theSocketStruct->mLocalEndIsConnected = false; + + ::OTRcvDisconnect(theSocketStruct->mEndPointRef,nil); + + break; + } + + case T_ORDREL: + { + theSocketStruct->mReceivedTOrdRel = true; + + // We can still write data, so don't clear mRemoteEndIsConnected + + ::OTRcvOrderlyDisconnect(theSocketStruct->mEndPointRef); + + break; + } + + case T_DISCONNECTCOMPLETE: + { + theSocketStruct->mReceivedTDisconnectComplete = true; + + theSocketStruct->mRemoteEndIsConnected = false; + + theSocketStruct->mLocalEndIsConnected = false; + + break; + } + } + } +/* +T_LISTEN OTListen +T_CONNECT OTRcvConnect +T_DATA OTRcv, OTRcvUData +T_DISCONNECT OTRcvDisconnect +T_ORDREL OTRcvOrderlyDisconnect +T_GODATA OTSnd, OTSndUData, OTLook +T_PASSCON none + +T_EXDATA OTRcv +T_GOEXDATA OTSnd, OTLook +T_UDERR OTRcvUDErr +*/ +} + + + +// Initialize the main socket data structure + +OSErr MacSocket_Startup(void) +{ + if (!sSocketsSetup) + { + for (int i = 0;i < kMaxNumSockets;i++) + { + InitSocket(&(sSockets[i])); + } + + ::InitOpenTransport(); + + sSocketsSetup = true; + } + + + return(noErr); +} + + + +// Cleanup before exiting + +OSErr MacSocket_Shutdown(void) +{ + if (sSocketsSetup) + { + for (int i = 0;i < kMaxNumSockets;i++) + { + SocketStruct *theSocketStruct = &(sSockets[i]); + + if (theSocketStruct->mIsInUse) + { + if (theSocketStruct->mEndPointRef != kOTInvalidEndpointRef) + { + OTResult theOTResult; + + + // Since we're killing the endpoint, I don't bother to send the disconnect (sorry!) + +/* + if (theSocketStruct->mLocalEndIsConnected) + { + // This is an abortive action, so we do a hard disconnect instead of an OTSndOrderlyDisconnect + + theOTResult = ::OTSndDisconnect(theSocketStruct->mEndPointRef, nil); + + // Now we have to watch for T_DISCONNECTCOMPLETE event + + theSocketStruct->mLocalEndIsConnected = false; + } +*/ + + theOTResult = ::OTCloseProvider(theSocketStruct->mEndPointRef); + + + theSocketStruct->mEndPointRef = kOTInvalidEndpointRef; + } + + if (theSocketStruct->mBindRequestedAddrInfo != nil) + { + ::OTFree((void *) theSocketStruct->mBindRequestedAddrInfo,T_BIND); + + theSocketStruct->mBindRequestedAddrInfo = nil; + } + + if (theSocketStruct->mAssignedAddrInfo != nil) + { + ::OTFree((void *) theSocketStruct->mAssignedAddrInfo,T_BIND); + + theSocketStruct->mAssignedAddrInfo = nil; + } + + if (theSocketStruct->mRemoteAddrInfo != nil) + { + ::OTFree((void *) theSocketStruct->mRemoteAddrInfo,T_CALL); + + theSocketStruct->mRemoteAddrInfo = nil; + } + + + } + } + + ::CloseOpenTransport(); + + sSocketsSetup = false; + } + + return(noErr); +} + + + + + + +// Allocate a socket + +OSErr MacSocket_socket(int *outSocketNum,const Boolean inDoThreadSwitching,const long inTimeoutTicks,MacSocket_IdleWaitCallback inIdleWaitCallback,void *inUserRefPtr) +{ +// Gotta roll support back in for threads eventually..... + +#pragma unused(inDoThreadSwitching) + + +OSErr errCode = noErr; + + + SetErrorMessageAndBailIfNil(outSocketNum,"MacSocket_socket: Bad parameter, outSocketNum == nil"); + + *outSocketNum = -1; + + + // Find an unused socket + + for (int i = 0;i < kMaxNumSockets;i++) + { + if (sSockets[i].mIsInUse == false) + { + OTResult theOTResult; + SocketStruct *theSocketStruct = &(sSockets[i]); + + + InitSocket(theSocketStruct); + + theSocketStruct->mIdleWaitCallback = inIdleWaitCallback; + theSocketStruct->mUserRefPtr = inUserRefPtr; + + theSocketStruct->mTimeoutTicks = inTimeoutTicks; + + + // Set up OT endpoint + + PrepareForAsyncOperation(theSocketStruct,T_OPENCOMPLETE); + + theOTResult = ::OTAsyncOpenEndpoint(OTCreateConfiguration(kTCPName),0,nil,OTNonYieldingNotifier,(void *) theSocketStruct); + + SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_socket: Can't create OT endpoint, OTAsyncOpenEndpoint() = ",theOTResult); + + BailIfError(MyBusyWait(theSocketStruct,false,&theOTResult,&(theSocketStruct->mReceivedTOpenComplete))); + + SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_socket: Can't create OT endpoint, OTAsyncOpenEndpoint() = ",theOTResult); + + + *outSocketNum = i; + + errCode = noErr; + + theSocketStruct->mIsInUse = true; + + break; + } + + else if (i == kMaxNumSockets - 1) + { + SetErrorMessageAndBail("MacSocket_socket: No sockets available"); + } + } + + +EXITPOINT: + + errno = errCode; + + return(errCode); +} + + + + +OSErr MacSocket_listen(const int inSocketNum,const int inPortNum) +{ +OSErr errCode = noErr; +SocketStruct *theSocketStruct = nil; + + + if (!SocketIndexIsValid(inSocketNum)) + { + SetErrorMessageAndBail("MacSocket_listen: Invalid socket number specified"); + } + + + theSocketStruct = &(sSockets[inSocketNum]); + + +OTResult theOTResult; + + + if (theSocketStruct->mBindRequestedAddrInfo == nil) + { + theSocketStruct->mBindRequestedAddrInfo = (TBind *) ::OTAlloc(theSocketStruct->mEndPointRef,T_BIND,T_ADDR,&theOTResult); + + SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_listen: Can't allocate OT T_BIND structure, OTAlloc() = ",theOTResult); + SetErrorMessageAndBailIfNil(theSocketStruct->mBindRequestedAddrInfo,"MacSocket_listen: Can't allocate OT T_BIND structure, OTAlloc() returned nil"); + } + + if (theSocketStruct->mAssignedAddrInfo == nil) + { + theSocketStruct->mAssignedAddrInfo = (TBind *) ::OTAlloc(theSocketStruct->mEndPointRef,T_BIND,T_ADDR,&theOTResult); + + SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_listen: Can't allocate OT T_BIND structure, OTAlloc() = ",theOTResult); + SetErrorMessageAndBailIfNil(theSocketStruct->mAssignedAddrInfo,"MacSocket_listen: Can't allocate OT T_BIND structure, OTAlloc() returned nil"); + } + + if (theSocketStruct->mRemoteAddrInfo == nil) + { + theSocketStruct->mRemoteAddrInfo = (TCall *) ::OTAlloc(theSocketStruct->mEndPointRef,T_CALL,T_ADDR,&theOTResult); + + SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_listen: Can't allocate OT T_CALL structure, OTAlloc() = ",theOTResult); + SetErrorMessageAndBailIfNil(theSocketStruct->mRemoteAddrInfo,"MacSocket_listen: Can't allocate OT T_CALL structure, OTAlloc() returned nil"); + } + + + if (!theSocketStruct->mEndpointIsBound) + { + InetInterfaceInfo theInetInterfaceInfo; + + theOTResult = ::OTInetGetInterfaceInfo(&theInetInterfaceInfo,kDefaultInetInterface); + + SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_listen: Can't determine OT interface info, OTInetGetInterfaceInfo() = ",theOTResult); + + + InetAddress *theInetAddress = (InetAddress *) theSocketStruct->mBindRequestedAddrInfo->addr.buf; + +// theInetAddress->fAddressType = AF_INET; +// theInetAddress->fPort = inPortNum; +// theInetAddress->fHost = theInetInterfaceInfo.fAddress; + + ::OTInitInetAddress(theInetAddress,inPortNum,theInetInterfaceInfo.fAddress); + + theSocketStruct->mBindRequestedAddrInfo->addr.len = sizeof(InetAddress); + + theSocketStruct->mBindRequestedAddrInfo->qlen = 1; + + + theOTResult = ::OTSetSynchronous(theSocketStruct->mEndPointRef); + + SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_listen: Can't set OT endpoint mode, OTSetSynchronous() = ",theOTResult); + + theOTResult = NegotiateIPReuseAddrOption(theSocketStruct->mEndPointRef,true); + + SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_listen: Can't set OT IP address reuse flag, NegotiateIPReuseAddrOption() = ",theOTResult); + + theOTResult = ::OTSetAsynchronous(theSocketStruct->mEndPointRef); + + SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_listen: Can't set OT endpoint mode, OTSetAsynchronous() = ",theOTResult); + + + PrepareForAsyncOperation(theSocketStruct,T_BINDCOMPLETE); + + theOTResult = ::OTBind(theSocketStruct->mEndPointRef,theSocketStruct->mBindRequestedAddrInfo,theSocketStruct->mAssignedAddrInfo); + + SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_listen: Can't bind OT endpoint, OTBind() = ",theOTResult); + + BailIfError(MyBusyWait(theSocketStruct,false,&theOTResult,&(theSocketStruct->mReceivedTBindComplete))); + + SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_listen: Can't bind OT endpoint, OTBind() = ",theOTResult); + + + theSocketStruct->mEndpointIsBound = true; + } + + + PrepareForAsyncOperation(theSocketStruct,T_LISTEN); + + theOTResult = ::OTListen(theSocketStruct->mEndPointRef,theSocketStruct->mRemoteAddrInfo); + + if (theOTResult == noErr) + { + PrepareForAsyncOperation(theSocketStruct,T_PASSCON); + + theOTResult = ::OTAccept(theSocketStruct->mEndPointRef,theSocketStruct->mEndPointRef,theSocketStruct->mRemoteAddrInfo); + + SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_listen: Can't begin OT accept, OTAccept() = ",theOTResult); + + BailIfError(MyBusyWait(theSocketStruct,false,&theOTResult,&(theSocketStruct->mReceivedTPassCon))); + + SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_listen: Can't accept OT connection, OTAccept() = ",theOTResult); + } + + else if (theOTResult == kOTNoDataErr) + { + theOTResult = noErr; + } + + else + { + SetErrorMessageAndLongIntAndBail("MacSocket_listen: Can't begin OT listen, OTListen() = ",theOTResult); + } + + + errCode = noErr; + + +EXITPOINT: + + if (theSocketStruct != nil) + { + theSocketStruct->mLastError = noErr; + + CopyCStrToCStr("",theSocketStruct->mErrMessage,sizeof(theSocketStruct->mErrMessage)); + + if (errCode != noErr) + { + theSocketStruct->mLastError = errCode; + + CopyCStrToCStr(GetErrorMessage(),theSocketStruct->mErrMessage,sizeof(theSocketStruct->mErrMessage)); + } + } + + errno = errCode; + + return(errCode); +} + + + + +OSErr MacSocket_connect(const int inSocketNum,char *inTargetAddressAndPort) +{ +OSErr errCode = noErr; +SocketStruct *theSocketStruct = nil; + + + if (!SocketIndexIsValid(inSocketNum)) + { + SetErrorMessageAndBail("MacSocket_connect: Invalid socket number specified"); + } + + theSocketStruct = &(sSockets[inSocketNum]); + + if (theSocketStruct->mEndpointIsBound) + { + SetErrorMessageAndBail("MacSocket_connect: Socket previously bound"); + } + + +OTResult theOTResult; + + theSocketStruct->mBindRequestedAddrInfo = (TBind *) ::OTAlloc(theSocketStruct->mEndPointRef,T_BIND,T_ADDR,&theOTResult); + + SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_connect: Can't allocate OT T_BIND structure, OTAlloc() = ",theOTResult); + SetErrorMessageAndBailIfNil(theSocketStruct->mBindRequestedAddrInfo,"MacSocket_connect: Can't allocate OT T_BIND structure, OTAlloc() returned nil"); + + + theSocketStruct->mAssignedAddrInfo = (TBind *) ::OTAlloc(theSocketStruct->mEndPointRef,T_BIND,T_ADDR,&theOTResult); + + SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_connect: Can't allocate OT T_BIND structure, OTAlloc() = ",theOTResult); + SetErrorMessageAndBailIfNil(theSocketStruct->mAssignedAddrInfo,"MacSocket_connect: Can't allocate OT T_BIND structure, OTAlloc() returned nil"); + + + theSocketStruct->mRemoteAddrInfo = (TCall *) ::OTAlloc(theSocketStruct->mEndPointRef,T_CALL,T_ADDR,&theOTResult); + + SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_connect: Can't allocate OT T_CALL structure, OTAlloc() = ",theOTResult); + SetErrorMessageAndBailIfNil(theSocketStruct->mRemoteAddrInfo,"MacSocket_connect: Can't allocate OT T_CALL structure, OTAlloc() returned nil"); + + + PrepareForAsyncOperation(theSocketStruct,T_BINDCOMPLETE); + + theOTResult = ::OTBind(theSocketStruct->mEndPointRef,nil,theSocketStruct->mAssignedAddrInfo); + + SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_connect: Can't bind OT endpoint, OTBind() = ",theOTResult); + + BailIfError(MyBusyWait(theSocketStruct,false,&theOTResult,&(theSocketStruct->mReceivedTBindComplete))); + + SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_connect: Can't bind OT endpoint, OTBind() = ",theOTResult); + + theSocketStruct->mEndpointIsBound = true; + + +TCall sndCall; +DNSAddress hostDNSAddress; + + // Set up target address + + sndCall.addr.buf = (UInt8 *) &hostDNSAddress; + sndCall.addr.len = ::OTInitDNSAddress(&hostDNSAddress,inTargetAddressAndPort); + sndCall.opt.buf = nil; + sndCall.opt.len = 0; + sndCall.udata.buf = nil; + sndCall.udata.len = 0; + sndCall.sequence = 0; + + // Connect! + + PrepareForAsyncOperation(theSocketStruct,T_CONNECT); + + theOTResult = ::OTConnect(theSocketStruct->mEndPointRef,&sndCall,nil); + + if (theOTResult == kOTNoDataErr) + { + theOTResult = noErr; + } + + SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_connect: Can't connect OT endpoint, OTConnect() = ",theOTResult); + + BailIfError(MyBusyWait(theSocketStruct,false,&theOTResult,&(theSocketStruct->mReceivedTConnect))); + + if (theOTResult == kMacSocket_TimeoutErr) + { + SetErrorMessageAndBail("MacSocket_connect: Can't connect OT endpoint, OTConnect() = kMacSocket_TimeoutErr"); + } + + else + { + SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_connect: Can't connect OT endpoint, OTConnect() = ",theOTResult); + } + + theOTResult = ::OTRcvConnect(theSocketStruct->mEndPointRef,nil); + + SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_connect: Can't complete connect on OT endpoint, OTRcvConnect() = ",theOTResult); + + + errCode = noErr; + + +#ifdef MACSOCKET_DEBUG + printf("MacSocket_connect: connect completed\n"); +#endif + +EXITPOINT: + + if (theSocketStruct != nil) + { + theSocketStruct->mLastError = noErr; + + CopyCStrToCStr("",theSocketStruct->mErrMessage,sizeof(theSocketStruct->mErrMessage)); + + if (errCode != noErr) + { + theSocketStruct->mLastError = errCode; + + CopyCStrToCStr(GetErrorMessage(),theSocketStruct->mErrMessage,sizeof(theSocketStruct->mErrMessage)); + } + } + + errno = errCode; + + return(errCode); +} + + + + +// Close a connection + +OSErr MacSocket_close(const int inSocketNum) +{ +OSErr errCode = noErr; +SocketStruct *theSocketStruct = nil; + + + if (!SocketIndexIsValid(inSocketNum)) + { + SetErrorMessageAndBail("MacSocket_close: Invalid socket number specified"); + } + + + theSocketStruct = &(sSockets[inSocketNum]); + + if (theSocketStruct->mEndPointRef != kOTInvalidEndpointRef) + { + OTResult theOTResult = noErr; + + // Try to play nice + + if (theSocketStruct->mReceivedTOrdRel) + { + // Already did an OTRcvOrderlyDisconnect() in the notifier + + if (theSocketStruct->mLocalEndIsConnected) + { + theOTResult = ::OTSndOrderlyDisconnect(theSocketStruct->mEndPointRef); + + theSocketStruct->mLocalEndIsConnected = false; + } + } + + else if (theSocketStruct->mLocalEndIsConnected) + { + theOTResult = ::OTSndOrderlyDisconnect(theSocketStruct->mEndPointRef); + + theSocketStruct->mLocalEndIsConnected = false; + + // Wait for other end to hang up too! + +// PrepareForAsyncOperation(theSocketStruct,T_ORDREL); +// +// errCode = MyBusyWait(theSocketStruct,false,&theOTResult,&(theSocketStruct->mReceivedTOrdRel)); + } + + + if (theOTResult != noErr) + { + ::OTCloseProvider(theSocketStruct->mEndPointRef); + } + + else + { + theOTResult = ::OTCloseProvider(theSocketStruct->mEndPointRef); + } + + theSocketStruct->mEndPointRef = kOTInvalidEndpointRef; + + errCode = theOTResult; + } + + + theSocketStruct->mIsInUse = false; + + +EXITPOINT: + + if (theSocketStruct != nil) + { + theSocketStruct->mLastError = noErr; + + CopyCStrToCStr("",theSocketStruct->mErrMessage,sizeof(theSocketStruct->mErrMessage)); + + if (errCode != noErr) + { + theSocketStruct->mLastError = errCode; + + CopyCStrToCStr(GetErrorMessage(),theSocketStruct->mErrMessage,sizeof(theSocketStruct->mErrMessage)); + } + } + + errno = errCode; + + return(errCode); +} + + + + +// Receive some bytes + +int MacSocket_recv(const int inSocketNum,void *outBuff,int outBuffLength,const Boolean inBlock) +{ +OSErr errCode = noErr; +int totalBytesRead = 0; +SocketStruct *theSocketStruct = nil; + + + SetErrorMessageAndBailIfNil(outBuff,"MacSocket_recv: Bad parameter, outBuff = nil"); + + if (outBuffLength <= 0) + { + SetErrorMessageAndBail("MacSocket_recv: Bad parameter, outBuffLength <= 0"); + } + + if (!SocketIndexIsValid(inSocketNum)) + { + SetErrorMessageAndBail("MacSocket_recv: Invalid socket number specified"); + } + + theSocketStruct = &(sSockets[inSocketNum]); + + if (!theSocketStruct->mLocalEndIsConnected) + { + SetErrorMessageAndBail("MacSocket_recv: Socket not connected"); + } + + if (theSocketStruct->mReceivedTOrdRel) + { + totalBytesRead = 0; + + goto EXITPOINT; + } + + + PrepareForAsyncOperation(theSocketStruct,0); + + for (;;) + { + int bytesRead; + OTResult theOTResult; + + + theOTResult = ::OTRcv(theSocketStruct->mEndPointRef,(void *) ((unsigned long) outBuff + (unsigned long) totalBytesRead),outBuffLength - totalBytesRead,nil); + + if (theOTResult >= 0) + { + bytesRead = theOTResult; + +#ifdef MACSOCKET_DEBUG + printf("MacSocket_recv: read %d bytes in part\n",bytesRead); +#endif + } + + else if (theOTResult == kOTNoDataErr) + { + bytesRead = 0; + } + + else + { + SetErrorMessageAndLongIntAndBail("MacSocket_recv: Can't receive OT data, OTRcv() = ",theOTResult); + } + + + totalBytesRead += bytesRead; + + + if (totalBytesRead <= 0) + { + if (theSocketStruct->mReceivedTOrdRel) + { + break; + } + + // This seems pretty stupid to me now. Maybe I'll delete this blocking garbage. + + if (inBlock) + { + if (TimeoutElapsed(theSocketStruct)) + { + SetErrorCodeAndMessageAndBail(kMacSocket_TimeoutErr,"MacSocket_recv: Receive operation timed-out"); + } + + if (theSocketStruct->mIdleWaitCallback != nil) + { + theOTResult = (*(theSocketStruct->mIdleWaitCallback))(theSocketStruct->mUserRefPtr); + + SetErrorMessageAndBailIfError(theOTResult,"MacSocket_recv: User cancelled operation"); + } + + continue; + } + } + + + break; + } + + errCode = noErr; + + +#ifdef MACSOCKET_DEBUG + printf("MacSocket_recv: read %d bytes in total\n",totalBytesRead); +#endif + + +EXITPOINT: + + if (theSocketStruct != nil) + { + theSocketStruct->mLastError = noErr; + + CopyCStrToCStr("",theSocketStruct->mErrMessage,sizeof(theSocketStruct->mErrMessage)); + + if (errCode != noErr) + { + theSocketStruct->mLastError = errCode; + + CopyCStrToCStr(GetErrorMessage(),theSocketStruct->mErrMessage,sizeof(theSocketStruct->mErrMessage)); + } + } + + errno = errCode; + + return(totalBytesRead); +} + + + +// Send some bytes + +int MacSocket_send(const int inSocketNum,const void *inBuff,int inBuffLength) +{ +OSErr errCode = noErr; +int bytesSent = 0; +SocketStruct *theSocketStruct = nil; + + + SetErrorMessageAndBailIfNil(inBuff,"MacSocket_send: Bad parameter, inBuff = nil"); + + if (inBuffLength <= 0) + { + SetErrorMessageAndBail("MacSocket_send: Bad parameter, inBuffLength <= 0"); + } + + if (!SocketIndexIsValid(inSocketNum)) + { + SetErrorMessageAndBail("MacSocket_send: Invalid socket number specified"); + } + + + theSocketStruct = &(sSockets[inSocketNum]); + + if (!theSocketStruct->mLocalEndIsConnected) + { + SetErrorMessageAndBail("MacSocket_send: Socket not connected"); + } + + +OTResult theOTResult; + + + PrepareForAsyncOperation(theSocketStruct,0); + + while (bytesSent < inBuffLength) + { + if (theSocketStruct->mIdleWaitCallback != nil) + { + theOTResult = (*(theSocketStruct->mIdleWaitCallback))(theSocketStruct->mUserRefPtr); + + SetErrorMessageAndBailIfError(theOTResult,"MacSocket_send: User cancelled"); + } + + + theOTResult = ::OTSnd(theSocketStruct->mEndPointRef,(void *) ((unsigned long) inBuff + bytesSent),inBuffLength - bytesSent,0); + + if (theOTResult >= 0) + { + bytesSent += theOTResult; + + theOTResult = noErr; + + // Reset timer.... + + PrepareForAsyncOperation(theSocketStruct,0); + } + + if (theOTResult == kOTFlowErr) + { + if (TimeoutElapsed(theSocketStruct)) + { + SetErrorCodeAndMessageAndBail(kMacSocket_TimeoutErr,"MacSocket_send: Send timed-out") + } + + theOTResult = noErr; + } + + SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_send: Can't send OT data, OTSnd() = ",theOTResult); + } + + + errCode = noErr; + +#ifdef MACSOCKET_DEBUG + printf("MacSocket_send: sent %d bytes\n",bytesSent); +#endif + + +EXITPOINT: + + if (theSocketStruct != nil) + { + theSocketStruct->mLastError = noErr; + + CopyCStrToCStr("",theSocketStruct->mErrMessage,sizeof(theSocketStruct->mErrMessage)); + + if (errCode != noErr) + { + theSocketStruct->mLastError = errCode; + + CopyCStrToCStr(GetErrorMessage(),theSocketStruct->mErrMessage,sizeof(theSocketStruct->mErrMessage)); + } + } + + if (errCode != noErr) + { + ::SysBeep(1); + } + + errno = errCode; + + return(bytesSent); +} + + + + + +static OSStatus NegotiateIPReuseAddrOption(EndpointRef inEndpoint,const Boolean inEnableReuseIP) +{ +OSStatus errCode; +UInt8 buf[kOTFourByteOptionSize]; +TOption* theOTOption; +TOptMgmt theOTRequest; +TOptMgmt theOTResult; + + + if (!OTIsSynchronous(inEndpoint)) + { + SetErrorMessageAndBail("NegotiateIPReuseAddrOption: Open Transport endpoint is not synchronous"); + } + + theOTRequest.opt.buf = buf; + theOTRequest.opt.len = sizeof(buf); + theOTRequest.flags = T_NEGOTIATE; + + theOTResult.opt.buf = buf; + theOTResult.opt.maxlen = kOTFourByteOptionSize; + + + theOTOption = (TOption *) buf; + + theOTOption->level = INET_IP; + theOTOption->name = IP_REUSEADDR; + theOTOption->len = kOTFourByteOptionSize; + theOTOption->status = 0; + *((UInt32 *) (theOTOption->value)) = inEnableReuseIP; + + errCode = ::OTOptionManagement(inEndpoint,&theOTRequest,&theOTResult); + + if (errCode == kOTNoError) + { + if (theOTOption->status != T_SUCCESS) + { + errCode = theOTOption->status; + } + + else + { + errCode = kOTNoError; + } + } + + +EXITPOINT: + + errno = errCode; + + return(errCode); +} + + + + + +// Some rough notes.... + + + +// OTAckSends(ep); +// OTAckSends(ep) // enable AckSend option +// ...... +// buf = OTAllocMem( nbytes); // Allocate nbytes of memory from OT +// OTSnd(ep, buf, nbytes, 0); // send a packet +// ...... +// NotifyProc( .... void* theParam) // Notifier Proc +// case T_MEMORYRELEASED: // process event +// OTFreeMem( theParam); // free up memory +// break; + + + +/* +struct InetInterfaceInfo +{ + InetHost fAddress; + InetHost fNetmask; + InetHost fBroadcastAddr; + InetHost fDefaultGatewayAddr; + InetHost fDNSAddr; + UInt16 fVersion; + UInt16 fHWAddrLen; + UInt8* fHWAddr; + UInt32 fIfMTU; + UInt8* fReservedPtrs[2]; + InetDomainName fDomainName; + UInt32 fIPSecondaryCount; + UInt8 fReserved[252]; +}; +typedef struct InetInterfaceInfo InetInterfaceInfo; + + + +((InetAddress *) addr.buf)->fHost + +struct TBind +{ + TNetbuf addr; + OTQLen qlen; +}; + +typedef struct TBind TBind; + +struct TNetbuf +{ + size_t maxlen; + size_t len; + UInt8* buf; +}; + +typedef struct TNetbuf TNetbuf; + + + struct InetAddress +{ + OTAddressType fAddressType; // always AF_INET + InetPort fPort; // Port number + InetHost fHost; // Host address in net byte order + UInt8 fUnused[8]; // Traditional unused bytes +}; +typedef struct InetAddress InetAddress; +*/ + + + +/* +static pascal void Notifier(void* context, OTEventCode event, OTResult result, void* cookie) +{ +EPInfo* epi = (EPInfo*) context; + + switch (event) + { + case T_LISTEN: + { + DoListenAccept(); + return; + } + + case T_ACCEPTCOMPLETE: + { + if (result != kOTNoError) + DBAlert1("Notifier: T_ACCEPTCOMPLETE - result %d",result); + return; + } + + case T_PASSCON: + { + if (result != kOTNoError) + { + DBAlert1("Notifier: T_PASSCON result %d", result); + return; + } + + OTAtomicAdd32(1, &gCntrConnections); + OTAtomicAdd32(1, &gCntrTotalConnections); + OTAtomicAdd32(1, &gCntrIntervalConnects); + + if ( OTAtomicSetBit(&epi->stateFlags, kPassconBit) != 0 ) + { + ReadData(epi); + } + + return; + } + + case T_DATA: + { + if ( OTAtomicSetBit(&epi->stateFlags, kPassconBit) != 0 ) + { + ReadData(epi); + } + + return; + } + + case T_GODATA: + { + SendData(epi); + return; + } + + case T_DISCONNECT: + { + DoRcvDisconnect(epi); + return; + } + + case T_DISCONNECTCOMPLETE: + { + if (result != kOTNoError) + DBAlert1("Notifier: T_DISCONNECT_COMPLETE result %d",result); + + return; + } + + case T_MEMORYRELEASED: + { + OTAtomicAdd32(-1, &epi->outstandingSends); + return; + } + + default: + { + DBAlert1("Notifier: unknown event <%x>", event); + return; + } + } +} +*/ diff --git a/openssl/MacOS/GetHTTPS.src/MacSocket.h b/openssl/MacOS/GetHTTPS.src/MacSocket.h new file mode 100644 index 000000000..ad59dc9e4 --- /dev/null +++ b/openssl/MacOS/GetHTTPS.src/MacSocket.h @@ -0,0 +1,103 @@ +#pragma once + + +#ifdef __cplusplus +extern "C" { +#endif + + + +enum +{ + kMacSocket_TimeoutErr = -2 +}; + + +// Since MacSocket does busy waiting, I do a callback while waiting + +typedef OSErr (*MacSocket_IdleWaitCallback)(void *); + + +// Call this before anything else! + +OSErr MacSocket_Startup(void); + + +// Call this to cleanup before quitting + +OSErr MacSocket_Shutdown(void); + + +// Call this to allocate a "socket" (reference number is returned in outSocketNum) +// Note that inDoThreadSwitching is pretty much irrelevant right now, since I ignore it +// The inTimeoutTicks parameter is applied during reads/writes of data +// The inIdleWaitCallback parameter specifies a callback which is called during busy-waiting periods +// The inUserRefPtr parameter is passed back to the idle-wait callback + +OSErr MacSocket_socket(int *outSocketNum,const Boolean inDoThreadSwitching,const long inTimeoutTicks,MacSocket_IdleWaitCallback inIdleWaitCallback,void *inUserRefPtr); + + +// Call this to connect to an IP/DNS address +// Note that inTargetAddressAndPort is in "IP:port" format-- e.g. 10.1.1.1:123 + +OSErr MacSocket_connect(const int inSocketNum,char *inTargetAddressAndPort); + + +// Call this to listen on a port +// Since this a low-performance implementation, I allow a maximum of 1 (one!) incoming request when I listen + +OSErr MacSocket_listen(const int inSocketNum,const int inPortNum); + + +// Call this to close a socket + +OSErr MacSocket_close(const int inSocketNum); + + +// Call this to receive data on a socket +// Most parameters' purpose are obvious-- except maybe "inBlock" which controls whether I wait for data or return immediately + +int MacSocket_recv(const int inSocketNum,void *outBuff,int outBuffLength,const Boolean inBlock); + + +// Call this to send data on a socket + +int MacSocket_send(const int inSocketNum,const void *inBuff,int inBuffLength); + + +// If zero bytes were read in a call to MacSocket_recv(), it may be that the remote end has done a half-close +// This function will let you check whether that's true or not + +Boolean MacSocket_RemoteEndIsClosing(const int inSocketNum); + + +// Call this to see if the listen has completed after a call to MacSocket_listen() + +Boolean MacSocket_ListenCompleted(const int inSocketNum); + + +// These really aren't very useful anymore + +Boolean MacSocket_LocalEndIsOpen(const int inSocketNum); +Boolean MacSocket_RemoteEndIsOpen(const int inSocketNum); + + +// You may wish to change the userRefPtr for a socket callback-- use this to do it + +void MacSocket_SetUserRefPtr(const int inSocketNum,void *inNewRefPtr); + + +// Call these to get the socket's IP:port descriptor + +void MacSocket_GetLocalIPAndPort(const int inSocketNum,char *outIPAndPort,const int inIPAndPortLength); +void MacSocket_GetRemoteIPAndPort(const int inSocketNum,char *outIPAndPort,const int inIPAndPortLength); + + +// Call this to get error info from a socket + +void MacSocket_GetSocketErrorInfo(const int inSocketNum,int *outSocketErrCode,char *outSocketErrString,const int inSocketErrStringMaxLength); + + +#ifdef __cplusplus +} +#endif -- cgit v1.2.3