~ubuntu-branches/ubuntu/trusty/openjade1.3/trusty

« back to all changes in this revision

Viewing changes to lib/EUCJPCodingSystem.cxx

  • Committer: Bazaar Package Importer
  • Author(s): Yann Dirson
  • Date: 2002-04-09 00:01:50 UTC
  • Revision ID: james.westby@ubuntu.com-20020409000150-r9rkyalxlhvf9ba3
Tags: upstream-1.3.1
ImportĀ upstreamĀ versionĀ 1.3.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (c) 1994 James Clark
 
2
// See the file COPYING for copying permission.
 
3
 
 
4
#include "splib.h"
 
5
 
 
6
#ifdef SP_MULTI_BYTE
 
7
 
 
8
#include "EUCJPCodingSystem.h"
 
9
 
 
10
#ifdef SP_NAMESPACE
 
11
namespace SP_NAMESPACE {
 
12
#endif
 
13
 
 
14
class EUCJPDecoder : public Decoder {
 
15
public:
 
16
  EUCJPDecoder() { }
 
17
  size_t decode(Char *, const char *, size_t, const char **);
 
18
private:
 
19
};
 
20
 
 
21
class EUCJPEncoder : public Encoder {
 
22
public:
 
23
  EUCJPEncoder() { }
 
24
  void output(const Char *, size_t, OutputByteStream *);
 
25
};
 
26
 
 
27
Decoder *EUCJPCodingSystem::makeDecoder() const
 
28
{
 
29
  return new EUCJPDecoder;
 
30
}
 
31
 
 
32
Encoder *EUCJPCodingSystem::makeEncoder() const
 
33
{
 
34
  return new EUCJPEncoder;
 
35
}
 
36
 
 
37
size_t EUCJPDecoder::decode(Char *to, const char *s,
 
38
                            size_t slen, const char **rest)
 
39
{
 
40
  Char *start = to;
 
41
  const unsigned char *us = (const unsigned char *)s;
 
42
  while (slen > 0) {
 
43
    if (!(*us & 0x80)) {
 
44
      // G0
 
45
      *to++ = *us++;
 
46
      slen--;
 
47
    }
 
48
    else if (*us == 0x8e) {
 
49
      // G2
 
50
      if (slen < 2)
 
51
        break;
 
52
      slen -= 2;
 
53
      ++us;
 
54
      *to++ = *us++ | 0x80;
 
55
    }
 
56
    else if (*us == 0x8f) {
 
57
      // G3
 
58
      if (slen < 3)
 
59
        break;
 
60
      slen -= 3;
 
61
      ++us;
 
62
      unsigned short n = (*us++ | 0x80) << 8;
 
63
      n |= (*us++ & ~0x80);
 
64
      *to++ = n;
 
65
    }
 
66
    else {
 
67
      // G1
 
68
      if (slen < 2)
 
69
        break;
 
70
      slen -= 2;
 
71
      unsigned short n = *us++ << 8;
 
72
      n |= (*us++ | 0x80);
 
73
      *to++ = n;
 
74
    }
 
75
  }
 
76
  *rest = (const char *)us;
 
77
  return to - start;
 
78
}
 
79
 
 
80
 
 
81
void EUCJPEncoder::output(const Char *s, size_t n, OutputByteStream *sb)
 
82
{
 
83
  for (; n > 0; s++, n--) {
 
84
    Char c = *s;
 
85
    unsigned short mask = (unsigned short)(c & 0x8080);
 
86
    if (mask == 0)
 
87
      sb->sputc((unsigned char)(c & 0xff));
 
88
    else if (mask == 0x8080) {
 
89
      sb->sputc((unsigned char)((c >> 8) & 0xff));
 
90
      sb->sputc((unsigned char)(c & 0xff));
 
91
    }
 
92
    else if (mask == 0x0080) {
 
93
      sb->sputc((unsigned char)0x8e);
 
94
      sb->sputc((unsigned char)(c & 0xff));
 
95
    }
 
96
    else {
 
97
      // mask == 0x8000
 
98
      sb->sputc((unsigned char)0x8f);
 
99
      sb->sputc((unsigned char)((c >> 8) & 0xff));
 
100
      sb->sputc((unsigned char)(c & 0x7f));
 
101
    }
 
102
  }
 
103
}
 
104
 
 
105
#ifdef SP_NAMESPACE
 
106
}
 
107
#endif
 
108
 
 
109
#else /* not SP_MULTI_BYTE */
 
110
 
 
111
#ifndef __GNUG__
 
112
static char non_empty_translation_unit; // sigh
 
113
#endif
 
114
 
 
115
#endif /* not SP_MULTI_BYTE */