aboutsummaryrefslogtreecommitdiffstats
path: root/tools/firmware-utils/src/encode_crc.c
blob: 647fb92e89051c3a15d90114872f5139bcf0c274 (plain)
1
2
3
4
5
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/* **************************************************************************

   This program creates a CRC checksum and encodes the file that is named
   in the command line.
   
   Compile with:  gcc encode_crc.c -Wall -o encode_crc

   Author:     Michael Margraf  (michael.margraf@freecom.com)
   Copyright:  Freecom Technology GmbH, Berlin, 2004
               www.freecom.com

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

 ************************************************************************* */


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>

// *******************************************************************
// CCITT polynom G(x)=x^16+x^12+x^5+1
#define POLYNOM  0x1021

// CRC algorithm with MSB first
int make_crc16(int crc, char new)
{
  int i;
  crc = crc ^ (((int)new) << 8);
  
  for(i=0; i<8; i++) {  // work on 8 bits in "new"
    crc <<= 1;          // MSBs first
    if(crc & 0x10000)  crc ^= POLYNOM;
  }
  return crc & 0xFFFF;
}

// *******************************************************************
// Reads the file "filename" into memory and returns pointer to the buffer.
static char *readfile(char *filename, int *size)
{
	FILE		*fp;
	char		*buffer;
	struct stat	info;
	
	if (stat(filename,&info)!=0)
		return NULL;

	if ((fp=fopen(filename,"r"))==NULL)
		return NULL;

	buffer=NULL;
	for (;;)
	{
		if ((buffer=(char *)malloc(info.st_size+1))==NULL)
			break;

		if (fread(buffer,1,info.st_size,fp)!=info.st_size)
		{
			free(buffer);
			buffer=NULL;
			break;
		}

		buffer[info.st_size]='\0';
		if(size) *size = info.st_size;

		break;
	}

	(void)fclose(fp);

	return buffer;
}


