home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 71 / CDROM71.ISO / internet / navoff / data1.cab / Sources / src / htsjava.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-04-01  |  9.6 KB  |  419 lines

  1. /* ------------------------------------------------------------ */
  2. /*
  3. HTTrack Website Copier, Offline Browser for Windows and Unix
  4. Copyright (C) Xavier Roche, Yann Philippot and other contributors
  5.  
  6. This program is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public License
  8. as published by the Free Software Foundation; either version 2
  9. of the License, or any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  19.  
  20.  
  21. Important notes:
  22.  
  23. - We hereby ask people using this source NOT to use it in purpose of grabbing
  24. emails addresses, or collecting any other private information on persons.
  25. This would disgrace our work, and spoil the many hours we spent on it.
  26.  
  27.  
  28. This project has been developed by Xavier Roche and Yann Philippot,
  29. from the company Serianet at Caen, France (http://www.serianet.com)
  30. and other contributors (see the greetings file)
  31.  
  32. Please visit our Website: http://www.httrack.com
  33. */
  34.  
  35.  
  36. /* ------------------------------------------------------------ */
  37. /* File: Java classes parser                                    */
  38. /* Author: Yann Philippot                                       */
  39. /* ------------------------------------------------------------ */
  40.  
  41.  
  42. /* Version: Oct/2000 */
  43. /* Fixed: problems with class structure (10/2000) */
  44.  
  45. // htsjava.c - Parseur de classes java
  46.  
  47. #include "stdio.h"
  48. #include "htssystem.h"
  49. #include "httrack.h"
  50. #include "htsjava.h"
  51.  
  52. #include <stdio.h>
  53. #include <stdlib.h>
  54. #include <string.h>
  55. //#include <math.h>
  56.  
  57. #ifndef HTS_LITTLE_ENDIAN
  58. #define REVERSE_ENDIAN 1
  59. #else
  60. #define REVERSE_ENDIAN 0
  61. #endif
  62.  
  63. /* big/little endian swap */
  64. #define hts_swap16(A) ( (((A) & 0xFF)<<8) | (((A) & 0xFF00)>>8) )
  65. #define hts_swap32(A) ( (( (hts_swap16(A)) & 0xFFFF)<<16) | (( (hts_swap16(A>>16)) & 0xFFFF)) )
  66.  
  67.  
  68. // ** HTS_xx sinon pas pris par VC++
  69. #define HTS_CLASS  7
  70. #define HTS_FIELDREF  9
  71. #define HTS_METHODREF  10
  72. #define HTS_STRING  8
  73. #define HTS_INTEGER  3
  74. #define HTS_FLOAT  4
  75. #define HTS_LONG  5
  76. #define HTS_DOUBLE  6
  77. #define HTS_INTERFACE  11
  78. #define HTS_NAMEANDTYPE  12
  79. #define HTS_ASCIZ  1
  80. #define HTS_UNICODE  2
  81.  
  82. #define JAVADEBUG 0
  83.  
  84. int hts_parse_java(char *file,char* err_msg)
  85. {
  86.   FILE *fpout;
  87.   JAVA_HEADER header;
  88.   RESP_STRUCT *tab;
  89.   
  90. #if JAVADEBUG
  91.   printf("fopen\n");
  92. #endif
  93.   if ((fpout = fopen(fconv(file), "r+b")) == NULL)
  94.   {
  95.     //fprintf(stderr, "Cannot open input file.\n");
  96.     sprintf(err_msg,"Unable to open file %s",file);
  97.     return 0;   // une erreur..
  98.   }
  99.     
  100. #if JAVADEBUG
  101.   printf("fread\n");
  102. #endif
  103.   //if (fread(&header,1,sizeof(JAVA_HEADER),fpout) != sizeof(JAVA_HEADER)) {   // pas complet..
  104.   if (fread(&header,1,10,fpout) != 10) {   // pas complet..
  105.     fclose(fpout);
  106.     sprintf(err_msg,"File header too small (file len = "LLintP")",(LLint)fsize(file));
  107.     return 0;
  108.   }
  109.  
  110. #if JAVADEBUG
  111.   printf("header\n");
  112. #endif
  113.   // tester en tΩte
  114. #if REVERSE_ENDIAN
  115.   header.magic = hts_swap32(header.magic);
  116.   header.count = hts_swap16(header.count); 
  117. #endif        
  118.   if(header.magic!=0xCAFEBABE) {
  119.     sprintf(err_msg,"non java file");
  120.     return 0;
  121.   }
  122.  
  123.   tab =(RESP_STRUCT*)calloct(header.count,sizeof(RESP_STRUCT));
  124.   if (!tab) {
  125.     sprintf(err_msg,"Unable to alloc %d bytes",sizeof(RESP_STRUCT));
  126.     fclose(fpout);
  127.     return 0;    // erreur..
  128.   }
  129.  
  130. #if JAVADEBUG
  131.   printf("calchead\n");
  132. #endif
  133.   {
  134.     int i;
  135.     
  136.     for (i = 1; i < header.count; i++) {
  137.       int err=0;  // ++    
  138.       tab[i]=readtable(fpout,tab[i],&err,err_msg);
  139.       if (!err) {
  140.         if ((tab[i].type == HTS_LONG) ||(tab[i].type == HTS_DOUBLE)) i++;  //2 element si double ou float
  141.       } else {    // ++ une erreur est survenue!
  142.         if (strnotempty(err_msg)==0)
  143.           strcpy(err_msg,"Internal readtable error");
  144.         freet(tab);
  145.         fclose(fpout);
  146.         return 0;
  147.       }
  148.     }
  149.     
  150.   }
  151.  
  152.   
  153. #if JAVADEBUG
  154.   printf("addfiles\n");
  155. #endif
  156.   {
  157.     unsigned int acess;
  158.     unsigned int Class;
  159.     unsigned int SClass;
  160.     int i;
  161.     acess = readshort(fpout);
  162.     Class = readshort(fpout);
  163.     SClass = readshort(fpout);
  164.     
  165.     for (i = 1; i <header.count; i++) {
  166.       
  167.       if (tab[i].type == HTS_CLASS) {
  168.         
  169.         if ((tab[i].index1<header.count) && (tab[i].index1>=0)) {
  170.           
  171.           
  172.           if((tab[i].index1!=SClass) && (tab[i].index1!=Class) && (tab[tab[i].index1].name[0]!='[')) {
  173.             
  174.             if(!strstr(tab[tab[i].index1].name,"java/")) {
  175.               char tempo[1024];
  176.               tempo[0]='\0';
  177.               
  178.               sprintf(tempo,"%s.class",tab[tab[i].index1].name);
  179. #if JAVADEBUG
  180.               printf("add %s\n",tempo);
  181. #endif
  182.               if (tab[tab[i].index1].file_position >= 0)
  183.                 hts_add_file(tempo,tab[tab[i].index1].file_position);
  184.             }
  185.             
  186.           }
  187.         } else { 
  188.           i=header.count;  // exit 
  189.         }
  190.       }
  191.       
  192.     }
  193.   }
  194.   
  195.  
  196. #if JAVADEBUG
  197.   printf("end\n");
  198. #endif
  199.   freet(tab);
  200.   fclose(fpout);
  201.   return 1;
  202. }
  203.  
  204.  
  205.  
  206.  
  207. // error: !=0 si erreur fatale
  208. RESP_STRUCT readtable(FILE *fp,RESP_STRUCT trans,int* error,char* err_msg)
  209. {
  210.   unsigned short int length;
  211.   int j;
  212.   *error = 0;  // pas d'erreur
  213.   trans.file_position=-1;
  214.   trans.type = (int)(unsigned char)fgetc(fp);
  215.   switch (trans.type) {
  216.   case HTS_CLASS:
  217.     strcpy(trans.name,"Class");
  218.     trans.index1 = readshort(fp);
  219.     break;
  220.     
  221.   case HTS_FIELDREF:
  222.     strcpy(trans.name,"Field Reference");
  223.     trans.index1 = readshort(fp);
  224.     readshort(fp);
  225.     break;
  226.     
  227.   case HTS_METHODREF:
  228.     strcpy(trans.name,"Method Reference");
  229.     trans.index1 = readshort(fp);
  230.     readshort(fp);
  231.     break;
  232.     
  233.   case HTS_INTERFACE:
  234.     strcpy(trans.name,"Interface Method Reference");
  235.     trans.index1 =readshort(fp);
  236.     readshort(fp);
  237.     break;
  238.   case HTS_NAMEANDTYPE:
  239.     strcpy(trans.name,"Name and Type");
  240.     trans.index1 = readshort(fp);
  241.     readshort(fp);
  242.     break;
  243.     
  244.   case HTS_STRING:                // CONSTANT_String
  245.     strcpy(trans.name,"String");
  246.     trans.index1 = readshort(fp);
  247.     break;
  248.     
  249.   case HTS_INTEGER:
  250.     strcpy(trans.name,"Integer");
  251.     for(j=0;j<4;j++) fgetc(fp);
  252.     break;
  253.     
  254.   case HTS_FLOAT:
  255.     strcpy(trans.name,"Float");
  256.     for(j=0;j<4;j++) fgetc(fp);
  257.     break;
  258.     
  259.   case HTS_LONG:
  260.     strcpy(trans.name,"Long");
  261.     for(j=0;j<8;j++) fgetc(fp);
  262.     break;
  263.   case HTS_DOUBLE:
  264.     strcpy(trans.name,"Double");
  265.     for(j=0;j<8;j++) fgetc(fp);
  266.     break;
  267.     
  268.   case HTS_ASCIZ:
  269.   case HTS_UNICODE:
  270.     
  271.     if (trans.type == HTS_ASCIZ)
  272.       strcpy(trans.name,"HTS_ASCIZ");
  273.     else
  274.       strcpy(trans.name,"HTS_UNICODE");
  275.     
  276.     {
  277.       char buffer[1024]; 
  278.       char *p;
  279.       
  280.       p=&buffer[0];
  281.  
  282.       //fflush(fp); 
  283.       trans.file_position=ftell(fp);
  284.       length = readshort(fp);
  285.       if (length<HTS_URLMAXSIZE) {
  286.         // while ((length > 0) && (length<500)) {
  287.         while (length > 0) {
  288.           *p++ =fgetc(fp);
  289.           
  290.           length--;
  291.         }
  292.         *p='\0';
  293.         
  294.         //#if JDEBUG
  295.         //      if(tris(buffer)==1) printf("%s\n ",buffer);
  296.         //      if(tris(buffer)==2)  printf("%s\n ",printname(buffer));
  297.         //#endif
  298.         if(tris(buffer)==1)  hts_add_file(buffer,trans.file_position);       
  299.         else if(tris(buffer)==2)  hts_add_file(printname(buffer),trans.file_position);
  300.  
  301.         strcpy(trans.name,buffer);
  302.       } else {    // gros pb
  303.         while ( (length > 0) && (!feof(fp))) {
  304.           fgetc(fp);
  305.           length--;
  306.         }
  307.         if (!feof(fp)) {
  308.           trans.type=-1;
  309.         } else {
  310.           sprintf(err_msg,"Internal stucture error (ASCII)");
  311.           *error = 1;
  312.         }
  313.         return(trans);
  314.       }
  315.     }
  316.     break;
  317.   default:
  318.     // printf("Type inconnue\n");
  319.     // on arrΩte tout 
  320.     sprintf(err_msg,"Internal structure unknown (type %d)",trans.type);
  321.     *error = 1;
  322.     return(trans);
  323.     break;
  324.   }  
  325.   return(trans);
  326. }
  327.  
  328.  
  329. unsigned short int readshort(FILE *fp)
  330. {
  331.   unsigned short int valint;
  332.   fread(&valint,sizeof(valint),1,fp);
  333.  
  334. #if REVERSE_ENDIAN
  335.   return hts_swap16(valint);
  336. #else
  337.   return valint;
  338. #endif
  339.   
  340. }
  341.  
  342. /*
  343.  
  344.   Darn, this one was really dirty
  345.  
  346. // a^b
  347. double my_pow(double a,double b) {
  348.   double r=1;
  349.   int i;
  350.   for(i=0;i<b;i++)
  351.     r*=a;
  352.   return r;
  353. }
  354.  
  355. unsigned int swap(long int nomber,int digit)
  356. {
  357.   long unsigned  int t,i;
  358.   long unsigned int d=0xff;
  359.   double total=0;
  360.   for(i=0;i<(long unsigned  int) digit;i++){ // **
  361.     t=nomber & d;
  362.     total=total+t*my_pow((double)16,(double)(digit*2-i*2-2));
  363.     nomber=nomber >> 8;
  364.   }
  365.   return (int)total;
  366. }
  367. */
  368.  
  369. int tris(char * buffer)
  370. {
  371.   //
  372.   // Java
  373.   if((buffer[0]=='[') && buffer[1]=='L' && (!strstr(buffer,"java/")) ) 
  374.     return 2;
  375.   if (strstr(buffer,".gif") || strstr(buffer,".jpg") || strstr(buffer,".jpeg") || strstr(buffer,".au") ) 
  376.     return 1;
  377.   // Ajouts R.X: test type
  378.   // Autres fichiers
  379.   {
  380.     char type[256];
  381.     type[0]='\0';
  382.     get_httptype(type,buffer,0);
  383.     if (strnotempty(type))     // type reconnu!
  384.       return 1;
  385.   }
  386.   return 0;
  387. }
  388.  
  389.  
  390. char * printname(char  name[1024])
  391. {
  392.   static char rname[1024];
  393.   //char *rname;
  394.   char *p;
  395.   char *p1;
  396.   int j;
  397.   rname[0]='\0';
  398.   //
  399.  
  400.   p=&name[0];
  401.   
  402.   if(*p!='[')  return "";
  403.   p+=2;
  404.   //rname=(char*)calloct(strlen(name)+8,sizeof(char));
  405.   p1=rname;
  406.   for (j = 0; j < (int) strlen(name); j++,p++) {
  407.     if (*p == '/') *p1='.'; 
  408.     if (*p==';'){*p1='\0';
  409.     strcat(rname,".class");
  410.     return (rname);}
  411.     else *p1=*p;
  412.     p1++;
  413.   }
  414.   p1-=3;
  415.   *p1='\0';
  416.   return (rname);
  417.   
  418. }
  419.