fork(1) download
  1. <?php
  2.  
  3. $null = chr(0);
  4. $data = file_get_contents('i');
  5.  
  6. // Find all the "words" from the data
  7. preg_match_all('|\b(\w+)\b|', $data, $matches);
  8.  
  9. // Calculate the total bytes the word uses in the data
  10. $freq = array()
  11. foreach ($matches[0] as $word) {
  12.  
  13. if (!isset($freq[$word])) { // Yet unused, initialize it
  14. $freq[$word] = strlen($word);
  15.  
  16. } else { // Seen before, just add to it
  17. $freq[$word] += strlen($word);
  18. }
  19. }
  20.  
  21.  
  22. // For first word we'll substitute it with '0', so start there.
  23. $num = 0;
  24.  
  25. // Prepare our dictionary
  26. $dict = array();
  27.  
  28. // For each of the words...
  29. foreach ($freq as $word => $size) {
  30. // Set aside the length of the word we're substituting
  31. $len = strlen($word);
  32.  
  33. // Calculate how many times the word was seen
  34. $seen = $size / $len;
  35.  
  36. // If the (dictionary entry + substitution size * occurances)
  37. // is less than the number of bytes the word currently takes or
  38. // if the "word" happens to be a number...
  39. if ($len + $seen * strlen($num) < $size || is_int($word)) {
  40.  
  41. // Then replace it with its representation in the data
  42. $data = preg_replace('|\b'. preg_quote($word) .'\b|', $num, $data);
  43.  
  44. // Add the word to the dictionary (it's index is it's substitution)
  45. $dict[] = $word;
  46.  
  47. // The next substitution gets the subsequent number
  48. $num++;
  49. }
  50. }
  51.  
  52. // Glue the dictionary data together with nulls
  53. $dict = implode($null, $dict);
  54.  
  55. // Glue the compressed data on the end of the dictionary with two nulls
  56. $comp = $dict . $null . $null . $data;
  57.  
  58. // Write the whole thing to file
  59. file_put_contents('o', $comp);
  60.  
  61. ?>
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty