aboutsummaryrefslogtreecommitdiff
path: root/libjava/classpath/java/util/jar/JarInputStream.java
blob: 4de6609a59610131000931a641c80301092ed77d (plain)
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
/* JarInputStream.java - InputStream for reading jar files
   Copyright (C) 2000, 2004 Free Software Foundation, Inc.

This file is part of GNU Classpath.

GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version. */

package java.util.jar;

import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

/**
 * InputStream for reading jar files.
 * XXX - verification of the signatures in the Manifest file is not yet
 * implemented.
 *
 * @since 1.2
 * @author Mark Wielaard (mark@klomp.org)
 */

public class JarInputStream extends ZipInputStream
{
  // Fields

  /** The manifest for this file or null when there was no manifest. */
  private Manifest manifest;

  /** The first real JarEntry for this file. Used by readManifest() to store
     an entry that isn't the manifest but that should be returned by
     getNextEntry next time it is called. Null when no firstEntry was read
     while searching for the manifest entry, or when it has already been
     returned by getNextEntry(). */
  private JarEntry firstEntry;

  // Constructors

  /**
   * Creates a new JarInputStream and tries to read the manifest.
   * If such a manifest is present the JarInputStream tries to verify all
   * the entry signatures while reading.
   *
   * @param in InputStream to read the jar from
   * @exception IOException when an error occurs when opening or reading
   */
  public JarInputStream(InputStream in) throws IOException
  {
    this(in, true);
  }

  /**
   * Creates a new JarInputStream and tries to read the manifest.
   * If such a manifest is present and verify is true, the JarInputStream
   * tries to verify all the entry signatures while reading.
   *
   * @param in InputStream to read the jar from
   * @param verify whether or not to verify the manifest entries
   * @exception IOException when an error occurs when opening or reading
   */
  public JarInputStream(InputStream in, boolean verify) throws IOException
  {
    super(in);
    readManifest(verify);
  }

  // Methods

  /**
   * Set the manifest if found. Skips all entries that start with "META-INF/"
   *
   * @param verify when true (and a Manifest is found) checks the Manifest,
   * when false no check is performed
   * @exception IOException if an error occurs while reading
   */
  private void readManifest(boolean verify) throws IOException
  {
    firstEntry = (JarEntry) super.getNextEntry();
    while ((firstEntry != null) &&
           firstEntry.getName().startsWith("META-INF/"))
      {
        if (firstEntry.getName().equals(JarFile.MANIFEST_NAME))
          {
            manifest = new Manifest(this);
          }
        firstEntry = (JarEntry) super.getNextEntry();
      }

    if (verify)
      {
        // XXX
      }
  }

  /**
   * Creates a JarEntry for a particular name and consults the manifest
   * for the Attributes of the entry.
   * Used by <code>ZipEntry.getNextEntry()</code>
   *
   * @param name the name of the new entry
   */
  protected ZipEntry createZipEntry(String name)
  {
    ZipEntry zipEntry = super.createZipEntry(name);
    JarEntry jarEntry = new JarEntry(zipEntry);
    if (manifest != null)
      {
        jarEntry.attr = manifest.getAttributes(name);
      }
    return jarEntry;
  }

  /**
   * Returns the Manifest for the jar file or null if there was no Manifest.
   */
  public Manifest getManifest()
  {
    return manifest;
  }

  /**
   * Returns the next entry or null when there are no more entries.
   * Does actually return a JarEntry, if you don't want to cast it yourself
   * use <code>getNextJarEntry()</code>. Does not return any entries found
   * at the beginning of the ZipFile that are special
   * (those that start with "META-INF/").
   *
   * @exception IOException if an IO error occurs when reading the entry
   */
  public ZipEntry getNextEntry() throws IOException
  {
    ZipEntry entry;
    if (firstEntry != null)
      {
        entry = firstEntry;
        firstEntry = null;
      }
    else
      {
        entry = super.getNextEntry();
      }
    return entry;
  }

  /**
   * Returns the next jar entry or null when there are no more entries.
   *
   * @exception IOException if an IO error occurs when reading the entry
   */
  public JarEntry getNextJarEntry() throws IOException
  {
    return (JarEntry) getNextEntry();
  }

