apps/num2sdnv/num2sdnv.c
changeset 0 2b3e5ec03512
equal deleted inserted replaced
-1:000000000000 0:2b3e5ec03512
       
     1 /*
       
     2  *    Copyright 2007 Intel Corporation
       
     3  * 
       
     4  *    Licensed under the Apache License, Version 2.0 (the "License");
       
     5  *    you may not use this file except in compliance with the License.
       
     6  *    You may obtain a copy of the License at
       
     7  * 
       
     8  *        http://www.apache.org/licenses/LICENSE-2.0
       
     9  * 
       
    10  *    Unless required by applicable law or agreed to in writing, software
       
    11  *    distributed under the License is distributed on an "AS IS" BASIS,
       
    12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       
    13  *    See the License for the specific language governing permissions and
       
    14  *    limitations under the License.
       
    15  */
       
    16 
       
    17 
       
    18 #ifdef HAVE_CONFIG_H
       
    19 #  include <dtn-config.h>
       
    20 #endif
       
    21 
       
    22 #include <stdio.h>
       
    23 #include <unistd.h>
       
    24 #include <errno.h>
       
    25 #include <strings.h>
       
    26 #include <string.h>
       
    27 #include <stdlib.h>
       
    28 
       
    29 #include "sdnv-c.h"
       
    30 
       
    31 char* progname;
       
    32 char* numstr;
       
    33 char* end;
       
    34 int len;
       
    35 u_char buf[1024];
       
    36 u_int64_t val;
       
    37 
       
    38 int mode = 0;
       
    39 #define ENCODE 1
       
    40 #define DECODE 2
       
    41 
       
    42 char hex[] = "0123456789abcdef";
       
    43 #define HEXTONUM(x) ((x) < 'a' ? (x) - '0' : x - 'a' + 10)
       
    44 
       
    45 int main(int argc, char* argv[])
       
    46 {
       
    47     size_t i;
       
    48     memset(buf, 0, sizeof(buf));
       
    49     
       
    50     progname = strrchr(argv[0], '/');
       
    51     if (progname == 0) {
       
    52         progname = argv[0];
       
    53     } else {
       
    54         progname++;
       
    55     }
       
    56     
       
    57     if (!strcmp(progname, "num2sdnv")) {
       
    58         mode = ENCODE;
       
    59     } else if (!strcmp(progname, "sdnv2num")) {
       
    60         mode = DECODE;
       
    61     }
       
    62 
       
    63     argv++;
       
    64     argc--;
       
    65     
       
    66     while (argc > 1) {
       
    67         char* arg = argv[0];
       
    68         argv++;
       
    69         argc--;
       
    70         
       
    71         if (!strcmp(arg, "-e")) {
       
    72             mode = ENCODE;
       
    73         } else if (!strcmp(arg, "-d")) {
       
    74             mode = DECODE;
       
    75         } else {
       
    76             fprintf(stderr, "unknown argument %s\n", arg);
       
    77             exit(1);
       
    78         }
       
    79     }
       
    80 
       
    81     if (argc != 1 || mode == 0) {
       
    82         fprintf(stderr, "usage: %s [-de] <num>\n"
       
    83                 " -e   encode number to sdnv\n"
       
    84                 " -d   decode sdnv to number\n",
       
    85                 progname);
       
    86         exit(1);
       
    87     }
       
    88 
       
    89     numstr = argv[0];
       
    90     if (mode == ENCODE) {
       
    91         if (numstr[0] == '0' && numstr[1] == 'x') {
       
    92             val = strtoull(numstr + 2, &end, 16);
       
    93         } else {
       
    94             val = strtoull(numstr, &end, 10);
       
    95         }
       
    96         if (*end != '\0') {
       
    97             fprintf(stderr, "invalid number %s\n", numstr);
       
    98             exit(1);
       
    99         }
       
   100         
       
   101         len = sdnv_encode(val, buf, sizeof(buf));
       
   102     } else {
       
   103         if (numstr[0] == '0' && numstr[1] == 'x') {
       
   104             numstr += 2;
       
   105         }
       
   106 
       
   107         if ((strlen(numstr) % 2) != 0) {
       
   108             fprintf(stderr, "number string %s must contain full bytes\n",
       
   109                     numstr);
       
   110             exit(1);
       
   111         }
       
   112         
       
   113         for (i = 0; i < strlen(numstr) / 2; ++i) {
       
   114             buf[i] = (HEXTONUM(numstr[2*i]) << 4) +
       
   115                      HEXTONUM(numstr[2*i + 1]);
       
   116         }
       
   117         len = sdnv_decode(buf, strlen(numstr)/2, &val);
       
   118     }
       
   119 
       
   120     printf("val:  %llu (0x%llx)\n", (unsigned long long)val, (unsigned long long)val);
       
   121     printf("len:  %d\n", len);
       
   122     printf("sdnv: ");
       
   123     if (len > 0) {
       
   124         for (i = 0; i < (size_t)len; ++i) {
       
   125             printf("%c%c", hex[(buf[i] >> 4) & 0xf], hex[buf[i] & 0xf]);
       
   126         }
       
   127     }
       
   128     printf("\n");
       
   129 
       
   130     return 0;
       
   131 }