String XercesParser::transcodeXmlCharToString(const XMLCh* const xmlch_str, unsigned int inputLength) { XERCES_CPP_NAMESPACE_USE; XMLTransService::Codes res; XMLTranscoder* transcoder = XMLPlatformUtils::fgTransService->makeNewTranscoderFor(XMLRecognizer::UTF_8, res, 4096, XMLPlatformUtils::fgMemoryManager ); if (res == XMLTransService::Ok) { String out; #if _XERCES_VERSION >= 30000 XMLByte outBuff[128]; XMLSize_t outputLength; XMLSize_t eaten = 0; XMLSize_t offset = 0; #else /* _XERCES_VERSION >= 30000 */ utf8 outBuff[128]; unsigned int outputLength; unsigned int eaten = 0; unsigned int offset = 0; #endif /* _XERCES_VERSION >= 30000 */ // unsigned int inputLength = XMLString::stringLen(xmlch_str); // dalfy caracters node need to transcode but give the size while (inputLength) { outputLength = transcoder->transcodeTo(xmlch_str + offset, inputLength, outBuff, 128, eaten, XMLTranscoder::UnRep_RepChar); out.append(outBuff, outputLength); offset += eaten; inputLength -= eaten; } delete transcoder; return out; } else { throw GenericException("XercesParser::transcodeXmlCharToString - Internal Error: Could not create UTF-8 string transcoder."); } }
LIBSBML_CPP_NAMESPACE_BEGIN /** @endcond */ /** @cond doxygenLibsbmlInternal */ /** * convert the given internal XMLCh* string to the UTF-8 char* string. */ char* XercesTranscode::transcodeToUTF8(const XMLCh* src_str) { if ( src_str == NULL ) { char* str = new char[1]; str[0] = '\0'; return str; } const XercesSize_t block_size = 8192; XMLTransService::Codes res_value; XMLTranscoder* transcoder = XMLPlatformUtils::fgTransService->makeNewTranscoderFor( XMLRecognizer::UTF_8, res_value, block_size); if ( transcoder == NULL ) { // this should not happen return xercesc::XMLString::transcode(src_str); } const XMLCh* cur_srcptr= src_str; XercesSize_t src_size = XMLString::stringLen(src_str); XercesSize_t read_size = 0; XercesSize_t dst_size = 0; char* utf8_str = new char[1]; utf8_str[0] = '\0'; while ( read_size < src_size ) { XMLByte* buf_tofill = new XMLByte[block_size+4]; XercesSize_t rest_size = src_size - read_size; XercesSize_t tmpbuf_size = (rest_size > block_size) ? block_size : rest_size; XercesSize_t numchars_eaten = 0; XercesSize_t numchars_dst = 0; // // converts from the internal XMLCh* encoding to the UTF-8 encoding. // // XMLTranscoder::UnRep_Throw : Throw an exception. // XMLTranscoder::UnRep_RepChar : Use the replacement char. // numchars_dst = transcoder->transcodeTo(cur_srcptr, tmpbuf_size, buf_tofill, block_size, numchars_eaten, XMLTranscoder::UnRep_RepChar); if (numchars_dst <= block_size) { for(int i=0; i < 4; i++) { buf_tofill[numchars_dst+i] = 0; } } cur_srcptr += numchars_eaten; read_size += numchars_eaten; dst_size += numchars_dst; char* new_str = new char[dst_size+1]; XMLString::copyString(new_str, utf8_str); XMLString::catString(new_str, reinterpret_cast<char*>(buf_tofill) ); delete [] utf8_str; delete [] buf_tofill; utf8_str = new_str; } delete transcoder; return utf8_str; }
XERCES_CPP_NAMESPACE_BEGIN UnixHTTPURLInputStream::UnixHTTPURLInputStream(const XMLURL& urlSource, const XMLNetHTTPInfo* httpInfo/*=0*/) : fSocket(0) , fBytesProcessed(0) , fMemoryManager(urlSource.getMemoryManager()) { // // Constants in ASCII to send/check in the HTTP request/response // const char GET[] = { chLatin_G, chLatin_E, chLatin_T, chSpace, chNull }; const char PUT[] = { chLatin_P, chLatin_U, chLatin_T, chSpace, chNull }; const char POST[] = { chLatin_P, chLatin_O, chLatin_S, chLatin_T, chSpace, chNull }; const char HTTP[] = { chLatin_H, chLatin_T, chLatin_T, chLatin_P, chNull }; const char HTTP10[] = { chSpace, chLatin_H, chLatin_T, chLatin_T, chLatin_P, chForwardSlash, chDigit_1, chPeriod, chDigit_0, chCR, chLF, chNull }; const char CRLF[] = { chCR, chLF, chNull }; const char CRLF2X[] = { chCR, chLF, chCR, chLF, chNull }; const char LF2X[] = { chLF, chLF, chNull }; const char HOST[] = { chLatin_H, chLatin_o, chLatin_s, chLatin_t, chColon, chSpace, chNull }; const char COLON[] = { chColon, chNull }; const char resp200 [] = { chSpace, chDigit_2, chDigit_0, chDigit_0, chSpace, chNull }; unsigned int charsEaten; unsigned int transSize; XMLTransService::Codes failReason; const unsigned int blockSize = 2048; const unsigned int bufSize = 5; static XMLCh portBuffer[bufSize+1]; // // Pull all of the parts of the URL out of the urlSource object // const XMLCh* hostName = urlSource.getHost(); const XMLCh* path = urlSource.getPath(); const XMLCh* fragment = urlSource.getFragment(); const XMLCh* query = urlSource.getQuery(); // // Convert the hostName to the platform's code page for gethostbyname and // inet_addr functions. // char* hostNameAsCharStar = XMLString::transcode(hostName, fMemoryManager); ArrayJanitor<char> janBuf1(hostNameAsCharStar, fMemoryManager); // // Convert all the parts of the urlSource object to ASCII so they can be // sent to the remote host in that format // transSize = XMLString::stringLen(hostName)+1; char* hostNameAsASCII = (char*) fMemoryManager->allocate ( (transSize+1) * sizeof(char) );//new char[transSize+1]; ArrayJanitor<char> janBuf2(hostNameAsASCII, fMemoryManager); XMLTranscoder* trans = XMLPlatformUtils::fgTransService->makeNewTranscoderFor("ISO8859-1", failReason, blockSize, fMemoryManager); trans->transcodeTo(hostName, transSize, (unsigned char *) hostNameAsASCII, transSize, charsEaten, XMLTranscoder::UnRep_Throw); char* pathAsASCII = 0; ArrayJanitor<char> janBuf3(pathAsASCII, fMemoryManager); if (path) { transSize = XMLString::stringLen(path)+1; pathAsASCII = (char*) fMemoryManager->allocate ( (transSize+1) * sizeof(char) );//new char[transSize+1]; trans->transcodeTo(path, transSize, (unsigned char *) pathAsASCII, transSize, charsEaten, XMLTranscoder::UnRep_Throw); } char* fragmentAsASCII = 0; ArrayJanitor<char> janBuf4(fragmentAsASCII, fMemoryManager); if (fragment) { transSize = XMLString::stringLen(fragment)+1; fragmentAsASCII = (char*) fMemoryManager->allocate ( (transSize+1) * sizeof(char) );//new char[transSize+1]; trans->transcodeTo(fragment, transSize, (unsigned char *) fragmentAsASCII, transSize, charsEaten, XMLTranscoder::UnRep_Throw); } char* queryAsASCII = 0; ArrayJanitor<char> janBuf5(queryAsASCII, fMemoryManager); if (query) { transSize = XMLString::stringLen(query)+1; queryAsASCII = (char*) fMemoryManager->allocate ( (transSize+1) * sizeof(char) );//new char[transSize+1]; trans->transcodeTo(query, transSize, (unsigned char *) queryAsASCII, transSize, charsEaten, XMLTranscoder::UnRep_Throw); } unsigned short portNumber = (unsigned short) urlSource.getPortNum(); // // Convert port number integer to unicode so we can transcode it to ASCII // XMLString::binToText((unsigned int) portNumber, portBuffer, bufSize, 10, fMemoryManager); transSize = XMLString::stringLen(portBuffer)+1; char* portAsASCII = (char*) fMemoryManager->allocate ( (transSize+1) * sizeof(char) );//new char[transSize+1]; trans->transcodeTo(portBuffer, transSize, (unsigned char *) portAsASCII, transSize, charsEaten, XMLTranscoder::UnRep_Throw); delete trans; // // Set up a socket. // struct hostent* hostEntPtr = 0; struct sockaddr_in sa; // Use the hostName in the local code page .... if ((hostEntPtr = gethostbyname(hostNameAsCharStar)) == NULL) { unsigned long numAddress = inet_addr(hostNameAsCharStar); if (numAddress < 0) { ThrowXMLwithMemMgr1(NetAccessorException, XMLExcepts::NetAcc_TargetResolution, hostName, fMemoryManager); } if ((hostEntPtr = gethostbyaddr((char *) &numAddress, sizeof(unsigned long), AF_INET)) == NULL) { ThrowXMLwithMemMgr1(NetAccessorException, XMLExcepts::NetAcc_TargetResolution, hostName, fMemoryManager); } } memset(&sa, '\0', sizeof(sockaddr_in)); // iSeries fix ?? memcpy((void *) &sa.sin_addr, (const void *) hostEntPtr->h_addr, hostEntPtr->h_length); sa.sin_family = hostEntPtr->h_addrtype; sa.sin_port = htons(portNumber); int s = socket(hostEntPtr->h_addrtype, SOCK_STREAM, 0); if (s < 0) { ThrowXMLwithMemMgr1(NetAccessorException, XMLExcepts::NetAcc_CreateSocket, urlSource.getURLText(), fMemoryManager); } if (connect(s, (struct sockaddr *) &sa, sizeof(sa)) < 0) { ThrowXMLwithMemMgr1(NetAccessorException, XMLExcepts::NetAcc_ConnSocket, urlSource.getURLText(), fMemoryManager); } // The port is open and ready to go. // Build up the http GET command to send to the server. // To do: We should really support http 1.1. This implementation // is weak. if(httpInfo==0) strcpy(fBuffer, GET); else switch(httpInfo->fHTTPMethod) { case XMLNetHTTPInfo::GET: strcpy(fBuffer, GET); break; case XMLNetHTTPInfo::PUT: strcpy(fBuffer, PUT); break; case XMLNetHTTPInfo::POST: strcpy(fBuffer, POST); break; } if (pathAsASCII != 0) { strcat(fBuffer, pathAsASCII); } if (queryAsASCII != 0) { size_t n = strlen(fBuffer); fBuffer[n] = chQuestion; fBuffer[n+1] = chNull; strcat(fBuffer, queryAsASCII); } if (fragmentAsASCII != 0) { strcat(fBuffer, fragmentAsASCII); } strcat(fBuffer, HTTP10); strcat(fBuffer, HOST); strcat(fBuffer, hostNameAsASCII); if (portNumber != 80) { strcat(fBuffer,COLON); strcat(fBuffer,portAsASCII); } strcat(fBuffer, CRLF); if(httpInfo!=0 && httpInfo->fHeaders!=0) strncat(fBuffer,httpInfo->fHeaders,httpInfo->fHeadersLen); strcat(fBuffer, CRLF); // Send the http request int lent = strlen(fBuffer); int aLent = 0; if ((aLent = write(s, (void *) fBuffer, lent)) != lent) { ThrowXMLwithMemMgr1(NetAccessorException, XMLExcepts::NetAcc_WriteSocket, urlSource.getURLText(), fMemoryManager); } if(httpInfo!=0 && httpInfo->fPayload!=0) { int aLent = 0; if ((aLent = write(s, (void *) httpInfo->fPayload, httpInfo->fPayloadLen)) != httpInfo->fPayloadLen) { ThrowXMLwithMemMgr1(NetAccessorException, XMLExcepts::NetAcc_WriteSocket, urlSource.getURLText(), fMemoryManager); } } // // get the response, check the http header for errors from the server. // aLent = read(s, (void *)fBuffer, sizeof(fBuffer)-1); if (aLent <= 0) { ThrowXMLwithMemMgr1(NetAccessorException, XMLExcepts::NetAcc_ReadSocket, urlSource.getURLText(), fMemoryManager); } fBufferEnd = fBuffer+aLent; *fBufferEnd = 0; // Find the break between the returned http header and any data. // (Delimited by a blank line) // Hang on to any data for use by the first read from this BinHTTPURLInputStream. // fBufferPos = strstr(fBuffer, CRLF2X); if (fBufferPos != 0) { fBufferPos += 4; *(fBufferPos-2) = 0; } else { fBufferPos = strstr(fBuffer, LF2X); if (fBufferPos != 0) { fBufferPos += 2; *(fBufferPos-1) = 0; } else fBufferPos = fBufferEnd; } // Make sure the header includes an HTTP 200 OK response. // char *p = strstr(fBuffer, HTTP); if (p == 0) { ThrowXMLwithMemMgr1(NetAccessorException, XMLExcepts::NetAcc_ReadSocket, urlSource.getURLText(), fMemoryManager); } p = strchr(p, chSpace); if (p == 0) { ThrowXMLwithMemMgr1(NetAccessorException, XMLExcepts::NetAcc_ReadSocket, urlSource.getURLText(), fMemoryManager); } if (memcmp(p, resp200, strlen(resp200))) { // Most likely a 404 Not Found error. // Should recognize and handle the forwarding responses. // ThrowXMLwithMemMgr1(NetAccessorException, XMLExcepts::File_CouldNotOpenFile, urlSource.getURLText(), fMemoryManager); } fSocket = s; }