diff options
author | Stephan Bergmann <sbergman@redhat.com> | 2019-01-18 08:18:56 +0100 |
---|---|---|
committer | Stephan Bergmann <sbergman@redhat.com> | 2019-01-18 11:14:42 +0100 |
commit | 49d8cc18a85b7cc30b04b56d52b89d9016d16120 (patch) | |
tree | 1cb1455188a10ee059bdcca1142e4c922604465c /vcl | |
parent | 34e0120ce5c9892380729a0a6e90c00c6b2eec9f (diff) |
In vcl/unx/generic/print/, use OStringBuffer instead of fixed-size arrays
...to avoid potential overflow in psp::appendStr as reported by GCC with
--enable-optimized:
> vcl/unx/generic/print/psputil.cxx: In function ‘sal_Int32 psp::appendStr(const sal_Char*, sal_Char*)’:
> vcl/unx/generic/print/psputil.cxx:127:13: error: ‘char* strncpy(char*, const char*, size_t)’ specified bound depends on the length of the source argument [-Werror=stringop-overflow=]
> strncpy (pDst, pSrc, nBytes + 1);
> ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
> vcl/unx/generic/print/psputil.cxx:126:31: note: length computed here
> sal_Int32 nBytes = strlen (pSrc);
> ~~~~~~~^~~~~~
Most of the time the original code wrote at the "end" of its char array, so
replacements with functionality based on OStringBuffer::append was
straightforward. A few places needed to use OStringBuffer::insert to mimic the
original code's writing at somewhat random positions in the char array.
The functions now taking an OStringBuffer argument still return the amount of
characters written. Even if that information would (indirectly) also be
available as part of the OStringBuffer's state, keeping the (somewhat redundant
now) counting of positions in the calling code should help avoid regressions.
Some of the code may be simplified in follow-up commits, dropping the external
counting.
The original psp::getValueOfDouble is still used elsewhere, so leave it alone
for now and add an OStringBuffer overload.
Change-Id: I2bb5d51505ca70cba485e6843183496ea3a4ce18
Reviewed-on: https://gerrit.libreoffice.org/66564
Tested-by: Jenkins
Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
Diffstat (limited to 'vcl')
-rw-r--r-- | vcl/unx/generic/print/bitmap_gfx.cxx | 93 | ||||
-rw-r--r-- | vcl/unx/generic/print/common_gfx.cxx | 178 | ||||
-rw-r--r-- | vcl/unx/generic/print/printerjob.cxx | 92 | ||||
-rw-r--r-- | vcl/unx/generic/print/psputil.cxx | 46 | ||||
-rw-r--r-- | vcl/unx/generic/print/psputil.hxx | 17 |
5 files changed, 211 insertions, 215 deletions
diff --git a/vcl/unx/generic/print/bitmap_gfx.cxx b/vcl/unx/generic/print/bitmap_gfx.cxx index eca8cbb90921..b52b892b3462 100644 --- a/vcl/unx/generic/print/bitmap_gfx.cxx +++ b/vcl/unx/generic/print/bitmap_gfx.cxx @@ -64,7 +64,7 @@ private: osl::File* const mpFile; sal_uInt32 mnColumn; sal_uInt32 mnOffset; - sal_Char mpFileBuffer[nBufferSize + 16]; + OStringBuffer mpFileBuffer; public: @@ -91,13 +91,13 @@ HexEncoder::~HexEncoder () void HexEncoder::WriteAscii (sal_uInt8 nByte) { - sal_uInt32 nOff = psp::getHexValueOf (nByte, mpFileBuffer + mnOffset); + sal_uInt32 nOff = psp::getHexValueOf (nByte, mpFileBuffer); mnColumn += nOff; mnOffset += nOff; if (mnColumn >= nLineLength) { - mnOffset += psp::appendStr ("\n", mpFileBuffer + mnOffset); + mnOffset += psp::appendStr ("\n", mpFileBuffer); mnColumn = 0; } if (mnOffset >= nBufferSize) @@ -115,7 +115,7 @@ HexEncoder::FlushLine () { if (mnOffset > 0) { - WritePS (mpFile, mpFileBuffer, mnOffset); + WritePS (mpFile, mpFileBuffer.makeStringAndClear()); mnOffset = 0; } } @@ -133,7 +133,7 @@ private: sal_uInt32 mnColumn; sal_uInt32 mnOffset; - sal_Char mpFileBuffer[nBufferSize + 16]; + OStringBuffer mpFileBuffer; inline void PutByte (sal_uInt8 nByte); inline void PutEOD (); @@ -182,7 +182,7 @@ Ascii85Encoder::ConvertToAscii85 () if (nByteValue == 0 && mnByte == 4) { /* special case of 4 Bytes in row */ - mpFileBuffer [mnOffset] = 'z'; + mpFileBuffer.append('z'); mnOffset += 1; mnColumn += 1; @@ -194,21 +194,22 @@ Ascii85Encoder::ConvertToAscii85 () // Of the up to 5 characters to be generated, do not generate the last (4 - mnByte) ones // that correspond to the (4 - mnByte) zero padding bytes added to the input: + auto const pos = mpFileBuffer.getLength(); if (mnByte == 4) { - mpFileBuffer [mnOffset + 4] = (nByteValue % 85) + 33; + mpFileBuffer.insert(pos, char((nByteValue % 85) + 33)); } nByteValue /= 85; if (mnByte >= 3) { - mpFileBuffer [mnOffset + 3] = (nByteValue % 85) + 33; + mpFileBuffer.insert(pos, char((nByteValue % 85) + 33)); } nByteValue /= 85; if (mnByte >= 2) { - mpFileBuffer [mnOffset + 2] = (nByteValue % 85) + 33; + mpFileBuffer.insert(pos, char((nByteValue % 85) + 33)); } nByteValue /= 85; - mpFileBuffer [mnOffset + 1] = (nByteValue % 85) + 33; + mpFileBuffer.insert(pos, char((nByteValue % 85) + 33)); nByteValue /= 85; - mpFileBuffer [mnOffset + 0] = (nByteValue % 85) + 33; + mpFileBuffer.insert(pos, char((nByteValue % 85) + 33)); mnColumn += (mnByte + 1); mnOffset += (mnByte + 1); @@ -217,10 +218,9 @@ Ascii85Encoder::ConvertToAscii85 () if (mnColumn > nLineLength) { sal_uInt32 nEolOff = mnColumn - nLineLength; - sal_uInt32 nBufOff = mnOffset - nEolOff; + auto const posNl = pos + (mnByte + 1) - nEolOff; - std::memmove (mpFileBuffer + nBufOff + 1, mpFileBuffer + nBufOff, nEolOff); - mpFileBuffer[ nBufOff ] = '\n'; + mpFileBuffer.insert(posNl, '\n'); mnOffset++; mnColumn = nEolOff; @@ -239,7 +239,7 @@ Ascii85Encoder::WriteAscii (sal_uInt8 nByte) if (mnColumn >= nLineLength) { - mnOffset += psp::appendStr ("\n", mpFileBuffer + mnOffset); + mnOffset += psp::appendStr ("\n", mpFileBuffer); mnColumn = 0; } if (mnOffset >= nBufferSize) @@ -257,7 +257,7 @@ Ascii85Encoder::FlushLine () { if (mnOffset > 0) { - WritePS (mpFile, mpFileBuffer, mnOffset); + WritePS (mpFile, mpFileBuffer.makeStringAndClear()); mnOffset = 0; } } @@ -476,23 +476,22 @@ PrinterGfx::DrawPS1GrayImage (const PrinterBmp& rBitmap, const tools::Rectangle& sal_uInt32 nWidth = rArea.GetWidth(); sal_uInt32 nHeight = rArea.GetHeight(); - sal_Char pGrayImage [512]; - sal_Int32 nChar = 0; + OStringBuffer pGrayImage; // image header - nChar += psp::getValueOf (nWidth, pGrayImage + nChar); - nChar += psp::appendStr (" ", pGrayImage + nChar); - nChar += psp::getValueOf (nHeight, pGrayImage + nChar); - nChar += psp::appendStr (" 8 ", pGrayImage + nChar); - nChar += psp::appendStr ("[ 1 0 0 1 0 ", pGrayImage + nChar); - nChar += psp::getValueOf (nHeight, pGrayImage + nChar); - nChar += psp::appendStr ("]", pGrayImage + nChar); - nChar += psp::appendStr (" {currentfile ", pGrayImage + nChar); - nChar += psp::getValueOf (nWidth, pGrayImage + nChar); - nChar += psp::appendStr (" string readhexstring pop}\n", pGrayImage + nChar); - nChar += psp::appendStr ("image\n", pGrayImage + nChar); - - WritePS (mpPageBody, pGrayImage, nChar); + psp::getValueOf (nWidth, pGrayImage); + psp::appendStr (" ", pGrayImage); + psp::getValueOf (nHeight, pGrayImage); + psp::appendStr (" 8 ", pGrayImage); + psp::appendStr ("[ 1 0 0 1 0 ", pGrayImage); + psp::getValueOf (nHeight, pGrayImage); + psp::appendStr ("]", pGrayImage); + psp::appendStr (" {currentfile ", pGrayImage); + psp::getValueOf (nWidth, pGrayImage); + psp::appendStr (" string readhexstring pop}\n", pGrayImage); + psp::appendStr ("image\n", pGrayImage); + + WritePS (mpPageBody, pGrayImage.makeStringAndClear()); // image body std::unique_ptr<HexEncoder> xEncoder(new HexEncoder (mpPageBody)); @@ -520,8 +519,7 @@ PrinterGfx::DrawPS1GrayImage (const PrinterBmp& rBitmap, const tools::Rectangle& void PrinterGfx::writePS2ImageHeader (const tools::Rectangle& rArea, psp::ImageType nType) { - sal_Int32 nChar = 0; - sal_Char pImage [512]; + OStringBuffer pImage; sal_Int32 nDictType = 0; switch (nType) @@ -533,16 +531,16 @@ PrinterGfx::writePS2ImageHeader (const tools::Rectangle& rArea, psp::ImageType n default: break; } - nChar += psp::getValueOf (rArea.GetWidth(), pImage + nChar); - nChar += psp::appendStr (" ", pImage + nChar); - nChar += psp::getValueOf (rArea.GetHeight(), pImage + nChar); - nChar += psp::appendStr (" ", pImage + nChar); - nChar += psp::getValueOf (nDictType, pImage + nChar); - nChar += psp::appendStr (" ", pImage + nChar); - nChar += psp::getValueOf (sal_Int32(1), pImage + nChar); // nCompressType - nChar += psp::appendStr (" psp_imagedict image\n", pImage + nChar); + psp::getValueOf (rArea.GetWidth(), pImage); + psp::appendStr (" ", pImage); + psp::getValueOf (rArea.GetHeight(), pImage); + psp::appendStr (" ", pImage); + psp::getValueOf (nDictType, pImage); + psp::appendStr (" ", pImage); + psp::getValueOf (sal_Int32(1), pImage); // nCompressType + psp::appendStr (" psp_imagedict image\n", pImage); - WritePS (mpPageBody, pImage, nChar); + WritePS (mpPageBody, pImage.makeStringAndClear()); } void @@ -564,15 +562,14 @@ PrinterGfx::writePS2Colorspace(const PrinterBmp& rBitmap, psp::ImageType nType) case psp::ImageType::PaletteImage: { - sal_Int32 nChar = 0; - sal_Char pImage [4096]; + OStringBuffer pImage; const sal_uInt32 nSize = rBitmap.GetPaletteEntryCount(); - nChar += psp::appendStr ("[/Indexed /DeviceRGB ", pImage + nChar); - nChar += psp::getValueOf (nSize - 1, pImage + nChar); - nChar += psp::appendStr ("\npsp_lzwstring\n", pImage + nChar); - WritePS (mpPageBody, pImage, nChar); + psp::appendStr ("[/Indexed /DeviceRGB ", pImage); + psp::getValueOf (nSize - 1, pImage); + psp::appendStr ("\npsp_lzwstring\n", pImage); + WritePS (mpPageBody, pImage.makeStringAndClear()); std::unique_ptr<ByteEncoder> xEncoder(new LZWEncoder(mpPageBody)); for (sal_uInt32 i = 0; i < nSize; i++) diff --git a/vcl/unx/generic/print/common_gfx.cxx b/vcl/unx/generic/print/common_gfx.cxx index a4fc227ad74f..a92d366ccd1d 100644 --- a/vcl/unx/generic/print/common_gfx.cxx +++ b/vcl/unx/generic/print/common_gfx.cxx @@ -300,23 +300,23 @@ PrinterGfx::EndSetClipRegion() void PrinterGfx::DrawRect (const tools::Rectangle& rRectangle ) { - char pRect [128]; - sal_Int32 nChar = 0; - - nChar = psp::getValueOf (rRectangle.TopLeft().X(), pRect); - nChar += psp::appendStr (" ", pRect + nChar); - nChar += psp::getValueOf (rRectangle.TopLeft().Y(), pRect + nChar); - nChar += psp::appendStr (" ", pRect + nChar); - nChar += psp::getValueOf (rRectangle.GetWidth(), pRect + nChar); - nChar += psp::appendStr (" ", pRect + nChar); - nChar += psp::getValueOf (rRectangle.GetHeight(), pRect + nChar); - nChar += psp::appendStr (" ", pRect + nChar); + OStringBuffer pRect; + + psp::getValueOf (rRectangle.TopLeft().X(), pRect); + psp::appendStr (" ", pRect); + psp::getValueOf (rRectangle.TopLeft().Y(), pRect); + psp::appendStr (" ", pRect); + psp::getValueOf (rRectangle.GetWidth(), pRect); + psp::appendStr (" ", pRect); + psp::getValueOf (rRectangle.GetHeight(), pRect); + psp::appendStr (" ", pRect); + auto const rect = pRect.makeStringAndClear(); if( maFillColor.Is() ) { PSSetColor (maFillColor); PSSetColor (); - WritePS (mpPageBody, pRect, nChar); + WritePS (mpPageBody, rect); WritePS (mpPageBody, "rectfill\n"); } if( maLineColor.Is() ) @@ -324,7 +324,7 @@ PrinterGfx::DrawRect (const tools::Rectangle& rRectangle ) PSSetColor (maLineColor); PSSetColor (); PSSetLineWidth (); - WritePS (mpPageBody, pRect, nChar); + WritePS (mpPageBody, rect); WritePS (mpPageBody, "rectstroke\n"); } } @@ -672,13 +672,12 @@ PrinterGfx::PSSetLineWidth () { if( currentState().mfLineWidth != maVirtualStatus.mfLineWidth ) { - char pBuffer[128]; - sal_Int32 nChar = 0; + OStringBuffer pBuffer; currentState().mfLineWidth = maVirtualStatus.mfLineWidth; - nChar = psp::getValueOfDouble (pBuffer, maVirtualStatus.mfLineWidth, 5); - nChar += psp::appendStr (" setlinewidth\n", pBuffer + nChar); - WritePS (mpPageBody, pBuffer, nChar); + psp::getValueOfDouble (pBuffer, maVirtualStatus.mfLineWidth, 5); + psp::appendStr (" setlinewidth\n", pBuffer); + WritePS (mpPageBody, pBuffer.makeStringAndClear()); } } @@ -691,30 +690,29 @@ PrinterGfx::PSSetColor () { currentState().maColor = rColor; - char pBuffer[128]; - sal_Int32 nChar = 0; + OStringBuffer pBuffer; if( mbColor ) { - nChar = psp::getValueOfDouble (pBuffer, + psp::getValueOfDouble (pBuffer, static_cast<double>(rColor.GetRed()) / 255.0, 5); - nChar += psp::appendStr (" ", pBuffer + nChar); - nChar += psp::getValueOfDouble (pBuffer + nChar, + psp::appendStr (" ", pBuffer); + psp::getValueOfDouble (pBuffer, static_cast<double>(rColor.GetGreen()) / 255.0, 5); - nChar += psp::appendStr (" ", pBuffer + nChar); - nChar += psp::getValueOfDouble (pBuffer + nChar, + psp::appendStr (" ", pBuffer); + psp::getValueOfDouble (pBuffer, static_cast<double>(rColor.GetBlue()) / 255.0, 5); - nChar += psp::appendStr (" setrgbcolor\n", pBuffer + nChar ); + psp::appendStr (" setrgbcolor\n", pBuffer ); } else { Color aColor( rColor.GetRed(), rColor.GetGreen(), rColor.GetBlue() ); sal_uInt8 nCol = aColor.GetLuminance(); - nChar = psp::getValueOfDouble( pBuffer, static_cast<double>(nCol) / 255.0, 5 ); - nChar += psp::appendStr( " setgray\n", pBuffer + nChar ); + psp::getValueOfDouble( pBuffer, static_cast<double>(nCol) / 255.0, 5 ); + psp::appendStr( " setgray\n", pBuffer ); } - WritePS (mpPageBody, pBuffer, nChar); + WritePS (mpPageBody, pBuffer.makeStringAndClear()); } } @@ -742,8 +740,7 @@ PrinterGfx::PSSetFont () sal_Int32 nTextWidth = rCurrent.mnTextWidth ? rCurrent.mnTextWidth : rCurrent.mnTextHeight; - sal_Char pSetFont [256]; - sal_Int32 nChar = 0; + OStringBuffer pSetFont; // postscript based fonts need reencoding if ( ( rCurrent.maEncoding == RTL_TEXTENCODING_MS_1252) @@ -756,43 +753,43 @@ PrinterGfx::PSSetFont () psp::GlyphSet::GetReencodedFontName (rCurrent.maEncoding, rCurrent.maFont); - nChar += psp::appendStr ("(", pSetFont + nChar); - nChar += psp::appendStr (aReencodedFont.getStr(), - pSetFont + nChar); - nChar += psp::appendStr (") cvn findfont ", - pSetFont + nChar); + psp::appendStr ("(", pSetFont); + psp::appendStr (aReencodedFont.getStr(), + pSetFont); + psp::appendStr (") cvn findfont ", + pSetFont); } else // tt based fonts mustn't reencode, the encoding is implied by the fontname // same for symbol type1 fonts, don't try to touch them { - nChar += psp::appendStr ("(", pSetFont + nChar); - nChar += psp::appendStr (rCurrent.maFont.getStr(), - pSetFont + nChar); - nChar += psp::appendStr (") cvn findfont ", - pSetFont + nChar); + psp::appendStr ("(", pSetFont); + psp::appendStr (rCurrent.maFont.getStr(), + pSetFont); + psp::appendStr (") cvn findfont ", + pSetFont); } if( ! rCurrent.mbArtItalic ) { - nChar += psp::getValueOf (nTextWidth, pSetFont + nChar); - nChar += psp::appendStr (" ", pSetFont + nChar); - nChar += psp::getValueOf (-nTextHeight, pSetFont + nChar); - nChar += psp::appendStr (" matrix scale makefont setfont\n", pSetFont + nChar); + psp::getValueOf (nTextWidth, pSetFont); + psp::appendStr (" ", pSetFont); + psp::getValueOf (-nTextHeight, pSetFont); + psp::appendStr (" matrix scale makefont setfont\n", pSetFont); } else // skew 15 degrees to right { - nChar += psp::appendStr ( " [", pSetFont + nChar); - nChar += psp::getValueOf (nTextWidth, pSetFont + nChar); - nChar += psp::appendStr (" 0 ", pSetFont + nChar); - nChar += psp::getValueOfDouble (pSetFont + nChar, 0.27*static_cast<double>(nTextWidth), 3 ); - nChar += psp::appendStr ( " ", pSetFont + nChar); - nChar += psp::getValueOf (-nTextHeight, pSetFont + nChar); - - nChar += psp::appendStr (" 0 0] makefont setfont\n", pSetFont + nChar); + psp::appendStr ( " [", pSetFont); + psp::getValueOf (nTextWidth, pSetFont); + psp::appendStr (" 0 ", pSetFont); + psp::getValueOfDouble (pSetFont, 0.27*static_cast<double>(nTextWidth), 3 ); + psp::appendStr ( " ", pSetFont); + psp::getValueOf (-nTextHeight, pSetFont); + + psp::appendStr (" 0 0] makefont setfont\n", pSetFont); } - WritePS (mpPageBody, pSetFont, nChar); + WritePS (mpPageBody, pSetFont.makeStringAndClear()); } @@ -809,33 +806,29 @@ PrinterGfx::PSRotate (sal_Int32 nAngle) sal_Int32 nFullAngle = nPostScriptAngle / 10; sal_Int32 nTenthAngle = nPostScriptAngle % 10; - sal_Char pRotate [48]; - sal_Int32 nChar = 0; + OStringBuffer pRotate; - nChar = psp::getValueOf (nFullAngle, pRotate); - nChar += psp::appendStr (".", pRotate + nChar); - nChar += psp::getValueOf (nTenthAngle, pRotate + nChar); - nChar += psp::appendStr (" rotate\n", pRotate + nChar); + psp::getValueOf (nFullAngle, pRotate); + psp::appendStr (".", pRotate); + psp::getValueOf (nTenthAngle, pRotate); + psp::appendStr (" rotate\n", pRotate); - WritePS (mpPageBody, pRotate, nChar); + WritePS (mpPageBody, pRotate.makeStringAndClear()); } void PrinterGfx::PSPointOp (const Point& rPoint, const sal_Char* pOperator) { - sal_Char pPSCommand [48]; - sal_Int32 nChar = 0; - - nChar = psp::getValueOf (rPoint.X(), pPSCommand); - nChar += psp::appendStr (" ", pPSCommand + nChar); - nChar += psp::getValueOf (rPoint.Y(), pPSCommand + nChar); - nChar += psp::appendStr (" ", pPSCommand + nChar); - nChar += psp::appendStr (pOperator, pPSCommand + nChar); - nChar += psp::appendStr ("\n", pPSCommand + nChar); + OStringBuffer pPSCommand; - DBG_ASSERT (nChar < 48, "Buffer overflow in PSPointOp"); + psp::getValueOf (rPoint.X(), pPSCommand); + psp::appendStr (" ", pPSCommand); + psp::getValueOf (rPoint.Y(), pPSCommand); + psp::appendStr (" ", pPSCommand); + psp::appendStr (pOperator, pPSCommand); + psp::appendStr ("\n", pPSCommand); - WritePS (mpPageBody, pPSCommand); + WritePS (mpPageBody, pPSCommand.makeStringAndClear()); } void @@ -918,14 +911,13 @@ void PrinterGfx::PSBinPath (const Point& rCurrent, Point& rOld, pspath_t eType, sal_Int32& nColumn) { - sal_Char pPath[48] = {0}; + OStringBuffer pPath; sal_Int32 nChar; // create the hex representation of the dx and dy path shift, store the field // width as it is needed for the building the command - sal_Int32 nXPrec = getAlignedHexValueOf (rCurrent.X() - rOld.X(), pPath + 1); - sal_Int32 nYPrec = getAlignedHexValueOf (rCurrent.Y() - rOld.Y(), pPath + 1 + nXPrec); - pPath [ 1 + nXPrec + nYPrec ] = 0; + sal_Int32 nXPrec = getAlignedHexValueOf (rCurrent.X() - rOld.X(), pPath); + sal_Int32 nYPrec = getAlignedHexValueOf (rCurrent.Y() - rOld.Y(), pPath); // build the command, it is a char with bit represention 000cxxyy // c represents the char, xx and yy repr. the field width of the dx and dy shift, @@ -948,7 +940,8 @@ PrinterGfx::PSBinPath (const Point& rCurrent, Point& rOld, default: OSL_FAIL("invalid y precision in binary path"); } cCmd += 'A'; - pPath[0] = cCmd; + pPath.insert(0, cCmd); + auto const path = pPath.makeStringAndClear(); // write the command to file, // line breaking at column nMaxTextColumn (80) @@ -957,15 +950,15 @@ PrinterGfx::PSBinPath (const Point& rCurrent, Point& rOld, { sal_Int32 nSegment = nMaxTextColumn - nColumn; - WritePS (mpPageBody, pPath, nSegment); + WritePS (mpPageBody, path.copy(0, nSegment)); WritePS (mpPageBody, "\n", 1); - WritePS (mpPageBody, pPath + nSegment, nChar - nSegment); + WritePS (mpPageBody, path.copy(nSegment)); nColumn = nChar - nSegment; } else { - WritePS (mpPageBody, pPath, nChar); + WritePS (mpPageBody, path); nColumn += nChar; } @@ -976,22 +969,21 @@ PrinterGfx::PSBinPath (const Point& rCurrent, Point& rOld, void PrinterGfx::PSScale (double fScaleX, double fScaleY) { - sal_Char pScale [48]; - sal_Int32 nChar = 0; + OStringBuffer pScale; - nChar = psp::getValueOfDouble (pScale, fScaleX, 5); - nChar += psp::appendStr (" ", pScale + nChar); - nChar += psp::getValueOfDouble (pScale + nChar, fScaleY, 5); - nChar += psp::appendStr (" scale\n", pScale + nChar); + psp::getValueOfDouble (pScale, fScaleX, 5); + psp::appendStr (" ", pScale); + psp::getValueOfDouble (pScale, fScaleY, 5); + psp::appendStr (" scale\n", pScale); - WritePS (mpPageBody, pScale, nChar); + WritePS (mpPageBody, pScale.makeStringAndClear()); } /* psshowtext helper routines: draw an hex string for show/xshow */ void PrinterGfx::PSHexString (const unsigned char* pString, sal_Int16 nLen) { - sal_Char pHexString [128]; + OStringBuffer pHexString; sal_Int32 nChar = 0; nChar = psp::appendStr ("<", pHexString); @@ -999,15 +991,15 @@ PrinterGfx::PSHexString (const unsigned char* pString, sal_Int16 nLen) { if (nChar >= (nMaxTextColumn - 1)) { - nChar += psp::appendStr ("\n", pHexString + nChar); - WritePS (mpPageBody, pHexString, nChar); + nChar += psp::appendStr ("\n", pHexString); + WritePS (mpPageBody, pHexString.makeStringAndClear()); nChar = 0; } - nChar += psp::getHexValueOf (static_cast<sal_Int32>(pString[i]), pHexString + nChar); + nChar += psp::getHexValueOf (static_cast<sal_Int32>(pString[i]), pHexString); } - nChar += psp::appendStr (">\n", pHexString + nChar); - WritePS (mpPageBody, pHexString, nChar); + nChar += psp::appendStr (">\n", pHexString); + WritePS (mpPageBody, pHexString.makeStringAndClear()); } void diff --git a/vcl/unx/generic/print/printerjob.cxx b/vcl/unx/generic/print/printerjob.cxx index ba68913619f9..7cf15556476e 100644 --- a/vcl/unx/generic/print/printerjob.cxx +++ b/vcl/unx/generic/print/printerjob.cxx @@ -362,12 +362,11 @@ PrinterJob::StartJob ( } // Language Level - sal_Char pLevel[16]; - sal_Int32 nSz = getValueOf(GetPostscriptLevel(&rSetupData), pLevel); - pLevel[nSz++] = '\n'; - pLevel[nSz ] = '\0'; + OStringBuffer pLevel; + getValueOf(GetPostscriptLevel(&rSetupData), pLevel); + pLevel.append('\n'); WritePS (mpJobHeader.get(), "%%LanguageLevel: "); - WritePS (mpJobHeader.get(), pLevel); + WritePS (mpJobHeader.get(), pLevel.makeStringAndClear()); // Other WritePS (mpJobHeader.get(), "%%DocumentData: Clean7Bit\n"); @@ -587,20 +586,19 @@ PrinterJob::StartPage (const JobData& rJobSetup) mnPortraits++; } - sal_Char pBBox [256]; - sal_Int32 nChar = 0; + OStringBuffer pBBox; - nChar = psp::appendStr ("%%PageBoundingBox: ", pBBox); - nChar += psp::getValueOf (mnLMarginPt, pBBox + nChar); - nChar += psp::appendStr (" ", pBBox + nChar); - nChar += psp::getValueOf (mnBMarginPt, pBBox + nChar); - nChar += psp::appendStr (" ", pBBox + nChar); - nChar += psp::getValueOf (mnWidthPt - mnRMarginPt, pBBox + nChar); - nChar += psp::appendStr (" ", pBBox + nChar); - nChar += psp::getValueOf (mnHeightPt - mnTMarginPt, pBBox + nChar); - nChar += psp::appendStr ("\n", pBBox + nChar); + psp::appendStr ("%%PageBoundingBox: ", pBBox); + psp::getValueOf (mnLMarginPt, pBBox); + psp::appendStr (" ", pBBox); + psp::getValueOf (mnBMarginPt, pBBox); + psp::appendStr (" ", pBBox); + psp::getValueOf (mnWidthPt - mnRMarginPt, pBBox); + psp::appendStr (" ", pBBox); + psp::getValueOf (mnHeightPt - mnTMarginPt, pBBox); + psp::appendStr ("\n", pBBox); - WritePS (pPageHeader, pBBox, nChar); + WritePS (pPageHeader, pBBox.makeStringAndClear()); /* #i7262# #i65491# write setup only before first page * (to %%Begin(End)Setup, instead of %%Begin(End)PageSetup) @@ -631,12 +629,11 @@ PrinterJob::EndPage () // copy page to paper and write page trailer according to DSC - sal_Char pTrailer[256]; - sal_Int32 nChar = 0; - nChar = psp::appendStr ("grestore grestore\n", pTrailer); - nChar += psp::appendStr ("showpage\n", pTrailer + nChar); - nChar += psp::appendStr ("%%PageTrailer\n\n", pTrailer + nChar); - WritePS (pPageBody, pTrailer, nChar); + OStringBuffer pTrailer; + psp::appendStr ("grestore grestore\n", pTrailer); + psp::appendStr ("showpage\n", pTrailer); + psp::appendStr ("%%PageTrailer\n\n", pTrailer); + WritePS (pPageBody, pTrailer.makeStringAndClear()); // this page is done for now, close it to avoid having too many open fd's @@ -750,39 +747,38 @@ bool PrinterJob::writePageSetup( osl::File* pFile, const JobData& rJob, bool bWr bSuccess = writeFeatureList( pFile, rJob, false ); WritePS (pFile, "%%EndPageSetup\n"); - sal_Char pTranslate [128]; - sal_Int32 nChar = 0; + OStringBuffer pTranslate; if( rJob.m_eOrientation == orientation::Portrait ) { - nChar = psp::appendStr ("gsave\n[", pTranslate); - nChar += psp::getValueOfDouble ( pTranslate + nChar, mfXScale, 5); - nChar += psp::appendStr (" 0 0 ", pTranslate + nChar); - nChar += psp::getValueOfDouble ( pTranslate + nChar, mfYScale, 5); - nChar += psp::appendStr (" ", pTranslate + nChar); - nChar += psp::getValueOf (mnRMarginPt, pTranslate + nChar); - nChar += psp::appendStr (" ", pTranslate + nChar); - nChar += psp::getValueOf (mnHeightPt-mnTMarginPt, - pTranslate + nChar); - nChar += psp::appendStr ("] concat\ngsave\n", - pTranslate + nChar); + psp::appendStr ("gsave\n[", pTranslate); + psp::getValueOfDouble ( pTranslate, mfXScale, 5); + psp::appendStr (" 0 0 ", pTranslate); + psp::getValueOfDouble ( pTranslate, mfYScale, 5); + psp::appendStr (" ", pTranslate); + psp::getValueOf (mnRMarginPt, pTranslate); + psp::appendStr (" ", pTranslate); + psp::getValueOf (mnHeightPt-mnTMarginPt, + pTranslate); + psp::appendStr ("] concat\ngsave\n", + pTranslate); } else { - nChar = psp::appendStr ("gsave\n", pTranslate); - nChar += psp::appendStr ("[ 0 ", pTranslate + nChar); - nChar += psp::getValueOfDouble ( pTranslate + nChar, -mfYScale, 5); - nChar += psp::appendStr (" ", pTranslate + nChar); - nChar += psp::getValueOfDouble ( pTranslate + nChar, mfXScale, 5); - nChar += psp::appendStr (" 0 ", pTranslate + nChar ); - nChar += psp::getValueOfDouble ( pTranslate + nChar, mnLMarginPt, 5 ); - nChar += psp::appendStr (" ", pTranslate + nChar); - nChar += psp::getValueOf (mnBMarginPt, pTranslate + nChar ); - nChar += psp::appendStr ("] concat\ngsave\n", - pTranslate + nChar); + psp::appendStr ("gsave\n", pTranslate); + psp::appendStr ("[ 0 ", pTranslate); + psp::getValueOfDouble ( pTranslate, -mfYScale, 5); + psp::appendStr (" ", pTranslate); + psp::getValueOfDouble ( pTranslate, mfXScale, 5); + psp::appendStr (" 0 ", pTranslate ); + psp::getValueOfDouble ( pTranslate, mnLMarginPt, 5 ); + psp::appendStr (" ", pTranslate); + psp::getValueOf (mnBMarginPt, pTranslate ); + psp::appendStr ("] concat\ngsave\n", + pTranslate); } - WritePS (pFile, pTranslate, nChar); + WritePS (pFile, pTranslate.makeStringAndClear()); return bSuccess; } diff --git a/vcl/unx/generic/print/psputil.cxx b/vcl/unx/generic/print/psputil.cxx index 0026ccfac43f..4658865ed028 100644 --- a/vcl/unx/generic/print/psputil.cxx +++ b/vcl/unx/generic/print/psputil.cxx @@ -28,20 +28,20 @@ namespace psp { */ sal_Int32 -getHexValueOf (sal_Int32 nValue, sal_Char* pBuffer) +getHexValueOf (sal_Int32 nValue, OStringBuffer& pBuffer) { const static sal_Char pHex [0x10] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; - pBuffer[0] = pHex [(nValue & 0xF0) >> 4]; - pBuffer[1] = pHex [(nValue & 0x0F) ]; + pBuffer.append(pHex [(nValue & 0xF0) >> 4]); + pBuffer.append(pHex [(nValue & 0x0F) ]); return 2; } sal_Int32 -getAlignedHexValueOf (sal_Int32 nValue, sal_Char* pBuffer) +getAlignedHexValueOf (sal_Int32 nValue, OStringBuffer& pBuffer) { // get sign bool bNegative = nValue < 0; @@ -62,25 +62,28 @@ getAlignedHexValueOf (sal_Int32 nValue, sal_Char* pBuffer) // convert the int into its hex representation, write it into the buffer sal_Int32 nRet = nPrecision; + auto const start = pBuffer.getLength(); while (nPrecision) { - nPrecision -= getHexValueOf (nValue % 256, pBuffer + nPrecision - 2 ); + OStringBuffer scratch; + nPrecision -= getHexValueOf (nValue % 256, scratch ); + pBuffer.insert(start, scratch.makeStringAndClear()); nValue /= 256; } // set sign bit if (bNegative) { - switch (pBuffer[0]) + switch (pBuffer[start]) { - case '0' : pBuffer[0] = '8'; break; - case '1' : pBuffer[0] = '9'; break; - case '2' : pBuffer[0] = 'A'; break; - case '3' : pBuffer[0] = 'B'; break; - case '4' : pBuffer[0] = 'C'; break; - case '5' : pBuffer[0] = 'D'; break; - case '6' : pBuffer[0] = 'E'; break; - case '7' : pBuffer[0] = 'F'; break; + case '0' : pBuffer[start] = '8'; break; + case '1' : pBuffer[start] = '9'; break; + case '2' : pBuffer[start] = 'A'; break; + case '3' : pBuffer[start] = 'B'; break; + case '4' : pBuffer[start] = 'C'; break; + case '5' : pBuffer[start] = 'D'; break; + case '6' : pBuffer[start] = 'E'; break; + case '7' : pBuffer[start] = 'F'; break; default: OSL_FAIL("Already a signed value"); } } @@ -90,18 +93,20 @@ getAlignedHexValueOf (sal_Int32 nValue, sal_Char* pBuffer) } sal_Int32 -getValueOf (sal_Int32 nValue, sal_Char* pBuffer) +getValueOf (sal_Int32 nValue, OStringBuffer& pBuffer) { sal_Int32 nChar = 0; if (nValue < 0) { - pBuffer [nChar++] = '-'; + pBuffer.append('-'); + ++nChar; nValue *= -1; } else if (nValue == 0) { - pBuffer [nChar++] = '0'; + pBuffer.append('0'); + ++nChar; return nChar; } @@ -114,17 +119,18 @@ getValueOf (sal_Int32 nValue, sal_Char* pBuffer) } while (nInvChar > 0) { - pBuffer [nChar++] = pInvBuffer [--nInvChar]; + pBuffer.append(pInvBuffer [--nInvChar]); + ++nChar; } return nChar; } sal_Int32 -appendStr (const sal_Char* pSrc, sal_Char* pDst) +appendStr (const sal_Char* pSrc, OStringBuffer& pDst) { sal_Int32 nBytes = strlen (pSrc); - strncpy (pDst, pSrc, nBytes + 1); + pDst.append(pSrc, nBytes); return nBytes; } diff --git a/vcl/unx/generic/print/psputil.hxx b/vcl/unx/generic/print/psputil.hxx index 1e58820bee16..ae3d78a730de 100644 --- a/vcl/unx/generic/print/psputil.hxx +++ b/vcl/unx/generic/print/psputil.hxx @@ -22,7 +22,9 @@ #include <osl/file.hxx> +#include <rtl/math.hxx> #include <rtl/ustring.hxx> +#include <rtl/strbuf.hxx> #include <rtl/string.hxx> #include <rtl/tencinfo.h> #include <rtl/textcvt.h> @@ -33,13 +35,16 @@ namespace psp { /* * string convenience routines - * sizeof(pBuffer) must be at least 2 Bytes, 0x00 <= nValue <= 0xFF, - * effective buffer of get*ValueOf() is NOT NULL-terminated */ -sal_Int32 getHexValueOf (sal_Int32 nValue, sal_Char* pBuffer); -sal_Int32 getAlignedHexValueOf (sal_Int32 nValue, sal_Char* pBuffer); -sal_Int32 getValueOf (sal_Int32 nValue, sal_Char* pBuffer); -sal_Int32 appendStr (const sal_Char* pSrc, sal_Char* pDst); +sal_Int32 getHexValueOf (sal_Int32 nValue, OStringBuffer& pBuffer); +sal_Int32 getAlignedHexValueOf (sal_Int32 nValue, OStringBuffer& pBuffer); +sal_Int32 getValueOf (sal_Int32 nValue, OStringBuffer& pBuffer); +sal_Int32 appendStr (const sal_Char* pSrc, OStringBuffer& pDst); + +inline void getValueOfDouble( OStringBuffer& pBuffer, double f, int nPrecision = 0) +{ + pBuffer.append(rtl::math::doubleToString( f, rtl_math_StringFormat_G, nPrecision, '.', true )); +} bool WritePS (osl::File* pFile, const sal_Char* pString); bool WritePS (osl::File* pFile, const sal_Char* pString, sal_uInt64 nInLength); |