Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit6f85c88

Browse files
authored
Merge pull request#156 from ViliusSutkus89/background-image
Fix out of bounds read in SplashBackgroundRenderer
2 parents1fabed1 +3f0f523 commit6f85c88

File tree

2 files changed

+20
-94
lines changed

2 files changed

+20
-94
lines changed

‎pdf2htmlEX/src/BackgroundRenderer/SplashBackgroundRenderer.cc‎

Lines changed: 20 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,17 @@
55
*/
66

77
#include<fstream>
8-
#include<vector>
9-
#include<memory>
108

119
#include<poppler-config.h>
12-
#include<PDFDoc.h>
13-
#include<goo/ImgWriter.h>
14-
#include<goo/PNGWriter.h>
15-
#include<goo/JpegWriter.h>
10+
#include<splash/SplashErrorCodes.h>
1611

1712
#include"Base64Stream.h"
18-
#include"util/const.h"
19-
2013
#include"SplashBackgroundRenderer.h"
2114

2215
namespacepdf2htmlEX {
2316

2417
using std::string;
2518
using std::ifstream;
26-
using std::vector;
27-
using std::unique_ptr;
2819

2920
const SplashColor SplashBackgroundRenderer::white = {255,255,255};
3021

@@ -35,6 +26,7 @@ SplashBackgroundRenderer::SplashBackgroundRenderer(const string & imgFormat, HTM
3526
, format(imgFormat)
3627
{
3728
bool supported =false;
29+
// ENABLE_LIBPNG and ENABLE_LIBJPEG are defines coming in from poppler-config.h
3830
#ifdef ENABLE_LIBPNG
3931
if (format.empty())
4032
format ="png";
@@ -47,7 +39,7 @@ SplashBackgroundRenderer::SplashBackgroundRenderer(const string & imgFormat, HTM
4739
#endif
4840
if (!supported)
4941
{
50-
throwstring("Image format not supported:") + format;
42+
throwstring("Image format not supported by Poppler:") + format;
5143
}
5244
}
5345

@@ -124,27 +116,26 @@ bool SplashBackgroundRenderer::render_page(PDFDoc * doc, int pageno)
124116

125117
voidSplashBackgroundRenderer::embed_image(int pageno)
126118
{
127-
// xmin->xmax is top->bottom
128-
int xmin, xmax, ymin, ymax;
129-
// poppler-0.84.0 hack to recover from the removal of *ModRegion tracking
130-
//
131119
auto * bitmap =getBitmap();
132-
xmin =0;
133-
xmax = bitmap->getWidth();
134-
ymin =0;
135-
ymax = bitmap->getHeight();
136-
//
137-
// end of hack
138-
139120
// dump the background image only when it is not empty
140-
if((xmin <= xmax) &&(ymin <= ymax))
121+
if(bitmap->getWidth() >=0 &&bitmap->getHeight() >=0)
141122
{
142123
{
143124
auto fn = html_renderer->str_fmt("%s/bg%x.%s", (param.embed_image ? param.tmp_dir : param.dest_dir).c_str(), pageno, format.c_str());
144125
if(param.embed_image)
145-
html_renderer->tmp_files.add((char*)fn);
126+
html_renderer->tmp_files.add((constchar *)fn);
127+
128+
SplashImageFileFormat splashImageFileFormat;
129+
if(format =="png")
130+
splashImageFileFormat = splashFormatPng;
131+
elseif(format =="jpg")
132+
splashImageFileFormat = splashFormatJpeg;
133+
else
134+
throwstring("Image format not supported:") + format;
146135

147-
dump_image((char*)fn, xmin, ymin, xmax, ymax);
136+
SplashError e = bitmap->writeImgFile(splashImageFileFormat, (constchar *)fn, param.actual_dpi, param.actual_dpi);
137+
if (e != splashOk)
138+
throwstring("Cannot write background image. SplashErrorCode:") +std::to_string(e);
148139
}
149140

150141
double h_scale = html_renderer->text_zoom_factor() * DEFAULT_DPI / param.actual_dpi;
@@ -154,10 +145,10 @@ void SplashBackgroundRenderer::embed_image(int pageno)
154145
auto & all_manager = html_renderer->all_manager;
155146

156147
f_page <<"<img class=\"" << CSS::BACKGROUND_IMAGE_CN
157-
<<"" << CSS::LEFT_CN << all_manager.left.install(((double)xmin) * h_scale)
158-
<<"" << CSS::BOTTOM_CN << all_manager.bottom.install(((double)getBitmapHeight() -1 - ymax) * v_scale)
159-
<<"" << CSS::WIDTH_CN << all_manager.width.install(((double)(xmax - xmin +1)) * h_scale)
160-
<<"" << CSS::HEIGHT_CN << all_manager.height.install(((double)(ymax - ymin +1)) * v_scale)
148+
<<"" << CSS::LEFT_CN << all_manager.left.install(0.0L)
149+
<<"" << CSS::BOTTOM_CN << all_manager.bottom.install(0.0L)
150+
<<"" << CSS::WIDTH_CN << all_manager.width.install(h_scale * bitmap->getWidth())
151+
<<"" << CSS::HEIGHT_CN << all_manager.height.install(v_scale * bitmap->getHeight())
161152
<<"\" alt=\"\" src=\"";
162153

163154
if(param.embed_image)
@@ -182,68 +173,4 @@ void SplashBackgroundRenderer::embed_image(int pageno)
182173
}
183174
}
184175

185-
// There might be mem leak when exception is thrown !
186-
voidSplashBackgroundRenderer::dump_image(constchar * filename,int x1,int y1,int x2,int y2)
187-
{
188-
int width = x2 - x1 +1;
189-
int height = y2 - y1 +1;
190-
if((width <=0) || (height <=0))
191-
throw"Bad metric for background image";
192-
193-
FILE * f =fopen(filename,"wb");
194-
if(!f)
195-
throwstring("Cannot open file for background image" ) + filename;
196-
197-
// use unique_ptr to auto delete the object upon exception
198-
unique_ptr<ImgWriter> writer;
199-
200-
if(false) { }
201-
#ifdef ENABLE_LIBPNG
202-
elseif(format =="png")
203-
{
204-
writer = unique_ptr<ImgWriter>(new PNGWriter);
205-
}
206-
#endif
207-
#ifdef ENABLE_LIBJPEG
208-
elseif(format =="jpg")
209-
{
210-
writer = unique_ptr<ImgWriter>(new JpegWriter);
211-
}
212-
#endif
213-
else
214-
{
215-
throwstring("Image format not supported:") + format;
216-
}
217-
218-
if(!writer->init(f, width, height, param.actual_dpi, param.actual_dpi))
219-
throw"Cannot initialize image writer";
220-
221-
auto * bitmap =getBitmap();
222-
assert(bitmap->getMode() == splashModeRGB8);
223-
224-
SplashColorPtr data = bitmap->getDataPtr();
225-
int row_size = bitmap->getRowSize();
226-
227-
vector<unsignedchar*> pointers;
228-
pointers.reserve(height);
229-
SplashColorPtr p = data + y1 * row_size + x1 *3;
230-
for(int i =0; i < height; ++i)
231-
{
232-
pointers.push_back(p);
233-
p += row_size;
234-
}
235-
236-
if(!writer->writePointers(pointers.data(), height))
237-
{
238-
throw"Cannot write background image";
239-
}
240-
241-
if(!writer->close())
242-
{
243-
throw"Cannot finish background image";
244-
}
245-
246-
fclose(f);
247-
}
248-
249176
}// namespace pdf2htmlEX

‎pdf2htmlEX/src/BackgroundRenderer/SplashBackgroundRenderer.h‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ class SplashBackgroundRenderer : public BackgroundRenderer, SplashOutputDev
5353
voidupdateRender(GfxState *state);
5454

5555
protected:
56-
voiddump_image(constchar * filename,int x1,int y1,int x2,int y2);
5756
HTMLRenderer * html_renderer;
5857
const Param & param;
5958
std::string format;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp