33#include <string.h>
44#include <stdlib.h>
55#include "gd.h"
6+ #include "gd_intern.h"
67#include "gdhelpers.h"
78#include "gd_errors.h"
89
@@ -2307,53 +2308,16 @@ void gdImageFilledRectangle (gdImagePtr im, int x1, int y1, int x2, int y2, int
23072308 _gdImageFilledVRectangle (im , x1 , y1 , x2 , y2 , color );
23082309}
23092310
2310- static int _gdValidateCopyRectBounds (
2311- const gdImagePtr dst ,
2312- const gdImagePtr src ,
2313- int dstX , int dstY ,
2314- int srcX , int srcY ,
2315- int w , int h
2316- ) {
2317- /* Check for null pointers */
2318- if (!dst || !src ) {
2319- return 0 ;
2320- }
2321-
2322- /* Check for overflow in dstX + w */
2323- if (w > 0 && dstX > INT_MAX - w ) {
2324- return 0 ;
2325- }
2326-
2327- /* Check for overflow in dstY + h */
2328- if (h > 0 && dstY > INT_MAX - h ) {
2329- return 0 ;
2330- }
2331-
2332- /* Check for overflow in srcX + w */
2333- if (w > 0 && srcX > INT_MAX - w ) {
2334- return 0 ;
2335- }
2336-
2337- /* Check for overflow in srcY + h */
2338- if (h > 0 && srcY > INT_MAX - h ) {
2339- return 0 ;
2340- }
2341-
2342- return 1 ;
2343- }
2344-
23452311void gdImageCopy (gdImagePtr dst , gdImagePtr src , int dstX , int dstY , int srcX , int srcY , int w , int h )
23462312{
2347- if (!_gdValidateCopyRectBounds (dst , src , dstX , dstY , srcX , srcY , w , h )) {
2348- return ;
2349- }
2350-
23512313 int c ;
23522314 int x , y ;
23532315 int tox , toy ;
23542316 int i ;
23552317 int colorMap [gdMaxColors ];
2356-
2318+ if (!gdImageClipCopy (dst , & dstX , & dstY , & srcX , & srcY , & w , & h )) {
2319+ return ;
2320+ }
23572321 if (dst -> trueColor ) {
23582322 /* 2.0: much easier when the destination is truecolor. */
23592323 /* 2.0.10: needs a transparent-index check that is still valid if
@@ -2429,14 +2393,13 @@ void gdImageCopy (gdImagePtr dst, gdImagePtr src, int dstX, int dstY, int srcX,
24292393 so it doesn't pay attention to the alpha channel. */
24302394void gdImageCopyMerge (gdImagePtr dst , gdImagePtr src , int dstX , int dstY , int srcX , int srcY , int w , int h , int pct )
24312395{
2432- if (!_gdValidateCopyRectBounds (dst , src , dstX , dstY , srcX , srcY , w , h )) {
2433- return ;
2434- }
2435-
24362396 int c , dc ;
24372397 int x , y ;
24382398 int tox , toy ;
24392399 int ncR , ncG , ncB ;
2400+ if (!gdImageClipCopy (dst , & dstX , & dstY , & srcX , & srcY , & w , & h )) {
2401+ return ;
2402+ }
24402403 toy = dstY ;
24412404
24422405 for (y = srcY ; y < (srcY + h ); y ++ ) {
@@ -2473,15 +2436,16 @@ void gdImageCopyMerge (gdImagePtr dst, gdImagePtr src, int dstX, int dstY, int s
24732436 so it doesn't pay attention to the alpha channel. */
24742437void gdImageCopyMergeGray (gdImagePtr dst , gdImagePtr src , int dstX , int dstY , int srcX , int srcY , int w , int h , int pct )
24752438{
2476- if (!_gdValidateCopyRectBounds (dst , src , dstX , dstY , srcX , srcY , w , h )) {
2477- return ;
2478- }
2479-
24802439 int c , dc ;
24812440 int x , y ;
24822441 int tox , toy ;
24832442 int ncR , ncG , ncB ;
24842443 float g ;
2444+
2445+ if (!gdImageClipCopy (dst , & dstX , & dstY , & srcX , & srcY , & w , & h )) {
2446+ return ;
2447+ }
2448+
24852449 toy = dstY ;
24862450
24872451 for (y = srcY ; (y < (srcY + h )); y ++ ) {
@@ -2531,14 +2495,6 @@ void gdImageCopyMergeGray (gdImagePtr dst, gdImagePtr src, int dstX, int dstY, i
25312495
25322496void gdImageCopyResized (gdImagePtr dst , gdImagePtr src , int dstX , int dstY , int srcX , int srcY , int dstW , int dstH , int srcW , int srcH )
25332497{
2534- if (!_gdValidateCopyRectBounds (dst , src , dstX , dstY , srcX , srcY , dstW , dstH )) {
2535- return ;
2536- }
2537-
2538- if (!_gdValidateCopyRectBounds (dst , src , dstX , dstY , srcX , srcY , srcW , srcH )) {
2539- return ;
2540- }
2541-
25422498 int c ;
25432499 int x , y ;
25442500 int tox , toy ;
@@ -2554,7 +2510,9 @@ void gdImageCopyResized (gdImagePtr dst, gdImagePtr src, int dstX, int dstY, int
25542510 if (overflow2 (sizeof (int ), srcH )) {
25552511 return ;
25562512 }
2557-
2513+ if (!gdImageClipCopyResized (dst , & dstX , & dstY , & dstW , & dstH , & srcX , & srcY , & srcW , & srcH )) {
2514+ return ;
2515+ }
25582516 stx = (int * ) gdMalloc (sizeof (int ) * srcW );
25592517 sty = (int * ) gdMalloc (sizeof (int ) * srcH );
25602518
@@ -2649,21 +2607,16 @@ void gdImageCopyResized (gdImagePtr dst, gdImagePtr src, int dstX, int dstY, int
26492607
26502608void gdImageCopyResampled (gdImagePtr dst , gdImagePtr src , int dstX , int dstY , int srcX , int srcY , int dstW , int dstH , int srcW , int srcH )
26512609{
2652- if (!_gdValidateCopyRectBounds (dst , src , dstX , dstY , srcX , srcY , dstW , dstH )) {
2653- return ;
2654- }
2655-
2656- if (!_gdValidateCopyRectBounds (dst , src , dstX , dstY , srcX , srcY , srcW , srcH )) {
2657- return ;
2658- }
2659-
26602610 int x , y ;
26612611 double sy1 , sy2 , sx1 , sx2 ;
26622612
26632613 if (!dst -> trueColor ) {
26642614 gdImageCopyResized (dst , src , dstX , dstY , srcX , srcY , dstW , dstH , srcW , srcH );
26652615 return ;
26662616 }
2617+ if (!gdImageClipCopyResized (dst , & dstX , & dstY , & dstW , & dstH , & srcX , & srcY , & srcW , & srcH )) {
2618+ return ;
2619+ }
26672620 for (y = dstY ; (y < dstY + dstH ); y ++ ) {
26682621 sy1 = ((double ) y - (double ) dstY ) * (double ) srcH / (double ) dstH ;
26692622 sy2 = ((double ) (y + 1 ) - (double ) dstY ) * (double ) srcH / (double ) dstH ;
0 commit comments