~ubuntu-branches/debian/sid/php-cassandra/sid

« back to all changes in this revision

Viewing changes to cassandra-1.3.0/src/Inet.c

  • Committer: Package Import Robot
  • Author(s): Ondřej Surý
  • Date: 2017-04-18 17:16:30 UTC
  • Revision ID: package-import@ubuntu.com-20170418171630-fw8udixss0879s32
Tags: upstream-1.3.0
Import upstream version 1.3.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * Copyright 2015-2016 DataStax, Inc.
 
3
 *
 
4
 * Licensed under the Apache License, Version 2.0 (the "License");
 
5
 * you may not use this file except in compliance with the License.
 
6
 * You may obtain a copy of the License at
 
7
 *
 
8
 * http://www.apache.org/licenses/LICENSE-2.0
 
9
 *
 
10
 * Unless required by applicable law or agreed to in writing, software
 
11
 * distributed under the License is distributed on an "AS IS" BASIS,
 
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
13
 * See the License for the specific language governing permissions and
 
14
 * limitations under the License.
 
15
 */
 
16
 
 
17
#include "php_driver.h"
 
18
#include "php_driver_types.h"
 
19
#include "util/inet.h"
 
20
#include "util/types.h"
 
21
 
 
22
zend_class_entry *php_driver_inet_ce = NULL;
 
23
 
 
24
void
 
25
php_driver_inet_init(INTERNAL_FUNCTION_PARAMETERS)
 
26
{
 
27
  php_driver_inet *self;
 
28
  char *string;
 
29
  php5to7_size string_len;
 
30
 
 
31
  if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &string, &string_len) == FAILURE) {
 
32
    return;
 
33
  }
 
34
 
 
35
  if (getThis() && instanceof_function(Z_OBJCE_P(getThis()), php_driver_inet_ce TSRMLS_CC)) {
 
36
    self = PHP_DRIVER_GET_INET(getThis());
 
37
  } else {
 
38
    object_init_ex(return_value, php_driver_inet_ce);
 
39
    self = PHP_DRIVER_GET_INET(return_value);
 
40
  }
 
41
 
 
42
  if (!php_driver_parse_ip_address(string, &self->inet TSRMLS_CC)) {
 
43
    return;
 
44
  }
 
45
}
 
46
 
 
47
/* {{{ Inet::__construct(string) */
 
48
PHP_METHOD(Inet, __construct)
 
49
{
 
50
  php_driver_inet_init(INTERNAL_FUNCTION_PARAM_PASSTHRU);
 
51
}
 
52
/* }}} */
 
53
 
 
54
/* {{{ Inet::__toString() */
 
55
PHP_METHOD(Inet, __toString)
 
56
{
 
57
  php_driver_inet *inet = PHP_DRIVER_GET_INET(getThis());
 
58
  char *string;
 
59
  php_driver_format_address(inet->inet, &string);
 
60
 
 
61
  PHP5TO7_RETVAL_STRING(string);
 
62
  efree(string);
 
63
}
 
64
/* }}} */
 
65
 
 
66
/* {{{ Inet::type() */
 
67
PHP_METHOD(Inet, type)
 
68
{
 
69
  php5to7_zval type = php_driver_type_scalar(CASS_VALUE_TYPE_INET TSRMLS_CC);
 
70
  RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(type), 1, 1);
 
71
}
 
72
/* }}} */
 
73
 
 
74
/* {{{ Inet::address() */
 
75
PHP_METHOD(Inet, address)
 
76
{
 
77
  php_driver_inet *inet = PHP_DRIVER_GET_INET(getThis());
 
78
  char *string;
 
79
  php_driver_format_address(inet->inet, &string);
 
80
 
 
81
  PHP5TO7_RETVAL_STRING(string);
 
82
  efree(string);
 
83
}
 
84
/* }}} */
 
85
 
 
86
ZEND_BEGIN_ARG_INFO_EX(arginfo__construct, 0, ZEND_RETURN_VALUE, 1)
 
87
  ZEND_ARG_INFO(0, address)
 
88
ZEND_END_ARG_INFO()
 
89
 
 
90
ZEND_BEGIN_ARG_INFO_EX(arginfo_none, 0, ZEND_RETURN_VALUE, 0)
 
91
ZEND_END_ARG_INFO()
 
92
 
 
93
static zend_function_entry php_driver_inet_methods[] = {
 
94
  PHP_ME(Inet, __construct, arginfo__construct, ZEND_ACC_CTOR|ZEND_ACC_PUBLIC)
 
95
  PHP_ME(Inet, __toString, arginfo_none, ZEND_ACC_PUBLIC)
 
96
  PHP_ME(Inet, type, arginfo_none, ZEND_ACC_PUBLIC)
 
97
  PHP_ME(Inet, address, arginfo_none, ZEND_ACC_PUBLIC)
 
98
  PHP_FE_END
 
99
};
 
100
 
 
101
static php_driver_value_handlers php_driver_inet_handlers;
 
