• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 Copyright (C) 2001-present by Serge Lamikhov-Center
3 
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10 
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
13 
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 THE SOFTWARE.
21 */
22 
23 #ifndef ELFIO_STRINGS_HPP
24 #define ELFIO_STRINGS_HPP
25 
26 #include <cstdlib>
27 #include <cstring>
28 #include <string>
29 
30 namespace ELFIO {
31 
32 //------------------------------------------------------------------------------
33 template <class S> class string_section_accessor_template
34 {
35   public:
36     //------------------------------------------------------------------------------
string_section_accessor_template(S * section)37     explicit string_section_accessor_template( S* section )
38         : string_section( section )
39     {
40     }
41 
42     //------------------------------------------------------------------------------
get_string(Elf_Word index) const43     const char* get_string( Elf_Word index ) const
44     {
45         if ( string_section ) {
46             const char* data = string_section->get_data();
47             if ( index < string_section->get_size() && nullptr != data ) {
48                 size_t string_length =
49                     strnlen( data + index, string_section->get_size() - index );
50                 if ( string_length < ( string_section->get_size() - index ) )
51                     return data + index;
52             }
53         }
54 
55         return nullptr;
56     }
57 
58     //------------------------------------------------------------------------------
add_string(const char * str)59     Elf_Word add_string( const char* str )
60     {
61         Elf_Word current_position = 0;
62 
63         if ( string_section ) {
64             // Strings are addeded to the end of the current section data
65             current_position =
66                 static_cast<Elf_Word>( string_section->get_size() );
67 
68             if ( current_position == 0 ) {
69                 char empty_string = '\0';
70                 string_section->append_data( &empty_string, 1 );
71                 current_position++;
72             }
73             string_section->append_data(
74                 str, static_cast<Elf_Word>( std::strlen( str ) + 1 ) );
75         }
76 
77         return current_position;
78     }
79 
80     //------------------------------------------------------------------------------
add_string(const std::string & str)81     Elf_Word add_string( const std::string& str )
82     {
83         return add_string( str.c_str() );
84     }
85 
86     //------------------------------------------------------------------------------
87   private:
88     S* string_section;
89 };
90 
91 using string_section_accessor = string_section_accessor_template<section>;
92 using const_string_section_accessor =
93     string_section_accessor_template<const section>;
94 
95 } // namespace ELFIO
96 
97 #endif // ELFIO_STRINGS_HPP
98