Mercurial > hg > freeDiameter
annotate libfdproto/ostr.c @ 707:e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
author | Sebastien Decugis <sdecugis@nict.go.jp> |
---|---|
date | Wed, 09 Feb 2011 18:08:54 +0900 |
parents | 4ffbc9f1e922 |
children | 571b3abaa5df |
rev | line source |
---|---|
706
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
1 /********************************************************************************************************* |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
2 * Software License Agreement (BSD License) * |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
3 * Author: Sebastien Decugis <sdecugis@nict.go.jp> * |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
4 * * |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
5 * Copyright (c) 2011, WIDE Project and NICT * |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
6 * All rights reserved. * |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
7 * * |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
8 * Redistribution and use of this software in source and binary forms, with or without modification, are * |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
9 * permitted provided that the following conditions are met: * |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
10 * * |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
11 * * Redistributions of source code must retain the above * |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
12 * copyright notice, this list of conditions and the * |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
13 * following disclaimer. * |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
14 * * |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
15 * * Redistributions in binary form must reproduce the above * |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
16 * copyright notice, this list of conditions and the * |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
17 * following disclaimer in the documentation and/or other * |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
18 * materials provided with the distribution. * |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
19 * * |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
20 * * Neither the name of the WIDE Project or NICT nor the * |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
21 * names of its contributors may be used to endorse or * |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
22 * promote products derived from this software without * |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
23 * specific prior written permission of WIDE Project and * |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
24 * NICT. * |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
25 * * |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED * |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
27 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A * |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
28 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR * |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
29 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
30 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR * |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
32 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
33 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
34 *********************************************************************************************************/ |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
35 |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
36 #include "fdproto-internal.h" |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
37 |
707
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
38 #if (!defined(DIAMID_IDNA_IGNORE) && !defined(DIAMID_IDNA_REJECT)) |
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
39 /* Process IDNA with stringprep -- See RFC5890 -- and libidn documentation... */ |
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
40 #include <idna.h> /* idna_to_ascii_8z() */ |
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
41 #endif /* !defined(DIAMID_IDNA_IGNORE) && !defined(DIAMID_IDNA_REJECT) */ |
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
42 |
706
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
43 /* Similar to strdup with (must be verified) os0_t */ |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
44 os0_t os0dup_int(os0_t s, size_t l) { |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
45 os0_t r; |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
46 CHECK_MALLOC_DO( r = calloc(l+1, 1), return NULL ); |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
47 memcpy(r, s, l); /* this might be faster than a strcpy or strdup because it can work with 32 or 64b blocks */ |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
48 return r; |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
49 } |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
50 |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
51 /* case sensitive comparison, fast */ |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
52 int fd_os_cmp_int(uint8_t * os1, size_t os1sz, uint8_t * os2, size_t os2sz) |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
53 { |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
54 ASSERT( os1 && os2); |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
55 if (os1sz < os2sz) |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
56 return -1; |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
57 if (os1sz > os2sz) |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
58 return 1; |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
59 return memcmp(os1, os2, os1sz); |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
60 } |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
61 |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
62 /* a local version of tolower() that does not depend on LC_CTYPE locale */ |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
63 static inline uint8_t asciitolower(uint8_t a) |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
64 { |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
65 if ((a >= 'A') && (a <= 'Z')) |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
66 return a + 32 /* == 'a' - 'A' */; |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
67 return a; |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
68 } |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
69 |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
70 /* a little less sensitive to case, slower. */ |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
71 int fd_os_almostcasecmp_int(uint8_t * os1, size_t os1sz, uint8_t * os2, size_t os2sz) |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
72 { |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
73 int i; |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
74 ASSERT( os1 && os2); |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
75 if (os1sz < os2sz) |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
76 return -1; |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
77 if (os1sz > os2sz) |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
78 return 1; |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
79 |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
80 for (i = 0; i < os1sz; i++) { |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
81 if (os1[i] == os2[i]) |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
82 continue; |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
83 |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
84 if (asciitolower(os1[i]) == asciitolower(os2[i])) |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
85 continue; |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
86 |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
87 return os1[i] < os2[i] ? -1 : 1; |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
88 } |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
89 |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
90 return 0; |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
91 } |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
92 |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
93 /* Check if the string contains only ASCII */ |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
94 int fd_os_is_valid_DiameterIdentity(uint8_t * os, size_t ossz) |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
95 { |
707
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
96 #ifdef DIAMID_IDNA_IGNORE |
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
97 |
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
98 /* Allow anything */ |
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
99 |
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
100 #else /* DIAMID_IDNA_IGNORE */ |
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
101 |
706
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
102 int i; |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
103 |
707
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
104 /* Allow only letters, digits, hyphen, dot */ |
706
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
105 for (i=0; i < ossz; i++) { |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
106 if (os[i] > 'z') |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
107 break; |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
108 if (os[i] >= 'a') |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
109 continue; |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
110 if ((os[i] >= 'A') && (os[i] <= 'Z')) |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
111 continue; |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
112 if ((os[i] == '-') || (os[i] == '.')) |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
113 continue; |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
114 if ((os[i] >= '0') && (os[i] <= '9')) |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
115 continue; |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
116 break; |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
117 } |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
118 if (i < ossz) { |
707
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
119 int nb = 1; |
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
120 /* To get a better display, check if the invalid char is UTF-8 */ |
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
121 if ((os[i] & 0xE0) == 0xC0 /* 110xxxxx */) { |
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
122 if ((i < ossz - 1) && ((os[i + 1] & 0xC0) == 0x80 /* 10xxxxxx */)) |
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
123 nb = 2; |
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
124 goto disp; |
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
125 } |
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
126 if ((os[i] & 0xF0) == 0xE0 /* 1110xxxx */) { |
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
127 if ((i < ossz - 2) && ((os[i + 1] & 0xC0) == 0x80 /* 10xxxxxx */) |
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
128 && ((os[i + 2] & 0xC0) == 0x80 /* 10xxxxxx */)) |
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
129 nb = 3; |
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
130 goto disp; |
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
131 } |
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
132 if ((os[i] & 0xF8) == 0xF0 /* 11110xxx */) { |
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
133 if ((i < ossz - 3) && ((os[i + 1] & 0xC0) == 0x80 /* 10xxxxxx */) |
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
134 && ((os[i + 2] & 0xC0) == 0x80 /* 10xxxxxx */) |
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
135 && ((os[i + 3] & 0xC0) == 0x80 /* 10xxxxxx */)) |
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
136 nb = 4; |
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
137 goto disp; |
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
138 } |
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
139 if ((os[i] & 0xFC) == 0xF8 /* 111110xx */) { |
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
140 if ((i < ossz - 4) && ((os[i + 1] & 0xC0) == 0x80 /* 10xxxxxx */) |
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
141 && ((os[i + 2] & 0xC0) == 0x80 /* 10xxxxxx */) |
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
142 && ((os[i + 3] & 0xC0) == 0x80 /* 10xxxxxx */) |
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
143 && ((os[i + 4] & 0xC0) == 0x80 /* 10xxxxxx */)) |
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
144 nb = 5; |
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
145 goto disp; |
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
146 } |
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
147 if ((os[i] & 0xFE) == 0xFC /* 1111110x */) { |
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
148 if ((i < ossz - 5) && ((os[i + 1] & 0xC0) == 0x80 /* 10xxxxxx */) |
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
149 && ((os[i + 2] & 0xC0) == 0x80 /* 10xxxxxx */) |
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
150 && ((os[i + 3] & 0xC0) == 0x80 /* 10xxxxxx */) |
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
151 && ((os[i + 4] & 0xC0) == 0x80 /* 10xxxxxx */) |
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
152 && ((os[i + 5] & 0xC0) == 0x80 /* 10xxxxxx */)) |
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
153 nb = 6; |
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
154 goto disp; |
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
155 } |
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
156 /* otherwise, we just display the hex code */ |
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
157 TRACE_DEBUG(INFO, "Invalid character (0xhhX) at offset %d in DiameterIdentity '%.*s'", os[i], i+1, ossz, os); |
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
158 return 0; |
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
159 disp: |
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
160 TRACE_DEBUG(INFO, "Invalid character '%.*s' at offset %d in DiameterIdentity '%.*s'", nb, os + i, i+1, ossz, os); |
706
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
161 return 0; |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
162 } |
707
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
163 |
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
164 #endif /* DIAMID_IDNA_IGNORE */ |
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
165 |
706
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
166 return 1; |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
167 } |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
168 |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
169 /* The following function validates a string as a Diameter Identity or applies the IDNA transformation on it */ |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
170 int fd_os_validate_DiameterIdentity(char ** id, size_t * outsz, int memory /* 0: *id can be realloc'd. 1: *id must be malloc'd on output (was static) */) |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
171 { |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
172 TRACE_ENTRY("%p %p", id, outsz); |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
173 |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
174 *outsz = strlen(*id); |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
175 |
707
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
176 #ifndef DIAMID_IDNA_IGNORE |
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
177 |
706
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
178 if (!fd_os_is_valid_DiameterIdentity((os0_t)*id, *outsz)) { |
707
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
179 |
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
180 #ifdef DIAMID_IDNA_REJECT |
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
181 |
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
182 TRACE_DEBUG(INFO, "The string '%s' is not a valid DiameterIdentity!", *id); |
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
183 TRACE_DEBUG(INFO, "Returning EINVAL since fD is compiled with option DIAMID_IDNA_REJECT."); |
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
184 return EINVAL; |
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
185 |
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
186 #else /* DIAMID_IDNA_REJECT */ |
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
187 |
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
188 char *processed; |
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
189 int ret; |
706
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
190 |
707
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
191 ret = idna_to_ascii_8z ( *id, &processed, IDNA_USE_STD3_ASCII_RULES ); |
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
192 if (ret == IDNA_SUCCESS) { |
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
193 TRACE_DEBUG(INFO, "The string '%s' is not a valid DiameterIdentity, it was changed to '%s'", *id, processed); |
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
194 if (memory == 0) |
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
195 free(*id); |
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
196 *id = processed; |
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
197 *outsz = strlen(processed); |
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
198 /* Done! */ |
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
199 } else { |
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
200 TRACE_DEBUG(INFO, "The string '%s' is not a valid DiameterIdentity and cannot be sanitanized: %s", *id, idna_strerror (ret)); |
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
201 return EINVAL; |
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
202 } |
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
203 |
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
204 #endif /* DIAMID_IDNA_REJECT */ |
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
205 } else |
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
206 #endif /* ! DIAMID_IDNA_IGNORE */ |
e387d5c6b6f5
Added support for Internationalized Domain Names (IDNA) using GNU libidn
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
207 { |
706
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
208 if (memory == 1) { |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
209 CHECK_MALLOC( *id = os0dup(*id, *outsz) ); |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
210 } |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
211 } |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
212 return 0; |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
213 } |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
214 |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
215 |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
216 |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
217 /********************************************************************************************************/ |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
218 /* Hash function -- credits to Austin Appleby, thank you ^^ */ |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
219 /* See http://murmurhash.googlepages.com for more information on this function */ |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
220 |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
221 /* the strings are NOT always aligned properly (ex: received in RADIUS message), so we use the aligned MurmurHash2 function as needed */ |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
222 #define _HASH_MIX(h,k,m) { k *= m; k ^= k >> r; k *= m; h *= m; h ^= k; } |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
223 uint32_t fd_os_hash ( uint8_t * string, size_t len ) |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
224 { |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
225 uint32_t hash = len; |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
226 uint8_t * data = string; |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
227 |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
228 const unsigned int m = 0x5bd1e995; |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
229 const int r = 24; |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
230 int align = (long)string & 3; |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
231 |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
232 if (!align || (len < 4)) { |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
233 /* In case data is aligned, MurmurHash2 function */ |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
234 while(len >= 4) |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
235 { |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
236 /* Mix 4 bytes at a time into the hash */ |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
237 uint32_t k = *(uint32_t *)data; /* We don't care about the byte order */ |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
238 |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
239 _HASH_MIX(hash, k, m); |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
240 |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
241 data += 4; |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
242 len -= 4; |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
243 } |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
244 |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
245 /* Handle the last few bytes of the input */ |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
246 switch(len) { |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
247 case 3: hash ^= data[2] << 16; |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
248 case 2: hash ^= data[1] << 8; |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
249 case 1: hash ^= data[0]; |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
250 hash *= m; |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
251 } |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
252 |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
253 } else { |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
254 /* Unaligned data, use alignment-safe slower version */ |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
255 |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
256 /* Pre-load the temp registers */ |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
257 uint32_t t = 0, d = 0; |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
258 switch(align) |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
259 { |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
260 case 1: t |= data[2] << 16; |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
261 case 2: t |= data[1] << 8; |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
262 case 3: t |= data[0]; |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
263 } |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
264 t <<= (8 * align); |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
265 |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
266 data += 4-align; |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
267 len -= 4-align; |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
268 |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
269 /* From this point, "data" can be read by chunks of 4 bytes */ |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
270 |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
271 int sl = 8 * (4-align); |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
272 int sr = 8 * align; |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
273 |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
274 /* Mix */ |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
275 while(len >= 4) |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
276 { |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
277 uint32_t k; |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
278 |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
279 d = *(unsigned int *)data; |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
280 k = (t >> sr) | (d << sl); |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
281 |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
282 _HASH_MIX(hash, k, m); |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
283 |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
284 t = d; |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
285 |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
286 data += 4; |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
287 len -= 4; |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
288 } |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
289 |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
290 /* Handle leftover data in temp registers */ |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
291 d = 0; |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
292 if(len >= align) |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
293 { |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
294 uint32_t k; |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
295 |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
296 switch(align) |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
297 { |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
298 case 3: d |= data[2] << 16; |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
299 case 2: d |= data[1] << 8; |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
300 case 1: d |= data[0]; |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
301 } |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
302 |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
303 k = (t >> sr) | (d << sl); |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
304 _HASH_MIX(hash, k, m); |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
305 |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
306 data += align; |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
307 len -= align; |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
308 |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
309 /* Handle tail bytes */ |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
310 |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
311 switch(len) |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
312 { |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
313 case 3: hash ^= data[2] << 16; |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
314 case 2: hash ^= data[1] << 8; |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
315 case 1: hash ^= data[0]; |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
316 hash *= m; |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
317 }; |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
318 } |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
319 else |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
320 { |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
321 switch(len) |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
322 { |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
323 case 3: d |= data[2] << 16; |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
324 case 2: d |= data[1] << 8; |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
325 case 1: d |= data[0]; |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
326 case 0: hash ^= (t >> sr) | (d << sl); |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
327 hash *= m; |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
328 } |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
329 } |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
330 |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
331 |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
332 } |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
333 |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
334 /* Do a few final mixes of the hash to ensure the last few |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
335 bytes are well-incorporated. */ |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
336 hash ^= hash >> 13; |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
337 hash *= m; |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
338 hash ^= hash >> 15; |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
339 |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
340 return hash; |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
341 } |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
diff
changeset
|
342 |