comparison mercurial/cext/base85.c @ 36226:e1138fc2c4e2

base85: allow clang-format oversight Differential Revision: https://phab.mercurial-scm.org/D2182
author Augie Fackler <augie@google.com>
date Mon, 12 Feb 2018 10:39:46 -0500
parents ce26a13869fb
children 186c6df3a373
comparison
equal deleted inserted replaced
36225:6c87d4113a90 36226:e1138fc2c4e2
12 #define PY_SSIZE_T_CLEAN 12 #define PY_SSIZE_T_CLEAN
13 #include <Python.h> 13 #include <Python.h>
14 14
15 #include "util.h" 15 #include "util.h"
16 16
17 static const char b85chars[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" 17 static const char b85chars[] =
18 "abcdefghijklmnopqrstuvwxyz!#$%&()*+-;<=>?@^_`{|}~"; 18 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
19 "abcdefghijklmnopqrstuvwxyz!#$%&()*+-;<=>?@^_`{|}~";
19 static char b85dec[256]; 20 static char b85dec[256];
20 21
21 static void b85prep(void) 22 static void b85prep(void)
22 { 23 {
23 unsigned i; 24 unsigned i;
103 cap = 4; 104 cap = 4;
104 for (j = 0; j < cap; i++, j++) { 105 for (j = 0; j < cap; i++, j++) {
105 c = b85dec[(int)*text++] - 1; 106 c = b85dec[(int)*text++] - 1;
106 if (c < 0) 107 if (c < 0)
107 return PyErr_Format( 108 return PyErr_Format(
108 PyExc_ValueError, 109 PyExc_ValueError,
109 "bad base85 character at position %d", 110 "bad base85 character at position %d",
110 (int)i); 111 (int)i);
111 acc = acc * 85 + c; 112 acc = acc * 85 + c;
112 } 113 }
113 if (i++ < len) { 114 if (i++ < len) {
114 c = b85dec[(int)*text++] - 1; 115 c = b85dec[(int)*text++] - 1;
115 if (c < 0) 116 if (c < 0)
116 return PyErr_Format( 117 return PyErr_Format(
117 PyExc_ValueError, 118 PyExc_ValueError,
118 "bad base85 character at position %d", 119 "bad base85 character at position %d",
119 (int)i); 120 (int)i);
120 /* overflow detection: 0xffffffff == "|NsC0", 121 /* overflow detection: 0xffffffff == "|NsC0",
121 * "|NsC" == 0x03030303 */ 122 * "|NsC" == 0x03030303 */
122 if (acc > 0x03030303 || (acc *= 85) > 0xffffffff - c) 123 if (acc > 0x03030303 || (acc *= 85) > 0xffffffff - c)
123 return PyErr_Format( 124 return PyErr_Format(
124 PyExc_ValueError, 125 PyExc_ValueError,
125 "bad base85 sequence at position %d", 126 "bad base85 sequence at position %d",
126 (int)i); 127 (int)i);
127 acc += c; 128 acc += c;
128 } 129 }
129 130
130 cap = olen < 4 ? olen : 4; 131 cap = olen < 4 ? olen : 4;
131 olen -= cap; 132 olen -= cap;
143 } 144 }
144 145
145 static char base85_doc[] = "Base85 Data Encoding"; 146 static char base85_doc[] = "Base85 Data Encoding";
146 147
147 static PyMethodDef methods[] = { 148 static PyMethodDef methods[] = {
148 {"b85encode", b85encode, METH_VARARGS, 149 {"b85encode", b85encode, METH_VARARGS,
149 "Encode text in base85.\n\n" 150 "Encode text in base85.\n\n"
150 "If the second parameter is true, pad the result to a multiple of " 151 "If the second parameter is true, pad the result to a multiple of "
151 "five characters.\n"}, 152 "five characters.\n"},
152 {"b85decode", b85decode, METH_VARARGS, "Decode base85 text.\n"}, 153 {"b85decode", b85decode, METH_VARARGS, "Decode base85 text.\n"},
153 {NULL, NULL} 154 {NULL, NULL},
154 }; 155 };
155 156
156 static const int version = 1; 157 static const int version = 1;
157 158
158 #ifdef IS_PY3K 159 #ifdef IS_PY3K
159 static struct PyModuleDef base85_module = { 160 static struct PyModuleDef base85_module = {
160 PyModuleDef_HEAD_INIT, 161 PyModuleDef_HEAD_INIT, "base85", base85_doc, -1, methods,
161 "base85",
162 base85_doc,
163 -1,
164 methods
165 }; 162 };
166 163
167 PyMODINIT_FUNC PyInit_base85(void) 164 PyMODINIT_FUNC PyInit_base85(void)
168 { 165 {
169 PyObject *m; 166 PyObject *m;