~thomas-juberg/bebot/main

« back to all changes in this revision

Viewing changes to Sources/AocLogin/protocolbuf/message/encoding/pb_base128.php

  • Committer: Thomas Juberg
  • Date: 2013-10-28 03:16:57 UTC
  • Revision ID: git-v1:57965118d83b5b9addd510b078cff2dedb78e21b
- Ensure we only have LF line endings

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<?php
2
 
 
3
 
/**
4
 
 * Base 128 varints - decodes and encodes base128 varints to/from decimal
5
 
 *
6
 
 * @author Nikolai Kordulla
7
 
 */
8
 
class base128varint
9
 
{
10
 
    // modus for output
11
 
    var $modus = 1;
12
 
 
13
 
 
14
 
    /**
15
 
     * @param int $modus - 1=Byte 2=String
16
 
     */
17
 
    public function __construct($modus)
18
 
    {
19
 
        $this->modus = $modus;
20
 
    }
21
 
 
22
 
 
23
 
    /**
24
 
     * @param $number - number as decimal
25
 
     *                Returns the base128 value of an dec value
26
 
     */
27
 
    public function set_value($number)
28
 
    {
29
 
        $string = decbin($number);
30
 
        if (strlen($string) < 8) {
31
 
            $hexstring = dechex(bindec($string));
32
 
            if (strlen($hexstring) % 2 == 1) {
33
 
                $hexstring = '0' . $hexstring;
34
 
            }
35
 
            if ($this->modus == 1) {
36
 
                return $this->hex_to_str($hexstring);
37
 
            }
38
 
            return $hexstring;
39
 
        }
40
 
 
41
 
        // split it and insert the mb byte
42
 
        $string_array = array();
43
 
        $pre = '1';
44
 
        while (strlen($string) > 0) {
45
 
            if (strlen($string) < 8) {
46
 
                $string = substr('00000000', 0, 7 - strlen($string) % 7) . $string;
47
 
                $pre = '0';
48
 
            }
49
 
            $string_array[] = $pre . substr($string, strlen($string) - 7, 7);
50
 
            $string = substr($string, 0, strlen($string) - 7);
51
 
            $pre = '1';
52
 
            if ($string == '0000000') {
53
 
                break;
54
 
            }
55
 
        }
56
 
 
57
 
        $hexstring = '';
58
 
        foreach ($string_array as $string) {
59
 
            $hexstring .= sprintf('%02X', bindec($string));
60
 
        }
61
 
 
62
 
        // now format to hexstring in the right format
63
 
        if ($this->modus == 1) {
64
 
            return $this->hex_to_str($hexstring);
65
 
        }
66
 
 
67
 
        return $hexstring;
68
 
    }
69
 
 
70
 
 
71
 
    /**
72
 
     * Returns the dec value of an base128
73
 
     *
74
 
     * @param string bstring
75
 
     */
76
 
    public function get_value($string)
77
 
    {
78
 
        // now just drop the msb and reorder it + parse it in own string
79
 
        $valuestring = '';
80
 
        $string_length = strlen($string);
81
 
 
82
 
        $i = 1;
83
 
 
84
 
        while ($string_length > $i) {
85
 
            // unset msb string and reorder it
86
 
            $valuestring = substr($string, $i, 7) . $valuestring;
87
 
            $i += 8;
88
 
        }
89
 
 
90
 
        // now interprete it
91
 
        return bindec($valuestring);
92
 
    }
93
 
 
94
 
 
95
 
    /**
96
 
     * Converts hex 2 ascii
97
 
     *
98
 
     * @param String $hex - the hex string
99
 
     */
100
 
    public function hex_to_str($hex)
101
 
    {
102
 
        $str = '';
103
 
 
104
 
        for ($i = 0; $i < strlen($hex); $i += 2) {
105
 
            $str .= chr(hexdec(substr($hex, $i, 2)));
106
 
        }
107
 
        return $str;
108
 
    }
109
 
 
110
 
}
111
 
 
112
 
?>
 
1
<?php
 
2
 
 
3
/**
 
4
 * Base 128 varints - decodes and encodes base128 varints to/from decimal
 
5
 *
 
6
 * @author Nikolai Kordulla
 
7
 */
 
8
class base128varint
 
9
{
 
10
    // modus for output
 
11
    var $modus = 1;
 
12
 
 
13
 
 
14
    /**
 
15
     * @param int $modus - 1=Byte 2=String
 
16
     */
 
17
    public function __construct($modus)
 
18
    {
 
19
        $this->modus = $modus;
 
20
    }
 
21
 
 
22
 
 
23
    /**
 
24
     * @param $number - number as decimal
 
25
     *                Returns the base128 value of an dec value
 
26
     */
 
27
    public function set_value($number)
 
28
    {
 
29
        $string = decbin($number);
 
30
        if (strlen($string) < 8) {
 
31
            $hexstring = dechex(bindec($string));
 
32
            if (strlen($hexstring) % 2 == 1) {
 
33
                $hexstring = '0' . $hexstring;
 
34
            }
 
35
            if ($this->modus == 1) {
 
36
                return $this->hex_to_str($hexstring);
 
37
            }
 
38
            return $hexstring;
 
39
        }
 
40
 
 
41
        // split it and insert the mb byte
 
42
        $string_array = array();
 
43
        $pre = '1';
 
44
        while (strlen($string) > 0) {
 
45
            if (strlen($string) < 8) {
 
46
                $string = substr('00000000', 0, 7 - strlen($string) % 7) . $string;
 
47
                $pre = '0';
 
48
            }
 
49
            $string_array[] = $pre . substr($string, strlen($string) - 7, 7);
 
50
            $string = substr($string, 0, strlen($string) - 7);
 
51
            $pre = '1';
 
52
            if ($string == '0000000') {
 
53
                break;
 
54
            }
 
55
        }
 
56
 
 
57
        $hexstring = '';
 
58
        foreach ($string_array as $string) {
 
59
            $hexstring .= sprintf('%02X', bindec($string));
 
60
        }
 
61
 
 
62
        // now format to hexstring in the right format
 
63
        if ($this->modus == 1) {
 
64
            return $this->hex_to_str($hexstring);
 
65
        }
 
66
 
 
67
        return $hexstring;
 
68
    }
 
69
 
 
70
 
 
71
    /**
 
72
     * Returns the dec value of an base128
 
73
     *
 
74
     * @param string bstring
 
75
     */
 
76
    public function get_value($string)
 
77
    {
 
78
        // now just drop the msb and reorder it + parse it in own string
 
79
        $valuestring = '';
 
80
        $string_length = strlen($string);
 
81
 
 
82
        $i = 1;
 
83
 
 
84
        while ($string_length > $i) {
 
85
            // unset msb string and reorder it
 
86
            $valuestring = substr($string, $i, 7) . $valuestring;
 
87
            $i += 8;
 
88
        }
 
89
 
 
90
        // now interprete it
 
91
        return bindec($valuestring);
 
92
    }
 
93
 
 
94
 
 
95
    /**
 
96
     * Converts hex 2 ascii
 
97
     *
 
98
     * @param String $hex - the hex string
 
99
     */
 
100
    public function hex_to_str($hex)
 
101
    {
 
102
        $str = '';
 
103
 
 
104
        for ($i = 0; $i < strlen($hex); $i += 2) {
 
105
            $str .= chr(hexdec(substr($hex, $i, 2)));
 
106
        }
 
107
        return $str;
 
108
    }
 
109
 
 
110
}
 
111
 
 
112
?>