jdk/src/share/bin/parse_manifest.c

Print this page
rev 5684 : 7200500: Launcher better input validation
Reviewed-by: darcy, jjh, mschoene


 473  *    0 Success
 474  *   -1 Unable to open jarfile
 475  *   -2 Error accessing the manifest from within the jarfile (most likely
 476  *      a manifest is not present, or this isn't a valid zip/jar file).
 477  */
 478 int
 479 JLI_ParseManifest(char *jarfile, manifest_info *info)
 480 {
 481     int     fd;
 482     zentry  entry;
 483     char    *lp;
 484     char    *name;
 485     char    *value;
 486     int     rc;
 487     char    *splashscreen_name = NULL;
 488 
 489     if ((fd = open(jarfile, O_RDONLY
 490 #ifdef O_BINARY
 491         | O_BINARY /* use binary mode on windows */
 492 #endif
 493         )) == -1)
 494         return (-1);
 495 
 496     info->manifest_version = NULL;
 497     info->main_class = NULL;
 498     info->jre_version = NULL;
 499     info->jre_restrict_search = 0;
 500     info->splashscreen_image_file_name = NULL;
 501     if (rc = find_file(fd, &entry, manifest_name) != 0) {
 502         close(fd);
 503         return (-2);
 504     }
 505     manifest = inflate_file(fd, &entry, NULL);
 506     if (manifest == NULL) {
 507         close(fd);
 508         return (-2);
 509     }
 510     lp = manifest;
 511     while ((rc = parse_nv_pair(&lp, &name, &value)) > 0) {
 512         if (JLI_StrCaseCmp(name, "Manifest-Version") == 0)
 513             info->manifest_version = value;
 514         else if (JLI_StrCaseCmp(name, "Main-Class") == 0)
 515             info->main_class = value;


 522             info->splashscreen_image_file_name = value;
 523         }
 524     }
 525     close(fd);
 526     if (rc == 0)
 527         return (0);
 528     else
 529         return (-2);
 530 }
 531 
 532 /*
 533  * Opens the jar file and unpacks the specified file from its contents.
 534  * Returns NULL on failure.
 535  */
 536 void *
 537 JLI_JarUnpackFile(const char *jarfile, const char *filename, int *size) {
 538     int     fd;
 539     zentry  entry;
 540     void    *data = NULL;
 541 
 542     fd = open(jarfile, O_RDONLY
 543 #ifdef O_BINARY
 544         | O_BINARY /* use binary mode on windows */
 545 #endif
 546         );
 547     if (fd != -1 && find_file(fd, &entry, filename) == 0) {


 548         data = inflate_file(fd, &entry, size);
 549     }
 550     close(fd);
 551     return (data);
 552 }
 553 
 554 /*
 555  * Specialized "free" function.
 556  */
 557 void
 558 JLI_FreeManifest()
 559 {
 560     if (manifest)
 561         free(manifest);
 562 }
 563 
 564 /*
 565  * Iterate over the manifest of the specified jar file and invoke the provided
 566  * closure function for each attribute encountered.
 567  *


 569  *    0 Success
 570  *   -1 Unable to open jarfile
 571  *   -2 Error accessing the manifest from within the jarfile (most likely
 572  *      this means a manifest is not present, or it isn't a valid zip/jar file).
 573  */
 574 int
 575 JLI_ManifestIterate(const char *jarfile, attribute_closure ac, void *user_data)
 576 {
 577     int     fd;
 578     zentry  entry;
 579     char    *mp;        /* manifest pointer */
 580     char    *lp;        /* pointer into manifest, updated during iteration */
 581     char    *name;
 582     char    *value;
 583     int     rc;
 584 
 585     if ((fd = open(jarfile, O_RDONLY
 586 #ifdef O_BINARY
 587         | O_BINARY /* use binary mode on windows */
 588 #endif
 589         )) == -1)
 590         return (-1);

 591 
 592     if (rc = find_file(fd, &entry, manifest_name) != 0) {
 593         close(fd);
 594         return (-2);
 595     }
 596 
 597     mp = inflate_file(fd, &entry, NULL);
 598     if (mp == NULL) {
 599         close(fd);
 600         return (-2);
 601     }
 602 
 603     lp = mp;
 604     while ((rc = parse_nv_pair(&lp, &name, &value)) > 0) {
 605         (*ac)(name, value, user_data);
 606     }
 607     free(mp);
 608     close(fd);
 609     return (rc == 0) ? 0 : -2;
 610 }


 473  *    0 Success
 474  *   -1 Unable to open jarfile
 475  *   -2 Error accessing the manifest from within the jarfile (most likely
 476  *      a manifest is not present, or this isn't a valid zip/jar file).
 477  */
 478 int
 479 JLI_ParseManifest(char *jarfile, manifest_info *info)
 480 {
 481     int     fd;
 482     zentry  entry;
 483     char    *lp;
 484     char    *name;
 485     char    *value;
 486     int     rc;
 487     char    *splashscreen_name = NULL;
 488 
 489     if ((fd = open(jarfile, O_RDONLY
 490 #ifdef O_BINARY
 491         | O_BINARY /* use binary mode on windows */
 492 #endif
 493         )) == -1) {
 494         return (-1);
 495     }
 496     info->manifest_version = NULL;
 497     info->main_class = NULL;
 498     info->jre_version = NULL;
 499     info->jre_restrict_search = 0;
 500     info->splashscreen_image_file_name = NULL;
 501     if (rc = find_file(fd, &entry, manifest_name) != 0) {
 502         close(fd);
 503         return (-2);
 504     }
 505     manifest = inflate_file(fd, &entry, NULL);
 506     if (manifest == NULL) {
 507         close(fd);
 508         return (-2);
 509     }
 510     lp = manifest;
 511     while ((rc = parse_nv_pair(&lp, &name, &value)) > 0) {
 512         if (JLI_StrCaseCmp(name, "Manifest-Version") == 0)
 513             info->manifest_version = value;
 514         else if (JLI_StrCaseCmp(name, "Main-Class") == 0)
 515             info->main_class = value;


 522             info->splashscreen_image_file_name = value;
 523         }
 524     }
 525     close(fd);
 526     if (rc == 0)
 527         return (0);
 528     else
 529         return (-2);
 530 }
 531 
 532 /*
 533  * Opens the jar file and unpacks the specified file from its contents.
 534  * Returns NULL on failure.
 535  */
 536 void *
 537 JLI_JarUnpackFile(const char *jarfile, const char *filename, int *size) {
 538     int     fd;
 539     zentry  entry;
 540     void    *data = NULL;
 541 
 542     if ((fd = open(jarfile, O_RDONLY
 543 #ifdef O_BINARY
 544         | O_BINARY /* use binary mode on windows */
 545 #endif
 546         )) == -1) {
 547         return NULL;
 548     }
 549     if (find_file(fd, &entry, filename) == 0) {
 550         data = inflate_file(fd, &entry, size);
 551     }
 552     close(fd);
 553     return (data);
 554 }
 555 
 556 /*
 557  * Specialized "free" function.
 558  */
 559 void
 560 JLI_FreeManifest()
 561 {
 562     if (manifest)
 563         free(manifest);
 564 }
 565 
 566 /*
 567  * Iterate over the manifest of the specified jar file and invoke the provided
 568  * closure function for each attribute encountered.
 569  *


 571  *    0 Success
 572  *   -1 Unable to open jarfile
 573  *   -2 Error accessing the manifest from within the jarfile (most likely
 574  *      this means a manifest is not present, or it isn't a valid zip/jar file).
 575  */
 576 int
 577 JLI_ManifestIterate(const char *jarfile, attribute_closure ac, void *user_data)
 578 {
 579     int     fd;
 580     zentry  entry;
 581     char    *mp;        /* manifest pointer */
 582     char    *lp;        /* pointer into manifest, updated during iteration */
 583     char    *name;
 584     char    *value;
 585     int     rc;
 586 
 587     if ((fd = open(jarfile, O_RDONLY
 588 #ifdef O_BINARY
 589         | O_BINARY /* use binary mode on windows */
 590 #endif
 591         )) == -1) {
 592         return (-1);
 593     }
 594 
 595     if (rc = find_file(fd, &entry, manifest_name) != 0) {
 596         close(fd);
 597         return (-2);
 598     }
 599 
 600     mp = inflate_file(fd, &entry, NULL);
 601     if (mp == NULL) {
 602         close(fd);
 603         return (-2);
 604     }
 605 
 606     lp = mp;
 607     while ((rc = parse_nv_pair(&lp, &name, &value)) > 0) {
 608         (*ac)(name, value, user_data);
 609     }
 610     free(mp);
 611     close(fd);
 612     return (rc == 0) ? 0 : -2;
 613 }