home *** CD-ROM | disk | FTP | other *** search
/ PC Open 48 / pcopen48.iso / Internet / HtTrack / DATA1.CAB / Sources / src / htstools.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-21  |  10.3 KB  |  376 lines

  1. /* ------------------------------------------------------------ */
  2. /*
  3. HTTrack Website Copier, Offline Browser for Windows and Unix
  4. Copyright (C) Xavier Roche, Yann Philippot
  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.  
  31. Please visit our Website: http://www.httrack.com
  32. */
  33.  
  34.  
  35. /* ------------------------------------------------------------ */
  36. /* File: httrack.c subroutines:                                 */
  37. /*       various tools (filename analyzing ..)                  */
  38. /* Author: Xavier Roche                                         */
  39. /* ------------------------------------------------------------ */
  40.  
  41. #include "htstools.h"
  42.  
  43. /* specific definitions */
  44. #include "htsbase.h"
  45. #include <stdio.h>
  46. #include <stdlib.h>
  47. #include <string.h>
  48. #include <ctype.h>
  49. /* END specific definitions */
  50.  
  51.  
  52. // forme α partir d'un lien et du contexte (origin_fil et origin_adr d'o∙ il est tirΘ) adr et fil
  53. // [adr et fil sont des buffers de 1ko]
  54. // 0 : ok
  55. // -1 : erreur
  56. // -2 : protocole non supportΘ (ftp)
  57. int ident_url_relatif(char *lien,char* origin_adr,char* origin_fil,char* adr,char* fil) {
  58.   int ok=0;
  59.  
  60.   adr[0]='\0'; fil[0]='\0';    //effacer buffers
  61.  
  62.   // lien non vide!
  63.   if (strnotempty(lien)==0) return -1;    // erreur!
  64.  
  65.   // filtrer les parazites (mailto & cie)
  66.   if (strfield(lien,"mailto:")) {  // ne pas traiter
  67.     ok=-1;
  68.   }
  69.   else if (strfield(lien,"news:")) {  // ne pas traiter
  70.     ok=-1;
  71.   }
  72.   //else if (strfield(lien,"file:")) {  // ne pas traiter
  73.   //  ok=-1;
  74.   //}
  75.   else if (strfield(lien,"javascript:")) {  // ne pas traiter
  76.     ok=-1;
  77.   }
  78.   else if (strstr(lien,":/")) {    // c'est une URL?
  79.     if (strfield(lien,"http:")) {
  80.       if (ident_url(lien,adr,fil)==-1) {        
  81.         ok=-1;    // erreur URL
  82.       }
  83.     } else if (strfield(lien,"file:")) {
  84.       if (ident_url(lien,adr,fil)==-1) {        
  85.         ok=-1;    // erreur URL
  86.       }
  87.     } else if (strfield(lien,"ftp:")) {
  88.       if (ftp_available()) {     // ftp supportΘ
  89.         if (ident_url(lien,adr,fil)==-1) {        
  90.           ok=-1;    // erreur URL
  91.         }
  92.       } else {
  93.         ok=-2;  // non supportΘ
  94.       }
  95.     } else {    // je ne connais pas ce protocole!!
  96.       ok=-1;
  97.     }   
  98.   } else {    // c'est un lien relatif
  99.     char* a;
  100.     
  101.     // On forme l'URL complΦte α partie de l'url actuelle
  102.     // et du chemin actuel si besoin est.
  103.     
  104.     // copier adresse
  105.     if (((int) strlen(origin_adr)<HTS_URLMAXSIZE) && ((int) strlen(origin_fil)<HTS_URLMAXSIZE) && ((int) strlen(lien)<HTS_URLMAXSIZE)) {
  106.       strcpy(adr,origin_adr);    // mΩme adresse
  107.  
  108.       /* bogus form: http:relative.html */
  109.       if (strfield(lien,"http:"))
  110.         lien+=5;
  111.  
  112.       if (*lien!='/') {  // sinon c'est un lien absolu
  113.         a=strchr(origin_fil,'?');
  114.         if (!a) a=origin_fil+strlen(origin_fil);
  115.         while((*a!='/') && ( ((int) a) > ((int) origin_fil)) ) a--;
  116.         if (*a=='/') {    // ok on a un '/'
  117.           if ( (((int) a)-((int) origin_fil)+1+strlen(lien)) < HTS_URLMAXSIZE) {
  118.             // copier chemin
  119.             strncpy(fil,origin_fil,((int) a)-((int) origin_fil)+1);
  120.             *(fil + ((int) a)-((int) origin_fil)+1)='\0';
  121.             
  122.             // copier chemin relatif
  123.             if (((int) strlen(fil)+(int) strlen(lien))<HTS_URLMAXSIZE) {
  124.               strcat(fil,lien + ((*lien=='/')?1:0) );      
  125.               // simplifier url pour les ../
  126.               fil_simplifie(fil);
  127.             } else
  128.               ok=-1;    // erreur
  129.           } else {    // erreur
  130.             ok=-1;    // erreur URL
  131.           }
  132.         } else {    // erreur
  133.           ok=-1;    // erreur URL
  134.         }
  135.       } else { // chemin absolu
  136.         // copier chemin directement
  137.         strcat(fil,lien);      
  138.       }  // *lien!='/'
  139.     } else
  140.       ok=-1;
  141.     
  142.   }  // test news: etc.
  143.  
  144.   // case insensitive pour adresse
  145.   {
  146.     char *a=jump_identification(adr);
  147.     while(*a) {
  148.       if ((*a>='A') && (*a<='Z'))
  149.         *a+='a'-'A';       
  150.       a++;
  151.     }
  152.   }
  153.   
  154.   return ok;
  155. }
  156.  
  157.  
  158.  
  159.  
  160.  
  161. // crΘer dans s, α partir du chemin courant curr_fil, le lien vers link (absolu)
  162. // un ident_url_relatif a dΘja ΘtΘ fait avant, pour que link ne soit pas un chemin relatif
  163. int lienrelatif(char* s,char* link,char* curr_fil) {
  164.   char _curr[HTS_URLMAXSIZE*2];
  165.   char newcurr_fil[HTS_URLMAXSIZE*2],newlink[HTS_URLMAXSIZE*2];
  166.   char* curr;
  167.   //int n=0;
  168.   char* a;
  169.   int slash=0;
  170.   //
  171.   newcurr_fil[0]='\0'; newlink[0]='\0';
  172.   //
  173.  
  174.   // patch: Θliminer les ? (paramΦtres) sinon bug
  175.   if ( (a=strchr(curr_fil,'?')) ) {
  176.     strncat(newcurr_fil,curr_fil,(int) a-(int) curr_fil);
  177.     curr_fil = newcurr_fil;
  178.   }
  179.   if ( (a=strchr(link,'?')) ) {
  180.     strncat(newlink,link,(int) a-(int) link);
  181.     link = newlink;
  182.   }
  183.  
  184.   // recopier uniquement le chemin courant
  185.   curr=_curr;
  186.   strcpy(curr,curr_fil);
  187.   if ((a=strchr(curr,'?'))==NULL)  // couper au ? (params)
  188.     a=curr+strlen(curr)-1;         // pas de params: aller α la fin
  189.   while((*a!='/') && ((int) a>(int) curr)) a--;       // chercher dernier / du chemin courant
  190.   if (*a=='/') *(a+1)='\0';                           // couper dernier /
  191.   
  192.   // "effacer" s
  193.   s[0]='\0';
  194.   
  195.   // sauter ce qui est commun aux 2 chemins
  196.   {
  197.     char *l,*c;
  198.     if (*link=='/') link++;  // sauter slash
  199.     if (*curr=='/') curr++;
  200.     l=link;
  201.     c=curr;
  202.     // couper ce qui est commun
  203. #if HTS_CASSE
  204.     while ((*link==*curr) && (*link!=0)) {link++; curr++; }
  205. #else
  206.     while ((streql(*link,*curr)) && (*link!=0)) {link++; curr++; }
  207. #endif
  208.     // mais on veut un rΘpertoirer entier!
  209.     // si on a /toto/.. et /toto2/.. on ne veut pas sauter /toto !
  210.     while(((*link!='/') || (*curr!='/')) && ((int) link>(int) l)) { link--; curr--; }
  211.     //if (*link=='/') link++;
  212.     //if (*curr=='/') curr++;
  213.   }
  214.   
  215.   // calculer la profondeur du rΘpertoire courant et remonter
  216.   // LES ../ ONT ETE SIMPLIFIES
  217.   a=curr;
  218.   if (*a=='/') a++;
  219.   while(*a) if (*(a++)=='/') strcat(s,"../");
  220.   //if (strlen(s)==0) strcat(s,"/");
  221.  
  222.   if (slash) strcat(s,"/");    // garder absolu!!
  223.   
  224.   // on est dans le rΘpertoire de dΘpart, copier
  225.   strcat(s,link + ((*link=='/')?1:0) );
  226.  
  227.   // on a maintenant une chaine de la forme ../../test/truc.html  
  228.   return 0;
  229. }
  230.  
  231.  
  232. // conversion chemin de fichier/dossier vers 8-3
  233. void long_to_83(char* n83,char* save) {
  234.   n83[0]='\0';
  235.  
  236.   while(*save) {
  237.     char fn83[16],fnl[256];
  238.     int i=0;
  239.     while((save[i]) && (save[i]!='/')) { fnl[i]=save[i]; i++; }
  240.     fnl[i]='\0';
  241.     // conversion
  242.     longfile_to_83(fn83,fnl);
  243.     strcat(n83,fn83);
  244.  
  245.     save+=i;
  246.     if (*save=='/') { strcat(n83,"/"); save++; }
  247.   }
  248. }
  249.  
  250.  
  251. // conversion nom de fichier/dossier vers 8-3
  252. void longfile_to_83(char* n83,char* save) {
  253.   int i=0,j=0;
  254.   char nom[8+1]="";
  255.   char ext[3+1]="";
  256.   
  257.   while((i<8) && (save[j]) && (save[j]!='.')) { if (save[j]!=' ') { nom[i]=save[j]; i++; } j++; }  // recopier nom
  258.   nom[i]='\0';
  259.   if (save[j]) {  // il reste au moins un point
  260.     i=strlen(save)-1;
  261.     while((i>0) && (save[i]!='.') && (save[i]!='/')) i--;    // rechercher dernier .
  262.     if (save[i]=='.') {  // point!
  263.       int j=0;
  264.       i++;
  265.       while((j<3) && (save[i]) ) { if (save[i]!=' ') { ext[j]=save[i]; j++; } i++; }
  266.       ext[j]='\0';
  267.     }
  268.   }
  269.   // corriger vers 8-3
  270.   n83[0]='\0';
  271.   strncat(n83,nom,8);
  272.   if (strnotempty(ext)) {
  273.     strcat(n83,".");
  274.     strncat(n83,ext,3);    
  275.   }
  276. }
  277.  
  278. // Θcrire backblue.gif
  279. int verif_backblue(char* base) {
  280.   if (!fexist(fconcat(base,"backblue.gif"))) {
  281.     FILE* fp = filecreate(fconcat(base,"backblue.gif"));
  282.     if (fp) {
  283.       int ret=0;
  284.       if (fwrite(HTS_DATA_BACK_GIF,HTS_DATA_BACK_GIF_LEN,1,fp) != HTS_DATA_BACK_GIF_LEN)
  285.         ret=1;
  286.       fclose(fp);
  287.       usercommand(0,NULL,fconcat(base,"backblue.gif"));
  288.       return ret;
  289.     } else
  290.       return 1;
  291.   } 
  292.   return 0;
  293. }
  294.  
  295.  
  296. // recherche chaεne de type truc<espaces>=
  297. // renvoi dΘcalage α effectuer ou 0 si non trouvΘ
  298. /* SECTION OPTIMISEE:
  299. #define rech_tageq(adr,s) ( \
  300.   ( (*(adr-1)=='<') || (is_space(*(adr-1))) ) ? \
  301.     ( (streql(*adr,*s)) ? \
  302.       (__rech_tageq(adr,s)) \
  303.       : 0 \
  304.     ) \
  305.     : 0\
  306.   )
  307. */
  308. /*
  309. HTS_INLINE int rech_tageq(const char* adr,const char* s) { 
  310.   if ( (*(adr-1)=='<') || (is_space(*(adr-1))) ) {   // <tag < tag etc
  311.     if (streql(*adr,*s)) {                           // tester premier octet (optimisation)
  312.       return __rech_tageq(adr,s);
  313.     }
  314.   }
  315.   return 0;
  316. }
  317. */
  318. // DeuxiΦme partie
  319. HTS_INLINE int __rech_tageq(const char* adr,const char* s) { 
  320.   int p;
  321.   p=strfield(adr,s);
  322.   if (p) {
  323.     while(is_space(adr[p])) p++;
  324.     if (adr[p]=='=') {
  325.       return p+1;
  326.     }
  327.   }
  328.   return 0;
  329. }
  330.  
  331. // tag sans =
  332. HTS_INLINE int rech_sampletag(const char* adr,const char* s) { 
  333.   register int p;
  334.   if ( (*(adr-1)=='<') || (is_space(*(adr-1))) ) {   // <tag < tag etc
  335.     p=strfield(adr,s);
  336.     if (p) {
  337.       if (!isalnum((unsigned char)adr[p])) {  // <srcbis n'est pas <src
  338.         return 1;
  339.       }
  340.       return 0;
  341.     }
  342.   }
  343.   return 0;
  344. }
  345.  
  346. // teste si le tag contenu dans from est Θgal α "tag"
  347. HTS_INLINE int check_tag(char* from,const char* tag) {
  348.   char* a=from+1;
  349.   int i=0;
  350.   char s[256];
  351.   while(is_space(*a)) a++;
  352.   while((isalnum((unsigned char)*a) || (*a=='/')) && (i<250)) { s[i++]=*a; a++; }
  353.   s[i++]='\0';
  354.   return (strfield2(s,tag));  // comparer
  355. }
  356.  
  357. // teste si un fichier dΘpasse le quota
  358. int istoobig(LLint size,LLint maxhtml,LLint maxnhtml,char* type) {
  359.   int ok=1;
  360.   if (size>0) {
  361.     if (is_hypertext_mime(type)) {
  362.       if (maxhtml>0) {
  363.         if (size>maxhtml)
  364.           ok=0;
  365.       }
  366.     } else {
  367.       if (maxnhtml>0) {
  368.         if (size>maxnhtml)
  369.           ok=0;
  370.       }
  371.     }
  372.   }
  373.   return (!ok);
  374. }
  375.  
  376.