summaryrefslogtreecommitdiffstats
path: root/third_party/jpeg-xl/lib/jpegli/error.cc
blob: 289261672de19358321d213a9c29fa86caf232cd (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
// Copyright (c) the JPEG XL Project Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

#include "lib/jpegli/error.h"

#include <setjmp.h>
#include <stdlib.h>
#include <string.h>

#include <string>

#include "lib/jpegli/common.h"

namespace jpegli {

const char* const kErrorMessageTable[] = {
    "Message codes are not supported, error message is in msg_parm.s string",
};

bool FormatString(char* buffer, const char* format, ...) {
  va_list args;
  va_start(args, format);
  vsnprintf(buffer, JMSG_STR_PARM_MAX, format, args);
  va_end(args);
  return false;
}

void ExitWithAbort(j_common_ptr cinfo) {
  (*cinfo->err->output_message)(cinfo);
  jpegli_destroy(cinfo);
  exit(EXIT_FAILURE);
}

void EmitMessage(j_common_ptr cinfo, int msg_level) {
  if (msg_level < 0) {
    if (cinfo->err->num_warnings <= 5 || cinfo->err->trace_level >= 3) {
      (*cinfo->err->output_message)(cinfo);
    }
    ++cinfo->err->num_warnings;
  } else if (cinfo->err->trace_level >= msg_level) {
    (*cinfo->err->output_message)(cinfo);
  }
}

void OutputMessage(j_common_ptr cinfo) {
  char buffer[JMSG_LENGTH_MAX];
  (*cinfo->err->format_message)(cinfo, buffer);
  fprintf(stderr, "%s\n", buffer);
}

void FormatMessage(j_common_ptr cinfo, char* buffer) {
  jpeg_error_mgr* err = cinfo->err;
  int code = err->msg_code;
  if (code == 0) {
    memcpy(buffer, cinfo->err->msg_parm.s, JMSG_STR_PARM_MAX);
  } else if (err->addon_message_table != nullptr &&
             code >= err->first_addon_message &&
             code <= err->last_addon_message) {
    std::string msg(err->addon_message_table[code - err->first_addon_message]);
    if (msg.find("%s") != std::string::npos) {
      snprintf(buffer, JMSG_LENGTH_MAX, msg.data(), err->msg_parm.s);
    } else {
      snprintf(buffer, JMSG_LENGTH_MAX, msg.data(), err->msg_parm.i[0],
               err->msg_parm.i[1], err->msg_parm.i[2], err->msg_parm.i[3],
               err->msg_parm.i[4], err->msg_parm.i[5], err->msg_parm.i[6],
               err->msg_parm.i[7]);
    }
  } else {
    snprintf(buffer, JMSG_LENGTH_MAX, "%s", kErrorMessageTable[0]);
  }
}

void ResetErrorManager(j_common_ptr cinfo) {
  memset(cinfo->err->msg_parm.s, 0, JMSG_STR_PARM_MAX);
  cinfo->err->msg_code = 0;
  cinfo->err->num_warnings = 0;
}

}  // namespace jpegli

struct jpeg_error_mgr* jpegli_std_error(struct jpeg_error_mgr* err) {
  err->error_exit = jpegli::ExitWithAbort;
  err->emit_message = jpegli::EmitMessage;
  err->output_message = jpegli::OutputMessage;
  err->format_message = jpegli::FormatMessage;
  err->reset_error_mgr = jpegli::ResetErrorManager;
  memset(err->msg_parm.s, 0, JMSG_STR_PARM_MAX);
  err->trace_level = 0;
  err->num_warnings = 0;
  // We don't support message codes and message table, but we define one here
  // in case the application has a custom format_message and tries to access
  // these fields there.
  err->msg_code = 0;
  err->jpeg_message_table = jpegli::kErrorMessageTable;
  err->last_jpeg_message = 0;
  err->addon_message_table = nullptr;
  err->first_addon_message = 0;
  err->last_addon_message = 0;
  return err;
}