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 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908
|
/*
* Copyright (C) 2009, Parrot Foundation.
*/
// pbc_checker.cpp
// A multiplatform checker for pbc files
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <stdexcept>
// Get most used symbols from std namespace
using std::cout;
using std::cerr;
using std::ifstream;
using std::string;
using std::hex;
using std::dec;
using std::setw;
using std::setfill;
using std::runtime_error;
//**********************************************************************
// Type used to read parrot opcodes
// You can use unsigned long here if your compiler does not support
// long long, but in that case the results might not be accurate.
typedef unsigned long long opcode;
// Constants used in pbc and his descriptions
const char unknown[] = "Unknown";
const unsigned char
ByteOrderLE = 0,
ByteOrderBE = 1;
const char * desc_byte_order(unsigned char c)
{
switch(c) {
case ByteOrderLE: return "Little endian";
case ByteOrderBE: return "Big endian";
default: return unknown;
}
}
const unsigned char
FpEncodingIEEE_754_8 = 0,
FpEncodingIEEE_i386_12 = 1,
FpEncodingIEEE_754_16 = 2;
const char * desc_fp_encoding(unsigned char c)
{
switch(c) {
case FpEncodingIEEE_754_8: return "IEEE 754 8 byte double";
case FpEncodingIEEE_i386_12: return "i386 little endian 12 byte long double";
case FpEncodingIEEE_754_16: return "IEEE 754 16 byte long double";
default: return unknown;
}
}
const char * desc_uuid_type(unsigned char c)
{
switch(c) {
case 0: return "none";
case 1: return "MD5";
default: return unknown;
}
}
const opcode
SegmentTypeDirectory = 0x00,
SegmentTypeDefault = 0x01,
SegmentTypeFixup = 0x02,
SegmentTypeConstantTable = 0x03,
SegmentTypeBytecode = 0x04,
SegmentTypePIRDebug = 0x05,
SegmentTypeAnnotations = 0x06,
SegmentTypePICData = 0x07,
SegmentTypeDependencies = 0x08;
const char * desc_segment_type(opcode t)
{
switch(t) {
case SegmentTypeDirectory: return "Directory";
case SegmentTypeDefault: return "Default";
case SegmentTypeFixup: return "Fixup";
case SegmentTypeConstantTable: return "Constant table";
case SegmentTypeBytecode: return "Bytecode";
case SegmentTypePIRDebug: return "PIR Debug";
case SegmentTypeAnnotations: return "Annotations";
case SegmentTypePICData: return "PIC Data";
case SegmentTypeDependencies: return "Dependencies";
default: return unknown;
}
}
const opcode
ConstantTypeNone = 0x00,
ConstantTypeNumber = 0x6E,
ConstantTypeString = 0x73,
ConstantTypePMC = 0x70,
ConstantTypeKey = 0x6B;
const char * desc_constant_type(opcode t)
{
switch(t) {
case ConstantTypeNone: return "None";
case ConstantTypeNumber: return "Number";
case ConstantTypeString: return "String";
case ConstantTypePMC: return "PMC";
case ConstantTypeKey: return "Key";
default: return unknown;
}
}
const opcode
KeyIntegerRegister = 0x00,
KeyStringRegister = 0x01,
KeyPMCRegister = 0x02,
KeyNumberRegister = 0x03,
KeyIntegerConstant = 0x10,
KeyStringConstant = 0x11,
KeyPMCConstant = 0x12,
KeyNumberConstant = 0x13;
const char * desc_key_type(opcode t)
{
switch (t) {
case KeyIntegerRegister: return "Integer register";
case KeyStringRegister: return "String register";
case KeyPMCRegister: return "PMC register";
case KeyNumberRegister: return "Number register";
case KeyIntegerConstant: return "Integer constant";
case KeyStringConstant: return "String constant";
case KeyPMCConstant: return "PMC constant";
case KeyNumberConstant: return "Number constant";
default: return unknown;
}
}
const opcode
AnnotationTypeInteger = 0x00,
AnnotationTypeString = 0x01,
AnnotationTypeNumber = 0x02,
AnnotationTypePMC = 0x03;
const char * desc_annotation_type(opcode t)
{
switch(t) {
case AnnotationTypeInteger: return "Integer";
case AnnotationTypeString: return "String";
case AnnotationTypeNumber: return "Number";
case AnnotationTypePMC: return "PMC";
default: return unknown;
}
}
//**********************************************************************
class ReadError : public runtime_error
{
public:
ReadError(const string &msg);
};
ReadError::ReadError(const string &msg) :
runtime_error("Error reading " + msg)
{
}
//**********************************************************************
void signature(ifstream &pbcfile)
{
static const char pbc_signature [] = {
0xFE, 0x50, 0x42, 0x43, 0x0D, 0x0A, 0x1A, 0x0A
};
char signature [sizeof (pbc_signature)];
pbcfile.read(signature, sizeof (signature));
if (pbcfile.gcount() != sizeof (signature))
throw ReadError("pbc signature");
if (! std::equal(pbc_signature, pbc_signature + sizeof(pbc_signature),
signature))
throw runtime_error("Invalid pbc signature");
}
//**********************************************************************
// Emit a open tag in the constructor
// and his closing in the destructor.
class TagEmit
{
public:
TagEmit(const string & name_n, std::ostream &os_n);
~TagEmit();
private:
string name;
std::ostream &os;
};
TagEmit::TagEmit(const string & name_n, std::ostream &os_n) :
name(name_n), os(os_n)
{
os << '<' << name << ">\n";
}
TagEmit::~TagEmit()
{
os << "</" << name << ">\n";
}
//**********************************************************************
class DirEntry
{
public:
DirEntry(string name_n, opcode type_n,
opcode offset_n, opcode length_n);
string getName() const;
opcode getType() const;
opcode getOffset() const;
opcode getLength() const;
private:
string name;
opcode type;
opcode offset;
opcode length;
};
DirEntry::DirEntry(string name_n, opcode type_n,
opcode offset_n, opcode length_n) :
name(name_n),
type(type_n),
offset(offset_n),
length(length_n)
{
}
string DirEntry::getName() const
{
return name;
}
opcode DirEntry::getType() const
{
return type;
}
opcode DirEntry::getOffset() const
{
return offset;
}
opcode DirEntry::getLength() const
{
return length;
}
bool operator < (const DirEntry & de1, const DirEntry & de2)
{
return de1.getOffset() < de2.getOffset();
}
//**********************************************************************
// Check that segments does not overlap
void check_overlap(const std::vector<DirEntry> & directory)
{
unsigned int entries = directory.size();
for (unsigned int i= 0; i < entries; ++i)
{
opcode offset = directory[i].getOffset();
opcode length = directory[i].getLength();
opcode after = offset + length;
for (unsigned int j= i + 1; j < entries; ++j) {
opcode offset2 = directory[j].getOffset();
if (offset2 >= offset && offset2 < after)
throw runtime_error("overlap");
opcode after2 = offset2 + directory[j].getLength();
if (offset >= offset2 && offset < after2)
throw runtime_error("overlap");
}
}
}
//**********************************************************************
class PbcFile
{
public:
PbcFile();
void read(const char *filename);
void read_header(ifstream &pbcfile);
void check_directory_format(ifstream &pbcfile);
void read_directory(ifstream &pbcfile);
void dump_segment(const DirEntry & entry, ifstream &pbcfile);
void dump_segment_default(ifstream &pbcfile);
void dump_segment_fixup(ifstream &pbcfile);
void dump_segment_constant(ifstream &pbcfile);
void dump_segment_bytecode(ifstream &pbcfile);
void dump_segment_pir_debug(ifstream &pbcfile);
void dump_segment_annotations(ifstream &pbcfile);
void dump_segment_pic_data(ifstream &pbcfile);
void dump_segment_dependencies(ifstream &pbcfile);
void dump_constant_string(ifstream &pbcfile);
void dump_constant_number(ifstream &pbcfile);
void dump_constant_key(ifstream &pbcfile);
void dump_bytes_hex(ifstream &pbcfile, opcode length);
string read_cstring(ifstream &pbcfile);
opcode read_opcode(ifstream &pbcfile);
private:
unsigned int opcode_size;
unsigned int pbc_major;
unsigned int pbc_minor;
unsigned char byte_order;
unsigned char fp_encoding;
std::vector<DirEntry> directory;
};
PbcFile::PbcFile()
{
opcode_size = 0;
}
void PbcFile::read(const char *filename)
{
ifstream pbcfile(filename);
if (! pbcfile.is_open())
throw runtime_error("Can't open file");
TagEmit filetag("PbcFile", cout);
read_header(pbcfile);
check_directory_format(pbcfile);
read_directory(pbcfile);
check_overlap(directory);
std::sort(directory.begin(), directory.end());
TagEmit tag("Segments", cout);
for (size_t i= 0; i < directory.size(); ++i)
dump_segment(directory[i], pbcfile);
}
void PbcFile::read_header(ifstream &pbcfile)
{
TagEmit tag("Header", cout);
signature(pbcfile);
unsigned char opcode_size = pbcfile.get();
unsigned char byte_order = pbcfile.get();
unsigned char fp_encoding = pbcfile.get();
unsigned char major = pbcfile.get();
unsigned char minor = pbcfile.get();
unsigned char patch = pbcfile.get();
unsigned char pbc_major = pbcfile.get();
unsigned char pbc_minor = pbcfile.get();
if (!pbcfile)
throw ReadError("pbc header data");
cout <<
"Opcode size : " << (int) opcode_size << '\n' <<
"Byte order : " << desc_byte_order(byte_order) <<
" (" << (int) byte_order << ")\n" <<
"Floating point encoding: " << desc_fp_encoding(fp_encoding) <<
" (" << (int) fp_encoding << ")\n" <<
"Parrot version : " <<
(int) major << '.' << (int) minor << '.' << (int) patch << '\n' <<
"PBC version : " <<
(int) pbc_major << '.' << (int) pbc_minor << '\n'
;
if (opcode_size > sizeof(opcode))
cerr << "*** Warning: opcode size too big for this program ***\n";
this->opcode_size = opcode_size;
this->pbc_major = pbc_major;
this->pbc_minor = pbc_minor;
this->byte_order = byte_order;
this->fp_encoding = fp_encoding;
if (byte_order != ByteOrderLE && byte_order != ByteOrderBE)
throw runtime_error("Invalid byte order");
unsigned char uuid_type = pbcfile.get();
cout <<
"UUID type : " << desc_uuid_type(uuid_type) <<
" (" << (int) uuid_type << ")\n"
;
unsigned char uuid_length = pbcfile.get();
cout <<
"UUID length : " << (int) uuid_length << '\n'
;
unsigned int curpos = 18 + uuid_length;
unsigned int endheader = ((curpos + 15) / 16) * 16;
pbcfile.ignore(endheader - 18);
}
void PbcFile::check_directory_format(ifstream &pbcfile)
{
TagEmit tag("DirectoryFormat", cout);
opcode dir_format = read_opcode(pbcfile);
cout << "Directory format: " << dir_format << '\n';
if (dir_format != 1)
throw runtime_error("Unknown directory format");
opcode unused = read_opcode(pbcfile);
unused = read_opcode(pbcfile);
unused = read_opcode(pbcfile);
for (opcode n = opcode_size * 4; n % 16; ++n)
pbcfile.ignore(1);
}
void PbcFile::read_directory(ifstream &pbcfile)
{
TagEmit tag("Directory", cout);
opcode size = read_opcode(pbcfile);
cout << "Directory segment size: " << size << '\n';
// Must be zero:
opcode in_type = read_opcode(pbcfile);
opcode in_id = read_opcode(pbcfile);
opcode op_size = read_opcode(pbcfile);
cout << "Internal type: " << in_type << '\n';
cout << "Internal id: " << in_id << '\n';
cout << "Op table size: " << op_size << '\n';
opcode entries = read_opcode(pbcfile);
cout << "Directory entries: " << entries << '\n';
for (unsigned int n= 0; n < entries; ++n)
{
opcode type = read_opcode(pbcfile);
cout <<
n << ": Type: '" << desc_segment_type(type) <<
" (" << type << ") "
"' Name: '";
string name = read_cstring(pbcfile);
cout << name;
opcode offset = read_opcode(pbcfile);
opcode length = read_opcode(pbcfile);
cout <<
"'\n Offset: " << offset << " Length: " << length << '\n';
DirEntry entry(name, type, offset, length);
directory.push_back(entry);
}
}
void PbcFile::dump_segment(const DirEntry &entry, ifstream &pbcfile)
{
cout << "Segment '" << entry.getName() << "'\n";
opcode const type = entry.getType();
cout << "Type: " << desc_segment_type(type) <<
" (0x" << hex << type << dec << ")\n";
// Set file read position to segment's start
size_t start = entry.getOffset() * opcode_size;
cout << "Start: 0x" << hex << setw(6) << start << dec << '\n';
pbcfile.seekg(start);
switch(type)
{
case SegmentTypeDirectory:
cout << "*** Unexpected directory segment ***\n";
break;
case SegmentTypeDefault:
dump_segment_default(pbcfile);
break;
case SegmentTypeFixup:
dump_segment_fixup(pbcfile);
break;
case SegmentTypeConstantTable:
dump_segment_constant(pbcfile);
break;
case SegmentTypeBytecode:
dump_segment_bytecode(pbcfile);
break;
case SegmentTypePIRDebug:
dump_segment_pir_debug(pbcfile);
break;
case SegmentTypeAnnotations:
dump_segment_annotations(pbcfile);
break;
case SegmentTypePICData:
dump_segment_pic_data(pbcfile);
break;
case SegmentTypeDependencies:
dump_segment_dependencies(pbcfile);
break;
default:
cout << "*** Unknown segment type ***\n";
break;
}
}
void PbcFile::dump_segment_default(ifstream &/*pbcfile*/)
{
TagEmit tag("SegmentDefault", cout);
cout << "*** UNIMPLEMENTED ***\n";
}
void PbcFile::dump_segment_fixup(ifstream &pbcfile)
{
TagEmit tag("SegmentFixup", cout);
opcode segsize = read_opcode(pbcfile);
cout << "Segment size: " << segsize;
opcode itype = read_opcode(pbcfile);
cout << " itype: " << itype;
opcode id = read_opcode(pbcfile);
cout << " id: " << id;
opcode size = read_opcode(pbcfile);
cout << " size: " << size;
opcode tablelength = read_opcode(pbcfile);
cout << " Number of fixups: " << tablelength << '\n';
for (opcode n= 0; n < tablelength; ++n) {
cout << "Fixup " << n;
opcode type = read_opcode(pbcfile);
cout << " Type: " <<
" (0x" << hex << type << dec << ')';
switch (type) {
case 0x01:
{
opcode label = read_opcode(pbcfile);
cout << " Label: " << label;
opcode sub = read_opcode(pbcfile);
cout << " Sub: " << sub;
}
break;
case 0x02:
{
string label = read_cstring(pbcfile);
cout << " Label: '" << label << '\'';
opcode sub = read_opcode(pbcfile);
cout << " Sub: " << sub;
}
break;
case 0x00:
cout << " None";
break;
default:
throw runtime_error("Invalid fixup");
}
cout << '\n';
}
}
void PbcFile::dump_segment_constant(ifstream &pbcfile)
{
TagEmit tag("SegmentConstantTable", cout);
opcode segsize = read_opcode(pbcfile);
cout << "Segment size: " << segsize;
opcode itype = read_opcode(pbcfile);
cout << " itype: " << itype;
opcode id = read_opcode(pbcfile);
cout << " id: " << id;
opcode size = read_opcode(pbcfile);
cout << " size: " << size;
opcode tablelength = read_opcode(pbcfile);
cout << " Number of constants: " << tablelength << '\n';
for (opcode n= 0; n < tablelength; ++n) {
cout << "Constant " << n;
opcode type = read_opcode(pbcfile);
cout << " Type: " << desc_constant_type(type) <<
" (0x" << hex << type << dec << ") ";
switch(type) {
case ConstantTypeString:
case ConstantTypePMC:
dump_constant_string(pbcfile);
break;
case ConstantTypeNumber:
dump_constant_number(pbcfile);
break;
case ConstantTypeKey:
dump_constant_key(pbcfile);
break;
default:
throw runtime_error("Unknown constant type");
}
}
}
void PbcFile::dump_segment_bytecode(ifstream &pbcfile)
{
TagEmit tag("SegmentBytecode", cout);
opcode segsize = read_opcode(pbcfile);
cout << "Segment size: " << segsize;
opcode itype = read_opcode(pbcfile);
cout << " itype: " << itype;
opcode id = read_opcode(pbcfile);
cout << " id: " << id;
opcode size = read_opcode(pbcfile);
cout << " size: " << size;
cout << hex;
for (opcode n= 0; n < size; ++n) {
opcode code = read_opcode(pbcfile);
if ((n % 8) == 0)
cout << '\n' << setfill('0') << setw (7) << n << ':';
else
cout << ' ';
cout << setfill('0') << setw(opcode_size * 2) << code;
}
cout << dec << '\n';
}
void PbcFile::dump_segment_pir_debug(ifstream &pbcfile)
{
TagEmit tag("SegmentPIRDebug", cout);
opcode segsize = read_opcode(pbcfile);
cout << "Segment size: " << segsize;
opcode itype = read_opcode(pbcfile);
cout << " itype: " << itype;
opcode id = read_opcode(pbcfile);
cout << " id: " << id;
opcode size = read_opcode(pbcfile);
cout << " size: " << size;
cout << '\n';
opcode tablelength = size;
for (opcode n= 0; n < tablelength; ++n) {
opcode linenum = read_opcode(pbcfile);
cout << " Line: " << linenum;
}
cout << '\n';
opcode mappings = read_opcode(pbcfile);
cout << "Mappings: " << mappings << '\n';
for (opcode n= 0; n < mappings; ++n) {
cout << " " << n;
opcode offset = read_opcode(pbcfile);
cout << " Offset: " << offset;
opcode filename = read_opcode(pbcfile);
cout << " File: " << filename;
cout << '\n';
}
}
void PbcFile::dump_segment_annotations(ifstream &pbcfile)
{
TagEmit tag("SegmentAnnotations", cout);
opcode segsize = read_opcode(pbcfile);
cout << "Segment size: " << segsize;
opcode itype = read_opcode(pbcfile);
cout << " itype: " << itype;
opcode id = read_opcode(pbcfile);
cout << " id: " << id;
opcode size = read_opcode(pbcfile);
cout << " size: " << size;
cout << '\n';
opcode tablelength = read_opcode(pbcfile);
cout << "Number of annotations: " << tablelength << '\n';
for (opcode i= 0; i < tablelength; ++i) {
cout << i;
opcode name = read_opcode(pbcfile);
cout << " Name: " << name;
opcode type = read_opcode(pbcfile);
cout << " Type: " << desc_annotation_type(type) << '\n';
}
opcode grouplength = read_opcode(pbcfile);
cout << "Number of annotation groups: " << grouplength << '\n';
for (opcode i= 0; i < grouplength; ++i) {
cout << i;
opcode bcpos = read_opcode(pbcfile);
cout << " Bytecode offset: " << bcpos;
opcode value = read_opcode(pbcfile);
cout << " Value: " << value << '\n';
}
opcode mappings = read_opcode(pbcfile);
cout << "Number of mappings: " << mappings << '\n';
for (opcode i= 0; i < mappings; ++i) {
cout << i;
opcode bcpos = read_opcode(pbcfile);
cout << " Bytecode offset: " << bcpos;
opcode annotation = read_opcode(pbcfile);
cout << " Annotation key: " << annotation;
opcode value = read_opcode(pbcfile);
cout << " Value: " << value << '\n';
}
}
void PbcFile::dump_segment_pic_data(ifstream &/*pbcfile*/)
{
TagEmit tag("SegmentPICData", cout);
cout << "*** UNIMPLEMENTED ***\n";
}
void PbcFile::dump_segment_dependencies(ifstream &/*pbcfile*/)
{
TagEmit tag("SegmentDependencies", cout);
cout << "*** UNIMPLEMENTED ***\n";
}
void PbcFile::dump_constant_string(ifstream &pbcfile)
{
opcode flags;
opcode charset;
const opcode encoding_none = 0xFFFF;
opcode encoding = encoding_none;
if (pbc_major > 5 && pbc_minor > 11) {
opcode flags_charset = read_opcode(pbcfile);
flags = flags_charset & 0xFF;
charset = flags_charset >> 8;
if (pbc_major > 6 || pbc_minor >= 17) {
encoding = charset >> 8;
charset &= 0xFF;
}
}
else {
flags = read_opcode(pbcfile);
charset = read_opcode(pbcfile);
}
cout << "Flags: 0x" << hex << setw(6) << flags << dec;
cout << " Charset: " << charset;
if (encoding != encoding_none)
cout << " Encoding: " << encoding;
// Encoding not saved, see TT #468
//opcode encoding = read_opcode(pbcfile);
//cout << " Encoding: "<< encoding;
opcode length = read_opcode(pbcfile);
cout << " Length: "<< length;
// Don't dump very long strings at full length.
opcode full = length;
length = std::min(length, (opcode)512);
cout << " \'";
for (opcode i= 0; i < length; ++i) {
unsigned char c = pbcfile.get();
if (! pbcfile)
throw ReadError("string constant");
if (c >= 32 && c < 128)
cout << c;
else
cout << "\\x" << hex << setw(2) << setfill('0') <<
(unsigned int) c << dec;
}
cout << '\'';
if (full > length) {
cout << "(...)";
pbcfile.ignore(full - length);
}
cout << '\n';
for (unsigned int i= full; i % opcode_size; ++i) {
pbcfile.ignore(1);
}
}
void PbcFile::dump_constant_number(ifstream &pbcfile)
{
cout << "Number constant: ";
switch(fp_encoding) {
case FpEncodingIEEE_754_8:
dump_bytes_hex(pbcfile, 8);
break;
case FpEncodingIEEE_i386_12:
dump_bytes_hex(pbcfile, 12);
break;
case FpEncodingIEEE_754_16:
dump_bytes_hex(pbcfile, 16);
break;
default:
// This must have been catched before reaching this point
throw std::logic_error("Bad number type");
}
}
void PbcFile::dump_constant_key(ifstream &pbcfile)
{
opcode components = read_opcode(pbcfile);
cout << "Key components: " << components << '\n';
for (opcode i= 0; i < components; ++i) {
cout << " " << i << ' ';
opcode type = read_opcode(pbcfile);
cout << "Type: " << desc_key_type (type) <<
" (0x" << hex << type << dec << ") ";
opcode value = read_opcode(pbcfile);
cout << "Value: " << value << '\n';
}
}
void PbcFile::dump_bytes_hex(ifstream &pbcfile, opcode length)
{
cout << "0x" << hex;
for (opcode i= 0; i < length; ++i) {
unsigned char c = pbcfile.get();
cout << setw(2) << setfill('0') << (unsigned int) c;
}
cout << dec << '\n';
}
string PbcFile::read_cstring(ifstream &pbcfile)
{
string r;
char c;
while ((c= pbcfile.get())) {
r+= c;
}
// cstrings are padded with trailing zeroes to opcode size
for (opcode l = r.size() + 1; l % opcode_size; ++l)
pbcfile.ignore(1);
if(! pbcfile)
throw ReadError("cstring");
return r;
}
opcode PbcFile::read_opcode(ifstream &pbcfile)
{
unsigned char buffer [32]; // Allow 256 bits opcode size
pbcfile.read((char *)buffer, opcode_size);
if (static_cast<unsigned int>(pbcfile.gcount()) != opcode_size)
throw ReadError("opcode");
opcode result = 0;
switch(byte_order)
{
case ByteOrderLE:
for (unsigned int i= 0; i < opcode_size; ++i) {
result <<= 8;
result += buffer [opcode_size - 1 - i];
}
break;
case ByteOrderBE:
for (unsigned int i= 0; i < opcode_size; ++i) {
result <<= 8;
result += buffer [i];
}
break;
default:
// This must have been catched before reaching this point
throw std::logic_error("Bad byte order");
}
return result;
}
//**********************************************************************
void pbc_checker_main(int argc, char **argv)
{
if (argc < 2)
throw runtime_error("Bad args");
PbcFile pbcfile;
pbcfile.read(argv[1]);
}
//**********************************************************************
int main(int argc, char **argv)
{
try
{
pbc_checker_main(argc, argv);
return 0;
}
catch(std::
exception &e)
{
cerr << "FAILED: " << e.what() << '\n';
}
return 1;
}
// End of pbc_checker.cpp
/*
* Local variables:
* c-file-style: "parrot"
* End:
* vim: expandtab shiftwidth=4:
*/
|