sc_bool sc_link_calculate_checksum(const sc_stream *stream, sc_check_sum *check_sum) { sc_char buffer[1024]; sc_uint32 data_read; const gchar *result = 0; GChecksum *checksum = g_checksum_new(SC_DEFAULT_CHECKSUM); g_assert(stream != 0); g_assert(check_sum != 0); g_checksum_reset(checksum); sc_stream_seek(stream, SC_STREAM_SEEK_SET, 0); while (sc_stream_eof(stream) == SC_FALSE) { if (sc_stream_read_data(stream, buffer, 1024, &data_read) == SC_RESULT_ERROR) { g_checksum_free(checksum); return SC_FALSE; } g_checksum_update(checksum, (guchar*)buffer, data_read); } // store results check_sum->len = g_checksum_type_get_length(SC_DEFAULT_CHECKSUM); result = g_checksum_get_string(checksum); memcpy(&(check_sum->data[0]), result, check_sum->len); g_checksum_free(checksum); sc_stream_seek(stream, SC_STREAM_SEEK_SET, 0); return SC_TRUE; }
sc_result sc_fs_storage_write_content(sc_addr addr, const sc_check_sum *check_sum, const sc_stream *stream) { // write content into file sc_char buffer[BuffSize]; sc_uint32 data_read, data_write; sc_stream *out_stream = 0; if (sc_fm_stream_new(fm_engine, check_sum, SC_STREAM_FLAG_WRITE, &out_stream) == SC_RESULT_OK) { g_assert(out_stream != null_ptr); // reset input stream positon to begin sc_stream_seek(stream, SC_STREAM_SEEK_SET, 0); while (sc_stream_eof(stream) == SC_FALSE) { if (sc_stream_read_data(stream, buffer, BuffSize, &data_read) == SC_RESULT_ERROR) { sc_stream_free(out_stream); return SC_RESULT_ERROR; } if (sc_stream_write_data(out_stream, buffer, data_read, &data_write) == SC_RESULT_ERROR) { sc_stream_free(out_stream); return SC_RESULT_ERROR; } if (data_read != data_write) { sc_stream_free(out_stream); return SC_RESULT_ERROR; } } sc_stream_free(out_stream); // reset input stream positon to begin sc_stream_seek(stream, SC_STREAM_SEEK_SET, 0); return sc_fs_storage_add_content_addr(addr, check_sum); } return SC_RESULT_ERROR_IO; }