473,796 Members | 2,765 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Works on Windows but not on HPUX, Help Please

Hi,

This code works fine on win and linux but not on hpux. All is compiled
with gcc. Can anyone help please?

/*************** *************** *************** *************** *************** **/
/*
*/
/* encrypt
*/
/*
*/
/*************** *************** *************** *************** *************** **/
/* Description: Utility function that simply encrypts a string. Used
for */
/* simple password storage in the config file.
*/
/*
*/
/*************** *************** *************** *************** *************** **/
void encrypt(char str[],int key)
{
unsigned int i;
for(i=0;i<strle n(str);++i)
{
str[i] = str[i] - key;
}
}

/*************** *************** *************** *************** *************** **/
/*
*/
/* decrypt
*/
/*
*/
/*************** *************** *************** *************** *************** **/
/* Description: Utility function that simply decrypts a string. Used
for */
/* simple password storage in the config file.
*/
/*
*/
/*************** *************** *************** *************** *************** **/
void decrypt(char str[],int key)
{
unsigned int i;
for(i=0;i<strle n(str);++i)
{
str[i] = str[i] + key;
}
}
The decrypt function even shortens the length, which as you can see,
shouldnt happen and doesnt on other OSs

Any help would be great, thanks
Kind regards
Danny

Jun 27 '06 #1
10 1863
In article <11************ *********@i40g2 000cwc.googlegr oups.com>,
Smurff <da***********@ gmail.com> wrote:
This code works fine on win and linux but not on hpux. All is compiled
with gcc. Can anyone help please?
You didn't really say what symptoms you saw.
void encrypt(char str[],int key)
{
unsigned int i;
for(i=0;i<strle n(str);++i)
{
str[i] = str[i] - key;
}
} void decrypt(char str[],int key)
{
unsigned int i;
for(i=0;i<strle n(str);++i)
{
str[i] = str[i] + key;
}
} The decrypt function even shortens the length, which as you can see,
shouldnt happen and doesnt on other OSs


If key happens to match any character in str[] then the subtraction
is going to produce a 0 at the corresponding position, and that is
going to have the effect of shortening the string as far as strlen()
is concerned.

But if that isn't happening for you on some systems with the same input,
then my hypothesis would be that you are encountering differences
as to whether char is signed or unsigned.
--
"It is important to remember that when it comes to law, computers
never make copies, only human beings make copies. Computers are given
commands, not permission. Only people can be given permission."
-- Brad Templeton
Jun 27 '06 #2
Walter Roberson wrote:

In article <11************ *********@i40g2 000cwc.googlegr oups.com>,
Smurff <da***********@ gmail.com> wrote:
This code works fine on win and linux but not on hpux.
All is compiled with gcc. Can anyone help please?


You didn't really say what symptoms you saw.
void encrypt(char str[],int key)
{
unsigned int i;
for(i=0;i<strle n(str);++i)
{
str[i] = str[i] - key;
}
}

void decrypt(char str[],int key)
{
unsigned int i;
for(i=0;i<strle n(str);++i)
{
str[i] = str[i] + key;
}
}

The decrypt function even shortens the length, which as you can see,
shouldnt happen and doesnt on other OSs


If key happens to match any character in str[] then the subtraction
is going to produce a 0 at the corresponding position, and that is
going to have the effect of shortening the string as far as strlen()
is concerned.

But if that isn't happening for you on some
systems with the same input,
then my hypothesis would be that you are encountering differences
as to whether char is signed or unsigned.


There might also be a problem with
implementation defined behavior
when char is signed and the result of the subtraction
isn't within the range of char.

If the value of key is INT_MIN,
that would cause undefined behavior in encrypt.

--
pete
Jun 27 '06 #3
Smurff wrote:
Hi,

This code works fine on win and linux but not on hpux. All is compiled
with gcc. Can anyone help please? The decrypt function even shortens the length, which as you can see,
shouldnt happen and doesnt on other OSs


Please post a COMPLETE minimal program that demonstrates the problem.
We need to see your test driver as well as the routine. What key? What
string?


Brian
Jun 27 '06 #4
Smurff wrote:
void encrypt(char str[],int key)
{
unsigned int i;
for(i=0;i<strle n(str);++i)
{
str[i] = str[i] - key;
}
}


