diff options
Diffstat (limited to 'apt-pkg/contrib')
-rw-r--r-- | apt-pkg/contrib/cdromutl.cc | 51 | ||||
-rw-r--r-- | apt-pkg/contrib/configuration.cc | 43 | ||||
-rw-r--r-- | apt-pkg/contrib/error.h | 29 | ||||
-rw-r--r-- | apt-pkg/contrib/fileutl.cc | 124 | ||||
-rw-r--r-- | apt-pkg/contrib/fileutl.h | 8 | ||||
-rw-r--r-- | apt-pkg/contrib/hashes.cc | 4 | ||||
-rw-r--r-- | apt-pkg/contrib/macros.h (renamed from apt-pkg/contrib/system.h) | 16 | ||||
-rw-r--r-- | apt-pkg/contrib/md5.cc | 3 | ||||
-rw-r--r-- | apt-pkg/contrib/netrc.cc | 211 | ||||
-rw-r--r-- | apt-pkg/contrib/netrc.h | 29 | ||||
-rw-r--r-- | apt-pkg/contrib/sha1.cc | 2 | ||||
-rw-r--r-- | apt-pkg/contrib/strutl.cc | 49 |
12 files changed, 462 insertions, 107 deletions
diff --git a/apt-pkg/contrib/cdromutl.cc b/apt-pkg/contrib/cdromutl.cc index 0cf9697ac..6dce82fe1 100644 --- a/apt-pkg/contrib/cdromutl.cc +++ b/apt-pkg/contrib/cdromutl.cc @@ -64,35 +64,44 @@ bool UnmountCdrom(string Path) { if (IsMounted(Path) == false) return true; - - int Child = ExecFork(); - // The child - if (Child == 0) + for (int i=0;i<3;i++) { - // Make all the fds /dev/null - for (int I = 0; I != 3; I++) - dup2(open("/dev/null",O_RDWR),I); + + int Child = ExecFork(); - if (_config->Exists("Acquire::cdrom::"+Path+"::UMount") == true) + // The child + if (Child == 0) { - if (system(_config->Find("Acquire::cdrom::"+Path+"::UMount").c_str()) != 0) + // Make all the fds /dev/null + for (int I = 0; I != 3; I++) + dup2(open("/dev/null",O_RDWR),I); + + if (_config->Exists("Acquire::cdrom::"+Path+"::UMount") == true) + { + if (system(_config->Find("Acquire::cdrom::"+Path+"::UMount").c_str()) != 0) + _exit(100); + _exit(0); + } + else + { + const char *Args[10]; + Args[0] = "umount"; + Args[1] = Path.c_str(); + Args[2] = 0; + execvp(Args[0],(char **)Args); _exit(100); - _exit(0); + } } - else - { - const char *Args[10]; - Args[0] = "umount"; - Args[1] = Path.c_str(); - Args[2] = 0; - execvp(Args[0],(char **)Args); - _exit(100); - } + + // if it can not be umounted, give it a bit more time + // this can happen when auto-mount magic or fs/cdrom prober attack + if (ExecWait(Child,"umount",true) == true) + return true; + sleep(1); } - // Wait for mount - return ExecWait(Child,"umount",true); + return false; } /*}}}*/ // MountCdrom - Mount a cdrom /*{{{*/ diff --git a/apt-pkg/contrib/configuration.cc b/apt-pkg/contrib/configuration.cc index ff49ce857..7588b041c 100644 --- a/apt-pkg/contrib/configuration.cc +++ b/apt-pkg/contrib/configuration.cc @@ -22,14 +22,8 @@ #include <apti18n.h> #include <vector> -#include <algorithm> #include <fstream> #include <iostream> - -#include <stdio.h> -#include <dirent.h> -#include <sys/stat.h> -#include <unistd.h> using namespace std; /*}}}*/ @@ -835,39 +829,10 @@ bool ReadConfigFile(Configuration &Conf,const string &FName,bool const &AsSectio // ReadConfigDir - Read a directory of config files /*{{{*/ // --------------------------------------------------------------------- /* */ -bool ReadConfigDir(Configuration &Conf,const string &Dir,bool const &AsSectional, - unsigned const &Depth) -{ - DIR *D = opendir(Dir.c_str()); - if (D == 0) - return _error->Errno("opendir",_("Unable to read %s"),Dir.c_str()); - - vector<string> List; - - for (struct dirent *Ent = readdir(D); Ent != 0; Ent = readdir(D)) - { - if (Ent->d_name[0] == '.') - continue; - - // Skip bad file names ala run-parts - const char *C = Ent->d_name; - for (; *C != 0; C++) - if (isalpha(*C) == 0 && isdigit(*C) == 0 && *C != '_' && *C != '-') - break; - if (*C != 0) - continue; - - // Make sure it is a file and not something else - string File = flCombine(Dir,Ent->d_name); - struct stat St; - if (stat(File.c_str(),&St) != 0 || S_ISREG(St.st_mode) == 0) - continue; - - List.push_back(File); - } - closedir(D); - - sort(List.begin(),List.end()); +bool ReadConfigDir(Configuration &Conf,const string &Dir, + bool const &AsSectional, unsigned const &Depth) +{ + vector<string> const List = GetListOfFilesInDir(Dir, "conf", true, true); // Read the files for (vector<string>::const_iterator I = List.begin(); I != List.end(); I++) diff --git a/apt-pkg/contrib/error.h b/apt-pkg/contrib/error.h index d92e40e9c..90747ff7e 100644 --- a/apt-pkg/contrib/error.h +++ b/apt-pkg/contrib/error.h @@ -40,27 +40,15 @@ #ifndef PKGLIB_ERROR_H #define PKGLIB_ERROR_H +#include <apt-pkg/macros.h> - -#ifdef __GNUG__ -// Methods have a hidden this parameter that is visible to this attribute -#define APT_MFORMAT1 __attribute__ ((format (printf, 2, 3))) -#define APT_MFORMAT2 __attribute__ ((format (printf, 3, 4))) -#else -#define APT_MFORMAT1 -#define APT_MFORMAT2 -#endif - #include <string> -#include <system.h> - -using std::string; class GlobalError { struct Item { - string Text; + std::string Text; bool Error; Item *Next; }; @@ -72,18 +60,18 @@ class GlobalError public: // Call to generate an error from a library call. - bool Errno(const char *Function,const char *Description,...) APT_MFORMAT2 __cold; - bool WarningE(const char *Function,const char *Description,...) APT_MFORMAT2 __cold; + bool Errno(const char *Function,const char *Description,...) __like_printf_2 __cold; + bool WarningE(const char *Function,const char *Description,...) __like_printf_2 __cold; /* A warning should be considered less severe than an error, and may be ignored by the client. */ - bool Error(const char *Description,...) APT_MFORMAT1 __cold; - bool Warning(const char *Description,...) APT_MFORMAT1 __cold; + bool Error(const char *Description,...) __like_printf_1 __cold; + bool Warning(const char *Description,...) __like_printf_1 __cold; // Simple accessors inline bool PendingError() {return PendingFlag;}; inline bool empty() {return List == 0;}; - bool PopMessage(string &Text); + bool PopMessage(std::string &Text); void Discard(); // Usefull routine to dump to cerr @@ -96,7 +84,4 @@ class GlobalError GlobalError *_GetErrorObj(); #define _error _GetErrorObj() -#undef APT_MFORMAT1 -#undef APT_MFORMAT2 - #endif diff --git a/apt-pkg/contrib/fileutl.cc b/apt-pkg/contrib/fileutl.cc index 4240d9f49..da32983f1 100644 --- a/apt-pkg/contrib/fileutl.cc +++ b/apt-pkg/contrib/fileutl.cc @@ -34,9 +34,11 @@ #include <sys/types.h> #include <sys/time.h> #include <sys/wait.h> +#include <dirent.h> #include <signal.h> #include <errno.h> #include <set> +#include <algorithm> /*}}}*/ using namespace std; @@ -195,6 +197,128 @@ bool FileExists(string File) return true; } /*}}}*/ +// GetListOfFilesInDir - returns a vector of files in the given dir /*{{{*/ +// --------------------------------------------------------------------- +/* If an extension is given only files with this extension are included + in the returned vector, otherwise every "normal" file is included. */ +std::vector<string> GetListOfFilesInDir(string const &Dir, string const &Ext, + bool const &SortList) +{ + return GetListOfFilesInDir(Dir, Ext, SortList, false); +} +std::vector<string> GetListOfFilesInDir(string const &Dir, string const &Ext, + bool const &SortList, bool const &AllowNoExt) +{ + std::vector<string> ext; + ext.reserve(2); + if (Ext.empty() == false) + ext.push_back(Ext); + if (AllowNoExt == true && ext.empty() == false) + ext.push_back(""); + return GetListOfFilesInDir(Dir, ext, SortList); +} +std::vector<string> GetListOfFilesInDir(string const &Dir, std::vector<string> const &Ext, + bool const &SortList) +{ + // Attention debuggers: need to be set with the environment config file! + bool const Debug = _config->FindB("Debug::GetListOfFilesInDir", false); + if (Debug == true) + { + std::clog << "Accept in " << Dir << " only files with the following " << Ext.size() << " extensions:" << std::endl; + if (Ext.empty() == true) + std::clog << "\tNO extension" << std::endl; + else + for (std::vector<string>::const_iterator e = Ext.begin(); + e != Ext.end(); ++e) + std::clog << '\t' << (e->empty() == true ? "NO" : *e) << " extension" << std::endl; + } + + std::vector<string> List; + DIR *D = opendir(Dir.c_str()); + if (D == 0) + { + _error->Errno("opendir",_("Unable to read %s"),Dir.c_str()); + return List; + } + + for (struct dirent *Ent = readdir(D); Ent != 0; Ent = readdir(D)) + { + // skip "hidden" files + if (Ent->d_name[0] == '.') + continue; + + // check for accepted extension: + // no extension given -> periods are bad as hell! + // extensions given -> "" extension allows no extension + if (Ext.empty() == false) + { + string d_ext = flExtension(Ent->d_name); + if (d_ext == Ent->d_name) // no extension + { + if (std::find(Ext.begin(), Ext.end(), "") == Ext.end()) + { + if (Debug == true) + std::clog << "Bad file: " << Ent->d_name << " → no extension" << std::endl; + continue; + } + } + else if (std::find(Ext.begin(), Ext.end(), d_ext) == Ext.end()) + { + if (Debug == true) + std::clog << "Bad file: " << Ent->d_name << " → bad extension »" << flExtension(Ent->d_name) << "«" << std::endl; + continue; + } + } + + // Skip bad filenames ala run-parts + const char *C = Ent->d_name; + for (; *C != 0; ++C) + if (isalpha(*C) == 0 && isdigit(*C) == 0 + && *C != '_' && *C != '-') { + // no required extension -> dot is a bad character + if (*C == '.' && Ext.empty() == false) + continue; + break; + } + + // we don't reach the end of the name -> bad character included + if (*C != 0) + { + if (Debug == true) + std::clog << "Bad file: " << Ent->d_name << " → bad character »" + << *C << "« in filename (period allowed: " << (Ext.empty() ? "no" : "yes") << ")" << std::endl; + continue; + } + + // skip filenames which end with a period. These are never valid + if (*(C - 1) == '.') + { + if (Debug == true) + std::clog << "Bad file: " << Ent->d_name << " → Period as last character" << std::endl; + continue; + } + + // Make sure it is a file and not something else + string const File = flCombine(Dir,Ent->d_name); + struct stat St; + if (stat(File.c_str(),&St) != 0 || S_ISREG(St.st_mode) == 0) + { + if (Debug == true) + std::clog << "Bad file: " << Ent->d_name << " → stat says not a good file" << std::endl; + continue; + } + + if (Debug == true) + std::clog << "Accept file: " << Ent->d_name << " in " << Dir << std::endl; + List.push_back(File); + } + closedir(D); + + if (SortList == true) + std::sort(List.begin(),List.end()); + return List; +} + /*}}}*/ // SafeGetCWD - This is a safer getcwd that returns a dynamic string /*{{{*/ // --------------------------------------------------------------------- /* We return / on failure. */ diff --git a/apt-pkg/contrib/fileutl.h b/apt-pkg/contrib/fileutl.h index 73b5ea3be..85a94898c 100644 --- a/apt-pkg/contrib/fileutl.h +++ b/apt-pkg/contrib/fileutl.h @@ -23,6 +23,7 @@ #include <string> +#include <vector> using std::string; @@ -81,6 +82,13 @@ bool RunScripts(const char *Cnf); bool CopyFile(FileFd &From,FileFd &To); int GetLock(string File,bool Errors = true); bool FileExists(string File); +// FIXME: next ABI-Break: merge the two method-headers +std::vector<string> GetListOfFilesInDir(string const &Dir, string const &Ext, + bool const &SortList); +std::vector<string> GetListOfFilesInDir(string const &Dir, string const &Ext, + bool const &SortList, bool const &AllowNoExt); +std::vector<string> GetListOfFilesInDir(string const &Dir, std::vector<string> const &Ext, + bool const &SortList); string SafeGetCWD(); void SetCloseExec(int Fd,bool Close); void SetNonBlock(int Fd,bool Block); diff --git a/apt-pkg/contrib/hashes.cc b/apt-pkg/contrib/hashes.cc index b43771ea7..985d89d90 100644 --- a/apt-pkg/contrib/hashes.cc +++ b/apt-pkg/contrib/hashes.cc @@ -14,9 +14,9 @@ #include <apt-pkg/hashes.h> #include <apt-pkg/fileutl.h> #include <apt-pkg/configuration.h> - +#include <apt-pkg/macros.h> + #include <unistd.h> -#include <system.h> #include <string> #include <iostream> /*}}}*/ diff --git a/apt-pkg/contrib/system.h b/apt-pkg/contrib/macros.h index f68df6265..c39caf198 100644 --- a/apt-pkg/contrib/system.h +++ b/apt-pkg/contrib/macros.h @@ -1,9 +1,8 @@ // -*- mode: cpp; mode: fold -*- // Description /*{{{*/ -// $Id: system.h,v 1.3 1999/12/10 23:40:29 jgg Exp $ /* ###################################################################### - System Header - Usefull private definitions + Macros Header - Various useful macro definitions This source is placed in the Public Domain, do with it what you will It was originally written by Brian C. White. @@ -11,8 +10,8 @@ ##################################################################### */ /*}}}*/ // Private header -#ifndef SYSTEM_H -#define SYSTEM_H +#ifndef MACROS_H +#define MACROS_H // MIN_VAL(SINT16) will return -0x8000 and MAX_VAL(SINT16) = 0x7FFF #define MIN_VAL(t) (((t)(-1) > 0) ? (t)( 0) : (t)(((1L<<(sizeof(t)*8-1)) ))) @@ -77,4 +76,13 @@ #define __cold /* no cold marker */ #endif +#ifdef __GNUG__ +// Methods have a hidden this parameter that is visible to this attribute + #define __like_printf_1 __attribute__ ((format (printf, 2, 3))) + #define __like_printf_2 __attribute__ ((format (printf, 3, 4))) +#else + #define __like_printf_1 /* no like-printf */ + #define __like_printf_2 /* no like-printf */ +#endif + #endif diff --git a/apt-pkg/contrib/md5.cc b/apt-pkg/contrib/md5.cc index 2bfd70f1b..c0fa8493d 100644 --- a/apt-pkg/contrib/md5.cc +++ b/apt-pkg/contrib/md5.cc @@ -37,14 +37,13 @@ // Include Files /*{{{*/ #include <apt-pkg/md5.h> #include <apt-pkg/strutl.h> +#include <apt-pkg/macros.h> #include <string.h> #include <unistd.h> #include <netinet/in.h> // For htonl #include <inttypes.h> #include <config.h> -#include <system.h> - /*}}}*/ // byteSwap - Swap bytes in a buffer /*{{{*/ diff --git a/apt-pkg/contrib/netrc.cc b/apt-pkg/contrib/netrc.cc new file mode 100644 index 000000000..d8027fc24 --- /dev/null +++ b/apt-pkg/contrib/netrc.cc @@ -0,0 +1,211 @@ +// -*- mode: cpp; mode: fold -*- +// Description /*{{{*/ +// $Id: netrc.c,v 1.38 2007-11-07 09:21:35 bagder Exp $ +/* ###################################################################### + + netrc file parser - returns the login and password of a give host in + a specified netrc-type file + + Originally written by Daniel Stenberg, <daniel@haxx.se>, et al. and + placed into the Public Domain, do with it what you will. + + ##################################################################### */ + /*}}}*/ + +#include <apt-pkg/configuration.h> +#include <apt-pkg/fileutl.h> +#include <iostream> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <pwd.h> + +#include "netrc.h" + + +/* Get user and password from .netrc when given a machine name */ + +enum { + NOTHING, + HOSTFOUND, /* the 'machine' keyword was found */ + HOSTCOMPLETE, /* the machine name following the keyword was found too */ + HOSTVALID, /* this is "our" machine! */ + HOSTEND /* LAST enum */ +}; + +/* make sure we have room for at least this size: */ +#define LOGINSIZE 64 +#define PASSWORDSIZE 64 +#define NETRC DOT_CHAR "netrc" + +/* returns -1 on failure, 0 if the host is found, 1 is the host isn't found */ +int parsenetrc (char *host, char *login, char *password, char *netrcfile = NULL) +{ + FILE *file; + int retcode = 1; + int specific_login = (login[0] != 0); + char *home = NULL; + bool netrc_alloc = false; + int state = NOTHING; + + char state_login = 0; /* Found a login keyword */ + char state_password = 0; /* Found a password keyword */ + int state_our_login = false; /* With specific_login, + found *our* login name */ + + if (!netrcfile) { + home = getenv ("HOME"); /* portable environment reader */ + + if (!home) { + struct passwd *pw; + pw = getpwuid (geteuid ()); + if(pw) + home = pw->pw_dir; + } + + if (!home) + return -1; + + asprintf (&netrcfile, "%s%s%s", home, DIR_CHAR, NETRC); + if(!netrcfile) + return -1; + else + netrc_alloc = true; + } + + file = fopen (netrcfile, "r"); + if(file) { + char *tok; + char *tok_buf; + bool done = false; + char netrcbuffer[256]; + + while (!done && fgets(netrcbuffer, sizeof (netrcbuffer), file)) { + tok = strtok_r (netrcbuffer, " \t\n", &tok_buf); + while (!done && tok) { + if(login[0] && password[0]) { + done = true; + break; + } + + switch(state) { + case NOTHING: + if (!strcasecmp ("machine", tok)) { + /* the next tok is the machine name, this is in itself the + delimiter that starts the stuff entered for this machine, + after this we need to search for 'login' and + 'password'. */ + state = HOSTFOUND; + } + break; + case HOSTFOUND: + /* extended definition of a "machine" if we have a "/" + we match the start of the string (host.startswith(token) */ + if ((strchr(host, '/') && strstr(host, tok) == host) || + (!strcasecmp (host, tok))) { + /* and yes, this is our host! */ + state = HOSTVALID; + retcode = 0; /* we did find our host */ + } + else + /* not our host */ + state = NOTHING; + break; + case HOSTVALID: + /* we are now parsing sub-keywords concerning "our" host */ + if (state_login) { + if (specific_login) + state_our_login = !strcasecmp (login, tok); + else + strncpy (login, tok, LOGINSIZE - 1); + state_login = 0; + } else if (state_password) { + if (state_our_login || !specific_login) + strncpy (password, tok, PASSWORDSIZE - 1); + state_password = 0; + } else if (!strcasecmp ("login", tok)) + state_login = 1; + else if (!strcasecmp ("password", tok)) + state_password = 1; + else if(!strcasecmp ("machine", tok)) { + /* ok, there's machine here go => */ + state = HOSTFOUND; + state_our_login = false; + } + break; + } /* switch (state) */ + + tok = strtok_r (NULL, " \t\n", &tok_buf); + } /* while(tok) */ + } /* while fgets() */ + + fclose(file); + } + + if (netrc_alloc) + free(netrcfile); + + return retcode; +} + +void maybe_add_auth (URI &Uri, string NetRCFile) +{ + if (_config->FindB("Debug::Acquire::netrc", false) == true) + std::clog << "maybe_add_auth: " << (string)Uri + << " " << NetRCFile << std::endl; + if (Uri.Password.empty () == true || Uri.User.empty () == true) + { + if (NetRCFile.empty () == false) + { + char login[64] = ""; + char password[64] = ""; + char *netrcfile = strdupa (NetRCFile.c_str ()); + + // first check for a generic host based netrc entry + char *host = strdupa (Uri.Host.c_str ()); + if (host && parsenetrc (host, login, password, netrcfile) == 0) + { + if (_config->FindB("Debug::Acquire::netrc", false) == true) + std::clog << "host: " << host + << " user: " << login + << " pass-size: " << strlen(password) + << std::endl; + Uri.User = string (login); + Uri.Password = string (password); + return; + } + + // if host did not work, try Host+Path next, this will trigger + // a lookup uri.startswith(host) in the netrc file parser (because + // of the "/" + char *hostpath = strdupa (string(Uri.Host+Uri.Path).c_str ()); + if (hostpath && parsenetrc (hostpath, login, password, netrcfile) == 0) + { + if (_config->FindB("Debug::Acquire::netrc", false) == true) + std::clog << "hostpath: " << hostpath + << " user: " << login + << " pass-size: " << strlen(password) + << std::endl; + Uri.User = string (login); + Uri.Password = string (password); + return; + } + } + } +} + +#ifdef DEBUG +int main(int argc, char* argv[]) +{ + char login[64] = ""; + char password[64] = ""; + + if(argc < 2) + return -1; + + if(0 == parsenetrc (argv[1], login, password, argv[2])) { + printf("HOST: %s LOGIN: %s PASSWORD: %s\n", argv[1], login, password); + } +} +#endif diff --git a/apt-pkg/contrib/netrc.h b/apt-pkg/contrib/netrc.h new file mode 100644 index 000000000..02a5eb09f --- /dev/null +++ b/apt-pkg/contrib/netrc.h @@ -0,0 +1,29 @@ +// -*- mode: cpp; mode: fold -*- +// Description /*{{{*/ +// $Id: netrc.h,v 1.11 2004/01/07 09:19:35 bagder Exp $ +/* ###################################################################### + + netrc file parser - returns the login and password of a give host in + a specified netrc-type file + + Originally written by Daniel Stenberg, <daniel@haxx.se>, et al. and + placed into the Public Domain, do with it what you will. + + ##################################################################### */ + /*}}}*/ +#ifndef NETRC_H +#define NETRC_H + +#include <apt-pkg/strutl.h> + +#define DOT_CHAR "." +#define DIR_CHAR "/" + +// Assume: password[0]=0, host[0] != 0. +// If login[0] = 0, search for login and password within a machine section +// in the netrc. +// If login[0] != 0, search for password within machine and login. +int parsenetrc (char *host, char *login, char *password, char *filename); + +void maybe_add_auth (URI &Uri, string NetRCFile); +#endif diff --git a/apt-pkg/contrib/sha1.cc b/apt-pkg/contrib/sha1.cc index b70f31dc6..eae52d52f 100644 --- a/apt-pkg/contrib/sha1.cc +++ b/apt-pkg/contrib/sha1.cc @@ -31,12 +31,12 @@ // Include Files /*{{{*/ #include <apt-pkg/sha1.h> #include <apt-pkg/strutl.h> +#include <apt-pkg/macros.h> #include <string.h> #include <unistd.h> #include <inttypes.h> #include <config.h> -#include <system.h> /*}}}*/ // SHA1Transform - Alters an existing SHA-1 hash /*{{{*/ diff --git a/apt-pkg/contrib/strutl.cc b/apt-pkg/contrib/strutl.cc index bed51881f..b285a9f2e 100644 --- a/apt-pkg/contrib/strutl.cc +++ b/apt-pkg/contrib/strutl.cc @@ -43,9 +43,10 @@ bool UTF8ToCodeset(const char *codeset, const string &orig, string *dest) { iconv_t cd; const char *inbuf; - char *inptr, *outbuf, *outptr; - size_t insize, outsize; - + char *inptr, *outbuf; + size_t insize, bufsize; + dest->clear(); + cd = iconv_open(codeset, "UTF-8"); if (cd == (iconv_t)(-1)) { // Something went wrong @@ -55,33 +56,49 @@ bool UTF8ToCodeset(const char *codeset, const string &orig, string *dest) else perror("iconv_open"); - // Clean the destination string - *dest = ""; - return false; } - insize = outsize = orig.size(); + insize = bufsize = orig.size(); inbuf = orig.data(); inptr = (char *)inbuf; - outbuf = new char[insize+1]; - outptr = outbuf; + outbuf = new char[bufsize]; + size_t lastError = -1; while (insize != 0) { + char *outptr = outbuf; + size_t outsize = bufsize; size_t const err = iconv(cd, &inptr, &insize, &outptr, &outsize); + dest->append(outbuf, outptr - outbuf); if (err == (size_t)(-1)) { - insize--; - outsize++; - inptr++; - *outptr = '?'; - outptr++; + switch (errno) + { + case EILSEQ: + insize--; + inptr++; + // replace a series of unknown multibytes with a single "?" + if (lastError != insize) { + lastError = insize - 1; + dest->append("?"); + } + break; + case EINVAL: + insize = 0; + break; + case E2BIG: + if (outptr == outbuf) + { + bufsize *= 2; + delete[] outbuf; + outbuf = new char[bufsize]; + } + break; + } } } - *outptr = '\0'; - *dest = outbuf; delete[] outbuf; iconv_close(cd); |