]> sjero.net Git - ltp2tcp/blob - ltp.c
Initial Commit
[ltp2tcp] / ltp.c
1 /******************************************************************************
2 Author: Samuel Jero
3
4 Date: 12/2010
5
6 Description:  LTP Header manipulation functions
7
8 ******************************************************************************/
9 #include "ltp.h"
10
11
12 /*
13  * SDNV has a zero in high-order bit position of last byte. The high-order
14  * bit of all preceding bytes is set to one. This returns the numeric value
15  * in an integer and sets the value of the second argument to the number of
16  * bytes used to code the SDNV. A -1 is returned if the evaluation fails
17  * (value exceeds maximum for signed integer). 0 is an acceptable value.
18  */
19
20 #define SDNV_MASK       0x7f
21
22 /*2rd arg is number of bytes in field (returned)*/
23 int evaluate_sdnv(const u_char* loc, int *bytecount)
24 {
25     int value = 0;
26     u_char curbyte;
27
28     *bytecount = 0;
29
30     /*
31      * Get 1st byte and continue to get them while high-order bit is 1
32      */
33
34     while((curbyte = *(loc++)) & ~SDNV_MASK) {
35         if(*bytecount >= (int) sizeof(int)) {
36             *bytecount = 0;
37             return -1;
38         }
39         value = value << 7;
40         value |= (curbyte & SDNV_MASK);
41         ++*bytecount;
42     }
43
44     /*
45      * Add in the byte whose high-order bit is 0 (last one)
46      */
47
48     value = value << 7;
49     value |= (curbyte & SDNV_MASK);
50     ++*bytecount;
51     return value;
52 }
53
54 /* Special Function to evaluate 64 bit SDNVs */
55 long long evaluate_sdnv_64(const u_char* loc, int *bytecount)
56 {
57     long long value = 0;
58     u_char curbyte;
59
60     *bytecount = 0;
61
62     /*
63      * Get 1st byte and continue to get them while high-order bit is 1
64      */
65
66     while((curbyte = *(loc++)) & ~SDNV_MASK) {
67         if(*bytecount >= (int) sizeof(long long)) {
68             *bytecount = 0;
69             return -1;
70         }
71         value = value<< 7;
72         value |= (curbyte & SDNV_MASK);
73         ++*bytecount;
74     }
75
76     /*
77      * Add in the byte whose high-order bit is 0 (last one)
78      */
79
80     value = value << 7;
81     value |= (curbyte & SDNV_MASK);
82     ++*bytecount;
83     return value;
84 }