annotate Modules/imageop.c @ 58925:eecf1bef052d 2.6

Merged revisions 78207 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r78207 | ezio.melotti | 2010-02-17 01:26:09 +0200 (Wed, 17 Feb 2010) | 1 line #7930: fix stripid ........
author Ezio Melotti <ezio.melotti@gmail.com>
date Tue, 16 Feb 2010 23:29:44 +0000
parents d6e67989ff51
children fd25030223b0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1084
b755a46201f6 Added imageopmodule.c, also added them to Makefile and config.c.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1
b755a46201f6 Added imageopmodule.c, also added them to Makefile and config.c.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2 /* imageopmodule - Various operations on pictures */
b755a46201f6 Added imageopmodule.c, also added them to Makefile and config.c.
Guido van Rossum <guido@python.org>
parents:
diff changeset
3
b755a46201f6 Added imageopmodule.c, also added them to Makefile and config.c.
Guido van Rossum <guido@python.org>
parents:
diff changeset
4 #ifdef sun
b755a46201f6 Added imageopmodule.c, also added them to Makefile and config.c.
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 #define signed
b755a46201f6 Added imageopmodule.c, also added them to Makefile and config.c.
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 #endif
b755a46201f6 Added imageopmodule.c, also added them to Makefile and config.c.
Guido van Rossum <guido@python.org>
parents:
diff changeset
7
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
8 #include "Python.h"
1084
b755a46201f6 Added imageopmodule.c, also added them to Makefile and config.c.
Guido van Rossum <guido@python.org>
parents:
diff changeset
9
8129
1094ebbd3a45 Purported fixes for 64-bit platforms with help from John Jorgensen
Guido van Rossum <guido@python.org>
parents: 6367
diff changeset
10 #if SIZEOF_INT == 4
1094ebbd3a45 Purported fixes for 64-bit platforms with help from John Jorgensen
Guido van Rossum <guido@python.org>
parents: 6367
diff changeset
11 typedef int Py_Int32;
1094ebbd3a45 Purported fixes for 64-bit platforms with help from John Jorgensen
Guido van Rossum <guido@python.org>
parents: 6367
diff changeset
12 typedef unsigned int Py_UInt32;
1094ebbd3a45 Purported fixes for 64-bit platforms with help from John Jorgensen
Guido van Rossum <guido@python.org>
parents: 6367
diff changeset
13 #else
1094ebbd3a45 Purported fixes for 64-bit platforms with help from John Jorgensen
Guido van Rossum <guido@python.org>
parents: 6367
diff changeset
14 #if SIZEOF_LONG == 4
1094ebbd3a45 Purported fixes for 64-bit platforms with help from John Jorgensen
Guido van Rossum <guido@python.org>
parents: 6367
diff changeset
15 typedef long Py_Int32;
1094ebbd3a45 Purported fixes for 64-bit platforms with help from John Jorgensen
Guido van Rossum <guido@python.org>
parents: 6367
diff changeset
16 typedef unsigned long Py_UInt32;
1094ebbd3a45 Purported fixes for 64-bit platforms with help from John Jorgensen
Guido van Rossum <guido@python.org>
parents: 6367
diff changeset
17 #else
1094ebbd3a45 Purported fixes for 64-bit platforms with help from John Jorgensen
Guido van Rossum <guido@python.org>
parents: 6367
diff changeset
18 #error "No 4-byte integral type"
1094ebbd3a45 Purported fixes for 64-bit platforms with help from John Jorgensen
Guido van Rossum <guido@python.org>
parents: 6367
diff changeset
19 #endif
1094ebbd3a45 Purported fixes for 64-bit platforms with help from John Jorgensen
Guido van Rossum <guido@python.org>
parents: 6367
diff changeset
20 #endif
1094ebbd3a45 Purported fixes for 64-bit platforms with help from John Jorgensen
Guido van Rossum <guido@python.org>
parents: 6367
diff changeset
21
1084
b755a46201f6 Added imageopmodule.c, also added them to Makefile and config.c.
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 #define CHARP(cp, xmax, x, y) ((char *)(cp+y*xmax+x))
3344
3b484812acf6 Added 16-bit pixel support for some operations.
Jack Jansen <jack.jansen@cwi.nl>
parents: 2061
diff changeset
23 #define SHORTP(cp, xmax, x, y) ((short *)(cp+2*(y*xmax+x)))
8129
1094ebbd3a45 Purported fixes for 64-bit platforms with help from John Jorgensen
Guido van Rossum <guido@python.org>
parents: 6367
diff changeset
24 #define LONGP(cp, xmax, x, y) ((Py_Int32 *)(cp+4*(y*xmax+x)))
1084
b755a46201f6 Added imageopmodule.c, also added them to Makefile and config.c.
Guido van Rossum <guido@python.org>
parents:
diff changeset
25
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
26 static PyObject *ImageopError;
31366
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
27 static PyObject *ImageopDict;
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
28
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
29 /**
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
30 * Check a coordonnate, make sure that (0 < value).
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
31 * Return 0 on error.
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
32 */
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
33 static int
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
34 check_coordonnate(int value, const char* name)
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
35 {
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
36 if ( 0 < value)
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
37 return 1;
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
38 PyErr_Format(PyExc_ValueError, "%s value is negative or nul", name);
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
39 return 0;
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
40 }
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
41
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
42 /**
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
43 * Check integer overflow to make sure that product == x*y*size.
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
44 * Return 0 on error.
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
45 */
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
46 static int
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
47 check_multiply_size(int product, int x, const char* xname, int y, const char* yname, int size)
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
48 {
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
49 if ( !check_coordonnate(x, xname) )
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
50 return 0;
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
51 if ( !check_coordonnate(y, yname) )
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
52 return 0;
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
53 if ( size == (product / y) / x )
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
54 return 1;
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
55 PyErr_SetString(ImageopError, "String has incorrect length");
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
56 return 0;
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
57 }
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
58
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
59 /**
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
60 * Check integer overflow to make sure that product == x*y.
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
61 * Return 0 on error.
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
62 */
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
63 static int
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
64 check_multiply(int product, int x, int y)
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
65 {
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
66 return check_multiply_size(product, x, "x", y, "y", 1);
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
67 }
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
68
31366
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
69 /* If this function returns true (the default if anything goes wrong), we're
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
70 behaving in a backward-compatible way with respect to how multi-byte pixels
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
71 are stored in the strings. The code in this module was originally written
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
72 for an SGI which is a big-endian system, and so the old code assumed that
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
73 4-byte integers hold the R, G, and B values in a particular order.
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
74 However, on little-endian systems the order is reversed, and so not
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
75 actually compatible with what gl.lrectwrite and imgfile expect.
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
76 (gl.lrectwrite and imgfile are also SGI-specific, however, it is
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
77 conceivable that the data handled here comes from or goes to an SGI or that
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
78 it is otherwise used in the expectation that the byte order in the strings
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
79 is as specified.)
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
80
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
81 The function returns the value of the module variable
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
82 "backward_compatible", or 1 if the variable does not exist or is not an
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
83 int.
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
84 */
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
85
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
86 static int
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
87 imageop_backward_compatible(void)
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
88 {
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
89 static PyObject *bcos;
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
90 PyObject *bco;
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
91 long rc;
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
92
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
93 if (ImageopDict == NULL) /* "cannot happen" */
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
94 return 1;
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
95 if (bcos == NULL) {
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
96 /* cache string object for future use */
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
97 bcos = PyString_FromString("backward_compatible");
31366
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
98 if (bcos == NULL)
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
99 return 1;
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
100 }
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
101 bco = PyDict_GetItem(ImageopDict, bcos);
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
102 if (bco == NULL)
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
103 return 1;
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
104 if (!PyInt_Check(bco))
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
105 return 1;
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
106 rc = PyInt_AsLong(bco);
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
107 if (PyErr_Occurred()) {
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
108 /* not an integer, or too large, or something */
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
109 PyErr_Clear();
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
110 rc = 1;
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
111 }
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
112 return rc != 0; /* convert to values 0, 1 */
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
113 }
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
114
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
115 static PyObject *
13495
423a5b950cc9 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
116 imageop_crop(PyObject *self, PyObject *args)
1084
b755a46201f6 Added imageopmodule.c, also added them to Makefile and config.c.
Guido van Rossum <guido@python.org>
parents:
diff changeset
117 {
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
118 char *cp, *ncp;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
119 short *nsp;
8129
1094ebbd3a45 Purported fixes for 64-bit platforms with help from John Jorgensen
Guido van Rossum <guido@python.org>
parents: 6367
diff changeset
120 Py_Int32 *nlp;
48310
4b8806456078 Issue 1179: [CVE-2007-4965] Integer overflow in imageop module.
Guido van Rossum <guido@python.org>
parents: 47527
diff changeset
121 int len, size, x, y, newx1, newx2, newy1, newy2, nlen;
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
122 int ix, iy, xstep, ystep;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
123 PyObject *rv;
1084
b755a46201f6 Added imageopmodule.c, also added them to Makefile and config.c.
Guido van Rossum <guido@python.org>
parents:
diff changeset
124
22772
59d3913a29c4 Convert more METH_OLDARGS & PyArg_Parse()
Neal Norwitz <nnorwitz@gmail.com>
parents: 21757
diff changeset
125 if ( !PyArg_ParseTuple(args, "s#iiiiiii", &cp, &len, &size, &x, &y,
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
126 &newx1, &newy1, &newx2, &newy2) )
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
127 return 0;
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
128
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
129 if ( size != 1 && size != 2 && size != 4 ) {
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
130 PyErr_SetString(ImageopError, "Size should be 1, 2 or 4");
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
131 return 0;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
132 }
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
133 if ( !check_multiply_size(len, x, "x", y, "y", size) )
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
134 return 0;
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
135
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
136 xstep = (newx1 < newx2)? 1 : -1;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
137 ystep = (newy1 < newy2)? 1 : -1;
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
138
48310
4b8806456078 Issue 1179: [CVE-2007-4965] Integer overflow in imageop module.
Guido van Rossum <guido@python.org>
parents: 47527
diff changeset
139 nlen = (abs(newx2-newx1)+1)*(abs(newy2-newy1)+1)*size;
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
140 if ( !check_multiply_size(nlen, abs(newx2-newx1)+1, "abs(newx2-newx1)+1", abs(newy2-newy1)+1, "abs(newy2-newy1)+1", size) )
48310
4b8806456078 Issue 1179: [CVE-2007-4965] Integer overflow in imageop module.
Guido van Rossum <guido@python.org>
parents: 47527
diff changeset
141 return 0;
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
142 rv = PyString_FromStringAndSize(NULL, nlen);
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
143 if ( rv == 0 )
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
144 return 0;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
145 ncp = (char *)PyString_AsString(rv);
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
146 nsp = (short *)ncp;
8129
1094ebbd3a45 Purported fixes for 64-bit platforms with help from John Jorgensen
Guido van Rossum <guido@python.org>
parents: 6367
diff changeset
147 nlp = (Py_Int32 *)ncp;
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
148 newy2 += ystep;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
149 newx2 += xstep;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
150 for( iy = newy1; iy != newy2; iy+=ystep ) {
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
151 for ( ix = newx1; ix != newx2; ix+=xstep ) {
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
152 if ( iy < 0 || iy >= y || ix < 0 || ix >= x ) {
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
153 if ( size == 1 )
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
154 *ncp++ = 0;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
155 else
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
156 *nlp++ = 0;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
157 } else {
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
158 if ( size == 1 )
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
159 *ncp++ = *CHARP(cp, x, ix, iy);
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
160 else if ( size == 2 )
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
161 *nsp++ = *SHORTP(cp, x, ix, iy);
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
162 else
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
163 *nlp++ = *LONGP(cp, x, ix, iy);
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
164 }
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
165 }
1084
b755a46201f6 Added imageopmodule.c, also added them to Makefile and config.c.
Guido van Rossum <guido@python.org>
parents:
diff changeset
166 }
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
167 return rv;
1084
b755a46201f6 Added imageopmodule.c, also added them to Makefile and config.c.
Guido van Rossum <guido@python.org>
parents:
diff changeset
168 }
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
169
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
170 static PyObject *
13495
423a5b950cc9 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
171 imageop_scale(PyObject *self, PyObject *args)
1084
b755a46201f6 Added imageopmodule.c, also added them to Makefile and config.c.
Guido van Rossum <guido@python.org>
parents:
diff changeset
172 {
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
173 char *cp, *ncp;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
174 short *nsp;
8129
1094ebbd3a45 Purported fixes for 64-bit platforms with help from John Jorgensen
Guido van Rossum <guido@python.org>
parents: 6367
diff changeset
175 Py_Int32 *nlp;
48310
4b8806456078 Issue 1179: [CVE-2007-4965] Integer overflow in imageop module.
Guido van Rossum <guido@python.org>
parents: 47527
diff changeset
176 int len, size, x, y, newx, newy, nlen;
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
177 int ix, iy;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
178 int oix, oiy;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
179 PyObject *rv;
1084
b755a46201f6 Added imageopmodule.c, also added them to Makefile and config.c.
Guido van Rossum <guido@python.org>
parents:
diff changeset
180
22772
59d3913a29c4 Convert more METH_OLDARGS & PyArg_Parse()
Neal Norwitz <nnorwitz@gmail.com>
parents: 21757
diff changeset
181 if ( !PyArg_ParseTuple(args, "s#iiiii",
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
182 &cp, &len, &size, &x, &y, &newx, &newy) )
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
183 return 0;
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
184
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
185 if ( size != 1 && size != 2 && size != 4 ) {
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
186 PyErr_SetString(ImageopError, "Size should be 1, 2 or 4");
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
187 return 0;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
188 }
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
189 if ( !check_multiply_size(len, x, "x", y, "y", size) )
48310
4b8806456078 Issue 1179: [CVE-2007-4965] Integer overflow in imageop module.
Guido van Rossum <guido@python.org>
parents: 47527
diff changeset
190 return 0;
4b8806456078 Issue 1179: [CVE-2007-4965] Integer overflow in imageop module.
Guido van Rossum <guido@python.org>
parents: 47527
diff changeset
191 nlen = newx*newy*size;
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
192 if ( !check_multiply_size(nlen, newx, "newx", newy, "newy", size) )
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
193 return 0;
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
194
48310
4b8806456078 Issue 1179: [CVE-2007-4965] Integer overflow in imageop module.
Guido van Rossum <guido@python.org>
parents: 47527
diff changeset
195 rv = PyString_FromStringAndSize(NULL, nlen);
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
196 if ( rv == 0 )
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
197 return 0;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
198 ncp = (char *)PyString_AsString(rv);
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
199 nsp = (short *)ncp;
8129
1094ebbd3a45 Purported fixes for 64-bit platforms with help from John Jorgensen
Guido van Rossum <guido@python.org>
parents: 6367
diff changeset
200 nlp = (Py_Int32 *)ncp;
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
201 for( iy = 0; iy < newy; iy++ ) {
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
202 for ( ix = 0; ix < newx; ix++ ) {
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
203 oix = ix * x / newx;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
204 oiy = iy * y / newy;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
205 if ( size == 1 )
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
206 *ncp++ = *CHARP(cp, x, oix, oiy);
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
207 else if ( size == 2 )
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
208 *nsp++ = *SHORTP(cp, x, oix, oiy);
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
209 else
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
210 *nlp++ = *LONGP(cp, x, oix, oiy);
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
211 }
1084
b755a46201f6 Added imageopmodule.c, also added them to Makefile and config.c.
Guido van Rossum <guido@python.org>
parents:
diff changeset
212 }
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
213 return rv;
1084
b755a46201f6 Added imageopmodule.c, also added them to Makefile and config.c.
Guido van Rossum <guido@python.org>
parents:
diff changeset
214 }
b755a46201f6 Added imageopmodule.c, also added them to Makefile and config.c.
Guido van Rossum <guido@python.org>
parents:
diff changeset
215
1194
310532015a7d Added tovideo routine
Jack Jansen <jack.jansen@cwi.nl>
parents: 1165
diff changeset
216 /* Note: this routine can use a bit of optimizing */
310532015a7d Added tovideo routine
Jack Jansen <jack.jansen@cwi.nl>
parents: 1165
diff changeset
217
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
218 static PyObject *
13495
423a5b950cc9 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
219 imageop_tovideo(PyObject *self, PyObject *args)
1194
310532015a7d Added tovideo routine
Jack Jansen <jack.jansen@cwi.nl>
parents: 1165
diff changeset
220 {
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
221 int maxx, maxy, x, y, len;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
222 int i;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
223 unsigned char *cp, *ncp;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
224 int width;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
225 PyObject *rv;
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
226
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
227
22772
59d3913a29c4 Convert more METH_OLDARGS & PyArg_Parse()
Neal Norwitz <nnorwitz@gmail.com>
parents: 21757
diff changeset
228 if ( !PyArg_ParseTuple(args, "s#iii", &cp, &len, &width, &maxx, &maxy) )
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
229 return 0;
1194
310532015a7d Added tovideo routine
Jack Jansen <jack.jansen@cwi.nl>
parents: 1165
diff changeset
230
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
231 if ( width != 1 && width != 4 ) {
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
232 PyErr_SetString(ImageopError, "Size should be 1 or 4");
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
233 return 0;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
234 }
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
235 if ( !check_multiply_size(len, maxx, "max", maxy, "maxy", width) )
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
236 return 0;
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
237
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
238 rv = PyString_FromStringAndSize(NULL, len);
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
239 if ( rv == 0 )
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
240 return 0;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
241 ncp = (unsigned char *)PyString_AsString(rv);
1194
310532015a7d Added tovideo routine
Jack Jansen <jack.jansen@cwi.nl>
parents: 1165
diff changeset
242
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
243 if ( width == 1 ) {
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
244 memcpy(ncp, cp, maxx); /* Copy first line */
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
245 ncp += maxx;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
246 for (y=1; y<maxy; y++) { /* Interpolate other lines */
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
247 for(x=0; x<maxx; x++) {
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
248 i = y*maxx + x;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
249 *ncp++ = ((int)cp[i] + (int)cp[i-maxx]) >> 1;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
250 }
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
251 }
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
252 } else {
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
253 memcpy(ncp, cp, maxx*4); /* Copy first line */
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
254 ncp += maxx*4;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
255 for (y=1; y<maxy; y++) { /* Interpolate other lines */
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
256 for(x=0; x<maxx; x++) {
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
257 i = (y*maxx + x)*4 + 1;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
258 *ncp++ = 0; /* Skip alfa comp */
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
259 *ncp++ = ((int)cp[i] + (int)cp[i-4*maxx]) >> 1;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
260 i++;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
261 *ncp++ = ((int)cp[i] + (int)cp[i-4*maxx]) >> 1;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
262 i++;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
263 *ncp++ = ((int)cp[i] + (int)cp[i-4*maxx]) >> 1;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
264 }
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
265 }
1194
310532015a7d Added tovideo routine
Jack Jansen <jack.jansen@cwi.nl>
parents: 1165
diff changeset
266 }
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
267 return rv;
1194
310532015a7d Added tovideo routine
Jack Jansen <jack.jansen@cwi.nl>
parents: 1165
diff changeset
268 }
310532015a7d Added tovideo routine
Jack Jansen <jack.jansen@cwi.nl>
parents: 1165
diff changeset
269
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
270 static PyObject *
13495
423a5b950cc9 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
271 imageop_grey2mono(PyObject *self, PyObject *args)
1132
6461dac9a9f2 * mymalloc.h: always allocate one extra byte, since some malloc's
Guido van Rossum <guido@python.org>
parents: 1084
diff changeset
272 {
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
273 int tres, x, y, len;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
274 unsigned char *cp, *ncp;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
275 unsigned char ovalue;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
276 PyObject *rv;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
277 int i, bit;
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
278
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
279
22772
59d3913a29c4 Convert more METH_OLDARGS & PyArg_Parse()
Neal Norwitz <nnorwitz@gmail.com>
parents: 21757
diff changeset
280 if ( !PyArg_ParseTuple(args, "s#iii", &cp, &len, &x, &y, &tres) )
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
281 return 0;
1132
6461dac9a9f2 * mymalloc.h: always allocate one extra byte, since some malloc's
Guido van Rossum <guido@python.org>
parents: 1084
diff changeset
282
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
283 if ( !check_multiply(len, x, y) )
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
284 return 0;
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
285
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
286 rv = PyString_FromStringAndSize(NULL, (len+7)/8);
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
287 if ( rv == 0 )
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
288 return 0;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
289 ncp = (unsigned char *)PyString_AsString(rv);
1132
6461dac9a9f2 * mymalloc.h: always allocate one extra byte, since some malloc's
Guido van Rossum <guido@python.org>
parents: 1084
diff changeset
290
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
291 bit = 0x80;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
292 ovalue = 0;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
293 for ( i=0; i < len; i++ ) {
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
294 if ( (int)cp[i] > tres )
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
295 ovalue |= bit;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
296 bit >>= 1;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
297 if ( bit == 0 ) {
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
298 *ncp++ = ovalue;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
299 bit = 0x80;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
300 ovalue = 0;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
301 }
1132
6461dac9a9f2 * mymalloc.h: always allocate one extra byte, since some malloc's
Guido van Rossum <guido@python.org>
parents: 1084
diff changeset
302 }
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
303 if ( bit != 0x80 )
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
304 *ncp++ = ovalue;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
305 return rv;
1132
6461dac9a9f2 * mymalloc.h: always allocate one extra byte, since some malloc's
Guido van Rossum <guido@python.org>
parents: 1084
diff changeset
306 }
6461dac9a9f2 * mymalloc.h: always allocate one extra byte, since some malloc's
Guido van Rossum <guido@python.org>
parents: 1084
diff changeset
307
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
308 static PyObject *
13495
423a5b950cc9 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
309 imageop_grey2grey4(PyObject *self, PyObject *args)
1165
d990c2b8a1e6 Added 2 and 4 bits grey formats
Jack Jansen <jack.jansen@cwi.nl>
parents: 1132
diff changeset
310 {
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
311 int x, y, len;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
312 unsigned char *cp, *ncp;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
313 unsigned char ovalue;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
314 PyObject *rv;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
315 int i;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
316 int pos;
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
317
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
318
22772
59d3913a29c4 Convert more METH_OLDARGS & PyArg_Parse()
Neal Norwitz <nnorwitz@gmail.com>
parents: 21757
diff changeset
319 if ( !PyArg_ParseTuple(args, "s#ii", &cp, &len, &x, &y) )
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
320 return 0;
1165
d990c2b8a1e6 Added 2 and 4 bits grey formats
Jack Jansen <jack.jansen@cwi.nl>
parents: 1132
diff changeset
321
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
322 if ( !check_multiply(len, x, y) )
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
323 return 0;
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
324
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
325 rv = PyString_FromStringAndSize(NULL, (len+1)/2);
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
326 if ( rv == 0 )
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
327 return 0;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
328 ncp = (unsigned char *)PyString_AsString(rv);
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
329 pos = 0;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
330 ovalue = 0;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
331 for ( i=0; i < len; i++ ) {
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
332 ovalue |= ((int)cp[i] & 0xf0) >> pos;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
333 pos += 4;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
334 if ( pos == 8 ) {
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
335 *ncp++ = ovalue;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
336 ovalue = 0;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
337 pos = 0;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
338 }
1165
d990c2b8a1e6 Added 2 and 4 bits grey formats
Jack Jansen <jack.jansen@cwi.nl>
parents: 1132
diff changeset
339 }
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
340 if ( pos != 0 )
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
341 *ncp++ = ovalue;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
342 return rv;
1165
d990c2b8a1e6 Added 2 and 4 bits grey formats
Jack Jansen <jack.jansen@cwi.nl>
parents: 1132
diff changeset
343 }
d990c2b8a1e6 Added 2 and 4 bits grey formats
Jack Jansen <jack.jansen@cwi.nl>
parents: 1132
diff changeset
344
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
345 static PyObject *
13495
423a5b950cc9 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
346 imageop_grey2grey2(PyObject *self, PyObject *args)
1165
d990c2b8a1e6 Added 2 and 4 bits grey formats
Jack Jansen <jack.jansen@cwi.nl>
parents: 1132
diff changeset
347 {
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
348 int x, y, len;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
349 unsigned char *cp, *ncp;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
350 unsigned char ovalue;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
351 PyObject *rv;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
352 int i;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
353 int pos;
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
354
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
355
22772
59d3913a29c4 Convert more METH_OLDARGS & PyArg_Parse()
Neal Norwitz <nnorwitz@gmail.com>
parents: 21757
diff changeset
356 if ( !PyArg_ParseTuple(args, "s#ii", &cp, &len, &x, &y) )
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
357 return 0;
1165
d990c2b8a1e6 Added 2 and 4 bits grey formats
Jack Jansen <jack.jansen@cwi.nl>
parents: 1132
diff changeset
358
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
359 if ( !check_multiply(len, x, y) )
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
360 return 0;
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
361
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
362 rv = PyString_FromStringAndSize(NULL, (len+3)/4);
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
363 if ( rv == 0 )
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
364 return 0;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
365 ncp = (unsigned char *)PyString_AsString(rv);
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
366 pos = 0;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
367 ovalue = 0;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
368 for ( i=0; i < len; i++ ) {
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
369 ovalue |= ((int)cp[i] & 0xc0) >> pos;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
370 pos += 2;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
371 if ( pos == 8 ) {
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
372 *ncp++ = ovalue;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
373 ovalue = 0;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
374 pos = 0;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
375 }
1165
d990c2b8a1e6 Added 2 and 4 bits grey formats
Jack Jansen <jack.jansen@cwi.nl>
parents: 1132
diff changeset
376 }
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
377 if ( pos != 0 )
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
378 *ncp++ = ovalue;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
379 return rv;
1165
d990c2b8a1e6 Added 2 and 4 bits grey formats
Jack Jansen <jack.jansen@cwi.nl>
parents: 1132
diff changeset
380 }
d990c2b8a1e6 Added 2 and 4 bits grey formats
Jack Jansen <jack.jansen@cwi.nl>
parents: 1132
diff changeset
381
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
382 static PyObject *
13495
423a5b950cc9 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
383 imageop_dither2mono(PyObject *self, PyObject *args)
1132
6461dac9a9f2 * mymalloc.h: always allocate one extra byte, since some malloc's
Guido van Rossum <guido@python.org>
parents: 1084
diff changeset
384 {
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
385 int sum, x, y, len;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
386 unsigned char *cp, *ncp;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
387 unsigned char ovalue;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
388 PyObject *rv;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
389 int i, bit;
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
390
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
391
22772
59d3913a29c4 Convert more METH_OLDARGS & PyArg_Parse()
Neal Norwitz <nnorwitz@gmail.com>
parents: 21757
diff changeset
392 if ( !PyArg_ParseTuple(args, "s#ii", &cp, &len, &x, &y) )
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
393 return 0;
1132
6461dac9a9f2 * mymalloc.h: always allocate one extra byte, since some malloc's
Guido van Rossum <guido@python.org>
parents: 1084
diff changeset
394
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
395 if ( !check_multiply(len, x, y) )
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
396 return 0;
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
397
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
398 rv = PyString_FromStringAndSize(NULL, (len+7)/8);
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
399 if ( rv == 0 )
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
400 return 0;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
401 ncp = (unsigned char *)PyString_AsString(rv);
1132
6461dac9a9f2 * mymalloc.h: always allocate one extra byte, since some malloc's
Guido van Rossum <guido@python.org>
parents: 1084
diff changeset
402
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
403 bit = 0x80;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
404 ovalue = 0;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
405 sum = 0;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
406 for ( i=0; i < len; i++ ) {
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
407 sum += cp[i];
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
408 if ( sum >= 256 ) {
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
409 sum -= 256;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
410 ovalue |= bit;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
411 }
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
412 bit >>= 1;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
413 if ( bit == 0 ) {
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
414 *ncp++ = ovalue;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
415 bit = 0x80;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
416 ovalue = 0;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
417 }
1132
6461dac9a9f2 * mymalloc.h: always allocate one extra byte, since some malloc's
Guido van Rossum <guido@python.org>
parents: 1084
diff changeset
418 }
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
419 if ( bit != 0x80 )
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
420 *ncp++ = ovalue;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
421 return rv;
1132
6461dac9a9f2 * mymalloc.h: always allocate one extra byte, since some malloc's
Guido van Rossum <guido@python.org>
parents: 1084
diff changeset
422 }
6461dac9a9f2 * mymalloc.h: always allocate one extra byte, since some malloc's
Guido van Rossum <guido@python.org>
parents: 1084
diff changeset
423
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
424 static PyObject *
13495
423a5b950cc9 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
425 imageop_dither2grey2(PyObject *self, PyObject *args)
1165
d990c2b8a1e6 Added 2 and 4 bits grey formats
Jack Jansen <jack.jansen@cwi.nl>
parents: 1132
diff changeset
426 {
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
427 int x, y, len;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
428 unsigned char *cp, *ncp;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
429 unsigned char ovalue;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
430 PyObject *rv;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
431 int i;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
432 int pos;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
433 int sum = 0, nvalue;
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
434
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
435
22772
59d3913a29c4 Convert more METH_OLDARGS & PyArg_Parse()
Neal Norwitz <nnorwitz@gmail.com>
parents: 21757
diff changeset
436 if ( !PyArg_ParseTuple(args, "s#ii", &cp, &len, &x, &y) )
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
437 return 0;
1165
d990c2b8a1e6 Added 2 and 4 bits grey formats
Jack Jansen <jack.jansen@cwi.nl>
parents: 1132
diff changeset
438
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
439 if ( !check_multiply(len, x, y) )
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
440 return 0;
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
441
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
442 rv = PyString_FromStringAndSize(NULL, (len+3)/4);
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
443 if ( rv == 0 )
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
444 return 0;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
445 ncp = (unsigned char *)PyString_AsString(rv);
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
446 pos = 1;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
447 ovalue = 0;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
448 for ( i=0; i < len; i++ ) {
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
449 sum += cp[i];
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
450 nvalue = sum & 0x180;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
451 sum -= nvalue;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
452 ovalue |= nvalue >> pos;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
453 pos += 2;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
454 if ( pos == 9 ) {
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
455 *ncp++ = ovalue;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
456 ovalue = 0;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
457 pos = 1;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
458 }
1165
d990c2b8a1e6 Added 2 and 4 bits grey formats
Jack Jansen <jack.jansen@cwi.nl>
parents: 1132
diff changeset
459 }
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
460 if ( pos != 0 )
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
461 *ncp++ = ovalue;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
462 return rv;
1165
d990c2b8a1e6 Added 2 and 4 bits grey formats
Jack Jansen <jack.jansen@cwi.nl>
parents: 1132
diff changeset
463 }
d990c2b8a1e6 Added 2 and 4 bits grey formats
Jack Jansen <jack.jansen@cwi.nl>
parents: 1132
diff changeset
464
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
465 static PyObject *
13495
423a5b950cc9 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
466 imageop_mono2grey(PyObject *self, PyObject *args)
1132
6461dac9a9f2 * mymalloc.h: always allocate one extra byte, since some malloc's
Guido van Rossum <guido@python.org>
parents: 1084
diff changeset
467 {
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
468 int v0, v1, x, y, len, nlen;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
469 unsigned char *cp, *ncp;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
470 PyObject *rv;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
471 int i, bit;
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
472
22772
59d3913a29c4 Convert more METH_OLDARGS & PyArg_Parse()
Neal Norwitz <nnorwitz@gmail.com>
parents: 21757
diff changeset
473 if ( !PyArg_ParseTuple(args, "s#iiii", &cp, &len, &x, &y, &v0, &v1) )
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
474 return 0;
1132
6461dac9a9f2 * mymalloc.h: always allocate one extra byte, since some malloc's
Guido van Rossum <guido@python.org>
parents: 1084
diff changeset
475
48310
4b8806456078 Issue 1179: [CVE-2007-4965] Integer overflow in imageop module.
Guido van Rossum <guido@python.org>
parents: 47527
diff changeset
476 nlen = x*y;
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
477 if ( !check_multiply(nlen, x, y) )
48310
4b8806456078 Issue 1179: [CVE-2007-4965] Integer overflow in imageop module.
Guido van Rossum <guido@python.org>
parents: 47527
diff changeset
478 return 0;
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
479 if ( (nlen+7)/8 != len ) {
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
480 PyErr_SetString(ImageopError, "String has incorrect length");
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
481 return 0;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
482 }
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
483
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
484 rv = PyString_FromStringAndSize(NULL, nlen);
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
485 if ( rv == 0 )
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
486 return 0;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
487 ncp = (unsigned char *)PyString_AsString(rv);
1132
6461dac9a9f2 * mymalloc.h: always allocate one extra byte, since some malloc's
Guido van Rossum <guido@python.org>
parents: 1084
diff changeset
488
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
489 bit = 0x80;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
490 for ( i=0; i < nlen; i++ ) {
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
491 if ( *cp & bit )
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
492 *ncp++ = v1;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
493 else
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
494 *ncp++ = v0;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
495 bit >>= 1;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
496 if ( bit == 0 ) {
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
497 bit = 0x80;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
498 cp++;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
499 }
1132
6461dac9a9f2 * mymalloc.h: always allocate one extra byte, since some malloc's
Guido van Rossum <guido@python.org>
parents: 1084
diff changeset
500 }
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
501 return rv;
1132
6461dac9a9f2 * mymalloc.h: always allocate one extra byte, since some malloc's
Guido van Rossum <guido@python.org>
parents: 1084
diff changeset
502 }
6461dac9a9f2 * mymalloc.h: always allocate one extra byte, since some malloc's
Guido van Rossum <guido@python.org>
parents: 1084
diff changeset
503
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
504 static PyObject *
13495
423a5b950cc9 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
505 imageop_grey22grey(PyObject *self, PyObject *args)
1165
d990c2b8a1e6 Added 2 and 4 bits grey formats
Jack Jansen <jack.jansen@cwi.nl>
parents: 1132
diff changeset
506 {
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
507 int x, y, len, nlen;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
508 unsigned char *cp, *ncp;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
509 PyObject *rv;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
510 int i, pos, value = 0, nvalue;
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
511
22772
59d3913a29c4 Convert more METH_OLDARGS & PyArg_Parse()
Neal Norwitz <nnorwitz@gmail.com>
parents: 21757
diff changeset
512 if ( !PyArg_ParseTuple(args, "s#ii", &cp, &len, &x, &y) )
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
513 return 0;
1165
d990c2b8a1e6 Added 2 and 4 bits grey formats
Jack Jansen <jack.jansen@cwi.nl>
parents: 1132
diff changeset
514
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
515 nlen = x*y;
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
516 if ( !check_multiply(nlen, x, y) ) {
48310
4b8806456078 Issue 1179: [CVE-2007-4965] Integer overflow in imageop module.
Guido van Rossum <guido@python.org>
parents: 47527
diff changeset
517 return 0;
4b8806456078 Issue 1179: [CVE-2007-4965] Integer overflow in imageop module.
Guido van Rossum <guido@python.org>
parents: 47527
diff changeset
518 }
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
519 if ( (nlen+3)/4 != len ) {
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
520 PyErr_SetString(ImageopError, "String has incorrect length");
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
521 return 0;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
522 }
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
523
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
524 rv = PyString_FromStringAndSize(NULL, nlen);
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
525 if ( rv == 0 )
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
526 return 0;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
527 ncp = (unsigned char *)PyString_AsString(rv);
1165
d990c2b8a1e6 Added 2 and 4 bits grey formats
Jack Jansen <jack.jansen@cwi.nl>
parents: 1132
diff changeset
528
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
529 pos = 0;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
530 for ( i=0; i < nlen; i++ ) {
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
531 if ( pos == 0 ) {
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
532 value = *cp++;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
533 pos = 8;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
534 }
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
535 pos -= 2;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
536 nvalue = (value >> pos) & 0x03;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
537 *ncp++ = nvalue | (nvalue << 2) |
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
538 (nvalue << 4) | (nvalue << 6);
1165
d990c2b8a1e6 Added 2 and 4 bits grey formats
Jack Jansen <jack.jansen@cwi.nl>
parents: 1132
diff changeset
539 }
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
540 return rv;
1165
d990c2b8a1e6 Added 2 and 4 bits grey formats
Jack Jansen <jack.jansen@cwi.nl>
parents: 1132
diff changeset
541 }
d990c2b8a1e6 Added 2 and 4 bits grey formats
Jack Jansen <jack.jansen@cwi.nl>
parents: 1132
diff changeset
542
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
543 static PyObject *
13495
423a5b950cc9 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
544 imageop_grey42grey(PyObject *self, PyObject *args)
1165
d990c2b8a1e6 Added 2 and 4 bits grey formats
Jack Jansen <jack.jansen@cwi.nl>
parents: 1132
diff changeset
545 {
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
546 int x, y, len, nlen;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
547 unsigned char *cp, *ncp;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
548 PyObject *rv;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
549 int i, pos, value = 0, nvalue;
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
550
22772
59d3913a29c4 Convert more METH_OLDARGS & PyArg_Parse()
Neal Norwitz <nnorwitz@gmail.com>
parents: 21757
diff changeset
551 if ( !PyArg_ParseTuple(args, "s#ii", &cp, &len, &x, &y) )
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
552 return 0;
1165
d990c2b8a1e6 Added 2 and 4 bits grey formats
Jack Jansen <jack.jansen@cwi.nl>
parents: 1132
diff changeset
553
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
554 nlen = x*y;
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
555 if ( !check_multiply(nlen, x, y) )
48310
4b8806456078 Issue 1179: [CVE-2007-4965] Integer overflow in imageop module.
Guido van Rossum <guido@python.org>
parents: 47527
diff changeset
556 return 0;
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
557 if ( (nlen+1)/2 != len ) {
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
558 PyErr_SetString(ImageopError, "String has incorrect length");
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
559 return 0;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
560 }
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
561
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
562 rv = PyString_FromStringAndSize(NULL, nlen);
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
563 if ( rv == 0 )
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
564 return 0;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
565 ncp = (unsigned char *)PyString_AsString(rv);
1165
d990c2b8a1e6 Added 2 and 4 bits grey formats
Jack Jansen <jack.jansen@cwi.nl>
parents: 1132
diff changeset
566
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
567 pos = 0;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
568 for ( i=0; i < nlen; i++ ) {
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
569 if ( pos == 0 ) {
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
570 value = *cp++;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
571 pos = 8;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
572 }
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
573 pos -= 4;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
574 nvalue = (value >> pos) & 0x0f;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
575 *ncp++ = nvalue | (nvalue << 4);
1165
d990c2b8a1e6 Added 2 and 4 bits grey formats
Jack Jansen <jack.jansen@cwi.nl>
parents: 1132
diff changeset
576 }
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
577 return rv;
1165
d990c2b8a1e6 Added 2 and 4 bits grey formats
Jack Jansen <jack.jansen@cwi.nl>
parents: 1132
diff changeset
578 }
d990c2b8a1e6 Added 2 and 4 bits grey formats
Jack Jansen <jack.jansen@cwi.nl>
parents: 1132
diff changeset
579
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
580 static PyObject *
13495
423a5b950cc9 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
581 imageop_rgb2rgb8(PyObject *self, PyObject *args)
1233
b3e6a435c48f Added converters for rgb<->rgb8 and rgb<->grey
Jack Jansen <jack.jansen@cwi.nl>
parents: 1198
diff changeset
582 {
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
583 int x, y, len, nlen;
31366
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
584 unsigned char *cp;
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
585 unsigned char *ncp;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
586 PyObject *rv;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
587 int i, r, g, b;
31366
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
588 int backward_compatible = imageop_backward_compatible();
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
589
22772
59d3913a29c4 Convert more METH_OLDARGS & PyArg_Parse()
Neal Norwitz <nnorwitz@gmail.com>
parents: 21757
diff changeset
590 if ( !PyArg_ParseTuple(args, "s#ii", &cp, &len, &x, &y) )
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
591 return 0;
1233
b3e6a435c48f Added converters for rgb<->rgb8 and rgb<->grey
Jack Jansen <jack.jansen@cwi.nl>
parents: 1198
diff changeset
592
49750
d6e67989ff51 Merged revisions 67266 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49347
diff changeset
593 if ( !check_multiply_size(len, x, "x", y, "y", 4) )
48310
4b8806456078 Issue 1179: [CVE-2007-4965] Integer overflow in imageop module.
Guido van Rossum <guido@python.org>
parents: 47527
diff changeset
594 return 0;
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
595 nlen = x*y;
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
596 if ( !check_multiply(nlen, x, y) )
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
597 return 0;
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
598
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
599 rv = PyString_FromStringAndSize(NULL, nlen);
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
600 if ( rv == 0 )
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
601 return 0;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
602 ncp = (unsigned char *)PyString_AsString(rv);
1233
b3e6a435c48f Added converters for rgb<->rgb8 and rgb<->grey
Jack Jansen <jack.jansen@cwi.nl>
parents: 1198
diff changeset
603
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
604 for ( i=0; i < nlen; i++ ) {
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
605 /* Bits in source: aaaaaaaa BBbbbbbb GGGggggg RRRrrrrr */
31366
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
606 if (backward_compatible) {
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
607 Py_UInt32 value = * (Py_UInt32 *) cp;
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
608 cp += 4;
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
609 r = (int) ((value & 0xff) / 255. * 7. + .5);
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
610 g = (int) (((value >> 8) & 0xff) / 255. * 7. + .5);
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
611 b = (int) (((value >> 16) & 0xff) / 255. * 3. + .5);
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
612 } else {
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
613 cp++; /* skip alpha channel */
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
614 b = (int) (*cp++ / 255. * 3. + .5);
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
615 g = (int) (*cp++ / 255. * 7. + .5);
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
616 r = (int) (*cp++ / 255. * 7. + .5);
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
617 }
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
618 *ncp++ = (unsigned char)((r<<5) | (b<<3) | g);
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
619 }
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
620 return rv;
1233
b3e6a435c48f Added converters for rgb<->rgb8 and rgb<->grey
Jack Jansen <jack.jansen@cwi.nl>
parents: 1198
diff changeset
621 }
b3e6a435c48f Added converters for rgb<->rgb8 and rgb<->grey
Jack Jansen <jack.jansen@cwi.nl>
parents: 1198
diff changeset
622
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
623 static PyObject *
13495
423a5b950cc9 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
624 imageop_rgb82rgb(PyObject *self, PyObject *args)
1233
b3e6a435c48f Added converters for rgb<->rgb8 and rgb<->grey
Jack Jansen <jack.jansen@cwi.nl>
parents: 1198
diff changeset
625 {
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
626 int x, y, len, nlen;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
627 unsigned char *cp;
31366
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
628 unsigned char *ncp;
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
629 PyObject *rv;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
630 int i, r, g, b;
31366
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
631 unsigned char value;
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
632 int backward_compatible = imageop_backward_compatible();
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
633
22772
59d3913a29c4 Convert more METH_OLDARGS & PyArg_Parse()
Neal Norwitz <nnorwitz@gmail.com>
parents: 21757
diff changeset
634 if ( !PyArg_ParseTuple(args, "s#ii", &cp, &len, &x, &y) )
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
635 return 0;
1233
b3e6a435c48f Added converters for rgb<->rgb8 and rgb<->grey
Jack Jansen <jack.jansen@cwi.nl>
parents: 1198
diff changeset
636
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
637 if ( !check_multiply(len, x, y) )
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
638 return 0;
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
639 nlen = x*y*4;
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
640 if ( !check_multiply_size(nlen, x, "x", y, "y", 4) )
48310
4b8806456078 Issue 1179: [CVE-2007-4965] Integer overflow in imageop module.
Guido van Rossum <guido@python.org>
parents: 47527
diff changeset
641 return 0;
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
642
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
643 rv = PyString_FromStringAndSize(NULL, nlen);
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
644 if ( rv == 0 )
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
645 return 0;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
646 ncp = (unsigned char *)PyString_AsString(rv);
1233
b3e6a435c48f Added converters for rgb<->rgb8 and rgb<->grey
Jack Jansen <jack.jansen@cwi.nl>
parents: 1198
diff changeset
647
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
648 for ( i=0; i < len; i++ ) {
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
649 /* Bits in source: RRRBBGGG
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
650 ** Red and Green are multiplied by 36.5, Blue by 85
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
651 */
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
652 value = *cp++;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
653 r = (value >> 5) & 7;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
654 g = (value ) & 7;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
655 b = (value >> 3) & 3;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
656 r = (r<<5) | (r<<3) | (r>>1);
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
657 g = (g<<5) | (g<<3) | (g>>1);
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
658 b = (b<<6) | (b<<4) | (b<<2) | b;
31366
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
659 if (backward_compatible) {
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
660 Py_UInt32 nvalue = r | (g<<8) | (b<<16);
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
661 * (Py_UInt32 *) ncp = nvalue;
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
662 ncp += 4;
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
663 } else {
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
664 *ncp++ = 0;
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
665 *ncp++ = b;
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
666 *ncp++ = g;
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
667 *ncp++ = r;
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
668 }
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
669 }
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
670 return rv;
1233
b3e6a435c48f Added converters for rgb<->rgb8 and rgb<->grey
Jack Jansen <jack.jansen@cwi.nl>
parents: 1198
diff changeset
671 }
b3e6a435c48f Added converters for rgb<->rgb8 and rgb<->grey
Jack Jansen <jack.jansen@cwi.nl>
parents: 1198
diff changeset
672
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
673 static PyObject *
13495
423a5b950cc9 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
674 imageop_rgb2grey(PyObject *self, PyObject *args)
1233
b3e6a435c48f Added converters for rgb<->rgb8 and rgb<->grey
Jack Jansen <jack.jansen@cwi.nl>
parents: 1198
diff changeset
675 {
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
676 int x, y, len, nlen;
31366
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
677 unsigned char *cp;
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
678 unsigned char *ncp;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
679 PyObject *rv;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
680 int i, r, g, b;
31366
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
681 int nvalue;
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
682 int backward_compatible = imageop_backward_compatible();
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
683
22772
59d3913a29c4 Convert more METH_OLDARGS & PyArg_Parse()
Neal Norwitz <nnorwitz@gmail.com>
parents: 21757
diff changeset
684 if ( !PyArg_ParseTuple(args, "s#ii", &cp, &len, &x, &y) )
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
685 return 0;
1233
b3e6a435c48f Added converters for rgb<->rgb8 and rgb<->grey
Jack Jansen <jack.jansen@cwi.nl>
parents: 1198
diff changeset
686
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
687 if ( !check_multiply_size(len, x, "x", y, "y", 4) )
48310
4b8806456078 Issue 1179: [CVE-2007-4965] Integer overflow in imageop module.
Guido van Rossum <guido@python.org>
parents: 47527
diff changeset
688 return 0;
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
689 nlen = x*y;
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
690 if ( !check_multiply(nlen, x, y) )
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
691 return 0;
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
692
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
693 rv = PyString_FromStringAndSize(NULL, nlen);
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
694 if ( rv == 0 )
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
695 return 0;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
696 ncp = (unsigned char *)PyString_AsString(rv);
1233
b3e6a435c48f Added converters for rgb<->rgb8 and rgb<->grey
Jack Jansen <jack.jansen@cwi.nl>
parents: 1198
diff changeset
697
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
698 for ( i=0; i < nlen; i++ ) {
31366
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
699 if (backward_compatible) {
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
700 Py_UInt32 value = * (Py_UInt32 *) cp;
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
701 cp += 4;
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
702 r = (int) ((value & 0xff) / 255. * 7. + .5);
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
703 g = (int) (((value >> 8) & 0xff) / 255. * 7. + .5);
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
704 b = (int) (((value >> 16) & 0xff) / 255. * 3. + .5);
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
705 } else {
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
706 cp++; /* skip alpha channel */
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
707 b = *cp++;
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
708 g = *cp++;
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
709 r = *cp++;
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
710 }
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
711 nvalue = (int)(0.30*r + 0.59*g + 0.11*b);
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
712 if ( nvalue > 255 ) nvalue = 255;
5287
5af766a1b531 Tweaks to keep the Microsoft compiler quier.
Guido van Rossum <guido@python.org>
parents: 4803
diff changeset
713 *ncp++ = (unsigned char)nvalue;
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
714 }
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
715 return rv;
1233
b3e6a435c48f Added converters for rgb<->rgb8 and rgb<->grey
Jack Jansen <jack.jansen@cwi.nl>
parents: 1198
diff changeset
716 }
b3e6a435c48f Added converters for rgb<->rgb8 and rgb<->grey
Jack Jansen <jack.jansen@cwi.nl>
parents: 1198
diff changeset
717
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
718 static PyObject *
13495
423a5b950cc9 ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13256
diff changeset
719 imageop_grey2rgb(PyObject *self, PyObject *args)
1233
b3e6a435c48f Added converters for rgb<->rgb8 and rgb<->grey
Jack Jansen <jack.jansen@cwi.nl>
parents: 1198
diff changeset
720 {
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
721 int x, y, len, nlen;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
722 unsigned char *cp;
31366
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
723 unsigned char *ncp;
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
724 PyObject *rv;
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
725 int i;
31366
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
726 unsigned char value;
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
727 int backward_compatible = imageop_backward_compatible();
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
728
22772
59d3913a29c4 Convert more METH_OLDARGS & PyArg_Parse()
Neal Norwitz <nnorwitz@gmail.com>
parents: 21757
diff changeset
729 if ( !PyArg_ParseTuple(args, "s#ii", &cp, &len, &x, &y) )
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
730 return 0;
1233
b3e6a435c48f Added converters for rgb<->rgb8 and rgb<->grey
Jack Jansen <jack.jansen@cwi.nl>
parents: 1198
diff changeset
731
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
732 if ( !check_multiply(len, x, y) )
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
733 return 0;
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
734 nlen = x*y*4;
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
735 if ( !check_multiply_size(nlen, x, "x", y, "y", 4) )
48310
4b8806456078 Issue 1179: [CVE-2007-4965] Integer overflow in imageop module.
Guido van Rossum <guido@python.org>
parents: 47527
diff changeset
736 return 0;
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
737
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
738 rv = PyString_FromStringAndSize(NULL, nlen);
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
739 if ( rv == 0 )
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
740 return 0;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
741 ncp = (unsigned char *)PyString_AsString(rv);
1233
b3e6a435c48f Added converters for rgb<->rgb8 and rgb<->grey
Jack Jansen <jack.jansen@cwi.nl>
parents: 1198
diff changeset
742
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
743 for ( i=0; i < len; i++ ) {
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
744 value = *cp++;
31366
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
745 if (backward_compatible) {
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
746 * (Py_UInt32 *) ncp = (Py_UInt32) value | ((Py_UInt32) value << 8 ) | ((Py_UInt32) value << 16);
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
747 ncp += 4;
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
748 } else {
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
749 *ncp++ = 0;
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
750 *ncp++ = value;
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
751 *ncp++ = value;
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
752 *ncp++ = value;
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
753 }
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
754 }
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
755 return rv;
1233
b3e6a435c48f Added converters for rgb<->rgb8 and rgb<->grey
Jack Jansen <jack.jansen@cwi.nl>
parents: 1198
diff changeset
756 }
b3e6a435c48f Added converters for rgb<->rgb8 and rgb<->grey
Jack Jansen <jack.jansen@cwi.nl>
parents: 1198
diff changeset
757
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
758 static PyMethodDef imageop_methods[] = {
22772
59d3913a29c4 Convert more METH_OLDARGS & PyArg_Parse()
Neal Norwitz <nnorwitz@gmail.com>
parents: 21757
diff changeset
759 { "crop", imageop_crop, METH_VARARGS },
59d3913a29c4 Convert more METH_OLDARGS & PyArg_Parse()
Neal Norwitz <nnorwitz@gmail.com>
parents: 21757
diff changeset
760 { "scale", imageop_scale, METH_VARARGS },
59d3913a29c4 Convert more METH_OLDARGS & PyArg_Parse()
Neal Norwitz <nnorwitz@gmail.com>
parents: 21757
diff changeset
761 { "grey2mono", imageop_grey2mono, METH_VARARGS },
59d3913a29c4 Convert more METH_OLDARGS & PyArg_Parse()
Neal Norwitz <nnorwitz@gmail.com>
parents: 21757
diff changeset
762 { "grey2grey2", imageop_grey2grey2, METH_VARARGS },
59d3913a29c4 Convert more METH_OLDARGS & PyArg_Parse()
Neal Norwitz <nnorwitz@gmail.com>
parents: 21757
diff changeset
763 { "grey2grey4", imageop_grey2grey4, METH_VARARGS },
59d3913a29c4 Convert more METH_OLDARGS & PyArg_Parse()
Neal Norwitz <nnorwitz@gmail.com>
parents: 21757
diff changeset
764 { "dither2mono", imageop_dither2mono, METH_VARARGS },
59d3913a29c4 Convert more METH_OLDARGS & PyArg_Parse()
Neal Norwitz <nnorwitz@gmail.com>
parents: 21757
diff changeset
765 { "dither2grey2", imageop_dither2grey2, METH_VARARGS },
59d3913a29c4 Convert more METH_OLDARGS & PyArg_Parse()
Neal Norwitz <nnorwitz@gmail.com>
parents: 21757
diff changeset
766 { "mono2grey", imageop_mono2grey, METH_VARARGS },
59d3913a29c4 Convert more METH_OLDARGS & PyArg_Parse()
Neal Norwitz <nnorwitz@gmail.com>
parents: 21757
diff changeset
767 { "grey22grey", imageop_grey22grey, METH_VARARGS },
59d3913a29c4 Convert more METH_OLDARGS & PyArg_Parse()
Neal Norwitz <nnorwitz@gmail.com>
parents: 21757
diff changeset
768 { "grey42grey", imageop_grey42grey, METH_VARARGS },
59d3913a29c4 Convert more METH_OLDARGS & PyArg_Parse()
Neal Norwitz <nnorwitz@gmail.com>
parents: 21757
diff changeset
769 { "tovideo", imageop_tovideo, METH_VARARGS },
59d3913a29c4 Convert more METH_OLDARGS & PyArg_Parse()
Neal Norwitz <nnorwitz@gmail.com>
parents: 21757
diff changeset
770 { "rgb2rgb8", imageop_rgb2rgb8, METH_VARARGS },
59d3913a29c4 Convert more METH_OLDARGS & PyArg_Parse()
Neal Norwitz <nnorwitz@gmail.com>
parents: 21757
diff changeset
771 { "rgb82rgb", imageop_rgb82rgb, METH_VARARGS },
59d3913a29c4 Convert more METH_OLDARGS & PyArg_Parse()
Neal Norwitz <nnorwitz@gmail.com>
parents: 21757
diff changeset
772 { "rgb2grey", imageop_rgb2grey, METH_VARARGS },
59d3913a29c4 Convert more METH_OLDARGS & PyArg_Parse()
Neal Norwitz <nnorwitz@gmail.com>
parents: 21757
diff changeset
773 { "grey2rgb", imageop_grey2rgb, METH_VARARGS },
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
774 { 0, 0 }
1084
b755a46201f6 Added imageopmodule.c, also added them to Makefile and config.c.
Guido van Rossum <guido@python.org>
parents:
diff changeset
775 };
b755a46201f6 Added imageopmodule.c, also added them to Makefile and config.c.
Guido van Rossum <guido@python.org>
parents:
diff changeset
776
b755a46201f6 Added imageopmodule.c, also added them to Makefile and config.c.
Guido van Rossum <guido@python.org>
parents:
diff changeset
777
24471
f66b3a3509c7 Excise DL_EXPORT/DL_IMPORT from Modules/*. Required adding a prototype
Mark Hammond <mhammond@skippinet.com.au>
parents: 22772
diff changeset
778 PyMODINIT_FUNC
13651
8a063423e0b7 Bunch of minor ANSIfications: 'void initfunc()' -> 'void initfunc(void)',
Thomas Wouters <thomas@python.org>
parents: 13495
diff changeset
779 initimageop(void)
1084
b755a46201f6 Added imageopmodule.c, also added them to Makefile and config.c.
Guido van Rossum <guido@python.org>
parents:
diff changeset
780 {
31366
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
781 PyObject *m;
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
782
46923
c2ab7a06ed24 Depreate imageop for removal in 3.0.
Brett Cannon <bcannon@gmail.com>
parents: 36324
diff changeset
783 if (PyErr_WarnPy3k("the imageop module has been removed in "
c2ab7a06ed24 Depreate imageop for removal in 3.0.
Brett Cannon <bcannon@gmail.com>
parents: 36324
diff changeset
784 "Python 3.0", 2) < 0)
c2ab7a06ed24 Depreate imageop for removal in 3.0.
Brett Cannon <bcannon@gmail.com>
parents: 36324
diff changeset
785 return;
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 48310
diff changeset
786
4803
9965df782afc Renamed.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4600
diff changeset
787 m = Py_InitModule("imageop", imageop_methods);
36324
1af7f719ed28 Check return result from Py_InitModule*(). This API can fail.
Neal Norwitz <nnorwitz@gmail.com>
parents: 31366
diff changeset
788 if (m == NULL)
1af7f719ed28 Check return result from Py_InitModule*(). This API can fail.
Neal Norwitz <nnorwitz@gmail.com>
parents: 31366
diff changeset
789 return;
31366
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
790 ImageopDict = PyModule_GetDict(m);
6367
2c29b8982643 Apply two changes, systematically:
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
791 ImageopError = PyErr_NewException("imageop.error", NULL, NULL);
2c29b8982643 Apply two changes, systematically:
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
792 if (ImageopError != NULL)
31366
99891b888cfd The format of the string data used in the imageop module is described
Sjoerd Mullender <sjoerd@acm.org>
parents: 24471
diff changeset
793 PyDict_SetItemString(ImageopDict, "error", ImageopError);
1084
b755a46201f6 Added imageopmodule.c, also added them to Makefile and config.c.
Guido van Rossum <guido@python.org>
parents:
diff changeset
794 }