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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734
|
/* libjclass - Library for reading java class files
* Copyright (C) 2003 Nicos Panayides
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* $Id: jstring.c,v 1.20 2004/03/21 04:33:07 anarxia Exp $
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <jclass/jstring.h>
#include <jclass/class.h>
/**
* jclass_utf8_to_string
* @utf8_string: A pointer to the UTF-8 string.
* @length: The length of the string in bytes.
*
* Converts a java UTF-8 string to its ASCII equivalent.
* Java uses a slightly different format than standard UTF-8.
*
* Returns: A string allocated with malloc.
*/
char* jclass_utf8_to_string(const uint8_t* utf8_string, uint16_t length)
{
char* internal_string;
uint16_t count;
uint8_t character[3];
uint16_t extra_chars = 0;
internal_string = (char*) malloc(length + 1);
internal_string[0] = '\0';
for(count = 0; count < length; count++)
{
character[0] = utf8_string[count];
if(character[0] == '\0')
{
internal_string[0] = '\0';
break;
}
if(character[0] < 0x7f)
internal_string[count - extra_chars] = character[0];
else /* multi byte */
{
count++;
extra_chars++;
character[1] = utf8_string[count];
if (character[1] < 0x7f) /* 2 byte */
{
internal_string[count - extra_chars] = ((character[0] & 0x1f) << 6) + (character[1] & 0x3f);
}
else /* 3 byte */
{
count++;
extra_chars++;
character[2] = utf8_string[count];
internal_string[count - extra_chars] = ((character[0] & 0xf) << 12) +
((character[1] & 0x3f) << 6) +
(character[2] & 0x3f);
}
}
}
internal_string[length - extra_chars] = '\0';
return internal_string;
}
/**
* jclass_get_printable_string
* @raw_string: The string to translate (it is not changed).
*
* Translates a string to a more human readable form.
* All control characters are converted to escape characters.
*
* Returns: A string allocated with malloc.
*/
char* jclass_get_printable_string(const char* raw_string)
{
char* new_string;
const char* str_ptr;
int string_length;
char* to_ptr;
if(raw_string == NULL)
return NULL;
/* count the length of the string */
str_ptr = raw_string;
string_length = 0;
while(*str_ptr != '\0')
{
string_length++;
if(*str_ptr >= 7 && *str_ptr <= 13)
string_length++;
else if(*str_ptr < ' ')
string_length += 2;
str_ptr++;
}
new_string = (char*) malloc(string_length + 1);
str_ptr = raw_string;
to_ptr = new_string;
while(*str_ptr != '\0')
{
if((*str_ptr < ' ') || (*str_ptr == '\\') || (*str_ptr == '\"') )
{
*(to_ptr++) = '\\';
switch(*str_ptr)
{
case 7:
*to_ptr = 'a';
break;
case 8:
*to_ptr = 'b';
break;
case 9:
*to_ptr = 't';
break;
case 10:
*to_ptr = 'n';
break;
case 11:
*to_ptr = 'v';
break;
case 12:
*to_ptr = 'f';
break;
case 13:
*to_ptr = 'r';
break;
case '\\':
*to_ptr = '\\';
break;
case '\"':
*to_ptr = '\"';
break;
default:
*(to_ptr++) = (*str_ptr / 10) + 30;
*to_ptr = (*str_ptr % 10) + 30;
}
}
else
*to_ptr = *str_ptr;
str_ptr++;
to_ptr++;
}
*to_ptr = '\0';
return new_string;
}
/**
* jclass_float_to_string
* @float_bytes: The float contained in a uint32 integer.
*
* Gives a string representation of a float.
*
* Returns: A string allocated with malloc.
*/
char* jclass_float_to_string(uint32_t float_bytes)
{
char* number;
int exponent;
int mantissa;
double float_num;
if(float_bytes == 0x7f800000)
number = strdup("+infinity");
else if(float_bytes == 0xff800000)
number = strdup("-infinity");
else if((float_bytes >= 0x7f800001 && float_bytes <= 0x7fffffff) ||
(float_bytes >= 0xff800001 && float_bytes <= 0xffffffff))
number = strdup("NaN");
else if(float_bytes == 0x00000000 || float_bytes == 0x80000000)
number = strdup("0");
else
{
number = (char*) malloc(40);
number[40] = '\0';
/* set sign */
if (float_bytes & 0x80000000)
number[0] = '-';
else
number[0] = ' ';
exponent = (float_bytes >> 23) & 0xff;
mantissa = (exponent == 0) ?
(float_bytes & 0x7fffff) << 1 :
(float_bytes & 0x7fffff) | 0x800000;
float_num = pow(2, (exponent - 150));
float_num *= mantissa;
sprintf(&number[1],"%f", float_num);
}
return number;
}
/**
* jclass_double_to_string
* @double_bytes: The double contained in a uint64.
*
* Converts a double to a string.
*
* Returns: A string allocated with malloc.
*/
char* jclass_double_to_string(uint64_t double_bytes)
{
char* number;
int exponent;
uint64_t mantissa;
double double_num;
if(double_bytes == 0x7ff0000000000000LL)
number = strdup("+infinity");
else if(double_bytes == 0xfff0000000000000LL)
number = strdup("-infinity");
else if((double_bytes >= 0x7ff0000000000001LL && double_bytes <= 0x7fffffffffffffffLL) ||
(double_bytes >= 0xfff0000000000001LL && double_bytes <= 0xffffffffffffffffLL))
number = strdup("NaN");
else if(double_bytes == 0x0000000000000000LL || double_bytes == 0x8000000000000000LL)
number = strdup("0");
else
{
number = (char*) malloc(80);
number[40] = '\0';
/* set sign */
if ((double_bytes >> 63))
number[0] = '-';
else
number[0] = ' ';
exponent = (double_bytes >> 52) & 0x7ffL;
mantissa = (exponent == 0) ?
(double_bytes & 0xfffffffffffffLL) << 1 :
(double_bytes & 0xfffffffffffffLL) | 0x10000000000000LL;
double_num = pow(2, (exponent - 1075));
double_num *= mantissa;
sprintf(&number[1],"%f", double_num);
}
return number;
}
/**
* jclass_descriptor_get_type
* @descriptor: The coded descriptor string.
*
* Gives the type of a coded descriptor.
*
* Returns: A string allocated with malloc containg the type in the descriptor.
*/
char* jclass_descriptor_get_type(const char* descriptor)
{
char* type;
int length;
int param_length;
int array_length;
int i;
int j;
length = strlen(descriptor);
i = 0;
/* the descriptor contains parameter information
* Skip it
*/
if(descriptor[0] == '(')
{
while(descriptor[i] != ')')
i++;
i++;
}
param_length = length - i;
array_length = 0;
while(descriptor[i] == '[')
{
i++;
array_length++;
}
if (descriptor[i] == 'L')
{
type = (char*) malloc(param_length + 1);
i++;
j = 0;
while(descriptor[i] != ';' && descriptor[i] != '\0')
{
if (descriptor[i] == '/')
type[j] = '.';
else
type[j] = descriptor[i];
j++;
i++;
}
type[j] = '\0';
}
else
{
switch(descriptor[i])
{
case 'B':
type = strdup("byte");
break;
case 'C':
type = strdup("char");
break;
case 'D':
type = strdup("double");
break;
case 'F':
type = strdup("float");
break;
case 'I':
type = strdup("int");
break;
case 'J':
type = strdup("long");
break;
case 'S':
type = strdup("short");
break;
case 'V':
type = strdup("void");
break;
case 'Z':
type = strdup("boolean");
break;
default:
type = (char*) malloc(2);
type[0] = descriptor[i];
type[1] = '\0';
}
}
if(array_length)
{
type = (char*) realloc(type,((array_length * 2) + strlen(type) + 1));
for(i = 0; i < array_length; i++)
strcat(type,"[]");
}
return type;
}
/**
* jclass_descriptor_get_parameters_array
* @descriptor: The coded descriptor.
*
* Gives the parameters part of a coded descriptor.
*
* Returns: A NULL terminated string array allocated with malloc containg the parameters
* in the descriptor.
*/
char** jclass_descriptor_get_parameters_array(const char* descriptor)
{
char** params;
const char *p;
int i;
int token_length;
int params_count;
int parsing_class;
int parsing_array;
if(descriptor[0] != '(') {
params = (char**) malloc(1 * sizeof(char*));
params[0] = NULL;
return params;
}
params_count = 0;
parsing_class = 0;
parsing_array = 0;
for (p = &descriptor[1]; *p != '\0' && *p != ')'; p++) {
/* Array */
if (*p == '[')
continue;
if (!parsing_class) {
switch (*p) {
case 'L': /* class */
parsing_class = 1;
case 'B': /* byte */
case 'C': /* char */
case 'D': /* double */
case 'F': /* float */
case 'I': /* int */
case 'J': /* long */
case 'S': /* short */
case 'V': /* void */
case 'Z': /* boolean */
params_count++;
}
}
else if (*p == ';')
parsing_class = 0;
}
params = (char**) malloc((params_count + 1) * sizeof(char*));
params[params_count] = NULL;
token_length = 0;
i = 0;
p = &descriptor[1];
for (i = 0; i < params_count; i++) {
params[i] = jclass_descriptor_get_type(p);
/* skip arrays */
while (*p == '[' && *p != '\0')
p++;
/* skip classes */
if (*p == 'L') {
while (*p != ';' && *p != '\0')
p++;
if (*p == ';')
p++;
}
else /* skip primitives */
p++;
}
return params;
}
/**
* jclass_descriptor_get_parameters
* @descriptor: The coded descriptor.
*
* Deprecated. Use jclass_descriptor_get_parameters_array() instead.
* Gives the parameters part of a coded descriptor.
*
* Returns: A string allocated with malloc containg the parameters
* in the descriptor.
*/
char* jclass_descriptor_get_parameters(const char* descriptor)
{
char* params;
char* internal_params;
char* curr_param;
int index;
char temp_char;
int token_length;
int params_length;
if(descriptor[0] != '(')
{
params = strdup("");
return params;
}
internal_params = strdup(&descriptor[1]);
params_length = 0;
while(internal_params[params_length] != ')')
params_length++;
params = strdup("(");
index = 0;
while(index < params_length)
{
token_length = 0;
while(internal_params[index + token_length] == '[')
token_length++;
if(internal_params[index + token_length] == 'L')
{
while(internal_params[index + token_length] != ';')
token_length++;
}
token_length++;
temp_char = internal_params[index + token_length];
internal_params[index + token_length] = '\0';
curr_param = jclass_descriptor_get_type(&(internal_params[index]));
internal_params[index + token_length] = temp_char;
index += token_length;
params = (char*) realloc(params, (strlen(params) + strlen(curr_param) + 3));
if(params[1] != '\0')
strcat(params, ", ");
strcat(params, curr_param);
free(curr_param);
}
free(internal_params);
params = (char*) realloc(params, (strlen(params) + 2));
strcat(params, ")");
return params;
}
/**
* jclass_get_class_from_method_signature
* @method_signature: The method signature.
*
* Gives the name of the class given a method signature for one
* of its methods.
*
* Returns: A string allocated with malloc.
*/
char* jclass_get_class_from_method_signature(const char* method)
{
char* class_name;
int start_index;
int finish_index;
if(method == NULL)
return NULL;
finish_index = strlen(method) - 1;
while(method[finish_index] != '(' && finish_index > 0)
finish_index--;
while(method[finish_index] != '.' && finish_index > 0)
finish_index--;
start_index = 0;
while(method[start_index] != '\0' && method[start_index] != ' '
&& method[start_index] != '(')
start_index++;
/* special case for constructors */
if(method[start_index] == '(')
{
finish_index = start_index;
start_index = 0;
}
else
start_index++;
if ((finish_index - start_index) > 0)
{
class_name = (char*) malloc(sizeof(char) * (1 + finish_index - start_index));
strncpy(class_name, &method[start_index], (finish_index - start_index));
class_name[finish_index - start_index] = '\0';
}
else
return NULL;
return class_name;
}
/**
* jclass_get_package_from_class_name
* @class_name: The fully qualified class name.
*
* Given a full class name it returns the package the class is a member of.
* If the class is not part of a package it returns NULL.
*
* Returns: A string allocated with malloc.
*/
char* jclass_get_package_from_class_name(const char* classname)
{
char* package;
int index;
int last_index;
if(classname == NULL)
return NULL;
index = 0;
last_index = 0;
while(classname[index] != '\0' && classname[index] != '(')
{
if(classname[index] == '.')
last_index = index;
index++;
}
if(last_index)
{
package = (char*) malloc(sizeof(char) * last_index + 1);
strncpy(package, classname, last_index);
package[last_index] = '\0';
}
else
package = NULL;
return package;
}
/**
* jclass_string_is_primitive_type
* @type_string: A null terminated string.
*
* Checks whether the given string is the name of a primitive type.
*
* Returns: 1 if the type given is a pritive type, 0 otherwise.
*/
int jclass_string_is_primitive_type(const char* type)
{
int prim;
prim = (strcmp("void", type) == 0) ||
(strcmp("byte", type) == 0) ||
(strcmp("char", type) == 0) ||
(strcmp("double", type) == 0) ||
(strcmp("float", type) == 0) ||
(strcmp("int", type) == 0) ||
(strcmp("long", type) == 0) ||
(strcmp("short", type) == 0) ||
(strcmp("boolean", type) == 0);
return prim;
}
/**
* jclass_classname_to_filename
* @class_name: The name of the class.
* @path_slash: The slash for your OS ('/' for Unices, '\\' for Win).
*
* Converts the given class name to a filename.
* For example java.lang.String will be converted to: java/lang/String.class.
*
* Returns: A string allocated with malloc with the filename.
*/
char* jclass_classname_to_filename(const char* class_name, char slash)
{
char* class_filename;
int i;
class_filename = (char*) malloc(strlen(class_name) + 7);
strcpy(class_filename, class_name);
/* replace . with / */
for(i = 0; i < strlen(class_filename); i++)
{
if(class_filename[i] == '.')
class_filename[i] = slash;
}
strcat(class_filename, ".class");
return class_filename;
}
/* Macro to save some typing in the get_access_flag_string function.
*/
#define ADD_FLAG_TO_STRING(flag) do { \
if(temp_string[0] != '\0') \
strcat(temp_string, " ");\
strcat(temp_string, flag); \
}while(0)
/**
* jclass_access_flag_to_string
* @access_flag: The access flag.
* @is_class: Set to 1 if the flags are for a class, 0 otherwise.
*
* Gives the string representation of an access flag.
*
* Returns: A string allocated with malloc.
*/
char* jclass_access_flag_to_string(uint16_t flag, int is_class)
{
char* access_string;
char temp_string[256];
temp_string[0] = '\0';
if (flag & ACC_PUBLIC)
strcat(temp_string, "public");
else if (flag & ACC_PRIVATE)
strcat(temp_string, "private");
else if (flag & ACC_PROTECTED)
strcat(temp_string, "protected");
if (flag & ACC_STATIC)
ADD_FLAG_TO_STRING("static");
if (flag & ACC_FINAL)
ADD_FLAG_TO_STRING("final");
if ((flag & ACC_SYNCHRONIZED) && !is_class)
ADD_FLAG_TO_STRING("synchronized");
if (flag & ACC_VOLATILE)
ADD_FLAG_TO_STRING("volatile");
if (flag & ACC_TRANSIENT)
ADD_FLAG_TO_STRING("transient");
if (flag & ACC_NATIVE)
ADD_FLAG_TO_STRING("native");
if (flag & ACC_STRICTFP)
ADD_FLAG_TO_STRING("strictfp");
if (flag & ACC_INTERFACE)
ADD_FLAG_TO_STRING("interface");
else if(is_class) {
if (flag & ACC_ABSTRACT)
ADD_FLAG_TO_STRING("abstract");
ADD_FLAG_TO_STRING("class");
}
access_string = strdup(temp_string);
return access_string;
}
#undef ADD_FLAG_TO_STRING
|