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
|
/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Functions for dealing with method prototypes
*/
#include "DexProto.h"
#include <stdlib.h>
#include <string.h>
/*
* ===========================================================================
* String Cache
* ===========================================================================
*/
/*
* Make sure that the given cache can hold a string of the given length,
* including the final '\0' byte.
*/
void dexStringCacheAlloc(DexStringCache* pCache, size_t length) {
if (pCache->allocatedSize != 0) {
if (pCache->allocatedSize >= length) {
return;
}
free((void*) pCache->value);
}
if (length <= sizeof(pCache->buffer)) {
pCache->value = pCache->buffer;
pCache->allocatedSize = 0;
} else {
pCache->value = (char*) malloc(length);
pCache->allocatedSize = length;
}
}
/*
* Initialize the given DexStringCache. Use this function before passing
* one into any other function.
*/
void dexStringCacheInit(DexStringCache* pCache) {
pCache->value = pCache->buffer;
pCache->allocatedSize = 0;
pCache->buffer[0] = '\0';
}
/*
* Release the allocated contents of the given DexStringCache, if any.
* Use this function after your last use of a DexStringCache.
*/
void dexStringCacheRelease(DexStringCache* pCache) {
if (pCache->allocatedSize != 0) {
free((void*) pCache->value);
pCache->value = pCache->buffer;
pCache->allocatedSize = 0;
}
}
/*
* If the given DexStringCache doesn't already point at the given value,
* make a copy of it into the cache. This always returns a writable
* pointer to the contents (whether or not a copy had to be made). This
* function is intended to be used after making a call that at least
* sometimes doesn't populate a DexStringCache.
*/
char* dexStringCacheEnsureCopy(DexStringCache* pCache, const char* value) {
if (value != pCache->value) {
size_t length = strlen(value) + 1;
dexStringCacheAlloc(pCache, length);
memcpy(pCache->value, value, length);
}
return pCache->value;
}
/*
* Abandon the given DexStringCache, and return a writable copy of the
* given value (reusing the string cache's allocation if possible).
* The return value must be free()d by the caller. Use this instead of
* dexStringCacheRelease() if you want the buffer to survive past the
* scope of the DexStringCache.
*/
char* dexStringCacheAbandon(DexStringCache* pCache, const char* value) {
if ((value == pCache->value) && (pCache->allocatedSize != 0)) {
char* result = pCache->value;
pCache->allocatedSize = 0;
pCache->value = pCache->buffer;
return result;
} else {
return strdup(value);
}
}
/*
* ===========================================================================
* Method Prototypes
* ===========================================================================
*/
/*
* Return the DexProtoId from the given DexProto. The DexProto must
* actually refer to a DexProtoId.
*/
static inline const DexProtoId* getProtoId(const DexProto* pProto) {
return dexGetProtoId(pProto->dexFile, pProto->protoIdx);
}
/* (documented in header file) */
const char* dexProtoGetShorty(const DexProto* pProto) {
const DexProtoId* protoId = getProtoId(pProto);
return dexStringById(pProto->dexFile, protoId->shortyIdx);
}
/* (documented in header file) */
const char* dexProtoGetMethodDescriptor(const DexProto* pProto,
DexStringCache* pCache) {
const DexFile* dexFile = pProto->dexFile;
const DexProtoId* protoId = getProtoId(pProto);
const DexTypeList* typeList = dexGetProtoParameters(dexFile, protoId);
size_t length = 3; // parens and terminating '\0'
u4 paramCount = (typeList == NULL) ? 0 : typeList->size;
u4 i;
for (i = 0; i < paramCount; i++) {
u4 idx = dexTypeListGetIdx(typeList, i);
length += strlen(dexStringByTypeIdx(dexFile, idx));
}
length += strlen(dexStringByTypeIdx(dexFile, protoId->returnTypeIdx));
dexStringCacheAlloc(pCache, length);
char *at = (char*) pCache->value;
*(at++) = '(';
for (i = 0; i < paramCount; i++) {
u4 idx = dexTypeListGetIdx(typeList, i);
const char* desc = dexStringByTypeIdx(dexFile, idx);
strcpy(at, desc);
at += strlen(desc);
}
*(at++) = ')';
strcpy(at, dexStringByTypeIdx(dexFile, protoId->returnTypeIdx));
return pCache->value;
}
/* (documented in header file) */
char* dexProtoCopyMethodDescriptor(const DexProto* pProto) {
DexStringCache cache;
dexStringCacheInit(&cache);
return dexStringCacheAbandon(&cache,
dexProtoGetMethodDescriptor(pProto, &cache));
}
/* (documented in header file) */
const char* dexProtoGetParameterDescriptors(const DexProto* pProto,
DexStringCache* pCache) {
DexParameterIterator iterator;
size_t length = 1; /* +1 for the terminating '\0' */
dexParameterIteratorInit(&iterator, pProto);
for (;;) {
const char* descriptor = dexParameterIteratorNextDescriptor(&iterator);
if (descriptor == NULL) {
break;
}
length += strlen(descriptor);
}
dexParameterIteratorInit(&iterator, pProto);
dexStringCacheAlloc(pCache, length);
char *at = (char*) pCache->value;
for (;;) {
const char* descriptor = dexParameterIteratorNextDescriptor(&iterator);
if (descriptor == NULL) {
break;
}
strcpy(at, descriptor);
at += strlen(descriptor);
}
return pCache->value;
}
/* (documented in header file) */
const char* dexProtoGetReturnType(const DexProto* pProto) {
const DexProtoId* protoId = getProtoId(pProto);
return dexStringByTypeIdx(pProto->dexFile, protoId->returnTypeIdx);
}
/* (documented in header file) */
size_t dexProtoGetParameterCount(const DexProto* pProto) {
const DexProtoId* protoId = getProtoId(pProto);
const DexTypeList* typeList =
dexGetProtoParameters(pProto->dexFile, protoId);
return (typeList == NULL) ? 0 : typeList->size;
}
/* (documented in header file) */
int dexProtoComputeArgsSize(const DexProto* pProto) {
const char* shorty = dexProtoGetShorty(pProto);
int count = 0;
/* Skip the return type. */
shorty++;
for (;;) {
switch (*(shorty++)) {
case '\0': {
return count;
}
case 'D':
case 'J': {
count += 2;
break;
}
default: {
count++;
break;
}
}
}
}
/*
* Common implementation for dexProtoCompare() and dexProtoCompareParameters().
*/
static int protoCompare(const DexProto* pProto1, const DexProto* pProto2,
bool compareReturnType) {
if (pProto1 == pProto2) {
// Easy out.
return 0;
} else {
const DexFile* dexFile1 = pProto1->dexFile;
const DexProtoId* protoId1 = getProtoId(pProto1);
const DexTypeList* typeList1 =
dexGetProtoParameters(dexFile1, protoId1);
int paramCount1 = (typeList1 == NULL) ? 0 : typeList1->size;
const DexFile* dexFile2 = pProto2->dexFile;
const DexProtoId* protoId2 = getProtoId(pProto2);
const DexTypeList* typeList2 =
dexGetProtoParameters(dexFile2, protoId2);
int paramCount2 = (typeList2 == NULL) ? 0 : typeList2->size;
if (protoId1 == protoId2) {
// Another easy out.
return 0;
}
// Compare return types.
if (compareReturnType) {
int result =
strcmp(dexStringByTypeIdx(dexFile1, protoId1->returnTypeIdx),
dexStringByTypeIdx(dexFile2, protoId2->returnTypeIdx));
if (result != 0) {
return result;
}
}
// Compare parameters.
int minParam = (paramCount1 > paramCount2) ? paramCount2 : paramCount1;
int i;
for (i = 0; i < minParam; i++) {
u4 idx1 = dexTypeListGetIdx(typeList1, i);
u4 idx2 = dexTypeListGetIdx(typeList2, i);
int result =
strcmp(dexStringByTypeIdx(dexFile1, idx1),
dexStringByTypeIdx(dexFile2, idx2));
if (result != 0) {
return result;
}
}
if (paramCount1 < paramCount2) {
return -1;
} else if (paramCount1 > paramCount2) {
return 1;
} else {
return 0;
}
}
}
/* (documented in header file) */
int dexProtoCompare(const DexProto* pProto1, const DexProto* pProto2) {
return protoCompare(pProto1, pProto2, true);
}
/* (documented in header file) */
int dexProtoCompareParameters(const DexProto* pProto1, const DexProto* pProto2){
return protoCompare(pProto1, pProto2, false);
}
/*
* Helper for dexProtoCompareToDescriptor(), which gets the return type
* descriptor from a method descriptor string.
*/
static const char* methodDescriptorReturnType(const char* descriptor) {
const char* result = strchr(descriptor, ')');
if (result == NULL) {
return NULL;
}
// The return type is the character just past the ')'.
return result + 1;
}
/*
* Helper for dexProtoCompareToDescriptor(), which indicates the end
* of an embedded argument type descriptor, which is also the
* beginning of the next argument type descriptor. Since this is for
* argument types, it doesn't accept 'V' as a valid type descriptor.
*/
static const char* methodDescriptorNextType(const char* descriptor) {
// Skip any array references.
while (*descriptor == '[') {
descriptor++;
}
switch (*descriptor) {
case 'B': case 'C': case 'D': case 'F':
case 'I': case 'J': case 'S': case 'Z': {
return descriptor + 1;
}
case 'L': {
const char* result = strchr(descriptor + 1, ';');
if (result != NULL) {
// The type ends just past the ';'.
return result + 1;
}
}
}
return NULL;
}
/*
* Common implementation for dexProtoCompareToDescriptor() and
* dexProtoCompareToParameterDescriptors(). The descriptor argument
* can be either a full method descriptor (with parens and a return
* type) or an unadorned concatenation of types (e.g. a list of
* argument types).
*/
static int protoCompareToParameterDescriptors(const DexProto* proto,
const char* descriptor, bool expectParens) {
char expectedEndChar = expectParens ? ')' : '\0';
DexParameterIterator iterator;
dexParameterIteratorInit(&iterator, proto);
if (expectParens) {
// Skip the '('.
assert (*descriptor == '(');
descriptor++;
}
for (;;) {
const char* protoDesc = dexParameterIteratorNextDescriptor(&iterator);
if (*descriptor == expectedEndChar) {
// It's the end of the descriptor string.
if (protoDesc == NULL) {
// It's also the end of the prototype's arguments.
return 0;
} else {
// The prototype still has more arguments.
return 1;
}
}
if (protoDesc == NULL) {
/*
* The prototype doesn't have arguments left, but the
* descriptor string does.
*/
return -1;
}
// Both prototype and descriptor have arguments. Compare them.
const char* nextDesc = methodDescriptorNextType(descriptor);
assert(nextDesc != NULL);
for (;;) {
char c1 = *(protoDesc++);
char c2 = (descriptor < nextDesc) ? *(descriptor++) : '\0';
if (c1 < c2) {
// This includes the case where the proto is shorter.
return -1;
} else if (c1 > c2) {
// This includes the case where the desc is shorter.
return 1;
} else if (c1 == '\0') {
// The two types are equal in length. (c2 necessarily == '\0'.)
break;
}
}
/*
* If we made it here, the two arguments matched, and
* descriptor == nextDesc.
*/
}
}
/* (documented in header file) */
int dexProtoCompareToDescriptor(const DexProto* proto,
const char* descriptor) {
// First compare the return types.
const char *returnType = methodDescriptorReturnType(descriptor);
assert(returnType != NULL);
int result = strcmp(dexProtoGetReturnType(proto), returnType);
if (result != 0) {
return result;
}
// The return types match, so we have to check arguments.
return protoCompareToParameterDescriptors(proto, descriptor, true);
}
/* (documented in header file) */
int dexProtoCompareToParameterDescriptors(const DexProto* proto,
const char* descriptors) {
return protoCompareToParameterDescriptors(proto, descriptors, false);
}
/*
* ===========================================================================
* Parameter Iterators
* ===========================================================================
*/
/*
* Initialize the given DexParameterIterator to be at the start of the
* parameters of the given prototype.
*/
void dexParameterIteratorInit(DexParameterIterator* pIterator,
const DexProto* pProto) {
pIterator->proto = pProto;
pIterator->cursor = 0;
pIterator->parameters =
dexGetProtoParameters(pProto->dexFile, getProtoId(pProto));
pIterator->parameterCount = (pIterator->parameters == NULL) ? 0
: pIterator->parameters->size;
}
/*
* Get the type_id index for the next parameter, if any. This returns
* kDexNoIndex if the last parameter has already been consumed.
*/
u4 dexParameterIteratorNextIndex(DexParameterIterator* pIterator) {
int cursor = pIterator->cursor;
int parameterCount = pIterator->parameterCount;
if (cursor >= parameterCount) {
// The iteration is complete.
return kDexNoIndex;
} else {
u4 idx = dexTypeListGetIdx(pIterator->parameters, cursor);
pIterator->cursor++;
return idx;
}
}
/*
* Get the type descriptor for the next parameter, if any. This returns
* NULL if the last parameter has already been consumed.
*/
const char* dexParameterIteratorNextDescriptor(
DexParameterIterator* pIterator) {
u4 idx = dexParameterIteratorNextIndex(pIterator);
if (idx == kDexNoIndex) {
return NULL;
}
return dexStringByTypeIdx(pIterator->proto->dexFile, idx);
}
|