Added one safety check to AFM parsing.
[wine/multimedia.git] / server / unicode.c
blob892807762fd036fe5540f1950c450f74e0e6f0c2
1 /*
2 * Unicode routines for use inside the server
4 * Copyright (C) 1999 Alexandre Julliard
5 */
7 #include <ctype.h>
8 #include <stdio.h>
10 #include "unicode.h"
12 /* dump a Unicode string with proper escaping */
13 int dump_strW( const WCHAR *str, size_t len, FILE *f, char escape[2] )
15 static const char escapes[32] = ".......abtnvfr.............e....";
16 char buffer[256];
17 char *pos = buffer;
18 int count = 0;
20 for (; len; str++, len--)
22 if (pos > buffer + sizeof(buffer) - 8)
24 fwrite( buffer, pos - buffer, 1, f );
25 count += pos - buffer;
26 pos = buffer;
28 if (*str > 127) /* hex escape */
30 if (len > 1 && str[1] < 128 && isxdigit((char)str[1]))
31 pos += sprintf( pos, "\\x%04x", *str );
32 else
33 pos += sprintf( pos, "\\x%x", *str );
34 continue;
36 if (*str < 32) /* octal or C escape */
38 if (!*str && len == 1) continue; /* do not output terminating NULL */
39 if (escapes[*str] != '.')
40 pos += sprintf( pos, "\\%c", escapes[*str] );
41 else if (len > 1 && str[1] >= '0' && str[1] <= '7')
42 pos += sprintf( pos, "\\%03o", *str );
43 else
44 pos += sprintf( pos, "\\%o", *str );
45 continue;
47 if (*str == '\\' || *str == escape[0] || *str == escape[1]) *pos++ = '\\';
48 *pos++ = *str;
50 fwrite( buffer, pos - buffer, 1, f );
51 count += pos - buffer;
52 return count;