예제 #1
0
/**
 * Compress data in ZIP buffer and move result to the write buffer of
 * the connection.
 * This function closes the connection on error.
 * @param Idx Connection handle.
 * @return true on success, false otherwise.
 */
GLOBAL bool
Zip_Flush( CONN_ID Idx )
{
	int result;
	unsigned char zipbuf[WRITEBUFFER_SLINK_LEN];
	int zipbuf_used = 0;
	z_stream *out;

	out = &My_Connections[Idx].zip.out;

	out->avail_in = (uInt)array_bytes(&My_Connections[Idx].zip.wbuf);
	if (!out->avail_in)
		return true;	/* nothing to do. */

	out->next_in = array_start(&My_Connections[Idx].zip.wbuf);
	assert(out->next_in != NULL);

	out->next_out = zipbuf;
	out->avail_out = (uInt)sizeof zipbuf;

#ifdef DEBUG_ZIP
	Log(LOG_DEBUG, "out->avail_in %d, out->avail_out %d",
		out->avail_in, out->avail_out);
#endif
	result = deflate( out, Z_SYNC_FLUSH );
	if(( result != Z_OK ) || ( out->avail_in > 0 ))
	{
		Log( LOG_ALERT, "Compression error: code %d!?", result );
		Conn_Close( Idx, "Compression error!", NULL, false );
		return false;
	}

	if (out->avail_out <= 0) {
		/* Not all data was compressed, because data became
		 * bigger while compressing it. */
		Log(LOG_ALERT, "Compression error: buffer overflow!?");
		Conn_Close(Idx, "Compression error!", NULL, false);
		return false;
	}

	assert(out->avail_out <= WRITEBUFFER_SLINK_LEN);

	zipbuf_used = WRITEBUFFER_SLINK_LEN - out->avail_out;
#ifdef DEBUG_ZIP
	Log(LOG_DEBUG, "zipbuf_used: %d", zipbuf_used);
#endif
	if (!array_catb(&My_Connections[Idx].wbuf,
			(char *)zipbuf, (size_t) zipbuf_used)) {
		Log (LOG_ALERT, "Compression error: can't copy data!?");
		Conn_Close(Idx, "Compression error!", NULL, false);
		return false;
	}

	My_Connections[Idx].bytes_out += zipbuf_used;
	My_Connections[Idx].zip.bytes_out += array_bytes(&My_Connections[Idx].zip.wbuf); 
	array_trunc(&My_Connections[Idx].zip.wbuf);

	return true;
} /* Zip_Flush */
예제 #2
0
파일: array.c 프로젝트: LucentW/ngircd
/* return false on failure (realloc failure, invalid src/dest array) */
bool
array_copyb(array * dest, const char *src, size_t len)
{
	assert(dest != NULL);
	assert(src != NULL );

	if (!src || !dest)
		return false;

	array_trunc(dest);
	return array_catb(dest, src, len);
}
예제 #3
0
void pathexec_run(char *file, char **argv, char **envp) {
  char *path;
  int savederrno;
  char *s;
  unsigned int split;
  
  s = strchr(file, '/');
  split = s ? s - file : strlen(file);
  if (file[split]) {
    execve(file, argv, envp);
    return;
  }
  
  path = getenv("PATH");
  if (!path || (strlen(path) == 0))
    path = "/bin:/usr/bin";
    
  savederrno = 0;
  for (;;) {
    s = strchr(path, ':');
    split = s ? s - path : 0;
    
    array_trunc(&_pathexec_tmp);
    array_append(&_pathexec_tmp, path, split);
    if (!split)
      array_append(&_pathexec_tmp, ".", 1);
    array_append(&_pathexec_tmp, "/", 1);
    array_append(&_pathexec_tmp, file, strlen(file));
    array_append_null(&_pathexec_tmp);
    
    if (!array_allocated(&_pathexec_tmp))
      return;
      
    execve(_pathexec_tmp.data, argv, envp);
    if (errno != ENOENT) {
      savederrno = errno;
      if ((errno != EACCES) && (errno != EPERM) && (errno != EISDIR))
        return;
    }
    
    if (!path[split]) {
      if (savederrno)
        errno = savederrno;
      return;
    }
    
    path += split;
    path += 1;
  }
}
예제 #4
0
int pathexec_env(char *name, char *value) {
  if (!name)
    return 0;
    
  array_trunc(&_pathexec_tmp);
  array_append(&_pathexec_tmp, name, strlen(name));
  if (value) {
    array_append(&_pathexec_tmp, "=", 1);
    array_append(&_pathexec_tmp, value, strlen(value));
  }
  array_append_null(&_pathexec_tmp);
  
  if (!array_allocated(&_pathexec_tmp))
    return 0;
    
  array_append(&_pathexec_plus, _pathexec_tmp.data, _pathexec_tmp.size);
  
  return 1;
}
예제 #5
0
파일: main.c 프로젝트: dpejesh/svcrypt
int main(int argc, char **argv, char **envp) {
  array_t array;
  bio_t bin;
  char bin_data[8192];
  bio_t *bio;
  int fd;
  char *name;
  int opt;
  ssize_t r;
  struct stat st;
  char *type = 0;
  
  while ((opt = sgetopt(argc, argv, "t:")) != -1) {
    switch (opt) {
      case '?':
        usage();
      case 't':
        type = soptarg;
        break;
    }
  }
  argv += soptind;
  
  if (!type)
    usage();
    
  array_init(&array, 1);
  
  /* Set the initial buffered io pointer to use stdin and set the default name
   * to '-'. */
  bio = bio_0;
  name = "-";
  for (;;) {
    if (*argv) {
      /* If a path was given then check to see if it's a directory and skip
       * over it if it is. */
      if (stat(*argv, &st))
        err(1, "fatal");
      if (S_ISDIR(st.st_mode)) {
        errno = EISDIR;
        warn(*argv);
        argv++;
        continue;
      }
      
      /* Open the file for reading and construct our own buffered io struct to
       * use for reading in the file and hashing it.  Also set the default
       * buffered io pointer to our own and update the name to reflect the
       * current file. */
      fd = open_read(*argv);
      if (fd == -1)
        err(1, "fatal");
      bio_init(&bin, read, fd, bin_data, sizeof(bin_data));
      bio = &bin;
      name = *argv;
    }
    /* If there's no more arguments passed on the command line and the default
     * buffered io pointer doesn't point to stdin then that means all of the
     * files have been hashed and it's time to exit. */
    else if ((bio != bio_0))
      break;
    
    if (!strcmp(type, "md4"))
      handle_md4(&array, bio);
    else if (!strcmp(type, "md5"))
      handle_md5(&array, bio);
    else if (!strcmp(type, "sha1"))
      handle_sha1(&array, bio);
    else if (!strcmp(type, "sha256"))
      handle_sha256(&array, bio);
    else if (!strcmp(type, "sha512"))
      handle_sha512(&array, bio);
    else
      errx(1, "fatal: %s", "unknown type");
      
    bio_put_str(bio_1, array_start(&array));
    bio_put_str(bio_1, "  ");
    bio_put_str(bio_1, name);
    bio_put_str(bio_1, "\n");
    bio_flush(bio_1);
    
    array_trunc(&array);
    
    if (*argv)
      close(fd);
    if ((!*argv) || (!*++argv))
      break;
  }
  
  return 0;
}
예제 #6
0
파일: cdbmake.c 프로젝트: dpejesh/svcdb
int main(int argc, char **argv, char **envp) {
  char ch;
  struct cdbmake cm;
  array_t data = ARRAY_INIT(1);
  int32_t dlen;
  int fd;
  uint32_t i;
  array_t key = ARRAY_INIT(1);
  int32_t klen;
  char *path;
  char *tmp;
  
  if (!*argv || !*++argv)
    usage();
  path = *argv;
  
  if (!*++argv)
    usage();
  tmp = *argv;
  
  /* Create the temporary file and start the cdb creation process with it. */
  fd = open("test.cdb", O_CREAT | O_TRUNC | O_WRONLY, 0644);
  if (fd == -1)
    strerr_die4sys(111, FATAL, "unable to create ", tmp, ": ");
  if (cdbmake_start(&cm, fd) == -1)
    strerr_die2sys(111, FATAL, "cdbmake initialization failed: ");
    
  for (;;) {
    /* Skip over new lines and require the first character to be '+'. */
    ch = get_ch();
    if (ch == '\n')
      break;
    if (ch != '+')
      die_format();
      
    /* Read the key length. */
    klen = get_len(',');
    if (klen == -1)
      die_format();
      
    /* Read the data length. */
    dlen = get_len(':');
    if (dlen == -1)
      die_format();
      
    /* Truncate the key array and load it with the key from the cdb record. */
    array_trunc(&key);
    for (i = 0; i < klen; i++) {
      ch = get_ch();
      array_append(&key, &ch, 1);
    }
    
    /* Verify the separator is ->. */
    if ((get_ch() != '-') || (get_ch() != '>'))
      die_format();
      
    /* Truncate the data array and load it with the data from the cdb record. */
    array_trunc(&data);
    for (i = 0; i < dlen; i++) {
      ch = get_ch();
      array_append(&data, &ch, 1);
    }
    
    /* The line is valid, so add it to the cdb file and check that it ends with
     * a new line. */
    if (cdbmake_add(&cm, array_start(&key), klen, array_start(&data), dlen) == -1)
      die_write();
    if (get_ch() != '\n')
      die_format();
  }
  
  /* Finish the cdb file, sync it to disk, close it, and finally rename it to
   * the target path. */
  if (cdbmake_finish(&cm) == -1)
    die_write();
  if (fsync(fd) == -1)
    die_write();
  if (close(fd) == -1)
    die_write();
  if (rename(tmp, path) == -1)
    strerr_die6sys(111, FATAL, "unable to rename ", tmp, " to ", path, ": ");
    
  _exit(0);
}
예제 #7
0
/**
 * uncompress data and copy it to read buffer.
 * Returns true if data has been unpacked or no
 * compressed data is currently pending in the zread buffer.
 * This function closes the connection on error.
 * @param Idx Connection handle.
 * @return true on success, false otherwise.
 */
