TweetFollow Us on Twitter

PROGRAMMER's CHALLENGE

Volume Number: 18 (2002)
Issue Number: 10
Column Tag: PROGRAMMER's CHALLENGE

PROGRAMMER's CHALLENGE

by Bob Boonstra

Area

As those of you who are regular readers know, the Programmer's Challenge problems have become more difficult over time. Just as all scientific discoveries worth making had been made by the mid-twentieth century, so it is that all simple Challenge problems have been posed and solved by this time. Then again, as they say, maybe not. This month's problem is borrowed from http://www.polymathlove.com/, where Gary Smith posts software he uses in teaching mathematics to elementary and middle school students. One of his programs is called Area Puzzles, where students create rectangles with specified areas to cover a grid subject to certain constraints.

The prototype for the code you should write is:

void Area(
   const short *cells,
   /* rectangle to be covered with smaller rectangles */
   /* index [row][col] as cells[row*rectWidth + col[ */
   /* value N>0 means this cell must be covered by a rectangle of area N */
   short rectWidth,
   short rectHeight,
   Rect yourRects[]
);

Your Area routine will be called with a rectangle of cells of width rectWidth and height rectHeight. Your task is to create a set of smaller rectangles (yourRect) that cover these cells. In doing so, you need to satisfy some constraints. Certain of the cells will have a nonzero value, and those cells must be covered by a rectangle with an area equal to that value. As an example, if the input cells were configured as follows ...

   0  0  3  0  6  0  0  0  0  8
   0  6  0  0  0  0  0  0  0  0
   0  0  0  0  0  0  4  0  0  0
   0  0  3  0  0  0  0  0  0  0
   0  0  0  0  0  0  0  0  0  0
   6  0  0  0  0  0  0  0 15  0
   0  0  0  0  0  0  0  0  0  0
   0 10  0  0 24  0  0  4  0  0
   0  0  0  0  0  0  0  0  0  0
   0  0  0  4  0  0  4  0  0  3

... you might create a set of rectangles like this, where each cell is shown with the number of the rectangle including that cell.

   1  1  1  2  2  2  3  3  3  3
   4  4  5  2  2  2  3  3  3  3
   4  4  5  6  6  6  6  7  7  7
   4  4  5  8  8  8  8  7  7  7
   9 10 10  8  8  8  8  7  7  7
   9 10 10  8  8  8  8  7  7  7
   9 10 10  8  8  8  8  7  7  7
   9 10 10  8  8  8  8 13 13 14
   9 10 10  8  8  8  8 13 13 14
   9 11 11 11 11 12 12 12 12 14

You should return the rectangles that cover the cell array and satisfy the constraints as yourRects. Each cell may be included in only one rectangle. If the cell has a nonzero value when Area is called, it must be included in a rectangle with an area equal to that value. Memory for the rectangles you create will be allocated for you, and there will be as many of those rectangles as there are nonzero values in the cells array. Any solution that covers the entire cells array and satisfies the constraints will be considered correct.

Scoring will be based on execution time - the winner will be the solution that correctly solves the puzzles with the smallest execution time.

This will be a native PowerPC Carbon C++ Challenge, using the Metrowerks CodeWarrior Pro 7.0 development environment. Please be certain that your code is carbonized, as I may evaluate this Challenge using Mac OS X. Also, when submitting you solution, please include the project file and the code you used to test your solution. Occasionally I receive a solution that will not compile and, while I always try to correct these problems, it is easier to do so if I have your entire project available.

Winner of the July, 2002 Challenge

Congratulations to Alan Hart (United Kingdom) for winning the July One Time Pad Challenge. Recall that this Challenge required readers to decrypt a sequence of messages using a "one time pad". I place the term in quotation marks because the pad was neither "one time", as it was used multiple times, nor was it random, as a true one-time pad would be. Contestants had the advantage of possessing a dictionary of all the possible words in the communication.

Alan's solution tries each possible offset until the decoding attempt results in a sequence of words found in the dictionary. The speed of Alan's solution is due in part to his decision to test the decoding of the first four characters of the message for each offset against the dictionary before proceeding with the rest of the decoding. Another factor is Alan's reuse (with acknowledgement) of ideas from Ernst Munter's solution to the PlayFair Challenge, specifically the dictionary indexing approach. That approach creates an index for each word based on the first three characters that points to the first word in the dictionary beginning with those three letters. I'm pleased to see past Challenge code reused successfully.

Ernst Munter's second place entry also uses a brute force method. His approach is to select a possible offset from the pad, decrypt the message using that offset, verify that the decrypted message contains only words from the dictionary, and repeat with a new offset until successful. Ernst's solution also uses a modified version of the SpellTree dictionary class he developed for the PlayFair Challenge.

Jonny Taylor's third-place solution also examined the first three characters of the decoded message to determine whether an offset was promising enough to continue decoding. As noted by others, because the message may contain special characters not found in the dictionary, offsets rejected by this approach must be revisited to skip potential special characters if the message is not successfully decoded. Moses Hall takes a different approach, creating a finite state machine encoding the dictionary. Rounding out the remainder of the five top-scoring entries, Jan Schotsman used the Altivec programming model and reports achieving a 5% increase in speed over the non-vectorized version.

The table below lists, for each of the solutions submitted, the number of test cases processed correctly, the execution time in seconds, the bonus awarded for code clarity and commentary, and the total score for each solution. It also lists the programming language of each entry. As usual, the number in parentheses after the entrant's name is the total number of Challenge points earned in all Challenges prior to this one.

   
Name                 Cases     Time   Bonus   Score   Lang   
                     Correct   (secs)
Alan Hart (39)        20       0.73    25%    5469.59   C++
Ernst Munter (872)    20       1.30    25%    9721.75   C++
Jonny Taylor (83)     20       1.67    25%   12499.44   C++
Moses Hall            20       2.54    15%   21572.92   C
Jan Schotsman (16)    20       3.05    5%    28959.52   C++
Tom Saxton (230)      20       3.08    5%    29268.66   C++
Damien Bobillot       15       12.11   15%  102918.96   C

Top Contestants ...

Listed here are the Top Contestants for the Programmer's Challenge, including everyone who has accumulated 20 or more points during the past two years. The numbers below include points awarded over the 24 most recent contests, including points earned by this month's entrants.

   
Rank    Name                Points      Wins     Total   
                            (24 mo)    (24 mo)   Points
   1.   Munter, Ernst         251        8         882
   2.   Saxton, Tom            65        2         230
   3.   Taylor, Jonathan       64        2          90
   4.   Stenger, Allen         53        1         118
   5.   Wihlborg, Claes        40        2          49
   6.   Hart, Alan             34        1          59
   7.   Rieken, Willeke        22        1         134
   8.   Landsbert, Robin       22        1          22
   9.   Gregg, Xan             20        1         140
   10.   Mallett, Jeff         20        1         114
   11.   Cooper, Tony          20        1          20
   12.   Truskier, Peter       20        1          20

Here is Alan's winning One Time Pad solution.

    OneTimePad.cp

    Copyright (c) 2002

    Alan Hart

/******************************************\
Problem definition:
----------------
   Decode multiple encrypted messages created using a known one time pad.
   Each message is encrypted using an unknown offset in the one time pad.
   Each character in the encrypted message is the sum of the corresponding  clear text 
   and pad characters.
   The sum is adjusted to remain in the valid character set range.
   The character set is 62 upper/lower case alpha-numerics
   that appear in words in a case-insensitive dictionary, plus 33 other punctuation and special 
   characters
   
   ("delimiters") that can appear in any locations between words.
   
   Total time is to be minimized.
   
Assumptions:
-----------
   We cannot make any assumptions about the number of delimiter characters that 
   may preceed the message or separate the words within it. In an ulikely extreme case 
   the message could be a sea of delimiters with a few short words distributed 
   anywhere within it. It is assumed that the test cases will not be pathological, and the 
   majority of characters in the message will form dictionary words. In particular, the 
   solution is optimized for messages with no leading delimiters before the first 
   dictionary word. It decrypts messges with leading delimiters during a second scan 
   of the pad.
   
Solution Summary:
-----------------
   The external interface calls are passed to a Decoder class which does the work.
   
   The gDecoder instance is dynamically assigned by InitOneTimePad () and allocates 
   a fixed sze array of 256 KBytes for the main dictionary index. Further index Branch 
   records are allocated dynamically during indexing.
   This adds a further 100KBytes to the space required in the case of the dictionry 
   supplied with the test data.
   The solution should fit comfortably in less than 500 KBytes heap space not 
   including the dictionary and message strings.
   
   The decoder creates a small static lookup table containing two concatenated copies   
   of the character set to allow for wrap-around when subtracting the pad and cipher 
   characters, allowing a simple lookup for decoding, and avoiding the need for range 
   limiting.
   
   Decoding is done by trying each possible pad offset in turn until the decode yields a 
   sequence of words that are found in the dictionary.
   
   During the first pass candidate pad offsets are found by testing the first 4 characters 
   at each pad offset with the first 4 message characters. If this fails all pad offsets are 
   retested on the whole message in case the first word does not start at the first 
   character of the message. Decoding with each candidate pad is aborted if any 
   invalid sequence of consecutive dictionary characters is detected or the end of the 
   pad is reached.
   
   The validity of a character sequence is tested in three stages:
   
   1. A sequence of three or more characters must have a "header" index value 
      matched by one or more dictionary words.
      A shorter word must have an index value that matches a bit map of 1 and 2 
      character words. 
   
   2. Characters following a valid header index must form 4-character sequences that 
      exist somewhere in the dictionary.
   
   3. Finally the word is compared with the dictionary entries using a case insensitive 
      character match and length comparison.
   
   The dictionary index uses a 32 character (5 bit) enumeration to allow quick 
   calculation of compact indices, and to enable bit mapping in a single 32 bit word.
   The 10 numeric characters are enumerated using 1 to 5, with pairs of digits sharing 
   the same index value. 6 to 31 enumerate the alphabetic characters, ignoring case. 
   Zero is used to denote any non-dictionary character.
   This enumeration means that words in the supplied dictionary containing numerals 
   in the indexed characters may not be in correct index order. To avoid having to re-
   sort the dictionary this is handled in the index building and searching procedures.
   
   This mapping and indexing system allows a quick confidence check to be carried 
   out during decoding so that non-indexed final dictionary searching is only done on 
   longer words, and only when the word is likely to exist.
   
   Words of one or two characters are recorded in a 32 x 32 bit map for a fast lookup.
   The index for a word of three or more characters is calculated by concatenating the 
   index values of the first three characters.
   The index selects one of an array of 32x32x32 (32K) Index records used for initial 
   and final validation of decoded words.
   If the Index value is the header for words longer than three characters it has a 
   pointer to a dynamically allocated 32 entry lookup table. Each entry in the table 
   points to the first word in the dictionary whose 4th character matches
   one character index value.
   The Index record also has a bit map of the valid fourth character indices that can 
   follow this sequence when it appears at character positions 3, 6, 9 ... in the body of 
   any word in the dictionary. 
   
   Optimizations applied included using unsigned types to remove compiled sign 
   extend instructions, longs to remove compiled byte mask instructions, and the 
   addition of the first pass decoder to limit the tested pad offsets to those yielding 
   valid indices for the first 4 message characters. Instruction sequencing was also 
   adjusted to minimize the time spent on processing failed pad offsets.
   
   Acknowledgement
   ----------------
   The dictionary indexing system uses some ideas gleaned by revisiting
   Ernst Munter's winning solution to the 1999 Playfair challenge.
   
\******************************************/
#include "OneTimePad.h"
//   use unsigned types wherever possible to minimize the insertion of sign extend 
// instructions by the compiler
typedef   unsigned char   uchar;
typedef   unsigned long   ulong;
typedef   unsigned long   blong;   //   used instead of bool to avoid character masking instructions

