示例#1
0
/*
 * Take apart an input zTXT and ouput its components into separate files
 * Returns 1 on success, 0 otherwise.
 */
static int
deconstruct_ztxt(ztxt *ztxtdb, char *inputfile)
{
  char          *inputbuf = NULL;
  long          insize;
  long          i;
  int           x;
  int           infile;
  int           outfile;
  struct stat   instats;
  u_int         filesize;

  if (strcmp(inputfile, "-") != 0)
    {
      /* Reading a normal input file */

      if (options.output_filename == NULL)
        {
          /* Form output filename for text */
          options.output_filename = strdup(inputfile);
          options.output_filename =
            (char *)realloc(options.output_filename,
                            (strlen(options.output_filename) + 10));

          strip_filename(options.output_filename);
          strcat(options.output_filename, ".txt");
        }

      /* Allocate the input buffer */
      x = stat(inputfile, &instats);
      errorif(x == -1);
      if (instats.st_size == 0)
        {
          fprintf(stderr, "Input contains no data.  Aborting.\n");
          free(options.output_filename);
          return 0;
        }
      inputbuf = (char *)malloc(instats.st_size + 1);
      errorif(inputbuf == NULL);

      /* Load and process the input file */
      infile = open(inputfile, O_RDONLY|O_BINARY);
      errorif(infile == -1);
      filesize = read(infile, inputbuf, instats.st_size);
      errorif(filesize == -1);
      close(infile);

      ztxt_set_output(ztxtdb, inputbuf, filesize);
    }
  else
    {
      /* Reading input from stdin */
      if (options.output_filename == NULL)
        {
          fprintf(stderr, "You must specify an output file with -o when "
                  "reading from stdin\n");
          return 0;
        }
      else if (!options.title_set)
        {
          fprintf(stderr, "You must specify a title with -t when reading "
                  "from stdin\n");
          free(options.output_filename);
          return 0;
        }

      /* Allocate the input buffer */
      inputbuf = (char *)malloc(10 * 1024);
      errorif(inputbuf == NULL);
      insize = 10 * 1024;
      i = 0;

      /* Read in all the data from stdin */
      while (!feof(stdin))
        {
          if (i >= insize - 1024)
            {
              inputbuf = (char *)realloc(inputbuf, insize + (10 * 1024));
              insize += 10 * 1024;
            }
          inputbuf[i] = fgetc(stdin);
          i++;
        }

      if (i == 0)
        {
          fprintf(stderr, "No data read from stdin.  Aborting.\n");
          free(inputbuf);
          free(options.output_filename);
          return 0;
        }
      inputbuf[i] = '\0';

      ztxt_set_output(ztxtdb, inputbuf, i);
    }

  /* Dismantle zTXT database */
  if (!ztxt_disect(ztxtdb))
    {
      fprintf(stderr,
              "An error occured while decompressing the zTXT database.\n"
              "The zTXT file may be corrupt.\n");
      free(options.output_filename);
      return 0;
    }


  /* Output decompressed text */
  outfile = open(options.output_filename, O_WRONLY|O_CREAT|O_TRUNC,
                 S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH);
  errorif(outfile == -1);

  x = write(outfile, ztxt_get_input(ztxtdb), ztxt_get_inputsize(ztxtdb));
  errorif(x != ztxt_get_inputsize(ztxtdb));

  close(outfile);


  /* Output bookmarks to file */
  if (options.bmrk_filename)
    output_bookmarks(ztxtdb, options.bmrk_filename);


  /* Output annotations to file */
  if (options.anno_filename)
    output_annotations(ztxtdb, options.anno_filename);


  /* Free memory */
  free(options.output_filename);
  // inputbuf does not need to be freed.  It has been handed over to the ztxt
  // db structure and will be freed when that structure is.

  return 1;
}
示例#2
0
/*
 * Create a zTXT with user supplied options and data.
 * Returns 0 on error, 1 if successful.
 */
static int
create_ztxt(ztxt *ztxtdb, char *inputfile)
{
  char          *inputbuf = NULL;
  long          insize;
  long          i;
  char          temptitle[255];
  int           x;
  int           infile;
  int           outfile;
  struct stat   instats;
  u_int         filesize;

  if (strcmp(inputfile, "-") != 0)
    {
      /* Reading a normal input file */

      if (!options.title_set)
        {
          strncpy(temptitle, inputfile, dmDBNameLength);
          strip_filename(temptitle);
          temptitle[dmDBNameLength] = '\0';
          ztxt_set_title(ztxtdb, temptitle);
        }

      if (options.output_filename == NULL)
        {
          /* Form DB filename */
          options.output_filename = strdup(inputfile);
          options.output_filename =
            (char *)realloc(options.output_filename,
                            (strlen(options.output_filename) + 10));

          strip_filename(options.output_filename);
          strcat(options.output_filename, ".pdb");
        }

      /* Allocate the input buffer */
      x = stat(inputfile, &instats);
      errorif(x == -1);
      if (instats.st_size == 0)
        {
          fprintf(stderr, "Input contains no data.  Aborting.\n");
          free(options.output_filename);
          return 0;
        }
      inputbuf = (char *)malloc(instats.st_size + 1);
      errorif(inputbuf == NULL);

      /* Load and process the input file */
      infile = open(inputfile, O_RDONLY);
      errorif(infile == -1);
      filesize = read(infile, inputbuf, instats.st_size);
      errorif(filesize == -1);
      close(infile);

      ztxt_set_data(ztxtdb, inputbuf, filesize);
    }
  else
    {
      /* Reading input from stdin */
      if (options.output_filename == NULL)
        {
          fprintf(stderr, "You must specify an output file with -o when "
                  "reading from stdin\n");
          return 0;
        }
      else if (!options.title_set)
        {
          fprintf(stderr, "You must specify a title with -t when reading "
                  "from stdin\n");
          free(options.output_filename);
          return 0;
        }

      /* Allocate the input buffer */
      inputbuf = (char *)malloc(10 * 1024);
      errorif(inputbuf == NULL);
      insize = 10 * 1024;
      i = 0;

      /* Read in all the data from stdin */
      while (!feof(stdin))
        {
          if (i >= insize - 1024)
            {
              inputbuf = (char *)realloc(inputbuf, insize + (10*1024));
              insize += 10*1024;
            }
          fgets(&(inputbuf[i]), 512, stdin);
          i += strlen(&(inputbuf[i]));
        }

      if (i == 0)
        {
          fprintf(stderr, "No data read from stdin.  Aborting.\n");
          free(inputbuf);
          free(options.output_filename);
          return 0;
        }
      inputbuf[i] = '\0';

      ztxt_set_data(ztxtdb, inputbuf, i);
    }


  /* Read in user regex file */
  if (!load_regex(ztxtdb))
    return 0;

  /* Read in bookmark list, if any */
  if (options.bmrk_filename)
    add_bookmark_list(ztxtdb);

  /* Read in annotation list, if any */
  if (options.anno_filename)
    add_annotation_list(ztxtdb);


  /***************
   * Make a zTXT *
   ***************/
  x = ztxt_process(ztxtdb, options.adjust_type, options.line_length);
  if (x > 0)
    {
      fprintf(stderr, "An error occured during the compression phase.\n"
              "Exiting...\n");
      free(inputbuf);
      free(options.output_filename);
      return 0;
    }

  if (options.list_bmrks)
    ztxt_list_bookmarks(ztxtdb);

  ztxt_generate_db(ztxtdb);


  /* Output database */
  outfile = open(options.output_filename, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY,
                 S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH);
  errorif(outfile == -1);

  x = write(outfile, ztxt_get_output(ztxtdb), ztxt_get_outputsize(ztxtdb));
  errorif(x != ztxt_get_outputsize(ztxtdb));

  close(outfile);


  /* Free some memory */
  free(inputbuf);
  free(options.output_filename);

  return 1;
}
示例#3
0
int main (int argc, char **argv)
{
  FILE *ifile;
  uint8_t *buffer, *ptr;
  uint32_t buflen, readfromfile;
  uint32_t offset;
  int done_with_buf;
  mpeg2t_t *mpeg2t;
  mpeg2t_es_t *es_pid;
  mpeg2t_pid_t *pidptr;
  bool verbose = false;
  fpos_t pos;
  const char *ProgName = argv[0];
  const char *usage = "";
  uint16_t pid = 0;
  const char *filename;

  //  int lastcc, ccset;
  while (true) {
    int c = -1;
    int option_index = 0;
    static struct option long_options[] = {
      { "help", 0, 0, '?' },
      { "version", 0, 0, 'v'},
      { "verbose", 0, 0, 'V'},
      { "pid", 1, 0, 'p' },
      { NULL, 0, 0, 0 }
    };

    c = getopt_long_only(argc, argv, "?vVp:",
			 long_options, &option_index);

    if (c == -1)
      break;
    
    switch (c) {
    case '?':
      printf("%s", usage);
      exit(1);
    case 'V':
      verbose = true;
      break;
    case 'v':
      printf("%s - %s version %s", 
	      ProgName, MPEG4IP_PACKAGE, MPEG4IP_VERSION);
      exit(1);
    case 'p': {
      int readval;
      if (sscanf(optarg, "%i", &readval) != 1) {
	fprintf(stderr, "can't read pid value %s\n", optarg);
	exit(1);
      }
      if (readval > 0x1fff) {
	fprintf(stderr, "illegal pid value %s\n", optarg);
      }
      pid = readval;
    }
    }
  }

  if (pid == 0) {
    fprintf(stderr, "must specify pid to extract\n");
    exit(-1);
  }
   
  filename = argv[optind];

  buffer = (uint8_t *)malloc(BUFFER_SIZE);

  if (verbose)
    mpeg2t_set_loglevel(LOG_DEBUG);
  else
    mpeg2t_set_loglevel(LOG_ERR);
  mpeg2t = create_mpeg2_transport();
  mpeg2t->save_frames_at_start = 1;
  ifile = fopen(filename, FOPEN_READ_BINARY);
  if (ifile == NULL) {
    fprintf(stderr, "Couldn't open file %s\n", filename);
    exit(1);
  }

  buflen = 0;
  readfromfile = 0;
  char outfilename[MAXPATHLEN], spid[20];
  strip_filename(filename, outfilename);
  sprintf(spid, ".%u", pid);
  strcat(outfilename, spid);
    FILE *ofile;
    //    bool has_pat = false;
    ofile = fopen(outfilename, FOPEN_WRITE_BINARY);

  //lastcc = 0;
  while (!feof(ifile)) {
    if (buflen > 0) {
      memmove(buffer, buffer + readfromfile - buflen, buflen);
    }
    fgetpos(ifile, &pos);
    uint64_t position;
    FPOS_TO_VAR(pos, uint64_t, position);
    fprintf(stdout, "file pos %llu\n", position);
    readfromfile = buflen + fread(buffer + buflen, 1, BUFFER_SIZE - buflen, ifile);
    buflen = readfromfile;
    ptr = buffer;
    done_with_buf = 0;


    while (done_with_buf == 0) {
      pidptr = mpeg2t_process_buffer(mpeg2t, ptr, buflen, &offset);
      ptr += offset;
      buflen -= offset;
      if (buflen < 188) {
	done_with_buf = 1;
      }
      if (pidptr != NULL && pidptr->pak_type == MPEG2T_ES_PAK) {
	mpeg2t_frame_t *p;
	es_pid = (mpeg2t_es_t *)pidptr;

	while ((p = mpeg2t_get_es_list_head(es_pid)) != NULL) {
	  if (pidptr->pid == pid) {
	    printf("Pid %x %d frame len %5d psts %d "U64" dts %d "U64"\n", 
		   pidptr->pid,
		   es_pid->stream_type,
		   p->frame_len,
		   p->have_ps_ts, p->ps_ts,
		   p->have_dts, p->dts);
	    
	    fwrite(p->frame, 1, p->frame_len, ofile);
	  }
	  mpeg2t_free_frame(p);
	}
      }
    }
  }
  free(buffer);
  fclose(ifile);
  fclose(ofile);
  return 0;
}
示例#4
0
/*
 * ServiceMain()
 *
 * Main routine for HIP service. Runs init_hip()
 */
