@@ -1588,7 +1588,7 @@ discard block |
||
1588 | 1588 | * |
1589 | 1589 | * @param array ref $root |
1590 | 1590 | * @param string $path |
1591 | - * @param object $asn1 |
|
1591 | + * @param ASN1 $asn1 |
|
1592 | 1592 | * @access private |
1593 | 1593 | */ |
1594 | 1594 | function _mapInExtensions(&$root, $path, $asn1) |
@@ -1638,7 +1638,7 @@ discard block |
||
1638 | 1638 | * |
1639 | 1639 | * @param array ref $root |
1640 | 1640 | * @param string $path |
1641 | - * @param object $asn1 |
|
1641 | + * @param ASN1 $asn1 |
|
1642 | 1642 | * @access private |
1643 | 1643 | */ |
1644 | 1644 | function _mapOutExtensions(&$root, $path, $asn1) |
@@ -168,6 +168,7 @@ |
||
168 | 168 | |
169 | 169 | /** |
170 | 170 | * Builds a string to be encoded in a QR code |
171 | + * @param string $label |
|
171 | 172 | */ |
172 | 173 | public function getQRText($label, $secret) |
173 | 174 | { |
@@ -76,6 +76,7 @@ |
||
76 | 76 | * @throws Ex\EnvironmentIsBrokenException |
77 | 77 | * @throws Ex\WrongKeyOrModifiedCiphertextException |
78 | 78 | * |
79 | + * @param string $password |
|
79 | 80 | * @return Key |
80 | 81 | */ |
81 | 82 | public function unlockKey($password) |
@@ -150,6 +150,9 @@ |
||
150 | 150 | return array($hi, $lo); |
151 | 151 | } |
152 | 152 | |
153 | + /** |
|
154 | + * @param string $tag |
|
155 | + */ |
|
153 | 156 | function get_table_pos($tag) { |
154 | 157 | $offset = $this->tables[$tag]['offset']; |
155 | 158 | $length = $this->tables[$tag]['length']; |
@@ -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 |
@@ -164,6 +164,7 @@ |
||
164 | 164 | |
165 | 165 | /** |
166 | 166 | * Get MD5 as binary string |
167 | + * @param string $string |
|
167 | 168 | */ |
168 | 169 | function _md5_16($string) |
169 | 170 | { |
@@ -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 |
@@ -1331,6 +1331,9 @@ |
||
1331 | 1331 | } |
1332 | 1332 | } |
1333 | 1333 | |
1334 | +/** |
|
1335 | + * @param string $size |
|
1336 | + */ |
|
1334 | 1337 | function _getpagesize($size) |
1335 | 1338 | { |
1336 | 1339 | if (is_string($size)) |
@@ -340,7 +340,7 @@ |
||
340 | 340 | * to the self::$_e[] table. |
341 | 341 | * This is only used in the F() function |
342 | 342 | * |
343 | - * @param array $r 32 bit binary, each bit in an array element |
|
343 | + * @param string $r 32 bit binary, each bit in an array element |
|
344 | 344 | * @return string 48 bit binary string |
345 | 345 | */ |
346 | 346 | private function e($r) |
@@ -38,556 +38,556 @@ |
||
38 | 38 | */ |
39 | 39 | class Cipher_DES extends Cipher |
40 | 40 | { |
41 | - /** @type integer BYTES_BLOCK The block size, in bytes */ |
|
42 | - const BYTES_BLOCK = 8; // 64 bits |
|
41 | + /** @type integer BYTES_BLOCK The block size, in bytes */ |
|
42 | + const BYTES_BLOCK = 8; // 64 bits |
|
43 | 43 | |
44 | - /** @type integer BYTES_KEY The key size, in bytes */ |
|
45 | - const BYTES_KEY = 8; // 64 bits |
|
44 | + /** @type integer BYTES_KEY The key size, in bytes */ |
|
45 | + const BYTES_KEY = 8; // 64 bits |
|
46 | 46 | |
47 | - /** @type array $sub_keys The permutated subkeys */ |
|
48 | - protected $sub_keys = array(); |
|
47 | + /** @type array $sub_keys The permutated subkeys */ |
|
48 | + protected $sub_keys = array(); |
|
49 | 49 | |
50 | - /* |
|
50 | + /* |
|
51 | 51 | * Tables initialized in the initTables() |
52 | 52 | */ |
53 | 53 | |
54 | - /** |
|
55 | - * @type array $_pc1 Permutated choice 1 (PC1), |
|
56 | - * This should be considered a constant |
|
57 | - */ |
|
58 | - protected static $_pc1 = array(); |
|
59 | - |
|
60 | - /** |
|
61 | - * @type array $_pc2 Permutated choice 2 (PC2), |
|
62 | - * This should be considered a constant |
|
63 | - */ |
|
64 | - protected static $_pc2 = array(); |
|
65 | - |
|
66 | - /** |
|
67 | - * @type array $_key_sched The key schedule, |
|
68 | - * This should be considered a constant |
|
69 | - */ |
|
70 | - protected static $_key_sched = array(); |
|
71 | - |
|
72 | - /** |
|
73 | - * @type array $_ip The Initial Permutation (IP), |
|
74 | - * This should be considered a constant |
|
75 | - */ |
|
76 | - private static $_ip = array(); |
|
77 | - |
|
78 | - /** |
|
79 | - * @type array $_e The Expansion table (E), |
|
80 | - * This should be considered a constant |
|
81 | - */ |
|
82 | - private static $_e = array(); |
|
83 | - |
|
84 | - /** |
|
85 | - * @type array $_s The Substitution Box (S), |
|
86 | - * This should be considered a constant |
|
87 | - */ |
|
88 | - private static $_s = array(); |
|
89 | - |
|
90 | - /** |
|
91 | - * @type array $_p The Permutation table (P), |
|
92 | - * This should be considered a constant |
|
93 | - */ |
|
94 | - private static $_p = array(); |
|
95 | - |
|
96 | - /** |
|
97 | - * @type array $_ip The The Final Permutation table (FP), |
|
98 | - * This should be considered a constant |
|
99 | - */ |
|
100 | - private static $_fp = array(); |
|
101 | - |
|
102 | - |
|
103 | - /** |
|
104 | - * Constructor, used only when calling this class directly |
|
105 | - * for classes that extend this class, call __construct1() |
|
106 | - * |
|
107 | - * @param string $key The key used for Encryption/Decryption |
|
108 | - * @return void |
|
109 | - */ |
|
110 | - public function __construct($key) |
|
111 | - { |
|
112 | - // set the DES key |
|
113 | - parent::__construct(PHP_Crypt::CIPHER_DES, $key, self::BYTES_KEY); |
|
114 | - |
|
115 | - // initialize variables |
|
116 | - $this->initTables(); |
|
117 | - |
|
118 | - // DES requires that data is 64 bits |
|
119 | - $this->blockSize(self::BYTES_BLOCK); |
|
120 | - |
|
121 | - // create the 16 rounds of 56 bit keys |
|
122 | - $this->keyPermutation(); |
|
123 | - } |
|
124 | - |
|
125 | - |
|
126 | - /** |
|
127 | - * Second Constructor, used only by child classes that extend this class |
|
128 | - * |
|
129 | - * @param string $cipher The name of the cipher extending this class |
|
130 | - * @param string $key The key used for Encryption/Decryption |
|
131 | - * @param integer $key_byte_sz The required byte size of the extending cipher |
|
132 | - * @return void |
|
133 | - */ |
|
134 | - protected function __construct1($cipher, $key, $key_byte_sz) |
|
135 | - { |
|
136 | - // set the key and key size |
|
137 | - parent::__construct($cipher, $key, $key_byte_sz); |
|
138 | - |
|
139 | - // initialize variables |
|
140 | - $this->initTables(); |
|
141 | - } |
|
142 | - |
|
143 | - |
|
144 | - /** |
|
145 | - * Destructor |
|
146 | - * |
|
147 | - * @return void |
|
148 | - */ |
|
149 | - public function __destruct() |
|
150 | - { |
|
151 | - parent::__destruct(); |
|
152 | - } |
|
153 | - |
|
154 | - |
|
155 | - /** |
|
156 | - * Encrypt plain text data using DES |
|
157 | - * |
|
158 | - * @return boolean Returns true |
|
159 | - */ |
|
160 | - public function encrypt(&$text) |
|
161 | - { |
|
162 | - $this->operation(parent::ENCRYPT); |
|
163 | - return $this->des($text); |
|
164 | - } |
|
165 | - |
|
166 | - |
|
167 | - /** |
|
168 | - * Decrypt a DES encrypted string |
|
169 | - * |
|
170 | - * @return boolean Returns true |
|
171 | - */ |
|
172 | - public function decrypt(&$text) |
|
173 | - { |
|
174 | - $this->operation(parent::DECRYPT); |
|
175 | - return $this->des($text); |
|
176 | - } |
|
177 | - |
|
178 | - |
|
179 | - /** |
|
180 | - * This is where the actual encrypt/decryption takes place. Since |
|
181 | - * encryption and decryption are the same algorithm in DES, we only |
|
182 | - * need one function to do both. |
|
183 | - * |
|
184 | - * @param string $data The string to be encrypted or decrypted |
|
185 | - * @return boolean Returns true |
|
186 | - */ |
|
187 | - protected function des(&$data) |
|
188 | - { |
|
189 | - $l = array(); |
|
190 | - $r = array(); |
|
191 | - |
|
192 | - // step two: Initial Permutation (IP) of plaintext |
|
193 | - $data = $this->ip($data); |
|
194 | - |
|
195 | - // divide the permuted block IP into a left half L0 of 32 bits, |
|
196 | - // and a right half R0 of 32 bits |
|
197 | - $l[0] = substr($data, 0, 32); |
|
198 | - $r[0] = substr($data, 32, 32); |
|
199 | - |
|
200 | - for ($n = 1; $n <= 16; ++$n) |
|
201 | - { |
|
202 | - $l[$n] = $r[$n - 1]; |
|
203 | - |
|
204 | - if ($this->operation() == parent::DECRYPT) |
|
205 | - $f = $this->F($r[$n - 1], $this->sub_keys[16 - $n]); |
|
206 | - else |
|
207 | - $f = $this->F($r[$n - 1], $this->sub_keys[$n - 1]); |
|
208 | - |
|
209 | - // XOR F with Ln |
|
210 | - $r[$n] = $this->xorBin($l[$n - 1], $f); |
|
211 | - } |
|
212 | - |
|
213 | - // now we combine L[16] and R[16] back into a 64-bit string, but we reverse |
|
214 | - // L[16] and R[16] so that it becomes R[16]L[16] |
|
215 | - $data = $r[16].$l[16]; |
|
216 | - |
|
217 | - // now do the final permutation |
|
218 | - $data = $this->fp($data); |
|
219 | - $data = parent::bin2Str($data); |
|
220 | - |
|
221 | - return true; |
|
222 | - } |
|
223 | - |
|
224 | - |
|
225 | - /** |
|
226 | - * The Key permutation, based on tables $_pc1 and $_pc2 |
|
227 | - * Create 16 subkeys, each of which is 48-bits long. |
|
228 | - * |
|
229 | - * @return void |
|
230 | - */ |
|
231 | - private function keyPermutation() |
|
232 | - { |
|
233 | - $this->sub_keys = array(); |
|
234 | - $pc1m = array(); |
|
235 | - $pcr = array(); |
|
236 | - $c = array(); |
|
237 | - $d = array(); |
|
238 | - |
|
239 | - // convert the key to binary |
|
240 | - $binkey = parent::str2Bin($this->key()); |
|
241 | - |
|
242 | - // reduce the key down to 56bits based on table $_pc1 |
|
243 | - for ($i = 0; $i < 56; ++$i) |
|
54 | + /** |
|
55 | + * @type array $_pc1 Permutated choice 1 (PC1), |
|
56 | + * This should be considered a constant |
|
57 | + */ |
|
58 | + protected static $_pc1 = array(); |
|
59 | + |
|
60 | + /** |
|
61 | + * @type array $_pc2 Permutated choice 2 (PC2), |
|
62 | + * This should be considered a constant |
|
63 | + */ |
|
64 | + protected static $_pc2 = array(); |
|
65 | + |
|
66 | + /** |
|
67 | + * @type array $_key_sched The key schedule, |
|
68 | + * This should be considered a constant |
|
69 | + */ |
|
70 | + protected static $_key_sched = array(); |
|
71 | + |
|
72 | + /** |
|
73 | + * @type array $_ip The Initial Permutation (IP), |
|
74 | + * This should be considered a constant |
|
75 | + */ |
|
76 | + private static $_ip = array(); |
|
77 | + |
|
78 | + /** |
|
79 | + * @type array $_e The Expansion table (E), |
|
80 | + * This should be considered a constant |
|
81 | + */ |
|
82 | + private static $_e = array(); |
|
83 | + |
|
84 | + /** |
|
85 | + * @type array $_s The Substitution Box (S), |
|
86 | + * This should be considered a constant |
|
87 | + */ |
|
88 | + private static $_s = array(); |
|
89 | + |
|
90 | + /** |
|
91 | + * @type array $_p The Permutation table (P), |
|
92 | + * This should be considered a constant |
|
93 | + */ |
|
94 | + private static $_p = array(); |
|
95 | + |
|
96 | + /** |
|
97 | + * @type array $_ip The The Final Permutation table (FP), |
|
98 | + * This should be considered a constant |
|
99 | + */ |
|
100 | + private static $_fp = array(); |
|
101 | + |
|
102 | + |
|
103 | + /** |
|
104 | + * Constructor, used only when calling this class directly |
|
105 | + * for classes that extend this class, call __construct1() |
|
106 | + * |
|
107 | + * @param string $key The key used for Encryption/Decryption |
|
108 | + * @return void |
|
109 | + */ |
|
110 | + public function __construct($key) |
|
111 | + { |
|
112 | + // set the DES key |
|
113 | + parent::__construct(PHP_Crypt::CIPHER_DES, $key, self::BYTES_KEY); |
|
114 | + |
|
115 | + // initialize variables |
|
116 | + $this->initTables(); |
|
117 | + |
|
118 | + // DES requires that data is 64 bits |
|
119 | + $this->blockSize(self::BYTES_BLOCK); |
|
120 | + |
|
121 | + // create the 16 rounds of 56 bit keys |
|
122 | + $this->keyPermutation(); |
|
123 | + } |
|
124 | + |
|
125 | + |
|
126 | + /** |
|
127 | + * Second Constructor, used only by child classes that extend this class |
|
128 | + * |
|
129 | + * @param string $cipher The name of the cipher extending this class |
|
130 | + * @param string $key The key used for Encryption/Decryption |
|
131 | + * @param integer $key_byte_sz The required byte size of the extending cipher |
|
132 | + * @return void |
|
133 | + */ |
|
134 | + protected function __construct1($cipher, $key, $key_byte_sz) |
|
135 | + { |
|
136 | + // set the key and key size |
|
137 | + parent::__construct($cipher, $key, $key_byte_sz); |
|
138 | + |
|
139 | + // initialize variables |
|
140 | + $this->initTables(); |
|
141 | + } |
|
142 | + |
|
143 | + |
|
144 | + /** |
|
145 | + * Destructor |
|
146 | + * |
|
147 | + * @return void |
|
148 | + */ |
|
149 | + public function __destruct() |
|
150 | + { |
|
151 | + parent::__destruct(); |
|
152 | + } |
|
153 | + |
|
154 | + |
|
155 | + /** |
|
156 | + * Encrypt plain text data using DES |
|
157 | + * |
|
158 | + * @return boolean Returns true |
|
159 | + */ |
|
160 | + public function encrypt(&$text) |
|
161 | + { |
|
162 | + $this->operation(parent::ENCRYPT); |
|
163 | + return $this->des($text); |
|
164 | + } |
|
165 | + |
|
166 | + |
|
167 | + /** |
|
168 | + * Decrypt a DES encrypted string |
|
169 | + * |
|
170 | + * @return boolean Returns true |
|
171 | + */ |
|
172 | + public function decrypt(&$text) |
|
173 | + { |
|
174 | + $this->operation(parent::DECRYPT); |
|
175 | + return $this->des($text); |
|
176 | + } |
|
177 | + |
|
178 | + |
|
179 | + /** |
|
180 | + * This is where the actual encrypt/decryption takes place. Since |
|
181 | + * encryption and decryption are the same algorithm in DES, we only |
|
182 | + * need one function to do both. |
|
183 | + * |
|
184 | + * @param string $data The string to be encrypted or decrypted |
|
185 | + * @return boolean Returns true |
|
186 | + */ |
|
187 | + protected function des(&$data) |
|
188 | + { |
|
189 | + $l = array(); |
|
190 | + $r = array(); |
|
191 | + |
|
192 | + // step two: Initial Permutation (IP) of plaintext |
|
193 | + $data = $this->ip($data); |
|
194 | + |
|
195 | + // divide the permuted block IP into a left half L0 of 32 bits, |
|
196 | + // and a right half R0 of 32 bits |
|
197 | + $l[0] = substr($data, 0, 32); |
|
198 | + $r[0] = substr($data, 32, 32); |
|
199 | + |
|
200 | + for ($n = 1; $n <= 16; ++$n) |
|
201 | + { |
|
202 | + $l[$n] = $r[$n - 1]; |
|
203 | + |
|
204 | + if ($this->operation() == parent::DECRYPT) |
|
205 | + $f = $this->F($r[$n - 1], $this->sub_keys[16 - $n]); |
|
206 | + else |
|
207 | + $f = $this->F($r[$n - 1], $this->sub_keys[$n - 1]); |
|
208 | + |
|
209 | + // XOR F with Ln |
|
210 | + $r[$n] = $this->xorBin($l[$n - 1], $f); |
|
211 | + } |
|
212 | + |
|
213 | + // now we combine L[16] and R[16] back into a 64-bit string, but we reverse |
|
214 | + // L[16] and R[16] so that it becomes R[16]L[16] |
|
215 | + $data = $r[16].$l[16]; |
|
216 | + |
|
217 | + // now do the final permutation |
|
218 | + $data = $this->fp($data); |
|
219 | + $data = parent::bin2Str($data); |
|
220 | + |
|
221 | + return true; |
|
222 | + } |
|
223 | + |
|
224 | + |
|
225 | + /** |
|
226 | + * The Key permutation, based on tables $_pc1 and $_pc2 |
|
227 | + * Create 16 subkeys, each of which is 48-bits long. |
|
228 | + * |
|
229 | + * @return void |
|
230 | + */ |
|
231 | + private function keyPermutation() |
|
232 | + { |
|
233 | + $this->sub_keys = array(); |
|
234 | + $pc1m = array(); |
|
235 | + $pcr = array(); |
|
236 | + $c = array(); |
|
237 | + $d = array(); |
|
238 | + |
|
239 | + // convert the key to binary |
|
240 | + $binkey = parent::str2Bin($this->key()); |
|
241 | + |
|
242 | + // reduce the key down to 56bits based on table $_pc1 |
|
243 | + for ($i = 0; $i < 56; ++$i) |
|
244 | 244 | $pc1m[$i] = $binkey[self::$_pc1[$i] - 1]; |
245 | 245 | |
246 | - // split $pc1m in half (C0 and D0) |
|
247 | - $c[0] = array_slice($pc1m, 0, 28); |
|
248 | - $d[0] = array_slice($pc1m, 28, 28); |
|
249 | - |
|
250 | - // now that $c[0] and $d[0] are defined, create 16 blocks for Cn and Dn |
|
251 | - // where 1 <= n <= 16 |
|
252 | - for ($i = 1; $i <= 16; ++$i) |
|
253 | - { |
|
254 | - // now set the next Cn and Dn as the previous Cn and Dn |
|
255 | - $c[$i] = $c[$i - 1]; |
|
256 | - $d[$i] = $d[$i - 1]; |
|
257 | - |
|
258 | - for ($j = 0; $j < self::$_key_sched[$i - 1]; ++$j) |
|
259 | - { |
|
260 | - // do a left shift, move each bit one place to the left, |
|
261 | - // except for the first bit, which is cycled to the end |
|
262 | - // of the block. |
|
263 | - $c[$i][] = array_shift($c[$i]); |
|
264 | - $d[$i][] = array_shift($d[$i]); |
|
265 | - } |
|
266 | - |
|
267 | - // We now form the sub_keys (Kn), for 1<=n<=16, by applying the |
|
268 | - // following permutation table to each of the concatenated |
|
269 | - // pairs CnDn. Each pair has 56 bits, but PC-2 only uses 48 |
|
270 | - // of these. |
|
271 | - $CnDn = array_merge($c[$i], $d[$i]); |
|
272 | - $this->sub_keys[$i - 1] = ""; |
|
273 | - for ($j = 0; $j < 48; ++$j) |
|
274 | - $this->sub_keys[$i - 1] .= $CnDn[self::$_pc2[$j] - 1]; |
|
275 | - } |
|
276 | - |
|
277 | - // the sub_keys are created, we are done with the key permutation |
|
278 | - } |
|
279 | - |
|
280 | - |
|
281 | - /** |
|
282 | - * Initial Permutation (IP) |
|
283 | - * Now we encode each 64-bit block of data. There is an initial permutation IP of |
|
284 | - * the 64 bits of the message data M. This rearranges the bits according to the |
|
285 | - * following table, where the entries in the table show the new arrangement of the |
|
286 | - * bits from their initial order. The 58th bit of M becomes the first bit of IP. |
|
287 | - * The 50th bit of M becomes the second bit of IP. The 7th bit of M is the last |
|
288 | - * bit of IP. |
|
289 | - * |
|
290 | - * According to the book Applied Cryptography (Bruce Schneier, 2nd edition, pg. 271): |
|
291 | - * The initial permution was used to make it easier to load plain text and cipher text |
|
292 | - * data into a DES chip in byte-sized pieces when doing DES in hardware. The IP and FP |
|
293 | - * are not necessary in software implementations and do not affect the security. However, |
|
294 | - * the IP and FP are part of the DES standard and not implementing it would deviate from |
|
295 | - * the standard, so we will do it here in phpCrypt. |
|
296 | - * |
|
297 | - * @param string $text |
|
298 | - * @return string the Initial Permutation (IP) |
|
299 | - */ |
|
300 | - private function ip($text) |
|
301 | - { |
|
302 | - $text = parent::str2Bin($text); |
|
303 | - $ip = ""; |
|
304 | - |
|
305 | - // loop through the 64 bit block, ordering it occording to $_ip |
|
306 | - for ($i = 0; $i < 64; ++$i) |
|
307 | - $ip .= $text[self::$_ip[$i] - 1]; |
|
308 | - |
|
309 | - return $ip; |
|
310 | - } |
|
311 | - |
|
312 | - |
|
313 | - /** |
|
314 | - * Function F - To calculate f, we first expand each block Rn-1 from 32 bits to 48 bits. |
|
315 | - * This is done by using a selection table that repeats some of the bits in Rn-1. We'll |
|
316 | - * call the use of this selection table the function E. Thus E(Rn-1) has a 32 bit input |
|
317 | - * block, and a 48 bit output block. |
|
318 | - * |
|
319 | - * @param string $r 32 bit binary, each bit in an array element |
|
320 | - * @param string $k 48 bit binary string |
|
321 | - * @return string 48 bit binary string |
|
322 | - */ |
|
323 | - private function f($r, $k) |
|
324 | - { |
|
325 | - $bin = parent::xorBin($k, $this->E($r)); |
|
326 | - |
|
327 | - // create a 32-bit string from $bits by passing it through the S-Boxes |
|
328 | - $bin = $this->s($bin); |
|
329 | - |
|
330 | - // now send permute $bin as defined by table self::$_p |
|
331 | - $bin = $this->p($bin); |
|
332 | - |
|
333 | - return $bin; |
|
334 | - } |
|
335 | - |
|
336 | - |
|
337 | - /** |
|
338 | - * Function E - Let E be such that the 48 bits of its output, written as 8 blocks of |
|
339 | - * 6 bits each, are obtained by selecting the bits in its inputs in order according |
|
340 | - * to the self::$_e[] table. |
|
341 | - * This is only used in the F() function |
|
342 | - * |
|
343 | - * @param array $r 32 bit binary, each bit in an array element |
|
344 | - * @return string 48 bit binary string |
|
345 | - */ |
|
346 | - private function e($r) |
|
347 | - { |
|
348 | - $e = ""; |
|
349 | - for ($i = 0; $i < 48; ++$i) |
|
350 | - $e .= $r[self::$_e[$i] - 1]; |
|
351 | - |
|
352 | - return $e; |
|
353 | - } |
|
354 | - |
|
355 | - |
|
356 | - /** |
|
357 | - * S-Box |
|
358 | - * Take a 48-bit string from F() and run it through the S-Boxes, this requires |
|
359 | - * us to break up the 48-bit string into 8 groups of 6 bits before sending it |
|
360 | - * through the S-Boxes |
|
361 | - * |
|
362 | - * @param string $bits The 48-bit string from F() to be processed |
|
363 | - * @return string A 32-bit string from created from the 48-bit string after passing through S-Boxes |
|
364 | - */ |
|
365 | - private function s($bits) |
|
366 | - { |
|
367 | - $s = ""; |
|
368 | - |
|
369 | - for ($i = 0; $i <= 42; $i += 6) |
|
370 | - { |
|
371 | - $sbits = substr($bits, $i, 6); |
|
372 | - |
|
373 | - // we need to determine the S-Box column number and row number |
|
374 | - // from the 6 bit string passed in, this is done using the following method: |
|
375 | - // The First & Last bits represent a number between 0-3, used to determine which row |
|
376 | - // The middle 4 bits represent a number between 0-15, used to determine the column |
|
377 | - $row = bindec("{$sbits[0]}{$sbits[5]}"); |
|
378 | - $col = bindec("{$sbits[1]}{$sbits[2]}{$sbits[3]}{$sbits[4]}"); |
|
379 | - |
|
380 | - // determine the position in the S-BOX, S-Box table is in self::$_s[] |
|
381 | - $pos = ($row * 16) + $col; |
|
382 | - |
|
383 | - // get the integer from the S-Box and convert it to binary |
|
384 | - $bin = decbin(self::$_s[($i / 6)][$pos]); |
|
385 | - $s .= str_pad($bin, 4, "0", STR_PAD_LEFT); |
|
386 | - } |
|
387 | - |
|
388 | - return $s; |
|
389 | - } |
|
390 | - |
|
391 | - |
|
392 | - /** |
|
393 | - * Permutation P |
|
394 | - * The permutation P is defined in self::$_p. P() returns a 32-bit output |
|
395 | - * from a 32-bit input from a binary string from the S-BOX by permuting |
|
396 | - * the bits of the input block. |
|
397 | - * This is only used inside of F() function |
|
398 | - * |
|
399 | - * @param string $s A 32-bit string originating from being passed through S-Box |
|
400 | - * @return string A 32-bit string, which is $s permuted through table self::$_p |
|
401 | - */ |
|
402 | - private function p($s) |
|
403 | - { |
|
404 | - $p = ""; |
|
405 | - for ($i = 0; $i < 32; ++$i) |
|
406 | - $p .= $s[self::$_p[$i] - 1]; |
|
407 | - |
|
408 | - return $p; |
|
409 | - } |
|
410 | - |
|
411 | - |
|
412 | - /** |
|
413 | - * Final Permutation (FP) |
|
414 | - * Read the comment about IP and FP being unecessary in software implmented DES (though |
|
415 | - * we will do it to follow the DES standard). |
|
416 | - * |
|
417 | - * @param string $bin A 64-bit binary string |
|
418 | - * @return string A 64-bit binary string that has been run through self::$_fp[] table |
|
419 | - */ |
|
420 | - private function fp($bin) |
|
421 | - { |
|
422 | - $fp = ""; |
|
423 | - for ($i = 0; $i < 64; ++$i) |
|
424 | - $fp .= $bin[self::$_fp[$i] - 1]; |
|
425 | - |
|
426 | - return $fp; |
|
427 | - } |
|
428 | - |
|
429 | - |
|
430 | - /** |
|
431 | - * Initialize all the tables, this function is called inside the constructor |
|
432 | - * |
|
433 | - * @return void |
|
434 | - */ |
|
435 | - private function initTables() |
|
436 | - { |
|
437 | - // permuted choice 1 (PC1) |
|
438 | - // these values are chars and should be run through chr() when used |
|
439 | - self::$_pc1 = array( |
|
440 | - 57, 49, 41, 33, 25, 17, 9, |
|
441 | - 1, 58, 50, 42, 34, 26, 18, |
|
442 | - 10, 2, 59, 51, 43, 35, 27, |
|
443 | - 19, 11, 3, 60, 52, 44, 36, |
|
444 | - 63, 55, 47, 39, 31, 23, 15, |
|
445 | - 7, 62, 54, 46, 38, 30, 22, |
|
446 | - 14, 6, 61, 53, 45, 37, 29, |
|
447 | - 21, 13, 5, 28, 20, 12, 4 |
|
448 | - ); |
|
449 | - |
|
450 | - // permuted choice 2 (PC2) |
|
451 | - // these values are chars and should be run through chr() when used |
|
452 | - self::$_pc2 = array( |
|
453 | - 14, 17, 11, 24, 1, 5, |
|
454 | - 3, 28, 15, 6, 21, 10, |
|
455 | - 23, 19, 12, 4, 26, 8, |
|
456 | - 16, 7, 27, 20, 13, 2, |
|
457 | - 41, 52, 31, 37, 47, 55, |
|
458 | - 30, 40, 51, 45, 33, 48, |
|
459 | - 44, 49, 39, 56, 34, 53, |
|
460 | - 46, 42, 50, 36, 29, 32 |
|
461 | - ); |
|
462 | - |
|
463 | - // initial permutation (IP) |
|
464 | - self::$_ip = array( |
|
465 | - 58, 50, 42, 34, 26, 18, 10, 2, |
|
466 | - 60, 52, 44, 36, 28, 20, 12, 4, |
|
467 | - 62, 54, 46, 38, 30, 22, 14, 6, |
|
468 | - 64, 56, 48, 40, 32, 24, 16, 8, |
|
469 | - 57, 49, 41, 33, 25, 17, 9, 1, |
|
470 | - 59, 51, 43, 35, 27, 19, 11, 3, |
|
471 | - 61, 53, 45, 37, 29, 21, 13, 5, |
|
472 | - 63, 55, 47, 39, 31, 23, 15, 7 |
|
473 | - ); |
|
474 | - |
|
475 | - // expansion (E) |
|
476 | - self::$_e = array( |
|
477 | - 32, 1, 2, 3, 4, 5, |
|
478 | - 4, 5, 6, 7, 8, 9, |
|
479 | - 8, 9, 10, 11, 12, 13, |
|
480 | - 12, 13, 14, 15, 16, 17, |
|
481 | - 16, 17, 18, 19, 20, 21, |
|
482 | - 20, 21, 22, 23, 24, 25, |
|
483 | - 24, 25, 26, 27, 28, 29, |
|
484 | - 28, 29, 30, 31, 32, 1 |
|
485 | - ); |
|
486 | - |
|
487 | - // substition box (S) |
|
488 | - self::$_s = array( |
|
489 | - /* S1 */ |
|
490 | - array( |
|
491 | - 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7, |
|
492 | - 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8, |
|
493 | - 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0, |
|
494 | - 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13 |
|
495 | - ), |
|
496 | - |
|
497 | - /* S2 */ |
|
498 | - array( |
|
499 | - 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10, |
|
500 | - 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5, |
|
501 | - 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15, |
|
502 | - 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9 |
|
503 | - ), |
|
504 | - |
|
505 | - /* S3 */ |
|
506 | - array( |
|
507 | - 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8, |
|
508 | - 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1, |
|
509 | - 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7, |
|
510 | - 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12 |
|
511 | - ), |
|
512 | - |
|
513 | - /* S4 */ |
|
514 | - array( |
|
515 | - 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15, |
|
516 | - 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9, |
|
517 | - 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4, |
|
518 | - 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14 |
|
519 | - ), |
|
520 | - |
|
521 | - /* S5 */ |
|
522 | - array( |
|
523 | - 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9, |
|
524 | - 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6, |
|
525 | - 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14, |
|
526 | - 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3 |
|
527 | - ), |
|
528 | - |
|
529 | - /* S6 */ |
|
530 | - array( |
|
531 | - 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11, |
|
532 | - 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8, |
|
533 | - 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6, |
|
534 | - 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13 |
|
535 | - ), |
|
536 | - |
|
537 | - /* S7 */ |
|
538 | - array( |
|
539 | - 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1, |
|
540 | - 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6, |
|
541 | - 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2, |
|
542 | - 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12 |
|
543 | - ), |
|
544 | - |
|
545 | - /* S8 */ |
|
546 | - array( |
|
547 | - 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7, |
|
548 | - 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2, |
|
549 | - 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8, |
|
550 | - 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11 |
|
551 | - ) |
|
552 | - ); |
|
553 | - |
|
554 | - // permutation (P) |
|
555 | - self::$_p = array( |
|
556 | - 16, 7, 20, 21, |
|
557 | - 29, 12, 28, 17, |
|
558 | - 1, 15, 23, 26, |
|
559 | - 5, 18, 31, 10, |
|
560 | - 2, 8, 24, 14, |
|
561 | - 32, 27, 3, 9, |
|
562 | - 19, 13, 30, 6, |
|
563 | - 22, 11, 4, 25 |
|
564 | - ); |
|
565 | - |
|
566 | - // final permutation (FP) |
|
567 | - self::$_fp = array( |
|
568 | - 40, 8, 48, 16, 56, 24, 64, 32, |
|
569 | - 39, 7, 47, 15, 55, 23, 63, 31, |
|
570 | - 38, 6, 46, 14, 54, 22, 62, 30, |
|
571 | - 37, 5, 45, 13, 53, 21, 61, 29, |
|
572 | - 36, 4, 44, 12, 52, 20, 60, 28, |
|
573 | - 35, 3, 43, 11, 51, 19, 59, 27, |
|
574 | - 34, 2, 42, 10, 50, 18, 58, 26, |
|
575 | - 33, 1, 41, 9, 49, 17, 57, 25 |
|
576 | - ); |
|
577 | - |
|
578 | - // key schedule used in KeyPermutation() |
|
579 | - self::$_key_sched = array(1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1); |
|
580 | - } |
|
581 | - |
|
582 | - |
|
583 | - /** |
|
584 | - * Indicates this is a block cipher |
|
585 | - * |
|
586 | - * @return integer Returns Cipher::BLOCK |
|
587 | - */ |
|
588 | - public function type() |
|
589 | - { |
|
590 | - return parent::BLOCK; |
|
591 | - } |
|
246 | + // split $pc1m in half (C0 and D0) |
|
247 | + $c[0] = array_slice($pc1m, 0, 28); |
|
248 | + $d[0] = array_slice($pc1m, 28, 28); |
|
249 | + |
|
250 | + // now that $c[0] and $d[0] are defined, create 16 blocks for Cn and Dn |
|
251 | + // where 1 <= n <= 16 |
|
252 | + for ($i = 1; $i <= 16; ++$i) |
|
253 | + { |
|
254 | + // now set the next Cn and Dn as the previous Cn and Dn |
|
255 | + $c[$i] = $c[$i - 1]; |
|
256 | + $d[$i] = $d[$i - 1]; |
|
257 | + |
|
258 | + for ($j = 0; $j < self::$_key_sched[$i - 1]; ++$j) |
|
259 | + { |
|
260 | + // do a left shift, move each bit one place to the left, |
|
261 | + // except for the first bit, which is cycled to the end |
|
262 | + // of the block. |
|
263 | + $c[$i][] = array_shift($c[$i]); |
|
264 | + $d[$i][] = array_shift($d[$i]); |
|
265 | + } |
|
266 | + |
|
267 | + // We now form the sub_keys (Kn), for 1<=n<=16, by applying the |
|
268 | + // following permutation table to each of the concatenated |
|
269 | + // pairs CnDn. Each pair has 56 bits, but PC-2 only uses 48 |
|
270 | + // of these. |
|
271 | + $CnDn = array_merge($c[$i], $d[$i]); |
|
272 | + $this->sub_keys[$i - 1] = ""; |
|
273 | + for ($j = 0; $j < 48; ++$j) |
|
274 | + $this->sub_keys[$i - 1] .= $CnDn[self::$_pc2[$j] - 1]; |
|
275 | + } |
|
276 | + |
|
277 | + // the sub_keys are created, we are done with the key permutation |
|
278 | + } |
|
279 | + |
|
280 | + |
|
281 | + /** |
|
282 | + * Initial Permutation (IP) |
|
283 | + * Now we encode each 64-bit block of data. There is an initial permutation IP of |
|
284 | + * the 64 bits of the message data M. This rearranges the bits according to the |
|
285 | + * following table, where the entries in the table show the new arrangement of the |
|
286 | + * bits from their initial order. The 58th bit of M becomes the first bit of IP. |
|
287 | + * The 50th bit of M becomes the second bit of IP. The 7th bit of M is the last |
|
288 | + * bit of IP. |
|
289 | + * |
|
290 | + * According to the book Applied Cryptography (Bruce Schneier, 2nd edition, pg. 271): |
|
291 | + * The initial permution was used to make it easier to load plain text and cipher text |
|
292 | + * data into a DES chip in byte-sized pieces when doing DES in hardware. The IP and FP |
|
293 | + * are not necessary in software implementations and do not affect the security. However, |
|
294 | + * the IP and FP are part of the DES standard and not implementing it would deviate from |
|
295 | + * the standard, so we will do it here in phpCrypt. |
|
296 | + * |
|
297 | + * @param string $text |
|
298 | + * @return string the Initial Permutation (IP) |
|
299 | + */ |
|
300 | + private function ip($text) |
|
301 | + { |
|
302 | + $text = parent::str2Bin($text); |
|
303 | + $ip = ""; |
|
304 | + |
|
305 | + // loop through the 64 bit block, ordering it occording to $_ip |
|
306 | + for ($i = 0; $i < 64; ++$i) |
|
307 | + $ip .= $text[self::$_ip[$i] - 1]; |
|
308 | + |
|
309 | + return $ip; |
|
310 | + } |
|
311 | + |
|
312 | + |
|
313 | + /** |
|
314 | + * Function F - To calculate f, we first expand each block Rn-1 from 32 bits to 48 bits. |
|
315 | + * This is done by using a selection table that repeats some of the bits in Rn-1. We'll |
|
316 | + * call the use of this selection table the function E. Thus E(Rn-1) has a 32 bit input |
|
317 | + * block, and a 48 bit output block. |
|
318 | + * |
|
319 | + * @param string $r 32 bit binary, each bit in an array element |
|
320 | + * @param string $k 48 bit binary string |
|
321 | + * @return string 48 bit binary string |
|
322 | + */ |
|
323 | + private function f($r, $k) |
|
324 | + { |
|
325 | + $bin = parent::xorBin($k, $this->E($r)); |
|
326 | + |
|
327 | + // create a 32-bit string from $bits by passing it through the S-Boxes |
|
328 | + $bin = $this->s($bin); |
|
329 | + |
|
330 | + // now send permute $bin as defined by table self::$_p |
|
331 | + $bin = $this->p($bin); |
|
332 | + |
|
333 | + return $bin; |
|
334 | + } |
|
335 | + |
|
336 | + |
|
337 | + /** |
|
338 | + * Function E - Let E be such that the 48 bits of its output, written as 8 blocks of |
|
339 | + * 6 bits each, are obtained by selecting the bits in its inputs in order according |
|
340 | + * to the self::$_e[] table. |
|
341 | + * This is only used in the F() function |
|
342 | + * |
|
343 | + * @param array $r 32 bit binary, each bit in an array element |
|
344 | + * @return string 48 bit binary string |
|
345 | + */ |
|
346 | + private function e($r) |
|
347 | + { |
|
348 | + $e = ""; |
|
349 | + for ($i = 0; $i < 48; ++$i) |
|
350 | + $e .= $r[self::$_e[$i] - 1]; |
|
351 | + |
|
352 | + return $e; |
|
353 | + } |
|
354 | + |
|
355 | + |
|
356 | + /** |
|
357 | + * S-Box |
|
358 | + * Take a 48-bit string from F() and run it through the S-Boxes, this requires |
|
359 | + * us to break up the 48-bit string into 8 groups of 6 bits before sending it |
|
360 | + * through the S-Boxes |
|
361 | + * |
|
362 | + * @param string $bits The 48-bit string from F() to be processed |
|
363 | + * @return string A 32-bit string from created from the 48-bit string after passing through S-Boxes |
|
364 | + */ |
|
365 | + private function s($bits) |
|
366 | + { |
|
367 | + $s = ""; |
|
368 | + |
|
369 | + for ($i = 0; $i <= 42; $i += 6) |
|
370 | + { |
|
371 | + $sbits = substr($bits, $i, 6); |
|
372 | + |
|
373 | + // we need to determine the S-Box column number and row number |
|
374 | + // from the 6 bit string passed in, this is done using the following method: |
|
375 | + // The First & Last bits represent a number between 0-3, used to determine which row |
|
376 | + // The middle 4 bits represent a number between 0-15, used to determine the column |
|
377 | + $row = bindec("{$sbits[0]}{$sbits[5]}"); |
|
378 | + $col = bindec("{$sbits[1]}{$sbits[2]}{$sbits[3]}{$sbits[4]}"); |
|
379 | + |
|
380 | + // determine the position in the S-BOX, S-Box table is in self::$_s[] |
|
381 | + $pos = ($row * 16) + $col; |
|
382 | + |
|
383 | + // get the integer from the S-Box and convert it to binary |
|
384 | + $bin = decbin(self::$_s[($i / 6)][$pos]); |
|
385 | + $s .= str_pad($bin, 4, "0", STR_PAD_LEFT); |
|
386 | + } |
|
387 | + |
|
388 | + return $s; |
|
389 | + } |
|
390 | + |
|
391 | + |
|
392 | + /** |
|
393 | + * Permutation P |
|
394 | + * The permutation P is defined in self::$_p. P() returns a 32-bit output |
|
395 | + * from a 32-bit input from a binary string from the S-BOX by permuting |
|
396 | + * the bits of the input block. |
|
397 | + * This is only used inside of F() function |
|
398 | + * |
|
399 | + * @param string $s A 32-bit string originating from being passed through S-Box |
|
400 | + * @return string A 32-bit string, which is $s permuted through table self::$_p |
|
401 | + */ |
|
402 | + private function p($s) |
|
403 | + { |
|
404 | + $p = ""; |
|
405 | + for ($i = 0; $i < 32; ++$i) |
|
406 | + $p .= $s[self::$_p[$i] - 1]; |
|
407 | + |
|
408 | + return $p; |
|
409 | + } |
|
410 | + |
|
411 | + |
|
412 | + /** |
|
413 | + * Final Permutation (FP) |
|
414 | + * Read the comment about IP and FP being unecessary in software implmented DES (though |
|
415 | + * we will do it to follow the DES standard). |
|
416 | + * |
|
417 | + * @param string $bin A 64-bit binary string |
|
418 | + * @return string A 64-bit binary string that has been run through self::$_fp[] table |
|
419 | + */ |
|
420 | + private function fp($bin) |
|
421 | + { |
|
422 | + $fp = ""; |
|
423 | + for ($i = 0; $i < 64; ++$i) |
|
424 | + $fp .= $bin[self::$_fp[$i] - 1]; |
|
425 | + |
|
426 | + return $fp; |
|
427 | + } |
|
428 | + |
|
429 | + |
|
430 | + /** |
|
431 | + * Initialize all the tables, this function is called inside the constructor |
|
432 | + * |
|
433 | + * @return void |
|
434 | + */ |
|
435 | + private function initTables() |
|
436 | + { |
|
437 | + // permuted choice 1 (PC1) |
|
438 | + // these values are chars and should be run through chr() when used |
|
439 | + self::$_pc1 = array( |
|
440 | + 57, 49, 41, 33, 25, 17, 9, |
|
441 | + 1, 58, 50, 42, 34, 26, 18, |
|
442 | + 10, 2, 59, 51, 43, 35, 27, |
|
443 | + 19, 11, 3, 60, 52, 44, 36, |
|
444 | + 63, 55, 47, 39, 31, 23, 15, |
|
445 | + 7, 62, 54, 46, 38, 30, 22, |
|
446 | + 14, 6, 61, 53, 45, 37, 29, |
|
447 | + 21, 13, 5, 28, 20, 12, 4 |
|
448 | + ); |
|
449 | + |
|
450 | + // permuted choice 2 (PC2) |
|
451 | + // these values are chars and should be run through chr() when used |
|
452 | + self::$_pc2 = array( |
|
453 | + 14, 17, 11, 24, 1, 5, |
|
454 | + 3, 28, 15, 6, 21, 10, |
|
455 | + 23, 19, 12, 4, 26, 8, |
|
456 | + 16, 7, 27, 20, 13, 2, |
|
457 | + 41, 52, 31, 37, 47, 55, |
|
458 | + 30, 40, 51, 45, 33, 48, |
|
459 | + 44, 49, 39, 56, 34, 53, |
|
460 | + 46, 42, 50, 36, 29, 32 |
|
461 | + ); |
|
462 | + |
|
463 | + // initial permutation (IP) |
|
464 | + self::$_ip = array( |
|
465 | + 58, 50, 42, 34, 26, 18, 10, 2, |
|
466 | + 60, 52, 44, 36, 28, 20, 12, 4, |
|
467 | + 62, 54, 46, 38, 30, 22, 14, 6, |
|
468 | + 64, 56, 48, 40, 32, 24, 16, 8, |
|
469 | + 57, 49, 41, 33, 25, 17, 9, 1, |
|
470 | + 59, 51, 43, 35, 27, 19, 11, 3, |
|
471 | + 61, 53, 45, 37, 29, 21, 13, 5, |
|
472 | + 63, 55, 47, 39, 31, 23, 15, 7 |
|
473 | + ); |
|
474 | + |
|
475 | + // expansion (E) |
|
476 | + self::$_e = array( |
|
477 | + 32, 1, 2, 3, 4, 5, |
|
478 | + 4, 5, 6, 7, 8, 9, |
|
479 | + 8, 9, 10, 11, 12, 13, |
|
480 | + 12, 13, 14, 15, 16, 17, |
|
481 | + 16, 17, 18, 19, 20, 21, |
|
482 | + 20, 21, 22, 23, 24, 25, |
|
483 | + 24, 25, 26, 27, 28, 29, |
|
484 | + 28, 29, 30, 31, 32, 1 |
|
485 | + ); |
|
486 | + |
|
487 | + // substition box (S) |
|
488 | + self::$_s = array( |
|
489 | + /* S1 */ |
|
490 | + array( |
|
491 | + 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7, |
|
492 | + 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8, |
|
493 | + 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0, |
|
494 | + 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13 |
|
495 | + ), |
|
496 | + |
|
497 | + /* S2 */ |
|
498 | + array( |
|
499 | + 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10, |
|
500 | + 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5, |
|
501 | + 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15, |
|
502 | + 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9 |
|
503 | + ), |
|
504 | + |
|
505 | + /* S3 */ |
|
506 | + array( |
|
507 | + 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8, |
|
508 | + 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1, |
|
509 | + 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7, |
|
510 | + 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12 |
|
511 | + ), |
|
512 | + |
|
513 | + /* S4 */ |
|
514 | + array( |
|
515 | + 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15, |
|
516 | + 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9, |
|
517 | + 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4, |
|
518 | + 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14 |
|
519 | + ), |
|
520 | + |
|
521 | + /* S5 */ |
|
522 | + array( |
|
523 | + 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9, |
|
524 | + 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6, |
|
525 | + 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14, |
|
526 | + 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3 |
|
527 | + ), |
|
528 | + |
|
529 | + /* S6 */ |
|
530 | + array( |
|
531 | + 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11, |
|
532 | + 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8, |
|
533 | + 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6, |
|
534 | + 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13 |
|
535 | + ), |
|
536 | + |
|
537 | + /* S7 */ |
|
538 | + array( |
|
539 | + 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1, |
|
540 | + 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6, |
|
541 | + 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2, |
|
542 | + 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12 |
|
543 | + ), |
|
544 | + |
|
545 | + /* S8 */ |
|
546 | + array( |
|
547 | + 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7, |
|
548 | + 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2, |
|
549 | + 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8, |
|
550 | + 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11 |
|
551 | + ) |
|
552 | + ); |
|
553 | + |
|
554 | + // permutation (P) |
|
555 | + self::$_p = array( |
|
556 | + 16, 7, 20, 21, |
|
557 | + 29, 12, 28, 17, |
|
558 | + 1, 15, 23, 26, |
|
559 | + 5, 18, 31, 10, |
|
560 | + 2, 8, 24, 14, |
|
561 | + 32, 27, 3, 9, |
|
562 | + 19, 13, 30, 6, |
|
563 | + 22, 11, 4, 25 |
|
564 | + ); |
|
565 | + |
|
566 | + // final permutation (FP) |
|
567 | + self::$_fp = array( |
|
568 | + 40, 8, 48, 16, 56, 24, 64, 32, |
|
569 | + 39, 7, 47, 15, 55, 23, 63, 31, |
|
570 | + 38, 6, 46, 14, 54, 22, 62, 30, |
|
571 | + 37, 5, 45, 13, 53, 21, 61, 29, |
|
572 | + 36, 4, 44, 12, 52, 20, 60, 28, |
|
573 | + 35, 3, 43, 11, 51, 19, 59, 27, |
|
574 | + 34, 2, 42, 10, 50, 18, 58, 26, |
|
575 | + 33, 1, 41, 9, 49, 17, 57, 25 |
|
576 | + ); |
|
577 | + |
|
578 | + // key schedule used in KeyPermutation() |
|
579 | + self::$_key_sched = array(1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1); |
|
580 | + } |
|
581 | + |
|
582 | + |
|
583 | + /** |
|
584 | + * Indicates this is a block cipher |
|
585 | + * |
|
586 | + * @return integer Returns Cipher::BLOCK |
|
587 | + */ |
|
588 | + public function type() |
|
589 | + { |
|
590 | + return parent::BLOCK; |
|
591 | + } |
|
592 | 592 | } |
593 | 593 | ?> |
@@ -232,10 +232,10 @@ discard block |
||
232 | 232 | * |
233 | 233 | * Unicode multi-byte character safe |
234 | 234 | * |
235 | - * @param plaintext source text to be encrypted |
|
235 | + * @param plaintext string text to be encrypted |
|
236 | 236 | * @param password the password to use to generate a key |
237 | - * @param nBits number of bits to be used in the key (128, 192, or 256) |
|
238 | - * @return encrypted text |
|
237 | + * @param nBits integer of bits to be used in the key (128, 192, or 256) |
|
238 | + * @return string text |
|
239 | 239 | */ |
240 | 240 | public static function encrypt($plaintext, $password, $nBits) |
241 | 241 | { |
@@ -315,8 +315,8 @@ discard block |
||
315 | 315 | * |
316 | 316 | * @param ciphertext source text to be decrypted |
317 | 317 | * @param password the password to use to generate a key |
318 | - * @param nBits number of bits to be used in the key (128, 192, or 256) |
|
319 | - * @return decrypted text |
|
318 | + * @param nBits integer of bits to be used in the key (128, 192, or 256) |
|
319 | + * @return string text |
|
320 | 320 | */ |
321 | 321 | public static function decrypt($ciphertext, $password, $nBits) |
322 | 322 | { |
@@ -755,11 +755,11 @@ discard block |
||
755 | 755 | } |
756 | 756 | |
757 | 757 | /** |
758 | - * Returns objectClass in an array |
|
759 | - * |
|
760 | - * @param string $distinguishedName The full DN of a contact |
|
761 | - * @return array |
|
762 | - */ |
|
758 | + * Returns objectClass in an array |
|
759 | + * |
|
760 | + * @param string $distinguishedName The full DN of a contact |
|
761 | + * @return array |
|
762 | + */ |
|
763 | 763 | public function getObjectClass($distinguishedName) { |
764 | 764 | if ($distinguishedName === NULL) { return false; } |
765 | 765 | if (!$this->getLdapBind()) { return false; } |
@@ -778,21 +778,21 @@ discard block |
||
778 | 778 | } |
779 | 779 | |
780 | 780 | /** |
781 | - * Find the Base DN of your domain controller |
|
782 | - * |
|
783 | - * @return string |
|
784 | - */ |
|
781 | + * Find the Base DN of your domain controller |
|
782 | + * |
|
783 | + * @return string |
|
784 | + */ |
|
785 | 785 | public function findBaseDn() { |
786 | 786 | $namingContext = $this->getRootDse(array('defaultnamingcontext')); |
787 | 787 | return $namingContext[0]['defaultnamingcontext'][0]; |
788 | 788 | } |
789 | 789 | |
790 | 790 | /** |
791 | - * Get the RootDSE properties from a domain controller |
|
792 | - * |
|
793 | - * @param string[] $attributes The attributes you wish to query e.g. defaultnamingcontext |
|
794 | - * @return array |
|
795 | - */ |
|
791 | + * Get the RootDSE properties from a domain controller |
|
792 | + * |
|
793 | + * @param string[] $attributes The attributes you wish to query e.g. defaultnamingcontext |
|
794 | + * @return array |
|
795 | + */ |
|
796 | 796 | public function getRootDse($attributes = array("*", "+")) { |
797 | 797 | if (!$this->ldapBind) { return (false); } |
798 | 798 |