Skip to content

Commit

Permalink
fix conversion problems
Browse files Browse the repository at this point in the history
  • Loading branch information
embetrix committed Nov 20, 2024
1 parent 782ac65 commit 5d88264
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ endif()

# Add the executable
add_executable(bmap-writer bmap-writer.cpp)
target_compile_options(bmap-writer PUBLIC -Wformat-security -Wconversion -Wsign-conversion -pedantic -Werror)

# Link the libraries
target_link_libraries(bmap-writer ${LIBXML2_LIBRARIES} ${OPENSSL_LIBRARIES} ${ZLIB_LIBRARIES} ${LIBLZMA_LIBRARIES})
Expand Down
29 changes: 15 additions & 14 deletions bmap-writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ struct range_t {

struct bmap_t {
std::vector<range_t> ranges;
int blockSize;
size_t blockSize;
};

bmap_t parseBMap(const std::string &filename) {
Expand All @@ -71,7 +71,7 @@ bmap_t parseBMap(const std::string &filename) {
if (node->type == XML_ELEMENT_NODE) {
if (strcmp((const char *)node->name, "BlockSize") == 0) {
xmlChar *blockSizeStr = xmlNodeGetContent(node);
bmapData.blockSize = std::stoi((const char *)blockSizeStr);
bmapData.blockSize = static_cast<size_t>(std::stoul((const char *)blockSizeStr));
xmlFree(blockSizeStr);
std::cout << "Parsed BlockSize: " << bmapData.blockSize << std::endl;
} else if (strcmp((const char *)node->name, "BlockMap") == 0) {
Expand Down Expand Up @@ -214,8 +214,8 @@ void BmapWriteImage(const std::string &imageFile, const bmap_t &bmap, const std:
}

for (const auto &range : bmap.ranges) {
int startBlock, endBlock;
if (sscanf(range.range.c_str(), "%d-%d", &startBlock, &endBlock) == 1) {
size_t startBlock, endBlock;
if (sscanf(range.range.c_str(), "%zu-%zu", &startBlock, &endBlock) == 1) {
endBlock = startBlock; // Handle single block range
}
std::cout << "Processing Range: startBlock=" << startBlock << ", endBlock=" << endBlock << std::endl;
Expand All @@ -225,18 +225,19 @@ void BmapWriteImage(const std::string &imageFile, const bmap_t &bmap, const std:
size_t bytesRead = 0;

if (compressionType == "gzip") {
gzseek(gzImg, startBlock * bmap.blockSize, SEEK_SET);
bytesRead = gzread(gzImg, buffer.data(), bufferSize);
if (bytesRead <= 0) {
gzseek(gzImg, static_cast<off_t>(startBlock * bmap.blockSize), SEEK_SET);
int readBytes = gzread(gzImg, buffer.data(), static_cast<unsigned int>(bufferSize));
if (readBytes < 0) {
perror("Failed to read from gzip image file");
close(dev_fd);
gzclose(gzImg);
return;
}
bytesRead = static_cast<size_t>(readBytes);
} else if (compressionType == "xz") {
imgFile.seekg(startBlock * bmap.blockSize, std::ios::beg);
imgFile.read(buffer.data(), bufferSize);
bytesRead = imgFile.gcount();
imgFile.seekg(static_cast<std::streamoff>(startBlock * bmap.blockSize), std::ios::beg);
imgFile.read(buffer.data(), static_cast<std::streamsize>(bufferSize));
bytesRead = static_cast<size_t>(imgFile.gcount());
if (bytesRead == 0 && imgFile.fail()) {
perror("Failed to read from xz image file");
close(dev_fd);
Expand All @@ -256,9 +257,9 @@ void BmapWriteImage(const std::string &imageFile, const bmap_t &bmap, const std:
return;
}
} else if (compressionType == "none") {
imgFile.seekg(startBlock * bmap.blockSize, std::ios::beg);
imgFile.read(buffer.data(), bufferSize);
bytesRead = imgFile.gcount();
imgFile.seekg(static_cast<std::streamoff>(startBlock * bmap.blockSize), std::ios::beg);
imgFile.read(buffer.data(), static_cast<std::streamsize>(bufferSize));
bytesRead = static_cast<size_t>(imgFile.gcount());
if (bytesRead == 0 && imgFile.fail()) {
perror("Failed to read from image file");
close(dev_fd);
Expand All @@ -285,7 +286,7 @@ void BmapWriteImage(const std::string &imageFile, const bmap_t &bmap, const std:
exit(EXIT_FAILURE);
}

if (pwrite(dev_fd, buffer.data(), bytesRead, startBlock * bmap.blockSize) < 0) {
if (pwrite(dev_fd, buffer.data(), bytesRead, static_cast<off_t>(startBlock * bmap.blockSize)) < 0) {
perror("Write to device failed");
close(dev_fd);
if (compressionType == "gzip") {
Expand Down

0 comments on commit 5d88264

Please sign in to comment.