home *** CD-ROM | disk | FTP | other *** search
/ C!T ROM 2 / ctrom_ii_b.zip / ctrom_ii_b / PROGRAM / C / LINGU11A / UI_TEXT.C < prev    next >
C/C++ Source or Header  |  1993-03-14  |  2KB  |  69 lines

  1. /* ui_text.c ---- (C) SichemSoft 1991 --- ASR 930314 */
  2. /* Roghorst 160,   6708 KS Wageningen,   Netherlands */
  3. /* text module for language independent applications */
  4.  
  5. #include <stdio.h>
  6. #include <alloc.h>
  7. #include <string.h>
  8. #include <process.h>
  9.  
  10. #define UIT_ENCRYPT   53
  11.  
  12. char **ui_text;
  13. static char *ui_textbuffer;
  14.  
  15. int ui_loadtext(char *fname,char *vers)
  16. /* fname is name of desired .etf file without extension
  17.     vers is optional version number, may also be "" */
  18. {
  19.     char *q; FILE *fp; unsigned len,i,count,check,checksum=0;
  20.     char buf[256],file[81]; unsigned long offset,size;
  21.  
  22.     /* open encrypted text file */
  23.     strcpy(file,fname);
  24.     if (!strchr(file,'.')) strcat(file,".ETF");
  25.     if ((fp=fopen(file,"rb"))==0) return 0;
  26.  
  27.     /* read file header */
  28.     if (!vers) vers="";
  29.     q=strrchr(file,'\\'); if (q) q++; else q=file;
  30.     len=strlen(q)+strlen(vers);
  31.     if (!fread(buf,(len+1)*sizeof(char),1,fp)) return 0;
  32.     if (strnicmp(buf,q,strlen(q))) return 0;
  33.     if (strnicmp(buf+strlen(q),vers,strlen(vers))) return 0;
  34.  
  35.     /* read number of items and characters and reserve space */
  36.     if (!fread(&count,sizeof(count),1,fp)) return 0;
  37.     ui_text=(char **)malloc(count*sizeof(char *));
  38.     if (!fread(&size,sizeof(size),1,fp)) return 0;
  39.     ui_textbuffer=(char *)malloc(size*sizeof(char));
  40.     if (!ui_text || !ui_textbuffer) return 0;
  41.  
  42.     /* read offsets and compute pointers */
  43.     for (i=0; i<count; i++) {
  44.         if (!fread(&offset,sizeof(offset),1,fp)) return 0;
  45.         ui_text[i]=ui_textbuffer+offset;
  46.     }
  47.  
  48.     /* read text lines and checksum */
  49.     fread(ui_textbuffer,size*sizeof(char),1,fp);
  50.     fread(&check,sizeof(check),1,fp);
  51.     fclose(fp);
  52.  
  53.     /* decrypt text lines and compute checksum */
  54.     for (offset=0; offset<size; offset++) {
  55.         q=ui_textbuffer+offset;
  56.         if (*q) { *q^=UIT_ENCRYPT; checksum+=(unsigned char)(*q); }
  57.     }
  58.  
  59.     /* check checksum */
  60.     if (check!=checksum) return 0;
  61.     return 1;
  62.  
  63. } /* ui_loadtext */
  64.  
  65. void ui_unloadtext(void)
  66. {
  67.     free(ui_text); free(ui_textbuffer);
  68. } /* ui_unloadtext */
  69.