/* (C) 2003-2007 Willem Jan Hengeveld <itsme@xs4all.nl>
 * Web: http://www.xs4all.nl/~itsme/
 *      http://wiki.xda-developers.com/
 *
 * $Id: tounicode.c 1502 2007-04-15 07:54:20Z itsme $
 */
#include <stdio.h>

int convert_to_unicode(unsigned char *inbuf, unsigned short *outbuf, int maxlen)
{
    int i;
    unsigned char c;
    for (i=0 ; i<maxlen ; i++)
    {
        c= inbuf[i];
        if (c==13 || c==10)
            break;
        outbuf[i]= c;
    }
    outbuf[i++]= 13;
    outbuf[i++]= 10;

    return i;
}
int main(int argc, char **argv)
{
    unsigned char inbuf[16384];
    unsigned short outbuf[16384+4];
    FILE *fin= NULL;
    FILE *fout= NULL;
    int outlen;

    if (argc!=3) { printf("Usage: tounicode infile outfile\n"); return 1; }

    fin= fopen(argv[1], "r"); if (fin==NULL) { perror(argv[1]); return 1; }
    fout= fopen(argv[2], "w+b"); if (fout==NULL) { perror(argv[2]); return 1; }

    while (!feof(fin))
    {
        if (NULL==fgets(inbuf, 16384, fin))
            break;

        outlen= convert_to_unicode(inbuf, outbuf, 16384);

        if (1 != fwrite(outbuf, 2*outlen, 1, fout))
        {
            perror("fwrite");
            break;
        }
    }
    fclose(fin);
    fclose(fout);
    return 0;
}
