/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
#include <Python.h>
#include "cStringIO.h"
#include <stdbool.h>
#include <stdint.h>
#include <netinet/in.h>
// TODO(dreiss): defval appears to be unused. Look into removing it.
// TODO(dreiss): Make parse_spec_args recursive, and cache the output
// permanently in the object. (Malloc and orphan.)
// TODO(dreiss): Why do we need cStringIO for reading, why not just char*?
// Can cStringIO let us work with a BufferedTransport?
// TODO(dreiss): Don't ignore the rv from cwrite (maybe).
/* ====== BEGIN UTILITIES ====== */
#define INIT_OUTBUF_SIZE 128
// Stolen out of TProtocol.h.
// It would be a huge pain to have both get this from one place.
typedef enum TType {
T_STOP = 0,
T_VOID = 1,
T_BOOL = 2,
T_BYTE = 3,
T_I08 = 3,
T_I16 = 6,
T_I32 = 8,
T_U64 = 9,
T_I64 = 10,
T_DOUBLE = 4,
T_STRING = 11,
T_UTF7 = 11,
T_STRUCT = 12,
T_MAP = 13,
T_SET = 14,
T_LIST = 15,
T_UTF8 = 16,
T_UTF16 = 17
} TType;
#ifndef __BYTE_ORDER
# if defined(BYTE_ORDER) && defined(LITTLE_ENDIAN) && defined(BIG_ENDIAN)
# define __BYTE_ORDER BYTE_ORDER
# define __LITTLE_ENDIAN LITTLE_ENDIAN
# define __BIG_ENDIAN BIG_ENDIAN
# else
# error "Cannot determine endianness"
# endif
#endif
// Same comment as the enum. Sorry.
#if __BYTE_ORDER == __BIG_ENDIAN
# define ntohll(n) (n)
# define htonll(n) (n)
#elif __BYTE_ORDER == __LITTLE_ENDIAN
# if defined(__GNUC__) && defined(__GLIBC__)
# include <byteswap.h>
# define ntohll(n) bswap_64(n)
# define htonll(n) bswap_64(n)
# else /* GNUC & GLIBC */
# define ntohll(n) ( (((unsigned long long)ntohl(n)) << 32) + ntohl(n >> 32) )
# define htonll(n) ( (((unsigned long long)htonl(n)) << 32) + htonl(n >> 32) )
# endif /* GNUC & GLIBC */
#else /* __BYTE_ORDER */
# error "Can't define htonll or ntohll!"
#endif
// Doing a benchmark shows that interning actually makes a difference, amazingly.
#define INTERN_STRING(value) _intern_ ## value
#define INT_CONV_ERROR_OCCURRED(v) ( ((v) == -1) && PyErr_Occurred() )
#define CHECK_RANGE(v, min, max) ( ((v) <= (max)) && ((v) >= (min)) )
// Py_ssize_t was not defined before Python 2.5
#if (PY_VERSION_HEX < 0x02050000)
typedef int Py_ssize_t;
#endif
/**
* A cache of the spec_args for a set or list,
* so we don't have to keep calling PyTuple_GET_ITEM.
*/
typedef struct {
TType element_type;
PyObject* typeargs;
} SetListTypeArgs;
/**
* A cache of the spec_args for a map,
* so we don't have to keep calling PyTuple_GET_ITEM.
*/
typedef struct {
TType ktag;
TType vtag;
PyObject* ktypeargs;
PyObject* vtypeargs;
} MapTypeArgs;
/**
* A cache of the spec_args for a struct,
* so we don't have to keep calling PyTuple_GET_ITEM.
*/
typedef struct {
PyObject* klass;
PyObject* spec;
} StructTypeArgs;
/**
* A cache of the item spec from a struct specification,
* so we don't have to keep calling PyTuple_GET_ITEM.
*/
typedef struct {
int tag;
TType type;
PyObject* attrname;
PyObject* typeargs;
PyObject* defval;
} StructItemSpec;
/**
* A cache of the two key attributes of a CReadableTransport,
* so we don't have to keep calling PyObject_GetAttr.
*/
typedef struct {
PyObject* stringiobuf;
PyObject* refill_callable;
} DecodeBuffer;
/** Pointer to interned string to speed up attribute lookup. */
static PyObject* INTERN_STRING(cstringio_buf);
/** Pointer to interned string to speed up attribute lookup. */
static PyObject* INTERN_STRING(cstringio_refill);
static inline bool
check_ssize_t_32(Py_ssize_t len) {
// error from getting the int
if (INT_CONV_ERROR_OCCURRED(len)) {
return false;
}
if (!CHECK_RANGE(len, 0, INT32_MAX)) {
PyErr_SetString(PyExc_OverflowError, "string size out of range");
return false;
}
return true;
}
static inline bool
parse_pyint(PyObject* o, int32_t* ret, int32_t min, int32_t max) {
long val = PyInt_AsLong(o);
if (INT_CONV_ERROR_OCCURRED(val)) {
return false;
}
if (!CHECK_RANGE(val, min, max)) {
PyErr_SetString(PyExc_OverflowError, "int out of range");
return false;
}
*ret = (int32_t) val;
return true;
}
/* --- FUNCTIONS TO PARSE STRUCT SPECIFICATOINS --- */
static bool
parse_set_list_args(SetListTypeArgs* dest, PyObject* typeargs) {
if (PyTuple_Size(typeargs) != 2) {
PyErr_SetString(PyExc_TypeError, "expecting tuple of size 2 for list/set type args");
return false;
}
dest->element_type = PyInt_AsLong(PyTuple_GET_ITEM(typeargs, 0));
if (INT_CONV_ERROR_OCCURRED(dest->element_type)) {
return false;
}
dest->typeargs = PyTuple_GET_ITEM(typeargs, 1);
return true;
}
static bool
parse_map_args(MapTypeArgs* dest, PyObject* typeargs) {
if (PyTuple_Size(typeargs) != 4) {
PyErr_SetString(PyExc_TypeError, "expecting 4 arguments for typeargs to map");
return false;
}
dest->ktag = PyInt_AsLong(PyTuple_GET_ITEM(typeargs, 0));
if (INT_CONV_ERROR_OCCURRED(dest->ktag)) {
return false;
}
dest->vtag = PyInt_AsLong(PyTuple_GET_ITEM(typeargs, 2));
if (INT_CONV_ERROR_OCCURRED(dest->vtag)) {
return false;
}
dest->ktypeargs = PyTuple_GET_ITEM(typeargs, 1);
dest->vtypeargs = PyTuple_GET_ITEM(typeargs, 3);
return true;
}
static bool
parse_struct_args(StructTypeArgs* dest, PyObject* typeargs) {
if (PyTuple_Size(typeargs) != 2) {
PyErr_SetString(PyExc_TypeError, "expecting tuple of size 2 for struct args");
return false;
}
dest->klass = PyTuple_GET_ITEM(typeargs, 0);
dest->spec = PyTuple_GET_ITEM(typeargs, 1);
return true;
}
static int
parse_struct_item_spec(StructItemSpec* dest, PyObject* spec_tuple) {
// i'd like to use ParseArgs here, but it seems to be a bottleneck.
if (PyTuple_Size(spec_tuple) != 5) {
PyErr_SetString(PyExc_TypeError, "expecting 5 arguments for spec tuple");
return false;
}
dest->tag = PyInt_AsLong(PyTuple_GET_ITEM(spec_tuple, 0));
if (INT_CONV_ERROR_OCCURRED(dest->tag)) {
return false;
}
dest->type = PyInt_AsLong(PyTuple_GET_ITEM(spec_tuple, 1));
if (INT_CONV_ERROR_OCCURRED(dest->type)) {
return false;
}
dest->attrname = PyTuple_GET_ITEM(spec_tuple, 2);
dest->typeargs = PyTuple_GET_ITEM(spec_tuple, 3);
dest->defval = PyTuple_GET_ITEM(spec_tuple, 4);
return true;
}
/* ====== END UTILITIES ====== */
/* ====== BEGIN WRITING FUNCTIONS ====== */
/* --- LOW-LEVEL WRITING FUNCTIONS --- */
static void writeByte(PyObject* outbuf, int8_t val) {
int8_t net = val;
PycStringIO->cwrite(outbuf, (char*)&net, sizeof(int8_t));
}
static void writeI16(PyObject* outbuf, int16_t val) {
int16_t net = (int16_t)htons(val);
PycStringIO->cwrite(outbuf, (char*)&net, sizeof(int16_t));
}
static void writeI32(PyObject* outbuf, int32_t val) {
int32_t net = (int32_t)htonl(val);
PycStringIO->cwrite(outbuf, (char*)&net, sizeof(int32_t));
}
static void writeI64(PyObject* outbuf, int64_t val) {
int64_t net = (int64_t)htonll(val);
PycStringIO->cwrite(outbuf, (char*)&net, sizeof(int64_t));
}
static void writeDouble(PyObject* outbuf, double dub) {
// Unfortunately, bitwise_cast doesn't work in C. Bad C!
u
评论5