summaryrefslogtreecommitdiff
path: root/xpp/utils/hexfile.c
diff options
context:
space:
mode:
Diffstat (limited to 'xpp/utils/hexfile.c')
-rw-r--r--xpp/utils/hexfile.c74
1 files changed, 62 insertions, 12 deletions
diff --git a/xpp/utils/hexfile.c b/xpp/utils/hexfile.c
index a4b12c5..5d6eaa7 100644
--- a/xpp/utils/hexfile.c
+++ b/xpp/utils/hexfile.c
@@ -278,6 +278,39 @@ int dump_hexfile2(struct hexdata *hexdata, FILE *outfile, uint8_t maxwidth)
return 0;
}
+void process_comment(struct hexdata *hexdata, char buf[])
+{
+ char *dollar_start;
+ char *dollar_end;
+ const char id_prefix[] = "Id: ";
+ char tmp[BUFSIZ];
+ char *p;
+ int len;
+
+ if(report_func)
+ report_func(LOG_INFO, "Comment: %s\n", buf + 1);
+ /* Search for RCS keywords */
+ if((dollar_start = strchr(buf, '$')) == NULL)
+ return;
+ if((dollar_end = strchr(dollar_start + 1, '$')) == NULL)
+ return;
+ /* Crop the '$' signs */
+ len = dollar_end - dollar_start;
+ len -= 2;
+ memcpy(tmp, dollar_start + 1, len);
+ tmp[len] = '\0';
+ p = tmp;
+ if(strstr(tmp, id_prefix) == NULL)
+ return;
+ p += strlen(id_prefix);
+ if((p = strchr(p, ' ')) == NULL)
+ return;
+ p++;
+ strncpy(hexdata->version_info, p, BUFSIZ - 1);
+ if((p = strchr(hexdata->version_info, ' ')) != NULL)
+ *p = '\0';
+}
+
struct hexdata *parse_hexfile(const char *fname, unsigned int maxlines)
{
FILE *fp;
@@ -314,18 +347,7 @@ struct hexdata *parse_hexfile(const char *fname, unsigned int maxlines)
}
chomp(buf);
if(buf[0] == '#') {
- char *dollar_start;
- char *dollar_end;
-
- if(report_func)
- report_func(LOG_INFO, "Comment: %s\n", buf + 1);
- /* Search for RCS keywords */
- if((dollar_start = strchr(buf, '$')) == NULL)
- continue;
- if((dollar_end = strchr(dollar_start + 1, '$')) == NULL)
- continue;
- *(dollar_end + 1) = '\0';
- strncpy(hexdata->version_info, dollar_start, BUFSIZ - 1);
+ process_comment(hexdata, buf);
continue;
}
if(buf[0] != ':') {
@@ -383,3 +405,31 @@ void dump_binary(struct hexdata *hexdata, const char *outfile)
fclose(fp);
}
+/*
+ * Algorithm lifted of sum(1) implementation from coreutils.
+ * We chose the default algorithm (BSD style).
+ */
+int calc_checksum(struct hexdata *hexdata)
+{
+ unsigned int i;
+ size_t len;
+ int ck = 0;
+
+ for(i = 0; i < hexdata->maxlines; i++) {
+ struct hexline *hexline = hexdata->lines[i];
+ unsigned char *p;
+
+ if(!hexline)
+ break;
+ if(hexline->d.content.header.tt == TT_EOF)
+ continue;
+ len = hexline->d.content.header.ll;
+ p = hexline->d.content.tt_data.data;
+ for(; len; p++, len--) {
+ ck = (ck >> 1) + ((ck & 1) << 15);
+ ck += *p;
+ ck &= 0xffff; /* Keep it within bounds. */
+ }
+ }
+ return ck;
+}