##// END OF EJS Templates
base85: allow clang-format oversight...
Augie Fackler -
r36244:e1138fc2 default
parent child Browse files
Show More
@@ -1,56 +1,55 b''
1 # Files that just need to be migrated to the formatter.
1 # Files that just need to be migrated to the formatter.
2 # Do not add new files here!
2 # Do not add new files here!
3 mercurial/cext/base85.c
4 mercurial/cext/dirs.c
3 mercurial/cext/dirs.c
5 mercurial/cext/manifest.c
4 mercurial/cext/manifest.c
6 mercurial/cext/mpatch.c
5 mercurial/cext/mpatch.c
7 mercurial/cext/osutil.c
6 mercurial/cext/osutil.c
8 mercurial/cext/revlog.c
7 mercurial/cext/revlog.c
9 # Vendored code that we should never format:
8 # Vendored code that we should never format:
10 contrib/python-zstandard/c-ext/bufferutil.c
9 contrib/python-zstandard/c-ext/bufferutil.c
11 contrib/python-zstandard/c-ext/compressiondict.c
10 contrib/python-zstandard/c-ext/compressiondict.c
12 contrib/python-zstandard/c-ext/compressionparams.c
11 contrib/python-zstandard/c-ext/compressionparams.c
13 contrib/python-zstandard/c-ext/compressionwriter.c
12 contrib/python-zstandard/c-ext/compressionwriter.c
14 contrib/python-zstandard/c-ext/compressobj.c
13 contrib/python-zstandard/c-ext/compressobj.c
15 contrib/python-zstandard/c-ext/compressor.c
14 contrib/python-zstandard/c-ext/compressor.c
16 contrib/python-zstandard/c-ext/compressoriterator.c
15 contrib/python-zstandard/c-ext/compressoriterator.c
17 contrib/python-zstandard/c-ext/constants.c
16 contrib/python-zstandard/c-ext/constants.c
18 contrib/python-zstandard/c-ext/decompressionwriter.c
17 contrib/python-zstandard/c-ext/decompressionwriter.c
19 contrib/python-zstandard/c-ext/decompressobj.c
18 contrib/python-zstandard/c-ext/decompressobj.c
20 contrib/python-zstandard/c-ext/decompressor.c
19 contrib/python-zstandard/c-ext/decompressor.c
21 contrib/python-zstandard/c-ext/decompressoriterator.c
20 contrib/python-zstandard/c-ext/decompressoriterator.c
22 contrib/python-zstandard/c-ext/frameparams.c
21 contrib/python-zstandard/c-ext/frameparams.c
23 contrib/python-zstandard/c-ext/python-zstandard.h
22 contrib/python-zstandard/c-ext/python-zstandard.h
24 contrib/python-zstandard/zstd.c
23 contrib/python-zstandard/zstd.c
25 contrib/python-zstandard/zstd/common/bitstream.h
24 contrib/python-zstandard/zstd/common/bitstream.h
26 contrib/python-zstandard/zstd/common/entropy_common.c
25 contrib/python-zstandard/zstd/common/entropy_common.c
27 contrib/python-zstandard/zstd/common/error_private.c
26 contrib/python-zstandard/zstd/common/error_private.c
28 contrib/python-zstandard/zstd/common/error_private.h
27 contrib/python-zstandard/zstd/common/error_private.h
29 contrib/python-zstandard/zstd/common/fse.h
28 contrib/python-zstandard/zstd/common/fse.h
30 contrib/python-zstandard/zstd/common/fse_decompress.c
29 contrib/python-zstandard/zstd/common/fse_decompress.c
31 contrib/python-zstandard/zstd/common/huf.h
30 contrib/python-zstandard/zstd/common/huf.h
32 contrib/python-zstandard/zstd/common/mem.h
31 contrib/python-zstandard/zstd/common/mem.h
33 contrib/python-zstandard/zstd/common/pool.c
32 contrib/python-zstandard/zstd/common/pool.c
34 contrib/python-zstandard/zstd/common/pool.h
33 contrib/python-zstandard/zstd/common/pool.h
35 contrib/python-zstandard/zstd/common/threading.c
34 contrib/python-zstandard/zstd/common/threading.c
36 contrib/python-zstandard/zstd/common/threading.h
35 contrib/python-zstandard/zstd/common/threading.h
37 contrib/python-zstandard/zstd/common/xxhash.c
36 contrib/python-zstandard/zstd/common/xxhash.c
38 contrib/python-zstandard/zstd/common/xxhash.h
37 contrib/python-zstandard/zstd/common/xxhash.h
39 contrib/python-zstandard/zstd/common/zstd_common.c
38 contrib/python-zstandard/zstd/common/zstd_common.c
40 contrib/python-zstandard/zstd/common/zstd_errors.h
39 contrib/python-zstandard/zstd/common/zstd_errors.h
41 contrib/python-zstandard/zstd/common/zstd_internal.h
40 contrib/python-zstandard/zstd/common/zstd_internal.h
42 contrib/python-zstandard/zstd/compress/fse_compress.c
41 contrib/python-zstandard/zstd/compress/fse_compress.c
43 contrib/python-zstandard/zstd/compress/huf_compress.c
42 contrib/python-zstandard/zstd/compress/huf_compress.c
44 contrib/python-zstandard/zstd/compress/zstd_compress.c
43 contrib/python-zstandard/zstd/compress/zstd_compress.c
45 contrib/python-zstandard/zstd/compress/zstd_opt.h
44 contrib/python-zstandard/zstd/compress/zstd_opt.h
46 contrib/python-zstandard/zstd/compress/zstdmt_compress.c
45 contrib/python-zstandard/zstd/compress/zstdmt_compress.c
47 contrib/python-zstandard/zstd/compress/zstdmt_compress.h
46 contrib/python-zstandard/zstd/compress/zstdmt_compress.h
48 contrib/python-zstandard/zstd/decompress/huf_decompress.c
47 contrib/python-zstandard/zstd/decompress/huf_decompress.c
49 contrib/python-zstandard/zstd/decompress/zstd_decompress.c
48 contrib/python-zstandard/zstd/decompress/zstd_decompress.c
50 contrib/python-zstandard/zstd/dictBuilder/cover.c
49 contrib/python-zstandard/zstd/dictBuilder/cover.c
51 contrib/python-zstandard/zstd/dictBuilder/divsufsort.c
50 contrib/python-zstandard/zstd/dictBuilder/divsufsort.c
52 contrib/python-zstandard/zstd/dictBuilder/divsufsort.h
51 contrib/python-zstandard/zstd/dictBuilder/divsufsort.h
53 contrib/python-zstandard/zstd/dictBuilder/zdict.c
52 contrib/python-zstandard/zstd/dictBuilder/zdict.c
54 contrib/python-zstandard/zstd/dictBuilder/zdict.h
53 contrib/python-zstandard/zstd/dictBuilder/zdict.h
55 contrib/python-zstandard/zstd/zstd.h
54 contrib/python-zstandard/zstd/zstd.h
56 hgext/fsmonitor/pywatchman/bser.c
55 hgext/fsmonitor/pywatchman/bser.c
@@ -1,185 +1,182 b''
1 /*
1 /*
2 base85 codec
2 base85 codec
3
3
4 Copyright 2006 Brendan Cully <brendan@kublai.com>
4 Copyright 2006 Brendan Cully <brendan@kublai.com>
5
5
6 This software may be used and distributed according to the terms of
6 This software may be used and distributed according to the terms of
7 the GNU General Public License, incorporated herein by reference.
7 the GNU General Public License, incorporated herein by reference.
8
8
9 Largely based on git's implementation
9 Largely based on git's implementation
10 */
10 */
11
11
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;
24
25
25 memset(b85dec, 0, sizeof(b85dec));
26 memset(b85dec, 0, sizeof(b85dec));
26 for (i = 0; i < sizeof(b85chars); i++)
27 for (i = 0; i < sizeof(b85chars); i++)
27 b85dec[(int)(b85chars[i])] = i + 1;
28 b85dec[(int)(b85chars[i])] = i + 1;
28 }
29 }
29
30
30 static PyObject *b85encode(PyObject *self, PyObject *args)
31 static PyObject *b85encode(PyObject *self, PyObject *args)
31 {
32 {
32 const unsigned char *text;
33 const unsigned char *text;
33 PyObject *out;
34 PyObject *out;
34 char *dst;
35 char *dst;
35 Py_ssize_t len, olen, i;
36 Py_ssize_t len, olen, i;
36 unsigned int acc, val, ch;
37 unsigned int acc, val, ch;
37 int pad = 0;
38 int pad = 0;
38
39
39 if (!PyArg_ParseTuple(args, "s#|i", &text, &len, &pad))
40 if (!PyArg_ParseTuple(args, "s#|i", &text, &len, &pad))
40 return NULL;
41 return NULL;
41
42
42 if (pad)
43 if (pad)
43 olen = ((len + 3) / 4 * 5) - 3;
44 olen = ((len + 3) / 4 * 5) - 3;
44 else {
45 else {
45 olen = len % 4;
46 olen = len % 4;
46 if (olen)
47 if (olen)
47 olen++;
48 olen++;
48 olen += len / 4 * 5;
49 olen += len / 4 * 5;
49 }
50 }
50 if (!(out = PyBytes_FromStringAndSize(NULL, olen + 3)))
51 if (!(out = PyBytes_FromStringAndSize(NULL, olen + 3)))
51 return NULL;
52 return NULL;
52
53
53 dst = PyBytes_AsString(out);
54 dst = PyBytes_AsString(out);
54
55
55 while (len) {
56 while (len) {
56 acc = 0;
57 acc = 0;
57 for (i = 24; i >= 0; i -= 8) {
58 for (i = 24; i >= 0; i -= 8) {
58 ch = *text++;
59 ch = *text++;
59 acc |= ch << i;
60 acc |= ch << i;
60 if (--len == 0)
61 if (--len == 0)
61 break;
62 break;
62 }
63 }
63 for (i = 4; i >= 0; i--) {
64 for (i = 4; i >= 0; i--) {
64 val = acc % 85;
65 val = acc % 85;
65 acc /= 85;
66 acc /= 85;
66 dst[i] = b85chars[val];
67 dst[i] = b85chars[val];
67 }
68 }
68 dst += 5;
69 dst += 5;
69 }
70 }
70
71
71 if (!pad)
72 if (!pad)
72 _PyBytes_Resize(&out, olen);
73 _PyBytes_Resize(&out, olen);
73
74
74 return out;
75 return out;
75 }
76 }
76
77
77 static PyObject *b85decode(PyObject *self, PyObject *args)
78 static PyObject *b85decode(PyObject *self, PyObject *args)
78 {
79 {
79 PyObject *out;
80 PyObject *out;
80 const char *text;
81 const char *text;
81 char *dst;
82 char *dst;
82 Py_ssize_t len, i, j, olen, cap;
83 Py_ssize_t len, i, j, olen, cap;
83 int c;
84 int c;
84 unsigned int acc;
85 unsigned int acc;
85
86
86 if (!PyArg_ParseTuple(args, "s#", &text, &len))
87 if (!PyArg_ParseTuple(args, "s#", &text, &len))
87 return NULL;
88 return NULL;
88
89
89 olen = len / 5 * 4;
90 olen = len / 5 * 4;
90 i = len % 5;
91 i = len % 5;
91 if (i)
92 if (i)
92 olen += i - 1;
93 olen += i - 1;
93 if (!(out = PyBytes_FromStringAndSize(NULL, olen)))
94 if (!(out = PyBytes_FromStringAndSize(NULL, olen)))
94 return NULL;
95 return NULL;
95
96
96 dst = PyBytes_AsString(out);
97 dst = PyBytes_AsString(out);
97
98
98 i = 0;
99 i = 0;
99 while (i < len) {
100 while (i < len) {
100 acc = 0;
101 acc = 0;
101 cap = len - i - 1;
102 cap = len - i - 1;
102 if (cap > 4)
103 if (cap > 4)
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;
132 for (j = 0; j < 4 - cap; j++)
133 for (j = 0; j < 4 - cap; j++)
133 acc *= 85;
134 acc *= 85;
134 if (cap && cap < 4)
135 if (cap && cap < 4)
135 acc += 0xffffff >> (cap - 1) * 8;
136 acc += 0xffffff >> (cap - 1) * 8;
136 for (j = 0; j < cap; j++) {
137 for (j = 0; j < cap; j++) {
137 acc = (acc << 8) | (acc >> 24);
138 acc = (acc << 8) | (acc >> 24);
138 *dst++ = acc;
139 *dst++ = acc;
139 }
140 }
140 }
141 }
141
142
142 return out;
143 return out;
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;
170 b85prep();
167 b85prep();
171
168
172 m = PyModule_Create(&base85_module);
169 m = PyModule_Create(&base85_module);
173 PyModule_AddIntConstant(m, "version", version);
170 PyModule_AddIntConstant(m, "version", version);
174 return m;
171 return m;
175 }
172 }
176 #else
173 #else
177 PyMODINIT_FUNC initbase85(void)
174 PyMODINIT_FUNC initbase85(void)
178 {
175 {
179 PyObject *m;
176 PyObject *m;
180 m = Py_InitModule3("base85", methods, base85_doc);
177 m = Py_InitModule3("base85", methods, base85_doc);
181
178
182 b85prep();
179 b85prep();
183 PyModule_AddIntConstant(m, "version", version);
180 PyModule_AddIntConstant(m, "version", version);
184 }
181 }
185 #endif
182 #endif
General Comments 0
You need to be logged in to leave comments. Login now