Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F82106501
base64_reader.hh
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Mon, Sep 9, 15:20
Size
10 KB
Mime Type
text/x-c
Expires
Wed, Sep 11, 15:20 (1 d, 21 h)
Engine
blob
Format
Raw Data
Handle
20652778
Attached To
rLIBMULTISCALE LibMultiScale
base64_reader.hh
View Options
/**
* @file base64_reader.hh
*
* @author Guillaume Anciaux <guillaume.anciaux@epfl.ch>
*
* @date Fri Mar 01 23:56:16 2013
*
* @brief Class enabling reading of base64 encoding
*
* @section LICENSE
*
* Copyright INRIA and CEA
*
* The LibMultiScale is a C++ parallel framework for the multiscale
* coupling methods dedicated to material simulations. This framework
* provides an API which makes it possible to program coupled simulations
* and integration of already existing codes.
*
* This Project was initiated in a collaboration between INRIA Futurs Bordeaux
* within ScAlApplix team and CEA/DPTA Ile de France.
* The project is now continued at the Ecole Polytechnique Fédérale de Lausanne
* within the LSMS/ENAC laboratory.
*
* This software is governed by the CeCILL-C license under French law and
* abiding by the rules of distribution of free software. You can use,
* modify and/ or redistribute the software under the terms of the CeCILL-C
* license as circulated by CEA, CNRS and INRIA at the following URL
* "http://www.cecill.info".
*
* As a counterpart to the access to the source code and rights to copy,
* modify and redistribute granted by the license, users are provided only
* with a limited warranty and the software's author, the holder of the
* economic rights, and the successive licensors have only limited
* liability.
*
* In this respect, the user's attention is drawn to the risks associated
* with loading, using, modifying and/or developing or reproducing the
* software by the user in light of its specific status of free software,
* that may mean that it is complicated to manipulate, and that also
* therefore means that it is reserved for developers and experienced
* professionals having in-depth computer knowledge. Users are therefore
* encouraged to load and test the software's suitability as regards their
* requirements in conditions enabling the security of their systems and/or
* data to be ensured and, more generally, to use and operate it in the
* same conditions as regards security.
*
* The fact that you are presently reading this means that you have had
* knowledge of the CeCILL-C license and that you accept its terms.
*
*/
#ifndef __LIBMULTISCALE_BASE64_READER_HH__
#define __LIBMULTISCALE_BASE64_READER_HH__
/* -------------------------------------------------------------------------- */
#include "cstring"
/* -------------------------------------------------------------------------- */
__BEGIN_LIBMULTISCALE__
/** Class that allow to push binary data
in base64 format to any file.
This class is mainly used by the paraview helper
to create binary XML VTK files.
The conversion is a 4/3 size conversion. */
class
Base64Reader
{
/* ------------------------------------------------------------------------ */
/* Constructors/Destructors */
/* ------------------------------------------------------------------------ */
public
:
Base64Reader
();
/* ------------------------------------------------------------------------ */
/* Methods */
/* ------------------------------------------------------------------------ */
//! push to file a Real
UInt
PopRealInBase64
(
Real
&
c
);
//! push to file an integer
UInt
PopIntegerInBase64
(
UInt
&
c
);
//! push to file a Byte
UInt
PopByteInBase64
(
unsigned
char
&
c
);
//! decode 3 bytes from 4 Base64 bytes (4/3 ratio)
UInt
Decode
(
unsigned
char
c0
,
unsigned
char
c1
,
unsigned
char
c2
,
unsigned
char
c3
,
unsigned
char
*
r0
,
unsigned
char
*
r1
,
unsigned
char
*
r2
);
//! set the reading stream
void
SetStream
(
char
*
str
,
UInt
l
);
//! reset internal reading variables
void
reset
();
private
:
//! initialisation process
void
InitBase64Stuff
();
/* ------------------------------------------------------------------------ */
/* Class Members */
/* ------------------------------------------------------------------------ */
//! decoding table
char
dtable
[
256
];
//! encoding table
char
etable
[
256
];
//! stage in conversion process(1,2 or 3)
UInt
n
;
//! stage for coded bytes
UInt
n_coded
;
//! used to code/decode
unsigned
char
igroup
[
3
],
ogroup
[
4
];
//! starting stream position
char
*
start_ptr
;
//! last reading position
char
*
current_ptr
;
//! size of the stream
UInt
len
;
//! readed bytes
unsigned
char
read_byte
[
3
];
//! readed bytes but still coded
char
read_coded_byte
[
4
];
//! temporary Real
Real
temp_Real
;
//! readed bytes in Real
UInt
read_bytes_in_Real
;
};
/* -------------------------------------------------------------------------- */
inline
void
Base64Reader
::
InitBase64Stuff
(){
memset
(
dtable
,
0xFF
,
256
);
memset
(
etable
,
0xFF
,
256
);
for
(
UInt
i
=
0
;
i
<
9
;
i
++
){
etable
[
i
]
=
(
char
)(
'A'
+
i
);
dtable
[
0
+
etable
[
i
]]
=
(
char
)
i
;
etable
[
i
+
9
]
=
(
char
)(
'J'
+
i
);
dtable
[
0
+
etable
[
i
+
9
]]
=
(
char
)(
i
+
9
);
etable
[
26
+
i
]
=
(
char
)(
'a'
+
i
);
dtable
[
0
+
etable
[
26
+
i
]]
=
(
char
)(
i
+
26
);
etable
[
26
+
i
+
9
]
=
(
char
)(
'j'
+
i
);
dtable
[
0
+
etable
[
26
+
i
+
9
]]
=
(
char
)(
i
+
26
+
9
);
}
for
(
UInt
i
=
0
;
i
<
8
;
i
++
){
etable
[
i
+
18
]
=
(
char
)(
'S'
+
i
);
dtable
[
0
+
etable
[
i
+
18
]]
=
(
char
)(
i
+
18
);
etable
[
26
+
i
+
18
]
=
(
char
)(
's'
+
i
);
dtable
[
0
+
etable
[
26
+
i
+
18
]]
=
(
char
)(
26
+
i
+
18
);
}
for
(
UInt
i
=
0
;
i
<
10
;
i
++
){
etable
[
52
+
i
]
=
(
char
)(
'0'
+
i
);
dtable
[
0
+
etable
[
i
+
52
]]
=
(
char
)(
i
+
52
);
}
etable
[
62
]
=
'+'
;
dtable
[
0
+
etable
[
62
]]
=
62
;
etable
[
63
]
=
'/'
;
dtable
[
0
+
etable
[
63
]]
=
63
;
}
/* -------------------------------------------------------------------------- */
inline
Base64Reader
::
Base64Reader
(){
InitBase64Stuff
();
reset
();
}
/* -------------------------------------------------------------------------- */
inline
void
Base64Reader
::
reset
(){
n
=
0
;
n_coded
=
0
;
read_bytes_in_Real
=
0
;
}
/* -------------------------------------------------------------------------- */
inline
void
Base64Reader
::
SetStream
(
char
*
str
,
UInt
l
){
current_ptr
=
str
;
start_ptr
=
str
;
len
=
l
;
DUMP
(
"n_coded "
<<
n_coded
,
DBG_DETAIL
);
if
(
n_coded
>
0
){
//je relie l'ancien stream et le nouveau
if
(
n_coded
==
1
&&
current_ptr
<
start_ptr
+
len
)
{
read_coded_byte
[
1
]
=
*
current_ptr
;
++
n_coded
;
++
current_ptr
;
}
DUMP
(
"n_coded "
<<
n_coded
,
DBG_DETAIL
);
if
(
n_coded
==
2
&&
current_ptr
<
start_ptr
+
len
)
{
read_coded_byte
[
2
]
=
*
current_ptr
;
++
n_coded
;
++
current_ptr
;
}
DUMP
(
"n_coded "
<<
n_coded
,
DBG_DETAIL
);
if
(
n_coded
==
3
&&
current_ptr
<
start_ptr
+
len
)
{
read_coded_byte
[
3
]
=
*
current_ptr
;
++
n_coded
;
++
current_ptr
;
}
DUMP
(
"n_coded "
<<
n_coded
,
DBG_DETAIL
);
}
}
/* -------------------------------------------------------------------------- */
inline
UInt
Base64Reader
::
Decode
(
unsigned
char
c0
,
unsigned
char
c1
,
unsigned
char
c2
,
unsigned
char
c3
,
unsigned
char
*
r0
,
unsigned
char
*
r1
,
unsigned
char
*
r2
){
unsigned
char
d0
,
d1
,
d2
,
d3
;
d0
=
dtable
[
0
+
c0
];
d1
=
dtable
[
0
+
c1
];
d2
=
dtable
[
0
+
c2
];
d3
=
dtable
[
0
+
c3
];
DUMP
(
"d0 "
<<
(
int
)
d0
<<
" d1 "
<<
(
int
)
d1
<<
" d2 "
<<
(
int
)
d2
<<
" d3 "
<<
(
int
)
d3
,
DBG_DETAIL
);
// Decode the 3 bytes
*
r0
=
(
char
)(((
d0
<<
2
)
&
0xFC
)
|
((
d1
>>
4
)
&
0x03
));
*
r1
=
(
char
)(((
d1
<<
4
)
&
0xF0
)
|
((
d2
>>
2
)
&
0x0F
));
*
r2
=
(
char
)(((
d2
<<
6
)
&
0xC0
)
|
((
d3
>>
0
)
&
0x3F
));
DUMP
(
"r0 "
<<
(
int
)
*
r0
<<
" r1 "
<<
(
int
)
*
r1
<<
" r2 "
<<
(
int
)
*
r2
,
DBG_DETAIL
);
// Return the number of bytes actually decoded
if
(
c2
==
'='
)
{
return
1
;
}
if
(
c3
==
'='
)
{
return
2
;
}
return
3
;
}
/* -------------------------------------------------------------------------- */
inline
UInt
Base64Reader
::
PopByteInBase64
(
unsigned
char
&
c
){
//initialise les blocs
LM_ASSERT
(
n
<
4
,
"this should not append check source code"
);
DUMP
(
"pop byte"
,
DBG_DETAIL
);
// si je suis a n = 3 je dois relire 4 octets de base64
if
(
n
==
0
){
DUMP
(
"n_coded "
<<
n_coded
,
DBG_DETAIL
);
if
(
n_coded
==
0
){
if
(
current_ptr
<
start_ptr
+
len
)
{
read_coded_byte
[
0
]
=
current_ptr
[
0
];
++
n_coded
;
DUMP
(
"n_coded "
<<
n_coded
,
DBG_DETAIL
);}
else
{
DUMP
(
"lala"
<<
n_coded
,
DBG_DETAIL
);
return
0
;}
if
(
current_ptr
+
1
<
start_ptr
+
len
)
{
read_coded_byte
[
1
]
=
current_ptr
[
1
];
++
n_coded
;
DUMP
(
"n_coded "
<<
n_coded
,
DBG_DETAIL
);}
else
{
DUMP
(
"lala"
<<
n_coded
,
DBG_DETAIL
);
return
0
;}
if
(
current_ptr
+
2
<
start_ptr
+
len
)
{
read_coded_byte
[
2
]
=
current_ptr
[
2
];
++
n_coded
;
DUMP
(
"n_coded "
<<
n_coded
,
DBG_DETAIL
);}
else
{
DUMP
(
"lala"
<<
n_coded
,
DBG_DETAIL
);
return
0
;}
if
(
current_ptr
+
3
<
start_ptr
+
len
)
{
read_coded_byte
[
3
]
=
current_ptr
[
3
];
++
n_coded
;
DUMP
(
"n_coded "
<<
n_coded
,
DBG_DETAIL
);}
else
{
DUMP
(
"lala"
<<
n_coded
,
DBG_DETAIL
);
return
0
;}
current_ptr
+=
4
;
}
}
if
(
n_coded
==
4
)
Decode
(
read_coded_byte
[
0
],
read_coded_byte
[
1
],
read_coded_byte
[
2
],
read_coded_byte
[
3
],
&
read_byte
[
0
],
&
read_byte
[
1
],
&
read_byte
[
2
]);
c
=
read_byte
[
n
];
DUMP
(
"readed charater "
<<
(
int
)
c
,
DBG_DETAIL
);
++
n
;
if
(
n
==
3
)
{
n
=
0
;
n_coded
=
0
;
}
return
1
;
}
/* -------------------------------------------------------------------------- */
inline
UInt
Base64Reader
::
PopIntegerInBase64
(
UInt
&
d
){
UInt
read
=
1
;
DUMP
(
"pushing "
<<
d
<<
" ( n = "
<<
n
<<
" )"
,
DBG_DETAIL
);
unsigned
char
*
c
=
(
unsigned
char
*
)
&
d
;
for
(
UInt
i
=
0
;
i
<
sizeof
(
int
)
;
++
i
){
read
&=
PopByteInBase64
(
c
[
i
]);
if
(
!
read
)
break
;
}
return
read
;
}
/* -------------------------------------------------------------------------- */
/* inline void Base64Reader::PopStrInBase64(char * str){ */
/* FATAL("unimplemented for the moment"); */
/* } */
/* -------------------------------------------------------------------------- */
inline
UInt
Base64Reader
::
PopRealInBase64
(
Real
&
d
){
UInt
read
=
1
;
DUMP
(
"pop Real , already red = "
<<
read_bytes_in_Real
,
DBG_DETAIL
);
unsigned
char
*
c
=
(
unsigned
char
*
)
&
temp_Real
;
for
(
UInt
i
=
read_bytes_in_Real
;
i
<
sizeof
(
Real
)
;
++
i
){
read
&=
PopByteInBase64
(
c
[
i
]);
if
(
read
)
++
read_bytes_in_Real
;
if
(
!
read
)
break
;
}
if
(
read
)
{
read_bytes_in_Real
=
0
;
d
=
temp_Real
;
DUMP
(
"readed Real "
<<
d
,
DBG_DETAIL
);
}
return
read
;
}
/* -------------------------------------------------------------------------- */
__END_LIBMULTISCALE__
#endif
/* __LIBMULTISCALE_BASE64_READER_HH__ */
Event Timeline
Log In to Comment