// *******************************************************************
int main(int argc, char** argv)
{
  if(argc < 3) {
    printf("ERROR: Argument missing!\n\n");
    return 1;
  }

  int count;  // size of file in bytes
  char *p, *master = readfile(argv[1], &count);
  if(!master) {
    printf("ERROR: File not found!\n");
    return 1;
  }

  int crc = 0xFFFF, z;

  p = master;
  for(z=0; z<count; z++)
    crc = make_crc16(crc, *(p++));  // calculate CRC
  short crc16 = (short)crc;

	/*
  if(argc > 2) {   // with flag for device recognition ?
    p = argv[2];
    for(z=strlen(p); z>0; z--) {
      crc ^= (int)(*p);
      *(p++) = (char)crc;  // encode device flag
    }
  }
	*/

  p = master;
  for(z=0; z<count; z++) {
    crc ^= (int)(*p);
    *(p++) = (char)crc;  // encode file
  }


  // write encoded file...
  FILE *fp = fopen(argv[2], "w");
  if(!fp) {
    printf("ERROR: File not writeable!\n");
    return 1;
  }

  if(argc > 3) {  // add flag for device recognition ?
    fwrite(argv[3], strlen(argv[3]), sizeof(char), fp);
  }
  else {
    // Device is an FSG, so byte swap (IXP4xx is big endian)
    crc16 = ((crc16 >> 8) & 0xFF) | ((crc16 << 8) & 0xFF00);
  }

  fwrite(&crc16, 1, sizeof(short), fp);     // first write CRC

  fwrite(master, count, sizeof(char), fp);  // write content
  fclose(fp);

  free(master);
  return 0;
}
mPassphrase = getArguments().getString(ARG_PASSPHRASE); // set values mNameEdit.setText(mName); mEmailEdit.setText(mEmail); mCreateButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { createKey(); } }); mBackButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mCreateKeyActivity.loadFragment(null, null, CreateKeyActivity.FRAG_ACTION_TO_LEFT); } }); return view; } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); mCreateKeyActivity = (CreateKeyActivity) getActivity(); } private void createKey() { Intent intent = new Intent(getActivity(), KeychainIntentService.class); intent.setAction(KeychainIntentService.ACTION_SAVE_KEYRING); KeychainIntentServiceHandler saveHandler = new KeychainIntentServiceHandler( getActivity(), getString(R.string.progress_building_key), ProgressDialog.STYLE_HORIZONTAL) { public void handleMessage(Message message) { // handle messages by standard KeychainIntentServiceHandler first super.handleMessage(message); if (message.arg1 == KeychainIntentServiceHandler.MESSAGE_OKAY) { // get returned data bundle Bundle returnData = message.getData(); if (returnData == null) { return; } final OperationResults.SaveKeyringResult result = returnData.getParcelable(OperationResultParcel.EXTRA_RESULT); if (result == null) { Log.e(Constants.TAG, "result == null"); return; } if (mUploadCheckbox.isChecked()) { // result will be displayed after upload uploadKey(result); } else { Intent data = new Intent(); data.putExtra(OperationResultParcel.EXTRA_RESULT, result); getActivity().setResult(Activity.RESULT_OK, data); getActivity().finish(); } } } }; // fill values for this action Bundle data = new Bundle(); SaveKeyringParcel parcel = new SaveKeyringParcel(); parcel.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd( Algorithm.RSA, 4096, null, KeyFlags.CERTIFY_OTHER, 0L)); parcel.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd( Algorithm.RSA, 4096, null, KeyFlags.SIGN_DATA, 0L)); parcel.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd( Algorithm.RSA, 4096, null, KeyFlags.ENCRYPT_COMMS | KeyFlags.ENCRYPT_STORAGE, 0L)); String userId = KeyRing.createUserId(mName, mEmail, null); parcel.mAddUserIds.add(userId); parcel.mChangePrimaryUserId = userId; parcel.mNewPassphrase = mPassphrase; // get selected key entries data.putParcelable(KeychainIntentService.SAVE_KEYRING_PARCEL, parcel); intent.putExtra(KeychainIntentService.EXTRA_DATA, data); // Create a new Messenger for the communication back Messenger messenger = new Messenger(saveHandler); intent.putExtra(KeychainIntentService.EXTRA_MESSENGER, messenger); saveHandler.showProgressDialog(getActivity()); getActivity().startService(intent); } private void uploadKey(final OperationResults.SaveKeyringResult saveKeyResult) { // Send all information needed to service to upload key in other thread final Intent intent = new Intent(getActivity(), KeychainIntentService.class); intent.setAction(KeychainIntentService.ACTION_UPLOAD_KEYRING); // set data uri as path to keyring Uri blobUri = KeychainContract.KeyRings.buildUnifiedKeyRingUri( saveKeyResult.mRingMasterKeyId); intent.setData(blobUri); // fill values for this action Bundle data = new Bundle(); // upload to favorite keyserver String keyserver = Preferences.getPreferences(getActivity()).getKeyServers()[0]; data.putString(KeychainIntentService.UPLOAD_KEY_SERVER, keyserver); intent.putExtra(KeychainIntentService.EXTRA_DATA, data); KeychainIntentServiceHandler saveHandler = new KeychainIntentServiceHandler(getActivity(), getString(R.string.progress_uploading), ProgressDialog.STYLE_HORIZONTAL) { public void handleMessage(Message message) { // handle messages by standard KeychainIntentServiceHandler first super.handleMessage(message); if (message.arg1 == KeychainIntentServiceHandler.MESSAGE_OKAY) { // TODO: upload operation needs a result! // TODO: then combine these results //if (result.getResult() == OperationResultParcel.RESULT_OK) { //Notify.showNotify(getActivity(), R.string.key_send_success, //Notify.Style.INFO); Intent data = new Intent(); data.putExtra(OperationResultParcel.EXTRA_RESULT, saveKeyResult); getActivity().setResult(Activity.RESULT_OK, data); getActivity().finish(); } } }; // Create a new Messenger for the communication back Messenger messenger = new Messenger(saveHandler); intent.putExtra(KeychainIntentService.EXTRA_MESSENGER, messenger); // show progress dialog saveHandler.showProgressDialog(getActivity()); // start service with intent getActivity().startService(intent); } }