Advertisement
Guest User

inet_pton6 javascript

a guest
Oct 9th, 2012
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* inet_pton.c -- convert IPv4 and IPv6 addresses from text to binary form
  2.  
  3.    Copyright (C) 2006 Free Software Foundation, Inc.
  4.  
  5.    This program is free software; you can redistribute it and/or modify
  6.    it under the terms of the GNU General Public License as published by
  7.    the Free Software Foundation; either version 2, or (at your option)
  8.    any later version.
  9.  
  10.    This program is distributed in the hope that it will be useful,
  11.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.    GNU General Public License for more details.
  14.  
  15.    You should have received a copy of the GNU General Public License
  16.    along with this program; if not, write to the Free Software Foundation,
  17.    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
  18.  
  19. /*
  20.  * Copyright (c) 1996,1999 by Internet Software Consortium.
  21.  *
  22.  * Permission to use, copy, modify, and distribute this software for any
  23.  * purpose with or without fee is hereby granted, provided that the above
  24.  * copyright notice and this permission notice appear in all copies.
  25.  *
  26.  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
  27.  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
  28.  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
  29.  * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  30.  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  31.  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  32.  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  33.  * SOFTWARE.
  34.  */
  35.  
  36. //port by snoj for speed test for nodejs
  37.  
  38. //http://www.koders.com/c/fid74A647B73741DDD374D5478366A00584E8740D43.aspx
  39.  function inet_pton6 (src)
  40. {
  41.   //static const char xdigits[] = "0123456789abcdef";
  42.   //unsigned char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp;
  43.   //const char *curtok;
  44.  
  45.   //int ch, saw_xdigit;
  46.   //unsigned val;
  47.  
  48.   var xdigits, tmp, tp, endp, colonp, curtok, ch, saw_xdigit, val;
  49.  
  50.   xdigits = "0123456789abcdef";
  51.   tmp = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
  52.   //tp = memset (tmp, '\0', NS_IN6ADDRSZ);
  53.   //endp = tp + NS_IN6ADDRSZ; end pointer (basically the length);
  54.  
  55.   colonp = null;
  56.   endp = 16;
  57.   tp = 0;
  58.   /* Leading :: requires sme special handling. */
  59.   if (src[0] == ':' && src[1] != ':') {
  60.     //console.log("address starts with one :");
  61.     return 0;
  62.   }
  63.     //if (*src != ':')
  64.       //return (0);
  65.   //curtok = src;
  66.   saw_xdigit = 0;
  67.   val = 0;
  68.   //while ((ch = tolower (*src++)) != '\0')
  69.   src = src.toLowerCase();
  70.   for(var i = 0; i<src.length; i++)
  71.     {
  72.       ch = parseInt(src[i],16) //src.charCodeAt(i)
  73.       //const char *pch;
  74.       var pch;
  75.  
  76.       //pch = strchr (xdigits, ch);
  77.       //pch = xdigits.indexOf(String.fromCharCode(ch));
  78.       pch = xdigits.indexOf(src[i]);
  79.       if (pch != -1)
  80.     {
  81.       val <<= 4;
  82.       //val |= (pch - xdigits);
  83.       val |= (ch);
  84.       if (val > 0xffff) {
  85.         //console.log("val > %s", 0xffff);
  86.         return (0);
  87.       }
  88.       saw_xdigit = 1;
  89.       continue;
  90.     }
  91.       if (src[i] == ':')
  92.     {
  93.       curtok = i;
  94.       if (!saw_xdigit)
  95.         {
  96.           if (colonp) {
  97.             //console.log("colonp not null?");
  98.         return (0);
  99.           }
  100.           colonp = tp;
  101.           continue;
  102.         }
  103.       else if (src[i] == '\0') //probably can delete this?
  104.         {
  105.           //console.log("wtf?")
  106.           return (0);
  107.         }
  108.       //if (tp + NS_INT16SZ > endp)
  109.       if ((tp + 2) > 16) {
  110.         //console.log("tp + 2 > 16");
  111.         return (0);
  112.       }
  113.       //*tp++ = (u_char) (val >> 8) & 0xff;
  114.       //*tp++ = (u_char) val & 0xff;
  115.       //tmp[tp++] = String.fromCharCode((val >> 8) &0xff)
  116.       //tmp[tp++] = String.fromCharCode((val) &0xff)
  117.       //console.log("tp = %s", tp);
  118.       //console.log(((val >> 8) & 0xff).toString(16));
  119.       //console.log(((val) &0xff).toString(16));
  120.       tmp[tp++] = ((val >> 8) & 0xff)//.toString(16) //.replace(/(0.)([a-f].)/i, '$2');
  121.       tmp[tp++] = ((val) & 0xff)//.toString(16) //.replace(/(0.)([a-f].)/i, '$2');
  122.       //tmp[tp++] = (val >> 8)
  123.       saw_xdigit = 0;
  124.       val = 0;
  125.       continue;
  126.     }
  127.       /*if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) &&
  128.       inet_pton4 (curtok, tp) > 0)
  129.     {
  130.       tp += NS_INADDRSZ;
  131.       saw_xdigit = 0;
  132.       break;        /* '\0' was seen by inet_pton4(). * /
  133.     }*/
  134.       //console.log("end of for loop");
  135.       return (0);
  136.     }
  137.   if (saw_xdigit)
  138.     {
  139.       //if (tp + NS_INT16SZ > endp)
  140.       if ((tp + 2) > 16) {
  141.     //console.log("tp + 2 > 16");
  142.     return (0);
  143.       }
  144.       //*tp++ = (u_char) (val >> 8) & 0xff;
  145.       //*tp++ = (u_char) val & 0xff;
  146.       //console.log(val);
  147.       //tmp[tp++] = String.fromCharCode((val >> 8) &0xff)
  148.       //tmp[tp++] = String.fromCharCode((val) &0xff)
  149.       tmp[tp++] = ((val >> 8) &0xff)//.toString(16) //.replace(/(0.)([a-f].)/i, '$2');
  150.       tmp[tp++] = ((val) &0xff)//.toString(16) //.replace(/(0.)([a-f].)/i, '$2');
  151.     }
  152.     //console.log(tmp);
  153.   if (colonp != null)
  154.     {
  155.       /*
  156.        * Since some memmove()'s erroneously fail to handle
  157.        * overlapping regions, we'll do the shift by hand.
  158.        */
  159.       //const int n = tp - colonp;
  160.       //int i;
  161.       n = tp - colonp;
  162.      
  163.       if (tp == endp) {
  164.         //console.log("tp == endp");
  165.     return (0);
  166.       }
  167.       //console.log("tp = %s, n = %s", tp , n);
  168.       for (var i = 0; i <= n; i++)
  169.     {
  170.       //endp[-i] = colonp[n - i];
  171.       //colonp[n - i] = 0;
  172.       //console.log("tmp[16-i] = %s, n = %s, colonp = %s, tmp[n+i] = %s", tmp[15-i], n, colonp, tmp[n+i]);
  173.       tmp[15-i] = tmp[n+i];
  174.       tmp[n+i] = 0;
  175.     }
  176.       tp = endp;
  177.     }
  178.   if (tp != endp) {
  179.     //console.log("now tp != endp");
  180.     return (0);
  181.   }
  182.   //memcpy (dst, tmp, NS_IN6ADDRSZ);
  183.   //return (1);
  184.   //console.log("eol");
  185.   return tmp.slice(0,15);
  186. }
  187.  
  188. console.log(inet_pton6("::"));
  189. console.log(inet_pton6("::a"));
  190. console.log(inet_pton6("a::"));
  191. console.log(inet_pton6("ffaa:FFFF::1a:a"));
  192. console.log(inet_pton6("0000:0000:0000:0000:0000:0000:0000:0000::0000"));
  193. console.time("inet_pton");
  194. for(var i = 0; i< 10000; i++) {
  195.     inet_pton6("0000:0000:0000:0000:0000:0000:0000:0000::0000");
  196. }
  197. console.timeEnd("inet_pton");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement