├── .gitignore ├── .travis.yml ├── phpunit.xml.dist ├── composer.json ├── LICENSE ├── README.md ├── Buffer.php └── Tests └── BufferTest.php /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor/ 2 | composer.lock 3 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | 3 | php: 4 | - 5.3.3 5 | - 5.3 6 | - 5.4 7 | - 5.5 8 | 9 | before_script: 10 | - composer install --prefer-source --dev 11 | 12 | script: phpunit --coverage-text --verbose 13 | -------------------------------------------------------------------------------- /phpunit.xml.dist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Tests 6 | 7 | 8 | 9 | 10 | 11 | . 12 | 13 | ./Tests/ 14 | ./vendor/ 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "dflydev/snort-buffer", 3 | "description": "Byte buffer statistics and analaysis", 4 | "license": "MIT", 5 | "authors": [ 6 | { 7 | "name": "Dragonfly Development Inc.", 8 | "email": "info@dflydev.com", 9 | "homepage": "http://dflydev.com" 10 | }, 11 | { 12 | "name": "Beau Simensen", 13 | "email": "beau@dflydev.com", 14 | "homepage": "http://beausimensen.com" 15 | } 16 | ], 17 | "require": { 18 | "php": ">=5.3.3" 19 | }, 20 | "autoload": { 21 | "psr-0": { 22 | "Dflydev\\Snort\\Buffer": "" 23 | } 24 | }, 25 | "target-dir": "Dflydev/Snort/Buffer", 26 | "extra": { 27 | "branch-alias": { 28 | "dev-master": "1.0-dev" 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2013 Dragonfly Development Inc. 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is furnished 8 | to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Snort - Buffer 2 | ============== 3 | 4 | Byte buffer statistics and analaysis. 5 | 6 | 7 | Requirements 8 | ------------ 9 | 10 | * PHP 5.3+ 11 | 12 | 13 | Installation 14 | ------------ 15 | 16 | Through [Composer][1] as [dflydev/snort-buffer][2]. 17 | 18 | 19 | Usage 20 | ----- 21 | 22 | ```php 23 | addData($bytes, 0, strlen($bytes)); 32 | 33 | // Number of occurances of the ASCII letter 'a'. 34 | print $buffer->count(0x61) . "\n"; 35 | 36 | // Number of occurances of any ASCII character 37 | // between 32-127. 38 | print $buffer->countRange(0x20, 128) . "\n"; 39 | ``` 40 | 41 | 42 | License 43 | ------- 44 | 45 | MIT, see LICENSE. 46 | 47 | 48 | Community 49 | --------- 50 | 51 | If you have questions or want to help out, join us in the **#dflydev** channel 52 | on **irc.freenode.net** or mention [@dflydev][4] on Twitter. 53 | 54 | 55 | Not Invented Here 56 | ----------------- 57 | 58 | This work was heavily influenced by [Apache Tika][3]. 59 | 60 | 61 | [1]: http://getcomposer.org 62 | [2]: https://packagist.org/packages/dflydev/snort 63 | [3]: http://tika.apache.org 64 | [4]: https://twitter.com/dflydev 65 | -------------------------------------------------------------------------------- /Buffer.php: -------------------------------------------------------------------------------- 1 | counts[$asciiValue])) { 17 | $this->counts[$asciiValue] = 0; 18 | } 19 | 20 | $this->counts[$asciiValue]++; 21 | $this->bytes[] = $byte; 22 | $this->total++; 23 | } 24 | } 25 | 26 | public function total() 27 | { 28 | return $this->total; 29 | } 30 | 31 | public function count($asciiValue) 32 | { 33 | if (isset($this->counts[$asciiValue])) { 34 | return $this->counts[$asciiValue]; 35 | } 36 | 37 | return 0; 38 | } 39 | 40 | public function countRange($fromInclusive, $toExclusive) 41 | { 42 | if ($fromInclusive < 0) { 43 | throw new \InvalidArgumentException('Invalid range; cannot count from a negative "from" value.'); 44 | } 45 | 46 | if ($fromInclusive > $toExclusive) { 47 | throw new \InvalidArgumentException('Invalid range; cannot count from a "from" value that is larger than the "to" value.'); 48 | } 49 | 50 | if ($fromInclusive === $toExclusive) { 51 | throw new \InvalidArgumentException('Invalid range; cannot count from a "from" value to the same "to" value since "to" value is exclusive.'); 52 | } 53 | 54 | $count = 0; 55 | for ($i = $fromInclusive; $i < $toExclusive; $i++) { 56 | $count += $this->count($i); 57 | } 58 | 59 | return $count; 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /Tests/BufferTest.php: -------------------------------------------------------------------------------- 1 | assertEquals(0, $buffer->total()); 14 | } 15 | 16 | public function testAddDataAddsData() 17 | { 18 | $data = "data"; 19 | 20 | $buffer = new Buffer; 21 | 22 | $buffer->addData($data, 0, strlen($data)); 23 | $this->assertEquals(4, $buffer->total()); 24 | 25 | $buffer->addData($data, 0, strlen($data)); 26 | $this->assertEquals(8, $buffer->total()); 27 | } 28 | 29 | public function testCount() 30 | { 31 | $data = 'aaaabbbccd'; 32 | 33 | $buffer = new Buffer; 34 | 35 | $this->assertEquals(0, $buffer->count(ord('a'))); 36 | $this->assertEquals(0, $buffer->count(ord('b'))); 37 | $this->assertEquals(0, $buffer->count(ord('c'))); 38 | $this->assertEquals(0, $buffer->count(ord('d'))); 39 | $this->assertEquals(0, $buffer->count(ord('e'))); 40 | 41 | $buffer->addData($data, 0, strlen($data)); 42 | 43 | $this->assertEquals(4, $buffer->count(ord('a'))); 44 | $this->assertEquals(3, $buffer->count(ord('b'))); 45 | $this->assertEquals(2, $buffer->count(ord('c'))); 46 | $this->assertEquals(1, $buffer->count(ord('d'))); 47 | $this->assertEquals(0, $buffer->count(ord('e'))); 48 | } 49 | 50 | public function testAddDataOffset() 51 | { 52 | $data = 'aaaabbbccd'; 53 | 54 | $buffer = new Buffer; 55 | 56 | $buffer->addData($data, 3, strlen($data) - 3); 57 | 58 | $this->assertEquals(1, $buffer->count(ord('a'))); 59 | $this->assertEquals(3, $buffer->count(ord('b'))); 60 | $this->assertEquals(2, $buffer->count(ord('c'))); 61 | $this->assertEquals(1, $buffer->count(ord('d'))); 62 | $this->assertEquals(0, $buffer->count(ord('e'))); 63 | } 64 | 65 | public function testCountRange() 66 | { 67 | $data = 'aaaabbbccd'; 68 | 69 | $buffer = new Buffer; 70 | 71 | $buffer->addData($data, 0, strlen($data)); 72 | 73 | $this->assertEquals(10, $buffer->countRange(ord('a'), ord('z'))); 74 | $this->assertEquals(10, $buffer->countRange(ord('a'), ord('e'))); 75 | $this->assertEquals(9, $buffer->countRange(ord('a'), ord('d'))); 76 | $this->assertEquals(6, $buffer->countRange(ord('b'), ord('e'))); 77 | } 78 | 79 | public function testCountRangeSameValueForToAndFrom() 80 | { 81 | $data = 'aaaabbbccd'; 82 | 83 | $buffer = new Buffer; 84 | 85 | $buffer->addData($data, 0, strlen($data)); 86 | 87 | $this->assertEquals(4, $buffer->countRange(ord('a'), ord('b'))); 88 | $this->assertEquals(1, $buffer->countRange(ord('d'), ord('e'))); 89 | $this->assertEquals(0, $buffer->countRange(ord('e'), ord('f'))); 90 | $this->assertEquals(0, $buffer->countRange(ord('y'), ord('z'))); 91 | } 92 | 93 | /** 94 | * @expectedException InvalidArgumentException 95 | */ 96 | public function testCountRangeNegativeFrom() 97 | { 98 | $buffer = new Buffer; 99 | $buffer->countRange(-1, 0); 100 | } 101 | 102 | /** 103 | * @expectedException InvalidArgumentException 104 | */ 105 | public function testCountRangeFromEqualsTo() 106 | { 107 | $buffer = new Buffer; 108 | $buffer->countRange(0, 0); 109 | } 110 | 111 | /** 112 | * @expectedException InvalidArgumentException 113 | */ 114 | public function testCountRangeToLessThanFrom() 115 | { 116 | $buffer = new Buffer; 117 | $buffer->countRange(1, 0); 118 | } 119 | } 120 | --------------------------------------------------------------------------------