xref: /aosp_15_r20/external/cronet/third_party/netty-tcnative/src/c/tcn.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 /*
2  * Copyright 2016 The Netty Project
3  *
4  * The Netty Project licenses this file to you under the Apache License,
5  * version 2.0 (the "License"); you may not use this file except in compliance
6  * with the License. You may obtain a copy of the License at:
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations
14  * under the License.
15  */
16 /* Licensed to the Apache Software Foundation (ASF) under one or more
17  * contributor license agreements.  See the NOTICE file distributed with
18  * this work for additional information regarding copyright ownership.
19  * The ASF licenses this file to You under the Apache License, Version 2.0
20  * (the "License"); you may not use this file except in compliance with
21  * the License.  You may obtain a copy of the License at
22  *
23  *     http://www.apache.org/licenses/LICENSE-2.0
24  *
25  * Unless required by applicable law or agreed to in writing, software
26  * distributed under the License is distributed on an "AS IS" BASIS,
27  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
28  * See the License for the specific language governing permissions and
29  * limitations under the License.
30  */
31 
32 #ifndef TCN_H
33 #define TCN_H
34 
35 // Start includes
36 #include <jni.h>
37 
38 #include "apr.h"
39 #include "apr_pools.h"
40 
41 #ifndef APR_HAS_THREADS
42 #error "Missing APR_HAS_THREADS support from APR."
43 #endif
44 
45 #include <stdio.h>
46 #include <stdlib.h>
47 #if defined(_WIN32) && !defined(__CYGWIN__)
48 #include <process.h>
49 #else
50 #include <unistd.h>
51 #endif
52 
53 #if defined(_DEBUG) || defined(DEBUG)
54 #include <assert.h>
55 #define TCN_ASSERT(x)  assert((x))
56 #else
57 #define TCN_ASSERT(x) (void)0
58 #endif
59 // End includes
60 
61 #define UNREFERENCED(P)      (P) = (P)
62 #define UNREFERENCED_STDARGS e = e; o = o
63 #ifdef WIN32
64 #define LLT(X) (X)
65 #else
66 #define LLT(X) ((long)(X))
67 #endif
68 #define P2J(P)          ((jlong)LLT(P))
69 #define J2P(P, T)       ((T)LLT((jlong)P))
70 /* On stack buffer size */
71 #define TCN_BUFFER_SZ   8192
72 #define TCN_STDARGS     JNIEnv *e, jobject o
73 
74 #define TCN_IMPLEMENT_CALL(RT, CL, FN)  \
75     JNIEXPORT RT JNICALL Java_io_netty_internal_tcnative_##CL##_##FN
76 
77 /* Private helper functions */
78 void            tcn_Throw(JNIEnv *, const char *, ...);
79 void            tcn_ThrowException(JNIEnv *, const char *);
80 void            tcn_ThrowAPRException(JNIEnv *, apr_status_t);
81 jstring         tcn_new_string(JNIEnv *, const char *);
82 jstring         tcn_new_stringn(JNIEnv *, const char *, size_t);
83 
84 #define J2S(V)  c##V
85 #define J2L(V)  p##V
86 
87 #define TCN_BEGIN_MACRO     if (1) {
88 #define TCN_END_MACRO       } else (void)(0)
89 
90 #define TCN_ALLOC_CSTRING(V)     \
91     const char *c##V = V ? (const char *)((*e)->GetStringUTFChars(e, V, 0)) : NULL
92 
93 #define TCN_FREE_CSTRING(V)      \
94     if (c##V) (*e)->ReleaseStringUTFChars(e, V, c##V)
95 
96 #define AJP_TO_JSTRING(V)   (*e)->NewStringUTF((e), (V))
97 
98 #define TCN_FREE_JSTRING(V)      \
99     TCN_BEGIN_MACRO              \
100         if (c##V)                \
101             free(c##V);          \
102     TCN_END_MACRO
103 
104 #define TCN_THROW_IF_ERR(x, r)                  \
105     TCN_BEGIN_MACRO                             \
106         apr_status_t R = (x);                   \
107         if (R != APR_SUCCESS) {                 \
108             tcn_ThrowAPRException(e, R);        \
109             (r) = 0;                            \
110             goto cleanup;                       \
111         }                                       \
112     TCN_END_MACRO
113 
114 #define TCN_LOAD_CLASS(E, C, N, R)                  \
115     TCN_BEGIN_MACRO                                 \
116         jclass _##C = (*(E))->FindClass((E), N);    \
117         if (_##C == NULL) {                         \
118             (*(E))->ExceptionClear((E));            \
119             return R;                               \
120         }                                           \
121         C = (*(E))->NewGlobalRef((E), _##C);        \
122         (*(E))->DeleteLocalRef((E), _##C);          \
123     TCN_END_MACRO
124 
125 #define TCN_UNLOAD_CLASS(E, C)                      \
126         (*(E))->DeleteGlobalRef((E), (C))
127 
128 #define TCN_GET_METHOD(E, C, M, N, S, R)            \
129     TCN_BEGIN_MACRO                                 \
130         M = (*(E))->GetMethodID((E), C, N, S);      \
131         if (M == NULL) {                            \
132             return R;                               \
133         }                                           \
134     TCN_END_MACRO
135 
136 #define TCN_GET_FIELD(E, C, F, N, S, R)            \
137     TCN_BEGIN_MACRO                                 \
138         F = (*(E))->GetFieldID((E), C, N, S);      \
139         if (F == NULL) {                            \
140             return R;                               \
141         }                                           \
142     TCN_END_MACRO
143 
144 #define TCN_MIN(a, b) ((a) < (b) ? (a) : (b))
145 
146 /* Return global String class
147  */
148 jclass tcn_get_string_class(void);
149 
150 jclass tcn_get_byte_array_class();
151 jfieldID tcn_get_key_material_certificate_chain_field();
152 jfieldID tcn_get_key_material_private_key_field();
153 
154 /* Get current thread JNIEnv
155  */
156 jint tcn_get_java_env(JNIEnv **);
157 
158 #endif /* TCN_H */
159