base64encode & base64decode     package:caTools     R Documentation

_C_o_n_v_e_r_t _R _v_e_c_t_o_r_s _t_o/_f_r_o_m _t_h_e _B_a_s_e_6_4 _f_o_r_m_a_t

_D_e_s_c_r_i_p_t_i_o_n:

     Convert R vectors of any type to and from the Base64 format for
     encrypting any binary data as string using alphanumeric subset of
     ASCII character set.

_U_s_a_g_e:

       z = base64encode(x, size=NA, endian=.Platform$endian)
       x = base64decode(z, what, size=NA, signed = TRUE, endian=.Platform$endian)

_A_r_g_u_m_e_n_t_s:

       x: vector or any structure that can be converted to a vector by 
           'as.vector' function. Strings are also allowed.

       z: String with Base64 code, using [A-Z,a-z,0-9,+,/,=] subset of 
          characters

    what: Either an object whose mode will give the mode of the vector
          to be created, or a character vector of length one describing
          the mode: one of '"numeric", "double", "integer", "int",
          "logical", "complex", "character", "raw".   Same as variable
          'what' in 'readBin' functions. 

    size: integer.  The number of bytes per element in the byte stream 
          stored in 'r'. The default, ''NA'', uses the natural size. 
          Same as variable 'size' in 'readBin' functions. 

  signed: logical. Only used for integers of sizes 1 and 2, when it
          determines if the quantity stored as raw should be regarded
          as a signed or unsigned integer. Same as variable 'signed' in
          'readBin' functions. 

  endian: If provided, can be used to swap endian-ness. Using '"swap"' 
          will force swapping of byte order. Use '"big"' (big-endian,
          aka IEEE,  aka "network") or '"little"' (little-endian,
          format used on PC/Intel  machines) to indicate type of data
          encoded in "raw" format. Same as variable 'endian' in
          'readBin' functions.

_D_e_t_a_i_l_s:

     The Base64 encoding is designed to encode arbitrary binary
     information for  transmission by electronic mail. It is defined by
     MIME (Multipurpose Internet  Mail Extensions) specification RFC
     1341, RFC 1421, RFC 2045 and others.  Triplets of 8-bit octets are
     encoded as groups of four characters, each  representing 6 bits of
     the source 24 bits. Only a 65-character subset 
     ([A-Z,a-z,0-9,+,/,=]) present in all variants of ASCII and EBCDIC
     is used,  enabling 6 bits to be represented per printable
     character.

     Default 'size's for different types of 'what': 'logical' - 4, 
     'integer' - 4, 'double' - 8 , 'complex' - 16,  'character' - 2,
     'raw' - 1.

_V_a_l_u_e:

     Function 'base64encode' returns a string with Base64 code.
     Function 'base64decode' returns vector of appropriate mode  and
     length (see 'x' above).

_A_u_t_h_o_r(_s):

     Jarek Tuszynski (SAIC) jaroslaw.w.tuszynski@saic.com

_R_e_f_e_r_e_n_c_e_s:


        *  Base64 description in _Connected: An Internet Encyclopedia_
           <URL: http://www.freesoft.org/CIE/RFC/1521/7.htm>

        *  MIME RFC 1341 <URL: http://www.faqs.org/rfcs/rfc1341.html>

        *  MIME RFC 1421 <URL: http://www.faqs.org/rfcs/rfc1421.html>

        *  MIME RFC 2045 <URL: http://www.faqs.org/rfcs/rfc2045.html>

        *  Portions of the code are based on Matlab code by Peter
           Acklam <URL:
           http://home.online.no/~pjacklam/matlab/software/util/datautil/>

_S_e_e _A_l_s_o:

     'xmlValue' from 'XML' package reads XML code which sometimes is
     encoded in Base64 format.

     'readBin', 'writeBin'

_E_x_a_m_p_l_e_s:

        x = (10*runif(10)>5) # logical
        for (i in c(NA, 1, 2, 4)) {
          y = base64encode(x, size=i)
          z = base64decode(y, typeof(x), size=i)
          stopifnot(x==z)
        }
        print("Checked base64 for encode/decode logical type")

        x = as.integer(1:10) # integer
        for (i in c(NA, 1, 2, 4)) {
          y = base64encode(x, size=i)
          z = base64decode(y, typeof(x), size=i)
          stopifnot(x==z)
        }
        print("Checked base64 encode/decode for integer type")
        
        x = (1:10)*pi        # double
        for (i in c(NA, 4, 8)) {
          y = base64encode(x, size=i)
          z = base64decode(y, typeof(x), size=i)
          stopifnot(mean(abs(x-z))<1e-5)
        }
        print("Checked base64 for encode/decode double type")
        
        x = log(as.complex(-(1:10)*pi))        # complex
        y = base64encode(x)
        z = base64decode(y, typeof(x))
        stopifnot(x==z)
        print("Checked base64 for encode/decode complex type")
       
        x = "Chance favors the prepared mind" # character
        y = base64encode(x)
        z = base64decode(y, typeof(x))
        stopifnot(x==z)
        print("Checked base64 for encode/decode character type")

