source: branches/version-2_5-dev/data/module/Compat/Compat/Function/convert_uuencode.php @ 19792

Revision 19792, 3.0 KB checked in by Seasoft, 13 years ago (diff)

#744(PHP4 互換用途ソースを将来的に切り捨てやすい仕組みづくり)

Line 
1<?php
2// +----------------------------------------------------------------------+
3// | PHP Version 4                                                        |
4// +----------------------------------------------------------------------+
5// | Copyright (c) 1997-2004 The PHP Group                                |
6// +----------------------------------------------------------------------+
7// | This source file is subject to version 3.0 of the PHP license,       |
8// | that is bundled with this package in the file LICENSE, and is        |
9// | available at through the world-wide-web at                           |
10// | http://www.php.net/license/3_0.txt.                                  |
11// | If you did not receive a copy of the PHP license and are unable to   |
12// | obtain it through the world-wide-web, please send a note to          |
13// | license@php.net so we can mail you a copy immediately.               |
14// +----------------------------------------------------------------------+
15// | Authors: Michael Wallner <mike@php.net>                              |
16// |          Aidan Lister <aidan@php.net>                                |
17// +----------------------------------------------------------------------+
18//
19// $Id: convert_uuencode.php,v 1.7 2005/01/26 04:55:13 aidan Exp $
20
21
22/**
23 * Replace convert_uuencode()
24 *
25 * @category    PHP
26 * @package     PHP_Compat
27 * @link        http://php.net/function.convert_uuencode
28 * @author      Michael Wallner <mike@php.net>
29 * @author      Aidan Lister <aidan@php.net>
30 * @version     $Revision: 1.7 $
31 * @since       PHP 5
32 * @require     PHP 4.0.0 (user_error)
33 */
34if (!function_exists('convert_uuencode')) {
35    function convert_uuencode($string)
36    {
37        // Sanity check
38        if (!is_scalar($string)) {
39            user_error('convert_uuencode() expects parameter 1 to be string, ' .
40                gettype($string) . ' given', E_USER_WARNING);
41            return false;
42        }
43
44        $u = 0;
45        $encoded = '';
46       
47        while ($c = count($bytes = unpack('c*', substr($string, $u, 45)))) {
48            $u += 45;
49            $encoded .= pack('c', $c + 0x20);
50
51            while ($c % 3) {
52                $bytes[++$c] = 0;
53            }
54
55            foreach (array_chunk($bytes, 3) as $b) {
56                $b0 = ($b[0] & 0xFC) >> 2;
57                $b1 = (($b[0] & 0x03) << 4) + (($b[1] & 0xF0) >> 4);
58                $b2 = (($b[1] & 0x0F) << 2) + (($b[2] & 0xC0) >> 6);
59                $b3 = $b[2] & 0x3F;
60               
61                $b0 = $b0 ? $b0 + 0x20 : 0x60;
62                $b1 = $b1 ? $b1 + 0x20 : 0x60;
63                $b2 = $b2 ? $b2 + 0x20 : 0x60;
64                $b3 = $b3 ? $b3 + 0x20 : 0x60;
65               
66                $encoded .= pack('c*', $b0, $b1, $b2, $b3);
67            }
68
69            $encoded .= "\n";
70        }
71       
72        // Add termination characters
73        $encoded .= "\x60\n";
74
75        return $encoded;
76    }
77}
78
79?>
Note: See TracBrowser for help on using the repository browser.