R/base64.R

Defines functions base64_decode base64_encode

Documented in base64_decode base64_encode

################################################################################
# This file is released under the GNU General Public License, Version 3, GPL-3 #
# Copyright (C) 2024 Yohann Demont                                             #
#                                                                              #
# It is part of IFC package, please cite:                                      #
# -IFC: An R Package for Imaging Flow Cytometry                                #
# -YEAR: 2024                                                                  #
# -COPYRIGHT HOLDERS: Yohann Demont, Gautier Stoll, Guido Kroemer,             #
#                     Jean-Pierre Marolleau, Loïc Garçon,                      #
#                     INSERM, UPD, CHU Amiens                                  #
#                                                                              #
# DISCLAIMER:                                                                  #
# -You are using this package on your own risk!                                #
# -We do not guarantee privacy nor confidentiality.                            #
# -This program is distributed in the hope that it will be useful, but WITHOUT #
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or        #
# FITNESS FOR A PARTICULAR PURPOSE. In no event shall the copyright holders or #
# contributors be liable for any direct, indirect, incidental, special,        #
# exemplary, or consequential damages (including, but not limited to,          #
# procurement of substitute goods or services; loss of use, data, or profits;  #
# or business interruption) however caused and on any theory of liability,     #
# whether in contract, strict liability, or tort (including negligence or      #
# otherwise) arising in any way out of the use of this software, even if       #
# advised of the possibility of such damage.                                   #
#                                                                              #
# You should have received a copy of the GNU General Public License            #
# along with IFC. If not, see <http://www.gnu.org/licenses/>.                  #
################################################################################

#' @title Raw to Base64 Encoding
#' @description
#' Encodes raw vector to base64 string
#' @param x a raw vector.
#' @param url a bool whether to encode for base64 url string. Default is \code{FALSE}. 
#' @source \url{https://en.wikibooks.org/wiki/Algorithm_Implementation/Miscellaneous/Base64}
#' @keywords internal
base64_encode <- function(x, url = FALSE) {
  if(is.null(x)) return("")
  return(structure(cpp_base64_encode(x, url), url = url))
}

#' @title Base64 to Raw Decoding
#' @description
#' Decodes base64 string to raw vector
#' @param x a base64 encoded string.
#' @param url a bool whether to decode from base64 url encoding. Default is \code{identical(attr(x, "url", exact = TRUE), TRUE)}.
#' @source \url{https://en.wikibooks.org/wiki/Algorithm_Implementation/Miscellaneous/Base64}
#' @keywords internal
base64_decode <- function(x, url = identical(attr(x, "url", exact = TRUE), TRUE)) {
  if(is.null(x)) return(raw(0))
  return(cpp_base64_decode(x, url))
}
gitdemont/IFC documentation built on Feb. 20, 2025, 12:55 a.m.