addrtostr.c (313537) | addrtostr.c (327234) |
---|---|
1/* 2 * Copyright (c) 1999 Kungliga Tekniska H��gskolan 3 * (Royal Institute of Technology, Stockholm, Sweden). 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: --- 96 unchanged lines hidden (view full) --- 105 * Keep this in mind if you think this function should have been coded 106 * to use pointer overlays. All the world's not a VAX. 107 */ 108 const u_char *srcaddr = (const u_char *)src; 109 char *dp; 110 size_t space_left, added_space; 111 int snprintfed; 112 struct { | 1/* 2 * Copyright (c) 1999 Kungliga Tekniska H��gskolan 3 * (Royal Institute of Technology, Stockholm, Sweden). 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: --- 96 unchanged lines hidden (view full) --- 105 * Keep this in mind if you think this function should have been coded 106 * to use pointer overlays. All the world's not a VAX. 107 */ 108 const u_char *srcaddr = (const u_char *)src; 109 char *dp; 110 size_t space_left, added_space; 111 int snprintfed; 112 struct { |
113 long base; 114 long len; | 113 int base; 114 int len; |
115 } best, cur; | 115 } best, cur; |
116 u_long words [IN6ADDRSZ / INT16SZ]; | 116 uint16_t words [IN6ADDRSZ / INT16SZ]; |
117 int i; 118 119 /* Preprocess: 120 * Copy the input (bytewise) array into a wordwise array. 121 * Find the longest run of 0x00's in src[] for :: shorthanding. 122 */ | 117 int i; 118 119 /* Preprocess: 120 * Copy the input (bytewise) array into a wordwise array. 121 * Find the longest run of 0x00's in src[] for :: shorthanding. 122 */ |
123 memset (words, 0, sizeof(words)); 124 for (i = 0; i < IN6ADDRSZ; i++) 125 words[i/2] |= (srcaddr[i] << ((1 - (i % 2)) << 3)); | 123 for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++) 124 words[i] = (srcaddr[2*i] << 8) | srcaddr[2*i + 1]; |
126 127 best.len = 0; 128 best.base = -1; 129 cur.len = 0; 130 cur.base = -1; | 125 126 best.len = 0; 127 best.base = -1; 128 cur.len = 0; 129 cur.base = -1; |
131 for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++) | 130 for (i = 0; i < (int)(IN6ADDRSZ / INT16SZ); i++) |
132 { 133 if (words[i] == 0) 134 { 135 if (cur.base == -1) 136 cur.base = i, cur.len = 1; 137 else cur.len++; 138 } 139 else if (cur.base != -1) --- 16 unchanged lines hidden (view full) --- 156 { \ 157 if (space_left == 0) { \ 158 errno = ENOSPC; \ 159 return (NULL); \ 160 } \ 161 *dp++ = c; \ 162 space_left--; \ 163 } | 131 { 132 if (words[i] == 0) 133 { 134 if (cur.base == -1) 135 cur.base = i, cur.len = 1; 136 else cur.len++; 137 } 138 else if (cur.base != -1) --- 16 unchanged lines hidden (view full) --- 155 { \ 156 if (space_left == 0) { \ 157 errno = ENOSPC; \ 158 return (NULL); \ 159 } \ 160 *dp++ = c; \ 161 space_left--; \ 162 } |
164 for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++) | 163 for (i = 0; i < (int)(IN6ADDRSZ / INT16SZ); i++) |
165 { 166 /* Are we inside the best run of 0x00's? 167 */ 168 if (best.base != -1 && i >= best.base && i < (best.base + best.len)) 169 { 170 if (i == best.base) 171 APPEND_CHAR(':'); 172 continue; --- 14 unchanged lines hidden (view full) --- 187 errno = ENOSPC; 188 return (NULL); 189 } 190 added_space = strlen(dp); 191 dp += added_space; 192 space_left -= added_space; 193 break; 194 } | 164 { 165 /* Are we inside the best run of 0x00's? 166 */ 167 if (best.base != -1 && i >= best.base && i < (best.base + best.len)) 168 { 169 if (i == best.base) 170 APPEND_CHAR(':'); 171 continue; --- 14 unchanged lines hidden (view full) --- 186 errno = ENOSPC; 187 return (NULL); 188 } 189 added_space = strlen(dp); 190 dp += added_space; 191 space_left -= added_space; 192 break; 193 } |
195 snprintfed = snprintf (dp, space_left, "%lx", words[i]); | 194 snprintfed = snprintf (dp, space_left, "%x", words[i]); |
196 if (snprintfed < 0) 197 return (NULL); 198 if ((size_t) snprintfed >= space_left) 199 { 200 errno = ENOSPC; 201 return (NULL); 202 } 203 dp += snprintfed; 204 space_left -= snprintfed; 205 } 206 207 /* Was it a trailing run of 0x00's? 208 */ 209 if (best.base != -1 && (best.base + best.len) == (IN6ADDRSZ / INT16SZ)) 210 APPEND_CHAR(':'); 211 APPEND_CHAR('\0'); 212 213 return (dst); 214} | 195 if (snprintfed < 0) 196 return (NULL); 197 if ((size_t) snprintfed >= space_left) 198 { 199 errno = ENOSPC; 200 return (NULL); 201 } 202 dp += snprintfed; 203 space_left -= snprintfed; 204 } 205 206 /* Was it a trailing run of 0x00's? 207 */ 208 if (best.base != -1 && (best.base + best.len) == (IN6ADDRSZ / INT16SZ)) 209 APPEND_CHAR(':'); 210 APPEND_CHAR('\0'); 211 212 return (dst); 213} |