@@ -295,6 +295,29 @@ static int stepsizeTable[89] = {
295
295
296
296
static PyObject * AudioopError ;
297
297
298
+ static int
299
+ audioop_check_size (int size )
300
+ {
301
+ if (size != 1 && size != 2 && size != 4 ) {
302
+ PyErr_SetString (AudioopError , "Size should be 1, 2 or 4" );
303
+ return 0 ;
304
+ }
305
+ else
306
+ return 1 ;
307
+ }
308
+
309
+ static int
310
+ audioop_check_parameters (int len , int size )
311
+ {
312
+ if (!audioop_check_size (size ))
313
+ return 0 ;
314
+ if (len % size != 0 ) {
315
+ PyErr_SetString (AudioopError , "not a whole number of frames" );
316
+ return 0 ;
317
+ }
318
+ return 1 ;
319
+ }
320
+
298
321
static PyObject *
299
322
audioop_getsample (PyObject * self , PyObject * args )
300
323
{
@@ -304,10 +327,8 @@ audioop_getsample(PyObject *self, PyObject *args)
304
327
305
328
if ( !PyArg_ParseTuple (args , "s#ii:getsample" , & cp , & len , & size , & i ) )
306
329
return 0 ;
307
- if ( size != 1 && size != 2 && size != 4 ) {
308
- PyErr_SetString (AudioopError , "Size should be 1, 2 or 4" );
309
- return 0 ;
310
- }
330
+ if (!audioop_check_parameters (len , size ))
331
+ return NULL ;
311
332
if ( i < 0 || i >= len /size ) {
312
333
PyErr_SetString (AudioopError , "Index out of range" );
313
334
return 0 ;
@@ -328,10 +349,8 @@ audioop_max(PyObject *self, PyObject *args)
328
349
329
350
if ( !PyArg_ParseTuple (args , "s#i:max" , & cp , & len , & size ) )
330
351
return 0 ;
331
- if ( size != 1 && size != 2 && size != 4 ) {
332
- PyErr_SetString (AudioopError , "Size should be 1, 2 or 4" );
333
- return 0 ;
334
- }
352
+ if (!audioop_check_parameters (len , size ))
353
+ return NULL ;
335
354
for ( i = 0 ; i < len ; i += size ) {
336
355
if ( size == 1 ) val = (int )* CHARP (cp , i );
337
356
else if ( size == 2 ) val = (int )* SHORTP (cp , i );
@@ -352,10 +371,8 @@ audioop_minmax(PyObject *self, PyObject *args)
352
371
353
372
if (!PyArg_ParseTuple (args , "s#i:minmax" , & cp , & len , & size ))
354
373
return NULL ;
355
- if (size != 1 && size != 2 && size != 4 ) {
356
- PyErr_SetString (AudioopError , "Size should be 1, 2 or 4" );
374
+ if (!audioop_check_parameters (len , size ))
357
375
return NULL ;
358
- }
359
376
for (i = 0 ; i < len ; i += size ) {
360
377
if (size == 1 ) val = (int ) * CHARP (cp , i );
361
378
else if (size == 2 ) val = (int ) * SHORTP (cp , i );
@@ -376,10 +393,8 @@ audioop_avg(PyObject *self, PyObject *args)
376
393
377
394
if ( !PyArg_ParseTuple (args , "s#i:avg" , & cp , & len , & size ) )
378
395
return 0 ;
379
- if ( size != 1 && size != 2 && size != 4 ) {
380
- PyErr_SetString (AudioopError , "Size should be 1, 2 or 4" );
381
- return 0 ;
382
- }
396
+ if (!audioop_check_parameters (len , size ))
397
+ return NULL ;
383
398
for ( i = 0 ; i < len ; i += size ) {
384
399
if ( size == 1 ) val = (int )* CHARP (cp , i );
385
400
else if ( size == 2 ) val = (int )* SHORTP (cp , i );
@@ -403,10 +418,8 @@ audioop_rms(PyObject *self, PyObject *args)
403
418
404
419
if ( !PyArg_ParseTuple (args , "s#i:rms" , & cp , & len , & size ) )
405
420
return 0 ;
406
- if ( size != 1 && size != 2 && size != 4 ) {
407
- PyErr_SetString (AudioopError , "Size should be 1, 2 or 4" );
408
- return 0 ;
409
- }
421
+ if (!audioop_check_parameters (len , size ))
422
+ return NULL ;
410
423
for ( i = 0 ; i < len ; i += size ) {
411
424
if ( size == 1 ) val = (int )* CHARP (cp , i );
412
425
else if ( size == 2 ) val = (int )* SHORTP (cp , i );
@@ -609,10 +622,8 @@ audioop_avgpp(PyObject *self, PyObject *args)
609
622
610
623
if ( !PyArg_ParseTuple (args , "s#i:avgpp" , & cp , & len , & size ) )
611
624
return 0 ;
612
- if ( size != 1 && size != 2 && size != 4 ) {
613
- PyErr_SetString (AudioopError , "Size should be 1, 2 or 4" );
614
- return 0 ;
615
- }
625
+ if (!audioop_check_parameters (len , size ))
626
+ return NULL ;
616
627
/* Compute first delta value ahead. Also automatically makes us
617
628
** skip the first extreme value
618
629
*/
@@ -666,10 +677,8 @@ audioop_maxpp(PyObject *self, PyObject *args)
666
677
667
678
if ( !PyArg_ParseTuple (args , "s#i:maxpp" , & cp , & len , & size ) )
668
679
return 0 ;
669
- if ( size != 1 && size != 2 && size != 4 ) {
670
- PyErr_SetString (AudioopError , "Size should be 1, 2 or 4" );
671
- return 0 ;
672
- }
680
+ if (!audioop_check_parameters (len , size ))
681
+ return NULL ;
673
682
/* Compute first delta value ahead. Also automatically makes us
674
683
** skip the first extreme value
675
684
*/
@@ -717,10 +726,8 @@ audioop_cross(PyObject *self, PyObject *args)
717
726
718
727
if ( !PyArg_ParseTuple (args , "s#i:cross" , & cp , & len , & size ) )
719
728
return 0 ;
720
- if ( size != 1 && size != 2 && size != 4 ) {
721
- PyErr_SetString (AudioopError , "Size should be 1, 2 or 4" );
722
- return 0 ;
723
- }
729
+ if (!audioop_check_parameters (len , size ))
730
+ return NULL ;
724
731
ncross = -1 ;
725
732
prevval = 17 ; /* Anything <> 0,1 */
726
733
for ( i = 0 ; i < len ; i += size ) {
@@ -745,6 +752,8 @@ audioop_mul(PyObject *self, PyObject *args)
745
752
746
753
if ( !PyArg_ParseTuple (args , "s#id:mul" , & cp , & len , & size , & factor ) )
747
754
return 0 ;
755
+ if (!audioop_check_parameters (len , size ))
756
+ return NULL ;
748
757
749
758
if ( size == 1 ) maxval = (double ) 0x7f ;
750
759
else if ( size == 2 ) maxval = (double ) 0x7fff ;
@@ -787,6 +796,12 @@ audioop_tomono(PyObject *self, PyObject *args)
787
796
if ( !PyArg_ParseTuple (args , "s#idd:tomono" ,
788
797
& cp , & len , & size , & fac1 , & fac2 ) )
789
798
return 0 ;
799
+ if (!audioop_check_parameters (len , size ))
800
+ return NULL ;
801
+ if (((len / size ) & 1 ) != 0 ) {
802
+ PyErr_SetString (AudioopError , "not a whole number of frames" );
803
+ return NULL ;
804
+ }
790
805
791
806
if ( size == 1 ) maxval = (double ) 0x7f ;
792
807
else if ( size == 2 ) maxval = (double ) 0x7fff ;
@@ -832,6 +847,8 @@ audioop_tostereo(PyObject *self, PyObject *args)
832
847
if ( !PyArg_ParseTuple (args , "s#idd:tostereo" ,
833
848
& cp , & len , & size , & fac1 , & fac2 ) )
834
849
return 0 ;
850
+ if (!audioop_check_parameters (len , size ))
851
+ return NULL ;
835
852
836
853
if ( size == 1 ) maxval = (double ) 0x7f ;
837
854
else if ( size == 2 ) maxval = (double ) 0x7fff ;
@@ -890,7 +907,8 @@ audioop_add(PyObject *self, PyObject *args)
890
907
if ( !PyArg_ParseTuple (args , "s#s#i:add" ,
891
908
& cp1 , & len1 , & cp2 , & len2 , & size ) )
892
909
return 0 ;
893
-
910
+ if (!audioop_check_parameters (len1 , size ))
911
+ return NULL ;
894
912
if ( len1 != len2 ) {
895
913
PyErr_SetString (AudioopError , "Lengths should be the same" );
896
914
return 0 ;
@@ -945,10 +963,8 @@ audioop_bias(PyObject *self, PyObject *args)
945
963
& cp , & len , & size , & bias ) )
946
964
return 0 ;
947
965
948
- if ( size != 1 && size != 2 && size != 4 ) {
949
- PyErr_SetString (AudioopError , "Size should be 1, 2 or 4" );
950
- return 0 ;
951
- }
966
+ if (!audioop_check_parameters (len , size ))
967
+ return NULL ;
952
968
953
969
rv = PyString_FromStringAndSize (NULL , len );
954
970
if ( rv == 0 )
@@ -981,10 +997,8 @@ audioop_reverse(PyObject *self, PyObject *args)
981
997
& cp , & len , & size ) )
982
998
return 0 ;
983
999
984
- if ( size != 1 && size != 2 && size != 4 ) {
985
- PyErr_SetString (AudioopError , "Size should be 1, 2 or 4" );
986
- return 0 ;
987
- }
1000
+ if (!audioop_check_parameters (len , size ))
1001
+ return NULL ;
988
1002
989
1003
rv = PyString_FromStringAndSize (NULL , len );
990
1004
if ( rv == 0 )
@@ -1018,11 +1032,10 @@ audioop_lin2lin(PyObject *self, PyObject *args)
1018
1032
& cp , & len , & size , & size2 ) )
1019
1033
return 0 ;
1020
1034
1021
- if ( (size != 1 && size != 2 && size != 4 ) ||
1022
- (size2 != 1 && size2 != 2 && size2 != 4 )) {
1023
- PyErr_SetString (AudioopError , "Size should be 1, 2 or 4" );
1024
- return 0 ;
1025
- }
1035
+ if (!audioop_check_parameters (len , size ))
1036
+ return NULL ;
1037
+ if (!audioop_check_size (size2 ))
1038
+ return NULL ;
1026
1039
1027
1040
if (len /size > INT_MAX /size2 ) {
1028
1041
PyErr_SetString (PyExc_MemoryError ,
@@ -1072,10 +1085,8 @@ audioop_ratecv(PyObject *self, PyObject *args)
1072
1085
& nchannels , & inrate , & outrate , & state ,
1073
1086
& weightA , & weightB ))
1074
1087
return NULL ;
1075
- if (size != 1 && size != 2 && size != 4 ) {
1076
- PyErr_SetString (AudioopError , "Size should be 1, 2 or 4" );
1088
+ if (!audioop_check_size (size ))
1077
1089
return NULL ;
1078
- }
1079
1090
if (nchannels < 1 ) {
1080
1091
PyErr_SetString (AudioopError , "# of channels should be >= 1" );
1081
1092
return NULL ;
@@ -1252,10 +1263,8 @@ audioop_lin2ulaw(PyObject *self, PyObject *args)
1252
1263
& cp , & len , & size ) )
1253
1264
return 0 ;
1254
1265
1255
- if ( size != 1 && size != 2 && size != 4 ) {
1256
- PyErr_SetString (AudioopError , "Size should be 1, 2 or 4" );
1257
- return 0 ;
1258
- }
1266
+ if (!audioop_check_parameters (len , size ))
1267
+ return NULL ;
1259
1268
1260
1269
rv = PyString_FromStringAndSize (NULL , len /size );
1261
1270
if ( rv == 0 )
@@ -1286,10 +1295,8 @@ audioop_ulaw2lin(PyObject *self, PyObject *args)
1286
1295
& cp , & len , & size ) )
1287
1296
return 0 ;
1288
1297
1289
- if ( size != 1 && size != 2 && size != 4 ) {
1290
- PyErr_SetString (AudioopError , "Size should be 1, 2 or 4" );
1291
- return 0 ;
1292
- }
1298
+ if (!audioop_check_parameters (len , size ))
1299
+ return NULL ;
1293
1300
1294
1301
if (len > INT_MAX /size ) {
1295
1302
PyErr_SetString (PyExc_MemoryError ,
@@ -1325,10 +1332,8 @@ audioop_lin2alaw(PyObject *self, PyObject *args)
1325
1332
& cp , & len , & size ) )
1326
1333
return 0 ;
1327
1334
1328
- if ( size != 1 && size != 2 && size != 4 ) {
1329
- PyErr_SetString (AudioopError , "Size should be 1, 2 or 4" );
1330
- return 0 ;
1331
- }
1335
+ if (!audioop_check_parameters (len , size ))
1336
+ return NULL ;
1332
1337
1333
1338
rv = PyString_FromStringAndSize (NULL , len /size );
1334
1339
if ( rv == 0 )
@@ -1359,10 +1364,8 @@ audioop_alaw2lin(PyObject *self, PyObject *args)
1359
1364
& cp , & len , & size ) )
1360
1365
return 0 ;
1361
1366
1362
- if ( size != 1 && size != 2 && size != 4 ) {
1363
- PyErr_SetString (AudioopError , "Size should be 1, 2 or 4" );
1364
- return 0 ;
1365
- }
1367
+ if (!audioop_check_parameters (len , size ))
1368
+ return NULL ;
1366
1369
1367
1370
if (len > INT_MAX /size ) {
1368
1371
PyErr_SetString (PyExc_MemoryError ,
@@ -1399,12 +1402,9 @@ audioop_lin2adpcm(PyObject *self, PyObject *args)
1399
1402
& cp , & len , & size , & state ) )
1400
1403
return 0 ;
1401
1404
1405
+ if (!audioop_check_parameters (len , size ))
1406
+ return NULL ;
1402
1407
1403
- if ( size != 1 && size != 2 && size != 4 ) {
1404
- PyErr_SetString (AudioopError , "Size should be 1, 2 or 4" );
1405
- return 0 ;
1406
- }
1407
-
1408
1408
str = PyString_FromStringAndSize (NULL , len /(size * 2 ));
1409
1409
if ( str == 0 )
1410
1410
return 0 ;
@@ -1507,10 +1507,8 @@ audioop_adpcm2lin(PyObject *self, PyObject *args)
1507
1507
& cp , & len , & size , & state ) )
1508
1508
return 0 ;
1509
1509
1510
- if ( size != 1 && size != 2 && size != 4 ) {
1511
- PyErr_SetString (AudioopError , "Size should be 1, 2 or 4" );
1512
- return 0 ;
1513
- }
1510
+ if (!audioop_check_parameters (len , size ))
1511
+ return NULL ;
1514
1512
1515
1513
/* Decode state, should have (value, step) */
1516
1514
if ( state == Py_None ) {
0 commit comments