home *** CD-ROM | disk | FTP | other *** search
/ The AGA Experience 2 / agavol2.iso / software / utilities / emulation / amoric / transf.c < prev    next >
C/C++ Source or Header  |  1995-12-17  |  6KB  |  358 lines

  1. /* File Transfer Program for Oric Software */
  2. /* UNIX Version by Boris GRANVEAUD 1994 */
  3.  
  4. /* Adapted to Amiga by JF FABRE */
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9.  
  10. #include <dos/dos.h>
  11. #include <proto/dos.h>
  12. #include <pragmas/dos_pragmas.h>
  13.  
  14. FILE *f1,*f2;
  15.  
  16. void FinFichier(void)
  17. {
  18.   printf("** End of file encountered !\n");
  19.   fclose(f2);
  20.   exit(0);
  21. }
  22.  
  23. void Attend1Zero(FILE *f)
  24. {
  25.   int c1,c2;
  26.  
  27.   c1=127;
  28.   for (;;)
  29.   {
  30.     c2=fgetc(f);
  31.  
  32.     if (feof(f))
  33.       FinFichier();
  34.       
  35.     if (((c1>127) && (c2<=127)) || ((c1<127) && (c2>=127)))
  36.       return;
  37.     c1=c2;
  38.   }
  39. }
  40.  
  41. void Attend8Zeros(FILE *f)
  42. {
  43.   int zeros,c1,c2;
  44.  
  45.   zeros=0; c1=127;
  46.   do
  47.   {
  48.     c2=fgetc(f);
  49.  
  50.     if (feof(f))
  51.       FinFichier();
  52.       
  53.     if (((c1>127) && (c2<=127)) || ((c1<127) && (c2>=127)))
  54.       zeros++;
  55.     c1=c2;
  56.   }
  57.   while (zeros<8);
  58. }
  59.  
  60. int LectureBit300(FILE *f,int seuil,int aff)
  61. {
  62.   long pos1,pos2,nb_oct;
  63.  
  64.   pos1=ftell(f);
  65.   Attend8Zeros(f);
  66.   pos2=ftell(f);
  67.  
  68.   nb_oct=pos2-pos1;
  69.  
  70.   if (nb_oct>seuil)
  71.   {
  72.     if (aff)    printf("0");
  73.     return 0;
  74.   }
  75.  
  76.   Attend8Zeros(f);
  77.  
  78.   if (aff)  printf("1");
  79.  
  80.   return 1;
  81. }
  82.  
  83. int LectureBit2400(FILE *f,int seuil,int aff)
  84. {
  85.   long pos1,pos2,nb_oct;
  86.  
  87.   pos1=ftell(f);
  88.   Attend1Zero(f);
  89.   Attend1Zero(f);
  90.   pos2=ftell(f);
  91.  
  92.   nb_oct=pos2-pos1;
  93.  
  94.   if (nb_oct>seuil)
  95.   {
  96.  
  97.     return 0;
  98.   }
  99.  
  100.  
  101.  
  102.   return 1;
  103. }
  104.  
  105. int AmorceValide(int b[13])
  106. {
  107.   int i;
  108.   char ch[20];
  109.   
  110.   ch[0]=0;
  111.   for (i=0;i<13;i++)
  112.     sprintf(ch,"%s%d",ch,b[i]);
  113.     
  114.   return (!strcmp(ch,"0011010000111"));
  115. }
  116.  
  117. void Synchronisation(FILE *f1,int mode,int seuil)
  118. {
  119.   int b[13],i;
  120.   
  121.   printf("Searching...\n");
  122.   
  123.   for (i=0;i<13;i++)
  124.     if (mode)
  125.       b[i]=LectureBit2400(f1,seuil,0);
  126.     else
  127.       b[i]=LectureBit300(f1,seuil,0);
  128.      
  129.   while (!AmorceValide(b))
  130.   {
  131.     for (i=0;i<12;i++)
  132.       b[i]=b[i+1];
  133.       
  134.     if (mode)
  135.       b[12]=LectureBit2400(f1,seuil,0);
  136.     else
  137.       b[12]=LectureBit300(f1,seuil,0);
  138.   }
  139. }
  140.  
  141. int Lecture1Octet(FILE *f1,int mode,int seuil,int aff)
  142. {
  143.   int octet,nb_1,i,b,stop[3],erreur;
  144.   long pos,offset;
  145.     
  146.   pos=ftell(f1);
  147.   
  148.   octet=0; nb_1=0; erreur=0;
  149.  
  150.   do
  151.   {
  152.     if (mode)
  153.       b=LectureBit2400(f1,seuil,aff);
  154.     else
  155.       b=LectureBit300(f1,seuil,aff);
  156.     if (b)
  157.       erreur=1;
  158.   }
  159.   while (b);
  160.   
  161.  /* if (aff) printf(" ");*/
  162.   
  163.   for (i=0;i<9;i++)
  164.   {
  165.     if (mode)
  166.       b=LectureBit2400(f1,seuil,aff);
  167.     else
  168.       b=LectureBit300(f1,seuil,aff);
  169.  
  170.     if ((i<=7) && (b))
  171.     {
  172.       octet+=b<<i; nb_1++;
  173.     }
  174.     if ((i==8) && (b))
  175.       nb_1++;
  176.   }
  177.  
  178.   if (!(nb_1 & 1))
  179.     erreur=1;
  180.       
  181.   for (i=0;i<3;i++)
  182.   {
  183.     if (mode)
  184.       stop[i]=LectureBit2400(f1,seuil,aff);
  185.     else
  186.       stop[i]=LectureBit300(f1,seuil,aff);
  187.   }
  188.   
  189.   while ((!stop[0]) || (!stop[1]) || (!stop[2]))
  190.   {
  191.     erreur=1;
  192.     
  193.     stop[0]=stop[1]; stop[1]=stop[2];
  194.     if (mode)
  195.       stop[2]=LectureBit2400(f1,seuil,aff);
  196.     else
  197.       stop[2]=LectureBit300(f1,seuil,aff);
  198.   }
  199.   
  200.   Attend1Zero(f1);
  201.  
  202.  
  203.   if ((erreur) && (aff))
  204.   {
  205.     printf(" %2x ",octet);
  206.     
  207.     if ((octet>=32) && (octet<=127))
  208.       printf(" %c",octet);
  209.     else
  210.       printf("  ");
  211.  
  212.     printf(" longueur %ld ",ftell(f1)-pos);
  213.  
  214.     printf("** Error found\nEnter new value :");fflush(stdout);fflush(stdin);
  215.     scanf("%x",&octet);fflush(stdin);
  216.     printf("offset:");fflush(stdout);fflush(stdin);
  217.     scanf("%ld",&offset);fflush(stdin);
  218.     fseek(f1,offset,SEEK_CUR);
  219.   }     
  220.  
  221.      
  222.   return octet;
  223. }
  224.  
  225. main(argc,argv)
  226.  
  227. unsigned int argc;
  228. char **argv;
  229.  
  230. {
  231.  
  232.   int c,i,j,nb_oct,frequence,periode,seuil,mode,octet,magic_number,octet1,octet2,octet3,octet4;
  233.   
  234.   if (argc<3)
  235.   {
  236.     printf("Usage : transf infile outfile\n");
  237.     exit(0);
  238.   }
  239.  
  240.   f1=fopen(*(argv+1),"rb");
  241.   if (!f1)
  242.   {
  243.     printf("Cannot open %s for reading\n",*(argv+1));
  244.     exit(0);
  245.   }
  246.  
  247.   f2=fopen(*(argv+2),"wb");
  248.   if (!f2)
  249.   {
  250.     printf("Cannot open %s for writing\n",*(argv+2));
  251.     exit(0);
  252.   }
  253.  
  254.   do
  255.   {
  256.     c=fgetc(f1);
  257.   }
  258.   while (c<130);
  259.  
  260.   do
  261.   {
  262.     c=fgetc(f1);
  263.   }
  264.   while (c>=127);
  265.  
  266.  
  267.   mode=1; /* mettre a 0 pour lent */
  268.   
  269.   printf("Sample rate :");
  270.   scanf("%d",&frequence);
  271.  
  272.   magic_number=(int)(56*frequence/44100);
  273.   if (argc==4) magic_number=0; 
  274.   printf("Magic number : %d\n",magic_number);
  275.   
  276.   if (mode)
  277.   {
  278.     periode=208*frequence/1000000;
  279.     seuil=periode*5/2;
  280.   }
  281.   else
  282.   {
  283.     periode=3328*frequence/1000000;
  284.     seuil=periode*3/4;
  285.   }
  286.   
  287.   printf("period: %d bytes  thresold: %d bytes\n",periode,seuil);
  288.   
  289.   Synchronisation(f1,mode,seuil);
  290.   Synchronisation(f1,mode,seuil);
  291.  
  292.   while (Lecture1Octet(f1,mode,seuil,0)!=0x24);
  293.  
  294.   fputc(0x16,f2);
  295.   fputc(0x16,f2);
  296.   fputc(0x16,f2);
  297.   fputc(0x16,f2);
  298.   fputc(0x16,f2);
  299.   fputc(0x16,f2);
  300.   fputc(0x16,f2);
  301.   fputc(0x16,f2);
  302.   fputc(0x24,f2);
  303.  
  304.   printf("Header...\n");
  305.     
  306.   for (i=0;i<4;i++)
  307.   {
  308.     octet=Lecture1Octet(f1,mode,seuil,0);
  309.     fputc(octet,f2);
  310.   }
  311.   
  312.   octet1=Lecture1Octet(f1,mode,seuil,0);
  313.   fputc(octet1,f2);
  314.   octet2=Lecture1Octet(f1,mode,seuil,0);
  315.   fputc(octet2,f2);
  316.   octet3=Lecture1Octet(f1,mode,seuil,0);
  317.   fputc(octet3,f2);
  318.   octet4=Lecture1Octet(f1,mode,seuil,0);
  319.   fputc(octet4,f2);
  320.   octet=Lecture1Octet(f1,mode,seuil,0);
  321.   fputc(octet,f2);
  322.   
  323.   printf("Start address :\011%4x\n",octet3*256+octet4);
  324.   printf("Ending address :\011%4x\n",octet1*256+octet2);
  325.   
  326.   nb_oct=octet1*256+octet2-(octet3*256+octet4);
  327.   printf("Data length :\011%4x\n",nb_oct);
  328.   
  329.   printf("Loading... ");
  330.   
  331.   while ((octet=Lecture1Octet(f1,mode,seuil,0)))
  332.   {
  333.     fputc(octet,f2);
  334.     printf("%c",octet);
  335.   }
  336.   fputc(0,f2);
  337.   
  338.   printf("\n");
  339.   
  340.   if (magic_number) fseek(f1,magic_number,SEEK_CUR);
  341.   
  342.   printf("Data...\n");
  343.   
  344.   for (j=0;j<nb_oct;j++)
  345.   {
  346.     /*printf("octet %4x ",j);*/
  347.     
  348.     octet=Lecture1Octet(f1,mode,seuil,1);
  349.     
  350.     fputc(octet,f2);
  351.   }
  352.  
  353.   fclose(f1);
  354.   fclose(f2);
  355.  
  356.   return 0;
  357. }
  358.