2
Copyright (c) 2006 uim Project http://uim.freedesktop.org/
6
Redistribution and use in source and binary forms, with or
7
without modification, are permitted provided that the
8
following conditions are met:
10
1. Redistributions of source code must retain the above
11
copyright notice, this list of conditions and the
13
2. Redistributions in binary form must reproduce the above
14
copyright notice, this list of conditions and the
15
following disclaimer in the documentation and/or other
16
materials provided with the distribution.
17
3. Neither the name of authors nor the names of its
18
contributors may be used to endorse or promote products
19
derived from this software without specific prior written
22
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
23
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
24
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
27
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
33
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37
#include "helper-message.h"
39
#define helper_encode(src) ((src) == 57 ? 33 : ((src) + 0x23))
40
#define helper_decode(src) ((src) == 33 ? 57 : ((src) - 0x23))
43
/* encode src string to Emacs safe 7bit ASCII string */
45
helper_message_encode(const char *src)
48
unsigned char *p = (unsigned char *)src;
51
if (src) length = strlen(src);
54
outbuf = (char *)malloc((length + 2) / 3 * 4 + 1);
58
for (i = 0; i < length - 2; i += 3) {
59
*ob = (char)helper_encode(*p >> 2);
60
*(ob + 1) = (char)helper_encode(((*p & 0x3) << 4) |
62
*(ob + 2) = (char)helper_encode(((*(p + 1) & 0xf) << 2) |
64
*(ob + 3) = (char)helper_encode(*(p + 2) & 0x3f);
69
if (length % 3 == 2) {
70
*ob = (char)helper_encode(*p >> 2);
71
*(ob + 1) = (char)helper_encode(((*p & 0x3) << 4) |
73
*(ob + 2) = (char)helper_encode((*(p + 1) & 0xf) << 2);
74
*(ob + 3) = (char)helper_encode(0);
76
} else if (length % 3 == 1) {
77
*ob = (char)helper_encode(*p >> 2);
78
*(ob + 1) = (char)helper_encode(((*p & 0x3) << 4));
79
*(ob + 2) = (char)helper_encode(0);
80
*(ob + 3) = (char)helper_encode(0);
90
/* decode src string to 8bit string */
92
helper_message_decode(const char *src)
96
unsigned char *p = (unsigned char *)src;
97
unsigned char p0, p1, p2, p3;
100
if (src) length = strlen(src);
102
outbuf = (char *)malloc(length / 4 * 3 + 1);
106
for (i = 0; i < length; i += 4) {
107
p0 = helper_decode(*p);
108
p1 = helper_decode(*(p + 1));
109
p2 = helper_decode(*(p + 2));
110
p3 = helper_decode(*(p + 3));
112
*ob = (char)((p0 << 2) | (p1 >> 4));
113
*(ob + 1) = (char)((p1 << 4) | (p2 >> 2));
114
*(ob + 2) = (char)((p2 << 6) | p3);
126
helper_send_message(const char *message)
128
char *enc = helper_message_encode(message);
129
a_printf("(h \"%s\") ", enc);