UUlib(3)
π UUlib(3) β Convert::UUlib π
okay so bestie, welcome to UUlib(3) β the Convert::UUlib page from User Contributed Perl Documentation, part 1 of 2 π«Ά. Grab a snack, we're decoding a truly massive pile of files.
β¨ NAME β¨
Convert::UUlib β decode uu/xx/b64/mime/yenc/etc-encoded data from a massive number of files. That's the whole vibe: decoder central, no cap. π
πΈ SYNOPSIS πΈ
Short version, bestie: load files, loop over them, check state, decode, print filename. Like this, exactly:
use Convert::UUlib ':all';
# read all the files named on the commandline and decode them
# into the CURRENT directory. See below for a longer example.
LoadFile $_ for @ARGV;
for my $uu (GetFileList) {
if ($uu->state & FILE_OK) {
$uu->decode;
print $uu->filename, "\n";
}
}
π¦ DESCRIPTION π¦
This module started as an interface to the uulib/uudeview library by Frank Pilhofer, which can be used to decode all kinds of usenet (and other) binary messages. Then upstream abandoned the project, and the library got continuously bugfixed and improved in this module, with major focuses on security fixes, correctness and speed. That does not mean this library is considered safe with untrusted data, but it surely is safer than the original uudeview. π«Ά
Read the file doc/library.pdf from the distribution for in-depth information about the C-library used in this interface, and the rest of this document and especially the non-trivial decoder program at the end. iykyk. π
π EXPORTED CONSTANTS π
π¬ Action code constants π¬
ACT_IDLEβ we don't do anything π΄ACT_SCANNINGβ scanning an input file πACT_DECODINGβ decoding into a temp file π§ACT_COPYINGβ copying temp to target π¦ACT_ENCODINGβ encoding a file β¨
π¨ Message severity levels π¨
MSG_MESSAGEβ just a message, nothing important π¬MSG_NOTEβ something that should be noticed πMSG_WARNINGβ important msg, processing continues β οΈMSG_ERRORβ processing has been terminated πMSG_FATALβ decoder cannot process further requests πMSG_PANICβ recovery impossible, app must terminate π±
βοΈ Options βοΈ
OPT_VERSIONβ version numberMAJOR.MINORplPATCH(ro) πOPT_FASTβ assumes only one part per file πOPT_DUMBNESSβ switch off the program's intelligence π΅βπ«OPT_BRACKPOLβ give numbers in[]higher precedence π§OPT_VERBOSEβ generate informative messages π£οΈOPT_DESPERATEβ try to decode incomplete files πOPT_IGNREPLYβ ignoreRE:plies (off by default) πOPT_OVERWRITEβ whether it's OK to overwrite ex. files π§OPT_SAVEPATHβ prefix to save-files on disk πOPT_IGNMODEβ ignore the original file mode πOPT_DEBUGβ print messages withFILE/LINEinfo πOPT_ERRNOβ get last error code forRET_IOERR(ro) π¨OPT_PROGRESSβ retrieve progress information πOPT_USETEXTβ handle text messages πOPT_PREAMBβ handle Mime preambles/epilogues πOPT_TINYB64β detect shortB64outside of Mime πOPT_ENCEXTβ extension for single-part encoded files π·οΈOPT_REMOVEβ remove input files after decoding (dangerous) πOPT_MOREMIMEβ strict MIME adherence π§OPT_DOTDOTβ".."-unescaping has not yet been done on input files π§OPT_RBUFβ set default read I/O buffer size in bytes π₯OPT_WBUFβ set default write I/O buffer size in bytes π€OPT_AUTOCHECKβ automatically check file list after everyloadfileβ
π Result/Error codes π
RET_OKβ everything went fine βRET_IOERRβ I/O Error - examine errno π¨RET_NOMEMβ not enough memory π΅RET_ILLVALβ illegal value for operation π«RET_NODATAβ decoder didn't find any data π³οΈRET_NOENDβ encoded data wasn't ended properly πRET_UNSUPβ unsupported function (encoding) πRET_EXISTSβ file exists (decoding) πRET_CONTβ continue -- special fromScanPartβ‘οΈRET_CANCELβ operation canceled β
ποΈ File States ποΈ
This code is zero, i.e. "false":
UUFILE_READβ Read in, but not further processed π«Ά
The following state codes are or'ed together:
FILE_MISPARTβ Missing Part(s) detected π§©FILE_NOBEGINβ No'begin'found π«FILE_NOENDβ No'end'found πFILE_NODATAβ File does not contain valid uudata π³οΈFILE_OKβ All Parts found, ready to decode βFILE_ERRORβ Error while decoding πFILE_DECODEDβ Successfully decoded πFILE_TMPFILEβ Temporary decoded file exists π
𧬠Encoding types π§¬
UU_ENCODEDβ UUencoded data πB64_ENCODEDβ Mime-Base64 data π§¬XX_ENCODEDβ XXencoded data βBH_ENCODEDβ Binhex encoded πPT_ENCODEDβ Plain-Text encoded (MIME) πQP_ENCODEDβ Quoted-Printable (MIME) π§ΎYENC_ENCODEDβ yEnc encoded (non-MIME) π¦
β¨ EXPORTED FUNCTIONS β¨
π§Ό Initializing and cleanup π§Ό
Initialize is automatically called when the module is loaded and allocates quite a small amount of memory for today's machines ;) CleanUp releases that again.
On my machine, a fairly complete decode with DBI backend needs about 10MB RSS to decode 20000 files.
π§Ή CleanUp π§Ή
Release memory, file items and clean up files. Should be called after a decoding run, if you want to start a new one. π«§
βοΈ Setting and querying options βοΈ
$option = GetOption OPT_xxx
SetOption OPT_xxx, opt-value
See the OPT_xxx constants above to see which options exist. Easy peasy. π
π Setting various callbacks π
SetMsgCallback [callback-function]
SetBusyCallback [callback-function]
SetFileCallback [callback-function]
SetFNameFilter [callback-function]
π Call the currently selected FNameFilter π
$file = FNameFilter $file
π₯ Loading sourcefiles, optionally fuzzy merge and start decoding π₯
π«Ά ($retval, $count) = LoadFile $fname, [$id, [$delflag, [$partno]]] π«Ά
Load the given file and scan it for encoded contents. Optionally tag it with the given $id, and if $delflag is true, delete the file after it is no longer necessary. If you are certain of the part number, you can specify it as the last argument.
A better (usually faster) way of doing this is using the SetFNameFilter functionality. π₯
π $retval = Smerge $pass π
If you are desperate, try to call Smerge with increasing $pass values, beginning at 0, to try to merge parts that usually would not have been merged.
Careful, bestie: most probably this will result in garbled files, so never do this by default, except: if theOPT_AUTOCHECKoption has been disabled (by default it is enabled) to speed up file loading, then you have to callSmerge -1after loading all files as an additional pre-pass (which is normally done byLoadFile). π
π $item = GetFileListItem $item_number π
Return the $item structure for the $item_number'th found file, or undef if no file with that number exists.
The first file has number 0, and the series has no holes, so you can iterate over all files by starting with zero and incrementing until you hit undef.
This function has to walk the linear list of files on each access, so if you want to iterate over all items, it is usually faster to use GetFileList.
π @items = GetFileList π
Similar to GetFileListItem, but returns all files in one go, which is very much faster for large number of items, and has no drawbacks when used for a small number of items. Obsessed. π
π§ Decoding files π§
π·οΈ $retval = $item->rename ($newname) π·οΈ
Change the ondisk filename where the decoded file will be saved.
π $retval = $item->decode_temp π
Decode the file into a temporary location, use $item->infile to retrieve the temporary filename.
ποΈ $retval = $item->remove_temp ποΈ
Remove the temporarily decoded file again.
π― $retval = $item->decode ([$target_path]) π―
Decode the file to its destination, or the given target path.
π $retval = $item->info (callback-function) π
That's the signature, bestie β this one is just listed here. π«Ά
π Querying (and setting) item attributes π
$state = $item->state
$mode = $item->mode ([newmode])
$uudet = $item->uudet
$size = $item->size
$filename = $item->filename ([newfilename})
$subfname = $item->subfname
$mimeid = $item->mimeid
$mimetype = $item->mimetype
$binfile = $item->binfile
π§© Information about source parts π§©
π§΅ $parts = $item->parts π§΅
Return information about all parts (source files) used to decode the file as a list of hashrefs with the following structure:
{
partno => <integer describing the part number, starting with 1>,
# the following member sonly exist when they contain useful information
sfname => <local pathname of the file where this part is from>,
filename => <the ondisk filename of the decoded file>,
subfname => <used to cluster postings, possibly the posting filename>,
subject => <the subject of the posting/mail>,
origin => <the possible source (From) address>,
mimetype => <the possible mimetype of the decoded file>,
mimeid => <the id part of the Content-Type>,
}
Usually you are interested mostly the sfname and possibly the partno and filename members.
π Functions below are not documented and not very well tested - feedback welcome π
QuickDecodeEncodeMultiEncodePartialEncodeToStreamEncodeToFileE_PrepSingleE_PrepPartial
These are here, but the original page says they're not documented and not very well tested β feedback welcome. So don't come for me if they act up. π
π EXTENSION FUNCTIONS π
Functions found in this module but not documented in the uulib documentation:
$msg = straction ACT_xxxβ Return a human readable string representing the given action code. π¬$msg = strerror RET_xxxβ Return a human readable string representing the given error code. π¨$str = strencoding xxx_ENCODEDβ Return the name of the encoding type as a string. π·οΈ$str = strmsglevel MSG_xxxβ Returns the message level as a string. πΆ
π«Ά SetFileNameCallback $cb π«Ά
Sets (or queries) the FileNameCallback, which is called whenever the decoding library can't find a filename and wants to extract a filename from the subject line of a posting. The callback will be called with two arguments, the subject line and the current candidate for the filename. The latter argument can be undef, which means that no filename could be found (and likely no one exists, so it is safe to also return undef in this case). If it doesn't return anything (not even undef!), then nothing happens, so this is a no-op callback:
sub cb {
return ();
}
If it returns undef, then this indicates that no filename could be found. In all other cases, the return value is taken to be the filename.
This is a slightly more useful callback:
sub cb {
return unless $_[1]; # skip "Re:"-plies et al.
my ($subject, $filename) = @_;
# if we find some *.rar, take it
return $1 if $subject =~ /(\w+\.rar)/;
# otherwise just pass what we have
return ();
}
πΈ LARGE EXAMPLE DECODER πΈ
okay so the decode workflow is literally this, bestie:
- Set options with
SetOptionorSetXXXCallback. - Load all source files with
LoadFile. - Optionally
Smerge. - Iterate over all
GetFileListitems (i.e. result files). CleanUpto delete files and free items.
The page then gives you the actual example-decoder file from the distribution to illustrate that workflow in a non-trivial example. Here it is, verbatim, no cap:
#!/usr/bin/perl
# decode all the files in the directory uusrc/ and copy
# the resulting files to uudst/
use Convert::UUlib ':all';
sub namefilter {
my ($path) = @_;
$path=~s/^.*[\/\\]//;
$path
}
sub busycb {
my ($action, $curfile, $partno, $numparts, $percent, $fsize) = @_;
$_[0]=straction($action);
print "busy_callback(", (join ",",@_), ")\n";
0
}
SetOption OPT_RBUF, 128*1024;
SetOption OPT_WBUF, 1024*1024;
SetOption OPT_IGNMODE, 1;
SetOption OPT_IGNMODE, 1;
SetOption OPT_VERBOSE, 1;
SetOption OPT_AUTOCHK, 0;
# show the three ways you can set callback functions. I normally
# prefer the one with the sub inplace.
SetFNameFilter \&namefilter;
SetBusyCallback "busycb", 333;
SetMsgCallback sub {
my ($msg, $level) = @_;
print uc strmsglevel $_[1], ": $msg\n";
};
# the following non-trivial FileNameCallback takes care
# of some subject lines not detected properly by uulib:
SetFileNameCallback sub {
return unless $_[1]; # skip "Re:"-plies et al.
local $_ = $_[0];
# the following rules are rather effective on some newsgroups,
# like alt.binaries.games.anime, where non-mime, uuencoded data
# is very common
# if we find some *.rar, take it as the filename
return $1 if /(\S{3,}\.(?:[rstuvwxyz]\d\d|rar))\s/i;
# one common subject format
return $1 if /- "(.{2,}?\..+?)" (?:yenc )?\(\d+\/\d+\)/i;
# - filename.par (04/55)
return $1 if /- "?(\S{3,}\.\S+?)"? (?:yenc )?\(\d+\/\d+\)/i;
# - (xxx) No. 1 sayuri81.jpg 756565 bytes
# - (20 files) No.17 Roseanne.jpg [2/2]
return $1 if /No\.[ 0-9]+ (\S+\....) (?:\d+ bytes )?\[/;
# try to detect some common forms of filenames
return $1 if /([a-z0-9_\-+.]{3,}\.[a-z]{3,4}(?:.\d+))/i;
# otherwise just pass what we have
()
};
# now read all files in the directory uusrc/*
for (<uusrc/*>) {
my ($retval, $count) = LoadFile ($_, $_, 1);
print "file($_), status(", strerror $retval, ") parts($count)\n";
}
Smerge -1;
SetOption OPT_SAVEPATH, "uudst/";
# now wade through all files and their source parts
for my $uu (GetFileList) {
print "file ", $uu->filename, "\n";
print " state ", $uu->state, "\n";
print " mode ", $uu->mode, "\n";
print " uudet ", strencoding $uu->uudet, "\n";
print " size ", $uu->size, "\n";
print " subfname ", $uu->subfname, "\n";
print " mimeid ", $uu->mimeid, "\n";
print " mimetype ", $uu->mimetype, "\n";
# print additional info about all parts
print " parts";
for ($uu->parts) {
for my $k (sort keys %$_) {
print " $k=$_->{$k}";
}
print "\n";
}
$uu->remove_temp;
if (my $err = $uu->decode) {
print " ERROR ", strerror $err, "\n";
} else {
print " successfully saved as uudst/", $uu->filename, "\n";
}
}
print "cleanup...\n";
CleanUp;
That's the whole non-trivial example β LoadFile, optional Smerge, GetFileList, then CleanUp. Iconic. π
π§ PERLMULTICORE SUPPORT π§
This module supports the perlmulticore standard (see <http://perlmulticore.schmorp.de/> for more info) for the following functions β generally these are the ones accessing the disk and/or using considerable CPU time:
LoadFile
$item->decode
$item->decode_temp
$item->remove_temp
$item->info
The perl interpreter gets reacquired/released on every callback invocation, so for performance reasons, callbacks should be avoided if that is costly.
Future versions might enable multicore support for more functions.
π BUGS AND LIMITATIONS π
The original uulib library this module uses was written back when main memory was measured in megabytes and buffer overflows as a security thing weren't even a concept. A lot of security fixes have been applied over the years β including some defense in depth mechanism that can shield against a lot of as-of-yet undetected bugs β but using this library for security purposes requires care.
Likewise, file sizes when the uulib library was written were tiny compared to today, so do not expect this library to handle files larger than 2GB.
Lastly, this module uses a very "C-like" interface, which means it doesn't protect you from invalid points as you might expect from "more perlish" modules β for example, accessing a file item object after calling CleanUp will likely result in crashes, memory corruption, or worse. π
π AUTHOR π
Marc Lehmann <schmorp@schmorp.de> β the original uulib library was written by Frank Pilhofer <fp@informatik.uni-frankfurt.de>, and later heavily bugfixed by Marc Lehmann.
π¦ SEE ALSO π¦
perl(1), and the uudeview homepage at <http://www.fpx.de/fp/Software/UUDeview/>. Go click around, bestie. β¨
This page is UUlib(3) from perl v5.40.2, dated 2024-06-10 β love u, go un-uuencode your dreams ππ
π¦ where this page comes from
- alpine 3.22: perl-convert-uulib-doc 1.8-r4 Β· (GPL-1.0-or-later OR Artistic-1.0-Perl) AND GPL-2.0-or-later AND Zlib
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms