Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/instrument/JarFacade.c
38767 views
/*1* Copyright (c) 2004, 2008, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425#ifdef _WIN3226/*27* Win* needs this include. However, Linux and Solaris do not.28* Having this include on Solaris SPARC breaks having non US-ASCII29* characters in the value of the Premain-Class attribute.30*/31#include <ctype.h>32#endif /* _WIN32 */33#include <string.h>34#include <stdlib.h>3536#include "jni.h"37#include "manifest_info.h"38#include "JarFacade.h"3940typedef struct {41jarAttribute* head;42jarAttribute* tail;43} iterationContext;4445static void46doAttribute(const char* name, const char* value, void* user_data) {47iterationContext* context = (iterationContext*) user_data;4849jarAttribute* attribute = (jarAttribute*)malloc(sizeof(jarAttribute));50if (attribute != NULL) {51attribute->name = strdup(name);52if (attribute->name == NULL) {53free(attribute);54} else {55char *begin = (char *)value;56char *end;57size_t value_len;5859/* skip any leading white space */60while (isspace(*begin)) {61begin++;62}6364/* skip any trailing white space */65end = &begin[strlen(begin)];66while (end > begin && isspace(end[-1])) {67end--;68}6970if (begin == end) {71/* no value so skip this attribute */72free(attribute->name);73free(attribute);74return;75}7677value_len = (size_t)(end - begin);78attribute->value = malloc(value_len + 1);79if (attribute->value == NULL) {80free(attribute->name);81free(attribute);82} else {83/* save the value without leading or trailing whitespace */84strncpy(attribute->value, begin, value_len);85attribute->value[value_len] = '\0';86attribute->next = NULL;87if (context->head == NULL) {88context->head = attribute;89} else {90context->tail->next = attribute;91}92context->tail = attribute;93}94}9596}97}9899/*100* Return a list of attributes from the main section of the given JAR101* file. Returns NULL if there is an error or there aren't any attributes.102*/103jarAttribute*104readAttributes(const char* jarfile)105{106int rc;107iterationContext context = { NULL, NULL };108109rc = JLI_ManifestIterate(jarfile, doAttribute, (void*)&context);110111if (rc == 0) {112return context.head;113} else {114freeAttributes(context.head);115return NULL;116}117}118119120/*121* Free a list of attributes122*/123void124freeAttributes(jarAttribute* head) {125while (head != NULL) {126jarAttribute* next = (jarAttribute*)head->next;127free(head->name);128free(head->value);129free(head);130head = next;131}132}133134/*135* Get the value of an attribute in an attribute list. Returns NULL136* if attribute not found.137*/138char*139getAttribute(const jarAttribute* attributes, const char* name) {140while (attributes != NULL) {141if (strcasecmp(attributes->name, name) == 0) {142return attributes->value;143}144attributes = (jarAttribute*)attributes->next;145}146return NULL;147}148149150