author | dcubed |
Mon, 24 Mar 2008 15:14:31 -0700 | |
changeset 273 | 3d51c181a2e5 |
parent 2 | 90ce3da70b43 |
child 285 | 1db7e0f6f7b0 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
2 |
* Copyright 2004-2005 Sun Microsystems, Inc. All Rights Reserved. |
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Sun designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Sun in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 |
* have any questions. |
|
24 |
*/ |
|
25 |
||
26 |
#include <string.h> |
|
27 |
#include <stdlib.h> |
|
28 |
||
29 |
#include "jni.h" |
|
30 |
#include "manifest_info.h" |
|
31 |
#include "JarFacade.h" |
|
32 |
||
33 |
typedef struct { |
|
34 |
jarAttribute* head; |
|
35 |
jarAttribute* tail; |
|
36 |
} iterationContext; |
|
37 |
||
38 |
static void |
|
39 |
doAttribute(const char* name, const char* value, void* user_data) { |
|
40 |
iterationContext* context = (iterationContext*) user_data; |
|
41 |
||
42 |
jarAttribute* attribute = (jarAttribute*)malloc(sizeof(jarAttribute)); |
|
43 |
if (attribute != NULL) { |
|
44 |
attribute->name = strdup(name); |
|
45 |
if (attribute->name == NULL) { |
|
46 |
free(attribute); |
|
47 |
} else { |
|
273
3d51c181a2e5
6274276: 3/2 java.lang.instrument JAR manifest processing does not remove spaces from class names
dcubed
parents:
2
diff
changeset
|
48 |
char *begin = value; |
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; |
3d51c181a2e5
6274276: 3/2 java.lang.instrument JAR manifest processing does not remove spaces from class names
dcubed
parents:
2
diff
changeset
|
50 |
|
3d51c181a2e5
6274276: 3/2 java.lang.instrument JAR manifest processing does not remove spaces from class names
dcubed
parents:
2
diff
changeset
|
51 |
/* skip any leading white space */ |
3d51c181a2e5
6274276: 3/2 java.lang.instrument JAR manifest processing does not remove spaces from class names
dcubed
parents:
2
diff
changeset
|
52 |
while (isspace(*begin)) { |
3d51c181a2e5
6274276: 3/2 java.lang.instrument JAR manifest processing does not remove spaces from class names
dcubed
parents:
2
diff
changeset
|
53 |
begin++; |
3d51c181a2e5
6274276: 3/2 java.lang.instrument JAR manifest processing does not remove spaces from class names
dcubed
parents:
2
diff
changeset
|
54 |
} |
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 |
/* 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
|
57 |
end = &begin[strlen(begin)]; |
3d51c181a2e5
6274276: 3/2 java.lang.instrument JAR manifest processing does not remove spaces from class names
dcubed
parents:
2
diff
changeset
|
58 |
while (end > begin && isspace(end[-1])) { |
3d51c181a2e5
6274276: 3/2 java.lang.instrument JAR manifest processing does not remove spaces from class names
dcubed
parents:
2
diff
changeset
|
59 |
end--; |
3d51c181a2e5
6274276: 3/2 java.lang.instrument JAR manifest processing does not remove spaces from class names
dcubed
parents:
2
diff
changeset
|
60 |
} |
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 |
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
|
63 |
/* 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
|
64 |
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
|
65 |
free(attribute); |
3d51c181a2e5
6274276: 3/2 java.lang.instrument JAR manifest processing does not remove spaces from class names
dcubed
parents:
2
diff
changeset
|
66 |
return; |
3d51c181a2e5
6274276: 3/2 java.lang.instrument JAR manifest processing does not remove spaces from class names
dcubed
parents:
2
diff
changeset
|
67 |
} |
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 |
size_t value_len = (size_t)(end - begin); |
3d51c181a2e5
6274276: 3/2 java.lang.instrument JAR manifest processing does not remove spaces from class names
dcubed
parents:
2
diff
changeset
|
70 |
attribute->value = malloc(value_len + 1); |
2 | 71 |
if (attribute->value == NULL) { |
72 |
free(attribute->name); |
|
73 |
free(attribute); |
|
74 |
} else { |
|
273
3d51c181a2e5
6274276: 3/2 java.lang.instrument JAR manifest processing does not remove spaces from class names
dcubed
parents:
2
diff
changeset
|
75 |
/* 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
|
76 |
strncpy(attribute->value, begin, value_len); |
3d51c181a2e5
6274276: 3/2 java.lang.instrument JAR manifest processing does not remove spaces from class names
dcubed
parents:
2
diff
changeset
|
77 |
attribute->value[value_len] = NULL; |
2 | 78 |
attribute->next = NULL; |
79 |
if (context->head == NULL) { |
|
80 |
context->head = attribute; |
|
81 |
} else { |
|
82 |
context->tail->next = attribute; |
|
83 |
} |
|
84 |
context->tail = attribute; |
|
85 |
} |
|
86 |
} |
|
87 |
||
88 |
} |
|
89 |
} |
|
90 |
||
91 |
/* |
|
92 |
* Return a list of attributes from the main section of the given JAR |
|
93 |
* file. Returns NULL if there is an error or there aren't any attributes. |
|
94 |
*/ |
|
95 |
jarAttribute* |
|
96 |
readAttributes(const char* jarfile) |
|
97 |
{ |
|
98 |
int rc; |
|
99 |
iterationContext context = { NULL, NULL }; |
|
100 |
||
101 |
rc = JLI_ManifestIterate(jarfile, doAttribute, (void*)&context); |
|
102 |
||
103 |
if (rc == 0) { |
|
104 |
return context.head; |
|
105 |
} else { |
|
106 |
freeAttributes(context.head); |
|
107 |
return NULL; |
|
108 |
} |
|
109 |
} |
|
110 |
||
111 |
||
112 |
/* |
|
113 |
* Free a list of attributes |
|
114 |
*/ |
|
115 |
void |
|
116 |
freeAttributes(jarAttribute* head) { |
|
117 |
while (head != NULL) { |
|
118 |
jarAttribute* next = (jarAttribute*)head->next; |
|
119 |
free(head->name); |
|
120 |
free(head->value); |
|
121 |
free(head); |
|
122 |
head = next; |
|
123 |
} |
|
124 |
} |
|
125 |
||
126 |
/* |
|
127 |
* Get the value of an attribute in an attribute list. Returns NULL |
|
128 |
* if attribute not found. |
|
129 |
*/ |
|
130 |
char* |
|
131 |
getAttribute(const jarAttribute* attributes, const char* name) { |
|
132 |
while (attributes != NULL) { |
|
133 |
if (strcasecmp(attributes->name, name) == 0) { |
|
134 |
return attributes->value; |
|
135 |
} |
|
136 |
attributes = (jarAttribute*)attributes->next; |
|
137 |
} |
|
138 |
return NULL; |
|
139 |
} |