Пример #1
0
int
main (int argc, char **argv)
{
    char *config = NULL;
    char *password = NULL;

    int ret;
    state_data myhandle;
    expand_data expandhandle;
    wvParseStruct ps;
    int c, index = 0;
    static struct option long_options[] = {
	{"config", 1, 0, 'x'},
	{"password", 1, 0, 'p'},
	{0, 0, 0, 0}
    };

    if (argc < 2)
	usage ();

    while (1)
      {
	  c = getopt_long (argc, argv, "x:p:", long_options, &index);
	  if (c == -1)
	      break;
	  switch (c)
	    {
	    case 'x':
		if (optarg)
		    config = optarg;
		else
		    wvError (("No config file given to config option"));
		break;
	    case 'p':
		if (optarg)
		    password = optarg;
		else
		    wvError (("No password given to password option"));
		break;
	    default:
		usage ();
		break;
	    }
      }

    wvInit ();
    ret = wvInitParser (&ps, argv[optind]);
    ps.filename = argv[optind];

    if (ret == 4)
      {
	  ret = 0;
	  if (password == NULL)
	    {
		wvError (
			 ("Password required, this is an encrypted document\n"));
		return (-1);
	    }
	  else
	    {
		wvSetPassword (password, &ps);
		if (wvDecrypt97 (&ps))
		  {
		      wvError (("Incorrect Password\n"));
		      return (-1);
		  }
	    }
      }
    else if (ret == 7)
      {
	  ret = 0;
	  if (password == NULL)
	    {
		wvError (
			 ("Password required, this is an encrypted document\n"));
		return (-1);
	    }
	  else
	    {
		wvSetPassword (password, &ps);
		if (wvDecrypt95 (&ps))
		  {
		      wvError (("Incorrect Password\n"));
		      return (-1);
		  }
	    }
      }

    if (ret)
      {
	  wvError (("startup error with file %s\n", argv[1]));
	  wvOLEFree (&ps);
	  return (2);
      }

    wvSetElementHandler (&ps, myelehandler);
    wvSetDocumentHandler (&ps, mydochandler);

    wvInitStateData (&myhandle);

    if (wvOpenConfig (&myhandle,config) == 0)
	wvError (("config file not found\n"));
    else
	ret = wvParseConfig (&myhandle);

    if (!ret)
      {
	  expandhandle.sd = &myhandle;
	  ps.userData = &expandhandle;
	  ret = wvText (&ps);
      }

    wvReleaseStateData (&myhandle);
    if (ret == 2)
	return (2);
    else if (ret != 0)
	ret = -1;
    wvOLEFree (&ps);
    return (ret);
}
int
main(int argc, char *argv[]) {
    FILE          *input;
    int           ret;
    wvParseStruct ps;

    if (argc < 2)
        usage();

    input = fopen(argv[1], "rb");
    if (!input) {
        GET_fprintf_HANDLER() (stderr, "Failed to open %s\n", argv[1]);
        return -1;
    }
    fclose(input);

    wvInit();
    ret = wvInitParser(&ps, argv[1]);

    if (ret == -1) {
        GET_fprintf_HANDLER() (stderr,
                               "%s couldn't be opened as any known word document\n",
                               argv[1]);
        return -1;
    }

    ret = wvQuerySupported(&ps.fib, NULL);

    GET_printf_HANDLER() ("Version: ");
    switch (ret & 0x7fff) {
        case WORD8:
            GET_printf_HANDLER() ("word8 or higher");
            break;

        case WORD7:
            GET_printf_HANDLER() ("word7");
            break;

        case WORD6:
            GET_printf_HANDLER() ("word6");
            break;

        case WORD5:
            GET_printf_HANDLER() ("word5");
            break;

        case WORD2:
            GET_printf_HANDLER() ("word2 (maybe)");
            break;

        default:
            GET_printf_HANDLER() ("unknown msword version");
            break;
    }

    GET_printf_HANDLER() (", Encrypted: ");
    if (ret & 0x8000)
        GET_printf_HANDLER() ("Yes\n");
    else
        GET_printf_HANDLER() ("No\n");

    wvOLEFree(&ps);
    return 0;
}
Пример #3
0
Файл: wvRTF.c Проект: AbiWord/wv
int
main (int argc, char *argv[])
{
    FILE *input;
    char *fname, *password;
    int ret;

    wvParseStruct ps;
    char *dir = NULL;

    rtfUserData ud;

    static struct option long_options[] = {
	{"charset", 1, 0, 'c'},
	{"password", 1, 0, 'p'},
	{"dir", 1, 0, 'd'},
	{"version", 0, 0, 'v'},
	{"help", 0, 0, '?'},
	{0, 0, 0, 0}
    };

    int c, index = 0;

    if (argc < 2)
      {
	  do_help ();
	  return 1;
      }

    while (1)
      {
	  c = getopt_long (argc, argv, "?vc:p:d:", long_options, &index);
	  if (c == -1)
	      break;
	  switch (c)
	    {
	    case '?':
		do_help ();
		return 0;
	    case 'v':
		do_version ();
		return 0;
	    case 'c':
		if (optarg)
		    charset = optarg;
		else
		    wvError (("No argument given to charset"));
		break;
	    case 'p':
		if (optarg)
		    password = optarg;
		else
		    wvError (("No password given to password option"));
		break;
	    case 'd':
		if (optarg)
		    dir = optarg;
		else
		    wvError (("No directory given to dir option"));
		break;
	    default:
		do_help ();
		return -1;
	    }
      }

    if (optind >= argc)
      {
	  fprintf (stderr, "No file name given to open\n");
	  return -1;
      }

    fname = argv[optind];

    input = fopen (fname, "rb");
    if (!input)
      {
	fprintf (stderr, "Failed to open %s\n", fname);
	  return -1;
      }
    fclose (input);

    wvInit ();
    ret = wvInitParser (&ps, fname);
    ps.filename = fname;
    ps.dir = dir;

    /* set to 0 */
    memset (&ud, 1, sizeof (rtfUserData));
    ps.userData = &ud;

    if (ret & 0x8000)		/* Password protected? */
      {
	  if ((ret & 0x7fff) == WORD8)
	    {
		ret = 0;
		if (password == NULL)
		  {
		      fprintf (stderr,
			       "Password required, this is an encrypted document\n");
		      return -1;
		  }
		else
		  {
		      wvSetPassword (password, &ps);
		      if (wvDecrypt97 (&ps))
			{
			    wvError (("Incorrect Password\n"));
			    return -1;
			}
		  }
	    }
	  else if (((ret & 0x7fff) == WORD7) || ((ret & 0x7fff) == WORD6))
	    {
		ret = 0;
		if (password == NULL)
		  {
		      fprintf (stderr,
			       "Password required, this is an encrypted document\n");
		      return -1;
		  }
		else
		  {
		      wvSetPassword (password, &ps);
		      if (wvDecrypt95 (&ps))
			{
			    wvError (("Incorrect Password\n"));
			    return -1;
			}
		  }
	    }
      }

    if (ret)
      {
	  wvError (("startup error\n"));
	  wvOLEFree (&ps);
	  return -1;
      }

    wvSetElementHandler (&ps, eleProc);
    wvSetDocumentHandler (&ps, docProc);
    wvSetCharHandler (&ps, charProc);
    wvSetSpecialCharHandler (&ps, specCharProc);

    wvText (&ps);

    /* free associated memory */
    wvOLEFree (&ps);

    return 0;
}