X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=src%2Fpedro%2Fpedroutil.cpp;h=09407ff08a52fcb09c47dfcff04fb43a4c87ffa8;hb=18023a6d3838bec98725b5a745bd29350bb9e337;hp=c19498281842148d203fb89b6fc2c5c10ccced4a;hpb=59b195460be532890867125c402e2a9ba8a0f31e;p=inkscape.git diff --git a/src/pedro/pedroutil.cpp b/src/pedro/pedroutil.cpp index c19498281..09407ff08 100644 --- a/src/pedro/pedroutil.cpp +++ b/src/pedro/pedroutil.cpp @@ -24,7 +24,7 @@ #include #include - +#include #include #include "pedroutil.h" @@ -72,7 +72,7 @@ namespace Pedro //################# -static char *base64encode = +static const char *base64encode = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; @@ -304,9 +304,6 @@ DOMString Base64Decoder::decodeToString(const DOMString &str) //######################################################################## //######################################################################## - - - void Sha1::hash(unsigned char *dataIn, int len, unsigned char *digest) { Sha1 sha1; @@ -314,7 +311,7 @@ void Sha1::hash(unsigned char *dataIn, int len, unsigned char *digest) sha1.finish(digest); } -static char *sha1hex = "0123456789abcdef"; +static const char *sha1hex = "0123456789abcdef"; DOMString Sha1::hashHex(unsigned char *dataIn, int len) { @@ -340,40 +337,53 @@ DOMString Sha1::hashHex(const DOMString &str) void Sha1::init() { - lenW = 0; - sizeHi = 0; - sizeLo = 0; + longNr = 0; + byteNr = 0; + nrBytesHi = 0; + nrBytesLo = 0; // Initialize H with the magic constants (see FIPS180 for constants) - H[0] = 0x67452301L; - H[1] = 0xefcdab89L; - H[2] = 0x98badcfeL; - H[3] = 0x10325476L; - H[4] = 0xc3d2e1f0L; + hashBuf[0] = 0x67452301L; + hashBuf[1] = 0xefcdab89L; + hashBuf[2] = 0x98badcfeL; + hashBuf[3] = 0x10325476L; + hashBuf[4] = 0xc3d2e1f0L; + + for (int i = 0; i < 4; i++) + inb[i] = 0; for (int i = 0; i < 80; i++) - W[i] = 0; + inBuf[i] = 0; } void Sha1::append(unsigned char ch) { - // Read the data into W and process blocks as they get full - W[lenW / 4] <<= 8; - W[lenW / 4] |= (unsigned long)ch; - if ((++lenW) % 64 == 0) + if (nrBytesLo == 0xffffffffL) + { + nrBytesHi++; + nrBytesLo = 0; + } + else + nrBytesLo++; + + inb[byteNr++] = (unsigned long)ch; + if (byteNr >= 4) + { + inBuf[longNr++] = inb[0] << 24 | inb[1] << 16 | + inb[2] << 8 | inb[3]; + byteNr = 0; + } + if (longNr >= 16) { - hashblock(); - lenW = 0; + transform(); + longNr = 0; } - sizeLo += 8; - sizeHi += (sizeLo < 8); } void Sha1::append(unsigned char *dataIn, int len) { - // Read the data into W and process blocks as they get full for (int i = 0; i < len; i++) append(dataIn[i]); } @@ -385,33 +395,39 @@ void Sha1::append(const DOMString &str) } -void Sha1::finish(unsigned char hashout[20]) +void Sha1::finish(unsigned char digest[20]) { - unsigned char pad0x80 = 0x80; - unsigned char pad0x00 = 0x00; - unsigned char padlen[8]; + //snapshot the bit count now before padding + unsigned long nrBitsLo = (nrBytesLo << 3) & 0xffffffff; + unsigned long nrBitsHi = (nrBytesHi << 3) | ((nrBytesLo >> 29) & 7); + + //Append terminal char + append(0x80); + + //pad until we have a 56 of 64 bytes, allowing for 8 bytes at the end + while (longNr != 14) + append(0); - // Pad with a binary 1 (e.g. 0x80), then zeroes, then length - padlen[0] = (unsigned char)((sizeHi >> 24) & 255); - padlen[1] = (unsigned char)((sizeHi >> 16) & 255); - padlen[2] = (unsigned char)((sizeHi >> 8) & 255); - padlen[3] = (unsigned char)((sizeHi >> 0) & 255); - padlen[4] = (unsigned char)((sizeLo >> 24) & 255); - padlen[5] = (unsigned char)((sizeLo >> 16) & 255); - padlen[6] = (unsigned char)((sizeLo >> 8) & 255); - padlen[7] = (unsigned char)((sizeLo >> 0) & 255); - append(&pad0x80, 1); + //##### Append length in bits + append((unsigned char)((nrBitsHi>>24) & 0xff)); + append((unsigned char)((nrBitsHi>>16) & 0xff)); + append((unsigned char)((nrBitsHi>> 8) & 0xff)); + append((unsigned char)((nrBitsHi ) & 0xff)); + append((unsigned char)((nrBitsLo>>24) & 0xff)); + append((unsigned char)((nrBitsLo>>16) & 0xff)); + append((unsigned char)((nrBitsLo>> 8) & 0xff)); + append((unsigned char)((nrBitsLo ) & 0xff)); - while (lenW != 56) - append(&pad0x00, 1); - append(padlen, 8); - // Output hash - for (int i = 0; i < 20; i++) + //copy out answer + int indx = 0; + for (int i=0 ; i<5 ; i++) { - hashout[i] = (unsigned char)(H[i / 4] >> 24); - H[i / 4] <<= 8; + digest[indx++] = (unsigned char)((hashBuf[i] >> 24) & 0xff); + digest[indx++] = (unsigned char)((hashBuf[i] >> 16) & 0xff); + digest[indx++] = (unsigned char)((hashBuf[i] >> 8) & 0xff); + digest[indx++] = (unsigned char)((hashBuf[i] ) & 0xff); } // Re-initialize the context (also zeroizes contents) @@ -419,10 +435,13 @@ void Sha1::finish(unsigned char hashout[20]) } -#define SHA_ROTL(X,n) ((((X) << (n)) | ((X) >> (32-(n)))) & 0xffffffffL) -void Sha1::hashblock() +#define SHA_ROTL(X,n) ((((X) << (n)) & 0xffffffff) | (((X) >> (32-(n))) & 0xffffffff)) + +void Sha1::transform() { + unsigned long *W = inBuf; + unsigned long *H = hashBuf; for (int t = 16; t <= 79; t++) W[t] = SHA_ROTL(W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16], 1); @@ -437,7 +456,7 @@ void Sha1::hashblock() for (int t = 0; t <= 19; t++) { - TEMP = (SHA_ROTL(A,5) + (((C^D)&B)^D) + + TEMP = (SHA_ROTL(A,5) + ((B&C)|((~B)&D)) + E + W[t] + 0x5a827999L) & 0xffffffffL; E = D; D = C; C = SHA_ROTL(B, 30); B = A; A = TEMP; } @@ -449,7 +468,7 @@ void Sha1::hashblock() } for (int t = 40; t <= 59; t++) { - TEMP = (SHA_ROTL(A,5) + ((B&C)|(D&(B|C))) + + TEMP = (SHA_ROTL(A,5) + ((B&C)|(B&D)|(C&D)) + E + W[t] + 0x8f1bbcdcL) & 0xffffffffL; E = D; D = C; C = SHA_ROTL(B, 30); B = A; A = TEMP; } @@ -460,18 +479,15 @@ void Sha1::hashblock() E = D; D = C; C = SHA_ROTL(B, 30); B = A; A = TEMP; } - H[0] += A; - H[1] += B; - H[2] += C; - H[3] += D; - H[4] += E; + H[0] = (H[0] + A) & 0xffffffffL; + H[1] = (H[1] + B) & 0xffffffffL; + H[2] = (H[2] + C) & 0xffffffffL; + H[3] = (H[3] + D) & 0xffffffffL; + H[4] = (H[4] + E) & 0xffffffffL; } - - - //######################################################################## //######################################################################## //### M D 5 H A S H I N G @@ -481,7 +497,6 @@ void Sha1::hashblock() - void Md5::hash(unsigned char *dataIn, unsigned long len, unsigned char *digest) { Md5 md5; @@ -525,7 +540,7 @@ void Md5::init() -/* +/** * Update with one character */ void Md5::append(unsigned char ch) @@ -582,38 +597,25 @@ void Md5::append(const DOMString &str) void Md5::finish(unsigned char *digest) { //snapshot the bit count now before padding - unsigned long nrBitsLo = nrBytesLo << 3; + unsigned long nrBitsLo = (nrBytesLo << 3) & 0xffffffff; unsigned long nrBitsHi = (nrBytesHi << 3) | ((nrBytesLo >> 29) & 7); //Append terminal char append(0x80); - //pad until we have a 56 of 64 bits, allowing for 8 bytes at the end - while (true) - { - int remain = (int)(nrBytesLo & 63); - if (remain == 56) - break; + //pad until we have a 56 of 64 bytes, allowing for 8 bytes at the end + while (longNr != 14) append(0); - } //##### Append length in bits - int shift; - shift = 0; - for (int i=0 ; i<4 ; i++) - { - unsigned char ch = (unsigned char)((nrBitsLo>>shift) & 0xff); - append(ch); - shift += 8; - } - - shift = 0; - for (int i=0 ; i<4 ; i++) - { - unsigned char ch = (unsigned char)((nrBitsHi>>shift) & 0xff); - append(ch); - shift += 8; - } + append((unsigned char)((nrBitsLo ) & 0xff)); + append((unsigned char)((nrBitsLo>> 8) & 0xff)); + append((unsigned char)((nrBitsLo>>16) & 0xff)); + append((unsigned char)((nrBitsLo>>24) & 0xff)); + append((unsigned char)((nrBitsHi ) & 0xff)); + append((unsigned char)((nrBitsHi>> 8) & 0xff)); + append((unsigned char)((nrBitsHi>>16) & 0xff)); + append((unsigned char)((nrBitsHi>>24) & 0xff)); //copy out answer int indx = 0; @@ -650,7 +652,8 @@ DOMString Md5::finishHex() //# The four core functions - F1 is optimized somewhat -// #define F1(x, y, z) (x & y | ~x & z) +// #define F1(x, y, z) (x & y | ~x & z) +#define M(x) ((x) &= 0xffffffff) #define F1(x, y, z) (z ^ (x & (y ^ z))) #define F2(x, y, z) F1(z, x, y) #define F3(x, y, z) (x ^ y ^ z) @@ -658,14 +661,14 @@ DOMString Md5::finishHex() // ## This is the central step in the MD5 algorithm. #define MD5STEP(f, w, x, y, z, data, s) \ - ( w += f(x, y, z) + data, w = w<>(32-s), w += x ) + ( w += (f(x, y, z) + data), M(w), w = w<>(32-s), w += x, M(w) ) /* * The core of the MD5 algorithm, this alters an existing MD5 hash to * reflect the addition of 16 longwords of new data. MD5Update blocks * the data and converts bytes into longwords for this routine. - * @parm buf points to an array of 4 unsigned longs - * @parm in points to an array of 16 unsigned longs + * @parm buf points to an array of 4 unsigned 32bit (at least) integers + * @parm in points to an array of 16 unsigned 32bit (at least) integers */ void Md5::transform() { @@ -753,8 +756,6 @@ void Md5::transform() - - //######################################################################## //######################################################################## //### T H R E A D @@ -890,9 +891,11 @@ TcpSocket::TcpSocket(const std::string &hostnameArg, int port) } + + #ifdef HAVE_SSL -static void cryptoLockCallback(int mode, int type, const char *file, int line) +static void cryptoLockCallback(int mode, int type, const char */*file*/, int /*line*/) { //printf("########### LOCK\n"); static int modes[CRYPTO_NUM_LOCKS]; /* = {0, 0, ... } */ @@ -950,9 +953,9 @@ static void cryptoLockCallback(int mode, int type, const char *file, int line) err: if (errstr) { - /* we cannot use bio_err here */ - fprintf(stderr, "openssl (lock_dbg_cb): %s (mode=%d, type=%d) at %s:%d\n", - errstr, mode, type, file, line); + //how do we pass a context pointer here? + //error("openssl (lock_dbg_cb): %s (mode=%d, type=%d) at %s:%d", + // errstr, mode, type, file, line); } } @@ -977,6 +980,27 @@ TcpSocket::TcpSocket(const TcpSocket &other) portno = other.portno; } + +void TcpSocket::error(const char *fmt, ...) +{ + static char buf[256]; + lastError = "TcpSocket err: "; + va_list args; + va_start(args, fmt); + vsnprintf(buf, 255, fmt, args); + va_end(args); + lastError.append(buf); + fprintf(stderr, "%s\n", lastError.c_str()); +} + + +DOMString &TcpSocket::getLastError() +{ + return lastError; +} + + + static bool tcp_socket_inited = false; void TcpSocket::init() @@ -1092,17 +1116,15 @@ static void infoCallback(const SSL *ssl, int where, int ret) bool TcpSocket::startTls() { #ifndef HAVE_SSL - fprintf(stderr, - "SSL starttls() error: client not compiled with SSL enabled\n"); + error("SSL starttls() error: client not compiled with SSL enabled"); return false; #else /*HAVE_SSL*/ if (!libssl_is_present) - { - fprintf(stderr, - "SSL starttls() error: the correct version of libssl was not found \n"); - return false; - } else { - + { + error("SSL starttls() error: the correct version of libssl was not found"); + return false; + } + sslStream = NULL; sslContext = NULL; @@ -1151,21 +1173,20 @@ bool TcpSocket::startTls() int ret = SSL_connect(sslStream); if (ret == 0) { - fprintf(stderr, "SSL connection not successful\n"); + error("SSL connection not successful"); disconnect(); return false; } else if (ret < 0) { int err = SSL_get_error(sslStream, ret); - fprintf(stderr, "SSL connect error %d\n", err); + error("SSL connect error %d", err); disconnect(); return false; } sslEnabled = true; return true; - } #endif /* HAVE_SSL */ } @@ -1174,20 +1195,20 @@ bool TcpSocket::connect() { if (hostname.size()<1) { - fprintf(stderr, "open: null hostname\n"); + error("open: null hostname"); return false; } if (portno<1) { - fprintf(stderr, "open: bad port number\n"); + error("open: bad port number"); return false; } sock = socket(PF_INET, SOCK_STREAM, 0); if (sock < 0) { - fprintf(stderr, "open: error creating socket\n"); + error("open: error creating socket"); return false; } @@ -1195,7 +1216,7 @@ bool TcpSocket::connect() struct hostent *server = gethostbyname(c_hostname); if (!server) { - fprintf(stderr, "open: could not locate host '%s'\n", c_hostname); + error("open: could not locate host '%s'", c_hostname); return false; } @@ -1209,7 +1230,7 @@ bool TcpSocket::connect() int ret = ::connect(sock, (const sockaddr *)&serv_addr, sizeof(serv_addr)); if (ret < 0) { - fprintf(stderr, "open: could not connect to host '%s'\n", c_hostname); + error("open: could not connect to host '%s'", c_hostname); return false; } @@ -1241,7 +1262,7 @@ bool TcpSocket::disconnect() case 0: case -1: default: - //printf("Shutdown failed"); + error("Shutdown failed"); ret = false; } SSL_free(sslStream); @@ -1293,10 +1314,10 @@ long TcpSocket::available() if (count<=0 && sslEnabled) { #ifdef HAVE_SSL - if (libssl_is_present) - { + if (libssl_is_present) + { return SSL_pending(sslStream); - } + } #endif } return count; @@ -1308,7 +1329,7 @@ bool TcpSocket::write(int ch) { if (!isConnected()) { - fprintf(stderr, "write: socket closed\n"); + error("write: socket closed"); return false; } unsigned char c = (unsigned char)ch; @@ -1324,7 +1345,7 @@ bool TcpSocket::write(int ch) switch(SSL_get_error(sslStream, r)) { default: - fprintf(stderr, "SSL write problem"); + error("SSL write problem"); return -1; } } @@ -1336,7 +1357,7 @@ bool TcpSocket::write(int ch) if (send(sock, (const char *)&c, 1, 0) < 0) //if (send(sock, &c, 1, 0) < 0) { - fprintf(stderr, "write: could not send data\n"); + error("write: could not send data"); return false; } } @@ -1347,7 +1368,7 @@ bool TcpSocket::write(char *str) { if (!isConnected()) { - fprintf(stderr, "write(str): socket closed\n"); + error("write(str): socket closed"); return false; } int len = strlen(str); @@ -1363,7 +1384,7 @@ bool TcpSocket::write(char *str) switch(SSL_get_error(sslStream, r)) { default: - fprintf(stderr, "SSL write problem"); + error("SSL write problem"); return -1; } } @@ -1375,7 +1396,7 @@ bool TcpSocket::write(char *str) if (send(sock, str, len, 0) < 0) //if (send(sock, &c, 1, 0) < 0) { - fprintf(stderr, "write: could not send data\n"); + error("write: could not send data"); return false; } } @@ -1430,11 +1451,11 @@ int TcpSocket::read() case SSL_ERROR_ZERO_RETURN: return -1; case SSL_ERROR_SYSCALL: - fprintf(stderr, "SSL read problem(syscall) %s\n", + error("SSL read problem(syscall) %s", ERR_error_string(ERR_get_error(), NULL)); return -1; default: - fprintf(stderr, "SSL read problem %s\n", + error("SSL read problem %s", ERR_error_string(ERR_get_error(), NULL)); return -1; } @@ -1445,7 +1466,7 @@ int TcpSocket::read() { if (recv(sock, (char *)&ch, 1, 0) <= 0) { - fprintf(stderr, "read: could not receive data\n"); + error("read: could not receive data"); disconnect(); return -1; }