aboutsummaryrefslogtreecommitdiffhomepage
path: root/inc/3rdparty/libraries/MOBIClass/FileObject.php
blob: 0df17df13c3eb639921a61776c35344e75b210c4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php

/**
 * Description of FileObject
 *
 * @author Sander
 */
abstract class FileObject {
	private $byteLength = -1;

	public function __construct($byteLength = -1){
		$this->byteLength = $byteLength;
	}

	public function getByteLength(){
		if($this->byteLength >= 0){
			return $this->byteLength;
		}
		return $this->getLength();
	}

	public function getLength(){
		throw new Exception("Sub-class needs to implement this if it doesn't have a fixed length");
	}

	/**
	 * Convert a string to byte format (maximum 4 bytes)
	 * @param string $string Input string
	 * @return int Output integer
	 */
	public function toInt($string){
		$out = 0;
		for($i = 0, $len = min(4, strlen($string)); $i < $len; $i++){
			$out = $out | (ord($string[$i]) << (($len-$i-1)*8));
		}
		return $out;
	}

	/**
	 * Convert a byte (stored in an integer) to a string
	 * @param byte $int
	 * @return string
	 */
	public function byteToString($int){
		return $this->toString($int, 1);
	}

	/**
	 * Convert a byte (stored in an integer) to a string
	 * @param byte $int
	 * @return string
	 */
	public function byteAsString($int){
		return $this->asString($int, 1);
	}

	/**
	 * Convert a short (stored in an integer) to a string
	 * @param short $int
	 * @return string
	 */
	public function shortToString($int){
		return $this->toString($int, 2);
	}

	/**
	 * Convert a short (stored in an integer) to a string
	 * @param short $int
	 * @return string
	 */
	public function shortAsString($int){
		return $this->asString($int, 2);
	}

	/**
	 * Convert a tri-byte (stored in an integer) to a string
	 * @param tri-byte $int
	 * @return string
	 */
	public function triToString($int){
		return $this->toString($int, 3);
	}

	/**
	 * Convert a tri-byte (stored in an integer) to a string
	 * @param tri-byte $int
	 * @return string
	 */
	public function triAsString($int){
		return $this->asString($int, 3);
	}

	/**
	 * Convert an integer to a string
	 * @param int $int
	 * @return string
	 */
	public function intToString($int){
		return $this->toString($int, 4);
	}

	/**
	 * Convert an integer to a string
	 * @param int $int
	 * @return string
	 */
	public function intAsString($int){
		return $this->asString($int, 4);
	}

	/**
	 * Convert a number of n bytes to a string
	 * @param int $int Number that should be converted
	 * @param int $size Number of bytes to convert
	 * @return string Output string
	 */
	private function toString($int, $size){
		$out = "";
		for($i = 0; $i < $size; $i++){
			$out = chr($int & 0xFF).$out;
			$int = $int >> 8;
		}
		return $out;
	}

	/**
	 * Convert a number of n bytes to a string
	 * @param int $int Number that should be converted
	 * @param int $size Number of bytes to convert
	 * @return string Output string
	 */
	private function asString($int, $size){
		$out = "";
		for($i = 0; $i < $size; $i++){
			if($i > 0) $out = " ".$out;
			$byte = dechex($int & 0xFF);
			if(strlen($byte) == 1) $byte = "0".$byte;
			$out = $byte.$out;
			$int = $int >> 8;
		}
		return $out;
	}

	/**
	 * Get the value
	 * @return mixed Value to get
	 */
    abstract public function get();

	/**
	 * Set the value
	 * @return mixed Value to set
	 */
    abstract public function set($value);

	/**
	 * Serialize the object
	 * @return string String representation
	 */
    abstract public function serialize();

	/**
	 * Unserialize the object
	 * @param string $data String representation
	 */
    abstract public function unserialize($data);
}
?>