static const ulong IndexOffset [128] = {
//   lookup table to convert character codes to index offset values
   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
   1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 0, 0, 0, 0, 0, 0,
   0, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
   21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 0, 0, 0, 0, 0,
   0, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
   21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 0, 0, 0, 0, 0
};
IndexValue
static inline ulong
IndexValue (const uchar* inWord) {
//   Return the index for the first three characters of a word
   ulong   index = IndexOffset [*(inWord ++)];
   index = (index << 5) | IndexOffset [*(inWord ++)];
   return (index << 5) | IndexOffset [*inWord];
}
struct Branch
struct Branch {
   const uchar**   fFirstWord;
   const uchar**   fLastWord;
      Branch () { fFirstWord = fLastWord = NULL; }
      
   blong   ValidateWord (uchar* inWord, ulong inWordLength) {
   //   Compare inWord with each word in this branch
   //   The indices are known to match, so inWord already points to the 4th character of 
   // the test word
   //   Note that the ambiguous indexing of numeric characters means it is possible to 
   // return a false positive.
   //   This is not considered a problem, as we are only trying to verify that the one time 
   // pad is correctly aligned to produce a string of credible words. However, it means 
   // that shorter dictionary words can appear within within the indexed range, and 
   // must be ignored.
      const   uchar**   wordPtr =fFirstWord;
      do {
         ulong         header = *(ulong*)(*wordPtr);
         if ((header & 0x0ff0000) && (header & 0x0ff00)) {
         //   3 or more characters in this dictionary word
            const uchar*   w1 = *wordPtr + 4;
            uchar*      w2 = inWord;
            long         difference =  0;
            long         len = inWordLength;
            while ( ! difference && *w1) {
            //   compare the words until the end of the dictionary word
            //   converting upper case characters in inWord to lower case
               difference = (*(w1++) - (*(w2++) | 0x20));
               len --;
            }
            if (difference == 0 && len == 0) return true;
   // words match
            if (difference > 0) return false;
            //   this and subsequent words are greater than inWord
         }
      } while (wordPtr ++ < fLastWord);
      return false;   //   no match found
   }
};
   
class    Index
class    Index {
//   Dictionary index entry for a three-character sequence
public:
//   Data encapsulation is not used, so that the decoder can access Index members 
// directly for higher performance
   Branch   *fBranches;   
      //   Dynamically allocated list of 32 pointers to words that start with this index
   ulong      fMap;
      //   Map of valid fourth character indices that can follow this index in the body 
      // of a word
      //   fMap bit 0 is used to register a valid three letter word for this index
      Index () { fBranches = NULL; fMap = 0; }
      
