From: H.G. Muller Date: Sun, 28 Feb 2016 14:58:49 +0000 (+0100) Subject: Add forgotten files 1.4.70b X-Git-Url: http://winboard.nl/cgi-bin?a=commitdiff_plain;h=refs%2Fheads%2Fmaster;p=polyglot.git Add forgotten files 1.4.70b The files pgheader.c and .h, which were new in 1.4.70b, had not been added to git yet. --- diff --git a/pgheader.c b/pgheader.c new file mode 100644 index 0000000..cff0d5b --- /dev/null +++ b/pgheader.c @@ -0,0 +1,544 @@ +/* +Copyright (c) 2012, Michel Van den Bergh + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +*/ + +#include "pgheader.h" +#include +#include +#include +#include +#include +#include +#include +#include + +const char * pgheader_version="1.0"; +const char * pgheader_magic="@PG@"; + +static char * errmsg[]={ + "No error.", + "OS error.", + "Badly formatted input file.", + "No header.", + "Input and output file are the same.", + "Bad parameter.", + "Bad header." +}; + +#ifndef WIN32 +#define O_BINARY 0x0 +#endif + +#ifdef _MSC_VER + typedef unsigned __int64 uint64_t; +#else + typedef unsigned long long int uint64_t; +#endif + +static int int_from_file(FILE *f, int l, uint64_t *r){ + int i,c; + for(i=0;i=0){ + for(j=0;j998){ + return PGHEADER_BAD_PARAMETER; + } + } + + + /* Step 2: estimate length */ + + nbheader= + strlen(pgheader_magic)+1 + +strlen(pgheader_version)+1 + +3/*nbpredef*/+1 + +3/*nbvariants*/+1 + +strlen(variants)+1 + +strlen(comment)+1; + + /* Step 3: allocate memory */ + + *header=malloc(nbheader); + if(!(*header)){ + return PGHEADER_OS_ERROR; + } + + /* Step 4: fill header */ + + strcpy(*header,pgheader_magic); + strcat(*header,"\x0a"); + strcat(*header,pgheader_version); + strcat(*header,"\x0a"); + sprintf(*header+strlen(*header),"%d",nbvariants+1); /* predef */ + strcat(*header,"\x0a"); + sprintf(*header+strlen(*header),"%d",nbvariants); + strcat(*header,"\x0a"); + strcat(*header,variants); + strcat(*header,"\x0a"); + strcat(*header,comment); + + return PGHEADER_NO_ERROR; + +} + + +int pgheader_read(char **header, const char *infile){ + int fin; + char buf[16]; + unsigned int nbheader; + unsigned int read_bytes; + + /* initial malloc */ + + nbheader=2048; + *header=malloc(nbheader); + if(!(*header)){ + return PGHEADER_OS_ERROR; + } + read_bytes=0; + + /* open input file */ + + fin=open(infile,O_RDONLY|O_BINARY); + if(fin==-1){ + *header=NULL; + return PGHEADER_OS_ERROR; + } + + + /* read bytes in input file */ + + while(1){ + int j,r; + int zero; + int last; + /* enlarge buffer if necessary */ + if(read_bytes>=nbheader){ + nbheader*=2; + *header=realloc(*header,nbheader); + } + + r=read(fin,buf,16); + if(r<16){ + free(*header); + *header=NULL; + close(fin); + return PGHEADER_BAD_FORMAT; + } + zero=1; + for(j=0;j<8;j++){ + if(buf[j]!=0){ + zero=0; + } + } + if(!zero){ + /* if we encounter a non null record here it means + we have not bailed out earlier */ + free(*header); + *header=NULL; + close(fin); + return PGHEADER_NO_HEADER; + } + last=0; + for(j=8;j<16;j++){ + (*header)[read_bytes+j-8]=buf[j]; + if(buf[j]==0){ + last=1; + } + + } + if(last){ + break; + } + read_bytes+=8; + } + close(fin); + return PGHEADER_NO_ERROR; +} + + +int pgheader_write(const char *header, const char *infile, const char *outfile){ + int fin,fout,i,ret; + char c; + char buf[16]; + char *raw_header; + unsigned int size; + + /* make sure we are dealing with a polyglot book */ + + if((ret=pgheader_detect(infile))){ + return ret; + } + + + + /* safety check! */ + if(!strcmp(infile,outfile)){ + return PGHEADER_NAME_COLLISION; + } + + /* open files */ + fin=open(infile,O_RDONLY|O_BINARY); + if(fin==-1){ + return PGHEADER_OS_ERROR; + } + fout=open(outfile,O_WRONLY|O_CREAT|O_TRUNC|O_BINARY,S_IRUSR|S_IWUSR); + if(fout==-1){ + close(fin); + return PGHEADER_OS_ERROR; + } + + /* skip null records in input file */ + + while(1){ + int j,r; + int zero; + r=read(fin,buf,16); + if(r<16){ + close(fin); + close(fout); + return PGHEADER_BAD_FORMAT; + } + zero=1; + for(j=0;j<8;j++){ + if(buf[j]!=0){ + zero=0; + } + } + if(!zero){ + break; + } + } + + /* write header to output file */ + + if((ret=pgheader_create_raw(&raw_header,header,&size))){ + return ret; + } + + for(i=0;i