~ubuntu-branches/debian/wheezy/libnjb/wheezy

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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#include "../config.h"
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "libnjb.h"
#include "njbusb.h"
#include "protocol.h"
#include "protocol3.h"
#include "unicode.h"
#include "njb_error.h"
#include "usb_io.h"
#include "ioutil.h"
#include "defs.h"
#include "base.h"

extern int __sub_depth;
int njb_unicode_flag = NJB_UC_8859;
#define MAX_STRING_LENGTH 512

/* This flag determines whether to use ISO 8859-1
 * or unicode UTF-8 for ALL strings */
void njb_set_unicode (int flag)
{
	njb_unicode_flag = flag;
}

/* Gets the length (in characters, not bytes) of a unicode 
 * UCS-2 string, eg a string which physically is 0x00 0x41 0x00 0x00
 * will return a value of 1. */
int ucs2strlen(const unsigned char *unicstr){

	__dsub= "ucs2strlen";

	int length=0;
	int i;

	__enter;

	/* Unicode strings are terminated with 2 * 0x00 */
	for(i=0; (unicstr[i] | unicstr[i+1])!='\0'; i+=2) {
		length++;
	}

	__leave;
	return length;
}

/* This routine returns the length in bytes that this
 * UCS-2 string would occupy if encoded as UTF-8 */
static int ucs2utf8len(const unsigned char *unicstr){
  int length=0;
  int i;
  for(i=0; (unicstr[i] | unicstr[i+1]) != '\0'; i+=2) {
    if (unicstr[i] == 0x00 && unicstr[i+1] < 0x80)
      length++;
    else if (unicstr[i] < 0x08)
      length+=2;
    else
      length+=3;
  }
  return length;
}

/* Create a new, allocated UCS-2 string that is a copy
 * of the parameter */
static unsigned char *ucs2strdup(const unsigned char *unicstr) {
  int length = ucs2strlen(unicstr);
  unsigned char *data;
  
  data= (char *) malloc(length*2+2);
  if ( data == NULL ) {
    return NULL;
  }
  memcpy(data, unicstr, length*2+2);
  return data;
}

/* This function converts an ordinary ISO 8859-1 string
 * to a unicode UTF-8 string */
char *strtoutf8(const unsigned char *str) {  
  unsigned char buffer[MAX_STRING_LENGTH];
  int l=0;
  int i;

  memset(buffer,0,MAX_STRING_LENGTH);
  
  for (i=0;i<strlen(str);i++) {
    if (str[i]<0x80) {
      buffer[l] = str[i];
      l++;
    } else {
      buffer[l] = 0xC0 | (str[i]>>6 & 0x03);
      buffer[l+1] = 0x80 | (str[i] & 0x3F);
      l+=2;
    }
    buffer[l] = 0x00;
  }
  /* The duplicate the string and return it */
  return strdup(buffer);
}

/* This function approximates an ISO 8859-1 string from
 * a UTF-8 string, leaving out untranslatable characters */
char *utf8tostr(const unsigned char *str) {
  unsigned char buffer[MAX_STRING_LENGTH];
  unsigned char *ucs2string;
  int i = 0;
  int l = 0;

  memset(buffer,0,MAX_STRING_LENGTH);
  
  ucs2string = strtoucs2(str);
  if (ucs2string == NULL)
    return NULL;

  for(i=0; (ucs2string[i] | ucs2string[i+1])!='\0'; i+=2) {
    if (ucs2string[i] == '\0') {
      buffer[l] = ucs2string[i+1];
      l++;
    }
  }
  buffer[l] = '\0';

  free(ucs2string);

  /* If there was nothing in this string, return NULL */
  if (l>0 || i == 0)
    return strdup(buffer);
  else
    return NULL;
}

/* Converts a unicode 2-byte string to a common string
 * quick and dirty (japanese unicodes etc, that use all 16 bits
 * will fail miserably) */