void WINAPI ServiceMain (DWORD ac, char **av)
{
  char path[MAX_PATH];
  g_srv_status_handle = RegisterServiceCtrlHandler(SERVICE_NAME, Handler);
  if (!g_srv_status_handle)
    {
      return;
    }

  g_srv_status.dwCurrentState = SERVICE_START_PENDING;
  g_srv_status.dwCheckPoint = 0;
  g_srv_status.dwWaitHint = 1000;
  SetServiceStatus (g_srv_status_handle, &g_srv_status);

  /* initialization process */
  if (!GetModuleFileName (0, path, MAX_PATH))
    {
      return;
    }

  strip_filename(path);
  _chdir(path);

  if (freopen("hip_ipsec_error.log", "a", stderr) == NULL)
    {
      return;
    }
  if (freopen("hip_ipsec.log", "a", stdout) == NULL)
    {
      return;
    }
  init_reg();
  init_hip(ac, av);

  /* notify that the service has been started */
  g_srv_status.dwCurrentState = SERVICE_RUNNING;
  g_srv_status.dwCheckPoint = 0;
  g_srv_status.dwWaitHint = 0;
  SetServiceStatus(g_srv_status_handle, &g_srv_status);

  while (g_srv_status.dwCurrentState != SERVICE_STOPPED)
    {
      switch (g_srv_status.dwCurrentState)
        {
        case SERVICE_STOP_PENDING:
          g_srv_status.dwCurrentState = SERVICE_STOPPED;
          SetServiceStatus(g_srv_status_handle, &g_srv_status);
          g_state = 1;
          break;
        default:
          Sleep(1000);
          /* TODO: any HIP status updating here */
          /*ret = hip_check_status();
           *  if (ret >= 0 && ret != status) {
           *       status = ret;
           *       update_status(status);
           *  }*/
          break;
        }
    }

  WSACleanup();
  hip_sadb_deinit();
}
static int xmp_read(const char *path, char *buf, size_t size, off_t offset,
   struct fuse_file_info *fi)
{
int fd;
int res;
char *buffer_t;
char *temp_file_name;
char *new_file;
(void) fi;
struct request_potato vrequest_potato;
struct reply_potato vreply_potato;
vrequest_potato.Opcode=2;
strcpy(vrequest_potato.fpath,path);
vrequest_potato.buffer_size=size;
vrequest_potato.buffer_offset=offset;
vrequest_potato.mode=fi->flags;

//check with tmp

int temp_filefd;
new_file=malloc(strlen(path)+2);
new_file=strip_filename(path);
temp_file_name=malloc((sizeof(char)*strlen(new_file))+5);
strcpy(temp_file_name,"/tmp/");
strcat(temp_file_name,new_file);
printf("temp file name = %s",temp_file_name);


temp_filefd=open(temp_file_name,O_RDONLY,0666);
//check file in tmp
if(temp_filefd==-1){
printf("\nFile not found in local");
vrequest_potato.cached_t=0;
}else{
printf("\nFile found in local");
vrequest_potato.cached_t=1;
close(temp_filefd);
}

vreply_potato=pass_msg(vrequest_potato);
if(vrequest_potato.cached_t==1){

	if(vreply_potato.update==1){
	//Receive file 
	char *buffer_t;
	printf("Bytes to be received = %d",vreply_potato.st_size_t);
	buffer_t=malloc(sizeof(char)*vreply_potato.st_size_t);
	int recvttl,lenr;
	for(recvttl=0,lenr=1;recvttl< vreply_potato.st_size_t && lenr!=0;recvttl+=lenr)
	    {
		lenr=recv(server_sock,(buffer_t+recvttl),vreply_potato.st_size_t-recvttl,0);
		if(lenr<0)
		{
		  perror("Recv");
		  exit(1);
		}
		printf("Recv returned: %d\n",lenr);
	    }

	//Put it in local 
	int local_fd;
	if((local_fd=open(temp_file_name,O_CREAT|O_RDWR|O_TRUNC,0666))<0){
	perror("\nLocal File cannot be created");
	}else{
	printf("\nfile created in local\n");
	printf("\nreceived buffer %s\n",buffer_t);
	write(local_fd,buffer_t,vreply_potato.st_size_t);
	close(local_fd);
	printf("\nfile written in local\n");
	}
	//Read file and close
	fd=open(temp_file_name,fi->flags);
		if (fd == -1)
			return -errno;
		res = pread(fd, buf, size, offset);
		if (res == -1)
			return -errno;
		close(fd);


	}else if(vreply_potato.update==0){
	//Reads from local
		fd=open(temp_file_name,fi->flags);
		if (fd == -1)
			return -errno;
		res = pread(fd, buf, size, offset);
		if (res == -1)
			return -errno;
		close(fd);
	}else{
		perror("read failed");
	}

}else if(vrequest_potato.cached_t==0){
	//Receive file 
	char *buffer_t;
	printf("Bytes to be received = %d",vreply_potato.st_size_t);
	buffer_t=malloc(sizeof(char)*vreply_potato.st_size_t);
	int recvttl,lenr;
	for(recvttl=0,lenr=1;recvttl< vreply_potato.st_size_t && lenr!=0;recvttl+=lenr)
	    {
		lenr=recv(server_sock,(buffer_t+recvttl),vreply_potato.st_size_t-recvttl,0);
		if(lenr<0)
		{
		  perror("Recv");
		  exit(1);
		}
		printf("Recv returned: %d\n",lenr);
	    }

	//Put it in local 
	int local_fd;
	if((local_fd=open(temp_file_name,O_CREAT|O_RDWR|O_TRUNC,0666))<0){
	perror("\nLocal File cannot be created");
	}else{
	printf("\nfile created in local\n");
	printf("\nreceived buffer %s\n",buffer_t);
	write(local_fd,buffer_t,vreply_potato.st_size_t);
	close(local_fd);
	printf("\nfile written in local\n");
	}
	//Read file and close
	char * read_buffer;
	read_buffer=malloc(sizeof(char)*size);
	fd=open(temp_file_name,O_RDONLY);
		if (fd == -1)
			return -errno;
		res = pread(fd, read_buffer, size, offset);
		if (res == -1)
			return -errno;
		strcpy(buf,read_buffer);
		close(fd);
}else{
perror("Should not come here");
}


return vreply_potato.rc;

/*

vreply_potato=pass_msg(vrequest_potato);




if(vreply_potato.rc==-1)
return vreply_potato.errno_t;

buffer_t=malloc(sizeof(char)*vreply_potato.rc);
int recvttl,lenr;
for(recvttl=0,lenr=1;recvttl< vreply_potato.rc && lenr!=0;recvttl+=lenr)
    {
        lenr=recv(server_sock,(buffer_t+recvttl),vreply_potato.rc-recvttl,0);
        if(lenr<0)
        {
          perror("Recv");
          exit(1);
        }
        printf("Recv returned: %d\n",lenr);
    }
strcpy(buf,buffer_t);

return vreply_potato.rc;
*/

}
示例#6
0
/* Look for the first matching album art bitmap in the following list:
 *  ./<trackname><size>.{jpeg,jpg,bmp}
 *  ./<albumname><size>.{jpeg,jpg,bmp}
 *  ./cover<size>.bmp
 *  ../<albumname><size>.{jpeg,jpg,bmp}
 *  ../cover<size>.{jpeg,jpg,bmp}
 *  ROCKBOX_DIR/albumart/<artist>-<albumname><size>.{jpeg,jpg,bmp}
 * <size> is the value of the size_string parameter, <trackname> and
 * <albumname> are read from the ID3 metadata.
 * If a matching bitmap is found, its filename is stored in buf.
 * Return value is true if a bitmap was found, false otherwise.
 *
 * If the first symbol in size_string is a colon (e.g. ":100x100")
 * then the colon is skipped ("100x100" will be used) and the track
 * specific image (./<trackname><size>.bmp) is tried last instead of first.
 */
