File: EUCJPCodingSystem.cxx

package info (click to toggle)
opensp 1.5.2-13
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye, buster, stretch
  • size: 8,932 kB
  • ctags: 10,036
  • sloc: cpp: 65,784; ansic: 17,124; sh: 11,503; xml: 2,704; makefile: 926; perl: 561; yacc: 288; sed: 16
file content (115 lines) | stat: -rw-r--r-- 2,280 bytes parent folder | download | duplicates (22)
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
// Copyright (c) 1994 James Clark
// See the file COPYING for copying permission.

#include "splib.h"

#ifdef SP_MULTI_BYTE

#include "EUCJPCodingSystem.h"

#ifdef SP_NAMESPACE
namespace SP_NAMESPACE {
#endif

class EUCJPDecoder : public Decoder {
public:
  EUCJPDecoder() { }
  size_t decode(Char *, const char *, size_t, const char **);
private:
};

class EUCJPEncoder : public Encoder {
public:
  EUCJPEncoder() { }
  void output(const Char *, size_t, OutputByteStream *);
};

Decoder *EUCJPCodingSystem::makeDecoder() const
{
  return new EUCJPDecoder;
}

Encoder *EUCJPCodingSystem::makeEncoder() const
{
  return new EUCJPEncoder;
}

size_t EUCJPDecoder::decode(Char *to, const char *s,
			    size_t slen, const char **rest)
{
  Char *start = to;
  const unsigned char *us = (const unsigned char *)s;
  while (slen > 0) {
    if (!(*us & 0x80)) {
      // G0
      *to++ = *us++;
      slen--;
    }
    else if (*us == 0x8e) {
      // G2
      if (slen < 2)
	break;
      slen -= 2;
      ++us;
      *to++ = *us++ | 0x80;
    }
    else if (*us == 0x8f) {
      // G3
      if (slen < 3)
	break;
      slen -= 3;
      ++us;
      unsigned short n = (*us++ | 0x80) << 8;
      n |= (*us++ & ~0x80);
      *to++ = n;
    }
    else {
      // G1
      if (slen < 2)
	break;
      slen -= 2;
      unsigned short n = *us++ << 8;
      n |= (*us++ | 0x80);
      *to++ = n;
    }
  }
  *rest = (const char *)us;
  return to - start;
}


void EUCJPEncoder::output(const Char *s, size_t n, OutputByteStream *sb)
{
  for (; n > 0; s++, n--) {
    Char c = *s;
    unsigned short mask = (unsigned short)(c & 0x8080);
    if (mask == 0)
      sb->sputc((unsigned char)(c & 0xff));
    else if (mask == 0x8080) {
      sb->sputc((unsigned char)((c >> 8) & 0xff));
      sb->sputc((unsigned char)(c & 0xff));
    }
    else if (mask == 0x0080) {
      sb->sputc((unsigned char)0x8e);
      sb->sputc((unsigned char)(c & 0xff));
    }
    else {
      // mask == 0x8000
      sb->sputc((unsigned char)0x8f);
      sb->sputc((unsigned char)((c >> 8) & 0xff));
      sb->sputc((unsigned char)(c & 0x7f));
    }
  }
}

#ifdef SP_NAMESPACE
}
#endif

#else /* not SP_MULTI_BYTE */

#ifndef __GNUG__
static char non_empty_translation_unit;	// sigh
#endif

#endif /* not SP_MULTI_BYTE */