~ubuntu-branches/debian/sid/subversion/sid

« back to all changes in this revision

Viewing changes to subversion/bindings/javahl/native/jniwrapper/jni_string.hpp

  • Committer: Package Import Robot
  • Author(s): James McCoy
  • Date: 2015-08-07 21:32:47 UTC
  • mfrom: (0.2.15) (4.1.7 experimental)
  • Revision ID: package-import@ubuntu.com-20150807213247-ozyewtmgsr6tkewl
Tags: 1.9.0-1
* Upload to unstable
* New upstream release.
  + Security fixes
    - CVE-2015-3184: Mixed anonymous/authenticated path-based authz with
      httpd 2.4
    - CVE-2015-3187: svn_repos_trace_node_locations() reveals paths hidden
      by authz
* Add >= 2.7 requirement for python-all-dev Build-Depends, needed to run
  tests.
* Remove Build-Conflicts against ruby-test-unit.  (Closes: #791844)
* Remove patches/apache_module_dependency in favor of expressing the
  dependencies in authz_svn.load/dav_svn.load.
* Build-Depend on apache2-dev (>= 2.4.16) to ensure ap_some_authn_required()
  is available when building mod_authz_svn and Depend on apache2-bin (>=
  2.4.16) for runtime support.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * @copyright
 
3
 * ====================================================================
 
4
 *    Licensed to the Apache Software Foundation (ASF) under one
 
5
 *    or more contributor license agreements.  See the NOTICE file
 
6
 *    distributed with this work for additional information
 
7
 *    regarding copyright ownership.  The ASF licenses this file
 
8
 *    to you under the Apache License, Version 2.0 (the
 
9
 *    "License"); you may not use this file except in compliance
 
10
 *    with the License.  You may obtain a copy of the License at
 
11
 *
 
12
 *      http://www.apache.org/licenses/LICENSE-2.0
 
13
 *
 
14
 *    Unless required by applicable law or agreed to in writing,
 
15
 *    software distributed under the License is distributed on an
 
16
 *    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 
17
 *    KIND, either express or implied.  See the License for the
 
18
 *    specific language governing permissions and limitations
 
19
 *    under the License.
 
20
 * ====================================================================
 
21
 * @endcopyright
 
22
 */
 
23
 
 
24
#ifndef SVN_JAVAHL_JNIWRAPPER_STRING_HPP
 
25
#define SVN_JAVAHL_JNIWRAPPER_STRING_HPP
 
26
 
 
27
#include <cstring>
 
28
#include <string>
 
29
 
 
30
#include <apr_pools.h>
 
31
 
 
32
#include "jni_object.hpp"
 
33
 
 
34
namespace Java {
 
35
 
 
36
/**
 
37
 * Object wrapper for @c java.lang.String.
 
38
 *
 
39
 * The associated JNI class reference is stored for the lifetime of
 
40
 * the JVM in the global class cache.
 
41
 *
 
42
 * @since New in 1.9.
 
43
 */
 
44
class String
 
45
{
 
46
public:
 
47
  /**
 
48
   * Constructs a wrapper around an existing string @a str.
 
49
   */
 
50
  explicit String(Env env, jstring str)
 
51
    : m_env(env),
 
52
      m_jthis(str)
 
53
    {}
 
54
 
 
55
  /**
 
56
   * Constructs a new string and wrapper from @a text.
 
57
   */
 
58
  explicit String(Env env, const char* text)
 
59
    : m_env(env),
 
60
      m_jthis(env.NewStringUTF(text))
 
61
    {}
 
62
 
 
63
  /**
 
64
   * Constructs a new string and wrapper from @a text.
 
65
   */
 
66
  explicit String(Env env, const std::string& text)
 
67
    : m_env(env),
 
68
      m_jthis(env.NewStringUTF(text.c_str()))
 
69
    {}
 
70
 
 
71
  /**
 
72
   * Returns the wrapped JNI object reference.
 
73
   */
 
74
  jstring get() const
 
75
    {
 
76
      return m_jthis;
 
77
    }
 
78
 
 
79
  /**
 
80
   * Returns the wrapped enviromnment reference.
 
81
   */
 
82
  Env get_env() const
 
83
    {
 
84
      return m_env;
 
85
    }
 
86
 
 
87
  /**
 
88
   * Returns the number of Unicode characters in the string.
 
89
   */
 
90
  jsize length() const
 
91
    {
 
92
      return m_env.GetStringLength(get());
 
93
    }
 
94
 
 
95
  /**
 
96
   * Returns the length of the modified UTF-8 representation of the
 
97
   * string.
 
98
   */
 
99
  jsize utf8_length() const
 
100
    {
 
101
      return m_env.GetStringUTFLength(get());
 
102
    }
 
103
 
 
104
  /**
 
105
   * Copies the contents of the modified UTF-8 representation of the
 
106
   * string into @a pool.
 
107
   */
 
108
  const char* strdup(apr_pool_t* pool) const;
 
109
 
 
110
  /**
 
111
   * Accessor class for the contents of the string.
 
112
   *
 
113
   * Objects of this class should be created within the scope where
 
114
   * the raw C string is required. They will create an immutable
 
115
   * modified UTF-8 representation of the string contents. The data
 
116
   * will be released by the destructor.
 
117
   */
 
118
  class Contents
 
119
  {
 
120
  public:
 
121
    /**
 
122
     * Constructs an immutable string contents accessor.
 
123
     */
 
124
    explicit Contents(const String& str)
 
125
      : m_str(str),
 
126
        m_text(!str.get() ? NULL
 
127
               : str.m_env.GetStringUTFChars(str.get(), NULL)),
 
128
        m_length(m_text ? jsize(::std::strlen(m_text)) : 0)
 
129
      {}
 
130
 
 
131
    /**
 
132
     * Releases the string contents, possibly committing changes to the JVM.
 
133
     */
 
134
    ~Contents()
 
135
      {
 
136
        if (m_text)
 
137
          m_str.m_env.ReleaseStringUTFChars(m_str.get(), NULL);
 
138
      }
 
139
 
 
140
    /**
 
141
     * Returns the C representation of the string contents.
 
142
     */
 
143
    const char* c_str() const
 
144
      {
 
145
        return m_text;
 
146
      }
 
147
 
 
148
    /**
 
149
     * Returns the length of the C representation of the string.
 
150
     */
 
151
    jsize utf8_length() const
 
152
      {
 
153
        return m_length;
 
154
      }
 
155
 
 
156
  protected:
 
157
    const String& m_str;
 
158
    const char* m_text;
 
159
    jsize m_length;
 
160
  };
 
161
 
 
162
  /**
 
163
   * Accessor class for the contents of the string.
 
164
   *
 
165
   * Behaves like the #Contents class, but the representation is
 
166
   * considered mutable and can be assigned a new value, which will be
 
167
   * subsequently committed to the JVM.
 
168
   */
 
169
  class MutableContents : protected Contents
 
170
  {
 
171
  public:
 
172
    /**
 
173
     * Constructs a mutable string contents accessor.
 
174
     */
 
175
    explicit MutableContents(String& str)
 
176
      : Contents(str),
 
177
        m_new_text(NULL)
 
178
      {}
 
179
 
 
180
    /**
 
181
     * Releases the string contents, possibly committing changes to the JVM.
 
182
     */
 
183
    ~MutableContents()
 
184
      {
 
185
        if (m_new_text)
 
186
          {
 
187
            // Prevent double-release by the Contents destructor.
 
188
            m_text = NULL;
 
189
            m_str.m_env.ReleaseStringUTFChars(m_str.get(), m_new_text);
 
190
          }
 
191
      }
 
192
 
 
193
    /**
 
194
     * Returns the C representation of the string contents.
 
195
     */
 
196
    const char* c_str() const
 
197
      {
 
198
        if (m_new_text)
 
199
          return m_new_text;
 
200
        return Contents::c_str();
 
201
      }
 
202
 
 
203
    /**
 
204
     * Returns the length of the C representation of the string.
 
205
     */
 
206
    jsize utf8_length() const
 
207
      {
 
208
        return Contents::utf8_length();
 
209
      }
 
210
 
 
211
    /**
 
212
     * Sets a new value for the string, to be committed to the JVM
 
213
     * when the accessor object is destroyed.
 
214
     * @throw std::invalid_argument if the @a new_text is @c null
 
215
     * @throw std::logic_error if this is a @c null or immutable string
 
216
     */
 
217
    void set_value(const char* new_text);
 
218
 
 
219
  private:
 
220
    const char* m_new_text;
 
221
  };
 
222
 
 
223
private:
 
224
  const Env m_env;
 
225
  const jstring m_jthis;
 
226
 
 
227
  /**
 
228
   * This object's implementation details.
 
229
   */
 
230
  class ClassImpl : public Object::ClassImpl
 
231
  {
 
232
    friend class ClassCacheImpl;
 
233
 
 
234
  protected:
 
235
    explicit ClassImpl(Env env, jclass cls)
 
236
      : Object::ClassImpl(env, cls)
 
237
      {}
 
238
 
 
239
  public:
 
240
    virtual ~ClassImpl();
 
241
  };
 
242
 
 
243
  friend class Contents;
 
244
  friend class MutableContents;
 
245
  friend class ClassCacheImpl;
 
246
  static const char* const m_class_name;
 
247
};
 
248
 
 
249
} // namespace Java
 
250
 
 
251
#endif // SVN_JAVAHL_JNIWRAPPER_STRING_HPP