Menu

[e85c93]: / rzx.c  Maximize  Restore  History

Download this file

1765 lines (1360 with data), 48.2 kB

   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
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
/* rzx.c: routines for dealing with .rzx files
Copyright (c) 2002-2015 Philip Kendall
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.
This 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, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Author contact information:
E-mail: philip-fuse@shadowmagic.org.uk
*/
#include "config.h"
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#ifdef HAVE_STRINGS_H
#include <strings.h> /* Needed for strcasecmp() on QNX6 */
#endif /* #ifdef HAVE_STRINGS_H */
#ifdef HAVE_GCRYPT_H
#include <gcrypt.h>
#endif /* #ifdef HAVE_GCRYPT_H */
#include "internals.h"
/* The strings used for each snapshot type */
typedef struct snapshot_string_t {
libspectrum_id_t format;
const char *string;
} snapshot_string_t;
static snapshot_string_t snapshot_strings[] = {
{ LIBSPECTRUM_ID_SNAPSHOT_SNA, "SNA" },
{ LIBSPECTRUM_ID_SNAPSHOT_SZX, "SZX" },
{ LIBSPECTRUM_ID_SNAPSHOT_Z80, "Z80" },
{ 0, NULL }, /* End marker */
};
typedef struct libspectrum_rzx_frame_t {
size_t instructions;
size_t count;
libspectrum_byte* in_bytes;
int repeat_last; /* Set if we should use the last
frame's IN bytes */
} libspectrum_rzx_frame_t;
typedef struct input_block_t {
libspectrum_rzx_frame_t *frames;
size_t count;
size_t allocated;
size_t tstates;
/* Used for recording to note the last non-repeated frame. We can't
really use a direct pointer to the frame here as that will move
around when we do a renew on the array, so just dereference it
every time */
size_t non_repeat;
} input_block_t;
typedef struct snapshot_block_t {
libspectrum_snap *snap;
int automatic;
} snapshot_block_t;
typedef struct signature_block_t {
size_t length; /* Length of the signed data from rzx->signed_start */
#ifdef HAVE_GCRYPT_H
gcry_mpi_t r, s;
#endif /* #ifdef HAVE_GCRYPT_H */
} signature_block_t;
typedef struct rzx_block_t {
libspectrum_rzx_block_id type;
union {
input_block_t input;
snapshot_block_t snap;
libspectrum_dword keyid;
signature_block_t signature;
} types;
} rzx_block_t;
struct libspectrum_rzx {
GSList *blocks;
/* Playback variables */
GSList *current_block;
input_block_t *current_input;
size_t current_frame;
libspectrum_rzx_frame_t *data_frame;
size_t in_count;
/* Signature parameters */
const libspectrum_byte *signed_start;
size_t signed_length;
};
static libspectrum_error
rzx_read_header( const libspectrum_byte **ptr, const libspectrum_byte *end );
static libspectrum_error
rzx_read_creator( const libspectrum_byte **ptr, const libspectrum_byte *end );
static libspectrum_error
rzx_read_snapshot( libspectrum_rzx *rzx, const libspectrum_byte **ptr,
const libspectrum_byte *end );
static libspectrum_error
rzx_read_input( libspectrum_rzx *rzx,
const libspectrum_byte **ptr, const libspectrum_byte *end );
static libspectrum_error
rzx_read_frames( input_block_t *block, const libspectrum_byte **ptr,
const libspectrum_byte *end );
static libspectrum_error
rzx_read_sign_start( libspectrum_rzx *rzx, const libspectrum_byte **ptr,
const libspectrum_byte *end );
static libspectrum_error
rzx_read_sign_end( libspectrum_rzx *rzx, const libspectrum_byte **ptr,
const libspectrum_byte *end );
static void
rzx_write_header( libspectrum_buffer *buffer, int sign );
static void
rzx_write_creator( libspectrum_buffer *buffer, libspectrum_buffer *block_data,
libspectrum_creator *creator );
static libspectrum_error
rzx_write_snapshot( libspectrum_buffer *buffer, libspectrum_buffer *block_data,
libspectrum_snap *snap, libspectrum_id_t snap_format,
libspectrum_creator *creator, int compress );
static void
rzx_write_input( input_block_t *block, libspectrum_buffer *buffer,
libspectrum_buffer *block_data, int compress );
static libspectrum_error
rzx_write_signed_start( libspectrum_buffer *buffer,
libspectrum_buffer *block_data,
libspectrum_rzx_dsa_key *key,
libspectrum_creator *creator );
static libspectrum_error
rzx_write_signed_end( libspectrum_buffer *buffer, libspectrum_buffer *block_data,
libspectrum_rzx_dsa_key *key );
/* The signature used to identify .rzx files */
static const char * const rzx_signature = "RZX!";
/* The IN count used to signify 'repeat last frame' */
const libspectrum_word libspectrum_rzx_repeat_frame = 0xffff;
/*
* Generic block handling routines
*/
static void
block_alloc( rzx_block_t **block, libspectrum_rzx_block_id type )
{
*block = libspectrum_new( rzx_block_t, 1 );
(*block)->type = type;
}
static libspectrum_error
block_free( rzx_block_t *block )
{
size_t i;
input_block_t *input;
#ifdef HAVE_GCRYPT_H
signature_block_t *signature;
#endif /* #ifdef HAVE_GCRYPT_H */
switch( block->type ) {
case LIBSPECTRUM_RZX_INPUT_BLOCK:
input = &( block->types.input );
for( i = 0; i < input->count; i++ )
if( !input->frames[i].repeat_last ) libspectrum_free( input->frames[i].in_bytes );
libspectrum_free( input->frames );
libspectrum_free( block );
return LIBSPECTRUM_ERROR_NONE;
case LIBSPECTRUM_RZX_SNAPSHOT_BLOCK:
libspectrum_snap_free( block->types.snap.snap );
libspectrum_free( block );
return LIBSPECTRUM_ERROR_NONE;
case LIBSPECTRUM_RZX_SIGN_START_BLOCK:
libspectrum_free( block );
return LIBSPECTRUM_ERROR_NONE;
case LIBSPECTRUM_RZX_SIGN_END_BLOCK:
#ifdef HAVE_GCRYPT_H
signature = &( block->types.signature );
gcry_mpi_release( signature->r );
gcry_mpi_release( signature->s );
#endif /* #ifdef HAVE_GCRYPT_H */
libspectrum_free( block );
return LIBSPECTRUM_ERROR_NONE;
case LIBSPECTRUM_RZX_CREATOR_BLOCK:
break;
}
libspectrum_print_error( LIBSPECTRUM_ERROR_LOGIC,
"unknown RZX block type %d at %s:%d", block->type,
__FILE__, __LINE__ );
return LIBSPECTRUM_ERROR_LOGIC;
}
static void
block_free_wrapper( gpointer data, gpointer user_data )
{
block_free( data );
}
static gint
find_block( gconstpointer a, gconstpointer b )
{
const rzx_block_t *block = a;
libspectrum_byte id = GPOINTER_TO_INT( b );
return block->type - id;
}
/*
* Main routines
*/
libspectrum_rzx*
libspectrum_rzx_alloc( void )
{
libspectrum_rzx *rzx = libspectrum_new( libspectrum_rzx, 1 );
rzx->blocks = NULL;
rzx->current_block = NULL;
rzx->current_input = NULL;
rzx->signed_start = NULL;
return rzx;
}
void
libspectrum_rzx_start_input( libspectrum_rzx *rzx, libspectrum_dword tstates )
{
rzx_block_t *block;
block_alloc( &block, LIBSPECTRUM_RZX_INPUT_BLOCK );
rzx->current_input = &( block->types.input );
rzx->current_input->tstates = tstates;
rzx->current_input->frames = NULL;
rzx->current_input->allocated = 0;
rzx->current_input->count = 0;
rzx->current_input->non_repeat = 0;
rzx->blocks = g_slist_append( rzx->blocks, block );
}
libspectrum_error
libspectrum_rzx_stop_input( libspectrum_rzx *rzx )
{
rzx->current_input = NULL;
return LIBSPECTRUM_ERROR_NONE;
}
libspectrum_error
libspectrum_rzx_add_snap( libspectrum_rzx *rzx, libspectrum_snap *snap, int automatic )
{
rzx_block_t *block;
libspectrum_rzx_stop_input( rzx );
block_alloc( &block, LIBSPECTRUM_RZX_SNAPSHOT_BLOCK );
block->types.snap.snap = snap;
block->types.snap.automatic = automatic;
rzx->blocks = g_slist_append( rzx->blocks, block );
return LIBSPECTRUM_ERROR_NONE;
}
libspectrum_error
libspectrum_rzx_rollback( libspectrum_rzx *rzx, libspectrum_snap **snap )
{
GSList *previous, *list;
rzx_block_t *block;
/* Find the last snapshot block in the file */
previous = NULL; list = rzx->blocks;
while( 1 ) {
list =
g_slist_find_custom( list,
GINT_TO_POINTER( LIBSPECTRUM_RZX_SNAPSHOT_BLOCK ),
find_block );
if( !list ) break;
previous = list;
list = list->next;
}
if( !previous ) {
libspectrum_print_error( LIBSPECTRUM_ERROR_CORRUPT,
"no snapshot block found in recording" );
return LIBSPECTRUM_ERROR_CORRUPT;
}
if( rzx->current_input ) {
libspectrum_rzx_stop_input( rzx );
}
/* Delete all blocks after the snapshot */
g_slist_foreach( previous->next, block_free_wrapper, NULL );
previous->next = NULL;
block = previous->data;
*snap = block->types.snap.snap;
return LIBSPECTRUM_ERROR_NONE;
}
libspectrum_error
libspectrum_rzx_rollback_to( libspectrum_rzx *rzx, libspectrum_snap **snap,
size_t which )
{
GSList *previous = NULL, *list;
rzx_block_t *block;
size_t i;
/* Find the nth snapshot block in the file */
for( i = 0, list = rzx->blocks; i <= which; i++, list = list->next ) {
list =
g_slist_find_custom( list,
GINT_TO_POINTER( LIBSPECTRUM_RZX_SNAPSHOT_BLOCK ),
find_block );
if( !list ) {
libspectrum_print_error( LIBSPECTRUM_ERROR_CORRUPT,
"snapshot block %lu not found in recording",
(unsigned long)which );
return LIBSPECTRUM_ERROR_CORRUPT;
}
previous = list;
}
if( rzx->current_input ) {
libspectrum_rzx_stop_input( rzx );
}
/* Delete all blocks after the snapshot */
g_slist_foreach( previous->next, block_free_wrapper, NULL );
previous->next = NULL;
block = previous->data;
*snap = block->types.snap.snap;
return LIBSPECTRUM_ERROR_NONE;
}
static libspectrum_error
input_block_resize( input_block_t *input, size_t new_count )
{
libspectrum_rzx_frame_t *ptr;
size_t new_allocated;
/* Get more space if we need it; allocate twice as much as we currently
have, with a minimum of 50 */
if( new_count > input->allocated ) {
new_allocated = input->allocated >= 25 ? 2 * input->allocated : 50;
if( new_allocated < new_count ) new_allocated = new_count;
ptr = libspectrum_renew( libspectrum_rzx_frame_t, input->frames,
new_allocated );
if( !ptr ) return LIBSPECTRUM_ERROR_MEMORY;
input->frames = ptr;
input->allocated = new_allocated;
}
return LIBSPECTRUM_ERROR_NONE;
}
libspectrum_error
libspectrum_rzx_store_frame( libspectrum_rzx *rzx, size_t instructions,
size_t count, libspectrum_byte *in_bytes )
{
input_block_t *input;
libspectrum_rzx_frame_t *frame;
libspectrum_error error;
input = rzx->current_input;
/* Check we've got an IRB to record to */
if( !input ) {
libspectrum_print_error(
LIBSPECTRUM_ERROR_INVALID,
"libspectrum_rzx_store_frame called with no active input block"
);
return LIBSPECTRUM_ERROR_INVALID;
}
/* Get more space if we need it */
if( input->allocated == input->count ) {
error = input_block_resize( input, input->count + 1 );
if( error ) return error;
}
frame = &input->frames[ input->count ];
frame->instructions = instructions;
/* Check for repeated frames */
if( input->count != 0 && count != 0 &&
count == input->frames[ input->non_repeat ].count &&
!memcmp( in_bytes, input->frames[ input->non_repeat ].in_bytes,
count )
) {
frame->repeat_last = 1;
frame->count = 0;
frame->in_bytes = NULL;
} else {
frame->repeat_last = 0;
frame->count = count;
/* Note this as the last non-repeated frame */
input->non_repeat = input->count;
if( count ) {
frame->in_bytes = libspectrum_new( libspectrum_byte, count );
memcpy( frame->in_bytes, in_bytes,
count * sizeof( *( frame->in_bytes ) ) );
} else {
frame->in_bytes = NULL;
}
}
/* Move along to the next frame */
input->count++;
return 0;
}
libspectrum_error
libspectrum_rzx_start_playback( libspectrum_rzx *rzx, int which,
libspectrum_snap **snap )
{
GSList *list, *previous;
rzx_block_t *block;
int i;
*snap = NULL;
for( i = which, previous = NULL, list = rzx->blocks;
list;
previous = list, list = list->next ) {
block = list->data;
/* Skip any blocks which aren't input recording blocks */
if( block->type != LIBSPECTRUM_RZX_INPUT_BLOCK ) continue;
/* Skip input recording blocks until we find the one we want */
if( i-- ) continue;
rzx->current_block = list;
rzx->current_input = &( block->types.input );
rzx->current_frame = 0; rzx->in_count = 0;
rzx->data_frame = rzx->current_input->frames;
/* If the previous frame was a snap, return that as well */
if( previous ) {
block = previous->data;
if( block->type == LIBSPECTRUM_RZX_SNAPSHOT_BLOCK )
*snap = block->types.snap.snap;
}
return LIBSPECTRUM_ERROR_NONE;
}
libspectrum_print_error(
LIBSPECTRUM_ERROR_INVALID,
"libspectrum_rzx_start_playback: input recording block %d does not exist",
which
);
return LIBSPECTRUM_ERROR_INVALID;
}
libspectrum_error
libspectrum_rzx_playback_frame( libspectrum_rzx *rzx, int *finished,
libspectrum_snap **snap )
{
*snap = NULL;
*finished = 0;
/* Check we read the correct number of INs during this frame */
if( rzx->in_count != rzx->data_frame->count ) {
libspectrum_print_error(
LIBSPECTRUM_ERROR_CORRUPT,
"libspectrum_rzx_playback_frame: wrong number of INs in frame %lu: expected %lu, got %lu",
(unsigned long)rzx->current_frame,
(unsigned long)rzx->data_frame->count, (unsigned long)rzx->in_count
);
return LIBSPECTRUM_ERROR_CORRUPT;
}
/* Increment the frame count and see if we've finished with this file */
if( ++rzx->current_frame >= rzx->current_input->count ) {
GSList *it = rzx->current_block->next;
rzx->current_block = NULL;
for( ; it; it = it->next ) {
rzx_block_t *block = it->data;
if( block->type == LIBSPECTRUM_RZX_INPUT_BLOCK ) {
rzx->current_block = it;
break;
} else if( block->type == LIBSPECTRUM_RZX_SNAPSHOT_BLOCK ) {
*snap = block->types.snap.snap;
}
}
if( rzx->current_block ) {
rzx_block_t *block = rzx->current_block->data;
rzx->current_input = &( block->types.input );
rzx->current_frame = 0; rzx->in_count = 0;
rzx->data_frame = rzx->current_input->frames;
} else {
*finished = 1;
}
return LIBSPECTRUM_ERROR_NONE;
}
/* Move the data frame pointer along, unless we're supposed to be
repeating the last frame */
if( !rzx->current_input->frames[ rzx->current_frame ].repeat_last )
rzx->data_frame = &rzx->current_input->frames[ rzx->current_frame ];
/* And start with the first byte of the new frame */
rzx->in_count = 0;
return LIBSPECTRUM_ERROR_NONE;
}
libspectrum_error
libspectrum_rzx_playback( libspectrum_rzx *rzx, libspectrum_byte *byte )
{
/* Check we're not trying to read off the end of the array */
if( rzx->in_count >= rzx->data_frame->count ) {
libspectrum_print_error(
LIBSPECTRUM_ERROR_CORRUPT,
"libspectrum_rzx_playback: more INs during frame %lu than stored in RZX file (%lu)",
(unsigned long)rzx->current_frame, (unsigned long)rzx->data_frame->count
);
return LIBSPECTRUM_ERROR_CORRUPT;
}
*byte = rzx->data_frame->in_bytes[ rzx->in_count++ ];
return LIBSPECTRUM_ERROR_NONE;
}
libspectrum_error
libspectrum_rzx_free( libspectrum_rzx *rzx )
{
g_slist_foreach( rzx->blocks, block_free_wrapper, NULL );
g_slist_free( rzx->blocks );
libspectrum_free( rzx );
return LIBSPECTRUM_ERROR_NONE;
}
size_t
libspectrum_rzx_tstates( libspectrum_rzx *rzx )
{
return rzx->current_input->tstates;
}
size_t
libspectrum_rzx_instructions( libspectrum_rzx *rzx )
{
return rzx->current_input->frames[ rzx->current_frame ].instructions;
}
libspectrum_dword
libspectrum_rzx_get_keyid( libspectrum_rzx *rzx )
{
GSList *list;
rzx_block_t *block;
list =
g_slist_find_custom( rzx->blocks,
GINT_TO_POINTER( LIBSPECTRUM_RZX_SIGN_START_BLOCK ),
find_block );
if( !list ) return 0;
block = list->data;
return block->types.keyid;
}
libspectrum_error
libspectrum_rzx_get_signature( libspectrum_rzx *rzx,
libspectrum_signature *signature )
{
GSList *list;
rzx_block_t *block;
signature_block_t *sigblock;
list =
g_slist_find_custom( rzx->blocks,
GINT_TO_POINTER( LIBSPECTRUM_RZX_SIGN_END_BLOCK ),
find_block );
if( !list ) {
libspectrum_print_error( LIBSPECTRUM_ERROR_CORRUPT,
"no end of signed data block found" );
return LIBSPECTRUM_ERROR_CORRUPT;
}
block = list->data;
sigblock = &( block->types.signature );
signature->start = rzx->signed_start;
signature->length = sigblock->length;
#ifdef HAVE_GCRYPT_H
signature->r = gcry_mpi_copy( sigblock->r );
signature->s = gcry_mpi_copy( sigblock->s );
#endif /* #ifdef HAVE_GCRYPT_H */
return LIBSPECTRUM_ERROR_NONE;
}
libspectrum_error
libspectrum_rzx_read( libspectrum_rzx *rzx, const libspectrum_byte *buffer,
size_t length )
{
libspectrum_error error;
const libspectrum_byte *ptr, *end;
libspectrum_byte *new_buffer;
libspectrum_id_t raw_type;
libspectrum_class_t class;
/* Find out if this file needs decompression */
new_buffer = NULL;
error = libspectrum_identify_file_raw( &raw_type, NULL, buffer, length );
if( error ) return error;
error = libspectrum_identify_class( &class, raw_type );
if( error ) return error;
if( class == LIBSPECTRUM_CLASS_COMPRESSED ) {
size_t new_length;
error = libspectrum_uncompress_file( &new_buffer, &new_length, NULL,
raw_type, buffer, length, NULL );
if( error ) return error;
buffer = new_buffer; length = new_length;
}
ptr = buffer; end = buffer + length;
error = rzx_read_header( &ptr, end );
if( error != LIBSPECTRUM_ERROR_NONE ) { libspectrum_free( new_buffer ); return error; }
rzx->signed_start = ptr;
while( ptr < end ) {
libspectrum_byte id;
id = *ptr++;
switch( id ) {
case LIBSPECTRUM_RZX_CREATOR_BLOCK:
error = rzx_read_creator( &ptr, end );
if( error != LIBSPECTRUM_ERROR_NONE ) {
libspectrum_free( new_buffer );
return error;
}
break;
case LIBSPECTRUM_RZX_SNAPSHOT_BLOCK:
error = rzx_read_snapshot( rzx, &ptr, end );
if( error != LIBSPECTRUM_ERROR_NONE ) {
libspectrum_free( new_buffer );
return error;
}
break;
case LIBSPECTRUM_RZX_INPUT_BLOCK:
error = rzx_read_input( rzx, &ptr, end );
if( error != LIBSPECTRUM_ERROR_NONE ) {
libspectrum_free( new_buffer );
return error;
}
break;
case LIBSPECTRUM_RZX_SIGN_START_BLOCK:
error = rzx_read_sign_start( rzx, &ptr, end );
if( error != LIBSPECTRUM_ERROR_NONE ) {
libspectrum_free( new_buffer );
return error;
}
break;
case LIBSPECTRUM_RZX_SIGN_END_BLOCK:
error = rzx_read_sign_end( rzx, &ptr, end );
if( error != LIBSPECTRUM_ERROR_NONE ) {
libspectrum_free( new_buffer );
return error;
}
break;
default:
libspectrum_print_error(
LIBSPECTRUM_ERROR_UNKNOWN,
"libspectrum_rzx_read: unknown RZX block ID 0x%02x", id
);
libspectrum_free( new_buffer );
return LIBSPECTRUM_ERROR_UNKNOWN;
}
}
libspectrum_free( new_buffer );
return LIBSPECTRUM_ERROR_NONE;
}
static libspectrum_error
rzx_read_header( const libspectrum_byte **ptr, const libspectrum_byte *end )
{
/* Check the header exists */
if( end - (*ptr) < (ptrdiff_t)strlen( rzx_signature ) + 6 ) {
libspectrum_print_error( LIBSPECTRUM_ERROR_CORRUPT,
"rzx_read_header: not enough data in buffer" );
return LIBSPECTRUM_ERROR_CORRUPT;
}
/* Check the RZX signature exists */
if( memcmp( *ptr, rzx_signature, strlen( rzx_signature ) ) ) {
libspectrum_print_error( LIBSPECTRUM_ERROR_SIGNATURE,
"rzx_read_header: RZX signature not found" );
return LIBSPECTRUM_ERROR_SIGNATURE;
}
/* Skip over the signature and the version numbers */
(*ptr) += strlen( rzx_signature ) + 2;
/* And skip the flags as well - separate call as we'll need the return
value if we ever fix the stuff below */
libspectrum_read_dword( ptr );
/* FIXME: how to handle signatures */
/* This is where the signed data starts (if it's signed at all) */
/* if( signature && ( flags & 0x01 ) ) signature->start = *ptr; */
return LIBSPECTRUM_ERROR_NONE;
}
static libspectrum_error
rzx_read_creator( const libspectrum_byte **ptr, const libspectrum_byte *end )
{
size_t length;
/* Check we've got enough data for the block */
if( end - (*ptr) < 28 ) {
libspectrum_print_error( LIBSPECTRUM_ERROR_CORRUPT,
"rzx_read_creator: not enough data in buffer" );
return LIBSPECTRUM_ERROR_CORRUPT;
}
/* Get the length */
length = libspectrum_read_dword( ptr );
/* Check there's still enough data (the -5 is because we've already read
the block ID and the length) */
if( end - (*ptr) < (ptrdiff_t)length - 5 ) {
libspectrum_print_error( LIBSPECTRUM_ERROR_CORRUPT,
"rzx_read_creator: not enough data in buffer" );
return LIBSPECTRUM_ERROR_CORRUPT;
}
(*ptr) += length - 5;
return LIBSPECTRUM_ERROR_NONE;
}
static libspectrum_error
rzx_read_snapshot( libspectrum_rzx *rzx, const libspectrum_byte **ptr,
const libspectrum_byte *end )
{
rzx_block_t *block;
libspectrum_snap *snap;
size_t blocklength, snaplength;
libspectrum_error error = LIBSPECTRUM_ERROR_NONE;
libspectrum_dword flags;
const libspectrum_byte *snap_ptr;
int done;
snapshot_string_t *type;
/* For deflated snapshot data: */
int compressed;
libspectrum_byte *gzsnap = NULL; size_t uncompressed_length = 0;
if( end - (*ptr) < 16 ) {
libspectrum_print_error( LIBSPECTRUM_ERROR_CORRUPT,
"rzx_read_snapshot: not enough data in buffer" );
return LIBSPECTRUM_ERROR_CORRUPT;
}
blocklength = libspectrum_read_dword( ptr );
if( end - (*ptr) < (ptrdiff_t)blocklength - 5 ) {
libspectrum_print_error( LIBSPECTRUM_ERROR_CORRUPT,
"rzx_read_snapshot: not enough data in buffer" );
return LIBSPECTRUM_ERROR_CORRUPT;
}
/* See if we want a compressed snap */
flags = libspectrum_read_dword( ptr );
/* We don't handle 'links' to external snapshots. I really think these
are just more trouble than they're worth */
if( flags & 0x01 ) {
/* TODO log error if this isn't the first block as that can't be easily
covered by prompting the user for a snapshot file (but should be
unlikely to encounter in reality?) */
(*ptr) += blocklength - 9;
return LIBSPECTRUM_ERROR_NONE;
}
/* Do we have a compressed snapshot? */
compressed = flags & 0x02;
/* How long is the (uncompressed) snap? */
(*ptr) += 4;
snaplength = libspectrum_read_dword( ptr );
(*ptr) -= 8;
/* If compressed, uncompress the data */
if( compressed ) {
#ifdef HAVE_ZLIB_H
error = libspectrum_zlib_inflate( (*ptr) + 8, blocklength - 17,
&gzsnap, &uncompressed_length );
if( error != LIBSPECTRUM_ERROR_NONE ) return error;
if( uncompressed_length != snaplength ) {
libspectrum_print_error(
LIBSPECTRUM_ERROR_CORRUPT,
"rzx_read_snapshot: compressed snapshot has wrong length"
);
libspectrum_free( gzsnap );
return LIBSPECTRUM_ERROR_CORRUPT;
}
snap_ptr = gzsnap;
#else /* #ifdef HAVE_ZLIB_H */
libspectrum_print_error(
LIBSPECTRUM_ERROR_UNKNOWN,
"rzx_read_snapshot: zlib needed for decompression\n"
);
return LIBSPECTRUM_ERROR_UNKNOWN;
#endif /* #ifdef HAVE_ZLIB_H */
} else {
/* If not compressed, check things are consistent */
if( blocklength != snaplength + 17 ) {
libspectrum_print_error(
LIBSPECTRUM_ERROR_CORRUPT,
"rzx_read_snapshot: inconsistent snapshot lengths"
);
return LIBSPECTRUM_ERROR_CORRUPT;
}
snap_ptr = (*ptr) + 8;
uncompressed_length = snaplength;
}
block_alloc( &block, LIBSPECTRUM_RZX_SNAPSHOT_BLOCK );
block->types.snap.snap = libspectrum_snap_alloc();
block->types.snap.automatic = 0;
snap = block->types.snap.snap;
for( done = 0, type = snapshot_strings; type->format; type++ ) {
if( !strncasecmp( (char*)*ptr, type->string, 4 ) ) {
error = libspectrum_snap_read( snap, snap_ptr, uncompressed_length,
type->format, NULL );
done = 1;
}
}
if( !done ) {
libspectrum_print_error(
LIBSPECTRUM_ERROR_UNKNOWN,
"%s:rzx_read_snapshot: unrecognised snapshot format", __FILE__
);
if( compressed ) libspectrum_free( gzsnap );
block_free( block );
return LIBSPECTRUM_ERROR_UNKNOWN;
}
if( error != LIBSPECTRUM_ERROR_NONE ) {
if( compressed ) libspectrum_free( gzsnap );
block_free( block );
return error;
}
/* Free the decompressed data (if we created it) */
if( compressed ) libspectrum_free( gzsnap );
/* Skip over the data */
(*ptr) += blocklength - 9;
rzx->blocks = g_slist_append( rzx->blocks, block );
return LIBSPECTRUM_ERROR_NONE;
}
static libspectrum_error
rzx_read_input( libspectrum_rzx *rzx,
const libspectrum_byte **ptr, const libspectrum_byte *end )
{
size_t blocklength;
libspectrum_dword flags; int compressed;
libspectrum_error error;
rzx_block_t *rzx_block;
input_block_t *block;
/* Check we've got enough data for the block */
if( end - (*ptr) < 18 ) {
libspectrum_print_error( LIBSPECTRUM_ERROR_CORRUPT,
"rzx_read_input: not enough data in buffer" );
return LIBSPECTRUM_ERROR_CORRUPT;
}
block_alloc( &rzx_block, LIBSPECTRUM_RZX_INPUT_BLOCK );
block = &( rzx_block->types.input );
/* Get the length and number of frames */
blocklength = libspectrum_read_dword( ptr );
block->count = libspectrum_read_dword( ptr );
/* Frame size is undefined, so just skip it */
(*ptr)++;
/* Allocate memory for the frames */
block->frames = libspectrum_new( libspectrum_rzx_frame_t, block->count );
block->allocated = block->count;
/* Fetch the T-state counter and the flags */
block->tstates = libspectrum_read_dword( ptr );
flags = libspectrum_read_dword( ptr );
compressed = flags & 0x02;
if( compressed ) {
#ifdef HAVE_ZLIB_H
libspectrum_byte *data; const libspectrum_byte *data_ptr;
size_t data_length = 0;
/* Discount the block intro */
blocklength -= 18;
/* Check that we've got enough compressed data */
if( end - (*ptr) < (ptrdiff_t)blocklength ) {
libspectrum_print_error( LIBSPECTRUM_ERROR_CORRUPT,
"rzx_read_input: not enough data in buffer" );
libspectrum_free( rzx_block );
return LIBSPECTRUM_ERROR_CORRUPT;
}
error = libspectrum_zlib_inflate( *ptr, blocklength, &data, &data_length );
if( error != LIBSPECTRUM_ERROR_NONE ) {
block_free( rzx_block );
return error;
}
*ptr += blocklength;
data_ptr = data;
error = rzx_read_frames( block, &data_ptr, data + data_length );
if( error ) { libspectrum_free( rzx_block ); libspectrum_free( data ); return error; }
libspectrum_free( data );
#else /* #ifdef HAVE_ZLIB_H */
libspectrum_print_error( LIBSPECTRUM_ERROR_UNKNOWN,
"rzx_read_input: zlib needed for decompression" );
libspectrum_free( rzx_block );
return LIBSPECTRUM_ERROR_UNKNOWN;
#endif /* #ifdef HAVE_ZLIB_H */
} else { /* Data not compressed */
error = rzx_read_frames( block, ptr, end );
if( error ) { libspectrum_free( rzx_block ); return error; }
}
rzx->blocks = g_slist_append( rzx->blocks, rzx_block );
return LIBSPECTRUM_ERROR_NONE;
}
static libspectrum_error
rzx_read_frames( input_block_t *block, const libspectrum_byte **ptr,
const libspectrum_byte *end )
{
size_t i, j;
/* And read in the frames */
for( i=0; i < block->count; i++ ) {
/* Check the two length bytes exist */
if( end - (*ptr) < 4 ) {
libspectrum_print_error( LIBSPECTRUM_ERROR_CORRUPT,
"rzx_read_frames: not enough data in buffer" );
for( j=0; j<i; j++ ) {
if( !block->frames[i].repeat_last ) libspectrum_free( block->frames[j].in_bytes );
}
return LIBSPECTRUM_ERROR_CORRUPT;
}
block->frames[i].instructions = libspectrum_read_word( ptr );
block->frames[i].count = libspectrum_read_word( ptr );
if( block->frames[i].count == libspectrum_rzx_repeat_frame ) {
block->frames[i].repeat_last = 1;
continue;
}
block->frames[i].repeat_last = 0;
if( end - (*ptr) < (ptrdiff_t)block->frames[i].count ) {
libspectrum_print_error( LIBSPECTRUM_ERROR_CORRUPT,
"rzx_read_frames: not enough data in buffer" );
for( j=0; j<i; j++ ) {
if( !block->frames[i].repeat_last ) libspectrum_free( block->frames[j].in_bytes );
}
return LIBSPECTRUM_ERROR_CORRUPT;
}
if( block->frames[i].count ) {
block->frames[i].in_bytes =
libspectrum_new( libspectrum_byte, block->frames[i].count );
memcpy( block->frames[i].in_bytes, *ptr, block->frames[i].count );
} else {
block->frames[i].in_bytes = NULL;
}
(*ptr) += block->frames[i].count;
}
return LIBSPECTRUM_ERROR_NONE;
}
static libspectrum_error
rzx_read_sign_start( libspectrum_rzx *rzx, const libspectrum_byte **ptr,
const libspectrum_byte *end )
{
rzx_block_t *block;
libspectrum_dword length;
/* Check we've got enough data for the length */
if( end - (*ptr) < 4 ) {
libspectrum_print_error(
LIBSPECTRUM_ERROR_CORRUPT,
"rzx_read_sign_start: not enough data in buffer"
);
return LIBSPECTRUM_ERROR_CORRUPT;
}
length = libspectrum_read_dword( ptr );
/* Check the length is at least the expected 13 bytes */
if( length < 13 ) {
libspectrum_print_error(
LIBSPECTRUM_ERROR_CORRUPT,
"rzx_read_sign_start: block length %lu less than the minimum 13 bytes",
(unsigned long)length
);
return LIBSPECTRUM_ERROR_CORRUPT;
}
/* Check there's still enough data (the -5 is because we've already read
the length and the block ID) */
if( end - (*ptr) < (ptrdiff_t)length - 5 ) {
libspectrum_print_error(
LIBSPECTRUM_ERROR_CORRUPT,
"rzx_read_sign_start: not enough data in buffer"
);
return LIBSPECTRUM_ERROR_CORRUPT;
}
block_alloc( &block, LIBSPECTRUM_RZX_SIGN_START_BLOCK );
block->types.keyid = libspectrum_read_dword( ptr );
/* Skip the week code */
*ptr += 4;
/* Skip anything we don't know about */
*ptr += length - 13;
rzx->blocks = g_slist_append( rzx->blocks, block );
return LIBSPECTRUM_ERROR_NONE;
}
static libspectrum_error
rzx_read_sign_end( libspectrum_rzx *rzx, const libspectrum_byte **ptr,
const libspectrum_byte *end )
{
rzx_block_t *block;
signature_block_t *signature;
size_t length;
/* Check we've got enough data for the length */
if( end - (*ptr) < 4 ) {
libspectrum_print_error( LIBSPECTRUM_ERROR_CORRUPT,
"rzx_read_sign_end: not enough data in buffer" );
return LIBSPECTRUM_ERROR_CORRUPT;
}
/* Get the length; the -5 is because we've read the block ID and the length
bytes */
length = libspectrum_read_dword( ptr ) - 5;
/* Check there's still enough data */
if( end - (*ptr) < (ptrdiff_t)length ) {
libspectrum_print_error( LIBSPECTRUM_ERROR_CORRUPT,
"rzx_read_sign_end: not enough data in buffer" );
return LIBSPECTRUM_ERROR_CORRUPT;
}
block_alloc( &block, LIBSPECTRUM_RZX_SIGN_END_BLOCK );
signature = &( block->types.signature );
/* - 5 as we don't sign the block ID and length of this block */
signature->length = ( *ptr - rzx->signed_start ) - 5;
#ifdef HAVE_GCRYPT_H
{
gcry_error_t error; size_t mpi_length;
error = gcry_mpi_scan( &signature->r, GCRYMPI_FMT_PGP, *ptr, length,
&mpi_length );
if( error ) {
libspectrum_print_error( LIBSPECTRUM_ERROR_CORRUPT,
"error reading 'r': %s",
gcry_strerror( error ) );
libspectrum_free( block );
return LIBSPECTRUM_ERROR_CORRUPT;
}
(*ptr) += mpi_length; length -= mpi_length;
error = gcry_mpi_scan( &signature->s, GCRYMPI_FMT_PGP, *ptr, length,
&mpi_length );
if( error ) {
libspectrum_print_error( LIBSPECTRUM_ERROR_CORRUPT,
"error reading 's': %s",
gcry_strerror( error ) );
gcry_mpi_release( signature->r );
libspectrum_free( block );
return LIBSPECTRUM_ERROR_CORRUPT;
}
(*ptr) += mpi_length; length -= mpi_length;
}
#endif /* #ifdef HAVE_GCRYPT_H */
(*ptr) += length;
rzx->blocks = g_slist_append( rzx->blocks, block );
return LIBSPECTRUM_ERROR_NONE;
}
libspectrum_error
libspectrum_rzx_write( libspectrum_byte **buffer, size_t *length,
libspectrum_rzx *rzx, libspectrum_id_t snap_format,
libspectrum_creator *creator, int compress,
libspectrum_rzx_dsa_key *key )
{
libspectrum_error error;
GSList *list;
libspectrum_byte *ptr = *buffer;
libspectrum_buffer *new_buffer = libspectrum_buffer_alloc();
libspectrum_buffer *block_data = libspectrum_buffer_alloc();
if( creator ) rzx_write_creator( new_buffer, block_data, creator );
if( key ) {
error = rzx_write_signed_start( new_buffer, block_data, key, creator );
if( error != LIBSPECTRUM_ERROR_NONE ) {
libspectrum_buffer_free( new_buffer );
libspectrum_buffer_free( block_data );
return error;
}
}
for( list = rzx->blocks; list; list = list->next ) {
rzx_block_t *block = list->data;
switch( block->type ) {
case LIBSPECTRUM_RZX_SNAPSHOT_BLOCK:
error = rzx_write_snapshot( new_buffer, block_data,
block->types.snap.snap, snap_format, creator,
compress );
if( error != LIBSPECTRUM_ERROR_NONE ) {
libspectrum_buffer_free( new_buffer );
libspectrum_buffer_free( block_data );
return error;
}
break;
case LIBSPECTRUM_RZX_INPUT_BLOCK:
/* z80 snapshots can't safely store an intermediate state */
snap_format = LIBSPECTRUM_ID_SNAPSHOT_SZX;
rzx_write_input( &( block->types.input ), new_buffer, block_data,
compress );
break;
case LIBSPECTRUM_RZX_CREATOR_BLOCK:
case LIBSPECTRUM_RZX_SIGN_START_BLOCK:
case LIBSPECTRUM_RZX_SIGN_END_BLOCK:
break;
}
}
if( key ) {
error = rzx_write_signed_end( new_buffer, block_data, key );
if( error != LIBSPECTRUM_ERROR_NONE ) {
libspectrum_buffer_free( new_buffer );
libspectrum_buffer_free( block_data );
return error;
}
}
rzx_write_header( block_data, key ? 1 : 0 );
libspectrum_buffer_append( buffer, length, &ptr, block_data );
libspectrum_buffer_free( block_data );
libspectrum_buffer_append( buffer, length, &ptr, new_buffer );
libspectrum_buffer_free( new_buffer );
return LIBSPECTRUM_ERROR_NONE;
}
static void
rzx_write_header( libspectrum_buffer *buffer, int sign )
{
libspectrum_buffer_write( buffer, rzx_signature, strlen( rzx_signature ) );
libspectrum_buffer_write_byte( buffer, 0 ); /* Major version number */
/* Flags */
#ifdef HAVE_GCRYPT_H
/* Minor version number: 12 if we're not signing, 13 if we are */
libspectrum_buffer_write_byte( buffer, sign ? 13 : 12 ); /* Minor version number */
libspectrum_buffer_write_dword( buffer, sign ? 0x01 : 0x00 );
#else /* #ifdef HAVE_GCRYPT_H */
libspectrum_buffer_write_byte( buffer, 12 ); /* Minor version number */
libspectrum_buffer_write_dword( buffer, 0 );
#endif /* #ifdef HAVE_GCRYPT_H */
}
static void
rzx_write_block_header( libspectrum_buffer *dest, libspectrum_buffer *src,
libspectrum_byte type )
{
size_t block_length = libspectrum_buffer_get_data_size( src ) +
sizeof( libspectrum_dword ) + sizeof( libspectrum_byte );
libspectrum_buffer_write_byte( dest, type );
libspectrum_buffer_write_dword( dest, block_length );
libspectrum_buffer_write_buffer( dest, src );
libspectrum_buffer_clear( src );
}
static void
rzx_write_creator( libspectrum_buffer *buffer, libspectrum_buffer *block_data,
libspectrum_creator *creator )
{
size_t custom_length = libspectrum_creator_custom_length( creator );
libspectrum_buffer_write( block_data, libspectrum_creator_program( creator ),
20 );
libspectrum_buffer_write_word( block_data, libspectrum_creator_major( creator ) );
libspectrum_buffer_write_word( block_data, libspectrum_creator_minor( creator ) );
if( custom_length ) {
libspectrum_buffer_write( block_data,
libspectrum_creator_custom( creator ),
custom_length );
}
rzx_write_block_header( buffer, block_data, LIBSPECTRUM_RZX_CREATOR_BLOCK );
}
static libspectrum_error
rzx_compress( libspectrum_buffer *dest, libspectrum_buffer *src, int *compress )
{
#ifdef HAVE_ZLIB_H
libspectrum_byte *out_data = libspectrum_buffer_get_data( src );
size_t out_length = libspectrum_buffer_get_data_size( src );
libspectrum_byte *compressed_data = NULL;
libspectrum_error error;
#endif
if( !(*compress) ) {
libspectrum_buffer_write_buffer( dest, src );
return LIBSPECTRUM_ERROR_NONE;
}
#ifdef HAVE_ZLIB_H
error = libspectrum_zlib_compress( out_data, out_length, &compressed_data,
&out_length );
if( error != LIBSPECTRUM_ERROR_NONE ||
out_length >= libspectrum_buffer_get_data_size( src ) ) {
*compress = 0;
/* We couldn't compress the data, so just copy the uncompressed data
across instead */
libspectrum_buffer_write_buffer( dest, src );
libspectrum_free( compressed_data );
return LIBSPECTRUM_ERROR_NONE;
}
libspectrum_buffer_write( dest, compressed_data, out_length );
libspectrum_free( compressed_data );
return LIBSPECTRUM_ERROR_NONE;
#else /* #ifdef HAVE_ZLIB_H */
libspectrum_print_error( LIBSPECTRUM_ERROR_UNKNOWN,
"rzx_compress: compression needs zlib" );
return LIBSPECTRUM_ERROR_UNKNOWN;
#endif /* #ifdef HAVE_ZLIB_H */
}
static libspectrum_error
rzx_write_snapshot( libspectrum_buffer *buffer, libspectrum_buffer *block_data,
libspectrum_snap *snap, libspectrum_id_t snap_format,
libspectrum_creator *creator, int compress )
{
libspectrum_error error = LIBSPECTRUM_ERROR_NONE;
int flags, done;
snapshot_string_t *type;
libspectrum_buffer *snap_data = libspectrum_buffer_alloc();
size_t uncompressed_data_size;
if( snap_format == LIBSPECTRUM_ID_UNKNOWN ) {
/* If not given a snap format, try using .z80. If that would result
in major information loss, use .szx instead */
snap_format = LIBSPECTRUM_ID_SNAPSHOT_Z80;
error = libspectrum_snap_write_buffer( block_data, &flags, snap,
snap_format, creator, 0 );
if( error ) { goto cleanup; }
if( flags & LIBSPECTRUM_FLAG_SNAPSHOT_MAJOR_INFO_LOSS ) {
libspectrum_buffer_clear( block_data );
snap_format = LIBSPECTRUM_ID_SNAPSHOT_SZX;
error = libspectrum_snap_write_buffer( block_data, &flags, snap,
snap_format, creator, 0 );
if( error ) { goto cleanup; }
}
} else {
error = libspectrum_snap_write_buffer( block_data, &flags, snap,
snap_format, creator, 0 );
if( error ) { goto cleanup; }
}
if( flags & LIBSPECTRUM_FLAG_SNAPSHOT_MAJOR_INFO_LOSS ) {
libspectrum_print_error( LIBSPECTRUM_ERROR_WARNING,
"%s:rzx_write_snapshot: embedded snapshot has lost a significant amount of information",
__FILE__ );
}
uncompressed_data_size = libspectrum_buffer_get_data_size( block_data );
rzx_compress( snap_data, block_data, &compress );
libspectrum_buffer_clear( block_data );
libspectrum_buffer_write_dword( block_data, compress ? 2 : 0 );
for( type = snapshot_strings, done = 0; type->format; type++ ) {
if( type->format == snap_format ) {
libspectrum_buffer_write( block_data, type->string, 4 );
done = 1;
break;
}
}
if( !done ) {
error = LIBSPECTRUM_ERROR_UNKNOWN;
libspectrum_print_error(
error,
"%s:rzx_write_snapshot: unexpected snap type %d", __FILE__, snap_format
);
goto cleanup;
}
/* Uncompressed snapshot length */
libspectrum_buffer_write_dword( block_data, uncompressed_data_size );
libspectrum_buffer_write_buffer( block_data, snap_data );
rzx_write_block_header( buffer, block_data, LIBSPECTRUM_RZX_SNAPSHOT_BLOCK );
cleanup:
libspectrum_buffer_free( snap_data );
return LIBSPECTRUM_ERROR_NONE;
}
static void
rzx_write_input( input_block_t *block, libspectrum_buffer *buffer,
libspectrum_buffer *block_data, int compress )
{
size_t i;
libspectrum_buffer *frame_data = libspectrum_buffer_alloc();
/* Write the frames */
for( i = 0; i < block->count; i++ ) {
libspectrum_rzx_frame_t *frame = &block->frames[i];
libspectrum_buffer_write_word( block_data, frame->instructions );
if( frame->repeat_last ) {
libspectrum_buffer_write_word( block_data, libspectrum_rzx_repeat_frame );
} else {
libspectrum_buffer_write_word( block_data, frame->count );
libspectrum_buffer_write( block_data, frame->in_bytes, frame->count );
}
}
rzx_compress( frame_data, block_data, &compress );
libspectrum_buffer_clear( block_data );
/* How many frames? */
libspectrum_buffer_write_dword( block_data, block->count );
/* Each frame has an undefined length, so write a zero */
libspectrum_buffer_write_byte( block_data, 0 );
/* T-state counter */
libspectrum_buffer_write_dword( block_data, block->tstates );
/* Flags */
libspectrum_buffer_write_dword( block_data, compress ? 0x02 : 0 );
libspectrum_buffer_write_buffer( block_data, frame_data );
rzx_write_block_header( buffer, block_data, LIBSPECTRUM_RZX_INPUT_BLOCK );
libspectrum_buffer_clear( block_data );
libspectrum_buffer_free( frame_data );
}
static libspectrum_error
rzx_write_signed_start( libspectrum_buffer *buffer,
libspectrum_buffer *block_data,
libspectrum_rzx_dsa_key *key,
libspectrum_creator *creator )
{
#ifdef HAVE_GCRYPT_H
/* Key ID */
if( !key || !key->y || strlen( key->y ) < 8 ) {
libspectrum_print_error( LIBSPECTRUM_ERROR_INVALID,
"rzx_write_signed_start: invalid key" );
return LIBSPECTRUM_ERROR_INVALID;
}
libspectrum_buffer_write_dword(
block_data, strtoul( &key->y[ strlen( key->y ) - 8 ], NULL, 16 )
);
/* Week code */
if( creator ) {
libspectrum_buffer_write_dword( block_data,
libspectrum_creator_competition_code( creator ) );
} else {
libspectrum_buffer_write_dword( block_data, 0 );
}
rzx_write_block_header( buffer, block_data,
LIBSPECTRUM_RZX_SIGN_START_BLOCK );
#endif /* #ifdef HAVE_GCRYPT_H */
return LIBSPECTRUM_ERROR_NONE;
}
static libspectrum_error
rzx_write_signed_end( libspectrum_buffer *buffer, libspectrum_buffer *block_data,
libspectrum_rzx_dsa_key *key )
{
#ifdef HAVE_GCRYPT_H
libspectrum_error error;
libspectrum_byte *signature; size_t sig_length;
/* Get the actual signature */
error = libspectrum_sign_data( &signature, &sig_length,
libspectrum_buffer_get_data( buffer ),
libspectrum_buffer_get_data_size( buffer ),
key );
if( error ) return error;
/* Write the signature */
libspectrum_buffer_write( block_data, signature, sig_length );
libspectrum_free( signature );
rzx_write_block_header( buffer, block_data,
LIBSPECTRUM_RZX_SIGN_END_BLOCK );
#endif /* #ifdef HAVE_GCRYPT_H */
return LIBSPECTRUM_ERROR_NONE;
}
void
libspectrum_rzx_insert_snap( libspectrum_rzx *rzx, libspectrum_snap *snap,
int where )
{
rzx_block_t *block;
block_alloc( &block, LIBSPECTRUM_RZX_SNAPSHOT_BLOCK );
block->types.snap.snap = snap;
block->types.snap.automatic = 0;
rzx->blocks = g_slist_insert( rzx->blocks, block, where );
}
/*
* Iterator functions
*/
libspectrum_rzx_iterator
libspectrum_rzx_iterator_begin( libspectrum_rzx *rzx )
{
return rzx->blocks;
}
libspectrum_rzx_iterator
libspectrum_rzx_iterator_next( libspectrum_rzx_iterator it )
{
return it->next;
}
libspectrum_rzx_iterator
libspectrum_rzx_iterator_last( libspectrum_rzx *rzx )
{
/* This function iterates over the whole list */
return g_slist_last( rzx->blocks );
}
libspectrum_rzx_block_id
libspectrum_rzx_iterator_get_type( libspectrum_rzx_iterator it )
{
rzx_block_t *block = it->data;
return block->type;
}
size_t
libspectrum_rzx_iterator_get_frames( libspectrum_rzx_iterator it )
{
rzx_block_t *block = it->data;
if( block->type != LIBSPECTRUM_RZX_INPUT_BLOCK ) return -1;
return block->types.input.count;
}
void
libspectrum_rzx_iterator_delete( libspectrum_rzx *rzx,
libspectrum_rzx_iterator it )
{
block_free( it->data );
rzx->blocks = g_slist_delete_link( rzx->blocks, it );
}
libspectrum_snap*
libspectrum_rzx_iterator_get_snap( libspectrum_rzx_iterator it )
{
rzx_block_t *block = it->data;
if( block->type != LIBSPECTRUM_RZX_SNAPSHOT_BLOCK ) return NULL;
return block->types.snap.snap;
}
int
libspectrum_rzx_iterator_snap_is_automatic( libspectrum_rzx_iterator it )
{
rzx_block_t *block = it->data;
if( block->type != LIBSPECTRUM_RZX_SNAPSHOT_BLOCK ) return 0;
return block->types.snap.automatic;
}
static libspectrum_error
input_block_merge( input_block_t *input, input_block_t *next_input )
{
libspectrum_error error;
/* Get more space if we need it */
if( input->allocated < input->count + next_input->count ) {
error = input_block_resize( input, input->count + next_input->count );
if( error ) return error;
}
/* Note in_bytes are not duplicated */
memcpy( &( input->frames[input->count] ), next_input->frames,
next_input->count * sizeof( libspectrum_rzx_frame_t ) );
input->non_repeat = input->count + next_input->non_repeat;
input->count += next_input->count;
next_input->count = 0; /* don't free reused in_bytes */
return 0;
}
libspectrum_error
libspectrum_rzx_finalise( libspectrum_rzx *rzx )
{
GSList *list, *item, *next_item;
rzx_block_t *block, *next_block;
libspectrum_error error;
int first_snap = 1;
int finalised = 0;
/* Delete interspersed snapshots */
list = rzx->blocks;
while( list ) {
item = list;
block = list->data;
list = list->next;
if( block->type == LIBSPECTRUM_RZX_SNAPSHOT_BLOCK ) {
if( first_snap ) {
first_snap = 0;
} else {
block_free( block );
rzx->blocks = g_slist_delete_link( rzx->blocks, item );
finalised = 1;
}
}
}
/* Merge adjacent input blocks */
list = rzx->blocks;
while( list ) {
block = list->data;
if( block->type == LIBSPECTRUM_RZX_INPUT_BLOCK ) {
next_item = list->next;
if( !next_item ) break;
next_block = next_item->data;
if( next_block->type == LIBSPECTRUM_RZX_INPUT_BLOCK ) {
error = input_block_merge( &( block->types.input ),
&( next_block->types.input ) );
if( error ) return error;
block_free( next_block );
rzx->blocks = g_slist_delete_link( rzx->blocks, next_item );
finalised = 1;
} else {
list = list->next;
}
} else {
list = list->next;
}
}
return finalised? LIBSPECTRUM_ERROR_NONE : LIBSPECTRUM_ERROR_INVALID;
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.