/* SPDX-License-Identifier: GPL-3.0-or-later */ /* Versioned two-slot store. Commit header is written only after all payloads. * A table occupies one sector; only one table and one sector are needed in RAM. */ #ifdef QUAD_HOST #include #else #include #endif #include "store.h" static const QStoreIO *io; static signed char active_slot=-1; static Q32 generation; static Q8 page[256]; Q16 q_store_crc(const Q8 *data,Q16 length) { Q16 crc=0xFFFF;Q8 bit; while(length--){crc^=(Q16)*data++<<8;for(bit=0;bit<8;++bit)crc=(crc&0x8000)?(Q16)((crc<<1)^0x1021):(Q16)(crc<<1);} return crc; } static Q32 get32(const Q8 *p) { #ifdef QUAD_HOST return ((Q32)p[0]<<24)|((Q32)p[1]<<16)|((Q32)p[2]<<8)|p[3]; #else /* CMOC's CoCo target stores unsigned long in big-endian byte order. */ Q32 n;memcpy(&n,p,4);return n; #endif } static void put32(Q8 *p,Q32 n) { #ifdef QUAD_HOST p[0]=(Q8)(n>>24);p[1]=(Q8)(n>>16);p[2]=(Q8)(n>>8);p[3]=(Q8)n; #else memcpy(p,&n,4); #endif } static Q8 checksum_ok(void) { return q_store_crc(page,254)==((Q16)page[254]<<8|page[255]); } static void checksum_finish(void) { Q16 crc=q_store_crc(page,254);page[254]=(Q8)(crc>>8);page[255]=(Q8)crc; } void q_preferences_default(QPreferences *p) { Q8 i;memset(p,0,sizeof(*p));p->style=1;p->sound=1;p->hz=60; for(i=0;i<4;++i)p->centers[i]=31; } static Q8 preferences_ok(void) { static const Q8 maximum[15]={16,1,1,2,1,1,1,1,1,2,60,63,63,63,63}; Q8 i;for(i=0;i<15;++i)if(page[12+i]>maximum[i])return 0; if(page[22]!=50&&page[22]!=60)return 0; return 1; } static void decode_preferences(QPreferences *p) { p->game.difficulty=(signed char)(page[12]-1); memcpy(&p->game.scoring,page+13,2); memcpy(&p->style,page+15,12);p->game.seed=get32(page+27); } static void encode_header(const QPreferences *p,Q32 gen) { memset(page,0,sizeof(page));page[0]='Q';page[1]='P';page[2]='S';page[3]='V'; page[4]=1;page[5]=34;page[6]=8;page[7]=10;put32(page+8,gen); page[12]=(Q8)(p->game.difficulty+1); memcpy(page+13,&p->game.scoring,2);memcpy(page+15,&p->style,12); put32(page+27,p->game.seed);checksum_finish(); } static Q8 table_ok(Q8 category) { Q8 i,j,empty=0;Q32 score,previous=Q_SCORE_MAX; if(page[80]!=category||page[81]!=1||page[82]!=80||!checksum_ok())return 0; for(i=0;i<10;++i){ score=get32(page+i*8); if(score>previous)return 0; previous=score; if(!score){empty=1;for(j=0;j<8;++j)if(page[i*8+j])return 0;} else{ if(empty||page[i*8+7]>1)return 0; for(j=4;j<7;++j)if(page[i*8+j]<'A'||page[i*8+j]>'Z')return 0; } } return 1; } static void encode_table(Q8 category,const QScore *scores) { Q8 i,j;memset(page,0,sizeof(page));page[80]=category;page[81]=1;page[82]=80; if(scores)for(i=0;i<10;++i)if(scores[i].score){ put32(page+i*8,scores[i].score); for(j=0;j<3;++j)page[i*8+4+j]=scores[i].initials[j]; page[i*8+7]=scores[i].rule; } checksum_finish(); } static Q8 validate_slot(Q8 slot,Q32 *gen,QPreferences *prefs) { Q8 status,index; status=io->open(slot,0);if(status)return status; status=io->read(slot,0,page); if(!status&&(page[0]!='Q'||page[1]!='P'||page[2]!='S'||page[3]!='V'||page[4]!=1|| page[5]!=34||page[6]!=8||page[7]!=10||!checksum_ok()||!preferences_ok()))status=Q_STORE_CORRUPT; if(!status){*gen=get32(page+8);decode_preferences(prefs);} for(index=0;!status&&index<34;++index){ status=io->read(slot,index+1,page); if(!status&&!table_ok(index))status=Q_STORE_CORRUPT; } if(io->close(slot)&&!status)status=Q_STORE_IO; return status; } Q8 q_store_load(const QStoreIO *ops,QPreferences *p) { Q8 a,b;Q32 generations[2];QPreferences candidates[2]; io=ops;active_slot=-1;generation=0; a=validate_slot(0,generations,candidates);b=validate_slot(1,generations+1,candidates+1); if(!a||!b){ active_slot=!a&&!b?((Q32)(generations[1]-generations[0])<0x80000000UL&&generations[1]!=generations[0]?1:0):(!a?0:1); generation=generations[active_slot];*p=candidates[active_slot]; return (a>Q_STORE_MISSING||b>Q_STORE_MISSING)?Q_STORE_RECOVERED:Q_STORE_OK; } return a==Q_STORE_MISSING&&b==Q_STORE_MISSING?Q_STORE_OK:Q_STORE_CORRUPT; } Q8 q_store_scores(Q8 category,QScore scores[10]) { Q8 status,i,j;memset(scores,0,sizeof(QScore)*10); if(category>=34)return Q_STORE_CORRUPT; if(active_slot<0)return Q_STORE_OK; status=io->open(active_slot,0);if(status)return status; status=io->read(active_slot,category+1,page); if(!status&&!table_ok(category))status=Q_STORE_CORRUPT; if(!status)for(i=0;i<10;++i){scores[i].score=get32(page+i*8);for(j=0;j<3;++j)scores[i].initials[j]=page[i*8+4+j];scores[i].rule=page[i*8+7];} if(io->close(active_slot)&&!status)status=Q_STORE_IO; return status; } Q8 q_store_save(const QPreferences *prefs,signed char category,const QScore *scores,Q8 reset_all) { Q8 target=active_slot==0?1:0,index,status,source_open=0,target_open=0; Q32 next=generation+1,verified;QPreferences checked; if(category < -1 || category>=34)return Q_STORE_CORRUPT; if(active_slot>=0&&!reset_all){status=io->open(active_slot,0);if(status)return status;source_open=1;} status=io->open(target,1);if(!status)target_open=1; if(!status){memset(page,0,sizeof(page));status=io->write(target,0,page);} for(index=0;!status&&index<34;++index){ if((signed char)index==category){encode_table(index,scores);if(!table_ok(index)){status=Q_STORE_CORRUPT;break;}} else if(reset_all||active_slot<0)encode_table(index,0); else {status=io->read(active_slot,index+1,page);if(!status&&!table_ok(index))status=Q_STORE_CORRUPT;} if(!status)status=io->write(target,index+1,page); } if(!status){encode_header(prefs,next);if(!preferences_ok())status=Q_STORE_CORRUPT;else status=io->write(target,0,page);} if(target_open&&io->close(target)&&!status)status=Q_STORE_IO; if(source_open&&io->close(active_slot)&&!status)status=Q_STORE_IO; if(!status)status=validate_slot(target,&verified,&checked); if(!status&&verified!=next)status=Q_STORE_CORRUPT; if(!status){active_slot=target;generation=next;} return status; } Q8 q_score_insert(QScore scores[10],Q32 score,const char initials[3],Q8 rule) { Q8 i,j; if(!score||rule>1)return 0; for(j=0;j<3;++j)if(initials[j]<'A'||initials[j]>'Z')return 0; for(i=0;i<10&&scores[i].score&&scores[i].score>=score;++i){} if(i==10)return 0; for(j=9;j>i;--j)scores[j]=scores[j-1]; scores[i].score=score;scores[i].rule=rule; for(j=0;j<3;++j)scores[i].initials[j]=initials[j]; return 1; } Q8 q_score_category(signed char difficulty,Q8 difficult) { return (Q8)((difficulty+1)*2+(difficult?1:0)); }