Mercurial > hg > xmms-sid
comparison src/xs_support.c @ 868:3c3569894b23
Cosmetics.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Fri, 09 Nov 2012 03:59:19 +0200 |
parents | 051ef70d0123 |
children | 5e33075ed9dd |
comparison
equal
deleted
inserted
replaced
867:051ef70d0123 | 868:3c3569894b23 |
---|---|
178 | 178 |
179 | 179 |
180 /* Concatenate a given string up to given dest size or \n. | 180 /* Concatenate a given string up to given dest size or \n. |
181 * If size max is reached, change the end to "..." | 181 * If size max is reached, change the end to "..." |
182 */ | 182 */ |
183 void xs_pnstrcat(gchar *dest, size_t iSize, const gchar *str) | 183 void xs_pnstrcat(gchar *dest, const size_t size, const gchar *str) |
184 { | 184 { |
185 size_t i, n; | 185 size_t i, n; |
186 const gchar *s; | 186 const gchar *s; |
187 gchar *d; | 187 gchar *d; |
188 | 188 |
189 d = dest; | 189 for (d = dest, i = 0; *d && i < size; i++, d++); |
190 i = 0; | |
191 while (*d && (i < iSize)) { | |
192 i++; | |
193 d++; | |
194 } | |
195 | 190 |
196 s = str; | 191 s = str; |
197 while (*s && (*s != '\n') && (i < iSize)) { | 192 while (*s && *s != '\n' && i < size) |
193 { | |
198 *d = *s; | 194 *d = *s; |
199 d++; | 195 d++; |
200 s++; | 196 s++; |
201 i++; | 197 i++; |
202 } | 198 } |
203 | 199 |
204 *d = 0; | 200 *d = 0; |
205 | 201 |
206 if (i >= iSize) { | 202 if (i >= size) |
203 { | |
207 i--; | 204 i--; |
208 d--; | 205 d--; |
209 n = 3; | 206 for (n = 3; i > 0 && n > 0; d--, i--, n--) |
210 while ((i > 0) && (n > 0)) { | |
211 *d = '.'; | 207 *d = '.'; |
212 d--; | |
213 i--; | |
214 n--; | |
215 } | |
216 } | 208 } |
217 } | 209 } |
218 | 210 |
219 | 211 |
220 /* Locate character in string | 212 /* Locate character in string |
254 # define xs_md5_bytereverse(buf, len) /* Nothing */ | 246 # define xs_md5_bytereverse(buf, len) /* Nothing */ |
255 #elif G_BYTE_ORDER == G_BIG_ENDIAN | 247 #elif G_BYTE_ORDER == G_BIG_ENDIAN |
256 static void xs_md5_bytereverse(guint8 *buf, guint l) | 248 static void xs_md5_bytereverse(guint8 *buf, guint l) |
257 { | 249 { |
258 guint32 t; | 250 guint32 t; |
259 do { | 251 do |
252 { | |
260 t = (guint32) ((guint) buf[3] << 8 | buf[2]) << 16 | ((guint) buf[1] << 8 | buf[0]); | 253 t = (guint32) ((guint) buf[3] << 8 | buf[2]) << 16 | ((guint) buf[1] << 8 | buf[0]); |
261 *(guint32 *) buf = t; | 254 *(guint32 *) buf = t; |
262 buf += sizeof(guint32); | 255 buf += sizeof(guint32); |
263 } while (--l); | 256 } while (--l); |
264 } | 257 } |
288 */ | 281 */ |
289 #define F1(x, y, z) (z ^ (x & (y ^ z))) | 282 #define F1(x, y, z) (z ^ (x & (y ^ z))) |
290 #define F2(x, y, z) F1(z, x, y) | 283 #define F2(x, y, z) F1(z, x, y) |
291 #define F3(x, y, z) (x ^ y ^ z) | 284 #define F3(x, y, z) (x ^ y ^ z) |
292 #define F4(x, y, z) (y ^ (x | ~z)) | 285 #define F4(x, y, z) (y ^ (x | ~z)) |
293 #define MD5STEP(f, w, x, y, z, data, s) \ | 286 #define MD5STEP(f, w, x, y, z, data, s) ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x ) |
294 ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x ) | |
295 | 287 |
296 static void xs_md5_transform(guint32 buf[4], guint32 const in[16]) | 288 static void xs_md5_transform(guint32 buf[4], guint32 const in[16]) |
297 { | 289 { |
298 register guint32 a, b, c, d; | 290 register guint32 a, b, c, d; |
299 | 291 |
391 ctx->bits[1] += len >> 29; | 383 ctx->bits[1] += len >> 29; |
392 | 384 |
393 t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */ | 385 t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */ |
394 | 386 |
395 /* Handle any leading odd-sized chunks */ | 387 /* Handle any leading odd-sized chunks */ |
396 if (t) { | 388 if (t) |
389 { | |
397 guint8 *p = (guint8 *) ctx->in + t; | 390 guint8 *p = (guint8 *) ctx->in + t; |
398 | 391 |
399 t = 64 - t; | 392 t = 64 - t; |
400 if (len < t) { | 393 if (len < t) |
394 { | |
401 memcpy(p, buf, len); | 395 memcpy(p, buf, len); |
402 return; | 396 return; |
403 } | 397 } |
404 memcpy(p, buf, t); | 398 memcpy(p, buf, t); |
405 xs_md5_bytereverse(ctx->in, 16); | 399 xs_md5_bytereverse(ctx->in, 16); |
407 buf += t; | 401 buf += t; |
408 len -= t; | 402 len -= t; |
409 } | 403 } |
410 | 404 |
411 /* Process data in 64-byte chunks */ | 405 /* Process data in 64-byte chunks */ |
412 while (len >= 64) { | 406 while (len >= 64) |
407 { | |
413 memcpy(ctx->in, buf, 64); | 408 memcpy(ctx->in, buf, 64); |
414 xs_md5_bytereverse(ctx->in, 16); | 409 xs_md5_bytereverse(ctx->in, 16); |
415 xs_md5_transform(ctx->buf, (guint32 *) ctx->in); | 410 xs_md5_transform(ctx->buf, (guint32 *) ctx->in); |
416 buf += 64; | 411 buf += 64; |
417 len -= 64; | 412 len -= 64; |
419 | 414 |
420 /* Handle any remaining bytes of data. */ | 415 /* Handle any remaining bytes of data. */ |
421 memcpy(ctx->in, buf, len); | 416 memcpy(ctx->in, buf, len); |
422 } | 417 } |
423 | 418 |
419 | |
424 /* Final wrapup - pad to 64-byte boundary with the bit pattern | 420 /* Final wrapup - pad to 64-byte boundary with the bit pattern |
425 * 1 0* (64-bit count of bits processed, MSB-first) | 421 * 1 0* (64-bit count of bits processed, MSB-first) |
426 */ | 422 */ |
427 void xs_md5_finish(xs_md5state_t *ctx, xs_md5hash_t digest) | 423 void xs_md5_finish(xs_md5state_t *ctx, xs_md5hash_t digest) |
428 { | 424 { |
439 | 435 |
440 /* Bytes of padding needed to make 64 bytes */ | 436 /* Bytes of padding needed to make 64 bytes */ |
441 count = 64 - 1 - count; | 437 count = 64 - 1 - count; |
442 | 438 |
443 /* Pad out to 56 mod 64 */ | 439 /* Pad out to 56 mod 64 */ |
444 if (count < 8) { | 440 if (count < 8) |
441 { | |
445 /* Two lots of padding: Pad the first block to 64 bytes */ | 442 /* Two lots of padding: Pad the first block to 64 bytes */ |
446 memset(p, 0, count); | 443 memset(p, 0, count); |
447 xs_md5_bytereverse(ctx->in, 16); | 444 xs_md5_bytereverse(ctx->in, 16); |
448 xs_md5_transform(ctx->buf, (guint32 *) ctx->in); | 445 xs_md5_transform(ctx->buf, (guint32 *) ctx->in); |
449 | 446 |
450 /* Now fill the next block with 56 bytes */ | 447 /* Now fill the next block with 56 bytes */ |
451 memset(ctx->in, 0, 56); | 448 memset(ctx->in, 0, 56); |
452 } else { | 449 } |
450 else | |
451 { | |
453 /* Pad block to 56 bytes */ | 452 /* Pad block to 56 bytes */ |
454 memset(p, 0, count - 8); | 453 memset(p, 0, count - 8); |
455 } | 454 } |
456 xs_md5_bytereverse(ctx->in, 14); | 455 xs_md5_bytereverse(ctx->in, 14); |
457 | 456 |