static unsigned char *_convert_lzw_dynamic(unsigned char *data_in, int max_bits,int use_rle, unsigned long in_len, unsigned long orig_len, int q, struct local_data *data) { unsigned char *data_out; int csize,orgcsize; int newcode,oldcode,k=0; int first=1,noadd; //printf("in_len = %d, orig_len = %d\n", in_len, orig_len); data->quirk = q; data->global_use_rle=use_rle; data->maxstr=(1<<max_bits); if((data_out=malloc(orig_len))==NULL) { //fprintf(stderr,"nomarch: out of memory!\n"); return NULL; } data->io.data_in_point=data_in; data->io.data_in_max=data_in+in_len; data->io.data_out_point=data_out; data->io.data_out_max=data_out+orig_len; data->dc_bitbox=data->dc_bitsleft=0; data->codeofs=0; outputrle(-1,NULL, &data->rd, &data->io); /* init RLE */ data->oldver=0; csize=9; /* initial code size */ if(max_bits==0) /* special case for static 12-bit */ data->oldver=1,csize=12,data->maxstr=4096; orgcsize=csize; inittable(orgcsize, data); oldcode=newcode=0; if(data->quirk & NOMARCH_QUIRK_SKIPMAX) data->io.data_in_point++; /* skip type 8 max. code size, always 12 */ if(max_bits==16) data->maxstr=(1<<*data->io.data_in_point++); /* but compress-type *may* change it (!) */ /* XXX */ if (data->maxstr > (1 << max_bits)) return NULL; data->nomarch_input_size = 0; while(1) { //printf("input_size = %d csize = %d\n", data->nomarch_input_size, csize); if(!readcode(&newcode,csize,data)) { //printf("readcode failed!\n"); break; } //printf("newcode = %x\n", newcode); if (data->quirk & NOMARCH_QUIRK_END101) { if (newcode == 0x101 /* data_out_point>=data_out_max */) { //printf("end\n"); break; } } noadd=0; if(first) { k=newcode,first=0; if(data->oldver) noadd=1; } if(newcode==256 && !data->oldver) { /* this *doesn't* reset the table (!), merely reduces code size again. * (It makes new strings by treading on the old entries.) * This took fscking forever to work out... :-( */ data->st_last=255; if (data->quirk & NOMARCH_QUIRK_START101) /* CM: Digital symphony data->quirk */ data->st_last++; /* XXX do we need a resync if there's a reset when *already* csize==9? * (er, assuming that can ever happen?) */ code_resync(csize, data); csize=orgcsize; if(!readcode(&newcode,csize,data)) break; } if((!data->oldver && newcode<=data->st_last) || (data->oldver && data->st_chr[newcode]!=UNUSED)) { outputstring(newcode, data); k=findfirstchr(newcode, data); } else { /* this is a bit of an assumption, but these ones don't seem to happen in * non-broken files, so... */ #if 0 /* actually, don't bother, just let the CRC tell the story. */ if(newcode>data->st_last+1) fprintf(stderr,"warning: bad LZW code\n"); #endif /* k=findfirstchr(oldcode);*/ /* don't think I actually need this */ outputstring(oldcode, data); outputchr(k, data); } if(data->st_last!=data->maxstr-1) { if(!noadd) { if(!addstring(oldcode,k,data)) { /* XXX I think this is meant to be non-fatal? * well, nothing for now, anyway... */ } if(data->st_last!=data->maxstr-1 && data->st_last==((1<<csize)-1)) { csize++; code_resync(csize-1, data); } } } oldcode=newcode; } if (~data->quirk & NOMARCH_QUIRK_NOCHK) { /* junk it on error */ if(data->io.data_in_point!=data->io.data_in_max) { free(data_out); return(NULL); } } return(data_out); }
int main(int argc, char *argv[]) { int idx, ckval; int topicidx; int columns = 0; help_indx entry; FILE *tfp, *ifp, *hfp; time_t run_time; if ( argc != 4 ) { printf("Usage:\tmkhtml <txt_file> <indx_file> <output_html_file>\n"); exit(-1); } if ((tfp = fopen(argv[1], "r")) == NULL) { fprintf(stderr, "can't open %s for reading\n", argv[1]); exit(-1); } if ((ifp = fopen(argv[2], "r")) == NULL) { fprintf(stderr, "can't open %s for reading\n", argv[2]); exit(-1); } if ((hfp = fopen(argv[3], "w")) == NULL) { fprintf(stderr, "can't open %s for writing\n", argv[2]); exit(-1); } printf("Scanning topics...\n"); while (fread(&alltopics[ntopics], sizeof(entry), 1, ifp) != 0) { memcpy(&alltopicslc[ntopics], &alltopics[ntopics], sizeof(help_indx)); for( idx = 0; idx < strlen(alltopicslc[ntopics].topic); idx++ ) { alltopicslc[ntopics].topic[idx] = tolower(alltopicslc[ntopics].topic[idx]); } ++ntopics; if( ntopics >= MAXTOPICS ) { fprintf(stderr, "Too many topics to convert.\n"); exit(-1); } } sorttopics(); printf("Converting topics...\n"); fprintf(hfp, "<HTML><HEAD><TITLE>RhostMUSH Help File [HTML Version]" "</TITLE></HEAD><BODY>\n" "<H1>RhostMUSH Help File [HTML Version]</H1>\n"); run_time = time(NULL); fprintf(hfp, "Generated: %s", ctime(&run_time) ); fprintf(hfp, "<HR><A NAME=\"topic index\"><H2>Topic Index</H2></A><PRE>\n"); for( topicidx = 0; topicidx < ntopics; topicidx++ ) { fprintf(hfp, "<A HREF=\"#"); outputurltext(hfp, alltopicslc[topicidx].topic); fprintf(hfp, "\">"); outputstring(hfp, alltopics[topicidx].topic); fprintf(hfp, "</A>"); if( ++columns == 3 ) { columns = 0; fprintf(hfp, "\n"); } else { ckval = strlen(alltopics[topicidx].topic); if ( ckval > 26 ) ckval = 26; for( idx = 0; idx < 26 - ckval; idx++ ) { fprintf(hfp, " "); } } } for( topicidx = 0; topicidx < ntopics; topicidx++ ) { fprintf(hfp, "<HR><A NAME=\""); outputurltext(hfp, alltopicslc[topicidx].topic); fprintf(hfp, "\"><H3>"); outputstring(hfp, alltopics[topicidx].topic); fprintf(hfp, "</H3></A><PRE>\n"); fseek(tfp, alltopics[topicidx].pos, SEEK_SET); if( alltopics[topicidx].len >= MAXTEXTLEN - 1 ) { fprintf(stderr, "Topic too long: %s\n", alltopics[topicidx].topic); exit(-1); } /* fread(bigtextbuff, alltopics[topicidx].len, 1, tfp); bigtextbuff[alltopics[topicidx].len] = '\0'; outputstring(hfp, bigtextbuff); */ for (;;) { if ( fgets(bigtextbuff, (MAXTEXTLEN - 1), tfp) == NULL ) break; if (bigtextbuff[0] == '&') break; outputstring(hfp, bigtextbuff); } fprintf(hfp, "</PRE>\n"/*, entry.topic ??? */); if( topicidx ) { fprintf(hfp, "<A HREF=\"#"); outputurltext(hfp, alltopicslc[topicidx-1].topic); fprintf(hfp, "\">[PREV]</A>\n"); } fprintf(hfp, "<A HREF=\"#topic index\">[TOP]</A>\n"); if( topicidx < ntopics - 1) { fprintf(hfp, "<A HREF=\"#"); outputurltext(hfp, alltopicslc[topicidx+1].topic); fprintf(hfp, "\">[NEXT]</A>\n"); } fprintf(hfp, "<BR>\n"); } fprintf(hfp, "</BODY></HTML>\n"); fclose(tfp); fclose(ifp); fclose(hfp); printf("%d topics converted\n", ntopics); return 0; }