--- parser3/src/main/pa_common.C 2005/11/24 13:50:29 1.204.6.9 +++ parser3/src/main/pa_common.C 2006/12/02 12:35:50 1.221 @@ -24,9 +24,9 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA. * -*/ + */ -static const char * const IDENT_COMMON_C="$Date: 2005/11/24 13:50:29 $"; +static const char * const IDENT_COMMON_C="$Date: 2006/12/02 12:35:50 $"; #include "pa_common.h" #include "pa_exception.h" @@ -430,17 +430,17 @@ static bool entry_readable(const String& } return false; } -bool file_readable(const String& file_spec) { +bool file_exist(const String& file_spec) { return entry_readable(file_spec, false); } -bool dir_readable(const String& file_spec) { +bool dir_exists(const String& file_spec) { return entry_readable(file_spec, true); } -const String* file_readable(const String& path, const String& name) { +const String* file_exist(const String& path, const String& name) { String& result=*new String(path); result << "/"; result << name; - return file_readable(result)?&result:0; + return file_exist(result)?&result:0; } bool file_executable(const String& file_spec) { return access(file_spec.cstr(String::L_FILE_SPEC), X_OK)==0; @@ -553,7 +553,7 @@ char* unescape_chars(const char* cp, int EscapeFirst, EscapeSecond } escapeState=EscapeRest; - uchar escapedValue=0; + uint escapedValue=0; int srcPos=0; int dstPos=0; while(srcPos < len) { @@ -569,12 +569,12 @@ char* unescape_chars(const char* cp, int } break; case EscapeFirst: - escapedValue=(uchar)(hex_value[ch] << 4); + escapedValue=hex_value[ch] << 4; escapeState=EscapeSecond; break; case EscapeSecond: escapedValue +=hex_value[ch]; - s[dstPos++]=escapedValue; + s[dstPos++]=(char)escapedValue; escapeState=EscapeRest; break; } @@ -627,9 +627,9 @@ static bool isLeap(int year) { } int getMonthDays(int year, int month) { - int monthDays[]={ + static int monthDays[]={ 31, - isLeap(year) ? 29 : 28, + 28, 31, 30, 31, @@ -641,7 +641,7 @@ int getMonthDays(int year, int month) { 30, 31 }; - return monthDays[month]; + return (month == 2 && isLeap(year)) ? 29 : monthDays[month]; } void remove_crlf(char* start, char* end) { @@ -809,7 +809,7 @@ g_mime_utils_base64_encode_step (const u case 2: *saveout++ = *inptr++; case 1: *saveout++ = *inptr++; } - ((char *)save)[0] += inlen; + *(char *)save = *(char *)save+(char)inlen; } /*d(printf ("mode = %d\nc1 = %c\nc2 = %c\n", @@ -922,9 +922,9 @@ g_mime_utils_base64_decode_step (const u saved = (saved << 6) | c; i++; if (i == 4) { - *outptr++ = saved >> 16; - *outptr++ = saved >> 8; - *outptr++ = saved; + *outptr++ = (unsigned char)(saved >> 16); + *outptr++ = (unsigned char)(saved >> 8); + *outptr++ = (unsigned char)(saved); i = 0; } } @@ -978,3 +978,49 @@ void pa_base64_decode(const char *in, si assert(result_size <= in_size); result[result_size]=0; // for text files } + + +int file_block_read(const int f, unsigned char* buffer, const size_t size){ + int nCount = read(f, buffer, size); + if (nCount < 0) + throw Exception(0, + 0, + "read failed: %s (%d)", strerror(errno), errno); + return nCount; +} + +const unsigned long pa_crc32(const char *in, size_t in_size) +{ + unsigned long crc32=0xFFFFFFFF; + + InitCrc32Table(); + for(size_t i = 0; i < in_size; i++) CalcCrc32(in[i], crc32); + + return ~crc32; +} + +const unsigned long pa_crc32(const String& file_spec) +{ + unsigned long crc32=0xFFFFFFFF; + file_read_action_under_lock(file_spec, "crc32", file_crc32_file_action, &crc32); + return ~crc32; +} + +static void file_crc32_file_action( + struct stat& finfo, + int f, + const String&, const char* /*fname*/, bool, + void *context) +{ + unsigned long& crc32=*static_cast(context); + if(finfo.st_size) { + InitCrc32Table(); + int nCount=0; + do { + unsigned char buffer[FILE_BUFFER_SIZE]; + nCount = file_block_read(f, buffer, sizeof(buffer)); + for(int i = 0; i < nCount; i++) CalcCrc32(buffer[i], crc32); + } while(nCount > 0); + } +} +