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
@@ -2326,53 +2327,16 @@ void gdImageFilledRectangle (gdImagePtr im, int x1, int y1, int x2, int y2, int
23262327 _gdImageFilledVRectangle (im , x1 , y1 , x2 , y2 , color );
23272328}
23282329
2329- static int _gdValidateCopyRectBounds (
2330- const gdImagePtr dst ,
2331- const gdImagePtr src ,
2332- int dstX , int dstY ,
2333- int srcX , int srcY ,
2334- int w , int h
2335- ) {
2336- /* Check for null pointers */
2337- if (!dst || !src ) {
2338- return 0 ;
2339- }
2340-
2341- /* Check for overflow in dstX + w */
2342- if (w > 0 && dstX > INT_MAX - w ) {
2343- return 0 ;
2344- }
2345-
2346- /* Check for overflow in dstY + h */
2347- if (h > 0 && dstY > INT_MAX - h ) {
2348- return 0 ;
2349- }
2350-
2351- /* Check for overflow in srcX + w */
2352- if (w > 0 && srcX > INT_MAX - w ) {
2353- return 0 ;
2354- }
2355-
2356- /* Check for overflow in srcY + h */
2357- if (h > 0 && srcY > INT_MAX - h ) {
2358- return 0 ;
2359- }
2360-
2361- return 1 ;
2362- }
2363-
23642330void gdImageCopy (gdImagePtr dst , gdImagePtr src , int dstX , int dstY , int srcX , int srcY , int w , int h )
23652331{
2366- if (!_gdValidateCopyRectBounds (dst , src , dstX , dstY , srcX , srcY , w , h )) {
2367- return ;
2368- }
2369-
23702332 int c ;
23712333 int x , y ;
23722334 int tox , toy ;
23732335 int i ;
23742336 int colorMap [gdMaxColors ];
2375-
2337+ if (!gdImageClipCopy (dst , & dstX , & dstY , & srcX , & srcY , & w , & h )) {
2338+ return ;
2339+ }
23762340 if (dst -> trueColor ) {
23772341 /* 2.0: much easier when the destination is truecolor. */
23782342 /* 2.0.10: needs a transparent-index check that is still valid if
@@ -2448,14 +2412,13 @@ void gdImageCopy (gdImagePtr dst, gdImagePtr src, int dstX, int dstY, int srcX,
24482412 so it doesn't pay attention to the alpha channel. */
24492413void gdImageCopyMerge (gdImagePtr dst , gdImagePtr src , int dstX , int dstY , int srcX , int srcY , int w , int h , int pct )
24502414{
2451- if (!_gdValidateCopyRectBounds (dst , src , dstX , dstY , srcX , srcY , w , h )) {
2452- return ;
2453- }
2454-
24552415 int c , dc ;
24562416 int x , y ;
24572417 int tox , toy ;
24582418 int ncR , ncG , ncB ;
2419+ if (!gdImageClipCopy (dst , & dstX , & dstY , & srcX , & srcY , & w , & h )) {
2420+ return ;
2421+ }
24592422 toy = dstY ;
24602423
24612424 for (y = srcY ; y < (srcY + h ); y ++ ) {
@@ -2492,15 +2455,16 @@ void gdImageCopyMerge (gdImagePtr dst, gdImagePtr src, int dstX, int dstY, int s
24922455 so it doesn't pay attention to the alpha channel. */
24932456void gdImageCopyMergeGray (gdImagePtr dst , gdImagePtr src , int dstX , int dstY , int srcX , int srcY , int w , int h , int pct )
24942457{
2495- if (!_gdValidateCopyRectBounds (dst , src , dstX , dstY , srcX , srcY , w , h )) {
2496- return ;
2497- }
2498-
24992458 int c , dc ;
25002459 int x , y ;
25012460 int tox , toy ;
25022461 int ncR , ncG , ncB ;
25032462 float g ;
2463+
2464+ if (!gdImageClipCopy (dst , & dstX , & dstY , & srcX , & srcY , & w , & h )) {
2465+ return ;
2466+ }
2467+
25042468 toy = dstY ;
25052469
25062470 for (y = srcY ; (y < (srcY + h )); y ++ ) {
@@ -2550,14 +2514,6 @@ void gdImageCopyMergeGray (gdImagePtr dst, gdImagePtr src, int dstX, int dstY, i
25502514
25512515void gdImageCopyResized (gdImagePtr dst , gdImagePtr src , int dstX , int dstY , int srcX , int srcY , int dstW , int dstH , int srcW , int srcH )
25522516{
2553- if (!_gdValidateCopyRectBounds (dst , src , dstX , dstY , srcX , srcY , dstW , dstH )) {
2554- return ;
2555- }
2556-
2557- if (!_gdValidateCopyRectBounds (dst , src , dstX , dstY , srcX , srcY , srcW , srcH )) {
2558- return ;
2559- }
2560-
25612517 int c ;
25622518 int x , y ;
25632519 int tox , toy ;
@@ -2573,7 +2529,9 @@ void gdImageCopyResized (gdImagePtr dst, gdImagePtr src, int dstX, int dstY, int
25732529 if (overflow2 (sizeof (int ), srcH )) {
25742530 return ;
25752531 }
2576-
2532+ if (!gdImageClipCopyResized (dst , & dstX , & dstY , & dstW , & dstH , & srcX , & srcY , & srcW , & srcH )) {
2533+ return ;
2534+ }
25772535 stx = (int * ) gdMalloc (sizeof (int ) * srcW );
25782536 sty = (int * ) gdMalloc (sizeof (int ) * srcH );
25792537
@@ -2668,21 +2626,16 @@ void gdImageCopyResized (gdImagePtr dst, gdImagePtr src, int dstX, int dstY, int
26682626
26692627void gdImageCopyResampled (gdImagePtr dst , gdImagePtr src , int dstX , int dstY , int srcX , int srcY , int dstW , int dstH , int srcW , int srcH )
26702628{
2671- if (!_gdValidateCopyRectBounds (dst , src , dstX , dstY , srcX , srcY , dstW , dstH )) {
2672- return ;
2673- }
2674-
2675- if (!_gdValidateCopyRectBounds (dst , src , dstX , dstY , srcX , srcY , srcW , srcH )) {
2676- return ;
2677- }
2678-
26792629 int x , y ;
26802630 double sy1 , sy2 , sx1 , sx2 ;
26812631
26822632 if (!dst -> trueColor ) {
26832633 gdImageCopyResized (dst , src , dstX , dstY , srcX , srcY , dstW , dstH , srcW , srcH );
26842634 return ;
26852635 }
2636+ if (!gdImageClipCopyResized (dst , & dstX , & dstY , & dstW , & dstH , & srcX , & srcY , & srcW , & srcH )) {
2637+ return ;
2638+ }
26862639 for (y = dstY ; (y < dstY + dstH ); y ++ ) {
26872640 sy1 = ((double ) y - (double ) dstY ) * (double ) srcH / (double ) dstH ;
26882641 sy2 = ((double ) (y + 1 ) - (double ) dstY ) * (double ) srcH / (double ) dstH ;
0 commit comments