Hi,

I don't know if this is the way doing it correctly.

I am currenty using version 3.0.1 final and I think I found a small bug and
I would like to propose a solution.
It is in org.apache.poi.hslf.HSLFSlideShow.readPictures

When certain pictures are to big, an error (OutOfMemory) is thrown but not
catched. No other pictures are read but it should read the next one.

In attachment you will find the old solution and the new one.

Regards,
Sujith Quintelier
        /**
         * Find and read in pictures contained in this presentation
         */
        private void readPictures() throws IOException {
                byte[] pictstream;

                try {
                        DocumentEntry entry = 
(DocumentEntry)filesystem.getRoot().getEntry("Pictures");
                        pictstream = new byte[entry.getSize()];
                        DocumentInputStream is = 
filesystem.createDocumentInputStream("Pictures");
                        is.read(pictstream);
                } catch (FileNotFoundException e){
                        // Silently catch exceptions if the presentation 
doesn't 
                        //  contain pictures - will use a null set instead
                        return;
                }

        List<PictureData> p = new ArrayList<PictureData>();
        int pos = 0;

                // An empty picture record (length 0) will take up 8 bytes
        while (pos <= (pictstream.length-8)) {
            int offset = pos;

            // Image signature
            int signature = LittleEndian.getUShort(pictstream, pos);
            pos += LittleEndian.SHORT_SIZE;
            // Image type + 0xF018
            int type = LittleEndian.getUShort(pictstream, pos);
            pos += LittleEndian.SHORT_SIZE;
            // Image size (excluding the 8 byte header)
            int imgsize = LittleEndian.getInt(pictstream, pos);
            pos += LittleEndian.INT_SIZE;

                        // The image size must be 0 or greater
                        // (0 is allowed, but odd, since we do wind on by the 
header each
                        //  time, so we won't get stuck)
                        if(imgsize < 0) {
                                throw new CorruptPowerPointFileException("The 
file contains a picture, at position " + p.size() + ", which has a negatively 
sized data length, so we can't trust any of the picture data");
                        }

                        // If they type (including the bonus 0xF018) is 0, skip 
it
                        if(type == 0) {
                                logger.log(POILogger.ERROR, "Problem reading 
picture: Invalid image type 0, on picture with length " + imgsize + ".\nYou 
document will probably become corrupted if you save it!");
                                logger.log(POILogger.ERROR, "" + pos);
                        } else {
          // Copy the data, ready to pass to PictureData
          byte[] imgdata = new byte[imgsize];
          if(imgsize > 0) {
                System.arraycopy(pictstream, pos, imgdata, 0, imgdata.length);
          }       
                                // Build the PictureData object from the data
                                try {
                                        PictureData pict = 
PictureData.create(type - 0xF018);
                                        pict.setRawData(imgdata);
                                        pict.setOffset(offset);
                                        p.add(pict);
                                } catch(IllegalArgumentException e) {
                                        logger.log(POILogger.ERROR, "Problem 
reading picture: " + e + "\nYour document will probably become corrupted if you 
save it!");
                                }
                        }
            
            pos += imgsize;
        }

                _pictures = (PictureData[])p.toArray(new PictureData[p.size()]);
        }
        /**
         * Find and read in pictures contained in this presentation
         */
        private void readPictures() throws IOException {
                byte[] pictstream;

                try {
                        DocumentEntry entry = 
(DocumentEntry)filesystem.getRoot().getEntry("Pictures");
                        pictstream = new byte[entry.getSize()];
                        DocumentInputStream is = 
filesystem.createDocumentInputStream("Pictures");
                        is.read(pictstream);
                } catch (FileNotFoundException e){
                        // Silently catch exceptions if the presentation 
doesn't 
                        //  contain pictures - will use a null set instead
                        return;
                }

        List<PictureData> p = new ArrayList<PictureData>();
        int pos = 0;

                // An empty picture record (length 0) will take up 8 bytes
        while (pos <= (pictstream.length-8)) {
            int offset = pos;

            // Image signature
            int signature = LittleEndian.getUShort(pictstream, pos);
            pos += LittleEndian.SHORT_SIZE;
            // Image type + 0xF018
            int type = LittleEndian.getUShort(pictstream, pos);
            pos += LittleEndian.SHORT_SIZE;
            // Image size (excluding the 8 byte header)
            int imgsize = LittleEndian.getInt(pictstream, pos);
            pos += LittleEndian.INT_SIZE;

                        // The image size must be 0 or greater
                        // (0 is allowed, but odd, since we do wind on by the 
header each
                        //  time, so we won't get stuck)
                        if(imgsize < 0) {
                                throw new CorruptPowerPointFileException("The 
file contains a picture, at position " + p.size() + ", which has a negatively 
sized data length, so we can't trust any of the picture data");
                        }

                        // If they type (including the bonus 0xF018) is 0, skip 
it
                        if(type == 0) {
                                logger.log(POILogger.ERROR, "Problem reading 
picture: Invalid image type 0, on picture with length " + imgsize + ".\nYou 
document will probably become corrupted if you save it!");
                                logger.log(POILogger.ERROR, "" + pos);
                        } else {
                                // Build the PictureData object from the data
                                try {
                            // Copy the data, ready to pass to PictureData
                            byte[] imgdata = new byte[imgsize];
                            if(imgsize > 0) {
                                System.arraycopy(pictstream, pos, imgdata, 0, 
imgdata.length);
                            }       
                                        PictureData pict = 
PictureData.create(type - 0xF018);
                                        pict.setRawData(imgdata);
                                        pict.setOffset(offset);
                                        p.add(pict);
                                } catch(IllegalArgumentException e) {
                                        logger.log(POILogger.ERROR, "Problem 
reading picture: " + e + "\nYour document will probably become corrupted if you 
save it!");
                                } catch(OutOfMemoryError e) {
                                        logger.log(POILogger.ERROR, "Problem 
reading picture: " + e + "\nYour document will probably become corrupted if you 
save it!");
                                }
                        }
            
            pos += imgsize;
        }

                _pictures = (PictureData[])p.toArray(new PictureData[p.size()]);
        }
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to