@@ -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 );
@@ -612,10 +625,8 @@ audioop_avgpp(PyObject *self, PyObject *args)
612
625
613
626
if ( !PyArg_ParseTuple (args , "s#i:avgpp" , & cp , & len , & size ) )
614
627
return 0 ;
615
- if ( size != 1 && size != 2 && size != 4 ) {
616
- PyErr_SetString (AudioopError , "Size should be 1, 2 or 4" );
617
- return 0 ;
618
- }
628
+ if (!audioop_check_parameters (len , size ))
629
+ return NULL ;
619
630
/* Compute first delta value ahead. Also automatically makes us
620
631
** skip the first extreme value
621
632
*/
@@ -669,10 +680,8 @@ audioop_maxpp(PyObject *self, PyObject *args)
669
680
670
681
if ( !PyArg_ParseTuple (args , "s#i:maxpp" , & cp , & len , & size ) )
671
682
return 0 ;
672
- if ( size != 1 && size != 2 && size != 4 ) {
673
- PyErr_SetString (AudioopError , "Size should be 1, 2 or 4" );
674
- return 0 ;
675
- }
683
+ if (!audioop_check_parameters (len , size ))
684
+ return NULL ;
676
685
/* Compute first delta value ahead. Also automatically makes us
677
686
** skip the first extreme value
678
687
*/
@@ -720,10 +729,8 @@ audioop_cross(PyObject *self, PyObject *args)
720
729
721
730
if ( !PyArg_ParseTuple (args , "s#i:cross" , & cp , & len , & size ) )
722
731
return 0 ;
723
- if ( size != 1 && size != 2 && size != 4 ) {
724
- PyErr_SetString (AudioopError , "Size should be 1, 2 or 4" );
725
- return 0 ;
726
- }
732
+ if (!audioop_check_parameters (len , size ))
733
+ return NULL ;
727
734
ncross = -1 ;
728
735
prevval = 17 ; /* Anything <> 0,1 */
729
736
for ( i = 0 ; i < len ; i += size ) {
@@ -748,6 +755,8 @@ audioop_mul(PyObject *self, PyObject *args)
748
755
749
756
if ( !PyArg_ParseTuple (args , "s#id:mul" , & cp , & len , & size , & factor ) )
750
757
return 0 ;
758
+ if (!audioop_check_parameters (len , size ))
759
+ return NULL ;
751
760
752
761
if ( size == 1 ) maxval = (double ) 0x7f ;
753
762
else if ( size == 2 ) maxval = (double ) 0x7fff ;
@@ -790,6 +799,12 @@ audioop_tomono(PyObject *self, PyObject *args)
790
799
if ( !PyArg_ParseTuple (args , "s#idd:tomono" ,
791
800
& cp , & len , & size , & fac1 , & fac2 ) )
792
801
return 0 ;
802
+ if (!audioop_check_parameters (len , size ))
803
+ return NULL ;
804
+ if (((len / size ) & 1 ) != 0 ) {
805
+ PyErr_SetString (AudioopError , "not a whole number of frames" );
806
+ return NULL ;
807
+ }
793
808
794
809
if ( size == 1 ) maxval = (double ) 0x7f ;
795
810
else if ( size == 2 ) maxval = (double ) 0x7fff ;
@@ -835,6 +850,8 @@ audioop_tostereo(PyObject *self, PyObject *args)
835
850
if ( !PyArg_ParseTuple (args , "s#idd:tostereo" ,
836
851
& cp , & len , & size , & fac1 , & fac2 ) )
837
852
return 0 ;
853
+ if (!audioop_check_parameters (len , size ))
854
+ return NULL ;
838
855
839
856
if ( size == 1 ) maxval = (double ) 0x7f ;
840
857
else if ( size == 2 ) maxval = (double ) 0x7fff ;
@@ -893,7 +910,8 @@ audioop_add(PyObject *self, PyObject *args)
893
910
if ( !PyArg_ParseTuple (args , "s#s#i:add" ,
894
911
& cp1 , & len1 , & cp2 , & len2 , & size ) )
895
912
return 0 ;
896
-
913
+ if (!audioop_check_parameters (len1 , size ))
914
+ return NULL ;
897
915
if ( len1 != len2 ) {
898
916
PyErr_SetString (AudioopError , "Lengths should be the same" );
899
917
return 0 ;
@@ -948,10 +966,8 @@ audioop_bias(PyObject *self, PyObject *args)
948
966
& cp , & len , & size , & bias ) )
949
967
return 0 ;
950
968
951
- if ( size != 1 && size != 2 && size != 4 ) {
952
- PyErr_SetString (AudioopError , "Size should be 1, 2 or 4" );
953
- return 0 ;
954
- }
969
+ if (!audioop_check_parameters (len , size ))
970
+ return NULL ;
955
971
956
972
rv = PyString_FromStringAndSize (NULL , len );
957
973
if ( rv == 0 )
@@ -984,10 +1000,8 @@ audioop_reverse(PyObject *self, PyObject *args)
984
1000
& cp , & len , & size ) )
985
1001
return 0 ;
986
1002
987
- if ( size != 1 && size != 2 && size != 4 ) {
988
- PyErr_SetString (AudioopError , "Size should be 1, 2 or 4" );
989
- return 0 ;
990
- }
1003
+ if (!audioop_check_parameters (len , size ))
1004
+ return NULL ;
991
1005
992
1006
rv = PyString_FromStringAndSize (NULL , len );
993
1007
if ( rv == 0 )
@@ -1021,11 +1035,10 @@ audioop_lin2lin(PyObject *self, PyObject *args)
1021
1035
& cp , & len , & size , & size2 ) )
1022
1036
return 0 ;
1023
1037
1024
- if ( (size != 1 && size != 2 && size != 4 ) ||
1025
- (size2 != 1 && size2 != 2 && size2 != 4 )) {
1026
- PyErr_SetString (AudioopError , "Size should be 1, 2 or 4" );
1027
- return 0 ;
1028
- }
1038
+ if (!audioop_check_parameters (len , size ))
1039
+ return NULL ;
1040
+ if (!audioop_check_size (size2 ))
1041
+ return NULL ;
1029
1042
1030
1043
if (len /size > INT_MAX /size2 ) {
1031
1044
PyErr_SetString (PyExc_MemoryError ,
@@ -1075,10 +1088,8 @@ audioop_ratecv(PyObject *self, PyObject *args)
1075
1088
& nchannels , & inrate , & outrate , & state ,
1076
1089
& weightA , & weightB ))
1077
1090
return NULL ;
1078
- if (size != 1 && size != 2 && size != 4 ) {
1079
- PyErr_SetString (AudioopError , "Size should be 1, 2 or 4" );
1091
+ if (!audioop_check_size (size ))
1080
1092
return NULL ;
1081
- }
1082
1093
if (nchannels < 1 ) {
1083
1094
PyErr_SetString (AudioopError , "# of channels should be >= 1" );
1084
1095
return NULL ;
@@ -1255,10 +1266,8 @@ audioop_lin2ulaw(PyObject *self, PyObject *args)
1255
1266
& cp , & len , & size ) )
1256
1267
return 0 ;
1257
1268
1258
- if ( size != 1 && size != 2 && size != 4 ) {
1259
- PyErr_SetString (AudioopError , "Size should be 1, 2 or 4" );
1260
- return 0 ;
1261
- }
1269
+ if (!audioop_check_parameters (len , size ))
1270
+ return NULL ;
1262
1271
1263
1272
rv = PyString_FromStringAndSize (NULL , len /size );
1264
1273
if ( rv == 0 )
@@ -1289,10 +1298,8 @@ audioop_ulaw2lin(PyObject *self, PyObject *args)
1289
1298
& cp , & len , & size ) )
1290
1299
return 0 ;
1291
1300
1292
- if ( size != 1 && size != 2 && size != 4 ) {
1293
- PyErr_SetString (AudioopError , "Size should be 1, 2 or 4" );
1294
- return 0 ;
1295
- }
1301
+ if (!audioop_check_parameters (len , size ))
1302
+ return NULL ;
1296
1303
1297
1304
if (len > INT_MAX /size ) {
1298
1305
PyErr_SetString (PyExc_MemoryError ,
@@ -1328,10 +1335,8 @@ audioop_lin2alaw(PyObject *self, PyObject *args)
1328
1335
& cp , & len , & size ) )
1329
1336
return 0 ;
1330
1337
1331
- if ( size != 1 && size != 2 && size != 4 ) {
1332
- PyErr_SetString (AudioopError , "Size should be 1, 2 or 4" );
1333
- return 0 ;
1334
- }
1338
+ if (!audioop_check_parameters (len , size ))
1339
+ return NULL ;
1335
1340
1336
1341
rv = PyString_FromStringAndSize (NULL , len /size );
1337
1342
if ( rv == 0 )
@@ -1362,10 +1367,8 @@ audioop_alaw2lin(PyObject *self, PyObject *args)
1362
1367
& cp , & len , & size ) )
1363
1368
return 0 ;
1364
1369
1365
- if ( size != 1 && size != 2 && size != 4 ) {
1366
- PyErr_SetString (AudioopError , "Size should be 1, 2 or 4" );
1367
- return 0 ;
1368
- }
1370
+ if (!audioop_check_parameters (len , size ))
1371
+ return NULL ;
1369
1372
1370
1373
if (len > INT_MAX /size ) {
1371
1374
PyErr_SetString (PyExc_MemoryError ,
@@ -1402,11 +1405,8 @@ audioop_lin2adpcm(PyObject *self, PyObject *args)
1402
1405
& cp , & len , & size , & state ) )
1403
1406
return 0 ;
1404
1407
1405
-
1406
- if ( size != 1 && size != 2 && size != 4 ) {
1407
- PyErr_SetString (AudioopError , "Size should be 1, 2 or 4" );
1408
- return 0 ;
1409
- }
1408
+ if (!audioop_check_parameters (len , size ))
1409
+ return NULL ;
1410
1410
1411
1411
str = PyString_FromStringAndSize (NULL , len /(size * 2 ));
1412
1412
if ( str == 0 )
@@ -1509,10 +1509,8 @@ audioop_adpcm2lin(PyObject *self, PyObject *args)
1509
1509
& cp , & len , & size , & state ) )
1510
1510
return 0 ;
1511
1511
1512
- if ( size != 1 && size != 2 && size != 4 ) {
1513
- PyErr_SetString (AudioopError , "Size should be 1, 2 or 4" );
1514
- return 0 ;
1515
- }
1512
+ if (!audioop_check_parameters (len , size ))
1513
+ return NULL ;
1516
1514
1517
1515
/* Decode state, should have (value, step) */
1518
1516
if ( state == Py_None ) {
0 commit comments