/* Qxw is a program to help construct and publish crosswords. Copyright 2011-2020 Mark Owen; Windows port by Peter Flippant http://www.quinapalus.com E-mail: qxw@quinapalus.com This file is part of Qxw. Qxw is free software: you can redistribute it and/or modify it under the terms of version 2 of the GNU General Public License as published by the Free Software Foundation. Qxw 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 Qxw. If not, see or write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #define PCRE2_CODE_UNIT_WIDTH 8 #include #include // required for string conversion functions #include #include "common.h" #include "qxw.h" #include "gui.h" #include "dicts.h" #include "alphabets.h" // default dictionaries #ifdef _WIN32 // Windows default dictionaries stored in subfolder of {app} folder #define NDEFDICTS 1 char*defdictfn[NDEFDICTS]={ "\\Dictionaries\\UKACD18plus.txt" }; #else #define NDEFDICTS 4 char*defdictfn[NDEFDICTS]={ "/usr/dict/words", "/usr/share/dict/words", "/usr/share/dict/british-english", "/usr/share/dict/american-english"}; #endif // UTILITY FUNCTIONS // utf-8 string to uchar string, printable characters only // there appears to be no efficient cross-platform library function to do this // s points to utf-8 string, ucs to output buffer of length l // return number of output uchars // assumes valid input; output guaranteed 0-terminated if l>1; if l==1 but no recognised utf8 writes 0 int utf8touchars(uchar*ucs,const char*p,int l) { int i,j,l0,n; uchar c; unsigned char*s=(unsigned char*)p; l0=l-1; if(l0==0) l0=1; for(i=0;i=0x20&&*s<0x7f) {ucs[i++]=*s++; continue;} // ASCII-7 printable if(*s<0xc0) {s++; continue;} if(*s<0xe0) {n=1; c=*s&0x1f;} else if(*s<0xf0) {n=2; c=*s&0x0f;} else if(*s<0xf8) {n=3; c=*s&0x07;} else if(*s<0xfc) {n=4; c=*s&0x03;} else if(*s<0xfe) {n=5; c=*s&0x01;} else {s++; continue;} s++; for(j=0;j> 6)+0xc0,n= 0; else if(c< 0x10000U) *q++=(c>>12)+0xe0,n= 1; else if(c< 0x200000U) *q++=(c>>18)+0xf0,n= 2; else if(c< 0x4000000U) *q++=(c>>24)+0xf8,n= 3; else if(c<0x80000000U) *q++=(c>>30)+0xfc,n= 4; else return q; for(;n>=0;n--) *q++=((c>>(n*6))&0x3f)+0x80; *q=0; return q; } // uchar string to utf-8 string, printable characters only; up to 6 bytes per input uchar + zero termination output char*ucharstoutf8(char*q,uchar*s) { *q=0; for(;*s;s++) q=uchartoutf8(q,*s); return q; } // length of a 0-terminated uchar string int ucharslen(uchar*s) { int i; for(i=0;s[i];i++) ; return i; } // number of bytes in UTF-8 char pointed to by p int q_mblen(char*p) { if(*p == '\0') return 0; if(!(*p & 0x80)) return 1; else if((*p & 0xe0) == 0xc0) { if ((*(p+1) & 0xc0) != 0x80) return -1; else return 2; } else if((*p & 0xf0) == 0xe0) { if((*(p+1) & 0xc0) != 0x80) return -1; else if((*(p+2) & 0xc0) != 0x80) return -1; else return 3; } else if((*p & 0xf8) == 0xf0) { if((*(p+1) & 0xc0) != 0x80) return -1; else if((*(p+2) & 0xc0) != 0x80) return -1; else if((*(p+3) & 0xc0) != 0x80) return -1; else return 4; } else if((*p & 0xfc) == 0xf8) { if((*(p+1) & 0xc0) != 0x80) return -1; else if((*(p+2) & 0xc0) != 0x80) return -1; else if((*(p+3) & 0xc0) != 0x80) return -1; else if((*(p+4) & 0xc0) != 0x80) return -1; else return 5; } else if((*p & 0xfe) == 0xfc) { if((*(p+1) & 0xc0) != 0x80) return -1; else if((*(p+2) & 0xc0) != 0x80) return -1; else if((*(p+3) & 0xc0) != 0x80) return -1; else if((*(p+4) & 0xc0) != 0x80) return -1; else if((*(p+5) & 0xc0) != 0x80) return -1; else return 6; } else return -1; } // CHARACTER MAPPING static unsigned char uchartoicctab[MAXUCHAR]; // convert UTF-32 to internal character code uchar icctouchar[MAXICC+1]; // convert internal character code to UTF-32 of canonical representative char icctoutf8[MAXICC+1][16]; // convert internal character code to UTF-8 string of canonical representative, 0-terminated char iccequivs[MAXICC][MAXEQUIV*8+1]; char iccseq[MAXICC+1]; static unsigned char uchartopair[MAXUCHAR]; static uchar pairtouchar[MAXICC][2]; // The following are largely to help with answer treatments etc. // ICCs are divided into three groups: basically alphabetic (0), numeric (1), symbols (2). These occur in that order // in the alphabet specification. Some answer treatments only look at the alphabetic group, or only // (possibly separately) at alphabetic + numeric. int niccused; // number of ICCs actually used char iccused[MAXICC+1]; // list of ICCs actually used int icctousedindex[MAXICC+1]; // convert ICC to index into iccused[] int iccusedtogroup[MAXICC+1]; // convert iccusedindex to group number int icctogroup[MAXICC+1]; // convert ICC to group number, -1 if not used int iccgroupstart[MAXICCGROUP+1]; // starts of groups within iccused[], plus one after the end int iccgroup=0; // in the "pair" case, icctoutf8 contains two characters into which dictionary characters in iccequivs will be expanded uchar ICCtouchar(char c) { // convert internal character code to UTF-32 or 0 if not represented uchar x; if(c<0||c>MAXICC) return 0; if(c==ICC_DASH) return '-'; x=icctouchar[(int)c]; if(x=='?') return 0; return icctouchar[(int)c]; } char uchartoICC(int c) { // convert UTF-32 to internal character code or 0 if not represented if(c<0||c>=MAXUCHAR) return 0; return uchartoicctab[c]; } // convert bitmap to internal character code // return >0: internal character code // -1: bitmap is zero // -2: more than one bit set int abmtoicc(ABM b) { if(b==0) return -1; if(onebit(b)) return logbase2(b)+1; return -2; } int isdisallowed(uchar uc) { if(uc>=MAXUCHAR) return 1; if(!ISUGRAPH(uc)) return 1; if(uc>0x7f) return 0; // all other disallowed characters are 7-bit if(strchr(".-?,[]^\"@#",uc)) return 1; return 0; } ABM abm_vow,abm_con,abm_use; // add an entry to the current alphabet // characters must be added in ascending order of internal code, so that // iccused[] is set correctly for Caesar ciphers etc. // Returns: // 0: OK (including case of empty rep string or bad icc) // 1: disallowed character in rep string (entire entry ignored) // 2: one or more disallowed character(s) in equiv string (character(s) ignored) int addalphamapentry(int icc,char*rep,char*equiv,int vow,int con,int seq) { uchar uc,s0[3],s1[MAXEQUIV+1]; int f,g,i,j; if(icc<1||icc>=MAXICC) return 0; DEB_AL printf("rep=%s equiv=%s\n",rep,equiv); i=utf8touchars(s0,rep,3); // convert rep to UTF-32, up to 2 characters DEB_AL { printf(" rep -> %d char(s):",i); for(j=0;jul,(*(struct answer**)q)->ul); if(u) return u; u=strcmp( (*(struct answer**)p)->cf,(*(struct answer**)q)->cf); return u; } static void clearcounts(int dn) { int i; line=0; dst_lines[dn]=0; dst_lines_f[dn]=0; dst_lines_fa[dn]=0; for(i=0;inext;free(dstrings[d]);dstrings[d]=p;} } void freedicts(void) { // free all memory allocated by loaddicts, even if it aborted mid-load int i; for(i=0;i=MAXUCHAR) continue; c=uchartoICC(u); if(c) { // characters is in basic alphabet; could check for "reject" chars here if(l2>=MXLE) return 0; // too long? strcpy(s1+l1,icctoutf8[c]); l1+=strlen(s1+l1); s2[l2++]=c; continue; } else { c=uchartopair[t[i]]; if(c) { // represents a pair of characters? c0=uchartoICC(pairtouchar[c][0]); if(c0) { if(l2>=MXLE) return 0; // too long? strcpy(s1+l1,icctoutf8[c0]); l1+=strlen(s1+l1); s2[l2++]=c0; } c0=uchartoICC(pairtouchar[c][1]); if(c0) { if(l2>=MXLE) return 0; // too long? strcpy(s1+l1,icctoutf8[c0]); l1+=strlen(s1+l1); s2[l2++]=c0; } } else { if(dst_u_rejline_count[dn][u]++==0) dst_u_rejline[dn][u]=line; // record first instance of rejected Unicode } } } s1[l1]=0; s2[l2]=0; DEB_DV { printf("adddictword: citation=<%s> canonical UTF-8=<%s> canonical ICC=<",s0,s1); printICCs(s2); printf(">\n Unicode:"); for(i=0;s2[i];i++) printf(" U+%06X",icctouchar[(unsigned int)s2[i]]); printf("\n"); } if(l2<1) { // too short? DEB_DV printf(" too short\n"); return 0; } // here s0 contains citation form in UTF-8 // s1 contains canonicalised form in UTF-8, length l1 // s2 contains canonicalised form in internal character code, length l2 1<=l2<=MXLE dst_lines[dn]++; pcremd = pcre2_match_data_create(120, NULL); if(sre) { i=pcre2_match(sre,(PCRE2_SPTR)s0,l0,0,0,pcremd,NULL); DEB_DI if(i<-1) printf("PCRE error %d\n",i); if(i<0) { DEB_DV printf(" failed file filter\n"); pcre2_match_data_free(pcremd); return 0; // failed match } } dst_lines_f[dn]++; if(are) { i=pcre2_match(are,(PCRE2_SPTR)s1,l1,0,0,pcremd,NULL); DEB_DI if(i<-1) printf("PCRE error %d\n",i); if(i<0) { DEB_DV printf(" failed answer filter\n"); pcre2_match_data_free(pcremd); return 0; // failed match } } dst_lines_fa[dn]++; pcre2_match_data_free(pcremd); if(memblkp==NULL||memblkl+2+l0+1+l2+1>MEMBLK) { // allocate more memory if needed (this always happens on first pass round loop) q=(struct memblk*)malloc(sizeof(struct memblk)); if(q==NULL) {return -2;} q->next=NULL; if(memblkp==NULL) dstrings[dn]=q; else memblkp->next=q; // link into list memblkp=q; memblkp->ct=0; memblkl=0; } *(memblkp->s+memblkl++)=(char)floor(f*10.0+128.5); // score with rounding *(memblkp->s+memblkl++)=dn; strcpy(memblkp->s+memblkl,s0);memblkl+=l0+1; // citation form, UTF-8 strcpy(memblkp->s+memblkl,s2);memblkl+=l2+1; // canonical form, internal character code memblkp->ct++; // count words in this memblk return 1; } // read n bytes from fp and interpret as little-endian integer static int getint(FILE*fp,int n) { int i,u; for(i=0,u=0;i=0;j--) { // big-endian... p=hnodep[(m>>j)&1]+n; if(*p==-1) { // node does not exist yet if(nhn>=MXHNODES) return -1; *p=nhn; hnodep[0][nhn]=-1; hnodep[1][nhn]=-1; hnodec[nhn]=-1; nhn++; } n=*p; } hnodec[n]=i; return 0; } // Attempt to load a .TSD file. Return number of words >=0 on success, <0 on error. static int loadtsd(FILE*fp,int format,int dn,pcre2_code*sre,pcre2_code*are) { int c,i,j,l,m,ml,n,u,nw; int hoff[MXLE+1]; // file offsets into Huffman coded block int dcount[MXLE+1]; // number of words of each length char s1[SLEN]; GError*error=NULL; gchar*sp=NULL; DEB_DI printf("attempting to load TSD format=0x%x\n",format); clearcounts(dn); if(format!='0'&&format!='1') return -1; // only TSD0 and TSD1 supported if(fseek(fp,4,SEEK_SET)<0) return -1; ml=getint(fp,2); // maximum length if(ml<1) return -1; DEB_DI printf("ml=%d\n",ml); if(format=='1') getint(fp,12*ml+12); // skip some bytes for(i=0;i0) { DEB_DI { printf("%02x %c ",i,isprint(i)?i:'?'); for(j=l-1;j>=0;j--) printf("%d",(m>>j)&1); printf("\n"); } if(addhcode(m,l,i)<0) return -1; } } nw=0; for(l=1;lMXLE) break; if(hoff[l]==0) continue; if(fseek(fp,hoff[l],SEEK_SET)<0) return -1; DEB_DI printf("starting to read length %d at offset %08x; dstrings[%d]=%p memblkp=%p\n",l,hoff[l],dn,(void*)dstrings[dn],(void*)memblkp); j=8; c=0; for(i=0;i>j)&1); n=hnodep[(c>>j)&1][n]; j++; u=hnodec[n]; if(u!=-1) { DEB_DI printf(" %02x %c\n",u,isprint(u)?u:'?'); if((u>=0x01&&u<0x7f)||(u>=0xa0&&u<=0xff)) { s1[m]=u; if(m=0) {at=i; goto exit;} // successfully read } rewind(fp); if(fgets(bom,5,fp)!=NULL) { // re-read BOM at start of file for(i=0; i0) { if(!(memcmp(bom,fenc[i].bom,fenc[i].lbom))) { mode=i; // BOM found so set mode fseek(fp,fenc[i].lbom,SEEK_SET); // and skip past BOM break; } } } // end of for loop if(mode>1) { // Cannot read this file encoding mode sprintf(t,"Dictionary %d\nCannot read file encoding:\ntry UTF-8 or ISO-8859-1 encoding",dn+1); if(!sil) reperr(t); rc=-1; goto exit; } if(mode==-1) rewind(fp); // No BOM, so read words from start of file } retry: at=0; freedstrings(dn); memblkl=0; // number of bytes stored so far in current memory block memblkp=0; // current memblk being filled dstrings[dn]=0; clearcounts(dn); DEB_DI printf("Reading dictionary file %s (in mode %d), owd=%d\n",dfnames[dn],mode,owd); for(;;) { // Loop through dictionary 'words' until EOF if(owd) strcpy(t,dafilters[dn]); else { if(feof(fp)) break; if(fgets(t,SLEN,fp)==NULL) break; line++; } j=strlen(t)-1; if(j<0) goto skipword; while(j>=0&&t[j]>=0&&t[j]<=' ') t[j--]=0; // Strip control characters/white space from end of string while (j>=0&&((t[j]>='0'&&t[j]<='9')||t[j]=='.'||t[j]=='+'||t[j]=='-')) j--; // get score (if any) from end j++; f=0.0; if(j==0||t[j-1]!=' ') j=strlen(t); // all digits, or no space? treat it as a 'word' else { sscanf(t+j,"%f",&f); if(f>= 10.0) f= 10.0; if(f<=-10.0) f=-10.0; t[j--]=0; // rest of input is treated as a 'word' (which may contain spaces) } while(j>=0&&t[j]>=0&&t[j]<=' ') t[j--]=0; // remove trailing white space j++; if(j<1) goto skipword; // t now contains 'word' for conversion; convert it first to UTF-8 for use as citation form if(mode>-1) { // Use encoding mode based on BOM sp=g_convert(t,-1,"UTF-8",fenc[mode].nenc,NULL,&l0,&error); if(error) { sprintf(t,"Dictionary %d\nFile not encoded in accordance with its BOM",dn+1); if(!sil) reperr(t); rc=-1; goto exit; } } else { // try fallback encodings in turn if(mode==-1) sp=g_convert(t,-1,"UTF-8","UTF-8",NULL,&l0,&error); // effectively a check for valid UTF-8 if(mode==-2) sp=g_convert(t,-1,"UTF-8","ISO-8859-1",NULL,&l0,&error); if(error) { DEB_DI { printf("error: %s\n",error->message); for(i=0;t[i];i++) printf(" %02x",t[i]); printf("\n"); } g_clear_error(&error); freedstrings(dn); rewind(fp); mode--; if(mode>-3) goto retry; // go round again and try next encoding // here if none of the encodings worked sprintf(t,"Dictionary %d\nFile does not use a recognised encoding:\ntry UTF-8 or ISO-8859-1",dn+1); if(!sil) reperr(t); rc=-1; goto exit; } } // here we have UTF-8 citation form of string in sp, length in l0 if(l0>=SLEN) goto skipword; strcpy(s0,sp); g_free(sp),sp=0; i=adddictword(s0,dn,sre,are,f); if(i==-2) {rc=-2; goto exit;} if(i==1) at++; skipword: if(sp) g_free(sp),sp=0; g_clear_error(&error); if(owd) break; } // End of 'word' for loop exit: if(sp) g_free(sp),sp=0; if(sre) pcre2_code_free(sre); if(are) pcre2_code_free(are); g_clear_error(&error); if(!owd) if(fp) fclose(fp); if(rc<0) return rc; else return at; } int loaddicts(int sil) { // load (or reload) dictionaries from dfnames[] // sil=1 suppresses error reporting // returns: 0=success; 1=bad file; 2=no words; 4=out of memory // sets dusedmask to list of dictionaries with anything in them struct answer*ap; struct memblk*p; int at,dn,i,j,k,l,rc,u; char t[SLEN]; unsigned int h; freedicts(); at=0; rc=0; dusedmask=0; for(dn=0;dn0) at+=u,dusedmask|=1<ct;i++) { // loop over all words ans[k].score =pow(10.0,((float)(*(unsigned char*)(p->s+l++))-128.0)/10.0); ans[k].cfdmask= ans[k].dmask =1<<*(unsigned char*)(p->s+l++); ans[k].cf =p->s+l; l+=strlen(p->s+l)+1; ans[k].acf =0; ans[k].ul =p->s+l; l+=strlen(p->s+l)+1; // in internal character code ans[k].banned =0; k++; } p=p->next; } } DEB_DI printf("k=%9d\n",k); assert(k==atotal); for(i=0;iul,ansp[i-1]->ul)) {j++;ap=ansp[j]=ansp[i];} else { ansp[j]->dmask|=ansp[i]->dmask; // union masks ansp[j]->score*=ansp[i]->score; // multiply scores over duplicate entries if(strcmp(ansp[i]->cf,ansp[i-1]->cf)) ap->acf=ansp[i],ap=ansp[i]; // different citation forms? link them together else ap->cfdmask|=ansp[i]->cfdmask; // cf:s the same: union masks } } atotal=j+1; for(i=0;iul[j];j++) h=h*113+ansp[i]->ul[j]; h=h%HTABSZ; ansp[i]->ahlink=ahtab[h]; ahtab[h]=i; } for(i=0;iscore>= 1e10) ansp[i]->score= 1e10; // clamp scores if(ansp[i]->score<=-1e10) ansp[i]->score=-1e10; } for(i=0;iacf==0) continue; ap=ansp[i]; do { // printf(" \"%s\"",ap->cf); ap=ap->acf; } while(ap); // printf("\n"); } DEB_DI printf("Total unique answers by entry: %d\n",atotal); return rc; // return 1 if any file failed to load ew4: freedicts(); if(!sil) reperr("Out of memory loading dictionaries"); return 4; } // Run through some likely candidates for default dictionaries // Return // 0: found something // !=0: nothing found int loaddefdicts() {int i; if(startup_dict1[0]) { // do we have a candidate preferred startup dictionary? strcpy(dfnames[0],startup_dict1); strcpy(dsfilters[0],startup_ff1); strcpy(dafilters[0],startup_af1); loaddicts(1); DEB_DI printf("read preferences default, atotal=%d\n",atotal); if(atotal!=0) return 0; // happy if we found any words at all } for(i=0;iul)) return !!(ansp[p]->dmask&dm); p=ansp[p]->ahlink; } return 0; }