-2

The program has several "encryption" algorithms. This one should blockwise reverse the input. "He|ll|o " becomes "o |ll|He" (block length of 2).

I add two strings, in this case appending the result string to the current "block" string and making that the result. When I add the result first and then the block it works fine and gives me back the original string. But when i try to reverse the order it just gives me the the last "block".

Several other functions that are used for "rotation" are above.

//amount of blocks
function amBl(i1:integer;i2:integer):integer;
begin
  if (i1 mod i2) <> 0 then result := (i1 div i2) else result := (i1 div i2) - 1;
end;

//calculation of block length
function calcBl(keyStr:string):integer;
var i:integer;
begin
  result := 0;
  for i := 1 to Length(keyStr) do
  begin
     result := (result + ord(keyStr[i])) mod 5;
     result := result + 2;
  end;

end;

//desperate try to add strings
function append(s1,s2:string):string;
begin
  insert(s2,s1,Length(s1)+1);
  result := s1;
end;

function rotation(inStr,keyStr:string):string;
var //array of chars -> string
    block,temp:string;
    //position in block  variable
    posB:integer;
    //block length and block count variable
    bl, bc:integer;
    //null character as placeholder
    n : ansiChar;

begin
   //calculating block length 2..6
   bl := calcBl(keyStr);
   setLength(block,bl);
   result := '';
   temp := '';
   {n := #00;}

   for bc := 0 to amBl(Length(inStr),bl) do
     begin
       //filling block with chars starting from back of virtual block (in inStr)
       for posB := 1 to bl do
       begin
       block[posB] := inStr[bc * bl + posB];
       {if inStr[bc * bl + posB] = ' ' then block[posB] := n;}
       end;

       //adding the block in front of the existing result string
       temp := result;
       result := block + temp;
       //result := append(block,temp);
       //result := concat(block,temp);

     end;

end;

(full code http://pastebin.com/6Uarerhk)

After all the loops "result" has the right value, but in the last step (between "result := block + temp" and the "end;" of the function) "block" replaces the content of "result" with itself completely, it doesn't add result at the end anymore. And as you can see I even used a temp variable to try to work around that.. doesnt change anything though.

  • 1
    Never assume a string result variable is in a known state. Where is result set? You are showing truncated code... First do result := '' then result := block+result – Arnaud Bouchez May 25 '14 at 19:57
  • 6
    You've posted an incomplete, non-compilable chunk of code with no context, no complete information, a function that isn't used, and a pretty unclear description of the problem. Can you [edit] your question and try again, starting with a compilable example that actually demonstrates the issue you're having? – Ken White May 25 '14 at 21:13
  • Sorry for all this missing information, i just read the guidelines and thought "only whats really necessary". I'm gonna try to improve that. What other important "context" is needed though? And what should i add to describe the problem better? I went through the code line by line watching the values and wrote that in a condensed form in the last paragraph. Thank you for your critique though, I hope I´m not gonna make those mistakes again... – linkcharger Jun 02 '14 at 21:11
  • @linkcharger It would help if you show a sample call with (1) specific input values, (2) expected result, (3) actual result. – Disillusioned Jun 03 '14 at 12:52
  • @linkcharger Your additional code confirms my hunch (and I've updated my answer with an explanation). If you use a 7 character input string, it won't matter you use as a key string to determine block length. Your input string will not be divisible by the block length, and you'll add garbage characters to the end of your string (or get an access violation). – Disillusioned Jun 03 '14 at 13:26

1 Answers1

0

I am 99.99% certain that your problem is due to a subtle bug in your code. However, your deliberate efforts to hide the relevant code mean that we're really shooting in the dark. You haven't even been clear about where you're seeing the shortened Result: GUI Control/Debugger/Writeln

The irony is that you have all the information at your fingertips to provide a small concise demonstration of your problem - including sample input and expected output.

So without the relevant information, I can only guess; I do think I have a good hunch though.

Try the following code and see if you have a similar experience with S3:

S1 := 'a'#0;
S2 := 'bc';
S3 := S1 + S2;

The reason for my hunch is that #0 is a valid character in a string: but whenever that string needs to be processed as PChar, #0 will be interpreted as a string terminator. This could very well cause the "strange behaviour" you're seeing.

So it's quite probable that you have at least one of the following 2 bugs in your code:

  1. You are always processing 1 too many characters; with the extra character being #0.
  2. When your input string has an odd number of characters: your algorithm (which relies on pairs of characters) adds an extra character with value #0.

Edit

With the additional source code, my hunch is confirmed:

  • Suppose you have a 5 character string, and key that produces block length 2.
  • Your inner loop (for posB := 1 to bl do) will read beyond the length of inStr on the last iteration of the outer loop.
  • So if the next character in memory happens to be #0, you will be doing exactly as described above.

Additional problem. You have the following code:

 //calculating block length 2..6
 bl := calcBl(keyStr);

Your assumption in the comment is wrong. From the implementation of calcBl, if keyStr is empty, your result will be 0.

Community
  • 1
  • 1
Disillusioned
  • 14,635
  • 3
  • 43
  • 77
  • I apologize for posting this before going on a trip with no internet, from other forums i was used to that i takes at least a week until ANYbody responded.. I'm gonna edit this, add the necessary information and then try your "hunch". Thanks for being patient with me ;) – linkcharger Jun 02 '14 at 21:08