Skip to content

Commit 1e928e3

Browse files
committed
Update C style to more closely adhere to PEP-7
1 parent 7885918 commit 1e928e3

File tree

1 file changed

+77
-66
lines changed

1 file changed

+77
-66
lines changed

Modules/binascii.c

Lines changed: 77 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -713,24 +713,18 @@ binascii_a2b_ascii85_impl(PyObject *module, Py_buffer *data, int fold_spaces,
713713
int wrap, Py_buffer *ignore)
714714
/*[clinic end generated code: output=6ab30f2a26d301a1 input=11c60c016d4f334b]*/
715715
{
716-
const unsigned char *ascii_data, *ignore_data;
717-
int group_pos = 0;
718-
unsigned char this_ch, this_digit;
719-
unsigned char ignore_map[256] = {0};
720-
uint32_t leftchar = 0;
721-
Py_ssize_t ascii_len, bin_len, chunk_len, ignore_len;
722-
binascii_state *state;
723-
724-
ascii_data = data->buf;
725-
ascii_len = data->len;
716+
const unsigned char *ascii_data = data->buf;
717+
Py_ssize_t ascii_len = data->len;
718+
binascii_state *state = NULL;
726719

727720
assert(ascii_len >= 0);
728721

729722
/* Consume Ascii85 prefix and suffix if present. */
730723
if (wrap) {
731-
if (ascii_len < 2 ||
732-
ascii_data[ascii_len - 2] != BASE85_A85_AFFIX ||
733-
ascii_data[ascii_len - 1] != BASE85_A85_SUFFIX) {
724+
if (ascii_len < 2
725+
|| ascii_data[ascii_len - 2] != BASE85_A85_AFFIX
726+
|| ascii_data[ascii_len - 1] != BASE85_A85_SUFFIX)
727+
{
734728
state = get_binascii_state(module);
735729
if (state != NULL) {
736730
PyErr_SetString(state->Error,
@@ -739,16 +733,17 @@ binascii_a2b_ascii85_impl(PyObject *module, Py_buffer *data, int fold_spaces,
739733
return NULL;
740734
}
741735
ascii_len -= 2;
742-
if (ascii_len >= 2 &&
743-
ascii_data[0] == BASE85_A85_PREFIX &&
744-
ascii_data[1] == BASE85_A85_AFFIX) {
736+
if (ascii_len >= 2
737+
&& ascii_data[0] == BASE85_A85_PREFIX
738+
&& ascii_data[1] == BASE85_A85_AFFIX) {
745739
ascii_data += 2;
746740
ascii_len -= 2;
747741
}
748742
}
749743

750744
/* Allocate output buffer. */
751-
bin_len = ascii_len;
745+
Py_ssize_t bin_len = ascii_len;
746+
unsigned char this_ch = 0;
752747
for (Py_ssize_t i = 0; i < ascii_len; i++) {
753748
this_ch = ascii_data[i];
754749
if (this_ch == 'y' || this_ch == 'z') {
@@ -767,53 +762,61 @@ binascii_a2b_ascii85_impl(PyObject *module, Py_buffer *data, int fold_spaces,
767762
}
768763

769764
/* Build ignore map. */
765+
unsigned char ignore_map[256] = {0};
770766
if (ignore->obj != NULL) {
771-
ignore_data = ignore->buf;
772-
ignore_len = ignore->len;
767+
const unsigned char *ignore_data = ignore->buf;
768+
Py_ssize_t ignore_len = ignore->len;
773769
for (Py_ssize_t i = 0; i < ignore_len; i++) {
774770
this_ch = ignore_data[i];
775771
ignore_map[this_ch] = -1;
776772
}
777773
}
778774

775+
uint32_t leftchar = 0;
776+
int group_pos = 0;
779777
for (; ascii_len > 0 || group_pos != 0; ascii_len--, ascii_data++) {
780778
/* Shift (in radix-85) data or padding into our buffer. */
779+
unsigned char this_digit;
781780
if (ascii_len > 0) {
782781
this_ch = *ascii_data;
783782
this_digit = table_a2b_base85_a85[this_ch];
784-
} else {
783+
}
784+
else {
785785
/* Pad with largest radix-85 digit when decoding. */
786786
this_digit = 84;
787787
}
788788
if (this_digit < 85) {
789-
if (leftchar > UINT32_MAX / 85 ||
790-
(leftchar *= 85) > UINT32_MAX - this_digit) {
789+
if (leftchar > UINT32_MAX / 85
790+
|| (leftchar *= 85) > UINT32_MAX - this_digit)
791+
{
791792
state = get_binascii_state(module);
792793
if (state != NULL) {
793794
PyErr_SetString(state->Error, "Ascii85 overflow");
794795
}
795-
goto error_end;
796+
goto error;
796797
}
797798
leftchar += this_digit;
798799
group_pos++;
799-
} else if ((this_ch == 'y' && fold_spaces) || this_ch == 'z') {
800+
}
801+
else if ((this_ch == 'y' && fold_spaces) || this_ch == 'z') {
800802
if (group_pos != 0) {
801803
state = get_binascii_state(module);
802804
if (state != NULL) {
803805
PyErr_Format(state->Error,
804806
"'%c' inside Ascii85 5-tuple", this_ch);
805807
}
806-
goto error_end;
808+
goto error;
807809
}
808810
leftchar = this_ch == 'y' ? BASE85_A85_Y : BASE85_A85_Z;
809811
group_pos = 5;
810-
} else if (!ignore_map[this_ch]) {
812+
}
813+
else if (!ignore_map[this_ch]) {
811814
state = get_binascii_state(module);
812815
if (state != NULL) {
813816
PyErr_Format(state->Error,
814817
"Non-Ascii85 digit found: %c", this_ch);
815818
}
816-
goto error_end;
819+
goto error;
817820
}
818821

819822
/* Wait until buffer is full. */
@@ -822,7 +825,7 @@ binascii_a2b_ascii85_impl(PyObject *module, Py_buffer *data, int fold_spaces,
822825
}
823826

824827
/* Write current chunk. */
825-
chunk_len = ascii_len < 1 ? 3 + ascii_len : 4;
828+
Py_ssize_t chunk_len = ascii_len < 1 ? 3 + ascii_len : 4;
826829
for (Py_ssize_t i = 0; i < chunk_len; i++) {
827830
*bin_data++ = (leftchar >> (24 - 8 * i)) & 0xff;
828831
}
@@ -833,7 +836,7 @@ binascii_a2b_ascii85_impl(PyObject *module, Py_buffer *data, int fold_spaces,
833836

834837
return PyBytesWriter_FinishWithPointer(writer, bin_data);
835838

836-
error_end:
839+
error:
837840
PyBytesWriter_Discard(writer);
838841
return NULL;
839842
}
@@ -874,9 +877,15 @@ binascii_b2a_ascii85_impl(PyObject *module, Py_buffer *data, int fold_spaces,
874877
XXX: Do a pre-pass above some threshold estimate (cf. 'yz')?
875878
*/
876879
Py_ssize_t out_len = 5 * ((bin_len + 3) / 4);
877-
if (wrap) out_len += 4;
878-
if (!pad && (bin_len % 4)) out_len -= 4 - (bin_len % 4);
879-
if (width && out_len) out_len += (out_len - 1) / width;
880+
if (wrap) {
881+
out_len += 4;
882+
}
883+
if (!pad && (bin_len % 4)) {
884+
out_len -= 4 - (bin_len % 4);
885+
}
886+
if (width && out_len) {
887+
out_len += (out_len - 1) / width;
888+
}
880889

881890
PyBytesWriter *writer = PyBytesWriter_Create(out_len);
882891
if (writer == NULL) {
@@ -978,58 +987,58 @@ binascii_a2b_base85_impl(PyObject *module, Py_buffer *data, int strict_mode,
978987
int z85)
979988
/*[clinic end generated code: output=c5b9118ffe77f1cb input=65c2a532ad64ebd5]*/
980989
{
981-
const unsigned char *ascii_data, *table_a2b;
982-
int group_pos = 0;
983-
unsigned char this_ch, this_digit;
984-
uint32_t leftchar = 0;
985-
Py_ssize_t ascii_len, bin_len, chunk_len;
986-
binascii_state *state;
987-
988-
table_a2b = z85 ? table_a2b_base85_z85 : table_a2b_base85;
989-
ascii_data = data->buf;
990-
ascii_len = data->len;
990+
const unsigned char *ascii_data = data->buf;
991+
Py_ssize_t ascii_len = data->len;
992+
binascii_state *state = NULL;
991993

992994
assert(ascii_len >= 0);
993995

994996
/* Allocate output buffer. */
995-
bin_len = 4 * ((ascii_len + 4) / 5);
996-
997+
Py_ssize_t bin_len = 4 * ((ascii_len + 4) / 5);
997998
PyBytesWriter *writer = PyBytesWriter_Create(bin_len);
998999
if (writer == NULL) {
9991000
return NULL;
10001001
}
10011002
unsigned char *bin_data = PyBytesWriter_GetData(writer);
10021003

1004+
const unsigned char *table_a2b = z85 ? table_a2b_base85_z85
1005+
: table_a2b_base85;
1006+
uint32_t leftchar = 0;
1007+
int group_pos = 0;
10031008
for (; ascii_len > 0 || group_pos != 0; ascii_len--, ascii_data++) {
10041009
/* Shift (in radix-85) data or padding into our buffer. */
1010+
unsigned char this_digit;
10051011
if (ascii_len > 0) {
1006-
this_ch = *ascii_data;
1012+
unsigned char this_ch = *ascii_data;
10071013
this_digit = table_a2b[this_ch];
1008-
} else {
1014+
}
1015+
else {
10091016
/* Pad with largest radix-85 digit when decoding. */
10101017
this_digit = 84;
10111018
}
10121019
if (this_digit < 85) {
1013-
if (leftchar > UINT32_MAX / 85 ||
1014-
(leftchar *= 85) > UINT32_MAX - this_digit) {
1020+
if (leftchar > UINT32_MAX / 85
1021+
|| (leftchar *= 85) > UINT32_MAX - this_digit)
1022+
{
10151023
state = get_binascii_state(module);
10161024
if (state != NULL) {
10171025
PyErr_Format(state->Error,
10181026
"%s overflow in hunk starting at byte %d",
10191027
z85 ? "z85" : "base85",
10201028
(data->len - ascii_len) / 5 * 5);
10211029
}
1022-
goto error_end;
1030+
goto error;
10231031
}
10241032
leftchar += this_digit;
10251033
group_pos++;
1026-
} else if (strict_mode) {
1034+
}
1035+
else if (strict_mode) {
10271036
state = get_binascii_state(module);
10281037
if (state != NULL) {
10291038
PyErr_Format(state->Error, "bad %s character at position %d",
10301039
z85 ? "z85" : "base85", data->len - ascii_len);
10311040
}
1032-
goto error_end;
1041+
goto error;
10331042
}
10341043

10351044
/* Wait until buffer is full. */
@@ -1038,7 +1047,7 @@ binascii_a2b_base85_impl(PyObject *module, Py_buffer *data, int strict_mode,
10381047
}
10391048

10401049
/* Write current chunk. */
1041-
chunk_len = ascii_len < 1 ? 3 + ascii_len : 4;
1050+
Py_ssize_t chunk_len = ascii_len < 1 ? 3 + ascii_len : 4;
10421051
for (Py_ssize_t i = 0; i < chunk_len; i++) {
10431052
*bin_data++ = (leftchar >> (24 - 8 * i)) & 0xff;
10441053
}
@@ -1049,7 +1058,7 @@ binascii_a2b_base85_impl(PyObject *module, Py_buffer *data, int strict_mode,
10491058

10501059
return PyBytesWriter_FinishWithPointer(writer, bin_data);
10511060

1052-
error_end:
1061+
error:
10531062
PyBytesWriter_Discard(writer);
10541063
return NULL;
10551064
}
@@ -1075,20 +1084,19 @@ binascii_b2a_base85_impl(PyObject *module, Py_buffer *data, int pad,
10751084
int newline, int z85)
10761085
/*[clinic end generated code: output=d3740e9a20c8e071 input=e4e07591f7a11ae4]*/
10771086
{
1078-
const unsigned char *bin_data, *table_b2a;
1079-
uint32_t leftchar = 0;
1080-
Py_ssize_t bin_len, group_len, out_len;
1081-
1082-
table_b2a = z85 ? table_b2a_base85_z85 : table_b2a_base85;
1083-
bin_data = data->buf;
1084-
bin_len = data->len;
1087+
const unsigned char *bin_data = data->buf;
1088+
Py_ssize_t bin_len = data->len;
10851089

10861090
assert(bin_len >= 0);
10871091

10881092
/* Allocate output buffer. */
1089-
out_len = 5 * ((bin_len + 3) / 4);
1090-
if (!pad && (bin_len % 4)) out_len -= 4 - (bin_len % 4);
1091-
if (newline) out_len++;
1093+
Py_ssize_t out_len = 5 * ((bin_len + 3) / 4);
1094+
if (!pad && (bin_len % 4)) {
1095+
out_len -= 4 - (bin_len % 4);
1096+
}
1097+
if (newline) {
1098+
out_len++;
1099+
}
10921100

10931101
PyBytesWriter *writer = PyBytesWriter_Create(out_len);
10941102
if (writer == NULL) {
@@ -1097,9 +1105,11 @@ binascii_b2a_base85_impl(PyObject *module, Py_buffer *data, int pad,
10971105
unsigned char *ascii_data = PyBytesWriter_GetData(writer);
10981106

10991107
/* Encode all full-length chunks. */
1108+
const unsigned char *table_b2a = z85 ? table_b2a_base85_z85
1109+
: table_b2a_base85;
11001110
for (; bin_len >= 4; bin_len -= 4, bin_data += 4) {
1101-
leftchar = (bin_data[0] << 24) | (bin_data[1] << 16) |
1102-
(bin_data[2] << 8) | bin_data[3];
1111+
uint32_t leftchar = (bin_data[0] << 24) | (bin_data[1] << 16) |
1112+
(bin_data[2] << 8) | bin_data[3];
11031113

11041114
ascii_data[4] = table_b2a[leftchar % 85];
11051115
leftchar /= 85;
@@ -1116,13 +1126,14 @@ binascii_b2a_base85_impl(PyObject *module, Py_buffer *data, int pad,
11161126

11171127
/* Encode partial-length final chunk. */
11181128
if (bin_len > 0) {
1129+
uint32_t leftchar = 0;
11191130
for (Py_ssize_t i = 0; i < 4; i++) {
11201131
leftchar <<= 8; /* Pad with zero when encoding. */
11211132
if (i < bin_len) {
11221133
leftchar |= *bin_data++;
11231134
}
11241135
}
1125-
group_len = pad ? 5 : bin_len + 1;
1136+
Py_ssize_t group_len = pad ? 5 : bin_len + 1;
11261137
for (Py_ssize_t i = 4; i >= 0; i--) {
11271138
if (i < group_len) {
11281139
ascii_data[i] = table_b2a[leftchar % 85];

0 commit comments

Comments
 (0)