mercurial/cext/base85.c
author Manuel Jacob <me@manueljacob.de>
Mon, 11 Jul 2022 01:51:20 +0200
branchstable
changeset 49378 094a5fa3cf52
parent 48821 b0dd39b91e7a
permissions -rw-r--r--
procutil: make stream detection in make_line_buffered more correct and strict In make_line_buffered(), we don’t want to wrap the stream if we know that lines get flushed to the underlying raw stream already. Previously, the heuristic was too optimistic. It assumed that any stream which is not an instance of io.BufferedIOBase doesn’t need wrapping. However, there are buffered streams that aren’t instances of io.BufferedIOBase, like Mercurial’s own winstdout. The new logic is different in two ways: First, only for the check, if unwraps any combination of WriteAllWrapper and winstdout. Second, it skips wrapping the stream only if it is an instance of io.RawIOBase (or already wrapped). If it is an instance of io.BufferedIOBase, it gets wrapped. In any other case, the function raises an exception. This ensures that, if an unknown stream is passed or we add another wrapper in the future, we don’t wrap the stream if it’s already line buffered or not wrap the stream if it’s not line buffered. In fact, this was already helpful during development of this change. Without it, I possibly would have forgot that WriteAllWrapper needs to be ignored for the check, leading to unnecessary wrapping if stdout is unbuffered. The alternative would have been to always wrap unknown streams. However, I don’t think that anyone would benefit from being less strict. We can expect streams from the standard library to be subclassing either io.RawIOBase or io.BufferedIOBase, so running Mercurial in the standard way should not regress by this change. Py2exe might replace sys.stdout and sys.stderr, but that currently breaks Mercurial anyway and also these streams don’t claim to be interactive, so this function is not called for them.

/*
 base85 codec

 Copyright 2006 Brendan Cully <brendan@kublai.com>

 This software may be used and distributed according to the terms of
 the GNU General Public License, incorporated herein by reference.

 Largely based on git's implementation
*/

#define PY_SSIZE_T_CLEAN
#include <Python.h>

#include "util.h"

static const char b85chars[] =
    "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    "abcdefghijklmnopqrstuvwxyz!#$%&()*+-;<=>?@^_`{|}~";
static char b85dec[256];

static void b85prep(void)
{
	unsigned i;

	memset(b85dec, 0, sizeof(b85dec));
	for (i = 0; i < sizeof(b85chars); i++) {
		b85dec[(int)(b85chars[i])] = i + 1;
	}
}

static PyObject *b85encode(PyObject *self, PyObject *args)
{
	const unsigned char *text;
	PyObject *out;
	char *dst;
	Py_ssize_t len, olen, i;
	unsigned int acc, val, ch;
	int pad = 0;

	if (!PyArg_ParseTuple(args, "y#|i", &text, &len, &pad)) {
		return NULL;
	}

	if (pad) {
		olen = ((len + 3) / 4 * 5) - 3;
	} else {
		olen = len % 4;
		if (olen) {
			olen++;
		}
		olen += len / 4 * 5;
	}
	if (!(out = PyBytes_FromStringAndSize(NULL, olen + 3))) {
		return NULL;
	}

	dst = PyBytes_AsString(out);

	while (len) {
		acc = 0;
		for (i = 24; i >= 0; i -= 8) {
			ch = *text++;
			acc |= ch << i;
			if (--len == 0) {
				break;
			}
		}
		for (i = 4; i >= 0; i--) {
			val = acc % 85;
			acc /= 85;
			dst[i] = b85chars[val];
		}
		dst += 5;
	}

	if (!pad) {
		_PyBytes_Resize(&out, olen);
	}

	return out;
}

static PyObject *b85decode(PyObject *self, PyObject *args)
{
	PyObject *out = NULL;
	const char *text;
	char *dst;
	Py_ssize_t len, i, j, olen, cap;
	int c;
	unsigned int acc;

	if (!PyArg_ParseTuple(args, "y#", &text, &len)) {
		return NULL;
	}

	olen = len / 5 * 4;
	i = len % 5;
	if (i) {
		olen += i - 1;
	}
	if (!(out = PyBytes_FromStringAndSize(NULL, olen))) {
		return NULL;
	}

	dst = PyBytes_AsString(out);

	i = 0;
	while (i < len) {
		acc = 0;
		cap = len - i - 1;
		if (cap > 4) {
			cap = 4;
		}
		for (j = 0; j < cap; i++, j++) {
			c = b85dec[(int)*text++] - 1;
			if (c < 0) {
				PyErr_Format(
				    PyExc_ValueError,
				    "bad base85 character at position %d",
				    (int)i);
				goto bail;
			}
			acc = acc * 85 + c;
		}
		if (i++ < len) {
			c = b85dec[(int)*text++] - 1;
			if (c < 0) {
				PyErr_Format(
				    PyExc_ValueError,
				    "bad base85 character at position %d",
				    (int)i);
				goto bail;
			}
			/* overflow detection: 0xffffffff == "|NsC0",
			 * "|NsC" == 0x03030303 */
			if (acc > 0x03030303 || (acc *= 85) > 0xffffffff - c) {
				PyErr_Format(
				    PyExc_ValueError,
				    "bad base85 sequence at position %d",
				    (int)i);
				goto bail;
			}
			acc += c;
		}

		cap = olen < 4 ? olen : 4;
		olen -= cap;
		for (j = 0; j < 4 - cap; j++) {
			acc *= 85;
		}
		if (cap && cap < 4) {
			acc += 0xffffff >> (cap - 1) * 8;
		}
		for (j = 0; j < cap; j++) {
			acc = (acc << 8) | (acc >> 24);
			*dst++ = acc;
		}
	}

	return out;
bail:
	Py_XDECREF(out);
	return NULL;
}

static char base85_doc[] = "Base85 Data Encoding";

static PyMethodDef methods[] = {
    {"b85encode", b85encode, METH_VARARGS,
     "Encode text in base85.\n\n"
     "If the second parameter is true, pad the result to a multiple of "
     "five characters.\n"},
    {"b85decode", b85decode, METH_VARARGS, "Decode base85 text.\n"},
    {NULL, NULL},
};

static const int version = 1;

static struct PyModuleDef base85_module = {
    PyModuleDef_HEAD_INIT, "base85", base85_doc, -1, methods,
};

PyMODINIT_FUNC PyInit_base85(void)
{
	PyObject *m;
	b85prep();

	m = PyModule_Create(&base85_module);
	PyModule_AddIntConstant(m, "version", version);
	return m;
}