-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheplkup_utils.c
191 lines (167 loc) · 4.9 KB
/
eplkup_utils.c
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
/*------------------------------------------------------------------------
-- Copyright (C) 2011-2014 Christopher Brochtrup
--
-- This file is part of eplkup.
--
-- eplkup is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
--
-- eplkup is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with eplkup. If not, see <http://www.gnu.org/licenses/>.
--
------------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <errno.h>
#include "string.h"
#include "iconv.h"
#include "eplkup_utils.h"
/*------------------------------------------------------------------------
-- Name: is_hex
--
-- Description:
-- Is the provided character a hex character?
--
-- Parameters:
-- (IN) c - The character to check.
--
-- Returns:
-- 0 = Character is not a hex character.
-- 1 = Character is a hex character.
--
------------------------------------------------------------------------*/
int is_hex(char c)
{
return (((c >= '0') && (c <= '9')) || ((c >= 'A') && (c <= 'F')) || ((c >= 'a') && (c <= 'f')));
} /* is_hex */
/*------------------------------------------------------------------------
-- Name: str_eq_i
--
-- Description:
-- Determine if 2 strings are equal (case insentive)
--
-- Parameters:
-- (IN) a - The first string to compare.
-- (IN) b - The second string to compare.
--
-- Returns:
-- 0 = Strings are not equal.
-- 1 = Strings are equal.
--
------------------------------------------------------------------------*/
int str_eq_i(const char *a, const char *b)
{
int i = 0;
int equal = 1;
int len_a = strlen(a);
int len_b = strlen(b);
if(len_a == len_b)
{
for(i = 0; i < len_a; i++)
{
if(tolower(a[i]) != tolower(b[i]))
{
equal = 0;
break;
}
}
}
else
{
equal = 0;
}
return equal;
} /* str_eq_i */
/*------------------------------------------------------------------------
-- Name: strncpy_len
--
-- Description:
-- Copy the specified number of characters from one buffer to another.
--
-- Parameters:
-- (OUT) dst - The destination buffer.
-- (IN) src - The source buffer.
-- (IN) len - The number of characters to copy (includes null terminator).
--
-- Returns:
-- The destination buffer.
--
------------------------------------------------------------------------*/
char *strncpy_len(char *dst, char *src, int len)
{
dst[0] = '\0';
strncat(dst, src, len);
return dst;
} /* strncpy_len */
/*------------------------------------------------------------------------
-- Name: convert_encoding
--
-- Description:
-- Convert string from one encoding to another encoding.
--
-- Parameters:
-- (OUT) dest - Destination string in the desired encoding.
-- (IN) max_dest - Maximum length of the destination string.
-- (IN/OUT) src - Source string in the original encoding.
-- (IN) max_src - Length of the source string.
-- (IN) dest_enc - The encoding of the "dest" string. Possible
-- encodings: http://www.gnu.org/software/libiconv/
-- (IN) src_enc - The encoding of the "src" string.
--
-- Returns:
-- Destination string in the desired encoding.
--
------------------------------------------------------------------------*/
char *convert_encoding(char *dest, int max_dest, char *src, int max_src, const char *dest_enc, const char *src_enc)
{
iconv_t cd;
size_t in_left = max_src;
char *out_buf = dest;
size_t out_left = max_dest;
size_t status_conv = 0;
cd = iconv_open(dest_enc, src_enc);
if(cd == (iconv_t)-1)
{
if(errno == EINVAL)
{
fprintf(stderr, "Error: Conversion from '%s' to '%s' not available.", src_enc, dest_enc);
}
else
{
fprintf(stderr, "Error: An unknown error occured with iconv_open.");
}
return NULL;
}
status_conv = iconv(cd, &src, &in_left, &out_buf, &out_left);
if(status_conv == (size_t)-1)
{
if(errno == EINVAL)
{
fprintf(stderr, "Warning: An incomplete multibyte sequence has been encountered in the input.");
}
else if(errno == E2BIG)
{
fprintf(stderr, "Error: dest is too small.");
return NULL;
}
else if(errno == EILSEQ)
{
fprintf(stderr, "Error: An invalid multibyte sequence has been encountered in the input.");
return NULL;
}
}
dest[max_dest - out_left] = '\0';
if(iconv_close(cd) != 0)
{
fprintf(stderr, "Warning: An unknown error occured with iconv_close.");
}
return dest;
} /* convert_encoding */