
package org.zaproxy.zap.extension.fuzz.payloads.processor;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import org.apache.log4j.Logger;
import org.parosproxy.paros.extension.encoder.Base64;
import org.zaproxy.zap.extension.fuzz.payloads.DefaultPayload;

public class Base64DecodeProcessor extends AbstractCharsetProcessor<DefaultPayload>
        implements DefaultPayloadProcessor {

    private static final Logger LOGGER = Logger.getLogger(Base64DecodeProcessor.class);

    public Base64DecodeProcessor() {
        super();
    }

    public Base64DecodeProcessor(String charsetName) {
        super(charsetName);
    }

    public Base64DecodeProcessor(Charset charset) {
        super(charset);
    }

    @Override
    public DefaultPayload process(DefaultPayload payload) {
        try {
            payload.setValue(
                    new String(
                            Base64.decode(payload.getValue(), Base64.NO_OPTIONS),
                            getCharsetName()));
        } catch (UnsupportedEncodingException ignore) {
            // Shouldn't happen, the encoding was already validated.
        } catch (IOException e) {
            LOGGER.warn("An error occurred while encoding the payload: " + e.getMessage());
        }
        return payload;
    }

    @Override
    public Base64DecodeProcessor copy() {
        return this;
    }
}
