• 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     string_section_accessor_template( S* section ) : string_section( section )
38     {
39     }
40 
41     //------------------------------------------------------------------------------
get_string(Elf_Word index) const42     const char* get_string( Elf_Word index ) const
43     {
44         if ( string_section ) {
45             if ( index < string_section->get_size() ) {
46                 const char* data = string_section->get_data();
47                 if ( 0 != data ) {
48                     return data + index;
49                 }
50             }
51         }
52 
53         return 0;
54     }
55 
56     //------------------------------------------------------------------------------
add_string(const char * str)57     Elf_Word add_string( const char* str )
58     {
59         Elf_Word current_position = 0;
60 
61         if ( string_section ) {
62             // Strings are addeded to the end of the current section data
63             current_position = (Elf_Word)string_section->get_size();
64 
65             if ( current_position == 0 ) {
66                 char empty_string = '\0';
67                 string_section->append_data( &empty_string, 1 );
68                 current_position++;
69             }
70             string_section->append_data( str,
71                                          (Elf_Word)std::strlen( str ) + 1 );
72         }
73 
74         return current_position;
75     }
76 
77     //------------------------------------------------------------------------------
add_string(const std::string & str)78     Elf_Word add_string( const std::string& str )
79     {
80         return add_string( str.c_str() );
81     }
82 
83     //------------------------------------------------------------------------------
84   private:
85     S* string_section;
86 };
87 
88 using string_section_accessor = string_section_accessor_template<section>;
89 using const_string_section_accessor =
90     string_section_accessor_template<const section>;
91 
92 } // namespace ELFIO
93 
94 #endif // ELFIO_STRINGS_HPP
95