Exemplo n.º 1
0
static void
runtime_test(void)
{
    /* size_t should be able to represent the length of any size buffer */
    IMP_ASSERT(sizeof(size_t) == sizeof(void *));

    /* we must be able to perform the assignment (Py_ssize_t) -> (size_t)
     * as long as the value is non-negative. */
    IMP_ASSERT(sizeof(size_t) >= sizeof(Py_ssize_t));

    /* char must be one octet */
    IMP_ASSERT(sizeof(char) == 1);

    /* Perform a basic test of the xor_strings function, including a test for
     * an off-by-one bug. */
    {
        char x[7] = "\x00hello";    /* NUL + "hello" + NUL */
        char y[7] = "\xffworld";    /* 0xff + "world" + NUL */
        char z[9] = "[ABCDEFG]";    /* "[ABCDEFG]" + NUL */

        xor_strings(z+1, x, y, 7);
        IMP_ASSERT(!memcmp(z, "[\xff\x1f\x0a\x1e\x00\x0b\x00]", 9));
    }

    /* Perform a basic test of the xor_string_with_char function, including a test for
     * an off-by-one bug. */
    {
        char x[7] = "\x00hello";    /* NUL + "hello" + NUL */
        char y = 170;               /* 0xaa */
        char z[9] = "[ABCDEFG]";    /* "[ABCDEFG]" + NUL */

        xor_string_with_char(z+1, x, y, 7);
        IMP_ASSERT(!memcmp(z, "[\xaa\xc2\xcf\xc6\xc6\xc5\xaa]", 9));
    }
}
Exemplo n.º 2
0
static PyObject *
strxor_c_function(PyObject *self, PyObject *args)
{
    PyObject *s, *retval;
    int c;
    Py_ssize_t length;

    if (!PyArg_ParseTuple(args, "Si", &s, &c))
        return NULL;

    if ((c < 0) || (c > 255)) {
        PyErr_SetString(PyExc_ValueError, "c must be in range(256)");
        return NULL;
    }

    length = PyBytes_GET_SIZE(s);
    assert(length >= 0);

    /* Create return string */
    retval = PyBytes_FromStringAndSize(NULL, length);
    if (!retval) {
        return NULL;
    }

    /* retval := a ^ chr(c)*length */
    xor_string_with_char(PyBytes_AS_STRING(retval), PyBytes_AS_STRING(s), (char) c, length);

    return retval;
}
Exemplo n.º 3
0
static PyObject *
strxor_c_function(PyObject *self, PyObject *args)
{
    char *s;
    PyObject *retval;
    int c;
    int length;
#ifdef HAS_NEW_BUFFER
    Py_buffer view;
    if (!PyArg_ParseTuple(args, "s*i", &view, &c))
        return NULL;
    s = (char*)view.buf;
    length = view.len;
#else
    if (!PyArg_ParseTuple(args, "s#i", &s, &length, &c))
        return NULL;
#endif
    if ((c < 0) || (c > 255)) {
        PyErr_SetString(PyExc_ValueError, "c must be in range(256)");
#ifdef HAS_NEW_BUFFER
        PyBuffer_Release(&view);
#endif
        return NULL;
    }
    assert(length >= 0);

    /* Create return string */
    retval = PyBytes_FromStringAndSize(NULL, length);
    if (!retval) {
#ifdef HAS_NEW_BUFFER
        PyBuffer_Release(&view);
#endif
        return NULL;
    }

    /* retval := a ^ chr(c)*length */
    xor_string_with_char(PyBytes_AS_STRING(retval), s, (char) c, length);

#ifdef HAS_NEW_BUFFER
    PyBuffer_Release(&view);
#endif
    return retval;
}