xref: /aosp_15_r20/external/aws-crt-java/src/native/uri.c (revision 3c7ae9de214676c52d19f01067dc1a404272dc11)
1 /**
2  * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  * SPDX-License-Identifier: Apache-2.0.
4  */
5 #include "crt.h"
6 #include "java_class_ids.h"
7 #include <aws/io/uri.h>
8 #include <jni.h>
9 
s_encoding_common(JNIEnv * env,jbyteArray buffer,jbyteArray cursor,int (* encoding_fn)(struct aws_byte_buf *,const struct aws_byte_cursor *))10 static jbyteArray s_encoding_common(
11     JNIEnv *env,
12     jbyteArray buffer,
13     jbyteArray cursor,
14     int (*encoding_fn)(struct aws_byte_buf *, const struct aws_byte_cursor *)) {
15 
16     struct aws_byte_cursor c_intermediate_cursor = aws_jni_byte_cursor_from_jbyteArray_acquire(env, buffer);
17     struct aws_byte_cursor c_byte_cursor = aws_jni_byte_cursor_from_jbyteArray_acquire(env, cursor);
18     jbyteArray uri_encoding = NULL;
19     struct aws_byte_buf c_byte_buf;
20     aws_byte_buf_init_copy_from_cursor(&c_byte_buf, aws_jni_get_allocator(), c_intermediate_cursor);
21     if (encoding_fn(&c_byte_buf, &c_byte_cursor)) {
22         aws_jni_throw_runtime_exception(env, "uri.encodingCommon: failed to encode buffer");
23         goto clean_up;
24     }
25     struct aws_byte_cursor uri_encoding_cursor = aws_byte_cursor_from_buf(&c_byte_buf);
26     uri_encoding = aws_jni_byte_array_from_cursor(env, &uri_encoding_cursor);
27 clean_up:
28     aws_jni_byte_cursor_from_jbyteArray_release(env, buffer, c_intermediate_cursor);
29     aws_jni_byte_cursor_from_jbyteArray_release(env, cursor, c_byte_cursor);
30     aws_byte_buf_clean_up(&c_byte_buf);
31     return uri_encoding;
32 }
33 
Java_software_amazon_awssdk_crt_io_Uri_appendEncodingUriPath(JNIEnv * env,jclass jni_class,jbyteArray encoded,jbyteArray path)34 JNIEXPORT jbyteArray JNICALL Java_software_amazon_awssdk_crt_io_Uri_appendEncodingUriPath(
35     JNIEnv *env,
36     jclass jni_class,
37     jbyteArray encoded,
38     jbyteArray path) {
39     (void)jni_class;
40     aws_cache_jni_ids(env);
41 
42     return s_encoding_common(env, encoded, path, aws_byte_buf_append_encoding_uri_path);
43 }
44 
Java_software_amazon_awssdk_crt_io_Uri_appendEncodingUriParam(JNIEnv * env,jclass jni_class,jbyteArray encoded,jbyteArray param)45 JNIEXPORT jbyteArray JNICALL Java_software_amazon_awssdk_crt_io_Uri_appendEncodingUriParam(
46     JNIEnv *env,
47     jclass jni_class,
48     jbyteArray encoded,
49     jbyteArray param) {
50     (void)jni_class;
51     aws_cache_jni_ids(env);
52 
53     return s_encoding_common(env, encoded, param, aws_byte_buf_append_encoding_uri_param);
54 }
55 
Java_software_amazon_awssdk_crt_io_Uri_appendDecodingUri(JNIEnv * env,jclass jni_class,jbyteArray base,jbyteArray encoded)56 JNIEXPORT jbyteArray JNICALL Java_software_amazon_awssdk_crt_io_Uri_appendDecodingUri(
57     JNIEnv *env,
58     jclass jni_class,
59     jbyteArray base,
60     jbyteArray encoded) {
61     (void)jni_class;
62     aws_cache_jni_ids(env);
63 
64     return s_encoding_common(env, base, encoded, aws_byte_buf_append_decoding_uri);
65 }
66