Advertisement
Guest User

climbo

a guest
Jan 24th, 2009
695
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.48 KB | None | 0 0
  1. /*
  2.  *     hacha.c: Une archivos con el Hacha de Windows en Linux.
  3.  * Copyright  (c) BatchDrake 2006 <BatchDrake@gmail.com>
  4.  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  5.  *
  6.  * This program is free software; you can redistribute it and/or modify
  7.  * it under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation; either version 2 of the License, or
  9.  *  (at your option) 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.  */
  22.  
  23.  
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include <stdlib.h>
  27.  
  28. inline char *xstrdup (char *str)
  29. {
  30.   register char *r;
  31.  
  32.   if ((r = strdup (str)) == NULL)
  33.     abort ();
  34.  
  35.   return r;
  36. }
  37.  
  38. inline void *xmalloc (int size)
  39. {
  40.   register void *r;
  41.  
  42.   if ((r = malloc (size)) == NULL)
  43.     abort ();
  44.  
  45.   return r;
  46. }
  47.  
  48. static int __expect_hacha_boundary (FILE *fp)
  49. {
  50.   char buf [5];
  51.  
  52.   if (fread (buf, 5, 1, fp) < 1)
  53.     return -1;
  54.  
  55.   if (memcmp (buf, "?????", 5) == 0)
  56.     return 1;
  57.  
  58.   return 0;
  59. }
  60.  
  61. void expect_hacha_marker (char *file, FILE *fp)
  62. {
  63.   int i;
  64.  
  65.   if ((i = __expect_hacha_boundary (fp)) == -1)
  66.   {
  67.     fprintf (stderr, "%s: Final de fichero inesperado. Archivo incompleto.\n", file);
  68.     exit (1);
  69.   }
  70.   else if (i == 0)
  71.   {
  72.     fprintf (stderr, "%s: Separador interno no encontrado (¿formato no reconocido?). Se aborta.\n", file);
  73.     exit (1);
  74.   }
  75. }
  76.  
  77. int string_end (FILE *fp)
  78. {
  79.   int r;
  80.  
  81.   r = __expect_hacha_boundary (fp);
  82.  
  83.   fseek (fp, -5, SEEK_CUR);
  84.  
  85.   return r;
  86. }
  87.  
  88. char *get_hacha_string (FILE *fp)
  89. {
  90.   char buffer [256];
  91.   char *result;
  92.   int i;
  93.  
  94.   for (i = 0; i < 256 && !string_end (fp); i++)
  95.     fread (&buffer[i], 1, 1, fp);
  96.  
  97.   buffer[i] = '\0';
  98.  
  99.   return xstrdup (buffer);
  100. }
  101.  
  102. int main (int argc, char **argv)
  103. {
  104.   FILE *fp;
  105.   FILE *out;
  106.   FILE *this_input;
  107.  
  108.   char b;
  109.   char *out_file, *tmp;
  110.   char *in_files, *template;
  111.   int size;
  112.   int frag_size;
  113.   int version;
  114.  
  115.   register int frsize;
  116.   register int i, p;
  117.   register int total_amount;
  118.  
  119.   if (argc != 2)
  120.   {
  121.     fprintf (stderr, "Forma de uso: %s archivo.0\n", argv[0]);
  122.     return 0;
  123.   }
  124.  
  125.   fp = fopen (argv[1], "rb");
  126.   if (fp == NULL)
  127.   {
  128.     perror (argv[1]);
  129.     exit (2);
  130.   }
  131.  
  132.   expect_hacha_marker (argv[1], fp);
  133.  
  134.   printf ("\n");
  135.  
  136.   fread (&version, sizeof (int), 1, fp);
  137.   printf ("\tHacha version 0.%d\n", version);
  138.   expect_hacha_marker (argv[1], fp);
  139.   out_file = get_hacha_string (fp);
  140.   expect_hacha_marker (argv[1], fp);
  141.   printf ("\tArchivo de salida: %s\n", out_file);
  142.   tmp = get_hacha_string (fp);
  143.  
  144.   if (!sscanf (tmp, "%i", &size))
  145.   {
  146.     fprintf (stderr, "%s: Error en formato de tamaño total.\n", argv[1]);
  147.     exit (3);
  148.   }
  149.  
  150.   free (tmp);
  151.  
  152.   expect_hacha_marker (argv[1], fp);
  153.  
  154.   tmp = get_hacha_string (fp);
  155.  
  156.   if (!sscanf (tmp, "%i", &frag_size))
  157.   {
  158.     fprintf (stderr, "%s: Error en formato de tamaño total.\n", argv[1]);
  159.     exit (3);
  160.   }
  161.  
  162.   free (tmp);
  163.  
  164.   expect_hacha_marker (argv[1], fp);
  165.  
  166.  
  167.   printf ("\tTamaño total: %g MiB\n", (float) size / (1024.0 * 1024.0));
  168.   printf ("\tTamaño por fragmento: %g MiB\n", (float) frag_size / (1024.0 * 1024.0));
  169.   printf ("\nSe espera que el archivo se haya dividido en %d partes.\n\n", size / frag_size + ((size % frag_size) ? 1 : 0));
  170.  
  171.  
  172.  
  173.   frsize = frag_size;
  174.   out = fopen (out_file, "wb");
  175.   if (out == NULL)
  176.   {
  177.     perror (out_file);
  178.     exit (4);
  179.   }
  180.  
  181.   template = xstrdup (argv[1]);
  182.   in_files = xmalloc (strlen (argv[1]) + 5);
  183.  
  184.   strcpy (in_files, argv[1]);
  185.   template [strlen (template) - 1] = '\0';
  186.  
  187.   for (i = 0, total_amount = 0; total_amount < size; i++)
  188.   {
  189.     printf ("Ligando fichero nº %d... ", i + 1);
  190.     fflush (stdout);
  191.    
  192.     if (i)
  193.     {
  194.       sprintf (in_files, "%s%d", template, i);
  195.       this_input = fopen (in_files, "rb");
  196.       if (this_input == NULL)
  197.       {
  198.         perror (in_files);
  199.         fprintf (stderr, "El archivo es necesario para la recomposición pero no se ha podido abrir. Se aborta.\n");
  200.         unlink (out_file);
  201.        
  202.         exit (1);
  203.       }
  204.     }
  205.     else
  206.       this_input = fp;
  207.    
  208.     if (total_amount + frsize > size)
  209.       frsize = size - total_amount;
  210.    
  211.     for (p = 0; p < frsize; p++)
  212.     {
  213.       if ((p % 1024) == 0 || p == (frsize - 1))
  214.         fprintf (stderr, "%3d%%\033[4D", ((p + 1)) / (frsize / 100));
  215.       if (fread (&b, 1, 1, this_input) < 1)
  216.       {
  217.         fprintf (stderr, "%s: fatal: El fichero está incompleto (¿descarga interrumpida?). Se aborta.\n", in_files);
  218.         unlink (out_file);
  219.        
  220.         exit (1);
  221.       }
  222.      
  223.       if (fwrite (&b, 1, 1, out) < 1)
  224.       {
  225.         perror (out_file);
  226.        
  227.         fprintf (stderr, "%s: fatal: Ha ocurrido un problema al escribir el fichero resultante. Se aborta.\n", in_files);
  228.         unlink (out_file);
  229.       }
  230.     }
  231.    
  232.     total_amount += p;
  233.    
  234.     printf ("Ok  \n");
  235.   }
  236.  
  237.  
  238.   printf ("\nTodo bien.\n");
  239.  
  240.   return 0;
  241. }
  242.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement