Esempio n. 1
0
/* Compress whatever is at avail_in and next_in and write to the output file.
   Return -1 if there is an error writing to the output file, otherwise 0.
   flush is assumed to be a valid deflate() flush value.  If flush is Z_FINISH,
   then the deflate() state is reset to start a new gzip stream.  If gz->direct
   is true, then simply write to the output file without compressing, and
   ignore flush. */
local int gz_comp(gz_statep state,
    int flush)
{
    int ret, got;
    unsigned have;
    z_streamp strm = &(state->strm);

    /* allocate memory if this is the first time through */
    if (state->size == 0 && gz_init(state) == -1)
        return -1;

    /* write directly if requested */
    if (state->direct) {
        got = write(state->fd, strm->next_in, strm->avail_in);
        if (got < 0 || (unsigned)got != strm->avail_in) {
            gz_error(state, Z_ERRNO, zstrerror());
            return -1;
        }
        strm->avail_in = 0;
        return 0;
    }

    /* run deflate() on provided input until it produces no more output */
    ret = Z_OK;
    do {
        /* write out current buffer contents if full, or if flushing, but if
           doing Z_FINISH then don't write until we get to Z_STREAM_END */
        if (strm->avail_out == 0 || (flush != Z_NO_FLUSH &&
            (flush != Z_FINISH || ret == Z_STREAM_END))) {
            have = (unsigned)(strm->next_out - state->x.next);
            if (have && ((got = write(state->fd, state->x.next, have)) < 0 ||
                         (unsigned)got != have)) {
                gz_error(state, Z_ERRNO, zstrerror());
                return -1;
            }
            if (strm->avail_out == 0) {
                strm->avail_out = state->size;
                strm->next_out = state->out;
            }
            state->x.next = strm->next_out;
        }

        /* compress */
        have = strm->avail_out;
        ret = deflate(strm, flush);
        if (ret == Z_STREAM_ERROR) {
            gz_error(state, Z_STREAM_ERROR,
                      "internal error: deflate stream corrupt");
            return -1;
        }
        have -= strm->avail_out;
    } while (have);

    /* if that completed a deflate stream, allow another to start */
    if (flush == Z_FINISH)
        deflateReset(strm);

    /* all done, no errors */
    return 0;
}
Esempio n. 2
0
void test_save(ZWay zway) {
	ZWError r;

	ZWBYTE *data = NULL;
	size_t length = 0;
	r = zway_controller_config_save(zway, &data, &length);
	if (r == NoError) {
		printf("Successfully saved! (size = %lu)\n",
				(unsigned long int) length);

		char path[MAX_PATH];
		strcpy(path, "/tmp/config.tgz");

		FILE *t = fopen(path, "wb");
		if (t != NULL) {
			fwrite(data, 1, length, t);
			fclose(t);

			printf("Config written to %s\n", path);
		} else {
			printf("Config write error: %s\n", strerror(errno));
		}

		free(data);
	} else {
		printf("Error saving configuration! (err = %s)\n", zstrerror(r));
	}
}
Esempio n. 3
0
/* Use read() to load a buffer -- return -1 on error, otherwise 0.  Read from
   state->fd, and update state->eof, state->err, and state->msg as appropriate.
   This function needs to loop on read(), since read() is not guaranteed to
   read the number of bytes requested, depending on the type of descriptor. */
local int gz_load(gz_statep state, unsigned char *buf, unsigned len, unsigned *have)
{
   int ret;

   *have = 0;
   do {
      ret = read(state->fd, buf + *have, len - *have);
      if (ret <= 0)
         break;
      *have += ret;
   } while (*have < len);
   if (ret < 0) {
      gz_error(state, Z_ERRNO, zstrerror());
      return -1;
   }
   if (ret == 0)
      state->eof = 1;
   return 0;
}
Esempio n. 4
0
void test_restore(ZWay zway) {
	char path[MAX_PATH];
	strcpy(path, "/tmp/config.tgz");

	printf("Reading config from %s\n", path);

	struct stat finfo;
	int tr = stat(path, &finfo);
	if (tr != 0) {
		if (errno == ENOENT) {
			printf("Config not found. Save one first!\n");
		} else {
			printf("Config read error: %s\n", strerror(errno));
		}
		return;
	}

	size_t length = finfo.st_size;
	ZWBYTE *data = (ZWBYTE*) malloc(length);
	if (data == NULL) {
		printf("Failed to alloc %zu bytes!\n", length);
		return;
	}

	FILE *t = fopen(path, "rb");
	if (t != NULL) {
		fread(data, 1, length, t);
		fclose(t);
	} else {
		free(data);
		printf("Config read error: %s\n", strerror(errno));
		return;
	}

	ZWError r = zway_controller_config_restore(zway, data, length, FALSE);
	if (r == NoError) {
		printf("Configuration successfully restored\n");
	} else {
		printf("Error restoring configuration! (err = %s)\n", zstrerror(r));
	}

	free(data);
}