      ~Index () { delete [] fBranches; }
      
   ulong      //   Return the number of words registered for this index value
   Register (
         const uchar** inWordList,   // Pointer to the first word to register
         ulong inIndex,            //   The index value for words to register 
         const uchar** inLastWord,   // The last word in the dictionary, for 
                                             //range checking
         Index* inIndexArray         
               //   The array if Index records, used to registerword body sequences
   ) {
   //   This is the dictionary indexing procedure
   //   - Registers the existence of one or more 3 character words for this index in fMap 
   // bit zero
   //   - If there are words of 4 or more characters with this index value, allocates a 
   // Branch array   and records the first and last words of the list for each 4th character.
   //   - Registers the existence of each 4-letter sequence in the body of each word in the 
   // fMap for its index
      const uchar**   wordPtr = inWordList;
      ulong         index = inIndex;
      const uchar*   word;
      Branch*      branch = NULL;
      do {
         uchar   c = IndexOffset [*((*wordPtr) + 3)];
         if (c) {
         //   4 or more characters
            if (fBranches == NULL) {
            //   allocate a new branch list to index words on the 4th character
               fBranches = new Branch [32];
               if (fBranches == 0)
                  return 0;   //   bail out and signal allocation failure
            }
         //   record the start and end words in each branch
            if (branch != fBranches + c) {   // end of previous branch
               if (branch)
               //   update the last word pointer for the old branch
                  branch->fLastWord = wordPtr - 1;
            //   move to the next branch
               branch = fBranches + c;
               if (branch->fFirstWord == NULL)
               //   this is the first word to be registered in this branch
               //   insert the first word pointer for this branch
                  branch->fFirstWord = wordPtr;
            }
         //   register the remaining map bits for the body of this word
            word = (*wordPtr) + 3;   //   skip the index
            while (*word && *(word+1) && *(word+2)) {
            //   while there are three or more characters left
            //   get the 4th character index
               c = IndexOffset [*(word + 3)];
               if (c == 0) break;   // no 4th character
            //   register the 4th character in the index bit map
               inIndexArray [ IndexValue (word) ].fMap |= ( 1 <<  c);
               word += 3;
            }
         } else
         //   register the 3 character word in bit 0 of the index map
            fMap |= 1;
         wordPtr ++;   //   next word
         
      //   until end of dictionary or index value changes
      } while (wordPtr < inLastWord && 
                  index == IndexValue (*wordPtr));
      
      if (branch)   //   update the last branch
         branch->fLastWord = wordPtr - 1;
   //   return the number of words registered
      return wordPtr - inWordList;
   }
   blong   ValidateWord (uchar* inWord, ulong inWordLength)
   {
   //   The dictionary lookup procedure.
   //   - Returns true if the word exists in the dictionary
      if (inWordLength == 3)
      //   3 character word. Check map bit 0
         return (fMap & 0x01);
      else if (fBranches) {
      //   Compare the 4th and subsequent characters with the words in the appropriate 
      // branch
         Branch*   branch = fBranches + IndexOffset [*(inWord + 3)];
         if (branch->fFirstWord)
            return branch->ValidateWord (inWord + 4, inWordLength - 4);
      }
      return false;
   }
};
class Decoder
class    Decoder {
private:
//   Pad information
   const uchar*   fPad;            //   local copy of the pad pointer
   const uchar*   fFirstPadChar;   
         //   pointer to the current first pad character
   const uchar*   fLastPadChar;      
         //   pointer to the last pad character to try
   ulong         fMessageLength;      //   length of the encrypted string
//   Dictionary infomation
   Index      fIndexArray [ 32*32*32 ];   //   the dictionary index array
   ulong         fShortWordMap [32];   
         //   a bit map for one and two character words
//   The decoder table
   ulong         fDecodeTable [190];   
         //   lookup table to convert a pair of cipher/pad characters to a clear character
public
   Decoder (const uchar* inPad)
   {
   //   Constructor initializes the data members
   //   The dictionary index is built separately to allow Branch allocation failure to be 
   //  handled gracefully
   //   initialize pad pointers and pad offset
      fPad =inPad;
      fFirstPadChar = fPad;
      
   //   Fill in the decoder table with two concatenated copies of the character set
      int   c;
      ulong*   t = fDecodeTable;
      for (c = 0x20; c < 0x7f; c ++, t ++)
         *t = *(t + 0x5f) = c;
      
   //   Clear the short word map
      for (c = 0; c < 32; c ++)
         fShortWordMap [c] = 0;
   }
   
