src/java.instrument/share/native/libinstrument/JarFacade.c
author rwestberg
Fri, 01 Jun 2018 10:15:48 +0200
changeset 50336 1b6ea6bcd21a
parent 47216 71c04702a3d5
permissions -rw-r--r--
8203237: JFR TestBiasedLockRevocationEvents should ignore events unrelated to the test Reviewed-by: egahlin, mgronlun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     2
 * Copyright (c) 2004, 2008, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
#include <string.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#include <stdlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include "jni.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include "manifest_info.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#include "JarFacade.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
typedef struct {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
    jarAttribute* head;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    jarAttribute* tail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
} iterationContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
doAttribute(const char* name, const char* value, void* user_data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    iterationContext* context = (iterationContext*) user_data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    jarAttribute* attribute = (jarAttribute*)malloc(sizeof(jarAttribute));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    if (attribute != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        attribute->name = strdup(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        if (attribute->name == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
            free(attribute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        } else {
285
1db7e0f6f7b0 6679866: 3/2 portability issues with JLI-batch-200803 on Win*
dcubed
parents: 273
diff changeset
    48
            char *begin = (char *)value;
273
3d51c181a2e5 6274276: 3/2 java.lang.instrument JAR manifest processing does not remove spaces from class names
dcubed
parents: 2
diff changeset
    49
            char *end;
285
1db7e0f6f7b0 6679866: 3/2 portability issues with JLI-batch-200803 on Win*
dcubed
parents: 273
diff changeset
    50
            size_t value_len;
273
3d51c181a2e5 6274276: 3/2 java.lang.instrument JAR manifest processing does not remove spaces from class names
dcubed
parents: 2
diff changeset
    51
3d51c181a2e5 6274276: 3/2 java.lang.instrument JAR manifest processing does not remove spaces from class names
dcubed
parents: 2
diff changeset
    52
            /* skip any leading white space */
23014
af8145529dd4 8035054: JarFacade.c should not include ctype.h
mikael
parents: 22964
diff changeset
    53
            while (*begin == ' ') {
273
3d51c181a2e5 6274276: 3/2 java.lang.instrument JAR manifest processing does not remove spaces from class names
dcubed
parents: 2
diff changeset
    54
                begin++;
3d51c181a2e5 6274276: 3/2 java.lang.instrument JAR manifest processing does not remove spaces from class names
dcubed
parents: 2
diff changeset
    55
            }
3d51c181a2e5 6274276: 3/2 java.lang.instrument JAR manifest processing does not remove spaces from class names
dcubed
parents: 2
diff changeset
    56
3d51c181a2e5 6274276: 3/2 java.lang.instrument JAR manifest processing does not remove spaces from class names
dcubed
parents: 2
diff changeset
    57
            /* skip any trailing white space */
3d51c181a2e5 6274276: 3/2 java.lang.instrument JAR manifest processing does not remove spaces from class names
dcubed
parents: 2
diff changeset
    58
            end = &begin[strlen(begin)];
23014
af8145529dd4 8035054: JarFacade.c should not include ctype.h
mikael
parents: 22964
diff changeset
    59
            while (end > begin && end[-1] == ' ') {
273
3d51c181a2e5 6274276: 3/2 java.lang.instrument JAR manifest processing does not remove spaces from class names
dcubed
parents: 2
diff changeset
    60
                end--;
3d51c181a2e5 6274276: 3/2 java.lang.instrument JAR manifest processing does not remove spaces from class names
dcubed
parents: 2
diff changeset
    61
            }
3d51c181a2e5 6274276: 3/2 java.lang.instrument JAR manifest processing does not remove spaces from class names
dcubed
parents: 2
diff changeset
    62
3d51c181a2e5 6274276: 3/2 java.lang.instrument JAR manifest processing does not remove spaces from class names
dcubed
parents: 2
diff changeset
    63
            if (begin == end) {
3d51c181a2e5 6274276: 3/2 java.lang.instrument JAR manifest processing does not remove spaces from class names
dcubed
parents: 2
diff changeset
    64
                /* no value so skip this attribute */
3d51c181a2e5 6274276: 3/2 java.lang.instrument JAR manifest processing does not remove spaces from class names
dcubed
parents: 2
diff changeset
    65
                free(attribute->name);
3d51c181a2e5 6274276: 3/2 java.lang.instrument JAR manifest processing does not remove spaces from class names
dcubed
parents: 2
diff changeset
    66
                free(attribute);
3d51c181a2e5 6274276: 3/2 java.lang.instrument JAR manifest processing does not remove spaces from class names
dcubed
parents: 2
diff changeset
    67
                return;
3d51c181a2e5 6274276: 3/2 java.lang.instrument JAR manifest processing does not remove spaces from class names
dcubed
parents: 2
diff changeset
    68
            }
3d51c181a2e5 6274276: 3/2 java.lang.instrument JAR manifest processing does not remove spaces from class names
dcubed
parents: 2
diff changeset
    69
285
1db7e0f6f7b0 6679866: 3/2 portability issues with JLI-batch-200803 on Win*
dcubed
parents: 273
diff changeset
    70
            value_len = (size_t)(end - begin);
273
3d51c181a2e5 6274276: 3/2 java.lang.instrument JAR manifest processing does not remove spaces from class names
dcubed
parents: 2
diff changeset
    71
            attribute->value = malloc(value_len + 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            if (attribute->value == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                free(attribute->name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                free(attribute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            } else {
273
3d51c181a2e5 6274276: 3/2 java.lang.instrument JAR manifest processing does not remove spaces from class names
dcubed
parents: 2
diff changeset
    76
                /* save the value without leading or trailing whitespace */
3d51c181a2e5 6274276: 3/2 java.lang.instrument JAR manifest processing does not remove spaces from class names
dcubed
parents: 2
diff changeset
    77
                strncpy(attribute->value, begin, value_len);
285
1db7e0f6f7b0 6679866: 3/2 portability issues with JLI-batch-200803 on Win*
dcubed
parents: 273
diff changeset
    78
                attribute->value[value_len] = '\0';
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                attribute->next = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                if (context->head == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                    context->head = attribute;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                    context->tail->next = attribute;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                context->tail = attribute;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * Return a list of attributes from the main section of the given JAR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * file. Returns NULL if there is an error or there aren't any attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
jarAttribute*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
readAttributes(const char* jarfile)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    int rc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    iterationContext context = { NULL, NULL };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    rc = JLI_ManifestIterate(jarfile, doAttribute, (void*)&context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    if (rc == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        return context.head;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        freeAttributes(context.head);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * Free a list of attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
freeAttributes(jarAttribute* head) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    while (head != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        jarAttribute* next = (jarAttribute*)head->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        free(head->name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        free(head->value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        free(head);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        head = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 * Get the value of an attribute in an attribute list. Returns NULL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 * if attribute not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
char*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
getAttribute(const jarAttribute* attributes, const char* name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    while (attributes != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        if (strcasecmp(attributes->name, name) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            return attributes->value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        attributes = (jarAttribute*)attributes->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
}