314 const char * isJar(const char * path) {
315 const char * result = BAD_FILE_MSG;
316
317 int fd = open(path, O_RDONLY);
318 if (fd != -1) {
319 unsigned char buf[CHUNK_SIZE];
320
321 ssize_t count = read(fd, buf, CHUNK_SIZE);
322 if (count >= MIN_SIZE) {
323 result = BAD_MAGIC_MSG;
324
325 // be sure the file is at least a ZIP file
326 if (LOCSIG_AT(buf)) {
327
328 off_t flen = LOCNAM(buf);
329 off_t xlen = LOCEXT(buf);
330 off_t start = LOCHDR + flen;
331 off_t end = start + xlen;
332
333 if (end <= count) {
334 end -= 4; // make sure there are 4 bytes to read at start
335 while (start < end) {
336 off_t xhid = SH(buf, start);
337 off_t xdlen = SH(buf, start + 2);
338
339 start += 4 + xdlen;
340 if (xhid == 0xcafe) {
341 // found the JAR magic
342 result = NULL;
343 break;
344 }
345 }
346 }
347 }
348 }
349
350 if (result != NULL) {
351 errno = BAD_MAGIC;
352 }
353
354 close (fd);
355 }
|
314 const char * isJar(const char * path) {
315 const char * result = BAD_FILE_MSG;
316
317 int fd = open(path, O_RDONLY);
318 if (fd != -1) {
319 unsigned char buf[CHUNK_SIZE];
320
321 ssize_t count = read(fd, buf, CHUNK_SIZE);
322 if (count >= MIN_SIZE) {
323 result = BAD_MAGIC_MSG;
324
325 // be sure the file is at least a ZIP file
326 if (LOCSIG_AT(buf)) {
327
328 off_t flen = LOCNAM(buf);
329 off_t xlen = LOCEXT(buf);
330 off_t start = LOCHDR + flen;
331 off_t end = start + xlen;
332
333 if (end <= count) {
334 // make sure there are 4 bytes to read at start
335 end -= 3;
336 while ((start < end) && (start < count-3)) {
337 off_t xhid = SH(buf, start);
338 off_t xdlen = SH(buf, start + 2);
339
340 start += 4 + xdlen;
341 if (xhid == 0xcafe) {
342 // found the JAR magic
343 result = NULL;
344 break;
345 }
346 }
347 }
348 }
349 }
350
351 if (result != NULL) {
352 errno = BAD_MAGIC;
353 }
354
355 close (fd);
356 }
|