1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
|
/*
* This file is part of the pdftk port to java
*
* Copyright (c) Marc Vinyals 2017-2018
*
* The program is a java port of PDFtk, the PDF Toolkit
* Copyright (c) 2003-2013 Steward and Lee, LLC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* The program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.gitlab.pdftk_java;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
/*
password character mapping by Acrobat (pre-1.7 PDF)
* characters in PDFDocEncoding use PDFDocEncoding
* some PDFDocEncoding characters are not accepted as input into Acrobat/Reader --
this is consistent with the next rule:
* unicode 0x200 and up: Acrobat/Reader prohibits input
* some chars below 0x200 are mapped to . (period)
* some chars below 0x200 are mapped to non-accented ASCII char
* many points are mapped to Windows CP-1250
These were tested on Windows 7 using Acrobat 7. From what I've read, Adobe's mapping
of characters that aren't in PDFDocEncoding is platform dependent. So this map should
only be used for mapping PDF passwords for decryption. When encrypting, it would be
best to enforce a limited set of characters for the purpose of platform independence.
*/
class passwords {
// unicode 0x100 - 0x1FF
static final int[] unicode_latin_extended_windows_map = {
// latin extended a
'A', // U+0100 Ā Latin Capital Letter A with macron
'a', // U+0101 ā Latin Small Letter A with macron
0xC3, // U+0102 Ă Latin Capital Letter A with breve
0xC4, // U+0103 ă Latin Small Letter A with breve
0xA5, // U+0104 Ą Latin Capital Letter A with ogonek
0xB9, // U+0105 ą Latin Small Letter A with ogonek
0xC6, // U+0106 Ć Latin Capital Letter C with acute
0xE6, // U+0107 ć Latin Small Letter C with acute
'.', // U+0108 Ĉ Latin Capital Letter C with circumflex
'.', // U+0109 ĉ Latin Small Letter C with circumflex
'.', // U+010A Ċ Latin Capital Letter C with dot above
'.', // U+010B ċ Latin Small Letter C with dot above
0xC8, // U+010C Č Latin Capital Letter C with caron
0xE8, // U+010D č Latin Small Letter C with caron
0xCF, // U+010E Ď Latin Capital Letter D with caron
0xEF, // U+010F ď Latin Small Letter D with caron
0xD0, // U+0110 Đ Latin Capital Letter D with stroke
0xF0, // U+0111 đ Latin Small Letter D with stroke
'E', // U+0112 Ē Latin Capital Letter E with macron
'e', // U+0113 ē Latin Small Letter E with macron
'.', // U+0114 Ĕ Latin Capital Letter E with breve
'.', // U+0115 ĕ Latin Small Letter E with breve
'E', // U+0116 Ė Latin Capital Letter E with dot above
'e', // U+0117 ė Latin Small Letter E with dot above
0xCA, // U+0118 Ę Latin Capital Letter E with ogonek
0xEA, // U+0119 ę Latin Small Letter E with ogonek
0xCC, // U+011A Ě Latin Capital Letter E with caron
0xEC, // U+011B ě Latin Small Letter E with caron
'.', // U+011C Ĝ Latin Capital Letter G with circumflex
'.', // U+011D ĝ Latin Small Letter G with circumflex
'G', // U+011E Ğ Latin Capital Letter G with breve
'g', // U+011F ğ Latin Small Letter G with breve
'.', // U+0120 Ġ Latin Capital Letter G with dot above
'.', // U+0121 ġ Latin Small Letter G with dot above
'G', // U+0122 Ģ Latin Capital Letter G with cedilla
'g', // U+0123 ģ Latin Small Letter G with cedilla
'.', // U+0124 Ĥ Latin Capital Letter H with circumflex
'.', // U+0125 ĥ Latin Small Letter H with circumflex
'.', // U+0126 Ħ Latin Capital Letter H with stroke
'.', // U+0127 ħ Latin Small Letter H with stroke
'.', // U+0128 Ĩ Latin Capital Letter I with tilde
'.', // U+0129 ĩ Latin Small Letter I with tilde
'I', // U+012A Ī Latin Capital Letter I with macron
'i', // U+012B ī Latin Small Letter I with macron
'.', // U+012C Ĭ Latin Capital Letter I with breve
'.', // U+012D ĭ Latin Small Letter I with breve
'I', // U+012E Į Latin Capital Letter I with ogonek
'i', // U+012F į Latin Small Letter I with ogonek
'I', // U+0130 İ Latin Capital Letter I with dot above
'i', // U+0131 ı Latin Small Letter dotless I
'.', // U+0132 IJ Latin Capital Ligature IJ
'.', // U+0133 ij Latin Small Ligature IJ
'.', // U+0134 Ĵ Latin Capital Letter J with circumflex
'.', // U+0135 ĵ Latin Small Letter J with circumflex
'K', // U+0136 Ķ Latin Capital Letter K with cedilla
'k', // U+0137 ķ Latin Small Letter K with cedilla
'.', // U+0138 ĸ Latin Small Letter Kra
0xC5, // U+0139 Ĺ Latin Capital Letter L with acute
0xE5, // U+013A ĺ Latin Small Letter L with acute
'L', // U+013B Ļ Latin Capital Letter L with cedilla
'l', // U+013C ļ Latin Small Letter L with cedilla
0xBC, // U+013D Ľ Latin Capital Letter L with caron
0xBE, // U+013E ľ Latin Small Letter L with caron
'.', // U+013F Ŀ Latin Capital Letter L with middle dot
'.', // U+0140 ŀ Latin Small Letter L with middle dot
0xA3, // U+0141 Ł Latin Capital Letter L with stroke
0xB3, // U+0142 ł Latin Small Letter L with stroke
0xD1, // U+0143 Ń Latin Capital Letter N with acute
0xF1, // U+0144 ń Latin Small Letter N with acute
'N', // U+0145 Ņ Latin Capital Letter N with cedilla
'n', // U+0146 ņ Latin Small Letter N with cedilla
0xD2, // U+0147 Ň Latin Capital Letter N with caron
0xF2, // U+0148 ň Latin Small Letter N with caron
'.', // U+0149 ʼn Latin Small Letter N preceded by apostrophe[1]
'.', // U+014A Ŋ Latin Capital Letter Eng
'.', // U+014B ŋ Latin Small Letter Eng
'O', // U+014C Ō Latin Capital Letter O with macron
'o', // U+014D ō Latin Small Letter O with macron
'.', // U+014E Ŏ Latin Capital Letter O with breve
'.', // U+014F ŏ Latin Small Letter O with breve
0xD5, // U+0150 Ő Latin Capital Letter O with double acute
0xF5, // U+0151 ő Latin Small Letter O with double acute
0226, // U+0152 Œ Latin Capital Ligature OE (mapped to PDFDocEncoding)
0234, // U+0153 œ Latin Small Ligature OE (mapped to PDFDocEncoding)
0xC0, // U+0154 Ŕ Latin Capital Letter R with acute
0xE0, // U+0155 ŕ Latin Small Letter R with acute
'R', // U+0156 Ŗ Latin Capital Letter R with cedilla
'r', // U+0157 ŗ Latin Small Letter R with cedilla
0xD8, // U+0158 Ř Latin Capital Letter R with caron
0xF8, // U+0159 ř Latin Small Letter R with caron
0x8C, // U+015A Ś Latin Capital Letter S with acute
0x9C, // U+015B ś Latin Small Letter S with acute
'.', // U+015C Ŝ Latin Capital Letter S with circumflex
'.', // U+015D ŝ Latin Small Letter S with circumflex
0xAA, // U+015E Ş Latin Capital Letter S with cedilla
0xBA, // U+015F ş Latin Small Letter S with cedilla
0x8A, // U+0160 Š Latin Capital Letter S with caron
0x9A, // U+0161 š Latin Small Letter S with caron
0xDE, // U+0162 Ţ Latin Capital Letter T with cedilla
0xFE, // U+0163 ţ Latin Small Letter T with cedilla
0x8D, // U+0164 Ť Latin Capital Letter T with caron
0x9D, // U+0165 ť Latin Small Letter T with caron
'T', // U+0166 Ŧ Latin Capital Letter T with stroke
't', // U+0167 ŧ Latin Small Letter T with stroke
'.', // U+0168 Ũ Latin Capital Letter U with tilde
'.', // U+0169 ũ Latin Small Letter U with tilde
'U', // U+016A Ū Latin Capital Letter U with macron
'u', // U+016B ū Latin Small Letter U with macron
'.', // U+016C Ŭ Latin Capital Letter U with breve
'.', // U+016D ŭ Latin Small Letter U with breve
0xD9, // U+016E Ů Latin Capital Letter U with ring above
0xF9, // U+016F ů Latin Small Letter U with ring above
0xDB, // U+0170 Ű Latin Capital Letter U with double acute
0xFB, // U+0171 ű Latin Small Letter U with double acute
'U', // U+0172 Ų Latin Capital Letter U with ogonek
'u', // U+0173 ų Latin Small Letter U with ogonek
'.', // U+0174 Ŵ Latin Capital Letter W with circumflex
'.', // U+0175 ŵ Latin Small Letter W with circumflex
'.', // U+0176 Ŷ Latin Capital Letter Y with circumflex
'.', // U+0177 ŷ Latin Small Letter Y with circumflex
0230, // U+0178 Ÿ Latin Capital Letter Y with diaeresis (mapped to PDFDocEncoding)
0x8F, // U+0179 Ź Latin Capital Letter Z with acute
0x9F, // U+017A ź Latin Small Letter Z with acute
0xAF, // U+017B Ż Latin Capital Letter Z with dot above
0xBF, // U+017C ż Latin Small Letter Z with dot above
0231, // U+017D Ž Latin Capital Letter Z with caron (mapped to PDFDocEncoding)
0236, // U+017E ž Latin Small Letter Z with caron (mapped to PDFDocEncoding)
'.', // U+017F ſ Latin Small Letter long S
// portion of latin extended b
'b', // U+0180 ƀ ƀ Latin Small Letter B with stroke
'.', // U+0181 Ɓ Ɓ Latin Capital Letter B with hook
'.', // U+0182 Ƃ Ƃ Latin Capital Letter B with top bar
'.', // U+0183 ƃ ƃ Latin Small Letter B with top bar
'.', // U+0184 Ƅ Ƅ Latin Capital Letter Tone Six
'.', // U+0185 ƅ ƅ Latin Small Letter Tone Six
'.', // U+0186 Ɔ Ɔ Latin Capital Letter Open O
'.', // U+0187 Ƈ Ƈ Latin Capital Letter C with hook
'.', // U+0188 ƈ ƈ Latin Small Letter C with hook
0xD0, // U+0189 Ɖ Ɖ Latin Capital Letter African D
'.', // U+018A Ɗ Ɗ Latin Capital Letter D with hook
'.', // U+018B Ƌ Ƌ Latin Capital Letter D with top bar
'.', // U+018C ƌ ƌ Latin Small Letter D with top bar
'.', // U+018D ƍ ƍ Latin Small Letter Turned Delta
'.', // U+018E Ǝ Ǝ Latin Capital Letter Reversed E
'.', // U+018F Ə Ə Latin Capital Letter Schwa
'.', // U+0190 Ɛ Ɛ Latin Capital Letter Open E
0x83, // U+0191 Ƒ Ƒ Latin Capital Letter F with hook (from win cp-1252)(maybe)
0x83, // U+0192 ƒ ƒ Latin Small Letter F with hook (from win cp-1252)(maybe)
'.', // U+0193 Ɠ Ɠ Latin Capital Letter G with hook
'.', // U+0194 Ɣ Ɣ Latin Capital Letter Gamma
'.', // U+0195 ƕ ƕ Latin Small Letter HV
'.', // U+0196 Ɩ Ɩ Latin Capital Letter Iota
'I', // U+0197 Ɨ Ɨ Latin Capital Letter I with stroke
'.', // U+0198 Ƙ Ƙ Latin Capital Letter K with hook
'.', // U+0199 ƙ ƙ Latin Small Letter K with hook
'l', // U+019A ƚ ƚ Latin Small Letter L with bar
'.', // U+019B ƛ ƛ Latin Small Letter Lambda with stroke
'.', // U+019C Ɯ Ɯ Latin Capital Letter Turned M
'.', // U+019D Ɲ Ɲ Latin Capital Letter N with left hook
'.', // U+019E ƞ ƞ Latin Small Letter N with long right leg
'O', // U+019F Ɵ Ɵ Latin Capital Letter O with middle tilde
'O', // U+01A0 Ơ Ơ Latin Capital Letter O with horn
'o', // U+01A1 ơ ơ Latin Small Letter O with horn
'.', // U+01A2 Ƣ Ƣ Latin Capital Letter OI (= Latin Capital Letter Gha)
'.', // U+01A3 ƣ ƣ Latin Small Letter OI (= Latin Small Letter Gha)
'.', // U+01A4 Ƥ Ƥ Latin Capital Letter P with hook
'.', // U+01A5 ƥ ƥ Latin Small Letter P with hook
'.', // U+01A6 Ʀ Ʀ Latin Letter YR
'.', // U+01A7 Ƨ Ƨ Latin Capital Letter Tone Two
'.', // U+01A8 ƨ ƨ Latin Small Letter Tone Two
'.', // U+01A9 Ʃ Ʃ Latin Capital Letter Esh
'.', // U+01AA ƪ ƪ Latin Letter Reversed Esh Loop
't', // U+01AB ƫ ƫ Latin Small Letter T with palatal hook
'.', // U+01AC Ƭ Ƭ Latin Capital Letter T with hook
'.', // U+01AD ƭ ƭ Latin Small Letter T with hook
'T', // U+01AE Ʈ Ʈ Latin Capital Letter T with retroflex hook
'U', // U+01AF Ư Ư Latin Capital Letter U with horn
'u', // U+01B0 ư ư Latin Small Letter U with horn
'.', // U+01B1 Ʊ Ʊ Latin Capital Letter Upsilon
'.', // U+01B2 Ʋ Ʋ Latin Capital Letter V with hook
'.', // U+01B3 Ƴ Ƴ Latin Capital Letter Y with hook
'.', // U+01B4 ƴ ƴ Latin Small Letter Y with hook
'.', // U+01B5 Ƶ Ƶ Latin Capital Letter Z with stroke
'.', // U+01B6 ƶ ƶ Latin Small Letter Z with stroke
'.', // U+01B7 Ʒ Ʒ Latin Capital Letter Ezh
'.', // U+01B8 Ƹ Ƹ Latin Capital Letter Ezh reversed
'.', // U+01B9 ƹ ƹ Latin Small Letter Ezh reversed
'.', // U+01BA ƺ ƺ Latin Small Letter Ezh with tail
'.', // U+01BB ƻ ƻ Latin Letter Two with stroke
'.', // U+01BC Ƽ Ƽ Latin Capital Letter Tone Five
'.', // U+01BD ƽ ƽ Latin Small Letter Tone Five
'.', // U+01BE ƾ ƾ Latin Letter Inverted Glottal Stop with stroke
'.', // U+01BF ƿ ƿ Latin Letter Wynn
'|', // U+01C0 ǀ ǀ Latin Letter Dental Click
// I couldn't figure this one out
'.', // U+01C1 ǁ ǁ Latin Letter Lateral Click
'.', // U+01C2 ǂ ǂ Latin Letter Alveolar Click
'!', // U+01C3 ǃ ǃ Latin Letter Retroflex Click
'.', // U+01C4 DŽ DŽ Latin Capital Letter DZ with caron
'.', // U+01C5 Dž Dž Latin Capital Letter D with Small Letter Z with caron
'.', // U+01C6 dž dž Latin Small Letter DZ with caron
'.', // U+01C7 LJ LJ Latin Capital Letter LJ
'.', // U+01C8 Lj Lj Latin Capital Letter L with Small Letter J
'.', // U+01C9 lj lj Latin Small Letter LJ
'.', // U+01CA NJ NJ Latin Capital Letter NJ
'.', // U+01CB Nj Nj Latin Capital Letter N with Small Letter J
'.', // U+01CC nj nj Latin Small Letter NJ
'.', // U+01CD Ǎ Ǎ Latin Capital Letter A with caron
'.', // U+01CE ǎ ǎ Latin Small Letter A with caron
'.', // U+01CF Ǐ Ǐ Latin Capital Letter I with caron
'.', // U+01D0 ǐ ǐ Latin Small Letter I with caron
'.', // U+01D1 Ǒ Ǒ Latin Capital Letter O with caron
'.', // U+01D2 ǒ ǒ Latin Small Letter O with caron
'.', // U+01D3 Ǔ Ǔ Latin Capital Letter U with caron
'.', // U+01D4 ǔ ǔ Latin Small Letter U with caron
'.', // U+01D5 Ǖ Ǖ Latin Capital Letter U with diaeresis and macron
'.', // U+01D6 ǖ ǖ Latin Small Letter U with diaeresis and macron
'.', // U+01D7 Ǘ Ǘ Latin Capital Letter U with diaeresis and acute
'.', // U+01D8 ǘ ǘ Latin Small Letter U with diaeresis and acute
'.', // U+01D9 Ǚ Ǚ Latin Capital Letter U with diaeresis and caron
'.', // U+01DA ǚ ǚ Latin Small Letter U with diaeresis and caron
'.', // U+01DB Ǜ Ǜ Latin Capital Letter U with diaeresis and grave
'.', // U+01DC ǜ ǜ Latin Small Letter U with diaeresis and grave
'.', // U+01DD ǝ ǝ Latin Small Letter Turned E
'A', // U+01DE Ǟ Ǟ Latin Capital Letter A with diaeresis and macron
'a', // U+01DF ǟ ǟ Latin Small Letter A with diaeresis and macron
'.', // U+01E0 Ǡ Ǡ Latin Capital Letter A with dot above and macron
'.', // U+01E1 ǡ ǡ Latin Small Letter A with dot above and macron
'.', // U+01E2 Ǣ Ǣ Latin Capital Letter Æ with macron
'.', // U+01E3 ǣ ǣ Latin Small Letter Æ with macron
'G', // U+01E4 Ǥ Ǥ Latin Capital Letter G with stroke
'g', // U+01E5 ǥ ǥ Latin Small Letter G with stroke
'.', // U+01E6 Ǧ Ǧ Latin Capital Letter G with caron
'.', // U+01E7 ǧ ǧ Latin Small Letter G with caron
'.', // U+01E8 Ǩ Ǩ Latin Capital Letter K with caron
'.', // U+01E9 ǩ ǩ Latin Small Letter K with caron
'.', // U+01EA Ǫ Ǫ Latin Capital Letter O with ogonek
'.', // U+01EB ǫ ǫ Latin Small Letter O with ogonek
'O', // U+01EC Ǭ Ǭ Latin Capital Letter O with ogonek and macron
'o', // U+01ED ǭ ǭ Latin Small Letter O with ogonek and macron
'.', // U+01EE Ǯ Ǯ Latin Capital Letter Ezh with caron
'.', // U+01EF ǯ ǯ Latin Small Letter Ezh with caron
'.', // U+01F0 ǰ ǰ Latin Small Letter J with caron
'.', // U+01F1 DZ DZ Latin Capital Letter DZ
'.', // U+01F2 Dz Dz Latin Capital Letter D with Small Letter Z
'.', // U+01F3 dz dz Latin Small Letter DZ
'.', // U+01F4 Ǵ Ǵ Latin Capital Letter G with acute
'.', // U+01F5 ǵ ǵ Latin Small Letter G with acute
'.', // U+01F6 Ƕ Ƕ Latin Capital Letter Hwair
'.', // U+01F7 Ƿ Ƿ Latin Capital Letter Wynn
'.', // U+01F8 Ǹ Ǹ Latin Capital Letter N with grave
'.', // U+01F9 ǹ ǹ Latin Small Letter N with grave
'.', // U+01FA Ǻ Ǻ Latin Capital Letter A with ring above and acute
'.', // U+01FB ǻ ǻ Latin Small Letter A with ring above and acute
'.', // U+01FC Ǽ Ǽ Latin Capital Letter Æ with acute
'.', // U+01FD ǽ ǽ Latin Small Letter Æ with acute
'.', // U+01FE Ǿ Ǿ Latin Capital Letter O with stroke and acute
'.' // U+01FF ǿ ǿ Latin Small Letter O with stroke and acute
};
/*
Older PDF security passwords use PDFDocEncoding, which is a modified iso-8859-1.
Newer PDF security (version 5) passwords use unicode.
This function is just for older PDF security passwords, since pdftk doesn't yet
handle version 5 security.
In practice, Acrobat/Reader accepts password characters outside of PDFDocEncoding.
Returns -1 if input ss uses invalid characters
otherwise returns the number of bytes copied to bb.
If bb is null, then returns the number of bytes needed to copy ss.
If we are encrypting, allow only the most conservative set of input characters
in order to maximize platform independence.
If we are decrypting, allow as many input characters as possible.
TRANSLATOR NOTES:
Returns a byte array; null if input uses invalid characters.
*/
static byte[] utf8_password_to_pdfdoc(String ss_, boolean encrypt_b) {
byte[] ss = ss_.getBytes(StandardCharsets.UTF_8);
int ss_size = ss.length;
ArrayList<Byte> bb = new ArrayList<Byte>(ss_size);
for (int ii = 0; ii < ss_size; ) {
// decode UTF-8
int data = 0;
//
// our greatest valid point is 0x2122, so testing out to three bytes is enough
if ((ss[ii] & 0x80) == 0) { // single byte
data = ss[ii];
ii++;
} else if ((ss[ii] & 0xE0) == 0xC0
&& ii + 1 < ss_size
&& (ss[ii + 1] & 0xC0) == 0x80) { // two-byte
data = (ss[ii] & 0x1f);
data = data << 6;
data = data + (ss[ii + 1] & 0x3f);
ii += 2;
} else if ((ss[ii] & 0xF0) == 0xE0
&& ii + 1 < ss_size
&& (ss[ii + 1] & 0xC0) == 0x80
&& ii + 2 < ss_size
&& (ss[ii + 2] & 0xC0) == 0x80) { // three-byte
data = (ss[ii] & 0x0f);
data = data << 6;
data = data + (ss[ii + 1] & 0x3f);
data = data << 6;
data = data + (ss[ii + 2] & 0x3f);
ii += 3;
} else { // something else: out of range
return null;
}
if (0x20 <= data && data < 0x7f || 0xa0 <= data && data <= 0xff) {
// data point good
} else { // convert some unicode points to PDFDocEncoding
switch (data) {
case 0x0152: // OE ligature
data = 0226;
break;
case 0x0153: // oe ligature
data = 0234;
break;
case 0x0160: // Scaron
data = 0227;
break;
case 0x017E: // zcaron
data = 0236;
break;
case 0x0178: // Ydieresis
data = 0230;
break;
case 0x017D: // Zcaron
data = 0231;
break;
case 0x0192: // florin
data = 0206;
break;
case 0x0161: // scaron
data = 0235;
break;
default:
if (encrypt_b) { // data point out of bounds
return null;
} else {
// password is being used to decrypt, so test for more mappings to PDFDocEncoding;
if (0x100 <= data && data <= 0x1FF) {
// use our latin extended windows map; these points are accepted by Acrobat/Reader
// but are mapped to single-byte alternatives in a platform-dependent way
data = unicode_latin_extended_windows_map[data - 0x100];
} else {
// in practice, Acorbat/Reader won't allow these characters to be input for V4
// security and earlier;
switch (data) {
case 0x20AC: // Euro
data = 0240;
break;
case 0x2022: // bullet
data = 0200;
break;
case 0x2020: // dagger
data = 0201;
break;
case 0x2021: // daggerdbl
data = 0202;
break;
case 0x2026: // ellipsis
data = 0203;
break;
case 0x02C6: // circumflex
data = 0032;
break;
case 0x2014: // emdash
data = 0204;
break;
case 0x2013: // endash
data = 0205;
break;
case 0x2039: // guilsinglleft
data = 0210;
break;
case 0x203A: // guilsinglright
data = 0211;
break;
case 0x2030: // perthousand
data = 0213;
break;
case 0x201E: // quotedblbase
data = 0214;
break;
case 0x201C: // quotedblleft
data = 0215;
break;
case 0x201D: // quotedblright
data = 0216;
break;
case 0x2018: // quoteleft
data = 0217;
break;
case 0x2019: // quoteright
data = 0220;
break;
case 0x201A: // quotesinglebase
data = 0221;
break;
case 0x02DC: // tilde
data = 0037;
break;
case 0x2122: // trademark
data = 0222;
break;
default:
return null; // data point is still out of bounds
}
}
}
}
}
bb.add((byte) data);
}
byte[] out = new byte[bb.size()];
for (int i = 0; i < out.length; ++i) out[i] = bb.get(i);
return out;
}
}
|