Exemple #1
0
static int find_aiff_chunk(FILE *in, char *type, unsigned int *len)
{
    unsigned char buf[8];
    int restarted = 0;

    while(1)
    {
        if(fread(buf,1,8,in) <8)
        {
            if(!restarted) {
                /* Handle out of order chunks by seeking back to the start
                 * to retry */
                restarted = 1;
                fseek(in, 12, SEEK_SET);
                continue;
            }
            fprintf(stderr, _("Warning: Unexpected EOF in AIFF chunk\n"));
            return 0;
        }

        *len = READ_U32_BE(buf+4);

        if(memcmp(buf,type,4))
        {
            if((*len) & 0x1)
                (*len)++;

            if(!seek_forward(in, *len))
                return 0;
        }
        else
            return 1;
    }
}
Exemple #2
0
static char *unarchive(struct gzip_handle *src_stream, FILE * out_stream,
		       file_header_t * (*get_headers) (struct gzip_handle *),
		       void (*free_headers) (file_header_t *),
		       const int extract_function,
		       const char *prefix, const char **extract_names, int *err)
{
	file_header_t *file_entry;
	int extract_flag;
	int i;
	char *buffer = NULL;

	*err = 0;

	archive_offset = 0;
	while ((file_entry = get_headers(src_stream)) != NULL) {
		extract_flag = TRUE;

		if (extract_names != NULL) {
			int found_flag = FALSE;
			char *p = file_entry->name;

			if (p[0] == '.' && p[1] == '/')
				p += 2;

			for (i = 0; extract_names[i] != 0; i++) {
				if (strcmp(extract_names[i], p) == 0) {
					found_flag = TRUE;
					break;
				}
			}
			if (extract_function & extract_exclude_list) {
				if (found_flag == TRUE) {
					extract_flag = FALSE;
				}
			} else {
				/* If its not found in the include list dont extract it */
				if (found_flag == FALSE) {
					extract_flag = FALSE;
				}
			}
		}

		if (extract_flag == TRUE) {
			buffer = extract_archive(src_stream, out_stream,
						 file_entry, extract_function,
						 prefix, err);
			*err = 0;	/* XXX: ignore extraction errors */
			if (*err) {
				free_headers(file_entry);
				break;
			}
		} else {
			/* seek past the data entry */
			seek_forward(src_stream, file_entry->size);
		}
		free_headers(file_entry);
	}

	return buffer;
}
Exemple #3
0
static int find_wav_chunk(FILE *in, char *type, unsigned int *len)
{
    unsigned char buf[8];

    while(1)
    {
        if(fread(buf,1,8,in) < 8) /* Suck down a chunk specifier */
        {
            fprintf(stderr, _("Warning: Unexpected EOF in reading WAV header\n"));
            return 0; /* EOF before reaching the appropriate chunk */
        }

        if(memcmp(buf, type, 4))
        {
            *len = READ_U32_LE(buf+4);
            if(!seek_forward(in, *len))
                return 0;

            buf[4] = 0;
            fprintf(stderr, _("Skipping chunk of type \"%s\", length %d\n"), buf, *len);
        }
        else
        {
            *len = READ_U32_LE(buf+4);
            return 1;
        }
    }
}
Exemple #4
0
/* Process keyboard input */
static gboolean
handle_keyboard (GIOChannel * source, GIOCondition cond, APP_STATE_T * state)
{
  gchar *str = NULL;
  char op;

  if (g_io_channel_read_line (source, &str, NULL, NULL,
          NULL) == G_IO_STATUS_NORMAL) {

    gchar *cmd = str;
    SKIP (cmd)
        op = *cmd;
    cmd++;
    switch (op) {
      case 'a':
        if (state->animate) {
          state->animate = FALSE;
        } else {
          state->animate = TRUE;
        }
        break;
      case 'p':
        pipeline_pause (state);
        break;
      case 'r':
        pipeline_play (state);
        break;
      case 'l':
        report_position_duration (state);
        break;
      case 'f':
        seek_forward (state);
        break;
      case 'b':
        seek_backward (state);
        break;
      case 'q':
        flush_start (state);
        gst_element_set_state (state->pipeline, GST_STATE_READY);
        break;
    }
  }
  g_free (str);
  return TRUE;
}
Exemple #5
0
/* Extract the data postioned at src_stream to either filesystem, stdout or
 * buffer depending on the value of 'function' which is defined in bbtargz.h
 *
 * prefix doesnt have to be just a directory, it may prefix the filename as well.
 *
 * e.g. '/var/lib/dpkg/info/dpkg.' will extract all files to the base bath
 * '/var/lib/dpkg/info/' and all files/dirs created in that dir will have
 * 'dpkg.' as their prefix
 *
 * For this reason if prefix does point to a dir then it must end with a
 * trailing '/' or else the last dir will be assumed to be the file prefix
 */
