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
|
###########################################################################
##
#W omputbin.gi OpenMath Package Max Nicosia
##
##
#Y Copyright (C) 1999, 2000, 2001, 2006
#Y School Math and Comp. Sci., University of St. Andrews, Scotland
#Y Copyright (C) 2004, 2005, 2006 Marco Costantini
##
## Low-level methods for output in the OpenMath binary format
##
###########################################################################
##
#M BigIntToListofInts( <integer> )
##
## Returns a list of 4 integers as to represent the number over 4 bytes
##
BindGlobal ( "BigIntToListofInts", function(int)
local hexValue, hexValueLength, finalHexString, lengthDiff, listofInts;
listofInts:=[];
finalHexString := "";
hexValue := HexStringInt(int);
hexValueLength := Length(hexValue);
if (hexValueLength < 8) then
lengthDiff := 8 - hexValueLength;
while lengthDiff > 0 do
Append(finalHexString,"0");
lengthDiff := lengthDiff - 1;
od;
fi;
Append(finalHexString, hexValue);
Add(listofInts, IntHexString(finalHexString{[1..2]}));
Add(listofInts, IntHexString(finalHexString{[3..4]}));
Add(listofInts, IntHexString(finalHexString{[5..6]}));
Add(listofInts, IntHexString(finalHexString{[7..8]}));
return listofInts;
end);
###########################################################################
#
#M IsIntFloat( <object> )
#
# Checks whethere it is a real float
BindGlobal( "IsIntFloat", function(x)
return Float(0) = x-Float(Int(x));
end);
###########################################################################
##
#M CreateListWithFalses( <integer> )
##
## Returns a list with the number of falses specified,
## if 0 then returns an empty list.
##
BindGlobal( "CreateListWithFalses", function(numFalses)
local listFalses, i;
listFalses := [];
for i in [1..numFalses] do
listFalses[i]:=false;
od;
return listFalses;
end);
###########################################################################
##
#M WriteIntasBytes( <stream>, <list> )
##
## Writes an integer as four bytes given a list representing the integer,
## in binary and the stream
##
BindGlobal( "WriteIntasBytes", function( stream, listofInts )
WriteByte(stream, listofInts[1]);
WriteByte(stream, listofInts[2]);
WriteByte(stream, listofInts[3]);
WriteByte(stream, listofInts[4]);
end);
###########################################################################
##
#M FindFirst1BinaryString( <string> )
##
## Obtains position of first 1 in the binary string
##
BindGlobal( "FindFirst1BinaryString", function( binStri )
local i, binStriLen;
binStriLen := Length(binStri);
i := 1;
while binStri[i] <> '1' and i <= binStriLen do
i := i +1;
od;
return i;
end);
###########################################################################
##
#M WriteDecasHex( <float> )
##
## Returns the decimal number in a hexadecimal representation
##
BindGlobal( "WriteDecasHex", function( decPart )
local intPart, resultHex, number, i, zeroF;
i := 0;
resultHex := "";
zeroF := Float("0.0");
while decPart <> zeroF do
if i > 10 then
break;
fi;
number := decPart * 16;
intPart := Int(number);
decPart := number - intPart;
Append(resultHex, HexStringInt(intPart));
i := i +1;
od;
return resultHex;
end);
###########################################################################
##
#M WriteHexAsBin( <hexNum>, <bool> )
##
## Returns the given hexadecimal number as a binary string.
## Leading zeroes are included if the flag is set to TRUE.
##
BindGlobal( "WriteHexAsBin", function( hexNum, withLeadingZeroes )
local hexNumLen, binStri, num, charStri, counter,
binArrayWithZeroes, binArrayNoLeadingZeroes;
hexNumLen := Length(hexNum);
binStri := "";
counter:= 1;
binArrayWithZeroes :=
["0000", "0001" ,"0010", "0011", "0100", "0101", "0110", "0111",
"1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"];
binArrayNoLeadingZeroes :=
["", "1" ,"10", "11", "100", "101", "110", "111", "1000",
"1001", "1010", "1011", "1100", "1101", "1110", "1111"];
charStri := hexNum{[counter]};
num := IntHexString(charStri);
if withLeadingZeroes then
Append(binStri, binArrayWithZeroes[num+1]);
else
Append(binStri, binArrayNoLeadingZeroes[num+1]);
fi;
counter := counter +1;
while counter <= hexNumLen do
charStri := hexNum{[counter]};
num := IntHexString(charStri);
Append(binStri, binArrayWithZeroes[num+1]);
counter := counter +1;
od;
return binStri;
end);
###########################################################################
##
#M WriteBinAsHex( <string> )
##
## Returns the binary string passed as a hexadecimal number
##
BindGlobal( "WriteBinAsHex", function( binStri )
local binStriLen, hexStri, counter, binArray,hexArray, limit, upper, lower;
binStriLen := Length(binStri);
hexStri := "";
counter:= 1;
binArray :=
["0000", "0001" ,"0010", "0011", "0100", "0101", "0110", "0111",
"1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"];
hexArray :=
["0", "1", "2", "3", "4", "5", "6", "7",
"8", "9", "A", "B", "C", "D", "E", "F"];
#limit := binStriLen /4;
upper := 4;
lower := 1;
while upper <= binStriLen do
for counter in [1..16] do
if binStri{[lower..upper]} = binArray[counter] then
Append(hexStri, hexArray[counter]);
break;
fi;
counter := counter + 1;
od;
upper := upper +4;
lower := lower + 4;
od;
return hexStri;
end);
###########################################################################
###
##M
## removes all falses at the start of the list.
#BindGlobal( "NormaliseBlist",
#function( list )
#local i, listLen, finalList;
#i:= 1;
#listLen := Length(list);
#while i <= listLen and list[i] = false do
# i := i +1;
#od;
#if i >= listLen then
# finalList := [list[listLen]];
#else
# finalList := list{[i..listLen]};
#fi;
#list := finalList;
#return i;
#end);
###########################################################################
##
#M WriteHexStriAsBytes( <string>, <stream> )
##
## Writes the hexadecimal string to the stream as bytes.
##
BindGlobal( "WriteHexStriAsBytes", function( hexStri, stream )
local hexStriLen, intValue, upper, lower;
upper := 2;
lower := 1;
intValue := 0;
hexStriLen := Length(hexStri);
while upper <= hexStriLen do
intValue := IntHexString(hexStri{[lower..upper]});
WriteByte(stream, intValue);
upper := upper +2;
lower := lower +2;
od;
end);
###########################################################################
##
#M WriteBinStringsAsBytes( <)
##
##
BindGlobal("WriteBinStringsAsBytes", function(sign,exponent,mantissa,stream)
local exponentLen, mantissaLen, firstPart, secondPart, numbZeroes, hexStri;
exponentLen := Length(exponent);
mantissaLen := Length(mantissa);
firstPart := "";
secondPart := mantissa;
if sign then
Append(firstPart, "1" );
else
Append(firstPart, "0" );
fi;
if exponentLen < 11 then
numbZeroes := 11 - exponentLen ;
while numbZeroes <> 0 do
Append(firstPart, "0");
numbZeroes := numbZeroes - 1;
od;
fi;
Append(firstPart, exponent);
if mantissaLen < 52 then
numbZeroes := 52 - mantissaLen;
while numbZeroes <> 0 do
Append(secondPart, "0");
numbZeroes := numbZeroes -1;
od;
fi;
Append(firstPart,secondPart);
hexStri := WriteBinAsHex(firstPart);
WriteHexStriAsBytes(hexStri, stream);
end);
###########################################################################
##
#O OMPutOMOBJ( <stream> )
#O OMPutEndOMOBJ( <stream> )
##
InstallMethod(OMPutOMOBJ, "to write OMOBJ in binary OpenMath", true,
[ IsOpenMathBinaryWriter ], 0,
function( writer )
WriteByte( writer![1], 24 );
end);
InstallMethod(OMPutEndOMOBJ, "to write /OMOBJ in binary OpenMath", true,
[ IsOpenMathBinaryWriter ], 0,
function( writer )
WriteByte( writer![1], 25 );
end);
###########################################################################
##
#M OMPut( <OMWriter>, <int> )
##
## Printing for integers: specified in the standard
##
InstallMethod(OMPut, "for an integer to binary OpenMath", true,
[ IsOpenMathBinaryWriter, IsInt ],0,
function( writer, int )
local intStri, intLength, intListLength;
intStri := String(AbsInt(int));
intLength := Length(intStri);
if int >= -128 and int <= 127 then
WriteByte( writer![1], 1);
if int < 0 then
int := 256 + int;
fi;
WriteByte(writer![1], int);
elif int >= -2^31 and int <= 2^31-1 then
WriteByte( writer![1], 129); #1+128
if int < 0 then
int := 2^32 + int;
fi;
intListLength := BigIntToListofInts(int);
WriteIntasBytes(writer![1], intListLength);
elif intLength >= 0 and intLength <= 255 then
WriteByte( writer![1], 2);
WriteByte(writer![1], intLength);
if int < 0 then
WriteByte(writer![1], 45); #base 10 | sign -
else
WriteByte(writer![1], 43); #base 10 | sign +
fi;
WriteAll(writer![1], intStri);
elif intLength > 255 then
WriteByte( writer![1], 130);#2+128
intListLength := BigIntToListofInts(intLength);
WriteIntasBytes(writer![1], intListLength);
if int < 0 then
WriteByte(writer![1], 45); #base 10 | sign -
else
WriteByte(writer![1], 43); #base 10 | sign +
fi;
WriteAll(writer![1], intStri);
fi;
end);
###########################################################################
##
#M OMPut( <OMWriter>, <float> )
##
##
##
InstallMethod(OMPut, "for a float to binary OpenMath", true,
[ IsOpenMathBinaryWriter, IS_MACFLOAT ],0,
function(writer, f)
local intPart, decPart, sign, decHex, decBin, decBinLen, exponent,
pos, mantissa, intBin, absIntPart;
WriteByte( writer![1], 3);
if f > 0 then
sign := false;
else
sign := true;
fi;
intPart := Int(f);
if IsIntFloat(f) then
decPart := 0;
else
decPart := f - intPart;
fi;
decHex := WriteDecasHex(decPart);
decBin := WriteHexAsBin(decHex, true);
decBinLen := Length(decBin);
absIntPart := AbsInt(intPart);
if absIntPart = 0 then
pos := FindFirst1BinaryString(decBin);
exponent := 1023 - pos;
exponent := WriteHexAsBin(HexStringInt(exponent), false);
mantissa := decBin{[pos+1..decBinLen]};
else
intBin := WriteHexAsBin(HexStringInt(absIntPart),false);
pos := Length(intBin) -1;
exponent := 1023 + pos;
exponent := WriteHexAsBin(HexStringInt(exponent), false);
Append(intBin, decBin);
mantissa := intBin{[2..Length(intBin)]};
fi;
if Length(mantissa) > 52 then
mantissa := mantissa{[1..52]};
fi;
WriteBinStringsAsBytes( sign, exponent, mantissa , writer![1]);
end);
###########################################################################
##
#M OMPutVar( <OMWriter>, <variable> )
##
##
##
InstallMethod(OMPutVar, "for a variable to binary OpenMath", true,
[ IsOpenMathBinaryWriter, IsObject ],0,
function(writer, var)
local varLength, varStri, varLengthList;
varStri := String(var);
varLength := Length(varStri);
if varLength >= 256 then
WriteByte( writer![1], 133); #5+128
varLengthList := BigIntToListofInts(varLength);
WriteIntasBytes(writer![1], varLengthList);
else
WriteByte( writer![1], 5);
WriteByte(writer![1], varLength);
fi;
WriteAll(writer![1], varStri);
end);
###########################################################################
##
#M OMPut( <OMWriter>, <symbol> )
##
##
##
InstallMethod( OMPutSymbol, "for a symbol to binary OpenMath", true,
[IsOpenMathBinaryWriter, IsString, IsString ],0,
function( writer, cd, name )
local cdLength, nameLength, cdListInt, nameListInt;
nameListInt := [];
cdLength := Length(cd);
nameLength := Length(name);
if (cdLength > 255 or nameLength > 255) then
WriteByte( writer![1], 136); #128+8
cdListInt := BigIntToListofInts(cdLength);
nameListInt := BigIntToListofInts(nameLength);
#writing the cd length as 4 bytes
WriteIntasBytes(writer![1], cdListInt);
#writing the name length as 4 bytes
WriteIntasBytes(writer![1], nameListInt);
else
WriteByte(writer![1], 8);
WriteByte(writer![1], cdLength);
WriteByte(writer![1], nameLength);
fi;
WriteAll(writer![1], cd);
WriteAll(writer![1], name);
end);
###########################################################################
##
#M OMPutOMATTR
#M OMPutEndOMATTR
##
InstallMethod(OMPutOMATTR, "to write OMATTR in Binary OpenMath", true,
[IsOpenMathBinaryWriter],0,
function( writer )
WriteByte( writer![1], 18 );
end);
InstallMethod(OMPutEndOMATTR, "to write /OMATTR in Binary OpenMath", true,
[IsOpenMathBinaryWriter],0,
function( writer )
OMIndent := OMIndent - 1;
WriteByte( writer![1], 19 );
end);
###########################################################################
##
#M OMPutOMATP
#M OMPutEndOMATP
##
InstallMethod(OMPutOMATP, "to write OMATP in Binary OpenMath", true,
[IsOpenMathBinaryWriter],0,
function( writer )
WriteByte( writer![1], 20);
end);
InstallMethod(OMPutEndOMATP, "to write /OMATP in Binary OpenMath", true,
[IsOpenMathBinaryWriter],0,
function( writer )
WriteByte( writer![1], 21 );
end);
###########################################################################
##
#M OMPutOMBIND
#M OMPutEndOMBIND
##
InstallMethod(OMPutOMBIND, "to write OMBIND in Binary OpenMath", true,
[IsOpenMathBinaryWriter],0,
function( writer )
WriteByte( writer![1], 26 );
end);
InstallMethod(OMPutEndOMBIND, "to write /OMBIND in Binary OpenMath", true,
[IsOpenMathBinaryWriter],0,
function( writer )
WriteByte( writer![1], 27 );
end);
###########################################################################
##
#M OMPutOMBVAR
#M OMPutEndOMBVAR
##
InstallMethod(OMPutOMBVAR, "to write OMBVAR in Binary OpenMath", true,
[IsOpenMathBinaryWriter],0,
function( writer )
WriteByte( writer![1], 28 );
end);
InstallMethod(OMPutEndOMBVAR, "to write /OMBVAR in Binary OpenMath", true,
[IsOpenMathBinaryWriter],0,
function( writer )
WriteByte( writer![1], 29 );
end);
###########################################################################
##
#M OMPutByteArray( <OMWriter>, <bitList> )
##
##
InstallGlobalFunction( OMPutByteArray, function( writer, bitList )
# TODO: fill the 2nd branch and move this function to omput.gi
local numBytesLength, bitListLength, tempList, numFalses, hexStri,
numBytes, quoVal, modVal;
if IsOpenMathBinaryWriter(writer) then
bitListLength := Length(bitList);
quoVal := QuoInt(bitListLength,8);
modVal := bitListLength mod 8;
if quoVal = 0 then
numBytes := 1;
numFalses := 8 - bitListLength;
else
if modVal <> 0 then
numBytes := quoVal +1;
numFalses := 8 - modVal;
else
numBytes := quoVal;
numFalses := 0;
fi;
fi;
if numBytes > 255 then
numBytesLength := BigIntToListofInts(numBytes);
WriteByte(writer![1],132); #4+128
#writing the string length as 4 bytes
WriteIntasBytes(writer![1], numBytesLength);
else
WriteByte(writer![1],4);
WriteByte(writer![1], numBytes);
fi;
tempList := CreateListWithFalses(numFalses);
Append(tempList, bitList);
hexStri := HexStringBlist(tempList);
WriteHexStriAsBytes(hexStri, writer![1]);
else
Error("Bytearrays are not supported in the XML mode yet!");
fi;
end);
###########################################################################
##
#M OMPut( <OMWriter>, <string> )
##
##
InstallMethod(OMPut, "for a string to binary OpenMath", true,
[IsOpenMathBinaryWriter, IsString ],0,
function( writer, string )
local strLength, strListLength;
strLength := Length(string);
if strLength > 255 then
strListLength := BigIntToListofInts(strLength);
WriteByte(writer![1], 134); # 6+128
#writing the string length as 4 bytes
WriteIntasBytes(writer![1], strListLength);
else
WriteByte(writer![1], 6);
WriteByte(writer![1], strLength);
fi;
WriteAll(writer![1],string);
end);
###########################################################################
##
#M OMPut( <OMWriter>, <foreign> )
##
##
InstallMethod( OMPutForeign, "for a foreign object to binary OpenMath", true,
[IsOpenMathBinaryWriter, IsString, IsString ],0,
function( writer, encString, objString )
local encStrLength, encStrListLength, objStrLength, objStrListLength;
encStrLength := Length(encString);
objStrLength := Length(objString);
if encStrLength > 255 or objStrLength > 255 then
WriteByte(writer![1], 140);#12+128
encStrListLength := BigIntToListofInts(encStrLength);
objStrListLength := BigIntToListofInts(objStrLength);
WriteIntasBytes(writer![1], encStrListLength);
WriteIntasBytes(writer![1], objStrListLength);
else
WriteByte(writer![1], 12);
WriteByte(writer![1], encStrLength);
WriteByte(writer![1], objStrLength);
fi;
WriteAll(writer![1], encString);
WriteAll(writer![1], objString);
end);
###########################################################################
##
#M OMPutOMAWithId( <OMWriter>, <reference> )
##
##
InstallMethod( OMPutOMAWithId, "to put Applications with Ids", true,
[IsOpenMathBinaryWriter , IsString],0,
function(writer, reference)
local referenceList, referenceLen;
referenceLen := Length(reference);
if referenceLen > 255 then
referenceList := BigIntToListofInts(referenceLen);
WriteByte(writer![1], 208); # 16+64+128
#writing the reference length as 4 bytes
WriteIntasBytes(writer![1], referenceList);
else
WriteByte(writer![1], 80); #16+64
WriteByte(writer![1], referenceLen);
fi;
WriteAll(writer![1], reference);
end);
###########################################################################
##
#O OMPutOMA( <OMWriter> );
#O OMPutEndOMA( <OMWriter> );
##
##
InstallMethod(OMPutOMA, "to write OMA in binary OpenMath", true,
[ IsOpenMathBinaryWriter ], 0,
function( writer )
WriteByte( writer![1], 16 );
end);
InstallMethod(OMPutEndOMA, "to write /OMA in binary OpenMath", true,
[ IsOpenMathBinaryWriter ], 0,
function( writer )
WriteByte( writer![1], 17 );
end);
###########################################################################
##
#O OMPutOME( <OMWriter> );
#O OMPutEndOME( <OMWriter> );
##
##
InstallMethod(OMPutOME, "to write OME in binary OpenMath", true,
[ IsOpenMathBinaryWriter ], 0,
function( writer )
WriteByte( writer![1], 22 );
end);
InstallMethod(OMPutEndOME, "to write /OME in binary OpenMath", true,
[ IsOpenMathBinaryWriter ], 0,
function( writer )
WriteByte( writer![1], 23 );
end);
###########################################################################
##
#M OMPut( <OMWriter>, <reference> )
##
## deals with external references for now
##
InstallMethod( OMPutReference, "for a stream and an object with reference",
true, [ IsOpenMathBinaryWriter, IsObject ], 0,
function( writer, x )
local refStri, refLength, lengthList;
if HasOMReference( x ) and not SuppressOpenMathReferences then
refStri := OMReference( x );
refLength := Length(refStri);
if refLength > 255 then
WriteByte (writer![1], 159); #31+128
lengthList := BigIntToListofInts(refLength);
WriteIntasBytes(writer![1], lengthList);
else
WriteByte (writer![1], 31);
WriteByte (writer![1], refLength);
fi;
WriteAll(writer![1], refStri);
else
OMPut( writer, x );
fi;
end);
###########################################################################
#E
|