   blong      IndexDictionary (const uchar** inDictionary, 
                                 ulong inNumWords)
   {
   //   Build the dictionary index and short word map
   //   Return false if Branch allocation falis
      const uchar**   wordPtr = inDictionary;
      const uchar**   lastWord = inDictionary + inNumWords;
      ulong      numWords;
      do {
      //   Process the next index value
         ulong      index = *(ulong*)(*wordPtr);
         numWords = 1;
         if ((index & 0x0ff0000) == 0)   
         //   Register a 1 character word in short word map zero
            *fShortWordMap |= (1 << 
                                 IndexOffset [(index >> 24) & 0xff]);
          else if ((index & 0x0ff00) == 0)
          //   Register a 2 character word in the appropriate short word map
            fShortWordMap[ IndexOffset[(index >> 24) & 0xff] ]
                  |= (1 << IndexOffset [(index >> 16) & 0xff]);
         else {
         //   3 or more characters
         //   Pass the word list to the appropriate Index record for registration
            index = IndexValue (*wordPtr);
            numWords = fIndexArray [index].Register (wordPtr, index, 
                                                lastWord, fIndexArray);
            if (numWords == 0)
            //   branch list allocation failure - bail out
               break;
         }
      //   Next word list
         wordPtr += numWords;
      } while (wordPtr < lastWord);
      
      return (numWords > 0);
   }
      
