Пример #1
0
void
test_helper_cw_input(const char *filename,
                     cr_CompressionType ctype,
                     const char *content,
                     int len)
{
    int ret;
    CR_FILE *file;
    char buffer[COMPRESSED_BUFFER_LEN+1];
    GError *tmp_err = NULL;

    file = cr_open(filename, CR_CW_MODE_READ, ctype, &tmp_err);
    g_assert(file);
    g_assert(!tmp_err);

    ret = cr_read(file, buffer, COMPRESSED_BUFFER_LEN, &tmp_err);
    g_assert_cmpint(ret, ==, len);
    g_assert(!tmp_err);

    buffer[ret] = '\0';
    g_assert_cmpstr(buffer, ==, content);
    ret = cr_close(file, &tmp_err);
    g_assert_cmpint(ret, ==, CRE_OK);
    g_assert(!tmp_err);
}
Пример #2
0
void
test_helper_cw_output(int type,
                      const char *filename,
                      cr_CompressionType ctype,
                      const char *content,
                      int len)
{
    int ret;
    CR_FILE *file;
    GError *tmp_err = NULL;
    file = cr_open(filename, CR_CW_MODE_WRITE, ctype, &tmp_err);
    g_assert(file);
    g_assert(!tmp_err);

    switch(type) {
        case OUTPUT_TYPE_WRITE:
            ret = cr_write(file, content, len, &tmp_err);
            g_assert_cmpint(ret, ==, len);
            g_assert(!tmp_err);
            break;

        case OUTPUT_TYPE_PUTS:
            ret = cr_puts(file, content, &tmp_err);
            g_assert_cmpint(ret, ==, len);
            g_assert(!tmp_err);
            break;

        case OUTPUT_TYPE_PRINTF:
            ret = cr_printf(&tmp_err, file, "%s", content);
            g_assert_cmpint(ret, ==, len);
            g_assert(!tmp_err);
            break;

        default:
            break;
    }

    ret = cr_close(file, &tmp_err);
    g_assert_cmpint(ret, ==, CRE_OK);
    g_assert(!tmp_err);

    // Read and compare

    test_helper_cw_input(filename, ctype, content, len);
}
Пример #3
0
static void
test_contentstating_multiwrite(Outputtest *outputtest,
                               G_GNUC_UNUSED gconstpointer test_data)
{
    CR_FILE *f;
    int ret;
    cr_ContentStat *stat;
    GError *tmp_err = NULL;

    const char *content = "sdlkjowykjnhsadyhfsoaf\nasoiuyseahlndsf\n";
    const int content_len = 39;
    const char *content_sha256 = "c9d112f052ab86270bfb484817a513d6ce188133ddc0"
                                 "7c0fc1ac32018b6da6c7";

    // Gz compression

    stat = cr_contentstat_new(CR_CHECKSUM_SHA256, &tmp_err);
    g_assert(stat);
    g_assert(!tmp_err);

    f = cr_sopen(outputtest->tmp_filename,
                 CR_CW_MODE_WRITE,
                 CR_CW_GZ_COMPRESSION,
                 stat,
                 &tmp_err);
    g_assert(f);
    g_assert(!tmp_err);

    ret = cr_write(f, content, 10, &tmp_err);
    g_assert_cmpint(ret, ==, 10);
    g_assert(!tmp_err);

    ret = cr_write(f, content+10, 29, &tmp_err);
    g_assert_cmpint(ret, ==, 29);
    g_assert(!tmp_err);

    cr_close(f, &tmp_err);
    g_assert(!tmp_err);

    g_assert_cmpint(stat->size, ==, content_len);
    g_assert_cmpstr(stat->checksum, ==, content_sha256);
    cr_contentstat_free(stat, &tmp_err);
    g_assert(!tmp_err);
}
Пример #4
0
int main()
{
	char string[256] = {0};

	fgets(string,256,stdin);

	cr_init("derpderpderpderp","AAAAAAAAAAAAAAAA");

	printf("Plaintext before: %s\n",string);

	cr_encrypt(string);	
	cr_decrypt(string);

	printf("Plaintext after : %s\n",string);


	cr_close();

	return 0;
}
Пример #5
0
int
cr_xmlfile_close(cr_XmlFile *f, GError **err)
{
    GError *tmp_err = NULL;

    assert(!err || *err == NULL);

    if (!f)
        return CRE_OK;

    if (f->header == 0) {
        cr_xmlfile_write_xml_header(f, &tmp_err);
        if (tmp_err) {
            int code = tmp_err->code;
            g_propagate_error(err, tmp_err);
            return code;
        }
    }

    if (f->footer == 0) {
        cr_xmlfile_write_xml_footer(f, &tmp_err);
        if (tmp_err) {
            int code = tmp_err->code;
            g_propagate_error(err, tmp_err);
            return code;
        }
    }

    cr_close(f->f, &tmp_err);
    if (tmp_err) {
        int code = tmp_err->code;
        g_propagate_prefixed_error(err, tmp_err,
                "Error while closing a file: ");
        return code;
    }

    g_free(f);

    return CRE_OK;
}
Пример #6
0
//Simple file display to show how easy it is to use the cached reader functions
int main(){
  char c;
  int refill_count=0;
  int byte_count=0;
  //Open a file
  cr_file* f = cr_open("text",20);

  //While there are useful bytes coming from it
  while((c=cr_read_byte(f))!=EOF){
    //Print them
    printf("%c",c);

  }

  //Then close the file
  printf("\nByte Count: %d",f->byte_tot); // Displaying the total number of bytes read.
  printf("\nRefill Count: %d\n",f->byte_tot/f->bufferlength); //Displaying the total number of times the buffer was filled. (No_of_bytes / buffersize).
  cr_close(f);

  //And finish
  return 0;
}
Пример #7
0
void gc_close(void) {
	free(data);
	cr_close();
}
Пример #8
0
static void
test_cr_error_handling(void)
{
    GError *tmp_err = NULL;
    cr_CompressionType type;
    CR_FILE *f;

    type = cr_detect_compression("/filename/that/should/not/exists", &tmp_err);
    g_assert_cmpint(type, ==, CR_CW_UNKNOWN_COMPRESSION);
    g_assert(tmp_err);
    g_assert_cmpint(tmp_err->code, ==, CRE_NOFILE);
    g_error_free(tmp_err);
    tmp_err = NULL;

    type = cr_detect_compression("/", &tmp_err);
    g_assert_cmpint(type, ==, CR_CW_UNKNOWN_COMPRESSION);
    g_assert(tmp_err);
    g_assert_cmpint(tmp_err->code, ==, CRE_NOFILE);
    g_error_free(tmp_err);
    tmp_err = NULL;

    f = cr_open("/", CR_CW_MODE_READ, CR_CW_AUTO_DETECT_COMPRESSION, &tmp_err);
    g_assert(!f);
    g_assert(tmp_err);
    g_assert_cmpint(tmp_err->code, ==, CRE_NOFILE);
    g_error_free(tmp_err);
    tmp_err = NULL;

    // Opening dir for writing

    f = cr_open("/", CR_CW_MODE_WRITE, CR_CW_NO_COMPRESSION, &tmp_err);
    g_assert(!f);
    g_assert(tmp_err);
    g_assert_cmpint(tmp_err->code, ==, CRE_IO);
    g_error_free(tmp_err);
    tmp_err = NULL;

    f = cr_open("/", CR_CW_MODE_WRITE, CR_CW_GZ_COMPRESSION, &tmp_err);
    g_assert(!f);
    g_assert(tmp_err);
    g_assert_cmpint(tmp_err->code, ==, CRE_GZ);
    g_error_free(tmp_err);
    tmp_err = NULL;

    f = cr_open("/", CR_CW_MODE_WRITE, CR_CW_BZ2_COMPRESSION, &tmp_err);
    g_assert(!f);
    g_assert(tmp_err);
    g_assert_cmpint(tmp_err->code, ==, CRE_IO);
    g_error_free(tmp_err);
    tmp_err = NULL;

    f = cr_open("/", CR_CW_MODE_WRITE, CR_CW_XZ_COMPRESSION, &tmp_err);
    g_assert(!f);
    g_assert(tmp_err);
    g_assert_cmpint(tmp_err->code, ==, CRE_XZ);
    g_error_free(tmp_err);
    tmp_err = NULL;

    // Opening plain text file as compressed

    char buf[256];
    int ret;

    // gzread can read compressed as well as uncompressed, so this test
    // is useful.
    f = cr_open(FILE_COMPRESSED_1_PLAIN, CR_CW_MODE_READ,
                CR_CW_GZ_COMPRESSION, &tmp_err);
    g_assert(f);
    ret = cr_read(f, buf, 256, &tmp_err);
    g_assert_cmpint(ret, ==, FILE_COMPRESSED_1_CONTENT_LEN);
    g_assert(!tmp_err);
    ret = cr_close(f, &tmp_err);
    g_assert_cmpint(ret, ==, CRE_OK);
    g_assert(!tmp_err);

    f = cr_open(FILE_COMPRESSED_1_PLAIN, CR_CW_MODE_READ,
                CR_CW_BZ2_COMPRESSION, &tmp_err);
    g_assert(f);
    ret = cr_read(f, buf, 256, &tmp_err);
    g_assert_cmpint(ret, ==, -1);
    g_assert(tmp_err);
    g_assert_cmpint(tmp_err->code, ==, CRE_BZ2);
    g_error_free(tmp_err);
    tmp_err = NULL;
    ret = cr_close(f, &tmp_err);
    g_assert_cmpint(ret, ==, CRE_OK);
    g_assert(!tmp_err);

    f = cr_open(FILE_COMPRESSED_1_PLAIN, CR_CW_MODE_READ,
                CR_CW_XZ_COMPRESSION, &tmp_err);
    g_assert(f);
    ret = cr_read(f, buf, 256, &tmp_err);
    g_assert_cmpint(ret, ==, -1);
    g_assert(tmp_err);
    g_assert_cmpint(tmp_err->code, ==, CRE_XZ);
    g_error_free(tmp_err);
    tmp_err = NULL;
    ret = cr_close(f, &tmp_err);
    g_assert_cmpint(ret, ==, CRE_OK);
    g_assert(!tmp_err);
}
Пример #9
0
int
cr_repomd_record_compress_and_fill(cr_RepomdRecord *record,
                                   cr_RepomdRecord *crecord,
                                   cr_ChecksumType checksum_type,
                                   cr_CompressionType record_compression,
                                   GError **err)
{
    const char *suffix;
    gchar *path, *cpath;
    gchar *clocation_real, *clocation_href;
    gchar *checksum, *cchecksum;
    int readed;
    char buf[BUFFER_SIZE];
    CR_FILE *cw_plain;
    CR_FILE *cw_compressed;
    gint64 gf_size = -1, cgf_size = -1;
    gint64 gf_time = -1, cgf_time = -1;
    struct stat gf_stat, cgf_stat;
    const char *checksum_str = cr_checksum_name_str(checksum_type);
    GError *tmp_err = NULL;

    assert(record);
    assert(crecord);
    assert(!err || *err == NULL);

    if (!(record->location_real) || !strlen(record->location_real)) {
        g_set_error(err, CR_REPOMD_RECORD_ERROR, CRE_BADARG,
                    "Empty locations in repomd record object");
        return CRE_BADARG;
    }

    if (!g_file_test(record->location_real, G_FILE_TEST_IS_REGULAR)) {
        // File doesn't exists
        g_warning("%s: File %s doesn't exists", __func__, record->location_real);
        g_set_error(err, CR_REPOMD_RECORD_ERROR, CRE_NOFILE,
                    "File %s doesn't exists or not a regular file",
                    record->location_real);
        return CRE_NOFILE;;
    }

    // Paths

    suffix = cr_compression_suffix(record_compression);

    clocation_real = g_strconcat(record->location_real, suffix, NULL);
    clocation_href = g_strconcat(record->location_href, suffix, NULL);
    crecord->location_real = g_string_chunk_insert(crecord->chunk,
                                                   clocation_real);
    crecord->location_href = g_string_chunk_insert(crecord->chunk,
                                                   clocation_href);
    g_free(clocation_real);
    g_free(clocation_href);

    path  = record->location_real;
    cpath = crecord->location_real;


    // Compress file + get size of non compressed file

    cw_plain = cr_open(path,
                       CR_CW_MODE_READ,
                       CR_CW_NO_COMPRESSION,
                       &tmp_err);
    if (!cw_plain) {
        int code = tmp_err->code;
        g_propagate_prefixed_error(err, tmp_err, "Cannot open %s: ", path);
        return code;
    }

    cw_compressed = cr_open(cpath,
                            CR_CW_MODE_WRITE,
                            record_compression,
                            &tmp_err);
    if (!cw_compressed) {
        int code = tmp_err->code;
        g_propagate_prefixed_error(err, tmp_err, "Cannot open %s: ", cpath);
        return code;
    }

    while ((readed = cr_read(cw_plain, buf, BUFFER_SIZE, &tmp_err)) > 0) {
        cr_write(cw_compressed, buf, (unsigned int) readed, &tmp_err);
        if (tmp_err)
            break;
    }

    cr_close(cw_plain, NULL);

    if (tmp_err) {
        int code = tmp_err->code;
        cr_close(cw_compressed, NULL);
        g_debug("%s: Error while repomd record compression: %s", __func__,
                tmp_err->message);
        g_propagate_prefixed_error(err, tmp_err,
                "Error while compression %s -> %s:", path, cpath);
        return code;
    }

    cr_close(cw_compressed, &tmp_err);
    if (tmp_err) {
        int code = tmp_err->code;
        g_propagate_prefixed_error(err, tmp_err,
                "Error while closing %s: ", path);
        return code;
    }


    // Compute checksums

    checksum  = cr_checksum_file(path, checksum_type, &tmp_err);
    if (tmp_err) {
        int code = tmp_err->code;
        g_propagate_prefixed_error(err, tmp_err,
                                   "Error while checksum calculation:");
        return code;
    }

    cchecksum = cr_checksum_file(cpath, checksum_type, &tmp_err);
    if (tmp_err) {
        int code = tmp_err->code;
        g_propagate_prefixed_error(err, tmp_err,
                                   "Error while checksum calculation:");
        return code;
    }


    // Get stats

    if (stat(path, &gf_stat)) {
        g_debug("%s: Error while stat() on %s", __func__, path);
        g_set_error(err, CR_REPOMD_RECORD_ERROR, CRE_IO,
                    "Cannot stat %s", path);
        return CRE_IO;
    } else {
        gf_size = gf_stat.st_size;
        gf_time = gf_stat.st_mtime;
    }

    if (stat(cpath, &cgf_stat)) {
        g_debug("%s: Error while stat() on %s", __func__, cpath);
        g_set_error(err, CR_REPOMD_RECORD_ERROR, CRE_IO,
                    "Cannot stat %s", cpath);
        return CRE_IO;
    } else {
        cgf_size = cgf_stat.st_size;
        cgf_time = cgf_stat.st_mtime;
    }


    // Results

    record->checksum = g_string_chunk_insert(record->chunk, checksum);
    record->checksum_type = g_string_chunk_insert(record->chunk, checksum_str);
    record->checksum_open = NULL;
    record->checksum_open_type = NULL;
    record->timestamp = gf_time;
    record->size = gf_size;
    record->size_open = -1;

    crecord->checksum = g_string_chunk_insert(crecord->chunk, cchecksum);
    crecord->checksum_type = g_string_chunk_insert(crecord->chunk, checksum_str);
    crecord->checksum_open = g_string_chunk_insert(record->chunk, checksum);
    crecord->checksum_open_type = g_string_chunk_insert(record->chunk, checksum_str);
    crecord->timestamp = cgf_time;
    crecord->size = cgf_size;
    crecord->size_open = gf_size;

    g_free(checksum);
    g_free(cchecksum);

    return CRE_OK;
}
Пример #10
0
contentStat *
cr_get_compressed_content_stat(const char *filename,
                               cr_ChecksumType checksum_type,
                               GError **err)
{
    GError *tmp_err = NULL;

    assert(filename);
    assert(!err || *err == NULL);

    if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
        g_set_error(err, CR_REPOMD_RECORD_ERROR, CRE_NOFILE,
                    "File %s doesn't exists or not a regular file", filename);
        return NULL;
    }


    // Open compressed file

    CR_FILE *cwfile = cr_open(filename,
                              CR_CW_MODE_READ,
                              CR_CW_AUTO_DETECT_COMPRESSION,
                              &tmp_err);
    if (!cwfile) {
        g_propagate_prefixed_error(err, tmp_err,
                                   "Cannot open a file %s: ", filename);
        return NULL;
    }


    // Read compressed file and calculate checksum and size

    cr_ChecksumCtx *checksum = cr_checksum_new(checksum_type, &tmp_err);
    if (tmp_err) {
        g_critical("%s: g_checksum_new() failed", __func__);
        g_propagate_prefixed_error(err, tmp_err,
                "Error while checksum calculation: ");
        return NULL;
    }

    gint64 size = 0;
    long readed;
    unsigned char buffer[BUFFER_SIZE];

    do {
        readed = cr_read(cwfile, (void *) buffer, BUFFER_SIZE, &tmp_err);
        if (readed == CR_CW_ERR) {
            g_debug("%s: Error while read compressed file %s: %s",
                    __func__, filename, tmp_err->message);
            g_propagate_prefixed_error(err, tmp_err,
                        "Error while read compressed file %s: ",
                        filename);
            break;
        }
        cr_checksum_update(checksum, buffer, readed, NULL);
        size += readed;
    } while (readed == BUFFER_SIZE);

    if (readed == CR_CW_ERR)
        return NULL;

    // Create result structure

    contentStat* result = g_malloc(sizeof(contentStat));
    if (result) {
        result->checksum = cr_checksum_final(checksum, NULL);
        result->size = size;
    } else {
        g_set_error(err, CR_REPOMD_RECORD_ERROR, CRE_MEMORY,
                    "Cannot allocate memory");
    }

    cr_close(cwfile, NULL);

    return result;
}
Пример #11
0
int
cr_xml_parser_generic(XML_Parser parser,
                      cr_ParserData *pd,
                      const char *path,
                      GError **err)
{
    /* Note: This function uses .err members of cr_ParserData! */

    int ret = CRE_OK;
    CR_FILE *f;
    GError *tmp_err = NULL;

    assert(parser);
    assert(pd);
    assert(path);
    assert(!err || *err == NULL);

    f = cr_open(path, CR_CW_MODE_READ, CR_CW_AUTO_DETECT_COMPRESSION, &tmp_err);
    if (tmp_err) {
        int code = tmp_err->code;
        g_propagate_prefixed_error(err, tmp_err, "Cannot open %s: ", path);
        return code;
    }

    while (1) {
        int len;
        void *buf = XML_GetBuffer(parser, XML_BUFFER_SIZE);
        if (!buf) {
            ret = CRE_MEMORY;
            g_set_error(err, ERR_DOMAIN, CRE_MEMORY,
                        "Out of memory: Cannot allocate buffer for xml parser '%s'",
                        path);
            break;
        }

        len = cr_read(f, buf, XML_BUFFER_SIZE, &tmp_err);
        if (tmp_err) {
            ret = tmp_err->code;
            g_critical("%s: Error while reading xml '%s': %s",
                       __func__, path, tmp_err->message);
            g_propagate_prefixed_error(err, tmp_err, "Read error: ");
            break;
        }

        if (!XML_ParseBuffer(parser, len, len == 0)) {
            ret = CRE_XMLPARSER;
            g_critical("%s: parsing error '%s': %s",
                       __func__,
                       path,
                       XML_ErrorString(XML_GetErrorCode(parser)));
            g_set_error(err, ERR_DOMAIN, CRE_XMLPARSER,
                        "Parse error '%s' at line: %d (%s)",
                        path,
                        (int) XML_GetCurrentLineNumber(parser),
                        (char *) XML_ErrorString(XML_GetErrorCode(parser)));
            break;
        }

        if (pd->err) {
            ret = pd->err->code;
            g_propagate_error(err, pd->err);
            break;
        }

        if (len == 0)
            break;
    }

    if (ret != CRE_OK) {
        // An error already encoutentered
        // just close the file without error checking
        cr_close(f, NULL);
    } else {
        // No error encountered yet
        cr_close(f, &tmp_err);
        if (tmp_err) {
            ret = tmp_err->code;
            g_propagate_prefixed_error(err, tmp_err, "Error while closing: ");
        }
    }

    return ret;
}