char *ucs2tostr(const unsigned char *unicstr){

	__dsub= "ucs2tostr";

	char *data = NULL;
	int i = 0;
	int l = 0;

	__enter;


	/* Real unicode support in UTF8 */
	if (njb_unicode_flag == NJB_UC_UTF8) {
	  int length8 = ucs2utf8len(unicstr);
	  data= (char *) malloc(length8+1);
	  if ( data == NULL ) {
	    NJB_ERROR(EO_NOMEM);
	    __leave;
	    return NULL;
	  }
	  for(l=0;(unicstr[l] | unicstr[l+1])!='\0'; l+=2) {
	    if (unicstr[l] == 0x00 && unicstr[l+1] < 0x80) {
	      data[i]=unicstr[l+1];
	      i++;
	    } else if (unicstr[l] < 0x08) {
	      data[i] = 0xc0 | (unicstr[l]<<2 & 0x1C) | (unicstr[l+1]>>6  & 0x03);
	      data[i+1] = 0x80 | (unicstr[l+1] & 0x3F);
	      i+=2;
	    } else {
	      data[i] = 0xe0 | (unicstr[l]>>4 & 0x0F);
	      data[i+1] = 0x80 | (unicstr[l]<<2 & 0x3C) | (unicstr[l+1]>>6 & 0x03);
	      data[i+2] = 0x80 | (unicstr[l+1] & 0x3F);
	      i+=3;
	    }
	  }
	  /* Terminate string */
	  data[i]=0x00;	  
	} else {
	  /* If we're running in ISO 8859-1 mode, approximate
	   * and concatenate, loosing any chars above 0xff */
	  int length=ucs2strlen(unicstr);

	  data= (char *) malloc(length+1);
	  if ( data == NULL ) {
	    NJB_ERROR(EO_NOMEM);
	    __leave;
	    return NULL;
	  }

	  l = 0;
	  for(i=0;l<length*2;){
	    if (unicstr[l] == 0x00) {
	      data[i]=unicstr[l+1];
	      i++;
	    }
	    l+=2;
	  }
	  /* Terminate string */
	  data[i]=0x00;
	}


	__leave;
	return data;
}

/* Convert a simple ISO 8859-1 or a Unicode
 * UTF8 string to a unicode UCS2 string */
unsigned char *strtoucs2(const unsigned char *str) {

	__dsub= "strtoucs2";

	unsigned char *data = NULL;
	int i=0;
	int l=0;

	__enter;

	/* Real unicode support in UTF8 */
	if (njb_unicode_flag == NJB_UC_UTF8) {
	  unsigned char buffer[MAX_STRING_LENGTH*2];

	  int length=0;
	  int i;

	  for(i=0; str[i] != '\0';) {
	    if (str[i] < 0x80) {
	      buffer[length] = 0x00;
	      buffer[length+1] = str[i];
	      length += 2;
	      i++;
	    } else {
	      unsigned char numbytes = 0;
	      unsigned char lenbyte = 0;
	      
	      /* Read the number of encoded bytes */
	      lenbyte = str[i];
	      while (lenbyte & 0x80) {
		numbytes++;
		lenbyte = lenbyte<<1;
	      }
	      /* UCS-2 can handle no more than 3 UTF-8 encoded bytes */
	      if (numbytes <= 3) {
		if (numbytes == 2 && str[i+1] > 0x80) {
		  /* This character can always be handled correctly */
		  buffer[length] = (str[i]>>3 & 0x07);
		  buffer[length+1] = (str[i]<<6 & 0xC0) | (str[i+1] & 0x3F);
		  i += 2;
		  length += 2;
		} else if (numbytes == 3 && str[i+1] > 0x80 && str[i+2] > 0x80) {
		  buffer[length] = (str[i]<<4 & 0xF0) | (str[i+1]>>2 & 0x0F);
		  buffer[length+1]= (str[i+1]<<6 & 0xC0) | (str[i+2] & 0x3F);
		  i += 3;
		  length += 2;
		} else {
		  /* Abnormal string character, just skip */
		  i += numbytes;
		}
	      } else {
		/* Just skip that character */
		i += numbytes;
	      }
	    }
	  }
	  /* Copy the buffer contents */
	  buffer[length] = 0x00;
	  buffer[length+1] = 0x00;
	  data = ucs2strdup(buffer);
	  if (data == NULL) {
	    NJB_ERROR(EO_NOMEM);
	    __leave;
	    return NULL;
	  }
	} else {
	  /* If we're running in ISO 8859-1 mode, approximate
	   * and concatenate, loosing any chars above 0xff */
	  data= (unsigned char *) malloc(2*strlen(str)+2);
	  if ( data == NULL ) {
	    NJB_ERROR(EO_NOMEM);
	    __leave;
	    return NULL;
	  }
	
	  for(i=0;i<=strlen(str);i++){
	    data[l]=0x00;
	    data[l+1]=str[i];
	    l+=2;
	  }
	}

	__leave;
	return data;
}