GLOBAL bool
Unzip_Buffer( CONN_ID Idx )
{
	int result;
	unsigned char unzipbuf[READBUFFER_LEN];
	int unzipbuf_used = 0;
	unsigned int z_rdatalen;
	unsigned int in_len;
	
	z_stream *in;

	assert( Idx > NONE );

	z_rdatalen = (unsigned int)array_bytes(&My_Connections[Idx].zip.rbuf);
	if (z_rdatalen == 0)
		return true;

	in = &My_Connections[Idx].zip.in;

	in->next_in = array_start(&My_Connections[Idx].zip.rbuf);
	assert(in->next_in != NULL);

	in->avail_in = z_rdatalen;
	in->next_out = unzipbuf;
	in->avail_out = (uInt)sizeof unzipbuf;

#ifdef DEBUG_ZIP
	Log(LOG_DEBUG, "in->avail_in %d, in->avail_out %d",
		in->avail_in, in->avail_out);
#endif
	result = inflate( in, Z_SYNC_FLUSH );
	if( result != Z_OK )
	{
		Log(LOG_ALERT, "Decompression error: %s (code=%d, ni=%d, ai=%d, no=%d, ao=%d)!?", in->msg, result, in->next_in, in->avail_in, in->next_out, in->avail_out);
		Conn_Close(Idx, "Decompression error!", NULL, false);
		return false;
	}

	assert(z_rdatalen >= in->avail_in);
	in_len = z_rdatalen - in->avail_in;
	unzipbuf_used = READBUFFER_LEN - in->avail_out;
#ifdef DEBUG_ZIP
	Log(LOG_DEBUG, "unzipbuf_used: %d - %d = %d", READBUFFER_LEN,
		in->avail_out, unzipbuf_used);
#endif
	assert(unzipbuf_used <= READBUFFER_LEN);
	if (!array_catb(&My_Connections[Idx].rbuf, (char*) unzipbuf,
			(size_t)unzipbuf_used)) {
		Log (LOG_ALERT, "Decompression error: can't copy data!?");
		Conn_Close(Idx, "Decompression error!", NULL, false);
		return false;
	}
	if( in->avail_in > 0 ) {
		array_moveleft(&My_Connections[Idx].zip.rbuf, 1, in_len );
	} else {
		array_trunc( &My_Connections[Idx].zip.rbuf );
		My_Connections[Idx].zip.bytes_in += unzipbuf_used;
	}

	return true;
} /* Unzip_Buffer */