102
 
 
103
static HashTable *
 
104
php_driver_inet_gc(zval *object, php5to7_zval_gc table, int *n TSRMLS_DC)
 
105
{
 
106
  *table = NULL;
 
107
  *n = 0;
 
108
  return zend_std_get_properties(object TSRMLS_CC);
 
109
}
 
110
 
 
111
static HashTable *
 
112
php_driver_inet_properties(zval *object TSRMLS_DC)
 
113
{
 
114
  char *string;
 
115
  php5to7_zval type;
 
116
  php5to7_zval address;
 
117
 
 
118
  php_driver_inet *self = PHP_DRIVER_GET_INET(object);
 
119
  HashTable      *props = zend_std_get_properties(object TSRMLS_CC);
 
120
 
 
121
  type = php_driver_type_scalar(CASS_VALUE_TYPE_INET TSRMLS_CC);
 
122
  PHP5TO7_ZEND_HASH_UPDATE(props, "type", sizeof("type"), PHP5TO7_ZVAL_MAYBE_P(type), sizeof(zval));
 
123
 
 
124
  php_driver_format_address(self->inet, &string);
 
125
  PHP5TO7_ZVAL_MAYBE_MAKE(address);
 
126
  PHP5TO7_ZVAL_STRING(PHP5TO7_ZVAL_MAYBE_P(address), string);
 
127
  efree(string);
 
128
  PHP5TO7_ZEND_HASH_UPDATE(props, "address", sizeof("address"), PHP5TO7_ZVAL_MAYBE_P(address), sizeof(zval));
 
129
 
 
130
  return props;
 
131
}
 
132
 
 
133
static int
 
134
php_driver_inet_compare(zval *obj1, zval *obj2 TSRMLS_DC)
 
135
{
 
136
  php_driver_inet *inet1 = NULL;
 
137
  php_driver_inet *inet2 = NULL;
 
138
 
 
139
  if (Z_OBJCE_P(obj1) != Z_OBJCE_P(obj2))
 
140
    return 1; /* different classes */
 
141
 
 
142
  inet1 = PHP_DRIVER_GET_INET(obj1);
 
143
  inet2 = PHP_DRIVER_GET_INET(obj2);
 
144
 
 
145
  if (inet1->inet.address_length != inet2->inet.address_length) {
 
146
   return inet1->inet.address_length < inet2->inet.address_length ? -1 : 1;
 
147
  }
 
148
  return memcmp(inet1->inet.address, inet2->inet.address, inet1->inet.address_length);
 
149
}
 
150
 
 
151
static unsigned
 
152
php_driver_inet_hash_value(zval *obj TSRMLS_DC)
 
153
{
 
154
  php_driver_inet *self = PHP_DRIVER_GET_INET(obj);
 
155
  return zend_inline_hash_func((const char *) self->inet.address,
 
156
                               self->inet.address_length);
 
157
}
 
158
 
 
159
static void
 
160
php_driver_inet_free(php5to7_zend_object_free *object TSRMLS_DC)
 
161
{
 
162
  php_driver_inet *self = PHP5TO7_ZEND_OBJECT_GET(inet, object);
 
163
 
 
164
  zend_object_std_dtor(&self->zval TSRMLS_CC);
 
165
  PHP5TO7_MAYBE_EFREE(self);
 
166
}
 
167
 
 
168
static php5to7_zend_object
 
169
php_driver_inet_new(zend_class_entry *ce TSRMLS_DC)
 
170
{
 
171
  php_driver_inet *self =
 
172
      PHP5TO7_ZEND_OBJECT_ECALLOC(inet, ce);
 
173
 
 
174
  PHP5TO7_ZEND_OBJECT_INIT(inet, self, ce);
 
175
}
 
176
 
 
177
void php_driver_define_Inet(TSRMLS_D)
 
178
{
 
179
  zend_class_entry ce;
 
180
 
 
181
  INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\Inet", php_driver_inet_methods);
 
182
  php_driver_inet_ce = zend_register_internal_class(&ce TSRMLS_CC);
 
183
  zend_class_implements(php_driver_inet_ce TSRMLS_CC, 1, php_driver_value_ce);
 
184
  memcpy(&php_driver_inet_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
 
185
  php_driver_inet_handlers.std.get_properties  = php_driver_inet_properties;
 
186
#if PHP_VERSION_ID >= 50400
 
187
  php_driver_inet_handlers.std.get_gc          = php_driver_inet_gc;
 
188
#endif
 
189
  php_driver_inet_handlers.std.compare_objects = php_driver_inet_compare;
 
190
  php_driver_inet_ce->ce_flags |= PHP5TO7_ZEND_ACC_FINAL;
 
191
  php_driver_inet_ce->create_object = php_driver_inet_new;
 
192
 
 
193
  php_driver_inet_handlers.hash_value = php_driver_inet_hash_value;
 
194
  php_driver_inet_handlers.std.clone_obj = NULL;
 
195
}