~mcf/mupdf

ref: e27ceb2b0e64b9a56ba79d844ea96553d87dc113 mupdf/platform/java/jni/fitzinputstream.c -rw-r--r-- 4.2 KiB
e27ceb2b — Robin Watts OSS-Fuzz 29728: Avoid buffer overflow. 1 year, 8 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/* FitzInputStream interface */

JNIEXPORT jboolean JNICALL
FUN(FitzInputStream_markSupported)(JNIEnv *env, jobject self)
{
	fz_context *ctx = get_context(env);
	fz_stream *stm = from_FitzInputStream_safe(env, self);
	jboolean closed = JNI_TRUE;

	if (!ctx || !stm) return JNI_FALSE;

	closed = (*env)->GetBooleanField(env, self, fid_FitzInputStream_closed);
	if (closed) jni_throw_uoe(env, "stream closed");

	return stm->seek != NULL;
}

JNIEXPORT void JNICALL
FUN(FitzInputStream_mark)(JNIEnv *env, jobject self, jint readlimit)
{
	fz_context *ctx = get_context(env);
	fz_stream *stm = from_FitzInputStream_safe(env, self);
	jlong markpos = 0;
	jboolean closed = JNI_TRUE;

	if (!ctx || !stm) return;
	if (stm->seek == NULL) jni_throw_uoe_void(env, "mark not supported");

	closed = (*env)->GetBooleanField(env, self, fid_FitzInputStream_closed);
	if (closed) jni_throw_uoe_void(env, "stream closed");

	fz_try(ctx)
		markpos = fz_tell(ctx, stm);
	fz_catch(ctx)
		jni_rethrow_void(env, ctx);

	(*env)->SetLongField(env, self, fid_FitzInputStream_markpos, markpos);
}

JNIEXPORT void JNICALL
FUN(FitzInputStream_reset)(JNIEnv *env, jobject self)
{
	fz_context *ctx = get_context(env);
	fz_stream *stm = from_FitzInputStream_safe(env, self);
	jboolean closed = JNI_TRUE;
	jlong markpos = -1;

	if (!ctx || !stm) return;

	if (stm->seek == NULL) jni_throw_uoe_void(env, "reset not supported");
	closed = (*env)->GetBooleanField(env, self, fid_FitzInputStream_closed);
	if (closed) jni_throw_uoe_void(env, "stream closed");

	markpos = (*env)->GetLongField(env, self, fid_FitzInputStream_markpos);
	if (markpos < 0) jni_throw_io_void(env, "mark not set");

	fz_try(ctx)
		fz_seek(ctx, stm, markpos, SEEK_SET);
	fz_catch(ctx)
		jni_rethrow_void(env, ctx);
}

JNIEXPORT jint JNICALL
FUN(FitzInputStream_available)(JNIEnv *env, jobject self)
{
	fz_context *ctx = get_context(env);
	fz_stream *stm = from_FitzInputStream_safe(env, self);
	jint available = 0;
	jboolean closed = JNI_TRUE;

	if (!ctx || !stm) return -1;

	closed = (*env)->GetBooleanField(env, self, fid_FitzInputStream_closed);
	if (closed) jni_throw_uoe(env, "stream closed");

	fz_try(ctx)
		available = fz_available(ctx, stm, 1);
	fz_catch(ctx)
		jni_rethrow(env, ctx);

	return available;
}

JNIEXPORT jint JNICALL
FUN(FitzInputStream_readByte)(JNIEnv *env, jobject self)
{
	fz_context *ctx = get_context(env);
	fz_stream *stm = from_FitzInputStream_safe(env, self);
	jboolean closed = JNI_TRUE;
	jbyte b = 0;

	if (!ctx || !stm) return -1;

	closed = (*env)->GetBooleanField(env, self, fid_FitzInputStream_closed);
	if (closed) jni_throw_uoe(env, "stream closed");

	fz_try(ctx)
		b = fz_read_byte(ctx, stm);
	fz_catch(ctx)
		jni_rethrow(env, ctx);

	return b;
}

JNIEXPORT jint JNICALL
FUN(FitzInputStream_readArray)(JNIEnv *env, jobject self, jobject jarr, jint off, jint len)
{
	fz_context *ctx = get_context(env);
	fz_stream *stm = from_FitzInputStream_safe(env, self);
	jboolean closed = JNI_TRUE;
	jbyte *arr = NULL;
	jint n = 0;

	if (!ctx || !stm) return -1;
	if (!jarr) jni_throw_arg(env, "buffer must not be null");

	closed = (*env)->GetBooleanField(env, self, fid_FitzInputStream_closed);
	if (closed) jni_throw_uoe(env, "stream closed");

	arr = (*env)->GetByteArrayElements(env, jarr, NULL);
	if (!arr) jni_throw_arg(env, "cannot get buffer to read into");

	fz_try(ctx)
		n = fz_read(ctx, stm, (unsigned char *) arr + off, len);
	fz_always(ctx)
		(*env)->ReleaseByteArrayElements(env, jarr, arr, 0);
	fz_catch(ctx)
		jni_rethrow(env, ctx);

	return n;
}

JNIEXPORT void JNICALL
FUN(FitzInputStream_finalize)(JNIEnv *env, jobject self)
{
	fz_context *ctx = get_context(env);
	fz_stream *stm = from_FitzInputStream_safe(env, self);
	if (!ctx || !stm) return;
	(*env)->SetLongField(env, self, fid_FitzInputStream_pointer, 0);
	fz_drop_stream(ctx, stm);
}

JNIEXPORT void JNICALL
FUN(FitzInputStream_close)(JNIEnv *env, jobject self)
{
	fz_context *ctx = get_context(env);
	fz_stream *stm = from_FitzInputStream_safe(env, self);
	jboolean closed = JNI_TRUE;

	if (!ctx || !stm) return;

	closed = (*env)->GetBooleanField(env, self, fid_FitzInputStream_closed);
	if (closed) jni_throw_uoe_void(env, "stream closed");

	(*env)->SetBooleanField(env, self, fid_FitzInputStream_closed, JNI_TRUE);
}