bool search_albumart_files(const struct mp3entry *id3, const char *size_string,
                           char *buf, int buflen)
{
    char path[MAX_PATH + 1];
    char dir[MAX_PATH + 1];
    bool found = false;
    int track_first = 1;
    int pass;
    const char *trackname;
    const char *artist;
    int dirlen;
    int albumlen;
    int pathlen;

    if (!id3 || !buf)
        return false;

    trackname = id3->path;

    if (strcmp(trackname, "No file!") == 0)
        return false;

    if (*size_string == ':')
    {
        size_string++;
        track_first = 0;
    }

    strip_filename(dir, sizeof(dir), trackname);
    dirlen = strlen(dir);
    albumlen = id3->album ? strlen(id3->album) : 0;

    for(pass = 0; pass < 2 - track_first; pass++)
    {
        if (track_first || pass)
        {
            /* the first file we look for is one specific to the
               current track */
            strip_extension(path, sizeof(path) - strlen(size_string) - 4,
                            trackname);
            strcat(path, size_string);
            strcat(path, "." EXT);
#ifdef USE_JPEG_COVER
            pathlen = strlen(path);
#endif
            found = try_exts(path, pathlen);
        }
        if (pass)
            break;
        if (!found && albumlen > 0)
        {
            /* if it doesn't exist,
            * we look for a file specific to the track's album name */
            pathlen = snprintf(path, sizeof(path),
                               "%s%s%s." EXT, dir, id3->album, size_string);
            fix_path_part(path, dirlen, albumlen);
            found = try_exts(path, pathlen);
        }

        if (!found)
        {
            /* if it still doesn't exist, we look for a generic file */
            pathlen = snprintf(path, sizeof(path),
                               "%scover%s." EXT, dir, size_string);
            found = try_exts(path, pathlen);
        }

#ifdef USE_JPEG_COVER
        if (!found && !*size_string)
        {
            snprintf (path, sizeof(path), "%sfolder.jpg", dir);
            found = file_exists(path);
        }
#endif

        artist = id3->albumartist != NULL ? id3->albumartist : id3->artist;

        if (!found && artist && id3->album)
        {
            /* look in the albumart subdir of .rockbox */
            pathlen = snprintf(path, sizeof(path),
                               ROCKBOX_DIR "/albumart/%s-%s%s." EXT,
                               artist,
                               id3->album,
                               size_string);
            fix_path_part(path, strlen(ROCKBOX_DIR "/albumart/"), MAX_PATH);
            found = try_exts(path, pathlen);
        }

        if (!found)
        {
            /* if it still doesn't exist,
            * we continue to search in the parent directory */
            strcpy(path, dir);
            path[dirlen - 1] = 0;
            strip_filename(dir, sizeof(dir), path);
            dirlen = strlen(dir);
        }

        /* only try parent if there is one */
        if (dirlen > 0)
        {
            if (!found && albumlen > 0)
            {
                /* we look in the parent directory
                * for a file specific to the track's album name */
                pathlen = snprintf(path, sizeof(path),
                                   "%s%s%s." EXT, dir, id3->album, size_string);
                fix_path_part(path, dirlen, albumlen);
                found = try_exts(path, pathlen);
            }

            if (!found)
            {
                /* if it still doesn't exist, we look in the parent directory
                * for a generic file */
                pathlen = snprintf(path, sizeof(path),
                                   "%scover%s." EXT, dir, size_string);
                found = try_exts(path, pathlen);
            }
        }
        if (found)
            break;
    }

    if (!found)
        return false;

    strlcpy(buf, path, buflen);
    logf("Album art found: %s", path);
    return true;
}