mercurial/base85.c
author Pierre-Yves David <pierre-yves.david@fb.com>
Wed, 19 Mar 2014 23:36:15 -0700
changeset 20876 ddd56f3eb786
parent 16848 19a915d43a68
child 26074 c1aefe57cf4e
permissions -rw-r--r--
bundle2: support for bundling and unbundling payload We add the ability to bundle and unbundle a payload in parts. The payload is the actual binary data of the part. It is used to convey all the applicative data. For now we stick to very simple implementation with all the data fit in single chunk. This open the door to some bundle2 testing usage. This will be improved before bundle2 get used for real. We need to be able to stream the payload in multiple part to exchange any changegroup efficiently. This simple version will do for now. Bundling and unbundling are done in the same changeset because the test for parts is less modular. However, the result is not too complex.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3283
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
     1
/*
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
     2
 base85 codec
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
     3
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
     4
 Copyright 2006 Brendan Cully <brendan@kublai.com>
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
     5
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
     6
 This software may be used and distributed according to the terms of
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
     7
 the GNU General Public License, incorporated herein by reference.
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
     8
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
     9
 Largely based on git's implementation
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    10
*/
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    11
16837
1b9d54c00d50 base85: use Py_ssize_t for string lengths
Adrian Buehlmann <adrian@cadifra.com>
parents: 16522
diff changeset
    12
#define PY_SSIZE_T_CLEAN
3283
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    13
#include <Python.h>
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    14
11362
f42ef9493fa9 base85.c: Added support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
    15
#include "util.h"
f42ef9493fa9 base85.c: Added support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
    16
