1/* 2 * pgn4web javascript chessboard 3 * copyright (C) 2009-2013 Paolo Casaschi 4 * see README file and http://pgn4web.casaschi.net 5 * for credits, license and more details 6 * 7 * Huffman encoding/decoding derived from code at http://rumkin.com/tools/compression/compress_huff.php 8 */ 9 10// version 1 of PGN encoding: 11// encodedPGN = nnn$xxx0 12// nnn = number representing bytes length of the decoded message 13// $ = dollar char (delimiter for length info) 14// xxx = encoded text (using LetterCodes below) 15// 0 = zero char (version marker) 16 17"use strict"; 18 19var encodingCharSet_dec; 20var encodingCharSet_enc; 21var encodingCharSet = encodingCharSet_dec = "$0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_"; 22var encodingVersion_dec; 23var encodingVersion_enc; 24var encodingVersion = encodingVersion_dec = 1; 25var errorString; 26 27if (((encodingCharSet_enc != undefined) && (encodingCharSet_enc != encodingCharSet_dec)) || 28 ((encodingVersion_enc != undefined) && (encodingVersion_enc != encodingVersion_dec))) { 29 errorString = "error: PGN encoding/decoding mismatch"; 30 if (typeof myAlert == "function") { myAlert(errorString); } 31 else { alert(errorString); } 32} 33 34function DecodePGN(bytes) { 35 36 if (bytes.charAt(bytes.length - 1) != encodingCharSet.charAt(encodingVersion)) { 37 errorString = "error: PGN encoding version mismatch (e:" + 38 bytes.charAt(bytes.length - 1) + " d:" + encodingCharSet.charAt(encodingVersion) + ")"; 39 if (typeof myAlert == "function") { myAlert(errorString); } 40 else { alert(errorString); } 41 } 42 43 var originalLength = parseInt(bytes.match(/^[0-9]*/), 10); 44 bytes = bytes.replace(/^[0-9]*\$/,""); 45 46 var l = new Array(); 47 l[0] = -146; 48 l[1] = -111; 49 l[2] = -66; 50 l[3] = -55; 51 l[4] = -6; 52 l[5] = 46; 53 l[6] = -8; 54 l[7] = 66; 55 l[8] = -10; 56 l[9] = 105; 57 l[10] = -12; 58 l[11] = 107; 59 l[12] = -14; 60 l[13] = 44; 61 l[14] = -16; 62 l[15] = 85; 63 l[16] = -18; 64 l[17] = 106; 65 l[18] = -20; 66 l[19] = 62; 67 l[20] = -22; 68 l[21] = 89; 69 l[22] = -24; 70 l[23] = 38; 71 l[24] = -40; 72 l[25] = -33; 73 l[26] = -30; 74 l[27] = -29; 75 l[28] = 17; 76 l[29] = 18; 77 l[30] = -32; 78 l[31] = 15; 79 l[32] = 16; 80 l[33] = -37; 81 l[34] = -36; 82 l[35] = 21; 83 l[36] = 22; 84 l[37] = -39; 85 l[38] = 19; 86 l[39] = 20; 87 l[40] = -48; 88 l[41] = -45; 89 l[42] = -44; 90 l[43] = 7; 91 l[44] = 8; 92 l[45] = -47; 93 l[46] = 5; 94 l[47] = 6; 95 l[48] = -52; 96 l[49] = -51; 97 l[50] = 12; 98 l[51] = 14; 99 l[52] = -54; 100 l[53] = 9; 101 l[54] = 11; 102 l[55] = -57; 103 l[56] = 34; 104 l[57] = -59; 105 l[58] = 78; 106 l[59] = -61; 107 l[60] = 57; 108 l[61] = -63; 109 l[62] = 63; 110 l[63] = -65; 111 l[64] = 109; 112 l[65] = 119; 113 l[66] = -80; 114 l[67] = -69; 115 l[68] = 101; 116 l[69] = -71; 117 l[70] = 55; 118 l[71] = -73; 119 l[72] = 69; 120 l[73] = -75; 121 l[74] = 118; 122 l[75] = -77; 123 l[76] = 121; 124 l[77] = -79; 125 l[78] = 73; 126 l[79] = 123; 127 l[80] = -82; 128 l[81] = 49; 129 l[82] = -86; 130 l[83] = -85; 131 l[84] = 13; 132 l[85] = 40; 133 l[86] = -88; 134 l[87] = 45; 135 l[88] = -90; 136 l[89] = 68; 137 l[90] = -92; 138 l[91] = 84; 139 l[92] = -94; 140 l[93] = 125; 141 l[94] = -96; 142 l[95] = 39; 143 l[96] = -98; 144 l[97] = 58; 145 l[98] = -100; 146 l[99] = 36; 147 l[100] = -102; 148 l[101] = 92; 149 l[102] = -104; 150 l[103] = 124; 151 l[104] = -108; 152 l[105] = -107; 153 l[106] = 3; 154 l[107] = 4; 155 l[108] = -110; 156 l[109] = 0; 157 l[110] = 2; 158 l[111] = -129; 159 l[112] = -118; 160 l[113] = -115; 161 l[114] = 50; 162 l[115] = -117; 163 l[116] = 102; 164 l[117] = 103; 165 l[118] = -122; 166 l[119] = -121; 167 l[120] = 48; 168 l[121] = 110; 169 l[122] = -126; 170 l[123] = -125; 171 l[124] = 10; 172 l[125] = 1; 173 l[126] = -128; 174 l[127] = 79; 175 l[128] = 117; 176 l[129] = -137; 177 l[130] = -132; 178 l[131] = 51; 179 l[132] = -134; 180 l[133] = 98; 181 l[134] = -136; 182 l[135] = 47; 183 l[136] = 112; 184 l[137] = -141; 185 l[138] = -140; 186 l[139] = 56; 187 l[140] = 81; 188 l[141] = -143; 189 l[142] = 104; 190 l[143] = -145; 191 l[144] = 114; 192 l[145] = 115; 193 l[146] = -192; 194 l[147] = -153; 195 l[148] = -150; 196 l[149] = 32; 197 l[150] = -152; 198 l[151] = 91; 199 l[152] = 93; 200 l[153] = -157; 201 l[154] = -156; 202 l[155] = 99; 203 l[156] = 120; 204 l[157] = -159; 205 l[158] = 116; 206 l[159] = -161; 207 l[160] = 75; 208 l[161] = -163; 209 l[162] = 80; 210 l[163] = -165; 211 l[164] = 67; 212 l[165] = -167; 213 l[166] = 77; 214 l[167] = -169; 215 l[168] = 90; 216 l[169] = -171; 217 l[170] = 60; 218 l[171] = -173; 219 l[172] = 33; 220 l[173] = -175; 221 l[174] = 37; 222 l[175] = -177; 223 l[176] = 96; 224 l[177] = -185; 225 l[178] = -182; 226 l[179] = -181; 227 l[180] = 248; 228 l[181] = 249; 229 l[182] = -184; 230 l[183] = 246; 231 l[184] = 247; 232 l[185] = -189; 233 l[186] = -188; 234 l[187] = 252; 235 l[188] = 253; 236 l[189] = -191; 237 l[190] = 250; 238 l[191] = 251; 239 l[192] = -228; 240 l[193] = -225; 241 l[194] = -196; 242 l[195] = 52; 243 l[196] = -198; 244 l[197] = 108; 245 l[198] = -208; 246 l[199] = -201; 247 l[200] = 83; 248 l[201] = -205; 249 l[202] = -204; 250 l[203] = 61; 251 l[204] = 72; 252 l[205] = -207; 253 l[206] = 113; 254 l[207] = 122; 255 l[208] = -212; 256 l[209] = -211; 257 l[210] = 65; 258 l[211] = 70; 259 l[212] = -214; 260 l[213] = 71; 261 l[214] = -216; 262 l[215] = 76; 263 l[216] = -218; 264 l[217] = 74; 265 l[218] = -220; 266 l[219] = 88; 267 l[220] = -222; 268 l[221] = 64; 269 l[222] = -224; 270 l[223] = 42; 271 l[224] = 94; 272 l[225] = -227; 273 l[226] = 53; 274 l[227] = 100; 275 l[228] = -232; 276 l[229] = -231; 277 l[230] = 54; 278 l[231] = 82; 279 l[232] = -234; 280 l[233] = 97; 281 l[234] = -236; 282 l[235] = 111; 283 l[236] = -238; 284 l[237] = 43; 285 l[238] = -240; 286 l[239] = 87; 287 l[240] = -242; 288 l[241] = 41; 289 l[242] = -244; 290 l[243] = 86; 291 l[244] = -246; 292 l[245] = 35; 293 l[246] = -256; 294 l[247] = -249; 295 l[248] = 59; 296 l[249] = -251; 297 l[250] = 95; 298 l[251] = -253; 299 l[252] = 126; 300 l[253] = -255; 301 l[254] = 254; 302 l[255] = 255; 303 l[256] = -384; 304 l[257] = -321; 305 l[258] = -290; 306 l[259] = -275; 307 l[260] = -268; 308 l[261] = -265; 309 l[262] = -264; 310 l[263] = 160; 311 l[264] = 161; 312 l[265] = -267; 313 l[266] = 158; 314 l[267] = 159; 315 l[268] = -272; 316 l[269] = -271; 317 l[270] = 164; 318 l[271] = 165; 319 l[272] = -274; 320 l[273] = 162; 321 l[274] = 163; 322 l[275] = -283; 323 l[276] = -280; 324 l[277] = -279; 325 l[278] = 152; 326 l[279] = 153; 327 l[280] = -282; 328 l[281] = 150; 329 l[282] = 151; 330 l[283] = -287; 331 l[284] = -286; 332 l[285] = 156; 333 l[286] = 157; 334 l[287] = -289; 335 l[288] = 154; 336 l[289] = 155; 337 l[290] = -306; 338 l[291] = -299; 339 l[292] = -296; 340 l[293] = -295; 341 l[294] = 176; 342 l[295] = 177; 343 l[296] = -298; 344 l[297] = 174; 345 l[298] = 175; 346 l[299] = -303; 347 l[300] = -302; 348 l[301] = 180; 349 l[302] = 181; 350 l[303] = -305; 351 l[304] = 178; 352 l[305] = 179; 353 l[306] = -314; 354 l[307] = -311; 355 l[308] = -310; 356 l[309] = 168; 357 l[310] = 169; 358 l[311] = -313; 359 l[312] = 166; 360 l[313] = 167; 361 l[314] = -318; 362 l[315] = -317; 363 l[316] = 172; 364 l[317] = 173; 365 l[318] = -320; 366 l[319] = 170; 367 l[320] = 171; 368 l[321] = -353; 369 l[322] = -338; 370 l[323] = -331; 371 l[324] = -328; 372 l[325] = -327; 373 l[326] = 128; 374 l[327] = 129; 375 l[328] = -330; 376 l[329] = 31; 377 l[330] = 127; 378 l[331] = -335; 379 l[332] = -334; 380 l[333] = 132; 381 l[334] = 133; 382 l[335] = -337; 383 l[336] = 130; 384 l[337] = 131; 385 l[338] = -346; 386 l[339] = -343; 387 l[340] = -342; 388 l[341] = 25; 389 l[342] = 26; 390 l[343] = -345; 391 l[344] = 23; 392 l[345] = 24; 393 l[346] = -350; 394 l[347] = -349; 395 l[348] = 29; 396 l[349] = 30; 397 l[350] = -352; 398 l[351] = 27; 399 l[352] = 28; 400 l[353] = -369; 401 l[354] = -362; 402 l[355] = -359; 403 l[356] = -358; 404 l[357] = 144; 405 l[358] = 145; 406 l[359] = -361; 407 l[360] = 142; 408 l[361] = 143; 409 l[362] = -366; 410 l[363] = -365; 411 l[364] = 148; 412 l[365] = 149; 413 l[366] = -368; 414 l[367] = 146; 415 l[368] = 147; 416 l[369] = -377; 417 l[370] = -374; 418 l[371] = -373; 419 l[372] = 136; 420 l[373] = 137; 421 l[374] = -376; 422 l[375] = 134; 423 l[376] = 135; 424 l[377] = -381; 425 l[378] = -380; 426 l[379] = 140; 427 l[380] = 141; 428 l[381] = -383; 429 l[382] = 138; 430 l[383] = 139; 431 l[384] = -448; 432 l[385] = -417; 433 l[386] = -402; 434 l[387] = -395; 435 l[388] = -392; 436 l[389] = -391; 437 l[390] = 224; 438 l[391] = 225; 439 l[392] = -394; 440 l[393] = 222; 441 l[394] = 223; 442 l[395] = -399; 443 l[396] = -398; 444 l[397] = 228; 445 l[398] = 229; 446 l[399] = -401; 447 l[400] = 226; 448 l[401] = 227; 449 l[402] = -410; 450 l[403] = -407; 451 l[404] = -406; 452 l[405] = 216; 453 l[406] = 217; 454 l[407] = -409; 455 l[408] = 214; 456 l[409] = 215; 457 l[410] = -414; 458 l[411] = -413; 459 l[412] = 220; 460 l[413] = 221; 461 l[414] = -416; 462 l[415] = 218; 463 l[416] = 219; 464 l[417] = -433; 465 l[418] = -426; 466 l[419] = -423; 467 l[420] = -422; 468 l[421] = 240; 469 l[422] = 241; 470 l[423] = -425; 471 l[424] = 238; 472 l[425] = 239; 473 l[426] = -430; 474 l[427] = -429; 475 l[428] = 244; 476 l[429] = 245; 477 l[430] = -432; 478 l[431] = 242; 479 l[432] = 243; 480 l[433] = -441; 481 l[434] = -438; 482 l[435] = -437; 483 l[436] = 232; 484 l[437] = 233; 485 l[438] = -440; 486 l[439] = 230; 487 l[440] = 231; 488 l[441] = -445; 489 l[442] = -444; 490 l[443] = 236; 491 l[444] = 237; 492 l[445] = -447; 493 l[446] = 234; 494 l[447] = 235; 495 l[448] = -480; 496 l[449] = -465; 497 l[450] = -458; 498 l[451] = -455; 499 l[452] = -454; 500 l[453] = 192; 501 l[454] = 193; 502 l[455] = -457; 503 l[456] = 190; 504 l[457] = 191; 505 l[458] = -462; 506 l[459] = -461; 507 l[460] = 196; 508 l[461] = 197; 509 l[462] = -464; 510 l[463] = 194; 511 l[464] = 195; 512 l[465] = -473; 513 l[466] = -470; 514 l[467] = -469; 515 l[468] = 184; 516 l[469] = 185; 517 l[470] = -472; 518 l[471] = 182; 519 l[472] = 183; 520 l[473] = -477; 521 l[474] = -476; 522 l[475] = 188; 523 l[476] = 189; 524 l[477] = -479; 525 l[478] = 186; 526 l[479] = 187; 527 l[480] = -496; 528 l[481] = -489; 529 l[482] = -486; 530 l[483] = -485; 531 l[484] = 208; 532 l[485] = 209; 533 l[486] = -488; 534 l[487] = 206; 535 l[488] = 207; 536 l[489] = -493; 537 l[490] = -492; 538 l[491] = 212; 539 l[492] = 213; 540 l[493] = -495; 541 l[494] = 210; 542 l[495] = 211; 543 l[496] = -504; 544 l[497] = -501; 545 l[498] = -500; 546 l[499] = 200; 547 l[500] = 201; 548 l[501] = -503; 549 l[502] = 198; 550 l[503] = 199; 551 l[504] = -508; 552 l[505] = -507; 553 l[506] = 204; 554 l[507] = 205; 555 l[508] = -510; 556 l[509] = 202; 557 l[510] = 203; 558 559 var a=0, b=0, e=0, i, o=""; 560 561 function B() { if (a===0) { b=encodingCharSet.indexOf(bytes.charAt(e++)); a=6; } return ((b>>--a)&0x01); } 562 563 while(originalLength>0) { i=0; 564 while(l[i]<0) { 565 if (B()) { i=-l[i]; } 566 else { i++; } 567 } 568 o+=String.fromCharCode(l[i]); 569 originalLength--; 570 } 571 572 return o; 573} 574 575