afpdev / alpheusafpparser

Library & parser for IBM Advanced Function Presentation (AFP) document/print stream format
GNU General Public License v3.0
16 stars 11 forks source link

BRS parsing failed #3

Closed RaphC closed 8 years ago

RaphC commented 8 years ago

Hi,

Thanks for your library which is a great AFP parser. I'm facing an issue about BRS parsing. I got an infinite loop certainly due to reserved offset (8-9) not handled. If I look at BDT which have a similar SF structure, I notice that reserved bites are ignored during the computation of triplets.

I thins the BRS_BeginResource should look like :


public class BRS_BeginResource extends StructuredFieldBaseName {
    byte[] reserved8_9 = {0x00,0x00};
    protected List triplets;

```
@Override
public void decodeAFP(byte[] sfData, int offset, int length, AFPParserConfiguration config) throws AFPParserException{
    super.decodeAFP(sfData, offset, length, config);

    int actualLength = getActualLength(sfData, offset, length);
    if(actualLength>8){
        reserved8_9 = new byte[]{sfData[offset +8], sfData[offset +9]};
    }else{
        reserved8_9 = null;
    }
    if(actualLength>10){
        triplets = TripletParser.parseTriplets(sfData, offset +10, actualLength -10, config);
    }else{
        triplets = null;
    }
}

@Override
public void writeAFP(OutputStream os, AFPParserConfiguration config) throws IOException{
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    baos.write(UtilCharacterEncoding.stringToByteArray(name, config.getAfpCharSet(), 8, Constants.EBCDIC_ID_FILLER));
    if(reserved8_9!=null) baos.write(reserved8_9);
    if(triplets!=null){
        for(Triplet triplet : triplets){
            triplet.writeAFP(baos, config);
        }
    }
    writeFullStructuredField(os, baos.toByteArray());
}   

public final List getTriplets() {
    return triplets;
}

public final void setTriplets(List triplets) {
    this.triplets = triplets;
}

public final void addTriplet(Triplet triplet){
    if(triplet==null) return;
    if(triplets==null) triplets = new ArrayList();
    triplets.add(triplet);
}

public final void removeTriplet(Triplet triplet){
    if(triplet==null || triplets==null) return;
    triplets.remove(triplet);
}
```

}
Regards
michaelknigge commented 8 years ago

Grab my fixes (see the pull request).... They fix the bug. Or checkout from my fork: https://github.com/michaelknigge/alpheusafpparser

Ah... but I've noticed my fix does not handle writeAFP()... I've just forgot about that... I'll fix this soon, too.

RaphC commented 8 years ago

Thanks @michaelknigge

afpdev commented 8 years ago

Hi RaphC & michaelknigge, thank you for your contributions. I merged the bugfixes from michael. For BRS I decided to go with RaphC's proposal and re-implemented BRS accordingly so the class reflects BRS's implementation correctly.

michaelknigge commented 8 years ago

Hi,

sorry, but I have to disagree. IMHO the proposal of RalphC is bad (sorry).... Let me explain why:

Maybe tomorrow I'll find some time to throw an eye on it and fix (some of) the issues.

Bye, Michael