Aside from being totally weak it's also plagued by the fact the LENGTH
OF THE STRING CAN CHANGE.

What if str[i] == key?

mmm homework.

Tom

Jun 27 '06 #5
Smurff wrote:
Hi,

This code works fine on win and linux but not on hpux. All is compiled
with gcc. Can anyone help please?

/*************** *************** *************** *************** *************** **/
/*
Box comments suck.
*/
As we can see here. They're allergic to varying line lengths. Just /* and */
with appropriate whitespace will do.
/* encrypt
*/
/*
*/
/*************** *************** *************** *************** *************** **/
/* Description: Utility function that simply encrypts a string. Used
for */
/* simple password storage in the config file.
*/
/*
*/
/*************** *************** *************** *************** *************** **/
void encrypt(char str[],int key)
{
unsigned int i;
for(i=0;i<strle n(str);++i)
Not advisable. You're counting on your compiler to optimize the evaluation
of strlen(). In this loop it can't do that, in fact, since you're changing
the string and hence may change its length.
{
str[i] = str[i] - key;


Wrong in two ways:

- What if str[i] - key == '\0'? You've just cut your string short.
- What if str[i] - key is not representable in a char? In particular, if
char is signed, the result is implementation-defined, since you're
subtracting an int from a char, which gets you an int. You don't want that.

Here's a possible trivial encryption function:

void encrypt(unsigne d char *data, size_t datalen, const unsigned char
*key, size_t keylen) {
size_t i;
for (i = 0; i != datalen; ++i) {
data[i] ^= key[i % keylen];
}
}

This function is its own decryption function. Either call it again to
decrypt or copy the body if you don't want to expose this implementation detail.

It operates on bytes, not characters. The resulting block of data could
contain NUL characters, or in general characters you can't print. You should
use an escape mechanism to read and write the encrypted data -- for example,
writing it as a string of hexadecimal digits or using Base64 encoding.

Do not use this to hide actual secrets, the kind your company's livelihood
depends on. Breaking the encryption is trivial.

S.
Jun 27 '06 #6
Sorry I am a muppet. I used static void and that fixed it.

Sorry to waist your time
Danny

Jun 27 '06 #7
"Smurff" <da***********@ gmail.com> writes:
Sorry I am a muppet. I used static void and that fixed it.

Sorry to waist your time


I don't see how using static void could affect the behavior of the
code fragments you posted.

Whatever else you do, you should pay attention to the excellent advice
you got in this thread. It's quite possible that your code will
happen to work in some circumstances, but will fail at precisely the
most embarrassing possible moment.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jun 28 '06 #8
Smurff wrote:
Sorry I am a muppet. I used static void and that fixed it.
You really are a muppet *if* you ignored all the good advice
in this thread.
Sorry to waist your time


goose,
a waist is a terrible thing to mind

Jun 28 '06 #9
In article <44***********@ mindspring.com> ,
pete <pf*****@mindsp ring.com> wrote:
In article <11************ *********@i40g2 000cwc.googlegr oups.com>,
Smurff <da***********@ gmail.com> wrote:
>void encrypt(char str[],int key)
>{
> unsigned int i;
> for(i=0;i<strle n(str);++i)
> {
> str[i] = str[i] - key;
> }
>}
If the value of key is INT_MIN,
that would cause undefined behavior in encrypt.


Hmmm, do I read C89 correctly that the char str[i] will be
converted to an int, not an unsigned int, even if char is unsigned,
with the exception of the case where sizeof char == sizeof int
[that being the case where the value of an unsigned char might
not fit within a signed int] ?
C89 3.2.1.1 Characters and Integers
Assuming that all values of char will fit within an int:

- if char is unsigned then its minimum value would be 0,
and if the value of key is INT_MIN then 0-INT_MIN could potentially
be INT_MAX+1 (2's complement system), but on 1's complement or
signed-magnitude then -INT_MIN is INT_MAX and no undefined behaviour
would have occured for those number systems

- if char is signed then its minimum value would be SCHAR_MIN
and that minus INT_MIN might exceed INT_MAX by abs(SCHAR_MIN)
or abs(SCHAR_MIN)+ 1 . But in this case, it would not just be the
key of value INT_MIN that leads to the undefined behaviour: it would
be any key between INT_MIN and INT_MIN-SCHAR_MIN that would lead
to arithmetic overflow on the subtraction.
On the other hand, once the subtraction is completed, the result
is being stored back into str[i] which is a char. If char is
signed then if the result of the subtraction is less than SCHAR_MIN
then the result of the store operation is undefined. If char
is unsigned then the result of the store operation *is* defined
no matter what the value of the subtraction was [unless the
subtraction result was undefined as per the above.]
In summary, if char is unsigned then there is undefined behaviour
in the case of key = INT_MIN on a 2s complement system, but no
undefined behaviour for 1s complement or signed magnitude;
if char is signed then there is a fair range of values of key
that will produce arithmetic overflow, and an even larger range of
values of key that will produce problems at the assignment operation
even if there was no arithmetic overflow.
--
"law -- it's a commodity"
-- Andrew Ryan (The Globe and Mail, 2005/11/26)
Jun 28 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
4123
by: Stephen Boulet | last post by:
I'm looking to install python on hpux 10.2. Anyone know of a site hosting a depot file of a recent python? Thanks. Stephen
1
1546
by: dirk dierickx | last post by:
All, I'm looking for a way to hide opened windows in X, these windows can be using any widget-set so it will not target gtk, qt, motif, etc directly (I know how to do it in gtk for example, but this will only work for gtk apps ofcourse). Just an easy way to select a certain window and hide it, is anything like this possible?
1
4141
by: Jorge | last post by:
All, I've got a process that creates some children and want to be alerted when they die. To do this, the SIGCHLD signal is registered in the process using sigaction to jump to a subprogram as soon as it's received. The problem comes when many children exit at the same time and their father seems to miss some of them. This happens quite often, but not always.
3
4673
by: Jeremy Lemaire | last post by:
Hello, I am working on cross platform code that is displaying a huge memory leak when compiled on 11.00 HPUX using the aCC -AA flag. It is not leaking on NT, LINUX, Solaris, or HPUX without the -AA flag. In another news group I came across some interesting (ok scarey) information regarding memory leaks in the STL list<...> container. I have compiled and executed the following code and verified that this does in fact leak on my system.
1
3644
by: smsabu2002 | last post by:
Hi, I am facing the build problem while installing the DBD-MySql perl module (ver 2.9008) using both GCC and CC compilers in HP-UX machine. For the Build using GCC, the compiler error is produced due to the unknown GCC compiler option "+DAportable". For the Build using CC, the preprocessor error is produced due to the recursive declration of macro "PerlIO" in perlio.h file.
4
2287
by: kongkolvyu | last post by:
Hi The strptime function works just fine on Solaris. Here is an example on how I use it: struct tm tmpTm; if(strptime("20010101010101","%Y%m%d%H%M%S",&tmpTm)==NULL) printf("Error,String convert to time Error\n"); On the HPUX platform, this call to strptime always returns NULL. Does anybody have an idea why this does not work.
1
2877
by: Jay Hamilton | last post by:
Hello, I am running into an invalid address alignment error on an HPUX box when I attempt to lookup a value in a STL map. The argument being passed in is a double, which is accessed from a structure; the map's key is also a double. The issue seemed to be occuring in the map's default comparison function so I implemented my own comparison operator, but then the error continued happening in my new fxn. Strangely, casting the double I...
0
2115
by: alokverma | last post by:
Hi all, I am trying to compile some code on HP-UX-Itanium faluire. using aCC: HP C/aC++ B3910B A.06.12 I have written a sample code below and using the following compilation command. *aCC -c -o test.o -AP -Ae -DHPUX test.cpp*
3
2334
by: amit2781 | last post by:
I am trying to build the my project on HPUX (HP-UX oscar B.11.11 U 9000/800 741028561 unlimited-user license) which access the Mysql library. But it is giving error as : Error : MySQL51/lib/hpux/libmysqld.a(client.o) - shared library must be position independent. Use +z or +Z to recompile. I am using this option in building: aCC -Wl,+s,-E,+vnocompatwarnings -z -g0 -b -Wl,-Bsymbolic. I also tried using the +Z, +z, also added...
0
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9535
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10200
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10021
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9061
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6800
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5453
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5582
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2931
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.