blob: 867a8aa1afc499ec839855a668355d688db74e00 [file] [log] [blame]
Mark Slee6e536442006-06-30 18:28:50 +00001<?php
2
3/** For transport operations */
4require_once THRIFT_ROOT.'/transport/TTransport.php';
5
6/**
7 * Binary implementation of the Thrift protocol.
8 *
9 * @package thrift.protocol
10 * @author Mark Slee <mcslee@facebook.com>
11 */
12class TBinaryProtocol extends TProtocol {
13
14 public function writeStructBegin($out, $name) {
15 return 0;
16 }
17
18 public function writeStructEnd($out) {
19 return 0;
20 }
21
22 public function writeFieldBegin($out, $fieldName, $fieldType, $fieldId) {
23 return
24 $this->writeByte($out, $fieldType) +
25 $this->writeI32($out, $fieldId);
26 }
27
28 public function writeFieldEnd($out) {
29 return 0;
30 }
31
32 public function writeFieldStop($out) {
33 return
34 $this->writeByte($out, TType::STOP);
35 }
36
37 public function writeMapBegin($out, $keyType, $valType, $size) {
38 return
39 $this->writeByte($out, $keyType) +
40 $this->writeByte($out, $valType) +
41 $this->writeI32($out, $size);
42 }
43
44 public function writeMapEnd($out) {
45 return 0;
46 }
47
48 public function writeListBegin($out, $elemType, $size) {
49 return
50 $this->writeByte($out, $elemType) +
51 $this->writeI32($out, $size);
52 }
53
54 public function writeListEnd($out) {
55 return 0;
56 }
57
58 public function writeSetBegin($out, $elemType, $size) {
59 return
60 $this->writeByte($out, $elemType) +
61 $this->writeI32($out, $size);
62 }
63
64 public function writeSetEnd($out) {
65 return 0;
66 }
67
68 public function writeByte($out, $byte) {
69 $data = pack('c', $byte);
70 $out->write($data, 1);
71 return 1;
72 }
73
74 public function writeI32($out, $i32) {
75 $data = pack('l', $i32);
76 //if (!defined('BIG_ENDIAN')) {
77 $data = strrev($data);
78 //}
79 $out->write($data, 4);
80 return 4;
81 }
82
83 public function writeI64($out, $i64) {
84 $hi = $i64 >> 32;
85 $lo = $i64 & 0xFFFFFFFF;
86 if (!defined('BIG_ENDIAN')) {
87 $data = pack('N2', $hi, $lo);
88 } else {
89 $data = pack('N2', $lo, $hi);
90 }
91 $out->write($data, 8);
92 return 8;
93 }
94
95 public function writeString($out, $str) {
96 $len = strlen($str);
97 $result = $this->writeI32($out, $len);
98 $out->write($str, $len);
99 return $result + $len;
100 }
101
102 public function readStructBegin($in, &$name) {
103 $name = '';
104 return 0;
105 }
106
107 public function readStructEnd($in) {
108 return 0;
109 }
110
111 public function readFieldBegin($in, &$name, &$fieldType, &$fieldId) {
112 $result = $this->readByte($in, $fieldType);
113 if ($fieldType == TType::STOP) {
114 $fieldId = 0;
115 return $result;
116 }
117 $result += $this->readI32($in, $fieldId);
118 return $result;
119 }
120
121 public function readFieldEnd($in) {
122 return 0;
123 }
124
125 public function readMapBegin($in, &$keyType, &$valType, &$size) {
126 $result = $this->readByte($in, $keyType);
127 $result += $this->readByte($in, $valType);
128 $result += $this->readI32($in, $size);
129 return $result;
130 }
131
132 public function readMapEnd($in) {
133 return 0;
134 }
135
136 public function readListBegin($in, &$elemType, &$size) {
137 $result = $this->readByte($in, $elemType);
138 $result += $this->readI32($in, $size);
139 return $result;
140 }
141
142 public function readListEnd($in) {
143 return 0;
144 }
145
146 public function readSetBegin($in, &$elemType, &$size) {
147 $result = $this->readByte($in, $elemType);
148 $result += $this->readI32($in, $size);
149 return $result;
150 }
151
152 public function readSetEnd($in) {
153 return 0;
154 }
155
156 public function readByte($in, &$byte) {
157 $data = $in->readAll(1);
158 $arr = unpack('c', $data);
159 $byte = $arr[1];
160 return 1;
161 }
162
163 public function readI32($in, &$i32) {
164 $data = $in->readAll(4);
165 if (!defined('BIG_ENDIAN')) {
166 $data = strrev($data);
167 }
168 $arr = unpack('l', $data);
169 $i32 = $arr[1];
170 return 4;
171 }
172
173 public function readI64($in, &$i64) {
174 $data = $in->readAll(8);
175 $arr = unpack('N2', $data);
176
177 // Check for a negative
178 if ($arr[1] & 0x80000000) {
179 $arr[1] = $arr[1] ^ 0xFFFFFFFF;
180 $arr[2] = $arr[2] ^ 0xFFFFFFFF;
181 $i64 = 0 - $arr[1]*4294967296 - $arr[2] - 1;
182 } else {
183 $i64 = $arr[1]*4294967296 + $arr[2];
184 }
185 return 8;
186 }
187
188 public function readString($in, &$str) {
189 $result = $this->readI32($in, $len);
190 $str = $in->readAll($len);
191 return $result + $len;
192 }
193}
194
195?>