Mercurial > pidgin
comparison libpurple/protocols/qq/utils.c @ 24146:ce94189f15ad
Flos Lonicerae <lonicerae(at)gmail.com>
* Merge lonicerae and ccpaging into trunk
author | SHiNE CsyFeK <csyfek@gmail.com> |
---|---|
date | Wed, 22 Oct 2008 14:52:26 +0000 |
parents | ec3f7d3e0445 |
children | 119d85c40d75 |
comparison
equal
deleted
inserted
replaced
24145:ec3f7d3e0445 | 24146:ce94189f15ad |
---|---|
102 * return gchar* array (needs to be freed by g_strfreev later), or NULL */ | 102 * return gchar* array (needs to be freed by g_strfreev later), or NULL */ |
103 gchar **split_data(guint8 *data, gint len, const gchar *delimit, gint expected_fields) | 103 gchar **split_data(guint8 *data, gint len, const gchar *delimit, gint expected_fields) |
104 { | 104 { |
105 guint8 *input; | 105 guint8 *input; |
106 gchar **segments; | 106 gchar **segments; |
107 gint i, j; | 107 gint count, j; |
108 | 108 |
109 g_return_val_if_fail(data != NULL && len != 0 && delimit != 0, NULL); | 109 g_return_val_if_fail(data != NULL && len != 0 && delimit != 0, NULL); |
110 | 110 |
111 /* as the last field would be string, but data is not ended with 0x00 | 111 /* as the last field would be string, but data is not ended with 0x00 |
112 * we have to duplicate the data and append a 0x00 at the end */ | 112 * we have to duplicate the data and append a 0x00 at the end */ |
116 | 116 |
117 segments = g_strsplit_set((gchar *) input, delimit, 0); | 117 segments = g_strsplit_set((gchar *) input, delimit, 0); |
118 if (expected_fields <= 0) | 118 if (expected_fields <= 0) |
119 return segments; | 119 return segments; |
120 | 120 |
121 for (i = 0; segments[i] != NULL; i++) {; | 121 for (count = 0; segments[count] != NULL; count++) {; |
122 } | 122 } |
123 if (i < expected_fields) { /* not enough fields */ | 123 if (count < expected_fields) { /* not enough fields */ |
124 purple_debug_error("QQ", "Invalid data, expect %d fields, found only %d, discard\n", | 124 purple_debug_error("QQ", "Less fields %d then %d\n", count, expected_fields); |
125 expected_fields, i); | |
126 g_strfreev(segments); | |
127 return NULL; | 125 return NULL; |
128 } else if (i > expected_fields) { /* more fields, OK */ | 126 } else if (count > expected_fields) { /* more fields, OK */ |
129 purple_debug_warning("QQ", "Dangerous data, expect %d fields, found %d, return all\n", | 127 purple_debug_warning("QQ", "More fields %d than %d\n", count, expected_fields); |
130 expected_fields, i); | |
131 /* free up those not used */ | 128 /* free up those not used */ |
132 for (j = expected_fields; j < i; j++) { | 129 for (j = expected_fields; j < count; j++) { |
133 purple_debug_warning("QQ", "field[%d] is %s\n", j, segments[j]); | 130 purple_debug_warning("QQ", "field[%d] is %s\n", j, segments[j]); |
134 g_free(segments[j]); | 131 g_free(segments[j]); |
135 } | 132 } |
136 | |
137 segments[expected_fields] = NULL; | 133 segments[expected_fields] = NULL; |
138 } | 134 } |
139 | 135 |
140 return segments; | 136 return segments; |
141 } | 137 } |
195 /* Sample: (1234567)*/ | 191 /* Sample: (1234567)*/ |
196 start = strchr(name, '('); | 192 start = strchr(name, '('); |
197 g_return_val_if_fail(start != NULL, NULL); | 193 g_return_val_if_fail(start != NULL, NULL); |
198 end = strchr(start, ')'); | 194 end = strchr(start, ')'); |
199 g_return_val_if_fail(end != NULL && (end - start) > 1, NULL); | 195 g_return_val_if_fail(end != NULL && (end - start) > 1, NULL); |
200 | 196 |
201 ret = g_strndup(start + 1, end - start - 1); | 197 ret = g_strndup(start + 1, end - start - 1); |
202 | 198 |
203 return ret; | 199 return ret; |
204 } | 200 } |
205 | 201 |