| 1 |
/* |
| 2 |
* Copyright (C) 2020 Linux Studio Plugins Project <https://lsp-plug.in/> |
| 3 |
* (C) 2020 Vladimir Sadovnikov <[email protected]> |
| 4 |
* |
| 5 |
* This file is part of lsp-runtime-lib |
| 6 |
* Created on: 29 авг. 2019 г. |
| 7 |
* |
| 8 |
* lsp-runtime-lib is free software: you can redistribute it and/or modify |
| 9 |
* it under the terms of the GNU Lesser General Public License as published by |
| 10 |
* the Free Software Foundation, either version 3 of the License, or |
| 11 |
* any later version. |
| 12 |
* |
| 13 |
* lsp-runtime-lib is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
* GNU Lesser General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU Lesser General Public License |
| 19 |
* along with lsp-runtime-lib. If not, see <https://www.gnu.org/licenses/>. |
| 20 |
*/ |
| 21 |
|
| 22 |
#include <lsp-plug.in/fmt/java/defs.h> |
| 23 |
#include <lsp-plug.in/fmt/java/Object.h> |
| 24 |
#include <lsp-plug.in/fmt/java/String.h> |
| 25 |
#include <lsp-plug.in/fmt/java/RawArray.h> |
| 26 |
#include <lsp-plug.in/fmt/java/Enum.h> |
| 27 |
#include <lsp-plug.in/fmt/java/wrappers.h> |
| 28 |
#include <lsp-plug.in/fmt/java/ObjectStreamClass.h> |
| 29 |
|
| 30 |
namespace lsp |
| 31 |
{ |
| 32 |
namespace java |
| 33 |
{ |
| 34 |
const char *Object::CLASS_NAME = "java.lang.Object"; |
| 35 |
|
| 36 |
Object::Object(const char *class_name) |
| 37 |
{ |
| 38 |
pClass = class_name; |
| 39 |
vSlots = NULL; |
| 40 |
nSlots = 0; |
| 41 |
vData = NULL; |
| 42 |
} |
| 43 |
|
| 44 |
Object::~Object() |
| 45 |
{ |
| 46 |
if (vSlots != NULL) |
| 47 |
::free(vSlots); |
| 48 |
if (vData != NULL) |
| 49 |
::free(vData); |
| 50 |
|
| 51 |
pClass = NULL; |
| 52 |
} |
| 53 |
|
| 54 |
bool Object::instanceof(const char *name) const |
| 55 |
{ |
| 56 |
if (name == pClass) |
| 57 |
return true; |
| 58 |
return ::strcmp(name, pClass) == 0; |
| 59 |
} |
| 60 |
|
| 61 |
template <class type_t, class cast_t> |
| 62 |
inline status_t Object::read_reference(const char *field, type_t *item, ftype_t type) const |
| 63 |
{ |
| 64 |
bool found = false; |
| 65 |
|
| 66 |
// Lookup slot |
| 67 |
for (ssize_t i=nSlots-1; i>=0; --i) |
| 68 |
{ |
| 69 |
const object_slot_t *s = &vSlots[i]; |
| 70 |
const ObjectStreamClass *os = s->desc; |
| 71 |
|
| 72 |
// Lookup field |
| 73 |
uint8_t *data = &vData[s->offset]; |
| 74 |
for (size_t j=0, m=os->fields(); j<m; ++j) |
| 75 |
{ |
| 76 |
// Align base of the current field |
| 77 |
const ObjectStreamField *f = os->field(j); |
| 78 |
|
| 79 |
// Check field match |
| 80 |
if (::strcmp(f->raw_name(), field) == 0) |
| 81 |
{ |
| 82 |
found = true; |
| 83 |
if (f->is_reference()) |
| 84 |
{ |
| 85 |
// Null value? |
| 86 |
const Object *obj = *reinterpret_cast<const Object **>(&data[f->offset()]); |
| 87 |
if (obj == NULL) |
| 88 |
return STATUS_NULL; |
| 89 |
|
| 90 |
// Can be cast? |
| 91 |
const cast_t *w = obj->cast<cast_t>(); |
| 92 |
if (w != NULL) |
| 93 |
{ |
| 94 |
// Return the value |
| 95 |
if (item != NULL) |
| 96 |
*item = w; |
| 97 |
return STATUS_OK; |
| 98 |
} |
| 99 |
} // is_reference |
| 100 |
} // strcmp |
| 101 |
} // for |
| 102 |
} |
| 103 |
|
| 104 |
return (found) ? STATUS_BAD_TYPE : STATUS_NOT_FOUND; |
| 105 |
} |
| 106 |
|
| 107 |
template <class type_t, class wrapper_t> |
| 108 |
inline status_t Object::read_prim_item(const char *field, type_t *item, ftype_t type) const |
| 109 |
{ |
| 110 |
bool found = false; |
| 111 |
|
| 112 |
// Lookup slot |
| 113 |
for (ssize_t i=nSlots-1; i>=0; --i) |
| 114 |
{ |
| 115 |
const object_slot_t *s = &vSlots[i]; |
| 116 |
const ObjectStreamClass *os = s->desc; |
| 117 |
|
| 118 |
// Lookup field |
| 119 |
uint8_t *data = &vData[s->offset]; |
| 120 |
for (size_t j=0, m=os->fields(); j<m; ++j) |
| 121 |
{ |
| 122 |
// Align base of the current field |
| 123 |
const ObjectStreamField *f = os->field(j); |
| 124 |
|
| 125 |
// Check field match |
| 126 |
if (::strcmp(f->raw_name(), field) == 0) |
| 127 |
{ |
| 128 |
found = true; |
| 129 |
|
| 130 |
// Type match? |
| 131 |
if (f->type() == type) |
| 132 |
{ |
| 133 |
if (item != NULL) |
| 134 |
*item = *reinterpret_cast<const type_t *>(&data[f->offset()]); |
| 135 |
return STATUS_OK; |
| 136 |
} |
| 137 |
else if (f->is_reference()) // Reference type? |
| 138 |
{ |
| 139 |
// Need to check for wrappers (e.g. java.lang.Long -> long) |
| 140 |
// Null value? |
| 141 |
const Object *obj = *reinterpret_cast<const Object **>(&data[f->offset()]); |
| 142 |
if (obj == NULL) |
| 143 |
return STATUS_NULL; |
| 144 |
|
| 145 |
// Can be cast to wrapper? |
| 146 |
const wrapper_t *w = obj->cast<const wrapper_t>(); |
| 147 |
if (w != NULL) |
| 148 |
return w->get_value(item); |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
return (found) ? STATUS_BAD_TYPE : STATUS_NOT_FOUND; |
| 155 |
} |
| 156 |
|
| 157 |
status_t Object::get_byte(const char *field, byte_t *dst) const |
| 158 |
{ |
| 159 |
return read_prim_item<byte_t, Byte>(field, dst, JFT_BYTE); |
| 160 |
} |
| 161 |
|
| 162 |
status_t Object::get_short(const char *field, short_t *dst) const |
| 163 |
{ |
| 164 |
return read_prim_item<short_t, Short>(field, dst, JFT_SHORT); |
| 165 |
} |
| 166 |
|
| 167 |
status_t Object::get_int(const char *field, int_t *dst) const |
| 168 |
{ |
| 169 |
return read_prim_item<int_t, Integer>(field, dst, JFT_INTEGER); |
| 170 |
} |
| 171 |
|
| 172 |
status_t Object::get_long(const char *field, long_t *dst) const |
| 173 |
{ |
| 174 |
return read_prim_item<long_t, Long>(field, dst, JFT_LONG); |
| 175 |
} |
| 176 |
|
| 177 |
status_t Object::get_float(const char *field, float_t *dst) const |
| 178 |
{ |
| 179 |
return read_prim_item<float_t, Float>(field, dst, JFT_FLOAT); |
| 180 |
} |
| 181 |
|
| 182 |
status_t Object::get_double(const char *field, double_t *dst) const |
| 183 |
{ |
| 184 |
return read_prim_item<double_t, Double>(field, dst, JFT_DOUBLE); |
| 185 |
} |
| 186 |
|
| 187 |
status_t Object::get_char(const char *field, char_t *dst) const |
| 188 |
{ |
| 189 |
return read_prim_item<char_t, Character>(field, dst, JFT_CHAR); |
| 190 |
} |
| 191 |
|
| 192 |
status_t Object::get_bool(const char *field, bool_t *dst) const |
| 193 |
{ |
| 194 |
return read_prim_item<bool_t, Boolean>(field, dst, JFT_BOOL); |
| 195 |
} |
| 196 |
|
| 197 |
status_t Object::get_object(const char *field, const Object **dst) const |
| 198 |
{ |
| 199 |
return read_reference<const Object *, Object>(field, dst, JFT_OBJECT); |
| 200 |
} |
| 201 |
|
| 202 |
status_t Object::get_array(const char *field, const RawArray **dst) const |
| 203 |
{ |
| 204 |
return read_reference<const RawArray *, RawArray>(field, dst, JFT_ARRAY); |
| 205 |
} |
| 206 |
|
| 207 |
status_t Object::get_enum(const char *field, const Enum **dst) const |
| 208 |
{ |
| 209 |
const Enum *en = NULL; |
| 210 |
status_t res = read_reference<const Enum *, Enum>(field, &en, JFT_OBJECT); |
| 211 |
if (res != STATUS_OK) |
| 212 |
return res; |
| 213 |
if (dst != NULL) |
| 214 |
*dst = en; |
| 215 |
return STATUS_OK; |
| 216 |
} |
| 217 |
|
| 218 |
status_t Object::get_enum(const char *field, LSPString *dst) const |
| 219 |
{ |
| 220 |
const Enum *en = NULL; |
| 221 |
status_t res = get_enum(field, &en); |
| 222 |
if (res != STATUS_OK) |
| 223 |
return res; |
| 224 |
else if (en == NULL) |
| 225 |
return STATUS_NULL; |
| 226 |
if (dst == NULL) |
| 227 |
return STATUS_OK; |
| 228 |
|
| 229 |
return (dst->set(en->name())) ? STATUS_OK : STATUS_NO_MEM; |
| 230 |
} |
| 231 |
|
| 232 |
status_t Object::get_enum(const char *field, const char **dst) const |
| 233 |
{ |
| 234 |
const Enum *en = NULL; |
| 235 |
status_t res = get_enum(field, &en); |
| 236 |
if (res != STATUS_OK) |
| 237 |
return res; |
| 238 |
else if (en == NULL) |
| 239 |
return STATUS_NULL; |
| 240 |
if (dst != NULL) |
| 241 |
*dst = en->name()->get_utf8(); |
| 242 |
|
| 243 |
return STATUS_OK; |
| 244 |
} |
| 245 |
|
| 246 |
status_t Object::get_string(const char *field, const String **dst) const |
| 247 |
{ |
| 248 |
const String *str = NULL; |
| 249 |
status_t res = read_reference<const String *, String>(field, &str, JFT_OBJECT); |
| 250 |
if (res != STATUS_OK) |
| 251 |
return res; |
| 252 |
if (dst != NULL) |
| 253 |
*dst = str; |
| 254 |
return STATUS_OK; |
| 255 |
} |
| 256 |
|
| 257 |
status_t Object::get_string(const char *field, LSPString *dst) const |
| 258 |
{ |
| 259 |
const String *str = NULL; |
| 260 |
status_t res = get_string(field, &str); |
| 261 |
if (res != STATUS_OK) |
| 262 |
return res; |
| 263 |
else if (str == NULL) |
| 264 |
return STATUS_NULL; |
| 265 |
if (dst == NULL) |
| 266 |
return STATUS_OK; |
| 267 |
|
| 268 |
return (dst->set(str->string())) ? STATUS_OK : STATUS_NO_MEM; |
| 269 |
} |
| 270 |
|
| 271 |
status_t Object::get_string(const char *field, const char **dst) const |
| 272 |
{ |
| 273 |
const String *str = NULL; |
| 274 |
status_t res = get_string(field, &str); |
| 275 |
if (res != STATUS_OK) |
| 276 |
return res; |
| 277 |
else if (str == NULL) |
| 278 |
return STATUS_NULL; |
| 279 |
if (dst != NULL) |
| 280 |
*dst = str->string()->get_utf8(); |
| 281 |
|
| 282 |
return STATUS_OK; |
| 283 |
} |
| 284 |
|
| 285 |
status_t Object::to_string_padded(LSPString *dst, size_t pad) |
| 286 |
{ |
| 287 |
// lsp_trace("*%p = new ", this); |
| 288 |
if (!dst->fmt_append_ascii("*%p = new ", this)) |
| 289 |
return STATUS_NO_MEM; |
| 290 |
if (!dst->append_utf8(pClass)) |
| 291 |
return STATUS_NO_MEM; |
| 292 |
if (!dst->append_ascii(" {\n")) |
| 293 |
return STATUS_NO_MEM; |
| 294 |
|
| 295 |
++pad; |
| 296 |
for (size_t i=0; i<nSlots; ++i) |
| 297 |
{ |
| 298 |
prim_ptr_t ptr; |
| 299 |
|
| 300 |
object_slot_t *s = &vSlots[i]; |
| 301 |
ObjectStreamClass *os = s->desc; |
| 302 |
uint8_t *data = &vData[s->offset]; |
| 303 |
|
| 304 |
// lsp_trace("i=%d, nslots=%d, s=%p, os=%p", int(i), int(nSlots), s, os); |
| 305 |
|
| 306 |
if (!pad_string(dst, pad)) |
| 307 |
return STATUS_NO_MEM; |
| 308 |
// lsp_trace("%s:\n", os->raw_name()); |
| 309 |
if (!dst->fmt_append_utf8("%s:\n", os->raw_name())) |
| 310 |
return STATUS_NO_MEM; |
| 311 |
|
| 312 |
++pad; |
| 313 |
|
| 314 |
// Dump fields |
| 315 |
for (size_t j=0, n=os->fields(); j<n; ++j) |
| 316 |
{ |
| 317 |
const ObjectStreamField *f = os->field(j); |
| 318 |
ptr.p_ubyte = &data[f->offset()]; |
| 319 |
|
| 320 |
if (!pad_string(dst, pad)) |
| 321 |
return STATUS_NO_MEM; |
| 322 |
// lsp_trace("ptr = %p (type=%d)", ptr.p_ubyte, int(f->type())); |
| 323 |
if (!dst->fmt_append_utf8("%s = ", f->name()->get_utf8())) |
| 324 |
return STATUS_NO_MEM; |
| 325 |
|
| 326 |
bool res = true; |
| 327 |
switch (f->type()) |
| 328 |
{ |
| 329 |
case JFT_BYTE: res = dst->fmt_append_utf8("(byte) %d\n", *ptr.p_byte); break; |
| 330 |
case JFT_DOUBLE: res = dst->fmt_append_utf8("(double) %f\n", *ptr.p_double); break; |
| 331 |
case JFT_FLOAT: res = dst->fmt_append_utf8("(float) %f\n", *ptr.p_float); break; |
| 332 |
case JFT_INTEGER: res = dst->fmt_append_utf8("(int) %d\n", int(*ptr.p_int)); break; |
| 333 |
case JFT_LONG: res = dst->fmt_append_utf8("(long) %lld\n", (long long)(*ptr.p_long)); break; |
| 334 |
case JFT_SHORT: res = dst->fmt_append_utf8("(short) %d\n", int(*ptr.p_short)); break; |
| 335 |
case JFT_BOOL: res = dst->fmt_append_utf8("(bool) %s\n", (*ptr.p_bool) ? "true" : "false"); break; |
| 336 |
case JFT_CHAR: |
| 337 |
res = dst->append_ascii("'"); |
| 338 |
if (res) |
| 339 |
res = dst->append(lsp_wchar_t(*ptr.p_char)); |
| 340 |
if (res) |
| 341 |
res = dst->append_ascii("'\n"); |
| 342 |
break; |
| 343 |
case JFT_ARRAY: |
| 344 |
case JFT_OBJECT: |
| 345 |
{ |
| 346 |
Object *obj = *ptr.p_object; |
| 347 |
if (obj != NULL) |
| 348 |
res = obj->to_string_padded(dst, pad) == STATUS_OK; |
| 349 |
else |
| 350 |
res = dst->append_ascii("null\n"); |
| 351 |
break; |
| 352 |
} |
| 353 |
default: |
| 354 |
return STATUS_CORRUPTED; |
| 355 |
} |
| 356 |
|
| 357 |
if (!res) |
| 358 |
return STATUS_NO_MEM; |
| 359 |
} |
| 360 |
// Dump data |
| 361 |
if (os->has_write_method()) |
| 362 |
{ |
| 363 |
size_t rows = (s->size + 0xf) >> 4; |
| 364 |
uint8_t *curr = &vData[s->offset]; |
| 365 |
uint8_t *end = &vData[s->offset + s->size]; |
| 366 |
|
| 367 |
bool res = true; |
| 368 |
for (size_t j=0; j<rows; ++j) |
| 369 |
{ |
| 370 |
if (!dst->fmt_append_ascii("%08x: ", int(j << 4))) |
| 371 |
return STATUS_NO_MEM; |
| 372 |
|
| 373 |
// Hex codes |
| 374 |
for (size_t k=0; k<0x10; ++k) |
| 375 |
{ |
| 376 |
if (&curr[k] < end) |
| 377 |
res = dst->fmt_append_ascii("%02x ", int(curr[k])); |
| 378 |
else |
| 379 |
res = dst->append_ascii(" "); |
| 380 |
if (res != STATUS_OK) |
| 381 |
return STATUS_NO_MEM; |
| 382 |
} |
| 383 |
|
| 384 |
// Characters |
| 385 |
for (size_t k=0; k<0x10; ++k) |
| 386 |
{ |
| 387 |
if (&curr[k] < end) |
| 388 |
{ |
| 389 |
uint8_t c = curr[k]; |
| 390 |
if ((c < 0x20) || (c >= 0x80)) |
| 391 |
c = '.'; |
| 392 |
res = dst->append(c); |
| 393 |
} |
| 394 |
else |
| 395 |
res = dst->append(' '); |
| 396 |
if (res != STATUS_OK) |
| 397 |
return STATUS_NO_MEM; |
| 398 |
} |
| 399 |
if (!dst->append('\n')) |
| 400 |
return STATUS_NO_MEM; |
| 401 |
} |
| 402 |
} |
| 403 |
--pad; |
| 404 |
} |
| 405 |
--pad; |
| 406 |
|
| 407 |
if (!pad_string(dst, pad)) |
| 408 |
return STATUS_NO_MEM; |
| 409 |
if (!dst->append_ascii("}\n")) |
| 410 |
return STATUS_NO_MEM; |
| 411 |
|
| 412 |
return STATUS_OK; |
| 413 |
} |
| 414 |
|
| 415 |
bool Object::pad_string(LSPString *dst, size_t pad) |
| 416 |
{ |
| 417 |
pad *= 2; |
| 418 |
while (pad--) |
| 419 |
if (!dst->append(' ')) |
| 420 |
return false; |
| 421 |
return true; |
| 422 |
} |
| 423 |
|
| 424 |
status_t Object::to_string(LSPString *dst) const |
| 425 |
{ |
| 426 |
Object *_this = const_cast<Object *>(this); |
| 427 |
return _this->to_string_padded(dst, 0); |
| 428 |
} |
| 429 |
|
| 430 |
} /* namespace java */ |
| 431 |
} /* namespace lsp */ |