@@ -1,58 +1,58 @@ |
||
1 | 1 | <?php |
2 | 2 | class AXSILPortal_V1_Auth { |
3 | - var $server; |
|
3 | + var $server; |
|
4 | 4 | |
5 | - private $url = ""; |
|
6 | - private $AAId = ""; |
|
7 | - private $apiKey = ""; |
|
5 | + private $url = ""; |
|
6 | + private $AAId = ""; |
|
7 | + private $apiKey = ""; |
|
8 | 8 | |
9 | - private $baseUrl = ""; |
|
9 | + private $baseUrl = ""; |
|
10 | 10 | |
11 | - function setUrl($url) { |
|
12 | - $this->url = $url; |
|
13 | - } |
|
11 | + function setUrl($url) { |
|
12 | + $this->url = $url; |
|
13 | + } |
|
14 | 14 | |
15 | - function setAAId($AAId) { |
|
16 | - $this->AAId = $AAId; |
|
17 | - } |
|
15 | + function setAAId($AAId) { |
|
16 | + $this->AAId = $AAId; |
|
17 | + } |
|
18 | 18 | |
19 | - function setApiKey($apiKey) { |
|
20 | - $this->apiKey = $apiKey; |
|
21 | - } |
|
19 | + function setApiKey($apiKey) { |
|
20 | + $this->apiKey = $apiKey; |
|
21 | + } |
|
22 | 22 | |
23 | 23 | |
24 | - // Verify that you can open the URL from the web server. |
|
25 | - function create() { |
|
26 | - if ($this->url != "" && $this->AAId != "" && $this->apiKey != "") { |
|
27 | - $this->baseUrl = $this->url."".$this->apiKey."/".$this->AAId; |
|
28 | - } else { |
|
29 | - die("Cannot initialize Agses webservice without credentials, please set them in settings"); |
|
30 | - } |
|
31 | - } |
|
24 | + // Verify that you can open the URL from the web server. |
|
25 | + function create() { |
|
26 | + if ($this->url != "" && $this->AAId != "" && $this->apiKey != "") { |
|
27 | + $this->baseUrl = $this->url."".$this->apiKey."/".$this->AAId; |
|
28 | + } else { |
|
29 | + die("Cannot initialize Agses webservice without credentials, please set them in settings"); |
|
30 | + } |
|
31 | + } |
|
32 | 32 | |
33 | - function createAuthenticationMessage($apn, $createFlickerCode, $returnPath, $authenticationLevel, $hedgeId) { |
|
33 | + function createAuthenticationMessage($apn, $createFlickerCode, $returnPath, $authenticationLevel, $hedgeId) { |
|
34 | 34 | |
35 | - $serviceCall = $this->baseUrl."/authmessage/".$apn."/create/".$hedgeId; |
|
35 | + $serviceCall = $this->baseUrl."/authmessage/".$apn."/create/".$hedgeId; |
|
36 | 36 | |
37 | - $json = file_get_contents($serviceCall); |
|
38 | - $response = json_decode($json, true); |
|
37 | + $json = file_get_contents($serviceCall); |
|
38 | + $response = json_decode($json, true); |
|
39 | 39 | |
40 | - return $response['flickerCode']; |
|
41 | - } |
|
42 | - |
|
43 | - /** |
|
44 | - * @param string $response |
|
45 | - * @param string $hedgeId |
|
46 | - */ |
|
47 | - function verifyResponse($apn, $response, $hedgeId) { |
|
40 | + return $response['flickerCode']; |
|
41 | + } |
|
42 | + |
|
43 | + /** |
|
44 | + * @param string $response |
|
45 | + * @param string $hedgeId |
|
46 | + */ |
|
47 | + function verifyResponse($apn, $response, $hedgeId) { |
|
48 | 48 | |
49 | - $serviceCall = $this->baseUrl."/authmessage/".$apn."/verify/".$hedgeId."/".$response; |
|
49 | + $serviceCall = $this->baseUrl."/authmessage/".$apn."/verify/".$hedgeId."/".$response; |
|
50 | 50 | |
51 | - $json = file_get_contents($serviceCall); |
|
52 | - $response = json_decode($json, true); |
|
51 | + $json = file_get_contents($serviceCall); |
|
52 | + $response = json_decode($json, true); |
|
53 | 53 | |
54 | - return $response['response']; |
|
55 | - } |
|
54 | + return $response['response']; |
|
55 | + } |
|
56 | 56 | |
57 | 57 | } |
58 | 58 | ?> |
59 | 59 | \ No newline at end of file |
@@ -4,5 +4,5 @@ |
||
4 | 4 | $up = -100; |
5 | 5 | $ut = 50; |
6 | 6 | for ($i = 0; $i <= 255; $i++) |
7 | - $cw[chr($i)] = 600; |
|
7 | + $cw[chr($i)] = 600; |
|
8 | 8 | ?> |
@@ -4,5 +4,5 @@ |
||
4 | 4 | $up = -100; |
5 | 5 | $ut = 50; |
6 | 6 | for ($i = 0; $i <= 255; $i++) |
7 | - $cw[chr($i)] = 600; |
|
7 | + $cw[chr($i)] = 600; |
|
8 | 8 | ?> |
@@ -69,41 +69,41 @@ discard block |
||
69 | 69 | var $defaultWidth; |
70 | 70 | var $maxStrLenRead; |
71 | 71 | |
72 | - function __construct() { |
|
73 | - $this->maxStrLenRead = 200000; // Maximum size of glyf table to read in as string (otherwise reads each glyph from file) |
|
74 | - } |
|
75 | - |
|
76 | - |
|
77 | - /** |
|
78 | - * @param string $file |
|
79 | - */ |
|
80 | - function getMetrics($file) { |
|
81 | - $this->filename = $file; |
|
82 | - $this->fh = fopen($file,'rb') or die('Can\'t open file ' . $file); |
|
83 | - $this->_pos = 0; |
|
84 | - $this->charWidths = ''; |
|
85 | - $this->glyphPos = array(); |
|
86 | - $this->charToGlyph = array(); |
|
87 | - $this->tables = array(); |
|
88 | - $this->otables = array(); |
|
89 | - $this->ascent = 0; |
|
90 | - $this->descent = 0; |
|
91 | - $this->TTCFonts = array(); |
|
92 | - $this->version = $version = $this->read_ulong(); |
|
93 | - if ($version == 0x4F54544F) |
|
94 | - die("Postscript outlines are not supported"); |
|
95 | - if ($version == 0x74746366) |
|
96 | - die("ERROR - TrueType Fonts Collections not supported"); |
|
97 | - if (!in_array($version, array(0x00010000, 0x74727565))) |
|
98 | - die("Not a TrueType font: version=".$version); |
|
99 | - $this->readTableDirectory(); |
|
100 | - $this->extractInfo(); |
|
101 | - fclose($this->fh); |
|
102 | - } |
|
103 | - |
|
104 | - |
|
105 | - function readTableDirectory() { |
|
106 | - $this->numTables = $this->read_ushort(); |
|
72 | + function __construct() { |
|
73 | + $this->maxStrLenRead = 200000; // Maximum size of glyf table to read in as string (otherwise reads each glyph from file) |
|
74 | + } |
|
75 | + |
|
76 | + |
|
77 | + /** |
|
78 | + * @param string $file |
|
79 | + */ |
|
80 | + function getMetrics($file) { |
|
81 | + $this->filename = $file; |
|
82 | + $this->fh = fopen($file,'rb') or die('Can\'t open file ' . $file); |
|
83 | + $this->_pos = 0; |
|
84 | + $this->charWidths = ''; |
|
85 | + $this->glyphPos = array(); |
|
86 | + $this->charToGlyph = array(); |
|
87 | + $this->tables = array(); |
|
88 | + $this->otables = array(); |
|
89 | + $this->ascent = 0; |
|
90 | + $this->descent = 0; |
|
91 | + $this->TTCFonts = array(); |
|
92 | + $this->version = $version = $this->read_ulong(); |
|
93 | + if ($version == 0x4F54544F) |
|
94 | + die("Postscript outlines are not supported"); |
|
95 | + if ($version == 0x74746366) |
|
96 | + die("ERROR - TrueType Fonts Collections not supported"); |
|
97 | + if (!in_array($version, array(0x00010000, 0x74727565))) |
|
98 | + die("Not a TrueType font: version=".$version); |
|
99 | + $this->readTableDirectory(); |
|
100 | + $this->extractInfo(); |
|
101 | + fclose($this->fh); |
|
102 | + } |
|
103 | + |
|
104 | + |
|
105 | + function readTableDirectory() { |
|
106 | + $this->numTables = $this->read_ushort(); |
|
107 | 107 | $this->searchRange = $this->read_ushort(); |
108 | 108 | $this->entrySelector = $this->read_ushort(); |
109 | 109 | $this->rangeShift = $this->read_ushort(); |
@@ -115,178 +115,178 @@ discard block |
||
115 | 115 | $record['offset'] = $this->read_ulong(); |
116 | 116 | $record['length'] = $this->read_ulong(); |
117 | 117 | $this->tables[$record['tag']] = $record; |
118 | - } |
|
119 | - } |
|
120 | - |
|
121 | - |
|
122 | - /** |
|
123 | - * @param integer[] $x |
|
124 | - * @param integer[] $y |
|
125 | - */ |
|
126 | - function sub32($x, $y) { |
|
127 | - $xlo = $x[1]; |
|
128 | - $xhi = $x[0]; |
|
129 | - $ylo = $y[1]; |
|
130 | - $yhi = $y[0]; |
|
131 | - if ($ylo > $xlo) { $xlo += 1 << 16; $yhi += 1; } |
|
132 | - $reslo = $xlo - $ylo; |
|
133 | - if ($yhi > $xhi) { $xhi += 1 << 16; } |
|
134 | - $reshi = $xhi - $yhi; |
|
135 | - $reshi = $reshi & 0xFFFF; |
|
136 | - return array($reshi, $reslo); |
|
137 | - } |
|
138 | - |
|
139 | - function calcChecksum($data) { |
|
140 | - if (strlen($data) % 4) { $data .= str_repeat("\0", (4 - (strlen($data) % 4))); } |
|
141 | - $hi = 0x0000; |
|
142 | - $lo = 0x0000; |
|
143 | - for ($i = 0; $i < strlen($data); $i += 4) { |
|
144 | - $hi += (ord($data[$i]) << 8) + ord($data[$i + 1]); |
|
145 | - $lo += (ord($data[$i + 2]) << 8) + ord($data[$i + 3]); |
|
146 | - $hi += $lo >> 16; |
|
147 | - $lo = $lo & 0xFFFF; |
|
148 | - $hi = $hi & 0xFFFF; |
|
149 | - } |
|
150 | - return array($hi, $lo); |
|
151 | - } |
|
152 | - |
|
153 | - function get_table_pos($tag) { |
|
154 | - $offset = $this->tables[$tag]['offset']; |
|
155 | - $length = $this->tables[$tag]['length']; |
|
156 | - return array($offset, $length); |
|
157 | - } |
|
158 | - |
|
159 | - function seek($pos) { |
|
160 | - $this->_pos = $pos; |
|
161 | - fseek($this->fh,$this->_pos); |
|
162 | - } |
|
163 | - |
|
164 | - /** |
|
165 | - * @param integer $delta |
|
166 | - */ |
|
167 | - function skip($delta) { |
|
168 | - $this->_pos = $this->_pos + $delta; |
|
169 | - fseek($this->fh,$this->_pos); |
|
170 | - } |
|
171 | - |
|
172 | - /** |
|
173 | - * @param string $tag |
|
174 | - */ |
|
175 | - function seek_table($tag, $offset_in_table = 0) { |
|
176 | - $tpos = $this->get_table_pos($tag); |
|
177 | - $this->_pos = $tpos[0] + $offset_in_table; |
|
178 | - fseek($this->fh, $this->_pos); |
|
179 | - return $this->_pos; |
|
180 | - } |
|
181 | - |
|
182 | - function read_tag() { |
|
183 | - $this->_pos += 4; |
|
184 | - return fread($this->fh, 4); |
|
185 | - } |
|
186 | - |
|
187 | - function read_short() { |
|
188 | - $this->_pos += 2; |
|
189 | - $s = fread($this->fh, 2); |
|
190 | - $a = (ord($s[0]) << 8) + ord($s[1]); |
|
191 | - if ($a & (1 << 15)) { $a = ($a - (1 << 16)); } |
|
192 | - return $a; |
|
193 | - } |
|
194 | - |
|
195 | - function unpack_short($s) { |
|
196 | - $a = (ord($s[0]) << 8) + ord($s[1]); |
|
197 | - if ($a & (1 << 15)) { |
|
198 | - $a = ($a - (1 << 16)); |
|
199 | - } |
|
200 | - return $a; |
|
201 | - } |
|
202 | - |
|
203 | - function read_ushort() { |
|
204 | - $this->_pos += 2; |
|
205 | - $s = fread($this->fh,2); |
|
206 | - return (ord($s[0])<<8) + ord($s[1]); |
|
207 | - } |
|
208 | - |
|
209 | - function read_ulong() { |
|
210 | - $this->_pos += 4; |
|
211 | - $s = fread($this->fh,4); |
|
212 | - // if large uInt32 as an integer, PHP converts it to -ve |
|
213 | - return (ord($s[0])*16777216) + (ord($s[1])<<16) + (ord($s[2])<<8) + ord($s[3]); // 16777216 = 1<<24 |
|
214 | - } |
|
215 | - |
|
216 | - function get_ushort($pos) { |
|
217 | - fseek($this->fh,$pos); |
|
218 | - $s = fread($this->fh,2); |
|
219 | - return (ord($s[0])<<8) + ord($s[1]); |
|
220 | - } |
|
221 | - |
|
222 | - function get_ulong($pos) { |
|
223 | - fseek($this->fh,$pos); |
|
224 | - $s = fread($this->fh,4); |
|
225 | - // iF large uInt32 as an integer, PHP converts it to -ve |
|
226 | - return (ord($s[0])*16777216) + (ord($s[1])<<16) + (ord($s[2])<<8) + ord($s[3]); // 16777216 = 1<<24 |
|
227 | - } |
|
228 | - |
|
229 | - function pack_short($val) { |
|
230 | - if ($val<0) { |
|
231 | - $val = abs($val); |
|
232 | - $val = ~$val; |
|
233 | - $val += 1; |
|
234 | - } |
|
235 | - return pack("n",$val); |
|
236 | - } |
|
237 | - |
|
238 | - /** |
|
239 | - * @param string $value |
|
240 | - */ |
|
241 | - function splice($stream, $offset, $value) { |
|
242 | - return substr($stream,0,$offset) . $value . substr($stream,$offset+strlen($value)); |
|
243 | - } |
|
244 | - |
|
245 | - /** |
|
246 | - * @param string|null $stream |
|
247 | - * @param integer $offset |
|
248 | - */ |
|
249 | - function _set_ushort($stream, $offset, $value) { |
|
250 | - $up = pack("n", $value); |
|
251 | - return $this->splice($stream, $offset, $up); |
|
252 | - } |
|
253 | - |
|
254 | - function _set_short($stream, $offset, $val) { |
|
255 | - if ($val<0) { |
|
256 | - $val = abs($val); |
|
257 | - $val = ~$val; |
|
258 | - $val += 1; |
|
259 | - } |
|
260 | - $up = pack("n",$val); |
|
261 | - return $this->splice($stream, $offset, $up); |
|
262 | - } |
|
263 | - |
|
264 | - function get_chunk($pos, $length) { |
|
265 | - fseek($this->fh,$pos); |
|
266 | - if ($length <1) { return ''; } |
|
267 | - return (fread($this->fh,$length)); |
|
268 | - } |
|
269 | - |
|
270 | - /** |
|
271 | - * @param string $tag |
|
272 | - */ |
|
273 | - function get_table($tag) { |
|
274 | - list($pos, $length) = $this->get_table_pos($tag); |
|
275 | - if ($length == 0) { die('Truetype font ('.$this->filename.'): error reading table: '.$tag); } |
|
276 | - fseek($this->fh,$pos); |
|
277 | - return (fread($this->fh,$length)); |
|
278 | - } |
|
279 | - |
|
280 | - /** |
|
281 | - * @param string $tag |
|
282 | - * @param null|string $data |
|
283 | - */ |
|
284 | - function add($tag, $data) { |
|
285 | - if ($tag == 'head') { |
|
286 | - $data = $this->splice($data, 8, "\0\0\0\0"); |
|
287 | - } |
|
288 | - $this->otables[$tag] = $data; |
|
289 | - } |
|
118 | + } |
|
119 | + } |
|
120 | + |
|
121 | + |
|
122 | + /** |
|
123 | + * @param integer[] $x |
|
124 | + * @param integer[] $y |
|
125 | + */ |
|
126 | + function sub32($x, $y) { |
|
127 | + $xlo = $x[1]; |
|
128 | + $xhi = $x[0]; |
|
129 | + $ylo = $y[1]; |
|
130 | + $yhi = $y[0]; |
|
131 | + if ($ylo > $xlo) { $xlo += 1 << 16; $yhi += 1; } |
|
132 | + $reslo = $xlo - $ylo; |
|
133 | + if ($yhi > $xhi) { $xhi += 1 << 16; } |
|
134 | + $reshi = $xhi - $yhi; |
|
135 | + $reshi = $reshi & 0xFFFF; |
|
136 | + return array($reshi, $reslo); |
|
137 | + } |
|
138 | + |
|
139 | + function calcChecksum($data) { |
|
140 | + if (strlen($data) % 4) { $data .= str_repeat("\0", (4 - (strlen($data) % 4))); } |
|
141 | + $hi = 0x0000; |
|
142 | + $lo = 0x0000; |
|
143 | + for ($i = 0; $i < strlen($data); $i += 4) { |
|
144 | + $hi += (ord($data[$i]) << 8) + ord($data[$i + 1]); |
|
145 | + $lo += (ord($data[$i + 2]) << 8) + ord($data[$i + 3]); |
|
146 | + $hi += $lo >> 16; |
|
147 | + $lo = $lo & 0xFFFF; |
|
148 | + $hi = $hi & 0xFFFF; |
|
149 | + } |
|
150 | + return array($hi, $lo); |
|
151 | + } |
|
152 | + |
|
153 | + function get_table_pos($tag) { |
|
154 | + $offset = $this->tables[$tag]['offset']; |
|
155 | + $length = $this->tables[$tag]['length']; |
|
156 | + return array($offset, $length); |
|
157 | + } |
|
158 | + |
|
159 | + function seek($pos) { |
|
160 | + $this->_pos = $pos; |
|
161 | + fseek($this->fh,$this->_pos); |
|
162 | + } |
|
163 | + |
|
164 | + /** |
|
165 | + * @param integer $delta |
|
166 | + */ |
|
167 | + function skip($delta) { |
|
168 | + $this->_pos = $this->_pos + $delta; |
|
169 | + fseek($this->fh,$this->_pos); |
|
170 | + } |
|
171 | + |
|
172 | + /** |
|
173 | + * @param string $tag |
|
174 | + */ |
|
175 | + function seek_table($tag, $offset_in_table = 0) { |
|
176 | + $tpos = $this->get_table_pos($tag); |
|
177 | + $this->_pos = $tpos[0] + $offset_in_table; |
|
178 | + fseek($this->fh, $this->_pos); |
|
179 | + return $this->_pos; |
|
180 | + } |
|
181 | + |
|
182 | + function read_tag() { |
|
183 | + $this->_pos += 4; |
|
184 | + return fread($this->fh, 4); |
|
185 | + } |
|
186 | + |
|
187 | + function read_short() { |
|
188 | + $this->_pos += 2; |
|
189 | + $s = fread($this->fh, 2); |
|
190 | + $a = (ord($s[0]) << 8) + ord($s[1]); |
|
191 | + if ($a & (1 << 15)) { $a = ($a - (1 << 16)); } |
|
192 | + return $a; |
|
193 | + } |
|
194 | + |
|
195 | + function unpack_short($s) { |
|
196 | + $a = (ord($s[0]) << 8) + ord($s[1]); |
|
197 | + if ($a & (1 << 15)) { |
|
198 | + $a = ($a - (1 << 16)); |
|
199 | + } |
|
200 | + return $a; |
|
201 | + } |
|
202 | + |
|
203 | + function read_ushort() { |
|
204 | + $this->_pos += 2; |
|
205 | + $s = fread($this->fh,2); |
|
206 | + return (ord($s[0])<<8) + ord($s[1]); |
|
207 | + } |
|
208 | + |
|
209 | + function read_ulong() { |
|
210 | + $this->_pos += 4; |
|
211 | + $s = fread($this->fh,4); |
|
212 | + // if large uInt32 as an integer, PHP converts it to -ve |
|
213 | + return (ord($s[0])*16777216) + (ord($s[1])<<16) + (ord($s[2])<<8) + ord($s[3]); // 16777216 = 1<<24 |
|
214 | + } |
|
215 | + |
|
216 | + function get_ushort($pos) { |
|
217 | + fseek($this->fh,$pos); |
|
218 | + $s = fread($this->fh,2); |
|
219 | + return (ord($s[0])<<8) + ord($s[1]); |
|
220 | + } |
|
221 | + |
|
222 | + function get_ulong($pos) { |
|
223 | + fseek($this->fh,$pos); |
|
224 | + $s = fread($this->fh,4); |
|
225 | + // iF large uInt32 as an integer, PHP converts it to -ve |
|
226 | + return (ord($s[0])*16777216) + (ord($s[1])<<16) + (ord($s[2])<<8) + ord($s[3]); // 16777216 = 1<<24 |
|
227 | + } |
|
228 | + |
|
229 | + function pack_short($val) { |
|
230 | + if ($val<0) { |
|
231 | + $val = abs($val); |
|
232 | + $val = ~$val; |
|
233 | + $val += 1; |
|
234 | + } |
|
235 | + return pack("n",$val); |
|
236 | + } |
|
237 | + |
|
238 | + /** |
|
239 | + * @param string $value |
|
240 | + */ |
|
241 | + function splice($stream, $offset, $value) { |
|
242 | + return substr($stream,0,$offset) . $value . substr($stream,$offset+strlen($value)); |
|
243 | + } |
|
244 | + |
|
245 | + /** |
|
246 | + * @param string|null $stream |
|
247 | + * @param integer $offset |
|
248 | + */ |
|
249 | + function _set_ushort($stream, $offset, $value) { |
|
250 | + $up = pack("n", $value); |
|
251 | + return $this->splice($stream, $offset, $up); |
|
252 | + } |
|
253 | + |
|
254 | + function _set_short($stream, $offset, $val) { |
|
255 | + if ($val<0) { |
|
256 | + $val = abs($val); |
|
257 | + $val = ~$val; |
|
258 | + $val += 1; |
|
259 | + } |
|
260 | + $up = pack("n",$val); |
|
261 | + return $this->splice($stream, $offset, $up); |
|
262 | + } |
|
263 | + |
|
264 | + function get_chunk($pos, $length) { |
|
265 | + fseek($this->fh,$pos); |
|
266 | + if ($length <1) { return ''; } |
|
267 | + return (fread($this->fh,$length)); |
|
268 | + } |
|
269 | + |
|
270 | + /** |
|
271 | + * @param string $tag |
|
272 | + */ |
|
273 | + function get_table($tag) { |
|
274 | + list($pos, $length) = $this->get_table_pos($tag); |
|
275 | + if ($length == 0) { die('Truetype font ('.$this->filename.'): error reading table: '.$tag); } |
|
276 | + fseek($this->fh,$pos); |
|
277 | + return (fread($this->fh,$length)); |
|
278 | + } |
|
279 | + |
|
280 | + /** |
|
281 | + * @param string $tag |
|
282 | + * @param null|string $data |
|
283 | + */ |
|
284 | + function add($tag, $data) { |
|
285 | + if ($tag == 'head') { |
|
286 | + $data = $this->splice($data, 8, "\0\0\0\0"); |
|
287 | + } |
|
288 | + $this->otables[$tag] = $data; |
|
289 | + } |
|
290 | 290 | |
291 | 291 | |
292 | 292 | |
@@ -295,841 +295,841 @@ discard block |
||
295 | 295 | |
296 | 296 | ///////////////////////////////////////////////////////////////////////////////////////// |
297 | 297 | |
298 | - function extractInfo() { |
|
299 | - /////////////////////////////////// |
|
300 | - // name - Naming table |
|
301 | - /////////////////////////////////// |
|
302 | - $this->sFamilyClass = 0; |
|
303 | - $this->sFamilySubClass = 0; |
|
304 | - |
|
305 | - $name_offset = $this->seek_table("name"); |
|
306 | - $format = $this->read_ushort(); |
|
307 | - if ($format != 0) { |
|
308 | - die("Unknown name table format ".$format); |
|
309 | - } |
|
310 | - $numRecords = $this->read_ushort(); |
|
311 | - $string_data_offset = $name_offset + $this->read_ushort(); |
|
312 | - $names = array(1=>'', 2=>'', 3=>'', 4=>'', 6=>''); |
|
313 | - $K = array_keys($names); |
|
314 | - $nameCount = count($names); |
|
315 | - for ($i = 0; $i < $numRecords; $i++) { |
|
316 | - $platformId = $this->read_ushort(); |
|
317 | - $encodingId = $this->read_ushort(); |
|
318 | - $languageId = $this->read_ushort(); |
|
319 | - $nameId = $this->read_ushort(); |
|
320 | - $length = $this->read_ushort(); |
|
321 | - $offset = $this->read_ushort(); |
|
322 | - if (!in_array($nameId, $K)) continue; |
|
323 | - $N = ''; |
|
324 | - if ($platformId == 3 && $encodingId == 1 && $languageId == 0x409) { // Microsoft, Unicode, US English, PS Name |
|
325 | - $opos = $this->_pos; |
|
326 | - $this->seek($string_data_offset + $offset); |
|
327 | - if ($length % 2 != 0) |
|
328 | - die("PostScript name is UTF-16BE string of odd length"); |
|
329 | - $length /= 2; |
|
330 | - $N = ''; |
|
331 | - while ($length > 0) { |
|
332 | - $char = $this->read_ushort(); |
|
333 | - $N .= (chr($char)); |
|
334 | - $length -= 1; |
|
335 | - } |
|
336 | - $this->_pos = $opos; |
|
337 | - $this->seek($opos); |
|
338 | - } else if ($platformId == 1 && $encodingId == 0 && $languageId == 0) { // Macintosh, Roman, English, PS Name |
|
339 | - $opos = $this->_pos; |
|
340 | - $N = $this->get_chunk($string_data_offset + $offset, $length); |
|
341 | - $this->_pos = $opos; |
|
342 | - $this->seek($opos); |
|
343 | - } |
|
344 | - if ($N && $names[$nameId] == '') { |
|
345 | - $names[$nameId] = $N; |
|
346 | - $nameCount -= 1; |
|
347 | - if ($nameCount == 0) break; |
|
348 | - } |
|
349 | - } |
|
350 | - if ($names[6]) |
|
351 | - $psName = $names[6]; |
|
352 | - else if ($names[4]) |
|
353 | - $psName = preg_replace('/ /', '-', $names[4]); |
|
354 | - else if ($names[1]) |
|
355 | - $psName = preg_replace('/ /', '-', $names[1]); |
|
356 | - else |
|
357 | - $psName = ''; |
|
358 | - if (!$psName) |
|
359 | - die("Could not find PostScript font name"); |
|
360 | - $this->name = $psName; |
|
361 | - if ($names[1]) { $this->familyName = $names[1]; } else { $this->familyName = $psName; } |
|
362 | - if ($names[2]) { $this->styleName = $names[2]; } else { $this->styleName = 'Regular'; } |
|
363 | - if ($names[4]) { $this->fullName = $names[4]; } else { $this->fullName = $psName; } |
|
364 | - if ($names[3]) { $this->uniqueFontID = $names[3]; } else { $this->uniqueFontID = $psName; } |
|
365 | - if ($names[6]) { $this->fullName = $names[6]; } |
|
366 | - |
|
367 | - /////////////////////////////////// |
|
368 | - // head - Font header table |
|
369 | - /////////////////////////////////// |
|
370 | - $this->seek_table("head"); |
|
371 | - $this->skip(18); |
|
372 | - $this->unitsPerEm = $unitsPerEm = $this->read_ushort(); |
|
373 | - $scale = 1000 / $unitsPerEm; |
|
374 | - $this->skip(16); |
|
375 | - $xMin = $this->read_short(); |
|
376 | - $yMin = $this->read_short(); |
|
377 | - $xMax = $this->read_short(); |
|
378 | - $yMax = $this->read_short(); |
|
379 | - $this->bbox = array(($xMin * $scale), ($yMin * $scale), ($xMax * $scale), ($yMax * $scale)); |
|
380 | - $this->skip(3 * 2); |
|
381 | - $indexToLocFormat = $this->read_ushort(); |
|
382 | - $glyphDataFormat = $this->read_ushort(); |
|
383 | - if ($glyphDataFormat != 0) { |
|
384 | - die('Unknown glyph data format '.$glyphDataFormat); |
|
385 | - } |
|
386 | - |
|
387 | - /////////////////////////////////// |
|
388 | - // hhea metrics table |
|
389 | - /////////////////////////////////// |
|
390 | - // ttf2t1 seems to use this value rather than the one in OS/2 - so put in for compatibility |
|
391 | - if (isset($this->tables["hhea"])) { |
|
392 | - $this->seek_table("hhea"); |
|
393 | - $this->skip(4); |
|
394 | - $hheaAscender = $this->read_short(); |
|
395 | - $hheaDescender = $this->read_short(); |
|
396 | - $this->ascent = ($hheaAscender * $scale); |
|
397 | - $this->descent = ($hheaDescender * $scale); |
|
398 | - } |
|
399 | - |
|
400 | - /////////////////////////////////// |
|
401 | - // OS/2 - OS/2 and Windows metrics table |
|
402 | - /////////////////////////////////// |
|
403 | - if (isset($this->tables["OS/2"])) { |
|
404 | - $this->seek_table("OS/2"); |
|
405 | - $version = $this->read_ushort(); |
|
406 | - $this->skip(2); |
|
407 | - $usWeightClass = $this->read_ushort(); |
|
408 | - $this->skip(2); |
|
409 | - $fsType = $this->read_ushort(); |
|
410 | - if ($fsType == 0x0002 || ($fsType & 0x0300) != 0) { |
|
411 | - die('ERROR - Font file '.$this->filename.' cannot be embedded due to copyright restrictions.'); |
|
412 | - $this->restrictedUse = true; |
|
413 | - } |
|
414 | - $this->skip(20); |
|
415 | - $sF = $this->read_short(); |
|
416 | - $this->sFamilyClass = ($sF >> 8); |
|
417 | - $this->sFamilySubClass = ($sF & 0xFF); |
|
418 | - $this->_pos += 10; //PANOSE = 10 byte length |
|
419 | - $panose = fread($this->fh, 10); |
|
420 | - $this->skip(26); |
|
421 | - $sTypoAscender = $this->read_short(); |
|
422 | - $sTypoDescender = $this->read_short(); |
|
423 | - if (!$this->ascent) $this->ascent = ($sTypoAscender * $scale); |
|
424 | - if (!$this->descent) $this->descent = ($sTypoDescender * $scale); |
|
425 | - if ($version > 1) { |
|
426 | - $this->skip(16); |
|
427 | - $sCapHeight = $this->read_short(); |
|
428 | - $this->capHeight = ($sCapHeight * $scale); |
|
429 | - } |
|
430 | - else { |
|
431 | - $this->capHeight = $this->ascent; |
|
432 | - } |
|
433 | - } |
|
434 | - else { |
|
435 | - $usWeightClass = 500; |
|
436 | - if (!$this->ascent) $this->ascent = ($yMax * $scale); |
|
437 | - if (!$this->descent) $this->descent = ($yMin * $scale); |
|
438 | - $this->capHeight = $this->ascent; |
|
439 | - } |
|
440 | - $this->stemV = 50 + intval(pow(($usWeightClass / 65.0), 2)); |
|
441 | - |
|
442 | - /////////////////////////////////// |
|
443 | - // post - PostScript table |
|
444 | - /////////////////////////////////// |
|
445 | - $this->seek_table("post"); |
|
446 | - $this->skip(4); |
|
447 | - $this->italicAngle = $this->read_short() + $this->read_ushort() / 65536.0; |
|
448 | - $this->underlinePosition = $this->read_short() * $scale; |
|
449 | - $this->underlineThickness = $this->read_short() * $scale; |
|
450 | - $isFixedPitch = $this->read_ulong(); |
|
451 | - |
|
452 | - $this->flags = 4; |
|
453 | - |
|
454 | - if ($this->italicAngle != 0) |
|
455 | - $this->flags = $this->flags | 64; |
|
456 | - if ($usWeightClass >= 600) |
|
457 | - $this->flags = $this->flags | 262144; |
|
458 | - if ($isFixedPitch) |
|
459 | - $this->flags = $this->flags | 1; |
|
460 | - |
|
461 | - /////////////////////////////////// |
|
462 | - // hhea - Horizontal header table |
|
463 | - /////////////////////////////////// |
|
464 | - $this->seek_table("hhea"); |
|
465 | - $this->skip(32); |
|
466 | - $metricDataFormat = $this->read_ushort(); |
|
467 | - if ($metricDataFormat != 0) { |
|
468 | - die('Unknown horizontal metric data format '.$metricDataFormat); |
|
469 | - } |
|
470 | - $numberOfHMetrics = $this->read_ushort(); |
|
471 | - if ($numberOfHMetrics == 0) { |
|
472 | - die('Number of horizontal metrics is 0'); |
|
473 | - } |
|
474 | - |
|
475 | - /////////////////////////////////// |
|
476 | - // maxp - Maximum profile table |
|
477 | - /////////////////////////////////// |
|
478 | - $this->seek_table("maxp"); |
|
479 | - $this->skip(4); |
|
480 | - $numGlyphs = $this->read_ushort(); |
|
481 | - |
|
482 | - |
|
483 | - /////////////////////////////////// |
|
484 | - // cmap - Character to glyph index mapping table |
|
485 | - /////////////////////////////////// |
|
486 | - $cmap_offset = $this->seek_table("cmap"); |
|
487 | - $this->skip(2); |
|
488 | - $cmapTableCount = $this->read_ushort(); |
|
489 | - $unicode_cmap_offset = 0; |
|
490 | - for ($i = 0; $i < $cmapTableCount; $i++) { |
|
491 | - $platformID = $this->read_ushort(); |
|
492 | - $encodingID = $this->read_ushort(); |
|
493 | - $offset = $this->read_ulong(); |
|
494 | - $save_pos = $this->_pos; |
|
495 | - if (($platformID == 3 && $encodingID == 1) || $platformID == 0) { // Microsoft, Unicode |
|
496 | - $format = $this->get_ushort($cmap_offset + $offset); |
|
497 | - if ($format == 4) { |
|
498 | - if (!$unicode_cmap_offset) $unicode_cmap_offset = $cmap_offset + $offset; |
|
499 | - break; |
|
500 | - } |
|
501 | - } |
|
502 | - $this->seek($save_pos); |
|
503 | - } |
|
504 | - if (!$unicode_cmap_offset) |
|
505 | - die('Font ('.$this->filename.') does not have cmap for Unicode (platform 3, encoding 1, format 4, or platform 0, any encoding, format 4)'); |
|
506 | - |
|
507 | - |
|
508 | - $glyphToChar = array(); |
|
509 | - $charToGlyph = array(); |
|
510 | - $this->getCMAP4($unicode_cmap_offset, $glyphToChar, $charToGlyph); |
|
511 | - |
|
512 | - /////////////////////////////////// |
|
513 | - // hmtx - Horizontal metrics table |
|
514 | - /////////////////////////////////// |
|
515 | - $this->getHMTX($numberOfHMetrics, $numGlyphs, $glyphToChar, $scale); |
|
516 | - |
|
517 | - } |
|
298 | + function extractInfo() { |
|
299 | + /////////////////////////////////// |
|
300 | + // name - Naming table |
|
301 | + /////////////////////////////////// |
|
302 | + $this->sFamilyClass = 0; |
|
303 | + $this->sFamilySubClass = 0; |
|
304 | + |
|
305 | + $name_offset = $this->seek_table("name"); |
|
306 | + $format = $this->read_ushort(); |
|
307 | + if ($format != 0) { |
|
308 | + die("Unknown name table format ".$format); |
|
309 | + } |
|
310 | + $numRecords = $this->read_ushort(); |
|
311 | + $string_data_offset = $name_offset + $this->read_ushort(); |
|
312 | + $names = array(1=>'', 2=>'', 3=>'', 4=>'', 6=>''); |
|
313 | + $K = array_keys($names); |
|
314 | + $nameCount = count($names); |
|
315 | + for ($i = 0; $i < $numRecords; $i++) { |
|
316 | + $platformId = $this->read_ushort(); |
|
317 | + $encodingId = $this->read_ushort(); |
|
318 | + $languageId = $this->read_ushort(); |
|
319 | + $nameId = $this->read_ushort(); |
|
320 | + $length = $this->read_ushort(); |
|
321 | + $offset = $this->read_ushort(); |
|
322 | + if (!in_array($nameId, $K)) continue; |
|
323 | + $N = ''; |
|
324 | + if ($platformId == 3 && $encodingId == 1 && $languageId == 0x409) { // Microsoft, Unicode, US English, PS Name |
|
325 | + $opos = $this->_pos; |
|
326 | + $this->seek($string_data_offset + $offset); |
|
327 | + if ($length % 2 != 0) |
|
328 | + die("PostScript name is UTF-16BE string of odd length"); |
|
329 | + $length /= 2; |
|
330 | + $N = ''; |
|
331 | + while ($length > 0) { |
|
332 | + $char = $this->read_ushort(); |
|
333 | + $N .= (chr($char)); |
|
334 | + $length -= 1; |
|
335 | + } |
|
336 | + $this->_pos = $opos; |
|
337 | + $this->seek($opos); |
|
338 | + } else if ($platformId == 1 && $encodingId == 0 && $languageId == 0) { // Macintosh, Roman, English, PS Name |
|
339 | + $opos = $this->_pos; |
|
340 | + $N = $this->get_chunk($string_data_offset + $offset, $length); |
|
341 | + $this->_pos = $opos; |
|
342 | + $this->seek($opos); |
|
343 | + } |
|
344 | + if ($N && $names[$nameId] == '') { |
|
345 | + $names[$nameId] = $N; |
|
346 | + $nameCount -= 1; |
|
347 | + if ($nameCount == 0) break; |
|
348 | + } |
|
349 | + } |
|
350 | + if ($names[6]) |
|
351 | + $psName = $names[6]; |
|
352 | + else if ($names[4]) |
|
353 | + $psName = preg_replace('/ /', '-', $names[4]); |
|
354 | + else if ($names[1]) |
|
355 | + $psName = preg_replace('/ /', '-', $names[1]); |
|
356 | + else |
|
357 | + $psName = ''; |
|
358 | + if (!$psName) |
|
359 | + die("Could not find PostScript font name"); |
|
360 | + $this->name = $psName; |
|
361 | + if ($names[1]) { $this->familyName = $names[1]; } else { $this->familyName = $psName; } |
|
362 | + if ($names[2]) { $this->styleName = $names[2]; } else { $this->styleName = 'Regular'; } |
|
363 | + if ($names[4]) { $this->fullName = $names[4]; } else { $this->fullName = $psName; } |
|
364 | + if ($names[3]) { $this->uniqueFontID = $names[3]; } else { $this->uniqueFontID = $psName; } |
|
365 | + if ($names[6]) { $this->fullName = $names[6]; } |
|
366 | + |
|
367 | + /////////////////////////////////// |
|
368 | + // head - Font header table |
|
369 | + /////////////////////////////////// |
|
370 | + $this->seek_table("head"); |
|
371 | + $this->skip(18); |
|
372 | + $this->unitsPerEm = $unitsPerEm = $this->read_ushort(); |
|
373 | + $scale = 1000 / $unitsPerEm; |
|
374 | + $this->skip(16); |
|
375 | + $xMin = $this->read_short(); |
|
376 | + $yMin = $this->read_short(); |
|
377 | + $xMax = $this->read_short(); |
|
378 | + $yMax = $this->read_short(); |
|
379 | + $this->bbox = array(($xMin * $scale), ($yMin * $scale), ($xMax * $scale), ($yMax * $scale)); |
|
380 | + $this->skip(3 * 2); |
|
381 | + $indexToLocFormat = $this->read_ushort(); |
|
382 | + $glyphDataFormat = $this->read_ushort(); |
|
383 | + if ($glyphDataFormat != 0) { |
|
384 | + die('Unknown glyph data format '.$glyphDataFormat); |
|
385 | + } |
|
386 | + |
|
387 | + /////////////////////////////////// |
|
388 | + // hhea metrics table |
|
389 | + /////////////////////////////////// |
|
390 | + // ttf2t1 seems to use this value rather than the one in OS/2 - so put in for compatibility |
|
391 | + if (isset($this->tables["hhea"])) { |
|
392 | + $this->seek_table("hhea"); |
|
393 | + $this->skip(4); |
|
394 | + $hheaAscender = $this->read_short(); |
|
395 | + $hheaDescender = $this->read_short(); |
|
396 | + $this->ascent = ($hheaAscender * $scale); |
|
397 | + $this->descent = ($hheaDescender * $scale); |
|
398 | + } |
|
399 | + |
|
400 | + /////////////////////////////////// |
|
401 | + // OS/2 - OS/2 and Windows metrics table |
|
402 | + /////////////////////////////////// |
|
403 | + if (isset($this->tables["OS/2"])) { |
|
404 | + $this->seek_table("OS/2"); |
|
405 | + $version = $this->read_ushort(); |
|
406 | + $this->skip(2); |
|
407 | + $usWeightClass = $this->read_ushort(); |
|
408 | + $this->skip(2); |
|
409 | + $fsType = $this->read_ushort(); |
|
410 | + if ($fsType == 0x0002 || ($fsType & 0x0300) != 0) { |
|
411 | + die('ERROR - Font file '.$this->filename.' cannot be embedded due to copyright restrictions.'); |
|
412 | + $this->restrictedUse = true; |
|
413 | + } |
|
414 | + $this->skip(20); |
|
415 | + $sF = $this->read_short(); |
|
416 | + $this->sFamilyClass = ($sF >> 8); |
|
417 | + $this->sFamilySubClass = ($sF & 0xFF); |
|
418 | + $this->_pos += 10; //PANOSE = 10 byte length |
|
419 | + $panose = fread($this->fh, 10); |
|
420 | + $this->skip(26); |
|
421 | + $sTypoAscender = $this->read_short(); |
|
422 | + $sTypoDescender = $this->read_short(); |
|
423 | + if (!$this->ascent) $this->ascent = ($sTypoAscender * $scale); |
|
424 | + if (!$this->descent) $this->descent = ($sTypoDescender * $scale); |
|
425 | + if ($version > 1) { |
|
426 | + $this->skip(16); |
|
427 | + $sCapHeight = $this->read_short(); |
|
428 | + $this->capHeight = ($sCapHeight * $scale); |
|
429 | + } |
|
430 | + else { |
|
431 | + $this->capHeight = $this->ascent; |
|
432 | + } |
|
433 | + } |
|
434 | + else { |
|
435 | + $usWeightClass = 500; |
|
436 | + if (!$this->ascent) $this->ascent = ($yMax * $scale); |
|
437 | + if (!$this->descent) $this->descent = ($yMin * $scale); |
|
438 | + $this->capHeight = $this->ascent; |
|
439 | + } |
|
440 | + $this->stemV = 50 + intval(pow(($usWeightClass / 65.0), 2)); |
|
441 | + |
|
442 | + /////////////////////////////////// |
|
443 | + // post - PostScript table |
|
444 | + /////////////////////////////////// |
|
445 | + $this->seek_table("post"); |
|
446 | + $this->skip(4); |
|
447 | + $this->italicAngle = $this->read_short() + $this->read_ushort() / 65536.0; |
|
448 | + $this->underlinePosition = $this->read_short() * $scale; |
|
449 | + $this->underlineThickness = $this->read_short() * $scale; |
|
450 | + $isFixedPitch = $this->read_ulong(); |
|
451 | + |
|
452 | + $this->flags = 4; |
|
453 | + |
|
454 | + if ($this->italicAngle != 0) |
|
455 | + $this->flags = $this->flags | 64; |
|
456 | + if ($usWeightClass >= 600) |
|
457 | + $this->flags = $this->flags | 262144; |
|
458 | + if ($isFixedPitch) |
|
459 | + $this->flags = $this->flags | 1; |
|
460 | + |
|
461 | + /////////////////////////////////// |
|
462 | + // hhea - Horizontal header table |
|
463 | + /////////////////////////////////// |
|
464 | + $this->seek_table("hhea"); |
|
465 | + $this->skip(32); |
|
466 | + $metricDataFormat = $this->read_ushort(); |
|
467 | + if ($metricDataFormat != 0) { |
|
468 | + die('Unknown horizontal metric data format '.$metricDataFormat); |
|
469 | + } |
|
470 | + $numberOfHMetrics = $this->read_ushort(); |
|
471 | + if ($numberOfHMetrics == 0) { |
|
472 | + die('Number of horizontal metrics is 0'); |
|
473 | + } |
|
474 | + |
|
475 | + /////////////////////////////////// |
|
476 | + // maxp - Maximum profile table |
|
477 | + /////////////////////////////////// |
|
478 | + $this->seek_table("maxp"); |
|
479 | + $this->skip(4); |
|
480 | + $numGlyphs = $this->read_ushort(); |
|
481 | + |
|
482 | + |
|
483 | + /////////////////////////////////// |
|
484 | + // cmap - Character to glyph index mapping table |
|
485 | + /////////////////////////////////// |
|
486 | + $cmap_offset = $this->seek_table("cmap"); |
|
487 | + $this->skip(2); |
|
488 | + $cmapTableCount = $this->read_ushort(); |
|
489 | + $unicode_cmap_offset = 0; |
|
490 | + for ($i = 0; $i < $cmapTableCount; $i++) { |
|
491 | + $platformID = $this->read_ushort(); |
|
492 | + $encodingID = $this->read_ushort(); |
|
493 | + $offset = $this->read_ulong(); |
|
494 | + $save_pos = $this->_pos; |
|
495 | + if (($platformID == 3 && $encodingID == 1) || $platformID == 0) { // Microsoft, Unicode |
|
496 | + $format = $this->get_ushort($cmap_offset + $offset); |
|
497 | + if ($format == 4) { |
|
498 | + if (!$unicode_cmap_offset) $unicode_cmap_offset = $cmap_offset + $offset; |
|
499 | + break; |
|
500 | + } |
|
501 | + } |
|
502 | + $this->seek($save_pos); |
|
503 | + } |
|
504 | + if (!$unicode_cmap_offset) |
|
505 | + die('Font ('.$this->filename.') does not have cmap for Unicode (platform 3, encoding 1, format 4, or platform 0, any encoding, format 4)'); |
|
506 | + |
|
507 | + |
|
508 | + $glyphToChar = array(); |
|
509 | + $charToGlyph = array(); |
|
510 | + $this->getCMAP4($unicode_cmap_offset, $glyphToChar, $charToGlyph); |
|
511 | + |
|
512 | + /////////////////////////////////// |
|
513 | + // hmtx - Horizontal metrics table |
|
514 | + /////////////////////////////////// |
|
515 | + $this->getHMTX($numberOfHMetrics, $numGlyphs, $glyphToChar, $scale); |
|
516 | + |
|
517 | + } |
|
518 | 518 | |
519 | 519 | |
520 | 520 | ///////////////////////////////////////////////////////////////////////////////////////// |
521 | 521 | ///////////////////////////////////////////////////////////////////////////////////////// |
522 | 522 | |
523 | 523 | |
524 | - /** |
|
525 | - * @return string |
|
526 | - */ |
|
527 | - function makeSubset($file, &$subset) { |
|
528 | - $this->filename = $file; |
|
529 | - $this->fh = fopen($file, 'rb') or die('Can\'t open file '.$file); |
|
530 | - $this->_pos = 0; |
|
531 | - $this->charWidths = ''; |
|
532 | - $this->glyphPos = array(); |
|
533 | - $this->charToGlyph = array(); |
|
534 | - $this->tables = array(); |
|
535 | - $this->otables = array(); |
|
536 | - $this->ascent = 0; |
|
537 | - $this->descent = 0; |
|
538 | - $this->skip(4); |
|
539 | - $this->maxUni = 0; |
|
540 | - $this->readTableDirectory(); |
|
541 | - |
|
542 | - |
|
543 | - /////////////////////////////////// |
|
544 | - // head - Font header table |
|
545 | - /////////////////////////////////// |
|
546 | - $this->seek_table("head"); |
|
547 | - $this->skip(50); |
|
548 | - $indexToLocFormat = $this->read_ushort(); |
|
549 | - $glyphDataFormat = $this->read_ushort(); |
|
550 | - |
|
551 | - /////////////////////////////////// |
|
552 | - // hhea - Horizontal header table |
|
553 | - /////////////////////////////////// |
|
554 | - $this->seek_table("hhea"); |
|
555 | - $this->skip(32); |
|
556 | - $metricDataFormat = $this->read_ushort(); |
|
557 | - $orignHmetrics = $numberOfHMetrics = $this->read_ushort(); |
|
558 | - |
|
559 | - /////////////////////////////////// |
|
560 | - // maxp - Maximum profile table |
|
561 | - /////////////////////////////////// |
|
562 | - $this->seek_table("maxp"); |
|
563 | - $this->skip(4); |
|
564 | - $numGlyphs = $this->read_ushort(); |
|
565 | - |
|
566 | - |
|
567 | - /////////////////////////////////// |
|
568 | - // cmap - Character to glyph index mapping table |
|
569 | - /////////////////////////////////// |
|
570 | - $cmap_offset = $this->seek_table("cmap"); |
|
571 | - $this->skip(2); |
|
572 | - $cmapTableCount = $this->read_ushort(); |
|
573 | - $unicode_cmap_offset = 0; |
|
574 | - for ($i = 0; $i < $cmapTableCount; $i++) { |
|
575 | - $platformID = $this->read_ushort(); |
|
576 | - $encodingID = $this->read_ushort(); |
|
577 | - $offset = $this->read_ulong(); |
|
578 | - $save_pos = $this->_pos; |
|
579 | - if (($platformID == 3 && $encodingID == 1) || $platformID == 0) { // Microsoft, Unicode |
|
580 | - $format = $this->get_ushort($cmap_offset + $offset); |
|
581 | - if ($format == 4) { |
|
582 | - $unicode_cmap_offset = $cmap_offset + $offset; |
|
583 | - break; |
|
584 | - } |
|
585 | - } |
|
586 | - $this->seek($save_pos); |
|
587 | - } |
|
588 | - |
|
589 | - if (!$unicode_cmap_offset) |
|
590 | - die('Font ('.$this->filename.') does not have cmap for Unicode (platform 3, encoding 1, format 4, or platform 0, any encoding, format 4)'); |
|
591 | - |
|
592 | - |
|
593 | - $glyphToChar = array(); |
|
594 | - $charToGlyph = array(); |
|
595 | - $this->getCMAP4($unicode_cmap_offset, $glyphToChar, $charToGlyph); |
|
596 | - |
|
597 | - $this->charToGlyph = $charToGlyph; |
|
598 | - |
|
599 | - /////////////////////////////////// |
|
600 | - // hmtx - Horizontal metrics table |
|
601 | - /////////////////////////////////// |
|
602 | - $scale = 1; // not used |
|
603 | - $this->getHMTX($numberOfHMetrics, $numGlyphs, $glyphToChar, $scale); |
|
604 | - |
|
605 | - /////////////////////////////////// |
|
606 | - // loca - Index to location |
|
607 | - /////////////////////////////////// |
|
608 | - $this->getLOCA($indexToLocFormat, $numGlyphs); |
|
609 | - |
|
610 | - $subsetglyphs = array(0=>0); |
|
611 | - $subsetCharToGlyph = array(); |
|
612 | - foreach ($subset AS $code) { |
|
613 | - if (isset($this->charToGlyph[$code])) { |
|
614 | - $subsetglyphs[$this->charToGlyph[$code]] = $code; // Old Glyph ID => Unicode |
|
615 | - $subsetCharToGlyph[$code] = $this->charToGlyph[$code]; // Unicode to old GlyphID |
|
616 | - |
|
617 | - } |
|
618 | - $this->maxUni = max($this->maxUni, $code); |
|
619 | - } |
|
620 | - |
|
621 | - list($start, $dummy) = $this->get_table_pos('glyf'); |
|
622 | - |
|
623 | - $glyphSet = array(); |
|
624 | - ksort($subsetglyphs); |
|
625 | - $n = 0; |
|
626 | - $fsLastCharIndex = 0; // maximum Unicode index (character code) in this font, according to the cmap subtable for platform ID 3 and platform- specific encoding ID 0 or 1. |
|
627 | - foreach ($subsetglyphs AS $originalGlyphIdx => $uni) { |
|
628 | - $fsLastCharIndex = max($fsLastCharIndex, $uni); |
|
629 | - $glyphSet[$originalGlyphIdx] = $n; // old glyphID to new glyphID |
|
630 | - $n++; |
|
631 | - } |
|
632 | - |
|
633 | - ksort($subsetCharToGlyph); |
|
634 | - foreach ($subsetCharToGlyph AS $uni => $originalGlyphIdx) { |
|
635 | - $codeToGlyph[$uni] = $glyphSet[$originalGlyphIdx]; |
|
636 | - } |
|
637 | - $this->codeToGlyph = $codeToGlyph; |
|
638 | - |
|
639 | - ksort($subsetglyphs); |
|
640 | - foreach ($subsetglyphs AS $originalGlyphIdx => $uni) { |
|
641 | - $this->getGlyphs($originalGlyphIdx, $start, $glyphSet, $subsetglyphs); |
|
642 | - } |
|
643 | - |
|
644 | - $numGlyphs = $numberOfHMetrics = count($subsetglyphs); |
|
645 | - |
|
646 | - //tables copied from the original |
|
647 | - $tags = array('name'); |
|
648 | - foreach ($tags AS $tag) { $this->add($tag, $this->get_table($tag)); } |
|
649 | - $tags = array('cvt ', 'fpgm', 'prep', 'gasp'); |
|
650 | - foreach ($tags AS $tag) { |
|
651 | - if (isset($this->tables[$tag])) { $this->add($tag, $this->get_table($tag)); } |
|
652 | - } |
|
653 | - |
|
654 | - // post - PostScript |
|
655 | - $opost = $this->get_table('post'); |
|
656 | - $post = "\x00\x03\x00\x00".substr($opost, 4, 12)."\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"; |
|
657 | - $this->add('post', $post); |
|
658 | - |
|
659 | - // Sort CID2GID map into segments of contiguous codes |
|
660 | - ksort($codeToGlyph); |
|
661 | - unset($codeToGlyph[0]); |
|
662 | - //unset($codeToGlyph[65535]); |
|
663 | - $rangeid = 0; |
|
664 | - $range = array(); |
|
665 | - $prevcid = -2; |
|
666 | - $prevglidx = -1; |
|
667 | - // for each character |
|
668 | - foreach ($codeToGlyph as $cid => $glidx) { |
|
669 | - if ($cid == ($prevcid + 1) && $glidx == ($prevglidx + 1)) { |
|
670 | - $range[$rangeid][] = $glidx; |
|
671 | - } else { |
|
672 | - // new range |
|
673 | - $rangeid = $cid; |
|
674 | - $range[$rangeid] = array(); |
|
675 | - $range[$rangeid][] = $glidx; |
|
676 | - } |
|
677 | - $prevcid = $cid; |
|
678 | - $prevglidx = $glidx; |
|
679 | - } |
|
680 | - |
|
681 | - // cmap - Character to glyph mapping - Format 4 (MS / ) |
|
682 | - $segCount = count($range) + 1; // + 1 Last segment has missing character 0xFFFF |
|
683 | - $searchRange = 1; |
|
684 | - $entrySelector = 0; |
|
685 | - while ($searchRange * 2 <= $segCount) { |
|
686 | - $searchRange = $searchRange * 2; |
|
687 | - $entrySelector = $entrySelector + 1; |
|
688 | - } |
|
689 | - $searchRange = $searchRange * 2; |
|
690 | - $rangeShift = $segCount * 2 - $searchRange; |
|
691 | - $length = 16 + (8 * $segCount) + ($numGlyphs + 1); |
|
692 | - $cmap = array(0, 1, // Index : version, number of encoding subtables |
|
693 | - 3, 1, // Encoding Subtable : platform (MS=3), encoding (Unicode) |
|
694 | - 0, 12, // Encoding Subtable : offset (hi,lo) |
|
695 | - 4, $length, 0, // Format 4 Mapping subtable: format, length, language |
|
696 | - $segCount * 2, |
|
697 | - $searchRange, |
|
698 | - $entrySelector, |
|
699 | - $rangeShift); |
|
700 | - |
|
701 | - // endCode(s) |
|
702 | - foreach ($range AS $start=>$subrange) { |
|
703 | - $endCode = $start + (count($subrange) - 1); |
|
704 | - $cmap[] = $endCode; // endCode(s) |
|
705 | - } |
|
706 | - $cmap[] = 0xFFFF; // endCode of last Segment |
|
707 | - $cmap[] = 0; // reservedPad |
|
708 | - |
|
709 | - // startCode(s) |
|
710 | - foreach ($range AS $start=>$subrange) { |
|
711 | - $cmap[] = $start; // startCode(s) |
|
712 | - } |
|
713 | - $cmap[] = 0xFFFF; // startCode of last Segment |
|
714 | - // idDelta(s) |
|
715 | - foreach ($range AS $start=>$subrange) { |
|
716 | - $idDelta = -($start - $subrange[0]); |
|
717 | - $n += count($subrange); |
|
718 | - $cmap[] = $idDelta; // idDelta(s) |
|
719 | - } |
|
720 | - $cmap[] = 1; // idDelta of last Segment |
|
721 | - // idRangeOffset(s) |
|
722 | - foreach ($range AS $subrange) { |
|
723 | - $cmap[] = 0; // idRangeOffset[segCount] Offset in bytes to glyph indexArray, or 0 |
|
724 | - |
|
725 | - } |
|
726 | - $cmap[] = 0; // idRangeOffset of last Segment |
|
727 | - foreach ($range AS $subrange) { |
|
728 | - foreach ($subrange AS $glidx) { |
|
729 | - $cmap[] = $glidx; |
|
730 | - } |
|
731 | - } |
|
732 | - $cmap[] = 0; // Mapping for last character |
|
733 | - $cmapstr = ''; |
|
734 | - foreach ($cmap AS $cm) { $cmapstr .= pack("n", $cm); } |
|
735 | - $this->add('cmap', $cmapstr); |
|
736 | - |
|
737 | - |
|
738 | - // glyf - Glyph data |
|
739 | - list($glyfOffset, $glyfLength) = $this->get_table_pos('glyf'); |
|
740 | - if ($glyfLength < $this->maxStrLenRead) { |
|
741 | - $glyphData = $this->get_table('glyf'); |
|
742 | - } |
|
743 | - |
|
744 | - $offsets = array(); |
|
745 | - $glyf = ''; |
|
746 | - $pos = 0; |
|
747 | - |
|
748 | - $hmtxstr = ''; |
|
749 | - $xMinT = 0; |
|
750 | - $yMinT = 0; |
|
751 | - $xMaxT = 0; |
|
752 | - $yMaxT = 0; |
|
753 | - $advanceWidthMax = 0; |
|
754 | - $minLeftSideBearing = 0; |
|
755 | - $minRightSideBearing = 0; |
|
756 | - $xMaxExtent = 0; |
|
757 | - $maxPoints = 0; // points in non-compound glyph |
|
758 | - $maxContours = 0; // contours in non-compound glyph |
|
759 | - $maxComponentPoints = 0; // points in compound glyph |
|
760 | - $maxComponentContours = 0; // contours in compound glyph |
|
761 | - $maxComponentElements = 0; // number of glyphs referenced at top level |
|
762 | - $maxComponentDepth = 0; // levels of recursion, set to 0 if font has only simple glyphs |
|
763 | - $this->glyphdata = array(); |
|
764 | - |
|
765 | - foreach ($subsetglyphs AS $originalGlyphIdx => $uni) { |
|
766 | - // hmtx - Horizontal Metrics |
|
767 | - $hm = $this->getHMetric($orignHmetrics, $originalGlyphIdx); |
|
768 | - $hmtxstr .= $hm; |
|
769 | - |
|
770 | - $offsets[] = $pos; |
|
771 | - $glyphPos = $this->glyphPos[$originalGlyphIdx]; |
|
772 | - $glyphLen = $this->glyphPos[$originalGlyphIdx + 1] - $glyphPos; |
|
773 | - if ($glyfLength < $this->maxStrLenRead) { |
|
774 | - $data = substr($glyphData, $glyphPos, $glyphLen); |
|
775 | - } |
|
776 | - else { |
|
777 | - if ($glyphLen > 0) $data = $this->get_chunk($glyfOffset + $glyphPos, $glyphLen); |
|
778 | - else $data = ''; |
|
779 | - } |
|
780 | - |
|
781 | - if ($glyphLen > 0) { |
|
782 | - $up = unpack("n", substr($data, 0, 2)); |
|
783 | - } |
|
784 | - |
|
785 | - if ($glyphLen > 2 && ($up[1] & (1 << 15))) { // If number of contours <= -1 i.e. composiste glyph |
|
786 | - $pos_in_glyph = 10; |
|
787 | - $flags = GF_MORE; |
|
788 | - $nComponentElements = 0; |
|
789 | - while ($flags & GF_MORE) { |
|
790 | - $nComponentElements += 1; // number of glyphs referenced at top level |
|
791 | - $up = unpack("n", substr($data, $pos_in_glyph, 2)); |
|
792 | - $flags = $up[1]; |
|
793 | - $up = unpack("n", substr($data, $pos_in_glyph + 2, 2)); |
|
794 | - $glyphIdx = $up[1]; |
|
795 | - $this->glyphdata[$originalGlyphIdx]['compGlyphs'][] = $glyphIdx; |
|
796 | - $data = $this->_set_ushort($data, $pos_in_glyph + 2, $glyphSet[$glyphIdx]); |
|
797 | - $pos_in_glyph += 4; |
|
798 | - if ($flags & GF_WORDS) { $pos_in_glyph += 4; } else { $pos_in_glyph += 2; } |
|
799 | - if ($flags & GF_SCALE) { $pos_in_glyph += 2; } else if ($flags & GF_XYSCALE) { $pos_in_glyph += 4; } else if ($flags & GF_TWOBYTWO) { $pos_in_glyph += 8; } |
|
800 | - } |
|
801 | - $maxComponentElements = max($maxComponentElements, $nComponentElements); |
|
802 | - } |
|
803 | - |
|
804 | - $glyf .= $data; |
|
805 | - $pos += $glyphLen; |
|
806 | - if ($pos % 4 != 0) { |
|
807 | - $padding = 4 - ($pos % 4); |
|
808 | - $glyf .= str_repeat("\0", $padding); |
|
809 | - $pos += $padding; |
|
810 | - } |
|
811 | - } |
|
812 | - |
|
813 | - $offsets[] = $pos; |
|
814 | - $this->add('glyf', $glyf); |
|
815 | - |
|
816 | - // hmtx - Horizontal Metrics |
|
817 | - $this->add('hmtx', $hmtxstr); |
|
818 | - |
|
819 | - // loca - Index to location |
|
820 | - $locastr = ''; |
|
821 | - if ((($pos + 1) >> 1) > 0xFFFF) { |
|
822 | - $indexToLocFormat = 1; // long format |
|
823 | - foreach ($offsets AS $offset) { $locastr .= pack("N", $offset); } |
|
824 | - } |
|
825 | - else { |
|
826 | - $indexToLocFormat = 0; // short format |
|
827 | - foreach ($offsets AS $offset) { $locastr .= pack("n", ($offset / 2)); } |
|
828 | - } |
|
829 | - $this->add('loca', $locastr); |
|
830 | - |
|
831 | - // head - Font header |
|
832 | - $head = $this->get_table('head'); |
|
833 | - $head = $this->_set_ushort($head, 50, $indexToLocFormat); |
|
834 | - $this->add('head', $head); |
|
835 | - |
|
836 | - |
|
837 | - // hhea - Horizontal Header |
|
838 | - $hhea = $this->get_table('hhea'); |
|
839 | - $hhea = $this->_set_ushort($hhea, 34, $numberOfHMetrics); |
|
840 | - $this->add('hhea', $hhea); |
|
841 | - |
|
842 | - // maxp - Maximum Profile |
|
843 | - $maxp = $this->get_table('maxp'); |
|
844 | - $maxp = $this->_set_ushort($maxp, 4, $numGlyphs); |
|
845 | - $this->add('maxp', $maxp); |
|
846 | - |
|
847 | - |
|
848 | - // OS/2 - OS/2 |
|
849 | - $os2 = $this->get_table('OS/2'); |
|
850 | - $this->add('OS/2', $os2); |
|
851 | - |
|
852 | - fclose($this->fh); |
|
853 | - |
|
854 | - // Put the TTF file together |
|
855 | - $stm = ''; |
|
856 | - $this->endTTFile($stm); |
|
857 | - return $stm; |
|
858 | - } |
|
859 | - |
|
860 | - ////////////////////////////////////////////////////////////////////////////////// |
|
861 | - // Recursively get composite glyph data |
|
862 | - function getGlyphData($originalGlyphIdx, &$maxdepth, &$depth, &$points, &$contours) { |
|
863 | - $depth++; |
|
864 | - $maxdepth = max($maxdepth, $depth); |
|
865 | - if (count($this->glyphdata[$originalGlyphIdx]['compGlyphs'])) { |
|
866 | - foreach ($this->glyphdata[$originalGlyphIdx]['compGlyphs'] AS $glyphIdx) { |
|
867 | - $this->getGlyphData($glyphIdx, $maxdepth, $depth, $points, $contours); |
|
868 | - } |
|
869 | - } else if (($this->glyphdata[$originalGlyphIdx]['nContours'] > 0) && $depth > 0) { // simple |
|
870 | - $contours += $this->glyphdata[$originalGlyphIdx]['nContours']; |
|
871 | - $points += $this->glyphdata[$originalGlyphIdx]['nPoints']; |
|
872 | - } |
|
873 | - $depth--; |
|
874 | - } |
|
875 | - |
|
876 | - |
|
877 | - ////////////////////////////////////////////////////////////////////////////////// |
|
878 | - // Recursively get composite glyphs |
|
879 | - function getGlyphs($originalGlyphIdx, &$start, &$glyphSet, &$subsetglyphs) { |
|
880 | - $glyphPos = $this->glyphPos[$originalGlyphIdx]; |
|
881 | - $glyphLen = $this->glyphPos[$originalGlyphIdx + 1] - $glyphPos; |
|
882 | - if (!$glyphLen) { |
|
883 | - return; |
|
884 | - } |
|
885 | - $this->seek($start + $glyphPos); |
|
886 | - $numberOfContours = $this->read_short(); |
|
887 | - if ($numberOfContours < 0) { |
|
888 | - $this->skip(8); |
|
889 | - $flags = GF_MORE; |
|
890 | - while ($flags & GF_MORE) { |
|
891 | - $flags = $this->read_ushort(); |
|
892 | - $glyphIdx = $this->read_ushort(); |
|
893 | - if (!isset($glyphSet[$glyphIdx])) { |
|
894 | - $glyphSet[$glyphIdx] = count($subsetglyphs); // old glyphID to new glyphID |
|
895 | - $subsetglyphs[$glyphIdx] = true; |
|
896 | - } |
|
897 | - $savepos = ftell($this->fh); |
|
898 | - $this->getGlyphs($glyphIdx, $start, $glyphSet, $subsetglyphs); |
|
899 | - $this->seek($savepos); |
|
900 | - if ($flags & GF_WORDS) { |
|
901 | - $this->skip(4); |
|
902 | - } else { |
|
903 | - $this->skip(2); |
|
904 | - } |
|
905 | - if ($flags & GF_SCALE) { |
|
906 | - $this->skip(2); |
|
907 | - } else if ($flags & GF_XYSCALE) { |
|
908 | - $this->skip(4); |
|
909 | - } else if ($flags & GF_TWOBYTWO) { |
|
910 | - $this->skip(8); |
|
911 | - } |
|
912 | - } |
|
913 | - } |
|
914 | - } |
|
915 | - |
|
916 | - ////////////////////////////////////////////////////////////////////////////////// |
|
917 | - |
|
918 | - /** |
|
919 | - * @param integer $numberOfHMetrics |
|
920 | - * @param integer $numGlyphs |
|
921 | - * @param integer $scale |
|
922 | - */ |
|
923 | - function getHMTX($numberOfHMetrics, $numGlyphs, &$glyphToChar, $scale) { |
|
924 | - $start = $this->seek_table("hmtx"); |
|
925 | - $aw = 0; |
|
926 | - $this->charWidths = str_pad('', 256 * 256 * 2, "\x00"); |
|
927 | - $nCharWidths = 0; |
|
928 | - if (($numberOfHMetrics * 4) < $this->maxStrLenRead) { |
|
929 | - $data = $this->get_chunk($start, ($numberOfHMetrics * 4)); |
|
930 | - $arr = unpack("n*", $data); |
|
931 | - } |
|
932 | - else { $this->seek($start); } |
|
933 | - for ($glyph = 0; $glyph < $numberOfHMetrics; $glyph++) { |
|
934 | - |
|
935 | - if (($numberOfHMetrics * 4) < $this->maxStrLenRead) { |
|
936 | - $aw = $arr[($glyph * 2) + 1]; |
|
937 | - } |
|
938 | - else { |
|
939 | - $aw = $this->read_ushort(); |
|
940 | - $lsb = $this->read_ushort(); |
|
941 | - } |
|
942 | - if (isset($glyphToChar[$glyph]) || $glyph == 0) { |
|
943 | - |
|
944 | - if ($aw >= (1 << 15)) { $aw = 0; } // 1.03 Some (arabic) fonts have -ve values for width |
|
945 | - // although should be unsigned value - comes out as e.g. 65108 (intended -50) |
|
946 | - if ($glyph == 0) { |
|
947 | - $this->defaultWidth = $scale * $aw; |
|
948 | - continue; |
|
949 | - } |
|
950 | - foreach ($glyphToChar[$glyph] AS $char) { |
|
951 | - if ($char != 0 && $char != 65535) { |
|
952 | - $w = intval(round($scale * $aw)); |
|
953 | - if ($w == 0) { $w = 65535; } |
|
954 | - if ($char < 196608) { |
|
955 | - $this->charWidths[$char * 2] = chr($w >> 8); |
|
956 | - $this->charWidths[$char * 2 + 1] = chr($w & 0xFF); |
|
957 | - $nCharWidths++; |
|
958 | - } |
|
959 | - } |
|
960 | - } |
|
961 | - } |
|
962 | - } |
|
963 | - $data = $this->get_chunk(($start + $numberOfHMetrics * 4), ($numGlyphs * 2)); |
|
964 | - $arr = unpack("n*", $data); |
|
965 | - $diff = $numGlyphs - $numberOfHMetrics; |
|
966 | - for ($pos = 0; $pos < $diff; $pos++) { |
|
967 | - $glyph = $pos + $numberOfHMetrics; |
|
968 | - if (isset($glyphToChar[$glyph])) { |
|
969 | - foreach ($glyphToChar[$glyph] AS $char) { |
|
970 | - if ($char != 0 && $char != 65535) { |
|
971 | - $w = intval(round($scale * $aw)); |
|
972 | - if ($w == 0) { $w = 65535; } |
|
973 | - if ($char < 196608) { |
|
974 | - $this->charWidths[$char * 2] = chr($w >> 8); |
|
975 | - $this->charWidths[$char * 2 + 1] = chr($w & 0xFF); |
|
976 | - $nCharWidths++; |
|
977 | - } |
|
978 | - } |
|
979 | - } |
|
980 | - } |
|
981 | - } |
|
982 | - // NB 65535 is a set width of 0 |
|
983 | - // First bytes define number of chars in font |
|
984 | - $this->charWidths[0] = chr($nCharWidths >> 8); |
|
985 | - $this->charWidths[1] = chr($nCharWidths & 0xFF); |
|
986 | - } |
|
987 | - |
|
988 | - /** |
|
989 | - * @param integer $numberOfHMetrics |
|
990 | - */ |
|
991 | - function getHMetric($numberOfHMetrics, $gid) { |
|
992 | - $start = $this->seek_table("hmtx"); |
|
993 | - if ($gid < $numberOfHMetrics) { |
|
994 | - $this->seek($start + ($gid * 4)); |
|
995 | - $hm = fread($this->fh, 4); |
|
996 | - } |
|
997 | - else { |
|
998 | - $this->seek($start + (($numberOfHMetrics - 1) * 4)); |
|
999 | - $hm = fread($this->fh, 2); |
|
1000 | - $this->seek($start + ($numberOfHMetrics * 2) + ($gid * 2)); |
|
1001 | - $hm .= fread($this->fh, 2); |
|
1002 | - } |
|
1003 | - return $hm; |
|
1004 | - } |
|
1005 | - |
|
1006 | - /** |
|
1007 | - * @param integer $indexToLocFormat |
|
1008 | - * @param integer $numGlyphs |
|
1009 | - */ |
|
1010 | - function getLOCA($indexToLocFormat, $numGlyphs) { |
|
1011 | - $start = $this->seek_table('loca'); |
|
1012 | - $this->glyphPos = array(); |
|
1013 | - if ($indexToLocFormat == 0) { |
|
1014 | - $data = $this->get_chunk($start, ($numGlyphs * 2) + 2); |
|
1015 | - $arr = unpack("n*", $data); |
|
1016 | - for ($n = 0; $n <= $numGlyphs; $n++) { |
|
1017 | - $this->glyphPos[] = ($arr[$n + 1] * 2); |
|
1018 | - } |
|
1019 | - } |
|
1020 | - else if ($indexToLocFormat == 1) { |
|
1021 | - $data = $this->get_chunk($start, ($numGlyphs * 4) + 4); |
|
1022 | - $arr = unpack("N*", $data); |
|
1023 | - for ($n = 0; $n <= $numGlyphs; $n++) { |
|
1024 | - $this->glyphPos[] = ($arr[$n + 1]); |
|
1025 | - } |
|
1026 | - } |
|
1027 | - else |
|
1028 | - die('Unknown location table format '.$indexToLocFormat); |
|
1029 | - } |
|
1030 | - |
|
1031 | - |
|
1032 | - // CMAP Format 4 |
|
1033 | - |
|
1034 | - /** |
|
1035 | - * @param integer $unicode_cmap_offset |
|
1036 | - */ |
|
1037 | - function getCMAP4($unicode_cmap_offset, &$glyphToChar, &$charToGlyph ) { |
|
1038 | - $this->maxUniChar = 0; |
|
1039 | - $this->seek($unicode_cmap_offset + 2); |
|
1040 | - $length = $this->read_ushort(); |
|
1041 | - $limit = $unicode_cmap_offset + $length; |
|
1042 | - $this->skip(2); |
|
1043 | - |
|
1044 | - $segCount = $this->read_ushort() / 2; |
|
1045 | - $this->skip(6); |
|
1046 | - $endCount = array(); |
|
1047 | - for ($i = 0; $i < $segCount; $i++) { $endCount[] = $this->read_ushort(); } |
|
1048 | - $this->skip(2); |
|
1049 | - $startCount = array(); |
|
1050 | - for ($i = 0; $i < $segCount; $i++) { $startCount[] = $this->read_ushort(); } |
|
1051 | - $idDelta = array(); |
|
1052 | - for ($i = 0; $i < $segCount; $i++) { $idDelta[] = $this->read_short(); } // ???? was unsigned short |
|
1053 | - $idRangeOffset_start = $this->_pos; |
|
1054 | - $idRangeOffset = array(); |
|
1055 | - for ($i = 0; $i < $segCount; $i++) { $idRangeOffset[] = $this->read_ushort(); } |
|
1056 | - |
|
1057 | - for ($n = 0; $n < $segCount; $n++) { |
|
1058 | - $endpoint = ($endCount[$n] + 1); |
|
1059 | - for ($unichar = $startCount[$n]; $unichar < $endpoint; $unichar++) { |
|
1060 | - if ($idRangeOffset[$n] == 0) |
|
1061 | - $glyph = ($unichar + $idDelta[$n]) & 0xFFFF; |
|
1062 | - else { |
|
1063 | - $offset = ($unichar - $startCount[$n]) * 2 + $idRangeOffset[$n]; |
|
1064 | - $offset = $idRangeOffset_start + 2 * $n + $offset; |
|
1065 | - if ($offset >= $limit) |
|
1066 | - $glyph = 0; |
|
1067 | - else { |
|
1068 | - $glyph = $this->get_ushort($offset); |
|
1069 | - if ($glyph != 0) |
|
1070 | - $glyph = ($glyph + $idDelta[$n]) & 0xFFFF; |
|
1071 | - } |
|
1072 | - } |
|
1073 | - $charToGlyph[$unichar] = $glyph; |
|
1074 | - if ($unichar < 196608) { $this->maxUniChar = max($unichar, $this->maxUniChar); } |
|
1075 | - $glyphToChar[$glyph][] = $unichar; |
|
1076 | - } |
|
1077 | - } |
|
1078 | - } |
|
1079 | - |
|
1080 | - |
|
1081 | - // Put the TTF file together |
|
1082 | - |
|
1083 | - /** |
|
1084 | - * @param string $stm |
|
1085 | - */ |
|
1086 | - function endTTFile(&$stm) { |
|
1087 | - $stm = ''; |
|
1088 | - $numTables = count($this->otables); |
|
1089 | - $searchRange = 1; |
|
1090 | - $entrySelector = 0; |
|
1091 | - while ($searchRange * 2 <= $numTables) { |
|
1092 | - $searchRange = $searchRange * 2; |
|
1093 | - $entrySelector = $entrySelector + 1; |
|
1094 | - } |
|
1095 | - $searchRange = $searchRange * 16; |
|
1096 | - $rangeShift = $numTables * 16 - $searchRange; |
|
1097 | - |
|
1098 | - // Header |
|
1099 | - if (_TTF_MAC_HEADER) { |
|
1100 | - $stm .= (pack("Nnnnn", 0x74727565, $numTables, $searchRange, $entrySelector, $rangeShift)); // Mac |
|
1101 | - } |
|
1102 | - else { |
|
1103 | - $stm .= (pack("Nnnnn", 0x00010000, $numTables, $searchRange, $entrySelector, $rangeShift)); // Windows |
|
1104 | - } |
|
1105 | - |
|
1106 | - // Table directory |
|
1107 | - $tables = $this->otables; |
|
1108 | - |
|
1109 | - ksort($tables); |
|
1110 | - $offset = 12 + $numTables * 16; |
|
1111 | - foreach ($tables AS $tag=>$data) { |
|
1112 | - if ($tag == 'head') { $head_start = $offset; } |
|
1113 | - $stm .= $tag; |
|
1114 | - $checksum = $this->calcChecksum($data); |
|
1115 | - $stm .= pack("nn", $checksum[0], $checksum[1]); |
|
1116 | - $stm .= pack("NN", $offset, strlen($data)); |
|
1117 | - $paddedLength = (strlen($data) + 3) & ~3; |
|
1118 | - $offset = $offset + $paddedLength; |
|
1119 | - } |
|
1120 | - |
|
1121 | - // Table data |
|
1122 | - foreach ($tables AS $tag=>$data) { |
|
1123 | - $data .= "\0\0\0"; |
|
1124 | - $stm .= substr($data, 0, (strlen($data) & ~3)); |
|
1125 | - } |
|
1126 | - |
|
1127 | - $checksum = $this->calcChecksum($stm); |
|
1128 | - $checksum = $this->sub32(array(0xB1B0, 0xAFBA), $checksum); |
|
1129 | - $chk = pack("nn", $checksum[0], $checksum[1]); |
|
1130 | - $stm = $this->splice($stm, ($head_start + 8), $chk); |
|
1131 | - return $stm; |
|
1132 | - } |
|
524 | + /** |
|
525 | + * @return string |
|
526 | + */ |
|
527 | + function makeSubset($file, &$subset) { |
|
528 | + $this->filename = $file; |
|
529 | + $this->fh = fopen($file, 'rb') or die('Can\'t open file '.$file); |
|
530 | + $this->_pos = 0; |
|
531 | + $this->charWidths = ''; |
|
532 | + $this->glyphPos = array(); |
|
533 | + $this->charToGlyph = array(); |
|
534 | + $this->tables = array(); |
|
535 | + $this->otables = array(); |
|
536 | + $this->ascent = 0; |
|
537 | + $this->descent = 0; |
|
538 | + $this->skip(4); |
|
539 | + $this->maxUni = 0; |
|
540 | + $this->readTableDirectory(); |
|
541 | + |
|
542 | + |
|
543 | + /////////////////////////////////// |
|
544 | + // head - Font header table |
|
545 | + /////////////////////////////////// |
|
546 | + $this->seek_table("head"); |
|
547 | + $this->skip(50); |
|
548 | + $indexToLocFormat = $this->read_ushort(); |
|
549 | + $glyphDataFormat = $this->read_ushort(); |
|
550 | + |
|
551 | + /////////////////////////////////// |
|
552 | + // hhea - Horizontal header table |
|
553 | + /////////////////////////////////// |
|
554 | + $this->seek_table("hhea"); |
|
555 | + $this->skip(32); |
|
556 | + $metricDataFormat = $this->read_ushort(); |
|
557 | + $orignHmetrics = $numberOfHMetrics = $this->read_ushort(); |
|
558 | + |
|
559 | + /////////////////////////////////// |
|
560 | + // maxp - Maximum profile table |
|
561 | + /////////////////////////////////// |
|
562 | + $this->seek_table("maxp"); |
|
563 | + $this->skip(4); |
|
564 | + $numGlyphs = $this->read_ushort(); |
|
565 | + |
|
566 | + |
|
567 | + /////////////////////////////////// |
|
568 | + // cmap - Character to glyph index mapping table |
|
569 | + /////////////////////////////////// |
|
570 | + $cmap_offset = $this->seek_table("cmap"); |
|
571 | + $this->skip(2); |
|
572 | + $cmapTableCount = $this->read_ushort(); |
|
573 | + $unicode_cmap_offset = 0; |
|
574 | + for ($i = 0; $i < $cmapTableCount; $i++) { |
|
575 | + $platformID = $this->read_ushort(); |
|
576 | + $encodingID = $this->read_ushort(); |
|
577 | + $offset = $this->read_ulong(); |
|
578 | + $save_pos = $this->_pos; |
|
579 | + if (($platformID == 3 && $encodingID == 1) || $platformID == 0) { // Microsoft, Unicode |
|
580 | + $format = $this->get_ushort($cmap_offset + $offset); |
|
581 | + if ($format == 4) { |
|
582 | + $unicode_cmap_offset = $cmap_offset + $offset; |
|
583 | + break; |
|
584 | + } |
|
585 | + } |
|
586 | + $this->seek($save_pos); |
|
587 | + } |
|
588 | + |
|
589 | + if (!$unicode_cmap_offset) |
|
590 | + die('Font ('.$this->filename.') does not have cmap for Unicode (platform 3, encoding 1, format 4, or platform 0, any encoding, format 4)'); |
|
591 | + |
|
592 | + |
|
593 | + $glyphToChar = array(); |
|
594 | + $charToGlyph = array(); |
|
595 | + $this->getCMAP4($unicode_cmap_offset, $glyphToChar, $charToGlyph); |
|
596 | + |
|
597 | + $this->charToGlyph = $charToGlyph; |
|
598 | + |
|
599 | + /////////////////////////////////// |
|
600 | + // hmtx - Horizontal metrics table |
|
601 | + /////////////////////////////////// |
|
602 | + $scale = 1; // not used |
|
603 | + $this->getHMTX($numberOfHMetrics, $numGlyphs, $glyphToChar, $scale); |
|
604 | + |
|
605 | + /////////////////////////////////// |
|
606 | + // loca - Index to location |
|
607 | + /////////////////////////////////// |
|
608 | + $this->getLOCA($indexToLocFormat, $numGlyphs); |
|
609 | + |
|
610 | + $subsetglyphs = array(0=>0); |
|
611 | + $subsetCharToGlyph = array(); |
|
612 | + foreach ($subset AS $code) { |
|
613 | + if (isset($this->charToGlyph[$code])) { |
|
614 | + $subsetglyphs[$this->charToGlyph[$code]] = $code; // Old Glyph ID => Unicode |
|
615 | + $subsetCharToGlyph[$code] = $this->charToGlyph[$code]; // Unicode to old GlyphID |
|
616 | + |
|
617 | + } |
|
618 | + $this->maxUni = max($this->maxUni, $code); |
|
619 | + } |
|
620 | + |
|
621 | + list($start, $dummy) = $this->get_table_pos('glyf'); |
|
622 | + |
|
623 | + $glyphSet = array(); |
|
624 | + ksort($subsetglyphs); |
|
625 | + $n = 0; |
|
626 | + $fsLastCharIndex = 0; // maximum Unicode index (character code) in this font, according to the cmap subtable for platform ID 3 and platform- specific encoding ID 0 or 1. |
|
627 | + foreach ($subsetglyphs AS $originalGlyphIdx => $uni) { |
|
628 | + $fsLastCharIndex = max($fsLastCharIndex, $uni); |
|
629 | + $glyphSet[$originalGlyphIdx] = $n; // old glyphID to new glyphID |
|
630 | + $n++; |
|
631 | + } |
|
632 | + |
|
633 | + ksort($subsetCharToGlyph); |
|
634 | + foreach ($subsetCharToGlyph AS $uni => $originalGlyphIdx) { |
|
635 | + $codeToGlyph[$uni] = $glyphSet[$originalGlyphIdx]; |
|
636 | + } |
|
637 | + $this->codeToGlyph = $codeToGlyph; |
|
638 | + |
|
639 | + ksort($subsetglyphs); |
|
640 | + foreach ($subsetglyphs AS $originalGlyphIdx => $uni) { |
|
641 | + $this->getGlyphs($originalGlyphIdx, $start, $glyphSet, $subsetglyphs); |
|
642 | + } |
|
643 | + |
|
644 | + $numGlyphs = $numberOfHMetrics = count($subsetglyphs); |
|
645 | + |
|
646 | + //tables copied from the original |
|
647 | + $tags = array('name'); |
|
648 | + foreach ($tags AS $tag) { $this->add($tag, $this->get_table($tag)); } |
|
649 | + $tags = array('cvt ', 'fpgm', 'prep', 'gasp'); |
|
650 | + foreach ($tags AS $tag) { |
|
651 | + if (isset($this->tables[$tag])) { $this->add($tag, $this->get_table($tag)); } |
|
652 | + } |
|
653 | + |
|
654 | + // post - PostScript |
|
655 | + $opost = $this->get_table('post'); |
|
656 | + $post = "\x00\x03\x00\x00".substr($opost, 4, 12)."\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"; |
|
657 | + $this->add('post', $post); |
|
658 | + |
|
659 | + // Sort CID2GID map into segments of contiguous codes |
|
660 | + ksort($codeToGlyph); |
|
661 | + unset($codeToGlyph[0]); |
|
662 | + //unset($codeToGlyph[65535]); |
|
663 | + $rangeid = 0; |
|
664 | + $range = array(); |
|
665 | + $prevcid = -2; |
|
666 | + $prevglidx = -1; |
|
667 | + // for each character |
|
668 | + foreach ($codeToGlyph as $cid => $glidx) { |
|
669 | + if ($cid == ($prevcid + 1) && $glidx == ($prevglidx + 1)) { |
|
670 | + $range[$rangeid][] = $glidx; |
|
671 | + } else { |
|
672 | + // new range |
|
673 | + $rangeid = $cid; |
|
674 | + $range[$rangeid] = array(); |
|
675 | + $range[$rangeid][] = $glidx; |
|
676 | + } |
|
677 | + $prevcid = $cid; |
|
678 | + $prevglidx = $glidx; |
|
679 | + } |
|
680 | + |
|
681 | + // cmap - Character to glyph mapping - Format 4 (MS / ) |
|
682 | + $segCount = count($range) + 1; // + 1 Last segment has missing character 0xFFFF |
|
683 | + $searchRange = 1; |
|
684 | + $entrySelector = 0; |
|
685 | + while ($searchRange * 2 <= $segCount) { |
|
686 | + $searchRange = $searchRange * 2; |
|
687 | + $entrySelector = $entrySelector + 1; |
|
688 | + } |
|
689 | + $searchRange = $searchRange * 2; |
|
690 | + $rangeShift = $segCount * 2 - $searchRange; |
|
691 | + $length = 16 + (8 * $segCount) + ($numGlyphs + 1); |
|
692 | + $cmap = array(0, 1, // Index : version, number of encoding subtables |
|
693 | + 3, 1, // Encoding Subtable : platform (MS=3), encoding (Unicode) |
|
694 | + 0, 12, // Encoding Subtable : offset (hi,lo) |
|
695 | + 4, $length, 0, // Format 4 Mapping subtable: format, length, language |
|
696 | + $segCount * 2, |
|
697 | + $searchRange, |
|
698 | + $entrySelector, |
|
699 | + $rangeShift); |
|
700 | + |
|
701 | + // endCode(s) |
|
702 | + foreach ($range AS $start=>$subrange) { |
|
703 | + $endCode = $start + (count($subrange) - 1); |
|
704 | + $cmap[] = $endCode; // endCode(s) |
|
705 | + } |
|
706 | + $cmap[] = 0xFFFF; // endCode of last Segment |
|
707 | + $cmap[] = 0; // reservedPad |
|
708 | + |
|
709 | + // startCode(s) |
|
710 | + foreach ($range AS $start=>$subrange) { |
|
711 | + $cmap[] = $start; // startCode(s) |
|
712 | + } |
|
713 | + $cmap[] = 0xFFFF; // startCode of last Segment |
|
714 | + // idDelta(s) |
|
715 | + foreach ($range AS $start=>$subrange) { |
|
716 | + $idDelta = -($start - $subrange[0]); |
|
717 | + $n += count($subrange); |
|
718 | + $cmap[] = $idDelta; // idDelta(s) |
|
719 | + } |
|
720 | + $cmap[] = 1; // idDelta of last Segment |
|
721 | + // idRangeOffset(s) |
|
722 | + foreach ($range AS $subrange) { |
|
723 | + $cmap[] = 0; // idRangeOffset[segCount] Offset in bytes to glyph indexArray, or 0 |
|
724 | + |
|
725 | + } |
|
726 | + $cmap[] = 0; // idRangeOffset of last Segment |
|
727 | + foreach ($range AS $subrange) { |
|
728 | + foreach ($subrange AS $glidx) { |
|
729 | + $cmap[] = $glidx; |
|
730 | + } |
|
731 | + } |
|
732 | + $cmap[] = 0; // Mapping for last character |
|
733 | + $cmapstr = ''; |
|
734 | + foreach ($cmap AS $cm) { $cmapstr .= pack("n", $cm); } |
|
735 | + $this->add('cmap', $cmapstr); |
|
736 | + |
|
737 | + |
|
738 | + // glyf - Glyph data |
|
739 | + list($glyfOffset, $glyfLength) = $this->get_table_pos('glyf'); |
|
740 | + if ($glyfLength < $this->maxStrLenRead) { |
|
741 | + $glyphData = $this->get_table('glyf'); |
|
742 | + } |
|
743 | + |
|
744 | + $offsets = array(); |
|
745 | + $glyf = ''; |
|
746 | + $pos = 0; |
|
747 | + |
|
748 | + $hmtxstr = ''; |
|
749 | + $xMinT = 0; |
|
750 | + $yMinT = 0; |
|
751 | + $xMaxT = 0; |
|
752 | + $yMaxT = 0; |
|
753 | + $advanceWidthMax = 0; |
|
754 | + $minLeftSideBearing = 0; |
|
755 | + $minRightSideBearing = 0; |
|
756 | + $xMaxExtent = 0; |
|
757 | + $maxPoints = 0; // points in non-compound glyph |
|
758 | + $maxContours = 0; // contours in non-compound glyph |
|
759 | + $maxComponentPoints = 0; // points in compound glyph |
|
760 | + $maxComponentContours = 0; // contours in compound glyph |
|
761 | + $maxComponentElements = 0; // number of glyphs referenced at top level |
|
762 | + $maxComponentDepth = 0; // levels of recursion, set to 0 if font has only simple glyphs |
|
763 | + $this->glyphdata = array(); |
|
764 | + |
|
765 | + foreach ($subsetglyphs AS $originalGlyphIdx => $uni) { |
|
766 | + // hmtx - Horizontal Metrics |
|
767 | + $hm = $this->getHMetric($orignHmetrics, $originalGlyphIdx); |
|
768 | + $hmtxstr .= $hm; |
|
769 | + |
|
770 | + $offsets[] = $pos; |
|
771 | + $glyphPos = $this->glyphPos[$originalGlyphIdx]; |
|
772 | + $glyphLen = $this->glyphPos[$originalGlyphIdx + 1] - $glyphPos; |
|
773 | + if ($glyfLength < $this->maxStrLenRead) { |
|
774 | + $data = substr($glyphData, $glyphPos, $glyphLen); |
|
775 | + } |
|
776 | + else { |
|
777 | + if ($glyphLen > 0) $data = $this->get_chunk($glyfOffset + $glyphPos, $glyphLen); |
|
778 | + else $data = ''; |
|
779 | + } |
|
780 | + |
|
781 | + if ($glyphLen > 0) { |
|
782 | + $up = unpack("n", substr($data, 0, 2)); |
|
783 | + } |
|
784 | + |
|
785 | + if ($glyphLen > 2 && ($up[1] & (1 << 15))) { // If number of contours <= -1 i.e. composiste glyph |
|
786 | + $pos_in_glyph = 10; |
|
787 | + $flags = GF_MORE; |
|
788 | + $nComponentElements = 0; |
|
789 | + while ($flags & GF_MORE) { |
|
790 | + $nComponentElements += 1; // number of glyphs referenced at top level |
|
791 | + $up = unpack("n", substr($data, $pos_in_glyph, 2)); |
|
792 | + $flags = $up[1]; |
|
793 | + $up = unpack("n", substr($data, $pos_in_glyph + 2, 2)); |
|
794 | + $glyphIdx = $up[1]; |
|
795 | + $this->glyphdata[$originalGlyphIdx]['compGlyphs'][] = $glyphIdx; |
|
796 | + $data = $this->_set_ushort($data, $pos_in_glyph + 2, $glyphSet[$glyphIdx]); |
|
797 | + $pos_in_glyph += 4; |
|
798 | + if ($flags & GF_WORDS) { $pos_in_glyph += 4; } else { $pos_in_glyph += 2; } |
|
799 | + if ($flags & GF_SCALE) { $pos_in_glyph += 2; } else if ($flags & GF_XYSCALE) { $pos_in_glyph += 4; } else if ($flags & GF_TWOBYTWO) { $pos_in_glyph += 8; } |
|
800 | + } |
|
801 | + $maxComponentElements = max($maxComponentElements, $nComponentElements); |
|
802 | + } |
|
803 | + |
|
804 | + $glyf .= $data; |
|
805 | + $pos += $glyphLen; |
|
806 | + if ($pos % 4 != 0) { |
|
807 | + $padding = 4 - ($pos % 4); |
|
808 | + $glyf .= str_repeat("\0", $padding); |
|
809 | + $pos += $padding; |
|
810 | + } |
|
811 | + } |
|
812 | + |
|
813 | + $offsets[] = $pos; |
|
814 | + $this->add('glyf', $glyf); |
|
815 | + |
|
816 | + // hmtx - Horizontal Metrics |
|
817 | + $this->add('hmtx', $hmtxstr); |
|
818 | + |
|
819 | + // loca - Index to location |
|
820 | + $locastr = ''; |
|
821 | + if ((($pos + 1) >> 1) > 0xFFFF) { |
|
822 | + $indexToLocFormat = 1; // long format |
|
823 | + foreach ($offsets AS $offset) { $locastr .= pack("N", $offset); } |
|
824 | + } |
|
825 | + else { |
|
826 | + $indexToLocFormat = 0; // short format |
|
827 | + foreach ($offsets AS $offset) { $locastr .= pack("n", ($offset / 2)); } |
|
828 | + } |
|
829 | + $this->add('loca', $locastr); |
|
830 | + |
|
831 | + // head - Font header |
|
832 | + $head = $this->get_table('head'); |
|
833 | + $head = $this->_set_ushort($head, 50, $indexToLocFormat); |
|
834 | + $this->add('head', $head); |
|
835 | + |
|
836 | + |
|
837 | + // hhea - Horizontal Header |
|
838 | + $hhea = $this->get_table('hhea'); |
|
839 | + $hhea = $this->_set_ushort($hhea, 34, $numberOfHMetrics); |
|
840 | + $this->add('hhea', $hhea); |
|
841 | + |
|
842 | + // maxp - Maximum Profile |
|
843 | + $maxp = $this->get_table('maxp'); |
|
844 | + $maxp = $this->_set_ushort($maxp, 4, $numGlyphs); |
|
845 | + $this->add('maxp', $maxp); |
|
846 | + |
|
847 | + |
|
848 | + // OS/2 - OS/2 |
|
849 | + $os2 = $this->get_table('OS/2'); |
|
850 | + $this->add('OS/2', $os2); |
|
851 | + |
|
852 | + fclose($this->fh); |
|
853 | + |
|
854 | + // Put the TTF file together |
|
855 | + $stm = ''; |
|
856 | + $this->endTTFile($stm); |
|
857 | + return $stm; |
|
858 | + } |
|
859 | + |
|
860 | + ////////////////////////////////////////////////////////////////////////////////// |
|
861 | + // Recursively get composite glyph data |
|
862 | + function getGlyphData($originalGlyphIdx, &$maxdepth, &$depth, &$points, &$contours) { |
|
863 | + $depth++; |
|
864 | + $maxdepth = max($maxdepth, $depth); |
|
865 | + if (count($this->glyphdata[$originalGlyphIdx]['compGlyphs'])) { |
|
866 | + foreach ($this->glyphdata[$originalGlyphIdx]['compGlyphs'] AS $glyphIdx) { |
|
867 | + $this->getGlyphData($glyphIdx, $maxdepth, $depth, $points, $contours); |
|
868 | + } |
|
869 | + } else if (($this->glyphdata[$originalGlyphIdx]['nContours'] > 0) && $depth > 0) { // simple |
|
870 | + $contours += $this->glyphdata[$originalGlyphIdx]['nContours']; |
|
871 | + $points += $this->glyphdata[$originalGlyphIdx]['nPoints']; |
|
872 | + } |
|
873 | + $depth--; |
|
874 | + } |
|
875 | + |
|
876 | + |
|
877 | + ////////////////////////////////////////////////////////////////////////////////// |
|
878 | + // Recursively get composite glyphs |
|
879 | + function getGlyphs($originalGlyphIdx, &$start, &$glyphSet, &$subsetglyphs) { |
|
880 | + $glyphPos = $this->glyphPos[$originalGlyphIdx]; |
|
881 | + $glyphLen = $this->glyphPos[$originalGlyphIdx + 1] - $glyphPos; |
|
882 | + if (!$glyphLen) { |
|
883 | + return; |
|
884 | + } |
|
885 | + $this->seek($start + $glyphPos); |
|
886 | + $numberOfContours = $this->read_short(); |
|
887 | + if ($numberOfContours < 0) { |
|
888 | + $this->skip(8); |
|
889 | + $flags = GF_MORE; |
|
890 | + while ($flags & GF_MORE) { |
|
891 | + $flags = $this->read_ushort(); |
|
892 | + $glyphIdx = $this->read_ushort(); |
|
893 | + if (!isset($glyphSet[$glyphIdx])) { |
|
894 | + $glyphSet[$glyphIdx] = count($subsetglyphs); // old glyphID to new glyphID |
|
895 | + $subsetglyphs[$glyphIdx] = true; |
|
896 | + } |
|
897 | + $savepos = ftell($this->fh); |
|
898 | + $this->getGlyphs($glyphIdx, $start, $glyphSet, $subsetglyphs); |
|
899 | + $this->seek($savepos); |
|
900 | + if ($flags & GF_WORDS) { |
|
901 | + $this->skip(4); |
|
902 | + } else { |
|
903 | + $this->skip(2); |
|
904 | + } |
|
905 | + if ($flags & GF_SCALE) { |
|
906 | + $this->skip(2); |
|
907 | + } else if ($flags & GF_XYSCALE) { |
|
908 | + $this->skip(4); |
|
909 | + } else if ($flags & GF_TWOBYTWO) { |
|
910 | + $this->skip(8); |
|
911 | + } |
|
912 | + } |
|
913 | + } |
|
914 | + } |
|
915 | + |
|
916 | + ////////////////////////////////////////////////////////////////////////////////// |
|
917 | + |
|
918 | + /** |
|
919 | + * @param integer $numberOfHMetrics |
|
920 | + * @param integer $numGlyphs |
|
921 | + * @param integer $scale |
|
922 | + */ |
|
923 | + function getHMTX($numberOfHMetrics, $numGlyphs, &$glyphToChar, $scale) { |
|
924 | + $start = $this->seek_table("hmtx"); |
|
925 | + $aw = 0; |
|
926 | + $this->charWidths = str_pad('', 256 * 256 * 2, "\x00"); |
|
927 | + $nCharWidths = 0; |
|
928 | + if (($numberOfHMetrics * 4) < $this->maxStrLenRead) { |
|
929 | + $data = $this->get_chunk($start, ($numberOfHMetrics * 4)); |
|
930 | + $arr = unpack("n*", $data); |
|
931 | + } |
|
932 | + else { $this->seek($start); } |
|
933 | + for ($glyph = 0; $glyph < $numberOfHMetrics; $glyph++) { |
|
934 | + |
|
935 | + if (($numberOfHMetrics * 4) < $this->maxStrLenRead) { |
|
936 | + $aw = $arr[($glyph * 2) + 1]; |
|
937 | + } |
|
938 | + else { |
|
939 | + $aw = $this->read_ushort(); |
|
940 | + $lsb = $this->read_ushort(); |
|
941 | + } |
|
942 | + if (isset($glyphToChar[$glyph]) || $glyph == 0) { |
|
943 | + |
|
944 | + if ($aw >= (1 << 15)) { $aw = 0; } // 1.03 Some (arabic) fonts have -ve values for width |
|
945 | + // although should be unsigned value - comes out as e.g. 65108 (intended -50) |
|
946 | + if ($glyph == 0) { |
|
947 | + $this->defaultWidth = $scale * $aw; |
|
948 | + continue; |
|
949 | + } |
|
950 | + foreach ($glyphToChar[$glyph] AS $char) { |
|
951 | + if ($char != 0 && $char != 65535) { |
|
952 | + $w = intval(round($scale * $aw)); |
|
953 | + if ($w == 0) { $w = 65535; } |
|
954 | + if ($char < 196608) { |
|
955 | + $this->charWidths[$char * 2] = chr($w >> 8); |
|
956 | + $this->charWidths[$char * 2 + 1] = chr($w & 0xFF); |
|
957 | + $nCharWidths++; |
|
958 | + } |
|
959 | + } |
|
960 | + } |
|
961 | + } |
|
962 | + } |
|
963 | + $data = $this->get_chunk(($start + $numberOfHMetrics * 4), ($numGlyphs * 2)); |
|
964 | + $arr = unpack("n*", $data); |
|
965 | + $diff = $numGlyphs - $numberOfHMetrics; |
|
966 | + for ($pos = 0; $pos < $diff; $pos++) { |
|
967 | + $glyph = $pos + $numberOfHMetrics; |
|
968 | + if (isset($glyphToChar[$glyph])) { |
|
969 | + foreach ($glyphToChar[$glyph] AS $char) { |
|
970 | + if ($char != 0 && $char != 65535) { |
|
971 | + $w = intval(round($scale * $aw)); |
|
972 | + if ($w == 0) { $w = 65535; } |
|
973 | + if ($char < 196608) { |
|
974 | + $this->charWidths[$char * 2] = chr($w >> 8); |
|
975 | + $this->charWidths[$char * 2 + 1] = chr($w & 0xFF); |
|
976 | + $nCharWidths++; |
|
977 | + } |
|
978 | + } |
|
979 | + } |
|
980 | + } |
|
981 | + } |
|
982 | + // NB 65535 is a set width of 0 |
|
983 | + // First bytes define number of chars in font |
|
984 | + $this->charWidths[0] = chr($nCharWidths >> 8); |
|
985 | + $this->charWidths[1] = chr($nCharWidths & 0xFF); |
|
986 | + } |
|
987 | + |
|
988 | + /** |
|
989 | + * @param integer $numberOfHMetrics |
|
990 | + */ |
|
991 | + function getHMetric($numberOfHMetrics, $gid) { |
|
992 | + $start = $this->seek_table("hmtx"); |
|
993 | + if ($gid < $numberOfHMetrics) { |
|
994 | + $this->seek($start + ($gid * 4)); |
|
995 | + $hm = fread($this->fh, 4); |
|
996 | + } |
|
997 | + else { |
|
998 | + $this->seek($start + (($numberOfHMetrics - 1) * 4)); |
|
999 | + $hm = fread($this->fh, 2); |
|
1000 | + $this->seek($start + ($numberOfHMetrics * 2) + ($gid * 2)); |
|
1001 | + $hm .= fread($this->fh, 2); |
|
1002 | + } |
|
1003 | + return $hm; |
|
1004 | + } |
|
1005 | + |
|
1006 | + /** |
|
1007 | + * @param integer $indexToLocFormat |
|
1008 | + * @param integer $numGlyphs |
|
1009 | + */ |
|
1010 | + function getLOCA($indexToLocFormat, $numGlyphs) { |
|
1011 | + $start = $this->seek_table('loca'); |
|
1012 | + $this->glyphPos = array(); |
|
1013 | + if ($indexToLocFormat == 0) { |
|
1014 | + $data = $this->get_chunk($start, ($numGlyphs * 2) + 2); |
|
1015 | + $arr = unpack("n*", $data); |
|
1016 | + for ($n = 0; $n <= $numGlyphs; $n++) { |
|
1017 | + $this->glyphPos[] = ($arr[$n + 1] * 2); |
|
1018 | + } |
|
1019 | + } |
|
1020 | + else if ($indexToLocFormat == 1) { |
|
1021 | + $data = $this->get_chunk($start, ($numGlyphs * 4) + 4); |
|
1022 | + $arr = unpack("N*", $data); |
|
1023 | + for ($n = 0; $n <= $numGlyphs; $n++) { |
|
1024 | + $this->glyphPos[] = ($arr[$n + 1]); |
|
1025 | + } |
|
1026 | + } |
|
1027 | + else |
|
1028 | + die('Unknown location table format '.$indexToLocFormat); |
|
1029 | + } |
|
1030 | + |
|
1031 | + |
|
1032 | + // CMAP Format 4 |
|
1033 | + |
|
1034 | + /** |
|
1035 | + * @param integer $unicode_cmap_offset |
|
1036 | + */ |
|
1037 | + function getCMAP4($unicode_cmap_offset, &$glyphToChar, &$charToGlyph ) { |
|
1038 | + $this->maxUniChar = 0; |
|
1039 | + $this->seek($unicode_cmap_offset + 2); |
|
1040 | + $length = $this->read_ushort(); |
|
1041 | + $limit = $unicode_cmap_offset + $length; |
|
1042 | + $this->skip(2); |
|
1043 | + |
|
1044 | + $segCount = $this->read_ushort() / 2; |
|
1045 | + $this->skip(6); |
|
1046 | + $endCount = array(); |
|
1047 | + for ($i = 0; $i < $segCount; $i++) { $endCount[] = $this->read_ushort(); } |
|
1048 | + $this->skip(2); |
|
1049 | + $startCount = array(); |
|
1050 | + for ($i = 0; $i < $segCount; $i++) { $startCount[] = $this->read_ushort(); } |
|
1051 | + $idDelta = array(); |
|
1052 | + for ($i = 0; $i < $segCount; $i++) { $idDelta[] = $this->read_short(); } // ???? was unsigned short |
|
1053 | + $idRangeOffset_start = $this->_pos; |
|
1054 | + $idRangeOffset = array(); |
|
1055 | + for ($i = 0; $i < $segCount; $i++) { $idRangeOffset[] = $this->read_ushort(); } |
|
1056 | + |
|
1057 | + for ($n = 0; $n < $segCount; $n++) { |
|
1058 | + $endpoint = ($endCount[$n] + 1); |
|
1059 | + for ($unichar = $startCount[$n]; $unichar < $endpoint; $unichar++) { |
|
1060 | + if ($idRangeOffset[$n] == 0) |
|
1061 | + $glyph = ($unichar + $idDelta[$n]) & 0xFFFF; |
|
1062 | + else { |
|
1063 | + $offset = ($unichar - $startCount[$n]) * 2 + $idRangeOffset[$n]; |
|
1064 | + $offset = $idRangeOffset_start + 2 * $n + $offset; |
|
1065 | + if ($offset >= $limit) |
|
1066 | + $glyph = 0; |
|
1067 | + else { |
|
1068 | + $glyph = $this->get_ushort($offset); |
|
1069 | + if ($glyph != 0) |
|
1070 | + $glyph = ($glyph + $idDelta[$n]) & 0xFFFF; |
|
1071 | + } |
|
1072 | + } |
|
1073 | + $charToGlyph[$unichar] = $glyph; |
|
1074 | + if ($unichar < 196608) { $this->maxUniChar = max($unichar, $this->maxUniChar); } |
|
1075 | + $glyphToChar[$glyph][] = $unichar; |
|
1076 | + } |
|
1077 | + } |
|
1078 | + } |
|
1079 | + |
|
1080 | + |
|
1081 | + // Put the TTF file together |
|
1082 | + |
|
1083 | + /** |
|
1084 | + * @param string $stm |
|
1085 | + */ |
|
1086 | + function endTTFile(&$stm) { |
|
1087 | + $stm = ''; |
|
1088 | + $numTables = count($this->otables); |
|
1089 | + $searchRange = 1; |
|
1090 | + $entrySelector = 0; |
|
1091 | + while ($searchRange * 2 <= $numTables) { |
|
1092 | + $searchRange = $searchRange * 2; |
|
1093 | + $entrySelector = $entrySelector + 1; |
|
1094 | + } |
|
1095 | + $searchRange = $searchRange * 16; |
|
1096 | + $rangeShift = $numTables * 16 - $searchRange; |
|
1097 | + |
|
1098 | + // Header |
|
1099 | + if (_TTF_MAC_HEADER) { |
|
1100 | + $stm .= (pack("Nnnnn", 0x74727565, $numTables, $searchRange, $entrySelector, $rangeShift)); // Mac |
|
1101 | + } |
|
1102 | + else { |
|
1103 | + $stm .= (pack("Nnnnn", 0x00010000, $numTables, $searchRange, $entrySelector, $rangeShift)); // Windows |
|
1104 | + } |
|
1105 | + |
|
1106 | + // Table directory |
|
1107 | + $tables = $this->otables; |
|
1108 | + |
|
1109 | + ksort($tables); |
|
1110 | + $offset = 12 + $numTables * 16; |
|
1111 | + foreach ($tables AS $tag=>$data) { |
|
1112 | + if ($tag == 'head') { $head_start = $offset; } |
|
1113 | + $stm .= $tag; |
|
1114 | + $checksum = $this->calcChecksum($data); |
|
1115 | + $stm .= pack("nn", $checksum[0], $checksum[1]); |
|
1116 | + $stm .= pack("NN", $offset, strlen($data)); |
|
1117 | + $paddedLength = (strlen($data) + 3) & ~3; |
|
1118 | + $offset = $offset + $paddedLength; |
|
1119 | + } |
|
1120 | + |
|
1121 | + // Table data |
|
1122 | + foreach ($tables AS $tag=>$data) { |
|
1123 | + $data .= "\0\0\0"; |
|
1124 | + $stm .= substr($data, 0, (strlen($data) & ~3)); |
|
1125 | + } |
|
1126 | + |
|
1127 | + $checksum = $this->calcChecksum($stm); |
|
1128 | + $checksum = $this->sub32(array(0xB1B0, 0xAFBA), $checksum); |
|
1129 | + $chk = pack("nn", $checksum[0], $checksum[1]); |
|
1130 | + $stm = $this->splice($stm, ($head_start + 8), $chk); |
|
1131 | + return $stm; |
|
1132 | + } |
|
1133 | 1133 | |
1134 | 1134 | |
1135 | 1135 |
@@ -4,5 +4,5 @@ |
||
4 | 4 | $up = -100; |
5 | 5 | $ut = 50; |
6 | 6 | for ($i = 0; $i <= 255; $i++) |
7 | - $cw[chr($i)] = 600; |
|
7 | + $cw[chr($i)] = 600; |
|
8 | 8 | ?> |
@@ -122,9 +122,9 @@ discard block |
||
122 | 122 | } |
123 | 123 | |
124 | 124 | /** |
125 | - * Compute key depending on object number where the encrypted data is stored |
|
126 | - * @param integer $n |
|
127 | - */ |
|
125 | + * Compute key depending on object number where the encrypted data is stored |
|
126 | + * @param integer $n |
|
127 | + */ |
|
128 | 128 | function _objectkey($n) |
129 | 129 | { |
130 | 130 | return substr($this->_md5_16($this->encryption_key.pack('VXxx', $n)), 0, 10); |
@@ -163,18 +163,18 @@ discard block |
||
163 | 163 | } |
164 | 164 | |
165 | 165 | /** |
166 | - * Get MD5 as binary string |
|
167 | - */ |
|
166 | + * Get MD5 as binary string |
|
167 | + */ |
|
168 | 168 | function _md5_16($string) |
169 | 169 | { |
170 | 170 | return pack('H*', md5($string)); |
171 | 171 | } |
172 | 172 | |
173 | 173 | /** |
174 | - * Compute O value |
|
175 | - * @param string $user_pass |
|
176 | - * @param string $owner_pass |
|
177 | - */ |
|
174 | + * Compute O value |
|
175 | + * @param string $user_pass |
|
176 | + * @param string $owner_pass |
|
177 | + */ |
|
178 | 178 | function _Ovalue($user_pass, $owner_pass) |
179 | 179 | { |
180 | 180 | $tmp = $this->_md5_16($owner_pass); |
@@ -183,18 +183,18 @@ discard block |
||
183 | 183 | } |
184 | 184 | |
185 | 185 | /** |
186 | - * Compute U value |
|
187 | - */ |
|
186 | + * Compute U value |
|
187 | + */ |
|
188 | 188 | function _Uvalue() |
189 | 189 | { |
190 | 190 | return RC4($this->encryption_key, $this->padding); |
191 | 191 | } |
192 | 192 | |
193 | 193 | /** |
194 | - * Compute encryption key |
|
195 | - * @param string $user_pass |
|
196 | - * @param integer $protection |
|
197 | - */ |
|
194 | + * Compute encryption key |
|
195 | + * @param string $user_pass |
|
196 | + * @param integer $protection |
|
197 | + */ |
|
198 | 198 | function _generateencryptionkey($user_pass, $owner_pass, $protection) |
199 | 199 | { |
200 | 200 | // Pad passwords |
@@ -227,16 +227,16 @@ discard block |
||
227 | 227 | { |
228 | 228 | |
229 | 229 | /** |
230 | - * Encrypt a text using AES encryption in Counter mode of operation |
|
231 | - * - see http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf |
|
232 | - * |
|
233 | - * Unicode multi-byte character safe |
|
234 | - * |
|
235 | - * @param plaintext string text to be encrypted |
|
236 | - * @param password the password to use to generate a key |
|
237 | - * @param nBits integer of bits to be used in the key (128, 192, or 256) |
|
238 | - * @return string text |
|
239 | - */ |
|
230 | + * Encrypt a text using AES encryption in Counter mode of operation |
|
231 | + * - see http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf |
|
232 | + * |
|
233 | + * Unicode multi-byte character safe |
|
234 | + * |
|
235 | + * @param plaintext string text to be encrypted |
|
236 | + * @param password the password to use to generate a key |
|
237 | + * @param nBits integer of bits to be used in the key (128, 192, or 256) |
|
238 | + * @return string text |
|
239 | + */ |
|
240 | 240 | public static function encrypt($plaintext, $password, $nBits) |
241 | 241 | { |
242 | 242 | $blockSize = 16; // block size fixed at 16 bytes / 128 bits (Nb=4) for AES |
@@ -311,13 +311,13 @@ discard block |
||
311 | 311 | } |
312 | 312 | |
313 | 313 | /** |
314 | - * Decrypt a text encrypted by AES in counter mode of operation |
|
315 | - * |
|
316 | - * @param ciphertext source text to be decrypted |
|
317 | - * @param password the password to use to generate a key |
|
318 | - * @param nBits integer of bits to be used in the key (128, 192, or 256) |
|
319 | - * @return string text |
|
320 | - */ |
|
314 | + * Decrypt a text encrypted by AES in counter mode of operation |
|
315 | + * |
|
316 | + * @param ciphertext source text to be decrypted |
|
317 | + * @param password the password to use to generate a key |
|
318 | + * @param nBits integer of bits to be used in the key (128, 192, or 256) |
|
319 | + * @return string text |
|
320 | + */ |
|
321 | 321 | public static function decrypt($ciphertext, $password, $nBits) |
322 | 322 | { |
323 | 323 | $blockSize = 16; // block size fixed at 16 bytes / 128 bits (Nb=4) for AES |
@@ -39,294 +39,294 @@ |
||
39 | 39 | */ |
40 | 40 | class Padding |
41 | 41 | { |
42 | - /** |
|
43 | - * Constructor |
|
44 | - * |
|
45 | - * @return void |
|
46 | - */ |
|
47 | - public function __construct() { } |
|
48 | - |
|
49 | - |
|
50 | - /** |
|
51 | - * Destructor |
|
52 | - * |
|
53 | - * @return void |
|
54 | - */ |
|
55 | - public function __destruct() { } |
|
56 | - |
|
57 | - |
|
58 | - /** |
|
59 | - * Returns a string padded using the specified padding scheme |
|
60 | - * |
|
61 | - * @param string $text The string to pad |
|
62 | - * @param integer $bytes The number of bytes to pad |
|
63 | - * @param integer $type One of the predefined padding types |
|
64 | - * @return boolean True on success, false on error |
|
65 | - */ |
|
66 | - public static function pad(&$text, $bytes, $type = PHP_Crypt::ZERO) |
|
67 | - { |
|
68 | - // if the size of padding is not greater than 1 |
|
69 | - // just return true, no padding will be done |
|
70 | - if (!($bytes > 0)) |
|
71 | - return true; |
|
72 | - |
|
73 | - switch ($type) |
|
74 | - { |
|
75 | - case PHP_Crypt::PAD_ZERO: |
|
76 | - return self::zeroPad($text, $bytes); |
|
77 | - break; |
|
78 | - case PHP_Crypt::PAD_ANSI_X923: |
|
79 | - return self::ansiX923Pad($text, $bytes); |
|
80 | - break; |
|
81 | - case PHP_Crypt::PAD_ISO_10126: |
|
82 | - return self::iso10126Pad($text, $bytes); |
|
83 | - break; |
|
84 | - case PHP_Crypt::PAD_PKCS7: |
|
85 | - return self::pkcs7Pad($text, $bytes); |
|
86 | - break; |
|
87 | - case PHP_Crypt::PAD_ISO_7816_4: |
|
88 | - return self::iso7816Pad($text, $bytes); |
|
89 | - break; |
|
90 | - default: |
|
91 | - trigger_error("$type is not a valid padding type.", E_USER_NOTICE); |
|
92 | - return self::zeroPad($text, $bytes); |
|
93 | - } |
|
94 | - |
|
95 | - return true; |
|
96 | - } |
|
97 | - |
|
98 | - |
|
99 | - /** |
|
100 | - * Strips padding from a string |
|
101 | - * |
|
102 | - * @param string $text The string strip padding from |
|
103 | - * @param integer $type One of the predefined padding types |
|
104 | - * @return boolean True on success, false on error |
|
105 | - */ |
|
106 | - public static function strip(&$text, $type = PHP_Crypt::ZERO) |
|
107 | - { |
|
108 | - switch ($type) |
|
109 | - { |
|
110 | - case PHP_Crypt::PAD_ZERO: |
|
111 | - return self::zeroStrip($text); |
|
112 | - break; |
|
113 | - case PHP_Crypt::PAD_ANSI_X923: |
|
114 | - return self::ansiX923Strip($text); |
|
115 | - break; |
|
116 | - case PHP_Crypt::PAD_ISO_10126: |
|
117 | - return self::iso10126Strip($text); |
|
118 | - break; |
|
119 | - case PHP_Crypt::PAD_PKCS7: |
|
120 | - return self::pkcs7Strip($text); |
|
121 | - break; |
|
122 | - case PHP_Crypt::PAD_ISO_7816_4: |
|
123 | - return self::iso7816Strip($text); |
|
124 | - break; |
|
125 | - default: |
|
126 | - trigger_error("$type is not a valid padding type.", E_USER_NOTICE); |
|
127 | - return self::zeroStrip($text); |
|
128 | - } |
|
129 | - |
|
130 | - return true; |
|
131 | - } |
|
132 | - |
|
133 | - |
|
134 | - /** |
|
135 | - * Pads a string with null bytes |
|
136 | - * |
|
137 | - * @param string $text The string to be padded |
|
138 | - * @param integer $bytes The number of bytes to pad |
|
139 | - * @return boolean Returns true |
|
140 | - */ |
|
141 | - private static function zeroPad(&$text, $bytes) |
|
142 | - { |
|
143 | - $len = $bytes + strlen($text); |
|
144 | - $text = str_pad($text, $len, chr(0), STR_PAD_RIGHT); |
|
145 | - return true; |
|
146 | - } |
|
147 | - |
|
148 | - |
|
149 | - /** |
|
150 | - * Strips null padding off a string |
|
151 | - * NOTE: This is generally a bad idea as there is no way |
|
152 | - * to distinguish a null byte that is not padding from |
|
153 | - * a null byte that is padding. Stripping null padding should |
|
154 | - * be handled by the developer at the application level. |
|
155 | - * |
|
156 | - * @param string $text The string with null padding to strip |
|
157 | - * @return boolean Returns true |
|
158 | - */ |
|
159 | - private static function zeroStrip(&$text) |
|
160 | - { |
|
161 | - // with NULL byte padding, we should not strip off the |
|
162 | - // null bytes, instead leave this to the developer at the |
|
163 | - // application level |
|
164 | - //$text = preg_replace('/\0+$/', '', $text); |
|
165 | - return true; |
|
166 | - } |
|
167 | - |
|
168 | - |
|
169 | - /** |
|
170 | - * Pads a string using ANSI X.923 |
|
171 | - * Adds null padding to the string, except for the last byte |
|
172 | - * which indicates the number of bytes padded |
|
173 | - * |
|
174 | - * @param string $text The string to pad |
|
175 | - * @param integer The number of bytes to pad |
|
176 | - * @param integer $bytes |
|
177 | - * @return boolean Returns true |
|
178 | - */ |
|
179 | - private static function ansiX923Pad(&$text, $bytes) |
|
180 | - { |
|
181 | - $len = $bytes + strlen($text); |
|
182 | - $text = str_pad($text, ($len - 1), "\0", STR_PAD_RIGHT); |
|
183 | - $text .= chr($bytes); |
|
184 | - return true; |
|
185 | - } |
|
186 | - |
|
187 | - |
|
188 | - /** |
|
189 | - * Strips ANSI X.923 padding from a string |
|
190 | - * |
|
191 | - * @param string $text The string to strip padding from |
|
192 | - * @return boolean Returns true |
|
193 | - */ |
|
194 | - private static function ansiX923Strip(&$text) |
|
195 | - { |
|
196 | - $pos = strlen($text) - 1; |
|
197 | - $c = ord($text[$pos]); |
|
198 | - |
|
199 | - if ($c == 0) |
|
200 | - return true; |
|
201 | - else if ($c == 1) |
|
202 | - $text = substr($text, 0, -1); |
|
203 | - else |
|
204 | - { |
|
205 | - // the total null bytes are 1 less than the value of the final byte |
|
206 | - $nc = $c - 1; |
|
207 | - $text = preg_replace('/\0{'.$nc.'}'.preg_quote(chr($c)).'$/', "", $text); |
|
208 | - } |
|
209 | - |
|
210 | - return true; |
|
211 | - } |
|
212 | - |
|
213 | - |
|
214 | - /** |
|
215 | - * Pads a string using ISO 10126 |
|
216 | - * Adds random bytes to the end of the string, except for the last |
|
217 | - * byte which indicates the number of padded bytes |
|
218 | - * |
|
219 | - * @param string $text The string to pad |
|
220 | - * @param integer The number of bytes to pad |
|
221 | - * @param integer $bytes |
|
222 | - * @return boolean Returns true |
|
223 | - */ |
|
224 | - private static function iso10126Pad(&$text, $bytes) |
|
225 | - { |
|
226 | - // create the random pad bytes, we do one less than |
|
227 | - // needed because the last byte is reserved for the |
|
228 | - // number of padded bytes |
|
229 | - for ($i = 0; $i < ($bytes - 1); ++$i) |
|
230 | - $text .= chr(mt_rand(0, 255)); |
|
231 | - |
|
232 | - // add the byte to indicate the padding length |
|
233 | - $text .= chr($bytes); |
|
234 | - return true; |
|
235 | - } |
|
236 | - |
|
237 | - |
|
238 | - /** |
|
239 | - * Strips ISO 10126 padding from a string |
|
240 | - * |
|
241 | - * @param string $text The string to strip padding from |
|
242 | - * @return boolean Returns true |
|
243 | - */ |
|
244 | - private static function iso10126Strip(&$text) |
|
245 | - { |
|
246 | - $pos = strlen($text) - 1; |
|
247 | - $c = ord($text[$pos]) * -1; |
|
248 | - |
|
249 | - // if we got a null byte at the end of the string, |
|
250 | - // just return |
|
251 | - if ($c == 0) |
|
252 | - return true; |
|
253 | - |
|
254 | - $text = substr($text, 0, $c); |
|
255 | - return true; |
|
256 | - } |
|
257 | - |
|
258 | - |
|
259 | - /** |
|
260 | - * Pads a string using PKCS7 |
|
261 | - * Adds padding using the bytes with the value of the |
|
262 | - * number of bytes need for padding |
|
263 | - * |
|
264 | - * @param string $text The string to pad |
|
265 | - * @param integer The number of bytes to pad |
|
266 | - * @param integer $bytes |
|
267 | - * @return boolean Returns true |
|
268 | - */ |
|
269 | - private static function pkcs7Pad(&$text, $bytes) |
|
270 | - { |
|
271 | - $len = $bytes + strlen($text); |
|
272 | - $text = str_pad($text, $len, chr($bytes), STR_PAD_RIGHT); |
|
273 | - return true; |
|
274 | - } |
|
275 | - |
|
276 | - |
|
277 | - /** |
|
278 | - * Strips PKCS7 padding from a string |
|
279 | - * |
|
280 | - * @param string $text The string to strip padding from |
|
281 | - * @return boolean Returns true |
|
282 | - */ |
|
283 | - private static function pkcs7Strip(&$text) |
|
284 | - { |
|
285 | - $pos = strlen($text) - 1; |
|
286 | - $c = ord($text[$pos]); |
|
287 | - |
|
288 | - if ($c == 0) |
|
289 | - return true; |
|
290 | - |
|
291 | - $text = preg_replace('/'.preg_quote(chr($c)).'{'.$c.'}$/', "", $text); |
|
292 | - return true; |
|
293 | - } |
|
294 | - |
|
295 | - |
|
296 | - /** |
|
297 | - * Pads a string using ISO/IEC 7816-4 |
|
298 | - * Adds byte 0x80 followed by null bytes to pad a string |
|
299 | - * |
|
300 | - * @param string $text The string to pad |
|
301 | - * @param integer The number of bytes to pad |
|
302 | - * @param integer $bytes |
|
303 | - * @return boolean Returns true |
|
304 | - */ |
|
305 | - private static function iso7816Pad(&$text, $bytes) |
|
306 | - { |
|
307 | - $text .= chr(0x80); |
|
308 | - $len = $bytes + strlen($text); |
|
309 | - |
|
310 | - // if we are only padding one byte, then 0x80 is all we need |
|
311 | - // else we follow up with null bytes |
|
312 | - if ($bytes > 1) |
|
313 | - $text = str_pad($text, ($len - 1), chr(0), STR_PAD_RIGHT); |
|
314 | - |
|
315 | - return true; |
|
316 | - } |
|
317 | - |
|
318 | - |
|
319 | - /** |
|
320 | - * Strips ISO/IEC 7816-4 padding from a string |
|
321 | - * |
|
322 | - * @param string $text The string to strip padding from |
|
323 | - * @return boolean Returns true |
|
324 | - */ |
|
325 | - private static function iso7816Strip(&$text) |
|
326 | - { |
|
327 | - $c = chr(0x80); |
|
328 | - $text = preg_replace('/'.preg_quote($c).'\0*$/', '', $text); |
|
329 | - return true; |
|
330 | - } |
|
42 | + /** |
|
43 | + * Constructor |
|
44 | + * |
|
45 | + * @return void |
|
46 | + */ |
|
47 | + public function __construct() { } |
|
48 | + |
|
49 | + |
|
50 | + /** |
|
51 | + * Destructor |
|
52 | + * |
|
53 | + * @return void |
|
54 | + */ |
|
55 | + public function __destruct() { } |
|
56 | + |
|
57 | + |
|
58 | + /** |
|
59 | + * Returns a string padded using the specified padding scheme |
|
60 | + * |
|
61 | + * @param string $text The string to pad |
|
62 | + * @param integer $bytes The number of bytes to pad |
|
63 | + * @param integer $type One of the predefined padding types |
|
64 | + * @return boolean True on success, false on error |
|
65 | + */ |
|
66 | + public static function pad(&$text, $bytes, $type = PHP_Crypt::ZERO) |
|
67 | + { |
|
68 | + // if the size of padding is not greater than 1 |
|
69 | + // just return true, no padding will be done |
|
70 | + if (!($bytes > 0)) |
|
71 | + return true; |
|
72 | + |
|
73 | + switch ($type) |
|
74 | + { |
|
75 | + case PHP_Crypt::PAD_ZERO: |
|
76 | + return self::zeroPad($text, $bytes); |
|
77 | + break; |
|
78 | + case PHP_Crypt::PAD_ANSI_X923: |
|
79 | + return self::ansiX923Pad($text, $bytes); |
|
80 | + break; |
|
81 | + case PHP_Crypt::PAD_ISO_10126: |
|
82 | + return self::iso10126Pad($text, $bytes); |
|
83 | + break; |
|
84 | + case PHP_Crypt::PAD_PKCS7: |
|
85 | + return self::pkcs7Pad($text, $bytes); |
|
86 | + break; |
|
87 | + case PHP_Crypt::PAD_ISO_7816_4: |
|
88 | + return self::iso7816Pad($text, $bytes); |
|
89 | + break; |
|
90 | + default: |
|
91 | + trigger_error("$type is not a valid padding type.", E_USER_NOTICE); |
|
92 | + return self::zeroPad($text, $bytes); |
|
93 | + } |
|
94 | + |
|
95 | + return true; |
|
96 | + } |
|
97 | + |
|
98 | + |
|
99 | + /** |
|
100 | + * Strips padding from a string |
|
101 | + * |
|
102 | + * @param string $text The string strip padding from |
|
103 | + * @param integer $type One of the predefined padding types |
|
104 | + * @return boolean True on success, false on error |
|
105 | + */ |
|
106 | + public static function strip(&$text, $type = PHP_Crypt::ZERO) |
|
107 | + { |
|
108 | + switch ($type) |
|
109 | + { |
|
110 | + case PHP_Crypt::PAD_ZERO: |
|
111 | + return self::zeroStrip($text); |
|
112 | + break; |
|
113 | + case PHP_Crypt::PAD_ANSI_X923: |
|
114 | + return self::ansiX923Strip($text); |
|
115 | + break; |
|
116 | + case PHP_Crypt::PAD_ISO_10126: |
|
117 | + return self::iso10126Strip($text); |
|
118 | + break; |
|
119 | + case PHP_Crypt::PAD_PKCS7: |
|
120 | + return self::pkcs7Strip($text); |
|
121 | + break; |
|
122 | + case PHP_Crypt::PAD_ISO_7816_4: |
|
123 | + return self::iso7816Strip($text); |
|
124 | + break; |
|
125 | + default: |
|
126 | + trigger_error("$type is not a valid padding type.", E_USER_NOTICE); |
|
127 | + return self::zeroStrip($text); |
|
128 | + } |
|
129 | + |
|
130 | + return true; |
|
131 | + } |
|
132 | + |
|
133 | + |
|
134 | + /** |
|
135 | + * Pads a string with null bytes |
|
136 | + * |
|
137 | + * @param string $text The string to be padded |
|
138 | + * @param integer $bytes The number of bytes to pad |
|
139 | + * @return boolean Returns true |
|
140 | + */ |
|
141 | + private static function zeroPad(&$text, $bytes) |
|
142 | + { |
|
143 | + $len = $bytes + strlen($text); |
|
144 | + $text = str_pad($text, $len, chr(0), STR_PAD_RIGHT); |
|
145 | + return true; |
|
146 | + } |
|
147 | + |
|
148 | + |
|
149 | + /** |
|
150 | + * Strips null padding off a string |
|
151 | + * NOTE: This is generally a bad idea as there is no way |
|
152 | + * to distinguish a null byte that is not padding from |
|
153 | + * a null byte that is padding. Stripping null padding should |
|
154 | + * be handled by the developer at the application level. |
|
155 | + * |
|
156 | + * @param string $text The string with null padding to strip |
|
157 | + * @return boolean Returns true |
|
158 | + */ |
|
159 | + private static function zeroStrip(&$text) |
|
160 | + { |
|
161 | + // with NULL byte padding, we should not strip off the |
|
162 | + // null bytes, instead leave this to the developer at the |
|
163 | + // application level |
|
164 | + //$text = preg_replace('/\0+$/', '', $text); |
|
165 | + return true; |
|
166 | + } |
|
167 | + |
|
168 | + |
|
169 | + /** |
|
170 | + * Pads a string using ANSI X.923 |
|
171 | + * Adds null padding to the string, except for the last byte |
|
172 | + * which indicates the number of bytes padded |
|
173 | + * |
|
174 | + * @param string $text The string to pad |
|
175 | + * @param integer The number of bytes to pad |
|
176 | + * @param integer $bytes |
|
177 | + * @return boolean Returns true |
|
178 | + */ |
|
179 | + private static function ansiX923Pad(&$text, $bytes) |
|
180 | + { |
|
181 | + $len = $bytes + strlen($text); |
|
182 | + $text = str_pad($text, ($len - 1), "\0", STR_PAD_RIGHT); |
|
183 | + $text .= chr($bytes); |
|
184 | + return true; |
|
185 | + } |
|
186 | + |
|
187 | + |
|
188 | + /** |
|
189 | + * Strips ANSI X.923 padding from a string |
|
190 | + * |
|
191 | + * @param string $text The string to strip padding from |
|
192 | + * @return boolean Returns true |
|
193 | + */ |
|
194 | + private static function ansiX923Strip(&$text) |
|
195 | + { |
|
196 | + $pos = strlen($text) - 1; |
|
197 | + $c = ord($text[$pos]); |
|
198 | + |
|
199 | + if ($c == 0) |
|
200 | + return true; |
|
201 | + else if ($c == 1) |
|
202 | + $text = substr($text, 0, -1); |
|
203 | + else |
|
204 | + { |
|
205 | + // the total null bytes are 1 less than the value of the final byte |
|
206 | + $nc = $c - 1; |
|
207 | + $text = preg_replace('/\0{'.$nc.'}'.preg_quote(chr($c)).'$/', "", $text); |
|
208 | + } |
|
209 | + |
|
210 | + return true; |
|
211 | + } |
|
212 | + |
|
213 | + |
|
214 | + /** |
|
215 | + * Pads a string using ISO 10126 |
|
216 | + * Adds random bytes to the end of the string, except for the last |
|
217 | + * byte which indicates the number of padded bytes |
|
218 | + * |
|
219 | + * @param string $text The string to pad |
|
220 | + * @param integer The number of bytes to pad |
|
221 | + * @param integer $bytes |
|
222 | + * @return boolean Returns true |
|
223 | + */ |
|
224 | + private static function iso10126Pad(&$text, $bytes) |
|
225 | + { |
|
226 | + // create the random pad bytes, we do one less than |
|
227 | + // needed because the last byte is reserved for the |
|
228 | + // number of padded bytes |
|
229 | + for ($i = 0; $i < ($bytes - 1); ++$i) |
|
230 | + $text .= chr(mt_rand(0, 255)); |
|
231 | + |
|
232 | + // add the byte to indicate the padding length |
|
233 | + $text .= chr($bytes); |
|
234 | + return true; |
|
235 | + } |
|
236 | + |
|
237 | + |
|
238 | + /** |
|
239 | + * Strips ISO 10126 padding from a string |
|
240 | + * |
|
241 | + * @param string $text The string to strip padding from |
|
242 | + * @return boolean Returns true |
|
243 | + */ |
|
244 | + private static function iso10126Strip(&$text) |
|
245 | + { |
|
246 | + $pos = strlen($text) - 1; |
|
247 | + $c = ord($text[$pos]) * -1; |
|
248 | + |
|
249 | + // if we got a null byte at the end of the string, |
|
250 | + // just return |
|
251 | + if ($c == 0) |
|
252 | + return true; |
|
253 | + |
|
254 | + $text = substr($text, 0, $c); |
|
255 | + return true; |
|
256 | + } |
|
257 | + |
|
258 | + |
|
259 | + /** |
|
260 | + * Pads a string using PKCS7 |
|
261 | + * Adds padding using the bytes with the value of the |
|
262 | + * number of bytes need for padding |
|
263 | + * |
|
264 | + * @param string $text The string to pad |
|
265 | + * @param integer The number of bytes to pad |
|
266 | + * @param integer $bytes |
|
267 | + * @return boolean Returns true |
|
268 | + */ |
|
269 | + private static function pkcs7Pad(&$text, $bytes) |
|
270 | + { |
|
271 | + $len = $bytes + strlen($text); |
|
272 | + $text = str_pad($text, $len, chr($bytes), STR_PAD_RIGHT); |
|
273 | + return true; |
|
274 | + } |
|
275 | + |
|
276 | + |
|
277 | + /** |
|
278 | + * Strips PKCS7 padding from a string |
|
279 | + * |
|
280 | + * @param string $text The string to strip padding from |
|
281 | + * @return boolean Returns true |
|
282 | + */ |
|
283 | + private static function pkcs7Strip(&$text) |
|
284 | + { |
|
285 | + $pos = strlen($text) - 1; |
|
286 | + $c = ord($text[$pos]); |
|
287 | + |
|
288 | + if ($c == 0) |
|
289 | + return true; |
|
290 | + |
|
291 | + $text = preg_replace('/'.preg_quote(chr($c)).'{'.$c.'}$/', "", $text); |
|
292 | + return true; |
|
293 | + } |
|
294 | + |
|
295 | + |
|
296 | + /** |
|
297 | + * Pads a string using ISO/IEC 7816-4 |
|
298 | + * Adds byte 0x80 followed by null bytes to pad a string |
|
299 | + * |
|
300 | + * @param string $text The string to pad |
|
301 | + * @param integer The number of bytes to pad |
|
302 | + * @param integer $bytes |
|
303 | + * @return boolean Returns true |
|
304 | + */ |
|
305 | + private static function iso7816Pad(&$text, $bytes) |
|
306 | + { |
|
307 | + $text .= chr(0x80); |
|
308 | + $len = $bytes + strlen($text); |
|
309 | + |
|
310 | + // if we are only padding one byte, then 0x80 is all we need |
|
311 | + // else we follow up with null bytes |
|
312 | + if ($bytes > 1) |
|
313 | + $text = str_pad($text, ($len - 1), chr(0), STR_PAD_RIGHT); |
|
314 | + |
|
315 | + return true; |
|
316 | + } |
|
317 | + |
|
318 | + |
|
319 | + /** |
|
320 | + * Strips ISO/IEC 7816-4 padding from a string |
|
321 | + * |
|
322 | + * @param string $text The string to strip padding from |
|
323 | + * @return boolean Returns true |
|
324 | + */ |
|
325 | + private static function iso7816Strip(&$text) |
|
326 | + { |
|
327 | + $c = chr(0x80); |
|
328 | + $text = preg_replace('/'.preg_quote($c).'\0*$/', '', $text); |
|
329 | + return true; |
|
330 | + } |
|
331 | 331 | } |
332 | 332 | ?> |
@@ -35,423 +35,423 @@ |
||
35 | 35 | */ |
36 | 36 | class PHP_Crypt |
37 | 37 | { |
38 | - // Ciphers |
|
39 | - const CIPHER_3DES = "3DES"; |
|
40 | - const CIPHER_3WAY = "3-Way"; |
|
41 | - const CIPHER_AES_128 = "AES-128"; |
|
42 | - const CIPHER_AES_192 = "AES-192"; |
|
43 | - const CIPHER_AES_256 = "AES-256"; |
|
44 | - const CIPHER_ARC4 = "ARC4"; // Alternative RC4 |
|
45 | - const CIPHER_BLOWFISH = "Blowfish"; |
|
46 | - const CIPHER_CAST_128 = "CAST-128"; |
|
47 | - const CIPHER_CAST_256 = "CAST-256"; |
|
48 | - const CIPHER_DES = "DES"; |
|
49 | - const CIPHER_ENIGMA = "Enigma"; |
|
50 | - const CIPHER_GOST = "GOST"; |
|
51 | - const CIPHER_RC2 = "RC2"; |
|
52 | - const CIPHER_RIJNDAEL_128 = "Rijndael-128"; |
|
53 | - const CIPHER_RIJNDAEL_192 = "Rijndael-192"; |
|
54 | - const CIPHER_RIJNDAEL_256 = "Rijndael-256"; |
|
55 | - const CIPHER_SKIPJACK = "Skipjack"; |
|
56 | - const CIPHER_SIMPLEXOR = "SimpleXOR"; |
|
57 | - const CIPHER_VIGENERE = "Vigenere"; // historical |
|
58 | - |
|
59 | - // Modes |
|
60 | - const MODE_CBC = "CBC"; |
|
61 | - const MODE_CFB = "CFB"; // 8 bit cfb mode |
|
62 | - const MODE_CTR = "CTR"; |
|
63 | - const MODE_ECB = "ECB"; |
|
64 | - const MODE_NCFB = "NCFB"; // blocksize cfb mode |
|
65 | - const MODE_NOFB = "NOFB"; // blocksize ofb mode |
|
66 | - const MODE_OFB = "OFB"; // 8 bit ofb mode |
|
67 | - const MODE_PCBC = "PCBC"; |
|
68 | - const MODE_RAW = "Raw"; // raw encryption, with no mode |
|
69 | - const MODE_STREAM = "Stream"; // used only for stream ciphers |
|
70 | - |
|
71 | - // The source of random data used to create keys and IV's |
|
72 | - // Used for PHP_Crypt::createKey(), PHP_Crypt::createIV() |
|
73 | - const RAND = "rand"; // uses mt_rand(), windows & unix |
|
74 | - const RAND_DEV_RAND = "/dev/random"; // unix only |
|
75 | - const RAND_DEV_URAND = "/dev/urandom"; // unix only |
|
76 | - const RAND_WIN_COM = "wincom"; // windows only, COM extension |
|
77 | - const RAND_DEFAULT_SZ = 32; // the default number of bytes returned |
|
78 | - |
|
79 | - // Padding types |
|
80 | - const PAD_ZERO = 0; |
|
81 | - const PAD_ANSI_X923 = 1; |
|
82 | - const PAD_ISO_10126 = 2; |
|
83 | - const PAD_PKCS7 = 3; |
|
84 | - const PAD_ISO_7816_4 = 4; |
|
85 | - |
|
86 | - |
|
87 | - /** @type object $cipher An instance of the cipher object selected */ |
|
88 | - private $cipher = null; |
|
89 | - |
|
90 | - /** @type object $mode An instance of the mode object selected */ |
|
91 | - private $mode = null; |
|
92 | - |
|
93 | - |
|
94 | - /** |
|
95 | - * Constructor |
|
96 | - * |
|
97 | - * @param string $key The key to use for the selected Cipher |
|
98 | - * @param string $cipher The type of cipher to use |
|
99 | - * @param string $mode The encrypt mode to use with the cipher |
|
100 | - * @param integer $padding The padding type to use. Defaults to PAD_ZERO |
|
101 | - * @return string |
|
102 | - */ |
|
103 | - public function __construct($key, $cipher = self::CIPHER_AES_128, $mode = self::MODE_ECB, $padding = self::PAD_ZERO) |
|
104 | - { |
|
105 | - /* |
|
38 | + // Ciphers |
|
39 | + const CIPHER_3DES = "3DES"; |
|
40 | + const CIPHER_3WAY = "3-Way"; |
|
41 | + const CIPHER_AES_128 = "AES-128"; |
|
42 | + const CIPHER_AES_192 = "AES-192"; |
|
43 | + const CIPHER_AES_256 = "AES-256"; |
|
44 | + const CIPHER_ARC4 = "ARC4"; // Alternative RC4 |
|
45 | + const CIPHER_BLOWFISH = "Blowfish"; |
|
46 | + const CIPHER_CAST_128 = "CAST-128"; |
|
47 | + const CIPHER_CAST_256 = "CAST-256"; |
|
48 | + const CIPHER_DES = "DES"; |
|
49 | + const CIPHER_ENIGMA = "Enigma"; |
|
50 | + const CIPHER_GOST = "GOST"; |
|
51 | + const CIPHER_RC2 = "RC2"; |
|
52 | + const CIPHER_RIJNDAEL_128 = "Rijndael-128"; |
|
53 | + const CIPHER_RIJNDAEL_192 = "Rijndael-192"; |
|
54 | + const CIPHER_RIJNDAEL_256 = "Rijndael-256"; |
|
55 | + const CIPHER_SKIPJACK = "Skipjack"; |
|
56 | + const CIPHER_SIMPLEXOR = "SimpleXOR"; |
|
57 | + const CIPHER_VIGENERE = "Vigenere"; // historical |
|
58 | + |
|
59 | + // Modes |
|
60 | + const MODE_CBC = "CBC"; |
|
61 | + const MODE_CFB = "CFB"; // 8 bit cfb mode |
|
62 | + const MODE_CTR = "CTR"; |
|
63 | + const MODE_ECB = "ECB"; |
|
64 | + const MODE_NCFB = "NCFB"; // blocksize cfb mode |
|
65 | + const MODE_NOFB = "NOFB"; // blocksize ofb mode |
|
66 | + const MODE_OFB = "OFB"; // 8 bit ofb mode |
|
67 | + const MODE_PCBC = "PCBC"; |
|
68 | + const MODE_RAW = "Raw"; // raw encryption, with no mode |
|
69 | + const MODE_STREAM = "Stream"; // used only for stream ciphers |
|
70 | + |
|
71 | + // The source of random data used to create keys and IV's |
|
72 | + // Used for PHP_Crypt::createKey(), PHP_Crypt::createIV() |
|
73 | + const RAND = "rand"; // uses mt_rand(), windows & unix |
|
74 | + const RAND_DEV_RAND = "/dev/random"; // unix only |
|
75 | + const RAND_DEV_URAND = "/dev/urandom"; // unix only |
|
76 | + const RAND_WIN_COM = "wincom"; // windows only, COM extension |
|
77 | + const RAND_DEFAULT_SZ = 32; // the default number of bytes returned |
|
78 | + |
|
79 | + // Padding types |
|
80 | + const PAD_ZERO = 0; |
|
81 | + const PAD_ANSI_X923 = 1; |
|
82 | + const PAD_ISO_10126 = 2; |
|
83 | + const PAD_PKCS7 = 3; |
|
84 | + const PAD_ISO_7816_4 = 4; |
|
85 | + |
|
86 | + |
|
87 | + /** @type object $cipher An instance of the cipher object selected */ |
|
88 | + private $cipher = null; |
|
89 | + |
|
90 | + /** @type object $mode An instance of the mode object selected */ |
|
91 | + private $mode = null; |
|
92 | + |
|
93 | + |
|
94 | + /** |
|
95 | + * Constructor |
|
96 | + * |
|
97 | + * @param string $key The key to use for the selected Cipher |
|
98 | + * @param string $cipher The type of cipher to use |
|
99 | + * @param string $mode The encrypt mode to use with the cipher |
|
100 | + * @param integer $padding The padding type to use. Defaults to PAD_ZERO |
|
101 | + * @return string |
|
102 | + */ |
|
103 | + public function __construct($key, $cipher = self::CIPHER_AES_128, $mode = self::MODE_ECB, $padding = self::PAD_ZERO) |
|
104 | + { |
|
105 | + /* |
|
106 | 106 | * CIPHERS |
107 | 107 | */ |
108 | - switch ($cipher) |
|
109 | - { |
|
110 | - case self::CIPHER_3DES: |
|
111 | - $this->cipher = new Cipher_3DES($key); |
|
112 | - break; |
|
108 | + switch ($cipher) |
|
109 | + { |
|
110 | + case self::CIPHER_3DES: |
|
111 | + $this->cipher = new Cipher_3DES($key); |
|
112 | + break; |
|
113 | 113 | |
114 | - case self::CIPHER_3WAY: |
|
115 | - $this->cipher = new Cipher_3WAY($key); |
|
116 | - break; |
|
114 | + case self::CIPHER_3WAY: |
|
115 | + $this->cipher = new Cipher_3WAY($key); |
|
116 | + break; |
|
117 | 117 | |
118 | - case self::CIPHER_AES_128: |
|
119 | - $this->cipher = new Cipher_AES_128($key); |
|
120 | - break; |
|
118 | + case self::CIPHER_AES_128: |
|
119 | + $this->cipher = new Cipher_AES_128($key); |
|
120 | + break; |
|
121 | 121 | |
122 | - case self::CIPHER_AES_192: |
|
123 | - $this->cipher = new Cipher_AES_192($key); |
|
124 | - break; |
|
122 | + case self::CIPHER_AES_192: |
|
123 | + $this->cipher = new Cipher_AES_192($key); |
|
124 | + break; |
|
125 | 125 | |
126 | - case self::CIPHER_AES_256: |
|
127 | - $this->cipher = new Cipher_AES_256($key); |
|
128 | - break; |
|
126 | + case self::CIPHER_AES_256: |
|
127 | + $this->cipher = new Cipher_AES_256($key); |
|
128 | + break; |
|
129 | 129 | |
130 | - case self::CIPHER_ARC4: // an alternative to RC4 |
|
131 | - $this->cipher = new Cipher_ARC4($key); |
|
132 | - break; |
|
130 | + case self::CIPHER_ARC4: // an alternative to RC4 |
|
131 | + $this->cipher = new Cipher_ARC4($key); |
|
132 | + break; |
|
133 | 133 | |
134 | - case self::CIPHER_BLOWFISH: |
|
135 | - $this->cipher = new Cipher_Blowfish($key); |
|
136 | - break; |
|
134 | + case self::CIPHER_BLOWFISH: |
|
135 | + $this->cipher = new Cipher_Blowfish($key); |
|
136 | + break; |
|
137 | 137 | |
138 | - case self::CIPHER_CAST_128: |
|
139 | - $this->cipher = new Cipher_CAST_128($key); |
|
140 | - break; |
|
138 | + case self::CIPHER_CAST_128: |
|
139 | + $this->cipher = new Cipher_CAST_128($key); |
|
140 | + break; |
|
141 | 141 | |
142 | - case self::CIPHER_CAST_256: |
|
143 | - $this->cipher = new Cipher_CAST_256($key); |
|
144 | - break; |
|
142 | + case self::CIPHER_CAST_256: |
|
143 | + $this->cipher = new Cipher_CAST_256($key); |
|
144 | + break; |
|
145 | 145 | |
146 | - case self::CIPHER_DES: |
|
147 | - $this->cipher = new Cipher_DES($key); |
|
148 | - break; |
|
146 | + case self::CIPHER_DES: |
|
147 | + $this->cipher = new Cipher_DES($key); |
|
148 | + break; |
|
149 | 149 | |
150 | - case self::CIPHER_ENIGMA: |
|
151 | - $this->cipher = new Cipher_Enigma($key); |
|
152 | - break; |
|
150 | + case self::CIPHER_ENIGMA: |
|
151 | + $this->cipher = new Cipher_Enigma($key); |
|
152 | + break; |
|
153 | 153 | |
154 | - case self::CIPHER_GOST: |
|
155 | - $this->cipher = new Cipher_GOST($key); |
|
156 | - break; |
|
154 | + case self::CIPHER_GOST: |
|
155 | + $this->cipher = new Cipher_GOST($key); |
|
156 | + break; |
|
157 | 157 | |
158 | - case self::CIPHER_RC2: |
|
159 | - $this->cipher = new Cipher_RC2($key); |
|
160 | - break; |
|
158 | + case self::CIPHER_RC2: |
|
159 | + $this->cipher = new Cipher_RC2($key); |
|
160 | + break; |
|
161 | 161 | |
162 | - case self::CIPHER_RIJNDAEL_128: |
|
163 | - $this->cipher = new Cipher_Rijndael_128($key); |
|
164 | - break; |
|
162 | + case self::CIPHER_RIJNDAEL_128: |
|
163 | + $this->cipher = new Cipher_Rijndael_128($key); |
|
164 | + break; |
|
165 | 165 | |
166 | - case self::CIPHER_RIJNDAEL_192: |
|
167 | - $this->cipher = new Cipher_Rijndael_192($key); |
|
168 | - break; |
|
166 | + case self::CIPHER_RIJNDAEL_192: |
|
167 | + $this->cipher = new Cipher_Rijndael_192($key); |
|
168 | + break; |
|
169 | 169 | |
170 | - case self::CIPHER_RIJNDAEL_256: |
|
171 | - $this->cipher = new Cipher_Rijndael_256($key); |
|
172 | - break; |
|
170 | + case self::CIPHER_RIJNDAEL_256: |
|
171 | + $this->cipher = new Cipher_Rijndael_256($key); |
|
172 | + break; |
|
173 | 173 | |
174 | - case self::CIPHER_SIMPLEXOR: |
|
175 | - $this->cipher = new Cipher_Simple_XOR($key); |
|
176 | - break; |
|
174 | + case self::CIPHER_SIMPLEXOR: |
|
175 | + $this->cipher = new Cipher_Simple_XOR($key); |
|
176 | + break; |
|
177 | 177 | |
178 | - case self::CIPHER_SKIPJACK: |
|
179 | - $this->cipher = new Cipher_Skipjack($key); |
|
180 | - break; |
|
178 | + case self::CIPHER_SKIPJACK: |
|
179 | + $this->cipher = new Cipher_Skipjack($key); |
|
180 | + break; |
|
181 | 181 | |
182 | - case self::CIPHER_VIGENERE: |
|
183 | - $this->cipher = new Cipher_Vigenere($key); |
|
184 | - break; |
|
182 | + case self::CIPHER_VIGENERE: |
|
183 | + $this->cipher = new Cipher_Vigenere($key); |
|
184 | + break; |
|
185 | 185 | |
186 | - default: |
|
187 | - trigger_error("$cipher is not a valid cipher", E_USER_WARNING); |
|
188 | - } |
|
186 | + default: |
|
187 | + trigger_error("$cipher is not a valid cipher", E_USER_WARNING); |
|
188 | + } |
|
189 | 189 | |
190 | 190 | |
191 | - /* |
|
191 | + /* |
|
192 | 192 | * MODES |
193 | 193 | */ |
194 | - switch ($mode) |
|
195 | - { |
|
196 | - case self::MODE_CBC: |
|
197 | - $this->mode = new Mode_CBC($this->cipher); |
|
198 | - break; |
|
199 | - |
|
200 | - case self::MODE_CFB: |
|
201 | - $this->mode = new Mode_CFB($this->cipher); |
|
202 | - break; |
|
203 | - |
|
204 | - case self::MODE_CTR: |
|
205 | - $this->mode = new Mode_CTR($this->cipher); |
|
206 | - break; |
|
207 | - |
|
208 | - case self::MODE_ECB: |
|
209 | - $this->mode = new Mode_ECB($this->cipher); |
|
210 | - break; |
|
211 | - |
|
212 | - case self::MODE_NCFB: |
|
213 | - $this->mode = new Mode_NCFB($this->cipher); |
|
214 | - break; |
|
215 | - |
|
216 | - case self::MODE_NOFB: |
|
217 | - $this->mode = new Mode_NOFB($this->cipher); |
|
218 | - break; |
|
219 | - |
|
220 | - case self::MODE_OFB: |
|
221 | - $this->mode = new Mode_OFB($this->cipher); |
|
222 | - break; |
|
223 | - |
|
224 | - case self::MODE_PCBC: |
|
225 | - $this->mode = new Mode_PCBC($this->cipher); |
|
226 | - break; |
|
227 | - |
|
228 | - case self::MODE_RAW: |
|
229 | - $this->mode = new Mode_RAW($this->cipher); |
|
230 | - break; |
|
231 | - |
|
232 | - case self::MODE_STREAM: |
|
233 | - $this->mode = new Mode_Stream($this->cipher); |
|
234 | - break; |
|
235 | - |
|
236 | - default: |
|
237 | - trigger_error("$mode is not a valid mode", E_USER_WARNING); |
|
238 | - } |
|
239 | - |
|
240 | - // set the default padding |
|
241 | - $this->padding($padding); |
|
242 | - } |
|
243 | - |
|
244 | - |
|
245 | - /** |
|
246 | - * Destructor |
|
247 | - * |
|
248 | - * @return void |
|
249 | - */ |
|
250 | - public function __destruct() |
|
251 | - { |
|
252 | - |
|
253 | - } |
|
254 | - |
|
255 | - |
|
256 | - /** |
|
257 | - * Encrypt a plain text message using the Mode and Cipher selected. |
|
258 | - * Some stream modes require this function to be called in a loop |
|
259 | - * which requires the use of $result parameter to retrieve |
|
260 | - * the decrypted data. |
|
261 | - * |
|
262 | - * @param string $text The plain text string |
|
263 | - * @return string The encrypted string |
|
264 | - */ |
|
265 | - public function encrypt($text) |
|
266 | - { |
|
267 | - // check that an iv is set, if required by the mode |
|
268 | - $this->mode->checkIV(); |
|
269 | - |
|
270 | - // the encryption is done inside the mode |
|
271 | - $this->mode->encrypt($text); |
|
272 | - return $text; |
|
273 | - } |
|
274 | - |
|
275 | - |
|
276 | - /** |
|
277 | - * Decrypt an encrypted message using the Mode and Cipher selected. |
|
278 | - * Some stream modes require this function to be called in a loop |
|
279 | - * which requires the use of $result parameter to retrieve |
|
280 | - * the decrypted data. |
|
281 | - * |
|
282 | - * @param string $text The encrypted string |
|
283 | - * @return string The decrypted string |
|
284 | - */ |
|
285 | - public function decrypt($text) |
|
286 | - { |
|
287 | - // check that an iv is set, if required by the mode |
|
288 | - $this->mode->checkIV(); |
|
289 | - |
|
290 | - // the decryption is done inside the mode |
|
291 | - $this->mode->decrypt($text); |
|
292 | - return $text; |
|
293 | - } |
|
294 | - |
|
295 | - |
|
296 | - /** |
|
297 | - * Return the cipher object being used |
|
298 | - * |
|
299 | - * @return object The Cipher object |
|
300 | - */ |
|
301 | - public function cipher() |
|
302 | - { |
|
303 | - return $this->cipher; |
|
304 | - } |
|
305 | - |
|
306 | - |
|
307 | - /** |
|
308 | - * Return the mode object being used |
|
309 | - * |
|
310 | - * @return object The Mode object |
|
311 | - */ |
|
312 | - public function mode() |
|
313 | - { |
|
314 | - return $this->mode; |
|
315 | - } |
|
316 | - |
|
317 | - |
|
318 | - /** |
|
319 | - * Returns the name of the cipher being used |
|
320 | - * |
|
321 | - * @return string The name of the cipher currently in use, |
|
322 | - * it will be one of the predefined phpCrypt cipher constants |
|
323 | - */ |
|
324 | - public function cipherName() |
|
325 | - { |
|
326 | - return $this->cipher->name(); |
|
327 | - } |
|
328 | - |
|
329 | - |
|
330 | - /** |
|
331 | - * Return the name of the mode being used |
|
332 | - * |
|
333 | - * @return string The name of the mode in use, it will |
|
334 | - * be one of the predefined phpCrypt mode constants |
|
335 | - */ |
|
336 | - public function modeName() |
|
337 | - { |
|
338 | - return $this->mode->name(); |
|
339 | - } |
|
340 | - |
|
341 | - |
|
342 | - /** |
|
343 | - * Returns Ciphers required block size in bytes |
|
344 | - * |
|
345 | - * @return integer The cipher data block size, in bytes |
|
346 | - */ |
|
347 | - public function cipherBlockSize() |
|
348 | - { |
|
349 | - return $this->cipher->blockSize(); |
|
350 | - } |
|
351 | - |
|
352 | - |
|
353 | - /** |
|
354 | - * Returns the cipher's required key size, in bytes |
|
355 | - * |
|
356 | - * @return integer The cipher's key size requirement, in bytes |
|
357 | - */ |
|
358 | - public function cipherKeySize() |
|
359 | - { |
|
360 | - return $this->cipher->keySize(); |
|
361 | - } |
|
362 | - |
|
363 | - |
|
364 | - /** |
|
365 | - * Sets and/or returns the key to be used. Normally you set |
|
366 | - * the key in the phpCrypt constructor. This can be usefully |
|
367 | - * if you need to change the key on the fly and don't want |
|
368 | - * to create a new instance of phpCrypt. |
|
369 | - * |
|
370 | - * If the $key parameter is not given, this function will simply |
|
371 | - * return the key currently in use. |
|
372 | - * |
|
373 | - * @param string $key Optional, The key to set |
|
374 | - * @return string The key being used |
|
375 | - */ |
|
376 | - public function cipherKey($key = "") |
|
377 | - { |
|
378 | - return $this->cipher->key($key); |
|
379 | - } |
|
380 | - |
|
381 | - |
|
382 | - /** |
|
383 | - * A helper function which will create a random key. Calls |
|
384 | - * Core::randBytes(). By default it will use PHP_Crypt::RAND for |
|
385 | - * the random source of bytes, and return a PHP_Crypt::RAND_DEFAULT_SZ |
|
386 | - * byte string. There are 4 ways to create a random byte string by |
|
387 | - * setting the $src parameter: |
|
388 | - * PHP_Crypt::RAND - Default, uses mt_rand() |
|
389 | - * PHP_Crypt::RAND_DEV_RAND - Unix only, uses /dev/random |
|
390 | - * PHP_Crypt::RAND_DEV_URAND - Unix only, uses /dev/urandom |
|
391 | - * PHP_Crypt::RAND_WIN_COM - Windows only, uses Microsoft's CAPICOM SDK |
|
392 | - * |
|
393 | - * @param string $src Optional, The source to use to create random bytes |
|
394 | - * @param integer $len Optional, The number of random bytes to return |
|
395 | - * @return string A random string of bytes |
|
396 | - */ |
|
397 | - public static function createKey($src = self::RAND, $len = self::RAND_DEFAULT_SZ) |
|
398 | - { |
|
399 | - return Core::randBytes($src, $len); |
|
400 | - } |
|
401 | - |
|
402 | - |
|
403 | - /** |
|
404 | - * Sets the IV to use. Note that you do not need to call |
|
405 | - * this function if creating an IV using createIV(). This |
|
406 | - * function is used when an IV has already been created |
|
407 | - * outside of phpCrypt and needs to be set. Alternatively |
|
408 | - * you can just pass the $iv parameter to the encrypt() |
|
409 | - * or decrypt() functions |
|
410 | - * |
|
411 | - * When the $iv parameter is not given, the function will |
|
412 | - * return the current IV being used. See createIV() if you |
|
413 | - * need to create an IV. |
|
414 | - * |
|
415 | - * @param string $iv Optional, The IV to use during Encryption/Decryption |
|
416 | - * @return void |
|
417 | - */ |
|
418 | - public function IV($iv = "") |
|
419 | - { |
|
420 | - return $this->mode->IV($iv); |
|
421 | - } |
|
422 | - |
|
423 | - |
|
424 | - /** |
|
425 | - * Creates an IV for the the Cipher selected, if one is required. |
|
426 | - * If you already have an IV to use, this function does not need |
|
427 | - * to be called, instead set it with setIV(). If you create an |
|
428 | - * IV with createIV(), you do not need to set it with setIV(), |
|
429 | - * as it is automatically set in this function |
|
430 | - * |
|
431 | - * $src values are: |
|
432 | - * PHP_Crypt::RAND - Default, uses mt_rand() |
|
433 | - * PHP_Crypt::RAND_DEV_RAND - Unix only, uses /dev/random |
|
434 | - * PHP_Crypt::RAND_DEV_URAND - Unix only, uses /dev/urandom |
|
435 | - * PHP_Crypt::RAND_WIN_COM - Windows only, uses Microsoft's CAPICOM SDK |
|
436 | - * |
|
437 | - * @param string $src Optional, how the IV is generated |
|
438 | - * @return string The IV that was created, and set for the mode |
|
439 | - */ |
|
440 | - public function createIV($src = self::RAND) |
|
441 | - { |
|
442 | - return $this->mode->createIV($src); |
|
443 | - } |
|
444 | - |
|
445 | - |
|
446 | - /** |
|
447 | - * Sets the type of padding to be used within the specified Mode |
|
448 | - * |
|
449 | - * @param string $type One of the predefined padding types |
|
450 | - * @return void |
|
451 | - */ |
|
452 | - public function padding($type = "") |
|
453 | - { |
|
454 | - return $this->mode->padding($type); |
|
455 | - } |
|
194 | + switch ($mode) |
|
195 | + { |
|
196 | + case self::MODE_CBC: |
|
197 | + $this->mode = new Mode_CBC($this->cipher); |
|
198 | + break; |
|
199 | + |
|
200 | + case self::MODE_CFB: |
|
201 | + $this->mode = new Mode_CFB($this->cipher); |
|
202 | + break; |
|
203 | + |
|
204 | + case self::MODE_CTR: |
|
205 | + $this->mode = new Mode_CTR($this->cipher); |
|
206 | + break; |
|
207 | + |
|
208 | + case self::MODE_ECB: |
|
209 | + $this->mode = new Mode_ECB($this->cipher); |
|
210 | + break; |
|
211 | + |
|
212 | + case self::MODE_NCFB: |
|
213 | + $this->mode = new Mode_NCFB($this->cipher); |
|
214 | + break; |
|
215 | + |
|
216 | + case self::MODE_NOFB: |
|
217 | + $this->mode = new Mode_NOFB($this->cipher); |
|
218 | + break; |
|
219 | + |
|
220 | + case self::MODE_OFB: |
|
221 | + $this->mode = new Mode_OFB($this->cipher); |
|
222 | + break; |
|
223 | + |
|
224 | + case self::MODE_PCBC: |
|
225 | + $this->mode = new Mode_PCBC($this->cipher); |
|
226 | + break; |
|
227 | + |
|
228 | + case self::MODE_RAW: |
|
229 | + $this->mode = new Mode_RAW($this->cipher); |
|
230 | + break; |
|
231 | + |
|
232 | + case self::MODE_STREAM: |
|
233 | + $this->mode = new Mode_Stream($this->cipher); |
|
234 | + break; |
|
235 | + |
|
236 | + default: |
|
237 | + trigger_error("$mode is not a valid mode", E_USER_WARNING); |
|
238 | + } |
|
239 | + |
|
240 | + // set the default padding |
|
241 | + $this->padding($padding); |
|
242 | + } |
|
243 | + |
|
244 | + |
|
245 | + /** |
|
246 | + * Destructor |
|
247 | + * |
|
248 | + * @return void |
|
249 | + */ |
|
250 | + public function __destruct() |
|
251 | + { |
|
252 | + |
|
253 | + } |
|
254 | + |
|
255 | + |
|
256 | + /** |
|
257 | + * Encrypt a plain text message using the Mode and Cipher selected. |
|
258 | + * Some stream modes require this function to be called in a loop |
|
259 | + * which requires the use of $result parameter to retrieve |
|
260 | + * the decrypted data. |
|
261 | + * |
|
262 | + * @param string $text The plain text string |
|
263 | + * @return string The encrypted string |
|
264 | + */ |
|
265 | + public function encrypt($text) |
|
266 | + { |
|
267 | + // check that an iv is set, if required by the mode |
|
268 | + $this->mode->checkIV(); |
|
269 | + |
|
270 | + // the encryption is done inside the mode |
|
271 | + $this->mode->encrypt($text); |
|
272 | + return $text; |
|
273 | + } |
|
274 | + |
|
275 | + |
|
276 | + /** |
|
277 | + * Decrypt an encrypted message using the Mode and Cipher selected. |
|
278 | + * Some stream modes require this function to be called in a loop |
|
279 | + * which requires the use of $result parameter to retrieve |
|
280 | + * the decrypted data. |
|
281 | + * |
|
282 | + * @param string $text The encrypted string |
|
283 | + * @return string The decrypted string |
|
284 | + */ |
|
285 | + public function decrypt($text) |
|
286 | + { |
|
287 | + // check that an iv is set, if required by the mode |
|
288 | + $this->mode->checkIV(); |
|
289 | + |
|
290 | + // the decryption is done inside the mode |
|
291 | + $this->mode->decrypt($text); |
|
292 | + return $text; |
|
293 | + } |
|
294 | + |
|
295 | + |
|
296 | + /** |
|
297 | + * Return the cipher object being used |
|
298 | + * |
|
299 | + * @return object The Cipher object |
|
300 | + */ |
|
301 | + public function cipher() |
|
302 | + { |
|
303 | + return $this->cipher; |
|
304 | + } |
|
305 | + |
|
306 | + |
|
307 | + /** |
|
308 | + * Return the mode object being used |
|
309 | + * |
|
310 | + * @return object The Mode object |
|
311 | + */ |
|
312 | + public function mode() |
|
313 | + { |
|
314 | + return $this->mode; |
|
315 | + } |
|
316 | + |
|
317 | + |
|
318 | + /** |
|
319 | + * Returns the name of the cipher being used |
|
320 | + * |
|
321 | + * @return string The name of the cipher currently in use, |
|
322 | + * it will be one of the predefined phpCrypt cipher constants |
|
323 | + */ |
|
324 | + public function cipherName() |
|
325 | + { |
|
326 | + return $this->cipher->name(); |
|
327 | + } |
|
328 | + |
|
329 | + |
|
330 | + /** |
|
331 | + * Return the name of the mode being used |
|
332 | + * |
|
333 | + * @return string The name of the mode in use, it will |
|
334 | + * be one of the predefined phpCrypt mode constants |
|
335 | + */ |
|
336 | + public function modeName() |
|
337 | + { |
|
338 | + return $this->mode->name(); |
|
339 | + } |
|
340 | + |
|
341 | + |
|
342 | + /** |
|
343 | + * Returns Ciphers required block size in bytes |
|
344 | + * |
|
345 | + * @return integer The cipher data block size, in bytes |
|
346 | + */ |
|
347 | + public function cipherBlockSize() |
|
348 | + { |
|
349 | + return $this->cipher->blockSize(); |
|
350 | + } |
|
351 | + |
|
352 | + |
|
353 | + /** |
|
354 | + * Returns the cipher's required key size, in bytes |
|
355 | + * |
|
356 | + * @return integer The cipher's key size requirement, in bytes |
|
357 | + */ |
|
358 | + public function cipherKeySize() |
|
359 | + { |
|
360 | + return $this->cipher->keySize(); |
|
361 | + } |
|
362 | + |
|
363 | + |
|
364 | + /** |
|
365 | + * Sets and/or returns the key to be used. Normally you set |
|
366 | + * the key in the phpCrypt constructor. This can be usefully |
|
367 | + * if you need to change the key on the fly and don't want |
|
368 | + * to create a new instance of phpCrypt. |
|
369 | + * |
|
370 | + * If the $key parameter is not given, this function will simply |
|
371 | + * return the key currently in use. |
|
372 | + * |
|
373 | + * @param string $key Optional, The key to set |
|
374 | + * @return string The key being used |
|
375 | + */ |
|
376 | + public function cipherKey($key = "") |
|
377 | + { |
|
378 | + return $this->cipher->key($key); |
|
379 | + } |
|
380 | + |
|
381 | + |
|
382 | + /** |
|
383 | + * A helper function which will create a random key. Calls |
|
384 | + * Core::randBytes(). By default it will use PHP_Crypt::RAND for |
|
385 | + * the random source of bytes, and return a PHP_Crypt::RAND_DEFAULT_SZ |
|
386 | + * byte string. There are 4 ways to create a random byte string by |
|
387 | + * setting the $src parameter: |
|
388 | + * PHP_Crypt::RAND - Default, uses mt_rand() |
|
389 | + * PHP_Crypt::RAND_DEV_RAND - Unix only, uses /dev/random |
|
390 | + * PHP_Crypt::RAND_DEV_URAND - Unix only, uses /dev/urandom |
|
391 | + * PHP_Crypt::RAND_WIN_COM - Windows only, uses Microsoft's CAPICOM SDK |
|
392 | + * |
|
393 | + * @param string $src Optional, The source to use to create random bytes |
|
394 | + * @param integer $len Optional, The number of random bytes to return |
|
395 | + * @return string A random string of bytes |
|
396 | + */ |
|
397 | + public static function createKey($src = self::RAND, $len = self::RAND_DEFAULT_SZ) |
|
398 | + { |
|
399 | + return Core::randBytes($src, $len); |
|
400 | + } |
|
401 | + |
|
402 | + |
|
403 | + /** |
|
404 | + * Sets the IV to use. Note that you do not need to call |
|
405 | + * this function if creating an IV using createIV(). This |
|
406 | + * function is used when an IV has already been created |
|
407 | + * outside of phpCrypt and needs to be set. Alternatively |
|
408 | + * you can just pass the $iv parameter to the encrypt() |
|
409 | + * or decrypt() functions |
|
410 | + * |
|
411 | + * When the $iv parameter is not given, the function will |
|
412 | + * return the current IV being used. See createIV() if you |
|
413 | + * need to create an IV. |
|
414 | + * |
|
415 | + * @param string $iv Optional, The IV to use during Encryption/Decryption |
|
416 | + * @return void |
|
417 | + */ |
|
418 | + public function IV($iv = "") |
|
419 | + { |
|
420 | + return $this->mode->IV($iv); |
|
421 | + } |
|
422 | + |
|
423 | + |
|
424 | + /** |
|
425 | + * Creates an IV for the the Cipher selected, if one is required. |
|
426 | + * If you already have an IV to use, this function does not need |
|
427 | + * to be called, instead set it with setIV(). If you create an |
|
428 | + * IV with createIV(), you do not need to set it with setIV(), |
|
429 | + * as it is automatically set in this function |
|
430 | + * |
|
431 | + * $src values are: |
|
432 | + * PHP_Crypt::RAND - Default, uses mt_rand() |
|
433 | + * PHP_Crypt::RAND_DEV_RAND - Unix only, uses /dev/random |
|
434 | + * PHP_Crypt::RAND_DEV_URAND - Unix only, uses /dev/urandom |
|
435 | + * PHP_Crypt::RAND_WIN_COM - Windows only, uses Microsoft's CAPICOM SDK |
|
436 | + * |
|
437 | + * @param string $src Optional, how the IV is generated |
|
438 | + * @return string The IV that was created, and set for the mode |
|
439 | + */ |
|
440 | + public function createIV($src = self::RAND) |
|
441 | + { |
|
442 | + return $this->mode->createIV($src); |
|
443 | + } |
|
444 | + |
|
445 | + |
|
446 | + /** |
|
447 | + * Sets the type of padding to be used within the specified Mode |
|
448 | + * |
|
449 | + * @param string $type One of the predefined padding types |
|
450 | + * @return void |
|
451 | + */ |
|
452 | + public function padding($type = "") |
|
453 | + { |
|
454 | + return $this->mode->padding($type); |
|
455 | + } |
|
456 | 456 | } |
457 | 457 | ?> |