Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Verify procspec #29

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Minor changes

* fixed tests on platform with no long-doubles ('noLD')
* restored tests on 32bits machines
* `spec_ID` extraction from Avantes exported files (`ttt` and `trt`) is now
more robust, meaning it should work for more files.
Expand Down
30 changes: 28 additions & 2 deletions R/parse_procspec.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#' <https://www.oceaninsight.com/>
#'
#' @inheritParams lr_parse_generic
#' @param check Logical (defaults to `FALSE`). Should we check if the file has
#' been modified since its creation by the spectrometer?
#'
#' @inherit lr_parse_generic return details
#'
Expand All @@ -14,11 +16,11 @@
#' @examples
#' lr_parse_procspec(system.file("testdata", "procspec_files",
#' "OceanOptics_Linux.ProcSpec",
#' package = "lightr"))
#' package = "lightr"), check = TRUE)
#'
#' @export
#'
lr_parse_procspec <- function(filename) {
lr_parse_procspec <- function(filename, check = FALSE) {
# We let R find the suitable tmp folder to extract files
tmp <- tempdir()

Expand All @@ -32,6 +34,30 @@ lr_parse_procspec <- function(filename) {
# Data files have the format ps_\d+.xml
data_file <- grep(pattern = "ps_\\d+\\.xml", extracted_files, value = TRUE)

if (check) {
if (!requireNamespace("openssl")) {
warning("The openssl package is required for check = TRUE. ",
"Skipping integrity check...", call. = FALSE)
} else {
sig <- read_xml(grep(pattern = "OOISignatures\\.xml$", extracted_files, value = TRUE))
saved_hash <- xml_text(xml_find_first(sig, ".//hashValue"))
saved_hash <- gsub(" ", "", saved_hash)
algo <- xml_text(xml_find_first(sig, ".//hashAlgorithm"))
if (algo == "SHA-512") {
actual_hash <- as.character(openssl::sha512(file(data_file)))
} else {
warning("Unknown hash in signature. Skipping.", call. = FALSE)
actual_hash <- saved_hash
}
if (actual_hash != saved_hash) {
stop(
"The file has been modified since its creation by the spectrometer. ",
"This can have serious consequences!\n",
"To bypass the warning, use 'check = FALSE'", call. = FALSE)
}
}
}

# OceanOptics softwares produce badly encoded characters. The only fix is to
# strip them before feeding the xml file to read_xml.
plain_text <- scan(data_file, what = character(), sep = "\n", quiet = TRUE)
Expand Down