Exemplo n.º 1
0
int32_t mz_get_file_crc(const char *path, uint32_t *result_crc)
{
    void *stream = NULL;
    void *crc32_stream = NULL;
    int32_t read = 0;
    int32_t err = MZ_OK;
    uint8_t buf[INT16_MAX];

    mz_stream_os_create(&stream);

    err = mz_stream_os_open(stream, path, MZ_OPEN_MODE_READ);

    mz_stream_crc32_create(&crc32_stream);
#ifdef HAVE_ZLIB
    mz_stream_crc32_set_update_func(crc32_stream,
        (mz_stream_crc32_update)mz_stream_zlib_get_crc32_update());
#elif defined(HAVE_LZMA)
    mz_stream_crc32_set_update_func(crc32_stream,
        (mz_stream_crc32_update)mz_stream_lzma_get_crc32_update());
#else
    #error ZLIB or LZMA required for CRC32
#endif

    mz_stream_crc32_open(crc32_stream, NULL, MZ_OPEN_MODE_READ);

    mz_stream_set_base(crc32_stream, stream);

    if (err == MZ_OK)
    {
        do
        {
            read = mz_stream_crc32_read(crc32_stream, buf, sizeof(buf));

            if (read < 0)
            {
                err = read;
                break;
            }
        }
        while ((err == MZ_OK) && (read > 0));

        mz_stream_os_close(stream);
    }

    mz_stream_crc32_close(crc32_stream);
    *result_crc = mz_stream_crc32_get_value(crc32_stream);
    mz_stream_crc32_delete(&crc32_stream);

    mz_stream_os_delete(&stream);

    return err;
}
Exemplo n.º 2
0
int32_t mz_file_get_crc(const char *path, uint32_t *result_crc)
{
    void *stream = NULL;
    uint32_t crc32 = 0;
    int32_t read = 0;
    int32_t err = MZ_OK;
    uint8_t buf[16384];

    mz_stream_os_create(&stream);

    err = mz_stream_os_open(stream, path, MZ_OPEN_MODE_READ);

    if (err == MZ_OK)
    {
        do
        {
            read = mz_stream_os_read(stream, buf, sizeof(buf));

            if (read < 0)
            {
                err = read;
                break;
            }

            crc32 = mz_crypt_crc32_update(crc32, buf, read);
        }
        while ((err == MZ_OK) && (read > 0));

        mz_stream_os_close(stream);
    }

    *result_crc = crc32;

    mz_stream_os_delete(&stream);

    return err;
}