  /**
   * XXX
   *
   * @param buf XXX
   * @param off XXX
   * @param len XXX
   * @return XXX
   * @exception IOException XXX
   */
  public int read(byte[]buf, int off, int len) throws IOException
  {
    // XXX if (verify) {}
    return super.read(buf, off, len);
  }
}
an> }, /* pc */ { ARMV7M_xPSR, 0x44, 32 }, /* xPSR */ }; static const struct rtos_register_stacking nuttx_stacking_cortex_m = { .stack_registers_size = 0x48, .stack_growth_direction = -1, .num_output_registers = 17, .register_offsets = nuttx_stack_offsets_cortex_m }; static const struct stack_register_offset nuttx_stack_offsets_cortex_m_fpu[] = { { ARMV7M_R0, 0x6c, 32 }, /* r0 */ { ARMV7M_R1, 0x70, 32 }, /* r1 */ { ARMV7M_R2, 0x74, 32 }, /* r2 */ { ARMV7M_R3, 0x78, 32 }, /* r3 */ { ARMV7M_R4, 0x08, 32 }, /* r4 */ { ARMV7M_R5, 0x0c, 32 }, /* r5 */ { ARMV7M_R6, 0x10, 32 }, /* r6 */ { ARMV7M_R7, 0x14, 32 }, /* r7 */ { ARMV7M_R8, 0x18, 32 }, /* r8 */ { ARMV7M_R9, 0x1c, 32 }, /* r9 */ { ARMV7M_R10, 0x20, 32 }, /* r10 */ { ARMV7M_R11, 0x24, 32 }, /* r11 */ { ARMV7M_R12, 0x7c, 32 }, /* r12 */ { ARMV7M_R13, 0, 32 }, /* sp */ { ARMV7M_R14, 0x80, 32 }, /* lr */ { ARMV7M_PC, 0x84, 32 }, /* pc */ { ARMV7M_xPSR, 0x88, 32 }, /* xPSR */ }; static const struct rtos_register_stacking nuttx_stacking_cortex_m_fpu = { .stack_registers_size = 0x8c, .stack_growth_direction = -1, .num_output_registers = 17, .register_offsets = nuttx_stack_offsets_cortex_m_fpu }; static int pid_offset = PID; static int state_offset = STATE; static int name_offset = NAME; static int xcpreg_offset = XCPREG; static int name_size = NAME_SIZE; static int rcmd_offset(const char *cmd, const char *name) { if (strncmp(cmd, name, strlen(name))) return -1; if (strlen(cmd) <= strlen(name) + 1) return -1; return atoi(cmd + strlen(name)); } static int nuttx_thread_packet(struct connection *connection, char const *packet, int packet_size) { char cmd[GDB_BUFFER_SIZE / 2 + 1] = ""; /* Extra byte for null-termination */ if (!strncmp(packet, "qRcmd", 5)) { size_t len = unhexify((uint8_t *)cmd, packet + 6, sizeof(cmd)); int offset; if (len <= 0) goto pass; offset = rcmd_offset(cmd, "nuttx.pid_offset"); if (offset >= 0) { LOG_INFO("pid_offset: %d", offset); pid_offset = offset; goto retok; } offset = rcmd_offset(cmd, "nuttx.state_offset"); if (offset >= 0) { LOG_INFO("state_offset: %d", offset); state_offset = offset; goto retok; } offset = rcmd_offset(cmd, "nuttx.name_offset"); if (offset >= 0) { LOG_INFO("name_offset: %d", offset); name_offset = offset; goto retok; } offset = rcmd_offset(cmd, "nuttx.xcpreg_offset"); if (offset >= 0) { LOG_INFO("xcpreg_offset: %d", offset); xcpreg_offset = offset; goto retok; } offset = rcmd_offset(cmd, "nuttx.name_size"); if (offset >= 0) { LOG_INFO("name_size: %d", offset); name_size = offset; goto retok; } } pass: return rtos_thread_packet(connection, packet, packet_size); retok: gdb_put_packet(connection, "OK", 2); return ERROR_OK; } static bool nuttx_detect_rtos(struct target *target) { if ((target->rtos->symbols) && (target->rtos->symbols[0].address != 0) && (target->rtos->symbols[1].address != 0)) { return true; } return false; } static int nuttx_create(struct target *target) { target->rtos->gdb_thread_packet = nuttx_thread_packet; LOG_INFO("target type name = %s", target->type->name); return 0; } static int nuttx_update_threads(struct rtos *rtos) { uint32_t thread_count; struct tcb tcb; int ret; uint32_t head; uint32_t tcb_addr; uint32_t i; uint8_t state; if (!rtos->symbols) { LOG_ERROR("No symbols for NuttX"); return -3; } /* free previous thread details */ rtos_free_threadlist(rtos); ret = target_read_buffer(rtos->target, rtos->symbols[1].address, sizeof(g_tasklist), (uint8_t *)&g_tasklist); if (ret) { LOG_ERROR("target_read_buffer : ret = %d\n", ret); return ERROR_FAIL; } thread_count = 0; for (i = 0; i < TASK_QUEUE_NUM; i++) { if (g_tasklist[i].addr == 0) continue; ret = target_read_u32(rtos->target, g_tasklist[i].addr, &head); if (ret) { LOG_ERROR("target_read_u32 : ret = %d\n", ret); return ERROR_FAIL; } /* readytorun head is current thread */ if (g_tasklist[i].addr == rtos->symbols[0].address) rtos->current_thread = head; tcb_addr = head; while (tcb_addr) { struct thread_detail *thread; ret = target_read_buffer(rtos->target, tcb_addr, sizeof(tcb), (uint8_t *)&tcb); if (ret) { LOG_ERROR("target_read_buffer : ret = %d\n", ret); return ERROR_FAIL; } thread_count++; rtos->thread_details = realloc(rtos->thread_details, sizeof(struct thread_detail) * thread_count); thread = &rtos->thread_details[thread_count - 1]; thread->threadid = tcb_addr; thread->exists = true; state = tcb.dat[state_offset - 8]; thread->extra_info_str = NULL; if (state < ARRAY_SIZE(task_state_str)) { thread->extra_info_str = malloc(256); snprintf(thread->extra_info_str, 256, "pid:%d, %s", tcb.dat[pid_offset - 8] | tcb.dat[pid_offset - 8 + 1] << 8, task_state_str[state]); } if (name_offset) { thread->thread_name_str = malloc(name_size + 1); snprintf(thread->thread_name_str, name_size, "%s", (char *)&tcb.dat[name_offset - 8]); } else { thread->thread_name_str = malloc(sizeof("None")); strcpy(thread->thread_name_str, "None"); } tcb_addr = tcb.flink; } } rtos->thread_count = thread_count; return 0; } /* * thread_id = tcb address; */ static int nuttx_get_thread_reg_list(struct rtos *rtos, int64_t thread_id, struct rtos_reg **reg_list, int *num_regs) { int retval; /* Check for armv7m with *enabled* FPU, i.e. a Cortex-M4F */ bool cm4_fpu_enabled = false; struct armv7m_common *armv7m_target = target_to_armv7m(rtos->target); if (is_armv7m(armv7m_target)) { if (armv7m_target->fp_feature == FPV4_SP) { /* Found ARM v7m target which includes a FPU */ uint32_t cpacr; retval = target_read_u32(rtos->target, FPU_CPACR, &cpacr); if (retval != ERROR_OK) { LOG_ERROR("Could not read CPACR register to check FPU state"); return -1; } /* Check if CP10 and CP11 are set to full access. */ if (cpacr & 0x00F00000) { /* Found target with enabled FPU */ cm4_fpu_enabled = 1; } } } const struct rtos_register_stacking *stacking; if (cm4_fpu_enabled) stacking = &nuttx_stacking_cortex_m_fpu; else stacking = &nuttx_stacking_cortex_m; return rtos_generic_stack_read(rtos->target, stacking, (uint32_t)thread_id + xcpreg_offset, reg_list, num_regs); } static int nuttx_get_symbol_list_to_lookup(struct symbol_table_elem *symbol_list[]) { unsigned int i; *symbol_list = (struct symbol_table_elem *) calloc(1, sizeof(struct symbol_table_elem) * ARRAY_SIZE(nuttx_symbol_list)); for (i = 0; i < ARRAY_SIZE(nuttx_symbol_list); i++) (*symbol_list)[i].symbol_name = nuttx_symbol_list[i]; return 0; } struct rtos_type nuttx_rtos = { .name = "nuttx", .detect_rtos = nuttx_detect_rtos, .create = nuttx_create, .update_threads = nuttx_update_threads, .get_thread_reg_list = nuttx_get_thread_reg_list, .get_symbol_list_to_lookup = nuttx_get_symbol_list_to_lookup, };