summaryrefslogtreecommitdiffstats
path: root/mbbsd/convert.c
blob: 611e54ac956577a9e85654aba56aed933663cb5b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/* $Id$ */
#include "bbs.h"

#ifdef CONVERT

#ifdef CONVERT
extern void big2gb_init(void*);
extern void gb2big_init(void*);
extern void big2uni_init(void*);
extern void uni2big_init(void*);
#endif

extern unsigned char *gb2big(unsigned char *, int *, int);
extern unsigned char *big2gb(unsigned char *, int *, int);
extern unsigned char *utf8_uni(unsigned char *, int *, int);
extern unsigned char *uni_utf8(unsigned char *, int *, int);
extern unsigned char *uni2big(unsigned char *, int *, int);
extern unsigned char *big2uni(unsigned char *, int *, int);

static ssize_t 
gb_input(void *buf, ssize_t icount)
{
    /* is sizeof(ssize_t) == sizeof(int)? not sure */
    int ic = (int) icount;
    gb2big((unsigned char *)buf, &ic, 0);
    return (ssize_t)ic;
}

static ssize_t 
gb_read(int fd, void *buf, size_t count)
{
    ssize_t icount = read(fd, buf, count);
    if (icount > 0)
        icount = gb_input(buf, icount);
    return icount;
}

static ssize_t 
gb_write(int fd, void *buf, size_t count)
{
    int     icount = (int)count;
    big2gb((unsigned char *)buf, &icount, 0);
    if(icount > 0)
    return write(fd, buf, (size_t)icount);
    else
    return count; /* fake */
}

static ssize_t 
utf8_input  (void *buf, ssize_t icount) 
{
    /* is sizeof(ssize_t) == sizeof(int)? not sure */
    int ic = (int) icount;
    utf8_uni(buf, &ic, 0);
    uni2big(buf, &ic, 0);
    return (ssize_t)ic;
}

static ssize_t 
utf8_read(int fd, void *buf, size_t count)
{
    ssize_t icount = read(fd, buf, count);
    if (icount > 0)
        icount = utf8_input(buf, icount);
    return icount;
}

static ssize_t 
utf8_write(int fd, void *buf, size_t count)
{
    int     icount = (int)count;
    static unsigned char *mybuf = NULL;
    static int   cmybuf = 0;

    /* utf8 output is a special case because 
     * we need larger buffer which can be 
     * tripple or more in size.
     * Current implementation uses 128 for each block.
     */

    if(cmybuf < count * 4) {
    cmybuf = (count*4+0x80) & (~0x7f) ;
    mybuf = (unsigned char*) realloc (mybuf, cmybuf);
    }
    memcpy(mybuf, buf, count);
    big2uni(mybuf, &icount, 0);
    uni_utf8(mybuf, &icount, 0);
    if(icount > 0)
    return write(fd, mybuf, (size_t)icount);
    else
    return count; /* fake */
}

static ssize_t 
norm_input(void *buf, ssize_t icount)
{
    return icount;
}

/* global function pointers */
read_write_type write_type = (read_write_type)write;
read_write_type read_type = read;
convert_type    input_type = norm_input;

// enable this in case some day we want to detect
// current type. but right now disable for less memory cost
// int      bbs_convert_type = CONV_NORMAL;

void set_converting_type(int which)
{
    if (which == CONV_NORMAL) {
    read_type = read;
    write_type = (read_write_type)write;
    /* for speed up, NULL is better.. */
    input_type = NULL; /* norm_input; */
    }
    else if (which == CONV_GB) {
    read_type = gb_read;
    write_type = gb_write;
    input_type = gb_input;
    }
    else if (which == CONV_UTF8) {
    read_type = utf8_read;
    write_type = utf8_write;
    input_type = utf8_input;
    }
    // bbs_convert_type = which;
}

void init_convert()
{
    big2gb_init(NULL);
    gb2big_init(NULL);
    big2uni_init(NULL);
    uni2big_init(NULL);
}

#endif