@@ -297,6 +297,29 @@ static int stepsizeTable[89] = {
297
297
298
298
static PyObject *AudioopError;
299
299
300
+
static int
301
+
audioop_check_size(int size)
302
+
{
303
+
if (size != 1 && size != 2 && size != 4) {
304
+
PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
305
+
return 0;
306
+
}
307
+
else
308
+
return 1;
309
+
}
310
+
311
+
static int
312
+
audioop_check_parameters(int len, int size)
313
+
{
314
+
if (!audioop_check_size(size))
315
+
return 0;
316
+
if (len % size != 0) {
317
+
PyErr_SetString(AudioopError, "not a whole number of frames");
318
+
return 0;
319
+
}
320
+
return 1;
321
+
}
322
+
300
323
static PyObject *
301
324
audioop_getsample(PyObject *self, PyObject *args)
302
325
{
@@ -306,10 +329,8 @@ audioop_getsample(PyObject *self, PyObject *args)
306
329
307
330
if ( !PyArg_ParseTuple(args, "s#in:getsample", &cp, &len, &size, &i) )
308
331
return 0;
309
-
if ( size != 1 && size != 2 && size != 4 ) {
310
-
PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
311
-
return 0;
312
-
}
332
+
if (!audioop_check_parameters(len, size))
333
+
return NULL;
313
334
if ( i < 0 || i >= len/size ) {
314
335
PyErr_SetString(AudioopError, "Index out of range");
315
336
return 0;
@@ -330,10 +351,8 @@ audioop_max(PyObject *self, PyObject *args)
330
351
331
352
if ( !PyArg_ParseTuple(args, "s#i:max", &cp, &len, &size) )
332
353
return 0;
333
-
if ( size != 1 && size != 2 && size != 4 ) {
334
-
PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
335
-
return 0;
336
-
}
354
+
if (!audioop_check_parameters(len, size))
355
+
return NULL;
337
356
for ( i=0; i<len; i+= size) {
338
357
if ( size == 1 ) val = (int)*CHARP(cp, i);
339
358
else if ( size == 2 ) val = (int)*SHORTP(cp, i);
@@ -354,10 +373,8 @@ audioop_minmax(PyObject *self, PyObject *args)
354
373
355
374
if (!PyArg_ParseTuple(args, "s#i:minmax", &cp, &len, &size))
356
375
return NULL;
357
-
if (size != 1 && size != 2 && size != 4) {
358
-
PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
376
+
if (!audioop_check_parameters(len, size))
359
377
return NULL;
360
-
}
361
378
for (i = 0; i < len; i += size) {
362
379
if (size == 1) val = (int) *CHARP(cp, i);
363
380
else if (size == 2) val = (int) *SHORTP(cp, i);
@@ -378,10 +395,8 @@ audioop_avg(PyObject *self, PyObject *args)
378
395
379
396
if ( !PyArg_ParseTuple(args, "s#i:avg", &cp, &len, &size) )
380
397
return 0;
381
-
if ( size != 1 && size != 2 && size != 4 ) {
382
-
PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
383
-
return 0;
384
-
}
398
+
if (!audioop_check_parameters(len, size))
399
+
return NULL;
385
400
for ( i=0; i<len; i+= size) {
386
401
if ( size == 1 ) val = (int)*CHARP(cp, i);
387
402
else if ( size == 2 ) val = (int)*SHORTP(cp, i);
@@ -405,10 +420,8 @@ audioop_rms(PyObject *self, PyObject *args)
405
420
406
421
if ( !PyArg_ParseTuple(args, "s#i:rms", &cp, &len, &size) )
407
422
return 0;
408
-
if ( size != 1 && size != 2 && size != 4 ) {
409
-
PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
410
-
return 0;
411
-
}
423
+
if (!audioop_check_parameters(len, size))
424
+
return NULL;
412
425
for ( i=0; i<len; i+= size) {
413
426
if ( size == 1 ) val = (int)*CHARP(cp, i);
414
427
else if ( size == 2 ) val = (int)*SHORTP(cp, i);
@@ -616,10 +629,8 @@ audioop_avgpp(PyObject *self, PyObject *args)
616
629
617
630
if ( !PyArg_ParseTuple(args, "s#i:avgpp", &cp, &len, &size) )
618
631
return 0;
619
-
if ( size != 1 && size != 2 && size != 4 ) {
620
-
PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
621
-
return 0;
622
-
}
632
+
if (!audioop_check_parameters(len, size))
633
+
return NULL;
623
634
/* Compute first delta value ahead. Also automatically makes us
624
635
** skip the first extreme value
625
636
*/
@@ -673,10 +684,8 @@ audioop_maxpp(PyObject *self, PyObject *args)
673
684
674
685
if ( !PyArg_ParseTuple(args, "s#i:maxpp", &cp, &len, &size) )
675
686
return 0;
676
-
if ( size != 1 && size != 2 && size != 4 ) {
677
-
PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
678
-
return 0;
679
-
}
687
+
if (!audioop_check_parameters(len, size))
688
+
return NULL;
680
689
/* Compute first delta value ahead. Also automatically makes us
681
690
** skip the first extreme value
682
691
*/
@@ -725,10 +734,8 @@ audioop_cross(PyObject *self, PyObject *args)
725
734
726
735
if ( !PyArg_ParseTuple(args, "s#i:cross", &cp, &len, &size) )
727
736
return 0;
728
-
if ( size != 1 && size != 2 && size != 4 ) {
729
-
PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
730
-
return 0;
731
-
}
737
+
if (!audioop_check_parameters(len, size))
738
+
return NULL;
732
739
ncross = -1;
733
740
prevval = 17; /* Anything <> 0,1 */
734
741
for ( i=0; i<len; i+= size) {
@@ -753,6 +760,8 @@ audioop_mul(PyObject *self, PyObject *args)
753
760
754
761
if ( !PyArg_ParseTuple(args, "s#id:mul", &cp, &len, &size, &factor ) )
755
762
return 0;
763
+
if (!audioop_check_parameters(len, size))
764
+
return NULL;
756
765
757
766
if ( size == 1 ) maxval = (double) 0x7f;
758
767
else if ( size == 2 ) maxval = (double) 0x7fff;
@@ -798,6 +807,12 @@ audioop_tomono(PyObject *self, PyObject *args)
798
807
return 0;
799
808
cp = pcp.buf;
800
809
len = pcp.len;
810
+
if (!audioop_check_parameters(len, size))
811
+
return NULL;
812
+
if (((len / size) & 1) != 0) {
813
+
PyErr_SetString(AudioopError, "not a whole number of frames");
814
+
return NULL;
815
+
}
801
816
802
817
if ( size == 1 ) maxval = (double) 0x7f;
803
818
else if ( size == 2 ) maxval = (double) 0x7fff;
@@ -845,6 +860,8 @@ audioop_tostereo(PyObject *self, PyObject *args)
845
860
if ( !PyArg_ParseTuple(args, "s#idd:tostereo",
846
861
&cp, &len, &size, &fac1, &fac2 ) )
847
862
return 0;
863
+
if (!audioop_check_parameters(len, size))
864
+
return NULL;
848
865
849
866
if ( size == 1 ) maxval = (double) 0x7f;
850
867
else if ( size == 2 ) maxval = (double) 0x7fff;
@@ -903,7 +920,8 @@ audioop_add(PyObject *self, PyObject *args)
903
920
if ( !PyArg_ParseTuple(args, "s#s#i:add",
904
921
&cp1, &len1, &cp2, &len2, &size ) )
905
922
return 0;
906
-
923
+
if (!audioop_check_parameters(len1, size))
924
+
return NULL;
907
925
if ( len1 != len2 ) {
908
926
PyErr_SetString(AudioopError, "Lengths should be the same");
909
927
return 0;
@@ -958,10 +976,8 @@ audioop_bias(PyObject *self, PyObject *args)
958
976
&cp, &len, &size , &bias) )
959
977
return 0;
960
978
961
-
if ( size != 1 && size != 2 && size != 4) {
962
-
PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
963
-
return 0;
964
-
}
979
+
if (!audioop_check_parameters(len, size))
980
+
return NULL;
965
981
966
982
rv = PyBytes_FromStringAndSize(NULL, len);
967
983
if ( rv == 0 )
@@ -994,10 +1010,8 @@ audioop_reverse(PyObject *self, PyObject *args)
994
1010
&cp, &len, &size) )
995
1011
return 0;
996
1012
997
-
if ( size != 1 && size != 2 && size != 4 ) {
998
-
PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
999
-
return 0;
1000
-
}
1013
+
if (!audioop_check_parameters(len, size))
1014
+
return NULL;
1001
1015
1002
1016
rv = PyBytes_FromStringAndSize(NULL, len);
1003
1017
if ( rv == 0 )
@@ -1031,11 +1045,10 @@ audioop_lin2lin(PyObject *self, PyObject *args)
1031
1045
&cp, &len, &size, &size2) )
1032
1046
return 0;
1033
1047
1034
-
if ( (size != 1 && size != 2 && size != 4) ||
1035
-
(size2 != 1 && size2 != 2 && size2 != 4)) {
1036
-
PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
1037
-
return 0;
1038
-
}
1048
+
if (!audioop_check_parameters(len, size))
1049
+
return NULL;
1050
+
if (!audioop_check_size(size2))
1051
+
return NULL;
1039
1052
1040
1053
if (len/size > PY_SSIZE_T_MAX/size2) {
1041
1054
PyErr_SetString(PyExc_MemoryError,
@@ -1086,10 +1099,8 @@ audioop_ratecv(PyObject *self, PyObject *args)
1086
1099
&nchannels, &inrate, &outrate, &state,
1087
1100
&weightA, &weightB))
1088
1101
return NULL;
1089
-
if (size != 1 && size != 2 && size != 4) {
1090
-
PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
1102
+
if (!audioop_check_size(size))
1091
1103
return NULL;
1092
-
}
1093
1104
if (nchannels < 1) {
1094
1105
PyErr_SetString(AudioopError, "# of channels should be >= 1");
1095
1106
return NULL;
@@ -1265,10 +1276,8 @@ audioop_lin2ulaw(PyObject *self, PyObject *args)
1265
1276
&cp, &len, &size) )
1266
1277
return 0 ;
1267
1278
1268
-
if ( size != 1 && size != 2 && size != 4) {
1269
-
PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
1270
-
return 0;
1271
-
}
1279
+
if (!audioop_check_parameters(len, size))
1280
+
return NULL;
1272
1281
1273
1282
rv = PyBytes_FromStringAndSize(NULL, len/size);
1274
1283
if ( rv == 0 )
@@ -1299,10 +1308,8 @@ audioop_ulaw2lin(PyObject *self, PyObject *args)
1299
1308
&cp, &len, &size) )
1300
1309
return 0;
1301
1310
1302
-
if ( size != 1 && size != 2 && size != 4) {
1303
-
PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
1304
-
return 0;
1305
-
}
1311
+
if (!audioop_check_parameters(len, size))
1312
+
return NULL;
1306
1313
1307
1314
if (len > PY_SSIZE_T_MAX/size) {
1308
1315
PyErr_SetString(PyExc_MemoryError,
@@ -1338,10 +1345,8 @@ audioop_lin2alaw(PyObject *self, PyObject *args)
1338
1345
&cp, &len, &size) )
1339
1346
return 0;
1340
1347
1341
-
if ( size != 1 && size != 2 && size != 4) {
1342
-
PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
1343
-
return 0;
1344
-
}
1348
+
if (!audioop_check_parameters(len, size))
1349
+
return NULL;
1345
1350
1346
1351
rv = PyBytes_FromStringAndSize(NULL, len/size);
1347
1352
if ( rv == 0 )
@@ -1372,10 +1377,8 @@ audioop_alaw2lin(PyObject *self, PyObject *args)
1372
1377
&cp, &len, &size) )
1373
1378
return 0;
1374
1379
1375
-
if ( size != 1 && size != 2 && size != 4) {
1376
-
PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
1377
-
return 0;
1378
-
}
1380
+
if (!audioop_check_parameters(len, size))
1381
+
return NULL;
1379
1382
1380
1383
if (len > PY_SSIZE_T_MAX/size) {
1381
1384
PyErr_SetString(PyExc_MemoryError,
@@ -1413,11 +1416,8 @@ audioop_lin2adpcm(PyObject *self, PyObject *args)
1413
1416
&cp, &len, &size, &state) )
1414
1417
return 0;
1415
1418
1416
-
1417
-
if ( size != 1 && size != 2 && size != 4) {
1418
-
PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
1419
-
return 0;
1420
-
}
1419
+
if (!audioop_check_parameters(len, size))
1420
+
return NULL;
1421
1421
1422
1422
str = PyBytes_FromStringAndSize(NULL, len/(size*2));
1423
1423
if ( str == 0 )
@@ -1522,10 +1522,8 @@ audioop_adpcm2lin(PyObject *self, PyObject *args)
1522
1522
&cp, &len, &size, &state) )
1523
1523
return 0;
1524
1524
1525
-
if ( size != 1 && size != 2 && size != 4) {
1526
-
PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
1527
-
return 0;
1528
-
}
1525
+
if (!audioop_check_parameters(len, size))
1526
+
return NULL;
1529
1527
1530
1528
/* Decode state, should have (value, step) */
1531
1529
if ( state == Py_None ) {
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4