PageRenderTime 40ms CodeModel.GetById 13ms app.highlight 23ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/release-0.0.0-rc0/hive/external/service/lib/php/ext/thrift_protocol/tags/1.0.0/php_thrift_protocol.cpp

#
C++ | 361 lines | 314 code | 43 blank | 4 comment | 28 complexity | 6096c9efa1b33fbb503c66a5ccdfb5f9 MD5 | raw file
  1#ifdef HAVE_CONFIG_H
  2#include "config.h"
  3#endif
  4
  5#include <sys/types.h>
  6#include <netinet/in.h>
  7#include <unistd.h>
  8#include <endian.h>
  9#include <byteswap.h>
 10
 11#if __BYTE_ORDER == __LITTLE_ENDIAN
 12#define ntohll(x) bswap_64(x)
 13#else
 14#define ntohll(x) x
 15#endif
 16
 17enum TType {
 18  T_STOP       = 0,
 19  T_VOID       = 1,
 20  T_BOOL       = 2,
 21  T_BYTE       = 3,
 22  T_I08        = 3,
 23  T_I16        = 6,
 24  T_I32        = 8,
 25  T_U64        = 9,
 26  T_I64        = 10,
 27  T_DOUBLE     = 4,
 28  T_STRING     = 11,
 29  T_UTF7       = 11,
 30  T_STRUCT     = 12,
 31  T_MAP        = 13,
 32  T_SET        = 14,
 33  T_LIST       = 15,
 34  T_UTF8       = 16,
 35  T_UTF16      = 17
 36};
 37
 38#include "php.h"
 39#include "php_thrift_protocol.h"
 40
 41static function_entry thrift_protocol_functions[] = {
 42  PHP_FE(thrift_protocol_binary_deserialize, NULL)
 43  {NULL, NULL, NULL}
 44} ;
 45
 46zend_module_entry thrift_protocol_module_entry = {
 47  STANDARD_MODULE_HEADER,
 48  "thrift_protocol",
 49  thrift_protocol_functions,
 50  NULL,
 51  NULL,
 52  NULL,
 53  NULL,
 54  NULL,
 55  "1.0",
 56  STANDARD_MODULE_PROPERTIES
 57};
 58
 59#ifdef COMPILE_DL_THRIFT_PROTOCOL
 60ZEND_GET_MODULE(thrift_protocol)
 61#endif
 62
 63class PHPTransport {
 64public:
 65  PHPTransport(zval* _p, size_t _buffer_size = 1024) :  buffer(reinterpret_cast<char*>(emalloc(_buffer_size))), buffer_remaining(0), buffer_size(_buffer_size), p(_p) {
 66    ZVAL_STRING(&funcname, "read", 0);
 67    // Get the transport for the passed protocol
 68    zval gettransport;
 69    ZVAL_STRING(&gettransport, "getTransport", 0);
 70    MAKE_STD_ZVAL(t);
 71    ZVAL_NULL(t);
 72    TSRMLS_FETCH();
 73    call_user_function(EG(function_table), &p, &gettransport, t, 0, NULL TSRMLS_CC);
 74  }
 75
 76  ~PHPTransport() {
 77    put_back();
 78    efree(buffer);
 79    zval_ptr_dtor(&t);
 80  }
 81
 82  void put_back() {
 83    if (buffer_remaining) {
 84      zval putbackfn;
 85      ZVAL_STRING(&putbackfn, "putBack", 0);
 86
 87      char* newbuf = (char*)emalloc(buffer_remaining + 1);
 88      memcpy(newbuf, buffer_ptr, buffer_remaining);
 89      newbuf[buffer_remaining] = '\0';
 90
 91      zval *args[1];
 92      MAKE_STD_ZVAL(args[0]);
 93      ZVAL_STRINGL(args[0], newbuf, buffer_remaining, 0);
 94
 95      TSRMLS_FETCH();
 96
 97      zval ret;
 98      call_user_function(EG(function_table), &t, &putbackfn, &ret, 1, args TSRMLS_CC);
 99      zval_ptr_dtor(args);
100      zval_dtor(&ret);
101    }
102    buffer_remaining = 0;
103    buffer_ptr = buffer;
104  }
105
106  zval* protocol() { return p; }
107  zval* transport() { return t; }
108
109  void readBytes(void* buf, size_t len) {
110    while (len) {
111      size_t chunk_size = MIN(len, buffer_remaining);
112      if (chunk_size) {
113        memcpy(buf, buffer_ptr, chunk_size);
114        buffer_ptr = reinterpret_cast<char*>(buffer_ptr) + chunk_size;
115        buffer_remaining -= chunk_size;
116        buf = reinterpret_cast<char*>(buf) + chunk_size;
117        len -= chunk_size;
118      }
119      if (! len) break;
120      refill();
121    }
122  }
123
124protected:
125  void refill() {
126    assert(buffer_remaining == 0);
127    zval retval;
128    ZVAL_NULL(&retval);
129
130    zval *args[1];
131    MAKE_STD_ZVAL(args[0]);
132    ZVAL_LONG(args[0], buffer_size);
133
134    TSRMLS_FETCH();
135
136    call_user_function(EG(function_table), &t, &funcname, &retval, 1, args TSRMLS_CC);
137    zval_ptr_dtor(args);
138
139    buffer_remaining = Z_STRLEN(retval);
140    memcpy(buffer, Z_STRVAL(retval), buffer_remaining);
141    zval_dtor(&retval);
142
143    buffer_ptr = buffer;
144  }
145
146  char* buffer;
147  char* buffer_ptr;
148  size_t buffer_remaining;
149  size_t buffer_size;
150
151  zval* p; 
152  zval* t;
153  zval funcname;
154};
155
156// Does not call the ctor on the object, all fields will be NULL
157void createObject(char* obj_typename, zval* return_value) {
158  TSRMLS_FETCH();
159  size_t obj_typename_len = strlen(obj_typename);
160  zend_class_entry* ce = zend_fetch_class(obj_typename, obj_typename_len, ZEND_FETCH_CLASS_DEFAULT TSRMLS_CC);
161  if (! ce) {
162    php_error_docref(NULL TSRMLS_CC, E_ERROR, "Class %s does not exist", obj_typename);
163    RETURN_NULL();
164  } 
165
166  object_and_properties_init(return_value, ce, NULL);
167}
168
169void binary_deserialize(long thrift_typeID, PHPTransport& transport, zval* return_value, char* structType) {
170  Z_TYPE_P(return_value) = IS_NULL; // just in case
171
172  switch (thrift_typeID) {
173    case T_STOP:
174    case T_VOID:
175      RETURN_NULL();
176      return; 
177    case T_STRUCT: {
178      assert(structType);
179      createObject(structType, return_value);
180      zval retval;
181      ZVAL_NULL(&retval);
182      zval *args[1];
183      args[0] = transport.protocol();
184      zval funcname;
185      ZVAL_STRING(&funcname, "read", 0);
186      transport.put_back(); // return our buffer to the userland T{Framed,Buffered}Transport for reading the field headers and such
187      TSRMLS_FETCH();
188      call_user_function(EG(function_table), &return_value, &funcname, &retval, 1, args TSRMLS_CC);
189      zval_dtor(&retval);
190      return;
191    } break;
192    case T_BOOL: {
193      uint8_t c;
194      transport.readBytes(&c, 1);
195      RETURN_BOOL(c != 0);
196    }
197  //case T_I08: // same numeric value as T_BYTE
198    case T_BYTE: { 
199      uint8_t c;
200      transport.readBytes(&c, 1);
201      RETURN_LONG(c); 
202    }
203    case T_I16: {
204      uint16_t c;
205      transport.readBytes(&c, 2);
206      RETURN_LONG(ntohs(c));
207    }
208    case T_I32: {
209      uint32_t c;
210      transport.readBytes(&c, 4);
211      RETURN_LONG(ntohl(c));
212    }
213    case T_U64:
214    case T_I64: {
215      uint64_t c;
216      transport.readBytes(&c, 8);
217      RETURN_LONG(ntohll(c));
218    }
219    case T_DOUBLE: {
220      union {
221        uint64_t c;
222        double d;
223      } a;
224      transport.readBytes(&(a.c), 8);
225      a.c = ntohll(a.c);
226      RETURN_DOUBLE(a.d);
227    }
228    //case T_UTF7: // aliases T_STRING
229    case T_UTF8:
230    case T_UTF16:
231    case T_STRING: {
232      uint32_t size;
233      transport.readBytes(&size, 4);
234      size = ntohl(size);
235      char* strbuf = (char*) emalloc(size + 1);
236      if (size) {
237        transport.readBytes(strbuf, size);
238      }
239      strbuf[size] = '\0';
240      ZVAL_STRINGL(return_value, strbuf, size, 0);
241      return;
242    }
243    case T_MAP: { // array of key -> value
244      uint8_t types[2];
245      uint32_t size;
246      transport.readBytes(types, 2);
247      transport.readBytes(&size, 4);
248      size = ntohl(size);
249      array_init(return_value);
250
251      for (uint32_t s = 0; s < size; ++s) {
252        zval *value;
253        MAKE_STD_ZVAL(value);
254
255        zval* key;
256        MAKE_STD_ZVAL(key);
257
258        binary_deserialize(types[0], transport, key, NULL);
259        binary_deserialize(types[1], transport, value, structType);
260        if (Z_TYPE_P(key) == IS_LONG) {
261          zend_hash_index_update(return_value->value.ht, Z_LVAL_P(key), &value, sizeof(zval *), NULL);
262        }
263        else {
264          convert_to_string_ex(&key);
265          zend_hash_update(return_value->value.ht, Z_STRVAL_P(key), Z_STRLEN_P(key) + 1, &value, sizeof(zval *), NULL);
266        }
267        zval_ptr_dtor(&key);
268      }
269      return; // return_value already populated
270    }
271    case T_LIST: { // array with autogenerated numeric keys
272      uint8_t type;
273      uint32_t size;
274      transport.readBytes(&type, 1);
275      transport.readBytes(&size, 4);
276      size = ntohl(size);
277
278      array_init(return_value);
279      for (uint32_t s = 0; s < size; ++s) {
280        zval *value;
281        MAKE_STD_ZVAL(value);
282        binary_deserialize(type, transport, value, structType);
283        zend_hash_next_index_insert(return_value->value.ht, &value, sizeof(zval *), NULL);
284      }
285      return;
286    }
287    case T_SET: { // array of key -> TRUE
288      uint8_t type;
289      uint32_t size;
290      transport.readBytes(&type, 1);
291      transport.readBytes(&size, 4);
292      size = ntohl(size);
293      array_init(return_value);
294
295      for (uint32_t s = 0; s < size; ++s) {
296        zval* key;
297        zval* value;
298        MAKE_STD_ZVAL(key);
299        MAKE_STD_ZVAL(value);
300        ZVAL_TRUE(value);
301
302        binary_deserialize(type, transport, key, NULL);
303
304        if (Z_TYPE_P(key) == IS_LONG) {
305          zend_hash_index_update(return_value->value.ht, Z_LVAL_P(key), &value, sizeof(zval *), NULL);
306        }
307        else {
308          convert_to_string_ex(&key);
309          zend_hash_update(return_value->value.ht, Z_STRVAL_P(key), Z_STRLEN_P(key) + 1, &value, sizeof(zval *), NULL);
310        }
311        zval_ptr_dtor(&key);
312      }
313      return;
314    }
315    default:
316      TSRMLS_FETCH();
317      php_error_docref(NULL TSRMLS_CC, E_ERROR, "Unknown thrift typeID %ld", thrift_typeID);
318      RETURN_NULL();
319  };
320  assert(0); // should never get here...
321}
322
323PHP_FUNCTION(thrift_protocol_binary_deserialize) {
324  int argc = ZEND_NUM_ARGS();
325  long thrift_typeID;
326
327  if (argc < 2) {
328    WRONG_PARAM_COUNT;
329  }
330
331  zval ***args = (zval***) emalloc(argc * sizeof(zval**));
332  zend_get_parameters_array_ex(argc, args);
333  convert_to_long_ex(args[0]);
334  thrift_typeID = Z_LVAL_PP(args[0]);
335
336  if (Z_TYPE_PP(args[1]) != IS_OBJECT) {
337    php_error_docref(NULL TSRMLS_CC, E_ERROR, "2nd parameter is not an object");
338    efree(args);
339    RETURN_NULL();
340  }
341 
342  char* structType = NULL;
343  if (argc >= 3) {
344    if (Z_TYPE_PP(args[2]) == IS_STRING) {
345      for (int s = 0; s < Z_STRLEN_PP(args[2]); ++s) {
346        if (isalpha(Z_STRVAL_PP(args[2])[s])) Z_STRVAL_PP(args[2])[s] = tolower(Z_STRVAL_PP(args[2])[s]);
347      }
348      structType = Z_STRVAL_PP(args[2]);
349    } else {
350      php_error_docref(NULL TSRMLS_CC, E_ERROR, "3rd parameter (if present) must be a string");
351      efree(args);
352      RETURN_NULL();
353    }
354  }
355
356  PHPTransport transport(*args[1]);
357
358  binary_deserialize(thrift_typeID, transport, return_value, structType);
359  efree(args);
360}
361