Path: blob/master/src/java.instrument/share/native/libinstrument/JarFacade.c
41149 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#include <string.h>26#include <stdlib.h>2728#include "jni.h"29#include "manifest_info.h"30#include "JarFacade.h"3132typedef struct {33jarAttribute* head;34jarAttribute* tail;35} iterationContext;3637static void38doAttribute(const char* name, const char* value, void* user_data) {39iterationContext* context = (iterationContext*) user_data;4041jarAttribute* attribute = (jarAttribute*)malloc(sizeof(jarAttribute));42if (attribute != NULL) {43attribute->name = strdup(name);44if (attribute->name == NULL) {45free(attribute);46} else {47char *begin = (char *)value;48char *end;49size_t value_len;5051/* skip any leading white space */52while (*begin == ' ') {53begin++;54}5556/* skip any trailing white space */57end = &begin[strlen(begin)];58while (end > begin && end[-1] == ' ') {59end--;60}6162if (begin == end) {63/* no value so skip this attribute */64free(attribute->name);65free(attribute);66return;67}6869value_len = (size_t)(end - begin);70attribute->value = malloc(value_len + 1);71if (attribute->value == NULL) {72free(attribute->name);73free(attribute);74} else {75/* save the value without leading or trailing whitespace */76strncpy(attribute->value, begin, value_len);77attribute->value[value_len] = '\0';78attribute->next = NULL;79if (context->head == NULL) {80context->head = attribute;81} else {82context->tail->next = attribute;83}84context->tail = attribute;85}86}8788}89}9091/*92* Return a list of attributes from the main section of the given JAR93* file. Returns NULL if there is an error or there aren't any attributes.94*/95jarAttribute*96readAttributes(const char* jarfile)97{98int rc;99iterationContext context = { NULL, NULL };100101rc = JLI_ManifestIterate(jarfile, doAttribute, (void*)&context);102103if (rc == 0) {104return context.head;105} else {106freeAttributes(context.head);107return NULL;108}109}110111112/*113* Free a list of attributes114*/115void116freeAttributes(jarAttribute* head) {117while (head != NULL) {118jarAttribute* next = (jarAttribute*)head->next;119free(head->name);120free(head->value);121free(head);122head = next;123}124}125126/*127* Get the value of an attribute in an attribute list. Returns NULL128* if attribute not found.129*/130char*131getAttribute(const jarAttribute* attributes, const char* name) {132while (attributes != NULL) {133if (strcasecmp(attributes->name, name) == 0) {134return attributes->value;135}136attributes = (jarAttribute*)attributes->next;137}138return NULL;139}140141142