static char *extract_archive(struct gzip_handle *src_stream, FILE * out_stream,
			     const file_header_t * file_entry,
			     const int function, const char *prefix, int *err)
{
	FILE *dst_stream = NULL;
	char *full_name = NULL;
	char *full_link_name = NULL;
	char *buffer = NULL;
	struct utimbuf t;

	*err = 0;

	/* prefix doesnt have to be a proper path it may prepend
	 * the filename as well */
	if (prefix != NULL) {
		/* strip leading '/' in filename to extract as prefix may not be dir */
		/* Cant use concat_path_file here as prefix might not be a directory */
		char *path = file_entry->name;
		if (strncmp("./", path, 2) == 0) {
			path += 2;
			if (strlen(path) == 0)
				/* Do nothing, current dir already exists. */
				return NULL;
		}
		full_name = xmalloc(strlen(prefix) + strlen(path) + 1);
		strcpy(full_name, prefix);
		strcat(full_name, path);
		if (file_entry->link_name) {
			full_link_name =
			    xmalloc(strlen(prefix) +
				    strlen(file_entry->link_name) + 1);
			strcpy(full_link_name, prefix);
			strcat(full_link_name, file_entry->link_name);
		}
	} else {
		full_name = xstrdup(file_entry->name);
		if (file_entry->link_name)
			full_link_name = xstrdup(file_entry->link_name);
	}

	if (function & extract_to_stream) {
		if (S_ISREG(file_entry->mode)) {
			*err =
			    gzip_copy(src_stream, out_stream, file_entry->size);
			archive_offset += file_entry->size;
		}
	} else if (function & extract_one_to_buffer) {
		if (S_ISREG(file_entry->mode)) {
			buffer = (char *)xmalloc(file_entry->size + 1);
			gzip_read(src_stream, buffer, file_entry->size);
			buffer[file_entry->size] = '\0';
			archive_offset += file_entry->size;
			goto cleanup;
		}
	} else if (function & extract_all_to_fs) {
		struct stat oldfile;
		int stat_res;
		stat_res = lstat(full_name, &oldfile);
		if (stat_res == 0) {	/* The file already exists */
			if ((function & extract_unconditional)
			    || (oldfile.st_mtime < file_entry->mtime)) {
				if (!S_ISDIR(oldfile.st_mode)) {
					unlink(full_name);	/* Directories might not be empty etc */
				}
			} else {
				if ((function & extract_quiet) != extract_quiet) {
					*err = -1;
					error_msg
					    ("%s not created: newer or same age file exists",
					     file_entry->name);
				}
				seek_forward(src_stream, file_entry->size);
				goto cleanup;
			}
		}
		if (function & extract_create_leading_dirs) {	/* Create leading directories with default umask */
			char *buf, *parent;
			buf = xstrdup(full_name);
			parent = dirname(buf);
			if (make_directory(parent, -1, FILEUTILS_RECUR) != 0) {
				if ((function & extract_quiet) != extract_quiet) {
					*err = -1;
					error_msg
					    ("couldn't create leading directories");
				}
			}
			free(buf);
		}
		switch (file_entry->mode & S_IFMT) {
		case S_IFREG:
			if (file_entry->link_name) {	/* Found a cpio hard link */
				if (link(full_link_name, full_name) != 0) {
					if ((function & extract_quiet) !=
					    extract_quiet) {
						*err = -1;
						perror_msg
						    ("Cannot link from %s to '%s'",
						     file_entry->name,
						     file_entry->link_name);
					}
				}
			} else {
				if ((dst_stream =
				     wfopen(full_name, "w")) == NULL) {
					*err = -1;
					seek_forward(src_stream,
						     file_entry->size);
					goto cleanup;
				}
				archive_offset += file_entry->size;
				*err =
				    gzip_copy(src_stream, dst_stream,
					      file_entry->size);
				fclose(dst_stream);
			}
			break;
		case S_IFDIR:
			if (stat_res != 0) {
				if (mkdir(full_name, file_entry->mode) < 0) {
					if ((function & extract_quiet) !=
					    extract_quiet) {
						*err = -1;
						perror_msg("Cannot make dir %s",
							   full_name);
					}
				}
			}
			break;
		case S_IFLNK:
			if (symlink(file_entry->link_name, full_name) < 0) {
				if ((function & extract_quiet) != extract_quiet) {
					*err = -1;
					perror_msg
					    ("Cannot create symlink from %s to '%s'",
					     file_entry->name,
					     file_entry->link_name);
				}
				goto cleanup;
			}
			break;
		case S_IFSOCK:
		case S_IFBLK:
		case S_IFCHR:
		case S_IFIFO:
			if (mknod
			    (full_name, file_entry->mode,
			     file_entry->device) == -1) {
				if ((function & extract_quiet) != extract_quiet) {
					*err = -1;
					perror_msg("Cannot create node %s",
						   file_entry->name);
				}
				goto cleanup;
			}
			break;
		default:
			*err = -1;
			perror_msg("Don't know how to handle %s", full_name);

		}

		/* Changing a symlink's properties normally changes the properties of the
		 * file pointed to, so dont try and change the date or mode, lchown does
		 * does the right thing, but isnt available in older versions of libc */
		if (S_ISLNK(file_entry->mode)) {
#if (__GLIBC__ > 2) && (__GLIBC_MINOR__ > 1)
			lchown(full_name, file_entry->uid, file_entry->gid);
#endif
		} else {
			if (function & extract_preserve_date) {
				t.actime = file_entry->mtime;
				t.modtime = file_entry->mtime;
				utime(full_name, &t);
			}
			chown(full_name, file_entry->uid, file_entry->gid);
			chmod(full_name, file_entry->mode);
		}
	} else {
		/* If we arent extracting data we have to skip it,
		 * if data size is 0 then then just do it anyway
		 * (saves testing for it) */
		seek_forward(src_stream, file_entry->size);
	}

	/* extract_list and extract_verbose_list can be used in conjunction
	 * with one of the above four extraction functions, so do this seperately */
	if (function & extract_verbose_list) {
		fprintf(out_stream, "%s %d/%d %8d %s ",
			mode_string(file_entry->mode), file_entry->uid,
			file_entry->gid, (int)file_entry->size,
			time_string(file_entry->mtime));
	}
	if ((function & extract_list) || (function & extract_verbose_list)) {
		/* fputs doesnt add a trailing \n, so use fprintf */
		fprintf(out_stream, "%s\n", file_entry->name);
	}

cleanup:
	free(full_name);
	if (full_link_name)
		free(full_link_name);

	return buffer;
}
Exemple #6
0
char *deb_extract(const char *package_filename, FILE * out_stream,
		  const int extract_function, const char *prefix,
		  const char *filename, int *err)
{
	FILE *deb_stream = NULL;
	const char **file_list = NULL;
	char *output_buffer = NULL;
	char *ared_file = NULL;
	struct gzip_handle tar_outer = { }, tar_inner = { };
	file_header_t *tar_header;

	*err = 0;

	if (filename != NULL) {
		file_list = xmalloc(sizeof(char *) * 2);
		file_list[0] = filename;
		file_list[1] = NULL;
	}

	if (extract_function & extract_control_tar_gz) {
		ared_file = "control.tar.gz";
	} else if (extract_function & extract_data_tar_gz) {
		ared_file = "data.tar.gz";
	} else {
		error_msg("Internal error: extract_function=%x\n",
			 extract_function);
		*err = -1;
		goto cleanup;
	}

	/* open the debian package to be worked on */
	deb_stream = wfopen(package_filename, "r");
	if (deb_stream == NULL) {
		*err = -1;
		goto cleanup;
	}
	/* set the buffer size */
	setvbuf(deb_stream, NULL, _IOFBF, 0x8000);

	tar_outer.file = deb_stream;
	gzip_exec(&tar_outer, NULL);

	/* walk through outer tar file to find ared_file */
	while ((tar_header = get_header_tar(&tar_outer)) != NULL) {
		int name_offset = 0;
		if (strncmp(tar_header->name, "./", 2) == 0)
			name_offset = 2;

		if (strcmp(ared_file, tar_header->name + name_offset) == 0) {
			tar_inner.gzip = &tar_outer;
			gzip_exec(&tar_inner, NULL);

			archive_offset = 0;

			output_buffer = unarchive(&tar_inner,
						  out_stream,
						  get_header_tar,
						  free_header_tar,
						  extract_function,
						  prefix, file_list, err);

			free_header_tar(tar_header);
			gzip_close(&tar_inner);
			break;
		}

		seek_forward(&tar_outer, tar_header->size);
		free_header_tar(tar_header);
	}

cleanup:
	gzip_close(&tar_outer);

	if (file_list)
		free(file_list);

	return output_buffer;
}
Exemple #7
0
static file_header_t *get_header_tar(struct gzip_handle *tar_stream)
{
	union {
		unsigned char raw[512];
		struct {
			char name[100];	/*   0-99 */
			char mode[8];	/* 100-107 */
			char uid[8];	/* 108-115 */
			char gid[8];	/* 116-123 */
			char size[12];	/* 124-135 */
			char mtime[12];	/* 136-147 */
			char chksum[8];	/* 148-155 */
			char typeflag;	/* 156-156 */
			char linkname[100];	/* 157-256 */
			char magic[6];	/* 257-262 */
			char version[2];	/* 263-264 */
			char uname[32];	/* 265-296 */
			char gname[32];	/* 297-328 */
			char devmajor[8];	/* 329-336 */
			char devminor[8];	/* 337-344 */
			char prefix[155];	/* 345-499 */
			char padding[12];	/* 500-512 */
		} formated;
	} tar;
	file_header_t *tar_entry = NULL;
	long i;
	long sum = 0;

	if (archive_offset % 512 != 0) {
		seek_forward(tar_stream, 512 - (archive_offset % 512));
	}

	if (gzip_read(tar_stream, tar.raw, 512) != 512) {
		/* Unfortunately its common for tar files to have all sorts of
		 * trailing garbage, fail silently */
//              error_msg("Couldnt read header");
		return (NULL);
	}
	archive_offset += 512;

	/* Check header has valid magic, unfortunately some tar files
	 * have empty (0'ed) tar entries at the end, which will
	 * cause this to fail, so fail silently for now
	 */
	if (strncmp(tar.formated.magic, "ustar", 5) != 0) {
#ifdef CONFIG_FEATURE_TAR_OLDGNU_COMPATABILITY
		if (strncmp(tar.formated.magic, "\0\0\0\0\0", 5) != 0)
#endif
			return (NULL);
	}

	/* Do checksum on headers */
	for (i = 0; i < 148; i++) {
		sum += tar.raw[i];
	}
	sum += ' ' * 8;
	for (i = 156; i < 512; i++) {
		sum += tar.raw[i];
	}
	if (sum != strtol(tar.formated.chksum, NULL, 8)) {
		if (strtol(tar.formated.chksum, NULL, 8) != 0)
			error_msg("Invalid tar header checksum");
		return (NULL);
	}

	/* convert to type'ed variables */
	tar_entry = xcalloc(1, sizeof(file_header_t));

	// tar_entry->name = xstrdup(tar.formated.name);

/*
	parse_mode(tar.formated.mode, &tar_entry->mode);
*/
	tar_entry->mode = 07777 & strtol(tar.formated.mode, NULL, 8);

	tar_entry->uid = strtol(tar.formated.uid, NULL, 8);
	tar_entry->gid = strtol(tar.formated.gid, NULL, 8);
	tar_entry->size = strtol(tar.formated.size, NULL, 8);
	tar_entry->mtime = strtol(tar.formated.mtime, NULL, 8);

	tar_entry->device = (strtol(tar.formated.devmajor, NULL, 8) << 8) +
	    strtol(tar.formated.devminor, NULL, 8);

	/* Fix mode, used by the old format */
	switch (tar.formated.typeflag) {
		/* hard links are detected as regular files with 0 size and a link name */
	case '1':
		tar_entry->mode |= S_IFREG;
		break;
	case 0:
	case '0':

#ifdef CONFIG_FEATURE_TAR_OLDGNU_COMPATABILITY
		if (last_char_is(tar_entry->name, '/')) {
			tar_entry->mode |= S_IFDIR;
		} else
#endif
			tar_entry->mode |= S_IFREG;
		break;
	case '2':
		tar_entry->mode |= S_IFLNK;
		break;
	case '3':
		tar_entry->mode |= S_IFCHR;
		break;
	case '4':
		tar_entry->mode |= S_IFBLK;
		break;
	case '5':
		tar_entry->mode |= S_IFDIR;
		break;
	case '6':
		tar_entry->mode |= S_IFIFO;
		break;
#ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS
	case 'L':{
			longname = xmalloc(tar_entry->size + 1);
			if (gzip_read(tar_stream, longname, tar_entry->size) !=
			    tar_entry->size)
				return NULL;
			longname[tar_entry->size] = '\0';
			archive_offset += tar_entry->size;

			return (get_header_tar(tar_stream));
		}
	case 'K':{
			linkname = xmalloc(tar_entry->size + 1);
			if (gzip_read(tar_stream, linkname, tar_entry->size) !=
			    tar_entry->size)
				return NULL;
			linkname[tar_entry->size] = '\0';
			archive_offset += tar_entry->size;

			return (get_header_tar(tar_stream));
		}
	case 'D':
	case 'M':
	case 'N':
	case 'S':
	case 'V':
		perror_msg("Ignoring GNU extension type %c",
			   tar.formated.typeflag);
#endif
	default:
		perror_msg("Unknown typeflag: 0x%x", tar.formated.typeflag);
		break;

	}

#ifdef CONFIG_FEATURE_TAR_GNU_EXTENSIONS
	if (longname) {
		tar_entry->name = longname;
		longname = NULL;
	} else
#endif
	{
		tar_entry->name = xstrndup(tar.formated.name, 100);

		if (tar.formated.prefix[0]) {
			char *temp = tar_entry->name;
			char *prefixTemp = xstrndup(tar.formated.prefix, 155);
			tar_entry->name = concat_path_file(prefixTemp, temp);
			free(temp);
			free(prefixTemp);
		}
	}

	if (linkname) {
		tar_entry->link_name = linkname;
		linkname = NULL;
	} else {
		tar_entry->link_name = *tar.formated.linkname != '\0' ?
		    xstrndup(tar.formated.linkname, 100) : NULL;
	}

	return (tar_entry);
}
Exemple #8
0
int git_index__parse(git_index *index, const char *buffer, size_t buffer_size)
{
	unsigned int i;
	struct index_header header;
	git_oid checksum_calculated, checksum_expected;

#define seek_forward(_increase) { \
	if (_increase >= buffer_size) \
		return GIT_EOBJCORRUPTED; \
	buffer += _increase; \
	buffer_size -= _increase;\
}

	if (buffer_size < INDEX_HEADER_SIZE + INDEX_FOOTER_SIZE)
		return GIT_EOBJCORRUPTED;

	/* Precalculate the SHA1 of the files's contents -- we'll match it to
	 * the provided SHA1 in the footer */
	git_hash_buf(&checksum_calculated, (const void *)buffer, buffer_size - INDEX_FOOTER_SIZE);

	/* Parse header */
	if (read_header(&header, buffer) < 0)
		return GIT_EOBJCORRUPTED;

	seek_forward(INDEX_HEADER_SIZE);

	index->entry_count = header.entry_count;

	/* If there is already a entires array, reuse it if it can hold all the
	 * entries. If not, free and reallocate */
	if (index->entry_count > index->entries_size) {
		free(index->entries);
		index->entries_size = (uint32_t)(index->entry_count * 1.3f);
		index->entries = git__malloc(index->entries_size * sizeof(git_index_entry));
	}

	/* Parse all the entries */
	for (i = 0; i < index->entry_count && buffer_size > INDEX_FOOTER_SIZE; ++i) {
		size_t entry_size;
		entry_size = read_entry(&index->entries[i], buffer, buffer_size);

		/* 0 bytes read means an object corruption */
		if (entry_size == 0)
			return GIT_EOBJCORRUPTED;

		seek_forward(entry_size);
	}

	if (i != index->entry_count)
		return GIT_EOBJCORRUPTED;

	/* There's still space for some extensions! */
	while (buffer_size > INDEX_FOOTER_SIZE) {
		size_t extension_size;

		extension_size = read_extension(index, buffer, buffer_size);

		/* see if we have read any bytes from the extension */
		if (extension_size == 0)
			return GIT_EOBJCORRUPTED;

		seek_forward(extension_size);
	}

	if (buffer_size != INDEX_FOOTER_SIZE)
		return GIT_EOBJCORRUPTED;

	/* 160-bit SHA-1 over the content of the index file before this checksum. */
	git_oid_mkraw(&checksum_expected, (const unsigned char *)buffer);

	if (git_oid_cmp(&checksum_calculated, &checksum_expected) != 0)
		return GIT_EOBJCORRUPTED;

#undef seek_forward

	return 0;
}
Exemple #9
0
int gm_encode(int gbdata[], int length, char binary[], int reader)
{
	/* Create a binary stream representation of the input data.
	   7 sets are defined - Chinese characters, Numerals, Lower case letters, Upper case letters,
	   Mixed numerals and latters, Control characters and 8-bit binary data */
	int sp, current_mode, next_mode, last_mode, glyph = 0;
	int c1, c2, done;
	int p = 0, ppos;
	int numbuf[3], punt = 0;
	int number_pad_posn, debug = 0;
	int byte_count_posn = 0, byte_count = 0;
	int shift, i;
	
	strcpy(binary, "");
	
	sp = 0;
	current_mode = 0;
	last_mode = 0;
	number_pad_posn = 0;
	
	if(reader) {
		concat(binary, "1010"); /* FNC3 - Reader Initialisation */
	}
	
	do {
		next_mode = seek_forward(gbdata, length, sp, current_mode);
		
		if(next_mode != current_mode) {
			switch(current_mode) {
				case 0:
					switch(next_mode) {
						case GM_CHINESE: concat(binary, "0001"); break;
						case GM_NUMBER: concat(binary, "0010"); break;
						case GM_LOWER: concat(binary, "0011"); break;
						case GM_UPPER: concat(binary, "0100"); break;
						case GM_MIXED: concat(binary, "0101"); break;
						case GM_BYTE: concat(binary, "0111"); break;
					}
					break;
				case GM_CHINESE:
					switch(next_mode) {
						case GM_NUMBER: concat(binary, "1111111100001"); break; // 8161
						case GM_LOWER: concat(binary, "1111111100010"); break; // 8162
						case GM_UPPER: concat(binary, "1111111100011"); break; // 8163
						case GM_MIXED: concat(binary, "1111111100100"); break; // 8164
						case GM_BYTE: concat(binary, "1111111100101"); break; // 8165
					}
					break;
				case GM_NUMBER:
					/* add numeric block padding value */
					switch(p) {
						case 1: binary[number_pad_posn] = '1'; binary[number_pad_posn + 1] = '0'; break; // 2 pad digits
						case 2: binary[number_pad_posn] = '0'; binary[number_pad_posn + 1] = '1'; break; // 1 pad digit
						case 3: binary[number_pad_posn] = '0'; binary[number_pad_posn + 1] = '0'; break; // 0 pad digits
					}
					switch(next_mode) {
						case GM_CHINESE: concat(binary, "1111111011"); break; // 1019
						case GM_LOWER: concat(binary, "1111111100"); break; // 1020
						case GM_UPPER: concat(binary, "1111111101"); break; // 1021
						case GM_MIXED: concat(binary, "1111111110"); break; // 1022
						case GM_BYTE: concat(binary, "1111111111"); break; // 1023
					}
					break;
				case GM_LOWER:
				case GM_UPPER:
					switch(next_mode) {
						case GM_CHINESE: concat(binary, "11100"); break; // 28
						case GM_NUMBER: concat(binary, "11101"); break; // 29
						case GM_LOWER:
						case GM_UPPER: concat(binary, "11110"); break; // 30
						case GM_MIXED: concat(binary, "1111100"); break; // 124
						case GM_BYTE: concat(binary, "1111110"); break; // 126
					}
					break;
				case GM_MIXED:
					switch(next_mode) {
						case GM_CHINESE: concat(binary, "1111110001"); break; // 1009
						case GM_NUMBER: concat(binary, "1111110010"); break; // 1010
						case GM_LOWER: concat(binary, "1111110011"); break; // 1011
						case GM_UPPER: concat(binary, "1111110100"); break; // 1012
						case GM_BYTE: concat(binary, "1111110111"); break; // 1015
					}
					break;
				case GM_BYTE:
					/* add byte block length indicator */
					add_byte_count(binary, byte_count_posn, byte_count);
					byte_count = 0;
					switch(next_mode) {
						case GM_CHINESE: concat(binary, "0001"); break; // 1
						case GM_NUMBER: concat(binary, "0010"); break; // 2
						case GM_LOWER: concat(binary, "0011"); break; // 3
						case GM_UPPER: concat(binary, "0100"); break; // 4
						case GM_MIXED: concat(binary, "0101"); break; // 5
					}
					break;
			}
			if(debug) {
				switch(next_mode) {
					case GM_CHINESE: printf("CHIN "); break;
					case GM_NUMBER: printf("NUMB "); break;
					case GM_LOWER: printf("LOWR "); break;
					case GM_UPPER: printf("UPPR "); break;
					case GM_MIXED: printf("MIXD "); break;
					case GM_BYTE: printf("BYTE "); break;
				}
			}
		}
		last_mode = current_mode;
		current_mode = next_mode;
		
		switch(current_mode) {
			case GM_CHINESE: 
				done = 0;
				if(gbdata[sp] > 0xff) {
					/* GB2312 character */
					c1 = (gbdata[sp] & 0xff00) >> 8;
					c2 = gbdata[sp] & 0xff;
					
					if((c1 >= 0xa0) && (c1 <= 0xa9)) {
						glyph = (0x60 * (c1 - 0xa1)) + (c2 - 0xa0);
					}
					if((c1 >= 0xb0) && (c1 <= 0xf7)) {
						glyph = (0x60 * (c1 - 0xb0 + 9)) + (c2  - 0xa0);
					}
					done = 1;
				}
				if(!(done)) {
					if(sp != (length - 1)) {
						if((gbdata[sp] == 0x13) && (gbdata[sp + 1] == 0x10)) {
							/* End of Line */
							glyph = 7776;
							sp++;
						}
						done = 1;
					}
				}
				if(!(done)) {
					if(sp != (length - 1)) {
						if(((gbdata[sp] >= '0') && (gbdata[sp] <= '9')) &&
							((gbdata[sp + 1] >= '0') && (gbdata[sp + 1] <= '9'))) {
							/* Two digits */
							glyph = 8033 + (10 * (gbdata[sp] - '0')) + (gbdata[sp + 1] - '0');
							sp++;
						}
					}
				}
				if(!(done)) {
					/* Byte value */
					glyph = 7777 + gbdata[sp];
				}
				
				if(debug) { printf("[%d] ", glyph); }
				
				if(glyph & 0x1000) { concat(binary, "1"); } else { concat(binary, "0"); }
				if(glyph & 0x800) { concat(binary, "1"); } else { concat(binary, "0"); }
				if(glyph & 0x400) { concat(binary, "1"); } else { concat(binary, "0"); }
				if(glyph & 0x200) { concat(binary, "1"); } else { concat(binary, "0"); }
				if(glyph & 0x100) { concat(binary, "1"); } else { concat(binary, "0"); }
				if(glyph & 0x80) { concat(binary, "1"); } else { concat(binary, "0"); }
				if(glyph & 0x40) { concat(binary, "1"); } else { concat(binary, "0"); }
				if(glyph & 0x20) { concat(binary, "1"); } else { concat(binary, "0"); }
				if(glyph & 0x10) { concat(binary, "1"); } else { concat(binary, "0"); }
				if(glyph & 0x08) { concat(binary, "1"); } else { concat(binary, "0"); }
				if(glyph & 0x04) { concat(binary, "1"); } else { concat(binary, "0"); }
				if(glyph & 0x02) { concat(binary, "1"); } else { concat(binary, "0"); }
				if(glyph & 0x01) { concat(binary, "1"); } else { concat(binary, "0"); }
				sp++;
				break;
				
			case GM_NUMBER:
				if(last_mode != current_mode) {
					/* Reserve a space for numeric digit padding value (2 bits) */
					number_pad_posn = strlen(binary);
					concat(binary, "XX");
				}
				p = 0;
				ppos = -1;
				
				/* Numeric compression can also include certain combinations of
				   non-numeric character */
				
				numbuf[0] = '0';
				numbuf[1] = '0';
				numbuf[2] = '0';
				do {
					if((gbdata[sp] >= '0') && (gbdata[sp] <= '9')) {
						numbuf[p] = gbdata[sp];
						sp++;
						p++;
					}
					switch(gbdata[sp]) {
						case ' ':
						case '+':
						case '-':
						case '.':
						case ',':
							punt = gbdata[sp];
							sp++;
							ppos = p;
							break;
					}
					if(sp < (length - 1)) {
						if((gbdata[sp] == 0x13) && (gbdata[sp + 1] == 0x10)) {
							/* <end of line> */
							punt = gbdata[sp];
							sp += 2;
							ppos = p;
						}
					}
				} while ((p < 3) && (sp < length));
				
				if(ppos != -1) {
					switch(punt) {
						case ' ': glyph = 0; break;
						case '+': glyph = 3; break;
						case '-': glyph = 6; break;
						case '.': glyph = 9; break;
						case ',': glyph = 12; break;
						case 0x13: glyph = 15; break;
					}
					glyph += ppos;
					glyph += 1000;
					
					if(debug) { printf("[%d] ", glyph); }
					
					if(glyph & 0x200) { concat(binary, "1"); } else { concat(binary, "0"); }
					if(glyph & 0x100) { concat(binary, "1"); } else { concat(binary, "0"); }
					if(glyph & 0x80) { concat(binary, "1"); } else { concat(binary, "0"); }
					if(glyph & 0x40) { concat(binary, "1"); } else { concat(binary, "0"); }
					if(glyph & 0x20) { concat(binary, "1"); } else { concat(binary, "0"); }
					if(glyph & 0x10) { concat(binary, "1"); } else { concat(binary, "0"); }
					if(glyph & 0x08) { concat(binary, "1"); } else { concat(binary, "0"); }
					if(glyph & 0x04) { concat(binary, "1"); } else { concat(binary, "0"); }
					if(glyph & 0x02) { concat(binary, "1"); } else { concat(binary, "0"); }
					if(glyph & 0x01) { concat(binary, "1"); } else { concat(binary, "0"); }
				}
				
				glyph = (100 * (numbuf[0] - '0')) + (10 * (numbuf[1] - '0')) + (numbuf[2] - '0');
				if(debug) { printf("[%d] ", glyph); }
				
				if(glyph & 0x200) { concat(binary, "1"); } else { concat(binary, "0"); }
				if(glyph & 0x100) { concat(binary, "1"); } else { concat(binary, "0"); }
				if(glyph & 0x80) { concat(binary, "1"); } else { concat(binary, "0"); }
				if(glyph & 0x40) { concat(binary, "1"); } else { concat(binary, "0"); }
				if(glyph & 0x20) { concat(binary, "1"); } else { concat(binary, "0"); }
				if(glyph & 0x10) { concat(binary, "1"); } else { concat(binary, "0"); }
				if(glyph & 0x08) { concat(binary, "1"); } else { concat(binary, "0"); }
				if(glyph & 0x04) { concat(binary, "1"); } else { concat(binary, "0"); }
				if(glyph & 0x02) { concat(binary, "1"); } else { concat(binary, "0"); }
				if(glyph & 0x01) { concat(binary, "1"); } else { concat(binary, "0"); }
				break;
				
			case GM_BYTE:
				if(last_mode != current_mode) {
					/* Reserve space for byte block length indicator (9 bits) */
					byte_count_posn = strlen(binary);
					concat(binary, "LLLLLLLLL");
				}
				if(byte_count == 512) {
					/* Maximum byte block size is 512 bytes. If longer is needed then start a new block */
					add_byte_count(binary, byte_count_posn, byte_count);
					concat(binary, "0111");
					byte_count_posn = strlen(binary);
					concat(binary, "LLLLLLLLL");
					byte_count = 0;
				}
				
				glyph = gbdata[sp];
				if(debug) { printf("[%d] ", glyph); }
				if(glyph & 0x80) { concat(binary, "1"); } else { concat(binary, "0"); }
				if(glyph & 0x40) { concat(binary, "1"); } else { concat(binary, "0"); }
				if(glyph & 0x20) { concat(binary, "1"); } else { concat(binary, "0"); }
				if(glyph & 0x10) { concat(binary, "1"); } else { concat(binary, "0"); }
				if(glyph & 0x08) { concat(binary, "1"); } else { concat(binary, "0"); }
				if(glyph & 0x04) { concat(binary, "1"); } else { concat(binary, "0"); }
				if(glyph & 0x02) { concat(binary, "1"); } else { concat(binary, "0"); }
				if(glyph & 0x01) { concat(binary, "1"); } else { concat(binary, "0"); }
				sp++;
				byte_count++;
				break;
				
			case GM_MIXED:
				shift = 1;
				if((gbdata[sp] >= '0') && (gbdata[sp] <= '9')) { shift = 0; }
				if((gbdata[sp] >= 'A') && (gbdata[sp] <= 'Z')) { shift = 0; }
				if((gbdata[sp] >= 'a') && (gbdata[sp] <= 'z')) { shift = 0; }
				if(gbdata[sp] == ' ') { shift = 0; }
				
				if(shift == 0) {
					/* Mixed Mode character */
					glyph = posn(EUROPIUM, gbdata[sp]);
					if(debug) { printf("[%d] ", glyph); }
					
					if(glyph & 0x20) { concat(binary, "1"); } else { concat(binary, "0"); }
					if(glyph & 0x10) { concat(binary, "1"); } else { concat(binary, "0"); }
					if(glyph & 0x08) { concat(binary, "1"); } else { concat(binary, "0"); }
					if(glyph & 0x04) { concat(binary, "1"); } else { concat(binary, "0"); }
					if(glyph & 0x02) { concat(binary, "1"); } else { concat(binary, "0"); }
					if(glyph & 0x01) { concat(binary, "1"); } else { concat(binary, "0"); }
				} else {
					/* Shift Mode character */
					concat(binary, "1111110110"); /* 1014 - shift indicator */
					add_shift_char(binary, gbdata[sp]);
				}
				
				sp++;
				break;
				
			case GM_UPPER:
				shift = 1;
				if((gbdata[sp] >= 'A') && (gbdata[sp] <= 'Z')) { shift = 0; }
				if(gbdata[sp] == ' ') { shift = 0; }
				
				if(shift == 0) {
					/* Upper Case character */
					glyph = posn("ABCDEFGHIJKLMNOPQRSTUVWXYZ ", gbdata[sp]);
					if(debug) { printf("[%d] ", glyph); }
					
					if(glyph & 0x10) { concat(binary, "1"); } else { concat(binary, "0"); }
					if(glyph & 0x08) { concat(binary, "1"); } else { concat(binary, "0"); }
					if(glyph & 0x04) { concat(binary, "1"); } else { concat(binary, "0"); }
					if(glyph & 0x02) { concat(binary, "1"); } else { concat(binary, "0"); }
					if(glyph & 0x01) { concat(binary, "1"); } else { concat(binary, "0"); }
				} else {
					/* Shift Mode character */
					concat(binary, "1111101"); /* 127 - shift indicator */
					add_shift_char(binary, gbdata[sp]);
				}
				
				sp++;
				break;
				
			case GM_LOWER: 
				shift = 1;
				if((gbdata[sp] >= 'a') && (gbdata[sp] <= 'z')) { shift = 0; }
				if(gbdata[sp] == ' ') { shift = 0; }
				
				if(shift == 0) {
					/* Lower Case character */
					glyph = posn("abcdefghijklmnopqrstuvwxyz ", gbdata[sp]);
					if(debug) { printf("[%d] ", glyph); }
					
					if(glyph & 0x10) { concat(binary, "1"); } else { concat(binary, "0"); }
					if(glyph & 0x08) { concat(binary, "1"); } else { concat(binary, "0"); }
					if(glyph & 0x04) { concat(binary, "1"); } else { concat(binary, "0"); }
					if(glyph & 0x02) { concat(binary, "1"); } else { concat(binary, "0"); }
					if(glyph & 0x01) { concat(binary, "1"); } else { concat(binary, "0"); }
				} else {
					/* Shift Mode character */
					concat(binary, "1111101"); /* 127 - shift indicator */
					add_shift_char(binary, gbdata[sp]);
				}
				
				sp++;
				break;
		}
		if(strlen(binary) > 9191) {
			return ERROR_TOO_LONG;
		}
		
	} while(sp < length);
Exemple #10
0
int aiff_open(FILE *in, oe_enc_opt *opt, unsigned char *buf, int buflen)
{
    int aifc; /* AIFC or AIFF? */
    unsigned int len;
    unsigned char *buffer;
    unsigned char buf2[8];
    aiff_fmt format;
    aifffile *aiff = malloc(sizeof(aifffile));
    int i;

    if(buf[11]=='C')
        aifc=1;
    else
        aifc=0;

    if(!find_aiff_chunk(in, "COMM", &len))
    {
        fprintf(stderr, _("Warning: No common chunk found in AIFF file\n"));
        return 0; /* EOF before COMM chunk */
    }

    if(len < 18) 
    {
        fprintf(stderr, _("Warning: Truncated common chunk in AIFF header\n"));
        return 0; /* Weird common chunk */
    }

    buffer = alloca(len);

    if(fread(buffer,1,len,in) < len)
    {
        fprintf(stderr, _("Warning: Unexpected EOF in reading AIFF header\n"));
        return 0;
    }

    format.channels = READ_U16_BE(buffer);
    format.totalframes = READ_U32_BE(buffer+2);
    format.samplesize = READ_U16_BE(buffer+6);
    format.rate = (int)read_IEEE80(buffer+8);

    aiff->bigendian = 1;

    if(aifc)
    {
        if(len < 22)
        {
            fprintf(stderr, _("Warning: AIFF-C header truncated.\n"));
            return 0;
        }

        if(!memcmp(buffer+18, "NONE", 4)) 
        {
            aiff->bigendian = 1;
        }
        else if(!memcmp(buffer+18, "sowt", 4)) 
        {
            aiff->bigendian = 0;
        }
        else
        {
            fprintf(stderr, _("Warning: Can't handle compressed AIFF-C (%c%c%c%c)\n"), *(buffer+18), *(buffer+19), *(buffer+20), *(buffer+21));
            return 0; /* Compressed. Can't handle */
        }
    }

    if(!find_aiff_chunk(in, "SSND", &len))
    {
        fprintf(stderr, _("Warning: No SSND chunk found in AIFF file\n"));
        return 0; /* No SSND chunk -> no actual audio */
    }

    if(len < 8) 
    {
        fprintf(stderr, _("Warning: Corrupted SSND chunk in AIFF header\n"));
        return 0; 
    }

    if(fread(buf2,1,8, in) < 8)
    {
        fprintf(stderr, _("Warning: Unexpected EOF reading AIFF header\n"));
        return 0;
    }

    format.offset = READ_U32_BE(buf2);
    format.blocksize = READ_U32_BE(buf2+4);

    if( format.blocksize == 0 &&
        (format.samplesize == 16 || format.samplesize == 8))
    {
        /* From here on, this is very similar to the wav code. Oh well. */
        
        opt->rate = format.rate;
        opt->channels = format.channels;
        opt->read_samples = wav_read; /* Similar enough, so we use the same */
        opt->total_samples_per_channel = format.totalframes;

        aiff->f = in;
        aiff->samplesread = 0;
        aiff->channels = format.channels;
        aiff->samplesize = format.samplesize;
        aiff->totalsamples = format.totalframes;

        if(aiff->channels>3)
          fprintf(stderr,"WARNING: AIFF[-C] files with greater than three channels use\n"
                  "speaker locations incompatable with Vorbis suppound definitions.\n"
                  "Not performaing channel location mapping.\n");

        opt->readdata = (void *)aiff;

        aiff->channel_permute = malloc(aiff->channels * sizeof(int));
        if (aiff->channels <= 6)
            /* Where we know the mappings, use them. */
            memcpy(aiff->channel_permute, aiff_permute_matrix[aiff->channels-1], 
                    sizeof(int) * aiff->channels);
        else
            /* Use a default 1-1 mapping */
            for (i=0; i < aiff->channels; i++)
                aiff->channel_permute[i] = i;

        seek_forward(in, format.offset); /* Swallow some data */
        return 1;
    }
    else
    {
        fprintf(stderr, 
                _("Warning: OggEnc does not support this type of AIFF/AIFC file\n"
                " Must be 8 or 16 bit PCM.\n"));
        return 0;
    }
}
Exemple #11
0
/*
 * Adds tar index data to the global files[] array.
 *
 * Returns 0 on success
 *        -1 on failure
 */
int accumulate(HashFile *hf, FILE *fp, char *archive, options_t *opt) {
    tar_block blk;
    char member[256];
    int LongLink = 0;
    size_t size, extra;
    size_t offset = 0;

    /* Add to HashFile archives list */
    if (archive) {
	hf->narchives++;
	hf->archives = realloc(hf->archives, hf->narchives * sizeof(char *));
	hf->archives[hf->narchives-1] = strdup(archive);	
    }

    /* Fill out the files[] array with the offsets, size and names */
    while(fread(&blk, sizeof(blk), 1, fp) == 1) {
	/*
	 * If a directory is too large to fit in the name (>100) but short
	 * enough to fit in the prefix the name field will be empty, this is
	 * not the cas for ordinary files where the name field is always
	 * non-empty
	 */
	if (!blk.header.name[0] && !blk.header.prefix[0])
	    break;

        /* get size of member, rounded to a multiple of TBLOCK */
	size = strtoul(blk.header.size, NULL, 8);
        extra = TBLOCK*((size+TBLOCK-1)/TBLOCK) - size;

        /* skip directories unless requested */
        if (opt->directories || blk.header.typeflag != DIRTYPE) {

            /*
	     * extract member name (prefix + name), unless last member
	     * was ././@LongLink
	     */
            if (LongLink == 0) {
                (void) strncpy(member, blk.header.prefix, 155);
	        if (strlen(blk.header.prefix) > 0 && blk.header.name[0])
		    (void) strcat(member, "/");
    	        (void) strncat(member, blk.header.name, 100);
            }
            
            /* account for gtar ././@LongLink */
            if (strcmp(member, "././@LongLink") == 0) {
                /* still expect filenames to fit into 256 bytes */
                if (size > 256) {
                    fread(member, 1, size > 256 ? 256 : size, fp);
                    fprintf(stderr,"././@LongLink too long size=%ld\n",
			    (long)size);
                    fprintf(stderr,"%s...\n", member);
                    exit(1);
                }
                /*
		 * extract full name of next member then rewind to start
		 * of header
		 */
                fread(member, 1, size > 256 ? 256 : size, fp);
                fseek(fp, -size, SEEK_CUR);
                LongLink = 1;
            } else {
                /* output offset, member name */
                /* printf("%lu %.256s\n", (long)offset, member); */
                LongLink = 0;

		if (nfiles >= files_alloc) {
		    if (files_alloc)
			files_alloc *= 2;
		    else
			files_alloc = 1024;
		    files = (tar_file *)realloc(files,
						files_alloc*sizeof(tar_file));
		}
		if (opt->basename) {
		    char *cp = strrchr(member, '/');
		    if (cp)
			memmove(member, cp+1, strlen(cp+1)+1);
		}

		if (opt->map) {
		    HashItem *hi = HashTableSearch(opt->map,
						   member,
						   strlen(member));
		    if (hi) {
			//fprintf(stderr, "Mapped %s to %s\n",
			//	member, hi->data.p);
			strcpy(files[nfiles].member, hi->data.p);
		    } else {
			//fprintf(stderr, "No map for %s\n",
			//	member);
			strcpy(files[nfiles].member, member);
		    }
		} else {
		    strcpy(files[nfiles].member, member);
		}

		files[nfiles].archive = hf->narchives-1;
		files[nfiles].pos = offset+sizeof(blk);
		files[nfiles].size = size;
		if (opt->verbose)
		    fprintf(stderr, "File %d: pos %010ld+%06d: %s\n",
			    nfiles,
			    (long)files[nfiles].pos,
			    files[nfiles].size,
			    files[nfiles].member);

		nfiles++;
            }
        }

        /* increment offset */
        size += extra;
	seek_forward(fp, size);
        offset += sizeof(blk) + size;
    }
}
Exemple #12
0
static int parse_index(git_index *index, const char *buffer, size_t buffer_size)
{
	unsigned int i;
	struct index_header header;
	git_oid checksum_calculated, checksum_expected;

#define seek_forward(_increase) { \
	if (_increase >= buffer_size) \
		return git__throw(GIT_EOBJCORRUPTED, "Failed to seek forward. Buffer size exceeded"); \
	buffer += _increase; \
	buffer_size -= _increase;\
}

	if (buffer_size < INDEX_HEADER_SIZE + INDEX_FOOTER_SIZE)
		return git__throw(GIT_EOBJCORRUPTED, "Failed to parse index. Buffer too small");

	/* Precalculate the SHA1 of the files's contents -- we'll match it to
	 * the provided SHA1 in the footer */
	git_hash_buf(&checksum_calculated, buffer, buffer_size - INDEX_FOOTER_SIZE);

	/* Parse header */
	if (read_header(&header, buffer) < GIT_SUCCESS)
		return git__throw(GIT_EOBJCORRUPTED, "Failed to parse index. Header is corrupted");

	seek_forward(INDEX_HEADER_SIZE);

	git_vector_clear(&index->entries);

	/* Parse all the entries */
	for (i = 0; i < header.entry_count && buffer_size > INDEX_FOOTER_SIZE; ++i) {
		size_t entry_size;
		git_index_entry *entry;

		entry = git__malloc(sizeof(git_index_entry));
		if (entry == NULL)
			return GIT_ENOMEM;

		entry_size = read_entry(entry, buffer, buffer_size);

		/* 0 bytes read means an object corruption */
		if (entry_size == 0)
			return git__throw(GIT_EOBJCORRUPTED, "Failed to parse index. Entry size is zero");

		if (git_vector_insert(&index->entries, entry) < GIT_SUCCESS)
			return GIT_ENOMEM;

		seek_forward(entry_size);
	}

	if (i != header.entry_count)
		return git__throw(GIT_EOBJCORRUPTED, "Failed to parse index. Header entries changed while parsing");

	/* There's still space for some extensions! */
	while (buffer_size > INDEX_FOOTER_SIZE) {
		size_t extension_size;

		extension_size = read_extension(index, buffer, buffer_size);

		/* see if we have read any bytes from the extension */
		if (extension_size == 0)
			return git__throw(GIT_EOBJCORRUPTED, "Failed to parse index. Extension size is zero");

		seek_forward(extension_size);
	}

	if (buffer_size != INDEX_FOOTER_SIZE)
		return git__throw(GIT_EOBJCORRUPTED, "Failed to parse index. Buffer size does not match index footer size");

	/* 160-bit SHA-1 over the content of the index file before this checksum. */
	git_oid_fromraw(&checksum_expected, (const unsigned char *)buffer);

	if (git_oid_cmp(&checksum_calculated, &checksum_expected) != 0)
		return git__throw(GIT_EOBJCORRUPTED, "Failed to parse index. Calculated checksum does not match expected checksum");

#undef seek_forward

	/* force sorting in the vector: the entries are
	 * assured to be sorted on the index */
	index->entries.sorted = 1;
	return GIT_SUCCESS;
}