   void   FindPadOffset (const uchar* inEncryptedMessage, 
                        uchar* outDecryptedMessage, ulong *outOffset)
   {
   //   The main routine called to decode messages
   
   //   Use trial and error to find a pad offset that successfully decodes the message
   //   Return the offset found in *outOffset
      
   //   Firat Pass
   //   Optimized search for candidate pad offsets that decode a valid word index at the 
   // first encrypted character
   //   Build lookup tables to decode each of the first 4 message characters to its index 
   // value for any pad character
   //   Shift the pad through a 4-character buffer and apply this to the 4 message 
   // characters
      typedef   ulong      PadMap [128];
      
      ulong         c0, c1, c2;
      ulong*       mapPtr;
      const uchar*   pad;
      blong         validOffset;
      ulong         map, i;
      const uchar*   cipher = inEncryptedMessage;
      PadMap      padMapArray [4];
   
   //   Measure the encrypted message length
      while (*(++cipher)) {;}
      fMessageLength = cipher - inEncryptedMessage;
      
      cipher = inEncryptedMessage;
      for (map = 0; map < 4; map ++, cipher ++) {
      //   create the decode table for the next cipher character
         mapPtr = padMapArray [map];
      //   clear the entries for invalid pad characters
         *(mapPtr + 0x7f) = 0;
         i = 0x20; while (i --) { *(mapPtr++) = 0; }
      //   set the decoded index values for each valid pad character
      //   calculate the first offset in the decode table for this cipher character
         ulong*   c = fDecodeTable + 0x3f + *cipher;
         i = 0x5f; while (i --) { *(mapPtr++) = I
                                       ndexOffset [*(c --)]; }
      }
   //   Start at the beginning of the one time pad.
   //   Decode these 4 characters with each pad offset and look for a valid word or index
   
      fFirstPadChar = fPad;
      validOffset = false;
      pad = fPad;
   //   prime a sequence of 4 characters with the first 3 valid pad characters
      ulong      padBuffer = 0;
      for (i = 0; i < 3; i ++) {
         while (*pad < 0x20 || *pad > 0x7e) {
            if (*pad == 0) return;   //   Abort if the pad is less than 4 characters
            pad ++;
         }
         padBuffer = (padBuffer << 8) | *(pad ++);
      }
      do {
      //   Shift the next valid pad character into the set
         while (*pad < 0x20 || *pad > 0x7e) { pad ++; }
         padBuffer = (padBuffer << 8) | *(pad ++);
         
         if ( (c0 = padMapArray[0] [padBuffer >> 24]) ) {
         //   1st character decodes to a dictionary character
            if ( (c1 = padMapArray[1] [(padBuffer >> 16) & 0xff] ) ) {
            //   2nd character decodes to a dictionary character
               if ( (c2 = padMapArray [2] [(padBuffer >> 8) & 0xff]) ) {
               //   select the appropriate Index record for the 3 characters
                  Index*   header =fIndexArray + ((((c0 << 5) | c1) << 5) | 
                                                                  c2);
               //   decode the 4th character
                  if ( (c0 = padMapArray [3] [padBuffer & 0xff]) )
                  //   check that the Branch index exists for the 4th character
                     validOffset = header->fBranches && 
                                       header->fBranches [c0].fFirstWord;
                  else   
                  //   check for a valid 3 character word
                     validOffset = header->fMap & 0x01;
               } else
               //   check for a valid 2 character word
                  validOffset = fShortWordMap [c0] & (1 << c1);
            } else
            //   check for a valid single character word
               validOffset = (*fShortWordMap) & (1 << c0);
               
            if (validOffset) {
            //   This pad offset decodes a valid word or index
            //   try to decode the whole message using this candidate pad
               validOffset = ApplyPad (inEncryptedMessage, 
                              outDecryptedMessage);
            }
         }
      //   move the pad offset to the next valid pad character
         do {
            if (*(fFirstPadChar + fMessageLength))
               fFirstPadChar ++;
            else
            //   We've run out of pad characters - end of pass 1
               goto SecondPass;
               
         } while (*fFirstPadChar < 0x20 || *fFirstPadChar > 0x7e);
            
      } while ( ! validOffset);
      if ( ! validOffset ) {
   SecondPass:
      //   Second Pass
      //   The search for the first index may have failed due to leading delimiters 
      //   Try to decode the message using every pad offset in turn
         fFirstPadChar = fPad;
         do {
            validOffset = ApplyPad (inEncryptedMessage, 
                                    outDecryptedMessage);
            fFirstPadChar ++;
         } while ( ! validOffset && *(fFirstPadChar + 
                                                fMessageLength));
      }
   //   return the offset
      *outOffset = fFirstPadChar - fPad - 1;
   }
   blong   ApplyPad (const uchar* inEncryptedMessage, 
                                       uchar* outDecryptedMessage)
   {
   //   Called by FindPadOffset() with fFirstPadChar pointing to the start of a candidate 
   //  pad
   //   Attempt to decode the message using this pad, and return success/failure
   
      const uchar*   pad;      //   pointer to the current pad character
      const uchar*   cipher;   //   pointer to the next encrypted character to decode
      uchar*      clear;   //   pointer to the next clear character to decode
      ulong         index;   //   index value of current word
      const ulong*   origin = fDecodeTable + 0x5f;
               //   pointer to the origin in the DecodeTable
      uchar*      word;   //   pointer to start of current word being processed
      Index      *body, *header;   //   pointers to the Index records for the 
                                       //   current word
      ulong         c0, c1, c2;
               //   index values of the first three characters of teh current word
      ulong         state = 0;   //   controls progress of the word decode process
      
      clear = outDecryptedMessage;
      cipher = inEncryptedMessage;
      pad = fFirstPadChar;
      
      do {
      //   for each character in the message
      
      //   next valid pad character
         while (*pad < 0x20 || *pad > 0x7e) {
            if (*pad) pad++;
            else goto Exit;   //   end of pad
         }
      //   decode the character
         *clear = *(origin + *cipher - *pad);         
         switch (state) {
            case 0:   //   looking for 1st character of a word
               if ( (c0 = IndexOffset [*clear]) ) {
                  word = clear;   //   1st character of a word
                  state = 1;
               }
               break;
            case 1:   //   looking for 2nd character of a word
               if ( (c1 = IndexOffset [*clear]) ) state = 2;
                        //   2nd dictionary char
               else if ((*fShortWordMap) & (1 << c0))
               //   valid 1 character word
                  state = 0;
               else goto Exit;
               break;
            case 2:   //   looking for 3rd character of a word
               if ( (c2 = IndexOffset [*clear]) ) {
                  header =fIndexArray + ((((c0 << 5) | c1) << 5) | c2);
                  state = 3;   //   3rd dictionary char
               } else if (fShortWordMap [c0] & (1 << c1))
               //   valid 2 character word
                  state = 0;
               else goto Exit;
               break;
            case 3:   //   looking for 4th character of a word
               if ( (c0 = IndexOffset [*clear]) ) {
                  if (header->fBranches && 
                                    header->fBranches [c0].fFirstWord) {
                     index = c0;   //   valid 4th dictionary char
                     body = NULL;
                     state = 4;   //   check body sequences
                  } else goto Exit;
               } else if (header->fMap & 0x01)
               //   valid 3 character word
                  state = 0;
               else goto Exit;
               break;
            case 4:
               if ( (c0 = IndexOffset [*clear]) ) {
                  if (body == NULL) {
                     index = (index << 5) | c0; // accumulate the body index
                     if (index > 1024)
                     //   third body index character. select the Index record
                        body = fIndexArray + index;
                  } else {   //   look up this character in the current body Index record
                     if (body->fMap & (1 << c0)) {
                     //   valid sequence - start building the next body index
                        index = c0;
                        body = NULL;
                     } else goto Exit;
                  } 
               } else {
               //   end of the word, confirm it is in the dictionary
                  if (header->ValidateWord (word, clear - word))
                     state = 0;
                  else goto Exit;
               }
            default:
               break;
         }
         pad ++;
         clear ++;
      } while (*(++cipher));
      
   Exit:
      *clear = 0;   //   terminate the decoded string
      return ((*cipher) == 0);   //   successful if we reached the end of the 
                                       //   message
   }
   
} *gDecoder;   //   global pointer to a dynamically allocated instance
InitOneTimePad
void InitOneTimePad (const char *oneTimePad, const char *dictionary[], long numDictionaryWords) {
//   Create the decoder
   gDecoder = new Decoder ((const uchar*) oneTimePad);
   if ( gDecoder) {
   //   Index the dictionary
      if ( ! gDecoder->IndexDictionary ((const uchar**) dictionary, 
                           (ulong) numDictionaryWords)) {
      //   Allocation failure
         delete gDecoder;
         gDecoder = NULL;
      }
   }
}
DecryptMessage
void DecryptMessage(const char *encryptedMessage, char *decryptedMessage, long *offset) {
   if (gDecoder)
      gDecoder->FindPadOffset ((uchar*) encryptedMessage, 
               (uchar*) decryptedMessage, (ulong*)offset);
}
TermOneTimePad
void TermOneTimePad(void) {
   delete gDecoder;
}

 
AAPL
$118.93
Apple Inc.
-0.07
MSFT
$47.81
Microsoft Corpora
+0.06
GOOG
$541.83
Google Inc.
+1.46

