~ubuntu-branches/ubuntu/hardy/uim/hardy

« back to all changes in this revision

Viewing changes to emacs/helper-message.c

  • Committer: Bazaar Package Importer
  • Author(s): Steinar H. Gunderson
  • Date: 2006-07-06 22:17:24 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20060706221724-6sobw1fcsxj647hp
Tags: 1:1.1.0-1.2
* Non-maintainer upload.
* Added -Wno-cast-align to CFLAGS, as per the RM's recommendations:

  < vorlon> Sesse: -Wno-cast-align and to hell with it :P

  Really fixes FTBFS. (Really Closes: #375081)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  Copyright (c) 2006 uim Project http://uim.freedesktop.org/
 
3
 
 
4
  All rights reserved.
 
5
 
 
6
  Redistribution and use in source and binary forms, with or
 
7
  without modification, are permitted provided that the
 
8
  following conditions are met:
 
9
 
 
10
  1. Redistributions of source code must retain the above
 
11
     copyright notice, this list of conditions and the
 
12
     following disclaimer.
 
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
 
20
     permission.
 
21
 
 
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.
 
35
*/
 
36
 
 
37
#include "helper-message.h"
 
38
 
 
39
#define helper_encode(src) ((src) == 57 ? 33 : ((src) + 0x23))
 
40
#define helper_decode(src) ((src) == 33 ? 57 : ((src) - 0x23))
 
41
 
 
42
 
 
43
/* encode src string to Emacs safe 7bit ASCII string */
 
44
char *
 
45
helper_message_encode(const char *src)
 
46
{
 
47
  int i, length = 0;
 
48
  unsigned char *p = (unsigned char *)src;
 
49
  char *outbuf, *ob;
 
50
  
 
51
  if (src) length = strlen(src);
 
52
 
 
53
  /* 24bit -> 32bit */
 
54
  outbuf = (char *)malloc((length + 2) / 3 * 4 + 1);
 
55
 
 
56
  ob = outbuf;
 
57
 
 
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) |
 
61
                                                                        (*(p + 1) >> 4));
 
62
        *(ob + 2) = (char)helper_encode(((*(p + 1) & 0xf) << 2) |
 
63
                                                                        (*(p + 2) >> 6));
 
64
        *(ob + 3) = (char)helper_encode(*(p + 2) & 0x3f);
 
65
        ob += 4;
 
66
        p += 3;
 
67
  }
 
68
 
 
69
  if (length % 3 == 2) {
 
70
        *ob = (char)helper_encode(*p >> 2);
 
71
        *(ob + 1) = (char)helper_encode(((*p & 0x3) << 4) |
 
72
                                                                        (*(p + 1) >> 4));
 
73
        *(ob + 2) = (char)helper_encode((*(p + 1) & 0xf) << 2);
 
74
        *(ob + 3) = (char)helper_encode(0);
 
75
        ob += 4;
 
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);
 
81
        ob += 4;
 
82
  }
 
83
 
 
84
  *ob = '\0';
 
85
 
 
86
  return outbuf;
 
87
}
 
88
 
 
89
 
 
90
/* decode src string to 8bit string */
 
91
char *
 
92
helper_message_decode(const char *src)
 
93
{
 
94
  int i, length = 0;
 
95
 
 
96
  unsigned char *p = (unsigned char *)src;
 
97
  unsigned char p0, p1, p2, p3;
 
98
  char *outbuf, *ob;
 
99
 
 
100
  if (src) length = strlen(src);
 
101
  
 
102
  outbuf = (char *)malloc(length / 4 * 3 + 1);
 
103
 
 
104
  ob = outbuf;
 
105
 
 
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));
 
111
 
 
112
        *ob = (char)((p0 << 2) | (p1 >> 4));
 
113
        *(ob + 1) = (char)((p1 << 4) | (p2 >> 2));
 
114
        *(ob + 2) = (char)((p2 << 6) | p3);
 
115
        ob += 3;
 
116
        p += 4;
 
117
  }
 
118
 
 
119
  *ob = '\0';
 
120
 
 
121
  return outbuf;
 
122
}
 
123
 
 
124
 
 
125
void
 
126
helper_send_message(const char *message)
 
127
{
 
128
  char *enc = helper_message_encode(message);
 
129
  a_printf("(h \"%s\") ", enc);
 
130
  free(enc);
 
131
}