encode.c 3.27 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 *	PostScript filter functions
 *
 *	Copyright 2003  Huw D M Davies
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
 */
#include <ctype.h>
#include <stdio.h>
#include <string.h>

#include "psdrv.h"
#include "wine/debug.h"

WINE_DEFAULT_DEBUG_CHANNEL(psdrv);

DWORD RLE_encode(BYTE *in_buf, DWORD len, BYTE *out_buf)
{
    BYTE *next_in = in_buf, *next_out = out_buf;
    DWORD rl;

    while(next_in < in_buf + len) { 
        if(next_in + 1 < in_buf + len) {
            if(*next_in == *(next_in + 1)) {
                next_in += 2;
                rl = 2;
                while(next_in < in_buf + len && *next_in == *(next_in - 1) && rl < 128) {
                    next_in++;
                    rl++;
                }
                *next_out++ = 257 - rl;
                *next_out++ = *(next_in - 1);
            } else {
                rl = 0;
                next_out++;
                while(next_in < in_buf + len && rl < 128) {
                    if(next_in + 2 < in_buf + len && *next_in == *(next_in + 1)) {
                        if(rl == 127 || *next_in == *(next_in + 2))
                            break;
                    }
                    *next_out++ = *next_in++;
                    rl++;
                }
                *(next_out - rl - 1) = rl - 1;
            }
        } else { /* special case: begin of run on last byte */
            *next_out++ = 0;
            *next_out++ = *next_in;
            break;
        }
    }
    *next_out++ = 128;
    return next_out - out_buf;
}

DWORD ASCII85_encode(BYTE *in_buf, DWORD len, BYTE *out_buf)
{
    DWORD number;
    BYTE *next_in = in_buf, *next_out = out_buf;
    int i;

    while(next_in + 3 < in_buf + len) {
        number = (next_in[0] << 24) |
                 (next_in[1] << 16) |
                 (next_in[2] <<  8) |
                 (next_in[3]      );
        next_in += 4;
        if(number == 0)
            *next_out++ = 'z';
        else {
            for(i = 4; i >= 0; i--) {
                next_out[i] = number % 85 + '!';
                number /= 85;
            }
            next_out += 5;
        }
    }

    if(next_in != in_buf + len) {
        number = (*next_in++ << 24) & 0xff000000;
        if(next_in < in_buf + len)
            number |= ((*next_in++ << 16) & 0x00ff0000);
        if(next_in < in_buf + len)
            number |= ((*next_in++ <<  8) & 0x0000ff00);

        for(i = len % 4 + 1; i < 5; i++)
            number /= 85;

        for(i = len % 4; i >= 0; i--) {
            next_out[i] = number % 85 + '!';
            number /= 85;
        }
        next_out += len % 4 + 1;
    }

    return next_out - out_buf;    
}