MacTech Search:
Community Search:

Software Updates via MacUpdate

Adobe Photoshop Elements 13.0 - Consumer...
Adobe Photoshop Elements 12--the #1 selling consumer photo editing software--helps you edit pictures with powerful, easy-to-use options and share them via print, the web, Facebook, and more.Version... Read more
Skype 7.2.0.412 - Voice-over-internet ph...
Skype allows you to talk to friends, family and co-workers across the Internet without the inconvenience of long distance telephone charges. Using peer-to-peer data transmission technology, Skype... Read more
HoudahSpot 3.9.6 - Advanced file search...
HoudahSpot is a powerful file search tool built upon MacOS X Spotlight. Spotlight unleashed Create detailed queries to locate the exact file you need Narrow down searches. Zero in on files Save... Read more
RapidWeaver 6.0.3 - Create template-base...
RapidWeaver is a next-generation Web design application to help you easily create professional-looking Web sites in minutes. No knowledge of complex code is required, RapidWeaver will take care of... Read more
iPhoto Library Manager 4.1.10 - Manage m...
iPhoto Library Manager lets you organize your photos into multiple iPhoto libraries. Separate your high school and college photos from your latest summer vacation pictures. Or keep some photo... Read more
iExplorer 3.5.1.9 - View and transfer al...
iExplorer is an iPhone browser for Mac lets you view the files on your iOS device. By using a drag and drop interface, you can quickly copy files and folders between your Mac and your iPhone or... Read more
MacUpdate Desktop 6.0.3 - Discover and i...
MacUpdate Desktop 6 brings seamless 1-click installs and version updates to your Mac. With a free MacUpdate account and MacUpdate Desktop 6, Mac users can now install almost any Mac app on macupdate.... Read more
SteerMouse 4.2.2 - Powerful third-party...
SteerMouse is an advanced driver for USB and Bluetooth mice. It also supports Apple Mighty Mouse very well. SteerMouse can assign various functions to buttons that Apple's software does not allow,... Read more
iMazing 1.1 - Complete iOS device manage...
iMazing (was DiskAid) is the ultimate iOS device manager with capabilities far beyond what iTunes offers. With iMazing and your iOS device (iPhone, iPad, or iPod), you can: Copy music to and from... Read more
PopChar X 7.0 - Floating window shows av...
PopChar X helps you get the most out of your font collection. With its crystal-clear interface, PopChar X provides a frustration-free way to access any font's special characters. Expanded... Read more

Latest Forum Discussions

See All

