// --------------------------------------------------------------------------- // ICUTransService: The protected virtual transcoding service API // --------------------------------------------------------------------------- XMLTranscoder* ICUTransService:: makeNewXMLTranscoder(const XMLCh* const encodingName , XMLTransService::Codes& resValue , const unsigned int blockSize , MemoryManager* const manager) { // // For encodings that end with "s390" we need to strip off the "s390" // from the encoding name and add ",swaplfnl" to the encoding name // that we pass into ICU on the ucnv_openU. // XMLCh* encodingNameToUse = (XMLCh*) encodingName; XMLCh* workBuffer = 0; if ( (XMLString::endsWith(encodingNameToUse, gs390Id)) || (XMLString::endsWith(encodingNameToUse, gS390Id)) ) { int workBufferSize = (XMLString::stringLen(encodingNameToUse) + XMLString::stringLen(gswaplfnlId) - XMLString::stringLen(gS390Id) + 1); workBuffer = (XMLCh*) manager->allocate(workBufferSize * sizeof(XMLCh)); int moveSize = XMLString::stringLen(encodingNameToUse) - XMLString::stringLen(gS390Id); XMLString::moveChars(workBuffer, encodingNameToUse, moveSize); XMLString::moveChars((workBuffer + moveSize), gswaplfnlId, XMLString::stringLen(gswaplfnlId)); encodingNameToUse = workBuffer; } // // If UChar and XMLCh are not the same size, then we have premassage the // encoding name into a UChar type string. // const UChar* actualName; UChar* tmpName = 0; if (sizeof(UChar) == sizeof(XMLCh)) { actualName = (const UChar*)encodingNameToUse; } else { tmpName = convertToUChar(encodingNameToUse, 0, manager); actualName = tmpName; } ArrayJanitor<UChar> janTmp(tmpName, manager); ArrayJanitor<XMLCh> janTmp1(workBuffer, manager); UErrorCode uerr = U_ZERO_ERROR; UConverter* converter = ucnv_openU(actualName, &uerr); if (!converter) { resValue = XMLTransService::UnsupportedEncoding; return 0; } return new (manager) ICUTranscoder(encodingName, converter, blockSize, manager); }
BitmapPNG* LoadPNG(vgui::InputStream& stream) { //allocate structures for read png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); if (!png_ptr) return NULL; png_infop info_ptr = png_create_info_struct(png_ptr); if (!info_ptr) { png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL); return NULL; } //set up our alternate read function png_set_read_fn(png_ptr,&stream,png_read_vgui_stream); //read the image png_read_png(png_ptr,info_ptr,PNG_TRANSFORM_PACKING | PNG_TRANSFORM_STRIP_16,NULL); png_bytepp rows = png_get_rows(png_ptr, info_ptr); png_read_image(png_ptr,rows); //we now have the information in row format, it needs to be converted to a single array //of 32bit pixel values specified as a uchar* so that we can create the image object uchar* data = new uchar[info_ptr->width*info_ptr->height*4]; convertToUChar(info_ptr,rows,data); //create the BitmpPNG that we are going to return BitmapPNG* returnVal = new BitmapPNG(info_ptr->width,info_ptr->height,data); //read the information after the image for completeness png_read_end(png_ptr,NULL); //free up png structures and other memory we've used delete[] data; png_destroy_read_struct(&png_ptr,&info_ptr,(png_infopp)NULL); return returnVal; }
unsigned int ICUTranscoder::transcodeTo( const XMLCh* const srcData , const unsigned int srcCount , XMLByte* const toFill , const unsigned int maxBytes , unsigned int& charsEaten , const UnRepOpts options) { // // Get a pointer to the buffer to transcode. If UChar and XMLCh are // the same size here, then use the original. Else, create a temp // one and put a janitor on it. // const UChar* srcPtr; UChar* tmpBufPtr = 0; if (sizeof(XMLCh) == sizeof(UChar)) { srcPtr = (const UChar*)srcData; } else { tmpBufPtr = convertToUChar(srcData, srcCount, getMemoryManager()); srcPtr = tmpBufPtr; } ArrayJanitor<UChar> janTmpBuf(tmpBufPtr, getMemoryManager()); // // Set the appropriate callback so that it will either fail or use // the rep char. Remember the old one so we can put it back. // UErrorCode err = U_ZERO_ERROR; UConverterFromUCallback oldCB = NULL; #if (U_ICU_VERSION_MAJOR_NUM < 2) void* orgContent; #else const void* orgContent; #endif ucnv_setFromUCallBack ( fConverter , (options == UnRep_Throw) ? UCNV_FROM_U_CALLBACK_STOP : UCNV_FROM_U_CALLBACK_SUBSTITUTE , NULL , &oldCB , &orgContent , &err ); // // Ok, lets transcode as many chars as we we can in one shot. The // ICU API gives enough info not to have to do this one char by char. // XMLByte* startTarget = toFill; const UChar* startSrc = srcPtr; err = U_ZERO_ERROR; ucnv_fromUnicode ( fConverter , (char**)&startTarget , (char*)(startTarget + maxBytes) , &startSrc , srcPtr + srcCount , 0 , false , &err ); // Rememember the status before we possibly overite the error code const bool res = (err == U_ZERO_ERROR); // Put the old handler back err = U_ZERO_ERROR; UConverterFromUCallback orgAction = NULL; ucnv_setFromUCallBack(fConverter, oldCB, NULL, &orgAction, &orgContent, &err); if (!res) { XMLCh tmpBuf[17]; XMLString::binToText((unsigned int)*startSrc, tmpBuf, 16, 16, getMemoryManager()); ThrowXMLwithMemMgr2 ( TranscodingException , XMLExcepts::Trans_Unrepresentable , tmpBuf , getEncodingName() , getMemoryManager() ); } // Fill in the chars we ate from the input charsEaten = startSrc - srcPtr; // Return the chars we stored return startTarget - toFill; }