##// 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 1 # Files that just need to be migrated to the formatter.
2 2 # Do not add new files here!
3 mercurial/cext/base85.c
4 3 mercurial/cext/dirs.c
5 4 mercurial/cext/manifest.c
6 5 mercurial/cext/mpatch.c
7 6 mercurial/cext/osutil.c
8 7 mercurial/cext/revlog.c
9 8 # Vendored code that we should never format:
10 9 contrib/python-zstandard/c-ext/bufferutil.c
11 10 contrib/python-zstandard/c-ext/compressiondict.c
12 11 contrib/python-zstandard/c-ext/compressionparams.c
13 12 contrib/python-zstandard/c-ext/compressionwriter.c
14 13 contrib/python-zstandard/c-ext/compressobj.c
15 14 contrib/python-zstandard/c-ext/compressor.c
16 15 contrib/python-zstandard/c-ext/compressoriterator.c
17 16 contrib/python-zstandard/c-ext/constants.c
18 17 contrib/python-zstandard/c-ext/decompressionwriter.c
19 18 contrib/python-zstandard/c-ext/decompressobj.c
20 19 contrib/python-zstandard/c-ext/decompressor.c
21 20 contrib/python-zstandard/c-ext/decompressoriterator.c
22 21 contrib/python-zstandard/c-ext/frameparams.c
23 22 contrib/python-zstandard/c-ext/python-zstandard.h
24 23 contrib/python-zstandard/zstd.c
25 24 contrib/python-zstandard/zstd/common/bitstream.h
26 25 contrib/python-zstandard/zstd/common/entropy_common.c
27 26 contrib/python-zstandard/zstd/common/error_private.c
28 27 contrib/python-zstandard/zstd/common/error_private.h
29 28 contrib/python-zstandard/zstd/common/fse.h
30 29 contrib/python-zstandard/zstd/common/fse_decompress.c
31 30 contrib/python-zstandard/zstd/common/huf.h
32 31 contrib/python-zstandard/zstd/common/mem.h
33 32 contrib/python-zstandard/zstd/common/pool.c
34 33 contrib/python-zstandard/zstd/common/pool.h
35 34 contrib/python-zstandard/zstd/common/threading.c
36 35 contrib/python-zstandard/zstd/common/threading.h
37 36 contrib/python-zstandard/zstd/common/xxhash.c
38 37 contrib/python-zstandard/zstd/common/xxhash.h
39 38 contrib/python-zstandard/zstd/common/zstd_common.c
40 39 contrib/python-zstandard/zstd/common/zstd_errors.h
41 40 contrib/python-zstandard/zstd/common/zstd_internal.h
42 41 contrib/python-zstandard/zstd/compress/fse_compress.c
43 42 contrib/python-zstandard/zstd/compress/huf_compress.c
44 43 contrib/python-zstandard/zstd/compress/zstd_compress.c
45 44 contrib/python-zstandard/zstd/compress/zstd_opt.h
46 45 contrib/python-zstandard/zstd/compress/zstdmt_compress.c
47 46 contrib/python-zstandard/zstd/compress/zstdmt_compress.h
48 47 contrib/python-zstandard/zstd/decompress/huf_decompress.c
49 48 contrib/python-zstandard/zstd/decompress/zstd_decompress.c
50 49 contrib/python-zstandard/zstd/dictBuilder/cover.c
51 50 contrib/python-zstandard/zstd/dictBuilder/divsufsort.c
52 51 contrib/python-zstandard/zstd/dictBuilder/divsufsort.h
53 52 contrib/python-zstandard/zstd/dictBuilder/zdict.c
54 53 contrib/python-zstandard/zstd/dictBuilder/zdict.h
55 54 contrib/python-zstandard/zstd/zstd.h
56 55 hgext/fsmonitor/pywatchman/bser.c
@@ -1,185 +1,182 b''
1 1 /*
2 2 base85 codec
3 3
4 4 Copyright 2006 Brendan Cully <brendan@kublai.com>
5 5
6 6 This software may be used and distributed according to the terms of
7 7 the GNU General Public License, incorporated herein by reference.
8 8
9 9 Largely based on git's implementation
10 10 */
11 11
12 12 #define PY_SSIZE_T_CLEAN
13 13 #include <Python.h>
14 14
15 15 #include "util.h"
16 16
17 static const char b85chars[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
18 "abcdefghijklmnopqrstuvwxyz!#$%&()*+-;<=>?@^_`{|}~";
17 static const char b85chars[] =
18 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
19 "abcdefghijklmnopqrstuvwxyz!#$%&()*+-;<=>?@^_`{|}~";
19 20 static char b85dec[256];
20 21
21 22 static void b85prep(void)
22 23 {
23 24 unsigned i;
24 25
25 26 memset(b85dec, 0, sizeof(b85dec));
26 27 for (i = 0; i < sizeof(b85chars); i++)
27 28 b85dec[(int)(b85chars[i])] = i + 1;
28 29 }
29 30
30 31 static PyObject *b85encode(PyObject *self, PyObject *args)
31 32 {
32 33 const unsigned char *text;
33 34 PyObject *out;
34 35 char *dst;
35 36 Py_ssize_t len, olen, i;
36 37 unsigned int acc, val, ch;
37 38 int pad = 0;
38 39
39 40 if (!PyArg_ParseTuple(args, "s#|i", &text, &len, &pad))
40 41 return NULL;
41 42
42 43 if (pad)
43 44 olen = ((len + 3) / 4 * 5) - 3;
44 45 else {
45 46 olen = len % 4;
46 47 if (olen)
47 48 olen++;
48 49 olen += len / 4 * 5;
49 50 }
50 51 if (!(out = PyBytes_FromStringAndSize(NULL, olen + 3)))
51 52 return NULL;
52 53
53 54 dst = PyBytes_AsString(out);
54 55
55 56 while (len) {
56 57 acc = 0;
57 58 for (i = 24; i >= 0; i -= 8) {
58 59 ch = *text++;
59 60 acc |= ch << i;
60 61 if (--len == 0)
61 62 break;
62 63 }
63 64 for (i = 4; i >= 0; i--) {
64 65 val = acc % 85;
65 66 acc /= 85;
66 67 dst[i] = b85chars[val];
67 68 }
68 69 dst += 5;
69 70 }
70 71
71 72 if (!pad)
72 73 _PyBytes_Resize(&out, olen);
73 74
74 75 return out;
75 76 }
76 77
77 78 static PyObject *b85decode(PyObject *self, PyObject *args)
78 79 {
79 80 PyObject *out;
80 81 const char *text;
81 82 char *dst;
82 83 Py_ssize_t len, i, j, olen, cap;
83 84 int c;
84 85 unsigned int acc;
85 86
86 87 if (!PyArg_ParseTuple(args, "s#", &text, &len))
87 88 return NULL;
88 89
89 90 olen = len / 5 * 4;
90 91 i = len % 5;
91 92 if (i)
92 93 olen += i - 1;
93 94 if (!(out = PyBytes_FromStringAndSize(NULL, olen)))
94 95 return NULL;
95 96
96 97 dst = PyBytes_AsString(out);
97 98
98 99 i = 0;
99 100 while (i < len) {
100 101 acc = 0;
101 102 cap = len - i - 1;
102 103 if (cap > 4)
103 104 cap = 4;
104 105 for (j = 0; j < cap; i++, j++) {
105 106 c = b85dec[(int)*text++] - 1;
106 107 if (c < 0)
107 108 return PyErr_Format(
108 PyExc_ValueError,
109 "bad base85 character at position %d",
110 (int)i);
109 PyExc_ValueError,
110 "bad base85 character at position %d",
111 (int)i);
111 112 acc = acc * 85 + c;
112 113 }
113 114 if (i++ < len) {
114 115 c = b85dec[(int)*text++] - 1;
115 116 if (c < 0)
116 117 return PyErr_Format(
117 PyExc_ValueError,
118 "bad base85 character at position %d",
119 (int)i);
118 PyExc_ValueError,
119 "bad base85 character at position %d",
120 (int)i);
120 121 /* overflow detection: 0xffffffff == "|NsC0",
121 122 * "|NsC" == 0x03030303 */
122 123 if (acc > 0x03030303 || (acc *= 85) > 0xffffffff - c)
123 124 return PyErr_Format(
124 PyExc_ValueError,
125 "bad base85 sequence at position %d",
126 (int)i);
125 PyExc_ValueError,
126 "bad base85 sequence at position %d",
127 (int)i);
127 128 acc += c;
128 129 }
129 130
130 131 cap = olen < 4 ? olen : 4;
131 132 olen -= cap;
132 133 for (j = 0; j < 4 - cap; j++)
133 134 acc *= 85;
134 135 if (cap && cap < 4)
135 136 acc += 0xffffff >> (cap - 1) * 8;
136 137 for (j = 0; j < cap; j++) {
137 138 acc = (acc << 8) | (acc >> 24);
138 139 *dst++ = acc;
139 140 }
140 141 }
141 142
142 143 return out;
143 144 }
144 145
145 146 static char base85_doc[] = "Base85 Data Encoding";
146 147
147 148 static PyMethodDef methods[] = {
148 {"b85encode", b85encode, METH_VARARGS,
149 "Encode text in base85.\n\n"
150 "If the second parameter is true, pad the result to a multiple of "
151 "five characters.\n"},
152 {"b85decode", b85decode, METH_VARARGS, "Decode base85 text.\n"},
153 {NULL, NULL}
149 {"b85encode", b85encode, METH_VARARGS,
150 "Encode text in base85.\n\n"
151 "If the second parameter is true, pad the result to a multiple of "
152 "five characters.\n"},
153 {"b85decode", b85decode, METH_VARARGS, "Decode base85 text.\n"},
154 {NULL, NULL},
154 155 };
155 156
156 157 static const int version = 1;
157 158
158 159 #ifdef IS_PY3K
159 160 static struct PyModuleDef base85_module = {
160 PyModuleDef_HEAD_INIT,
161 "base85",
162 base85_doc,
163 -1,
164 methods
161 PyModuleDef_HEAD_INIT, "base85", base85_doc, -1, methods,
165 162 };
166 163
167 164 PyMODINIT_FUNC PyInit_base85(void)
168 165 {
169 166 PyObject *m;
170 167 b85prep();
171 168
172 169 m = PyModule_Create(&base85_module);
173 170 PyModule_AddIntConstant(m, "version", version);
174 171 return m;
175 172 }
176 173 #else
177 174 PyMODINIT_FUNC initbase85(void)
178 175 {
179 176 PyObject *m;
180 177 m = Py_InitModule3("base85", methods, base85_doc);
181 178
182 179 b85prep();
183 180 PyModule_AddIntConstant(m, "version", version);
184 181 }
185 182 #endif
General Comments 0
You need to be logged in to leave comments. Login now