annotate ostc4pack/src/OSTC4pack_V4.cpp @ 187:485c5135cf7f cleanup-2

cleanup: forgotten cleanup from 9da7dd50e2ec Simply cleanup something forgotten in 9da7dd50e2ec. There is so much duplicate code, that things are very easy to forget when cleaning up. Signed-off-by: Jan Mulder <jlmulder@xs4all.nl>
author Jan Mulder <jlmulder@xs4all.nl>
date Fri, 15 Mar 2019 12:53:14 +0100
parents ad98da7e74f8
children 01f40cb1057e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
49
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
1 #include <iostream>
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
2
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
3 /* run this program using the console pauser or add your own getch, system("pause") or input loop */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
4
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
5
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
6
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
7
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
8 #include <stdlib.h>
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
9
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
10 /******************************************************************************/
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
11 /* Start of crcmodel.h */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
12 /******************************************************************************/
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
13 /* */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
14 /* Author : Ross Williams (ross@guest.adelaide.edu.au.). */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
15 /* Date : 3 June 1993. */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
16 /* Status : Public domain. */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
17 /* */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
18 /* Description : This is the header (.h) file for the reference */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
19 /* implementation of the Rocksoft^tm Model CRC Algorithm. For more */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
20 /* information on the Rocksoft^tm Model CRC Algorithm, see the document */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
21 /* titled "A Painless Guide to CRC Error Detection Algorithms" by Ross */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
22 /* Williams (ross@guest.adelaide.edu.au.). This document is likely to be in */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
23 /* "ftp.adelaide.edu.au/pub/rocksoft". */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
24 /* */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
25 /* Note: Rocksoft is a trademark of Rocksoft Pty Ltd, Adelaide, Australia. */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
26 /* */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
27 /******************************************************************************/
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
28 /* */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
29 /* How to Use This Package */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
30 /* ----------------------- */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
31 /* Step 1: Declare a variable of type cm_t. Declare another variable */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
32 /* (p_cm say) of type p_cm_t and initialize it to point to the first */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
33 /* variable (e.g. p_cm_t p_cm = &cm_t). */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
34 /* */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
35 /* Step 2: Assign values to the parameter fields of the structure. */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
36 /* If you don't know what to assign, see the document cited earlier. */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
37 /* For example: */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
38 /* p_cm->cm_width = 16; */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
39 /* p_cm->cm_poly = 0x8005L; */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
40 /* p_cm->cm_init = 0L; */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
41 /* p_cm->cm_refin = TRUE; */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
42 /* p_cm->cm_refot = TRUE; */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
43 /* p_cm->cm_xorot = 0L; */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
44 /* Note: Poly is specified without its top bit (18005 becomes 8005). */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
45 /* Note: Width is one bit less than the raw poly width. */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
46 /* */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
47 /* Step 3: Initialize the instance with a call cm_ini(p_cm); */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
48 /* */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
49 /* Step 4: Process zero or more message bytes by placing zero or more */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
50 /* successive calls to cm_nxt. Example: cm_nxt(p_cm,ch); */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
51 /* */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
52 /* Step 5: Extract the CRC value at any time by calling crc = cm_crc(p_cm); */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
53 /* If the CRC is a 16-bit value, it will be in the bottom 16 bits. */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
54 /* */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
55 /******************************************************************************/
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
56 /* */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
57 /* Design Notes */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
58 /* ------------ */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
59 /* PORTABILITY: This package has been coded very conservatively so that */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
60 /* it will run on as many machines as possible. For example, all external */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
61 /* identifiers have been restricted to 6 characters and all internal ones to */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
62 /* 8 characters. The prefix cm (for Crc Model) is used as an attempt to avoid */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
63 /* namespace collisions. This package is endian independent. */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
64 /* */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
65 /* EFFICIENCY: This package (and its interface) is not designed for */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
66 /* speed. The purpose of this package is to act as a well-defined reference */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
67 /* model for the specification of CRC algorithms. If you want speed, cook up */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
68 /* a specific table-driven implementation as described in the document cited */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
69 /* above. This package is designed for validation only; if you have found or */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
70 /* implemented a CRC algorithm and wish to describe it as a set of parameters */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
71 /* to the Rocksoft^tm Model CRC Algorithm, your CRC algorithm implementation */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
72 /* should behave identically to this package under those parameters. */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
73 /* */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
74 /******************************************************************************/
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
75
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
76 /* The following #ifndef encloses this entire */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
77 /* header file, rendering it indempotent. */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
78 #ifndef CM_DONE
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
79 #define CM_DONE
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
80
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
81 /******************************************************************************/
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
82
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
83 /* The following definitions are extracted from my style header file which */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
84 /* would be cumbersome to distribute with this package. The DONE_STYLE is the */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
85 /* idempotence symbol used in my style header file. */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
86
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
87 #ifndef DONE_STYLE
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
88
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
89 typedef unsigned long ulong;
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
90 typedef unsigned char * p_ubyte_;
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
91
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
92 #ifndef TRUE
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
93 #define FALSE 0
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
94 #define TRUE 1
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
95 #endif
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
96
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
97 /* Change to the second definition if you don't have prototypes. */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
98 #define P_(A) A
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
99 /* #define P_(A) () */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
100
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
101 /* Uncomment this definition if you don't have void. */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
102 /* typedef int void; */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
103
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
104 #endif
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
105
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
106 /******************************************************************************/
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
107
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
108 /* CRC Model Abstract Type */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
109 /* ----------------------- */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
110 /* The following type stores the context of an executing instance of the */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
111 /* model algorithm. Most of the fields are model parameters which must be */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
112 /* set before the first initializing call to cm_ini. */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
113 typedef struct
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
114 {
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
115 int cm_width; /* Parameter: Width in bits [8,32]. */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
116 ulong cm_poly; /* Parameter: The algorithm's polynomial. */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
117 ulong cm_init; /* Parameter: Initial register value. */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
118 bool cm_refin; /* Parameter: Reflect input bytes? */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
119 bool cm_refot; /* Parameter: Reflect output CRC? */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
120 ulong cm_xorot; /* Parameter: XOR this to output CRC. */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
121
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
122 ulong cm_reg; /* Context: Context during execution. */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
123 } cm_t;
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
124 typedef cm_t *p_cm_t;
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
125
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
126 /******************************************************************************/
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
127
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
128 /* Functions That Implement The Model */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
129 /* ---------------------------------- */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
130 /* The following functions animate the cm_t abstraction. */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
131
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
132 void cm_ini P_((p_cm_t p_cm));
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
133 /* Initializes the argument CRC model instance. */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
134 /* All parameter fields must be set before calling this. */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
135
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
136 void cm_nxt P_((p_cm_t p_cm,int ch));
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
137 /* Processes a single message byte [0,255]. */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
138
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
139 void cm_blk P_((p_cm_t p_cm,p_ubyte_ blk_adr,ulong blk_len));
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
140 /* Processes a block of message bytes. */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
141
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
142 ulong cm_crc P_((p_cm_t p_cm));
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
143 /* Returns the CRC value for the message bytes processed so far. */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
144
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
145 /******************************************************************************/
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
146
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
147 /* Functions For Table Calculation */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
148 /* ------------------------------- */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
149 /* The following function can be used to calculate a CRC lookup table. */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
150 /* It can also be used at run-time to create or check static tables. */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
151
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
152 ulong cm_tab P_((p_cm_t p_cm,int index));
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
153 /* Returns the i'th entry for the lookup table for the specified algorithm. */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
154 /* The function examines the fields cm_width, cm_poly, cm_refin, and the */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
155 /* argument table index in the range [0,255] and returns the table entry in */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
156 /* the bottom cm_width bytes of the return value. */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
157
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
158 /******************************************************************************/
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
159
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
160 /* End of the header file idempotence #ifndef */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
161 #endif
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
162
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
163 /******************************************************************************/
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
164 /* End of crcmodel.h */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
165 /******************************************************************************/
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
166
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
167
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
168 /******************************************************************************/
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
169 /* Start of crcmodel.c */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
170 /******************************************************************************/
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
171 /* */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
172 /* Author : Ross Williams (ross@guest.adelaide.edu.au.). */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
173 /* Date : 3 June 1993. */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
174 /* Status : Public domain. */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
175 /* */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
176 /* Description : This is the implementation (.c) file for the reference */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
177 /* implementation of the Rocksoft^tm Model CRC Algorithm. For more */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
178 /* information on the Rocksoft^tm Model CRC Algorithm, see the document */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
179 /* titled "A Painless Guide to CRC Error Detection Algorithms" by Ross */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
180 /* Williams (ross@guest.adelaide.edu.au.). This document is likely to be in */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
181 /* "ftp.adelaide.edu.au/pub/rocksoft". */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
182 /* */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
183 /* Note: Rocksoft is a trademark of Rocksoft Pty Ltd, Adelaide, Australia. */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
184 /* */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
185 /******************************************************************************/
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
186 /* */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
187 /* Implementation Notes */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
188 /* -------------------- */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
189 /* To avoid inconsistencies, the specification of each function is not echoed */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
190 /* here. See the header file for a description of these functions. */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
191 /* This package is light on checking because I want to keep it short and */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
192 /* simple and portable (i.e. it would be too messy to distribute my entire */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
193 /* C culture (e.g. assertions package) with this package. */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
194 /* */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
195 /******************************************************************************/
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
196
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
197 #include "crcmodel.h"
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
198
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
199 /******************************************************************************/
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
200
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
201 /* The following definitions make the code more readable. */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
202
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
203 #define BITMASK(X) (1L << (X))
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
204 #define MASK32 0xFFFFFFFFL
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
205 #define LOCAL static
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
206
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
207 /******************************************************************************/
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
208
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
209 ulong reflect P_((ulong v,int b))
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
210 {
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
211 int i;
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
212 ulong t = v;
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
213 for (i=0; i<b; i++)
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
214 {
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
215 if (t & 1L)
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
216 v|= BITMASK((b-1)-i);
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
217 else
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
218 v&= ~BITMASK((b-1)-i);
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
219 t>>=1;
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
220 }
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
221 return v;
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
222 }
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
223
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
224 /******************************************************************************/
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
225
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
226 LOCAL ulong widmask (p_cm_t p_cm)
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
227 {
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
228 return (((1L<<(p_cm->cm_width-1))-1L)<<1)|1L;
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
229 }
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
230
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
231 /******************************************************************************/
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
232
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
233 void cm_ini (p_cm_t p_cm)
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
234 {
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
235 p_cm->cm_reg = p_cm->cm_init;
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
236 }
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
237
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
238 /******************************************************************************/
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
239
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
240 void cm_nxt (p_cm_t p_cm,int ch)
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
241 {
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
242 int i;
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
243 ulong uch = (ulong) ch;
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
244 ulong topbit = BITMASK(p_cm->cm_width-1);
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
245
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
246 if (p_cm->cm_refin) uch = reflect(uch,8);
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
247 p_cm->cm_reg ^= (uch << (p_cm->cm_width-8));
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
248 for (i=0; i<8; i++)
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
249 {
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
250 if (p_cm->cm_reg & topbit)
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
251 p_cm->cm_reg = (p_cm->cm_reg << 1) ^ p_cm->cm_poly;
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
252 else
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
253 p_cm->cm_reg <<= 1;
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
254 p_cm->cm_reg &= widmask(p_cm);
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
255 }
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
256 }
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
257
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
258 /******************************************************************************/
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
259
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
260 void cm_blk (p_cm_t p_cm,p_ubyte_ blk_adr,ulong blk_len)
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
261 {
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
262 while (blk_len--) cm_nxt(p_cm,*blk_adr++);
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
263 }
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
264
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
265 /******************************************************************************/
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
266
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
267 ulong cm_crc (p_cm_t p_cm)
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
268 {
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
269 if (p_cm->cm_refot)
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
270 return p_cm->cm_xorot ^ reflect(p_cm->cm_reg,p_cm->cm_width);
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
271 else
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
272 return p_cm->cm_xorot ^ p_cm->cm_reg;
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
273 }
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
274
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
275 /******************************************************************************/
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
276
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
277 ulong cm_tab (p_cm_t p_cm,int index)
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
278 {
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
279 int i;
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
280 ulong r;
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
281 ulong topbit = BITMASK(p_cm->cm_width-1);
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
282 ulong inbyte = (ulong) index;
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
283
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
284 if (p_cm->cm_refin) inbyte = reflect(inbyte,8);
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
285 r = inbyte << (p_cm->cm_width-8);
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
286 for (i=0; i<8; i++)
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
287 if (r & topbit)
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
288 r = (r << 1) ^ p_cm->cm_poly;
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
289 else
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
290 r<<=1;
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
291 if (p_cm->cm_refin) r = reflect(r,p_cm->cm_width);
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
292 return r & widmask(p_cm);
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
293 }
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
294
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
295 /******************************************************************************/
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
296 /* End of crcmodel.c */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
297 /******************************************************************************/
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
298
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
299 #define uint32_t unsigned int
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
300 #define uint8_t unsigned char
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
301
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
302 uint32_t CRC_CalcBlockCRC(uint32_t *buffer, uint32_t words)
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
303 {
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
304 cm_t crc_model;
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
305 uint32_t word_to_do;
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
306 uint8_t byte_to_do;
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
307 int i;
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
308
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
309 // Values for the STM32F generator.
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
310
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
311 crc_model.cm_width = 32; // 32-bit CRC
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
312 crc_model.cm_poly = 0x04C11DB7; // CRC-32 polynomial
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
313 crc_model.cm_init = 0xFFFFFFFF; // CRC initialized to 1's
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
314 crc_model.cm_refin = FALSE; // CRC calculated MSB first
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
315 crc_model.cm_refot = FALSE; // Final result is not bit-reversed
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
316 crc_model.cm_xorot = 0x00000000; // Final result XOR'ed with this
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
317
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
318 cm_ini(&crc_model);
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
319
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
320 while (words--)
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
321 {
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
322 // The STM32F10x hardware does 32-bit words at a time!!!
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
323
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
324 word_to_do = *buffer++;
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
325
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
326 // Do all bytes in the 32-bit word.
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
327
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
328 for (i = 0; i < sizeof(word_to_do); i++)
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
329 {
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
330 // We calculate a *byte* at a time. If the CRC is MSB first we
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
331 // do the next MS byte and vica-versa.
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
332
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
333 if (crc_model.cm_refin == FALSE)
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
334 {
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
335 // MSB first. Do the next MS byte.
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
336
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
337 byte_to_do = (uint8_t) ((word_to_do & 0xFF000000) >> 24);
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
338 word_to_do <<= 8;
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
339 }
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
340 else
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
341 {
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
342 // LSB first. Do the next LS byte.
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
343
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
344 byte_to_do = (uint8_t) (word_to_do & 0x000000FF);
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
345 word_to_do >>= 8;
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
346 }
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
347
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
348 cm_nxt(&crc_model, byte_to_do);
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
349 }
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
350 }
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
351
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
352 // Return the final result.
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
353
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
354 return (cm_crc(&crc_model));
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
355 }
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
356
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
357
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
358 unsigned checksum(void *buffer, size_t len, unsigned int seed)
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
359 {
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
360 unsigned char *buf = (unsigned char *)buffer;
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
361 size_t i;
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
362
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
363 for (i = 0; i < len; ++i)
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
364 seed += (unsigned int)(*buf++);
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
365 return seed;
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
366 }
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
367
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
368 unsigned int crc32c_checksum(unsigned char* message, int length) {
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
369 int i, j;
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
370 unsigned int byte, crc, mask;
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
371 static unsigned int table[256] = {0};
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
372
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
373 /* Set up the table, if necessary. */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
374
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
375 if (table[1] == 0) {
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
376 for (byte = 0; byte <= 255; byte++) {
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
377 crc = byte;
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
378 for (j = 7; j >= 0; j--) { // Do eight times.
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
379 mask = -(crc & 1);
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
380 crc = (crc >> 1) ^ (0xEDB88320 & mask);
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
381 }
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
382 table[byte] = crc;
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
383 }
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
384 }
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
385
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
386 /* Through with table setup, now calculate the CRC. */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
387 i = 0;
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
388 crc = 0xFFFFFFFF;
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
389 while (length--) {
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
390 byte = message[i];
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
391 crc = (crc >> 8) ^ table[(crc ^ byte) & 0xFF];
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
392 i = i + 1;
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
393 }
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
394 return ~crc;
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
395 }
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
396
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
397
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
398
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
399
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
400
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
401
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
402 /******************************************************************************/
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
403
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
404 /* MAIN */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
405 /* ----------------------- */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
406 /* */
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
407
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
408
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
409 #include <stdio.h>
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
410 #include <string.h>
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
411 int main(int argc, char** argv) {
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
412
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
413
162
ad98da7e74f8 OSTC4pack_V4: do not hard code output filename for fonts
Jan Mulder <jlmulder@xs4all.nl>
parents: 49
diff changeset
414 if (argc != 3) {
ad98da7e74f8 OSTC4pack_V4: do not hard code output filename for fonts
Jan Mulder <jlmulder@xs4all.nl>
parents: 49
diff changeset
415 fprintf(stderr, "Usage: OSTC4pack_V4 <type> <bin file>\n");
ad98da7e74f8 OSTC4pack_V4: do not hard code output filename for fonts
Jan Mulder <jlmulder@xs4all.nl>
parents: 49
diff changeset
416 return(-1);
ad98da7e74f8 OSTC4pack_V4: do not hard code output filename for fonts
Jan Mulder <jlmulder@xs4all.nl>
parents: 49
diff changeset
417 }
ad98da7e74f8 OSTC4pack_V4: do not hard code output filename for fonts
Jan Mulder <jlmulder@xs4all.nl>
parents: 49
diff changeset
418
49
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
419 FILE *fp, * fpout;
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
420 size_t len;
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
421 unsigned char buf[1050000];
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
422 char *file = argv[2];
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
423 int type = atoi(argv[1]);
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
424 unsigned int pruefsumme;
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
425
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
426 //write File with length and cheksum
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
427 char filename[500], filenameout[510] ;
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
428 sprintf(filename,"%s",file);
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
429 int filelength = strlen(filename);
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
430 filename[filelength -4] = 0;
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
431
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
432 if (NULL == (fp = fopen(file, "rb")))
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
433 {
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
434 printf("Unable to open %s for reading\n", file);
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
435 return -1;
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
436 }
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
437 len = fread(buf, sizeof(char), sizeof(buf), fp);
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
438 printf("%d bytes read (hex: %#x )\n", len,len);
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
439 // unsigned int checksum = crc32c_checksum(buf, len);
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
440 unsigned int checksum = CRC_CalcBlockCRC((uint32_t *)buf, (uint32_t)(len/4));
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
441 printf("The checksum of %s is %#x\n", file, checksum);
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
442
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
443 fclose(fp);
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
444 if(type == 0)
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
445 sprintf(filenameout,"%s_upload.bin",filename);
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
446 else
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
447 if(type == 2)
162
ad98da7e74f8 OSTC4pack_V4: do not hard code output filename for fonts
Jan Mulder <jlmulder@xs4all.nl>
parents: 49
diff changeset
448 sprintf(filenameout,"%s_upload.bin",filename);
49
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
449 else
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
450 sprintf(filenameout,"%s_upload.bin",filename);
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
451
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
452 unsigned char buf2[4];
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
453
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
454 buf2[0] = 0xFF & (len >> 24);;
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
455 buf2[1] = 0xFF & (len >> 16);;
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
456 buf2[2] = 0xFF & (len >> 8);
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
457 buf2[3] = 0xFF & len;
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
458 fpout = fopen(filenameout, "wb");
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
459 fwrite(buf2,sizeof(char),4,fp);
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
460
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
461
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
462 unsigned char buf3offset[4];
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
463 unsigned char bufVersion[4];
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
464 if(type == 2)
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
465 {
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
466 buf3offset[0] = 0x10;
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
467 buf3offset[1] = 0x00;
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
468 buf3offset[2] = 0x03;
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
469 buf3offset[3] = 0x20;
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
470 bufVersion[0] = buf[0x00];
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
471 bufVersion[1] = buf[0x01];
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
472 bufVersion[2] = buf[0x02];
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
473 bufVersion[3] = buf[0x03];
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
474 }
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
475 else
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
476 if(type == 0)
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
477 {
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
478 buf3offset[0] = 0xFE;
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
479 buf3offset[1] = 'R';
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
480 buf3offset[2] = 'T';
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
481 buf3offset[3] = 'E';
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
482 bufVersion[0] = buf[0x5000];
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
483 bufVersion[1] = buf[0x5001];
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
484 bufVersion[2] = buf[0x5002];
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
485 bufVersion[3] = buf[0x5003];
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
486 }
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
487 else
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
488 {
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
489 buf3offset[0] = 0xFF;
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
490 buf3offset[1] = 0;
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
491 buf3offset[2] = 'H';
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
492 buf3offset[3] = 'W';
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
493 bufVersion[0] = buf[0x10000];
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
494 bufVersion[1] = buf[0x10001];
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
495 bufVersion[2] = buf[0x10002];
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
496 bufVersion[3] = buf[0x10003];
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
497 }
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
498
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
499 fwrite(buf3offset,sizeof(char),4,fp);
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
500
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
501 pruefsumme = len + (256*256*256*buf3offset[0]) + (256*256*buf3offset[1]) + (256*buf3offset[2]) + buf3offset[3];
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
502 fwrite(&pruefsumme,sizeof(char),4,fp);
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
503
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
504 fwrite(bufVersion,sizeof(char),4,fp);
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
505
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
506 for(int i = 0;i <len;i++)
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
507 {
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
508 if(fwrite(&buf[i],1,1,fpout) != 1)
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
509 printf("error writing\n");
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
510 }
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
511
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
512
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
513 buf2[0] = 0xFF & (checksum >> 24);// & 0xFF000000;
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
514 buf2[1] = 0xFF & (checksum >> 16);
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
515 buf2[2] = 0xFF & (checksum >> 8);
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
516 buf2[3] = 0xFF & checksum;
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
517
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
518 fwrite(buf2,sizeof(char),4,fp);
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
519
1e707b34667e add o4pack files
heinrichsweikamp
parents:
diff changeset
520 }