libpurple/protocols/gg/lib/encoding.c

changeset 35630
8e5d0d726b09
parent 35620
fb20cfee648a
parent 35627
fd11790cc4d6
equal deleted inserted replaced
35629:5aecb81f23ab 35630:8e5d0d726b09
86 if ((dst_length != -1) && (len > dst_length)) 86 if ((dst_length != -1) && (len > dst_length))
87 len = dst_length; 87 len = dst_length;
88 88
89 result = malloc(len + 1); 89 result = malloc(len + 1);
90 90
91 if (result == NULL) 91 if (result == NULL)
92 return NULL; 92 return NULL;
93 93
94 for (i = 0, j = 0; (src[i] != 0) && (i < src_length) && (j < len); i++) { 94 for (i = 0, j = 0; (src[i] != 0) && (i < src_length) && (j < len); i++) {
95 uint16_t uc; 95 uint16_t uc;
96 96
97 if ((unsigned char) src[i] < 0x80) 97 if ((unsigned char) src[i] < 0x80)
98 uc = (unsigned char) src[i]; 98 uc = (unsigned char) src[i];
99 else 99 else
100 uc = table_cp1250[(unsigned char) src[i] - 128]; 100 uc = table_cp1250[(unsigned char) src[i] - 128];
101 101
102 if (uc < 0x80) 102 if (uc < 0x80)
103 result[j++] = (char) uc; 103 result[j++] = (char) uc;
104 else if (uc < 0x800) { 104 else if (uc < 0x800) {
105 if (j + 1 > len) 105 if (j + 1 > len)
106 break; 106 break;
107 result[j++] = 0xc0 | ((uc >> 6) & 0x1f); 107 result[j++] = 0xc0 | ((uc >> 6) & 0x1f);
148 if (result == NULL) 148 if (result == NULL)
149 return NULL; 149 return NULL;
150 150
151 for (i = 0, j = 0; (src[i] != 0) && (i < src_length) && (j < len); i++) { 151 for (i = 0, j = 0; (src[i] != 0) && (i < src_length) && (j < len); i++) {
152 if ((unsigned char) src[i] >= 0xf5) { 152 if ((unsigned char) src[i] >= 0xf5) {
153 if (uc_left != 0) 153 if (uc_left != 0)
154 result[j++] = '?'; 154 result[j++] = '?';
155 /* Restricted sequences */ 155 /* Restricted sequences */
156 result[j++] = '?'; 156 result[j++] = '?';
157 uc_left = 0; 157 uc_left = 0;
158 } else if ((src[i] & 0xf8) == 0xf0) { 158 } else if ((src[i] & 0xf8) == 0xf0) {
159 if (uc_left != 0) 159 if (uc_left != 0)
160 result[j++] = '?'; 160 result[j++] = '?';
161 uc = src[i] & 0x07; 161 uc = src[i] & 0x07;
162 uc_left = 3; 162 uc_left = 3;
163 uc_min = 0x10000; 163 uc_min = 0x10000;
164 } else if ((src[i] & 0xf0) == 0xe0) { 164 } else if ((src[i] & 0xf0) == 0xe0) {
165 if (uc_left != 0) 165 if (uc_left != 0)
166 result[j++] = '?'; 166 result[j++] = '?';
167 uc = src[i] & 0x0f; 167 uc = src[i] & 0x0f;
168 uc_left = 2; 168 uc_left = 2;
169 uc_min = 0x800; 169 uc_min = 0x800;
170 } else if ((src[i] & 0xe0) == 0xc0) { 170 } else if ((src[i] & 0xe0) == 0xc0) {
171 if (uc_left != 0) 171 if (uc_left != 0)
172 result[j++] = '?'; 172 result[j++] = '?';
173 uc = src[i] & 0x1f; 173 uc = src[i] & 0x1f;
174 uc_left = 1; 174 uc_left = 1;
175 uc_min = 0x80; 175 uc_min = 0x80;
176 } else if ((src[i] & 0xc0) == 0x80) { 176 } else if ((src[i] & 0xc0) == 0x80) {
223 * \param src_length Długość ciągu źródłowego w bajtach (jeśli -1, zostanie obliczona na podstawie zawartości \p src). 223 * \param src_length Długość ciągu źródłowego w bajtach (jeśli -1, zostanie obliczona na podstawie zawartości \p src).
224 * \param dst_length Długość ciągu docelowego w bajtach (jeśli -1, nieograniczona). 224 * \param dst_length Długość ciągu docelowego w bajtach (jeśli -1, nieograniczona).
225 * 225 *
226 * \return Zaalokowany bufor z tekstem w kodowaniu docelowym. 226 * \return Zaalokowany bufor z tekstem w kodowaniu docelowym.
227 */ 227 */
228 char *gg_encoding_convert(const char *src, gg_encoding_t src_encoding, gg_encoding_t dst_encoding, int src_length, int dst_length) 228 char *gg_encoding_convert(const char *src, gg_encoding_t src_encoding,
229 gg_encoding_t dst_encoding, int src_length, int dst_length)
229 { 230 {
230 char *result; 231 char *result;
231 232
232 if (src == NULL) { 233 if (src == NULL) {
233 errno = EINVAL; 234 errno = EINVAL;
267 return gg_encoding_convert_cp1250_utf8(src, src_length, dst_length); 268 return gg_encoding_convert_cp1250_utf8(src, src_length, dst_length);
268 269
269 errno = EINVAL; 270 errno = EINVAL;
270 return NULL; 271 return NULL;
271 } 272 }
272

mercurial