home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l460 / 1.ddi / MATBIN.DI$ / BINPATCH.M < prev    next >
Encoding:
Text File  |  1993-06-15  |  2.4 KB  |  89 lines

  1. function status = binpatch(filename, patch_data)
  2. %binpatch    A program for patching binary files
  3. %
  4. %    Usage: binpatch('filename', patch_data)
  5. %
  6. %    where patch_data is a matrix with three columns
  7. %          for address, old_value, and new_value
  8. %
  9. % Marc Ullman   June 12, 1993
  10. % Copyright (C) 1993, The MathWorks, Inc.
  11. % All Rights Reserved.
  12. %
  13.  
  14. status = -1;
  15. successes = 0;
  16. previous_patches = 0;
  17. mismatches = 0;
  18. num_patches = size(patch_data,1) - 1;
  19.  
  20. if (sum(patch_data) ~= 0)
  21.     fprintf('binpatch error: Patch data is corrupt\n')
  22.     return
  23. end
  24.  
  25. fid = fopen(filename,'rb+  ');
  26. if (fid == -1)
  27.     fprintf('binpatch error: Unable to open "%s"\n', filename)
  28.     return
  29. end
  30.  
  31. for i=1:num_patches
  32.     stat = fseek(fid, patch_data(i,1), 'bof');
  33.     if (stat ~= 0)
  34.     fprintf('binpatch error: Seek failed in "%s"\n', filename)
  35.     return
  36.     end
  37.  
  38.     old_value = fread(fid, 1, 'uchar');
  39.     if (old_value == [])
  40.     fprintf('binpatch error: Failed reading from "%s"\n', filename)
  41.     return
  42.     end
  43.     if (old_value == patch_data(i,3))
  44.     previous_patches = previous_patches + 1;
  45.     fprintf('Address %.8d has already been patched\n', patch_data(i,1))
  46.     elseif (old_value ~= patch_data(i,2))
  47.     mismatches = mismatches + 1;
  48.     fprintf('Patch data differs from input file at address %.8d\n', ...
  49.       patch_data(i,1));
  50.     fprintf('         input file: %.3d         patch data: %.3d\n', ...
  51.       old_value, patch_data(i,2));
  52.     else
  53.     stat = fseek(fid, patch_data(i,1), 'bof');
  54.     if (stat ~= 0)
  55.         fprintf('binpatch error: Seek failed in "%s"\n', filename)
  56.         return
  57.     end    
  58.  
  59.     count = fwrite(fid, patch_data(i,3), 'uchar');
  60.     if (count == 1)
  61.         successes = successes + 1;
  62.     else
  63.         fprintf('binpatch Error: Failed writing to "%s"\n', filename)
  64.         return
  65.     end
  66.     end
  67. end
  68.  
  69. stat = fclose(fid);
  70. if (stat ~= 0)
  71.     fprintf('binpatch error: Failed closing "%s"\n', filename)
  72.     return
  73. end
  74.  
  75. status = 0;
  76. if (previous_patches == num_patches)
  77.     fprintf('\n"%s" was already patched - no changes were necessary\n', ...
  78.       filename)
  79. elseif (successes + previous_patches == num_patches)
  80.     fprintf('\n"%s" was patched successfully\n', filename)
  81.  
  82. elseif (mismatches + previous_patches == num_patches)
  83.     fprintf('\n"%s" was not modified\n', filename)
  84. else
  85.     fprintf('\n"%s" was only partially patched - it may now be corrupt!\n', ...
  86.       filename)
  87.     status = -1;
  88. end
  89.