Thursday, June 2, 2011

Puzzler for C developer role

Today, I've created puzzler for C developer at company I work for. This is one of rare cases where we can publish the code, and it's few lines anyhow, so here I give it a shot. You can try to solve the puzzler, as described in comments. It's very easy and you should have no problem solving it without Solaris. All you need is a C compiler.

Bonus question: why did I choose (R) and (C) instead of R and C ?

Bellow is the code which you can find also on snipplr:


  1. /** This program should be compiled using following command:
  2.  $ cc -g -o test_hr_test test_hr_test.c
  3.  Original developer Malvin left company to pursue his dream and went
  4.  to live with wild ligers. No-one ever heard any word about him since
  5.  he left. We've managed to recover source code from his source repo.,
  6.  but program seems to be broken. Strangely Malvin used iso-8859-1 as
  7.  his console encoding.
  8.  Hints:
  9.  - program does not compile
  10.  - we were not able to recover whole function permutateBlockRev()
  11.  - program crashes on our X86 solaris 10 matchine (not on linux)
  12.  Are you the one which can help us recover the password ?
  13.  Send it to: rpc-hr@ri-rpc.sk
  14.  */
  15. #include <stdio.h>
  16. #include <string.h>
  17. /** bit manipulation macros */
  18. #define CLRBIT( STR, IDX ) ( (STR)[(IDX)/8] &= ~(0x01 << (7 - ((IDX)%8))) )
  19. #define SETBIT( STR, IDX ) ( (STR)[(IDX)/8] |= (0x01 << (7 - ((IDX)%8))) )
  20. #define GETBIT( STR, IDX ) (( ((STR)[(IDX)/8]) >> (7 - ((IDX)%8)) ) & 0x01)
  21. /** permutation works on unsigned char blocks of size 8 */
  22. #define BLOCK_SIZE 8
  23. typedef unsigned char[BLOCK_SIZE] block;
  24. /** Function does reverse permutation to DES initial block permutation as described at
  25.  http://en.wikipedia.org/wiki/DES_supplementary_material
  26.  @param b - block which will be permutated in-place
  27.  @note indexes start from 0 contrary to wikipedia initialization vector - C like index
  28.  */
  29. void permutateBlockRev(block b)
  30. {
  31. int i;
  32. static const int initialPermuteMap[64] =
  33. {
  34. 57, 49, 41, 33, 25, 17, 9, 1,
  35. 59, 51, 43, 35, 27, 19, 11, 3,
  36. 61, 53, 45, 37, 29, 21, 13, 5,
  37. 63, 55, 47, 39, 31, 23, 15, 7,
  38. 56, 48, 40, 32, 24, 16, 8, 0,
  39. 58, 50, 42, 34, 26, 18, 10, 2,
  40. 60, 52, 44, 36, 28, 20, 12, 4,
  41. 62, 54, 46, 38, 30, 22, 14, 6
  42. };
  43. /* There are few lines of code missing */
  44. }
  45. int main (void)
  46. {
  47. unsigned char str[BLOCK_SIZE + 1] = "DummyStr";
  48. block permutated = { 0x7a, 0xd2, 0x21, 0x3c, 0x05, 0x85, 0x8d, 0x71 };
  49. permutateBlockRev(permutated);
  50. strcpy(str, permutated);
  51. printf("Password is: %s\n", str);
  52. }

No comments: