Exemplo n.º 1
1
static int fill_packet_struct_serial(struct emwin_packet *ep, 
				     char *serialdata, int datasize){
  int i;
  char *fmt = "/PF%12s /PN %d /PT %d /CS %d /FD%22c";
  char *b;

  /*
   * This (ep->bbdata) is the bb stuff that we get and what we will retransmit.
   * We should really retransmit the cleaned stuff (but of course
   * that would not a BB server).
   *
   * Note: Encrypt the data portion as well as the first and last six bytes.
   *       If those should not be encrypted, then the loop below should read
   *
   *	 memset(ep->bbdata, '\0', EMWIN_PACKET_SIZE);
   *     for(i = EMWIN_NULLPAD_SIZE;
   *		i < datasize - EMWIN_NULLPAD_SIZE; ++i){
   *		ep->bbdata[i] = serialdata[i] ^ 0xff;
   *	 }
   */
  /*  bb_xor(ep->bbdata, serialdata, data_size); */
  for(i = 0; i < datasize; ++i){
    ep->bbdata[i] = serialdata[i] ^ 0xff;
  }
  ep->bbdata_size = datasize;

  /* 
   * Get rid of the initial and final 6 NULL's that are always present.
   */
  datasize -= EMWIN_NULLPAD_SIZE * 2;
  for(i = 0; i < datasize; ++i){
    ep->rawdata[i] = serialdata[i + EMWIN_NULLPAD_SIZE];
  }
  ep->rawdata_size = datasize;

  /*
   * let ep->data point to the real data.
   */
  ep->data = &(ep->rawdata[EMWIN_HEADER_SIZE]);
  ep->data_size = datasize - EMWIN_HEADER_SIZE;

  /*
   * Form the V1 and V2 queue packets, as for the network case.
   */
  memcpy(&ep->queue_data_v1[EMWIN_QUEUE_ENVELOPE_SIZE],
	 ep->bbdata, ep->bbdata_size);
  pack_uint32(ep->queue_data_v1, (uint32_t)ep->bbdata_size, 0);
  ep->queue_data_v1_size = EMWIN_QUEUE_ENVELOPE_SIZE + ep->bbdata_size;

  if((g.serverprotocol == PROTOCOL_EMWIN1) || (build_queue_data_v2(ep) != 0) ||
     (ep->compress_ratio >= g.min_compress_ratio)){
    memcpy(&ep->queue_data_v2[EMWIN_QUEUE_ENVELOPE_SIZE],
	   ep->bbdata, ep->bbdata_size);
    pack_uint32(ep->queue_data_v2, (uint32_t)ep->bbdata_size, 0);
    ep->queue_data_v2_size = EMWIN_QUEUE_ENVELOPE_SIZE + ep->bbdata_size;
  }

  update_stats_frames_received((unsigned int)ep->bbdata_size);
  
  i = sscanf(ep->rawdata, fmt, ep->header.filename, &ep->header.blockno, 
	     &ep->header.numblocks, &ep->header.checksum, &ep->header.dtstamp);
  /*
   *  log_info("%d of %d", ep->header.blockno, ep->header.numblocks); 
   */

  if(i < 5){
    update_stats_frames(1);
    return(2);	/* error in header format */
  }

  ep->header.dtstamp[EMWIN_TIMESTAMP_LEN - 1] = '\0';

  if(checksum(ep) != 0){
    update_stats_frames(1);
    return(3);
  }

  /*
   * This next function checks the file name and also converts it
   * to lower case.
   */
  if(checkfilename(ep) != 0){
    update_stats_frames(1);
    return(4);
  }

  /*
   * Sometimes, if the 1024 block does not contain enough data,
   * it is filled with NULL's. We get rid of them, but only if
   * it is txt file.
   */
  if(is_text_file(ep->header.filename)){

    b = &(ep->data[0]);
    i = 0;
    while((*b != '\0') && (i < ep->data_size)){
      ++i;
      ++b;
    }

    ep->data_size = i;
  }

  update_stats_frames(0);

  return(0);
}
Exemplo n.º 2
1
static int fill_packet_struct_bb(struct emwin_packet *ep, 
				      char *bbdata, int datasize){
  /*
   * The width of the string fields in fmt depend on what is defined
   * in emwin.h, for the length of the file name and the time stamp.
   * If the file name has exactly 12 characters this format PF%12s/PN is
   * correct. But if it has less, and the rest are blanks, the /PN
   * will not match.  For that reason we write it as "PF%12s /PN"
   * because that space before the /PN is actually discarded.
   */  
  int i;
  /*   char *fmt = "/PF%12s/PN %d /PT %d /CS %d /FD%31s"; */
  char *fmt = "/PF%12s /PN %d /PT %d /CS %d /FD%22c";
  char *b;

  /*
   * This (ep->bbdata) is the bb stuff that we get and what we will retransmit.
   * We should really retransmit the cleaned stuff (but of course
   * that would not a BB server). On the other hand, if we retransmit
   * just the cleaned data (and write the program to be prepared
   * to get the data as such) we can reduce bandwidth significantly
   * by eliminating the transmission of unnecessary stuff
   * (NULL's, padding, etc) that these packets contain.
   */
  memcpy(ep->bbdata, bbdata, datasize);
  ep->bbdata_size = datasize;

  /* 
   * Get the unxored header + data block.
   * Get rid of the initial and final 6 NULL's
   * that are always present, and decode each character.
   * ep->rawdata then contains the unxored header + data block.
   */
  datasize -= (EMWIN_NULLPAD_SIZE * 2);
  for(i = 0; i < datasize; ++i){
    ep->rawdata[i] = bbdata[i + EMWIN_NULLPAD_SIZE] ^ 0xff;
  }
  ep->rawdata_size = datasize;
  
  /*
   * ep->data points to the start of the data block.
   */
  ep->data = &(ep->rawdata[EMWIN_HEADER_SIZE]);
  ep->data_size = datasize - EMWIN_HEADER_SIZE;

  /*
   * The V1 queue packet is just the bbdata, prepended by the size. The V2
   * depends on the raw (unxored) data.
   */
  memcpy(&ep->queue_data_v1[EMWIN_QUEUE_ENVELOPE_SIZE],
	 ep->bbdata, ep->bbdata_size);
  pack_uint32(ep->queue_data_v1, (uint32_t)ep->bbdata_size, 0);
  ep->queue_data_v1_size = EMWIN_QUEUE_ENVELOPE_SIZE + ep->bbdata_size;

  /*
   * For V2 we use the function in v2.c. If it fails, fall back to a V1
   * style queue packet as above.
   *
   * Moreover, if the configuration has specified a V1 only server, V2 clients
   * get V1 packages only.
   */
  if((g.serverprotocol == PROTOCOL_EMWIN1) || (build_queue_data_v2(ep) != 0) ||
     (ep->compress_ratio >= g.min_compress_ratio)){
    memcpy(&ep->queue_data_v2[EMWIN_QUEUE_ENVELOPE_SIZE],
	   ep->bbdata, ep->bbdata_size);
    pack_uint32(ep->queue_data_v2, (uint32_t)ep->bbdata_size, 0);
    ep->queue_data_v2_size = EMWIN_QUEUE_ENVELOPE_SIZE + ep->bbdata_size;
  }

  update_stats_frames_received((unsigned int)ep->bbdata_size);

  /*
   * Get the elements of the header.
   */  
  i = sscanf(ep->rawdata, fmt, ep->header.filename, &ep->header.blockno, 
	     &ep->header.numblocks, &ep->header.checksum, &ep->header.dtstamp);

  if(i < 5){
    update_stats_frames(1);
    return(2);	/* error in header format */
  }

  ep->header.dtstamp[EMWIN_TIMESTAMP_LEN - 1] = '\0';

  if(checksum(ep) != 0){
    update_stats_frames(1);
    return(3);
  }

  /*
   * This next function checks the file name and also converts it
   * to lower case.
   */
  if(checkfilename(ep) != 0){
    update_stats_frames(1);
    return(4);
  }

  /*
   *  log_verbose("%s: %d of %d", ep->header.filename,
   *	ep->header.blockno, ep->header.numblocks); 
   */

  /*
   * Sometimes, if the 1024 block does not contain enough data,
   * it is filled with NULL's. We get rid of them, but only if
   * it is txt file.
   */
  if(is_text_file(ep->header.filename)){

    b = &(ep->data[0]);
    i = 0;
    while((*b != '\0') && (i < ep->data_size)){
      ++i;
      ++b;
    }

    ep->data_size = i;
  }

  update_stats_frames(0);

  return(0);
}
Exemplo n.º 3
0
void read_stl_file(const char *filename, std::vector<GTri3>& TriList)
{
	if (is_text_file(filename))
		read_stl(filename, TriList);
	else
		load_stl(filename, TriList);
}
Exemplo n.º 4
0
void problem_data_load_from_dump_dir(problem_data_t *problem_data, struct dump_dir *dd, char **excluding)
{
    char *short_name;
    char *full_name;

    dd_init_next_file(dd);
    while (dd_get_next_file(dd, &short_name, &full_name))
    {
        if (excluding && is_in_string_list(short_name, excluding))
        {
            //log("Excluded:'%s'", short_name);
            goto next;
        }

        if (short_name[0] == '#'
         || (short_name[0] && short_name[strlen(short_name) - 1] == '~')
        ) {
            //log("Excluded (editor backup file):'%s'", short_name);
            goto next;
        }

        ssize_t sz = 4*1024;
        char *text = NULL;
        bool editable = is_editable_file(short_name);

        if (!editable)
        {
            text = is_text_file(full_name, &sz);
            if (!text)
            {
                problem_data_add(problem_data,
                        short_name,
                        full_name,
                        CD_FLAG_BIN + CD_FLAG_ISNOTEDITABLE
                );
                goto next;
            }
        }

        char *content;
        if (sz < 4*1024) /* did is_text_file read entire file? */
        {
            /* yes */
            content = text;
        }
        else
        {
            /* no, need to read it all */
            free(text);
            content = dd_load_text(dd, short_name);
        }
        /* Strip '\n' from one-line elements: */
        char *nl = strchr(content, '\n');
        if (nl && nl[1] == '\0')
            *nl = '\0';

        /* Sanitize possibly corrupted utf8.
         * Of control chars, allow only tab and newline.
         */
        char *sanitized = sanitize_utf8(content,
                (SANITIZE_ALL & ~SANITIZE_LF & ~SANITIZE_TAB)
        );
        if (sanitized)
        {
            free(content);
            content = sanitized;
        }

        int flags = 0;

        if (editable)
            flags |= CD_FLAG_TXT | CD_FLAG_ISEDITABLE;
        else
            flags |= CD_FLAG_TXT | CD_FLAG_ISNOTEDITABLE;

        static const char *const list_files[] = {
            FILENAME_UID       ,
            FILENAME_PACKAGE   ,
            FILENAME_EXECUTABLE,
            FILENAME_TIME      ,
            FILENAME_COUNT     ,
            NULL
        };
        if (is_in_string_list(short_name, (char**)list_files))
            flags |= CD_FLAG_LIST;

        if (strcmp(short_name, FILENAME_TIME) == 0)
            flags |= CD_FLAG_UNIXTIME;

        problem_data_add(problem_data,
                short_name,
                content,
                flags
        );
        free(content);
 next:
        free(short_name);
        free(full_name);
    }
}
Exemplo n.º 5
0
static void bloom_add_folder(path *path, FILE *bloom_file) {
  char *oldnull = *path + strlen(*path);
  DIR *d = opendir(*path);
  if (d == NULL) {
    puts("warning: opendir failed");
    return;
  }
  
  struct dirent e;
  struct dirent *e2;
  while (1) {
    if (readdir_r(d, &e, &e2) != 0) {
      puts("warning: readdir failed");
      closedir(d);
      return;
    }
    if (e2 == NULL) {
      closedir(d);
      return;
    }
    if (strlen(*path) + strlen(e.d_name) + 1 + 1 > sizeof(*path)) {
      puts("warning: blocked traversal - path too long");
      closedir(d);
      return;
    }
    if (e.d_name[0] == '.') {
      continue;
    }
    strcat(*path, e.d_name);
    FILE *f;
    switch (e.d_type) {
      case DT_UNKNOWN:
        puts("warning: dirent with unknown type, too lazy to stat it");
        break;
      case DT_DIR:
        if (!folder_bad(e.d_name)) {
          strcat(*path, "/");
          bloom_add_folder(path, bloom_file);
        }
        break;
      case DT_REG:
        f = fopen(*path, "r");
        if (f == NULL) {
          puts("warning: fopen() failed");
          break;
        }
        if (!is_text_file(f)) {
          printf("ignoring %s (binary)\n", *path);
          fclose(f);
          break;
        }
        assert(fwrite(*path, strlen(*path)+1, 1, bloom_file) == 1);
        printf("blooming %s\n", *path);
        bloom_filter filter;
        memset(&filter, 0, sizeof(filter));
        make_bloom_from_file(f, filter);
        fclose(f);
        assert(fwrite(&filter, sizeof(bloom_filter), 1, bloom_file) == 1);
        break;
      default:
        break;
    }
    *oldnull = 0;
  }
}
Exemplo n.º 6
0
void load_problem_data_from_dump_dir(problem_data_t *problem_data, struct dump_dir *dd)
{
    char *short_name;
    char *full_name;

    dd_init_next_file(dd);
    while (dd_get_next_file(dd, &short_name, &full_name))
    {
        ssize_t sz = 4*1024;
        char *text = NULL;
        bool editable = is_editable_file(short_name);

        if (!editable)
        {
            text = is_text_file(full_name, &sz);
            if (!text)
            {
                add_to_problem_data_ext(problem_data,
                        short_name,
                        full_name,
                        CD_FLAG_BIN + CD_FLAG_ISNOTEDITABLE
                );
                free(short_name);
                free(full_name);
                continue;
            }
        }

        char *content;
        if (sz < 4*1024) /* did is_text_file read entire file? */
        {
            content = text;
            /* Strip '\n' from one-line elements: */
            char *nl = strchr(content, '\n');
            if (nl && nl[1] == '\0')
                *nl = '\0';
        }
        else
        {
            /* no, need to read it all */
            free(text);
            content = dd_load_text(dd, short_name);
        }

        int flags = 0;

        if (editable)
            flags |= CD_FLAG_TXT | CD_FLAG_ISEDITABLE;
        else
            flags |= CD_FLAG_TXT | CD_FLAG_ISNOTEDITABLE;

        static const char *const list_files[] = {
            FILENAME_UID       ,
            FILENAME_PACKAGE   ,
            FILENAME_EXECUTABLE,
            FILENAME_TIME      ,
            FILENAME_COUNT     ,
            NULL
        };
        if (is_in_string_list(short_name, (char**)list_files))
            flags |= CD_FLAG_LIST;

        if (strcmp(short_name, FILENAME_TIME) == 0)
            flags |= CD_FLAG_UNIXTIME;

        add_to_problem_data_ext(problem_data,
                short_name,
                content,
                flags
        );
        free(short_name);
        free(full_name);
        free(content);
    }
}