Mystery Case Files: Dire Grove, Sacred G...
Mystery Case Files: Dire Grove, Sacred Grove HD Review By Jennifer Allen on November 28th, 2014 Our Rating: iPad Only App - Designed for the iPad A decent new installment for the popular Mystery Case Files series.   | Read more »
Castaway Paradise – Tips, Tricks, and St...
Ahoy there, castaways: Were you curious about our own thoughts regarding this pristine shipwreck? Check out our Castaway Paradise review! Castaway Paradise is out for iOS, finally giving mobile gamers the opportunity to enjoy the idyllic lifestyle... | Read more »
Castaway Paradise VIP Subs are on Sale f...
Castaway Paradise VIP Subs are on Sale for a Limited Time, and a Special Holiday Update is Coming Soon Posted by Rob Rich on November 28th, 2014 [ | Read more »
Primitive Review
Primitive Review By Jordan Minor on November 28th, 2014 Our Rating: :: FOLK ARTUniversal App - Designed for iPhone and iPad True to its name, Primitive is about as straightforward as runners get.   | Read more »
7 tips to get ahead of the competition i...
7 tips to get ahead of the competition in Dynasty of Dungeons Posted by Simon Reed on November 28th, 2014 [ permalink ] Playcrab has launched their action-packed new dungeon crawler, Dynasty of Dungeons, today. | Read more »
Master of Tea Kung Fu Review
Master of Tea Kung Fu Review By Jordan Minor on November 28th, 2014 Our Rating: :: ONE DROP RULESUniversal App - Designed for iPhone and iPad Master of Tea Kung Fu is a creative and complex caffeinated brawler.   | Read more »
Monster Strike Review
Monster Strike Review By Campbell Bird on November 28th, 2014 Our Rating: :: BILLIARD STRATEGYUniversal App - Designed for iPhone and iPad Collect monsters and battle by flinging them across the battlefield in this strangely... | Read more »
Proun+ Review
Proun+ Review By Jennifer Allen on November 28th, 2014 Our Rating: :: TWITCHY RACINGUniversal App - Designed for iPhone and iPad Twitchy racing aplenty in Proun+, an enjoyably tricky title.   | Read more »
Lucha Amigos (Games)
Lucha Amigos 1.0 Device: iOS Universal Category: Games Price: $1.99, Version: 1.0 (iTunes) Description: Forget Ninja Turtles, and meet Wrestlers Turtles! Crazier, Spicier and…Bouncier! Sling carapaces of 7 Luchadores to knock all... | Read more »
Record of Agarest War Zero (Games)
Record of Agarest War Zero 1.0 Device: iOS Universal Category: Games Price: $7.99, Version: 1.0 (iTunes) Description: HyperDevbox Holiday Turkey Black Friday Special Pricing! To celebrate the opening of the holiday season HyperDevbox... | Read more »

Price Scanner via MacPrices.net

Up To 75% Off Infovole Text Apps Over Black F...
Infovole’s entire range of apps, including the Textkraft family of word processors for iPads and iPhones, is being offered at 50-75% off over the Black Friday and Cyber Monday weekend. The five-day... Read more
Black Friday: Up to $60 off Mac minis, NY tax...
 B&H Photo has new 2014 Mac minis on sale for up to $60 off MSRP as part of their Black Friday sale. Shipping is free, and B&H charges NY sales tax only: - 1.4GHz Mac mini: $449.99 $50 off... Read more
Black Friday: 27-inch 5K iMac for $2299, save...
 B&H Photo continues to offer Black Friday sale prices on the 27″ 3.5GHz 5K iMac, in stock today and on sale for $2299 including free shipping plus NY sales tax only. Their price is $200 off MSRP... Read more
Karalux Announces 24K Gold-Plated iPhone 6
Karalux, a Vietnam-based jewellery firm, has launched a unique 24 karat gold-plated iPhone 6 version with gold-cast monolithic dragon on its back panel. The real 24 karat gold plated enclosure doesn’... Read more
Black Friday: 13-inch 2.6GHz Retina MacBook P...
 B&H Photo has lowered their price for the 13″ 2.6GHz/128GB Retina MacBook Pro to $1159 for Black Friday. That’s $140 off MSRP, and it’s the lowest price for this model (except for Apple’s $1099... Read more
View all the Black Friday sales on our Mac Pr...
We’ve updated our Mac Price Trackers with the latest information on prices, bundles, and availability on systems from Apple’s authorized internet/catalog resellers. View Black Friday sale prices at a... Read more
Black Friday: 11-inch MacBook Air for $779, s...
 Best Buy has lowered their price for the 2014 11″ 1.4GHz/128GB MacBook Air to $779.99 for Black Friday. That’s $120 off MSRP. Choose free shipping or free local store pickup (if available). Sale... Read more
Apple Store Black Friday sale for 2014: $100...
BLACK FRIDAY The Apple Store has posted their Black Friday deals for 2014. Receive a $100 PRODUCT(RED) branded iTunes gift card with the purchase of select Macs, $50 with iPads, and $25 with iPods,... Read more
Black Friday: 15% off iTunes Gift Cards
Staples is offering 15% off $50 and $100 iTunes Gift Cards on their online store as part of their Black Friday sale. Click here for more information. Shipping is free. Best Buy is offering $100... Read more
BEVL Releases Dock Tailored for iPhone 6 and...
Seattle based BEVL has released their first product: an iPhone dock that is divergent in build quality, rock-solid function and visual simplicity to complement the iPhone. BEVL is now accepting... Read more

Jobs Board

*Apple* Solutions Consultant (ASC) - Apple (...
**Job Summary** The ASC is an Apple employee who serves as an Apple brand ambassador and influencer in a Reseller's store. The ASC's role is to grow Apple Read more
Senior Event Manager, *Apple* Retail Market...
…This senior level position is responsible for leading and imagining the Apple Retail Team's global event strategy. Delivering an overarching brand story; in-store, Read more
*Apple* Retail - Multiple Positions (US) - A...
Sales Specialist - Retail Customer Service and Sales Transform Apple Store visitors into loyal Apple customers. When customers enter the store, you're also the Read more
*Apple* Solutions Consultant (ASC) - Apple (...
**Job Summary** The ASC is an Apple employee who serves as an Apple brand ambassador and influencer in a Reseller's store. The ASC's role is to grow Apple Read more
*Apple* Solutions Consultant (ASC) - Apple (...
**Job Summary** The ASC is an Apple employee who serves as an Apple brand ambassador and influencer in a Reseller's store. The ASC's role is to grow Apple Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.