3283
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    17
static const char b85chars[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    18
	"abcdefghijklmnopqrstuvwxyz!#$%&()*+-;<=>?@^_`{|}~";
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    19
static char b85dec[256];
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    20
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    21
static void
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    22
b85prep(void)
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    23
{
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    24
	int i;
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    25
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    26
	memset(b85dec, 0, sizeof(b85dec));
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    27
	for (i = 0; i < sizeof(b85chars); i++)
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    28
		b85dec[(int)(b85chars[i])] = i + 1;
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    29
}
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    30
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    31
static PyObject *
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    32
b85encode(PyObject *self, PyObject *args)
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    33
{
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    34
	const unsigned char *text;
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    35
	PyObject *out;
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    36
	char *dst;
16837
1b9d54c00d50 base85: use Py_ssize_t for string lengths
Adrian Buehlmann <adrian@cadifra.com>
parents: 16522
diff changeset
    37
	Py_ssize_t len, olen, i;
3283
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    38
	unsigned int acc, val, ch;
7190
aecea6934fdd Some additional space/tab cleanups
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3369
diff changeset
    39
	int pad = 0;
3283
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    40
3369
4bad632913d8 python2.5 PyArg_ParseTuple fix
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3332
diff changeset
    41
	if (!PyArg_ParseTuple(args, "s#|i", &text, &len, &pad))
3283
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    42
		return NULL;
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    43
7190
aecea6934fdd Some additional space/tab cleanups
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3369
diff changeset
    44
	if (pad)
aecea6934fdd Some additional space/tab cleanups
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3369
diff changeset
    45
		olen = ((len + 3) / 4 * 5) - 3;
aecea6934fdd Some additional space/tab cleanups
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3369
diff changeset
    46
	else {
aecea6934fdd Some additional space/tab cleanups
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3369
diff changeset
    47
		olen = len % 4;
aecea6934fdd Some additional space/tab cleanups
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3369
diff changeset
    48
		if (olen)
aecea6934fdd Some additional space/tab cleanups
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3369
diff changeset
    49
			olen++;
aecea6934fdd Some additional space/tab cleanups
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3369
diff changeset
    50
		olen += len / 4 * 5;
aecea6934fdd Some additional space/tab cleanups
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3369
diff changeset
    51
	}
11362
f42ef9493fa9 base85.c: Added support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
    52
	if (!(out = PyBytes_FromStringAndSize(NULL, olen + 3)))
3283
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    53
		return NULL;
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    54
11362
f42ef9493fa9 base85.c: Added support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
    55
	dst = PyBytes_AsString(out);
3283
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    56
3288
e93c926e069e Handle odd-sized base85 input and output
Brendan Cully <brendan@kublai.com>
parents: 3283
diff changeset
    57
	while (len) {
3283
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    58
		acc = 0;
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    59
		for (i = 24; i >= 0; i -= 8) {
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    60
			ch = *text++;
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    61
			acc |= ch << i;
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    62
			if (--len == 0)
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    63
				break;
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    64
		}
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    65
		for (i = 4; i >= 0; i--) {
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    66
			val = acc % 85;
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    67
			acc /= 85;
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    68
			dst[i] = b85chars[val];
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    69
		}
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    70
		dst += 5;
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    71
	}
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    72
7190
aecea6934fdd Some additional space/tab cleanups
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3369
diff changeset
    73
	if (!pad)
11362
f42ef9493fa9 base85.c: Added support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
    74
		_PyBytes_Resize(&out, olen);
3288
e93c926e069e Handle odd-sized base85 input and output
Brendan Cully <brendan@kublai.com>
parents: 3283
diff changeset
    75
3283
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    76
	return out;
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    77
}
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    78
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    79
static PyObject *
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    80
b85decode(PyObject *self, PyObject *args)
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    81
{
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    82
	PyObject *out;
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    83
	const char *text;
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    84
	char *dst;
16837
1b9d54c00d50 base85: use Py_ssize_t for string lengths
Adrian Buehlmann <adrian@cadifra.com>
parents: 16522
diff changeset
    85
	Py_ssize_t len, i, j, olen, cap;
1b9d54c00d50 base85: use Py_ssize_t for string lengths
Adrian Buehlmann <adrian@cadifra.com>
parents: 16522
diff changeset
    86
	int c;
3283
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    87
	unsigned int acc;
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    88
3369
4bad632913d8 python2.5 PyArg_ParseTuple fix
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 3332
diff changeset
    89
	if (!PyArg_ParseTuple(args, "s#", &text, &len))
3283
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    90
		return NULL;
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    91
3288
e93c926e069e Handle odd-sized base85 input and output
Brendan Cully <brendan@kublai.com>
parents: 3283
diff changeset
    92
	olen = len / 5 * 4;
e93c926e069e Handle odd-sized base85 input and output
Brendan Cully <brendan@kublai.com>
parents: 3283
diff changeset
    93
	i = len % 5;
e93c926e069e Handle odd-sized base85 input and output
Brendan Cully <brendan@kublai.com>
parents: 3283
diff changeset
    94
	if (i)
e93c926e069e Handle odd-sized base85 input and output
Brendan Cully <brendan@kublai.com>
parents: 3283
diff changeset
    95
		olen += i - 1;
11362
f42ef9493fa9 base85.c: Added support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
    96
	if (!(out = PyBytes_FromStringAndSize(NULL, olen)))
3283
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    97
		return NULL;
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    98
11362
f42ef9493fa9 base85.c: Added support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
    99
	dst = PyBytes_AsString(out);
3283
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   100
3288
e93c926e069e Handle odd-sized base85 input and output
Brendan Cully <brendan@kublai.com>
parents: 3283
diff changeset
   101
	i = 0;
e93c926e069e Handle odd-sized base85 input and output
Brendan Cully <brendan@kublai.com>
parents: 3283
diff changeset
   102
	while (i < len)
3283
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   103
	{
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   104
		acc = 0;
3288
e93c926e069e Handle odd-sized base85 input and output
Brendan Cully <brendan@kublai.com>
parents: 3283
diff changeset
   105
		cap = len - i - 1;
e93c926e069e Handle odd-sized base85 input and output
Brendan Cully <brendan@kublai.com>
parents: 3283
diff changeset
   106
		if (cap > 4)
e93c926e069e Handle odd-sized base85 input and output
Brendan Cully <brendan@kublai.com>
parents: 3283
diff changeset
   107
			cap = 4;
e93c926e069e Handle odd-sized base85 input and output
Brendan Cully <brendan@kublai.com>
parents: 3283
diff changeset
   108
		for (j = 0; j < cap; i++, j++)
3283
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   109
		{
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   110
			c = b85dec[(int)*text++] - 1;
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   111
			if (c < 0)
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 7190
diff changeset
   112
				return PyErr_Format(
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 7190
diff changeset
   113
					PyExc_ValueError,
16848
19a915d43a68 base85: cast Py_ssize_t values to int (issue3481)
Adrian Buehlmann <adrian@cadifra.com>
parents: 16837
diff changeset
   114
					"bad base85 character at position %d",
19a915d43a68 base85: cast Py_ssize_t values to int (issue3481)
Adrian Buehlmann <adrian@cadifra.com>
parents: 16837
diff changeset
   115
					(int)i);
3283
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   116
			acc = acc * 85 + c;
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   117
		}
3288
e93c926e069e Handle odd-sized base85 input and output
Brendan Cully <brendan@kublai.com>
parents: 3283
diff changeset
   118
		if (i++ < len)
3283
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   119
		{
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   120
			c = b85dec[(int)*text++] - 1;
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   121
			if (c < 0)
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 7190
diff changeset
   122
				return PyErr_Format(
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 7190
diff changeset
   123
					PyExc_ValueError,
16848
19a915d43a68 base85: cast Py_ssize_t values to int (issue3481)
Adrian Buehlmann <adrian@cadifra.com>
parents: 16837
diff changeset
   124
					"bad base85 character at position %d",
19a915d43a68 base85: cast Py_ssize_t values to int (issue3481)
Adrian Buehlmann <adrian@cadifra.com>
parents: 16837
diff changeset
   125
					(int)i);
3288
e93c926e069e Handle odd-sized base85 input and output
Brendan Cully <brendan@kublai.com>
parents: 3283
diff changeset
   126
			/* overflow detection: 0xffffffff == "|NsC0",
e93c926e069e Handle odd-sized base85 input and output
Brendan Cully <brendan@kublai.com>
parents: 3283
diff changeset
   127
			 * "|NsC" == 0x03030303 */
e93c926e069e Handle odd-sized base85 input and output
Brendan Cully <brendan@kublai.com>
parents: 3283
diff changeset
   128
			if (acc > 0x03030303 || (acc *= 85) > 0xffffffff - c)
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 7190
diff changeset
   129
				return PyErr_Format(
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 7190
diff changeset
   130
					PyExc_ValueError,
16848
19a915d43a68 base85: cast Py_ssize_t values to int (issue3481)
Adrian Buehlmann <adrian@cadifra.com>
parents: 16837
diff changeset
   131
					"bad base85 sequence at position %d",
19a915d43a68 base85: cast Py_ssize_t values to int (issue3481)
Adrian Buehlmann <adrian@cadifra.com>
parents: 16837
diff changeset
   132
					(int)i);
3288
e93c926e069e Handle odd-sized base85 input and output
Brendan Cully <brendan@kublai.com>
parents: 3283
diff changeset
   133
			acc += c;
3283
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   134
		}
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   135
3288
e93c926e069e Handle odd-sized base85 input and output
Brendan Cully <brendan@kublai.com>
parents: 3283
diff changeset
   136
		cap = olen < 4 ? olen : 4;
e93c926e069e Handle odd-sized base85 input and output
Brendan Cully <brendan@kublai.com>
parents: 3283
diff changeset
   137
		olen -= cap;
e93c926e069e Handle odd-sized base85 input and output
Brendan Cully <brendan@kublai.com>
parents: 3283
diff changeset
   138
		for (j = 0; j < 4 - cap; j++)
e93c926e069e Handle odd-sized base85 input and output
Brendan Cully <brendan@kublai.com>
parents: 3283
diff changeset
   139
			acc *= 85;
e93c926e069e Handle odd-sized base85 input and output
Brendan Cully <brendan@kublai.com>
parents: 3283
diff changeset
   140
		if (cap && cap < 4)
e93c926e069e Handle odd-sized base85 input and output
Brendan Cully <brendan@kublai.com>
parents: 3283
diff changeset
   141
			acc += 0xffffff >> (cap - 1) * 8;
e93c926e069e Handle odd-sized base85 input and output
Brendan Cully <brendan@kublai.com>
parents: 3283
diff changeset
   142
		for (j = 0; j < cap; j++)
3283
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   143
		{
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   144
			acc = (acc << 8) | (acc >> 24);
3288
e93c926e069e Handle odd-sized base85 input and output
Brendan Cully <brendan@kublai.com>
parents: 3283
diff changeset
   145
			*dst++ = acc;
3283
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   146
		}
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   147
	}
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   148
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   149
	return out;
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   150
}
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   151
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   152
static char base85_doc[] = "Base85 Data Encoding";
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   153
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   154
static PyMethodDef methods[] = {
3288
e93c926e069e Handle odd-sized base85 input and output
Brendan Cully <brendan@kublai.com>
parents: 3283
diff changeset
   155
	{"b85encode", b85encode, METH_VARARGS,
7190
aecea6934fdd Some additional space/tab cleanups
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3369
diff changeset
   156
	 "Encode text in base85.\n\n"
aecea6934fdd Some additional space/tab cleanups
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3369
diff changeset
   157
	 "If the second parameter is true, pad the result to a multiple of "
aecea6934fdd Some additional space/tab cleanups
Thomas Arendsen Hein <thomas@intevation.de>
parents: 3369
diff changeset
   158
	 "five characters.\n"},
3288
e93c926e069e Handle odd-sized base85 input and output
Brendan Cully <brendan@kublai.com>
parents: 3283
diff changeset
   159
	{"b85decode", b85decode, METH_VARARGS, "Decode base85 text.\n"},
3283
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   160
	{NULL, NULL}
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   161
};
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   162
11362
f42ef9493fa9 base85.c: Added support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
   163
#ifdef IS_PY3K
f42ef9493fa9 base85.c: Added support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
   164
static struct PyModuleDef base85_module = {
f42ef9493fa9 base85.c: Added support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
   165
	PyModuleDef_HEAD_INIT,
f42ef9493fa9 base85.c: Added support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
   166
	"base85",
f42ef9493fa9 base85.c: Added support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
   167
	base85_doc,
f42ef9493fa9 base85.c: Added support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
   168
	-1,
f42ef9493fa9 base85.c: Added support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
   169
	methods
f42ef9493fa9 base85.c: Added support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
   170
};
f42ef9493fa9 base85.c: Added support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
   171
f42ef9493fa9 base85.c: Added support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
   172
PyMODINIT_FUNC PyInit_base85(void)
f42ef9493fa9 base85.c: Added support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
   173
{
f42ef9493fa9 base85.c: Added support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
   174
	b85prep();
f42ef9493fa9 base85.c: Added support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
   175
f42ef9493fa9 base85.c: Added support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
   176
	return PyModule_Create(&base85_module);
f42ef9493fa9 base85.c: Added support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
   177
}
f42ef9493fa9 base85.c: Added support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
   178
#else
3283
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   179
PyMODINIT_FUNC initbase85(void)
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   180
{
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   181
	Py_InitModule3("base85", methods, base85_doc);
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   182
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   183
	b85prep();
1f2c3983a6c5 Add a base85 codec
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   184
}
11362
f42ef9493fa9 base85.c: Added support for py3k.
Renato Cunha <renatoc@gmail.com>
parents: 10282
diff changeset
   185
#endif